1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-2000 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #if defined(PLATFORM_FREEBSD)
15 #include <machine/joystick.h>
18 #include "libgame/libgame.h"
22 #define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0
23 #define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1
25 void translate_joyname(int *joysymbol, char **name, int mode)
33 { JOY_LEFT, "joystick_left" },
34 { JOY_RIGHT, "joystick_right" },
35 { JOY_UP, "joystick_up" },
36 { JOY_DOWN, "joystick_down" },
37 { JOY_BUTTON_1, "joystick_button_1" },
38 { JOY_BUTTON_2, "joystick_button_2" },
43 if (mode == TRANSLATE_JOYSYMBOL_TO_JOYNAME)
45 *name = "[undefined]";
49 if (*joysymbol == translate_joy[i].joysymbol)
51 *name = translate_joy[i].name;
56 else if (mode == TRANSLATE_JOYNAME_TO_JOYSYMBOL)
62 if (strcmp(*name, translate_joy[i].name) == 0)
64 *joysymbol = translate_joy[i].joysymbol;
71 char *getJoyNameFromJoySymbol(int joysymbol)
75 translate_joyname(&joysymbol, &name, TRANSLATE_JOYSYMBOL_TO_JOYNAME);
79 int getJoySymbolFromJoyName(char *name)
83 translate_joyname(&joysymbol, &name, TRANSLATE_JOYNAME_TO_JOYSYMBOL);
87 int getJoystickNrFromDeviceName(char *device_name)
92 if (device_name == NULL || device_name[0] == '\0')
95 c = device_name[strlen(device_name) - 1];
97 if (c >= '0' && c <= '9')
98 joystick_nr = (int)(c - '0');
100 if (joystick_nr < 0 || joystick_nr >= MAX_PLAYERS)
106 #if !defined(PLATFORM_MSDOS)
107 static int JoystickPosition(int middle, int margin, int actual)
112 if (margin < middle && actual > middle)
114 if (margin > middle && actual < middle)
117 range = ABS(margin - middle);
118 pos = ABS(actual - middle);
119 percentage = (int)(pos * 100 / range);
121 if (percentage > 100)
128 #if defined(TARGET_SDL)
130 static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL };
131 static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
132 static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
134 SDL_Joystick *Get_SDL_Joystick(int nr)
136 return sdl_joystick[nr];
139 boolean Open_SDL_Joystick(int nr)
141 if (nr < 0 || nr > MAX_PLAYERS)
144 return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE);
147 void Close_SDL_Joystick(int nr)
149 if (nr < 0 || nr > MAX_PLAYERS)
152 SDL_JoystickClose(sdl_joystick[nr]);
155 boolean Check_SDL_JoystickOpened(int nr)
157 if (nr < 0 || nr > MAX_PLAYERS)
160 return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
163 void HandleJoystickEvent(Event *event)
167 case SDL_JOYAXISMOTION:
168 if (event->jaxis.axis < 2)
170 sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value;
173 printf("js_%d %s-axis: %d\n",
175 (event->jaxis.axis == 0 ? "x" : "y"),
181 case SDL_JOYBUTTONDOWN:
182 if (event->jbutton.button < 2)
184 sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE;
187 printf("js_%d button %d: pressed\n",
188 event->jbutton.which,
189 event->jbutton.button);
194 case SDL_JOYBUTTONUP:
195 if (event->jbutton.button < 2)
197 sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE;
200 printf("js_%d button %d: released\n",
201 event->jbutton.which,
202 event->jbutton.button);
212 int Get_SDL_Joystick_Axis(int nr, int axis)
214 if (nr < 0 || nr > MAX_PLAYERS)
217 if (axis < 0 || axis > 1)
220 return sdl_js_axis[nr][axis];
223 void CheckJoystickData()
227 int Joystick(int player_nr)
229 int joystick_nr = stored_player[player_nr].joystick_fd;
230 int js_x,js_y, js_b1,js_b2;
231 int left, right, up, down;
234 if (joystick_status == JOYSTICK_OFF)
237 if (game_status == SETUPINPUT)
240 if (!setup.input[player_nr].use_joystick ||
241 !Check_SDL_JoystickOpened(joystick_nr))
244 js_x = sdl_js_axis[joystick_nr][0];
245 js_y = sdl_js_axis[joystick_nr][1];
247 js_b1 = sdl_js_button[joystick_nr][0];
248 js_b2 = sdl_js_button[joystick_nr][1];
253 printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n",
254 joystick_nr, js_x, js_y, js_b1, js_b2);
259 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
260 setup.input[player_nr].joy.xleft, js_x);
261 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
262 setup.input[player_nr].joy.xright, js_x);
263 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
264 setup.input[player_nr].joy.yupper, js_y);
265 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
266 setup.input[player_nr].joy.ylower, js_y);
268 if (left > JOYSTICK_PERCENT)
270 else if (right > JOYSTICK_PERCENT)
272 if (up > JOYSTICK_PERCENT)
274 else if (down > JOYSTICK_PERCENT)
278 result |= JOY_BUTTON_1;
280 result |= JOY_BUTTON_2;
285 printf("result == 0x%08x\n", result);
293 #else /* !TARGET_SDL */
295 void CheckJoystickData()
300 for(i=0; i<MAX_PLAYERS; i++)
302 if (setup.input[i].joy.xmiddle <= distance)
303 setup.input[i].joy.xmiddle = distance;
304 if (setup.input[i].joy.ymiddle <= distance)
305 setup.input[i].joy.ymiddle = distance;
307 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
308 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
309 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
310 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
312 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
313 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
314 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
315 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
319 #if defined(PLATFORM_UNIX)
320 int Joystick(int player_nr)
323 struct joystick joy_ctrl;
325 struct joystick_control
333 int joystick_fd = stored_player[player_nr].joystick_fd;
334 int js_x,js_y, js_b1,js_b2;
335 int left, right, up, down;
338 if (joystick_status == JOYSTICK_OFF)
341 if (game_status == SETUPINPUT)
344 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
347 if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
349 Error(ERR_WARN, "cannot read joystick device '%s'",
350 setup.input[player_nr].joy.device_name);
351 joystick_status = JOYSTICK_OFF;
362 js_b1 = joy_ctrl.buttons & 1;
363 js_b2 = joy_ctrl.buttons & 2;
366 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
367 setup.input[player_nr].joy.xleft, js_x);
368 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
369 setup.input[player_nr].joy.xright, js_x);
370 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
371 setup.input[player_nr].joy.yupper, js_y);
372 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
373 setup.input[player_nr].joy.ylower, js_y);
375 if (left > JOYSTICK_PERCENT)
377 else if (right > JOYSTICK_PERCENT)
379 if (up > JOYSTICK_PERCENT)
381 else if (down > JOYSTICK_PERCENT)
385 result |= JOY_BUTTON_1;
387 result |= JOY_BUTTON_2;
392 #else /* PLATFORM_MSDOS */
394 /* allegro global variables for joystick control */
395 extern int num_joysticks;
396 extern JOYSTICK_INFO joy[];
398 int Joystick(int player_nr)
400 int joystick_nr = stored_player[player_nr].joystick_fd;
403 if (joystick_status == JOYSTICK_OFF)
406 if (game_status == SETUPINPUT)
412 /* the allegro global variable 'num_joysticks' contains the number
413 of joysticks found at initialization under MS-DOS / Windows */
416 if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
421 if (joystick_nr >= num_joysticks ||
422 (game_status == PLAYING && !setup.input[player_nr].use_joystick))
425 if (joystick_nr >= num_joysticks)
433 if (joy[joystick_nr].stick[0].axis[0].d1)
435 else if (joy[joystick_nr].stick[0].axis[0].d2)
437 if (joy[joystick_nr].stick[0].axis[1].d1)
439 else if (joy[joystick_nr].stick[0].axis[1].d2)
442 if (joy[joystick_nr].button[0].b)
443 result |= JOY_BUTTON_1;
444 if (joy[joystick_nr].button[1].b)
445 result |= JOY_BUTTON_2;
449 #endif /* PLATFORM_MSDOS */
451 #endif /* !TARGET_SDL */
453 int JoystickButton(int player_nr)
455 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
456 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
461 if (last_joy_button[player_nr])
462 result = JOY_BUTTON_PRESSED;
464 result = JOY_BUTTON_NEW_PRESSED;
468 if (last_joy_button[player_nr])
469 result = JOY_BUTTON_NEW_RELEASED;
471 result = JOY_BUTTON_NOT_PRESSED;
474 last_joy_button[player_nr] = joy_button;
483 for (i=0; i<MAX_PLAYERS; i++)
487 if (!setup.input[i].use_joystick)
492 result |= Joystick(i);
498 int AnyJoystickButton()
503 for (i=0; i<MAX_PLAYERS; i++)
507 if (!setup.input[i].use_joystick)
512 result |= JoystickButton(i);
515 result = JoystickButton(i);
516 if (result != JOY_BUTTON_NOT_PRESSED)