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