1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
14 #if defined(PLATFORM_FREEBSD)
15 #include <machine/joystick.h>
18 #include "libgame/libgame.h"
22 #if !defined(PLATFORM_MSDOS)
23 static int JoystickPosition(int middle, int margin, int actual)
28 if (margin < middle && actual > middle)
30 if (margin > middle && actual < middle)
33 range = ABS(margin - middle);
34 pos = ABS(actual - middle);
35 percentage = (int)(pos * 100 / range);
44 #if defined(TARGET_SDL)
46 static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL };
47 static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
48 static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };
50 SDL_Joystick *Get_SDL_Joystick(int nr)
52 return sdl_joystick[nr];
55 boolean Open_SDL_Joystick(int nr)
57 if (nr < 0 || nr > MAX_PLAYERS)
60 return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE);
63 void Close_SDL_Joystick(int nr)
65 if (nr < 0 || nr > MAX_PLAYERS)
68 SDL_JoystickClose(sdl_joystick[nr]);
71 boolean Check_SDL_JoystickOpened(int nr)
73 if (nr < 0 || nr > MAX_PLAYERS)
76 return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
79 void HandleJoystickEvent(Event *event)
83 case SDL_JOYAXISMOTION:
84 if (event->jaxis.axis < 2)
86 sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value;
89 printf("js_%d %s-axis: %d\n",
91 (event->jaxis.axis == 0 ? "x" : "y"),
97 case SDL_JOYBUTTONDOWN:
98 if (event->jbutton.button < 2)
100 sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE;
103 printf("js_%d button %d: pressed\n",
104 event->jbutton.which,
105 event->jbutton.button);
110 case SDL_JOYBUTTONUP:
111 if (event->jbutton.button < 2)
113 sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE;
116 printf("js_%d button %d: released\n",
117 event->jbutton.which,
118 event->jbutton.button);
128 int Get_SDL_Joystick_Axis(int nr, int axis)
130 if (nr < 0 || nr > MAX_PLAYERS)
133 if (axis < 0 || axis > 1)
136 return sdl_js_axis[nr][axis];
139 void CheckJoystickData()
143 int Joystick(int player_nr)
145 int joystick_nr = stored_player[player_nr].joystick_fd;
146 int js_x,js_y, js_b1,js_b2;
147 int left, right, up, down;
150 if (joystick_status == JOYSTICK_OFF)
153 if (game_status == SETUPINPUT)
156 if (!setup.input[player_nr].use_joystick ||
157 !Check_SDL_JoystickOpened(joystick_nr))
160 js_x = sdl_js_axis[joystick_nr][0];
161 js_y = sdl_js_axis[joystick_nr][1];
163 js_b1 = sdl_js_button[joystick_nr][0];
164 js_b2 = sdl_js_button[joystick_nr][1];
169 printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n",
170 joystick_nr, js_x, js_y, js_b1, js_b2);
175 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
176 setup.input[player_nr].joy.xleft, js_x);
177 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
178 setup.input[player_nr].joy.xright, js_x);
179 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
180 setup.input[player_nr].joy.yupper, js_y);
181 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
182 setup.input[player_nr].joy.ylower, js_y);
184 if (left > JOYSTICK_PERCENT)
186 else if (right > JOYSTICK_PERCENT)
188 if (up > JOYSTICK_PERCENT)
190 else if (down > JOYSTICK_PERCENT)
194 result |= JOY_BUTTON_1;
196 result |= JOY_BUTTON_2;
201 printf("result == 0x%08x\n", result);
209 #else /* !TARGET_SDL */
211 void CheckJoystickData()
216 for(i=0; i<MAX_PLAYERS; i++)
218 if (setup.input[i].joy.xmiddle <= distance)
219 setup.input[i].joy.xmiddle = distance;
220 if (setup.input[i].joy.ymiddle <= distance)
221 setup.input[i].joy.ymiddle = distance;
223 if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
224 setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
225 if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
226 setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
228 if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
229 setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
230 if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
231 setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
235 #if defined(PLATFORM_UNIX)
236 int Joystick(int player_nr)
239 struct joystick joy_ctrl;
241 struct joystick_control
249 int joystick_fd = stored_player[player_nr].joystick_fd;
250 int js_x,js_y, js_b1,js_b2;
251 int left, right, up, down;
254 if (joystick_status == JOYSTICK_OFF)
257 if (game_status == SETUPINPUT)
260 if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
263 if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
265 Error(ERR_WARN, "cannot read joystick device '%s'",
266 setup.input[player_nr].joy.device_name);
267 joystick_status = JOYSTICK_OFF;
278 js_b1 = joy_ctrl.buttons & 1;
279 js_b2 = joy_ctrl.buttons & 2;
282 left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
283 setup.input[player_nr].joy.xleft, js_x);
284 right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
285 setup.input[player_nr].joy.xright, js_x);
286 up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
287 setup.input[player_nr].joy.yupper, js_y);
288 down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
289 setup.input[player_nr].joy.ylower, js_y);
291 if (left > JOYSTICK_PERCENT)
293 else if (right > JOYSTICK_PERCENT)
295 if (up > JOYSTICK_PERCENT)
297 else if (down > JOYSTICK_PERCENT)
301 result |= JOY_BUTTON_1;
303 result |= JOY_BUTTON_2;
308 #else /* PLATFORM_MSDOS */
310 /* allegro global variables for joystick control */
311 extern int num_joysticks;
312 extern JOYSTICK_INFO joy[];
314 int Joystick(int player_nr)
316 int joystick_nr = stored_player[player_nr].joystick_fd;
319 if (joystick_status == JOYSTICK_OFF)
322 if (game_status == SETUPINPUT)
328 /* the allegro global variable 'num_joysticks' contains the number
329 of joysticks found at initialization under MS-DOS / Windows */
332 if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick)
337 if (joystick_nr >= num_joysticks ||
338 (game_status == PLAYING && !setup.input[player_nr].use_joystick))
341 if (joystick_nr >= num_joysticks)
349 if (joy[joystick_nr].stick[0].axis[0].d1)
351 else if (joy[joystick_nr].stick[0].axis[0].d2)
353 if (joy[joystick_nr].stick[0].axis[1].d1)
355 else if (joy[joystick_nr].stick[0].axis[1].d2)
358 if (joy[joystick_nr].button[0].b)
359 result |= JOY_BUTTON_1;
360 if (joy[joystick_nr].button[1].b)
361 result |= JOY_BUTTON_2;
365 #endif /* PLATFORM_MSDOS */
367 #endif /* !TARGET_SDL */
369 int JoystickButton(int player_nr)
371 static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 };
372 int joy_button = (Joystick(player_nr) & JOY_BUTTON);
377 if (last_joy_button[player_nr])
378 result = JOY_BUTTON_PRESSED;
380 result = JOY_BUTTON_NEW_PRESSED;
384 if (last_joy_button[player_nr])
385 result = JOY_BUTTON_NEW_RELEASED;
387 result = JOY_BUTTON_NOT_PRESSED;
390 last_joy_button[player_nr] = joy_button;
399 for (i=0; i<MAX_PLAYERS; i++)
403 if (!setup.input[i].use_joystick)
408 result |= Joystick(i);
414 int AnyJoystickButton()
419 for (i=0; i<MAX_PLAYERS; i++)
423 if (!setup.input[i].use_joystick)
428 result |= JoystickButton(i);
431 result = JoystickButton(i);
432 if (result != JOY_BUTTON_NOT_PRESSED)