comparison src/video/windx5/SDL_dx5events.c @ 4393:9afe12fb4c41 SDL-1.2

Fixed bug #901 Tim Angus 2009-12-11 11:45:46 PST Disable mouse event generation when state is not SDL_APPMOUSEFOCUS If a Windows SDL application is minimised by using alt-tab, SDL_APPMOUSEFOCUS is lost as part of the minimisation. Unfortunately, the directx driver doesn't pay any attention to this state when generating mouse button events, so clicking on the Desktop can cause mouse clicks in the SDL application, while it's still minimised. The attached patch fixes this. It looks much more complicated than it actually is due to indentation; here it is ignoring whitespace: tma@abraxas:~/sources/SDL-1.2-svn$ svn diff -x -b Index: src/video/windx5/SDL_dx5events.c =================================================================== --- src/video/windx5/SDL_dx5events.c (revision 5376) +++ src/video/windx5/SDL_dx5events.c (working copy) @@ -374,10 +374,9 @@ if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) { mouse_lost = 1; ClipCursor(NULL); - } - + } else { /* If the mouse was lost, regain some sense of mouse state */ - if ( mouse_lost && (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) { + if ( mouse_lost ) { POINT mouse_pos; Uint8 old_state; Uint8 new_state; @@ -548,6 +547,7 @@ if ( xrel || yrel ) { post_mouse_motion(1, xrel, yrel); } + } } /* The main Win32 event handler */
author Sam Lantinga <slouken@libsdl.org>
date Mon, 14 Dec 2009 22:41:31 +0000
parents 07b330419439
children
comparison
equal deleted inserted replaced
4392:2b8c1aea633b 4393:9afe12fb4c41
372 372
373 /* If mouse focus has been lost, make sure we release the cursor. */ 373 /* If mouse focus has been lost, make sure we release the cursor. */
374 if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) { 374 if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
375 mouse_lost = 1; 375 mouse_lost = 1;
376 ClipCursor(NULL); 376 ClipCursor(NULL);
377 } 377 } else {
378 378 /* If the mouse was lost, regain some sense of mouse state */
379 /* If the mouse was lost, regain some sense of mouse state */ 379 if ( mouse_lost ) {
380 if ( mouse_lost && (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) { 380 POINT mouse_pos;
381 POINT mouse_pos; 381 Uint8 old_state;
382 Uint8 old_state; 382 Uint8 new_state;
383 Uint8 new_state; 383
384 384 /* Set ourselves up with the current cursor position */
385 /* Set ourselves up with the current cursor position */ 385 GetCursorPos(&mouse_pos);
386 GetCursorPos(&mouse_pos); 386 ScreenToClient(SDL_Window, &mouse_pos);
387 ScreenToClient(SDL_Window, &mouse_pos); 387 post_mouse_motion( 0, (Sint16)mouse_pos.x, (Sint16)mouse_pos.y);
388 post_mouse_motion( 0, (Sint16)mouse_pos.x, (Sint16)mouse_pos.y); 388
389 389 /* Check for mouse button changes */
390 /* Check for mouse button changes */ 390 old_state = SDL_GetMouseState(NULL, NULL);
391 old_state = SDL_GetMouseState(NULL, NULL); 391 new_state = 0;
392 new_state = 0; 392 { /* Get the new DirectInput button state for the mouse */
393 { /* Get the new DirectInput button state for the mouse */ 393 #if DIRECTINPUT_VERSION >= 0x700
394 #if DIRECTINPUT_VERSION >= 0x700 394 DIMOUSESTATE2 distate;
395 DIMOUSESTATE2 distate; 395 #else
396 #else 396 DIMOUSESTATE distate;
397 DIMOUSESTATE distate; 397 #endif
398 #endif 398 HRESULT result;
399 HRESULT result; 399
400 400 result=IDirectInputDevice2_GetDeviceState(SDL_DIdev[1],
401 result=IDirectInputDevice2_GetDeviceState(SDL_DIdev[1], 401 sizeof(distate), &distate);
402 sizeof(distate), &distate); 402 if ( result != DI_OK ) {
403 if ( result != DI_OK ) { 403 /* Try again next time */
404 /* Try again next time */ 404 SetDIerror(
405 SetDIerror( 405 "IDirectInputDevice2::GetDeviceState", result);
406 "IDirectInputDevice2::GetDeviceState", result); 406 return;
407 return;
408 }
409 for ( i=3; i>=0; --i ) {
410 if ( (distate.rgbButtons[i]&0x80) == 0x80 ) {
411 new_state |= 0x01;
412 } 407 }
413 new_state <<= 1; 408 for ( i=3; i>=0; --i ) {
414 } 409 if ( (distate.rgbButtons[i]&0x80) == 0x80 ) {
415 } 410 new_state |= 0x01;
416 for ( i=0; i<8; ++i ) { 411 }
417 if ( (old_state&0x01) != (new_state&0x01) ) { 412 new_state <<= 1;
418 button = (Uint8)(i+1);
419 /* Map DI button numbers to SDL */
420 switch ( button ) {
421 case 2: button = SDL_BUTTON_RIGHT; break;
422 case 3: button = SDL_BUTTON_MIDDLE; break;
423 case 4: button = SDL_BUTTON_X1; break;
424 case 5: button = SDL_BUTTON_X2; break;
425 default: break;
426 } 413 }
427 if ( new_state & 0x01 ) { 414 }
428 /* Grab mouse so we get mouse-up */ 415 for ( i=0; i<8; ++i ) {
429 if ( ++mouse_pressed > 0 ) { 416 if ( (old_state&0x01) != (new_state&0x01) ) {
430 SetCapture(SDL_Window); 417 button = (Uint8)(i+1);
431 } 418 /* Map DI button numbers to SDL */
432 state = SDL_PRESSED; 419 switch ( button ) {
433 } else { 420 case 2: button = SDL_BUTTON_RIGHT; break;
434 /* Release mouse after all mouse-ups */ 421 case 3: button = SDL_BUTTON_MIDDLE; break;
435 if ( --mouse_pressed <= 0 ) { 422 case 4: button = SDL_BUTTON_X1; break;
436 ReleaseCapture(); 423 case 5: button = SDL_BUTTON_X2; break;
437 mouse_pressed = 0; 424 default: break;
438 } 425 }
439 state = SDL_RELEASED; 426 if ( new_state & 0x01 ) {
427 /* Grab mouse so we get mouse-up */
428 if ( ++mouse_pressed > 0 ) {
429 SetCapture(SDL_Window);
430 }
431 state = SDL_PRESSED;
432 } else {
433 /* Release mouse after all mouse-ups */
434 if ( --mouse_pressed <= 0 ) {
435 ReleaseCapture();
436 mouse_pressed = 0;
437 }
438 state = SDL_RELEASED;
439 }
440 if ( mouse_buttons_swapped ) {
441 if ( button == 1 ) button = 3;
442 else
443 if ( button == 3 ) button = 1;
444 }
445 posted = SDL_PrivateMouseButton(state, button,
446 0, 0);
440 } 447 }
441 if ( mouse_buttons_swapped ) { 448 old_state >>= 1;
442 if ( button == 1 ) button = 3; 449 new_state >>= 1;
443 else 450 }
444 if ( button == 3 ) button = 1; 451 mouse_lost = 0;
445 } 452 return;
446 posted = SDL_PrivateMouseButton(state, button, 453 }
447 0, 0); 454
448 } 455 /* Translate mouse messages */
449 old_state >>= 1; 456 xrel = 0;
450 new_state >>= 1; 457 yrel = 0;
451 } 458 for ( i=0; i<(int)numevents; ++i ) {
452 mouse_lost = 0; 459 switch (ptrbuf[i].dwOfs) {
453 return; 460 case DIMOFS_X:
454 } 461 if ( timestamp != ptrbuf[i].dwTimeStamp ) {
455 462 if ( xrel || yrel ) {
456 /* Translate mouse messages */ 463 post_mouse_motion(1, xrel, yrel);
457 xrel = 0; 464 xrel = 0;
458 yrel = 0; 465 yrel = 0;
459 for ( i=0; i<(int)numevents; ++i ) { 466 }
460 switch (ptrbuf[i].dwOfs) { 467 timestamp = ptrbuf[i].dwTimeStamp;
461 case DIMOFS_X: 468 }
462 if ( timestamp != ptrbuf[i].dwTimeStamp ) { 469 xrel += (Sint16)ptrbuf[i].dwData;
470 break;
471 case DIMOFS_Y:
472 if ( timestamp != ptrbuf[i].dwTimeStamp ) {
473 if ( xrel || yrel ) {
474 post_mouse_motion(1, xrel, yrel);
475 xrel = 0;
476 yrel = 0;
477 }
478 timestamp = ptrbuf[i].dwTimeStamp;
479 }
480 yrel += (Sint16)ptrbuf[i].dwData;
481 break;
482 case DIMOFS_Z:
463 if ( xrel || yrel ) { 483 if ( xrel || yrel ) {
464 post_mouse_motion(1, xrel, yrel); 484 post_mouse_motion(1, xrel, yrel);
465 xrel = 0; 485 xrel = 0;
466 yrel = 0; 486 yrel = 0;
467 } 487 }
468 timestamp = ptrbuf[i].dwTimeStamp; 488 timestamp = 0;
469 } 489 if((int)ptrbuf[i].dwData > 0)
470 xrel += (Sint16)ptrbuf[i].dwData; 490 button = SDL_BUTTON_WHEELUP;
471 break; 491 else
472 case DIMOFS_Y: 492 button = SDL_BUTTON_WHEELDOWN;
473 if ( timestamp != ptrbuf[i].dwTimeStamp ) { 493 posted = SDL_PrivateMouseButton(
494 SDL_PRESSED, button, 0, 0);
495 posted |= SDL_PrivateMouseButton(
496 SDL_RELEASED, button, 0, 0);
497 break;
498 case DIMOFS_BUTTON0:
499 case DIMOFS_BUTTON1:
500 case DIMOFS_BUTTON2:
501 case DIMOFS_BUTTON3:
502 #if DIRECTINPUT_VERSION >= 0x700
503 case DIMOFS_BUTTON4:
504 case DIMOFS_BUTTON5:
505 case DIMOFS_BUTTON6:
506 case DIMOFS_BUTTON7:
507 #endif
474 if ( xrel || yrel ) { 508 if ( xrel || yrel ) {
475 post_mouse_motion(1, xrel, yrel); 509 post_mouse_motion(1, xrel, yrel);
476 xrel = 0; 510 xrel = 0;
477 yrel = 0; 511 yrel = 0;
478 } 512 }
479 timestamp = ptrbuf[i].dwTimeStamp; 513 timestamp = 0;
480 } 514 button = (Uint8)(ptrbuf[i].dwOfs-DIMOFS_BUTTON0)+1;
481 yrel += (Sint16)ptrbuf[i].dwData; 515 /* Map DI button numbers to SDL */
482 break; 516 switch ( button ) {
483 case DIMOFS_Z: 517 case 2: button = SDL_BUTTON_RIGHT; break;
484 if ( xrel || yrel ) { 518 case 3: button = SDL_BUTTON_MIDDLE; break;
485 post_mouse_motion(1, xrel, yrel); 519 case 4: button = SDL_BUTTON_X1; break;
486 xrel = 0; 520 case 5: button = SDL_BUTTON_X2; break;
487 yrel = 0; 521 default: break;
488 } 522 }
489 timestamp = 0; 523 if ( ptrbuf[i].dwData & 0x80 ) {
490 if((int)ptrbuf[i].dwData > 0) 524 /* Grab mouse so we get mouse-up */
491 button = SDL_BUTTON_WHEELUP; 525 if ( ++mouse_pressed > 0 ) {
492 else 526 SetCapture(SDL_Window);
493 button = SDL_BUTTON_WHEELDOWN; 527 }
494 posted = SDL_PrivateMouseButton( 528 state = SDL_PRESSED;
495 SDL_PRESSED, button, 0, 0); 529 } else {
496 posted |= SDL_PrivateMouseButton( 530 /* Release mouse after all mouse-ups */
497 SDL_RELEASED, button, 0, 0); 531 if ( --mouse_pressed <= 0 ) {
498 break; 532 ReleaseCapture();
499 case DIMOFS_BUTTON0: 533 mouse_pressed = 0;
500 case DIMOFS_BUTTON1: 534 }
501 case DIMOFS_BUTTON2: 535 state = SDL_RELEASED;
502 case DIMOFS_BUTTON3: 536 }
503 #if DIRECTINPUT_VERSION >= 0x700 537 if ( mouse_buttons_swapped ) {
504 case DIMOFS_BUTTON4: 538 if ( button == 1 ) button = 3;
505 case DIMOFS_BUTTON5: 539 else
506 case DIMOFS_BUTTON6: 540 if ( button == 3 ) button = 1;
507 case DIMOFS_BUTTON7: 541 }
508 #endif 542 posted = SDL_PrivateMouseButton(state, button,
509 if ( xrel || yrel ) { 543 0, 0);
510 post_mouse_motion(1, xrel, yrel); 544 break;
511 xrel = 0; 545 }
512 yrel = 0; 546 }
513 } 547 if ( xrel || yrel ) {
514 timestamp = 0; 548 post_mouse_motion(1, xrel, yrel);
515 button = (Uint8)(ptrbuf[i].dwOfs-DIMOFS_BUTTON0)+1; 549 }
516 /* Map DI button numbers to SDL */
517 switch ( button ) {
518 case 2: button = SDL_BUTTON_RIGHT; break;
519 case 3: button = SDL_BUTTON_MIDDLE; break;
520 case 4: button = SDL_BUTTON_X1; break;
521 case 5: button = SDL_BUTTON_X2; break;
522 default: break;
523 }
524 if ( ptrbuf[i].dwData & 0x80 ) {
525 /* Grab mouse so we get mouse-up */
526 if ( ++mouse_pressed > 0 ) {
527 SetCapture(SDL_Window);
528 }
529 state = SDL_PRESSED;
530 } else {
531 /* Release mouse after all mouse-ups */
532 if ( --mouse_pressed <= 0 ) {
533 ReleaseCapture();
534 mouse_pressed = 0;
535 }
536 state = SDL_RELEASED;
537 }
538 if ( mouse_buttons_swapped ) {
539 if ( button == 1 ) button = 3;
540 else
541 if ( button == 3 ) button = 1;
542 }
543 posted = SDL_PrivateMouseButton(state, button,
544 0, 0);
545 break;
546 }
547 }
548 if ( xrel || yrel ) {
549 post_mouse_motion(1, xrel, yrel);
550 } 550 }
551 } 551 }
552 552
553 /* The main Win32 event handler */ 553 /* The main Win32 event handler */
554 LRESULT DX5_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 554 LRESULT DX5_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)