From 629e37b7ed07a616733214c3df603664c48480d2 Mon Sep 17 00:00:00 2001 From: Szymon Wilczek Date: Thu, 3 Jul 2008 22:03:58 +0000 Subject: [PATCH] Relative mode for tablets. Info on wiki. --- include/SDL_mouse.h | 2 +- src/events/SDL_mouse.c | 82 +++++++++++++++++++++++++++-------- src/events/SDL_mouse_c.h | 3 +- src/video/x11/SDL_x11events.c | 10 ++++- src/video/x11/SDL_x11video.c | 5 +++ 5 files changed, 81 insertions(+), 21 deletions(-) diff --git a/include/SDL_mouse.h b/include/SDL_mouse.h index b777717ef..1aa803b37 100644 --- a/include/SDL_mouse.h +++ b/include/SDL_mouse.h @@ -92,7 +92,7 @@ extern DECLSPEC SDL_WindowID SDLCALL SDL_GetMouseFocusWindow(void); * * \sa SDL_GetRelativeMouseMode() */ -extern DECLSPEC int SDLCALL SDL_SetRelativeMouseMode(SDL_bool enabled); +extern DECLSPEC int SDLCALL SDL_SetRelativeMouseMode(SDL_bool enabled, int index); /** * \fn SDL_bool SDL_GetRelativeMouseMode() diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c index 89a1dee51..99bc6bcc2 100644 --- a/src/events/SDL_mouse.c +++ b/src/events/SDL_mouse.c @@ -33,8 +33,8 @@ static int SDL_current_mouse; static SDL_Mouse **SDL_mice; int *SDL_IdIndex; int SDL_highestId; - - +int last_x, last_y; +int x_max, y_max; /* Public functions */ int SDL_MouseInit(void) @@ -85,6 +85,8 @@ SDL_AddMouse(const SDL_Mouse * mouse, int index, char* name) SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY); SDL_SetCursor(SDL_mice[index]->def_cursor); + SDL_mice[index]->proximity=SDL_TRUE; + SDL_mice[index]->relative_mode=SDL_FALSE; SDL_SelectMouse(selected_mouse); return index; @@ -180,9 +182,9 @@ FlushMouseMotion(void *param, SDL_Event * event) } int -SDL_SetRelativeMouseMode(SDL_bool enabled) +SDL_SetRelativeMouseMode(SDL_bool enabled, int index) { - SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse); + SDL_Mouse *mouse = SDL_GetMouse(index); if (!mouse) { return -1; @@ -326,12 +328,20 @@ SDL_SendProximity(int id, int x, int y, int type) if(SDL_ProcessEvents[type]==SDL_ENABLE) { SDL_Event event; - event.proximity.which=index; + event.proximity.which=(Uint8)index; event.proximity.x=x; event.proximity.y=y; event.type=type; event.proximity.type=type; posted = (SDL_PushEvent(&event) > 0); + if(type==SDL_PROXIMITYIN) + { + SDL_mice[index]->proximity=SDL_TRUE; + } + else + { + SDL_mice[index]->proximity=SDL_FALSE; + } } return posted; } @@ -348,16 +358,21 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) if (!mouse || mouse->flush_motion) { return 0; } - - if (relative) { + if(mouse->proximity==SDL_FALSE) + { + last_x=x; + last_y=y; + return 0; + } + if (mouse->relative_mode==SDL_TRUE && mouse->proximity==SDL_TRUE) { /* Push the cursor around */ - xrel = x; - yrel = y; - x = (mouse->x + xrel); - y = (mouse->y + yrel); + xrel = x - last_x; + yrel = y - last_y; + //x = (mouse->x + xrel); + //y = (mouse->y + yrel); } else { - xrel = x - mouse->x; - yrel = y - mouse->y; + xrel = x - last_x; + yrel = y - last_y; } /* Drop events that don't change state */ @@ -369,10 +384,37 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) } /* Update internal mouse state */ - if (!mouse->relative_mode) { + if (mouse->relative_mode==SDL_FALSE) { mouse->x = x; mouse->y = y; } + else + { + if(mouse->x+xrel>x_max) + { + mouse->x=x_max; + } + else if(mouse->x+xrel<0) + { + mouse->x=0; + } + else + { + mouse->x+=xrel; + } + if(mouse->y+yrel>y_max) + { + mouse->y=y_max; + } + else if(mouse->y+yrel<0) + { + mouse->y=0; + } + else + { + mouse->y+=yrel; + } + } mouse->xdelta += xrel; mouse->ydelta += yrel; mouse->z=z; @@ -385,10 +427,10 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) /* Post the event, if desired */ posted = 0; - if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE) { + if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE && SDL_mice[index]->proximity==SDL_TRUE) { SDL_Event event; event.motion.type = SDL_MOUSEMOTION; - event.motion.which = (Uint8) index; +event.motion.which = (Uint8) index; event.motion.state = mouse->buttonstate; event.motion.x = mouse->x; event.motion.y = mouse->y; @@ -398,6 +440,8 @@ SDL_SendMouseMotion(int id, int relative, int x, int y,int z) event.motion.windowID = mouse->focus; posted = (SDL_PushEvent(&event) > 0); } + last_x=x; + last_y=y; return posted; } @@ -724,5 +768,9 @@ char* SDL_GetMouseName(int index) return mouse->name; } - +void SDL_UpdateCoordinates(int x, int y) +{ + x_max=x; + y_max=y; +} /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h index 3346bf52b..0569099ec 100644 --- a/src/events/SDL_mouse_c.h +++ b/src/events/SDL_mouse_c.h @@ -67,6 +67,7 @@ struct SDL_Mouse char* name; Uint8 buttonstate; SDL_bool relative_mode; + SDL_bool proximity; SDL_bool flush_motion; SDL_Cursor *cursors; @@ -118,7 +119,7 @@ extern int SDL_GetNumOfMice(void); extern char* SDL_GetMouseName(int index); - +extern void SDL_UpdateCoordinates(int x, int y); #endif /* _SDL_mouse_c_h */ diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index 29fde2c79..f5a2433c1 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -37,6 +37,7 @@ extern int button_pressed; extern int button_released; extern int proximity_in; extern int proximity_out; +extern int x_max,y_max; static void @@ -126,8 +127,8 @@ X11_DispatchEvent(_THIS) (xevent.xcrossing.mode != NotifyUngrab) && (xevent.xcrossing.detail != NotifyInferior)) { XDeviceMotionEvent* move=(XDeviceMotionEvent*)&xevent; - SDL_SendMouseMotion(move->deviceid, 0, - move->x, move->y,move->axis_data[2]); + //SDL_SendMouseMotion(move->deviceid, 0, + // move->x, move->y,move->axis_data[2]); SDL_SetMouseFocus(move->deviceid, 0); } } @@ -297,6 +298,11 @@ X11_DispatchEvent(_THIS) #ifdef DEBUG_MOTION printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y); #endif + XWindowAttributes attrib; + XGetWindowAttributes(videodata->display, ((XAnyEvent*)&xevent)->window, &attrib); + /*x_max=attrib.width; + y_max=attrib.height;*/ + SDL_UpdateCoordinates(attrib.width, attrib.height); XDeviceMotionEvent* move=(XDeviceMotionEvent*)&xevent; SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y,move->axis_data[2]); diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c index 35347a3ef..ee8abb7f9 100644 --- a/src/video/x11/SDL_x11video.c +++ b/src/video/x11/SDL_x11video.c @@ -362,4 +362,9 @@ X11_VideoQuit(_THIS) free(SDL_XDevices); } +/*void X11_ForwardWindowCoordinates(int x, int y) +{ + SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; +}*/ + /* vim: set ts=4 sw=4 expandtab: */