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

Commit

Permalink
Date: Sun, 07 Dec 2008 13:35:23 +0100
Browse files Browse the repository at this point in the history
From: Couriersud
Subject: SDL: Mouse last_x, last_y into SDL_Mouse

the attached diff moves the static vars last_x and last_y into
SDL_Mouse. These, as far as I understand it, should be tied to the
individual mouse.

The patch also makes the code check for out of window conditions of
mouse->x,y when relative movements are passed to MouseSendMotion.

Also attached is the latest DirectFB code (dfb20081208) supporting
multiple mice and keyboards. This works quite well with sdlmame now. It
however needs more testing with different directfb configurations.
  • Loading branch information
slouken committed Dec 8, 2008
1 parent fd3a9cb commit 1405532
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 97 deletions.
60 changes: 31 additions & 29 deletions src/events/SDL_mouse.c
Expand Up @@ -33,7 +33,6 @@ static int SDL_current_mouse = -1;
static SDL_Mouse **SDL_mice = NULL;
static int *SDL_IdIndex = NULL;
static int SDL_highestId = -1;
static int last_x, last_y; /* the last reported x and y coordinates by the system cursor */


/* Public functions */
Expand Down Expand Up @@ -61,12 +60,15 @@ SDL_SetMouseIndexId(int id, int index)
}
if (id > SDL_highestId) {
int *indexes;
int i;
indexes = (int *) SDL_realloc(SDL_IdIndex, (id + 1) * sizeof(int));
if (!indexes) {
SDL_OutOfMemory();
return -1;
}
SDL_IdIndex = indexes;
for (i = SDL_highestId + 1; i <= id; i++)
SDL_IdIndex[i] = -1;
SDL_IdIndex[id] = index;
SDL_highestId = id;
} else {
Expand Down Expand Up @@ -378,8 +380,8 @@ SDL_SendProximity(int id, int x, int y, int type)
return 0;
}

last_x = x;
last_y = y;
mouse->last_x = x;
mouse->last_y = y;
if (SDL_ProcessEvents[type] == SDL_ENABLE) {
SDL_Event event;
event.proximity.which = (Uint8) index;
Expand All @@ -405,27 +407,28 @@ SDL_SendMouseMotion(int id, int relative, int x, int y, int pressure)
int posted;
int xrel;
int yrel;
int x_max = 0, y_max = 0;

if (!mouse || mouse->flush_motion) {
return 0;
}

/* if the mouse is out of proximity we don't to want to have any motion from it */
if (mouse->proximity == SDL_FALSE) {
last_x = x;
last_y = y;
mouse->last_x = x;
mouse->last_y = y;
return 0;
}

/* the relative motion is calculated regarding the system cursor last position */
if (relative) {
xrel = x;
yrel = y;
x = (last_x + x);
y = (last_y + y);
x = (mouse->last_x + x);
y = (mouse->last_y + y);
} else {
xrel = x - last_x;
yrel = y - last_y;
xrel = x - mouse->last_x;
yrel = y - mouse->last_y;
}

/* Drop events that don't change state */
Expand All @@ -441,27 +444,26 @@ SDL_SendMouseMotion(int id, int relative, int x, int y, int pressure)
mouse->x = x;
mouse->y = y;
} else {
/* while using the relative mode and many windows, we have to be
sure that the pointers find themselves inside the windows */
int x_max, y_max;
mouse->x += xrel;
mouse->y += yrel;
}

SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
SDL_GetWindowSize(mouse->focus, &x_max, &y_max);

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;
}
/* make sure that the pointers find themselves inside the windows */
/* only check if mouse->xmax is set ! */
if (x_max && mouse->x > x_max) {
mouse->x = x_max;
} else if (mouse->x < 0) {
mouse->x = 0;
}

if (y_max && mouse->y > y_max) {
mouse->y = y_max;
} else if (mouse->y < 0) {
mouse->y = 0;
}

mouse->xdelta += xrel;
mouse->ydelta += yrel;
mouse->pressure = pressure;
Expand Down Expand Up @@ -491,8 +493,8 @@ SDL_SendMouseMotion(int id, int relative, int x, int y, int pressure)
event.motion.cursor = mouse->current_end;
posted = (SDL_PushEvent(&event) > 0);
}
last_x = x;
last_y = y;
mouse->last_x = x;
mouse->last_y = y;
return posted;
}

Expand Down
1 change: 1 addition & 0 deletions src/events/SDL_mouse_c.h
Expand Up @@ -71,6 +71,7 @@ struct SDL_Mouse
int z; /* for future use */
int xdelta;
int ydelta;
int last_x, last_y; /* the last reported x and y coordinates */
char *name;
Uint8 buttonstate;
SDL_bool relative_mode;
Expand Down
2 changes: 1 addition & 1 deletion src/video/SDL_video.c
Expand Up @@ -1054,7 +1054,7 @@ SDL_GetWindowSize(SDL_WindowID windowID, int *w, int *h)
{
SDL_Window *window = SDL_GetWindowFromID(windowID);

if (!window) {
if (window) {
if (w) {
*w = window->w;
}
Expand Down

0 comments on commit 1405532

Please sign in to comment.