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