src/joystick/riscos/SDL_sysjoystick.c
author Cameron Cawley <ccawley2011@gmail.com>
Tue, 18 Jun 2019 23:55:01 +0100
branchSDL-1.2
changeset 12913 d07b4feae69c
parent 12779 8f5a01f3a9bd
permissions -rw-r--r--
riscos: Improve error reporting
slouken@630
     1
/*
slouken@630
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@630
     4
slouken@630
     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@630
     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@630
     9
slouken@630
    10
    This library is distributed in the hope that it will be useful,
slouken@630
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@630
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@630
    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@630
    18
slouken@630
    19
    Sam Lantinga
slouken@1312
    20
    slouken@libsdl.org
slouken@630
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@630
    23
slouken@1635
    24
#ifdef SDL_JOYSTICK_RISCOS
slouken@1635
    25
slouken@630
    26
/*
slouken@1035
    27
   RISC OS - Joystick support by Alan Buckley (alan_baa@hotmail.com) - 10 April 2003
slouken@630
    28
slouken@630
    29
   Note: Currently assumes joystick is present if joystick module is loaded
slouken@630
    30
   and that there is one joystick with four buttons.
slouken@630
    31
*/
slouken@630
    32
slouken@630
    33
/* This is the system specific header for the SDL joystick API */
slouken@630
    34
slouken@1418
    35
#include "SDL_events.h"
slouken@630
    36
#include "SDL_joystick.h"
slouken@1361
    37
#include "../SDL_sysjoystick.h"
slouken@1361
    38
#include "../SDL_joystick_c.h"
slouken@630
    39
slouken@630
    40
#include "kernel.h"
slouken@630
    41
ccawley2011@12778
    42
#ifndef Joystick_Read
ccawley2011@12778
    43
#define Joystick_Read 0x43F40
ccawley2011@12778
    44
#endif
slouken@630
    45
slouken@630
    46
struct joystick_hwdata 
slouken@630
    47
{
slouken@630
    48
	int joystate;
slouken@630
    49
};
slouken@630
    50
slouken@630
    51
slouken@630
    52
/* Function to scan the system for joysticks.
slouken@630
    53
 * This function should set SDL_numjoysticks to the number of available
slouken@630
    54
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@630
    55
 * It should return number of joysticks, or -1 on an unrecoverable fatal error.
slouken@630
    56
 */
slouken@630
    57
int SDL_SYS_JoystickInit(void)
slouken@630
    58
{
slouken@630
    59
	_kernel_swi_regs regs;
slouken@630
    60
slouken@630
    61
	 /* Try to read joystick 0 */
slouken@630
    62
	regs.r[0] = 0;
ccawley2011@12778
    63
	if (_kernel_swi(Joystick_Read, &regs, &regs) == NULL)
slouken@630
    64
	{
slouken@630
    65
		/* Switch works so assume we've got a joystick */
slouken@630
    66
		return 1;
slouken@630
    67
	}
slouken@630
    68
	/* Switch fails so it looks like there's no joystick here */
slouken@630
    69
slouken@630
    70
	return(0);
slouken@630
    71
}
slouken@630
    72
slouken@630
    73
/* Function to get the device-dependent name of a joystick */
slouken@630
    74
const char *SDL_SYS_JoystickName(int index)
slouken@630
    75
{
slouken@630
    76
	if (index == 0)
slouken@630
    77
	{
slouken@1035
    78
		return "RISC OS Joystick 0";
slouken@630
    79
	}
slouken@630
    80
slouken@630
    81
	SDL_SetError("No joystick available with that index");
slouken@630
    82
	return(NULL);
slouken@630
    83
}
slouken@630
    84
slouken@630
    85
/* Function to open a joystick for use.
slouken@630
    86
   The joystick to open is specified by the index field of the joystick.
slouken@630
    87
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@630
    88
   It returns 0, or -1 if there is an error.
slouken@630
    89
 */
slouken@630
    90
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
slouken@630
    91
{
ccawley2011@12913
    92
	if(!(joystick->hwdata=SDL_malloc(sizeof(struct joystick_hwdata)))) {
ccawley2011@12913
    93
		SDL_OutOfMemory();
slouken@630
    94
		return -1;
ccawley2011@12913
    95
	}
slouken@630
    96
slouken@630
    97
	/* Don't know how to get exact count of buttons so assume max of 4 for now */
slouken@630
    98
	joystick->nbuttons=4;
slouken@630
    99
slouken@630
   100
	joystick->nhats=0;
slouken@630
   101
	joystick->nballs=0;
slouken@630
   102
	joystick->naxes=2;
slouken@630
   103
	joystick->hwdata->joystate=0;
slouken@630
   104
slouken@630
   105
	return 0;
slouken@630
   106
slouken@630
   107
}
slouken@630
   108
slouken@630
   109
/* Function to update the state of a joystick - called as a device poll.
slouken@630
   110
 * This function shouldn't update the joystick structure directly,
slouken@630
   111
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@630
   112
 * and update joystick device state.
slouken@630
   113
 */
slouken@630
   114
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
slouken@630
   115
{
slouken@630
   116
	_kernel_swi_regs regs;
slouken@630
   117
	regs.r[0] = joystick->index;
slouken@630
   118
ccawley2011@12778
   119
	if (_kernel_swi(Joystick_Read, &regs, &regs) == NULL)
slouken@630
   120
	{
slouken@630
   121
		int newstate = regs.r[0];
slouken@630
   122
		int oldstate = joystick->hwdata->joystate;
slouken@630
   123
		if (newstate != oldstate)
slouken@630
   124
		{
slouken@630
   125
			if ((newstate & 0xFF) != (oldstate & 0xFF))
slouken@630
   126
			{
slouken@630
   127
				int y = regs.r[0] & 0xFF;
slouken@630
   128
				/* Convert to signed values */
slouken@630
   129
				if (y >= 128) y -= 256;
slouken@630
   130
				SDL_PrivateJoystickAxis(joystick,1,-y * 256); /* Up and down opposite to result in SDL */
slouken@630
   131
			}
slouken@630
   132
			if ((newstate & 0xFF00) != (oldstate & 0xFF00))
slouken@630
   133
			{
slouken@630
   134
				int x = (regs.r[0] & 0xFF00) >> 8;
slouken@630
   135
				if (x >= 128) x -= 256;
slouken@630
   136
				SDL_PrivateJoystickAxis(joystick,0,x * 256);
slouken@630
   137
			}
slouken@630
   138
slouken@630
   139
			if ((newstate & 0xFF0000) != (oldstate & 0xFF0000))
slouken@630
   140
			{
slouken@630
   141
				int buttons = (regs.r[0] & 0xFF0000) >> 16;
slouken@630
   142
				int oldbuttons = (oldstate & 0xFF0000) >> 16;
slouken@630
   143
				int i;
slouken@630
   144
				for (i = 0; i < joystick->nbuttons; i++)
slouken@630
   145
				{
slouken@630
   146
					if ((buttons & (1<<i)) != (oldbuttons & (1<<i)))
slouken@630
   147
					{
slouken@630
   148
						if (buttons & (1<<i)) SDL_PrivateJoystickButton(joystick,i,SDL_PRESSED);
slouken@630
   149
						else SDL_PrivateJoystickButton(joystick,i,SDL_RELEASED);
slouken@630
   150
					}
slouken@630
   151
				}
slouken@630
   152
			}
slouken@630
   153
			joystick->hwdata->joystate = newstate;
slouken@630
   154
		}		
slouken@630
   155
	}
slouken@630
   156
slouken@630
   157
	return;
slouken@630
   158
}
slouken@630
   159
slouken@630
   160
/* Function to close a joystick after use */
slouken@630
   161
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
slouken@630
   162
{
slouken@630
   163
	if(joystick->hwdata)
slouken@1336
   164
		SDL_free(joystick->hwdata);
slouken@630
   165
	return;
slouken@630
   166
}
slouken@630
   167
slouken@630
   168
/* Function to perform any system-specific joystick related cleanup */
slouken@630
   169
void SDL_SYS_JoystickQuit(void)
slouken@630
   170
{
slouken@630
   171
	SDL_numjoysticks=0;
slouken@630
   172
slouken@630
   173
	return;
slouken@630
   174
}
slouken@630
   175
slouken@1635
   176
#endif /* SDL_JOYSTICK_RISCOS */