src/joystick/beos/SDL_bejoystick.cc
author Sam Lantinga
Fri, 14 Apr 2006 04:46:47 +0000
changeset 1635 92947e3a18db
parent 1403 376665398b25
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Make sure code is only compiled if the appropriate subsystem is enabled
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@0
    31
extern "C" {
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@0
    39
#define MAX_JOYSTICKS	16	
slouken@0
    40
slouken@0
    41
/* A list of available joysticks */
slouken@0
    42
static char *SDL_joyport[MAX_JOYSTICKS];
slouken@0
    43
static char *SDL_joyname[MAX_JOYSTICKS];
slouken@0
    44
slouken@0
    45
/* The private structure used to keep track of a joystick */
slouken@0
    46
struct joystick_hwdata {
slouken@0
    47
	BJoystick *stick;
slouken@0
    48
	uint8 *new_hats;
slouken@0
    49
	int16 *new_axes;
slouken@0
    50
};
slouken@0
    51
slouken@0
    52
/* Function to scan the system for joysticks.
slouken@0
    53
 * This function should set SDL_numjoysticks to the number of available
slouken@0
    54
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@0
    55
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@0
    56
 */
slouken@0
    57
int SDL_SYS_JoystickInit(void)
slouken@0
    58
{
slouken@0
    59
	BJoystick joystick;
slouken@0
    60
	int numjoysticks;
slouken@0
    61
	int i;
slouken@0
    62
	int32 nports;
slouken@0
    63
	char name[B_OS_NAME_LENGTH];
slouken@0
    64
	
slouken@0
    65
	/* Search for attached joysticks */
slouken@0
    66
	nports = joystick.CountDevices();
slouken@0
    67
	numjoysticks = 0;
slouken@1336
    68
	SDL_memset(SDL_joyport, 0, (sizeof SDL_joyport));
slouken@1336
    69
	SDL_memset(SDL_joyname, 0, (sizeof SDL_joyname));
slouken@0
    70
	for ( i=0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i ) {
slouken@0
    71
		if ( joystick.GetDeviceName(i, name) == B_OK ) {
slouken@0
    72
			if ( joystick.Open(name) != B_ERROR ) {
slouken@0
    73
				BString stick_name;
slouken@0
    74
				joystick.GetControllerName(&stick_name);
slouken@0
    75
				SDL_joyport[numjoysticks] = strdup(name);
slouken@0
    76
				SDL_joyname[numjoysticks] =
slouken@0
    77
					           strdup(stick_name.String());
slouken@0
    78
				numjoysticks++;
slouken@0
    79
				joystick.Close();
slouken@0
    80
			}
slouken@0
    81
		}
slouken@0
    82
	}
slouken@0
    83
	return(numjoysticks);
slouken@0
    84
}
slouken@0
    85
slouken@0
    86
/* Function to get the device-dependent name of a joystick */
slouken@0
    87
const char *SDL_SYS_JoystickName(int index)
slouken@0
    88
{
slouken@0
    89
	return SDL_joyname[index];
slouken@0
    90
}
slouken@0
    91
slouken@0
    92
/* Function to open a joystick for use.
slouken@0
    93
   The joystick to open is specified by the index field of the joystick.
slouken@0
    94
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@0
    95
   It returns 0, or -1 if there is an error.
slouken@0
    96
 */
slouken@0
    97
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@0
    98
{
slouken@0
    99
	BJoystick *stick;
slouken@0
   100
slouken@0
   101
	/* Create the joystick data structure */
slouken@0
   102
	joystick->hwdata = (struct joystick_hwdata *)
slouken@1336
   103
	                   SDL_malloc(sizeof(*joystick->hwdata));
slouken@0
   104
	if ( joystick->hwdata == NULL ) {
slouken@0
   105
		SDL_OutOfMemory();
slouken@0
   106
		return(-1);
slouken@0
   107
	}
slouken@1336
   108
	SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
slouken@0
   109
	stick = new BJoystick;
slouken@0
   110
	joystick->hwdata->stick = stick;
slouken@0
   111
slouken@0
   112
	/* Open the requested joystick for use */
slouken@0
   113
	if ( stick->Open(SDL_joyport[joystick->index]) == B_ERROR ) {
slouken@0
   114
		SDL_SetError("Unable to open joystick");
slouken@0
   115
		SDL_SYS_JoystickClose(joystick);
slouken@0
   116
		return(-1);
slouken@0
   117
	}
slouken@0
   118
slouken@0
   119
	/* Set the joystick to calibrated mode */
slouken@0
   120
	stick->EnableCalibration();
slouken@0
   121
slouken@0
   122
	/* Get the number of buttons, hats, and axes on the joystick */
slouken@0
   123
	joystick->nbuttons = stick->CountButtons();
slouken@0
   124
	joystick->naxes = stick->CountAxes();
slouken@0
   125
	joystick->nhats = stick->CountHats();
slouken@0
   126
slouken@0
   127
	joystick->hwdata->new_axes = (int16 *)
slouken@1336
   128
	                  SDL_malloc(joystick->naxes*sizeof(int16));
slouken@0
   129
	joystick->hwdata->new_hats = (uint8 *)
slouken@1336
   130
	                  SDL_malloc(joystick->nhats*sizeof(uint8));
slouken@0
   131
	if ( ! joystick->hwdata->new_hats || ! joystick->hwdata->new_axes ) {
slouken@0
   132
		SDL_OutOfMemory();
slouken@0
   133
		SDL_SYS_JoystickClose(joystick);
slouken@0
   134
		return(-1);
slouken@0
   135
	}
slouken@0
   136
slouken@0
   137
	/* We're done! */
slouken@0
   138
	return(0);
slouken@0
   139
}
slouken@0
   140
slouken@0
   141
/* Function to update the state of a joystick - called as a device poll.
slouken@0
   142
 * This function shouldn't update the joystick structure directly,
slouken@0
   143
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@0
   144
 * and update joystick device state.
slouken@0
   145
 */
slouken@0
   146
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@0
   147
{
slouken@0
   148
	static const Uint8 hat_map[9] = {
slouken@0
   149
             SDL_HAT_CENTERED,
slouken@0
   150
             SDL_HAT_UP,
slouken@0
   151
             SDL_HAT_RIGHTUP,
slouken@0
   152
             SDL_HAT_RIGHT,
slouken@0
   153
             SDL_HAT_RIGHTDOWN,
slouken@0
   154
             SDL_HAT_DOWN,
slouken@0
   155
             SDL_HAT_LEFTDOWN,
slouken@0
   156
             SDL_HAT_LEFT,
slouken@0
   157
             SDL_HAT_LEFTUP
slouken@0
   158
	};
slouken@0
   159
	const int JITTER = (32768/10);	/* 10% jitter threshold (ok?) */
slouken@0
   160
slouken@0
   161
	BJoystick *stick;
slouken@0
   162
	int i, change;
slouken@0
   163
	int16 *axes;
slouken@0
   164
	uint8 *hats;
slouken@0
   165
	uint32 buttons;
slouken@0
   166
slouken@0
   167
	/* Set up data pointers */
slouken@0
   168
	stick = joystick->hwdata->stick;
slouken@0
   169
	axes = joystick->hwdata->new_axes;
slouken@0
   170
	hats = joystick->hwdata->new_hats;
slouken@0
   171
slouken@0
   172
	/* Get the new joystick state */
slouken@0
   173
	stick->Update();
slouken@0
   174
	stick->GetAxisValues(axes);
slouken@0
   175
	stick->GetHatValues(hats);
slouken@0
   176
	buttons = stick->ButtonValues();
slouken@0
   177
slouken@0
   178
	/* Generate axis motion events */
slouken@0
   179
	for ( i=0; i<joystick->naxes; ++i ) {
slouken@0
   180
		change = ((int32)axes[i] - joystick->axes[i]);
slouken@0
   181
		if ( (change > JITTER) || (change < -JITTER) ) {
slouken@0
   182
			SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slouken@0
   183
		}
slouken@0
   184
	}
slouken@0
   185
slouken@0
   186
	/* Generate hat change events */
slouken@0
   187
	for ( i=0; i<joystick->nhats; ++i ) {
slouken@0
   188
		if ( hats[i] != joystick->hats[i] ) {
slouken@0
   189
			SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]);
slouken@0
   190
		}
slouken@0
   191
	}
slouken@0
   192
slouken@0
   193
	/* Generate button events */
slouken@0
   194
	for ( i=0; i<joystick->nbuttons; ++i ) {
slouken@0
   195
		if ( (buttons&0x01) != joystick->buttons[i] ) {
slouken@0
   196
			SDL_PrivateJoystickButton(joystick, i, (buttons&0x01));
slouken@0
   197
		}
slouken@0
   198
		buttons >>= 1;
slouken@0
   199
	}
slouken@0
   200
}
slouken@0
   201
slouken@0
   202
/* Function to close a joystick after use */
slouken@0
   203
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@0
   204
{
slouken@0
   205
	if ( joystick->hwdata ) {
slouken@0
   206
		joystick->hwdata->stick->Close();
slouken@0
   207
		delete joystick->hwdata->stick;
slouken@0
   208
		if ( joystick->hwdata->new_hats ) {
slouken@1336
   209
			SDL_free(joystick->hwdata->new_hats);
slouken@0
   210
		}
slouken@0
   211
		if ( joystick->hwdata->new_axes ) {
slouken@1336
   212
			SDL_free(joystick->hwdata->new_axes);
slouken@0
   213
		}
slouken@1336
   214
		SDL_free(joystick->hwdata);
slouken@0
   215
		joystick->hwdata = NULL;
slouken@0
   216
	}
slouken@0
   217
}
slouken@0
   218
slouken@0
   219
/* Function to perform any system-specific joystick related cleanup */
slouken@0
   220
void SDL_SYS_JoystickQuit(void)
slouken@0
   221
{
slouken@0
   222
	int i;
slouken@0
   223
slouken@0
   224
	for ( i=0; SDL_joyport[i]; ++i ) {
slouken@1336
   225
		SDL_free(SDL_joyport[i]);
slouken@0
   226
	}
slouken@0
   227
	SDL_joyport[0] = NULL;
slouken@0
   228
slouken@0
   229
	for ( i=0; SDL_joyname[i]; ++i ) {
slouken@1336
   230
		SDL_free(SDL_joyname[i]);
slouken@0
   231
	}
slouken@0
   232
	SDL_joyname[0] = NULL;
slouken@0
   233
}
slouken@0
   234
slouken@0
   235
}; // extern "C"
slouken@1635
   236
slouken@1635
   237
#endif /* SDL_JOYSTICK_BEOS */