Don't clobber speed when reporting an invalid value
[xorg/acecad] / src / acecad.c
1 /*
2  * Copyright (c) 2001 Edouard TISSERANT <tissered@esstin.u-nancy.fr>
3  * Parts inspired from Shane Watts <shane@bofh.asn.au> XFree86 3 Acecad Driver
4  * Thanks to Emily, from AceCad, For giving me documents.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
21  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  *
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <xorg-server.h>
32 #include <xorgVersion.h>
33 #define XORG_VERSION_BOTCHED XORG_VERSION_NUMERIC(1,4,0,0,0)
34 #if XORG_VERSION_CURRENT >= XORG_VERSION_BOTCHED
35 #define XORG_BOTCHED_INPUT 1
36 #else
37 #define XORG_BOTCHED_INPUT 0
38 #endif
39
40 /*****************************************************************************
41  *      Standard Headers
42  ****************************************************************************/
43
44 #ifdef HAVE_LINUX_INPUT_H
45 #include <asm/types.h>
46 #include <linux/input.h>
47 #ifndef EV_SYN
48 #define EV_SYN EV_RST
49 #define SYN_REPORT 0
50 #endif
51 #ifdef BUS_PCI
52 #undef BUS_PCI
53 #endif
54 #ifdef BUS_ISA
55 #undef BUS_ISA
56 #endif
57 #endif
58
59 #include <misc.h>
60 #include <xf86.h>
61 #ifndef NEED_XF86_TYPES
62 #define NEED_XF86_TYPES
63 #endif
64 #include <xf86_OSproc.h>
65 #include <xisb.h>
66 #include <xf86Xinput.h>
67 #include <exevents.h>
68 #include <xf86Module.h>
69
70 #include <string.h>
71 #include <stdio.h>
72
73 #include <errno.h>
74 #ifdef HAVE_LINUX_INPUT_H
75 #include <fcntl.h>
76 #ifdef HAVE_SYSFS_LIBSYSFS_H
77 #include <sysfs/libsysfs.h>
78 #include <dlfcn.h>
79 #endif
80 #endif
81
82 #include <X11/Xatom.h>
83 #include <xserver-properties.h>
84
85 /* Previously found in xf86Xinput.h */
86 #ifdef DBG
87 #undef DBG
88 #endif
89 #define DBG(lvl, f) {if ((lvl) <= xf86GetVerbosity()) f;}
90
91 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
92 #error "XINPUT ABI 12 required."
93 #endif
94
95 /*****************************************************************************
96  *      Local Headers
97  ****************************************************************************/
98 #include "acecad.h"
99
100 /*****************************************************************************
101  *      Variables without includable headers
102  ****************************************************************************/
103
104 /*****************************************************************************
105  *      Local Variables
106  ****************************************************************************/
107
108 /* max number of input events to read in one read call */
109 #define MAX_EVENTS 50
110
111 static char const *default_options[] =
112 {
113         "BaudRate", "9600",
114         "StopBits", "1",
115         "DataBits", "8",
116         "Parity", "Odd",
117         "Vmin", "1",
118         "Vtime", "10",
119         "FlowControl", "Xoff",
120         NULL
121 };
122
123 _X_EXPORT InputDriverRec ACECAD =
124 {
125         1,
126         "acecad",
127         NULL,
128         AceCadPreInit,
129         AceCadUnInit,
130         NULL,
131         default_options
132 };
133
134 static XF86ModuleVersionInfo VersionRec =
135 {
136         "acecad",
137         MODULEVENDORSTRING,
138         MODINFOSTRING1,
139         MODINFOSTRING2,
140         XORG_VERSION_CURRENT,
141         PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
142         ABI_CLASS_XINPUT,
143         ABI_XINPUT_VERSION,
144         MOD_CLASS_XINPUT,
145         {0, 0, 0, 0}
146 };
147
148
149 _X_EXPORT XF86ModuleData acecadModuleData = {
150         &VersionRec,
151         SetupProc,
152         TearDownProc
153 };
154
155 /*****************************************************************************
156  *      Function Definitions
157  ****************************************************************************/
158
159 static pointer
160 SetupProc(      pointer module,
161                 pointer options,
162                 int *errmaj,
163                 int *errmin )
164 {
165         xf86AddInputDriver(&ACECAD, module, 0);
166         return module;
167 }
168
169 static void
170 TearDownProc( pointer p )
171 {
172 }
173
174 #ifdef HAVE_LINUX_INPUT_H
175 static int
176 IsUSBLine(int fd)
177 {
178     int version;
179     int err;
180
181     SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
182
183     if (!err) {
184         xf86MsgVerb(X_PROBED, 4, "Kernel Input driver version is %d.%d.%d\n",
185                 version >> 16, (version >> 8) & 0xff, version & 0xff);
186         return 1;
187     } else {
188         xf86MsgVerb(X_PROBED, 4, "No Kernel Input driver found\n");
189         return 0;
190     }
191 }
192
193 /* Heavily inspired by synaptics/eventcomm.c */
194
195 #define DEV_INPUT_EVENT "/dev/input/event"
196 #define EV_DEV_NAME_MAXLEN 64
197 #define SET_EVENT_NUM(str, num) \
198     snprintf(str, EV_DEV_NAME_MAXLEN, "%s%d", DEV_INPUT_EVENT, num)
199
200 static Bool
201 fd_query_acecad(int fd, const char *ace_name) {
202     char name[256] = "Unknown";
203     int cmp_at = strlen(ace_name);
204     if (cmp_at > 255)
205         cmp_at = 255;
206     ioctl(fd, EVIOCGNAME(sizeof(name)), name);
207     name[cmp_at] = '\0';
208     if (xf86NameCmp(name, ace_name) == 0)
209         return TRUE;
210     return FALSE;
211 }
212
213 static char ace_name_default[7] = "acecad";
214
215 #ifdef HAVE_SYSFS_LIBSYSFS_H
216 static char usb_bus_name[4] = "usb";
217 static char acecad_driver_name[11] = "usb_acecad";
218 #endif
219
220 static Bool
221 AceCadAutoDevProbe(InputInfoPtr local, int verb)
222 {
223     /* We are trying to find the right eventX device */
224     int i = 0;
225     Bool have_evdev = FALSE;
226     int noent_cnt = 0;
227     const int max_skip = 10;
228     const char *ace_name = xf86FindOptionValue(local->options, "Name");
229     char fname[EV_DEV_NAME_MAXLEN];
230     int np;
231
232 #ifdef HAVE_SYSFS_LIBSYSFS_H
233     struct sysfs_bus *usb_bus = NULL;
234     struct sysfs_driver *acecad_driver = NULL;
235     struct sysfs_device *candidate = NULL;
236     char *link = NULL;
237     struct dlist *devs = NULL;
238     struct dlist *links = NULL;
239
240     xf86MsgVerb(X_INFO, verb, "%s: querying sysfs for Acecad tablets\n", local->name);
241     usb_bus = sysfs_open_bus(usb_bus_name);
242     if (usb_bus) {
243         xf86MsgVerb(X_PROBED, 4, "%s: usb bus opened\n", local->name);
244         acecad_driver = sysfs_get_bus_driver(usb_bus, acecad_driver_name);
245         if (acecad_driver) {
246             xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad driver opened\n", local->name);
247             devs = sysfs_get_driver_devices(acecad_driver);
248             if (devs) {
249                 xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad devices retrieved\n", local->name);
250                 dlist_for_each_data(devs, candidate, struct sysfs_device) {
251                     xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s\n", local->name, candidate->name, candidate->path);
252                     links = sysfs_open_link_list(candidate->path);
253                     dlist_for_each_data(links, link, char) {
254                         if (sscanf(link, "input:event%d", &i) == 1) {
255                             xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s: %s\n", local->name, candidate->name, candidate->path, link);
256                             break;
257                         }
258                     }
259                     sysfs_close_list(links);
260                     if (i > 0) /* We found something */
261                         break;
262                 }
263             } else
264                 xf86MsgVerb(X_WARNING, 4, "%s: no usb_acecad devices found\n", local->name);
265         } else
266             xf86MsgVerb(X_WARNING, 4, "%s: usb_acecad driver not found\n", local->name);
267     } else
268         xf86MsgVerb(X_WARNING, 4, "%s: usb bus not found\n", local->name);
269     sysfs_close_bus(usb_bus);
270
271     if (i > 0) {
272         /* We found something */
273         np = SET_EVENT_NUM(fname, i);
274         if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
275             xf86MsgVerb(X_WARNING, verb, "%s: unable to manage event device %d\n", local->name, i);
276         } else {
277             goto ProbeFound;
278         }
279     } else
280         xf86MsgVerb(X_WARNING, verb, "%s: no Acecad devices found via sysfs\n", local->name);
281
282 #endif
283
284     if (!ace_name)
285         ace_name = ace_name_default;
286
287     xf86MsgVerb(X_INFO, verb, "%s: probing event devices for Acecad tablets\n", local->name);
288     for (i = 0; ; i++) {
289         int fd = -1;
290         Bool is_acecad;
291
292         np = SET_EVENT_NUM(fname, i);
293         if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
294             xf86MsgVerb(X_WARNING, verb, "%s: too many devices, giving up %d\n", local->name, i);
295             break;
296         }
297         SYSCALL(fd = open(fname, O_RDONLY));
298         if (fd < 0) {
299             if (errno == ENOENT) {
300                 if (++noent_cnt >= max_skip)
301                     break;
302                 else
303                     continue;
304             } else {
305                 continue;
306             }
307         }
308         noent_cnt = 0;
309         have_evdev = TRUE;
310         is_acecad = fd_query_acecad(fd, ace_name);
311         SYSCALL(close(fd));
312         if (is_acecad) {
313             goto ProbeFound;
314         }
315     }
316     xf86MsgVerb(X_WARNING, verb, "%s: no Acecad event device found (checked %d nodes, no device name started with '%s')\n",
317             local->name, i + 1, ace_name);
318     if (i <= max_skip)
319         xf86MsgVerb(X_WARNING, verb, "%s: The /dev/input/event* device nodes seem to be missing\n",
320                 local->name);
321     if (i > max_skip && !have_evdev)
322         xf86MsgVerb(X_WARNING, verb, "%s: The evdev kernel module seems to be missing\n", local->name);
323     return FALSE;
324
325 ProbeFound:
326     xf86Msg(X_PROBED, "%s auto-dev sets device to %s\n",
327             local->name, fname);
328     xf86ReplaceStrOption(local->options, "Device", fname);
329     return TRUE;
330 }
331
332 #endif
333
334 static int
335 AceCadPreInit(InputDriverPtr drv, InputInfoPtr local, int flags)
336 {
337     AceCadPrivatePtr priv = calloc (1, sizeof(AceCadPrivateRec));
338     int speed;
339     int msgtype;
340     const char *s;
341
342     if (!priv)
343         return BadAlloc;
344
345     memset(priv, 0, sizeof(AceCadPrivateRec));
346     local->private = priv;
347
348     local->device_control = DeviceControl;
349     local->type_name = XI_TABLET;
350
351     priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
352
353     s = xf86FindOptionValue(local->options, "Device");
354     if (!s || (s && (xf86NameCmp(s, "auto-dev") == 0))) {
355 #ifdef HAVE_LINUX_INPUT_H
356         priv->flags |= AUTODEV_FLAG;
357         if (!AceCadAutoDevProbe(local, 0))
358         {
359             xf86Msg(X_ERROR, "%s: unable to find device\n", local->name);
360             goto SetupProc_fail;
361         }
362 #else
363         xf86Msg(X_NOT_IMPLEMENTED, "%s: device autodetection not implemented, sorry\n", local->name);
364         goto SetupProc_fail;
365 #endif
366     }
367
368     local->fd = xf86OpenSerial (local->options);
369     if (local->fd == -1)
370     {
371         xf86Msg(X_ERROR, "%s: unable to open device\n", local->name);
372         goto SetupProc_fail;
373     }
374     xf86ErrorFVerb( 6, "tty port opened successfully\n" );
375
376 #ifdef HAVE_LINUX_INPUT_H
377     if (IsUSBLine(local->fd)) {
378         priv->flags |= USB_FLAG;
379
380         local->read_input = USBReadInput;
381
382         if (USBQueryHardware(local) != Success)
383         {
384             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
385             goto SetupProc_fail;
386         }
387     } else
388 #endif
389     {
390         local->read_input = ReadInput;
391
392         msgtype = X_DEFAULT;
393         if (xf86FindOptionValue(local->options, "ReportSpeed")) {
394             msgtype = X_CONFIG;
395             speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
396         } else {
397             speed = 85;
398         }
399
400         switch (speed)
401         {
402             case 120:
403                 priv->acecadReportSpeed = 'Q';
404                 break;
405             case 85:
406                 priv->acecadReportSpeed = 'R';
407                 break;
408             case 10:
409                 priv->acecadReportSpeed = 'S';
410                 break;
411             case 2:
412                 priv->acecadReportSpeed = 'T';
413                 break;
414             default:
415                 priv->acecadReportSpeed = 'R';
416                 xf86Msg(X_ERROR, "%s: ReportSpeed value %d invalid. Possible values: 120, 85, 10, 2. Defaulting to 85\n", local->name, speed);
417                 speed = 85;
418                 msgtype = X_DEFAULT;
419         }
420
421         xf86Msg(msgtype, "%s report %d points/s\n", local->name, speed);
422
423         priv->buffer = XisbNew (local->fd, 200);
424
425         /*
426          * Verify that hardware is attached and fuctional
427          */
428         if (QueryHardware(priv) != Success)
429         {
430             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
431             goto SetupProc_fail;
432         }
433     }
434
435     s = xf86FindOptionValue(local->options, "Mode");
436     msgtype = s ? X_CONFIG : X_DEFAULT;
437     if (!(s && (xf86NameCmp(s, "relative") == 0)))
438     {
439         priv->flags |= ABSOLUTE_FLAG;
440     }
441
442     xf86Msg(msgtype, "%s is in %s mode\n", local->name, (priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
443     DBG (9, XisbTrace (priv->buffer, 1));
444
445     xf86ProcessCommonOptions(local, local->options);
446
447     if (local->fd != -1)
448     {
449         xf86RemoveEnabledDevice (local);
450         if (priv->buffer)
451         {
452             XisbFree(priv->buffer);
453             priv->buffer = NULL;
454         }
455         xf86CloseSerial(local->fd);
456     }
457     xf86RemoveEnabledDevice (local);
458     local->fd = -1;
459     return Success;
460
461     /*
462      * If something went wrong, cleanup and return NULL
463      */
464 SetupProc_fail:
465     return BadAlloc;
466 }
467
468 static void
469 AceCadUnInit(InputDriverPtr drv, InputInfoPtr local, int flags)
470 {
471     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
472
473     if (local->fd > -1)
474     {
475         xf86CloseSerial (local->fd);
476         local->fd = -1;
477     }
478
479     if (priv) {
480         if (priv->buffer)
481             XisbFree (priv->buffer);
482         free (priv);
483         local->private = NULL;
484     }
485 }
486
487 static Bool
488 DeviceControl (DeviceIntPtr dev, int mode)
489 {
490     Bool RetValue;
491
492     switch (mode)
493     {
494         case DEVICE_INIT:
495             DeviceInit(dev);
496             RetValue = Success;
497             break;
498         case DEVICE_ON:
499             RetValue = DeviceOn(dev);
500             break;
501         case DEVICE_OFF:
502             RetValue = DeviceOff(dev);
503             break;
504         case DEVICE_CLOSE:
505             RetValue = DeviceClose(dev);
506             break;
507         default:
508             RetValue = BadValue;
509     }
510
511     return RetValue;
512 }
513
514 static Bool
515 DeviceOn (DeviceIntPtr dev)
516 {
517     char buffer[256];
518     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
519     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
520
521     xf86MsgVerb(X_INFO, 4, "%s Device On\n", local->name);
522
523     local->fd = xf86OpenSerial(local->options);
524     if (local->fd == -1)
525     {
526         xf86Msg(X_WARNING, "%s: cannot open input device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
527         priv->flags &= ~AVAIL_FLAG;
528 #ifdef HAVE_LINUX_INPUT_H
529         if ((priv->flags & AUTODEV_FLAG) && AceCadAutoDevProbe(local, 4))
530             local->fd = xf86OpenSerial(local->options);
531         if (local->fd == -1)
532 #endif
533             return !Success;
534     }
535     priv->flags |= AVAIL_FLAG;
536
537
538     if (!(priv->flags & USB_FLAG)) {
539         priv->buffer = XisbNew(local->fd, 200);
540         if (!priv->buffer)
541         {
542             xf86CloseSerial(local->fd);
543             local->fd = -1;
544             return !Success;
545         }
546
547         /* Rets qu'a l'envoyer a la tablette */
548         sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed, ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
549         XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
550     }
551
552     xf86FlushInput(local->fd);
553     xf86AddEnabledDevice (local);
554     dev->public.on = TRUE;
555     return Success;
556 }
557
558 static Bool
559 DeviceOff (DeviceIntPtr dev)
560 {
561     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
562     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
563
564     xf86MsgVerb(X_INFO, 4, "%s Device Off\n", local->name);
565
566     if (local->fd != -1)
567     {
568         xf86RemoveEnabledDevice (local);
569         if (priv->buffer)
570         {
571             XisbFree(priv->buffer);
572             priv->buffer = NULL;
573         }
574         xf86CloseSerial(local->fd);
575     }
576
577
578     xf86RemoveEnabledDevice (local);
579     dev->public.on = FALSE;
580     return Success;
581 }
582
583 static Bool
584 DeviceClose (DeviceIntPtr dev)
585 {
586     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
587
588     xf86MsgVerb(X_INFO, 4, "%s Device Close\n", local->name);
589
590     return Success;
591 }
592
593 static void
594 ControlProc(DeviceIntPtr dev, PtrCtrl *ctrl)
595 {
596     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
597
598     xf86MsgVerb(X_INFO, 4, "%s Control Proc\n", local->name);
599 }
600
601 static Bool
602 DeviceInit (DeviceIntPtr dev)
603 {
604     int rx, ry;
605     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
606     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
607     unsigned char map[] = {0, 1, 2, 3};
608     int history_size;
609     Atom btn_labels[3];
610     Atom axes_labels[3];
611
612     btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
613     btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
614     btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
615
616     if ((priv->flags & ABSOLUTE_FLAG))
617     {
618         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
619         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
620         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
621     } else
622     {
623         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
624         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
625         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Z);
626     }
627
628     xf86MsgVerb(X_INFO, 4, "%s Init\n", local->name);
629
630     /* 3 boutons */
631     if (InitButtonClassDeviceStruct (dev, 3,
632                 btn_labels,
633                 map) == FALSE)
634     {
635         xf86Msg(X_ERROR, "%s: unable to allocate ButtonClassDeviceStruct\n", local->name);
636         return !Success;
637     }
638
639     if (InitFocusClassDeviceStruct (dev) == FALSE)
640     {
641         xf86Msg(X_ERROR, "%s: unable to allocate FocusClassDeviceStruct\n", local->name);
642         return !Success;
643     }
644
645     if (InitPtrFeedbackClassDeviceStruct(dev, ControlProc) == FALSE) {
646         xf86Msg(X_ERROR, "%s: unable to init ptr feedback\n", local->name);
647         return !Success;
648     }
649
650     history_size = xf86SetIntOption(local->options , "HistorySize", 0);
651
652     /* 3 axes */
653     if (InitValuatorClassDeviceStruct (dev, 3,
654                 axes_labels,
655                 history_size,
656                 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
657             == FALSE)
658     {
659         xf86Msg(X_ERROR, "%s: unable to allocate ValuatorClassDeviceStruct\n", local->name);
660         return !Success;
661     }
662     else
663     {
664
665         InitValuatorAxisStruct(dev,
666                 0,
667                 axes_labels[0],
668                 0,                      /* min val */
669 #if XORG_BOTCHED_INPUT
670                 screenInfo.screens[0]->width,
671 #else
672                 priv->acecadMaxX,       /* max val */
673 #endif
674                 1000,                   /* resolution */
675                 0,                      /* min_res */
676                 1000                    /* max_res */
677                 , Absolute
678                 );
679         InitValuatorAxisStruct(dev,
680                 1,
681                 axes_labels[1],
682                 0,                      /* min val */
683 #if XORG_BOTCHED_INPUT
684                 screenInfo.screens[0]->height,
685 #else
686                 priv->acecadMaxY,       /* max val */
687 #endif
688                 1000,                   /* resolution */
689                 0,                      /* min_res */
690                 1000                    /* max_res */
691                 , Absolute
692                 );
693         InitValuatorAxisStruct(dev,
694                 2,
695                 axes_labels[2],
696                 0,                      /* min val */
697                 priv->acecadMaxZ,       /* max val */
698                 1000,                   /* resolution */
699                 0,                      /* min_res */
700                 1000            /* max_res */
701                 , Absolute
702                 );
703
704     }
705
706     if (InitProximityClassDeviceStruct (dev) == FALSE)
707     {
708         xf86Msg(X_ERROR, "%s: unable to allocate ProximityClassDeviceStruct\n", local->name);
709         return !Success;
710     }
711
712     xf86MotionHistoryAllocate (local);
713
714
715     /* On ne peut pas calculer l'increment avant, faute d'ecran pour
716        connaitre la taille... */
717
718     if (priv->acecadInc > 95)
719         priv->acecadInc = 95;
720     if (priv->acecadInc < 1)
721     {
722         /* guess the best increment value given video mode */
723         rx = priv->acecadMaxX / screenInfo.screens[0]->width;
724         ry = priv->acecadMaxY / screenInfo.screens[0]->height;
725         if (rx < ry)
726             priv->acecadInc = rx;
727         else
728             priv->acecadInc = ry;
729         if (priv->acecadInc < 1)
730             priv->acecadInc = 1;
731     }
732
733     xf86Msg(X_INFO, "%s Increment: %d\n", local->name, priv->acecadInc);
734
735     return Success;
736 }
737
738 static void
739 ReadInput (InputInfoPtr local)
740 {
741     int x, y, z;
742     int prox, buttons;
743     int is_absolute;
744     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
745
746     /*xf86Msg(X_INFO, "ACECAD Tablet Read Input\n");*/
747
748     is_absolute = (priv->flags & ABSOLUTE_FLAG);
749
750     /*
751      * set blocking to -1 on the first call because we know there is data to
752      * read. Xisb automatically clears it after one successful read so that
753      * succeeding reads are preceeded buy a select with a 0 timeout to prevent
754      * read from blocking indefinately.
755      */
756     XisbBlockDuration (priv->buffer, -1);
757
758     while (AceCadGetPacket (priv) == Success)
759     {
760         x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
761         y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
762
763         if (!(priv->flags & ABSOLUTE_FLAG))
764         {
765             x = priv->packet[0] & XSIGN_BIT? x:-x;
766             y = priv->packet[0] & YSIGN_BIT? y:-y;
767         }
768         else
769         {
770             y = priv->acecadMaxY - y ;
771         }
772
773
774         z = ((int)priv->packet[5] << 2) |
775             (((int)priv->packet[6] & 0x01) << 1) |
776             (((int)priv->packet[6] & 0x10) >> 4);
777
778         buttons = ((int)priv->packet[0] & 0x07) |
779             ((int)priv->packet[6] & 0x02 << 2);
780
781         prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
782
783         if (prox)
784         {
785             if (!(priv->acecadOldProximity))
786             {
787                 /*xf86Msg(X_INFO, "ACECAD Tablet ProxIN %d %d %d\n",x, y, z);*/
788                 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
789             }
790
791             if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
792                     || (!is_absolute && (x || y)))
793             {
794                 if (is_absolute || priv->acecadOldProximity)
795                 {
796                     /*xf86Msg(X_INFO, "ACECAD Tablet Motion %d %d %d\n", x, y, z);*/
797                     xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
798                 }
799             }
800
801             if (priv->acecadOldButtons != buttons)
802             {
803                 int delta = buttons ^ priv->acecadOldButtons;
804                 while (delta)
805                 {
806                     int id = ffs(delta);
807                     delta &= ~(1 << (id-1));
808
809                     /*xf86Msg(X_INFO, "ACECAD Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
810                     xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
811                 }
812             }
813
814             priv->acecadOldButtons = buttons;
815             priv->acecadOldX = x;
816             priv->acecadOldY = y;
817             priv->acecadOldZ = z;
818             priv->acecadOldProximity = prox;
819         }
820         else
821         {
822             if (priv->acecadOldProximity)
823             {
824                 /*xf86Msg(X_INFO, "ACECAD Tablet ProxOUT %d %d %d\n",x, y, z);*/
825                 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
826             }
827             priv->acecadOldProximity = 0;
828         }
829     }
830     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
831 }
832
833 #ifdef HAVE_LINUX_INPUT_H
834 #define set_bit(byte,nb,bit)    (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
835 static void
836 USBReadInput (InputInfoPtr local)
837 {
838     int len;
839     struct input_event * event;
840     char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
841     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
842     int x = priv->acecadOldX;
843     int y = priv->acecadOldY;
844     int z = priv->acecadOldZ;
845     int report_x = 0, report_y = 0;
846     int prox = priv->acecadOldProximity;
847     int buttons = priv->acecadOldButtons;
848     /* Is autodev active? */
849     int autodev = priv->flags & AUTODEV_FLAG;
850     /* Was the device available last time we checked? */
851     int avail = priv->flags & AVAIL_FLAG;
852
853     SYSCALL(len = xf86ReadSerial(local->fd, eventbuf, sizeof(eventbuf)));
854
855     if (len <= 0) {
856         if (avail) {
857             xf86Msg(X_ERROR, "%s: error reading device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
858         }
859         if (NOTAVAIL) {
860             priv->flags &= ~AVAIL_FLAG;
861             if(autodev) {
862                 if (AceCadAutoDevProbe(local, 4)) {
863                     DeviceOff(local->dev);
864                     DeviceOn(local->dev);
865                 }
866             }
867         }
868         return;
869     } else {
870         if (!avail) {
871             /* If the device wasn't available last time we checked */
872             xf86Msg(X_INFO, "%s: device %s is available again\n", local->name, xf86FindOptionValue(local->options, "Device"));
873             priv->flags |= AVAIL_FLAG;
874         }
875     }
876
877     for (event = (struct input_event *)eventbuf;
878             event < (struct input_event *)(eventbuf+len); event++) {
879
880         switch (event->type) {
881             case EV_SYN: /* 2.6.x */
882                 if (event->code != SYN_REPORT)
883                     xf86Msg(X_ERROR, "%s: unknown EV_SYN code %d\n", local->name, event->code);
884                 break;
885             case EV_ABS:
886                 switch (event->code) {
887                     case ABS_X:
888                         x = event->value;
889                         break;
890
891                     case ABS_Y:
892                         y = event->value;
893                         break;
894
895                     case ABS_PRESSURE:
896                         z = event->value;
897                         break;
898
899                     case ABS_MISC:
900                         break;
901
902                 }
903                 break; /* EV_ABS */
904
905             case EV_KEY:
906                 switch (event->code) {
907                     case BTN_TOOL_PEN:
908                         prox = event->value;
909                         break;
910
911                     case BTN_TOUCH:
912                         buttons = set_bit(buttons,0,event->value);
913                         break;
914
915                     case BTN_STYLUS:
916                         buttons = set_bit(buttons,1,event->value);
917                         break;
918
919                     case BTN_STYLUS2:
920                         buttons = set_bit(buttons,2,event->value);
921                         break;
922                 }
923                 break; /* EV_KEY */
924             default:
925                 xf86Msg(X_ERROR, "%s: unknown event type/code %d/%d\n", local->name, event->type, event->code);
926         } /* switch event->type */
927
928         /* Linux Kernel 2.6.x sends EV_SYN/SYN_REPORT as an event terminator,
929          * whereas 2.4.x sends EV_ABS/ABS_MISC. We have to support both.
930          */
931         if (!(  (event->type == EV_SYN && event->code == SYN_REPORT) ||
932                     (event->type == EV_ABS && event->code == ABS_MISC)
933              )) {
934             continue;
935         }
936
937         if (prox)
938         {
939 #if XORG_BOTCHED_INPUT
940             ConvertProc(local, 0, 3, x, y, 0, 0, 0, 0, &report_x, &report_y);
941 #else
942             report_x = x;
943             report_y = y;
944 #endif
945             if (!(priv->acecadOldProximity))
946             {
947                 xf86PostProximityEvent(local->dev, 1, 0, 3 , report_x, report_y, z);
948             }
949
950
951             xf86PostMotionEvent(local->dev, 1, 0, 3, report_x, report_y, z);
952
953             if (priv->acecadOldButtons != buttons)
954             {
955                 int delta = buttons ^ priv->acecadOldButtons;
956                 while (delta)
957                 {
958                     int id = ffs(delta);
959                     delta &= ~(1 << (id-1));
960
961                     xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, report_x, report_y, z);
962                 }
963             }
964         }
965         else
966         {
967             if (priv->acecadOldProximity)
968             {
969                 xf86PostProximityEvent(local->dev, 0, 0, 3, report_x, report_y, z);
970             }
971             priv->acecadOldProximity = 0;
972         }
973
974         priv->acecadOldButtons = buttons;
975         priv->acecadOldX = x;
976         priv->acecadOldY = y;
977         priv->acecadOldZ = z;
978         priv->acecadOldProximity = prox;
979     }
980     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
981 }
982 #endif
983
984 /*
985  * The ConvertProc function may need to be tailored for your device.
986  * This function converts the device's valuator outputs to x and y coordinates
987  * to simulate mouse events.
988  */
989 static Bool
990 ConvertProc (InputInfoPtr local, int first, int num,
991         int v0, int v1, int v2, int v3, int v4, int v5,
992         int *x, int *y)
993 {
994     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
995
996     /* TODO: should have a structure to hold which screen the
997      * pointer is attached to? */
998     // xf86Msg(X_INFO, "%s: coordinate conversion in : %d, %d\n", local->name, v0, v1);
999     *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
1000     *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
1001     // xf86Msg(X_INFO, "%s: coordinate conversion out: %d, %d\n", local->name, *x, *y);
1002     return TRUE;
1003 }
1004
1005
1006 #define WriteString(str)\
1007     XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
1008
1009
1010 static Bool
1011 QueryHardware (AceCadPrivatePtr priv)
1012 {
1013
1014     /* Reset */
1015     WriteString("z0");
1016
1017     /* Wait */
1018     milisleep (250);
1019
1020     /* Prompt Mode in order to not be disturbed */
1021     WriteString(ACECAD_PROMPT_MODE);
1022
1023     /* Flush */
1024     while (XisbRead(priv->buffer) >= 0);
1025
1026     /* Ask for Config packet*/
1027     WriteString(ACECAD_CONFIG);
1028
1029     /* Read the packet */
1030     XisbBlockDuration (priv->buffer, 1000000);
1031     NewPacket (priv);
1032
1033     /*xf86Msg(X_CONFIG, "ACECAD Tablet init envoyé \n");*/
1034
1035     if ((AceCadGetPacket (priv) == Success))
1036     {
1037         priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
1038         priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
1039         priv->acecadMaxZ = 512;
1040         xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d\n", priv->acecadMaxX, priv->acecadMaxY);
1041     }
1042     else
1043         return !Success;
1044
1045     /*xf86Msg(X_INFO, "ACECAD Tablet query hardware fini \n");*/
1046     return Success;
1047 }
1048
1049 #define BITS_PER_LONG (sizeof(long) * 8)
1050 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
1051 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
1052 #define OFF(x)  ((x)%BITS_PER_LONG)
1053 #define LONG(x) ((x)/BITS_PER_LONG)
1054
1055 #ifdef HAVE_LINUX_INPUT_H
1056 static Bool
1057 USBQueryHardware (InputInfoPtr local)
1058 {
1059     AceCadPrivatePtr    priv = (AceCadPrivatePtr) local->private;
1060     unsigned long       bit[EV_MAX][NBITS(KEY_MAX)];
1061     int                 i, j;
1062     int                 abs[5];
1063     char                name[256] = "Unknown";
1064
1065     ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
1066     xf86MsgVerb(X_PROBED, 4, "Kernel Input device name: \"%s\"\n", name);
1067
1068     memset(bit, 0, sizeof(bit));
1069     ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
1070
1071     for (i = 0; i < EV_MAX; i++)
1072         if (test_bit(i, bit[0])) {
1073             ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
1074             for (j = 0; j < KEY_MAX; j++)
1075                 if (test_bit(j, bit[i])) {
1076                     if (i == EV_ABS) {
1077                         ioctl(local->fd, EVIOCGABS(j), abs);
1078                         switch (j) {
1079                             case ABS_X:
1080                                 priv->acecadMaxX = abs[2];
1081                                 break;
1082
1083                             case ABS_Y:
1084                                 priv->acecadMaxY = abs[2];
1085                                 break;
1086
1087                             case ABS_PRESSURE:
1088                                 priv->acecadMaxZ = abs[2];
1089                                 break;
1090                         }
1091                     }
1092                 }
1093         }
1094
1095     xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY, priv->acecadMaxZ);
1096     return Success;
1097 }
1098 #endif
1099
1100 static void
1101 NewPacket (AceCadPrivatePtr priv)
1102 {
1103     priv->packeti = 0;
1104 }
1105
1106 static Bool
1107 AceCadGetPacket (AceCadPrivatePtr priv)
1108 {
1109     int count = 0;
1110     int c = 0;
1111
1112     while((c = XisbRead(priv->buffer)) >= 0 )
1113     {
1114
1115         /*
1116          * fail after 500 bytes so the server doesn't hang forever if a
1117          * device sends bad data.
1118          */
1119         if (count++ > 500)
1120         {
1121             NewPacket (priv);
1122             return !Success;
1123         }
1124
1125         if (c & PHASING_BIT)
1126         {
1127             NewPacket(priv);
1128
1129             /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
1130             XisbBlockDuration (priv->buffer, 10000);
1131             priv->packet[priv->packeti++] = c;
1132             count = ACECAD_PACKET_SIZE - 1;
1133             while (count-- && (c = XisbRead(priv->buffer)) >= 0)
1134             {
1135                 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
1136                 priv->packet[priv->packeti++] = c;
1137             }
1138             XisbBlockDuration (priv->buffer, 0);
1139             if(c > 0)
1140                 return Success;
1141         }
1142     }
1143     return !Success;
1144 }