src/joystick/beos/SDL_bejoystick.cc
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1635 92947e3a18db
child 2859 99210400e8b9
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1403
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1635
    24
#ifdef SDL_JOYSTICK_BEOS
slouken@1635
    25
slouken@0
    26
/* This is the system specific header for the SDL joystick API */
slouken@0
    27
slouken@0
    28
#include <be/support/String.h>
slouken@0
    29
#include <be/device/Joystick.h>
slouken@0
    30
slouken@1895
    31
extern "C"
slouken@1895
    32
{
slouken@0
    33
slouken@0
    34
#include "SDL_joystick.h"
slouken@1361
    35
#include "../SDL_sysjoystick.h"
slouken@1361
    36
#include "../SDL_joystick_c.h"
slouken@0
    37
slouken@0
    38
slouken@0
    39
/* The maximum number of joysticks we'll detect */
slouken@1895
    40
#define MAX_JOYSTICKS	16
slouken@0
    41
slouken@0
    42
/* A list of available joysticks */
slouken@1895
    43
    static char *SDL_joyport[MAX_JOYSTICKS];
slouken@1895
    44
    static char *SDL_joyname[MAX_JOYSTICKS];
slouken@0
    45
slouken@0
    46
/* The private structure used to keep track of a joystick */
slouken@1895
    47
    struct joystick_hwdata
slouken@1895
    48
    {
slouken@1895
    49
        BJoystick *stick;
slouken@1895
    50
        uint8 *new_hats;
slouken@1895
    51
        int16 *new_axes;
slouken@1895
    52
    };
slouken@0
    53
slouken@0
    54
/* Function to scan the system for joysticks.
slouken@0
    55
 * This function should set SDL_numjoysticks to the number of available
slouken@0
    56
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@0
    57
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@0
    58
 */
slouken@1895
    59
    int SDL_SYS_JoystickInit(void)
slouken@1895
    60
    {
slouken@1895
    61
        BJoystick joystick;
slouken@1895
    62
        int numjoysticks;
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@1895
    69
          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@1895
    72
        for (i = 0; (SDL_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@1895
    78
                      SDL_joyport[numjoysticks] = strdup(name);
slouken@1895
    79
                      SDL_joyname[numjoysticks] = strdup(stick_name.String());
slouken@1895
    80
                      numjoysticks++;
slouken@1895
    81
                      joystick.Close();
slouken@1895
    82
                }
slouken@1895
    83
            }
slouken@1895
    84
        }
slouken@1895
    85
        return (numjoysticks);
slouken@1895
    86
    }
slouken@0
    87
slouken@0
    88
/* Function to get the device-dependent name of a joystick */
slouken@1895
    89
    const char *SDL_SYS_JoystickName(int index)
slouken@1895
    90
    {
slouken@1895
    91
        return SDL_joyname[index];
slouken@1895
    92
    }
slouken@0
    93
slouken@0
    94
/* Function to open a joystick for use.
slouken@0
    95
   The joystick to open is specified by the index field of the joystick.
slouken@0
    96
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@0
    97
   It returns 0, or -1 if there is an error.
slouken@0
    98
 */
slouken@1895
    99
    int SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
slouken@1895
   100
    {
slouken@1895
   101
        BJoystick *stick;
slouken@0
   102
slouken@1895
   103
        /* Create the joystick data structure */
slouken@1895
   104
        joystick->hwdata = (struct joystick_hwdata *)
slouken@1895
   105
            SDL_malloc(sizeof(*joystick->hwdata));
slouken@1895
   106
        if (joystick->hwdata == NULL) {
slouken@1895
   107
            SDL_OutOfMemory();
slouken@1895
   108
            return (-1);
slouken@1895
   109
        }
slouken@1895
   110
        SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
slouken@1895
   111
        stick = new BJoystick;
slouken@1895
   112
        joystick->hwdata->stick = stick;
slouken@0
   113
slouken@1895
   114
        /* Open the requested joystick for use */
slouken@1895
   115
        if (stick->Open(SDL_joyport[joystick->index]) == B_ERROR) {
slouken@1895
   116
            SDL_SetError("Unable to open joystick");
slouken@1895
   117
            SDL_SYS_JoystickClose(joystick);
slouken@1895
   118
            return (-1);
slouken@1895
   119
        }
slouken@0
   120
slouken@1895
   121
        /* Set the joystick to calibrated mode */
slouken@1895
   122
        stick->EnableCalibration();
slouken@0
   123
slouken@1895
   124
        /* Get the number of buttons, hats, and axes on the joystick */
slouken@1895
   125
        joystick->nbuttons = stick->CountButtons();
slouken@1895
   126
        joystick->naxes = stick->CountAxes();
slouken@1895
   127
        joystick->nhats = stick->CountHats();
slouken@0
   128
slouken@1895
   129
        joystick->hwdata->new_axes = (int16 *)
slouken@1895
   130
            SDL_malloc(joystick->naxes * sizeof(int16));
slouken@1895
   131
        joystick->hwdata->new_hats = (uint8 *)
slouken@1895
   132
            SDL_malloc(joystick->nhats * sizeof(uint8));
slouken@1895
   133
        if (!joystick->hwdata->new_hats || !joystick->hwdata->new_axes) {
slouken@1895
   134
            SDL_OutOfMemory();
slouken@1895
   135
            SDL_SYS_JoystickClose(joystick);
slouken@1895
   136
            return (-1);
slouken@1895
   137
        }
slouken@0
   138
slouken@1895
   139
        /* We're done! */
slouken@1895
   140
        return (0);
slouken@1895
   141
    }
slouken@0
   142
slouken@0
   143
/* Function to update the state of a joystick - called as a device poll.
slouken@0
   144
 * This function shouldn't update the joystick structure directly,
slouken@0
   145
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@0
   146
 * and update joystick device state.
slouken@0
   147
 */
slouken@1895
   148
    void SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@1895
   149
    {
slouken@1895
   150
        static const Uint8 hat_map[9] = {
slouken@1895
   151
            SDL_HAT_CENTERED,
slouken@1895
   152
            SDL_HAT_UP,
slouken@1895
   153
            SDL_HAT_RIGHTUP,
slouken@1895
   154
            SDL_HAT_RIGHT,
slouken@1895
   155
            SDL_HAT_RIGHTDOWN,
slouken@1895
   156
            SDL_HAT_DOWN,
slouken@1895
   157
            SDL_HAT_LEFTDOWN,
slouken@1895
   158
            SDL_HAT_LEFT,
slouken@1895
   159
            SDL_HAT_LEFTUP
slouken@1895
   160
        };
slouken@1895
   161
        const int JITTER = (32768 / 10);        /* 10% jitter threshold (ok?) */
slouken@0
   162
slouken@1895
   163
        BJoystick *stick;
slouken@1895
   164
        int i, change;
slouken@1895
   165
        int16 *axes;
slouken@1895
   166
        uint8 *hats;
slouken@1895
   167
        uint32 buttons;
slouken@0
   168
slouken@1895
   169
        /* Set up data pointers */
slouken@1895
   170
        stick = joystick->hwdata->stick;
slouken@1895
   171
        axes = joystick->hwdata->new_axes;
slouken@1895
   172
        hats = joystick->hwdata->new_hats;
slouken@0
   173
slouken@1895
   174
        /* Get the new joystick state */
slouken@1895
   175
        stick->Update();
slouken@1895
   176
        stick->GetAxisValues(axes);
slouken@1895
   177
        stick->GetHatValues(hats);
slouken@1895
   178
        buttons = stick->ButtonValues();
slouken@0
   179
slouken@1895
   180
        /* Generate axis motion events */
slouken@1895
   181
        for (i = 0; i < joystick->naxes; ++i) {
slouken@1895
   182
            change = ((int32) axes[i] - joystick->axes[i]);
slouken@1895
   183
            if ((change > JITTER) || (change < -JITTER)) {
slouken@1895
   184
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slouken@1895
   185
            }
slouken@1895
   186
        }
slouken@0
   187
slouken@1895
   188
        /* Generate hat change events */
slouken@1895
   189
        for (i = 0; i < joystick->nhats; ++i) {
slouken@1895
   190
            if (hats[i] != joystick->hats[i]) {
slouken@1895
   191
                SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
slouken@1895
   192
            }
slouken@1895
   193
        }
slouken@0
   194
slouken@1895
   195
        /* Generate button events */
slouken@1895
   196
        for (i = 0; i < joystick->nbuttons; ++i) {
slouken@1895
   197
            if ((buttons & 0x01) != joystick->buttons[i]) {
slouken@1895
   198
                SDL_PrivateJoystickButton(joystick, i, (buttons & 0x01));
slouken@1895
   199
            }
slouken@1895
   200
            buttons >>= 1;
slouken@1895
   201
        }
slouken@1895
   202
    }
slouken@0
   203
slouken@0
   204
/* Function to close a joystick after use */
slouken@1895
   205
    void SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@1895
   206
    {
slouken@1895
   207
        if (joystick->hwdata) {
slouken@1895
   208
            joystick->hwdata->stick->Close();
slouken@1895
   209
            delete joystick->hwdata->stick;
slouken@1895
   210
            if (joystick->hwdata->new_hats) {
slouken@1895
   211
                SDL_free(joystick->hwdata->new_hats);
slouken@1895
   212
            }
slouken@1895
   213
            if (joystick->hwdata->new_axes) {
slouken@1895
   214
                SDL_free(joystick->hwdata->new_axes);
slouken@1895
   215
            }
slouken@1895
   216
            SDL_free(joystick->hwdata);
slouken@1895
   217
            joystick->hwdata = NULL;
slouken@1895
   218
        }
slouken@1895
   219
    }
slouken@0
   220
slouken@0
   221
/* Function to perform any system-specific joystick related cleanup */
slouken@1895
   222
    void SDL_SYS_JoystickQuit(void)
slouken@1895
   223
    {
slouken@1895
   224
        int i;
slouken@0
   225
slouken@1895
   226
        for (i = 0; SDL_joyport[i]; ++i) {
slouken@1895
   227
            SDL_free(SDL_joyport[i]);
slouken@1895
   228
        }
slouken@1895
   229
        SDL_joyport[0] = NULL;
slouken@0
   230
slouken@1895
   231
        for (i = 0; SDL_joyname[i]; ++i) {
slouken@1895
   232
            SDL_free(SDL_joyname[i]);
slouken@1895
   233
        }
slouken@1895
   234
        SDL_joyname[0] = NULL;
slouken@1895
   235
    }
slouken@0
   236
slouken@1895
   237
};                              // extern "C"
slouken@1635
   238
slouken@1635
   239
#endif /* SDL_JOYSTICK_BEOS */
slouken@1895
   240
/* vi: set ts=4 sw=4 expandtab: */