Navigation Menu

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

Commit

Permalink
Fixed X11 mouse motion/button events - it's not actually safe to cast…
Browse files Browse the repository at this point in the history
… mouse events to device events.

Fixed building SDL without XInput support
Simplified the process of registering a mouse device
  • Loading branch information
slouken committed Jan 1, 2009
1 parent 96930bc commit e4e5e43
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 197 deletions.
70 changes: 25 additions & 45 deletions src/events/SDL_mouse.c
Expand Up @@ -31,8 +31,6 @@
static int SDL_num_mice = 0;
static int SDL_current_mouse = -1;
static SDL_Mouse **SDL_mice = NULL;
static int *SDL_IdIndex = NULL;
static int SDL_highestId = -1;


/* Public functions */
Expand All @@ -51,62 +49,44 @@ SDL_GetMouse(int index)
return SDL_mice[index];
}

int
SDL_SetMouseIndexId(int id, int index)
{
if (id < 0) {
SDL_SetError("Invalid Mouse ID");
return -1;
}
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 {
SDL_IdIndex[id] = index;
}
return 1;
}

int
static int
SDL_GetMouseIndexId(int id)
{
if (id < 0 || id > SDL_highestId) {
return -1;
int index;
SDL_Mouse *mouse;

for (index = 0; index < SDL_num_mice; ++index) {
mouse = SDL_GetMouse(index);
if (mouse->id == id) {
return index;
}
}
return SDL_IdIndex[id];
return -1;
}

int
SDL_AddMouse(const SDL_Mouse * mouse, int index, char *name, int pressure_max,
SDL_AddMouse(const SDL_Mouse * mouse, char *name, int pressure_max,
int pressure_min, int ends)
{
SDL_Mouse **mice;
int selected_mouse;
int length;
int index, length;

/* Add the mouse to the list of mice */
if (index < 0 || index >= SDL_num_mice || SDL_mice[index]) {
mice =
(SDL_Mouse **) SDL_realloc(SDL_mice,
(SDL_num_mice + 1) * sizeof(*mice));
if (!mice) {
SDL_OutOfMemory();
return -1;
}
if (SDL_GetMouseIndexId(mouse->id) != -1) {
SDL_SetError("Mouse ID already in use");
}

SDL_mice = mice;
index = SDL_num_mice++;
/* Add the mouse to the list of mice */
mice = (SDL_Mouse **) SDL_realloc(SDL_mice,
(SDL_num_mice + 1) * sizeof(*mice));
if (!mice) {
SDL_OutOfMemory();
return -1;
}

SDL_mice = mice;
index = SDL_num_mice++;

SDL_mice[index] = (SDL_Mouse *) SDL_malloc(sizeof(*SDL_mice[index]));
if (!SDL_mice[index]) {
SDL_OutOfMemory();
Expand Down
9 changes: 2 additions & 7 deletions src/events/SDL_mouse_c.h
Expand Up @@ -64,6 +64,7 @@ struct SDL_Mouse
int current_end;

/* Data common to all mice */
int id;
SDL_WindowID focus;
int which;
int x;
Expand All @@ -89,19 +90,13 @@ struct SDL_Mouse
/* Initialize the mouse subsystem */
extern int SDL_MouseInit(void);

/* Assign an id to a mouse at an index */
extern int SDL_SetMouseIndexId(int id, int index);

/* Get the index of a mouse specified by id */
extern int SDL_GetMouseIndexId(int id);

/* Get the mouse at an index */
extern SDL_Mouse *SDL_GetMouse(int index);

/* Add a mouse, possibly reattaching at a particular index (or -1),
returning the index of the mouse, or -1 if there was an error.
*/
extern int SDL_AddMouse(const SDL_Mouse * mouse, int index, char *name,
extern int SDL_AddMouse(const SDL_Mouse * mouse, char *name,
int pressure_max, int pressure_min, int ends);

/* Remove a mouse at an index, clearing the slot for later */
Expand Down
3 changes: 1 addition & 2 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -32,8 +32,7 @@
SDL_Mouse mouse;

SDL_zero(mouse);
data->mouse = SDL_AddMouse(&mouse, -1, "Mouse", 0, 0, 1);
SDL_SetMouseIndexId(data->mouse, data->mouse);
data->mouse = SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
}

void
Expand Down
11 changes: 4 additions & 7 deletions src/video/directfb/SDL_DirectFB_mouse.c
Expand Up @@ -47,6 +47,7 @@ EnumMice(DFBInputDeviceID device_id,
SDL_Mouse mouse;

SDL_zero(mouse);
mouse.id = device_id;
mouse.CreateCursor = DirectFB_CreateCursor;
mouse.ShowCursor = DirectFB_ShowCursor;
mouse.MoveCursor = DirectFB_MoveCursor;
Expand All @@ -55,10 +56,8 @@ EnumMice(DFBInputDeviceID device_id,
mouse.FreeMouse = DirectFB_FreeMouse;
mouse.cursor_shown = 1;

SDL_SetMouseIndexId(device_id, devdata->num_mice);
SDL_AddMouse(&mouse, devdata->num_mice, desc.name, 0, 0, 1);
devdata->mouse_id[devdata->num_mice] = device_id;
devdata->num_mice++;
SDL_AddMouse(&mouse, desc.name, 0, 0, 1);
devdata->mouse_id[devdata->num_mice++] = device_id;
}
return DFENUM_OK;
}
Expand Down Expand Up @@ -91,9 +90,7 @@ DirectFB_InitMouse(_THIS)
mouse.FreeMouse = DirectFB_FreeMouse;
mouse.cursor_shown = 1;

SDL_SetMouseIndexId(0, 0); /* ID == Index ! */
devdata->mouse_id[0] = 0;
SDL_AddMouse(&mouse, 0, "Mouse", 0, 0, 1);
SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
devdata->num_mice = 1;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/video/uikit/SDL_uikitview.m
Expand Up @@ -49,8 +49,9 @@ - (id)initWithFrame:(CGRect)frame {

int i;
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
mice[i].id = i;
mice[i].driverdata = NULL;
SDL_AddMouse(&mice[i], i, "Mouse", 0, 0, 1);
SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1);
}
self.multipleTouchEnabled = YES;

Expand Down
6 changes: 3 additions & 3 deletions src/video/win32/SDL_win32mouse.c
Expand Up @@ -150,7 +150,7 @@ WIN_InitMouse(_THIS)
/* we're saving the handle to the device */
mice[index] = deviceList[i].hDevice;
SDL_zero(mouse);
SDL_SetMouseIndexId(index, index);
mouse.id = index;
l = SDL_strlen(device_name);

/* we're checking if the device isn't by any chance a tablet */
Expand All @@ -176,10 +176,10 @@ WIN_InitMouse(_THIS)
data->WTInfoA(WTI_DEVICES, DVC_NPRESSURE, &pressure);
data->WTInfoA(WTI_DEVICES, DVC_NCSRTYPES, &cursors);
data->mouse =
SDL_AddMouse(&mouse, index, device_name, pressure.axMax,
SDL_AddMouse(&mouse, device_name, pressure.axMax,
pressure.axMin, cursors);
} else {
data->mouse = SDL_AddMouse(&mouse, index, device_name, 0, 0, 1);
data->mouse = SDL_AddMouse(&mouse, device_name, 0, 0, 1);
}
++index;
SDL_free(buffer);
Expand Down
82 changes: 58 additions & 24 deletions src/video/x11/SDL_x11events.c
Expand Up @@ -28,6 +28,7 @@
#include "SDL_syswm.h"
#include "SDL_x11video.h"
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_mouse_c.h"

static void
X11_DispatchEvent(_THIS)
Expand Down Expand Up @@ -91,10 +92,11 @@ X11_DispatchEvent(_THIS)
#endif
if ((xevent.xcrossing.mode != NotifyGrab) &&
(xevent.xcrossing.mode != NotifyUngrab)) {
XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
SDL_SetMouseFocus(move->deviceid, data->windowID);
SDL_SendMouseMotion(move->deviceid, 0, move->x,
move->y, move->axis_data[2]);
/* FIXME: Should we reset data for all mice? */
#if 0
SDL_SetMouseFocus(0, data->windowID);
SDL_SendMouseMotion(0, 0, move->x, move->y, 0);
#endif
}
}
break;
Expand All @@ -112,8 +114,10 @@ X11_DispatchEvent(_THIS)
if ((xevent.xcrossing.mode != NotifyGrab) &&
(xevent.xcrossing.mode != NotifyUngrab) &&
(xevent.xcrossing.detail != NotifyInferior)) {
XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
SDL_SetMouseFocus(move->deviceid, 0);
/* FIXME: Should we reset data for all mice? */
#if 0
SDL_SetMouseFocus(0, 0);
#endif
}
}
break;
Expand Down Expand Up @@ -276,39 +280,69 @@ X11_DispatchEvent(_THIS)
}
break;

case MotionNotify:
#ifdef DEBUG_MOTION
printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
#endif
SDL_SendMouseMotion(0, 0, xevent.xmotion.x, xevent.xmotion.y, 0);
break;

case ButtonPress:
SDL_SendMouseButton(0, SDL_PRESSED, xevent.xbutton.button);
break;

case ButtonRelease:
SDL_SendMouseButton(0, SDL_RELEASED, xevent.xbutton.button);
break;

default:{
if (xevent.type == motion) { /* MotionNotify */
#if SDL_VIDEO_DRIVER_X11_XINPUT
for (i = 0; i < SDL_GetNumMice(); ++i) {
SDL_Mouse *mouse;
X11_MouseData *data;

mouse = SDL_GetMouse(i);
data = (X11_MouseData *)mouse->driverdata;
if (!data) {
continue;
}

if (xevent.type == data->motion) { /* MotionNotify */
XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
#ifdef DEBUG_MOTION
printf("X11 motion: %d,%d\n", move->x, move->y);
#endif
SDL_SendMouseMotion(move->deviceid, 0, move->x,
move->y, move->axis_data[2]);
} else if (xevent.type == button_pressed) { /* ButtonPress */
SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y, move->axis_data[2]);
return;
}
if (xevent.type == data->button_pressed) { /* ButtonPress */
XDeviceButtonPressedEvent *pressed =
(XDeviceButtonPressedEvent *) & xevent;
SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
pressed->button);
} else if (xevent.type == button_released) { /* ButtonRelease */
SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED, pressed->button);
return;
}
if (xevent.type == data->button_released) { /* ButtonRelease */
XDeviceButtonReleasedEvent *released =
(XDeviceButtonReleasedEvent *) & xevent;
SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
released->button);
} else if (xevent.type == proximity_in) {
SDL_SendMouseButton(released->deviceid, SDL_RELEASED, released->button);
return;
}
if (xevent.type == data->proximity_in) {
XProximityNotifyEvent *proximity =
(XProximityNotifyEvent *) & xevent;
SDL_SendProximity(proximity->deviceid, proximity->x,
proximity->y, SDL_PROXIMITYIN);
} else if (xevent.type == proximity_out) {
SDL_SendProximity(proximity->deviceid, proximity->x, proximity->y, SDL_PROXIMITYIN);
return;
}
if (xevent.type == data->proximity_out) {
XProximityNotifyEvent *proximity =
(XProximityNotifyEvent *) & xevent;
SDL_SendProximity(proximity->deviceid, proximity->x,
proximity->y, SDL_PROXIMITYOUT);
SDL_SendProximity(proximity->deviceid, proximity->x, proximity->y, SDL_PROXIMITYOUT);
return;
}
}
#endif
#ifdef DEBUG_XEVENTS
else {
printf("Unhandled event %d\n", xevent.type);
}
printf("Unhandled event %d\n", xevent.type);
#endif
}
break;
Expand Down

0 comments on commit e4e5e43

Please sign in to comment.