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