src/joystick/beos/SDL_bejoystick.cc
author Sam Lantinga
Sat, 06 Oct 2012 12:16:32 -0700
changeset 6566 dd7e57847ea9
parent 6138 4c64952a58fb
child 6698 28ab2ef7bfc9
permissions -rw-r--r--
Add flags to the vidmode debug output
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #ifdef SDL_JOYSTICK_BEOS
    24 
    25 /* This is the system specific header for the SDL joystick API */
    26 
    27 #include <be/support/String.h>
    28 #include <be/device/Joystick.h>
    29 
    30 extern "C"
    31 {
    32 
    33 #include "SDL_joystick.h"
    34 #include "../SDL_sysjoystick.h"
    35 #include "../SDL_joystick_c.h"
    36 
    37 
    38 /* The maximum number of joysticks we'll detect */
    39 #define MAX_JOYSTICKS	16
    40 
    41 /* A list of available joysticks */
    42     static char *SDL_joyport[MAX_JOYSTICKS];
    43     static char *SDL_joyname[MAX_JOYSTICKS];
    44 
    45 /* The private structure used to keep track of a joystick */
    46     struct joystick_hwdata
    47     {
    48         BJoystick *stick;
    49         uint8 *new_hats;
    50         int16 *new_axes;
    51     };
    52 
    53 /* Function to scan the system for joysticks.
    54  * This function should set SDL_numjoysticks to the number of available
    55  * joysticks.  Joystick 0 should be the system default joystick.
    56  * It should return 0, or -1 on an unrecoverable fatal error.
    57  */
    58     int SDL_SYS_JoystickInit(void)
    59     {
    60         BJoystick joystick;
    61         int numjoysticks;
    62         int i;
    63         int32 nports;
    64         char name[B_OS_NAME_LENGTH];
    65 
    66         /* Search for attached joysticks */
    67           nports = joystick.CountDevices();
    68           numjoysticks = 0;
    69           SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
    70           SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
    71         for (i = 0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
    72         {
    73             if (joystick.GetDeviceName(i, name) == B_OK) {
    74                 if (joystick.Open(name) != B_ERROR) {
    75                     BString stick_name;
    76                       joystick.GetControllerName(&stick_name);
    77                       SDL_joyport[numjoysticks] = strdup(name);
    78                       SDL_joyname[numjoysticks] = strdup(stick_name.String());
    79                       numjoysticks++;
    80                       joystick.Close();
    81                 }
    82             }
    83         }
    84         return (numjoysticks);
    85     }
    86 
    87 /* Function to get the device-dependent name of a joystick */
    88     const char *SDL_SYS_JoystickName(int index)
    89     {
    90         return SDL_joyname[index];
    91     }
    92 
    93 /* Function to open a joystick for use.
    94    The joystick to open is specified by the index field of the joystick.
    95    This should fill the nbuttons and naxes fields of the joystick structure.
    96    It returns 0, or -1 if there is an error.
    97  */
    98     int SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
    99     {
   100         BJoystick *stick;
   101 
   102         /* Create the joystick data structure */
   103         joystick->hwdata = (struct joystick_hwdata *)
   104             SDL_malloc(sizeof(*joystick->hwdata));
   105         if (joystick->hwdata == NULL) {
   106             SDL_OutOfMemory();
   107             return (-1);
   108         }
   109         SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
   110         stick = new BJoystick;
   111         joystick->hwdata->stick = stick;
   112 
   113         /* Open the requested joystick for use */
   114         if (stick->Open(SDL_joyport[joystick->index]) == B_ERROR) {
   115             SDL_SetError("Unable to open joystick");
   116             SDL_SYS_JoystickClose(joystick);
   117             return (-1);
   118         }
   119 
   120         /* Set the joystick to calibrated mode */
   121         stick->EnableCalibration();
   122 
   123         /* Get the number of buttons, hats, and axes on the joystick */
   124         joystick->nbuttons = stick->CountButtons();
   125         joystick->naxes = stick->CountAxes();
   126         joystick->nhats = stick->CountHats();
   127 
   128         joystick->hwdata->new_axes = (int16 *)
   129             SDL_malloc(joystick->naxes * sizeof(int16));
   130         joystick->hwdata->new_hats = (uint8 *)
   131             SDL_malloc(joystick->nhats * sizeof(uint8));
   132         if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
   133             SDL_OutOfMemory();
   134             SDL_SYS_JoystickClose(joystick);
   135             return (-1);
   136         }
   137 
   138         /* We're done! */
   139         return (0);
   140     }
   141 
   142 /* Function to update the state of a joystick - called as a device poll.
   143  * This function shouldn't update the joystick structure directly,
   144  * but instead should call SDL_PrivateJoystick*() to deliver events
   145  * and update joystick device state.
   146  */
   147     void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   148     {
   149         static const Uint8 hat_map[9] = {
   150             SDL_HAT_CENTERED,
   151             SDL_HAT_UP,
   152             SDL_HAT_RIGHTUP,
   153             SDL_HAT_RIGHT,
   154             SDL_HAT_RIGHTDOWN,
   155             SDL_HAT_DOWN,
   156             SDL_HAT_LEFTDOWN,
   157             SDL_HAT_LEFT,
   158             SDL_HAT_LEFTUP
   159         };
   160         const int JITTER = (32768 / 10);        /* 10% jitter threshold (ok?) */
   161 
   162         BJoystick *stick;
   163         int i, change;
   164         int16 *axes;
   165         uint8 *hats;
   166         uint32 buttons;
   167 
   168         /* Set up data pointers */
   169         stick = joystick->hwdata->stick;
   170         axes = joystick->hwdata->new_axes;
   171         hats = joystick->hwdata->new_hats;
   172 
   173         /* Get the new joystick state */
   174         stick->Update();
   175         stick->GetAxisValues(axes);
   176         stick->GetHatValues(hats);
   177         buttons = stick->ButtonValues();
   178 
   179         /* Generate axis motion events */
   180         for (i = 0; i < joystick->naxes; ++i) {
   181             change = ((int32) axes[i] - joystick->axes[i]);
   182             if ((change > JITTER) || (change < -JITTER)) {
   183                 SDL_PrivateJoystickAxis(joystick, i, axes[i]);
   184             }
   185         }
   186 
   187         /* Generate hat change events */
   188         for (i = 0; i < joystick->nhats; ++i) {
   189             if (hats[i] != joystick->hats[i]) {
   190                 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
   191             }
   192         }
   193 
   194         /* Generate button events */
   195         for (i = 0; i < joystick->nbuttons; ++i) {
   196             if ((buttons & 0x01) != joystick->buttons[i]) {
   197                 SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
   198             }
   199             buttons >>= 1;
   200         }
   201     }
   202 
   203 /* Function to close a joystick after use */
   204     void SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   205     {
   206         if (joystick->hwdata) {
   207             joystick->hwdata->stick->Close();
   208             delete joystick->hwdata->stick;
   209             if (joystick->hwdata->new_hats) {
   210                 SDL_free(joystick->hwdata->new_hats);
   211             }
   212             if (joystick->hwdata->new_axes) {
   213                 SDL_free(joystick->hwdata->new_axes);
   214             }
   215             SDL_free(joystick->hwdata);
   216             joystick->hwdata = NULL;
   217         }
   218     }
   219 
   220 /* Function to perform any system-specific joystick related cleanup */
   221     void SDL_SYS_JoystickQuit(void)
   222     {
   223         int i;
   224 
   225         for (i = 0; SDL_joyport[i]; ++i) {
   226             SDL_free(SDL_joyport[i]);
   227         }
   228         SDL_joyport[0] = NULL;
   229 
   230         for (i = 0; SDL_joyname[i]; ++i) {
   231             SDL_free(SDL_joyname[i]);
   232         }
   233         SDL_joyname[0] = NULL;
   234     }
   235 
   236 };                              // extern "C"
   237 
   238 #endif /* SDL_JOYSTICK_BEOS */
   239 /* vi: set ts=4 sw=4 expandtab: */