X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fjoystick.c;h=d5a78a290465dfa71730ab29d00de1fe7ef23e92;hb=61c3da024802ecc0268bab42d7499fc0346e4fd3;hp=4bca889366acfa85edcd7e25305693a288587fb0;hpb=41e8d55b767c898f20c29a1b0b8d2ef8840be2f5;p=rocksndiamonds.git diff --git a/src/libgame/joystick.c b/src/libgame/joystick.c index 4bca8893..d5a78a29 100644 --- a/src/libgame/joystick.c +++ b/src/libgame/joystick.c @@ -1,7 +1,7 @@ /*********************************************************** * Artsoft Retro-Game Library * *----------------------------------------------------------* -* (c) 1995-2002 Artsoft Entertainment * +* (c) 1995-2006 Artsoft Entertainment * * Holger Schemel * * Detmolder Strasse 189 * * 33604 Bielefeld * @@ -19,20 +19,97 @@ #include "misc.h" -#define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0 -#define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1 +/* ========================================================================= */ +/* platform dependent joystick functions */ +/* ========================================================================= */ -#if 0 -static int joystick_device = 0; -char *joystick_device_name[MAX_PLAYERS] = +#if defined(PLATFORM_UNIX) && !defined(TARGET_SDL) +void UnixInitJoysticks() { - DEV_JOYSTICK_0, - DEV_JOYSTICK_1, - DEV_JOYSTICK_2, - DEV_JOYSTICK_3 -}; + static boolean unix_joystick_subsystem_initialized = FALSE; + boolean print_warning = !unix_joystick_subsystem_initialized; + int i; + + for (i = 0; i < MAX_PLAYERS; i++) + { + char *device_name = setup.input[i].joy.device_name; + + /* this allows subsequent calls to 'InitJoysticks' for re-initialization */ + if (joystick.fd[i] != -1) + { + close(joystick.fd[i]); + joystick.fd[i] = -1; + } + + if (!setup.input[i].use_joystick) + continue; + + if (access(device_name, R_OK) != 0) + { + if (print_warning) + Error(ERR_WARN, "cannot access joystick device '%s'", device_name); + + continue; + } + + if ((joystick.fd[i] = open(device_name, O_RDONLY)) < 0) + { + if (print_warning) + Error(ERR_WARN, "cannot open joystick device '%s'", device_name); + + continue; + } + + joystick.status = JOYSTICK_ACTIVATED; + } + + unix_joystick_subsystem_initialized = TRUE; +} + +boolean UnixReadJoystick(int fd, int *x, int *y, boolean *b1, boolean *b2) +{ +#if defined(PLATFORM_FREEBSD) + struct joystick joy_ctrl; +#else + struct joystick_control + { + int buttons; + int x; + int y; + } joy_ctrl; +#endif + + if (read(fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl)) + return FALSE; + + if (x != NULL) + *x = joy_ctrl.x; + if (y != NULL) + *y = joy_ctrl.y; + +#if defined(PLATFORM_FREEBSD) + if (b1 != NULL) + *b1 = joy_ctrl.b1; + if (b2 != NULL) + *b2 = joy_ctrl.b2; +#else + if (b1 != NULL) + *b1 = joy_ctrl.buttons & 1; + if (b2 != NULL) + *b2 = joy_ctrl.buttons & 2; #endif + return TRUE; +} +#endif /* PLATFORM_UNIX && !TARGET_SDL */ + + +/* ========================================================================= */ +/* platform independent joystick functions */ +/* ========================================================================= */ + +#define TRANSLATE_JOYSYMBOL_TO_JOYNAME 0 +#define TRANSLATE_JOYNAME_TO_JOYSYMBOL 1 void translate_joyname(int *joysymbol, char **name, int mode) { @@ -56,7 +133,7 @@ void translate_joyname(int *joysymbol, char **name, int mode) { *name = "[undefined]"; - for (i=0; i<6; i++) + for (i = 0; i < 6; i++) { if (*joysymbol == translate_joy[i].joysymbol) { @@ -69,9 +146,9 @@ void translate_joyname(int *joysymbol, char **name, int mode) { *joysymbol = 0; - for (i=0; i<6; i++) + for (i = 0; i < 6; i++) { - if (strcmp(*name, translate_joy[i].name) == 0) + if (strEqual(*name, translate_joy[i].name)) { *joysymbol = translate_joy[i].joysymbol; break; @@ -129,208 +206,34 @@ char *getDeviceNameFromJoystickNr(int joystick_nr) joystick_device_name[joystick_nr] : ""); } -#if !defined(PLATFORM_MSDOS) -static int JoystickPosition(int middle, int margin, int actual) +static int JoystickPositionPercent(int center, int border, int actual) { - long range, pos; - int percentage; + int range, position; + int percent; - if (margin < middle && actual > middle) + if (border < center && actual > center) return 0; - if (margin > middle && actual < middle) + if (border > center && actual < center) return 0; - range = ABS(margin - middle); - pos = ABS(actual - middle); - percentage = (int)(pos * 100 / range); - - if (percentage > 100) - percentage = 100; + range = ABS(border - center); + position = ABS(actual - center); - return percentage; -} -#endif + percent = (int)(position * 100 / range); -#if defined(TARGET_SDL) + if (percent > 100) + percent = 100; -static SDL_Joystick *sdl_joystick[MAX_PLAYERS] = { NULL, NULL, NULL, NULL }; -static int sdl_js_axis[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; -static int sdl_js_button[MAX_PLAYERS][2] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; - -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]; + return percent; } -void CheckJoystickData() -{ -} - -int Joystick(int player_nr) -{ -#if 0 - int joystick_nr = stored_player[player_nr].joystick_fd; -#else - int joystick_nr = joystick.fd[player_nr]; -#endif - int js_x,js_y, js_b1,js_b2; - int left, right, up, down; - int result = 0; - - if (joystick.status != JOYSTICK_ACTIVATED) - 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= 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) @@ -343,62 +246,37 @@ void CheckJoystickData() } } -#if defined(PLATFORM_UNIX) int Joystick(int player_nr) { -#if defined(PLATFORM_FREEBSD) - struct joystick joy_ctrl; -#else - struct joystick_control - { - int buttons; - int x; - int y; - } joy_ctrl; -#endif - -#if 0 - int joystick_fd = stored_player[player_nr].joystick_fd; -#else int joystick_fd = joystick.fd[player_nr]; -#endif - int js_x,js_y, js_b1,js_b2; + int js_x, js_y; + boolean js_b1, js_b2; int left, right, up, down; - int result = 0; + int result = JOY_NO_ACTION; if (joystick.status != JOYSTICK_ACTIVATED) - return 0; + return JOY_NO_ACTION; if (joystick_fd < 0 || !setup.input[player_nr].use_joystick) - return 0; + return JOY_NO_ACTION; - if (read(joystick_fd, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl)) + if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2)) { Error(ERR_WARN, "cannot read joystick device '%s'", setup.input[player_nr].joy.device_name); + joystick.status = JOYSTICK_NOT_AVAILABLE; - return 0; + return JOY_NO_ACTION; } - js_x = joy_ctrl.x; - js_y = joy_ctrl.y; - -#if defined(PLATFORM_FREEBSD) - js_b1 = joy_ctrl.b1; - js_b2 = joy_ctrl.b2; -#else - js_b1 = joy_ctrl.buttons & 1; - js_b2 = joy_ctrl.buttons & 2; -#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); + left = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle, + setup.input[player_nr].joy.xleft, js_x); + right = JoystickPositionPercent(setup.input[player_nr].joy.xmiddle, + setup.input[player_nr].joy.xright, js_x); + up = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle, + setup.input[player_nr].joy.yupper, js_y); + down = JoystickPositionPercent(setup.input[player_nr].joy.ymiddle, + setup.input[player_nr].joy.ylower, js_y); if (left > JOYSTICK_PERCENT) result |= JOY_LEFT; @@ -417,74 +295,6 @@ int Joystick(int player_nr) return result; } -#else /* PLATFORM_MSDOS */ - -/* allegro global variables for joystick control */ -extern int num_joysticks; -extern JOYSTICK_INFO joy[]; - -int Joystick(int player_nr) -{ -#if 0 - int joystick_nr = stored_player[player_nr].joystick_fd; -#else - int joystick_nr = joystick.fd[player_nr]; -#endif - int result = 0; - - if (joystick.status != JOYSTICK_ACTIVATED) - return 0; - - if (joystick_nr < 0) - return 0; - - /* the allegro global variable 'num_joysticks' contains the number - of joysticks found at initialization under MS-DOS / Windows */ - -#if 0 - if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick) - return 0; -#else - -#if 1 - /* - if (joystick_nr >= num_joysticks || - (game_status == PLAYING && !setup.input[player_nr].use_joystick)) - return 0; - */ - - if (joystick_nr >= num_joysticks || !setup.input[player_nr].use_joystick) - return 0; - -#else - if (joystick_nr >= num_joysticks) - return 0; -#endif - -#endif - - poll_joystick(); - - if (joy[joystick_nr].stick[0].axis[0].d1) - result |= JOY_LEFT; - else if (joy[joystick_nr].stick[0].axis[0].d2) - result |= JOY_RIGHT; - if (joy[joystick_nr].stick[0].axis[1].d1) - result |= JOY_UP; - else if (joy[joystick_nr].stick[0].axis[1].d2) - result |= JOY_DOWN; - - if (joy[joystick_nr].button[0].b) - result |= JOY_BUTTON_1; - if (joy[joystick_nr].button[1].b) - result |= JOY_BUTTON_2; - - return result; -} -#endif /* PLATFORM_MSDOS */ - -#endif /* !TARGET_SDL */ - int JoystickButton(int player_nr) { static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 }; @@ -515,17 +325,8 @@ int AnyJoystick() int i; int result = 0; - for (i=0; i