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