comparison src/video/windx5/SDL_dx5events.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
55 static int mouse_pressed; 55 static int mouse_pressed;
56 static int mouse_buttons_swapped = 0; 56 static int mouse_buttons_swapped = 0;
57 57
58 /* The translation table from a DirectInput scancode to an SDL keysym */ 58 /* The translation table from a DirectInput scancode to an SDL keysym */
59 static SDLKey DIK_keymap[256]; 59 static SDLKey DIK_keymap[256];
60 static SDL_keysym *TranslateKey (UINT scancode, SDL_keysym * keysym, 60 static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym * keysym,
61 int pressed); 61 int pressed);
62 62
63 /* DJM: If the user setup the window for us, we want to save his window proc, 63 /* DJM: If the user setup the window for us, we want to save his window proc,
64 and give him a chance to handle some messages. */ 64 and give him a chance to handle some messages. */
65 #ifdef STRICT 65 #ifdef STRICT
66 #define WNDPROCTYPE WNDPROC 66 #define WNDPROCTYPE WNDPROC
68 #define WNDPROCTYPE FARPROC 68 #define WNDPROCTYPE FARPROC
69 #endif 69 #endif
70 static WNDPROCTYPE userWindowProc = NULL; 70 static WNDPROCTYPE userWindowProc = NULL;
71 71
72 static HWND 72 static HWND
73 GetTopLevelParent (HWND hWnd) 73 GetTopLevelParent(HWND hWnd)
74 { 74 {
75 HWND hParentWnd; 75 HWND hParentWnd;
76 while (1) { 76 while (1) {
77 hParentWnd = GetParent (hWnd); 77 hParentWnd = GetParent(hWnd);
78 if (hParentWnd == NULL) 78 if (hParentWnd == NULL)
79 break; 79 break;
80 hWnd = hParentWnd; 80 hWnd = hParentWnd;
81 } 81 }
82 return hWnd; 82 return hWnd;
83 } 83 }
84 84
85 /* Convert a DirectInput return code to a text message */ 85 /* Convert a DirectInput return code to a text message */
86 static void 86 static void
87 SetDIerror (char *function, int code) 87 SetDIerror(char *function, int code)
88 { 88 {
89 static char *error; 89 static char *error;
90 static char errbuf[1024]; 90 static char errbuf[1024];
91 91
92 errbuf[0] = 0; 92 errbuf[0] = 0;
111 break; 111 break;
112 case DIERR_NOTINITIALIZED: 112 case DIERR_NOTINITIALIZED:
113 error = "Device not initialized"; 113 error = "Device not initialized";
114 break; 114 break;
115 default: 115 default:
116 SDL_snprintf (errbuf, SDL_arraysize (errbuf), 116 SDL_snprintf(errbuf, SDL_arraysize(errbuf),
117 "%s: Unknown DirectInput error: 0x%x", function, code); 117 "%s: Unknown DirectInput error: 0x%x", function, code);
118 break; 118 break;
119 } 119 }
120 if (!errbuf[0]) { 120 if (!errbuf[0]) {
121 SDL_snprintf (errbuf, SDL_arraysize (errbuf), "%s: %s", function, 121 SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: %s", function,
122 error); 122 error);
123 } 123 }
124 SDL_SetError ("%s", errbuf); 124 SDL_SetError("%s", errbuf);
125 return; 125 return;
126 } 126 }
127 127
128 /* Initialize DirectInput 128 /* Initialize DirectInput
129 Note: If NONEXCLUSIVE access is requested for the devices, normal 129 Note: If NONEXCLUSIVE access is requested for the devices, normal
130 windows input messages will continue to be generated for that 130 windows input messages will continue to be generated for that
131 input device, in addition to DirectInput messages. 131 input device, in addition to DirectInput messages.
132 */ 132 */
133 static void handle_keyboard (const int numevents, 133 static void handle_keyboard(const int numevents, DIDEVICEOBJECTDATA * events);
134 DIDEVICEOBJECTDATA * events); 134 static void handle_mouse(const int numevents, DIDEVICEOBJECTDATA * events);
135 static void handle_mouse (const int numevents, DIDEVICEOBJECTDATA * events);
136 struct 135 struct
137 { 136 {
138 char *name; 137 char *name;
139 REFGUID guid; 138 REFGUID guid;
140 LPCDIDATAFORMAT format; 139 LPCDIDATAFORMAT format;
153 (DISCL_FOREGROUND | DISCL_EXCLUSIVE), handle_mouse}, { 152 (DISCL_FOREGROUND | DISCL_EXCLUSIVE), handle_mouse}, {
154 NULL, NULL, NULL, 0, 0, NULL} 153 NULL, NULL, NULL, 0, 0, NULL}
155 }; 154 };
156 155
157 static int 156 static int
158 DX5_DInputInit (_THIS) 157 DX5_DInputInit(_THIS)
159 { 158 {
160 int i; 159 int i;
161 LPDIRECTINPUTDEVICE device; 160 LPDIRECTINPUTDEVICE device;
162 HRESULT result; 161 HRESULT result;
163 DIPROPDWORD dipdw; 162 DIPROPDWORD dipdw;
164 HWND topwnd; 163 HWND topwnd;
165 164
166 /* Create the DirectInput object */ 165 /* Create the DirectInput object */
167 result = DInputCreate (SDL_Instance, DIRECTINPUT_VERSION, &dinput, NULL); 166 result = DInputCreate(SDL_Instance, DIRECTINPUT_VERSION, &dinput, NULL);
168 if (result != DI_OK) { 167 if (result != DI_OK) {
169 SetDIerror ("DirectInputCreate", result); 168 SetDIerror("DirectInputCreate", result);
170 return (-1); 169 return (-1);
171 } 170 }
172 171
173 /* Create all of our registered input devices */ 172 /* Create all of our registered input devices */
174 SDL_DIndev = 0; 173 SDL_DIndev = 0;
175 for (i = 0; inputs[i].name; ++i) { 174 for (i = 0; inputs[i].name; ++i) {
176 /* Create the DirectInput device */ 175 /* Create the DirectInput device */
177 result = IDirectInput_CreateDevice (dinput, inputs[i].guid, 176 result = IDirectInput_CreateDevice(dinput, inputs[i].guid,
178 &device, NULL); 177 &device, NULL);
179 if (result != DI_OK) { 178 if (result != DI_OK) {
180 SetDIerror ("DirectInput::CreateDevice", result); 179 SetDIerror("DirectInput::CreateDevice", result);
181 return (-1); 180 return (-1);
182 } 181 }
183 result = IDirectInputDevice_QueryInterface (device, 182 result = IDirectInputDevice_QueryInterface(device,
184 &IID_IDirectInputDevice2, 183 &IID_IDirectInputDevice2,
185 (LPVOID *) & 184 (LPVOID *) & SDL_DIdev[i]);
186 SDL_DIdev[i]); 185 IDirectInputDevice_Release(device);
187 IDirectInputDevice_Release (device);
188 if (result != DI_OK) { 186 if (result != DI_OK) {
189 SetDIerror ("DirectInputDevice::QueryInterface", result); 187 SetDIerror("DirectInputDevice::QueryInterface", result);
190 return (-1); 188 return (-1);
191 } 189 }
192 topwnd = GetTopLevelParent (SDL_Window); 190 topwnd = GetTopLevelParent(SDL_Window);
193 result = IDirectInputDevice2_SetCooperativeLevel (SDL_DIdev[i], 191 result = IDirectInputDevice2_SetCooperativeLevel(SDL_DIdev[i],
194 topwnd, 192 topwnd,
195 inputs[i]. 193 inputs[i].win_level);
196 win_level);
197 if (result != DI_OK) { 194 if (result != DI_OK) {
198 SetDIerror ("DirectInputDevice::SetCooperativeLevel", result); 195 SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
199 return (-1); 196 return (-1);
200 } 197 }
201 result = IDirectInputDevice2_SetDataFormat (SDL_DIdev[i], 198 result = IDirectInputDevice2_SetDataFormat(SDL_DIdev[i],
202 inputs[i].format); 199 inputs[i].format);
203 if (result != DI_OK) { 200 if (result != DI_OK) {
204 SetDIerror ("DirectInputDevice::SetDataFormat", result); 201 SetDIerror("DirectInputDevice::SetDataFormat", result);
205 return (-1); 202 return (-1);
206 } 203 }
207 204
208 /* Set buffered input -- we aren't polling */ 205 /* Set buffered input -- we aren't polling */
209 SDL_memset (&dipdw, 0, sizeof (dipdw)); 206 SDL_memset(&dipdw, 0, sizeof(dipdw));
210 dipdw.diph.dwSize = sizeof (dipdw); 207 dipdw.diph.dwSize = sizeof(dipdw);
211 dipdw.diph.dwHeaderSize = sizeof (dipdw.diph); 208 dipdw.diph.dwHeaderSize = sizeof(dipdw.diph);
212 dipdw.diph.dwObj = 0; 209 dipdw.diph.dwObj = 0;
213 dipdw.diph.dwHow = DIPH_DEVICE; 210 dipdw.diph.dwHow = DIPH_DEVICE;
214 dipdw.dwData = INPUT_QSIZE; 211 dipdw.dwData = INPUT_QSIZE;
215 result = IDirectInputDevice2_SetProperty (SDL_DIdev[i], 212 result = IDirectInputDevice2_SetProperty(SDL_DIdev[i],
216 DIPROP_BUFFERSIZE, 213 DIPROP_BUFFERSIZE,
217 &dipdw.diph); 214 &dipdw.diph);
218 if (result != DI_OK) { 215 if (result != DI_OK) {
219 SetDIerror ("DirectInputDevice::SetProperty", result); 216 SetDIerror("DirectInputDevice::SetProperty", result);
220 return (-1); 217 return (-1);
221 } 218 }
222 219
223 /* Create an event to be signaled when input is ready */ 220 /* Create an event to be signaled when input is ready */
224 SDL_DIevt[i] = CreateEvent (NULL, FALSE, FALSE, NULL); 221 SDL_DIevt[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
225 if (SDL_DIevt[i] == NULL) { 222 if (SDL_DIevt[i] == NULL) {
226 SDL_SetError ("Couldn't create DirectInput event"); 223 SDL_SetError("Couldn't create DirectInput event");
227 return (-1); 224 return (-1);
228 } 225 }
229 result = IDirectInputDevice2_SetEventNotification (SDL_DIdev[i], 226 result = IDirectInputDevice2_SetEventNotification(SDL_DIdev[i],
230 SDL_DIevt[i]); 227 SDL_DIevt[i]);
231 if (result != DI_OK) { 228 if (result != DI_OK) {
232 SetDIerror ("DirectInputDevice::SetEventNotification", result); 229 SetDIerror("DirectInputDevice::SetEventNotification", result);
233 return (-1); 230 return (-1);
234 } 231 }
235 SDL_DIfun[i] = inputs[i].fun; 232 SDL_DIfun[i] = inputs[i].fun;
236 233
237 /* Acquire the device for input */ 234 /* Acquire the device for input */
238 IDirectInputDevice2_Acquire (SDL_DIdev[i]); 235 IDirectInputDevice2_Acquire(SDL_DIdev[i]);
239 236
240 /* Increment the number of devices we have */ 237 /* Increment the number of devices we have */
241 ++SDL_DIndev; 238 ++SDL_DIndev;
242 } 239 }
243 mouse_pressed = 0; 240 mouse_pressed = 0;
244 mouse_buttons_swapped = GetSystemMetrics (SM_SWAPBUTTON); 241 mouse_buttons_swapped = GetSystemMetrics(SM_SWAPBUTTON);
245 242
246 /* DirectInput is ready! */ 243 /* DirectInput is ready! */
247 return (0); 244 return (0);
248 } 245 }
249 246
250 /* Clean up DirectInput */ 247 /* Clean up DirectInput */
251 static void 248 static void
252 DX5_DInputQuit (_THIS) 249 DX5_DInputQuit(_THIS)
253 { 250 {
254 int i; 251 int i;
255 252
256 if (dinput != NULL) { 253 if (dinput != NULL) {
257 /* Close and release all DirectInput devices */ 254 /* Close and release all DirectInput devices */
258 for (i = 0; i < MAX_INPUTS; ++i) { 255 for (i = 0; i < MAX_INPUTS; ++i) {
259 if (SDL_DIdev[i] != NULL) { 256 if (SDL_DIdev[i] != NULL) {
260 IDirectInputDevice2_Unacquire (SDL_DIdev[i]); 257 IDirectInputDevice2_Unacquire(SDL_DIdev[i]);
261 IDirectInputDevice2_SetEventNotification (SDL_DIdev[i], NULL); 258 IDirectInputDevice2_SetEventNotification(SDL_DIdev[i], NULL);
262 if (SDL_DIevt[i] != NULL) { 259 if (SDL_DIevt[i] != NULL) {
263 CloseHandle (SDL_DIevt[i]); 260 CloseHandle(SDL_DIevt[i]);
264 SDL_DIevt[i] = NULL; 261 SDL_DIevt[i] = NULL;
265 } 262 }
266 IDirectInputDevice2_Release (SDL_DIdev[i]); 263 IDirectInputDevice2_Release(SDL_DIdev[i]);
267 SDL_DIdev[i] = NULL; 264 SDL_DIdev[i] = NULL;
268 } 265 }
269 } 266 }
270 /* Release DirectInput */ 267 /* Release DirectInput */
271 IDirectInput_Release (dinput); 268 IDirectInput_Release(dinput);
272 dinput = NULL; 269 dinput = NULL;
273 } 270 }
274 } 271 }
275 272
276 /* Flag to tell SDL whether or not we queued an event */ 273 /* Flag to tell SDL whether or not we queued an event */
277 static int posted = 0; 274 static int posted = 0;
278 275
279 /* Input event handler functions */ 276 /* Input event handler functions */
280 static void 277 static void
281 handle_keyboard (const int numevents, DIDEVICEOBJECTDATA * keybuf) 278 handle_keyboard(const int numevents, DIDEVICEOBJECTDATA * keybuf)
282 { 279 {
283 int i; 280 int i;
284 SDL_keysym keysym; 281 SDL_keysym keysym;
285 282
286 /* Translate keyboard messages */ 283 /* Translate keyboard messages */
287 for (i = 0; i < numevents; ++i) { 284 for (i = 0; i < numevents; ++i) {
288 if (keybuf[i].dwData & 0x80) { 285 if (keybuf[i].dwData & 0x80) {
289 posted = SDL_PrivateKeyboard (SDL_PRESSED, 286 posted = SDL_PrivateKeyboard(SDL_PRESSED,
290 TranslateKey (keybuf[i].dwOfs, 287 TranslateKey(keybuf[i].dwOfs,
291 &keysym, 1)); 288 &keysym, 1));
292 } else { 289 } else {
293 posted = SDL_PrivateKeyboard (SDL_RELEASED, 290 posted = SDL_PrivateKeyboard(SDL_RELEASED,
294 TranslateKey (keybuf[i].dwOfs, 291 TranslateKey(keybuf[i].dwOfs,
295 &keysym, 0)); 292 &keysym, 0));
296 } 293 }
297 } 294 }
298 } 295 }
299 static void 296 static void
300 handle_mouse (const int numevents, DIDEVICEOBJECTDATA * ptrbuf) 297 handle_mouse(const int numevents, DIDEVICEOBJECTDATA * ptrbuf)
301 { 298 {
302 int i; 299 int i;
303 Sint16 xrel, yrel; 300 Sint16 xrel, yrel;
304 Uint8 state; 301 Uint8 state;
305 Uint8 button; 302 Uint8 button;
321 POINT mouse_pos; 318 POINT mouse_pos;
322 Uint8 old_state; 319 Uint8 old_state;
323 Uint8 new_state; 320 Uint8 new_state;
324 321
325 /* Set ourselves up with the current cursor position */ 322 /* Set ourselves up with the current cursor position */
326 GetCursorPos (&mouse_pos); 323 GetCursorPos(&mouse_pos);
327 ScreenToClient (SDL_Window, &mouse_pos); 324 ScreenToClient(SDL_Window, &mouse_pos);
328 posted = SDL_PrivateMouseMotion (0, 0, 325 posted = SDL_PrivateMouseMotion(0, 0,
329 (Sint16) mouse_pos.x, 326 (Sint16) mouse_pos.x,
330 (Sint16) mouse_pos.y); 327 (Sint16) mouse_pos.y);
331 328
332 /* Check for mouse button changes */ 329 /* Check for mouse button changes */
333 old_state = SDL_GetMouseState (NULL, NULL); 330 old_state = SDL_GetMouseState(NULL, NULL);
334 new_state = 0; 331 new_state = 0;
335 { /* Get the new DirectInput button state for the mouse */ 332 { /* Get the new DirectInput button state for the mouse */
336 DIMOUSESTATE distate; 333 DIMOUSESTATE distate;
337 HRESULT result; 334 HRESULT result;
338 335
339 result = IDirectInputDevice2_GetDeviceState (SDL_DIdev[1], 336 result = IDirectInputDevice2_GetDeviceState(SDL_DIdev[1],
340 sizeof (distate), 337 sizeof(distate),
341 &distate); 338 &distate);
342 if (result != DI_OK) { 339 if (result != DI_OK) {
343 /* Try again next time */ 340 /* Try again next time */
344 SetDIerror ("IDirectInputDevice2::GetDeviceState", result); 341 SetDIerror("IDirectInputDevice2::GetDeviceState", result);
345 return; 342 return;
346 } 343 }
347 for (i = 3; i >= 0; --i) { 344 for (i = 3; i >= 0; --i) {
348 if ((distate.rgbButtons[i] & 0x80) == 0x80) { 345 if ((distate.rgbButtons[i] & 0x80) == 0x80) {
349 new_state |= 0x01; 346 new_state |= 0x01;
363 button = 2; 360 button = 2;
364 } 361 }
365 if (new_state & 0x01) { 362 if (new_state & 0x01) {
366 /* Grab mouse so we get mouse-up */ 363 /* Grab mouse so we get mouse-up */
367 if (++mouse_pressed > 0) { 364 if (++mouse_pressed > 0) {
368 SetCapture (SDL_Window); 365 SetCapture(SDL_Window);
369 } 366 }
370 state = SDL_PRESSED; 367 state = SDL_PRESSED;
371 } else { 368 } else {
372 /* Release mouse after all mouse-ups */ 369 /* Release mouse after all mouse-ups */
373 if (--mouse_pressed <= 0) { 370 if (--mouse_pressed <= 0) {
374 ReleaseCapture (); 371 ReleaseCapture();
375 mouse_pressed = 0; 372 mouse_pressed = 0;
376 } 373 }
377 state = SDL_RELEASED; 374 state = SDL_RELEASED;
378 } 375 }
379 if (mouse_buttons_swapped) { 376 if (mouse_buttons_swapped) {
380 if (button == 1) 377 if (button == 1)
381 button = 3; 378 button = 3;
382 else if (button == 3) 379 else if (button == 3)
383 button = 1; 380 button = 1;
384 } 381 }
385 posted = SDL_PrivateMouseButton (state, button, 0, 0); 382 posted = SDL_PrivateMouseButton(state, button, 0, 0);
386 } 383 }
387 old_state >>= 1; 384 old_state >>= 1;
388 new_state >>= 1; 385 new_state >>= 1;
389 } 386 }
390 mouse_lost = 0; 387 mouse_lost = 0;
397 for (i = 0; i < (int) numevents; ++i) { 394 for (i = 0; i < (int) numevents; ++i) {
398 switch (ptrbuf[i].dwOfs) { 395 switch (ptrbuf[i].dwOfs) {
399 case DIMOFS_X: 396 case DIMOFS_X:
400 if (timestamp != ptrbuf[i].dwTimeStamp) { 397 if (timestamp != ptrbuf[i].dwTimeStamp) {
401 if (xrel || yrel) { 398 if (xrel || yrel) {
402 posted = SDL_PrivateMouseMotion (0, 1, xrel, yrel); 399 posted = SDL_PrivateMouseMotion(0, 1, xrel, yrel);
403 xrel = 0; 400 xrel = 0;
404 yrel = 0; 401 yrel = 0;
405 } 402 }
406 timestamp = ptrbuf[i].dwTimeStamp; 403 timestamp = ptrbuf[i].dwTimeStamp;
407 } 404 }
408 xrel += (Sint16) ptrbuf[i].dwData; 405 xrel += (Sint16) ptrbuf[i].dwData;
409 break; 406 break;
410 case DIMOFS_Y: 407 case DIMOFS_Y:
411 if (timestamp != ptrbuf[i].dwTimeStamp) { 408 if (timestamp != ptrbuf[i].dwTimeStamp) {
412 if (xrel || yrel) { 409 if (xrel || yrel) {
413 posted = SDL_PrivateMouseMotion (0, 1, xrel, yrel); 410 posted = SDL_PrivateMouseMotion(0, 1, xrel, yrel);
414 xrel = 0; 411 xrel = 0;
415 yrel = 0; 412 yrel = 0;
416 } 413 }
417 timestamp = ptrbuf[i].dwTimeStamp; 414 timestamp = ptrbuf[i].dwTimeStamp;
418 } 415 }
419 yrel += (Sint16) ptrbuf[i].dwData; 416 yrel += (Sint16) ptrbuf[i].dwData;
420 break; 417 break;
421 case DIMOFS_Z: 418 case DIMOFS_Z:
422 if (xrel || yrel) { 419 if (xrel || yrel) {
423 posted = SDL_PrivateMouseMotion (0, 1, xrel, yrel); 420 posted = SDL_PrivateMouseMotion(0, 1, xrel, yrel);
424 xrel = 0; 421 xrel = 0;
425 yrel = 0; 422 yrel = 0;
426 } 423 }
427 timestamp = 0; 424 timestamp = 0;
428 if ((int) ptrbuf[i].dwData > 0) 425 if ((int) ptrbuf[i].dwData > 0)
429 button = SDL_BUTTON_WHEELUP; 426 button = SDL_BUTTON_WHEELUP;
430 else 427 else
431 button = SDL_BUTTON_WHEELDOWN; 428 button = SDL_BUTTON_WHEELDOWN;
432 posted = SDL_PrivateMouseButton (SDL_PRESSED, button, 0, 0); 429 posted = SDL_PrivateMouseButton(SDL_PRESSED, button, 0, 0);
433 posted |= SDL_PrivateMouseButton (SDL_RELEASED, button, 0, 0); 430 posted |= SDL_PrivateMouseButton(SDL_RELEASED, button, 0, 0);
434 break; 431 break;
435 case DIMOFS_BUTTON0: 432 case DIMOFS_BUTTON0:
436 case DIMOFS_BUTTON1: 433 case DIMOFS_BUTTON1:
437 case DIMOFS_BUTTON2: 434 case DIMOFS_BUTTON2:
438 case DIMOFS_BUTTON3: 435 case DIMOFS_BUTTON3:
439 if (xrel || yrel) { 436 if (xrel || yrel) {
440 posted = SDL_PrivateMouseMotion (0, 1, xrel, yrel); 437 posted = SDL_PrivateMouseMotion(0, 1, xrel, yrel);
441 xrel = 0; 438 xrel = 0;
442 yrel = 0; 439 yrel = 0;
443 } 440 }
444 timestamp = 0; 441 timestamp = 0;
445 button = (Uint8) (ptrbuf[i].dwOfs - DIMOFS_BUTTON0) + 1; 442 button = (Uint8) (ptrbuf[i].dwOfs - DIMOFS_BUTTON0) + 1;
452 button = 2; 449 button = 2;
453 } 450 }
454 if (ptrbuf[i].dwData & 0x80) { 451 if (ptrbuf[i].dwData & 0x80) {
455 /* Grab mouse so we get mouse-up */ 452 /* Grab mouse so we get mouse-up */
456 if (++mouse_pressed > 0) { 453 if (++mouse_pressed > 0) {
457 SetCapture (SDL_Window); 454 SetCapture(SDL_Window);
458 } 455 }
459 state = SDL_PRESSED; 456 state = SDL_PRESSED;
460 } else { 457 } else {
461 /* Release mouse after all mouse-ups */ 458 /* Release mouse after all mouse-ups */
462 if (--mouse_pressed <= 0) { 459 if (--mouse_pressed <= 0) {
463 ReleaseCapture (); 460 ReleaseCapture();
464 mouse_pressed = 0; 461 mouse_pressed = 0;
465 } 462 }
466 state = SDL_RELEASED; 463 state = SDL_RELEASED;
467 } 464 }
468 if (mouse_buttons_swapped) { 465 if (mouse_buttons_swapped) {
469 if (button == 1) 466 if (button == 1)
470 button = 3; 467 button = 3;
471 else if (button == 3) 468 else if (button == 3)
472 button = 1; 469 button = 1;
473 } 470 }
474 posted = SDL_PrivateMouseButton (state, button, 0, 0); 471 posted = SDL_PrivateMouseButton(state, button, 0, 0);
475 break; 472 break;
476 } 473 }
477 } 474 }
478 if (xrel || yrel) { 475 if (xrel || yrel) {
479 posted = SDL_PrivateMouseMotion (0, 1, xrel, yrel); 476 posted = SDL_PrivateMouseMotion(0, 1, xrel, yrel);
480 } 477 }
481 } 478 }
482 479
483 /* The main Win32 event handler */ 480 /* The main Win32 event handler */
484 LRESULT 481 LRESULT
485 DX5_HandleMessage (_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 482 DX5_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
486 { 483 {
487 switch (msg) { 484 switch (msg) {
488 #ifdef WM_ACTIVATEAPP 485 #ifdef WM_ACTIVATEAPP
489 case WM_ACTIVATEAPP: 486 case WM_ACTIVATEAPP:
490 { 487 {
491 int i, active; 488 int i, active;
492 489
493 active = (wParam && (GetForegroundWindow () == hwnd)); 490 active = (wParam && (GetForegroundWindow() == hwnd));
494 if (active) { 491 if (active) {
495 for (i = 0; SDL_DIdev[i]; ++i) { 492 for (i = 0; SDL_DIdev[i]; ++i) {
496 IDirectInputDevice2_Acquire (SDL_DIdev[i]); 493 IDirectInputDevice2_Acquire(SDL_DIdev[i]);
497 } 494 }
498 } else { 495 } else {
499 for (i = 0; SDL_DIdev[i]; ++i) { 496 for (i = 0; SDL_DIdev[i]; ++i) {
500 IDirectInputDevice2_Unacquire (SDL_DIdev[i]); 497 IDirectInputDevice2_Unacquire(SDL_DIdev[i]);
501 } 498 }
502 mouse_lost = 1; 499 mouse_lost = 1;
503 } 500 }
504 } 501 }
505 break; 502 break;
510 { 507 {
511 WPARAM BitsPerPixel; 508 WPARAM BitsPerPixel;
512 WORD SizeX, SizeY; 509 WORD SizeX, SizeY;
513 510
514 /* Ack! The display changed size and/or depth! */ 511 /* Ack! The display changed size and/or depth! */
515 SizeX = LOWORD (lParam); 512 SizeX = LOWORD(lParam);
516 SizeY = HIWORD (lParam); 513 SizeY = HIWORD(lParam);
517 BitsPerPixel = wParam; 514 BitsPerPixel = wParam;
518 /* We cause this message when we go fullscreen */ 515 /* We cause this message when we go fullscreen */
519 } 516 }
520 break; 517 break;
521 #endif /* WM_DISPLAYCHANGE */ 518 #endif /* WM_DISPLAYCHANGE */
556 { 553 {
557 /* Only post the event if we're watching for it */ 554 /* Only post the event if we're watching for it */
558 if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) { 555 if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
559 SDL_SysWMmsg wmmsg; 556 SDL_SysWMmsg wmmsg;
560 557
561 SDL_VERSION (&wmmsg.version); 558 SDL_VERSION(&wmmsg.version);
562 wmmsg.hwnd = hwnd; 559 wmmsg.hwnd = hwnd;
563 wmmsg.msg = msg; 560 wmmsg.msg = msg;
564 wmmsg.wParam = wParam; 561 wmmsg.wParam = wParam;
565 wmmsg.lParam = lParam; 562 wmmsg.lParam = lParam;
566 posted = SDL_PrivateSysWMEvent (&wmmsg); 563 posted = SDL_PrivateSysWMEvent(&wmmsg);
567 564
568 /* DJM: If the user isn't watching for private 565 /* DJM: If the user isn't watching for private
569 messages in her SDL event loop, then pass it 566 messages in her SDL event loop, then pass it
570 along to any win32 specific window proc. 567 along to any win32 specific window proc.
571 */ 568 */
572 } else if (userWindowProc) { 569 } else if (userWindowProc) {
573 return CallWindowProc (userWindowProc, hwnd, msg, wParam, 570 return CallWindowProc(userWindowProc, hwnd, msg, wParam,
574 lParam); 571 lParam);
575 } 572 }
576 } 573 }
577 break; 574 break;
578 } 575 }
579 return (DefWindowProc (hwnd, msg, wParam, lParam)); 576 return (DefWindowProc(hwnd, msg, wParam, lParam));
580 } 577 }
581 578
582 /* This function checks the windows message queue and DirectInput and returns 579 /* This function checks the windows message queue and DirectInput and returns
583 1 if there was input, 0 if there was no input, or -1 if the application has 580 1 if there was input, 0 if there was no input, or -1 if the application has
584 posted a quit message. 581 posted a quit message.
585 */ 582 */
586 static int 583 static int
587 DX5_CheckInput (_THIS, int timeout, BOOL processInput) 584 DX5_CheckInput(_THIS, int timeout, BOOL processInput)
588 { 585 {
589 MSG msg; 586 MSG msg;
590 int i; 587 int i;
591 HRESULT result; 588 HRESULT result;
592 DWORD event; 589 DWORD event;
593 590
594 /* Check the normal windows queue (highest preference) */ 591 /* Check the normal windows queue (highest preference) */
595 posted = 0; 592 posted = 0;
596 while (!posted && PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) { 593 while (!posted && PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
597 if (GetMessage (&msg, NULL, 0, 0) > 0) { 594 if (GetMessage(&msg, NULL, 0, 0) > 0) {
598 DispatchMessage (&msg); 595 DispatchMessage(&msg);
599 } else { 596 } else {
600 return (-1); 597 return (-1);
601 } 598 }
602 } 599 }
603 if (posted) { 600 if (posted) {
604 return (1); 601 return (1);
605 } 602 }
606 603
607 /* Pump the DirectInput flow */ 604 /* Pump the DirectInput flow */
608 if (SDL_GetAppState () & SDL_APPINPUTFOCUS) { 605 if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
609 for (i = 0; i < SDL_DIndev; ++i) { 606 for (i = 0; i < SDL_DIndev; ++i) {
610 result = IDirectInputDevice2_Poll (SDL_DIdev[i]); 607 result = IDirectInputDevice2_Poll(SDL_DIdev[i]);
611 if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED)) { 608 if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED)) {
612 if (SDL_strcmp (inputs[i].name, "mouse") == 0) { 609 if (SDL_strcmp(inputs[i].name, "mouse") == 0) {
613 mouse_lost = 1; 610 mouse_lost = 1;
614 } 611 }
615 IDirectInputDevice2_Acquire (SDL_DIdev[i]); 612 IDirectInputDevice2_Acquire(SDL_DIdev[i]);
616 IDirectInputDevice2_Poll (SDL_DIdev[i]); 613 IDirectInputDevice2_Poll(SDL_DIdev[i]);
617 } 614 }
618 } 615 }
619 } 616 }
620 617
621 /* Wait for messages and input events */ 618 /* Wait for messages and input events */
622 event = MsgWaitForMultipleObjects (SDL_DIndev, SDL_DIevt, FALSE, 619 event = MsgWaitForMultipleObjects(SDL_DIndev, SDL_DIevt, FALSE,
623 timeout, QS_ALLEVENTS); 620 timeout, QS_ALLEVENTS);
624 if ((event >= WAIT_OBJECT_0) && (event < (WAIT_OBJECT_0 + SDL_DIndev))) { 621 if ((event >= WAIT_OBJECT_0) && (event < (WAIT_OBJECT_0 + SDL_DIndev))) {
625 DWORD numevents; 622 DWORD numevents;
626 static DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE]; 623 static DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
627 624
628 event -= WAIT_OBJECT_0; 625 event -= WAIT_OBJECT_0;
629 numevents = INPUT_QSIZE; 626 numevents = INPUT_QSIZE;
630 result = 627 result =
631 IDirectInputDevice2_GetDeviceData (SDL_DIdev[event], 628 IDirectInputDevice2_GetDeviceData(SDL_DIdev[event],
632 sizeof (DIDEVICEOBJECTDATA), 629 sizeof(DIDEVICEOBJECTDATA),
633 evtbuf, &numevents, 0); 630 evtbuf, &numevents, 0);
634 if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED)) { 631 if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED)) {
635 if (SDL_strcmp (inputs[event].name, "mouse") == 0) { 632 if (SDL_strcmp(inputs[event].name, "mouse") == 0) {
636 mouse_lost = 1; 633 mouse_lost = 1;
637 } 634 }
638 IDirectInputDevice2_Acquire (SDL_DIdev[event]); 635 IDirectInputDevice2_Acquire(SDL_DIdev[event]);
639 result = 636 result =
640 IDirectInputDevice2_GetDeviceData (SDL_DIdev[event], 637 IDirectInputDevice2_GetDeviceData(SDL_DIdev[event],
641 sizeof 638 sizeof
642 (DIDEVICEOBJECTDATA), 639 (DIDEVICEOBJECTDATA),
643 evtbuf, &numevents, 0); 640 evtbuf, &numevents, 0);
644 } 641 }
645 /* Handle the events */ 642 /* Handle the events */
646 if (result == DI_OK && processInput) { 643 if (result == DI_OK && processInput) {
647 /* Note: This can post multiple events to event queue 644 /* Note: This can post multiple events to event queue
648 */ 645 */
650 return (1); 647 return (1);
651 } 648 }
652 } 649 }
653 if (event != WAIT_TIMEOUT) { 650 if (event != WAIT_TIMEOUT) {
654 /* Maybe there was a windows message? */ 651 /* Maybe there was a windows message? */
655 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) { 652 if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
656 if (GetMessage (&msg, NULL, 0, 0) > 0) { 653 if (GetMessage(&msg, NULL, 0, 0) > 0) {
657 DispatchMessage (&msg); 654 DispatchMessage(&msg);
658 } else { 655 } else {
659 return (-1); 656 return (-1);
660 } 657 }
661 return (1); 658 return (1);
662 } 659 }
664 return (0); 661 return (0);
665 } 662 }
666 663
667 /* Change cooperative level based on whether or not we are fullscreen */ 664 /* Change cooperative level based on whether or not we are fullscreen */
668 void 665 void
669 DX5_DInputReset (_THIS, int fullscreen) 666 DX5_DInputReset(_THIS, int fullscreen)
670 { 667 {
671 DWORD level; 668 DWORD level;
672 int i; 669 int i;
673 HRESULT result; 670 HRESULT result;
674 HWND topwnd; 671 HWND topwnd;
678 if (fullscreen) { 675 if (fullscreen) {
679 level = inputs[i].raw_level; 676 level = inputs[i].raw_level;
680 } else { 677 } else {
681 level = inputs[i].win_level; 678 level = inputs[i].win_level;
682 } 679 }
683 IDirectInputDevice2_Unacquire (SDL_DIdev[i]); 680 IDirectInputDevice2_Unacquire(SDL_DIdev[i]);
684 topwnd = GetTopLevelParent (SDL_Window); 681 topwnd = GetTopLevelParent(SDL_Window);
685 result = 682 result =
686 IDirectInputDevice2_SetCooperativeLevel (SDL_DIdev[i], 683 IDirectInputDevice2_SetCooperativeLevel(SDL_DIdev[i],
687 topwnd, level); 684 topwnd, level);
688 IDirectInputDevice2_Acquire (SDL_DIdev[i]); 685 IDirectInputDevice2_Acquire(SDL_DIdev[i]);
689 if (result != DI_OK) { 686 if (result != DI_OK) {
690 SetDIerror ("DirectInputDevice::SetCooperativeLevel", result); 687 SetDIerror("DirectInputDevice::SetCooperativeLevel", result);
691 } 688 }
692 } 689 }
693 } 690 }
694 mouse_lost = 1; 691 mouse_lost = 1;
695 692
696 /* Flush pending input */ 693 /* Flush pending input */
697 DX5_CheckInput (this, 0, FALSE); 694 DX5_CheckInput(this, 0, FALSE);
698 } 695 }
699 696
700 void 697 void
701 DX5_PumpEvents (_THIS) 698 DX5_PumpEvents(_THIS)
702 { 699 {
703 /* Wait for messages and DirectInput */ 700 /* Wait for messages and DirectInput */
704 while (DX5_CheckInput (this, 0, TRUE) > 0) { 701 while (DX5_CheckInput(this, 0, TRUE) > 0) {
705 /* Loop and check again */ ; 702 /* Loop and check again */ ;
706 } 703 }
707 } 704 }
708 705
709 void 706 void
710 DX5_InitOSKeymap (_THIS) 707 DX5_InitOSKeymap(_THIS)
711 { 708 {
712 #ifndef DIK_PAUSE 709 #ifndef DIK_PAUSE
713 #define DIK_PAUSE 0xC5 710 #define DIK_PAUSE 0xC5
714 #endif 711 #endif
715 #ifndef DIK_OEM_102 712 #ifndef DIK_OEM_102
716 #define DIK_OEM_102 0x56 /* < > | on UK/Germany keyboards */ 713 #define DIK_OEM_102 0x56 /* < > | on UK/Germany keyboards */
717 #endif 714 #endif
718 int i; 715 int i;
719 716
720 /* Map the DIK scancodes to SDL keysyms */ 717 /* Map the DIK scancodes to SDL keysyms */
721 for (i = 0; i < SDL_arraysize (DIK_keymap); ++i) 718 for (i = 0; i < SDL_arraysize(DIK_keymap); ++i)
722 DIK_keymap[i] = 0; 719 DIK_keymap[i] = 0;
723 720
724 /* Defined DIK_* constants */ 721 /* Defined DIK_* constants */
725 DIK_keymap[DIK_ESCAPE] = SDLK_ESCAPE; 722 DIK_keymap[DIK_ESCAPE] = SDLK_ESCAPE;
726 DIK_keymap[DIK_1] = SDLK_1; 723 DIK_keymap[DIK_1] = SDLK_1;
834 DIK_keymap[DIK_RWIN] = SDLK_RMETA; 831 DIK_keymap[DIK_RWIN] = SDLK_RMETA;
835 DIK_keymap[DIK_APPS] = SDLK_MENU; 832 DIK_keymap[DIK_APPS] = SDLK_MENU;
836 } 833 }
837 834
838 static SDL_keysym * 835 static SDL_keysym *
839 TranslateKey (UINT scancode, SDL_keysym * keysym, int pressed) 836 TranslateKey(UINT scancode, SDL_keysym * keysym, int pressed)
840 { 837 {
841 /* Set the keysym information */ 838 /* Set the keysym information */
842 keysym->scancode = (unsigned char) scancode; 839 keysym->scancode = (unsigned char) scancode;
843 keysym->sym = DIK_keymap[scancode]; 840 keysym->sym = DIK_keymap[scancode];
844 keysym->mod = KMOD_NONE; 841 keysym->mod = KMOD_NONE;
848 #ifndef NO_GETKEYBOARDSTATE 845 #ifndef NO_GETKEYBOARDSTATE
849 BYTE keystate[256]; 846 BYTE keystate[256];
850 Uint16 wchars[2]; 847 Uint16 wchars[2];
851 #endif 848 #endif
852 849
853 vkey = MapVirtualKey (scancode, 1); 850 vkey = MapVirtualKey(scancode, 1);
854 #ifdef NO_GETKEYBOARDSTATE 851 #ifdef NO_GETKEYBOARDSTATE
855 /* Uh oh, better hope the vkey is close enough.. */ 852 /* Uh oh, better hope the vkey is close enough.. */
856 keysym->unicode = vkey; 853 keysym->unicode = vkey;
857 #else 854 #else
858 GetKeyboardState (keystate); 855 GetKeyboardState(keystate);
859 if (SDL_ToUnicode 856 if (SDL_ToUnicode
860 (vkey, scancode, keystate, wchars, 857 (vkey, scancode, keystate, wchars,
861 sizeof (wchars) / sizeof (wchars[0]), 0) == 1) { 858 sizeof(wchars) / sizeof(wchars[0]), 0) == 1) {
862 keysym->unicode = wchars[0]; 859 keysym->unicode = wchars[0];
863 } 860 }
864 #endif /* NO_GETKEYBOARDSTATE */ 861 #endif /* NO_GETKEYBOARDSTATE */
865 } 862 }
866 return (keysym); 863 return (keysym);
867 } 864 }
868 865
869 int 866 int
870 DX5_CreateWindow (_THIS) 867 DX5_CreateWindow(_THIS)
871 { 868 {
872 char *windowid = SDL_getenv ("SDL_WINDOWID"); 869 char *windowid = SDL_getenv("SDL_WINDOWID");
873 int i; 870 int i;
874 871
875 /* Clear out DirectInput variables in case we fail */ 872 /* Clear out DirectInput variables in case we fail */
876 for (i = 0; i < MAX_INPUTS; ++i) { 873 for (i = 0; i < MAX_INPUTS; ++i) {
877 SDL_DIdev[i] = NULL; 874 SDL_DIdev[i] = NULL;
878 SDL_DIevt[i] = NULL; 875 SDL_DIevt[i] = NULL;
879 SDL_DIfun[i] = NULL; 876 SDL_DIfun[i] = NULL;
880 } 877 }
881 878
882 SDL_RegisterApp (NULL, 0, 0); 879 SDL_RegisterApp(NULL, 0, 0);
883 880
884 SDL_windowid = (windowid != NULL); 881 SDL_windowid = (windowid != NULL);
885 if (SDL_windowid) { 882 if (SDL_windowid) {
886 SDL_Window = (HWND) SDL_strtoull (windowid, NULL, 0); 883 SDL_Window = (HWND) SDL_strtoull(windowid, NULL, 0);
887 if (SDL_Window == NULL) { 884 if (SDL_Window == NULL) {
888 SDL_SetError ("Couldn't get user specified window"); 885 SDL_SetError("Couldn't get user specified window");
889 return (-1); 886 return (-1);
890 } 887 }
891 888
892 /* DJM: we want all event's for the user specified 889 /* DJM: we want all event's for the user specified
893 window to be handled by SDL. 890 window to be handled by SDL.
894 */ 891 */
895 userWindowProc = 892 userWindowProc =
896 (WNDPROCTYPE) GetWindowLongPtr (SDL_Window, GWLP_WNDPROC); 893 (WNDPROCTYPE) GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
897 SetWindowLongPtr (SDL_Window, GWLP_WNDPROC, (LONG_PTR) WinMessage); 894 SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR) WinMessage);
898 } else { 895 } else {
899 SDL_Window = CreateWindow (SDL_Appname, SDL_Appname, 896 SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
900 (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU 897 (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
901 | WS_MINIMIZEBOX), CW_USEDEFAULT, 898 | WS_MINIMIZEBOX), CW_USEDEFAULT,
902 CW_USEDEFAULT, 0, 0, NULL, NULL, 899 CW_USEDEFAULT, 0, 0, NULL, NULL,
903 SDL_Instance, NULL); 900 SDL_Instance, NULL);
904 if (SDL_Window == NULL) { 901 if (SDL_Window == NULL) {
905 SDL_SetError ("Couldn't create window"); 902 SDL_SetError("Couldn't create window");
906 return (-1); 903 return (-1);
907 } 904 }
908 ShowWindow (SDL_Window, SW_HIDE); 905 ShowWindow(SDL_Window, SW_HIDE);
909 } 906 }
910 907
911 /* Initialize DirectInput */ 908 /* Initialize DirectInput */
912 if (DX5_DInputInit (this) < 0) { 909 if (DX5_DInputInit(this) < 0) {
913 return (-1); 910 return (-1);
914 } 911 }
915 912
916 /* JC 14 Mar 2006 913 /* JC 14 Mar 2006
917 Flush the message loop or this can cause big problems later 914 Flush the message loop or this can cause big problems later
918 Especially if the user decides to use dialog boxes or assert()! 915 Especially if the user decides to use dialog boxes or assert()!
919 */ 916 */
920 WIN_FlushMessageQueue (); 917 WIN_FlushMessageQueue();
921 918
922 /* Ready to roll */ 919 /* Ready to roll */
923 return (0); 920 return (0);
924 } 921 }
925 922
926 void 923 void
927 DX5_DestroyWindow (_THIS) 924 DX5_DestroyWindow(_THIS)
928 { 925 {
929 /* Close down DirectInput */ 926 /* Close down DirectInput */
930 DX5_DInputQuit (this); 927 DX5_DInputQuit(this);
931 928
932 /* Destroy our window */ 929 /* Destroy our window */
933 if (SDL_windowid) { 930 if (SDL_windowid) {
934 SetWindowLongPtr (SDL_Window, GWLP_WNDPROC, 931 SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR) userWindowProc);
935 (LONG_PTR) userWindowProc);
936 } else { 932 } else {
937 DestroyWindow (SDL_Window); 933 DestroyWindow(SDL_Window);
938 } 934 }
939 SDL_UnregisterApp (); 935 SDL_UnregisterApp();
940 936
941 /* JC 14 Mar 2006 937 /* JC 14 Mar 2006
942 Flush the message loop or this can cause big problems later 938 Flush the message loop or this can cause big problems later
943 Especially if the user decides to use dialog boxes or assert()! 939 Especially if the user decides to use dialog boxes or assert()!
944 */ 940 */
945 WIN_FlushMessageQueue (); 941 WIN_FlushMessageQueue();
946 } 942 }
947 943
948 /* vi: set ts=4 sw=4 expandtab: */ 944 /* vi: set ts=4 sw=4 expandtab: */