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