don't use hardcoded descriptions of joystick buttons/axes
authorBradley Bell <btb@icculus.org>
Mon, 4 Apr 2005 08:56:34 +0000 (08:56 +0000)
committerBradley Bell <btb@icculus.org>
Mon, 4 Apr 2005 08:56:34 +0000 (08:56 +0000)
ChangeLog
arch/sdl/joy.c
main/kconfig.c

index 5144e3a..3793e7b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2005-04-04  Bradley Bell  <btb@icculus.org>
 
+       * arch/sdl/joy.c, main/kconfig.c: don't use hardcoded descriptions
+       of joystick buttons/axes
+
        * arch/win32/midi.c: don't play midi if -nosound
 
        * arch/win32/mingw_init.c: remove redundant joy_init()
index 1287530..ee5e40c 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: joy.c,v 1.15 2004-11-23 00:16:10 btb Exp $ */
+/* $Id: joy.c,v 1.16 2005-04-04 08:56:34 btb Exp $ */
 /*
  *
  * SDL joystick support
@@ -19,6 +19,7 @@
 #include "console.h"
 #include "event.h"
 #include "text.h"
+#include "u_mem.h"
 
 #define MAX_JOYSTICKS 16
 
@@ -26,7 +27,8 @@
 #define MAX_BUTTONS_PER_JOYSTICK 16
 #define MAX_HATS_PER_JOYSTICK 4
 
-extern int joybutton_text[]; //from kconfig.c
+extern char *joybutton_text[]; //from kconfig.c
+extern char *joyaxis_text[]; //from kconfig.c
 
 char joy_present = 0;
 int num_joysticks = 0;
@@ -141,6 +143,7 @@ void joy_axis_handler(SDL_JoyAxisEvent *jae)
 int joy_init()
 {
        int i,j,n;
+       char temp[10];
 
        if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
                con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
@@ -187,17 +190,29 @@ int joy_init()
                        con_printf(CON_VERBOSE, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
 
                        for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
+                       {
+                               sprintf(temp, "J%d A%d", i + 1, j + 1);
+                               joyaxis_text[Joystick.n_axes] = d_strdup(temp);
                                SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
+                       }
                        for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
+                       {
+                               sprintf(temp, "J%d B%d", i + 1, j + 1);
+                               joybutton_text[Joystick.n_buttons] = d_strdup(temp);
                                SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
+                       }
                        for (j=0; j < SDL_Joysticks[num_joysticks].n_hats; j++)
                        {
                                SDL_Joysticks[num_joysticks].hat_map[j] = Joystick.n_buttons;
                                //a hat counts as four buttons
-                               joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_U:TNUM_HAT_U;
-                               joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_R:TNUM_HAT_R;
-                               joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_D:TNUM_HAT_D;
-                               joybutton_text[Joystick.n_buttons++] = j?TNUM_HAT2_L:TNUM_HAT_L;
+                               sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0202);
+                               joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
+                               sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0177);
+                               joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
+                               sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0200);
+                               joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
+                               sprintf(temp, "J%d H%d%c", i + 1, j + 1, 0201);
+                               joybutton_text[Joystick.n_buttons++] = d_strdup(temp);
                        }
 
                        num_joysticks++;
@@ -210,6 +225,7 @@ int joy_init()
        }
 
        joy_num_axes = Joystick.n_axes;
+       atexit(joy_close);
 
        return joy_present;
 }
@@ -218,6 +234,10 @@ void joy_close()
 {
        while (num_joysticks)
                SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
+       while (Joystick.n_axes--)
+               d_free(joyaxis_text[Joystick.n_axes]);
+       while (Joystick.n_buttons--)
+               d_free(joybutton_text[Joystick.n_buttons]);
 }
 
 void joy_get_pos(int *x, int *y)
index c871338..c12fb75 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: kconfig.c,v 1.34 2004-12-03 10:06:22 btb Exp $ */
+/* $Id: kconfig.c,v 1.35 2005-04-04 08:56:34 btb Exp $ */
 /*
 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
@@ -23,7 +23,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
 #endif
 
 #ifdef RCS
-static char rcsid[] = "$Id: kconfig.c,v 1.34 2004-12-03 10:06:22 btb Exp $";
+static char rcsid[] = "$Id: kconfig.c,v 1.35 2005-04-04 08:56:34 btb Exp $";
 #endif
 
 #ifdef WINDOWS
@@ -117,6 +117,9 @@ int invert_text[2] = { TNUM_N, TNUM_Y };
          -1, -1, -1, -1
        };
        int joyaxis_text[7] = { TNUM_X1, TNUM_Y1, TNUM_Z1, TNUM_UN, TNUM_P1,TNUM_R1,TNUM_YA1 };
+#elif defined(SDL_INPUT)
+char *joybutton_text[MAX_BUTTONS];
+char *joyaxis_text[JOY_MAX_AXES];
 #else
        int joybutton_text[28] = 
        { TNUM_BTN_1, TNUM_BTN_2, TNUM_BTN_3, TNUM_BTN_4,
@@ -850,6 +853,8 @@ int get_item_height(kc_item *item)
                        case BT_JOY_BUTTON:
 #ifdef USE_LINUX_JOY
                                sprintf(btext, "J%d B%d", j_button[item->value].joydev, j_Get_joydev_button_number(item->value));
+#elif defined(SDL_INPUT)
+                               strncpy(btext, joybutton_text[item->value], 10);
 #else
                                if ( joybutton_text[item->value] !=-1 )
                                        strncpy( btext, Text_string[ joybutton_text[item->value]  ], 10 );
@@ -860,6 +865,8 @@ int get_item_height(kc_item *item)
                        case BT_JOY_AXIS:
 #ifdef USE_LINUX_JOY
                                sprintf( btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number (item->value) );
+#elif defined(SDL_INPUT)
+                               strncpy(btext, joyaxis_text[item->value], 10);
 #else
                                strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
 #endif
@@ -1377,6 +1384,8 @@ WIN(DDGRUNLOCK(dd_grd_curcanv));
                        case BT_JOY_BUTTON:
 #ifdef USE_LINUX_JOY
                                sprintf(btext, "J%d B%d", j_button[item->value].joydev, j_Get_joydev_button_number(item->value));
+#elif defined(SDL_INPUT)
+                               strncpy(btext, joybutton_text[item->value], 10);
 #else
 # ifndef MACINTOSH
 #  ifdef WINDOWS
@@ -1400,6 +1409,8 @@ WIN(DDGRUNLOCK(dd_grd_curcanv));
                        case BT_JOY_AXIS:
 #ifdef USE_LINUX_JOY
                                sprintf(btext, "J%d A%d", j_axis[item->value].joydev, j_Get_joydev_axis_number(item->value));
+#elif defined(SDL_INPUT)
+                               strncpy(btext, joyaxis_text[item->value], 10);
 #else
                                strncpy(btext, Text_string[joyaxis_text[item->value]], 10);
 #endif
@@ -1597,7 +1608,8 @@ WIN(DDGRUNLOCK(dd_grd_curcanv));
                                }
                        }
                } else {
-                       for (i=0; i<MAX_BUTTONS; i++ )  {
+                       for (i = 0; i < MAX_BUTTONS; i++)
+                       {
                                if ( joy_get_button_state(i) )
                                        code = i;
                        }