src/joystick/iphoneos/SDL_sysjoystick.m
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 6885 700f1b25f77f
child 7191 75360622e65f
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


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