49 fd = open(device, O_RDONLY | O_NONBLOCK);
52 sprintf(
err,
"Unable to open device: %s",device);
59 strcpy(
err,
"Unable to read number of axes");
66 strcpy(
err,
"Unable to read number of buttons");
72 strcpy(
name,
"Unknown");
74 printf(
"\33[1m\33[36mGamepad\33[0m %s\n",
name);
84 axes[i].callback=NULL;
94 strcpy(
err,
"Unable to read axes mapping");
101 strcpy(
err,
"Unable to read buttons mapping");
113 strcpy(
err,
"Unable to set axes mapping");
120 strcpy(
err,
"Unable to set buttons mapping");
133 sprintf(
err,
"This device does not have this axis: Axis %d",
id);
144 sprintf(
err,
"This device does not have this Button: Button %d",
id);
158 struct js_event buffer[64];
160 ret = read (
fd, buffer,
sizeof(
struct js_event)*64);
164 for(
int i=0; i<ret/(
signed int)
sizeof(
struct js_event);i++)
166 if(buffer[i].type & JS_EVENT_BUTTON & ~JS_EVENT_INIT)
168 if(debug)printf(
"Button %d Value %d\n",buffer[i].number,buffer[i].value);
169 if(
buttons[buffer[i].number].callback==NULL)
174 }
else if(buffer[i].type == JS_EVENT_AXIS)
176 if(debug)printf(
"Axis %d Value %d\n",buffer[i].number,buffer[i].value);
177 if(
axes[buffer[i].number].callback==NULL)
180 axes[buffer[i].number].
value = buffer[i].value;
193 printf(
"%s (raising SIGINT)\n",
err);
197 printf(
"Warning!! ");
202 printf(
"%s, ",
err);fflush(stdout);
Gamepad communication class declaration generic code.
void plerr(int ret)
Print local error function This function prints the error present in the err variable (this is a priv...
int Dispatch(bool debug=false)
This function checks the gamepad status and calls the respective callback.
~class_gamepad()
Class destructor.
class_gamepad()
Class constructor.
int GetButtonMapping(void)
Get the button mapping from the device.
int SetButtonMapping(void)
Set the button mapping to the device.
int RegisterCallback(e_type type, int id, void(*callback)(int value, void *data), void *data)
Register a callback for a specific button or axis.
int StartComm(const char *device)
Initialize comm with the gamepad.
__u16 m_buttons[KEY_MAX-BTN_MISC+1]