1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1995-2006 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #if defined(PLATFORM_FREEBSD)
15 #include <machine/joystick.h>
22 /* ========================================================================= */
23 /* platform dependent joystick functions */
24 /* ========================================================================= */
26 #if defined(PLATFORM_UNIX) && !defined(TARGET_SDL)
27 void UnixInitJoysticks()
29 static boolean unix_joystick_subsystem_initialized = FALSE;
30 boolean print_warning = !unix_joystick_subsystem_initialized;
33 for (i = 0; i < MAX_PLAYERS; i++)
35 char *device_name = setup.input[i].joy.device_name;
37 /* this allows subsequent calls to 'InitJoysticks' for re-initialization */
38 if (joystick.fd[i] != -1)
40 close(joystick.fd[i]);
44 if (!setup.input[i].use_joystick)
47 if (access(device_name, R_OK) != 0)
50 Error(ERR_WARN, "cannot access joystick device '%s'", device_name);
55 if ((joystick.fd[i] = open(device_name, O_RDONLY)) < 0)
58 Error(ERR_WARN, "cannot open joystick device '%s'", device_name);
63 joystick.status = JOYSTICK_ACTIVATED;
66 unix_joystick_subsystem_initialized = TRUE;
69 boolean UnixReadJoystick(int fd, int *x, int *y, boolean *b1, boolean *b2)
71 #if defined(PLATFORM_FREEBSD)
72 struct joystick joy_ctrl;
74 struct joystick_control
82 if (read(fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
90 #if defined(PLATFORM_FREEBSD)
97 *b1 = joy_ctrl.buttons & 1;
99 *b2 = joy_ctrl.buttons & 2;
104 #endif /* PLATFORM_UNIX && !TARGET_SDL */
107 /* ========================================================================= */
108 /* platform independent joystick functions */
109 /* ========================================================================= */
111 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
112 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
114 void translate_joyname(int *joysymbol, char **name, int mode)
122 { JOY_LEFT, "joystick_left" },
123 { JOY_RIGHT, "joystick_right" },
124 { JOY_UP, "joystick_up" },
125 { JOY_DOWN, "joystick_down" },
126 { JOY_BUTTON_1, "joystick_button_1" },
127 { JOY_BUTTON_2, "joystick_button_2" },
132 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
134 *name = "[undefined]";
136 for (i = 0; i < 6; i++)
138 if (*joysymbol == translate_joy[i].joysymbol)
140 *name = translate_joy[i].name;
145 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
149 for (i = 0; i < 6; i++)
151 if (strEqual(*name, translate_joy[i].name))
153 *joysymbol = translate_joy[i].joysymbol;
160 char *getJoyNameFromJoySymbol(int joysymbol)
164 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
168 int getJoySymbolFromJoyName(char *name)
172 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
176 int getJoystickNrFromDeviceName(char *device_name)
181 if (device_name == NULL || device_name[0] == '\0')
184 c = device_name[strlen(device_name) - 1];
186 if (c >= '0' && c <= '9')
187 joystick_nr = (int)(c - '0');
189 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
195 char *getDeviceNameFromJoystickNr(int joystick_nr)
197 static char *joystick_device_name[MAX_PLAYERS] =
205 return (joystick_nr >= 0 && joystick_nr <= 3 ?
206 joystick_device_name[joystick_nr] : "");
209 static int JoystickPositionPercent(int center, int border, int actual)
211 long range, position;
214 if (border < center && actual > center)
216 if (border > center && actual < center)
219 range = ABS(border - center);
220 position = ABS(actual - center);
222 percent = (int)(position * 100 / range);
230 void CheckJoystickData()
235 for (i = 0; i < MAX_PLAYERS; i++)
237 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
238 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
239 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
240 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
242 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
243 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
244 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
245 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
249 int Joystick(int player_nr)
251 int joystick_fd = joystick.fd[player_nr];
253 boolean js_b1, js_b2;
254 int left, right, up, down;
255 int result = JOY_NO_ACTION;
257 if (joystick.status != JOYSTICK_ACTIVATED)
258 return JOY_NO_ACTION;
260 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
261 return JOY_NO_ACTION;
263 if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2))
265 Error(ERR_WARN, "cannot read joystick device '%s'",
266 setup.input[player_nr].joy.device_name);
268 joystick.status = JOYSTICK_NOT_AVAILABLE;
269 return JOY_NO_ACTION;
272 left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
273 setup.input[player_nr].joy.xleft, js_x);
274 right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle,
275 setup.input[player_nr].joy.xright, js_x);
276 up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
277 setup.input[player_nr].joy.yupper, js_y);
278 down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle,
279 setup.input[player_nr].joy.ylower, js_y);
281 if (left > JOYSTICK_PERCENT)
283 else if (right > JOYSTICK_PERCENT)
285 if (up > JOYSTICK_PERCENT)
287 else if (down > JOYSTICK_PERCENT)
291 result |= JOY_BUTTON_1;
293 result |= JOY_BUTTON_2;
298 int JoystickButton(int player_nr)
300 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
301 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
306 if (last_joy_button[player_nr])
307 result = JOY_BUTTON_PRESSED;
309 result = JOY_BUTTON_NEW_PRESSED;
313 if (last_joy_button[player_nr])
314 result = JOY_BUTTON_NEW_RELEASED;
316 result = JOY_BUTTON_NOT_PRESSED;
319 last_joy_button[player_nr] = joy_button;
328 for (i = 0; i < MAX_PLAYERS; i++)
329 result |= Joystick(i);
334 int AnyJoystickButton()
337 int result = JOY_BUTTON_NOT_PRESSED;
339 for (i = 0; i < MAX_PLAYERS; i++)
341 result = JoystickButton(i);
342 if (result != JOY_BUTTON_NOT_PRESSED)
349 void DeactivateJoystick()
351 /* Temporarily deactivate joystick. This is needed for calibration
352 screens, where the player has to select a joystick device that
353 should be calibrated. If there is a totally uncalibrated joystick
354 active, it may be impossible (due to messed up input from joystick)
355 to select the joystick device to calibrate even when trying to use
356 the mouse or keyboard to select the device. */
358 if (joystick.status & JOYSTICK_AVAILABLE)
359 joystick.status &= ~JOYSTICK_ACTIVE;
362 void ActivateJoystick()
364 /* reactivate temporarily deactivated joystick */
366 if (joystick.status & JOYSTICK_AVAILABLE)
367 joystick.status |= JOYSTICK_ACTIVE;