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