src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5262 b530ef003506
child 6138 4c64952a58fb
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@2765
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2765
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2765
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2765
    20
*/
slouken@2765
    21
#include "SDL_config.h"
slouken@2765
    22
slouken@2765
    23
/* This is the system specific header for the SDL joystick API */
slouken@2765
    24
slouken@2765
    25
#include "SDL_joystick.h"
slouken@2765
    26
#include "../SDL_sysjoystick.h"
slouken@2765
    27
#include "../SDL_joystick_c.h"
slouken@2765
    28
#import "SDLUIAccelerationDelegate.h"
slouken@2765
    29
slouken@2765
    30
const char *accelerometerName = "iPhone accelerometer";
slouken@2765
    31
slouken@2765
    32
/* Function to scan the system for joysticks.
slouken@2765
    33
 * This function should set SDL_numjoysticks to the number of available
slouken@2765
    34
 * joysticks.  Joystick 0 should be the system default joystick.
slouken@2765
    35
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@2765
    36
 */
slouken@2765
    37
int
slouken@2765
    38
SDL_SYS_JoystickInit(void)
slouken@2765
    39
{
slouken@2765
    40
    SDL_numjoysticks = 1;
slouken@2765
    41
    return (1);
slouken@2765
    42
}
slouken@2765
    43
slouken@2765
    44
/* Function to get the device-dependent name of a joystick */
slouken@2765
    45
const char *
slouken@2765
    46
SDL_SYS_JoystickName(int index)
slouken@2765
    47
{
slouken@2765
    48
	switch(index) {
slouken@2765
    49
		case 0:
slouken@2765
    50
			return accelerometerName;
slouken@2765
    51
		default:
slouken@2765
    52
			SDL_SetError("No joystick available with that index");
slouken@2765
    53
			return NULL;
slouken@2765
    54
	}
slouken@2765
    55
}
slouken@2765
    56
slouken@2765
    57
/* Function to open a joystick for use.
slouken@2765
    58
   The joystick to open is specified by the index field of the joystick.
slouken@2765
    59
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@2765
    60
   It returns 0, or -1 if there is an error.
slouken@2765
    61
 */
slouken@2765
    62
int
slouken@2765
    63
SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
slouken@2765
    64
{
slouken@2765
    65
	if (joystick->index == 0) {
slouken@2765
    66
		joystick->naxes = 3;
slouken@2765
    67
		joystick->nhats = 0;
slouken@2765
    68
		joystick->nballs = 0;
slouken@2765
    69
		joystick->nbuttons = 0;
slouken@2765
    70
		joystick->name  = accelerometerName;
slouken@2765
    71
		[[SDLUIAccelerationDelegate sharedDelegate] startup];
slouken@2765
    72
		return 0;
slouken@2765
    73
	}
slouken@2765
    74
	else {
slouken@2765
    75
		SDL_SetError("No joystick available with that index");
slouken@2765
    76
		return (-1);
slouken@2765
    77
	}
slouken@2765
    78
	
slouken@2765
    79
}
slouken@2765
    80
slouken@2765
    81
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
    82
 * This function shouldn't update the joystick structure directly,
slouken@2765
    83
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
    84
 * and update joystick device state.
slouken@2765
    85
 */
slouken@2765
    86
void
slouken@2765
    87
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
    88
{
slouken@2765
    89
	
slouken@2765
    90
	Sint16 orientation[3];
slouken@2765
    91
	
slouken@2765
    92
	if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
slouken@2765
    93
	
slouken@2765
    94
		[[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
slouken@2765
    95
		[[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
slouken@2765
    96
		
slouken@2765
    97
		SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
slouken@2765
    98
		SDL_PrivateJoystickAxis(joystick, 1, orientation[1]);
slouken@2765
    99
		SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
slouken@2765
   100
slouken@2765
   101
	}
slouken@2765
   102
	
slouken@2765
   103
    return;
slouken@2765
   104
}
slouken@2765
   105
slouken@2765
   106
/* Function to close a joystick after use */
slouken@2765
   107
void
slouken@2765
   108
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   109
{
slouken@2765
   110
	if (joystick->index == 0 && [[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
slouken@2765
   111
		[[SDLUIAccelerationDelegate sharedDelegate] shutdown];
slouken@2765
   112
	}
slouken@2765
   113
	SDL_SetError("No joystick open with that index");
slouken@2765
   114
slouken@2765
   115
    return;
slouken@2765
   116
}
slouken@2765
   117
slouken@2765
   118
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   119
void
slouken@2765
   120
SDL_SYS_JoystickQuit(void)
slouken@2765
   121
{
slouken@2765
   122
    return;
slouken@2765
   123
}
slouken@2765
   124
/* vi: set ts=4 sw=4 expandtab: */