|
|
1.1.1.6 root 1: /*
1.1 root 2: * UAE - The Un*x Amiga Emulator
1.1.1.6 root 3: *
1.1.1.2 root 4: * Joystick emulation for Linux and BSD. They share too much code to
5: * split this file.
1.1.1.6 root 6: *
7: * This uses the deprecated 0.x Linux joystick API.
8: *
1.1 root 9: * Copyright 1997 Bernd Schmidt
1.1.1.2 root 10: * Copyright 1998 Krister Walfridsson
1.1.1.6 root 11: * Copyright 2003-2004 Richard Drummond
12: * Copyright 2004 Nick Seow (Alternative Linux joystick device path)
1.1 root 13: */
14:
15: #include "sysconfig.h"
16: #include "sysdeps.h"
17:
18: #include "options.h"
19: #include "memory.h"
20: #include "custom.h"
1.1.1.6 root 21: #include "inputdevice.h"
22:
23: #define JS_MAXPATHLEN 20 // Longest device name would be "/dev/input/js"
24: char js_prefix[JS_MAXPATHLEN]; // Joystick device, which varies, except number
1.1 root 25:
1.1.1.4 root 26: #ifdef HAVE_MACHINE_JOYSTICK_H
1.1 root 27:
1.1.1.2 root 28: /* The BSD way. */
29:
1.1.1.4 root 30: # include <machine/joystick.h>
31: typedef struct joystick uae_joystick_t;
1.1.1.2 root 32:
33: #define JS_DEVNAME_PREFIX "joy"
34:
35: #else
36:
37: /* The Linux way. */
1.1 root 38:
39: /* There are too many different versions of <linux/joystick.h>. Rather
40: * than trying to work correctly with all of them, we duplicate the
41: * necessary definitions here. */
1.1.1.2 root 42: typedef struct
1.1 root 43: {
44: int buttons;
45: int x;
46: int y;
1.1.1.2 root 47: } uae_joystick_t;
1.1 root 48:
1.1.1.6 root 49: #define JS_DEVNAME_PREFIX "js" // Try this first
1.1.1.2 root 50:
51: #endif
52:
1.1.1.6 root 53: /* Hard code these for the old joystick API */
54: #define MAX_BUTTONS 2
55: #define MAX_AXLES 2
56: #define FIRST_AXLE 0
57: #define FIRST_BUTTON 2
58:
59: static int nr_joysticks;
1.1.1.2 root 60:
61: static int js0, js1;
1.1 root 62:
1.1.1.3 root 63: struct joy_range
64: {
65: int minx, maxx, miny, maxy;
1.1.1.6 root 66: int centrex, centrey;
1.1.1.3 root 67: } range0, range1;
68:
1.1.1.6 root 69: static void read_joy(int nr)
1.1 root 70: {
1.1.1.2 root 71: uae_joystick_t buffer;
1.1 root 72: int len;
73: int fd = nr == 0 ? js0 : js1;
1.1.1.3 root 74: struct joy_range *r = nr == 0 ? &range0 : &range1;
1.1 root 75:
76: if (nr >= nr_joysticks)
1.1.1.6 root 77: return;
78:
1.1 root 79: len = read(fd, &buffer, sizeof(buffer));
1.1.1.6 root 80: if (len != sizeof(buffer))
81: return;
82:
83: /* According to old 0.x JS API, we don't know the range
84: * or the centre for either axis, so we try to work these
85: * out as we go along.
86: *
87: * Must be a better way to do this . . .
88: */
1.1.1.3 root 89: if (buffer.x < r->minx) r->minx = buffer.x;
90: if (buffer.y < r->miny) r->miny = buffer.y;
91: if (buffer.x > r->maxx) r->maxx = buffer.x;
92: if (buffer.y > r->maxy) r->maxy = buffer.y;
1.1.1.6 root 93:
94: r->centrex = (r->maxx-r->minx)/2 + r->minx;
95: r->centrey = (r->maxy-r->miny)/2 + r->miny;
96:
97: /* Translate these values to be centred on 0 and
98: * feed 'em to the inputdevice system */
99: setjoystickstate (nr, 0, buffer.x - r->centrex, r->centrex);
100: setjoystickstate (nr, 1, buffer.y - r->centrey, r->centrey);
101:
1.1.1.4 root 102: #ifdef HAVE_MACHINE_JOYSTICK_H
1.1.1.6 root 103: setjoybuttonstate (nr, 0, buffer.b1);
104: setjoybuttonstate (nr, 1, buffer.b2);
1.1.1.4 root 105: #else
1.1.1.6 root 106: setjoybuttonstate (nr, 0, buffer.buttons & 1);
107: setjoybuttonstate (nr, 1, buffer.buttons & 2);
1.1.1.4 root 108: #endif
1.1 root 109: }
110:
1.1.1.6 root 111: static int init_joysticks(void)
1.1 root 112: {
113: nr_joysticks = 0;
114: js0 = -1; js1 = -1;
1.1.1.6 root 115: char js_path[JS_MAXPATHLEN]; // temporary buffer for device name
116:
117: snprintf (js_prefix, JS_MAXPATHLEN, "/dev/%s", JS_DEVNAME_PREFIX);
118:
119: snprintf (js_path, JS_MAXPATHLEN, "%s0", js_prefix);
120: if ((js0 = open (js_path, O_RDONLY)) >= 0)
121: nr_joysticks++;
122:
123: snprintf (js_path, JS_MAXPATHLEN, "%s1", js_prefix);
124: if ((js1 = open (js_path, O_RDONLY)) >= 0)
125: nr_joysticks++;
126:
127: #ifdef __linux__
128: if (nr_joysticks == 0) {
129: /*
130: * If we haven't found any joysticks yet,
131: * look for /dev/input/js* nodes
132: */
133: sprintf (js_prefix, "/dev/input/%s", JS_DEVNAME_PREFIX);
134:
135: snprintf (js_path, JS_MAXPATHLEN, "%s0", js_prefix);
136: if ((js0 = open (js_path, O_RDONLY)) >= 0)
137: nr_joysticks++;
138:
139: snprintf (js_path, JS_MAXPATHLEN, "%s1", js_prefix);
140: if ((js1 = open (js_path, O_RDONLY)) >= 0)
141: nr_joysticks++;
1.1 root 142: }
1.1.1.6 root 143: #endif
144:
145: write_log ("Found %d joystick(s)\n", nr_joysticks);
1.1.1.3 root 146:
147: range0.minx = INT_MAX;
148: range0.maxx = INT_MIN;
149: range0.miny = INT_MAX;
150: range0.maxy = INT_MIN;
151: range1.minx = INT_MAX;
152: range1.maxx = INT_MIN;
153: range1.miny = INT_MAX;
154: range1.maxy = INT_MIN;
1.1.1.6 root 155: range0.centrex = 0;
156: range1.centrey = 0;
157: return 1;
1.1 root 158: }
159:
1.1.1.6 root 160: static void close_joysticks(void)
1.1 root 161: {
162: if (js0 >= 0)
1.1.1.3 root 163: close (js0);
1.1 root 164: if (js1 >= 0)
1.1.1.3 root 165: close (js1);
1.1 root 166: }
1.1.1.6 root 167:
168: static int acquire_joy (int num, int flags)
169: {
170: return 1;
171: }
172:
173: static void unacquire_joy (int num)
174: {
175: }
176:
177: static int get_joystick_num (void)
178: {
179: return nr_joysticks;
180: }
181:
182: static void read_joysticks (void)
183: {
184: int i;
185: for (i = 0; i < get_joystick_num (); i++) {
186: if (currprefs.input_selected_setting == 0) {
187: if (jsem_isjoy (0, &currprefs) != i && jsem_isjoy (1, &currprefs) != i)
188: continue;
189: }
190: read_joy (i);
191: }
192: }
193:
194: static char *get_joystick_name (int joy)
195: {
196: static char name[100];
197: sprintf (name, "%d: %s%d", joy + 1, js_prefix, joy);
198: return name;
199: }
200:
201: static int get_joystick_widget_num (int joy)
202: {
203: return MAX_AXLES + MAX_BUTTONS;
204: }
205:
206: static int get_joystick_widget_type (int joy, int num, char *name, uae_u32 *dummy)
207: {
208: if (num >= MAX_AXLES && num < MAX_AXLES+MAX_BUTTONS) {
209: if (name)
210: sprintf (name, "Button %d", num + 1 - MAX_AXLES);
211: return IDEV_WIDGET_BUTTON;
212: } else if (num < MAX_AXLES) {
213: if (name)
214: sprintf (name, "Axis %d", num + 1);
215: return IDEV_WIDGET_AXIS;
216: }
217: return IDEV_WIDGET_NONE;
218: }
219:
220: static int get_joystick_widget_first (int joy, int type)
221: {
222: switch (type) {
223: case IDEV_WIDGET_BUTTON:
224: return FIRST_BUTTON;
225: case IDEV_WIDGET_AXIS:
226: return FIRST_AXLE;
227: }
228:
229: return -1;
230: }
231:
232: struct inputdevice_functions inputdevicefunc_joystick = {
233: init_joysticks, close_joysticks, acquire_joy, unacquire_joy,
234: read_joysticks, get_joystick_num, get_joystick_name,
235: get_joystick_widget_num, get_joystick_widget_type,
236: get_joystick_widget_first
237: };
238:
239: /*
240: * Set default inputdevice config for joysticks
241: */
242: void input_get_default_joystick (struct uae_input_device *uid)
243: {
244: int i, port;
245:
246: for (i = 0; i < nr_joysticks; i++) {
247: port = i & 1;
248: uid[i].eventid[ID_AXIS_OFFSET + 0][0] = port ? INPUTEVENT_JOY2_HORIZ : INPUTEVENT_JOY1_HORIZ;
249: uid[i].eventid[ID_AXIS_OFFSET + 1][0] = port ? INPUTEVENT_JOY2_VERT : INPUTEVENT_JOY1_VERT;
250: uid[i].eventid[ID_BUTTON_OFFSET + 0][0] = port ? INPUTEVENT_JOY2_FIRE_BUTTON : INPUTEVENT_JOY1_FIRE_BUTTON;
251: uid[i].eventid[ID_BUTTON_OFFSET + 1][0] = port ? INPUTEVENT_JOY2_2ND_BUTTON : INPUTEVENT_JOY1_2ND_BUTTON;
252: uid[i].eventid[ID_BUTTON_OFFSET + 2][0] = port ? INPUTEVENT_JOY2_3RD_BUTTON : INPUTEVENT_JOY1_3RD_BUTTON;
253: }
254: uid[0].enabled = 1;
255: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.