src/joystick/amigaos/SDL_sysjoystick.c
author Sam Lantinga
Sat, 25 Feb 2006 15:01:44 +0000
changeset 1429 aff0170f9f1b
parent 1402 d910939febfa
child 1635 92947e3a18db
permissions -rw-r--r--
Date: Sat, 25 Feb 2006 14:26:19 +0100 (CET)
From: "Torsten Giebl"
Subject: [SDL] testpalette.diff

To get rid of Mousecursor Fullscreen problems on Mac OSX.
     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 #include "SDL_config.h"
    23 
    24 /* This is the system specific header for the SDL joystick API */
    25 
    26 #include <libraries/lowlevel.h>
    27 #if defined(__SASC) || defined(STORMC4_WOS)
    28 #include <proto/exec.h>
    29 #include <proto/lowlevel.h>
    30 #include <proto/graphics.h>
    31 #else
    32 #include <inline/exec.h>
    33 #include <inline/lowlevel.h>
    34 #include <inline/graphics.h>
    35 #endif
    36 #include "mydebug.h"
    37 
    38 extern struct ExecBase *SysBase;
    39 extern struct GfxBase *GfxBase;
    40 
    41 #include "SDL_joystick.h"
    42 #include "../SDL_sysjoystick.h"
    43 #include "../SDL_joystick_c.h"
    44 
    45 /* Function to scan the system for joysticks.
    46  * This function should set SDL_numjoysticks to the number of available
    47  * joysticks.  Joystick 0 should be the system default joystick.
    48  * It should return 0, or -1 on an unrecoverable fatal error.
    49  */
    50 
    51 
    52 /* Amiga specific datas */
    53 struct Library *LowLevelBase=NULL;
    54 
    55 ULONG joybut[]=
    56 {
    57 	JPF_BUTTON_RED,
    58 	JPF_BUTTON_BLUE,
    59 	JPF_BUTTON_PLAY,
    60 	JPF_BUTTON_YELLOW,
    61 	JPF_BUTTON_GREEN,
    62 	JPF_BUTTON_FORWARD,
    63 	JPF_BUTTON_REVERSE,
    64 };
    65 
    66 struct joystick_hwdata
    67 {
    68 	ULONG joystate;
    69 };
    70 
    71 int SDL_SYS_JoystickInit(void)
    72 {
    73 	if(!LowLevelBase)
    74 	{
    75 		if(LowLevelBase=OpenLibrary("lowlevel.library",37))
    76 			return 2;
    77 	}
    78 	else
    79 		return 2;
    80 
    81 	D(bug("%ld joysticks available.\n",SDL_numjoysticks));
    82 
    83 	return 0;
    84 }
    85 
    86 /* Function to get the device-dependent name of a joystick */
    87 const char *SDL_SYS_JoystickName(int index)
    88 {
    89 	if(index<2&&LowLevelBase)
    90 	{
    91 		switch(index)
    92 		{
    93 			case 0:
    94 				return "Port 1 Amiga Joystick/Joypad";
    95 			case 1:
    96 				return "Port 2 Amiga Joystick/Joypad";
    97 		}
    98 	}
    99 
   100 	SDL_SetError("No joystick available with that index");
   101 	return(NULL);
   102 }
   103 
   104 /* Function to open a joystick for use.
   105    The joystick to open is specified by the index field of the joystick.
   106    This should fill the nbuttons and naxes fields of the joystick structure.
   107    It returns 0, or -1 if there is an error.
   108  */
   109 
   110 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
   111 {
   112 	ULONG temp,i;
   113 	D(bug("Opening joystick %ld\n",joystick->index));
   114 
   115 	if(!(joystick->hwdata=SDL_malloc(sizeof(struct joystick_hwdata))))
   116 		return -1;
   117 
   118 /* This loop is to check if the controller is a joypad */
   119 
   120 	for(i=0;i<20;i++)
   121 	{
   122 		temp=ReadJoyPort(joystick->index^1); // fix to invert amiga joyports
   123 		WaitTOF();
   124 	}
   125 
   126 	if((temp&JP_TYPE_MASK)==JP_TYPE_GAMECTLR)
   127 		joystick->nbuttons=7;
   128 	else
   129 		joystick->nbuttons=3;
   130 
   131 	joystick->nhats=0;
   132 	joystick->nballs=0;
   133 	joystick->naxes=2;
   134 	joystick->hwdata->joystate=0L;
   135 
   136 	return 0;
   137 }
   138 
   139 /* Function to update the state of a joystick - called as a device poll.
   140  * This function shouldn't update the joystick structure directly,
   141  * but instead should call SDL_PrivateJoystick*() to deliver events
   142  * and update joystick device state.
   143  */
   144 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
   145 {
   146 	ULONG data;
   147 	int i;
   148 
   149 	if(joystick->index<2)
   150 	{
   151 		data=ReadJoyPort(joystick->index);
   152 
   153 		if(data&JP_DIRECTION_MASK)
   154 		{
   155 			if(data&JPF_JOY_DOWN)
   156 			{
   157 				if(!(joystick->hwdata->joystate&JPF_JOY_DOWN))
   158 					SDL_PrivateJoystickAxis(joystick,0,127);
   159 			}
   160 			else if(data&JPF_JOY_UP)
   161 			{
   162 				if(!(joystick->hwdata->joystate&JPF_JOY_UP))
   163 					SDL_PrivateJoystickAxis(joystick,0,-127);
   164 			}
   165 			else if(joystick->hwdata->joystate&(JPF_JOY_UP|JPF_JOY_DOWN))
   166 				SDL_PrivateJoystickAxis(joystick,0,0);
   167 
   168 			if(data&JPF_JOY_LEFT)
   169 			{
   170 				if(!(joystick->hwdata->joystate&JPF_JOY_LEFT))
   171 					SDL_PrivateJoystickAxis(joystick,1,-127);
   172 			}
   173 			else if(data&JPF_JOY_RIGHT)
   174 			{
   175 				if(!(joystick->hwdata->joystate&JPF_JOY_RIGHT))
   176 					SDL_PrivateJoystickAxis(joystick,1,127);
   177 			}
   178 			else if(joystick->hwdata->joystate&(JPF_JOY_LEFT|JPF_JOY_RIGHT))
   179 				SDL_PrivateJoystickAxis(joystick,1,0);
   180 		}
   181 		else if(joystick->hwdata->joystate&(JPF_JOY_LEFT|JPF_JOY_RIGHT))
   182 		{
   183 				SDL_PrivateJoystickAxis(joystick,1,0);
   184 		}
   185 		else if(joystick->hwdata->joystate&(JPF_JOY_UP|JPF_JOY_DOWN))
   186 		{
   187 				SDL_PrivateJoystickAxis(joystick,0,0);
   188 		}
   189 
   190 		for(i=0;i<joystick->nbuttons;i++)
   191 		{
   192 			if( (data&joybut[i]) )
   193 			{
   194 				if(i==1)
   195 					data&=(~(joybut[2]));
   196 
   197 				if(!(joystick->hwdata->joystate&joybut[i]))
   198 					SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
   199 			}
   200 			else if(joystick->hwdata->joystate&joybut[i])
   201 				SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
   202 		}
   203 
   204 		joystick->hwdata->joystate=data;
   205 	}
   206 
   207 	return;
   208 }
   209 
   210 /* Function to close a joystick after use */
   211 void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
   212 {
   213 	if(joystick->hwdata)
   214 		SDL_free(joystick->hwdata);
   215 	return;
   216 }
   217 
   218 /* Function to perform any system-specific joystick related cleanup */
   219 
   220 void SDL_SYS_JoystickQuit(void)
   221 {
   222 	if(LowLevelBase)
   223 	{
   224 		CloseLibrary(LowLevelBase);
   225 		LowLevelBase=NULL;
   226 		SDL_numjoysticks=0;
   227 	}
   228 
   229 	return;
   230 }
   231