Mercurial > sdl-ios-xcode
annotate src/joystick/beos/SDL_bejoystick.cc @ 1287:15a89a0c52bf
Date: Tue, 15 Feb 2005 21:28:48 +0900 (JST)
From: "Michael Leonhard"
Subject: [SDL] resize bug on Win32 and patch
This is my first post to this mailing list. In this email I will detail a
bug in the behavior of resizable SDL windows on Win32. Then I will
explain the solution and provide a patch.
Symptoms:
Under Windows, an SDL display created with the SDL_RESIZABLE flag exhibits
quirky behavior when being maximized. The window is resized to the proper
size, but it is shifted upwards about half the height of the title bar.
Similarly, a window whose origin is above the top of the screen will
spontaneously move its upper-left origin upon being resized. After two
such resize-induced moves, the title bar will be entirely off the top edge
of the screen. Subsequently, when the mouse is clicked and released on
the window border, the window will shrink its height spontaneously. This
height shrinkage occurs even if the user did not resize the border.
To observe this curious situation, please invoke:
SDL-1.2.8/test/testwm.exe -resize
Cause:
A pair of integers, SDL_windowX and SDL_windowY, are defined in
video/wincommon/SDL_sysevents.c. They are used by the DirectX video
driver and the DIB video driver:
video/windx5/SDL_dx5video.c
video/windib/SDL_dibvideo.c
As I understand the source code, the primary use of these variables is to
create a rectangle that represents the surface area in CLIENT SPACE.
Client space refers to a coordinate system that originates at the upper
left corner of a Win32 Window's drawable area. This is just inside the
window border and title bar. This client space rectangle, called bounds,
is subsequently converted to screen space with a call to
AdjustWindowRectEx. The problem is found in SDL's handling of the
WM_WINDOWPOSCHANGED message. According to MSDN,
"The WM_WINDOWPOSCHANGED message is sent to a window whose
size, position, or place in the Z order has changed as a
result of a call to the SetWindowPos function or another
window-management function."
I have confirmed that this message is indeed being sent to the SDL window
when the mouse is clicked on the window border, even if the window border
is not dragged.
In video/wincommon/SDL_sysevents.c, on line 464, in response to the
WM_WINDOWPOSCHANGED message, the (potentially) new client rectangle is
obtained. This rectangle is translated into screen coordinates and THEN
assigned to the SDL_windowX and Y variables. Thus screen coordinates are
being assigned to client coordinate variables. Once this is understood,
the solution is apparent: assign SDL_windowX and Y before translating the
rectangle to screen coordinates. This is accomplished by the following
patch.
-Mike_L
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 29 Jan 2006 08:50:06 +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" |