Commit 65899724 authored by Sam Lantinga's avatar Sam Lantinga

Fixed the responder chain for event handling, the listener fully handles mouse...

Fixed the responder chain for event handling, the listener fully handles mouse events - even in fullscreen mode.
The only reason we need a custom view is to handle right mouse down.

Implemented mouse grabbing, although it's kind of clunky right now.  I'll be adding a relative mode that will be smoother soon.
parent c379a10f
...@@ -29,43 +29,7 @@ ...@@ -29,43 +29,7 @@
#include "../video/SDL_sysvideo.h" #include "../video/SDL_sysvideo.h"
/* Global mouse information */ /* The mouse state */
typedef struct SDL_Mouse SDL_Mouse;
struct SDL_Mouse
{
/* Create a cursor from a surface */
SDL_Cursor *(*CreateCursor) (SDL_Surface * surface, int hot_x, int hot_y);
/* Show the specified cursor, or hide if cursor is NULL */
int (*ShowCursor) (SDL_Cursor * cursor);
/* This is called when a mouse motion event occurs */
void (*MoveCursor) (SDL_Cursor * cursor);
/* Free a window manager cursor */
void (*FreeCursor) (SDL_Cursor * cursor);
/* Warp the mouse to (x,y) */
void (*WarpMouse) (SDL_Mouse * mouse, SDL_Window * window, int x, int y);
/* Data common to all mice */
SDL_Window *focus;
int x;
int y;
int xdelta;
int ydelta;
int last_x, last_y; /* the last reported x and y coordinates */
Uint8 buttonstate;
SDL_bool relative_mode;
SDL_Cursor *cursors;
SDL_Cursor *def_cursor;
SDL_Cursor *cur_cursor;
SDL_bool cursor_shown;
};
static SDL_Mouse SDL_mouse; static SDL_Mouse SDL_mouse;
...@@ -76,6 +40,12 @@ SDL_MouseInit(void) ...@@ -76,6 +40,12 @@ SDL_MouseInit(void)
return (0); return (0);
} }
SDL_Mouse *
SDL_GetMouse(void)
{
return &SDL_mouse;
}
void void
SDL_ResetMouse(void) SDL_ResetMouse(void)
{ {
...@@ -85,7 +55,7 @@ SDL_ResetMouse(void) ...@@ -85,7 +55,7 @@ SDL_ResetMouse(void)
SDL_Window * SDL_Window *
SDL_GetMouseFocus(void) SDL_GetMouseFocus(void)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
return mouse->focus; return mouse->focus;
} }
...@@ -93,7 +63,7 @@ SDL_GetMouseFocus(void) ...@@ -93,7 +63,7 @@ SDL_GetMouseFocus(void)
void void
SDL_SetMouseFocus(SDL_Window * window) SDL_SetMouseFocus(SDL_Window * window)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
if (mouse->focus == window) { if (mouse->focus == window) {
return; return;
...@@ -114,7 +84,7 @@ SDL_SetMouseFocus(SDL_Window * window) ...@@ -114,7 +84,7 @@ SDL_SetMouseFocus(SDL_Window * window)
int int
SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y) SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
int posted; int posted;
int xrel; int xrel;
int yrel; int yrel;
...@@ -204,7 +174,7 @@ SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y) ...@@ -204,7 +174,7 @@ SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
int int
SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button) SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
int posted; int posted;
Uint32 type; Uint32 type;
...@@ -253,7 +223,7 @@ SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button) ...@@ -253,7 +223,7 @@ SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
int int
SDL_SendMouseWheel(SDL_Window * window, int x, int y) SDL_SendMouseWheel(SDL_Window * window, int x, int y)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
int posted; int posted;
if (window) { if (window) {
...@@ -285,7 +255,7 @@ SDL_MouseQuit(void) ...@@ -285,7 +255,7 @@ SDL_MouseQuit(void)
Uint8 Uint8
SDL_GetMouseState(int *x, int *y) SDL_GetMouseState(int *x, int *y)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
if (x) { if (x) {
*x = mouse->x; *x = mouse->x;
...@@ -299,7 +269,7 @@ SDL_GetMouseState(int *x, int *y) ...@@ -299,7 +269,7 @@ SDL_GetMouseState(int *x, int *y)
Uint8 Uint8
SDL_GetRelativeMouseState(int *x, int *y) SDL_GetRelativeMouseState(int *x, int *y)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
if (x) { if (x) {
*x = mouse->xdelta; *x = mouse->xdelta;
...@@ -315,10 +285,10 @@ SDL_GetRelativeMouseState(int *x, int *y) ...@@ -315,10 +285,10 @@ SDL_GetRelativeMouseState(int *x, int *y)
void void
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y) SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
if (mouse->WarpMouse) { if (mouse->WarpMouse) {
mouse->WarpMouse(mouse, window, x, y); mouse->WarpMouse(window, x, y);
} else { } else {
SDL_SendMouseMotion(window, 0, x, y); SDL_SendMouseMotion(window, 0, x, y);
} }
...@@ -327,7 +297,7 @@ SDL_WarpMouseInWindow(SDL_Window * window, int x, int y) ...@@ -327,7 +297,7 @@ SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
int int
SDL_SetRelativeMouseMode(SDL_bool enabled) SDL_SetRelativeMouseMode(SDL_bool enabled)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
/* Flush pending mouse motion */ /* Flush pending mouse motion */
SDL_FlushEvent(SDL_MOUSEMOTION); SDL_FlushEvent(SDL_MOUSEMOTION);
...@@ -349,7 +319,7 @@ SDL_SetRelativeMouseMode(SDL_bool enabled) ...@@ -349,7 +319,7 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)
SDL_bool SDL_bool
SDL_GetRelativeMouseMode() SDL_GetRelativeMouseMode()
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
return mouse->relative_mode; return mouse->relative_mode;
} }
...@@ -358,7 +328,7 @@ SDL_Cursor * ...@@ -358,7 +328,7 @@ SDL_Cursor *
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask, SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
int w, int h, int hot_x, int hot_y) int w, int h, int hot_x, int hot_y)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
SDL_Surface *surface; SDL_Surface *surface;
SDL_Cursor *cursor; SDL_Cursor *cursor;
int x, y; int x, y;
...@@ -424,7 +394,7 @@ SDL_CreateCursor(const Uint8 * data, const Uint8 * mask, ...@@ -424,7 +394,7 @@ SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
void void
SDL_SetCursor(SDL_Cursor * cursor) SDL_SetCursor(SDL_Cursor * cursor)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
/* Set the new cursor */ /* Set the new cursor */
if (cursor) { if (cursor) {
...@@ -458,7 +428,7 @@ SDL_SetCursor(SDL_Cursor * cursor) ...@@ -458,7 +428,7 @@ SDL_SetCursor(SDL_Cursor * cursor)
SDL_Cursor * SDL_Cursor *
SDL_GetCursor(void) SDL_GetCursor(void)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse) { if (!mouse) {
return NULL; return NULL;
...@@ -469,7 +439,7 @@ SDL_GetCursor(void) ...@@ -469,7 +439,7 @@ SDL_GetCursor(void)
void void
SDL_FreeCursor(SDL_Cursor * cursor) SDL_FreeCursor(SDL_Cursor * cursor)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
SDL_Cursor *curr, *prev; SDL_Cursor *curr, *prev;
if (!cursor) { if (!cursor) {
...@@ -503,7 +473,7 @@ SDL_FreeCursor(SDL_Cursor * cursor) ...@@ -503,7 +473,7 @@ SDL_FreeCursor(SDL_Cursor * cursor)
int int
SDL_ShowCursor(int toggle) SDL_ShowCursor(int toggle)
{ {
SDL_Mouse *mouse = &SDL_mouse; SDL_Mouse *mouse = SDL_GetMouse();
SDL_bool shown; SDL_bool shown;
if (!mouse) { if (!mouse) {
......
...@@ -24,15 +24,54 @@ ...@@ -24,15 +24,54 @@
#ifndef _SDL_mouse_c_h #ifndef _SDL_mouse_c_h
#define _SDL_mouse_c_h #define _SDL_mouse_c_h
#include "SDL_mouse.h"
struct SDL_Cursor struct SDL_Cursor
{ {
struct SDL_Cursor *next; struct SDL_Cursor *next;
void *driverdata; void *driverdata;
}; };
typedef struct
{
/* Create a cursor from a surface */
SDL_Cursor *(*CreateCursor) (SDL_Surface * surface, int hot_x, int hot_y);
/* Show the specified cursor, or hide if cursor is NULL */
int (*ShowCursor) (SDL_Cursor * cursor);
/* This is called when a mouse motion event occurs */
void (*MoveCursor) (SDL_Cursor * cursor);
/* Free a window manager cursor */
void (*FreeCursor) (SDL_Cursor * cursor);
/* Warp the mouse to (x,y) */
void (*WarpMouse) (SDL_Window * window, int x, int y);
/* Data common to all mice */
SDL_Window *focus;
int x;
int y;
int xdelta;
int ydelta;
int last_x, last_y; /* the last reported x and y coordinates */
Uint8 buttonstate;
SDL_bool relative_mode;
SDL_Cursor *cursors;
SDL_Cursor *def_cursor;
SDL_Cursor *cur_cursor;
SDL_bool cursor_shown;
} SDL_Mouse;
/* Initialize the mouse subsystem */ /* Initialize the mouse subsystem */
extern int SDL_MouseInit(void); extern int SDL_MouseInit(void);
/* Get the mouse state structure */
SDL_Mouse *SDL_GetMouse(void);
/* Clear the mouse state */ /* Clear the mouse state */
extern void SDL_ResetMouse(void); extern void SDL_ResetMouse(void);
......
...@@ -49,62 +49,7 @@ ConvertMouseButtonToSDL(int button) ...@@ -49,62 +49,7 @@ ConvertMouseButtonToSDL(int button)
void void
Cocoa_HandleMouseEvent(_THIS, NSEvent *event) Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
{ {
int i; /* We're correctly using views even in fullscreen mode now */
NSPoint point = { 0, 0 };
SDL_Window *window;
SDL_Window *focus = SDL_GetMouseFocus();
/* 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_Rect bounds;
Cocoa_GetDisplayBounds(_this, display, &bounds);
point = [NSEvent mouseLocation];
point.x = point.x - bounds.x;
point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - bounds.y;
if ((point.x >= 0 && point.x < candidate->w) &&
(point.y >= 0 && point.y < candidate->h)) {
/* This is it! */
window = candidate;
break;
} else if (candidate == focus) {
SDL_SetMouseFocus(NULL);
}
}
}
if (!window) {
return;
}
switch ([event type]) {
case NSLeftMouseDown:
case NSOtherMouseDown:
case NSRightMouseDown:
SDL_SendMouseButton(window, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
break;
case NSLeftMouseUp:
case NSOtherMouseUp:
case NSRightMouseUp:
SDL_SendMouseButton(window, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
break;
case NSScrollWheel:
Cocoa_HandleMouseWheel(window, event);
break;
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged: /* usually middle mouse dragged */
case NSMouseMoved:
SDL_SendMouseMotion(window, 0, (int)point.x, (int)point.y);
break;
default: /* just to avoid compiler warnings */
break;
}
} }
void void
......
...@@ -29,11 +29,7 @@ ...@@ -29,11 +29,7 @@
typedef struct SDL_WindowData SDL_WindowData; typedef struct SDL_WindowData SDL_WindowData;
/* *INDENT-OFF* */ /* *INDENT-OFF* */
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6
@interface Cocoa_WindowListener : NSResponder <NSWindowDelegate> {
#else
@interface Cocoa_WindowListener : NSResponder { @interface Cocoa_WindowListener : NSResponder {
#endif
SDL_WindowData *_data; SDL_WindowData *_data;
} }
...@@ -59,6 +55,8 @@ typedef struct SDL_WindowData SDL_WindowData; ...@@ -59,6 +55,8 @@ typedef struct SDL_WindowData SDL_WindowData;
-(void) mouseUp:(NSEvent *) theEvent; -(void) mouseUp:(NSEvent *) theEvent;
-(void) rightMouseUp:(NSEvent *) theEvent; -(void) rightMouseUp:(NSEvent *) theEvent;
-(void) otherMouseUp:(NSEvent *) theEvent; -(void) otherMouseUp:(NSEvent *) theEvent;
-(void) mouseEntered:(NSEvent *)theEvent;
-(void) mouseExited:(NSEvent *)theEvent;
-(void) mouseMoved:(NSEvent *) theEvent; -(void) mouseMoved:(NSEvent *) theEvent;
-(void) mouseDragged:(NSEvent *) theEvent; -(void) mouseDragged:(NSEvent *) theEvent;
-(void) rightMouseDragged:(NSEvent *) theEvent; -(void) rightMouseDragged:(NSEvent *) theEvent;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment