src/joystick/iphoneos/SDL_sysjoystick.m
changeset 2765 f55c87ae336b
child 2859 99210400e8b9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Sat Oct 04 06:46:59 2008 +0000
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* This is the system specific header for the SDL joystick API */
    1.28 +
    1.29 +#include "SDL_joystick.h"
    1.30 +#include "../SDL_sysjoystick.h"
    1.31 +#include "../SDL_joystick_c.h"
    1.32 +#import "SDLUIAccelerationDelegate.h"
    1.33 +
    1.34 +const char *accelerometerName = "iPhone accelerometer";
    1.35 +
    1.36 +/* Function to scan the system for joysticks.
    1.37 + * This function should set SDL_numjoysticks to the number of available
    1.38 + * joysticks.  Joystick 0 should be the system default joystick.
    1.39 + * It should return 0, or -1 on an unrecoverable fatal error.
    1.40 + */
    1.41 +int
    1.42 +SDL_SYS_JoystickInit(void)
    1.43 +{
    1.44 +    SDL_numjoysticks = 1;
    1.45 +    return (1);
    1.46 +}
    1.47 +
    1.48 +/* Function to get the device-dependent name of a joystick */
    1.49 +const char *
    1.50 +SDL_SYS_JoystickName(int index)
    1.51 +{
    1.52 +	switch(index) {
    1.53 +		case 0:
    1.54 +			return accelerometerName;
    1.55 +		default:
    1.56 +			SDL_SetError("No joystick available with that index");
    1.57 +			return NULL;
    1.58 +	}
    1.59 +}
    1.60 +
    1.61 +/* Function to open a joystick for use.
    1.62 +   The joystick to open is specified by the index field of the joystick.
    1.63 +   This should fill the nbuttons and naxes fields of the joystick structure.
    1.64 +   It returns 0, or -1 if there is an error.
    1.65 + */
    1.66 +int
    1.67 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
    1.68 +{
    1.69 +	if (joystick->index == 0) {
    1.70 +		joystick->naxes = 3;
    1.71 +		joystick->nhats = 0;
    1.72 +		joystick->nballs = 0;
    1.73 +		joystick->nbuttons = 0;
    1.74 +		joystick->name  = accelerometerName;
    1.75 +		[[SDLUIAccelerationDelegate sharedDelegate] startup];
    1.76 +		return 0;
    1.77 +	}
    1.78 +	else {
    1.79 +		SDL_SetError("No joystick available with that index");
    1.80 +		return (-1);
    1.81 +	}
    1.82 +	
    1.83 +}
    1.84 +
    1.85 +/* Function to update the state of a joystick - called as a device poll.
    1.86 + * This function shouldn't update the joystick structure directly,
    1.87 + * but instead should call SDL_PrivateJoystick*() to deliver events
    1.88 + * and update joystick device state.
    1.89 + */
    1.90 +void
    1.91 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    1.92 +{
    1.93 +	
    1.94 +	Sint16 orientation[3];
    1.95 +	
    1.96 +	if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
    1.97 +	
    1.98 +		[[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
    1.99 +		[[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
   1.100 +		
   1.101 +		SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
   1.102 +		SDL_PrivateJoystickAxis(joystick, 1, orientation[1]);
   1.103 +		SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
   1.104 +
   1.105 +	}
   1.106 +	
   1.107 +    return;
   1.108 +}
   1.109 +
   1.110 +/* Function to close a joystick after use */
   1.111 +void
   1.112 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.113 +{
   1.114 +	if (joystick->index == 0 && [[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
   1.115 +		[[SDLUIAccelerationDelegate sharedDelegate] shutdown];
   1.116 +	}
   1.117 +	SDL_SetError("No joystick open with that index");
   1.118 +
   1.119 +    return;
   1.120 +}
   1.121 +
   1.122 +/* Function to perform any system-specific joystick related cleanup */
   1.123 +void
   1.124 +SDL_SYS_JoystickQuit(void)
   1.125 +{
   1.126 +    return;
   1.127 +}
   1.128 +/* vi: set ts=4 sw=4 expandtab: */