Repository navigation
Expand file tree
/
Copy pathJoyInterface.cpp
More file actions
203 lines (139 loc) · 5.83 KB
/
Copy pathJoyInterface.cpp
File metadata and controls
203 lines (139 loc) · 5.83 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2011-2012 Paulo Augusto Peccin. See licence.txt distributed with this file.
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "org_joy_JoyInterface.h"
#include "linux/joystick.h"
#define AXIS_MIN -32767
#define AXIS_MAX 32767
#define DEVICES_MAX 16
#define EVENT_READ_MAX 64
#define ACCEPT_MIN_AXES 0 // Maybe only POV as buttons?
#define ACCEPT_MAX_AXES 8
#define ACCEPT_MIN_BUTTONS 0 // Maybe only analog triggers?
#define ACCEPT_MAX_BUTTONS 24
typedef struct {
char buttons = -1;
char axes = -1;
char name[64];
int fd = -1;
} Info;
typedef struct {
int buttons = 0;
int axes[6];
} State;
Info infos[DEVICES_MAX];
State states[DEVICES_MAX];
int connectDeviceAndUpdateInfo(int id) {
if (id < 0 || id > DEVICES_MAX) return false;
Info *info = &infos[id];
if (info->fd >= 0) return true; // Already connected
char dev[32];
sprintf(dev, "/dev/input/js%d", id);
int fd = open(dev, O_RDONLY);
if (fd < 0) return false; // cannot open device, unacceptable
char axes = -1;
int io = ioctl(fd, JSIOCGAXES, &axes);
if (io < 0) return false; // cannot get number of axes, unacceptable
char buttons = -1;
io = ioctl(fd, JSIOCGBUTTONS, &buttons);
if (io < 0) return false; // cannot get number of buttons, unacceptable
char name[sizeof(info->name)];
io = ioctl(fd, JSIOCGNAME(sizeof(name)), name);
if (io < 0) sprintf(name, "Unknown"); // cannot get name, ok...
else name[sizeof(name) - 1] = 0; // terminate name string just to be sure
// Check for acceptabe vaues
if (axes < ACCEPT_MIN_AXES || axes > ACCEPT_MAX_AXES) return false;
if (buttons < ACCEPT_MIN_BUTTONS || buttons > ACCEPT_MAX_BUTTONS) return false;
// Everything ok, lets store the info permanently
info->axes = axes;
info->buttons = buttons;
sprintf(info->name, "%s", name);
info->fd = fd;
return true;
}
int updateState(int id) {
if (id < 0 || id > DEVICES_MAX) return false;
if (infos[id].fd < 0) return false; // Not Connected
State *state = &states[id];
struct js_event event;
int fd = infos[id].fd;
int sel;
fd_set rfds;
struct timeval timeout;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
int c = 0;
for(; c < EVENT_READ_MAX; c++) {
timeout.tv_sec = timeout.tv_usec = 0; // will return immediately
sel = select(fd + 1, &rfds, NULL, NULL, &timeout);
if (sel != 1) return errno >= 0;
TEMP_FAILURE_RETRY(read (fd, &event, sizeof(struct js_event)));
if (errno < 0) return false;
if ((event.type & JS_EVENT_BUTTON) != 0) {
if (event.value == 1) state->buttons |= (1 << event.number);
else state->buttons &= ~(1 << event.number);
}
else if ((event.type & JS_EVENT_AXIS) != 0)
state->axes[event.number] = event.value;
}
return true;
}
JNIEXPORT jint JNICALL Java_org_joy_JoyInterface_getMaxDevices (JNIEnv *, jclass) {
return (jint) DEVICES_MAX;
}
JNIEXPORT jboolean JNICALL Java_org_joy_JoyInterface_isConnected (JNIEnv *, jclass, jint id) {
if (!connectDeviceAndUpdateInfo(id)) return JNI_FALSE;
if (!updateState(id)) return JNI_FALSE;
return JNI_TRUE;
}
JNIEXPORT jboolean JNICALL Java_org_joy_JoyInterface_updateInfo (JNIEnv *jEnv, jclass, jint id, jobject jInfo) {
if (!connectDeviceAndUpdateInfo(id)) return JNI_FALSE;
Info *info = &infos[id];
jclass jInfoClass = jEnv->GetObjectClass(jInfo);
jfieldID fIDname = jEnv->GetFieldID(jInfoClass, "name", "Ljava/lang/String;");
jstring jName = jEnv->NewStringUTF(info->name);
jEnv->SetObjectField(jInfo, fIDname, jName);
jfieldID fIDpov = jEnv->GetFieldID(jInfoClass, "pov", "Z");
jEnv->SetBooleanField(jInfo, fIDpov, 0); // no POV concept in Linux driver
jfieldID fIDbuttons = jEnv->GetFieldID(jInfoClass, "buttons", "I");
jEnv->SetIntField(jInfo, fIDbuttons, info->buttons);
jfieldID fIDaxes = jEnv->GetFieldID(jInfoClass, "axes", "I");
jEnv->SetIntField(jInfo, fIDaxes, info->axes);
jboolean isCopy;
jfieldID fIDaxesMaxValues = jEnv->GetFieldID(jInfoClass, "axesMaxValues", "[I");
jintArray maxsArray = (jintArray) jEnv->GetObjectField(jInfo, fIDaxesMaxValues);
jint* maxsValues = jEnv->GetIntArrayElements(maxsArray, &isCopy);
maxsValues[0] = maxsValues[1] = maxsValues[2] = maxsValues[3] = maxsValues[4] = maxsValues[5] = AXIS_MAX;
jEnv->ReleaseIntArrayElements(maxsArray, maxsValues, isCopy ? 0 : JNI_ABORT);
jfieldID fIDaxesMinValues = jEnv->GetFieldID(jInfoClass, "axesMinValues", "[I");
jintArray minsArray = (jintArray) jEnv->GetObjectField(jInfo, fIDaxesMinValues);
jint* minsValues = jEnv->GetIntArrayElements(minsArray, &isCopy);
minsValues[0] = minsValues[1] = minsValues[2] = minsValues[3] = minsValues[4] = minsValues[5] = AXIS_MIN;
jEnv->ReleaseIntArrayElements(minsArray, minsValues, isCopy ? 0 : JNI_ABORT);
return JNI_TRUE;
}
// variables to store java classes and field ids that should never change for the same VM
jclass jStateClass;
jfieldID fIDdata;
int idsStored = false;
JNIEXPORT jboolean JNICALL Java_org_joy_JoyInterface_updateState (JNIEnv *jEnv, jclass, jint id, jobject jState) {
if (!updateState(id)) return JNI_FALSE; // not connected or error updating state
// get classes and fields ids only if necessary
if (!idsStored) {
jStateClass = jEnv->GetObjectClass(jState);
fIDdata = jEnv->GetFieldID(jStateClass, "data", "[I");
idsStored = true;
}
jboolean isCopy;
jintArray dataArray = (jintArray) jEnv->GetObjectField(jState, fIDdata);
jint* dataValues = jEnv->GetIntArrayElements(dataArray, &isCopy);
State *state = &states[id];
dataValues[0] = 0; // no POV concept in Linux driver
dataValues[1] = state->buttons;
dataValues[2] = state->axes[0]; dataValues[3] = state->axes[1]; dataValues[4] = state->axes[2];
dataValues[5] = state->axes[3]; dataValues[6] = state->axes[4]; dataValues[7] = state->axes[5];
jEnv->ReleaseIntArrayElements(dataArray, dataValues, isCopy ? 0 : JNI_ABORT);
return JNI_TRUE;
}