evdev: use a separate structure to denote accumulated motion events

Signed-off-by: Tiago Vignatti <tiago.vignatti@intel.com>
dev
Tiago Vignatti 13 years ago
parent a157fc11a9
commit a52b2e4b20
  1. 93
      compositor/evdev.c

@ -51,6 +51,16 @@ struct evdev_input_device {
int is_touchpad; int is_touchpad;
}; };
/* event type flags */
#define EVDEV_ABSOLUTE_MOTION (1 << 0)
#define EVDEV_RELATIVE_MOTION (1 << 1)
struct evdev_motion_accumulator {
int x, y;
int dx, dy;
int type; /* event type flags */
};
static inline void static inline void
evdev_process_key(struct evdev_input_device *device, evdev_process_key(struct evdev_input_device *device,
struct input_event *e, int time) struct input_event *e, int time)
@ -101,29 +111,30 @@ evdev_process_key(struct evdev_input_device *device,
static inline void static inline void
evdev_process_absolute_motion(struct evdev_input_device *device, evdev_process_absolute_motion(struct evdev_input_device *device,
struct input_event *e, int *x, int *y, struct input_event *e, struct evdev_motion_accumulator *accum)
int *absolute_event)
{ {
const int screen_width = device->output->current->width; const int screen_width = device->output->current->width;
const int screen_height = device->output->current->height; const int screen_height = device->output->current->height;
switch (e->code) { switch (e->code) {
case ABS_X: case ABS_X:
*absolute_event = device->tool; accum->x = (e->value - device->abs.min_x) * screen_width /
*x = (e->value - device->abs.min_x) * screen_width / (device->abs.max_x - device->abs.min_x) +
(device->abs.max_x - device->abs.min_x) + device->output->x; device->output->x;
accum->type = EVDEV_ABSOLUTE_MOTION;
break; break;
case ABS_Y: case ABS_Y:
*absolute_event = device->tool; accum->y = (e->value - device->abs.min_y) * screen_height /
*y = (e->value - device->abs.min_y) * screen_height / (device->abs.max_y - device->abs.min_y) +
(device->abs.max_y - device->abs.min_y) + device->output->y; device->output->y;
accum->type = EVDEV_ABSOLUTE_MOTION;
break; break;
} }
} }
static inline void static inline void
evdev_process_absolute_motion_touchpad(struct evdev_input_device *device, evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
struct input_event *e, int *dx, int *dy) struct input_event *e, struct evdev_motion_accumulator *accum)
{ {
/* FIXME: Make this configurable somehow. */ /* FIXME: Make this configurable somehow. */
const int touchpad_speed = 700; const int touchpad_speed = 700;
@ -134,34 +145,41 @@ evdev_process_absolute_motion_touchpad(struct evdev_input_device *device,
if (device->abs.reset_x) if (device->abs.reset_x)
device->abs.reset_x = 0; device->abs.reset_x = 0;
else { else {
*dx = (e->value - device->abs.old_x) * touchpad_speed / accum->dx = (e->value - device->abs.old_x) *
touchpad_speed /
(device->abs.max_x - device->abs.min_x); (device->abs.max_x - device->abs.min_x);
} }
device->abs.old_x = e->value; device->abs.old_x = e->value;
accum->type = EVDEV_RELATIVE_MOTION;
break; break;
case ABS_Y: case ABS_Y:
e->value -= device->abs.min_y; e->value -= device->abs.min_y;
if (device->abs.reset_y) if (device->abs.reset_y)
device->abs.reset_y = 0; device->abs.reset_y = 0;
else { else {
*dy = (e->value - device->abs.old_y) * touchpad_speed / accum->dy = (e->value - device->abs.old_y) *
touchpad_speed /
/* maybe use x size here to have the same scale? */ /* maybe use x size here to have the same scale? */
(device->abs.max_y - device->abs.min_y); (device->abs.max_y - device->abs.min_y);
} }
device->abs.old_y = e->value; device->abs.old_y = e->value;
accum->type = EVDEV_RELATIVE_MOTION;
break; break;
} }
} }
static inline void static inline void
evdev_process_relative_motion(struct input_event *e, int *dx, int *dy) evdev_process_relative_motion(struct input_event *e,
struct evdev_motion_accumulator *accum)
{ {
switch (e->code) { switch (e->code) {
case REL_X: case REL_X:
*dx += e->value; accum->dx += e->value;
accum->type = EVDEV_RELATIVE_MOTION;
break; break;
case REL_Y: case REL_Y:
*dy += e->value; accum->dy += e->value;
accum->type = EVDEV_RELATIVE_MOTION;
break; break;
} }
} }
@ -188,13 +206,14 @@ is_motion_event(struct input_event *e)
} }
static void static void
evdev_flush_motion(struct wl_input_device *device, uint32_t time, int x, int y, evdev_flush_motion(struct wl_input_device *device, uint32_t time,
int dx, int dy, int absolute_event) struct evdev_motion_accumulator accum)
{ {
if (dx != 0 || dy != 0) if (accum.type == EVDEV_RELATIVE_MOTION)
notify_motion(device, time, x + dx, y + dy); notify_motion(device, time, accum.x + accum.dx,
if (absolute_event) accum.y + accum.dy);
notify_motion(device, time, x, y); if (accum.type == EVDEV_ABSOLUTE_MOTION)
notify_motion(device, time, accum.x, accum.y);
} }
static int static int
@ -203,8 +222,8 @@ evdev_input_device_data(int fd, uint32_t mask, void *data)
struct wlsc_compositor *ec; struct wlsc_compositor *ec;
struct evdev_input_device *device = data; struct evdev_input_device *device = data;
struct input_event ev[8], *e, *end; struct input_event ev[8], *e, *end;
int len, dx, dy, absolute_event; int len;
int x, y; struct evdev_motion_accumulator accumulator;
uint32_t time; uint32_t time;
ec = (struct wlsc_compositor *) ec = (struct wlsc_compositor *)
@ -212,11 +231,11 @@ evdev_input_device_data(int fd, uint32_t mask, void *data)
if (!ec->focus) if (!ec->focus)
return 1; return 1;
dx = 0; accumulator.dx = 0;
dy = 0; accumulator.dy = 0;
absolute_event = 0; accumulator.type = 0;
x = device->master->base.input_device.x; accumulator.x = device->master->base.input_device.x;
y = device->master->base.input_device.y; accumulator.y = device->master->base.input_device.y;
len = read(fd, &ev, sizeof ev); len = read(fd, &ev, sizeof ev);
if (len < 0 || len % sizeof e[0] != 0) { if (len < 0 || len % sizeof e[0] != 0) {
@ -234,23 +253,23 @@ evdev_input_device_data(int fd, uint32_t mask, void *data)
* events and send as a bunch */ * events and send as a bunch */
if (!is_motion_event(e)) { if (!is_motion_event(e)) {
evdev_flush_motion(&device->master->base.input_device, evdev_flush_motion(&device->master->base.input_device,
time, x, y, dx, dy, absolute_event); time, accumulator);
dx = 0; accumulator.dx = 0;
dy = 0; accumulator.dy = 0;
absolute_event = 0; accumulator.type = 0;
} }
switch (e->type) { switch (e->type) {
case EV_REL: case EV_REL:
evdev_process_relative_motion(e, &dx, &dy); evdev_process_relative_motion(e, &accumulator);
break; break;
case EV_ABS: case EV_ABS:
if (device->is_touchpad) if (device->is_touchpad)
evdev_process_absolute_motion_touchpad(device, evdev_process_absolute_motion_touchpad(device,
e, &dx, &dy); e, &accumulator);
else else
evdev_process_absolute_motion(device, e, evdev_process_absolute_motion(device, e,
&x, &y, &absolute_event); &accumulator);
break; break;
case EV_KEY: case EV_KEY:
evdev_process_key(device, e, time); evdev_process_key(device, e, time);
@ -258,8 +277,8 @@ evdev_input_device_data(int fd, uint32_t mask, void *data)
} }
} }
evdev_flush_motion(&device->master->base.input_device, time, x, y, dx, evdev_flush_motion(&device->master->base.input_device, time,
dy, absolute_event); accumulator);
return 1; return 1;
} }

Loading…
Cancel
Save