Skip to content

Instantly share code, notes, and snippets.

@TechplexEngineer
Created September 23, 2024 13:14
Show Gist options
  • Save TechplexEngineer/8c2386f742bda19149d185126b5af2c0 to your computer and use it in GitHub Desktop.
Save TechplexEngineer/8c2386f742bda19149d185126b5af2c0 to your computer and use it in GitHub Desktop.
Test to get Joystick Ioctl
#include <stdio.h>
#include <linux/joystick.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/input.h>
#define KEY_MAX_LARGE 0x2FF
#define KEY_MAX_SMALL 0x1FF
#define JSIOCSBTNMAP_LARGE _IOW('j', 0x33, __u16[KEY_MAX_LARGE - BTN_MISC + 1])
// #define JSIOCSBTNMAP_SMALL _IOW('j', 0x33, __u16[KEY_MAX_SMALL - BTN_MISC + 1])
#define JSIOCGBTNMAP_LARGE _IOR('j', 0x34, __u16[KEY_MAX_LARGE - BTN_MISC + 1])
// #define JSIOCGBTNMAP_SMALL _IOR('j', 0x34, __u16[KEY_MAX_SMALL - BTN_MISC + 1])
int main() {
// Printing the values of various joystick ioctls in hexadecimal
// printf("JSIOCGVERSION: 0x%x\n", JSIOCGVERSION);
printf("const JSIOCGAXES = 0x%x;\n", JSIOCGAXES);
printf("const JSIOCGBUTTONS = 0x%x;\n", JSIOCGBUTTONS);
printf("const JSIOCGNAME256 = 0x%x;\n", JSIOCGNAME(256)); // Typically, this macro needs a buffer size.
printf("const JSIOCSCORR = 0x%x;\n", JSIOCSCORR);
printf("const JSIOCGCORR = 0x%x;\n", JSIOCGCORR);
printf("const JSIOCSAXMAP = 0x%x;\n", JSIOCSAXMAP);
printf("const JSIOCGAXMAP = 0x%x;\n", JSIOCGAXMAP);
printf("const JSIOCSBTNMAP = 0x%x;\n", JSIOCSBTNMAP);
printf("const JSIOCGBTNMAP_LARGE = 0x%x;\n", JSIOCGBTNMAP_LARGE);
printf("const ABS_MAX = %d;\n", ABS_MAX);
printf("const BTNMAP_SIZE = %d;\n", 0x2FF - BTN_MISC + 1);
// BTN_MISC
printf("const BTN_MISC = %d;\n", BTN_MISC);
int fd = open("/dev/input/js0", O_RDONLY);
int version = 0x000800;
ioctl(fd, JSIOCGVERSION, &version);
// printf("Joystick driver version: %d\n", version);
printf("Driver version is %d.%d.%d.\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of unsigned char: %zu bytes\n", sizeof(unsigned char));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment