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

Commit

Permalink
Relative mode for tablets. Info on wiki.
Browse files Browse the repository at this point in the history
  • Loading branch information
Szymon Wilczek committed Jul 3, 2008
1 parent b97ca87 commit 629e37b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
2 changes: 1 addition & 1 deletion include/SDL_mouse.h
Expand Up @@ -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()
Expand Down
82 changes: 65 additions & 17 deletions src/events/SDL_mouse.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 */
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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: */
3 changes: 2 additions & 1 deletion src/events/SDL_mouse_c.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */

Expand Down
10 changes: 8 additions & 2 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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]);
Expand Down
5 changes: 5 additions & 0 deletions src/video/x11/SDL_x11video.c
Expand Up @@ -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: */

0 comments on commit 629e37b

Please sign in to comment.