Use a local variable for history_size.
[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 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
83 #include <X11/Xatom.h>
84 #include <xserver-properties.h>
85 #endif
86
87 /* Previously found in xf86Xinput.h */
88 #ifdef DBG
89 #undef DBG
90 #endif
91 #define DBG(lvl, f) {if ((lvl) <= xf86GetVerbosity()) f;}
92
93 /*****************************************************************************
94  *      Local Headers
95  ****************************************************************************/
96 #include "acecad.h"
97
98 /*****************************************************************************
99  *      Variables without includable headers
100  ****************************************************************************/
101
102 /*****************************************************************************
103  *      Local Variables
104  ****************************************************************************/
105
106 /* max number of input events to read in one read call */
107 #define MAX_EVENTS 50
108
109 static const char *default_options[] =
110 {
111         "BaudRate", "9600",
112         "StopBits", "1",
113         "DataBits", "8",
114         "Parity", "Odd",
115         "Vmin", "1",
116         "Vtime", "10",
117         "FlowControl", "Xoff",
118         NULL
119 };
120
121 _X_EXPORT InputDriverRec ACECAD =
122 {
123         1,
124         "acecad",
125         NULL,
126         AceCadPreInit,
127         NULL,
128         NULL
129 };
130
131 static XF86ModuleVersionInfo VersionRec =
132 {
133         "acecad",
134         MODULEVENDORSTRING,
135         MODINFOSTRING1,
136         MODINFOSTRING2,
137         XORG_VERSION_CURRENT,
138         PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
139         ABI_CLASS_XINPUT,
140         ABI_XINPUT_VERSION,
141         MOD_CLASS_XINPUT,
142         {0, 0, 0, 0}
143 };
144
145
146 _X_EXPORT XF86ModuleData acecadModuleData = {
147         &VersionRec,
148         SetupProc,
149         TearDownProc
150 };
151
152 /*****************************************************************************
153  *      Function Definitions
154  ****************************************************************************/
155
156 static pointer
157 SetupProc(      pointer module,
158                 pointer options,
159                 int *errmaj,
160                 int *errmin )
161 {
162         xf86AddInputDriver(&ACECAD, module, 0);
163         return module;
164 }
165
166 static void
167 TearDownProc( pointer p )
168 {
169 }
170
171 #ifdef HAVE_LINUX_INPUT_H
172 static int
173 IsUSBLine(int fd)
174 {
175     int version;
176     int err;
177
178     SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
179
180     if (!err) {
181         xf86MsgVerb(X_PROBED, 4, "Kernel Input driver version is %d.%d.%d\n",
182                 version >> 16, (version >> 8) & 0xff, version & 0xff);
183         return 1;
184     } else {
185         xf86MsgVerb(X_PROBED, 4, "No Kernel Input driver found\n");
186         return 0;
187     }
188 }
189
190 /* Heavily inspired by synaptics/eventcomm.c */
191
192 #define DEV_INPUT_EVENT "/dev/input/event"
193 #define EV_DEV_NAME_MAXLEN 64
194 #define SET_EVENT_NUM(str, num) \
195     snprintf(str, EV_DEV_NAME_MAXLEN, "%s%d", DEV_INPUT_EVENT, num)
196
197 static Bool
198 fd_query_acecad(int fd, char *ace_name) {
199     char name[256] = "Unknown";
200     int cmp_at = strlen(ace_name);
201     if (cmp_at > 255)
202         cmp_at = 255;
203     ioctl(fd, EVIOCGNAME(sizeof(name)), name);
204     name[cmp_at] = '\0';
205     if (xf86NameCmp(name, ace_name) == 0)
206         return TRUE;
207     return FALSE;
208 }
209
210 static char ace_name_default[7] = "acecad";
211
212 #ifdef HAVE_SYSFS_LIBSYSFS_H
213 static char usb_bus_name[4] = "usb";
214 static char acecad_driver_name[11] = "usb_acecad";
215 #endif
216
217 static Bool
218 AceCadAutoDevProbe(InputInfoPtr local, int verb)
219 {
220     /* We are trying to find the right eventX device */
221     int i = 0;
222     Bool have_evdev = FALSE;
223     int noent_cnt = 0;
224     const int max_skip = 10;
225     char *ace_name = xf86FindOptionValue(local->options, "Name");
226     char fname[EV_DEV_NAME_MAXLEN];
227     int np;
228
229 #ifdef HAVE_SYSFS_LIBSYSFS_H
230     struct sysfs_bus *usb_bus = NULL;
231     struct sysfs_driver *acecad_driver = NULL;
232     struct sysfs_device *candidate = NULL;
233     char *link = NULL;
234     struct dlist *devs = NULL;
235     struct dlist *links = NULL;
236
237     xf86MsgVerb(X_INFO, verb, "%s: querying sysfs for Acecad tablets\n", local->name);
238     usb_bus = sysfs_open_bus(usb_bus_name);
239     if (usb_bus) {
240         xf86MsgVerb(X_PROBED, 4, "%s: usb bus opened\n", local->name);
241         acecad_driver = sysfs_get_bus_driver(usb_bus, acecad_driver_name);
242         if (acecad_driver) {
243             xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad driver opened\n", local->name);
244             devs = sysfs_get_driver_devices(acecad_driver);
245             if (devs) {
246                 xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad devices retrieved\n", local->name);
247                 dlist_for_each_data(devs, candidate, struct sysfs_device) {
248                     xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s\n", local->name, candidate->name, candidate->path);
249                     links = sysfs_open_link_list(candidate->path);
250                     dlist_for_each_data(links, link, char) {
251                         if (sscanf(link, "input:event%d", &i) == 1) {
252                             xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s: %s\n", local->name, candidate->name, candidate->path, link);
253                             break;
254                         }
255                     }
256                     sysfs_close_list(links);
257                     if (i > 0) /* We found something */
258                         break;
259                 }
260             } else
261                 xf86MsgVerb(X_WARNING, 4, "%s: no usb_acecad devices found\n", local->name);
262         } else
263             xf86MsgVerb(X_WARNING, 4, "%s: usb_acecad driver not found\n", local->name);
264     } else
265         xf86MsgVerb(X_WARNING, 4, "%s: usb bus not found\n", local->name);
266     sysfs_close_bus(usb_bus);
267
268     if (i > 0) {
269         /* We found something */
270         np = SET_EVENT_NUM(fname, i);
271         if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
272             xf86MsgVerb(X_WARNING, verb, "%s: unable to manage event device %d\n", local->name, i);
273         } else {
274             goto ProbeFound;
275         }
276     } else
277         xf86MsgVerb(X_WARNING, verb, "%s: no Acecad devices found via sysfs\n", local->name);
278
279 #endif
280
281     if (!ace_name)
282         ace_name = ace_name_default;
283
284     xf86MsgVerb(X_INFO, verb, "%s: probing event devices for Acecad tablets\n", local->name);
285     for (i = 0; ; i++) {
286         int fd = -1;
287         Bool is_acecad;
288
289         np = SET_EVENT_NUM(fname, i);
290         if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
291             xf86MsgVerb(X_WARNING, verb, "%s: too many devices, giving up %d\n", local->name, i);
292             break;
293         }
294         SYSCALL(fd = open(fname, O_RDONLY));
295         if (fd < 0) {
296             if (errno == ENOENT) {
297                 if (++noent_cnt >= max_skip)
298                     break;
299                 else
300                     continue;
301             } else {
302                 continue;
303             }
304         }
305         noent_cnt = 0;
306         have_evdev = TRUE;
307         is_acecad = fd_query_acecad(fd, ace_name);
308         SYSCALL(close(fd));
309         if (is_acecad) {
310             goto ProbeFound;
311         }
312     }
313     xf86MsgVerb(X_WARNING, verb, "%s: no Acecad event device found (checked %d nodes, no device name started with '%s')\n",
314             local->name, i + 1, ace_name);
315     if (i <= max_skip)
316         xf86MsgVerb(X_WARNING, verb, "%s: The /dev/input/event* device nodes seem to be missing\n",
317                 local->name);
318     if (i > max_skip && !have_evdev)
319         xf86MsgVerb(X_WARNING, verb, "%s: The evdev kernel module seems to be missing\n", local->name);
320     return FALSE;
321
322 ProbeFound:
323     xf86Msg(X_PROBED, "%s auto-dev sets device to %s\n",
324             local->name, fname);
325     xf86ReplaceStrOption(local->options, "Device", fname);
326     return TRUE;
327 }
328
329 #endif
330
331 static InputInfoPtr
332 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
333 {
334     InputInfoPtr local = xf86AllocateInput(drv, 0);
335     AceCadPrivatePtr priv = calloc (1, sizeof(AceCadPrivateRec));
336     int speed;
337     int msgtype;
338     char *s;
339
340     if ((!local) || (!priv))
341         goto SetupProc_fail;
342
343     memset(priv, 0, sizeof(AceCadPrivateRec));
344
345     local->name = dev->identifier;
346     local->type_name = XI_TABLET;
347     local->flags = XI86_SEND_DRAG_EVENTS;
348 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
349     local->motion_history_proc = xf86GetMotionEvents;
350 #endif
351     local->control_proc = NULL;
352     local->switch_mode = NULL;
353     local->conversion_proc = ConvertProc;
354     local->reverse_conversion_proc = ReverseConvertProc;
355     local->dev = NULL;
356     local->private = priv;
357     local->private_flags = 0;
358     local->conf_idev = dev;
359     local->device_control = DeviceControl;
360     /*local->always_core_feedback = 0;*/
361
362     xf86CollectInputOptions(local, default_options, NULL);
363
364     xf86OptionListReport(local->options);
365
366     priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
367
368     s = xf86FindOptionValue(local->options, "Device");
369     if (!s || (s && (xf86NameCmp(s, "auto-dev") == 0))) {
370 #ifdef HAVE_LINUX_INPUT_H
371         priv->flags |= AUTODEV_FLAG;
372         if (!AceCadAutoDevProbe(local, 0))
373         {
374             xf86Msg(X_ERROR, "%s: unable to find device\n", local->name);
375             goto SetupProc_fail;
376         }
377 #else
378         xf86Msg(X_NOT_IMPLEMENTED, "%s: device autodetection not implemented, sorry\n", local->name);
379         goto SetupProc_fail;
380 #endif
381     }
382
383     local->fd = xf86OpenSerial (local->options);
384     if (local->fd == -1)
385     {
386         xf86Msg(X_ERROR, "%s: unable to open device\n", local->name);
387         goto SetupProc_fail;
388     }
389     xf86ErrorFVerb( 6, "tty port opened successfully\n" );
390
391 #ifdef HAVE_LINUX_INPUT_H
392     if (IsUSBLine(local->fd)) {
393         priv->flags |= USB_FLAG;
394
395         local->read_input = USBReadInput;
396
397         if (USBQueryHardware(local) != Success)
398         {
399             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
400             goto SetupProc_fail;
401         }
402     } else
403 #endif
404     {
405         local->read_input = ReadInput;
406
407         msgtype = X_DEFAULT;
408         if (xf86FindOptionValue(local->options, "ReportSpeed")) {
409             msgtype = X_CONFIG;
410             speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
411         } else {
412             speed = 85;
413         }
414
415         switch (speed)
416         {
417             case 120:
418                 priv->acecadReportSpeed = 'Q';
419                 break;
420             case 85:
421                 priv->acecadReportSpeed = 'R';
422                 break;
423             case 10:
424                 priv->acecadReportSpeed = 'S';
425                 break;
426             case 2:
427                 priv->acecadReportSpeed = 'T';
428                 break;
429             default:
430                 priv->acecadReportSpeed = 'R';
431                 speed = 85;
432                 xf86Msg(X_ERROR, "%s: ReportSpeed value %d invalid. Possible values: 120, 85, 10, 2. Defaulting to 85\n", local->name, speed);
433                 msgtype = X_DEFAULT;
434         }
435
436         xf86Msg(msgtype, "%s report %d points/s\n", local->name, speed);
437
438         priv->buffer = XisbNew (local->fd, 200);
439
440         /*
441          * Verify that hardware is attached and fuctional
442          */
443         if (QueryHardware(priv) != Success)
444         {
445             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
446             goto SetupProc_fail;
447         }
448     }
449
450     s = xf86FindOptionValue(local->options, "Mode");
451     msgtype = s ? X_CONFIG : X_DEFAULT;
452     if (!(s && (xf86NameCmp(s, "relative") == 0)))
453     {
454         priv->flags |= ABSOLUTE_FLAG;
455     }
456
457     xf86Msg(msgtype, "%s is in %s mode\n", local->name, (priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
458     DBG (9, XisbTrace (priv->buffer, 1));
459
460     local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
461
462     xf86ProcessCommonOptions(local, local->options);
463
464     local->flags |= XI86_CONFIGURED;
465
466     if (local->fd != -1)
467     {
468         RemoveEnabledDevice (local->fd);
469         if (priv->buffer)
470         {
471             XisbFree(priv->buffer);
472             priv->buffer = NULL;
473         }
474         xf86CloseSerial(local->fd);
475     }
476     RemoveEnabledDevice (local->fd);
477     local->fd = -1;
478     return local;
479
480     /*
481      * If something went wrong, cleanup and return NULL
482      */
483 SetupProc_fail:
484     if ((local) && (local->fd))
485         xf86CloseSerial (local->fd);
486     if ((priv) && (priv->buffer))
487         XisbFree (priv->buffer);
488     if (priv) {
489         free (priv);
490         if (local)
491                 local->private = NULL;
492     }
493     xf86DeleteInput(local, 0);
494     return NULL;
495 }
496
497 static Bool
498 DeviceControl (DeviceIntPtr dev, int mode)
499 {
500     Bool RetValue;
501
502     switch (mode)
503     {
504         case DEVICE_INIT:
505             DeviceInit(dev);
506             RetValue = Success;
507             break;
508         case DEVICE_ON:
509             RetValue = DeviceOn(dev);
510             break;
511         case DEVICE_OFF:
512             RetValue = DeviceOff(dev);
513             break;
514         case DEVICE_CLOSE:
515             RetValue = DeviceClose(dev);
516             break;
517         default:
518             RetValue = BadValue;
519     }
520
521     return RetValue;
522 }
523
524 static Bool
525 DeviceOn (DeviceIntPtr dev)
526 {
527     char buffer[256];
528     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
529     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
530
531     xf86MsgVerb(X_INFO, 4, "%s Device On\n", local->name);
532
533     local->fd = xf86OpenSerial(local->options);
534     if (local->fd == -1)
535     {
536         xf86Msg(X_WARNING, "%s: cannot open input device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
537         priv->flags &= ~AVAIL_FLAG;
538 #ifdef HAVE_LINUX_INPUT_H
539         if ((priv->flags & AUTODEV_FLAG) && AceCadAutoDevProbe(local, 4))
540             local->fd = xf86OpenSerial(local->options);
541         if (local->fd == -1)
542 #endif
543             return !Success;
544     }
545     priv->flags |= AVAIL_FLAG;
546
547
548     if (!(priv->flags & USB_FLAG)) {
549         priv->buffer = XisbNew(local->fd, 200);
550         if (!priv->buffer)
551         {
552             xf86CloseSerial(local->fd);
553             local->fd = -1;
554             return !Success;
555         }
556
557         /* Rets qu'a l'envoyer a la tablette */
558         sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed, ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
559         XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
560     }
561
562     xf86FlushInput(local->fd);
563     xf86AddEnabledDevice (local);
564     dev->public.on = TRUE;
565     return Success;
566 }
567
568 static Bool
569 DeviceOff (DeviceIntPtr dev)
570 {
571     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
572     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
573
574     xf86MsgVerb(X_INFO, 4, "%s Device Off\n", local->name);
575
576     if (local->fd != -1)
577     {
578         RemoveEnabledDevice (local->fd);
579         if (priv->buffer)
580         {
581             XisbFree(priv->buffer);
582             priv->buffer = NULL;
583         }
584         xf86CloseSerial(local->fd);
585     }
586
587
588     xf86RemoveEnabledDevice (local);
589     dev->public.on = FALSE;
590     return Success;
591 }
592
593 static Bool
594 DeviceClose (DeviceIntPtr dev)
595 {
596     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
597
598     xf86MsgVerb(X_INFO, 4, "%s Device Close\n", local->name);
599
600     return Success;
601 }
602
603 static void
604 ControlProc(DeviceIntPtr dev, PtrCtrl *ctrl)
605 {
606     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
607
608     xf86MsgVerb(X_INFO, 4, "%s Control Proc\n", local->name);
609 }
610
611 static Bool
612 DeviceInit (DeviceIntPtr dev)
613 {
614     int rx, ry;
615     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
616     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
617     unsigned char map[] = {0, 1, 2, 3};
618     int history_size;
619 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
620     Atom btn_labels[3];
621     Atom axes_labels[3];
622
623     btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
624     btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
625     btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
626
627     if ((priv->flags & ABSOLUTE_FLAG))
628     {
629         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
630         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
631         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
632     } else
633     {
634         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
635         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
636         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Z);
637     }
638 #endif
639
640     xf86MsgVerb(X_INFO, 4, "%s Init\n", local->name);
641
642     /* 3 boutons */
643     if (InitButtonClassDeviceStruct (dev, 3,
644 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
645                 btn_labels,
646 #endif
647                 map) == FALSE)
648     {
649         xf86Msg(X_ERROR, "%s: unable to allocate ButtonClassDeviceStruct\n", local->name);
650         return !Success;
651     }
652
653     if (InitFocusClassDeviceStruct (dev) == FALSE)
654     {
655         xf86Msg(X_ERROR, "%s: unable to allocate FocusClassDeviceStruct\n", local->name);
656         return !Success;
657     }
658
659     if (InitPtrFeedbackClassDeviceStruct(dev, ControlProc) == FALSE) {
660         xf86Msg(X_ERROR, "%s: unable to init ptr feedback\n", local->name);
661         return !Success;
662     }
663
664     history_size = xf86SetIntOption(local->options , "HistorySize", 0);
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                 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 (InputInfoPtr 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 HAVE_LINUX_INPUT_H
858 #define set_bit(byte,nb,bit)    (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
859 static void
860 USBReadInput (InputInfoPtr 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 = xf86ReadSerial(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 /*
1015  * The ConvertProc function may need to be tailored for your device.
1016  * This function converts the device's valuator outputs to x and y coordinates
1017  * to simulate mouse events.
1018  */
1019 static Bool
1020 ConvertProc (InputInfoPtr local, int first, int num,
1021         int v0, int v1, int v2, int v3, int v4, int v5,
1022         int *x, int *y)
1023 {
1024     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1025
1026     /* TODO: should have a structure to hold which screen the
1027      * pointer is attached to? */
1028     // xf86Msg(X_INFO, "%s: coordinate conversion in : %d, %d\n", local->name, v0, v1);
1029     *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
1030     *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
1031     // xf86Msg(X_INFO, "%s: coordinate conversion out: %d, %d\n", local->name, *x, *y);
1032     return TRUE;
1033 }
1034
1035
1036 static Bool
1037 ReverseConvertProc (InputInfoPtr local,
1038         int x, int  y,
1039         int *valuators)
1040 {
1041     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1042
1043     // xf86Msg(X_INFO, "%s: reverse coordinate conversion in : %d, %d\n", local->name, x, y);
1044     valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
1045     valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
1046     // xf86Msg(X_INFO, "%s: reverse coordinate conversion out: %d, %d\n", local->name, valuators[0], valuators[1]);
1047
1048     return TRUE;
1049 }
1050
1051
1052 #define WriteString(str)\
1053     XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
1054
1055
1056 static Bool
1057 QueryHardware (AceCadPrivatePtr priv)
1058 {
1059
1060     /* Reset */
1061     WriteString("z0");
1062
1063     /* Wait */
1064     milisleep (250);
1065
1066     /* Prompt Mode in order to not be disturbed */
1067     WriteString(ACECAD_PROMPT_MODE);
1068
1069     /* Flush */
1070     while (XisbRead(priv->buffer) >= 0);
1071
1072     /* Ask for Config packet*/
1073     WriteString(ACECAD_CONFIG);
1074
1075     /* Read the packet */
1076     XisbBlockDuration (priv->buffer, 1000000);
1077     NewPacket (priv);
1078
1079     /*xf86Msg(X_CONFIG, "ACECAD Tablet init envoyé \n");*/
1080
1081     if ((AceCadGetPacket (priv) == Success))
1082     {
1083         priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
1084         priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
1085         priv->acecadMaxZ = 512;
1086         xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d\n", priv->acecadMaxX, priv->acecadMaxY);
1087     }
1088     else
1089         return !Success;
1090
1091     /*xf86Msg(X_INFO, "ACECAD Tablet query hardware fini \n");*/
1092     return Success;
1093 }
1094
1095 #define BITS_PER_LONG (sizeof(long) * 8)
1096 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
1097 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
1098 #define OFF(x)  ((x)%BITS_PER_LONG)
1099 #define LONG(x) ((x)/BITS_PER_LONG)
1100
1101 #ifdef HAVE_LINUX_INPUT_H
1102 static Bool
1103 USBQueryHardware (InputInfoPtr local)
1104 {
1105     AceCadPrivatePtr    priv = (AceCadPrivatePtr) local->private;
1106     unsigned long       bit[EV_MAX][NBITS(KEY_MAX)];
1107     int                 i, j;
1108     int                 abs[5];
1109     char                name[256] = "Unknown";
1110
1111     ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
1112     xf86MsgVerb(X_PROBED, 4, "Kernel Input device name: \"%s\"\n", name);
1113
1114     memset(bit, 0, sizeof(bit));
1115     ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
1116
1117     for (i = 0; i < EV_MAX; i++)
1118         if (test_bit(i, bit[0])) {
1119             ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
1120             for (j = 0; j < KEY_MAX; j++)
1121                 if (test_bit(j, bit[i])) {
1122                     if (i == EV_ABS) {
1123                         ioctl(local->fd, EVIOCGABS(j), abs);
1124                         switch (j) {
1125                             case ABS_X:
1126                                 priv->acecadMaxX = abs[2];
1127                                 break;
1128
1129                             case ABS_Y:
1130                                 priv->acecadMaxY = abs[2];
1131                                 break;
1132
1133                             case ABS_PRESSURE:
1134                                 priv->acecadMaxZ = abs[2];
1135                                 break;
1136                         }
1137                     }
1138                 }
1139         }
1140
1141     xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY, priv->acecadMaxZ);
1142     return Success;
1143 }
1144 #endif
1145
1146 static void
1147 NewPacket (AceCadPrivatePtr priv)
1148 {
1149     priv->packeti = 0;
1150 }
1151
1152 static Bool
1153 AceCadGetPacket (AceCadPrivatePtr priv)
1154 {
1155     int count = 0;
1156     int c = 0;
1157
1158     while((c = XisbRead(priv->buffer)) >= 0 )
1159     {
1160
1161         /*
1162          * fail after 500 bytes so the server doesn't hang forever if a
1163          * device sends bad data.
1164          */
1165         if (count++ > 500)
1166         {
1167             NewPacket (priv);
1168             return !Success;
1169         }
1170
1171         if (c & PHASING_BIT)
1172         {
1173             NewPacket(priv);
1174
1175             /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
1176             XisbBlockDuration (priv->buffer, 10000);
1177             priv->packet[priv->packeti++] = c;
1178             count = ACECAD_PACKET_SIZE - 1;
1179             while (count-- && (c = XisbRead(priv->buffer)) >= 0)
1180             {
1181                 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
1182                 priv->packet[priv->packeti++] = c;
1183             }
1184             XisbBlockDuration (priv->buffer, 0);
1185             if(c > 0)
1186                 return Success;
1187         }
1188     }
1189     return !Success;
1190 }