summaryrefslogtreecommitdiff
path: root/emulators/vice/patches/patch-bi
blob: a686081d095c0609db14de614c086f8b0539476a (plain)
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
$NetBSD: patch-bi,v 1.1 2004/07/26 17:22:40 dillo Exp $

--- src/arch/unix/joy_usb.c.orig	2004-07-26 14:35:45.000000000 +0200
+++ src/arch/unix/joy_usb.c
@@ -0,0 +1,305 @@
+/*
+ * joy_usb.c - NetBSD/FreeBSD USB joystick support.
+ *
+ * Written by
+ *   Dieter Baron <dillo@nih.at>
+ *
+ * This file is part of VICE, the Versatile Commodore Emulator.
+ * See README for copyright notice.
+ *
+ *  This program 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.
+ *
+ *  This program 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ *  02111-1307  USA.
+ *
+ */
+
+#include "vice.h"
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "cmdline.h"
+#include "joy.h"
+#include "joystick.h"
+#include "keyboard.h"
+#include "log.h"
+#include "resources.h"
+#include "types.h"
+
+#if defined(HAS_JOYSTICK) && defined(HAS_USB_JOYSTICK)
+
+#define ITEM_AXIS	0
+#define ITEM_BUTTON	1
+#define ITEM_HAT	2
+
+int hat_or[] = {
+    1, 9, 8, 10, 2, 6, 4, 5,
+};
+
+extern log_t joystick_log;
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbhid.h>
+#include <errno.h>
+#include <stdlib.h>
+#ifdef HAVE_USBHID_H
+#include <usbhid.h>
+#else
+#include <libusbhid.h>
+#endif
+
+#define MAX_DEV		4	/* number of uhid devices to try */
+
+struct usb_joy_item {
+    struct hid_item item;
+    struct usb_joy_item *next;
+
+    int is_hat;
+    int min_or;
+    int min_val;
+    int max_or;
+    int max_val;
+};
+
+static struct usb_joy_item *usb_joy_item[2];
+
+static int usb_joy_fd[2] = { -1, -1 };
+static int usb_joy_size[2];
+static char *usb_joy_buf[2];
+
+static int usb_joy_add_item(struct usb_joy_item **item, struct hid_item *hi,
+			    int orval, int type)
+{
+    struct usb_joy_item *it;
+    int w;
+
+    if ((it=malloc(sizeof(*it))) == NULL) {
+	/* XXX */
+	return -1;
+    }
+
+    it->next = *item;
+    *item = it;
+
+    memcpy(&it->item, hi, sizeof(*hi));
+    switch (type) {
+    case ITEM_AXIS:
+	w = (hi->logical_maximum-hi->logical_minimum)/3;
+	it->is_hat = 0;
+	it->min_or = orval;
+	it->min_val = hi->logical_minimum+w;
+	it->max_or = orval*2;
+	it->max_val = hi->logical_maximum-w;
+	break;
+
+    case ITEM_BUTTON:
+	it->is_hat = 0;
+	it->min_or = 0;
+	it->min_val = -1;
+	it->max_or = orval;
+	it->max_val = hi->logical_maximum-1;
+	break;
+
+    case ITEM_HAT:
+	it->is_hat = 1;
+	it->min_val = hi->logical_minimum;
+	break;
+    }
+
+    return 0;
+}
+
+static void usb_free_item(struct usb_joy_item **item)
+{
+    struct usb_joy_item *it, *it2;
+    
+    it=*item;
+    while (it) {
+	it2 = it;
+	it = it->next;
+	free(it2);
+    }
+    *item = NULL;
+}
+
+int usb_joystick_init(void)
+{
+    int i, j, id, fd;
+    report_desc_t report;
+    struct hid_item h;
+    struct hid_data *d;
+    int is_joy, found;
+    char dev[32];
+
+    for (j=i=0; i<2 && j<MAX_DEV; j++) {
+	sprintf(dev, "/dev/uhid%d", j);
+	fd = open(dev, O_RDONLY|O_NONBLOCK);
+	if (fd < 0)
+	    continue;
+
+	if (ioctl(fd, USB_GET_REPORT_ID, &id) < 0) {
+	    log_warning(joystick_log,
+			_("Cannot get report id for joystick device `%s'."),
+			dev);
+	    close(fd);
+	}
+
+	if ((report=hid_get_report_desc(fd)) == NULL) {
+	    log_warning(joystick_log,
+		_("Cannot report description for joystick device `%s'."),
+		dev);
+	    close(fd);
+	    continue;
+	}
+	usb_joy_size[i] = hid_report_size(report, hid_input, id);
+
+	usb_joy_item[i] = NULL;
+
+	found = 0;
+	is_joy = 0;
+	for (d=hid_start_parse(report, 1<<hid_input, id);
+	     hid_get_item(d, &h);) {
+	    if (h.kind == hid_collection
+		&& HID_PAGE(h.usage) == HUP_GENERIC_DESKTOP
+		&& (HID_USAGE(h.usage) == HUG_JOYSTICK
+		    || HID_USAGE(h.usage) == HUG_GAME_PAD)) {
+		is_joy = 1;
+		continue;
+	    }
+	    if (!is_joy)
+		continue;
+	    
+	    switch (HID_PAGE(h.usage)) {
+	    case HUP_GENERIC_DESKTOP:
+		switch (HID_USAGE(h.usage)) {
+		case HUG_X:
+		case HUG_RX:
+		    if (usb_joy_add_item(usb_joy_item+i, &h,
+					 4, ITEM_AXIS) == 0)
+			found |= 4;
+		    break;
+
+		case HUG_Y:
+		case HUG_RY:
+		    if (usb_joy_add_item(usb_joy_item+i, &h,
+					 1, ITEM_AXIS) == 0)
+			found |= 1;
+		    break;
+		case HUG_HAT_SWITCH:
+		    if (usb_joy_add_item(usb_joy_item+i, &h,
+					 0, ITEM_HAT) == 0)
+			found |= 5;
+		    break;
+		}
+		break;
+		
+	    case HUP_BUTTON:
+		if (usb_joy_add_item(usb_joy_item+i, &h,
+				     16, ITEM_BUTTON) == 0)
+		    found |= 16;
+		break;
+	    }
+	}
+	hid_end_parse(d);
+
+	if (found != 21) {
+	    close(fd);
+	    usb_free_item(usb_joy_item+i);
+	    log_message(joystick_log,
+		     _("Not all axes found in joystick device `%s'."), dev);
+	    continue;
+	}
+	
+	if ((usb_joy_buf[i]=malloc(usb_joy_size[i])) == NULL) {
+	    log_warning(joystick_log,
+			_("Cannot allocate buffer for joystick device `%s'."),
+			dev);
+	    close(fd);
+	    usb_free_item(usb_joy_item+i);
+	    continue;
+	}
+
+	log_message(joystick_log,
+		    _("USB joystick found: `%s'."), dev);
+	usb_joy_fd[i] = fd;
+	i++;
+    }
+}
+
+void usb_joystick_close(void)
+{
+    int i;
+
+    for (i=0; i<2; i++) {
+	if (usb_joy_fd[i] < 0)
+	    continue;
+
+	close(usb_joy_fd[i]);
+	usb_joy_fd[i] = -1;
+	usb_free_item(usb_joy_item+i);
+    }
+}
+
+void usb_joystick(void)
+{
+    int i, jp, val, ret;
+    struct usb_joy_item *it;
+
+    for (i=0; i<2; i++) {
+	jp = joystick_port_map[i];
+	if (jp != JOYDEV_USB_0 && jp != JOYDEV_USB_1)
+	    continue;
+
+	jp -= JOYDEV_USB_0;
+
+	if (usb_joy_fd[jp] < 0)
+	    continue;
+
+	val = 0;
+	while ((ret=read(usb_joy_fd[jp], usb_joy_buf[jp], usb_joy_size[jp]))
+	       == usb_joy_size[jp])
+	    val = 1;
+	if (ret != -1 && errno != EAGAIN) {
+	    /* XXX */
+	    printf("strange read return: %d/%d\n",
+		   ret, errno);
+	    continue;
+	}
+	if (!val)
+	    continue;
+
+	joystick_set_value_absolute(i+1, 0);
+
+	for (it=usb_joy_item[jp]; it; it=it->next) {
+	    val = hid_get_data(usb_joy_buf[jp], &it->item);
+	    if (it->is_hat) {
+		val -= it->min_val;
+		if (val >= 0 && val <= 7)
+		    joystick_set_value_or(i+1, hat_or[val]);
+	    }
+	    else {
+		if (val <= it->min_val) {
+		    joystick_set_value_or(i+1, it->min_or);
+		}
+		else if (val > it->max_val) {
+		    joystick_set_value_or(i+1, it->max_or);
+		}
+	    }
+	}
+    }
+}
+
+#endif /* HAS_JOYSTICK && HAS_USB_JOYSTICK */
+