src/joystick/mint/SDL_sysjoystick.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 05 May 2005 10:52:22 +0000
changeset 1054 e89d7f826b4a
parent 963 92c247cec42d
child 1079 39b5606fa543
permissions -rw-r--r--
Add support for ARAnyM Atari emulator
slouken@302
     1
/*
slouken@302
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@302
     4
slouken@302
     5
    This library is free software; you can redistribute it and/or
slouken@302
     6
    modify it under the terms of the GNU Library General Public
slouken@302
     7
    License as published by the Free Software Foundation; either
slouken@302
     8
    version 2 of the License, or (at your option) any later version.
slouken@302
     9
slouken@302
    10
    This library is distributed in the hope that it will be useful,
slouken@302
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@302
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@302
    13
    Library General Public License for more details.
slouken@302
    14
slouken@302
    15
    You should have received a copy of the GNU Library General Public
slouken@302
    16
    License along with this library; if not, write to the Free
slouken@302
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@302
    18
slouken@302
    19
    Sam Lantinga
slouken@302
    20
    slouken@libsdl.org
slouken@302
    21
*/
slouken@302
    22
slouken@302
    23
#ifdef SAVE_RCSID
slouken@302
    24
static char rcsid =
slouken@302
    25
 "@(#) $Id$";
slouken@302
    26
#endif
slouken@302
    27
slouken@302
    28
/*
slouken@302
    29
 *	Atari Joystick/Joypad drivers
slouken@302
    30
 *
slouken@302
    31
 *	Patrice Mandin
slouken@302
    32
 */
slouken@302
    33
slouken@305
    34
#include <stdio.h>
slouken@302
    35
#include <stdlib.h>
slouken@302
    36
#include <string.h>
slouken@302
    37
slouken@557
    38
#include <mint/cookie.h>
slouken@302
    39
#include <mint/osbind.h>
slouken@302
    40
slouken@302
    41
#include "SDL_types.h"
slouken@302
    42
#include "SDL_error.h"
slouken@302
    43
#include "SDL_joystick.h"
slouken@302
    44
#include "SDL_sysjoystick.h"
slouken@302
    45
#include "SDL_joystick_c.h"
slouken@302
    46
slouken@305
    47
#include "SDL_ikbdinterrupt_s.h"
slouken@305
    48
#include "SDL_xbiosevents_c.h"
slouken@305
    49
#include "SDL_xbiosinterrupt_s.h"
slouken@302
    50
slouken@302
    51
/*--- Const ---*/
slouken@302
    52
slouken@302
    53
/* We can have:
slouken@305
    54
	1 joystick on IKBD port 1, read via hardware I/O
slouken@305
    55
      or same joystick on IKBD port 1, read via xbios
slouken@302
    56
	2 joypads on ports A,B
slouken@302
    57
	  or 4 joysticks on joypads ports A,B
slouken@302
    58
	  or 1 lightpen on joypad port A
slouken@302
    59
	  or 2 analog paddles on joypads ports A,B
slouken@302
    60
	2 joysticks on parallel port
slouken@302
    61
*/
slouken@302
    62
slouken@302
    63
enum {
slouken@302
    64
	IKBD_JOY1=0,
slouken@305
    65
	XBIOS_JOY1,
slouken@302
    66
	PORTA_PAD,
slouken@302
    67
	PORTB_PAD,
slouken@302
    68
	PORTA_JOY0,
slouken@302
    69
	PORTA_JOY1,
slouken@302
    70
	PORTB_JOY0,
slouken@302
    71
	PORTB_JOY1,
slouken@302
    72
	PORTA_LP,
slouken@302
    73
	PORTA_ANPAD,
slouken@302
    74
	PORTB_ANPAD,
slouken@302
    75
#if 0
slouken@302
    76
	PARA_JOY0,
slouken@302
    77
	PARA_JOY1,
slouken@302
    78
#endif
slouken@302
    79
	MAX_JOYSTICKS
slouken@302
    80
};
slouken@302
    81
slouken@302
    82
enum {
slouken@302
    83
	MCH_ST=0,
slouken@302
    84
	MCH_STE,
slouken@302
    85
	MCH_TT,
patmandin@1054
    86
	MCH_F30,
patmandin@1054
    87
	MCH_ARANYM
slouken@302
    88
};
slouken@302
    89
slouken@302
    90
/*	Joypad buttons
slouken@302
    91
 *		Procontroller note:
slouken@302
    92
 *			L,R are connected to 4,6
slouken@302
    93
 *			X,Y,Z are connected to 7,8,9
slouken@302
    94
 */
slouken@302
    95
slouken@302
    96
enum {
slouken@302
    97
	JP_UP=0,	JP_DOWN,	JP_LEFT,	JP_RIGHT,
slouken@302
    98
	JP_KPMULT,	JP_KP7,		JP_KP4,		JP_KP1,
slouken@302
    99
	JP_KP0,		JP_KP8,		JP_KP5,		JP_KP2,
slouken@302
   100
	JP_KPNUM,	JP_KP9,		JP_KP6,		JP_KP3,
slouken@302
   101
	JP_PAUSE,	JP_FIRE0,	JP_UNDEF0,	JP_FIRE1,
slouken@302
   102
	JP_UNDEF1,	JP_FIRE2,	JP_UNDEF2,	JP_OPTION
slouken@302
   103
};
slouken@302
   104
slouken@302
   105
#define JP_NUM_BUTTONS 17
slouken@302
   106
slouken@302
   107
#define PORT_JS_RIGHT	(1<<0)
slouken@302
   108
#define PORT_JS_LEFT	(1<<1)
slouken@302
   109
#define PORT_JS_DOWN	(1<<2)
slouken@302
   110
#define PORT_JS_UP		(1<<3)
slouken@302
   111
#define PORT_JS_FIRE	(1<<4)
slouken@302
   112
slouken@302
   113
/*--- Types ---*/
slouken@302
   114
slouken@302
   115
typedef struct {
slouken@302
   116
	SDL_bool enabled;
slouken@302
   117
	unsigned char *name;
slouken@302
   118
	Uint32 prevstate;
slouken@302
   119
} atarijoy_t;
slouken@302
   120
slouken@302
   121
/*--- Variables ---*/
slouken@302
   122
slouken@302
   123
static atarijoy_t atarijoysticks[MAX_JOYSTICKS]={
slouken@302
   124
	{SDL_FALSE,"IKBD joystick port 1",0},
slouken@305
   125
	{SDL_FALSE,"Xbios joystick port 1",0},
slouken@302
   126
	{SDL_FALSE,"Joypad port A",0},
slouken@302
   127
	{SDL_FALSE,"Joypad port B",0},
slouken@302
   128
	{SDL_FALSE,"Joystick 0 port A",0},
slouken@302
   129
	{SDL_FALSE,"Joystick 1 port A",0},
slouken@302
   130
	{SDL_FALSE,"Joystick 0 port B",0},
slouken@302
   131
	{SDL_FALSE,"Joystick 1 port B",0},
slouken@302
   132
	{SDL_FALSE,"Lightpen port A",0},
slouken@302
   133
	{SDL_FALSE,"Analog paddle port A",0},
slouken@302
   134
	{SDL_FALSE,"Analog paddle port B",0}
slouken@302
   135
#if 0
slouken@302
   136
	,{SDL_FALSE,"Joystick 0 parallel port",0},
slouken@302
   137
	{SDL_FALSE,"Joystick 1 parallel port",0}
slouken@302
   138
#endif
slouken@302
   139
};
slouken@302
   140
slouken@302
   141
static const int jp_buttons[JP_NUM_BUTTONS]={
slouken@305
   142
	JP_FIRE0,	JP_FIRE1,	JP_FIRE2,	JP_PAUSE,
slouken@305
   143
	JP_OPTION,	JP_KPMULT,	JP_KPNUM,	JP_KP0,
slouken@305
   144
	JP_KP1,		JP_KP2,		JP_KP3,		JP_KP4,
slouken@305
   145
	JP_KP5,		JP_KP6,		JP_KP7,		JP_KP8,
slouken@305
   146
	JP_KP9
slouken@302
   147
};
slouken@302
   148
slouken@302
   149
static SDL_bool joypad_ports_enabled=SDL_FALSE;
slouken@302
   150
slouken@302
   151
/* Updated joypad ports */
slouken@302
   152
static Uint16 jp_paddles[4];
slouken@302
   153
static Uint16 jp_lightpens[2];
slouken@302
   154
static Uint16 jp_directions;
slouken@302
   155
static Uint16 jp_fires;
slouken@302
   156
static Uint32 jp_joypads[2];
slouken@302
   157
slouken@302
   158
/*--- Functions prototypes ---*/
slouken@302
   159
slouken@302
   160
static int GetEnabledAtariJoystick(int index);
slouken@302
   161
static void UpdateJoypads(void);
slouken@302
   162
slouken@302
   163
/*--- Functions ---*/
slouken@302
   164
slouken@302
   165
int SDL_SYS_JoystickInit(void)
slouken@302
   166
{
slouken@302
   167
	int i;
slouken@302
   168
	unsigned long cookie_mch;
slouken@302
   169
	const char *envr=getenv("SDL_JOYSTICK_ATARI");
slouken@302
   170
	
slouken@302
   171
#define TEST_JOY_ENABLED(env,idstring,num) \
slouken@302
   172
	if (strstr(env,idstring"-off")) { \
slouken@302
   173
		atarijoysticks[num].enabled=SDL_FALSE; \
slouken@302
   174
	} \
slouken@302
   175
	if (strstr(env,idstring"-on")) { \
slouken@302
   176
		atarijoysticks[num].enabled=SDL_TRUE; \
slouken@302
   177
	}
slouken@302
   178
slouken@302
   179
	/* Cookie _MCH present ? if not, assume ST machine */
slouken@302
   180
	if (Getcookie(C__MCH, &cookie_mch) != C_FOUND) {
slouken@302
   181
		cookie_mch = MCH_ST << 16;
slouken@302
   182
	}
slouken@302
   183
slouken@302
   184
	/* Enable some default joysticks */
slouken@302
   185
	if ((cookie_mch == MCH_ST<<16) || ((cookie_mch>>16) == MCH_STE) ||
patmandin@1054
   186
		(cookie_mch == MCH_TT<<16) || (cookie_mch == MCH_F30<<16) ||
patmandin@1054
   187
		(cookie_mch == MCH_ARANYM<<16)) {
slouken@305
   188
		atarijoysticks[IKBD_JOY1].enabled=(SDL_AtariIkbd_enabled!=0);
slouken@302
   189
	}
slouken@302
   190
	if ((cookie_mch == MCH_STE<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@302
   191
		atarijoysticks[PORTA_PAD].enabled=SDL_TRUE;
slouken@302
   192
		atarijoysticks[PORTB_PAD].enabled=SDL_TRUE;
slouken@302
   193
	}
slouken@305
   194
	if (!atarijoysticks[IKBD_JOY1].enabled) {
slouken@305
   195
		atarijoysticks[XBIOS_JOY1].enabled=(SDL_AtariXbios_enabled!=0);
slouken@305
   196
	}
slouken@302
   197
slouken@302
   198
	/* Read environment for joysticks to enable */
slouken@302
   199
	if (envr) {
slouken@302
   200
		/* IKBD on any Atari, maybe clones */
slouken@302
   201
		if ((cookie_mch == MCH_ST<<16) || ((cookie_mch>>16) == MCH_STE) ||
patmandin@1054
   202
			(cookie_mch == MCH_TT<<16) || (cookie_mch == MCH_F30<<16) ||
patmandin@1054
   203
			(cookie_mch == MCH_ARANYM<<16)) {
slouken@305
   204
			if (SDL_AtariIkbd_enabled!=0) {
slouken@305
   205
				TEST_JOY_ENABLED(envr, "ikbd-joy1", IKBD_JOY1);
slouken@302
   206
			}
slouken@302
   207
		}
slouken@302
   208
		/* Joypads ports only on STE and Falcon */
slouken@302
   209
		if ((cookie_mch == MCH_STE<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@302
   210
			TEST_JOY_ENABLED(envr, "porta-pad", PORTA_PAD);
slouken@302
   211
			if (!atarijoysticks[PORTA_PAD].enabled) {
slouken@302
   212
				TEST_JOY_ENABLED(envr, "porta-joy0", PORTA_JOY0);
slouken@302
   213
				TEST_JOY_ENABLED(envr, "porta-joy1", PORTA_JOY1);
slouken@302
   214
				if (!(atarijoysticks[PORTA_JOY0].enabled) && !(atarijoysticks[PORTA_JOY1].enabled)) {
slouken@302
   215
					TEST_JOY_ENABLED(envr, "porta-lp", PORTA_LP);
slouken@302
   216
					if (!atarijoysticks[PORTA_LP].enabled) {
slouken@302
   217
						TEST_JOY_ENABLED(envr, "porta-anpad", PORTA_ANPAD);
slouken@302
   218
					}
slouken@302
   219
				}
slouken@302
   220
			}
slouken@302
   221
slouken@302
   222
			TEST_JOY_ENABLED(envr, "portb-pad", PORTB_PAD);
slouken@302
   223
			if (!atarijoysticks[PORTB_PAD].enabled) {
slouken@302
   224
				TEST_JOY_ENABLED(envr, "portb-joy0", PORTB_JOY0);
slouken@302
   225
				TEST_JOY_ENABLED(envr, "portb-joy1", PORTB_JOY1);
slouken@302
   226
				if (!(atarijoysticks[PORTB_JOY0].enabled) && !(atarijoysticks[PORTB_JOY1].enabled)) {
slouken@302
   227
					TEST_JOY_ENABLED(envr, "portb-anpad", PORTB_ANPAD);
slouken@302
   228
				}
slouken@302
   229
			}
slouken@302
   230
		}
slouken@305
   231
slouken@305
   232
		if (!atarijoysticks[IKBD_JOY1].enabled) {
slouken@305
   233
			if (SDL_AtariXbios_enabled!=0) {
slouken@305
   234
				TEST_JOY_ENABLED(envr, "xbios-joy1", XBIOS_JOY1);
slouken@305
   235
			}
slouken@305
   236
		}
slouken@302
   237
#if 0
slouken@302
   238
		/* Parallel port on any Atari, maybe clones */
slouken@302
   239
		if ((cookie_mch == MCH_ST<<16) || ((cookie_mch>>16) == MCH_STE) ||
slouken@302
   240
			(cookie_mch == MCH_TT<<16) || (cookie_mch == MCH_F30<<16)) {
slouken@302
   241
			TEST_JOY_ENABLED(envr, "para-joy0", PARA_JOY0);
slouken@302
   242
			TEST_JOY_ENABLED(envr, "para-joy1", PARA_JOY1);
slouken@302
   243
		}
slouken@302
   244
#endif
slouken@302
   245
	}
slouken@302
   246
slouken@302
   247
	/* Need to update joypad ports ? */
slouken@302
   248
	joypad_ports_enabled=SDL_FALSE;
slouken@302
   249
	for (i=PORTA_PAD;i<=PORTB_ANPAD;i++) {
slouken@302
   250
		if (atarijoysticks[i].enabled) {
slouken@302
   251
			joypad_ports_enabled=SDL_TRUE;
slouken@302
   252
			break;
slouken@302
   253
		}
slouken@302
   254
	}
slouken@302
   255
slouken@302
   256
	SDL_numjoysticks = 0;
slouken@302
   257
	for (i=0;i<MAX_JOYSTICKS;i++) {
slouken@302
   258
		if (atarijoysticks[i].enabled) {
slouken@302
   259
			++SDL_numjoysticks;
slouken@302
   260
		}
slouken@302
   261
	}
slouken@302
   262
slouken@302
   263
	return(SDL_numjoysticks);
slouken@302
   264
}
slouken@302
   265
slouken@302
   266
static int GetEnabledAtariJoystick(int index)
slouken@302
   267
{
slouken@302
   268
	int i,j;
slouken@302
   269
slouken@302
   270
	/* Return the nth'index' enabled atari joystick */
slouken@302
   271
	j=0;
slouken@302
   272
	for (i=0;i<MAX_JOYSTICKS;i++) {
slouken@305
   273
		if (!atarijoysticks[i].enabled) {
slouken@305
   274
			continue;
slouken@302
   275
		}
slouken@305
   276
slouken@305
   277
		if (j==index) {
slouken@305
   278
			break;
slouken@305
   279
		}
slouken@305
   280
slouken@305
   281
		++j;
slouken@302
   282
	}
slouken@302
   283
	if (i==MAX_JOYSTICKS)
slouken@302
   284
		return -1;
slouken@302
   285
slouken@302
   286
	return i;
slouken@302
   287
}
slouken@302
   288
slouken@302
   289
const char *SDL_SYS_JoystickName(int index)
slouken@302
   290
{
slouken@302
   291
	int numjoystick;
slouken@302
   292
slouken@302
   293
	numjoystick=GetEnabledAtariJoystick(index);
slouken@302
   294
	if (numjoystick==-1)
slouken@302
   295
		return NULL;
slouken@302
   296
slouken@302
   297
	return(atarijoysticks[numjoystick].name);
slouken@302
   298
}
slouken@302
   299
slouken@302
   300
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@302
   301
{
slouken@302
   302
	int numjoystick;
slouken@302
   303
	
slouken@302
   304
	numjoystick=GetEnabledAtariJoystick(joystick->index);
slouken@302
   305
	if (numjoystick==-1)
slouken@302
   306
		return -1;
slouken@302
   307
	
patmandin@963
   308
	joystick->naxes=0;
patmandin@963
   309
	joystick->nhats=0;
patmandin@963
   310
	joystick->nballs=0;
patmandin@963
   311
patmandin@963
   312
	switch(numjoystick) {
patmandin@963
   313
		case PORTA_PAD:
patmandin@963
   314
		case PORTB_PAD:
patmandin@963
   315
			joystick->nhats=1;
patmandin@963
   316
			joystick->nbuttons=JP_NUM_BUTTONS;
patmandin@963
   317
			break;
patmandin@963
   318
		case PORTA_LP:
patmandin@963
   319
		case PORTA_ANPAD:
patmandin@963
   320
		case PORTB_ANPAD:
patmandin@963
   321
			joystick->naxes=2;
patmandin@963
   322
			joystick->nbuttons=2;
patmandin@963
   323
			break;
patmandin@963
   324
		default:
patmandin@963
   325
			joystick->nhats=1;
patmandin@963
   326
			joystick->nbuttons=1;
patmandin@963
   327
			break;
slouken@302
   328
	}
slouken@302
   329
slouken@302
   330
	return(0);
slouken@302
   331
}
slouken@302
   332
slouken@302
   333
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@302
   334
{
slouken@302
   335
	int numjoystick;
patmandin@963
   336
	Uint8 hatstate;
slouken@302
   337
	Uint32 curstate,prevstate;
slouken@302
   338
	
slouken@302
   339
	numjoystick=GetEnabledAtariJoystick(joystick->index);
slouken@302
   340
	if (numjoystick==-1)
slouken@302
   341
		return;
slouken@302
   342
slouken@302
   343
	prevstate = atarijoysticks[numjoystick].prevstate;
slouken@302
   344
slouken@302
   345
	if (joypad_ports_enabled) {
slouken@302
   346
		Supexec(UpdateJoypads);
slouken@302
   347
	}
slouken@302
   348
slouken@302
   349
	switch (numjoystick) {
slouken@302
   350
		case IKBD_JOY1:
slouken@305
   351
		case XBIOS_JOY1:
slouken@302
   352
			{
slouken@305
   353
				curstate = 0;
slouken@305
   354
slouken@305
   355
				if (numjoystick==IKBD_JOY1) {
slouken@305
   356
					curstate = SDL_AtariIkbd_joystick & 0xff;
slouken@305
   357
				}
slouken@305
   358
				if (numjoystick==XBIOS_JOY1) {
slouken@305
   359
					curstate = SDL_AtariXbios_joystick & 0xff;
slouken@305
   360
				}
slouken@305
   361
slouken@302
   362
				if (curstate != prevstate) {
patmandin@963
   363
					hatstate = SDL_HAT_CENTERED;
patmandin@963
   364
					if (curstate & IKBD_JOY_LEFT) {
patmandin@963
   365
						hatstate |= SDL_HAT_LEFT;
slouken@302
   366
					}
patmandin@963
   367
					if (curstate & IKBD_JOY_RIGHT) {
patmandin@963
   368
						hatstate |= SDL_HAT_RIGHT;
slouken@302
   369
					}
patmandin@963
   370
					if (curstate & IKBD_JOY_UP) {
patmandin@963
   371
						hatstate |= SDL_HAT_UP;
patmandin@963
   372
					}
patmandin@963
   373
					if (curstate & IKBD_JOY_DOWN) {
patmandin@963
   374
						hatstate |= SDL_HAT_DOWN;
patmandin@963
   375
					}
patmandin@963
   376
					SDL_PrivateJoystickHat(joystick, 0, hatstate);
patmandin@963
   377
slouken@302
   378
					/* Button */
slouken@302
   379
					if ((curstate & IKBD_JOY_FIRE) && !(prevstate & IKBD_JOY_FIRE)) {
slouken@302
   380
						SDL_PrivateJoystickButton(joystick,0,SDL_PRESSED);
slouken@302
   381
					}
slouken@302
   382
					if (!(curstate & IKBD_JOY_FIRE) && (prevstate & IKBD_JOY_FIRE)) {
slouken@302
   383
						SDL_PrivateJoystickButton(joystick,0,SDL_RELEASED);
slouken@302
   384
					}
slouken@302
   385
				}
slouken@305
   386
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   387
			}
slouken@302
   388
			break;
slouken@302
   389
		case PORTA_PAD:
slouken@302
   390
		case PORTB_PAD:
slouken@302
   391
			{
slouken@302
   392
				int numjoypad,i;
slouken@302
   393
				
slouken@302
   394
				numjoypad=0;
slouken@302
   395
				if (numjoystick==PORTB_PAD) numjoypad=1;
slouken@302
   396
				
slouken@302
   397
				curstate=jp_joypads[numjoypad];
slouken@302
   398
				if (curstate!=prevstate) {
patmandin@963
   399
					hatstate = SDL_HAT_CENTERED;
patmandin@963
   400
					if (curstate & (1<<JP_LEFT)) {
patmandin@963
   401
						hatstate |= SDL_HAT_LEFT;
slouken@302
   402
					}
patmandin@963
   403
					if (curstate & (1<<JP_RIGHT)) {
patmandin@963
   404
						hatstate |= SDL_HAT_RIGHT;
slouken@302
   405
					}
patmandin@963
   406
					if (curstate & (1<<JP_UP)) {
patmandin@963
   407
						hatstate |= SDL_HAT_UP;
patmandin@963
   408
					}
patmandin@963
   409
					if (curstate & (1<<JP_DOWN)) {
patmandin@963
   410
						hatstate |= SDL_HAT_DOWN;
patmandin@963
   411
					}
patmandin@963
   412
					SDL_PrivateJoystickHat(joystick, 0, hatstate);
patmandin@963
   413
slouken@302
   414
					/* Buttons */
slouken@302
   415
					for (i=0;i<JP_NUM_BUTTONS;i++) {
slouken@302
   416
						int button;
slouken@302
   417
						
slouken@302
   418
						button=1<<jp_buttons[i];
slouken@302
   419
slouken@302
   420
						if ((curstate & button) && !(prevstate & button)) {
slouken@302
   421
							SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@302
   422
						}
slouken@302
   423
						if (!(curstate & button) && (prevstate & button)) {
slouken@302
   424
							SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@302
   425
						}
slouken@302
   426
					}
slouken@302
   427
				}
slouken@302
   428
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   429
			}
slouken@302
   430
			break;
slouken@302
   431
		case PORTA_JOY0:
slouken@302
   432
		case PORTA_JOY1:
slouken@302
   433
		case PORTB_JOY0:
slouken@302
   434
		case PORTB_JOY1:
slouken@302
   435
			{
slouken@302
   436
				int fire_shift=0,dir_shift=0;
slouken@302
   437
				
slouken@302
   438
				if (numjoystick==PORTA_JOY0) {	fire_shift=0; dir_shift=0; }
slouken@302
   439
				if (numjoystick==PORTA_JOY1) {	fire_shift=1; dir_shift=4; }
slouken@302
   440
				if (numjoystick==PORTB_JOY0) {	fire_shift=2; dir_shift=8; }
slouken@302
   441
				if (numjoystick==PORTB_JOY1) {	fire_shift=3; dir_shift=12; }
slouken@302
   442
slouken@302
   443
				curstate = (jp_directions>>dir_shift) & 15;
slouken@302
   444
				curstate |= ((jp_fires>>fire_shift) & 1)<<4;
slouken@302
   445
slouken@302
   446
				if (curstate != prevstate) {
patmandin@963
   447
					hatstate = SDL_HAT_CENTERED;
patmandin@963
   448
					if (curstate & PORT_JS_LEFT) {
patmandin@963
   449
						hatstate |= SDL_HAT_LEFT;
slouken@302
   450
					}
patmandin@963
   451
					if (curstate & PORT_JS_RIGHT) {
patmandin@963
   452
						hatstate |= SDL_HAT_RIGHT;
slouken@302
   453
					}
patmandin@963
   454
					if (curstate & PORT_JS_UP) {
patmandin@963
   455
						hatstate |= SDL_HAT_UP;
patmandin@963
   456
					}
patmandin@963
   457
					if (curstate & PORT_JS_DOWN) {
patmandin@963
   458
						hatstate |= SDL_HAT_DOWN;
patmandin@963
   459
					}
patmandin@963
   460
					SDL_PrivateJoystickHat(joystick, 0, hatstate);
patmandin@963
   461
slouken@302
   462
					/* Button */
slouken@302
   463
					if ((curstate & PORT_JS_FIRE) && !(prevstate & PORT_JS_FIRE)) {
slouken@302
   464
						SDL_PrivateJoystickButton(joystick,0,SDL_PRESSED);
slouken@302
   465
					}
slouken@302
   466
					if (!(curstate & PORT_JS_FIRE) && (prevstate & PORT_JS_FIRE)) {
slouken@302
   467
						SDL_PrivateJoystickButton(joystick,0,SDL_RELEASED);
slouken@302
   468
					}
slouken@302
   469
				}
slouken@302
   470
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   471
			}
slouken@302
   472
			break;
slouken@302
   473
		case PORTA_LP:
slouken@302
   474
			{
slouken@302
   475
				int i;
slouken@302
   476
slouken@302
   477
				curstate = jp_lightpens[0]>>1;
slouken@302
   478
				curstate |= (jp_lightpens[1]>>1)<<15;
slouken@302
   479
				curstate |= (jp_fires & 3)<<30;
slouken@302
   480
slouken@302
   481
				if (curstate != prevstate) {
slouken@302
   482
					/* X axis */
slouken@507
   483
					SDL_PrivateJoystickAxis(joystick,0,jp_lightpens[0] ^ 0x8000);
slouken@302
   484
					/* Y axis */
slouken@507
   485
					SDL_PrivateJoystickAxis(joystick,1,jp_lightpens[1] ^ 0x8000);
slouken@302
   486
					/* Buttons */
slouken@302
   487
					for (i=0;i<2;i++) {
slouken@302
   488
						int button;
slouken@302
   489
						
slouken@302
   490
						button=1<<(30+i);
slouken@302
   491
slouken@302
   492
						if ((curstate & button) && !(prevstate & button)) {
slouken@302
   493
							SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@302
   494
						}
slouken@302
   495
						if (!(curstate & button) && (prevstate & button)) {
slouken@302
   496
							SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@302
   497
						}
slouken@302
   498
					}
slouken@302
   499
				}
slouken@302
   500
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   501
			}
slouken@302
   502
			break;
slouken@302
   503
		case PORTA_ANPAD:
slouken@302
   504
		case PORTB_ANPAD:
slouken@302
   505
			{
slouken@302
   506
				int numpaddle, i;
slouken@302
   507
				
slouken@302
   508
				numpaddle=0<<1;
slouken@302
   509
				if (numjoystick==PORTB_ANPAD) numpaddle=1<<1;
slouken@302
   510
slouken@302
   511
				curstate = jp_paddles[numpaddle]>>1;
slouken@302
   512
				curstate |= (jp_paddles[numpaddle+1]>>1)<<15;
slouken@302
   513
				curstate |= ((jp_fires>>numpaddle) & 3)<<30;
slouken@302
   514
slouken@302
   515
				if (curstate != prevstate) {
slouken@302
   516
					/* X axis */
slouken@507
   517
					SDL_PrivateJoystickAxis(joystick,0,jp_paddles[numpaddle] ^ 0x8000);
slouken@302
   518
					/* Y axis */
slouken@507
   519
					SDL_PrivateJoystickAxis(joystick,1,jp_paddles[numpaddle+1] ^ 0x8000);
slouken@302
   520
					/* Buttons */
slouken@302
   521
					for (i=0;i<2;i++) {
slouken@302
   522
						int button;
slouken@302
   523
						
slouken@302
   524
						button=1<<(30+i);
slouken@302
   525
slouken@302
   526
						if ((curstate & button) && !(prevstate & button)) {
slouken@302
   527
							SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@302
   528
						}
slouken@302
   529
						if (!(curstate & button) && (prevstate & button)) {
slouken@302
   530
							SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@302
   531
						}
slouken@302
   532
					}
slouken@302
   533
				}
slouken@302
   534
				atarijoysticks[numjoystick].prevstate = curstate;
slouken@302
   535
			}
slouken@302
   536
			break;
slouken@302
   537
#if 0
slouken@302
   538
		case PARA_JOY0:
slouken@302
   539
		case PARA_JOY1:
slouken@302
   540
			break;
slouken@302
   541
#endif
slouken@302
   542
	};
slouken@302
   543
slouken@302
   544
	return;
slouken@302
   545
}
slouken@302
   546
slouken@302
   547
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@302
   548
{
slouken@302
   549
	return;
slouken@302
   550
}
slouken@302
   551
slouken@302
   552
void SDL_SYS_JoystickQuit(void)
slouken@302
   553
{
slouken@302
   554
	SDL_numjoysticks=0;
slouken@302
   555
	return;
slouken@302
   556
}
slouken@302
   557
slouken@302
   558
/*--- Joypad I/O read/write interface ---*/
slouken@302
   559
slouken@302
   560
#define JOYPAD_IO_BASE (0xffff9200)
slouken@302
   561
struct JOYPAD_IO_S {
slouken@302
   562
	Uint16 fires;
slouken@302
   563
	Uint16 directions;
slouken@302
   564
	Uint16 dummy1[6];
slouken@302
   565
	Uint16 paddles[4];
slouken@302
   566
	Uint16 dummy2[4];
slouken@302
   567
	Uint16 lightpens[2];
slouken@302
   568
};
slouken@302
   569
#define JOYPAD_IO ((*(volatile struct JOYPAD_IO_S *)JOYPAD_IO_BASE))
slouken@302
   570
slouken@302
   571
static void UpdateJoypads(void)
slouken@302
   572
{
slouken@302
   573
	Uint16 tmp;
slouken@302
   574
slouken@302
   575
	/*--- This function is called in supervisor mode ---*/
slouken@302
   576
slouken@302
   577
	/* Update joysticks */
slouken@302
   578
	jp_fires = (~(JOYPAD_IO.fires)) & 15;
slouken@302
   579
	jp_directions = (~(JOYPAD_IO.directions));
slouken@302
   580
	
slouken@302
   581
	/* Update lightpen */
slouken@302
   582
	tmp = JOYPAD_IO.lightpens[0] & 1023;
slouken@302
   583
	jp_lightpens[0] = (tmp<<6) | (tmp>>4);
slouken@302
   584
	tmp = JOYPAD_IO.lightpens[1] & 1023;
slouken@302
   585
	jp_lightpens[1] = (tmp<<6) | (tmp>>4);
slouken@302
   586
	
slouken@302
   587
	/* Update paddles */
slouken@302
   588
	tmp = (JOYPAD_IO.paddles[0] & 255);
slouken@302
   589
	jp_paddles[0] = (tmp<<8) | tmp;
slouken@302
   590
	tmp = (JOYPAD_IO.paddles[1] & 255);
slouken@302
   591
	jp_paddles[1] = (tmp<<8) | tmp;
slouken@302
   592
	tmp = (JOYPAD_IO.paddles[2] & 255);
slouken@302
   593
	jp_paddles[2] = (tmp<<8) | tmp;
slouken@302
   594
	tmp = (JOYPAD_IO.paddles[3] & 255);
slouken@302
   595
	jp_paddles[3] = (tmp<<8) | tmp;
slouken@302
   596
slouken@302
   597
	/* Update joypad 0 */	
slouken@302
   598
	JOYPAD_IO.directions=0xfffe;
slouken@302
   599
	jp_joypads[0]=((~(JOYPAD_IO.fires)) & 3)<<(16);
slouken@302
   600
	JOYPAD_IO.directions=0xfffe;
slouken@302
   601
	jp_joypads[0] |= ((~(JOYPAD_IO.directions))>>8) & 15;
slouken@302
   602
slouken@302
   603
	JOYPAD_IO.directions=0xfffd;
slouken@302
   604
	jp_joypads[0] |= ((~(JOYPAD_IO.fires)) & 3)<<(16+2);
slouken@302
   605
	JOYPAD_IO.directions=0xfffd;
slouken@302
   606
	jp_joypads[0] |= (((~(JOYPAD_IO.directions))>>8) & 15)<<4;
slouken@302
   607
slouken@302
   608
	JOYPAD_IO.directions=0xfffb;
slouken@302
   609
	jp_joypads[0] |= ((~(JOYPAD_IO.fires)) & 3)<<(16+4);
slouken@302
   610
	JOYPAD_IO.directions=0xfffb;
slouken@302
   611
	jp_joypads[0] |= (((~(JOYPAD_IO.directions))>>8) & 15)<<8;
slouken@302
   612
slouken@302
   613
	JOYPAD_IO.directions=0xfff7;
slouken@302
   614
	jp_joypads[0] |= ((~(JOYPAD_IO.fires)) & 3)<<(16+6);
slouken@302
   615
	JOYPAD_IO.directions=0xfff7;
slouken@302
   616
	jp_joypads[0] |= (((~(JOYPAD_IO.directions))>>8) & 15)<<12;
slouken@302
   617
slouken@302
   618
	/* Update joypad 1 */	
slouken@302
   619
	JOYPAD_IO.directions=0xffef;
slouken@302
   620
	jp_joypads[1]=((~(JOYPAD_IO.fires)) & (3<<2))<<(16-2);
slouken@302
   621
	JOYPAD_IO.directions=0xffef;
slouken@302
   622
	jp_joypads[1] |= ((~(JOYPAD_IO.directions))>>12) & 15;
slouken@302
   623
slouken@302
   624
	JOYPAD_IO.directions=0xffdf;
slouken@302
   625
	jp_joypads[1] |= ((~(JOYPAD_IO.fires)) & (3<<2))<<(16);
slouken@302
   626
	JOYPAD_IO.directions=0xffdf;
slouken@302
   627
	jp_joypads[1] |= (((~(JOYPAD_IO.directions))>>12) & 15)<<4;
slouken@302
   628
slouken@302
   629
	JOYPAD_IO.directions=0xffbf;
slouken@302
   630
	jp_joypads[1] |= ((~(JOYPAD_IO.fires)) & (3<<2))<<(16+2);
slouken@302
   631
	JOYPAD_IO.directions=0xffbf;
slouken@302
   632
	jp_joypads[1] |= (((~(JOYPAD_IO.directions))>>12) & 15)<<8;
slouken@302
   633
slouken@302
   634
	JOYPAD_IO.directions=0xff7f;
slouken@302
   635
	jp_joypads[1] |= ((~(JOYPAD_IO.fires)) & (3<<2))<<(16+4);
slouken@302
   636
	JOYPAD_IO.directions=0xff7f;
slouken@302
   637
	jp_joypads[1] |= (((~(JOYPAD_IO.directions))>>12) & 15)<<12;
slouken@302
   638
}