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