src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 21:11:28 -0800
changeset 6693 3c98e4ec2180
parent 6138 4c64952a58fb
child 6698 28ab2ef7bfc9
permissions -rwxr-xr-x
Fixed iOS joystick support for new API
     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     return (1);
    41 }
    42 
    43 /* Function to get the device-dependent name of a joystick */
    44 const char *
    45 SDL_SYS_JoystickNameForIndex(int index)
    46 {
    47 	switch(index) {
    48 		case 0:
    49 			return accelerometerName;
    50 		default:
    51 			SDL_SetError("No joystick available with that index");
    52 			return NULL;
    53 	}
    54 }
    55 
    56 /* Function to open a joystick for use.
    57    The joystick to open is specified by the index field of the joystick.
    58    This should fill the nbuttons and naxes fields of the joystick structure.
    59    It returns 0, or -1 if there is an error.
    60  */
    61 int
    62 SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
    63 {
    64 	if (device_index == 0) {
    65 		joystick->naxes = 3;
    66 		joystick->nhats = 0;
    67 		joystick->nballs = 0;
    68 		joystick->nbuttons = 0;
    69 		[[SDLUIAccelerationDelegate sharedDelegate] startup];
    70 		return 0;
    71 	}
    72 	else {
    73 		SDL_SetError("No joystick available with that index");
    74 		return (-1);
    75 	}
    76 	
    77 }
    78 
    79 /* Function to update the state of a joystick - called as a device poll.
    80  * This function shouldn't update the joystick structure directly,
    81  * but instead should call SDL_PrivateJoystick*() to deliver events
    82  * and update joystick device state.
    83  */
    84 void
    85 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    86 {
    87 	
    88 	Sint16 orientation[3];
    89 	
    90 	if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
    91 	
    92 		[[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
    93 		[[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
    94 		
    95 		SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
    96 		SDL_PrivateJoystickAxis(joystick, 1, orientation[1]);
    97 		SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
    98 
    99 	}
   100 	
   101     return;
   102 }
   103 
   104 /* Function to close a joystick after use */
   105 void
   106 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   107 {
   108 	if ([[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
   109 		[[SDLUIAccelerationDelegate sharedDelegate] shutdown];
   110 	}
   111 	SDL_SetError("No joystick open with that index");
   112 
   113     return;
   114 }
   115 
   116 /* Function to perform any system-specific joystick related cleanup */
   117 void
   118 SDL_SYS_JoystickQuit(void)
   119 {
   120     return;
   121 }
   122 
   123 /* Function to perform the mapping from device index to the instance id for this index */
   124 SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
   125 {
   126     return index;
   127 }
   128 
   129 /* Function to determine is this joystick is attached to the system right now */
   130 int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
   131 {
   132     return 1;
   133 }
   134 
   135 int SDL_SYS_NumJoysticks()
   136 {
   137     return 1;
   138 }
   139 
   140 int SDL_SYS_JoystickNeedsPolling()
   141 {
   142     return 0;
   143 }
   144 
   145 void SDL_SYS_JoystickDetect()
   146 {
   147 }
   148 
   149 JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
   150 {
   151     JoystickGUID guid;
   152     // the GUID is just the first 16 chars of the name for now
   153     const char *name = SDL_SYS_JoystickNameForIndex( device_index );
   154     SDL_zero( guid );
   155     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   156     return guid;
   157 }
   158 
   159 JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
   160 {
   161     JoystickGUID guid;
   162     // the GUID is just the first 16 chars of the name for now
   163     const char *name = joystick->name;
   164     SDL_zero( guid );
   165     SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
   166     return guid;
   167 }
   168 
   169 /* vi: set ts=4 sw=4 expandtab: */