Page 1 of 1

ps2sdk pad left_p/right_p

Posted: Tue Nov 23, 2004 9:00 am
by radad
It appears that left_p and right_p in padButtonStatus need to be swapped. (Incidentally rigth_p is spelt incorrectly).

Posted: Tue Nov 23, 2004 10:46 pm
by radad
Also square_p and cross_p need to be swapped. And while we are at it I thought that the 'unsigned char btns[2]' would be better as 'unsigned short btns'.

I would like to propose the following patch to libpad.h:

Code: Select all

20,35c20,35
< #define PAD_LEFT      0x8000
< #define PAD_DOWN      0x4000
< #define PAD_RIGHT     0x2000
< #define PAD_UP        0x1000
< #define PAD_START     0x0800
< #define PAD_R3        0x0400
< #define PAD_L3        0x0200
< #define PAD_SELECT    0x0100
< #define PAD_SQUARE    0x0080
< #define PAD_CROSS     0x0040
< #define PAD_CIRCLE    0x0020
< #define PAD_TRIANGLE  0x0010
< #define PAD_R1        0x0008
< #define PAD_L1        0x0004
< #define PAD_R2        0x0002
< #define PAD_L2        0x0001
---
> #define PAD_LEFT      0x0080
> #define PAD_DOWN      0x0040
> #define PAD_RIGHT     0x0020
> #define PAD_UP        0x0010
> #define PAD_START     0x0008
> #define PAD_R3        0x0004
> #define PAD_L3        0x0002
> #define PAD_SELECT    0x0001
> #define PAD_SQUARE    0x8000
> #define PAD_CROSS     0x4000
> #define PAD_CIRCLE    0x2000
> #define PAD_TRIANGLE  0x1000
> #define PAD_R1        0x0800
> #define PAD_L1        0x0400
> #define PAD_R2        0x0200
> #define PAD_L2        0x0100
98c98
<     unsigned char btns&#91;2&#93;;
---
>     unsigned short btns;
104a105
>     unsigned char right_p;
106d106
<     unsigned char rigth_p;
111d110
<     unsigned char square_p;
112a112
>     unsigned char square_p;
I tested this functionality with the following code:

Code: Select all

#include <tamtypes.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <libpad.h>
#include <fileio.h>
#include <malloc.h>

#include "gsDefs.h"
#include "gsDriver.h"
#include "gsPipe.h"
#include "gsFont.h"

#define PAD_TYPE 0

static char padBuf&#91;256&#93; __attribute__&#40;&#40;aligned&#40;64&#41;&#41;&#41;;

void* LoadMemalign&#40;const char* filename, int align&#41;
&#123;
    int filehandle = fioOpen&#40;filename, O_RDONLY&#41;;
    int filesize = fioLseek&#40;filehandle, 0, SEEK_END&#41;;
    void* mem = memalign&#40;align, filesize&#41;;
    fioLseek&#40;filehandle, 0, SEEK_SET&#41;;
    if &#40;fioRead&#40;filehandle, mem, filesize&#41; <= 0&#41;
    &#123;
        free&#40;mem&#41;;
        mem = 0;
    &#125;
    fioClose&#40;filehandle&#41;;
    return mem;
&#125;

void LoadModules&#40;&#41;
&#123;
    const char* modules&#91;&#93; = &#123;
#if PAD_TYPE != 1
        "rom0&#58;SIO2MAN",
        "rom0&#58;PADMAN",
#else
        "rom0&#58;XSIO2MAN",
        "rom0&#58;XPADMAN",
#endif
        0 &#125;;
    const char** thismodule = modules;
    
    while &#40;*thismodule&#41;
    &#123;
        //printf&#40;"Loading Module&#58; %s\n", *thismodule&#41;;
        int ret = SifLoadModule&#40;*thismodule, 0, NULL&#41;;
        if &#40;ret < 0&#41;
        &#123;
            printf&#40;"Error Loading Module&#58; %s\n", *thismodule&#41;;
        &#125;
        ++thismodule;
    &#125;;
&#125;

void waitPadReady&#40;int port, int slot&#41;
&#123;
    // todo check for PAD_STATE_DISCONN
    int state = 0;
    do
    &#123;
        state=padGetState&#40;port, slot&#41;;
    &#125;
    while&#40;&#40;state != PAD_STATE_STABLE&#41; && &#40;state != PAD_STATE_FINDCTP1&#41;&#41;;
&#125;

void DrawButton&#40;gsFont& myFont, int y, const char* name, int up&#41;
&#123;
    myFont.Print&#40;10, 110, y, 2, GS_SET_RGBA&#40;0xFF,0xFF,0xFF,0xFF&#41;, GSFONT_ALIGN_LEFT, name&#41;;
    if &#40;up&#41;
        myFont.Print&#40;100, 200, y, 2, GS_SET_RGBA&#40;0x00,0xFF,0x00,0xFF&#41;, GSFONT_ALIGN_LEFT, "UP"&#41;;
    else
        myFont.Print&#40;100, 200, y, 2, GS_SET_RGBA&#40;0xFF,0x00,0x00,0xFF&#41;, GSFONT_ALIGN_LEFT, "DOWN"&#41;;
&#125;

void DrawButton&#40;gsFont& myFont, int y, const char* name, int down, int pressure&#41;
&#123;
    DrawButton&#40;myFont, y, name, down&#41;;
    char buffer&#91;100&#93;;
    sprintf&#40;buffer, "%d", pressure&#41;;
    myFont.Print&#40;200, 300, y, 2, GS_SET_RGBA&#40;0xFF,0xFF,0xFF,0xFF&#41;, GSFONT_ALIGN_LEFT, buffer&#41;;
&#125;

int main&#40;&#41;
&#123;
    gsDriver myGsDriver;
    myGsDriver.setDisplayMode&#40;640, 480, 180, 70, 
        GS_PSMCT32, 2, GS_TV_AUTO, GS_TV_INTERLACE,
        GS_ENABLE, GS_PSMZ32&#41;;    
    myGsDriver.drawPipe.setAlphaEnable&#40;GS_ENABLE&#41;;
    
    gsFont myFont;
    myFont.assignPipe&#40;&myGsDriver.drawPipe&#41;;
    gsFontTex* fontTex = &#40;gsFontTex*&#41;LoadMemalign&#40;"mc0&#58;/FONTS/arial.fnt", 64&#41;;
    if &#40;fontTex&#41;
    &#123;
        // Upload into the beginning of texture mem &#40;with texture-buffer width set to 256&#41;
        myFont.uploadFont&#40;fontTex, myGsDriver.getTextureBufferBase&#40;&#41;, 
            fontTex->TexWidth, // Use the fontTex width as texbuffer width &#40;can use diff width&#41;
            0, 0 &#41;;
    &#125;
        
    SifInitRpc&#40;0&#41;;
    LoadModules&#40;&#41;;

    int ret = 0;
    
    ret = padInit&#40;0&#41;;
    if&#40;ret < 0&#41;
        printf&#40;"Failed to initialise pad server&#58; %d\n\n", -ret&#41;;
    
    int port = 0; // 0 -> Connector 1, 1 -> Connector 2
    int slot = 0; // Always zero if not using multitap
    
    //char padBuf&#91;256&#93; __attribute__&#40;&#40;aligned&#40;64&#41;&#41;&#41;;
    ret = padPortOpen&#40;port, slot, padBuf&#41;;
    if&#40;ret < 0&#41;
        printf&#40;"padOpenPort failed&#58; %d\n\n", -ret&#41;;
    
    waitPadReady&#40;port, slot&#41;;
    // When using MMODE_LOCK, user cant change mode with Select button
    padSetMainMode&#40;port, slot, PAD_MMODE_DUALSHOCK, PAD_MMODE_LOCK&#41;;

    waitPadReady&#40;port, slot&#41;;        
    padEnterPressMode&#40;port, slot&#41;;
    
    while &#40;true&#41;
    &#123;
        waitPadReady&#40;port, slot&#41;;

        padButtonStatus buttons;
        ret = padRead&#40;port, slot, &buttons&#41;; // port, slot, buttons

        if &#40;ret != 0&#41;
        &#123;
            // Clear the screen &#40;with ZBuffer Disabled&#41;
            myGsDriver.drawPipe.setZTestEnable&#40;GS_DISABLE&#41;;
            myGsDriver.drawPipe.RectFlat&#40;0,0,639,479,0,GS_SET_RGBA&#40;0x00,0x00,0x00,0x80&#41;&#41;;
            myGsDriver.drawPipe.setZTestEnable&#40;GS_ENABLE&#41;;

            DrawButton&#40;myFont,  10, "LEFT",  buttons.btns & PAD_LEFT,  buttons.left_p&#41;;
            DrawButton&#40;myFont,  30, "RIGHT", buttons.btns & PAD_RIGHT, buttons.right_p&#41;;
            DrawButton&#40;myFont,  50, "UP",    buttons.btns & PAD_UP,    buttons.up_p&#41;;
            DrawButton&#40;myFont,  70, "DOWN",  buttons.btns & PAD_DOWN,  buttons.down_p&#41;;

            DrawButton&#40;myFont, 110, "SQUARE",   buttons.btns & PAD_SQUARE,   buttons.square_p&#41;;
            DrawButton&#40;myFont, 130, "CROSS",    buttons.btns & PAD_CROSS,    buttons.cross_p&#41;;
            DrawButton&#40;myFont, 150, "CIRCLE",   buttons.btns & PAD_CIRCLE,   buttons.circle_p&#41;;
            DrawButton&#40;myFont, 170, "TRIANGLE", buttons.btns & PAD_TRIANGLE, buttons.triangle_p&#41;;

            DrawButton&#40;myFont, 210, "L1",       buttons.btns & PAD_L1,       buttons.l1_p&#41;;
            DrawButton&#40;myFont, 230, "L2",       buttons.btns & PAD_L2,       buttons.l2_p&#41;;
            DrawButton&#40;myFont, 250, "L3",       buttons.btns & PAD_L3&#41;;
            DrawButton&#40;myFont, 270, "R1",       buttons.btns & PAD_R1,       buttons.r1_p&#41;;
            DrawButton&#40;myFont, 290, "R2",       buttons.btns & PAD_R2,       buttons.r2_p&#41;;
            DrawButton&#40;myFont, 310, "R3",       buttons.btns & PAD_R3&#41;;
            
            DrawButton&#40;myFont, 350, "SELECT",   buttons.btns & PAD_SELECT&#41;;
            DrawButton&#40;myFont, 370, "START",    buttons.btns & PAD_START&#41;;

            &#123;
                char buffer&#91;100&#93;;
                sprintf&#40;buffer, "Right Joy %d %d", buttons.rjoy_h, buttons.rjoy_v&#41;;
                myFont.Print&#40;10, 210, 410, 2, GS_SET_RGBA&#40;0xFF,0xFF,0xFF,0xFF&#41;, GSFONT_ALIGN_LEFT, buffer&#41;;
                sprintf&#40;buffer, "Left  Joy %d %d", buttons.ljoy_h, buttons.ljoy_v&#41;;
                myFont.Print&#40;10, 210, 430, 2, GS_SET_RGBA&#40;0xFF,0xFF,0xFF,0xFF&#41;, GSFONT_ALIGN_LEFT, buffer&#41;;
            &#125;
            
            myGsDriver.drawPipe.RectFlat&#40;
                    320 - 128 - 10 + buttons.rjoy_h, 240 - 128 - 10 + buttons.rjoy_v,
                    320 - 128 + 10 + buttons.rjoy_h, 240 - 128 + 10 + buttons.rjoy_v,
                    3, GS_SET_RGBA&#40;0xFF,0x00,0x00,0x80&#41;&#41;;
            myGsDriver.drawPipe.RectFlat&#40;
                    320 - 128 - 10 + buttons.ljoy_h, 240 - 128 - 10 + buttons.ljoy_v,
                    320 - 128 + 10 + buttons.ljoy_h, 240 - 128 + 10 + buttons.ljoy_v,
                    3, GS_SET_RGBA&#40;0x00,0xFF,0x00,0x80&#41;&#41;;
            myGsDriver.drawPipe.RectFlat&#40;
                    320 - 10 + &#40;buttons.right_p - buttons.left_p&#41;/2, 240 - 10 + &#40;buttons.down_p - buttons.up_p&#41;/2,
                    320 + 10 + &#40;buttons.right_p - buttons.left_p&#41;/2, 240 + 10 + &#40;buttons.down_p - buttons.up_p&#41;/2,
                    3, GS_SET_RGBA&#40;0x00,0x00,0xFF,0x80&#41;&#41;;
            
            myGsDriver.drawPipe.Flush&#40;&#41;;
            myGsDriver.WaitForVSync&#40;&#41;;
            myGsDriver.swapBuffers&#40;&#41;;
        &#125; 
    &#125;
    
    return 0;
&#125;

Can others please test it to make the changes are correct?

Posted: Wed Nov 24, 2004 10:20 am
by pixel
Well, your patch is not a unified diff (-u option of the diff command line; if you're using cvs, that would then be "cvs diff -u libpad.h"), so I don't know if it can be applied safely using the patch software. Can you provide a unified version of your patch ? Thanks.

Posted: Wed Nov 24, 2004 11:18 am
by radad
sorry, here is cvs unified diff:

Code: Select all

Index&#58; ee/rpc/pad/include/libpad.h
===================================================================
RCS file&#58; /home/ps2cvs/ps2sdk/ee/rpc/pad/include/libpad.h,v
retrieving revision 1.2
diff -u -r1.2 libpad.h
--- ee/rpc/pad/include/libpad.h	14 Sep 2004 14&#58;41&#58;27 -0000	1.2
+++ ee/rpc/pad/include/libpad.h	24 Nov 2004 01&#58;16&#58;40 -0000
@@ -20,22 +20,22 @@
 /*
  * Button bits
  */
-#define PAD_LEFT      0x8000
-#define PAD_DOWN      0x4000
-#define PAD_RIGHT     0x2000
-#define PAD_UP        0x1000
-#define PAD_START     0x0800
-#define PAD_R3        0x0400
-#define PAD_L3        0x0200
-#define PAD_SELECT    0x0100
-#define PAD_SQUARE    0x0080
-#define PAD_CROSS     0x0040
-#define PAD_CIRCLE    0x0020
-#define PAD_TRIANGLE  0x0010
-#define PAD_R1        0x0008
-#define PAD_L1        0x0004
-#define PAD_R2        0x0002
-#define PAD_L2        0x0001
+#define PAD_LEFT      0x0080
+#define PAD_DOWN      0x0040
+#define PAD_RIGHT     0x0020
+#define PAD_UP        0x0010
+#define PAD_START     0x0008
+#define PAD_R3        0x0004
+#define PAD_L3        0x0002
+#define PAD_SELECT    0x0001
+#define PAD_SQUARE    0x8000
+#define PAD_CROSS     0x4000
+#define PAD_CIRCLE    0x2000
+#define PAD_TRIANGLE  0x1000
+#define PAD_R1        0x0800
+#define PAD_L1        0x0400
+#define PAD_R2        0x0200
+#define PAD_L2        0x0100
 
 /*
  * Pad states
@@ -98,21 +98,21 @@
 &#123;
     unsigned char ok;
     unsigned char mode;
-    unsigned char btns&#91;2&#93;;
+    unsigned short btns;
     // joysticks
     unsigned char rjoy_h;
     unsigned char rjoy_v;
     unsigned char ljoy_h;
     unsigned char ljoy_v;
     // pressure mode
+    unsigned char right_p;
     unsigned char left_p;
-    unsigned char rigth_p;
     unsigned char up_p;
     unsigned char down_p;
     unsigned char triangle_p;
     unsigned char circle_p;
-    unsigned char square_p;
     unsigned char cross_p;
+    unsigned char square_p;
     unsigned char l1_p;
     unsigned char r1_p;
     unsigned char l2_p;

Posted: Mon Dec 13, 2004 12:29 pm
by radad
If everyone agrees with the changes I would like to get it checked in. Could someone please do it for me? Or should I get cvs write access myself?

Posted: Mon Dec 13, 2004 4:46 pm
by pixel
Huh, sorry, I completely forgot this thread.

Humm, now that I look at your patch, I think there's something wrong... Why exactly changing from unsigned char btns[2] to unsigned short btns ? That would only break backward compatibility with other ps2sdk softwares which are using pad... I can agree that the actual design maybe look a bit dodgy, but you would only get people angry to change their code afterward.

Otherwise, I can agree with the left_p, right/rigth_p and square_p, cross_p fixing...

Posted: Mon Dec 13, 2004 4:59 pm
by ooPo
On that topic, when would be a good time to fix the design?

Posted: Tue Dec 14, 2004 9:40 am
by radad
I agree that it would break backwards compatibility. The problem is that the hash defines (such as PAD_LEFT) only make sense when you use this line as taken from the pad example:

Code: Select all

paddata = 0xffff ^ &#40;&#40;buttons.btns&#91;0&#93; << 8&#41; | buttons.btns&#91;1&#93;&#41;;
This line reverses the order of the bytes, that is why I reversed the order of the hash defines. The btns array are really one value as shown by the defines which is why I changed it to a short.

I believe this is better. So we either change it now and get a few people angry or we leave it as it is and explain to every newcomer why it was done this way.

I did think about using a union so it would still be backwards compatible but the defines had to be reversed so it is not really possible.

Posted: Tue Dec 14, 2004 4:48 pm
by pixel
I fully agree with you about the fact that the example is swapping it anyway. But I believe that 99% of the ps2sdk applications which are using the pad will break afterward and would need changes in the source (unlike the mispelled rigth_p, which shouldn't be used that much). So I won't change it unless a few people around that forum agree with that change: I don't want to be blamed alone for breaking softwares :P

Posted: Wed Dec 15, 2004 1:56 am
by ooPo
Heh. No matter what you do in cvs someone will complain. :)

Fix the spelling error. If nobody complains (much), then do the bigger change.

Posted: Wed Dec 15, 2004 2:27 am
by blackdroid
99% of nothing is still 0 pixel, cant honestly figure out if there is anything else thatn the pad sample that actually uses the libpad

Posted: Wed Dec 15, 2004 2:53 am
by pixel
ps2menu ? ps2bor ? keylauncher ? neogeo/cdps2 ? all the dummy games examples such as the tetris, the space invaders, etc ? aww :) Is that all ? :P

Posted: Wed Dec 15, 2004 9:28 am
by radad
I agree with your concerns but the fix is simple. Most apps have copied the pad example:

Code: Select all

paddata = 0xffff ^ &#40;&#40;buttons.btns&#91;0&#93; << 8&#41; | buttons.btns&#91;1&#93;&#41;;
The fix is to replace it with this:

Code: Select all

paddata = buttons.btns;
See, they are extracting it into a short so they can use the defines. It is now simpler.

Posted: Wed Dec 15, 2004 9:35 am
by radad
At least fix up the right_p and the square_p. No one should complain about those. I doubt anyone are using them anyway.

As an interim solution have a macro to extract the button data:

Code: Select all

#define PAD_BUTTONS&#40;buttons&#41; &#40;0xffff ^ &#40;&#40;buttons.btns&#91;0&#93; << 8&#41; | buttons.btns&#91;1&#93;&#41;&#41;
Then move all code which uses it to use the macro instead. Then one day in the future replace the array of bytes with the short. As long as everyone is using the macro there should be no problem.

Posted: Wed Dec 15, 2004 12:07 pm
by ooPo
Someone should get this radad guy some cvs write access, too. :)

Posted: Wed Dec 15, 2004 11:33 pm
by blackdroid
pixel wrote:ps2menu ? ps2bor ? keylauncher ? neogeo/cdps2 ? all the dummy games examples such as the tetris, the space invaders, etc ? aww :) Is that all ? :P
they are packaged already, new version requires new work like fixing API changes. we cant get too afraid of changing bad code in ps2sdk aslong as we fix the 'samples' using it.

Posted: Thu Dec 16, 2004 12:00 am
by evilo
yeah, if it can help to have a nice & clean sdk, it worth the pain to make some minor modifications (this one or others) to source code using it. But be sure to document it correctly with the next official release (big warning to avoid bad side-effect)

Posted: Tue Dec 21, 2004 9:18 am
by radad
I get the feeling that most agree with the change so I have updated cvs with the changes. I will take the blame if anyone complains.

Posted: Tue Dec 21, 2004 10:14 pm
by Guest
radad wrote:I get the feeling that most agree with the change so I have updated cvs with the changes. I will take the blame if anyone complains.
All you did was change code, right ? Don't sweat it. Things will be fine.

Its when you touch text headers, text files, etc... non-code things that gets people all worked up. ;)

Hi Mr. B.! :)

Posted: Thu Dec 30, 2004 3:01 am
by BraveDog
radad wrote:I get the feeling that most agree with the change so I have updated cvs with the changes. I will take the blame if anyone complains.
Hehe, I'm not complaining. Just glad I found this thread.
I updated my emulator to work with this.
-BraveDog

Posted: Sun Jan 02, 2005 9:51 pm
by weltall
radad wrote:I agree with your concerns but the fix is simple. Most apps have copied the pad example:

Code: Select all

paddata = 0xffff ^ &#40;&#40;buttons.btns&#91;0&#93; << 8&#41; | buttons.btns&#91;1&#93;&#41;;
The fix is to replace it with this:

Code: Select all

paddata = buttons.btns;
See, they are extracting it into a short so they can use the defines. It is now simpler.
ok tried this substituited all 0xffff ^ ((buttons.btns[0] << 8) | buttons.btns[1]); with paddata = buttons.btns;. but i don't work right. i have a function to the start button and it execute it 2 times! and there is nothing inserted, also it does two left press without touching anithing also sometimes i have to press two times or press longer a button or it don't work!

this is what i use:

Code: Select all

				if &#40;controllerReturn != 0&#41; 
				&#123;
					//paddata = 0xffff ^ &#40;&#40;buttons.btns&#91;0&#93; << 8&#41; | buttons.btns&#91;1&#93;&#41;; sdk 1.1 or before
					paddata = buttons.btns;
					new_pad = paddata & ~old_pad;
					old_pad = paddata;

				&#125;

Posted: Sun Jan 02, 2005 10:59 pm
by evilo
radad,

ok, I downloaded the last ps2sdk (1.2) yesterday, and get some funny recompilation session with my current wip...

the big problem is that the substitution code (following your update to the pad lib) is not working as it miss a little bit mask to work correctly
"paddata = 0xffff ^ buttons.btns;"

else it cause ALL pad buttons to be sawn as pushed together !!

So solution :
1. update pad lib ?
2. update pad sample code (that is not correct) ?

I first thought that it was due to my extraterestrian pad code (that is anyway inherited from the pad sample in the sdk), but since I'm not the only one to report problem with it ...

evilo.

Posted: Tue Jan 04, 2005 9:01 am
by radad
You are correct. Sorry for that. I have fixed it up in cvs now. It would be nice if it could be updated in the 1.2 release also.