src/joystick/amigaos/SDL_sysjoystick.c
author Sam Lantinga
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
slouken@21
     1
/*
slouken@21
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@21
     4
slouken@21
     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@21
     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@21
     9
slouken@21
    10
    This library is distributed in the hope that it will be useful,
slouken@21
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@21
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@21
    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@21
    18
slouken@21
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@21
    21
*/
slouken@21
    22
slouken@21
    23
slouken@21
    24
/* This is the system specific header for the SDL joystick API */
slouken@21
    25
slouken@21
    26
#include <stdio.h>		/* For the definition of NULL */
slouken@21
    27
slouken@21
    28
#include <libraries/lowlevel.h>
slouken@255
    29
#if defined(__SASC) || defined(STORMC4_WOS)
slouken@21
    30
#include <proto/exec.h>
slouken@21
    31
#include <proto/lowlevel.h>
slouken@21
    32
#include <proto/graphics.h>
slouken@21
    33
#else
slouken@21
    34
#include <inline/exec.h>
slouken@21
    35
#include <inline/lowlevel.h>
slouken@21
    36
#include <inline/graphics.h>
slouken@21
    37
#endif
slouken@21
    38
#include "mydebug.h"
slouken@21
    39
slouken@21
    40
extern struct ExecBase *SysBase;
slouken@21
    41
extern struct GfxBase *GfxBase;
slouken@21
    42
#include <stdlib.h>
slouken@21
    43
slouken@21
    44
#include "SDL_error.h"
slouken@21
    45
#include "SDL_joystick.h"
slouken@21
    46
#include "SDL_sysjoystick.h"
slouken@21
    47
#include "SDL_joystick_c.h"
slouken@21
    48
slouken@21
    49
/* Function to scan the system for joysticks.
slouken@21
    50
 * This function should set SDL_numjoysticks to the number of available
slouken@21
    51
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@21
    52
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@21
    53
 */
slouken@21
    54
slouken@21
    55
slouken@21
    56
/* Amiga specific datas */
slouken@21
    57
struct Library *LowLevelBase=NULL;
slouken@21
    58
slouken@21
    59
ULONG joybut[]=
slouken@21
    60
{
slouken@21
    61
	JPF_BUTTON_RED,
slouken@21
    62
	JPF_BUTTON_BLUE,
slouken@21
    63
	JPF_BUTTON_PLAY,
slouken@21
    64
	JPF_BUTTON_YELLOW,
slouken@21
    65
	JPF_BUTTON_GREEN,
slouken@21
    66
	JPF_BUTTON_FORWARD,
slouken@21
    67
	JPF_BUTTON_REVERSE,
slouken@21
    68
};
slouken@21
    69
slouken@255
    70
struct joystick_hwdata
slouken@21
    71
{
slouken@21
    72
	ULONG joystate;
slouken@21
    73
};
slouken@21
    74
slouken@21
    75
int SDL_SYS_JoystickInit(void)
slouken@21
    76
{
slouken@21
    77
	if(!LowLevelBase)
slouken@21
    78
	{
slouken@21
    79
		if(LowLevelBase=OpenLibrary("lowlevel.library",37))
slouken@21
    80
			return 2;
slouken@21
    81
	}
slouken@21
    82
	else
slouken@21
    83
		return 2;
slouken@21
    84
slouken@21
    85
	D(bug("%ld joysticks available.\n",SDL_numjoysticks));
slouken@21
    86
slouken@21
    87
	return 0;
slouken@21
    88
}
slouken@21
    89
slouken@21
    90
/* Function to get the device-dependent name of a joystick */
slouken@21
    91
const char *SDL_SYS_JoystickName(int index)
slouken@21
    92
{
slouken@21
    93
	if(index<2&&LowLevelBase)
slouken@21
    94
	{
slouken@21
    95
		switch(index)
slouken@21
    96
		{
slouken@21
    97
			case 0:
slouken@21
    98
				return "Port 1 Amiga Joystick/Joypad";
slouken@21
    99
			case 1:
slouken@21
   100
				return "Port 2 Amiga Joystick/Joypad";
slouken@21
   101
		}
slouken@21
   102
	}
slouken@21
   103
slouken@21
   104
	SDL_SetError("No joystick available with that index");
slouken@21
   105
	return(NULL);
slouken@21
   106
}
slouken@21
   107
slouken@21
   108
/* Function to open a joystick for use.
slouken@21
   109
   The joystick to open is specified by the index field of the joystick.
slouken@21
   110
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@21
   111
   It returns 0, or -1 if there is an error.
slouken@21
   112
 */
slouken@21
   113
slouken@21
   114
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@21
   115
{
slouken@21
   116
	ULONG temp,i;
slouken@21
   117
	D(bug("Opening joystick %ld\n",joystick->index));
slouken@21
   118
slouken@21
   119
	if(!(joystick->hwdata=malloc(sizeof(struct joystick_hwdata))))
slouken@21
   120
		return -1;
slouken@21
   121
slouken@21
   122
/* This loop is to check if the controller is a joypad */
slouken@21
   123
slouken@21
   124
	for(i=0;i<20;i++)
slouken@21
   125
	{
slouken@255
   126
		temp=ReadJoyPort(joystick->index^1); // fix to invert amiga joyports
slouken@21
   127
		WaitTOF();
slouken@21
   128
	}
slouken@21
   129
slouken@21
   130
	if((temp&JP_TYPE_MASK)==JP_TYPE_GAMECTLR)
slouken@21
   131
		joystick->nbuttons=7;
slouken@21
   132
	else
slouken@21
   133
		joystick->nbuttons=3;
slouken@21
   134
slouken@21
   135
	joystick->nhats=0;
slouken@21
   136
	joystick->nballs=0;
slouken@21
   137
	joystick->naxes=2;
slouken@21
   138
	joystick->hwdata->joystate=0L;
slouken@21
   139
slouken@21
   140
	return 0;
slouken@21
   141
}
slouken@21
   142
slouken@21
   143
/* Function to update the state of a joystick - called as a device poll.
slouken@21
   144
 * This function shouldn't update the joystick structure directly,
slouken@21
   145
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@21
   146
 * and update joystick device state.
slouken@21
   147
 */
slouken@21
   148
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@21
   149
{
slouken@255
   150
	ULONG data;
slouken@21
   151
	int i;
slouken@21
   152
slouken@21
   153
	if(joystick->index<2)
slouken@21
   154
	{
slouken@21
   155
		data=ReadJoyPort(joystick->index);
slouken@21
   156
slouken@21
   157
		if(data&JP_DIRECTION_MASK)
slouken@21
   158
		{
slouken@21
   159
			if(data&JPF_JOY_DOWN)
slouken@21
   160
			{
slouken@21
   161
				if(!(joystick->hwdata->joystate&JPF_JOY_DOWN))
slouken@21
   162
					SDL_PrivateJoystickAxis(joystick,0,127);
slouken@21
   163
			}
slouken@21
   164
			else if(data&JPF_JOY_UP)
slouken@21
   165
			{
slouken@21
   166
				if(!(joystick->hwdata->joystate&JPF_JOY_UP))
slouken@21
   167
					SDL_PrivateJoystickAxis(joystick,0,-127);
slouken@21
   168
			}
slouken@21
   169
			else if(joystick->hwdata->joystate&(JPF_JOY_UP|JPF_JOY_DOWN))
slouken@21
   170
				SDL_PrivateJoystickAxis(joystick,0,0);
slouken@21
   171
slouken@21
   172
			if(data&JPF_JOY_LEFT)
slouken@21
   173
			{
slouken@21
   174
				if(!(joystick->hwdata->joystate&JPF_JOY_LEFT))
slouken@21
   175
					SDL_PrivateJoystickAxis(joystick,1,-127);
slouken@21
   176
			}
slouken@21
   177
			else if(data&JPF_JOY_RIGHT)
slouken@21
   178
			{
slouken@21
   179
				if(!(joystick->hwdata->joystate&JPF_JOY_RIGHT))
slouken@21
   180
					SDL_PrivateJoystickAxis(joystick,1,127);
slouken@21
   181
			}
slouken@21
   182
			else if(joystick->hwdata->joystate&(JPF_JOY_LEFT|JPF_JOY_RIGHT))
slouken@21
   183
				SDL_PrivateJoystickAxis(joystick,1,0);
slouken@21
   184
		}
slouken@21
   185
		else if(joystick->hwdata->joystate&(JPF_JOY_LEFT|JPF_JOY_RIGHT))
slouken@21
   186
		{
slouken@21
   187
				SDL_PrivateJoystickAxis(joystick,1,0);
slouken@21
   188
		}
slouken@21
   189
		else if(joystick->hwdata->joystate&(JPF_JOY_UP|JPF_JOY_DOWN))
slouken@21
   190
		{
slouken@21
   191
				SDL_PrivateJoystickAxis(joystick,0,0);
slouken@21
   192
		}
slouken@21
   193
slouken@21
   194
		for(i=0;i<joystick->nbuttons;i++)
slouken@21
   195
		{
slouken@21
   196
			if( (data&joybut[i]) )
slouken@21
   197
			{
slouken@21
   198
				if(i==1)
slouken@21
   199
					data&=(~(joybut[2]));
slouken@21
   200
slouken@21
   201
				if(!(joystick->hwdata->joystate&joybut[i]))
slouken@21
   202
					SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@21
   203
			}
slouken@21
   204
			else if(joystick->hwdata->joystate&joybut[i])
slouken@21
   205
				SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@21
   206
		}
slouken@21
   207
slouken@21
   208
		joystick->hwdata->joystate=data;
slouken@21
   209
	}
slouken@21
   210
slouken@21
   211
	return;
slouken@21
   212
}
slouken@21
   213
slouken@21
   214
/* Function to close a joystick after use */
slouken@21
   215
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@21
   216
{
slouken@21
   217
	if(joystick->hwdata)
slouken@21
   218
		free(joystick->hwdata);
slouken@21
   219
	return;
slouken@21
   220
}
slouken@21
   221
slouken@21
   222
/* Function to perform any system-specific joystick related cleanup */
slouken@21
   223
slouken@21
   224
void SDL_SYS_JoystickQuit(void)
slouken@21
   225
{
slouken@21
   226
	if(LowLevelBase)
slouken@21
   227
	{
slouken@21
   228
		CloseLibrary(LowLevelBase);
slouken@21
   229
		LowLevelBase=NULL;
slouken@21
   230
		SDL_numjoysticks=0;
slouken@21
   231
	}
slouken@21
   232
slouken@21
   233
	return;
slouken@21
   234
}
slouken@21
   235