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