src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 26 Nov 2012 22:27:49 -0800
changeset 6698 28ab2ef7bfc9
parent 6693 3c98e4ec2180
child 6707 70eeb7e3ec2f
permissions -rwxr-xr-x
Completed adding new hotplug stubs for the joystick implementations
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@6698
    71
	} else {
slouken@2765
    72
		SDL_SetError("No joystick available with that index");
slouken@2765
    73
		return (-1);
slouken@2765
    74
	}
slouken@2765
    75
}
slouken@2765
    76
slouken@2765
    77
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
    78
 * This function shouldn't update the joystick structure directly,
slouken@2765
    79
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
    80
 * and update joystick device state.
slouken@2765
    81
 */
slouken@2765
    82
void
slouken@2765
    83
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
    84
{
slouken@2765
    85
	
slouken@2765
    86
	Sint16 orientation[3];
slouken@2765
    87
	
slouken@2765
    88
	if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
slouken@2765
    89
	
slouken@2765
    90
		[[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
slouken@2765
    91
		[[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
slouken@2765
    92
		
slouken@2765
    93
		SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
slouken@2765
    94
		SDL_PrivateJoystickAxis(joystick, 1, orientation[1]);
slouken@2765
    95
		SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
slouken@2765
    96
slouken@2765
    97
	}
slouken@2765
    98
	
slouken@2765
    99
    return;
slouken@2765
   100
}
slouken@2765
   101
slouken@2765
   102
/* Function to close a joystick after use */
slouken@2765
   103
void
slouken@2765
   104
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   105
{
slouken@6693
   106
	if ([[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
slouken@2765
   107
		[[SDLUIAccelerationDelegate sharedDelegate] shutdown];
slouken@2765
   108
	}
slouken@2765
   109
	SDL_SetError("No joystick open with that index");
slouken@2765
   110
slouken@2765
   111
    return;
slouken@2765
   112
}
slouken@2765
   113
slouken@2765
   114
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   115
void
slouken@2765
   116
SDL_SYS_JoystickQuit(void)
slouken@2765
   117
{
slouken@2765
   118
    return;
slouken@2765
   119
}
slouken@6693
   120
slouken@6693
   121
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6693
   122
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
slouken@6693
   123
{
slouken@6693
   124
    return index;
slouken@6693
   125
}
slouken@6693
   126
slouken@6693
   127
/* Function to determine is this joystick is attached to the system right now */
slouken@6693
   128
int SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6693
   129
{
slouken@6693
   130
    return 1;
slouken@6693
   131
}
slouken@6693
   132
slouken@6693
   133
int SDL_SYS_NumJoysticks()
slouken@6693
   134
{
slouken@6693
   135
    return 1;
slouken@6693
   136
}
slouken@6693
   137
slouken@6693
   138
int SDL_SYS_JoystickNeedsPolling()
slouken@6693
   139
{
slouken@6693
   140
    return 0;
slouken@6693
   141
}
slouken@6693
   142
slouken@6693
   143
void SDL_SYS_JoystickDetect()
slouken@6693
   144
{
slouken@6693
   145
}
slouken@6693
   146
slouken@6693
   147
JoystickGUID SDL_SYS_PrivateJoystickGetDeviceGUID( int device_index )
slouken@6693
   148
{
slouken@6693
   149
    JoystickGUID guid;
slouken@6693
   150
    // the GUID is just the first 16 chars of the name for now
slouken@6693
   151
    const char *name = SDL_SYS_JoystickNameForIndex( device_index );
slouken@6693
   152
    SDL_zero( guid );
slouken@6693
   153
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6693
   154
    return guid;
slouken@6693
   155
}
slouken@6693
   156
slouken@6693
   157
JoystickGUID SDL_SYS_PrivateJoystickGetGUID(SDL_Joystick * joystick)
slouken@6693
   158
{
slouken@6693
   159
    JoystickGUID guid;
slouken@6693
   160
    // the GUID is just the first 16 chars of the name for now
slouken@6693
   161
    const char *name = joystick->name;
slouken@6693
   162
    SDL_zero( guid );
slouken@6693
   163
    SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
slouken@6693
   164
    return guid;
slouken@6693
   165
}
slouken@6693
   166
slouken@2765
   167
/* vi: set ts=4 sw=4 expandtab: */