Debian Codegraph @

gdraw/gdrawwacomdriver.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 /*
31 This file exists because the XFree wacom_drv.o does not work for me.
32 When properly configured it sends no device events. It sends no core events.
33
34 However thanks to John E. Joganic wacdump program I know what the event
35 stream on /dev/input/event0 looks like
36 http://linux.joganic.com/wacom/
37 Some docs are at: http://www.frogmouth.net/hid-doco/x286.html
38
39 To the best of my knowledge this is entirely linux specific and won't work
40 anywhere else.
41
42 This file then, attempts to generate the correct device events and pass them
43 on to my windows
44
45 The following options configure it:
46 _WACOM_DRV_BROKEN if defined then enable this driver.
47 _WACOM_SILENT Don't print anything if we can't open the device
48 _WACOM_SEND_EVENTS Send (core-like) events even to windows that aren't ours
49 Other windows can detect that these aren't "real" events
50 so this often doesn't work, and we can't get the state field
51 right.
52 _WACOM_EXTRA_TOP
53 _WACOM_EXTRA_BOTTOM
54 _WACOM_EXTRA_LEFT
55 _WACOM_EXTRA_RIGHT
56 My tablet has a rectangle drawn on it, but even outside this
57 rectangle the tablet is active. These bounds allow you to
58 scale just this rectangle to the screen. If you set them all
59 to 0 the entire table (even the bits outside the rectangle)
60 is scaled to the screen.
61 */
62
63 #ifndef _WACOM_DRV_BROKEN
64 static void *a_file_must_define_something = (void *) &a_file_must_define_something;
65 #else
66
67 # ifndef _WACOM_EXTRA_TOP
68 # define _WACOM_EXTRA_TOP 1120 /* About this many lines above the rectangle */
69 # define _WACOM_EXTRA_LEFT 0 /* No columns to the left of the rectangle */
70 # define _WACOM_EXTRA_BOTTOM (-240) /* Actually the ymax value reported by the driver is less than what I get at the edge of the rectangle, but the tablet will generate values greater than this maximum */
71 # define _WACOM_EXTRA_RIGHT 0
72 # endif
73
74 # include "gxdrawP.h"
75
76 # include <X11/Xatom.h>
77
78 # include <stdio.h>
79 # include <stdlib.h>
80 # include <string.h>
81 # include <unistd.h>
82
83 # include <linux/input.h>
84 # include <sys/ioctl.h>
85
86 # include <errno.h>
87 # include <fcntl.h>
88 # include <sys/stat.h>
89 # include <sys/time.h>
90 # include <sys/types.h>
91
92 # include <math.h>
93
94 typedef struct wacom_state {
95 int sizex, sizey;
96 double scale;
97 int lastx, lasty;
98 int screenx, screeny;
99 int last_pressure;
100 int max_pressure;
101 int last_distance;
102 int last_xtilt;
103 int last_ytilt;
104 int last_wheel; /* always seems to be 0? */
105 int mouse_button_state;
106 int stylus_button_state;
107 int eraser_button_state;
108 enum tools { at_mouse=2, at_stylus=4, at_eraser=8 } active_tool;
109
110 struct timeval time_off; /* offset from gettimeofday to X server timestamp */
111 int win_x, win_y;
112 int x_state;
113 Window xw;
114 GXWindow gw, grabbed;
115 } WState;
116
117 static void Wacom_FigureTimeOffset(GXDisplay *gdisp) {
118 /* Figure the offset between real time and server timestamps */
119 /* First I go through a lot of rigamaroul to get an event with a time */
120 /* stamp from the server */
121 struct timeval tv, server_tv;
122 WState *ws = gdisp->wacom_state;
123 XSetWindowAttributes attrs;
124 XEvent e;
125 char prop_data[4];
126
127 attrs.event_mask = PropertyChangeMask;
128 XChangeWindowAttributes(gdisp->display,gdisp->root,CWEventMask,&attrs);
129 XChangeProperty(gdisp->display,gdisp->root,XA_STRING,XA_STRING,8,PropModeReplace,
130 prop_data,sizeof(prop_data));
131 XWindowEvent(gdisp->display,gdisp->root,PropertyChangeMask,&e);
132
133 gettimeofday(&tv,NULL);
134 server_tv.tv_sec = e.xproperty.time/1000;
135 server_tv.tv_usec = (e.xproperty.time%1000)*1000;
136 ws->time_off.tv_usec = tv.tv_usec-server_tv.tv_usec;
137 ws->time_off.tv_sec = tv.tv_sec-server_tv.tv_sec;
138 if ( ws->time_off.tv_usec<0 ) {
139 ws->time_off.tv_usec += 1000000;
140 --ws->time_off.tv_sec;
141 }
142
143 attrs.event_mask = 0;
144 XChangeWindowAttributes(gdisp->display,gdisp->root,CWEventMask,&attrs);
145 }
146
147 void _GXDraw_Wacom_Init(GXDisplay *gdisp) {
148 WState *ws;
149 /* Some of this code is stolen from xf86Wacom.c, the XFree driver for wacom */
150 char name[256];
151 uint8_t bit[2][(KEY_MAX-1)/8];
152 int abs[5];
153 int j;
154 /* End stolen section */
155 double xscale, yscale;
156
157 gdisp->wacom_fd = open("/dev/input/event0",O_RDONLY);
158 if ( gdisp->wacom_fd==-1 ) {
159 # ifndef _WACOM_SILENT
160 if ( errno==EACCES )
161 fprintf( stderr,
162 "/dev/input/event0 is protected so you cannot read from it\n"
163 "If you wish PfaEdit to process wacom events directly you must:\n"
164 "\t$ su\n"
165 "\t# chmod 644 /dev/input/event0\n" );
166 else if ( errno==ENODEV )
167 fprintf( stderr,
168 "The system claims no device is associated with /dev/input/event0.\n"
169 "This is probably because the wacom driver isn't properly initialized on\n"
170 "your system. Try the following:\n\t$ su\n"
171 "\t# modprobe wacom.o\n"
172 "\t# modprobe evdev\n"
173 "You may want to alter your /etc/rc.d/rc.local file to do this automagically.\n" );
174 else if ( errno==ENOENT )
175 fprintf( stderr,
176 "The file /dev/input/event0 does not exist on your system. This means that\n"
177 "your machine is not configured to handle the type of event processing used\n"
178 "by the current wacom driver. All I can suggest is that you upgrade to a newer\n"
179 "version of the OS" );
180 else
181 perror("Failed to open /dev/input/event0");
182 #endif
183 return;
184 }
185 fcntl(gdisp->wacom_fd,F_SETFL,O_NONBLOCK);
186
187 ws = gdisp->wacom_state = calloc(1,sizeof(WState));
188
189 ws->screenx = -1;
190 ws->screeny = -1;
191
192 /* See http://www.frogmouth.net/hid-doco/x286.html for docs */
193 ioctl(gdisp->wacom_fd, EVIOCGNAME(sizeof(name)), name);
194 fprintf( stderr, "GDraw Wacom Driver opened device %s\n", name);
195
196 memset(bit, 0, sizeof(bit));
197 ioctl(gdisp->wacom_fd, EVIOCGBIT(0, EV_MAX), bit[0]);
198
199 if (bit[0][EV_ABS/8] & (1<<(EV_ABS%8)) ) {
200 ioctl(gdisp->wacom_fd, EVIOCGBIT(EV_ABS, KEY_MAX), bit[1]);
201 for (j = 0; j < ABS_MAX; j++) if (bit[1][j/8] & (1<<(j%8))) {
202 ioctl(gdisp->wacom_fd, EVIOCGABS(j), abs);
203 /* abs contains current-val, min-val, max-val ?flat? ?fuzz? */
204 /* get initial state variables that have values. Also their bounds, if relevant */
205 switch (j) {
206 case ABS_X:
207 ws->sizex = abs[2];
208 ws->lastx = abs[0];
209 break;
210 case ABS_Y:
211 ws->sizey = abs[2];
212 ws->lasty = abs[0];
213 break;
214 case ABS_WHEEL:
215 ws->last_wheel = abs[0];
216 break;
217 case ABS_PRESSURE:
218 ws->max_pressure = abs[2];
219 ws->last_pressure = abs[0];
220 break;
221 case ABS_DISTANCE:
222 ws->last_distance = abs[0];
223 break;
224 case ABS_TILT_X:
225 ws->last_xtilt = abs[0];
226 break;
227 case ABS_TILT_Y:
228 ws->last_ytilt = abs[0];
229 break;
230 }
231 }
232 }
233
234 fprintf( stderr, "Wacom XMax=%d YMax=%d PressureMax=%d\n",
235 ws->sizex, ws->sizey, ws->max_pressure );
236 ws->sizex -= _WACOM_EXTRA_LEFT+_WACOM_EXTRA_RIGHT;
237 ws->sizey -= _WACOM_EXTRA_TOP+_WACOM_EXTRA_BOTTOM;
238 if ( ws->sizex<=0 )
239 xscale = 1;
240 else
241 xscale = gdisp->groot->pos.width / (double) ws->sizex;
242 if ( ws->sizey<=0 )
243 yscale = xscale;
244 else {
245 yscale = gdisp->groot->pos.height / (double) ws->sizey;
246 if ( ws->sizex==0 )
247 xscale = yscale;
248 }
249 if ( xscale>yscale )
250 yscale = xscale;
251 ws->scale = yscale;
252
253 if ( ws->lastx!=0 )
254 ws->screenx = rint((ws->lastx-_WACOM_EXTRA_LEFT)*ws->scale);
255 if ( ws->lasty!=0 )
256 ws->screeny = rint((ws->lasty-_WACOM_EXTRA_TOP)*ws->scale);
257
258 Wacom_FigureTimeOffset(gdisp);
259 }
260
261 # ifdef _WACOM_SEND_EVENTS
262 static void Wacom_SendCoreEvent(GXDisplay *gdisp,int button,int type) {
263 WState *ws = gdisp->wacom_state;
264 XEvent e;
265 struct timeval tv;
266
267 /* We don't need to send MotionNotify events because WarpPointer does */
268 /* that for us (it won't get the state as we might like it, but there's */
269 /* nothing I can do about that) */
270 if ( ws->xw==None )
271 return;
272
273 memset(&e,0,sizeof(e));
274 e.type = type==et_mousedown ? ButtonPress : ButtonRelease;
275 e.xbutton.display = gdisp->display;
276 e.xbutton.window = ws->xw;
277 e.xbutton.root = gdisp->root;
278 e.xbutton.subwindow = None;
279 e.xbutton.x = ws->win_x;
280 e.xbutton.y = ws->win_y;
281 e.xbutton.x_root = ws->screenx;
282 e.xbutton.y_root = ws->screeny;
283 e.xbutton.state = ws->x_state;
284 e.xbutton.same_screen = true;
285
286 gettimeofday(&tv,NULL);
287 tv.tv_sec -= ws->time_off.tv_sec;
288 tv.tv_usec -= ws->time_off.tv_usec;
289 if ( tv.tv_usec<0 ) {
290 tv.tv_usec += 1000000;
291 --tv.tv_sec;
292 }
293 e.xbutton.time = tv.tv_sec*1000 + tv.tv_usec/1000;
294 XSendEvent(gdisp->display,e.xbutton.window,true,
295 type==et_mousedown ? ButtonPressMask : ButtonReleaseMask,
296 &e );
297 }
298 # endif
299 static void Wacom_SendEvent(GXDisplay *gdisp,char *device,int button,int type) {
300 WState *ws = gdisp->wacom_state;
301 GEvent e;
302 struct timeval tv;
303 Window junk;
304
305 e.type = type;
306 if ( ws->grabbed!=NULL && ws->grabbed!=ws->gw ) {
307 XTranslateCoordinates(gdisp->display,ws->gw->w,ws->grabbed->w,
308 ws->win_x,ws->win_y,
309 &ws->win_x,&ws->win_y,&junk);
310 ws->gw = ws->grabbed;
311 ws->xw = ws->grabbed->w;
312 }
313 if ( ws->gw==NULL ) { /* Not one of our windows */
314 # ifdef _WACOM_SEND_EVENTS
315 if ( type==et_mouseup || type==et_mousedown )
316 Wacom_SendCoreEvent(gdisp,button,type);
317 # endif
318 return;
319 }
320 e.w = (GWindow) (ws->gw);
321 if ( type==et_mousedown )
322 ws->grabbed = ws->gw;
323 else if ( type==et_mouseup )
324 ws->grabbed = NULL;
325
326 gettimeofday(&tv,NULL);
327 tv.tv_sec -= ws->time_off.tv_sec;
328 tv.tv_usec -= ws->time_off.tv_usec;
329 if ( tv.tv_usec<0 ) {
330 tv.tv_usec += 1000000;
331 --tv.tv_sec;
332 }
333 e.u.mouse.time = tv.tv_sec*1000 + tv.tv_usec/1000;
334
335 e.u.mouse.x = ws->win_x;
336 e.u.mouse.y = ws->win_y;
337 e.u.mouse.state = ws->active_tool == at_mouse ? ws->mouse_button_state :
338 ws->active_tool == at_stylus ? ws->stylus_button_state :
339 ws->eraser_button_state;
340 e.u.mouse.state |= (ws->x_state&0xff);
341 e.u.mouse.device = device;
342 e.u.mouse.button = button;
343
344 if ( type==et_mousedown ) {
345 int diff, temp;
346 if (( diff = ws->win_x-gdisp->bs.release_x )<0 ) diff= -diff;
347 if (( temp = ws->win_y-gdisp->bs.release_y )<0 ) temp= -temp;
348 if ( diff+temp<gdisp->bs.double_wiggle &&
349 ws->xw == gdisp->bs.release_w &&
350 button == gdisp->bs.release_button &&
351 e.u.mouse.time-gdisp->bs.release_time < gdisp->bs.double_time &&
352 e.u.mouse.time >= gdisp->bs.release_time ) /* Time can wrap */
353 ++ gdisp->bs.cur_click;
354 else
355 gdisp->bs.cur_click = 1;
356 e.u.mouse.clicks = gdisp->bs.cur_click;
357 } else if ( type==et_mouseup ) {
358 gdisp->bs.release_time = e.u.mouse.time;
359 gdisp->bs.release_w = ws->xw;
360 gdisp->bs.release_x = ws->win_x;
361 gdisp->bs.release_y = ws->win_y;
362 gdisp->bs.release_button = button;
363 e.u.mouse.clicks = gdisp->bs.cur_click;
364 }
365
366 e.u.mouse.pressure = ws->last_pressure;
367 e.u.mouse.xtilt = ws->last_xtilt;
368 e.u.mouse.ytilt = ws->last_ytilt;
369 e.u.mouse.separation = ws->last_distance;
370
371 GDrawPostEvent(&e);
372 }
373
374 static Bool Wacom_FindMotion(Display *d,XEvent *e,char *arg) {
375 WState *ws = (WState *) arg;
376
377 return( e->type==MotionNotify &&
378 e->xmotion.x_root==ws->screenx &&
379 e->xmotion.y_root==ws->screeny );
380 }
381
382 static void Wacom_MovePointer(GXDisplay *gdisp) {
383 WState *ws = gdisp->wacom_state;
384 XEvent e;
385 void *ret;
386
387 /* Old docs say that XWarpPointer generates events just as if the user */
388 /* moved the mouse. New docs do not say this, but there does seem to */
389 /* be at least one MotionNotify event (we'll only get it for our own */
390 /* windows, of course */
391 XWarpPointer(gdisp->display,None,gdisp->root,0,0,0,0,ws->screenx,ws->screeny);
392
393 ws->gw = NULL;
394 ws->xw = None;
395 XSync(gdisp->display,false);
396 if ( XCheckIfEvent(gdisp->display,&e,Wacom_FindMotion,(char *)ws)) {
397 ws->xw = e.xany.window;
398 if ( XFindContext(gdisp->display,e.xany.window,gdisp->mycontext,(void *) &ret)==0 )
399 ws->gw = (GXWindow) ret;
400 if ( ws->gw!=NULL && _GXDraw_WindowOrParentsDying((GXWindow) ws->gw))
401 ws->gw = NULL;
402 ws->win_x = e.xmotion.x;
403 ws->win_y = e.xmotion.y;
404 ws->x_state = e.xmotion.state;
405 }
406 # ifdef _WACOM_SEND_EVENTS
407 if ( ws->xw==None ) {
408 int x,y,junk;
409 unsigned int state;
410 Window child, cur, root;
411
412 cur = gdisp->root;
413 x = ws->screenx; y = ws->screeny;
414 for (;;) {
415 XQueryPointer(gdisp->display,cur,&root,&child,&junk,&junk,&x,&y,&state);
416 if ( child==None )
417 break;
418 cur = child;
419 }
420
421 ws->xw = cur;
422 ws->win_x = x;
423 ws->win_y = y;
424 ws->x_state = state;
425 }
426 # endif
427 }
428
429 static void Wacom_TestMotion(GXDisplay *gdisp) {
430 int x, y;
431 WState *ws = gdisp->wacom_state;
432 char *device;
433
434 x = rint((ws->lastx-_WACOM_EXTRA_LEFT)*ws->scale);
435 y = rint((ws->lasty-_WACOM_EXTRA_TOP)*ws->scale);
436 if ( x==ws->screenx && y==ws->screeny )
437 return;
438
439 /* Don't move cursor during init process */
440 if ( ws->screenx==-1 || ws->screeny==-1 ) {
441 if ( x!=0 )
442 ws->screenx = x;
443 if ( y!=0 )
444 ws->screeny = y;
445 return;
446 }
447
448 ws->screenx = x; ws->screeny = y;
449 if ( ws->active_tool==0 ) {
450 fprintf( stderr, "Warning: Don't know what the current tool is on the wacom tablet.\n Assuming Mouse. If that is wrong lift the tool up off the palette\n and replace it.\n" );
451 ws->active_tool = at_mouse;
452 }
453 device = ws->active_tool==at_mouse ? "Mouse1" :
454 ws->active_tool==at_stylus ? "stylus" :
455 "eraser" ;
456 Wacom_MovePointer(gdisp);
457 Wacom_SendEvent(gdisp,device,0,et_mousemove);
458 }
459
460 static void Wacom_ProcessEvent(GXDisplay *gdisp,WState *ws,struct input_event *ev) {
461
462 switch ( ev->type ) {
463 case EV_MSC:
464 /* I don't care about the serial number */
465 break;
466 case EV_KEY:
467 if ( ev->code == BTN_LEFT ) {
468 if ( ev->value ) ws->mouse_button_state |= 0x100;
469 else ws->mouse_button_state &= ~0x100;
470 Wacom_SendEvent(gdisp,"Mouse1",1,ev->value?et_mousedown:et_mouseup);
471 } else if ( ev->code == BTN_RIGHT ) {
472 if ( ev->value ) ws->mouse_button_state |= 0x400;
473 else ws->mouse_button_state &= ~0x400;
474 Wacom_SendEvent(gdisp,"Mouse1",3,ev->value?et_mousedown:et_mouseup);
475 } else if ( ev->code == BTN_MIDDLE ) {
476 if ( ev->value ) ws->mouse_button_state |= 0x200;
477 else ws->mouse_button_state &= ~0x200;
478 Wacom_SendEvent(gdisp,"Mouse1",2,ev->value?et_mousedown:et_mouseup);
479 } else if ( ev->code == BTN_TOUCH ) {
480 if ( ws->active_tool == at_stylus ) {
481 if ( ev->value ) ws->stylus_button_state |= 0x100;
482 else ws->stylus_button_state &= ~0x100;
483 Wacom_SendEvent(gdisp,"stylus",1,ev->value?et_mousedown:et_mouseup);
484 } else {
485 if ( ev->value ) ws->eraser_button_state |= 0x100;
486 else ws->eraser_button_state &= ~0x100;
487 Wacom_SendEvent(gdisp,"eraser",1,ev->value?et_mousedown:et_mouseup);
488 }
489 } else if ( ev->code==BTN_STYLUS2 ) {
490 /* Button on the pen was pushed */
491 if ( ev->value ) ws->stylus_button_state |= 0x200;
492 else ws->stylus_button_state &= ~0x200;
493 Wacom_SendEvent(gdisp,"stylus",2,ev->value?et_mousedown:et_mouseup);
494 } else if ( ev->code == BTN_TOOL_PEN && ev->value ) {
495 ws->active_tool = at_stylus;
496 } else if ( ev->code == BTN_TOOL_RUBBER && ev->value ) {
497 ws->active_tool = at_eraser;
498 } else if ( ev->code == BTN_TOOL_MOUSE && ev->value ) {
499 ws->active_tool = at_mouse;
500 }
501 break;
502 case EV_ABS:
503 if ( ev->code==ABS_X ) {
504 ws->lastx = ev->value;
505 Wacom_TestMotion(gdisp);
506 } else if ( ev->code==ABS_Y ) {
507 ws->lasty = ev->value;
508 Wacom_TestMotion(gdisp);
509 } else if ( ev->code==ABS_WHEEL )
510 ws->last_wheel = ev->value;
511 else if ( ev->code==ABS_PRESSURE )
512 ws->last_pressure = ev->value;
513 else if ( ev->code==ABS_DISTANCE )
514 ws->last_distance = ev->value;
515 else if ( ev->code==ABS_TILT_X )
516 ws->last_xtilt = ev->value;
517 else if ( ev->code==ABS_TILT_Y )
518 ws->last_ytilt = ev->value;
519 break;
520 }
521 }
522
523 void _GXDraw_Wacom_TestEvents(GXDisplay *gdisp) {
524 struct input_event ev[EV_MAX];
525 int i,len;
526
527 while ( (len=read(gdisp->wacom_fd,ev,sizeof(ev)))>0 ) {
528 len /= sizeof(ev[0]);
529 for ( i=0; i<len; ++i )
530 Wacom_ProcessEvent(gdisp,gdisp->wacom_state,&ev[i]);
531 }
532 }
533 #endif