1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 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 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
23 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
26 static int joystick_device = 0;
27 char *joystick_device_name[MAX_PLAYERS] =
37 void translate_joyname(int *joysymbol, char **name, int mode)
45 { JOY_LEFT, "joystick_left" },
46 { JOY_RIGHT, "joystick_right" },
47 { JOY_UP, "joystick_up" },
48 { JOY_DOWN, "joystick_down" },
49 { JOY_BUTTON_1, "joystick_button_1" },
50 { JOY_BUTTON_2, "joystick_button_2" },
55 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
57 *name = "[undefined]";
61 if (*joysymbol == translate_joy[i].joysymbol)
63 *name = translate_joy[i].name;
68 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
74 if (strcmp(*name, translate_joy[i].name) == 0)
76 *joysymbol = translate_joy[i].joysymbol;
83 char *getJoyNameFromJoySymbol(int joysymbol)
87 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
91 int getJoySymbolFromJoyName(char *name)
95 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
99 int getJoystickNrFromDeviceName(char *device_name)
104 if (device_name == NULL || device_name[0] == '\0')
107 c = device_name[strlen(device_name) - 1];
109 if (c >= '0' && c <= '9')
110 joystick_nr = (int)(c - '0');
112 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
118 char *getDeviceNameFromJoystickNr(int joystick_nr)
120 static char *joystick_device_name[MAX_PLAYERS] =
128 return (joystick_nr >= 0 && joystick_nr <= 3 ?
129 joystick_device_name[joystick_nr] : "");
132 #if !defined(PLATFORM_MSDOS)
133 static int JoystickPosition(int middle, int margin, int actual)
138 if (margin < middle && actual > middle)
140 if (margin > middle && actual < middle)
143 range = ABS(margin - middle);
144 pos = ABS(actual - middle);
145 percentage = (int)(pos * 100 / range);
147 if (percentage > 100)
154 #if defined(TARGET_SDL)
156 static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL };
157 static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
158 static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
160 SDL_Joystick *Get_SDL_Joystick(int nr)
162 return sdl_joystick[nr];
165 boolean Open_SDL_Joystick(int nr)
167 if (nr < 0 || nr > MAX_PLAYERS)
170 return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE);
173 void Close_SDL_Joystick(int nr)
175 if (nr < 0 || nr > MAX_PLAYERS)
178 SDL_JoystickClose(sdl_joystick[nr]);
181 boolean Check_SDL_JoystickOpened(int nr)
183 if (nr < 0 || nr > MAX_PLAYERS)
186 return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
189 void HandleJoystickEvent(Event *event)
193 case SDL_JOYAXISMOTION:
194 if (event->jaxis.axis < 2)
196 sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value;
199 printf("js_%d %s-axis: %d\n",
201 (event->jaxis.axis == 0 ? "x" : "y"),
207 case SDL_JOYBUTTONDOWN:
208 if (event->jbutton.button < 2)
210 sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE;
213 printf("js_%d button %d: pressed\n",
214 event->jbutton.which,
215 event->jbutton.button);
220 case SDL_JOYBUTTONUP:
221 if (event->jbutton.button < 2)
223 sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE;
226 printf("js_%d button %d: released\n",
227 event->jbutton.which,
228 event->jbutton.button);
238 int Get_SDL_Joystick_Axis(int nr, int axis)
240 if (nr < 0 || nr > MAX_PLAYERS)
243 if (axis < 0 || axis > 1)
246 return sdl_js_axis[nr][axis];
249 void CheckJoystickData()
253 int Joystick(int player_nr)
256 int joystick_nr = stored_player[player_nr].joystick_fd;
258 int joystick_nr = joystick.fd[player_nr];
260 int js_x,js_y, js_b1,js_b2;
261 int left, right, up, down;
264 if (joystick.status != JOYSTICK_ACTIVATED)
267 if (!setup.input[player_nr].use_joystick ||
268 !Check_SDL_JoystickOpened(joystick_nr))
271 js_x = sdl_js_axis[joystick_nr][0];
272 js_y = sdl_js_axis[joystick_nr][1];
274 js_b1 = sdl_js_button[joystick_nr][0];
275 js_b2 = sdl_js_button[joystick_nr][1];
280 printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n",
281 joystick_nr, js_x, js_y, js_b1, js_b2);
286 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
287 setup.input[player_nr].joy.xleft, js_x);
288 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
289 setup.input[player_nr].joy.xright, js_x);
290 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
291 setup.input[player_nr].joy.yupper, js_y);
292 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
293 setup.input[player_nr].joy.ylower, js_y);
295 if (left > JOYSTICK_PERCENT)
297 else if (right > JOYSTICK_PERCENT)
299 if (up > JOYSTICK_PERCENT)
301 else if (down > JOYSTICK_PERCENT)
305 result |= JOY_BUTTON_1;
307 result |= JOY_BUTTON_2;
312 printf("result == 0x%08x\n", result);
320 #else /* !TARGET_SDL */
322 void CheckJoystickData()
327 for(i=0; i<MAX_PLAYERS; i++)
329 if (setup.input[i].joy.xmiddle <= distance)
330 setup.input[i].joy.xmiddle = distance;
331 if (setup.input[i].joy.ymiddle <= distance)
332 setup.input[i].joy.ymiddle = distance;
334 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
335 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
336 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
337 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
339 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
340 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
341 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
342 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
346 #if defined(PLATFORM_UNIX)
347 int Joystick(int player_nr)
349 #if defined(PLATFORM_FREEBSD)
350 struct joystick joy_ctrl;
352 struct joystick_control
361 int joystick_fd = stored_player[player_nr].joystick_fd;
363 int joystick_fd = joystick.fd[player_nr];
365 int js_x,js_y, js_b1,js_b2;
366 int left, right, up, down;
369 if (joystick.status != JOYSTICK_ACTIVATED)
372 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
375 if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
377 Error(ERR_WARN, "cannot read joystick device '%s'",
378 setup.input[player_nr].joy.device_name);
379 joystick.status = JOYSTICK_NOT_AVAILABLE;
386 #if defined(PLATFORM_FREEBSD)
390 js_b1 = joy_ctrl.buttons & 1;
391 js_b2 = joy_ctrl.buttons & 2;
394 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
395 setup.input[player_nr].joy.xleft, js_x);
396 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
397 setup.input[player_nr].joy.xright, js_x);
398 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
399 setup.input[player_nr].joy.yupper, js_y);
400 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
401 setup.input[player_nr].joy.ylower, js_y);
403 if (left > JOYSTICK_PERCENT)
405 else if (right > JOYSTICK_PERCENT)
407 if (up > JOYSTICK_PERCENT)
409 else if (down > JOYSTICK_PERCENT)
413 result |= JOY_BUTTON_1;
415 result |= JOY_BUTTON_2;
420 #else /* PLATFORM_MSDOS */
422 /* allegro global variables for joystick control */
423 extern int num_joysticks;
424 extern JOYSTICK_INFO joy[];
426 int Joystick(int player_nr)
429 int joystick_nr = stored_player[player_nr].joystick_fd;
431 int joystick_nr = joystick.fd[player_nr];
435 if (joystick.status != JOYSTICK_ACTIVATED)
441 /* the allegro global variable 'num_joysticks' contains the number
442 of joysticks found at initialization under MS-DOS / Windows */
445 if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
451 if (joystick_nr >= num_joysticks ||
452 (game_status == PLAYING && !setup.input[player_nr].use_joystick))
456 if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
460 if (joystick_nr >= num_joysticks)
468 if (joy[joystick_nr].stick[0].axis[0].d1)
470 else if (joy[joystick_nr].stick[0].axis[0].d2)
472 if (joy[joystick_nr].stick[0].axis[1].d1)
474 else if (joy[joystick_nr].stick[0].axis[1].d2)
477 if (joy[joystick_nr].button[0].b)
478 result |= JOY_BUTTON_1;
479 if (joy[joystick_nr].button[1].b)
480 result |= JOY_BUTTON_2;
484 #endif /* PLATFORM_MSDOS */
486 #endif /* !TARGET_SDL */
488 int JoystickButton(int player_nr)
490 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
491 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
496 if (last_joy_button[player_nr])
497 result = JOY_BUTTON_PRESSED;
499 result = JOY_BUTTON_NEW_PRESSED;
503 if (last_joy_button[player_nr])
504 result = JOY_BUTTON_NEW_RELEASED;
506 result = JOY_BUTTON_NOT_PRESSED;
509 last_joy_button[player_nr] = joy_button;
518 for (i=0; i<MAX_PLAYERS; i++)
522 if (!setup.input[i].use_joystick)
527 result |= Joystick(i);
533 int AnyJoystickButton()
538 for (i=0; i<MAX_PLAYERS; i++)
542 if (!setup.input[i].use_joystick)
547 result |= JoystickButton(i);
550 result = JoystickButton(i);
551 if (result != JOY_BUTTON_NOT_PRESSED)
558 void DeactivateJoystickForCalibration()
560 /* Temporarily deactivate joystick. This is needed for calibration
561 screens, where the player has to select a joystick device that
562 should be calibrated. If there is a totally uncalibrated joystick
563 active, it may be impossible (due to messed up input from joystick)
564 to select the joystick device to calibrate even when trying to use
565 the mouse or keyboard to select the device. */
567 if (joystick.status & JOYSTICK_AVAILABLE)
568 joystick.status &= ~JOYSTICK_ACTIVE;
571 void ActivateJoystickIfAvailable()
573 /* reactivate temporarily deactivated joystick */
575 if (joystick.status & JOYSTICK_AVAILABLE)
576 joystick.status |= JOYSTICK_ACTIVE;