src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Oct 2008 06:46:59 +0000
changeset 2765 f55c87ae336b
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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