-
Notifications
You must be signed in to change notification settings - Fork 13
/
ofxGamepadLinux.cpp
63 lines (55 loc) · 1.35 KB
/
ofxGamepadLinux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "ofxGamepadLinux.h"
#include <fcntl.h>
#include <linux/joystick.h>
ofxGamepadLinux::ofxGamepadLinux(string f):ofxGamepad()
{
filename=f;
if ((fd = open(filename.c_str(), O_RDONLY)) < 0) {
std::ostringstream str;
str << filename << ": " << strerror(errno);
throw std::runtime_error(str.str());
} else {
// ok
uint8_t num_axis = 0;
uint8_t num_button = 0;
ioctl(fd, JSIOCGAXES, &num_axis);
ioctl(fd, JSIOCGBUTTONS, &num_button);
setNumAxis(num_axis);
setNumButtons(num_button);
// Get Name
char name_c_str[1024];
if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0) {
std::ostringstream str;
str << filename << ": " << strerror(errno);
throw std::runtime_error(str.str());
} else {
name = name_c_str;
}
fcntl( fd, F_SETFL, O_NONBLOCK );
}
string msg=name;
msg += ": "+ofToString(getNumAxis())+" axis";
msg += ", "+ofToString(getNumButtons())+" buttons";
ofLog(OF_LOG_NOTICE, msg);
}
ofxGamepadLinux::~ofxGamepadLinux()
{
}
void ofxGamepadLinux::update()
{
struct js_event event;
read(fd, &event, sizeof(struct js_event));
/* see what to do with the event */
switch (event.type & ~JS_EVENT_INIT) {
case JS_EVENT_AXIS:
axisChanged(event.number, event.value);
break;
case JS_EVENT_BUTTON:
buttonChanged(event.number, event.value);
break;
}
}
void ofxGamepadLinux::exit()
{
close(fd);
}