blob: d79db8fad593cef829f567765ded1a0bf1dceca8 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05008 *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -05009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050017 */
18
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050019#include <stdio.h>
20#include <stdlib.h>
21#include <linux/input.h>
22#include <fcntl.h>
23#include <unistd.h>
24
25#include <linux/input.h>
26
27#include "wayland.h"
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050028#include "wayland-system-compositor.h"
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050029
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050030struct evdev_input_device {
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050031 struct wlsc_input_device *device;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050032 struct wl_event_source *source;
Kristian Høgsberg0ee84082008-11-25 23:16:31 -050033 int tool, new_x, new_y;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050034 int base_x, base_y;
35 int fd;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050036};
37
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050038static void evdev_input_device_data(int fd, uint32_t mask, void *data)
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050039{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050040 struct evdev_input_device *device = data;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050041 struct input_event ev[8], *e, *end;
42 int len, value, dx, dy, absolute_event;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050043 int x, y;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050044
45 dx = 0;
46 dy = 0;
47 absolute_event = 0;
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -050048 wlsc_device_get_position(device->device, &x, &y);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050049
50 len = read(fd, &ev, sizeof ev);
51 if (len < 0 || len % sizeof e[0] != 0) {
52 /* FIXME: handle error... reopen device? */;
53 return;
54 }
55
56 e = ev;
57 end = (void *) ev + len;
58 for (e = ev; e < end; e++) {
59 /* Get the signed value, earlier kernels had this as unsigned */
60 value = e->value;
61
62 switch (e->type) {
63 case EV_REL:
64 switch (e->code) {
65 case REL_X:
66 dx += value;
67 break;
68
69 case REL_Y:
70 dy += value;
71 break;
72 }
Kristian Høgsbergec8ef722008-11-24 00:13:13 -050073 break;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050074
75 case EV_ABS:
76 absolute_event = 1;
77 switch (e->code) {
78 case ABS_X:
Kristian Høgsberg0ee84082008-11-25 23:16:31 -050079 if (device->new_x) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050080 device->base_x = x - value;
Kristian Høgsberg0ee84082008-11-25 23:16:31 -050081 device->new_x = 0;
82 }
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050083 x = device->base_x + value;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050084 break;
85 case ABS_Y:
Kristian Høgsberg0ee84082008-11-25 23:16:31 -050086 if (device->new_y) {
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050087 device->base_y = y - value;
Kristian Høgsberg0ee84082008-11-25 23:16:31 -050088 device->new_y = 0;
89 }
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050090 y = device->base_y + value;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050091 break;
92 }
Kristian Høgsbergec8ef722008-11-24 00:13:13 -050093 break;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -050094
95 case EV_KEY:
96 if (value == 2)
97 break;
98
99 switch (e->code) {
100 case BTN_TOUCH:
101 case BTN_TOOL_PEN:
102 case BTN_TOOL_RUBBER:
103 case BTN_TOOL_BRUSH:
104 case BTN_TOOL_PENCIL:
105 case BTN_TOOL_AIRBRUSH:
106 case BTN_TOOL_FINGER:
107 case BTN_TOOL_MOUSE:
108 case BTN_TOOL_LENS:
Kristian Høgsberg0ee84082008-11-25 23:16:31 -0500109 if (device->tool == 0 && value) {
110 device->new_x = 1;
111 device->new_y = 1;
112 }
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500113 device->tool = value ? e->code : 0;
114 break;
115
116 case BTN_LEFT:
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500117 case BTN_RIGHT:
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500118 case BTN_MIDDLE:
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500119 case BTN_SIDE:
120 case BTN_EXTRA:
121 case BTN_FORWARD:
122 case BTN_BACK:
123 case BTN_TASK:
124 notify_button(device->device, e->code, value);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500125 break;
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500126
127 default:
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500128 notify_key(device->device, e->code, value);
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -0500129 break;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500130 }
131 }
132 }
133
134 if (dx != 0 || dy != 0)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500135 notify_motion(device->device, x + dx, y + dy);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500136 if (absolute_event && device->tool)
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500137 notify_motion(device->device, x, y);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500138}
139
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500140struct evdev_input_device *
Kristian Høgsberg82f6e8a2008-12-19 13:47:53 -0500141evdev_input_device_create(struct wlsc_input_device *master,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500142 struct wl_display *display, const char *path)
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500143{
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500144 struct evdev_input_device *device;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500145 struct wl_event_loop *loop;
146
147 device = malloc(sizeof *device);
148 if (device == NULL)
149 return NULL;
150
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500151 device->tool = 1;
Kristian Høgsberg0ee84082008-11-25 23:16:31 -0500152 device->new_x = 1;
153 device->new_y = 1;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500154 device->device = master;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500155
156 device->fd = open(path, O_RDONLY);
157 if (device->fd < 0) {
158 free(device);
159 fprintf(stderr, "couldn't create pointer for %s: %m\n", path);
160 return NULL;
161 }
162
163 loop = wl_display_get_event_loop(display);
164 device->source = wl_event_loop_add_fd(loop, device->fd,
165 WL_EVENT_READABLE,
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500166 evdev_input_device_data, device);
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500167 if (device->source == NULL) {
168 close(device->fd);
169 free(device);
170 return NULL;
171 }
172
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -0500173 return device;
Kristian Høgsbergf9bc7952008-11-02 10:12:29 -0500174}