src/joystick/iphoneos/SDL_sysjoystick.m
author Ryan C. Gordon <icculus@icculus.org>
Sat, 14 Jun 2014 23:31:23 -0400
changeset 8860 c4133d635375
parent 8149 681eb46b8ac4
child 8921 4a9feef61c85
permissions -rw-r--r--
Removed SDL_SYS_JoystickNeedsPolling().

It was simpler to just have the polling (actually: hotplug detection)
functions return immediately if it's not an appropriate time to poll.

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