#🔒 fcntl and ioctl system calls for joysticks

18 messages · Page 1 of 1 (latest)

dawn vault
#

Hello, would someone help me to understand what this code is doing: https://gist.github.com/rdb/8864666#file-js_linux-py-L94, how does this work?
How do I find a connected device in the '/dev' directory?
If I connect a joystick 🕹️, what is a way that I can query it to learn what buttons, axis, lights, etc. it may have and how to interface with them?

upper galeBOT
#

@dawn vault

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

js_linux.py line 94

# Open the joystick device.```
snow mango
#

This code:

fn = '/dev/input/js0'
print('Opening %s...' % fn)
jsdev = open(fn, 'rb')

assumes the joystick is in fact connected as the device file /dev/input/js0.

This line:

ioctl(jsdev, 0x80006a13 + (0x10000 * len(buf)), buf) # JSIOCGNAME(len)

tries to obtain the joystick device's name, I assume. I assume the ioctl control value is 0x80006a13 plus the length of the buffer (shifted left 16 bits, to land above the 6a13). I assume there's some C macro JSIOCGNAME which does this calculation if you had C to access things.

#

@dawn vault ^^

dawn vault
#

How does a person know this?
How can I query the device file to get details about the connected device?
Are there any particular references that would help me have a more comprehensive understanding of what is going on?

snow mango
#

I have only the shallowest understanding. I've used C, and I've used ioctl calls before but never for a joystick.

#

FYI, from man ioctl on a Linux box here:

NAME
       ioctl - control device

SYNOPSIS
       #include <sys/ioctl.h>

       int ioctl(int fd, unsigned long request, ...);

DESCRIPTION
       The ioctl() system call manipulates the underlying device parameters of
       special files.  In particular, many operating characteristics of  char‐
       acter  special  files  (e.g., terminals) may be controlled with ioctl()
       requests.  The argument fd must be an open file descriptor.

       The second argument is a device-dependent request code.  The third  ar‐
       gument  is an untyped pointer to memory.  It's traditionally char *argp
       (from the days before void * was valid C), and will  be  so  named  for
       this discussion.

       An  ioctl() request has encoded in it whether the argument is an in pa‐
       rameter or out parameter, and the size of the argument argp  in  bytes.
       Macros and defines used in specifying an ioctl() request are located in
       the file <sys/ioctl.h>.  See NOTES.

RETURN VALUE
       Usually, on success zero is returned.  A few ioctl() requests  use  the
       return  value  as an output parameter and return a nonnegative value on
       success.  On error, -1 is returned, and errno is set appropriately.
#

A "special file" is a device file, usually from /dev.

dawn vault
snow mango
#

Ah.

#

Anything in that Linux doc?

dawn vault
#

I am reading . . . however, I have to go meet up with someone, but I will be back in a moment.

snow mango
#

The first pystromo module might be adapted for modern python perhaps

upper galeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.