# Joysticks under linux

**URL:** <https://discourse.libsdl.org/t/joysticks-under-linux/9896>\
**Category:** SDL Development\
**Created:** [December 25, 2003, 9:59pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896 "2003-12-25T21:59:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miston\_Drirr](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@Miston\_Drirr](https://discourse.libsdl.org/u/Miston_Drirr)\
**Post date:** [December 25, 2003, 9:59pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/1 "2003-12-25T21:59:01Z")

</div>

Hello,

Under linux, when using the ‘js’ devices, I get the number of analog  
inputs reported correctly, however when I use the ‘event’ devices, hats  
are ignored.

Searching through the sources I found they are explicitly ignored  
(SDL\_sysjoystick.c, lines 419 - 423)

Is there any reason for this?

This is true at least on SDL 1.2.6

Thanks!  
Drirr

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [December 25, 2003, 10:38pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/2 "2003-12-25T22:38:41Z")

</div>

> Searching through the sources I found they are explicitly ignored  
> (SDL\_sysjoystick.c, lines 419 - 423)

We don’t handle hats in that loop, since it’s just to configure the  
axes…we handle them in the loop right below it. (line 449, etc).

What joystick and interface (usb? gameport? serial?) and kernel are you  
using? Are you sure the kernel is reporting them as hats and not axes  
when using the event interface? Perhaps the kernel’s not reporting them  
at all in that case?

It may be a bug in SDL, but it might not be in this instance, too.

–ryan.

---

<div class="post-metadata">

**Author:** ![Miston\_Drirr](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@Miston\_Drirr](https://discourse.libsdl.org/u/Miston_Drirr)\
**Post date:** [December 26, 2003, 9:38pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/3 "2003-12-26T21:38:00Z")

</div>

Hi,

I’m using kernel 2.4.22 and an adapted xbox pad connected throgh USB  
with a driver from the xbox-linux project, but I’m on a PC.

I did some more testing. It seems SDL does actually find them, it was  
just I was expecting it to behave the same whether it was though a ‘js’  
or ‘event’ device and I thought hats went sometimes undetected, sorry.

When I load joydev, SDL reports 10 buttons and 14 axes, which is good  
-but not ‘correct’-. However when I use evdev, I get 10 buttons, 6 axes  
and 4 hats reported from SDL. Which is ‘correct’ in the sense that  
that’s what the pad driver reports.

I prefer how SDL behaves when using the ‘js’ driver, because when the  
hats (which physically are analog buttons) are reported as axes I can  
get their abolute position, not just a bitfield of positions.

The reason the driver maps the buttons to hats, I guess, is the lack of  
more meaningful identifiers (in linux/input.h)

Is there a way of having the same behavior through ‘js’ and ‘event’  
devices? Or, perhaps, is there a way to get the analog value of the hats  
from within SDL?

Thanks  
DrirrOn Fri, 2003-12-26 at 06:37, Ryan C. Gordon wrote:

> > Searching through the sources I found they are explicitly ignored  
> > (SDL\_sysjoystick.c, lines 419 - 423)
> 
> We don’t handle hats in that loop, since it’s just to configure the  
> axes…we handle them in the loop right below it. (line 449, etc).
> 
> What joystick and interface (usb? gameport? serial?) and kernel are you  
> using? Are you sure the kernel is reporting them as hats and not axes  
> when using the event interface? Perhaps the kernel’s not reporting them  
> at all in that case?
> 
> It may be a bug in SDL, but it might not be in this instance, too.
> 
> –ryan.

---

<div class="post-metadata">

**Author:** ![icculus](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/icculus/32/81_2.png) [@icculus](https://discourse.libsdl.org/u/icculus)\
**Post date:** [December 27, 2003, 1:38am UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/4 "2003-12-27T01:38:00Z")

</div>

> Is there a way of having the same behavior through ‘js’ and ‘event’  
> devices? Or, perhaps, is there a way to get the analog value of the hats  
> from within SDL?

I don’t think we can fix this at the SDL level if the kernel is handing  
us absolute values. Then again, I don’t know much about the kernel  
interface, so there might be a way to coerce it to do so.

I would ask the linux-xbox crew to add a module option that lets you  
make the hat look like a relative and not absolute axis, though.

–ryan.

---

<div class="post-metadata">

**Author:** ![Miston\_Drirr](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@Miston\_Drirr](https://discourse.libsdl.org/u/Miston_Drirr)\
**Post date:** [December 27, 2003, 9:59am UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/5 "2003-12-27T09:59:01Z")

</div>

> I don’t think we can fix this at the SDL level if the kernel is handing  
> us absolute values. Then again, I don’t know much about the kernel  
> interface, so there might be a way to coerce it to do so.

For the kernel, hats and axes are the same thing, absolute analog  
inputs. Tweaking SDL a bit by making it not treat hats differently while  
using event queues, I can, at least in my case, have a uniform behavior  
with event queues and js devices.

What is the advantage of not handling hats as analog inputs and  
discarding the value? Why not just treat all absolute analog inputs as  
axes?

DrirrOn Sat, 2003-12-27 at 09:34, Ryan C. Gordon wrote:

---

<div class="post-metadata">

**Author:** ![Miston\_Drirr](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@Miston\_Drirr](https://discourse.libsdl.org/u/Miston_Drirr)\
**Post date:** [January 1, 2004, 8:32pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/6 "2004-01-01T20:32:48Z")

</div>

Ok, after not receiving a follow-up to my post I think I’ll just have to  
live with the joystick hat handling as is, right? It’s just sometimes  
I’d like to know the reasons behind design decisions which, IMHO, can be  
improved. Of course, it is very likely I just missed something here.

Drirr.On Sat, 2003-12-27 at 12:01, Miston Drirr wrote:

> On Sat, 2003-12-27 at 09:34, Ryan C. Gordon wrote:
> 
> > I don’t think we can fix this at the SDL level if the kernel is handing  
> > us absolute values. Then again, I don’t know much about the kernel  
> > interface, so there might be a way to coerce it to do so.
> 
> For the kernel, hats and axes are the same thing, absolute analog  
> inputs. Tweaking SDL a bit by making it not treat hats differently while  
> using event queues, I can, at least in my case, have a uniform behavior  
> with event queues and js devices.
> 
> What is the advantage of not handling hats as analog inputs and  
> discarding the value? Why not just treat all absolute analog inputs as  
> axes?

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [January 2, 2004, 6:54pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/7 "2004-01-02T18:54:42Z")

</div>

> Ok, after not receiving a follow-up to my post I think I’ll just have to  
> live with the joystick hat handling as is, right? It’s just sometimes  
> I’d like to know the reasons behind design decisions which, IMHO, can be  
> improved. Of course, it is very likely I just missed something here.

The design decision was made after noticing that several APIs treat hats  
separately, and that games use hats in a different way from true analog  
axes. The bug here is that your joystick isn’t handled consistently.

If you look at the code in SDL\_sysjoystick.c, you’ll notice that the hats  
are ignored in the first pass, and then a second pass is made to detect the  
hats. Try enabling DEBUG\_INPUT\_EVENTS and see what is being printed.

See ya!  
-Sam Lantinga, Software Engineer, Blizzard Entertainment

---

<div class="post-metadata">

**Author:** ![Miston\_Drirr](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@Miston\_Drirr](https://discourse.libsdl.org/u/Miston_Drirr)\
**Post date:** [January 5, 2004, 12:41pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/8 "2004-01-05T12:41:28Z")

</div>

Hi,

I tried defining DEBUG\_INPUT\_EVENTS, and indeed, they get recognised and  
treated like hats.

It’s just there’s no way under SDL to get the absolute position of hats,  
if available.

If I do a patch to allow the user to choose whether to map hats to axes,  
would it be welcome?

For OSes where the analog value is availabe, it would behave like a  
regular analog axis. For the rest it would return -32768, 0 or 32768.

Perhaps through a function like: void SDL\_JoystickRemapHats(int do\_map);  
or a flag to SDL\_Init. Of course, it would default to the present  
behavior if nothing is specified.

DrirrOn Sat, 2004-01-03 at 02:49, Sam Lantinga wrote:

> > Ok, after not receiving a follow-up to my post I think I’ll just have to  
> > live with the joystick hat handling as is, right? It’s just sometimes  
> > I’d like to know the reasons behind design decisions which, IMHO, can be  
> > improved. Of course, it is very likely I just missed something here.
> 
> The design decision was made after noticing that several APIs treat hats  
> separately, and that games use hats in a different way from true analog  
> axes. The bug here is that your joystick isn’t handled consistently.
> 
> If you look at the code in SDL\_sysjoystick.c, you’ll notice that the hats  
> are ignored in the first pass, and then a second pass is made to detect the  
> hats. Try enabling DEBUG\_INPUT\_EVENTS and see what is being printed.

---

<div class="post-metadata">

**Author:** ![Alan\_Swanson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/alan_swanson/32/222_2.png) [@Alan\_Swanson](https://discourse.libsdl.org/u/Alan_Swanson)\
**Post date:** [January 5, 2004, 1:11pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/9 "2004-01-05T13:11:37Z")

</div>

> It’s just there’s no way under SDL to get the absolute position of hats,  
> if available.

If you are using Linux (and the subject line is a bit of a giveaway)  
with joydev and not evdev then you can just the environment variable;

export SDL\_LINUX\_JOYSTICK=3D"‘JOYSTICK DEVICE NAME’ AXES HATS BALLS"  
export SDL\_LINUX\_JOYSTICK=3D"‘WingMan Extreme Digital 3D’ 5 0 0"

Which would remap the hat on the Wingman to an axis.

> It’s just there’s no way under SDL to get the absolute position of  
> hats,

Not a solution for evdev, but how many joystick hats actually return  
full useful analog hat information? Well, except yours :-)On Mon, 2004-01-05 at 14:39, Miston Drirr wrote:

–  
Alan.

“One must never be purposelessnessnesslessness.”  
-------------- next part --------------  
A non-text attachment was scrubbed…  
Name: not available  
Type: application/pgp-signature  
Size: 189 bytes  
Desc: This is a digitally signed message part  
URL: [http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040105/e5518f8d/attachment.pgp](http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040105/e5518f8d/attachment.pgp)

---

<div class="post-metadata">

**Author:** ![Miston\_Drirr](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@Miston\_Drirr](https://discourse.libsdl.org/u/Miston_Drirr)\
**Post date:** [January 5, 2004, 4:50pm UTC](https://discourse.libsdl.org/t/joysticks-under-linux/9896/10 "2004-01-05T16:50:53Z")

</div>

Hi,

Well, I really have no idea if any other joystick out there returns any  
useful analog hat info. But certainly under linux the kernel can handle  
it and at least in this particualr case, the joystick and the driver  
too, it’s only SDL which doesn’t. The kernel actually has many other  
analog inputs besides hats and axes (rudder, throttle, wheel, gas,  
brake, pressure, etc…) I don’t know whether any driver actually use  
them though, but SDL would treat them already as axes.

What I’m proposing is optionally handling hats as axes, and not  
discarding the analog value. I’m pretty sure this can be done without  
breaking backward compatibility.

DrirrOn Mon, 2004-01-05 at 21:10, Alan Swanson wrote:

> On Mon, 2004-01-05 at 14:39, Miston Drirr wrote:
> 
> > It’s just there’s no way under SDL to get the absolute position of hats,  
> > if available.
> 
> If you are using Linux (and the subject line is a bit of a giveaway)  
> with joydev and not evdev then you can just the environment variable;
> 
> export SDL\_LINUX\_JOYSTICK=3D"‘JOYSTICK DEVICE NAME’ AXES HATS BALLS"  
> export SDL\_LINUX\_JOYSTICK=3D"‘WingMan Extreme Digital 3D’ 5 0 0"
> 
> Which would remap the hat on the Wingman to an axis.
> 
> > It’s just there’s no way under SDL to get the absolute position of  
> > hats,
> 
> Not a solution for evdev, but how many joystick hats actually return  
> full useful analog hat information? Well, except yours 🙂
