-
Notifications
You must be signed in to change notification settings - Fork 14
/
joystick.c
324 lines (263 loc) · 7.04 KB
/
joystick.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
(C) Copyright 2007,2008, Stephen M. Cameron.
This file is part of wordwarvi.
wordwarvi is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
wordwarvi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with wordwarvi; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "compat.h"
#include "joystick.h"
#ifdef __WIN32__
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
/* window handle */
#include <gdk/gdkwin32.h>
/* joystick axes limits */
#define MIN_AXIS -32767
#define MAX_AXIS 32767
/* direct input interface */
LPDIRECTINPUT8 dinput = NULL;
/* joystick interface */
LPDIRECTINPUTDEVICE8 joystick = NULL;
/* device enumeration context */
struct DEVINFO
{
GUID productGuid;
GUID deviceID;
const CHAR *vendor;
};
/* device enumeration callback */
BOOL CALLBACK EnumDevCallback(const DIDEVICEINSTANCE *dev, VOID *ctx)
{
/* take the first joystick we get */
if (GET_DIDEVICE_TYPE(dev->dwDevType) == DI8DEVTYPE_JOYSTICK ||
GET_DIDEVICE_TYPE(dev->dwDevType) == DI8DEVTYPE_GAMEPAD)
{
struct DEVINFO *info = (struct DEVINFO *)ctx;
info->productGuid = dev->guidProduct;
info->deviceID = dev->guidInstance;
info->vendor = dev->tszInstanceName;
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
/* device objects enumeration callback */
BOOL CALLBACK EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE *devobj, VOID *ctx)
{
/* set DIPROP_RANGE for the axis in order to scale min/max values */
if (devobj->dwType & DIDFT_AXIS)
{
HRESULT hr;
DIPROPRANGE dipr;
dipr.diph.dwSize = sizeof(DIPROPRANGE);
dipr.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipr.diph.dwHow = DIPH_BYID;
dipr.diph.dwObj = devobj->dwType;
dipr.lMin = MIN_AXIS;
dipr.lMax = MAX_AXIS;
hr = IDirectInputDevice_SetProperty(joystick, DIPROP_RANGE, &dipr.diph);
if (FAILED(hr))
return DIENUM_STOP;
}
return DIENUM_CONTINUE;
}
int open_joystick(char *joystick_device, GdkWindow *window)
{
HINSTANCE hi = 0;
HRESULT hr = 0;
HWND hwin = 0;
struct DEVINFO devinfo = {0};
/* create interface */
hi = GetModuleHandle(NULL);
hr = DirectInput8Create(hi, DIRECTINPUT_VERSION, &IID_IDirectInput8, (VOID**)&dinput, NULL);
if (FAILED(hr))
return -1;
/* look for a joystick */
hr = IDirectInput8_EnumDevices(dinput, DI8DEVCLASS_GAMECTRL, EnumDevCallback, &devinfo, DIEDFL_ATTACHEDONLY);
if(FAILED(hr))
return -1;
/* obtain joystick interface */
hr = IDirectInput8_CreateDevice(dinput, &devinfo.deviceID, &joystick, NULL);
if(FAILED(hr))
return -1;
/* set data format to "simple joystick" */
hr = IDirectInputDevice2_SetDataFormat(joystick, &c_dfDIJoystick2);
if(FAILED(hr))
return -1;
/* set the cooperative level */
#ifdef __WIN32__
hwin = GDK_WINDOW_HWND(window);
#endif
hr = IDirectInputDevice2_SetCooperativeLevel(joystick, hwin, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
if(FAILED(hr))
return -1;
/* enumerate axes, buttons, povs */
hr = IDirectInputDevice2_EnumObjects(joystick, EnumObjectsCallback, NULL, DIDFT_ALL);
if(FAILED(hr))
return -1;
return 0;
}
int read_joystick_event(struct js_event *jse)
{
/* not implemented */
return 0;
}
void set_joystick_y_axis(int axis)
{
/* fixme: add axis selection */
}
void set_joystick_x_axis(int axis)
{
/* fixme: add axis selection */
}
void close_joystick(void)
{
if(joystick) {
IDirectInputDevice2_Unacquire(joystick);
joystick = NULL;
}
if (dinput) {
IDirectInput8_Release(dinput);
dinput = NULL;
}
}
int get_joystick_status(struct wwvi_js_event *wjse)
{
HRESULT hr;
DIJOYSTATE2 js;
int i;
if (!joystick || !wjse)
return -1;
/* poll the device to read the current state */
hr = IDirectInputDevice2_Poll(joystick);
if (FAILED(hr)) {
/* input stream has been interrupted, re-acquire and try again */
hr = IDirectInputDevice2_Acquire(joystick);
while(hr == DIERR_INPUTLOST)
hr = IDirectInputDevice2_Acquire(joystick);
/* other errors may occur when the app is minimized
or in the process of switching, so just try again later */
if (FAILED(hr))
return -1;
/* try to poll again */
hr = IDirectInputDevice2_Poll(joystick);
if (FAILED(hr))
return -1;
}
/* get the device state */
hr = IDirectInputDevice2_GetDeviceState(joystick, sizeof(DIJOYSTATE2), &js);
if(FAILED(hr))
return -1;
/* write out state */
wjse->stick_x = js.lX;
wjse->stick_y = js.lY;
for (i = 0; i < 11; ++i) {
wjse->button[i] = (js.rgbButtons[i] & 0x80) ? 1 : 0;
}
return 0;
}
#else
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
static int joystick_fd = -1;
/* These are sensible on Logitech Dual Action Rumble and xbox360 controller. */
static int joystick_x_axis = 0;
static int joystick_y_axis = 1;
int open_joystick(char *joystick_device, __attribute__((unused)) void *window)
{
if (joystick_device == NULL)
joystick_device = JOYSTICK_DEVNAME;
joystick_fd = open(joystick_device, JOYSTICK_DEV_OPEN_FLAGS);
if (joystick_fd < 0)
return joystick_fd;
/* maybe ioctls to interrogate features here? */
return joystick_fd;
}
int read_joystick_event(struct js_event *jse)
{
int bytes;
bytes = read(joystick_fd, jse, sizeof(*jse));
if (bytes == -1)
return 0;
if (bytes == sizeof(*jse))
return 1;
printf("Unexpected bytes from joystick:%d\n", bytes);
return -1;
}
void close_joystick()
{
close(joystick_fd);
}
int get_joystick_status(struct wwvi_js_event *wjse)
{
int rc;
struct js_event jse;
if (joystick_fd < 0)
return -1;
/* memset(wjse, 0, sizeof(*wjse)); */
while ((rc = read_joystick_event(&jse) == 1)) {
jse.type &= ~JS_EVENT_INIT; /* ignore synthetic events */
if (jse.type == JS_EVENT_AXIS) {
if (jse.number == joystick_x_axis)
wjse->stick_x = jse.value;
if (jse.number == joystick_y_axis)
wjse->stick_y = jse.value;
} else if (jse.type == JS_EVENT_BUTTON) {
if (jse.number < 11) {
switch (jse.value) {
case 0:
case 1: wjse->button[jse.number] = jse.value;
break;
default:
break;
}
}
}
}
/* printf("%d\n", wjse->stick1_y); */
return 0;
}
void set_joystick_y_axis(int axis)
{
joystick_y_axis = axis;
}
void set_joystick_x_axis(int axis)
{
joystick_x_axis = axis;
}
#endif /* __WIN32__ */
#if 0
/* a little test program */
int main(int argc, char *argv[])
{
int fd, rc;
int done = 0;
struct js_event jse;
fd = open_joystick();
if (fd < 0) {
printf("open failed.\n");
exit(1);
}
while (!done) {
rc = read_joystick_event(&jse);
usleep(1000);
if (rc == 1) {
printf("Event: time %8u, value %8hd, type: %3u, axis/button: %u\n",
jse.time, jse.value, jse.type, jse.number);
}
}
}
#endif