annotate src/joystick/iphoneos/SDL_sysjoystick.m @ 3583:65f66674b7fb

Allocate memory only if we're going to overwrite an environment variable
author Sam Lantinga <slouken@libsdl.org>
date Wed, 16 Dec 2009 16:08:17 +0000
parents 99210400e8b9
children b530ef003506
rev   line source
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
2859
99210400e8b9 Updated copyright date
Sam Lantinga <slouken@libsdl.org>
parents: 2765
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 #include "SDL_config.h"
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 /* This is the system specific header for the SDL joystick API */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 #include "SDL_joystick.h"
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 #include "../SDL_sysjoystick.h"
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 #include "../SDL_joystick_c.h"
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 #import "SDLUIAccelerationDelegate.h"
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 const char *accelerometerName = "iPhone accelerometer";
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 /* Function to scan the system for joysticks.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 * This function should set SDL_numjoysticks to the number of available
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 * joysticks. Joystick 0 should be the system default joystick.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 * It should return 0, or -1 on an unrecoverable fatal error.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 int
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 SDL_SYS_JoystickInit(void)
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 SDL_numjoysticks = 1;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42 return (1);
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 /* Function to get the device-dependent name of a joystick */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 const char *
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 SDL_SYS_JoystickName(int index)
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 switch(index) {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 case 0:
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 return accelerometerName;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 default:
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 SDL_SetError("No joystick available with that index");
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 return NULL;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 /* Function to open a joystick for use.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 The joystick to open is specified by the index field of the joystick.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 This should fill the nbuttons and naxes fields of the joystick structure.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 It returns 0, or -1 if there is an error.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 int
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 if (joystick->index == 0) {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 joystick->naxes = 3;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 joystick->nhats = 0;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 joystick->nballs = 0;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 joystick->nbuttons = 0;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 joystick->name = accelerometerName;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 [[SDLUIAccelerationDelegate sharedDelegate] startup];
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 return 0;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 else {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 SDL_SetError("No joystick available with that index");
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 return (-1);
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 /* Function to update the state of a joystick - called as a device poll.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 * This function shouldn't update the joystick structure directly,
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 * but instead should call SDL_PrivateJoystick*() to deliver events
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 * and update joystick device state.
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 void
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 Sint16 orientation[3];
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 if ([[SDLUIAccelerationDelegate sharedDelegate] hasNewData]) {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 [[SDLUIAccelerationDelegate sharedDelegate] getLastOrientation: orientation];
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 [[SDLUIAccelerationDelegate sharedDelegate] setHasNewData: NO];
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 SDL_PrivateJoystickAxis(joystick, 0, orientation[0]);
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 SDL_PrivateJoystickAxis(joystick, 1, orientation[1]);
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 SDL_PrivateJoystickAxis(joystick, 2, orientation[2]);
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 return;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 /* Function to close a joystick after use */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 void
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 if (joystick->index == 0 && [[SDLUIAccelerationDelegate sharedDelegate] isRunning]) {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 [[SDLUIAccelerationDelegate sharedDelegate] shutdown];
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 SDL_SetError("No joystick open with that index");
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 return;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 /* Function to perform any system-specific joystick related cleanup */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 void
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 SDL_SYS_JoystickQuit(void)
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 {
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 return;
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 }
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
125 /* vi: set ts=4 sw=4 expandtab: */