src/joystick/haiku/SDL_haikujoystick.cc
changeset 7981 6d538bc1b395
parent 7719 31b5f9ff36ca
child 8093 b43765095a6f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/haiku/SDL_haikujoystick.cc	Thu Nov 14 11:51:24 2013 -0500
     1.3 @@ -0,0 +1,281 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "SDL_config.h"
    1.25 +
    1.26 +#ifdef SDL_JOYSTICK_HAIKU
    1.27 +
    1.28 +/* This is the system specific header for the SDL joystick API */
    1.29 +
    1.30 +#include <os/support/String.h>
    1.31 +#include <os/device/Joystick.h>
    1.32 +
    1.33 +extern "C"
    1.34 +{
    1.35 +
    1.36 +#include "SDL_joystick.h"
    1.37 +#include "../SDL_sysjoystick.h"
    1.38 +#include "../SDL_joystick_c.h"
    1.39 +
    1.40 +
    1.41 +/* The maximum number of joysticks we'll detect */
    1.42 +#define MAX_JOYSTICKS	16
    1.43 +
    1.44 +/* A list of available joysticks */
    1.45 +    static char *SDL_joyport[MAX_JOYSTICKS];
    1.46 +    static char *SDL_joyname[MAX_JOYSTICKS];
    1.47 +
    1.48 +/* The private structure used to keep track of a joystick */
    1.49 +    struct joystick_hwdata
    1.50 +    {
    1.51 +        BJoystick *stick;
    1.52 +        uint8 *new_hats;
    1.53 +        int16 *new_axes;
    1.54 +    };
    1.55 +
    1.56 +    static int SDL_SYS_numjoysticks = 0;
    1.57 +
    1.58 +/* Function to scan the system for joysticks.
    1.59 + * This function should set SDL_numjoysticks to the number of available
    1.60 + * joysticks.  Joystick 0 should be the system default joystick.
    1.61 + * It should return 0, or -1 on an unrecoverable fatal error.
    1.62 + */
    1.63 +    int SDL_SYS_JoystickInit(void)
    1.64 +    {
    1.65 +        BJoystick joystick;
    1.66 +        int i;
    1.67 +        int32 nports;
    1.68 +        char name[B_OS_NAME_LENGTH];
    1.69 +
    1.70 +        /* Search for attached joysticks */
    1.71 +          nports = joystick.CountDevices();
    1.72 +          SDL_SYS_numjoysticks = 0;
    1.73 +          SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
    1.74 +          SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
    1.75 +        for (i = 0; (SDL_SYS_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i)
    1.76 +        {
    1.77 +            if (joystick.GetDeviceName(i, name) == B_OK) {
    1.78 +                if (joystick.Open(name) != B_ERROR) {
    1.79 +                    BString stick_name;
    1.80 +                      joystick.GetControllerName(&stick_name);
    1.81 +                      SDL_joyport[SDL_SYS_numjoysticks] = strdup(name);
    1.82 +                      SDL_joyname[SDL_SYS_numjoysticks] = strdup(stick_name.String());
    1.83 +                      SDL_SYS_numjoysticks++;
    1.84 +                      joystick.Close();
    1.85 +                }
    1.86 +            }
    1.87 +        }
    1.88 +        return (SDL_SYS_numjoysticks);
    1.89 +    }
    1.90 +
    1.91 +    int SDL_SYS_NumJoysticks()
    1.92 +    {
    1.93 +        return SDL_SYS_numjoysticks;
    1.94 +    }
    1.95 +
    1.96 +    void SDL_SYS_JoystickDetect()
    1.97 +    {
    1.98 +    }
    1.99 +
   1.100 +    SDL_bool SDL_SYS_JoystickNeedsPolling()
   1.101 +    {
   1.102 +        return SDL_FALSE;
   1.103 +    }
   1.104 +
   1.105 +/* Function to get the device-dependent name of a joystick */
   1.106 +    const char *SDL_SYS_JoystickNameForDeviceIndex(int device_index)
   1.107 +    {
   1.108 +        return SDL_joyname[device_index];
   1.109 +    }
   1.110 +
   1.111 +/* Function to perform the mapping from device index to the instance id for this index */
   1.112 +    SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
   1.113 +    {
   1.114 +        return device_index;
   1.115 +    }
   1.116 +
   1.117 +/* Function to open a joystick for use.
   1.118 +   The joystick to open is specified by the index field of the joystick.
   1.119 +   This should fill the nbuttons and naxes fields of the joystick structure.
   1.120 +   It returns 0, or -1 if there is an error.
   1.121 + */
   1.122 +    int SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
   1.123 +    {
   1.124 +        BJoystick *stick;
   1.125 +
   1.126 +        /* Create the joystick data structure */
   1.127 +        joystick->instance_id = device_index;
   1.128 +        joystick->hwdata = (struct joystick_hwdata *)
   1.129 +            SDL_malloc(sizeof(*joystick->hwdata));
   1.130 +        if (joystick->hwdata == NULL) {
   1.131 +            return SDL_OutOfMemory();
   1.132 +        }
   1.133 +        SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
   1.134 +        stick = new BJoystick;
   1.135 +        joystick->hwdata->stick = stick;
   1.136 +
   1.137 +        /* Open the requested joystick for use */
   1.138 +        if (stick->Open(SDL_joyport[device_index]) == B_ERROR) {
   1.139 +            SDL_SYS_JoystickClose(joystick);
   1.140 +            return SDL_SetError("Unable to open joystick");
   1.141 +        }
   1.142 +
   1.143 +        /* Set the joystick to calibrated mode */
   1.144 +        stick->EnableCalibration();
   1.145 +
   1.146 +        /* Get the number of buttons, hats, and axes on the joystick */
   1.147 +        joystick->nbuttons = stick->CountButtons();
   1.148 +        joystick->naxes = stick->CountAxes();
   1.149 +        joystick->nhats = stick->CountHats();
   1.150 +
   1.151 +        joystick->hwdata->new_axes = (int16 *)
   1.152 +            SDL_malloc(joystick->naxes * sizeof(int16));
   1.153 +        joystick->hwdata->new_hats = (uint8 *)
   1.154 +            SDL_malloc(joystick->nhats * sizeof(uint8));
   1.155 +        if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
   1.156 +            SDL_SYS_JoystickClose(joystick);
   1.157 +            return SDL_OutOfMemory();
   1.158 +        }
   1.159 +
   1.160 +        /* We're done! */
   1.161 +        return (0);
   1.162 +    }
   1.163 +
   1.164 +/* Function to determine is this joystick is attached to the system right now */
   1.165 +    SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   1.166 +    {
   1.167 +        return SDL_TRUE;
   1.168 +    }
   1.169 +
   1.170 +/* Function to update the state of a joystick - called as a device poll.
   1.171 + * This function shouldn't update the joystick structure directly,
   1.172 + * but instead should call SDL_PrivateJoystick*() to deliver events
   1.173 + * and update joystick device state.
   1.174 + */
   1.175 +    void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   1.176 +    {
   1.177 +        static const Uint8 hat_map[9] = {
   1.178 +            SDL_HAT_CENTERED,
   1.179 +            SDL_HAT_UP,
   1.180 +            SDL_HAT_RIGHTUP,
   1.181 +            SDL_HAT_RIGHT,
   1.182 +            SDL_HAT_RIGHTDOWN,
   1.183 +            SDL_HAT_DOWN,
   1.184 +            SDL_HAT_LEFTDOWN,
   1.185 +            SDL_HAT_LEFT,
   1.186 +            SDL_HAT_LEFTUP
   1.187 +        };
   1.188 +        const int JITTER = (32768 / 10);        /* 10% jitter threshold (ok?) */
   1.189 +
   1.190 +        BJoystick *stick;
   1.191 +        int i, change;
   1.192 +        int16 *axes;
   1.193 +        uint8 *hats;
   1.194 +        uint32 buttons;
   1.195 +
   1.196 +        /* Set up data pointers */
   1.197 +        stick = joystick->hwdata->stick;
   1.198 +        axes = joystick->hwdata->new_axes;
   1.199 +        hats = joystick->hwdata->new_hats;
   1.200 +
   1.201 +        /* Get the new joystick state */
   1.202 +        stick->Update();
   1.203 +        stick->GetAxisValues(axes);
   1.204 +        stick->GetHatValues(hats);
   1.205 +        buttons = stick->ButtonValues();
   1.206 +
   1.207 +        /* Generate axis motion events */
   1.208 +        for (i = 0; i < joystick->naxes; ++i) {
   1.209 +            change = ((int32) axes[i] - joystick->axes[i]);
   1.210 +            if ((change > JITTER) || (change < -JITTER)) {
   1.211 +                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
   1.212 +            }
   1.213 +        }
   1.214 +
   1.215 +        /* Generate hat change events */
   1.216 +        for (i = 0; i < joystick->nhats; ++i) {
   1.217 +            if (hats[i] != joystick->hats[i]) {
   1.218 +                SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
   1.219 +            }
   1.220 +        }
   1.221 +
   1.222 +        /* Generate button events */
   1.223 +        for (i = 0; i < joystick->nbuttons; ++i) {
   1.224 +            if ((buttons & 0x01) != joystick->buttons[i]) {
   1.225 +                SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
   1.226 +            }
   1.227 +            buttons >>= 1;
   1.228 +        }
   1.229 +    }
   1.230 +
   1.231 +/* Function to close a joystick after use */
   1.232 +    void SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.233 +    {
   1.234 +        if (joystick->hwdata) {
   1.235 +            joystick->hwdata->stick->Close();
   1.236 +            delete joystick->hwdata->stick;
   1.237 +            SDL_free(joystick->hwdata->new_hats);
   1.238 +            SDL_free(joystick->hwdata->new_axes);
   1.239 +            SDL_free(joystick->hwdata);
   1.240 +            joystick->hwdata = NULL;
   1.241 +        }
   1.242 +    }
   1.243 +
   1.244 +/* Function to perform any system-specific joystick related cleanup */
   1.245 +    void SDL_SYS_JoystickQuit(void)
   1.246 +    {
   1.247 +        int i;
   1.248 +
   1.249 +        for (i = 0; SDL_joyport[i]; ++i) {
   1.250 +            SDL_free(SDL_joyport[i]);
   1.251 +        }
   1.252 +        SDL_joyport[0] = NULL;
   1.253 +
   1.254 +        for (i = 0; SDL_joyname[i]; ++i) {
   1.255 +            SDL_free(SDL_joyname[i]);
   1.256 +        }
   1.257 +        SDL_joyname[0] = NULL;
   1.258 +    }
   1.259 +
   1.260 +    SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
   1.261 +    {
   1.262 +        SDL_JoystickGUID guid;
   1.263 +        /* the GUID is just the first 16 chars of the name for now */
   1.264 +        const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
   1.265 +        SDL_zero( guid );
   1.266 +        SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.267 +        return guid;
   1.268 +    }
   1.269 +
   1.270 +    SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   1.271 +    {
   1.272 +        SDL_JoystickGUID guid;
   1.273 +        /* the GUID is just the first 16 chars of the name for now */
   1.274 +        const char *name = joystick->name;
   1.275 +        SDL_zero( guid );
   1.276 +        SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   1.277 +        return guid;
   1.278 +    }
   1.279 +
   1.280 +};                              // extern "C"
   1.281 +
   1.282 +#endif /* SDL_JOYSTICK_HAIKU */
   1.283 +
   1.284 +/* vi: set ts=4 sw=4 expandtab: */