src/joystick/win32/SDL_mmjoystick.c
author Sam Lantinga
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 49 6f3c474f9abd
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
/* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */
slouken@0
    29
slouken@0
    30
#include <stdlib.h>
slouken@0
    31
#include <stdio.h>		/* For the definition of NULL */
slouken@0
    32
slouken@0
    33
#include "SDL_error.h"
slouken@0
    34
#include "SDL_joystick.h"
slouken@0
    35
#include "SDL_sysjoystick.h"
slouken@0
    36
#include "SDL_joystick_c.h"
slouken@0
    37
slouken@0
    38
#include <windows.h>
slouken@0
    39
#include <mmsystem.h>
slouken@0
    40
slouken@0
    41
#define MAX_JOYSTICKS	2	/* only 2 are supported in the multimedia API */
slouken@0
    42
#define MAX_AXES	6	/* each joystick can have up to 6 axes */
slouken@0
    43
#define MAX_BUTTONS	32	/* and 32 buttons                      */
slouken@0
    44
#define AXIS_MIN	-32768  /* minimum value for axis coordinate */
slouken@0
    45
#define AXIS_MAX	32767   /* maximum value for axis coordinate */
slouken@0
    46
#define JOY_AXIS_THRESHOLD	(((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
slouken@0
    47
#define JOY_BUTTON_FLAG(n)	(1<<n)
slouken@0
    48
slouken@0
    49
slouken@0
    50
/* array to hold joystick ID values */
slouken@0
    51
static UINT	SYS_JoystickID[MAX_JOYSTICKS];
slouken@0
    52
static JOYCAPS	SYS_Joystick[MAX_JOYSTICKS];
slouken@0
    53
slouken@0
    54
/* The private structure used to keep track of a joystick */
slouken@0
    55
struct joystick_hwdata
slouken@0
    56
{
slouken@0
    57
	/* joystick ID */
slouken@0
    58
	UINT	id;
slouken@0
    59
slouken@0
    60
	/* values used to translate device-specific coordinates into
slouken@0
    61
	   SDL-standard ranges */
slouken@0
    62
	struct _transaxis {
slouken@0
    63
		int offset;
slouken@0
    64
		float scale;
slouken@0
    65
	} transaxis[6];
slouken@0
    66
};
slouken@0
    67
slouken@0
    68
/* Convert a win32 Multimedia API return code to a text message */
slouken@0
    69
static void SetMMerror(char *function, int code);
slouken@0
    70
slouken@0
    71
slouken@0
    72
/* Function to scan the system for joysticks.
slouken@0
    73
 * This function should set SDL_numjoysticks to the number of available
slouken@0
    74
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@0
    75
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@0
    76
 */
slouken@0
    77
int SDL_SYS_JoystickInit(void)
slouken@0
    78
{
slouken@0
    79
	int	i;
slouken@0
    80
	int maxdevs;
slouken@0
    81
	int numdevs;
slouken@0
    82
	JOYINFOEX joyinfo;
slouken@0
    83
	JOYCAPS	joycaps;
slouken@0
    84
	MMRESULT result;
slouken@0
    85
slouken@0
    86
	numdevs = 0;
slouken@0
    87
	maxdevs = joyGetNumDevs();
slouken@0
    88
	if ( maxdevs > MAX_JOYSTICKS ) {
slouken@0
    89
		maxdevs = MAX_JOYSTICKS;
slouken@0
    90
	}
slouken@0
    91
slouken@0
    92
	SYS_JoystickID[0] = JOYSTICKID1;
slouken@0
    93
	SYS_JoystickID[1] = JOYSTICKID2;
slouken@0
    94
slouken@0
    95
	for ( i = 0; (i < maxdevs); ++i ) {
slouken@0
    96
		result = joyGetPosEx(SYS_JoystickID[i], &joyinfo);
slouken@0
    97
		if ( result == JOYERR_NOERROR ) {
slouken@0
    98
			result = joyGetDevCaps(SYS_JoystickID[i], &joycaps, sizeof(joycaps));
slouken@0
    99
			if ( result == JOYERR_NOERROR ) {
slouken@0
   100
				SYS_JoystickID[numdevs] = SYS_JoystickID[i];
slouken@0
   101
				SYS_Joystick[numdevs] = joycaps;
slouken@0
   102
				numdevs++;
slouken@0
   103
			}
slouken@0
   104
		}
slouken@0
   105
	}
slouken@0
   106
	return(numdevs);
slouken@0
   107
}
slouken@0
   108
slouken@0
   109
/* Function to get the device-dependent name of a joystick */
slouken@0
   110
const char *SDL_SYS_JoystickName(int index)
slouken@0
   111
{
slouken@0
   112
	/***-> test for invalid index ? */
slouken@0
   113
	return(SYS_Joystick[index].szPname);
slouken@0
   114
}
slouken@0
   115
slouken@0
   116
/* Function to open a joystick for use.
slouken@0
   117
   The joystick to open is specified by the index field of the joystick.
slouken@0
   118
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@0
   119
   It returns 0, or -1 if there is an error.
slouken@0
   120
 */
slouken@0
   121
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@0
   122
{
slouken@0
   123
	int index, i;
slouken@0
   124
	int caps_flags[MAX_AXES-2] =
slouken@0
   125
		{ JOYCAPS_HASZ, JOYCAPS_HASR, JOYCAPS_HASU, JOYCAPS_HASV };
slouken@0
   126
	int axis_min[MAX_AXES], axis_max[MAX_AXES];
slouken@0
   127
slouken@0
   128
slouken@0
   129
	/* shortcut */
slouken@0
   130
	index = joystick->index;
slouken@0
   131
	axis_min[0] = SYS_Joystick[index].wXmin;
slouken@0
   132
	axis_max[0] = SYS_Joystick[index].wXmax;
slouken@0
   133
	axis_min[1] = SYS_Joystick[index].wYmin;
slouken@0
   134
	axis_max[1] = SYS_Joystick[index].wYmax;
slouken@0
   135
	axis_min[2] = SYS_Joystick[index].wZmin;
slouken@0
   136
	axis_max[2] = SYS_Joystick[index].wZmax;
slouken@0
   137
	axis_min[3] = SYS_Joystick[index].wRmin;
slouken@0
   138
	axis_max[3] = SYS_Joystick[index].wRmax;
slouken@0
   139
	axis_min[4] = SYS_Joystick[index].wUmin;
slouken@0
   140
	axis_max[4] = SYS_Joystick[index].wUmax;
slouken@0
   141
	axis_min[5] = SYS_Joystick[index].wVmin;
slouken@0
   142
	axis_max[5] = SYS_Joystick[index].wVmax;
slouken@0
   143
slouken@0
   144
	/* allocate memory for system specific hardware data */
slouken@0
   145
	joystick->hwdata = (struct joystick_hwdata *) malloc(sizeof(*joystick->hwdata));
slouken@0
   146
	if (joystick->hwdata == NULL)
slouken@0
   147
	{
slouken@0
   148
		SDL_OutOfMemory();
slouken@0
   149
		return(-1);
slouken@0
   150
	}
slouken@0
   151
	memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
slouken@0
   152
slouken@0
   153
	/* set hardware data */
slouken@0
   154
	joystick->hwdata->id = SYS_JoystickID[index];
slouken@0
   155
	for ( i = 0; i < MAX_AXES; ++i ) {
slouken@0
   156
		if ( (i<2) || (SYS_Joystick[index].wCaps & caps_flags[i-2]) ) {
slouken@0
   157
			joystick->hwdata->transaxis[i].offset =
slouken@0
   158
				AXIS_MIN - axis_min[i];
slouken@0
   159
			joystick->hwdata->transaxis[i].scale =
slouken@0
   160
				(float)(AXIS_MAX - AXIS_MIN) / (axis_max[i] - axis_min[i]);
slouken@0
   161
		} else {
slouken@0
   162
			joystick->hwdata->transaxis[i].offset = 0;
slouken@0
   163
			joystick->hwdata->transaxis[i].scale = 1.0; /* Just in case */
slouken@0
   164
		}
slouken@0
   165
	}
slouken@0
   166
slouken@0
   167
	/* fill nbuttons, naxes, and nhats fields */
slouken@0
   168
	joystick->nbuttons = SYS_Joystick[index].wNumButtons;
slouken@0
   169
	joystick->naxes = SYS_Joystick[index].wNumAxes;
slouken@0
   170
	if ( SYS_Joystick[index].wCaps & JOYCAPS_HASPOV ) {
slouken@0
   171
		joystick->nhats = 1;
slouken@0
   172
	} else {
slouken@0
   173
		joystick->nhats = 0;
slouken@0
   174
	}
slouken@0
   175
	return(0);
slouken@0
   176
}
slouken@0
   177
slouken@0
   178
static Uint8 TranslatePOV(DWORD value)
slouken@0
   179
{
slouken@0
   180
	Uint8 pos;
slouken@0
   181
slouken@0
   182
	pos = SDL_HAT_CENTERED;
slouken@0
   183
	if ( value != JOY_POVCENTERED ) {
slouken@0
   184
		if ( (value > JOY_POVLEFT) || (value < JOY_POVRIGHT) ) {
slouken@0
   185
			pos |= SDL_HAT_UP;
slouken@0
   186
		}
slouken@0
   187
		if ( (value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD) ) {
slouken@0
   188
			pos |= SDL_HAT_RIGHT;
slouken@0
   189
		}
slouken@0
   190
		if ( (value > JOY_POVRIGHT) && (value < JOY_POVLEFT) ) {
slouken@0
   191
			pos |= SDL_HAT_DOWN;
slouken@0
   192
		}
slouken@0
   193
		if ( value > JOY_POVBACKWARD ) {
slouken@0
   194
			pos |= SDL_HAT_LEFT;
slouken@0
   195
		}
slouken@0
   196
	}
slouken@0
   197
	return(pos);
slouken@0
   198
}
slouken@0
   199
slouken@0
   200
/* Function to update the state of a joystick - called as a device poll.
slouken@0
   201
 * This function shouldn't update the joystick structure directly,
slouken@0
   202
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@0
   203
 * and update joystick device state.
slouken@0
   204
 */
slouken@0
   205
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@0
   206
{
slouken@0
   207
	MMRESULT result;
slouken@0
   208
	int i;
slouken@0
   209
	DWORD flags[MAX_AXES] = { JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, 
slouken@0
   210
				  JOY_RETURNR, JOY_RETURNU, JOY_RETURNV };
slouken@0
   211
	DWORD pos[MAX_AXES];
slouken@0
   212
	struct _transaxis *transaxis;
slouken@0
   213
	int value, change;
slouken@0
   214
	JOYINFOEX joyinfo;
slouken@0
   215
slouken@0
   216
	joyinfo.dwSize = sizeof(joyinfo);
slouken@0
   217
	joyinfo.dwFlags = JOY_RETURNALL|JOY_RETURNPOVCTS;
slouken@0
   218
	if ( ! joystick->hats ) {
slouken@0
   219
		joyinfo.dwFlags &= ~(JOY_RETURNPOV|JOY_RETURNPOVCTS);
slouken@0
   220
	}
slouken@0
   221
	result = joyGetPosEx(joystick->hwdata->id, &joyinfo);
slouken@0
   222
	if ( result != JOYERR_NOERROR ) {
slouken@0
   223
		SetMMerror("joyGetPosEx", result);
slouken@0
   224
		return;
slouken@0
   225
	}
slouken@0
   226
slouken@0
   227
	/* joystick motion events */
slouken@0
   228
	pos[0] = joyinfo.dwXpos;
slouken@0
   229
	pos[1] = joyinfo.dwYpos;
slouken@0
   230
	pos[2] = joyinfo.dwZpos;
slouken@0
   231
	pos[3] = joyinfo.dwRpos;
slouken@0
   232
	pos[4] = joyinfo.dwUpos;
slouken@0
   233
	pos[5] = joyinfo.dwVpos;
slouken@0
   234
slouken@0
   235
	transaxis = joystick->hwdata->transaxis;
slouken@0
   236
	for (i = 0; i < joystick->naxes; i++) {
slouken@0
   237
		if (joyinfo.dwFlags & flags[i]) {
slouken@0
   238
			value = (int)((float)(pos[i] + transaxis[i].offset) * transaxis[i].scale);
slouken@0
   239
			change = (value - joystick->axes[i]);
slouken@0
   240
			if ( (change < -JOY_AXIS_THRESHOLD) || (change > JOY_AXIS_THRESHOLD) ) {
slouken@0
   241
				SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)value);
slouken@0
   242
			}
slouken@0
   243
		}
slouken@0
   244
	}
slouken@0
   245
slouken@0
   246
	/* joystick button events */
slouken@0
   247
	if ( joyinfo.dwFlags & JOY_RETURNBUTTONS ) {
slouken@0
   248
		for ( i = 0; i < joystick->nbuttons; ++i ) {
slouken@0
   249
			if ( joyinfo.dwButtons & JOY_BUTTON_FLAG(i) ) {
slouken@0
   250
				if ( ! joystick->buttons[i] ) {
slouken@0
   251
					SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_PRESSED);
slouken@0
   252
				}
slouken@0
   253
			} else {
slouken@0
   254
				if ( joystick->buttons[i] ) {
slouken@0
   255
					SDL_PrivateJoystickButton(joystick, (Uint8)i, SDL_RELEASED);
slouken@0
   256
				}
slouken@0
   257
			}
slouken@0
   258
		}
slouken@0
   259
	}
slouken@0
   260
slouken@0
   261
	/* joystick hat events */
slouken@0
   262
	if ( joyinfo.dwFlags & JOY_RETURNPOV ) {
slouken@0
   263
		Uint8 pos;
slouken@0
   264
slouken@0
   265
		pos = TranslatePOV(joyinfo.dwPOV);
slouken@0
   266
		if ( pos != joystick->hats[0] ) {
slouken@0
   267
			SDL_PrivateJoystickHat(joystick, 0, pos);
slouken@0
   268
		}
slouken@0
   269
	}
slouken@0
   270
}
slouken@0
   271
slouken@0
   272
/* Function to close a joystick after use */
slouken@0
   273
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@0
   274
{
slouken@0
   275
	if (joystick->hwdata != NULL) {
slouken@0
   276
		/* free system specific hardware data */
slouken@0
   277
		free(joystick->hwdata);
slouken@0
   278
	}
slouken@0
   279
}
slouken@0
   280
slouken@0
   281
/* Function to perform any system-specific joystick related cleanup */
slouken@0
   282
void SDL_SYS_JoystickQuit(void)
slouken@0
   283
{
slouken@0
   284
	return;
slouken@0
   285
}
slouken@0
   286
slouken@0
   287
slouken@0
   288
/* implementation functions */
slouken@0
   289
void SetMMerror(char *function, int code)
slouken@0
   290
{
slouken@0
   291
	static char *error;
slouken@0
   292
	static char  errbuf[BUFSIZ];
slouken@0
   293
slouken@0
   294
	errbuf[0] = 0;
slouken@0
   295
	switch (code) 
slouken@0
   296
	{
slouken@0
   297
		case MMSYSERR_NODRIVER:
slouken@0
   298
			error = "Joystick driver not present";
slouken@0
   299
		break;
slouken@0
   300
slouken@0
   301
		case MMSYSERR_INVALPARAM:
slouken@0
   302
		case JOYERR_PARMS:
slouken@0
   303
			error = "Invalid parameter(s)";
slouken@0
   304
		break;
slouken@0
   305
		
slouken@0
   306
		case MMSYSERR_BADDEVICEID:
slouken@0
   307
			error = "Bad device ID";
slouken@0
   308
		break;
slouken@0
   309
slouken@0
   310
		case JOYERR_UNPLUGGED:
slouken@0
   311
			error = "Joystick not attached";
slouken@0
   312
		break;
slouken@0
   313
slouken@0
   314
		case JOYERR_NOCANDO:
slouken@0
   315
			error = "Can't capture joystick input";
slouken@0
   316
		break;
slouken@0
   317
slouken@0
   318
		default:
slouken@0
   319
			sprintf(errbuf, "%s: Unknown Multimedia system error: 0x%x",
slouken@0
   320
								function, code);
slouken@0
   321
		break;
slouken@0
   322
	}
slouken@0
   323
slouken@0
   324
	if ( ! errbuf[0] ) {
slouken@0
   325
		sprintf(errbuf, "%s: %s", function, error);
slouken@0
   326
	}
slouken@0
   327
	SDL_SetError("%s", errbuf);
slouken@0
   328
}