Mercurial > sdl-ios-xcode
annotate src/joystick/beos/SDL_bejoystick.cc @ 1192:54aa9aa32327
To: sdl@libsdl.org
From: Christian Walther <cwalther@gmx.ch>
Date: Fri, 18 Nov 2005 23:39:02 +0100
Subject: [SDL] Mouse position bugs on Mac OS X
The attached patch fixes a few bugs in SDL related to the mouse position
in windowed mode on Mac OS X, reproduced using the attached minimal test
program - at least here on 10.3.9, with SDL CVS from today. Could anyone
test whether the bugs exist and are fixed by the patch on 10.2 and 10.4?
1. When using OpenGL, the vertical mouse positions obtained through
events or SDL_GetMouseState() are off by one.
2. When using OpenGL, SDL_WarpMouse() inverts the y coordinate.
3. Clicks on the topmost pixel row of the window are not recognized.
1 and 2 do not occur in non-OpenGL mode, while 3 does. All three only
occur in windowed mode, not in fullscreen.
The cause for 1 and 3 is that in Cocoa, "the location of the mouse"
seems to be defined as "the location of the top left corner of the mouse
pointer's hot pixel" (this is not documented, it's just what I found out
here), which together with the fact that Cocoa's usual y coordinates
start at the bottom and increase upwards means that the y coordinate of
the mouse runs from 1 to h, not from 0 to h-1, in a window of height h.
If it does work on 10.2 and 10.4 (I'll try to test it as soon as I can,
but at the moment all I have at hand is 10.3.9), can this be applied to
the CVS?
-Christian
To: sdl@libsdl.org
From: Christian Walther <cwalther@gmx.ch>
Date: Mon, 28 Nov 2005 10:41:51 +0100
Subject: [SDL] Re: Mouse position bugs on Mac OS X
I wrote:
> I'll try to test it as soon as I can, but at the moment all I have at hand is 10.3.9
So, here are the results of my tests (with patched and unpatched
frameworks compiled with Xcode 1.5 (gcc 3.3) on 10.3.9):
On 10.1.5, my test program doesn't run because of "Undefined symbols:
SDL undefined reference to _CGMainDisplayID expected to be defined in
Carbon". I guess not supporting 10.1 was a deliberate decision then and
that's OK with me.
On 10.2.8, 10.3.9, and 10.4.0, the bugs exist as described in my
original post and are fixed by my patch. That is, there is no difference
between pre/post 10.3 and the patched version works correctly in all
combinations of GL/non-GL and windowed/fullscreen.
I therefore recommend the patch for inclusion.
-Christian
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 28 Nov 2005 13:58:26 +0000 |
parents | b8d311d90021 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* This is the system specific header for the SDL joystick API */ | |
29 | |
30 #include <stdio.h> | |
31 #include <stdlib.h> | |
32 #include <string.h> | |
33 | |
34 #include <be/support/String.h> | |
35 #include <be/device/Joystick.h> | |
36 | |
37 extern "C" { | |
38 | |
39 #include "SDL_error.h" | |
40 #include "SDL_joystick.h" | |
41 #include "SDL_sysjoystick.h" | |
42 #include "SDL_joystick_c.h" | |
43 | |
44 | |
45 /* The maximum number of joysticks we'll detect */ | |
46 #define MAX_JOYSTICKS 16 | |
47 | |
48 /* A list of available joysticks */ | |
49 static char *SDL_joyport[MAX_JOYSTICKS]; | |
50 static char *SDL_joyname[MAX_JOYSTICKS]; | |
51 | |
52 /* The private structure used to keep track of a joystick */ | |
53 struct joystick_hwdata { | |
54 BJoystick *stick; | |
55 uint8 *new_hats; | |
56 int16 *new_axes; | |
57 }; | |
58 | |
59 /* Function to scan the system for joysticks. | |
60 * This function should set SDL_numjoysticks to the number of available | |
61 * joysticks. Joystick 0 should be the system default joystick. | |
62 * It should return 0, or -1 on an unrecoverable fatal error. | |
63 */ | |
64 int SDL_SYS_JoystickInit(void) | |
65 { | |
66 BJoystick joystick; | |
67 int numjoysticks; | |
68 int i; | |
69 int32 nports; | |
70 char name[B_OS_NAME_LENGTH]; | |
71 | |
72 /* Search for attached joysticks */ | |
73 nports = joystick.CountDevices(); | |
74 numjoysticks = 0; | |
75 memset(SDL_joyport, 0, (sizeof SDL_joyport)); | |
76 memset(SDL_joyname, 0, (sizeof SDL_joyname)); | |
77 for ( i=0; (SDL_numjoysticks < MAX_JOYSTICKS) && (i < nports); ++i ) { | |
78 if ( joystick.GetDeviceName(i, name) == B_OK ) { | |
79 if ( joystick.Open(name) != B_ERROR ) { | |
80 BString stick_name; | |
81 joystick.GetControllerName(&stick_name); | |
82 SDL_joyport[numjoysticks] = strdup(name); | |
83 SDL_joyname[numjoysticks] = | |
84 strdup(stick_name.String()); | |
85 numjoysticks++; | |
86 joystick.Close(); | |
87 } | |
88 } | |
89 } | |
90 return(numjoysticks); | |
91 } | |
92 | |
93 /* Function to get the device-dependent name of a joystick */ | |
94 const char *SDL_SYS_JoystickName(int index) | |
95 { | |
96 return SDL_joyname[index]; | |
97 } | |
98 | |
99 /* Function to open a joystick for use. | |
100 The joystick to open is specified by the index field of the joystick. | |
101 This should fill the nbuttons and naxes fields of the joystick structure. | |
102 It returns 0, or -1 if there is an error. | |
103 */ | |
104 int SDL_SYS_JoystickOpen(SDL_Joystick *joystick) | |
105 { | |
106 BJoystick *stick; | |
107 | |
108 /* Create the joystick data structure */ | |
109 joystick->hwdata = (struct joystick_hwdata *) | |
110 malloc(sizeof(*joystick->hwdata)); | |
111 if ( joystick->hwdata == NULL ) { | |
112 SDL_OutOfMemory(); | |
113 return(-1); | |
114 } | |
115 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); | |
116 stick = new BJoystick; | |
117 joystick->hwdata->stick = stick; | |
118 | |
119 /* Open the requested joystick for use */ | |
120 if ( stick->Open(SDL_joyport[joystick->index]) == B_ERROR ) { | |
121 SDL_SetError("Unable to open joystick"); | |
122 SDL_SYS_JoystickClose(joystick); | |
123 return(-1); | |
124 } | |
125 | |
126 /* Set the joystick to calibrated mode */ | |
127 stick->EnableCalibration(); | |
128 | |
129 /* Get the number of buttons, hats, and axes on the joystick */ | |
130 joystick->nbuttons = stick->CountButtons(); | |
131 joystick->naxes = stick->CountAxes(); | |
132 joystick->nhats = stick->CountHats(); | |
133 | |
134 joystick->hwdata->new_axes = (int16 *) | |
135 malloc(joystick->naxes*sizeof(int16)); | |
136 joystick->hwdata->new_hats = (uint8 *) | |
137 malloc(joystick->nhats*sizeof(uint8)); | |
138 if ( ! joystick->hwdata->new_hats || ! joystick->hwdata->new_axes ) { | |
139 SDL_OutOfMemory(); | |
140 SDL_SYS_JoystickClose(joystick); | |
141 return(-1); | |
142 } | |
143 | |
144 /* We're done! */ | |
145 return(0); | |
146 } | |
147 | |
148 /* Function to update the state of a joystick - called as a device poll. | |
149 * This function shouldn't update the joystick structure directly, | |
150 * but instead should call SDL_PrivateJoystick*() to deliver events | |
151 * and update joystick device state. | |
152 */ | |
153 void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) | |
154 { | |
155 static const Uint8 hat_map[9] = { | |
156 SDL_HAT_CENTERED, | |
157 SDL_HAT_UP, | |
158 SDL_HAT_RIGHTUP, | |
159 SDL_HAT_RIGHT, | |
160 SDL_HAT_RIGHTDOWN, | |
161 SDL_HAT_DOWN, | |
162 SDL_HAT_LEFTDOWN, | |
163 SDL_HAT_LEFT, | |
164 SDL_HAT_LEFTUP | |
165 }; | |
166 const int JITTER = (32768/10); /* 10% jitter threshold (ok?) */ | |
167 | |
168 BJoystick *stick; | |
169 int i, change; | |
170 int16 *axes; | |
171 uint8 *hats; | |
172 uint32 buttons; | |
173 | |
174 /* Set up data pointers */ | |
175 stick = joystick->hwdata->stick; | |
176 axes = joystick->hwdata->new_axes; | |
177 hats = joystick->hwdata->new_hats; | |
178 | |
179 /* Get the new joystick state */ | |
180 stick->Update(); | |
181 stick->GetAxisValues(axes); | |
182 stick->GetHatValues(hats); | |
183 buttons = stick->ButtonValues(); | |
184 | |
185 /* Generate axis motion events */ | |
186 for ( i=0; i<joystick->naxes; ++i ) { | |
187 change = ((int32)axes[i] - joystick->axes[i]); | |
188 if ( (change > JITTER) || (change < -JITTER) ) { | |
189 SDL_PrivateJoystickAxis(joystick, i, axes[i]); | |
190 } | |
191 } | |
192 | |
193 /* Generate hat change events */ | |
194 for ( i=0; i<joystick->nhats; ++i ) { | |
195 if ( hats[i] != joystick->hats[i] ) { | |
196 SDL_PrivateJoystickHat(joystick, i, hat_map[hats[i]]); | |
197 } | |
198 } | |
199 | |
200 /* Generate button events */ | |
201 for ( i=0; i<joystick->nbuttons; ++i ) { | |
202 if ( (buttons&0x01) != joystick->buttons[i] ) { | |
203 SDL_PrivateJoystickButton(joystick, i, (buttons&0x01)); | |
204 } | |
205 buttons >>= 1; | |
206 } | |
207 } | |
208 | |
209 /* Function to close a joystick after use */ | |
210 void SDL_SYS_JoystickClose(SDL_Joystick *joystick) | |
211 { | |
212 if ( joystick->hwdata ) { | |
213 joystick->hwdata->stick->Close(); | |
214 delete joystick->hwdata->stick; | |
215 if ( joystick->hwdata->new_hats ) { | |
216 free(joystick->hwdata->new_hats); | |
217 } | |
218 if ( joystick->hwdata->new_axes ) { | |
219 free(joystick->hwdata->new_axes); | |
220 } | |
221 free(joystick->hwdata); | |
222 joystick->hwdata = NULL; | |
223 } | |
224 } | |
225 | |
226 /* Function to perform any system-specific joystick related cleanup */ | |
227 void SDL_SYS_JoystickQuit(void) | |
228 { | |
229 int i; | |
230 | |
231 for ( i=0; SDL_joyport[i]; ++i ) { | |
232 free(SDL_joyport[i]); | |
233 } | |
234 SDL_joyport[0] = NULL; | |
235 | |
236 for ( i=0; SDL_joyname[i]; ++i ) { | |
237 free(SDL_joyname[i]); | |
238 } | |
239 SDL_joyname[0] = NULL; | |
240 } | |
241 | |
242 }; // extern "C" |