35 #include <linux/joystick.h>
45 #include <sigc++/functors/slot.h>
46 #include <sigc++/functors/ptr_fun.h>
47 #include <sigc++/functors/mem_fun.h>
48 #include <boost/shared_ptr.hpp>
151 fd = open(device, O_RDONLY | O_NONBLOCK);
154 sprintf(
err,
"Unable to open device: %s",device);
161 strcpy(
err,
"Unable to read number of axes");
168 strcpy(
err,
"Unable to read number of buttons");
174 strcpy(
name,
"Unknown");
176 printf(
"\33[1m\33[36mGamepad\33[0m %s\n",
name);
187 axes_.push_back(button);
200 struct js_event buffer[64];
202 ret = read (
fd, buffer,
sizeof(
struct js_event)*64);
206 for(
int i=0; i<ret/(
signed int)
sizeof(
struct js_event);i++)
208 if(buffer[i].type & JS_EVENT_BUTTON & ~JS_EVENT_INIT)
210 if(debug)printf(
"Button %d Value %d\n",buffer[i].number,buffer[i].value);
211 if(
buttons_[buffer[i].number]->callback.empty())
214 buttons_[buffer[i].number]->value = buffer[i].value;
216 }
else if(buffer[i].type == JS_EVENT_AXIS)
218 if(debug)printf(
"Axis %d Value %d\n",buffer[i].number,buffer[i].value);
219 if(
axes_[buffer[i].number]->callback.empty())
222 axes_[buffer[i].number]->value = buffer[i].value;
223 axes_[buffer[i].number]->callback(
axes_[buffer[i].number]->value);
246 printf(
"%s (raising SIGINT)\n",
err);
250 printf(
"Warning!! ");
255 printf(
"%s, ",
err);fflush(stdout);
275 if(i>0 || i<
axes_.size())
~Gamepad()
Class destructor.
__u8 m_axes[ABS_CNT]
Mapping variable.
int n_axes
Number of axes in the game pad.
char name[1024]
Name of the game pad.
int dispatch(bool debug=false)
This function checks the gamepad status and calls the respective callback.
int n_buttons
Number of buttons in the game pad.
boost::shared_ptr< Button > ButtonPtr
int startComm(const char *device)
Initialize comm with the gamepad.
void plerr(int ret)
Print local error function.
std::vector< ButtonPtr > axes_
Vector of axes, buttons and axes share the same data structure Button.
char text[1024]
Auxiliary string variable.
Gamepad()
Class constructor.
__u16 m_buttons[KEY_MAX-BTN_MISC+1]
Mapping variable.
int ret
Auxiliary error variable.
e_type
Type of input available on the game pad.
std::vector< ButtonPtr > buttons_
Vector of buttons.
char err[1024]
Error message.
ButtonPtr buttons(uint i)
int fd
File descriptor of the device.