| 1 |
/* |
| 2 |
* Copyright (C) 2009 Red Hat, Inc. |
| 3 |
* Copyright (C) 2012 Collabora Ltd. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 11 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 13 |
* more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public License |
| 16 |
* along with this program; if not, write to the Free Software Foundation, |
| 17 |
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <clutter/clutter.h> |
| 23 |
|
| 24 |
#if defined CLUTTER_WINDOWING_X11 && OS_LINUX && HAVE_XINPUT_2_2 |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <math.h> |
| 32 |
#include <linux/input.h> |
| 33 |
#include <linux/uinput.h> |
| 34 |
#include <dlfcn.h> |
| 35 |
|
| 36 |
#include <clutter/x11/clutter-x11.h> |
| 37 |
|
| 38 |
#define ABS_MAX_X 32768 |
| 39 |
#define ABS_MAX_Y 32768 |
| 40 |
|
| 41 |
#define TOUCH_POINTS 10 |
| 42 |
|
| 43 |
static ClutterPoint gesture_points[] = { |
| 44 |
{ 100., 100. }, |
| 45 |
{ 110., 100. }, |
| 46 |
{ 120., 100. }, |
| 47 |
{ 130., 100. }, |
| 48 |
{ 140., 100. }, |
| 49 |
{ 150., 100. }, |
| 50 |
{ 160., 100. }, |
| 51 |
{ 170., 100. }, |
| 52 |
{ 180., 100. }, |
| 53 |
{ 190., 100. }, |
| 54 |
}; |
| 55 |
|
| 56 |
typedef struct _State State; |
| 57 |
|
| 58 |
struct _State |
| 59 |
{ |
| 60 |
gboolean pass; |
| 61 |
ClutterPoint gesture_points_to_check[TOUCH_POINTS]; |
| 62 |
int gesture_points; |
| 63 |
}; |
| 64 |
|
| 65 |
static int fd = -1; |
| 66 |
|
| 67 |
static void send_event(int fd, int type, int code, int value, int sec, int usec) |
| 68 |
{ |
| 69 |
static int sec_offset = -1; |
| 70 |
static long last_time = -1; |
| 71 |
long newtime; |
| 72 |
struct input_event event; |
| 73 |
|
| 74 |
event.type = type; |
| 75 |
event.code = code; |
| 76 |
event.value = value; |
| 77 |
|
| 78 |
if (sec_offset == -1) |
| 79 |
sec_offset = sec; |
| 80 |
|
| 81 |
sec -= sec_offset; |
| 82 |
newtime = sec * 1000000 + usec; |
| 83 |
|
| 84 |
if (last_time > 0) |
| 85 |
usleep(newtime - last_time); |
| 86 |
|
| 87 |
gettimeofday(&event.time, NULL); |
| 88 |
|
| 89 |
if (write(fd, &event, sizeof(event)) < sizeof(event)) |
| 90 |
perror("Send event failed."); |
| 91 |
|
| 92 |
last_time = newtime; |
| 93 |
} |
| 94 |
|
| 95 |
static gboolean |
| 96 |
event_cb (ClutterActor *actor, ClutterEvent *event, State *state) |
| 97 |
{ |
| 98 |
int i; |
| 99 |
|
| 100 |
if (event->type != CLUTTER_TOUCH_BEGIN && |
| 101 |
event->type != CLUTTER_TOUCH_UPDATE) |
| 102 |
return FALSE; |
| 103 |
|
| 104 |
state->gesture_points_to_check[state->gesture_points].x = ceil (event->touch.x); |
| 105 |
state->gesture_points_to_check[state->gesture_points].y = ceil (event->touch.y); |
| 106 |
state->gesture_points++; |
| 107 |
|
| 108 |
if (state->gesture_points == TOUCH_POINTS) |
| 109 |
{ |
| 110 |
for (i = 0; i < TOUCH_POINTS; i++) |
| 111 |
{ |
| 112 |
if (state->gesture_points_to_check[i].x != gesture_points[i].x || |
| 113 |
state->gesture_points_to_check[i].y != gesture_points[i].y) |
| 114 |
{ |
| 115 |
if (g_test_verbose ()) |
| 116 |
g_print ("error: expected (%d, %d) but found (%d, %d) at position %d\n", |
| 117 |
(int) gesture_points[i].x, (int) gesture_points[i].y, |
| 118 |
(int) state->gesture_points_to_check[i].x, |
| 119 |
(int) state->gesture_points_to_check[i].y, |
| 120 |
i); |
| 121 |
state->pass = FALSE; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
clutter_main_quit (); |
| 127 |
} |
| 128 |
|
| 129 |
return TRUE; |
| 130 |
} |
| 131 |
|
| 132 |
static void |
| 133 |
screen_coords_to_device (int screen_x, int screen_y, |
| 134 |
int *device_x, int *device_y) |
| 135 |
{ |
| 136 |
int display_width = DisplayWidth (clutter_x11_get_default_display (), |
| 137 |
clutter_x11_get_default_screen ()); |
| 138 |
int display_height = DisplayHeight (clutter_x11_get_default_display (), |
| 139 |
clutter_x11_get_default_screen ()); |
| 140 |
|
| 141 |
*device_x = (screen_x * ABS_MAX_X) / display_width; |
| 142 |
*device_y = (screen_y * ABS_MAX_Y) / display_height; |
| 143 |
} |
| 144 |
|
| 145 |
static gboolean |
| 146 |
perform_gesture (gpointer data) |
| 147 |
{ |
| 148 |
int i; |
| 149 |
|
| 150 |
for (i = 0; i < TOUCH_POINTS; i++) |
| 151 |
{ |
| 152 |
int x = gesture_points[i].x; |
| 153 |
int y = gesture_points[i].y; |
| 154 |
|
| 155 |
screen_coords_to_device (x, y, &x, &y); |
| 156 |
|
| 157 |
send_event(fd, EV_ABS, ABS_MT_SLOT, 0, 1, i * 100); |
| 158 |
send_event(fd, EV_ABS, ABS_MT_TRACKING_ID, 1, 1, i * 100 + 10); |
| 159 |
|
| 160 |
send_event(fd, EV_ABS, ABS_MT_POSITION_X, x, 1, i * 100 + 20); |
| 161 |
send_event(fd, EV_ABS, ABS_MT_POSITION_Y, y, 1, i * 100 + 30); |
| 162 |
send_event(fd, EV_SYN, SYN_MT_REPORT, 0, 1, i * 100 + 40); |
| 163 |
send_event(fd, EV_SYN, SYN_REPORT, 0, 1, i * 100 + 50); |
| 164 |
} |
| 165 |
|
| 166 |
send_event(fd, EV_ABS, ABS_MT_TRACKING_ID, -1, 1, TOUCH_POINTS * 100 + 10); |
| 167 |
send_event(fd, EV_SYN, SYN_MT_REPORT, 0, 1, TOUCH_POINTS * 100 + 20); |
| 168 |
send_event(fd, EV_SYN, SYN_REPORT, 0, 1, TOUCH_POINTS * 100 + 30); |
| 169 |
|
| 170 |
return G_SOURCE_REMOVE; |
| 171 |
} |
| 172 |
|
| 173 |
static int |
| 174 |
setup (struct uinput_user_dev *dev, int fd) |
| 175 |
{ |
| 176 |
strcpy (dev->name, "eGalax Touch Screen"); |
| 177 |
dev->id.bustype = 0x18; |
| 178 |
dev->id.vendor = 0xeef; |
| 179 |
dev->id.product = 0x20; |
| 180 |
dev->id.version = 0x1; |
| 181 |
|
| 182 |
if (ioctl (fd, UI_SET_EVBIT, EV_SYN) == -1) |
| 183 |
goto error; |
| 184 |
|
| 185 |
if (ioctl (fd, UI_SET_EVBIT, EV_KEY) == -1) |
| 186 |
goto error; |
| 187 |
|
| 188 |
if (ioctl (fd, UI_SET_KEYBIT, BTN_TOUCH) == -1) |
| 189 |
goto error; |
| 190 |
|
| 191 |
if (ioctl (fd, UI_SET_EVBIT, EV_ABS) == -1) |
| 192 |
goto error; |
| 193 |
|
| 194 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_X) == -1) |
| 195 |
goto error; |
| 196 |
else |
| 197 |
{ |
| 198 |
int idx = ABS_X; |
| 199 |
dev->absmin[idx] = 0; |
| 200 |
dev->absmax[idx] = ABS_MAX_X; |
| 201 |
dev->absfuzz[idx] = 1; |
| 202 |
dev->absflat[idx] = 0; |
| 203 |
|
| 204 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 205 |
dev->absmax[idx]++; |
| 206 |
} |
| 207 |
|
| 208 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_Y) == -1) |
| 209 |
goto error; |
| 210 |
else |
| 211 |
{ |
| 212 |
int idx = ABS_Y; |
| 213 |
dev->absmin[idx] = 0; |
| 214 |
dev->absmax[idx] = ABS_MAX_Y; |
| 215 |
dev->absfuzz[idx] = 1; |
| 216 |
dev->absflat[idx] = 0; |
| 217 |
|
| 218 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 219 |
dev->absmax[idx]++; |
| 220 |
} |
| 221 |
|
| 222 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_PRESSURE) == -1) |
| 223 |
goto error; |
| 224 |
else |
| 225 |
{ |
| 226 |
int idx = ABS_PRESSURE; |
| 227 |
dev->absmin[idx] = 0; |
| 228 |
dev->absmax[idx] = 0; |
| 229 |
dev->absfuzz[idx] = 0; |
| 230 |
dev->absflat[idx] = 0; |
| 231 |
|
| 232 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 233 |
dev->absmax[idx]++; |
| 234 |
} |
| 235 |
|
| 236 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_MT_TOUCH_MAJOR) == -1) |
| 237 |
goto error; |
| 238 |
else |
| 239 |
{ |
| 240 |
int idx = ABS_MT_TOUCH_MAJOR; |
| 241 |
dev->absmin[idx] = 0; |
| 242 |
dev->absmax[idx] = 255; |
| 243 |
dev->absfuzz[idx] = 1; |
| 244 |
dev->absflat[idx] = 0; |
| 245 |
|
| 246 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 247 |
dev->absmax[idx]++; |
| 248 |
} |
| 249 |
|
| 250 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_MT_WIDTH_MAJOR) == -1) |
| 251 |
goto error; |
| 252 |
else |
| 253 |
{ |
| 254 |
int idx = ABS_MT_WIDTH_MAJOR; |
| 255 |
dev->absmin[idx] = 0; |
| 256 |
dev->absmax[idx] = 255; |
| 257 |
dev->absfuzz[idx] = 1; |
| 258 |
dev->absflat[idx] = 0; |
| 259 |
|
| 260 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 261 |
dev->absmax[idx]++; |
| 262 |
} |
| 263 |
|
| 264 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_MT_POSITION_X) == -1) |
| 265 |
goto error; |
| 266 |
else |
| 267 |
{ |
| 268 |
int idx = ABS_MT_POSITION_X; |
| 269 |
dev->absmin[idx] = 0; |
| 270 |
dev->absmax[idx] = ABS_MAX_X; |
| 271 |
dev->absfuzz[idx] = 1; |
| 272 |
dev->absflat[idx] = 0; |
| 273 |
|
| 274 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 275 |
dev->absmax[idx]++; |
| 276 |
} |
| 277 |
|
| 278 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y) == -1) |
| 279 |
goto error; |
| 280 |
else |
| 281 |
{ |
| 282 |
int idx = ABS_MT_POSITION_Y; |
| 283 |
dev->absmin[idx] = 0; |
| 284 |
dev->absmax[idx] = ABS_MAX_Y; |
| 285 |
dev->absfuzz[idx] = 1; |
| 286 |
dev->absflat[idx] = 0; |
| 287 |
|
| 288 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 289 |
dev->absmax[idx]++; |
| 290 |
} |
| 291 |
|
| 292 |
if (ioctl (fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID) == -1) |
| 293 |
goto error; |
| 294 |
else |
| 295 |
{ |
| 296 |
int idx = ABS_MT_TRACKING_ID; |
| 297 |
dev->absmin[idx] = 0; |
| 298 |
dev->absmax[idx] = 5; |
| 299 |
dev->absfuzz[idx] = 0; |
| 300 |
dev->absflat[idx] = 0; |
| 301 |
|
| 302 |
if (dev->absmin[idx] == dev->absmax[idx]) |
| 303 |
dev->absmax[idx]++; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
return 0; |
| 309 |
error: |
| 310 |
perror ("ioctl failed."); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
static int |
| 315 |
init_uinput (void) |
| 316 |
{ |
| 317 |
struct uinput_user_dev dev; |
| 318 |
|
| 319 |
fd = open ("/dev/uinput", O_RDWR); |
| 320 |
if (fd < 0 && errno == ENODEV) |
| 321 |
fd = open ("/dev/input/uinput", O_RDWR); |
| 322 |
if (fd < 0) |
| 323 |
{ |
| 324 |
if (g_test_verbose ()) |
| 325 |
perror ("open"); |
| 326 |
|
| 327 |
return 0; |
| 328 |
}; |
| 329 |
|
| 330 |
memset (&dev, 0, sizeof (dev)); |
| 331 |
setup (&dev, fd); |
| 332 |
|
| 333 |
if (write (fd, &dev, sizeof (dev)) < sizeof (dev)) |
| 334 |
{ |
| 335 |
if (g_test_verbose ()) |
| 336 |
perror ("write"); |
| 337 |
|
| 338 |
goto error; |
| 339 |
} |
| 340 |
|
| 341 |
if (ioctl (fd, UI_DEV_CREATE, NULL) == -1) |
| 342 |
{ |
| 343 |
if (g_test_verbose ()) |
| 344 |
perror ("ioctl"); |
| 345 |
|
| 346 |
goto error; |
| 347 |
} |
| 348 |
|
| 349 |
return 1; |
| 350 |
|
| 351 |
error: |
| 352 |
if (fd != -1) |
| 353 |
close (fd); |
| 354 |
|
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
#endif /* defined CLUTTER_WINDOWING_X11 && OS_LINUX && HAVE_XINPUT_2_2 */ |
| 359 |
|
| 360 |
static void |
| 361 |
events_touch (void) |
| 362 |
{ |
| 363 |
#if defined CLUTTER_WINDOWING_X11 && OS_LINUX && HAVE_XINPUT_2_2 |
| 364 |
ClutterActor *stage; |
| 365 |
State state; |
| 366 |
|
| 367 |
/* bail out if we could not initialize evdev */ |
| 368 |
if (!init_uinput ()) |
| 369 |
return; |
| 370 |
|
| 371 |
state.pass = TRUE; |
| 372 |
state.gesture_points = 0; |
| 373 |
|
| 374 |
stage = clutter_test_get_stage (); |
| 375 |
g_signal_connect (stage, "event", G_CALLBACK (event_cb), &state); |
| 376 |
clutter_stage_set_fullscreen (CLUTTER_STAGE (stage), TRUE); |
| 377 |
clutter_actor_show (stage); |
| 378 |
|
| 379 |
clutter_threads_add_timeout (500, perform_gesture, &state); |
| 380 |
|
| 381 |
clutter_main (); |
| 382 |
|
| 383 |
if (g_test_verbose ()) |
| 384 |
g_print ("end result: %s\n", state.pass ? "pass" : "FAIL"); |
| 385 |
|
| 386 |
g_assert (state.pass); |
| 387 |
#endif /* defined CLUTTER_WINDOWING_X11 && OS_LINUX && HAVE_XINPUT_2_2 */ |
| 388 |
} |
| 389 |
|
| 390 |
CLUTTER_TEST_SUITE ( |
| 391 |
CLUTTER_TEST_UNIT ("/events/touch", events_touch) |
| 392 |
) |