src/joystick/iphoneos/SDL_sysjoystick.m
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 17:47:36 +0000
branchgsoc2008_iphone
changeset 2363 49b243db2e04
parent 2348 dd32c95434f4
permissions -rw-r--r--
Fixed a bug where SDL_SYS_JoystickUpdate would send update events even when the accelerometer delegate hadn't received new information from the hardware.
hfutrell@2348
     1
/*
hfutrell@2348
     2
    SDL - Simple DirectMedia Layer
hfutrell@2348
     3
    Copyright (C) 1997-2006 Sam Lantinga
hfutrell@2348
     4
hfutrell@2348
     5
    This library is free software; you can redistribute it and/or
hfutrell@2348
     6
    modify it under the terms of the GNU Lesser General Public
hfutrell@2348
     7
    License as published by the Free Software Foundation; either
hfutrell@2348
     8
    version 2.1 of the License, or (at your option) any later version.
hfutrell@2348
     9
hfutrell@2348
    10
    This library is distributed in the hope that it will be useful,
hfutrell@2348
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
hfutrell@2348
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
hfutrell@2348
    13
    Lesser General Public License for more details.
hfutrell@2348
    14
hfutrell@2348
    15
    You should have received a copy of the GNU Lesser General Public
hfutrell@2348
    16
    License along with this library; if not, write to the Free Software
hfutrell@2348
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
hfutrell@2348
    18
hfutrell@2348
    19
    Sam Lantinga
hfutrell@2348
    20
    slouken@libsdl.org
hfutrell@2348
    21
*/
hfutrell@2348
    22
#include "SDL_config.h"
hfutrell@2348
    23
hfutrell@2348
    24
/* This is the system specific header for the SDL joystick API */
hfutrell@2348
    25
hfutrell@2348
    26
#include "SDL_joystick.h"
hfutrell@2348
    27
#include "../SDL_sysjoystick.h"
hfutrell@2348
    28
#include "../SDL_joystick_c.h"
hfutrell@2348
    29
#import "SDLUIAccelerationDelegate.h"
hfutrell@2348
    30
hfutrell@2348
    31
const char *accelerometerName = "iPhone accelerometer";
hfutrell@2348
    32
hfutrell@2348
    33
/* Function to scan the system for joysticks.
hfutrell@2348
    34
 * This function should set SDL_numjoysticks to the number of available
hfutrell@2348
    35
 * joysticks.  Joystick 0 should be the system default joystick.
hfutrell@2348
    36
 * It should return 0, or -1 on an unrecoverable fatal error.
hfutrell@2348
    37
 */
hfutrell@2348
    38
int
hfutrell@2348
    39
SDL_SYS_JoystickInit(void)
hfutrell@2348
    40
{
hfutrell@2348
    41
    SDL_numjoysticks = 1;
hfutrell@2348
    42
    return (1);
hfutrell@2348
    43
}
hfutrell@2348
    44
hfutrell@2348
    45
/* Function to get the device-dependent name of a joystick */
hfutrell@2348
    46
const char *
hfutrell@2348
    47
SDL_SYS_JoystickName(int index)
hfutrell@2348
    48
{
hfutrell@2348
    49
	switch(index) {
hfutrell@2348
    50
		case 0:
hfutrell@2348
    51
			return accelerometerName;
hfutrell@2348
    52
		default:
hfutrell@2348
    53
			SDL_SetError("No joystick available with that index");
hfutrell@2348
    54
			return NULL;
hfutrell@2348
    55
	}
hfutrell@2348
    56
}
hfutrell@2348
    57
hfutrell@2348
    58
/* Function to open a joystick for use.
hfutrell@2348
    59
   The joystick to open is specified by the index field of the joystick.
hfutrell@2348
    60
   This should fill the nbuttons and naxes fields of the joystick structure.
hfutrell@2348
    61
   It returns 0, or -1 if there is an error.
hfutrell@2348
    62
 */
hfutrell@2348
    63
int
hfutrell@2348
    64
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
hfutrell@2348
    65
{
hfutrell@2348
    66
	if (joystick->index == 0) {
hfutrell@2348
    67
		joystick->naxes = 3;
hfutrell@2348
    68
		joystick->nhats = 0;
hfutrell@2348
    69
		joystick->nballs = 0;
hfutrell@2348
    70
		joystick->nbuttons = 0;
hfutrell@2348
    71
		joystick->name  = accelerometerName;
hfutrell@2348
    72
		[[SDLUIAccelerationDelegate sharedDelegate] startup];
hfutrell@2348
    73
		return 0;
hfutrell@2348
    74
	}
hfutrell@2348
    75
	else {
hfutrell@2348
    76
		SDL_SetError("No joystick available with that index");
hfutrell@2348
    77
		return (-1);
hfutrell@2348
    78
	}
hfutrell@2348
    79
	
hfutrell@2348
    80
}
hfutrell@2348
    81
hfutrell@2348
    82
/* Function to update the state of a joystick - called as a device poll.
hfutrell@2348
    83
 * This function shouldn't update the joystick structure directly,
hfutrell@2348
    84
 * but instead should call SDL_PrivateJoystick*() to deliver events
hfutrell@2348
    85
 * and update joystick device state.
hfutrell@2348
    86
 */
hfutrell@2348
    87
void
hfutrell@2348
    88
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
hfutrell@2348
    89
{
hfutrell@2348
    90
	
hfutrell@2348
    91
	Sint16 orientation[3];
hfutrell@2348
    92
	
hfutrell@2348
    93
	if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
hfutrell@2348
    94
	
hfutrell@2363
    95
		[[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
hfutrell@2348
    96
		[[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
hfutrell@2348
    97
		
hfutrell@2348
    98
		SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
hfutrell@2348
    99
		SDL_PrivateJoystickAxis(joystick, 1, orientation[1]);
hfutrell@2348
   100
		SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
hfutrell@2348
   101
hfutrell@2348
   102
	}
hfutrell@2348
   103
	
hfutrell@2348
   104
    return;
hfutrell@2348
   105
}
hfutrell@2348
   106
hfutrell@2348
   107
/* Function to close a joystick after use */
hfutrell@2348
   108
void
hfutrell@2348
   109
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
hfutrell@2348
   110
{
hfutrell@2348
   111
	if (joystick->index == 0 && [[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
hfutrell@2348
   112
		[[SDLUIAccelerationDelegate sharedDelegate] shutdown];
hfutrell@2348
   113
	}
hfutrell@2348
   114
	SDL_SetError("No joystick open with that index");
hfutrell@2348
   115
hfutrell@2348
   116
    return;
hfutrell@2348
   117
}
hfutrell@2348
   118
hfutrell@2348
   119
/* Function to perform any system-specific joystick related cleanup */
hfutrell@2348
   120
void
hfutrell@2348
   121
SDL_SYS_JoystickQuit(void)
hfutrell@2348
   122
{
hfutrell@2348
   123
    return;
hfutrell@2348
   124
}
hfutrell@2348
   125
/* vi: set ts=4 sw=4 expandtab: */