+SDL_Joystick *Get_SDL_Joystick(int nr)
+{
+ return sdl_joystick[nr];
+}
+
+boolean Open_SDL_Joystick(int nr)
+{
+ if (nr < 0 || nr > MAX_PLAYERS)
+ return FALSE;
+
+ return ((sdl_joystick[nr] = SDL_JoystickOpen(nr)) == NULL ? FALSE : TRUE);
+}
+
+void Close_SDL_Joystick(int nr)
+{
+ if (nr < 0 || nr > MAX_PLAYERS)
+ return;
+
+ SDL_JoystickClose(sdl_joystick[nr]);
+}
+
+boolean Check_SDL_JoystickOpened(int nr)
+{
+ if (nr < 0 || nr > MAX_PLAYERS)
+ return FALSE;
+
+ return (SDL_JoystickOpened(nr) ? TRUE : FALSE);
+}
+
+void HandleJoystickEvent(Event *event)
+{
+ switch(event->type)
+ {
+ case SDL_JOYAXISMOTION:
+ if (event->jaxis.axis < 2)
+ {
+ sdl_js_axis[event->jaxis.which][event->jaxis.axis]= event->jaxis.value;
+
+#if 0
+ printf("js_%d %s-axis: %d\n",
+ event->jaxis.which,
+ (event->jaxis.axis == 0 ? "x" : "y"),
+ event->jaxis.value);
+#endif
+ }
+ break;
+
+ case SDL_JOYBUTTONDOWN:
+ if (event->jbutton.button < 2)
+ {
+ sdl_js_button[event->jbutton.which][event->jbutton.button] = TRUE;
+
+#if 0
+ printf("js_%d button %d: pressed\n",
+ event->jbutton.which,
+ event->jbutton.button);
+#endif
+ }
+ break;
+
+ case SDL_JOYBUTTONUP:
+ if (event->jbutton.button < 2)
+ {
+ sdl_js_button[event->jbutton.which][event->jbutton.button] = FALSE;
+
+#if 0
+ printf("js_%d button %d: released\n",
+ event->jbutton.which,
+ event->jbutton.button);
+#endif
+ }
+ break;
+
+ default:
+ break;
+ }
+}
+
+int Get_SDL_Joystick_Axis(int nr, int axis)
+{
+ if (nr < 0 || nr > MAX_PLAYERS)
+ return 0;
+
+ if (axis < 0 || axis > 1)
+ return 0;
+
+ return sdl_js_axis[nr][axis];
+}
+
+void CheckJoystickData()
+{
+}
+
+int Joystick(int player_nr)
+{
+ int joystick_nr = stored_player[player_nr].joystick_fd;
+ int js_x,js_y, js_b1,js_b2;
+ int left, right, up, down;
+ int result = 0;
+
+ if (joystick_status == JOYSTICK_OFF)
+ return 0;
+
+ if (game_status == SETUPINPUT)
+ return 0;
+
+ if (!setup.input[player_nr].use_joystick ||
+ !Check_SDL_JoystickOpened(joystick_nr))
+ return 0;
+
+ js_x = sdl_js_axis[joystick_nr][0];
+ js_y = sdl_js_axis[joystick_nr][1];
+
+ js_b1 = sdl_js_button[joystick_nr][0];
+ js_b2 = sdl_js_button[joystick_nr][1];
+
+
+
+#if 0
+ printf("JOYSTICK %d: js_x == %d, js_y == %d, js_b1 == %d, js_b2 == %d\n",
+ joystick_nr, js_x, js_y, js_b1, js_b2);
+#endif
+
+
+
+ left = JoystickPosition(setup.input[player_nr].joy.xmiddle,
+ setup.input[player_nr].joy.xleft, js_x);
+ right = JoystickPosition(setup.input[player_nr].joy.xmiddle,
+ setup.input[player_nr].joy.xright, js_x);
+ up = JoystickPosition(setup.input[player_nr].joy.ymiddle,
+ setup.input[player_nr].joy.yupper, js_y);
+ down = JoystickPosition(setup.input[player_nr].joy.ymiddle,
+ setup.input[player_nr].joy.ylower, js_y);
+
+ if (left > JOYSTICK_PERCENT)
+ result |= JOY_LEFT;
+ else if (right > JOYSTICK_PERCENT)
+ result |= JOY_RIGHT;
+ if (up > JOYSTICK_PERCENT)
+ result |= JOY_UP;
+ else if (down > JOYSTICK_PERCENT)
+ result |= JOY_DOWN;
+
+ if (js_b1)
+ result |= JOY_BUTTON_1;
+ if (js_b2)
+ result |= JOY_BUTTON_2;
+
+
+
+#if 0
+ printf("result == 0x%08x\n", result);
+#endif
+
+
+
+ return result;
+}
+
+#else /* !TARGET_SDL */
+
+void CheckJoystickData()
+{
+ int i;
+ int distance = 100;
+
+ for(i=0; i<MAX_PLAYERS; i++)
+ {
+ if (setup.input[i].joy.xmiddle <= distance)
+ setup.input[i].joy.xmiddle = distance;
+ if (setup.input[i].joy.ymiddle <= distance)
+ setup.input[i].joy.ymiddle = distance;
+
+ if (setup.input[i].joy.xleft >= setup.input[i].joy.xmiddle)
+ setup.input[i].joy.xleft = setup.input[i].joy.xmiddle - distance;
+ if (setup.input[i].joy.xright <= setup.input[i].joy.xmiddle)
+ setup.input[i].joy.xright = setup.input[i].joy.xmiddle + distance;
+
+ if (setup.input[i].joy.yupper >= setup.input[i].joy.ymiddle)
+ setup.input[i].joy.yupper = setup.input[i].joy.ymiddle - distance;
+ if (setup.input[i].joy.ylower <= setup.input[i].joy.ymiddle)
+ setup.input[i].joy.ylower = setup.input[i].joy.ymiddle + distance;
+ }
+}
+
+#if defined(PLATFORM_UNIX)
+int Joystick(int player_nr)