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

Commit

Permalink
Fixed the responder chain for event handling, the listener fully hand…
Browse files Browse the repository at this point in the history
…les 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.
  • Loading branch information
slouken committed Feb 21, 2011
1 parent e03bd2b commit ff10d68
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 177 deletions.
76 changes: 23 additions & 53 deletions src/events/SDL_mouse.c
Expand Up @@ -29,43 +29,7 @@
#include "../video/SDL_sysvideo.h"


/* Global mouse information */

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;
};

/* The mouse state */
static SDL_Mouse SDL_mouse;


Expand All @@ -76,6 +40,12 @@ SDL_MouseInit(void)
return (0);
}

SDL_Mouse *
SDL_GetMouse(void)
{
return &SDL_mouse;
}

void
SDL_ResetMouse(void)
{
Expand All @@ -85,15 +55,15 @@ SDL_ResetMouse(void)
SDL_Window *
SDL_GetMouseFocus(void)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

return mouse->focus;
}

void
SDL_SetMouseFocus(SDL_Window * window)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

if (mouse->focus == window) {
return;
Expand All @@ -114,7 +84,7 @@ SDL_SetMouseFocus(SDL_Window * window)
int
SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
int xrel;
int yrel;
Expand Down Expand Up @@ -204,7 +174,7 @@ SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
int
SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
Uint32 type;

Expand Down Expand Up @@ -253,7 +223,7 @@ SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
int
SDL_SendMouseWheel(SDL_Window * window, int x, int y)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();
int posted;

if (window) {
Expand Down Expand Up @@ -285,7 +255,7 @@ SDL_MouseQuit(void)
Uint8
SDL_GetMouseState(int *x, int *y)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

if (x) {
*x = mouse->x;
Expand All @@ -299,7 +269,7 @@ SDL_GetMouseState(int *x, int *y)
Uint8
SDL_GetRelativeMouseState(int *x, int *y)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

if (x) {
*x = mouse->xdelta;
Expand All @@ -315,10 +285,10 @@ SDL_GetRelativeMouseState(int *x, int *y)
void
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

if (mouse->WarpMouse) {
mouse->WarpMouse(mouse, window, x, y);
mouse->WarpMouse(window, x, y);
} else {
SDL_SendMouseMotion(window, 0, x, y);
}
Expand All @@ -327,7 +297,7 @@ SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
int
SDL_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

/* Flush pending mouse motion */
SDL_FlushEvent(SDL_MOUSEMOTION);
Expand All @@ -349,7 +319,7 @@ SDL_SetRelativeMouseMode(SDL_bool enabled)
SDL_bool
SDL_GetRelativeMouseMode()
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

return mouse->relative_mode;
}
Expand All @@ -358,7 +328,7 @@ SDL_Cursor *
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
int w, int h, int hot_x, int hot_y)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();
SDL_Surface *surface;
SDL_Cursor *cursor;
int x, y;
Expand Down Expand Up @@ -424,7 +394,7 @@ SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
void
SDL_SetCursor(SDL_Cursor * cursor)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

/* Set the new cursor */
if (cursor) {
Expand Down Expand Up @@ -458,7 +428,7 @@ SDL_SetCursor(SDL_Cursor * cursor)
SDL_Cursor *
SDL_GetCursor(void)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();

if (!mouse) {
return NULL;
Expand All @@ -469,7 +439,7 @@ SDL_GetCursor(void)
void
SDL_FreeCursor(SDL_Cursor * cursor)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();
SDL_Cursor *curr, *prev;

if (!cursor) {
Expand Down Expand Up @@ -503,7 +473,7 @@ SDL_FreeCursor(SDL_Cursor * cursor)
int
SDL_ShowCursor(int toggle)
{
SDL_Mouse *mouse = &SDL_mouse;
SDL_Mouse *mouse = SDL_GetMouse();
SDL_bool shown;

if (!mouse) {
Expand Down
39 changes: 39 additions & 0 deletions src/events/SDL_mouse_c.h
Expand Up @@ -24,15 +24,54 @@
#ifndef _SDL_mouse_c_h
#define _SDL_mouse_c_h

#include "SDL_mouse.h"

struct SDL_Cursor
{
struct SDL_Cursor *next;
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 */
extern int SDL_MouseInit(void);

/* Get the mouse state structure */
SDL_Mouse *SDL_GetMouse(void);

/* Clear the mouse state */
extern void SDL_ResetMouse(void);

Expand Down
57 changes: 1 addition & 56 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -49,62 +49,7 @@
void
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
{
int i;
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;
}
/* We're correctly using views even in fullscreen mode now */
}

void
Expand Down
6 changes: 2 additions & 4 deletions src/video/cocoa/SDL_cocoawindow.h
Expand Up @@ -29,11 +29,7 @@
typedef struct SDL_WindowData SDL_WindowData;

/* *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 {
#endif
SDL_WindowData *_data;
}

Expand All @@ -59,6 +55,8 @@ typedef struct SDL_WindowData SDL_WindowData;
-(void) mouseUp:(NSEvent *) theEvent;
-(void) rightMouseUp:(NSEvent *) theEvent;
-(void) otherMouseUp:(NSEvent *) theEvent;
-(void) mouseEntered:(NSEvent *)theEvent;
-(void) mouseExited:(NSEvent *)theEvent;
-(void) mouseMoved:(NSEvent *) theEvent;
-(void) mouseDragged:(NSEvent *) theEvent;
-(void) rightMouseDragged:(NSEvent *) theEvent;
Expand Down

0 comments on commit ff10d68

Please sign in to comment.