Purge close_proc, hasn't been called by the server for ages.
[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 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
619     Atom btn_labels[3];
620     Atom axes_labels[3];
621
622     btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
623     btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
624     btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
625
626     if ((priv->flags & ABSOLUTE_FLAG))
627     {
628         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
629         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
630         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
631     } else
632     {
633         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
634         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
635         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Z);
636     }
637 #endif
638
639     xf86MsgVerb(X_INFO, 4, "%s Init\n", local->name);
640
641     /* 3 boutons */
642     if (InitButtonClassDeviceStruct (dev, 3,
643 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
644                 btn_labels,
645 #endif
646                 map) == FALSE)
647     {
648         xf86Msg(X_ERROR, "%s: unable to allocate ButtonClassDeviceStruct\n", local->name);
649         return !Success;
650     }
651
652     if (InitFocusClassDeviceStruct (dev) == FALSE)
653     {
654         xf86Msg(X_ERROR, "%s: unable to allocate FocusClassDeviceStruct\n", local->name);
655         return !Success;
656     }
657
658     if (InitPtrFeedbackClassDeviceStruct(dev, ControlProc) == FALSE) {
659         xf86Msg(X_ERROR, "%s: unable to init ptr feedback\n", local->name);
660         return !Success;
661     }
662
663
664     /* 3 axes */
665     if (InitValuatorClassDeviceStruct (dev, 3,
666 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
667                 axes_labels,
668 #endif
669 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
670                 xf86GetMotionEvents,
671 #endif
672                 local->history_size,
673                 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
674             == FALSE)
675     {
676         xf86Msg(X_ERROR, "%s: unable to allocate ValuatorClassDeviceStruct\n", local->name);
677         return !Success;
678     }
679     else
680     {
681
682         InitValuatorAxisStruct(dev,
683                 0,
684 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
685                 axes_labels[0],
686 #endif
687                 0,                      /* min val */
688 #if XORG_BOTCHED_INPUT
689                 screenInfo.screens[0]->width,
690 #else
691                 priv->acecadMaxX,       /* max val */
692 #endif
693                 1000,                   /* resolution */
694                 0,                      /* min_res */
695                 1000);                  /* max_res */
696         InitValuatorAxisStruct(dev,
697                 1,
698 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
699                 axes_labels[1],
700 #endif
701                 0,                      /* min val */
702 #if XORG_BOTCHED_INPUT
703                 screenInfo.screens[0]->height,
704 #else
705                 priv->acecadMaxY,       /* max val */
706 #endif
707                 1000,                   /* resolution */
708                 0,                      /* min_res */
709                 1000);                  /* max_res */
710         InitValuatorAxisStruct(dev,
711                 2,
712 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
713                 axes_labels[2],
714 #endif
715                 0,                      /* min val */
716                 priv->acecadMaxZ,       /* max val */
717                 1000,                   /* resolution */
718                 0,                      /* min_res */
719                 1000);          /* max_res */
720
721     }
722
723     if (InitProximityClassDeviceStruct (dev) == FALSE)
724     {
725         xf86Msg(X_ERROR, "%s: unable to allocate ProximityClassDeviceStruct\n", local->name);
726         return !Success;
727     }
728
729     xf86MotionHistoryAllocate (local);
730
731
732     /* On ne peut pas calculer l'increment avant, faute d'ecran pour
733        connaitre la taille... */
734
735     if (priv->acecadInc > 95)
736         priv->acecadInc = 95;
737     if (priv->acecadInc < 1)
738     {
739         /* guess the best increment value given video mode */
740         rx = priv->acecadMaxX / screenInfo.screens[0]->width;
741         ry = priv->acecadMaxY / screenInfo.screens[0]->height;
742         if (rx < ry)
743             priv->acecadInc = rx;
744         else
745             priv->acecadInc = ry;
746         if (priv->acecadInc < 1)
747             priv->acecadInc = 1;
748     }
749
750     xf86Msg(X_INFO, "%s Increment: %d\n", local->name, priv->acecadInc);
751
752     return Success;
753 }
754
755 static void
756 ReadInput (InputInfoPtr local)
757 {
758     int x, y, z;
759     int prox, buttons;
760     int is_core_pointer = 0, is_absolute;
761     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
762
763     /*xf86Msg(X_INFO, "ACECAD Tablet Read Input\n");*/
764
765     is_absolute = (priv->flags & ABSOLUTE_FLAG);
766 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
767     is_core_pointer = xf86IsCorePointer(local->dev);
768 #endif
769
770     /*
771      * set blocking to -1 on the first call because we know there is data to
772      * read. Xisb automatically clears it after one successful read so that
773      * succeeding reads are preceeded buy a select with a 0 timeout to prevent
774      * read from blocking indefinately.
775      */
776     XisbBlockDuration (priv->buffer, -1);
777
778     while (AceCadGetPacket (priv) == Success)
779     {
780         x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
781         y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
782
783         if (!(priv->flags & ABSOLUTE_FLAG))
784         {
785             x = priv->packet[0] & XSIGN_BIT? x:-x;
786             y = priv->packet[0] & YSIGN_BIT? y:-y;
787         }
788         else
789         {
790             y = priv->acecadMaxY - y ;
791         }
792
793
794         z = ((int)priv->packet[5] << 2) |
795             (((int)priv->packet[6] & 0x01) << 1) |
796             (((int)priv->packet[6] & 0x10) >> 4);
797
798         buttons = ((int)priv->packet[0] & 0x07) |
799             ((int)priv->packet[6] & 0x02 << 2);
800
801         prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
802
803         if (prox)
804         {
805             if (!(priv->acecadOldProximity))
806                 if (!is_core_pointer)
807                 {
808                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxIN %d %d %d\n",x, y, z);*/
809                     xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
810                 }
811
812             if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
813                     || (!is_absolute && (x || y)))
814             {
815                 if (is_absolute || priv->acecadOldProximity)
816                 {
817                     /*xf86Msg(X_INFO, "ACECAD Tablet Motion %d %d %d\n", x, y, z);*/
818                     xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
819                 }
820             }
821
822             if (priv->acecadOldButtons != buttons)
823             {
824                 int delta = buttons ^ priv->acecadOldButtons;
825                 while (delta)
826                 {
827                     int id = ffs(delta);
828                     delta &= ~(1 << (id-1));
829
830                     /*xf86Msg(X_INFO, "ACECAD Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
831                     xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
832                 }
833             }
834
835             priv->acecadOldButtons = buttons;
836             priv->acecadOldX = x;
837             priv->acecadOldY = y;
838             priv->acecadOldZ = z;
839             priv->acecadOldProximity = prox;
840         }
841         else
842         {
843             if (!is_core_pointer)
844                 if (priv->acecadOldProximity)
845                 {
846                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxOUT %d %d %d\n",x, y, z);*/
847                     xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
848                 }
849             priv->acecadOldProximity = 0;
850         }
851     }
852     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
853 }
854
855 #ifdef HAVE_LINUX_INPUT_H
856 #define set_bit(byte,nb,bit)    (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
857 static void
858 USBReadInput (InputInfoPtr local)
859 {
860     int len;
861     struct input_event * event;
862     char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
863     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
864     int x = priv->acecadOldX;
865     int y = priv->acecadOldY;
866     int z = priv->acecadOldZ;
867     int report_x, report_y;
868     int prox = priv->acecadOldProximity;
869     int buttons = priv->acecadOldButtons;
870     int is_core_pointer = 0;
871 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
872     is_core_pointer = xf86IsCorePointer(local->dev);
873 #endif
874     /* Is autodev active? */
875     int autodev = priv->flags & AUTODEV_FLAG;
876     /* Was the device available last time we checked? */
877     int avail = priv->flags & AVAIL_FLAG;
878
879     SYSCALL(len = xf86ReadSerial(local->fd, eventbuf, sizeof(eventbuf)));
880
881     if (len <= 0) {
882         if (avail) {
883             xf86Msg(X_ERROR, "%s: error reading device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
884         }
885         if (NOTAVAIL) {
886             priv->flags &= ~AVAIL_FLAG;
887             if(autodev) {
888                 if (AceCadAutoDevProbe(local, 4)) {
889                     DeviceOff(local->dev);
890                     DeviceOn(local->dev);
891                 }
892             }
893         }
894         return;
895     } else {
896         if (!avail) {
897             /* If the device wasn't available last time we checked */
898             xf86Msg(X_INFO, "%s: device %s is available again\n", local->name, xf86FindOptionValue(local->options, "Device"));
899             priv->flags |= AVAIL_FLAG;
900         }
901     }
902
903     for (event = (struct input_event *)eventbuf;
904             event < (struct input_event *)(eventbuf+len); event++) {
905
906         switch (event->type) {
907             case EV_SYN: /* 2.6.x */
908                 if (event->code != SYN_REPORT)
909                     xf86Msg(X_ERROR, "%s: unknown EV_SYN code %d\n", local->name, event->code);
910                 break;
911             case EV_ABS:
912                 switch (event->code) {
913                     case ABS_X:
914                         x = event->value;
915                         break;
916
917                     case ABS_Y:
918                         y = event->value;
919                         break;
920
921                     case ABS_PRESSURE:
922                         z = event->value;
923                         break;
924
925                     case ABS_MISC:
926                         break;
927
928                 }
929                 break; /* EV_ABS */
930
931             case EV_KEY:
932                 switch (event->code) {
933                     case BTN_TOOL_PEN:
934                         prox = event->value;
935                         break;
936
937                     case BTN_TOUCH:
938                         buttons = set_bit(buttons,0,event->value);
939                         break;
940
941                     case BTN_STYLUS:
942                         buttons = set_bit(buttons,1,event->value);
943                         break;
944
945                     case BTN_STYLUS2:
946                         buttons = set_bit(buttons,2,event->value);
947                         break;
948                 }
949                 break; /* EV_KEY */
950             default:
951                 xf86Msg(X_ERROR, "%s: unknown event type/code %d/%d\n", local->name, event->type, event->code);
952         } /* switch event->type */
953
954         /* Linux Kernel 2.6.x sends EV_SYN/SYN_REPORT as an event terminator,
955          * whereas 2.4.x sends EV_ABS/ABS_MISC. We have to support both.
956          */
957         if (!(  (event->type == EV_SYN && event->code == SYN_REPORT) ||
958                     (event->type == EV_ABS && event->code == ABS_MISC)
959              )) {
960             continue;
961         }
962
963         if (prox)
964         {
965 #if XORG_BOTCHED_INPUT
966             ConvertProc(local, 0, 3, x, y, 0, 0, 0, 0, &report_x, &report_y);
967 #else
968             report_x = x;
969             report_y = y;
970 #endif
971             if (!(priv->acecadOldProximity))
972                 if (!is_core_pointer)
973                 {
974                     xf86PostProximityEvent(local->dev, 1, 0, 3 , report_x, report_y, z);
975                 }
976
977
978             xf86PostMotionEvent(local->dev, 1, 0, 3, report_x, report_y, z);
979
980             if (priv->acecadOldButtons != buttons)
981             {
982                 int delta = buttons ^ priv->acecadOldButtons;
983                 while (delta)
984                 {
985                     int id = ffs(delta);
986                     delta &= ~(1 << (id-1));
987
988                     xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, report_x, report_y, z);
989                 }
990             }
991         }
992         else
993         {
994             if (!is_core_pointer)
995                 if (priv->acecadOldProximity)
996                 {
997                     xf86PostProximityEvent(local->dev, 0, 0, 3, report_x, report_y, z);
998                 }
999             priv->acecadOldProximity = 0;
1000         }
1001
1002         priv->acecadOldButtons = buttons;
1003         priv->acecadOldX = x;
1004         priv->acecadOldY = y;
1005         priv->acecadOldZ = z;
1006         priv->acecadOldProximity = prox;
1007     }
1008     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
1009 }
1010 #endif
1011
1012 /*
1013  * The ConvertProc function may need to be tailored for your device.
1014  * This function converts the device's valuator outputs to x and y coordinates
1015  * to simulate mouse events.
1016  */
1017 static Bool
1018 ConvertProc (InputInfoPtr local, int first, int num,
1019         int v0, int v1, int v2, int v3, int v4, int v5,
1020         int *x, int *y)
1021 {
1022     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1023
1024     /* TODO: should have a structure to hold which screen the
1025      * pointer is attached to? */
1026     // xf86Msg(X_INFO, "%s: coordinate conversion in : %d, %d\n", local->name, v0, v1);
1027     *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
1028     *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
1029     // xf86Msg(X_INFO, "%s: coordinate conversion out: %d, %d\n", local->name, *x, *y);
1030     return TRUE;
1031 }
1032
1033
1034 static Bool
1035 ReverseConvertProc (InputInfoPtr local,
1036         int x, int  y,
1037         int *valuators)
1038 {
1039     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1040
1041     // xf86Msg(X_INFO, "%s: reverse coordinate conversion in : %d, %d\n", local->name, x, y);
1042     valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
1043     valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
1044     // xf86Msg(X_INFO, "%s: reverse coordinate conversion out: %d, %d\n", local->name, valuators[0], valuators[1]);
1045
1046     return TRUE;
1047 }
1048
1049
1050 #define WriteString(str)\
1051     XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
1052
1053
1054 static Bool
1055 QueryHardware (AceCadPrivatePtr priv)
1056 {
1057
1058     /* Reset */
1059     WriteString("z0");
1060
1061     /* Wait */
1062     milisleep (250);
1063
1064     /* Prompt Mode in order to not be disturbed */
1065     WriteString(ACECAD_PROMPT_MODE);
1066
1067     /* Flush */
1068     while (XisbRead(priv->buffer) >= 0);
1069
1070     /* Ask for Config packet*/
1071     WriteString(ACECAD_CONFIG);
1072
1073     /* Read the packet */
1074     XisbBlockDuration (priv->buffer, 1000000);
1075     NewPacket (priv);
1076
1077     /*xf86Msg(X_CONFIG, "ACECAD Tablet init envoyé \n");*/
1078
1079     if ((AceCadGetPacket (priv) == Success))
1080     {
1081         priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
1082         priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
1083         priv->acecadMaxZ = 512;
1084         xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d\n", priv->acecadMaxX, priv->acecadMaxY);
1085     }
1086     else
1087         return !Success;
1088
1089     /*xf86Msg(X_INFO, "ACECAD Tablet query hardware fini \n");*/
1090     return Success;
1091 }
1092
1093 #define BITS_PER_LONG (sizeof(long) * 8)
1094 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
1095 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
1096 #define OFF(x)  ((x)%BITS_PER_LONG)
1097 #define LONG(x) ((x)/BITS_PER_LONG)
1098
1099 #ifdef HAVE_LINUX_INPUT_H
1100 static Bool
1101 USBQueryHardware (InputInfoPtr local)
1102 {
1103     AceCadPrivatePtr    priv = (AceCadPrivatePtr) local->private;
1104     unsigned long       bit[EV_MAX][NBITS(KEY_MAX)];
1105     int                 i, j;
1106     int                 abs[5];
1107     char                name[256] = "Unknown";
1108
1109     ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
1110     xf86MsgVerb(X_PROBED, 4, "Kernel Input device name: \"%s\"\n", name);
1111
1112     memset(bit, 0, sizeof(bit));
1113     ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
1114
1115     for (i = 0; i < EV_MAX; i++)
1116         if (test_bit(i, bit[0])) {
1117             ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
1118             for (j = 0; j < KEY_MAX; j++)
1119                 if (test_bit(j, bit[i])) {
1120                     if (i == EV_ABS) {
1121                         ioctl(local->fd, EVIOCGABS(j), abs);
1122                         switch (j) {
1123                             case ABS_X:
1124                                 priv->acecadMaxX = abs[2];
1125                                 break;
1126
1127                             case ABS_Y:
1128                                 priv->acecadMaxY = abs[2];
1129                                 break;
1130
1131                             case ABS_PRESSURE:
1132                                 priv->acecadMaxZ = abs[2];
1133                                 break;
1134                         }
1135                     }
1136                 }
1137         }
1138
1139     xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY, priv->acecadMaxZ);
1140     return Success;
1141 }
1142 #endif
1143
1144 static void
1145 NewPacket (AceCadPrivatePtr priv)
1146 {
1147     priv->packeti = 0;
1148 }
1149
1150 static Bool
1151 AceCadGetPacket (AceCadPrivatePtr priv)
1152 {
1153     int count = 0;
1154     int c = 0;
1155
1156     while((c = XisbRead(priv->buffer)) >= 0 )
1157     {
1158
1159         /*
1160          * fail after 500 bytes so the server doesn't hang forever if a
1161          * device sends bad data.
1162          */
1163         if (count++ > 500)
1164         {
1165             NewPacket (priv);
1166             return !Success;
1167         }
1168
1169         if (c & PHASING_BIT)
1170         {
1171             NewPacket(priv);
1172
1173             /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
1174             XisbBlockDuration (priv->buffer, 10000);
1175             priv->packet[priv->packeti++] = c;
1176             count = ACECAD_PACKET_SIZE - 1;
1177             while (count-- && (c = XisbRead(priv->buffer)) >= 0)
1178             {
1179                 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
1180                 priv->packet[priv->packeti++] = c;
1181             }
1182             XisbBlockDuration (priv->buffer, 0);
1183             if(c > 0)
1184                 return Success;
1185         }
1186     }
1187     return !Success;
1188 }