X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Flibgame%2Fjoystick.c;h=dfe1cca4ab1e520be35cf402393fd373135555d6;hp=d5a78a290465dfa71730ab29d00de1fe7ef23e92;hb=e02c111162d5d8a13715de71d93494120380ec9b;hpb=61c3da024802ecc0268bab42d7499fc0346e4fd3 diff --git a/src/libgame/joystick.c b/src/libgame/joystick.c index d5a78a29..dfe1cca4 100644 --- a/src/libgame/joystick.c +++ b/src/libgame/joystick.c @@ -1,15 +1,13 @@ -/*********************************************************** -* Artsoft Retro-Game Library * -*----------------------------------------------------------* -* (c) 1995-2006 Artsoft Entertainment * -* Holger Schemel * -* Detmolder Strasse 189 * -* 33604 Bielefeld * -* Germany * -* e-mail: info@artsoft.org * -*----------------------------------------------------------* -* joystick.c * -***********************************************************/ +// ============================================================================ +// Artsoft Retro-Game Library +// ---------------------------------------------------------------------------- +// (c) 1995-2014 by Artsoft Entertainment +// Holger Schemel +// info@artsoft.org +// http://www.artsoft.org/ +// ---------------------------------------------------------------------------- +// joystick.c +// ============================================================================ #if defined(PLATFORM_FREEBSD) #include @@ -19,91 +17,6 @@ #include "misc.h" -/* ========================================================================= */ -/* platform dependent joystick functions */ -/* ========================================================================= */ - -#if defined(PLATFORM_UNIX) && !defined(TARGET_SDL) -void UnixInitJoysticks() -{ - 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 */ /* ========================================================================= */ @@ -206,6 +119,39 @@ char *getDeviceNameFromJoystickNr(int joystick_nr) joystick_device_name[joystick_nr] : ""); } +char *getFormattedJoystickName(const char *name_raw) +{ + static char name[MAX_JOYSTICK_NAME_LEN + 1]; + boolean name_skip_space = TRUE; + int i, j; + + if (name_raw == NULL) + name_raw = "(unknown joystick)"; + + // copy joystick name, cutting leading and multiple spaces + for (i = 0, j = 0; i < strlen(name_raw) && i < MAX_JOYSTICK_NAME_LEN; i++) + { + if (name_raw[i] != ' ') + { + name[j++] = name_raw[i]; + name_skip_space = FALSE; + } + else if (!name_skip_space) + { + name[j++] = name_raw[i]; + name_skip_space = TRUE; + } + } + + // cut trailing space + if (j > 0 && name[j - 1] == ' ') + j--; + + name[j] = '\0'; + + return name; +} + static int JoystickPositionPercent(int center, int border, int actual) { int range, position; @@ -246,21 +192,24 @@ void CheckJoystickData() } } -int Joystick(int player_nr) +int JoystickExt(int player_nr, boolean use_as_joystick_nr) { - int joystick_fd = joystick.fd[player_nr]; + int joystick_nr = joystick.nr[player_nr]; int js_x, js_y; boolean js_b1, js_b2; int left, right, up, down; int result = JOY_NO_ACTION; + if (use_as_joystick_nr) + joystick_nr = player_nr; + if (joystick.status != JOYSTICK_ACTIVATED) return JOY_NO_ACTION; - if (joystick_fd < 0 || !setup.input[player_nr].use_joystick) + if (joystick_nr < 0) return JOY_NO_ACTION; - if (!ReadJoystick(joystick_fd, &js_x, &js_y, &js_b1, &js_b2)) + if (!ReadJoystick(joystick_nr, &js_x, &js_y, &js_b1, &js_b2)) { Error(ERR_WARN, "cannot read joystick device '%s'", setup.input[player_nr].joy.device_name); @@ -295,10 +244,15 @@ int Joystick(int player_nr) return result; } -int JoystickButton(int player_nr) +int Joystick(int player_nr) +{ + return JoystickExt(player_nr, FALSE); +} + +int JoystickButtonExt(int player_nr, boolean use_as_joystick_nr) { static int last_joy_button[MAX_PLAYERS] = { 0, 0, 0, 0 }; - int joy_button = (Joystick(player_nr) & JOY_BUTTON); + int joy_button = (JoystickExt(player_nr, use_as_joystick_nr) & JOY_BUTTON); int result; if (joy_button) @@ -320,13 +274,18 @@ int JoystickButton(int player_nr) return result; } +int JoystickButton(int player_nr) +{ + return JoystickButtonExt(player_nr, FALSE); +} + int AnyJoystick() { int i; int result = 0; for (i = 0; i < MAX_PLAYERS; i++) - result |= Joystick(i); + result |= JoystickExt(i, TRUE); return result; } @@ -338,7 +297,7 @@ int AnyJoystickButton() for (i = 0; i < MAX_PLAYERS; i++) { - result = JoystickButton(i); + result = JoystickButtonExt(i, TRUE); if (result != JOY_BUTTON_NOT_PRESSED) break; }