Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed mouse events for fullscreen windows on Mac OS X
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Dec 3, 2009
1 parent f6595c1 commit b24e4a4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -171,6 +171,7 @@ struct SDL_VideoDisplay

int num_windows;
SDL_Window *windows;
SDL_Window *fullscreen_window;

SDL_Renderer *current_renderer;

Expand Down
2 changes: 2 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -768,13 +768,15 @@ SDL_UpdateFullscreenMode(SDL_Window * window, SDL_bool attempt)
SDL_DisplayMode fullscreen_mode;
if (SDL_GetWindowDisplayMode(window->id, &fullscreen_mode) == 0) {
SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
display->fullscreen_window = window;
return;
}
}
}

/* Nope, restore the desktop mode */
SDL_SetDisplayModeForDisplay(display, NULL);
display->fullscreen_window = NULL;
}

int
Expand Down
14 changes: 14 additions & 0 deletions src/video/cocoa/SDL_cocoaevents.m
Expand Up @@ -187,6 +187,20 @@ - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sende
break;
}
switch ([event type]) {
case NSLeftMouseDown:
case NSOtherMouseDown:
case NSRightMouseDown:
case NSLeftMouseUp:
case NSOtherMouseUp:
case NSRightMouseUp:
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged: /* usually middle mouse dragged */
case NSMouseMoved:
Cocoa_HandleMouseEvent(_this, event);
/* Pass through to NSApp to make sure everything stays in sync */
[NSApp sendEvent:event];
break;
case NSKeyDown:
case NSKeyUp:
case NSFlagsChanged:
Expand Down
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoamouse.h
Expand Up @@ -25,6 +25,7 @@
#define _SDL_cocoamouse_h

extern void Cocoa_InitMouse(_THIS);
extern void Cocoa_HandleMouseEvent(_THIS, NSEvent * event);
extern void Cocoa_QuitMouse(_THIS);

#endif /* _SDL_cocoamouse_h */
Expand Down
82 changes: 82 additions & 0 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -21,6 +21,7 @@
*/
#include "SDL_config.h"

#include "SDL_events.h"
#include "SDL_cocoavideo.h"

#include "../../events/SDL_mouse_c.h"
Expand All @@ -35,6 +36,87 @@
data->mouse = SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
}

static int
ConvertMouseButtonToSDL(int button)
{
switch (button)
{
case 0:
return(SDL_BUTTON_LEFT); /* 1 */
case 1:
return(SDL_BUTTON_RIGHT); /* 3 */
case 2:
return(SDL_BUTTON_MIDDLE); /* 2 */
}
return button;
}

void
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
SDL_Mouse *mouse = SDL_GetMouse(data->mouse);
int i;
NSPoint point;
SDL_Window *window;

/* See if there are any fullscreen windows that might handle this event */
window = NULL;
for (i = 0; i < _this->num_displays; ++i) {
SDL_VideoDisplay *display = &_this->displays[i];
SDL_Window *candidate = display->fullscreen_window;

if (candidate) {
SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
NSRect rect = CGDisplayBounds(displaydata->display);

point = [NSEvent mouseLocation];
point.x = point.x - rect.origin.x;
point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - rect.origin.y;
if (point.x < 0 || point.x >= candidate->w ||
point.y < 0 || point.y >= candidate->h) {
/* The mouse is out of this fullscreen display */
if (mouse->focus == candidate->id) {
SDL_SetMouseFocus(data->mouse, 0);
}
} else {
/* This is it! */
window = candidate;
break;
}
}
}
if (!window) {
return;
}

/* Set the focus appropriately */
if (mouse->focus != window->id) {
SDL_SetMouseFocus(data->mouse, window->id);
}

switch ([event type]) {
case NSLeftMouseDown:
case NSOtherMouseDown:
case NSRightMouseDown:
SDL_SendMouseButton(data->mouse, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
break;
case NSLeftMouseUp:
case NSOtherMouseUp:
case NSRightMouseUp:
SDL_SendMouseButton(data->mouse, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
break;
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged: /* usually middle mouse dragged */
case NSMouseMoved:
SDL_SendMouseMotion(data->mouse, 0, (int)point.x, (int)point.y, 0);
break;
default: /* just to avoid compiler warnings */
break;
}
}

void
Cocoa_QuitMouse(_THIS)
{
Expand Down
13 changes: 2 additions & 11 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -235,17 +235,8 @@ - (void)mouseMoved:(NSEvent *)theEvent

index = _data->videodata->mouse;
mouse = SDL_GetMouse(index);

point = [NSEvent mouseLocation];
if ( (window->flags & SDL_WINDOW_FULLSCREEN) ) {
NSRect rect = CGDisplayBounds(_data->display);

point.x = point.x - rect.origin.x;
point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - rect.origin.y;
} else {
point.x = point.x - window->x;
point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - window->y;
}
point = [theEvent locationInWindow];
point.y = window->h - point.y;
if ( point.x < 0 || point.x >= window->w ||
point.y < 0 || point.y >= window->h ) {
if (mouse->focus != 0) {
Expand Down

0 comments on commit b24e4a4

Please sign in to comment.