Payerns (open2x) Sdl Lib + Usb Joysticks & Cradle


joyrider

Active Member
Joined
Mar 29, 2006
Messages
589
Age
43
Website
www.willemssoft.be
Hey,

i had some time to play with the cradle however i got a problem.
i'm using the open2x toolchain under cygwin on windows. I had compiled all the libs myself using the open2x-libs.sh file. I'm using the test SDL 1.2.11 version for the sdl libs.

Now i know my joysticks work using the cradle since the default gp2x menu / user interface recognizes my joysticks and i can move around the menu using the joysticks attached to the cradle.

i'm using a dual usb joypad where i can hook up 1 or 2 psx / ps2 joypads to. (MP-8866 (Dual) convertor)

now i was wondering since i have compiled everything myself if the script lacks some compile / configure parameter for having usb joystick support enabled ?

i looked at the SDL_sysjoystick.c file from linux in the source code and saw something about the convertor i used. so i guess it should be supported but i wonder why it doesn't work.

can anyone help me with this ? tell me if have to recompile the sdl libs with some special parameters or sumthing. or if i'm missing something.

The joysticks show up as /Dev/input/event1

SDL_Numjoysticks() always returns 1 even though there are 3 joytiscks attached (2 on cradle, and one from the gp2x itself)

looked at the sdl source code some more and saw this :

/* Function to scan the system for joysticks */
int SDL_SYS_JoystickInit(void)
{
/* The base path of the joystick devices */
const char *joydev_pattern[] = {
#if SDL_INPUT_LINUXEV
"/dev/input/event%d",
#endif

so SDL_INPUT_LINUXEV should probably be set for my joystick to be recognized now i don't know if this can and may be set using -DSDL_INPUT_LINUXEV at compile time ? would it work then ? cause i really am not in the mood to recompile everything again just to find out it doesn't work ? (at least i think i have to recompile everything again what's based on SDL right io SDL_GFX, SDL_MIXER, ETC?)
 
Guess what i recompiled sdl today with the -DSDL_INPUT_LINUXEV flag and it works ! ONE of my joysticks get recognized now :D i don't know why this flag isn't on by default though would have saved me some troubles cause during the process of figuring this out i managed to clean all the "old libs" using the script from the open2x libs :x

anyway i still can't use both joysticks at the same time though :( sdl seems to only recgonize one of the 2 attached joysticks. anyone got any idea's about that ? the source code of sdl_joystick does seem to handle it (the multiple attached joysticks) at least i think it does don't understand it all that good.

If someone could elaborate on it if its supported or not (2 joysticks connected to my MP-8866 (Dual) convertor to the cradle) by looking at the source code or perhaps payern himself ;)
 
okay looked at it some more and this is what i found out so far.

Compiling with -DSDL_INPUT_LINUXEV makes the mp-8866 dual joy convertor work but currently only one joystick is detected don't know why (yet, still have to debug a bit for that). Also there are some problems with the current code for example, the code will work perfectly fine with a normal PSX joypad attached to it but when you attach a dual shock or PS2 controller to the convertor and you enable analog mode and press any of the dpad buttons you'll get a segmentation fault. will also have to figure out why.

I'm using the default joystick test program that comes with the sdl libraries to test all the joysticks. The segfault always happens. In windows those dpad buttons get recognized as a hat middle when nothing is pressed, left right up and down. (using the same test program).

anyway i hope you read this paeryn and perhaps i can help u test it / debug it if you don't happen to have such convertor yourself.

update:
figured out what goes wrong for detecting the joysticks but am unable atm to fix it.

this is what happens : in CountLogicalJoysticks the joystick device gets openend and using ioctl(fd, JSIOCGBUTTONS, &n) we try to read how many buttons the joystick has. However this always returns -1 so it wasn't succesfull for some reason to find out the nr of joystick buttons. Then we come to point where the name of the joystick gets compared with the name of the built logical map (if (!SDL_strcmp(name, joystick_logicalmap[j].name) && (nbuttons==-1 || nbuttons==joystick_logicalmap[j].nbuttons)) {) but since nbuttons is set to -1 (we couldn't find out the nr of buttons) we only check the name and there are 2 entries with the same name (WiseGroup.,Ltd MP-8866 Dual USB Joypad) for a joystick with (2 psx pads connected) 24 buttons and a joystick with 12 buttons (1 psx pad connected) it takes the first entry it finds being always a single logical joystick. (the 1st entry). So it never detects the 2nd joystick.

i hope you understand what i'm saying paeryn. Or anyone else that cares (just look at sdl's source code @ sdl_sysjoystic).

update 2:
i looked up the errno value after the failure of ioctl and it's 22 being : EINVAL /* Invalid argument */
is this normal ?

edit 3:
it basicly comes down to this ioctl(fd, JSIOCGBUTTONS, &n) always gives Faulty information about the nr of buttons using a mp-8866 convertor or fails so if it can't detect the nr of buttons correctly we can't map the correct logical joystick map to it. So ive settled down with always returning it as 2 logical joysticks even when there's only one attached to the convertor. and that basicly works except it gives false information about the nr of joysticks attached but when running the unmodified sdl joystick code and u don't attach a joystick to the convertor there still gets a joystick detected so it's wrong then 2 ;)

Also is there another way besides ioctl(fd, JSIOCGBUTTONS, &n) to detect the nr of buttons based on a deveice if there is perhaps we could use that to detect the nr of buttons.
 
I don't use 1.2.11 myself but I've just looked through the configure stuff, in SDL_config.h.in SDL_INPUT_LINUXEV is written as SDL_JOYSTICK_LINUXEV so I'll alter that - I'm changing the INPUT to JOYSTICK since it makes more sense. Should be in svn now.

As to the failure of the ioctl, that seems to be a problem - I've just checked and I get the same with my generic pad. I'll see if I can find any way of getting the driver to give the information.

Edit 1:
You edited whilst I was replying ;-)
The code is already there in EV_ConfigJoystick() but that function only gets called at the end of SYS_JoystickOpen() which is too late.

Edit 2:
I just checked with the official sdl svn, they only changed the SDL_INPUT_LINUXEV discrepency 3 weeks ago - but they went with changing JOYSTICK to INPUT so I have updated our svn to keep the same naming. Joystick made more sense to me since it's only refered to in the joystick code.
 
hey paeryn. i think i got it working :

I looked at the orginal source code how they detected the nr of buttons and i applied it to the countlogicaljoysticks function. I tested it again and now it detects the 24 buttons when joysticks are connected and 12 buttons when 1 joystick is connected :D (there is a catch though you have to plug out and plug in the covnertor whenever you attach or deattach a joystick from the convertor, but this is the same behaviour i get in windows, i have to do this there as well)

I also tested this with my usb smart joy convertor and it reports 16 buttons (which is correct because it's mapped diffrent than the mp8866 convertor and in windows the usb smart joy adapter also reports 16 buttons whereas the mp8866 reports 12 for each joystick)


this is what i made of the CountLogicalJoysticks function:
does this seem correct to you ? it does seem to work but you probably would have made it better ;) perhaps you can test it with your convertor ? Also i haven't found out yet why the joystick test program from sdl itself segfaults whenever i enable the analog sticks and press on the dpad buttons. i had it working once during all my testing cause i remember it showing hat up hat down etc just don't know what it was i changed hehe)

CODE

#define test_bit1(nr, addr) \
(((1UL << ((nr) & 31)) & (((const unsigned int *) addr)[(nr) >> 5])) != 0)


static int CountLogicalJoysticks(int max)
{
register int i, j, k, l, ret, prev;
const char* name;
int nbuttons, fd,teller,n;
unsigned long keybit[40];

ret = 0;

for(i = 0; i < max; i++) {
name = SDL_SYS_JoystickName(i);

fd = open(SDL_joylist.fname, O_RDONLY, 0);
if ( fd >= 0 ) {
if(ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) {
nbuttons = -1;
} else {
nbuttons=-1;
n=0;
for ( l=BTN_JOYSTICK; l <= BTN_THUMBR; l++ ) {
if ( test_bit1(l, keybit) ) {
#ifdef DEBUG_INPUT_EVENTS
printf("Joystick has button: 0x%x\n", l);
#endif
n++;
}
}
for ( l=BTN_MISC; l <= BTN_9; l++ ) {
if ( test_bit1(l, keybit) ) {
#ifdef DEBUG_INPUT_EVENTS
printf("Joystick has button: 0x%x\n", l);
#endif
n++;
}
}
if(n > 0)
nbuttons = n;
#ifdef DEBUG_INPUT_EVENTS
printf("buttons found :%d\n",nbuttons);
#endif
}
close(fd);
}


if (name) {
for(j = 0; j < SDL_arraysize(joystick_logicalmap); j++) {
if (!SDL_strcmp(name, joystick_logicalmap[j].name) && (nbuttons==-1 || nbuttons==joystick_logicalmap[j].nbuttons)) {
prev = i;
SDL_joylist[prev].map = &(joystick_logicalmap[j]);

for(k = 1; k < joystick_logicalmap[j].njoys; k++) {
SDL_joylist[prev].next = max + ret;
SDL_joylist[max+ret].prev = prev;

prev = max + ret;
SDL_joylist[prev].logicalno = k;
SDL_joylist[prev].map = &(joystick_logicalmap[j]);
ret++;
}

break;
}
}
}
}

return ret;
}
 
QUOTE
I also tested this with my usb smart joy convertor and it reports 16 buttons (which is correct because it's mapped diffrent than the mp8866 convertor and in windows the usb smart joy adapter also reports 16 buttons whereas the mp8866 reports 12 for each joystick)

Do both of them work properly for the second controller? I can't think of how the smart joy maps to 16 buttons, is that 16 between the two or 16 each?
QUOTE
this is what i made of the CountLogicalJoysticks function:
does this seem correct to you ? it does seem to work but you probably would have made it better ;) perhaps you can test it with your convertor ? Also i haven't found out yet why the joystick test program from sdl itself segfaults whenever i enable the analog sticks and press on the dpad buttons. i had it working once during all my testing cause i remember it showing hat up hat down etc just don't know what it was i changed hehe)

Yep that's the bit to do it, I'll add it in to svn as soon as I get the chance. I'll probably bring it into a separate function and only use that method if the proper one fails.
I'll try the sdl joystick program later but both of my pads are standard PC pads. Maybe sdl gets confused when switching the analogue sticks on/off. Have you tried testing with the analogue switched on before you start the program?
 
paeryn said:
Do both of them work properly for the second controller? I can't think of how the smart joy maps to 16 buttons, is that 16 between the two or 16 each?
Oh no the smart joy convertor is a single convertor you can only attach one psx pad to it. and it maps by default to 16 buttons for one controller i think it does this to prevent problems with analog controls you see when analog isn't enabled only the first 10 buttons get used (L1,L2,R1,R2,start, select, cross,circle,square,triangle). when you enable the analog controls buttons 11 - 16 get used as well being : buttons 10 & 11 for the click buttons on the analog sticks and buttons 12 - 16 are used for the dpad then. I don't know if there exists a smart joy adaptor that has options to connect 2 or more controllers the one i have is for a single psx pad to connect.
what i meant with it works was that it detects 16 buttons correctly just like in windows.

paeryn said:
Yep that's the bit to do it, I'll add it in to svn as soon as I get the chance. I'll probably bring it into a separate function and only use that method if the proper one fails.
I'll try the sdl joystick program later but both of my pads are standard PC pads. Maybe sdl gets confused when switching the analogue sticks on/off. Have you tried testing with the analogue switched on before you start the program?
hmm i think i did but am not sure anymore i will try it out again later today.
- just tested it, it still segfaults even when the analog controls are enabled before you start the program. I'll look into it and let u know if i can find something :
- hmm with the joystick_logicalmap for the mp-8866 doesn't contain any hats which is wrong at least if i look at how windows handles it. it should have 1 hat (the dpad buttons when analog controllers are enabled) i think that causes the segfault. am goana look further into it and add an hat control map and see if it fixes it. Also i'll hook up a generic (non analog / dualshock pad as well to see how that one works:
- well in windows it gets detected the same as the dual shock one, on the gp2x also :
out put from the default SDL_joystick test program :
Watching joystick 1: (WiseGroup.,Ltd MP-8866 Dual USB Joypad)
Joystick has 6 axes, 1 hats, 0 balls, and 12 buttons

btw this is how windows detects the mp8866 controller :
mp8866.jpg

The "gezichtveldverdeling" one is the hat

i can't check in linux since i only have a vmware image of linux and vmware emulates the joysticks as a diffrent one.

*Final Update*:
paeryn i added a hat logicalmap and now it doesn't segfault anymore when i enable analog controllers and press the dpad buttons. it just works like in windows now.
i don't think i have to give you the code for this but i can post it if you like. I just basicly added a logical hat mapping for each of mp8866 controllers for 1,2,3,4 joysticks. changed the logical layout to add 1 hat control per joystick and added the hat control mapping to the joystick logicalmap. And it works without a problem then.

I don't own any other multiple joystick convertors so i can't make those work or test those but if people have some they could do the same basic testing as i did and add their own maps for their own convertors or perhaps based on an image of how windows sees it support for more convertors can be added?

*another update*:
hey paeryn, i just installed ubuntu and tested it out here. the weird thing is sdl detects it here as a joystick with 6 axises and no hat control. whereas on the gp2x (so your version) it detects it as a hat control, the same in windows there's also a hat control detected. Pretty weird that its not the same perhaps your way of detecting the joysticks capabilities is better and correct ?
 
Last edited by a moderator:
Back
Top