Replace LocalDevicePtr with InputInfoPtr.
[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 _X_EXPORT InputDriverRec ACECAD =
110 {
111         1,
112         "acecad",
113         NULL,
114         AceCadPreInit,
115         NULL,
116         NULL
117 };
118
119 static XF86ModuleVersionInfo VersionRec =
120 {
121         "acecad",
122         MODULEVENDORSTRING,
123         MODINFOSTRING1,
124         MODINFOSTRING2,
125         XORG_VERSION_CURRENT,
126         PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
127         ABI_CLASS_XINPUT,
128         ABI_XINPUT_VERSION,
129         MOD_CLASS_XINPUT,
130         {0, 0, 0, 0}
131 };
132
133
134 _X_EXPORT XF86ModuleData acecadModuleData = {
135         &VersionRec,
136         SetupProc,
137         TearDownProc
138 };
139
140 /*****************************************************************************
141  *      Function Definitions
142  ****************************************************************************/
143
144 static pointer
145 SetupProc(      pointer module,
146                 pointer options,
147                 int *errmaj,
148                 int *errmin )
149 {
150         xf86AddInputDriver(&ACECAD, module, 0);
151         return module;
152 }
153
154 static void
155 TearDownProc( pointer p )
156 {
157 }
158
159 static const char *default_options[] =
160 {
161         "BaudRate", "9600",
162         "StopBits", "1",
163         "DataBits", "8",
164         "Parity", "Odd",
165         "Vmin", "1",
166         "Vtime", "10",
167         "FlowControl", "Xoff",
168         NULL
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 = xcalloc (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->close_proc = CloseProc;
353     local->switch_mode = NULL;
354     local->conversion_proc = ConvertProc;
355     local->reverse_conversion_proc = ReverseConvertProc;
356     local->dev = NULL;
357     local->private = priv;
358     local->private_flags = 0;
359     local->conf_idev = dev;
360     local->device_control = DeviceControl;
361     /*local->always_core_feedback = 0;*/
362
363     xf86CollectInputOptions(local, default_options, NULL);
364
365     xf86OptionListReport(local->options);
366
367     priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
368
369     s = xf86FindOptionValue(local->options, "Device");
370     if (!s || (s && (xf86NameCmp(s, "auto-dev") == 0))) {
371 #ifdef HAVE_LINUX_INPUT_H
372         priv->flags |= AUTODEV_FLAG;
373         if (!AceCadAutoDevProbe(local, 0))
374         {
375             xf86Msg(X_ERROR, "%s: unable to find device\n", local->name);
376             goto SetupProc_fail;
377         }
378 #else
379         xf86Msg(X_NOT_IMPLEMENTED, "%s: device autodetection not implemented, sorry\n", local->name);
380         goto SetupProc_fail;
381 #endif
382     }
383
384     local->fd = xf86OpenSerial (local->options);
385     if (local->fd == -1)
386     {
387         xf86Msg(X_ERROR, "%s: unable to open device\n", local->name);
388         goto SetupProc_fail;
389     }
390     xf86ErrorFVerb( 6, "tty port opened successfully\n" );
391
392 #ifdef HAVE_LINUX_INPUT_H
393     if (IsUSBLine(local->fd)) {
394         priv->flags |= USB_FLAG;
395
396         local->read_input = USBReadInput;
397
398         if (USBQueryHardware(local) != Success)
399         {
400             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
401             goto SetupProc_fail;
402         }
403     } else
404 #endif
405     {
406         local->read_input = ReadInput;
407
408         msgtype = X_DEFAULT;
409         if (xf86FindOptionValue(local->options, "ReportSpeed")) {
410             msgtype = X_CONFIG;
411             speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
412         } else {
413             speed = 85;
414         }
415
416         switch (speed)
417         {
418             case 120:
419                 priv->acecadReportSpeed = 'Q';
420                 break;
421             case 85:
422                 priv->acecadReportSpeed = 'R';
423                 break;
424             case 10:
425                 priv->acecadReportSpeed = 'S';
426                 break;
427             case 2:
428                 priv->acecadReportSpeed = 'T';
429                 break;
430             default:
431                 priv->acecadReportSpeed = 'R';
432                 speed = 85;
433                 xf86Msg(X_ERROR, "%s: ReportSpeed value %d invalid. Possible values: 120, 85, 10, 2. Defaulting to 85\n", local->name, speed);
434                 msgtype = X_DEFAULT;
435         }
436
437         xf86Msg(msgtype, "%s report %d points/s\n", local->name, speed);
438
439         priv->buffer = XisbNew (local->fd, 200);
440
441         /*
442          * Verify that hardware is attached and fuctional
443          */
444         if (QueryHardware(priv) != Success)
445         {
446             xf86Msg(X_ERROR, "%s: unable to query/initialize hardware (not an %s?).\n", local->name, local->type_name);
447             goto SetupProc_fail;
448         }
449     }
450
451     s = xf86FindOptionValue(local->options, "Mode");
452     msgtype = s ? X_CONFIG : X_DEFAULT;
453     if (!(s && (xf86NameCmp(s, "relative") == 0)))
454     {
455         priv->flags |= ABSOLUTE_FLAG;
456     }
457
458     xf86Msg(msgtype, "%s is in %s mode\n", local->name, (priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
459     DBG (9, XisbTrace (priv->buffer, 1));
460
461     local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
462
463     xf86ProcessCommonOptions(local, local->options);
464
465     local->flags |= XI86_CONFIGURED;
466
467     if (local->fd != -1)
468     {
469         RemoveEnabledDevice (local->fd);
470         if (priv->buffer)
471         {
472             XisbFree(priv->buffer);
473             priv->buffer = NULL;
474         }
475         xf86CloseSerial(local->fd);
476     }
477     RemoveEnabledDevice (local->fd);
478     local->fd = -1;
479     return local;
480
481     /*
482      * If something went wrong, cleanup and return NULL
483      */
484 SetupProc_fail:
485     if ((local) && (local->fd))
486         xf86CloseSerial (local->fd);
487     if ((priv) && (priv->buffer))
488         XisbFree (priv->buffer);
489     if (priv) {
490         xfree (priv);
491         if (local)
492                 local->private = NULL;
493     }
494     xf86DeleteInput(local, 0);
495     return NULL;
496 }
497
498 static Bool
499 DeviceControl (DeviceIntPtr dev, int mode)
500 {
501     Bool RetValue;
502
503     switch (mode)
504     {
505         case DEVICE_INIT:
506             DeviceInit(dev);
507             RetValue = Success;
508             break;
509         case DEVICE_ON:
510             RetValue = DeviceOn(dev);
511             break;
512         case DEVICE_OFF:
513             RetValue = DeviceOff(dev);
514             break;
515         case DEVICE_CLOSE:
516             RetValue = DeviceClose(dev);
517             break;
518         default:
519             RetValue = BadValue;
520     }
521
522     return RetValue;
523 }
524
525 static Bool
526 DeviceOn (DeviceIntPtr dev)
527 {
528     char buffer[256];
529     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
530     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
531
532     xf86MsgVerb(X_INFO, 4, "%s Device On\n", local->name);
533
534     local->fd = xf86OpenSerial(local->options);
535     if (local->fd == -1)
536     {
537         xf86Msg(X_WARNING, "%s: cannot open input device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
538         priv->flags &= ~AVAIL_FLAG;
539 #ifdef HAVE_LINUX_INPUT_H
540         if ((priv->flags & AUTODEV_FLAG) && AceCadAutoDevProbe(local, 4))
541             local->fd = xf86OpenSerial(local->options);
542         if (local->fd == -1)
543 #endif
544             return !Success;
545     }
546     priv->flags |= AVAIL_FLAG;
547
548
549     if (!(priv->flags & USB_FLAG)) {
550         priv->buffer = XisbNew(local->fd, 200);
551         if (!priv->buffer)
552         {
553             xf86CloseSerial(local->fd);
554             local->fd = -1;
555             return !Success;
556         }
557
558         /* Rets qu'a l'envoyer a la tablette */
559         sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed, ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
560         XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
561     }
562
563     xf86FlushInput(local->fd);
564     xf86AddEnabledDevice (local);
565     dev->public.on = TRUE;
566     return Success;
567 }
568
569 static Bool
570 DeviceOff (DeviceIntPtr dev)
571 {
572     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
573     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
574
575     xf86MsgVerb(X_INFO, 4, "%s Device Off\n", local->name);
576
577     if (local->fd != -1)
578     {
579         RemoveEnabledDevice (local->fd);
580         if (priv->buffer)
581         {
582             XisbFree(priv->buffer);
583             priv->buffer = NULL;
584         }
585         xf86CloseSerial(local->fd);
586     }
587
588
589     xf86RemoveEnabledDevice (local);
590     dev->public.on = FALSE;
591     return Success;
592 }
593
594 static Bool
595 DeviceClose (DeviceIntPtr dev)
596 {
597     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
598
599     xf86MsgVerb(X_INFO, 4, "%s Device Close\n", local->name);
600
601     return Success;
602 }
603
604 static void
605 ControlProc(DeviceIntPtr dev, PtrCtrl *ctrl)
606 {
607     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
608
609     xf86MsgVerb(X_INFO, 4, "%s Control Proc\n", local->name);
610 }
611
612 static Bool
613 DeviceInit (DeviceIntPtr dev)
614 {
615     int rx, ry;
616     InputInfoPtr local = (InputInfoPtr) dev->public.devicePrivate;
617     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
618     unsigned char map[] = {0, 1, 2, 3};
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
665     /* 3 axes */
666     if (InitValuatorClassDeviceStruct (dev, 3,
667 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
668                 axes_labels,
669 #endif
670 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
671                 xf86GetMotionEvents,
672 #endif
673                 local->history_size,
674                 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
675             == FALSE)
676     {
677         xf86Msg(X_ERROR, "%s: unable to allocate ValuatorClassDeviceStruct\n", local->name);
678         return !Success;
679     }
680     else
681     {
682
683         InitValuatorAxisStruct(dev,
684                 0,
685 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
686                 axes_labels[0],
687 #endif
688                 0,                      /* min val */
689 #if XORG_BOTCHED_INPUT
690                 screenInfo.screens[0]->width,
691 #else
692                 priv->acecadMaxX,       /* max val */
693 #endif
694                 1000,                   /* resolution */
695                 0,                      /* min_res */
696                 1000);                  /* max_res */
697         InitValuatorAxisStruct(dev,
698                 1,
699 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
700                 axes_labels[1],
701 #endif
702                 0,                      /* min val */
703 #if XORG_BOTCHED_INPUT
704                 screenInfo.screens[0]->height,
705 #else
706                 priv->acecadMaxY,       /* max val */
707 #endif
708                 1000,                   /* resolution */
709                 0,                      /* min_res */
710                 1000);                  /* max_res */
711         InitValuatorAxisStruct(dev,
712                 2,
713 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
714                 axes_labels[2],
715 #endif
716                 0,                      /* min val */
717                 priv->acecadMaxZ,       /* max val */
718                 1000,                   /* resolution */
719                 0,                      /* min_res */
720                 1000);          /* max_res */
721
722     }
723
724     if (InitProximityClassDeviceStruct (dev) == FALSE)
725     {
726         xf86Msg(X_ERROR, "%s: unable to allocate ProximityClassDeviceStruct\n", local->name);
727         return !Success;
728     }
729
730     xf86MotionHistoryAllocate (local);
731
732
733     /* On ne peut pas calculer l'increment avant, faute d'ecran pour
734        connaitre la taille... */
735
736     if (priv->acecadInc > 95)
737         priv->acecadInc = 95;
738     if (priv->acecadInc < 1)
739     {
740         /* guess the best increment value given video mode */
741         rx = priv->acecadMaxX / screenInfo.screens[0]->width;
742         ry = priv->acecadMaxY / screenInfo.screens[0]->height;
743         if (rx < ry)
744             priv->acecadInc = rx;
745         else
746             priv->acecadInc = ry;
747         if (priv->acecadInc < 1)
748             priv->acecadInc = 1;
749     }
750
751     xf86Msg(X_INFO, "%s Increment: %d\n", local->name, priv->acecadInc);
752
753     return Success;
754 }
755
756 static void
757 ReadInput (InputInfoPtr local)
758 {
759     int x, y, z;
760     int prox, buttons;
761     int is_core_pointer = 0, is_absolute;
762     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
763
764     /*xf86Msg(X_INFO, "ACECAD Tablet Read Input\n");*/
765
766     is_absolute = (priv->flags & ABSOLUTE_FLAG);
767 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
768     is_core_pointer = xf86IsCorePointer(local->dev);
769 #endif
770
771     /*
772      * set blocking to -1 on the first call because we know there is data to
773      * read. Xisb automatically clears it after one successful read so that
774      * succeeding reads are preceeded buy a select with a 0 timeout to prevent
775      * read from blocking indefinately.
776      */
777     XisbBlockDuration (priv->buffer, -1);
778
779     while (AceCadGetPacket (priv) == Success)
780     {
781         x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
782         y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
783
784         if (!(priv->flags & ABSOLUTE_FLAG))
785         {
786             x = priv->packet[0] & XSIGN_BIT? x:-x;
787             y = priv->packet[0] & YSIGN_BIT? y:-y;
788         }
789         else
790         {
791             y = priv->acecadMaxY - y ;
792         }
793
794
795         z = ((int)priv->packet[5] << 2) |
796             (((int)priv->packet[6] & 0x01) << 1) |
797             (((int)priv->packet[6] & 0x10) >> 4);
798
799         buttons = ((int)priv->packet[0] & 0x07) |
800             ((int)priv->packet[6] & 0x02 << 2);
801
802         prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
803
804         if (prox)
805         {
806             if (!(priv->acecadOldProximity))
807                 if (!is_core_pointer)
808                 {
809                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxIN %d %d %d\n",x, y, z);*/
810                     xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
811                 }
812
813             if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
814                     || (!is_absolute && (x || y)))
815             {
816                 if (is_absolute || priv->acecadOldProximity)
817                 {
818                     /*xf86Msg(X_INFO, "ACECAD Tablet Motion %d %d %d\n", x, y, z);*/
819                     xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
820                 }
821             }
822
823             if (priv->acecadOldButtons != buttons)
824             {
825                 int delta = buttons ^ priv->acecadOldButtons;
826                 while (delta)
827                 {
828                     int id = ffs(delta);
829                     delta &= ~(1 << (id-1));
830
831                     /*xf86Msg(X_INFO, "ACECAD Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
832                     xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
833                 }
834             }
835
836             priv->acecadOldButtons = buttons;
837             priv->acecadOldX = x;
838             priv->acecadOldY = y;
839             priv->acecadOldZ = z;
840             priv->acecadOldProximity = prox;
841         }
842         else
843         {
844             if (!is_core_pointer)
845                 if (priv->acecadOldProximity)
846                 {
847                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxOUT %d %d %d\n",x, y, z);*/
848                     xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
849                 }
850             priv->acecadOldProximity = 0;
851         }
852     }
853     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
854 }
855
856 #ifdef HAVE_LINUX_INPUT_H
857 #define set_bit(byte,nb,bit)    (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
858 static void
859 USBReadInput (InputInfoPtr local)
860 {
861     int len;
862     struct input_event * event;
863     char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
864     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
865     int x = priv->acecadOldX;
866     int y = priv->acecadOldY;
867     int z = priv->acecadOldZ;
868     int report_x, report_y;
869     int prox = priv->acecadOldProximity;
870     int buttons = priv->acecadOldButtons;
871     int is_core_pointer = 0;
872 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
873     is_core_pointer = xf86IsCorePointer(local->dev);
874 #endif
875     /* Is autodev active? */
876     int autodev = priv->flags & AUTODEV_FLAG;
877     /* Was the device available last time we checked? */
878     int avail = priv->flags & AVAIL_FLAG;
879
880     SYSCALL(len = xf86ReadSerial(local->fd, eventbuf, sizeof(eventbuf)));
881
882     if (len <= 0) {
883         if (avail) {
884             xf86Msg(X_ERROR, "%s: error reading device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
885         }
886         if (NOTAVAIL) {
887             priv->flags &= ~AVAIL_FLAG;
888             if(autodev) {
889                 if (AceCadAutoDevProbe(local, 4)) {
890                     DeviceOff(local->dev);
891                     DeviceOn(local->dev);
892                 }
893             }
894         }
895         return;
896     } else {
897         if (!avail) {
898             /* If the device wasn't available last time we checked */
899             xf86Msg(X_INFO, "%s: device %s is available again\n", local->name, xf86FindOptionValue(local->options, "Device"));
900             priv->flags |= AVAIL_FLAG;
901         }
902     }
903
904     for (event = (struct input_event *)eventbuf;
905             event < (struct input_event *)(eventbuf+len); event++) {
906
907         switch (event->type) {
908             case EV_SYN: /* 2.6.x */
909                 if (event->code != SYN_REPORT)
910                     xf86Msg(X_ERROR, "%s: unknown EV_SYN code %d\n", local->name, event->code);
911                 break;
912             case EV_ABS:
913                 switch (event->code) {
914                     case ABS_X:
915                         x = event->value;
916                         break;
917
918                     case ABS_Y:
919                         y = event->value;
920                         break;
921
922                     case ABS_PRESSURE:
923                         z = event->value;
924                         break;
925
926                     case ABS_MISC:
927                         break;
928
929                 }
930                 break; /* EV_ABS */
931
932             case EV_KEY:
933                 switch (event->code) {
934                     case BTN_TOOL_PEN:
935                         prox = event->value;
936                         break;
937
938                     case BTN_TOUCH:
939                         buttons = set_bit(buttons,0,event->value);
940                         break;
941
942                     case BTN_STYLUS:
943                         buttons = set_bit(buttons,1,event->value);
944                         break;
945
946                     case BTN_STYLUS2:
947                         buttons = set_bit(buttons,2,event->value);
948                         break;
949                 }
950                 break; /* EV_KEY */
951             default:
952                 xf86Msg(X_ERROR, "%s: unknown event type/code %d/%d\n", local->name, event->type, event->code);
953         } /* switch event->type */
954
955         /* Linux Kernel 2.6.x sends EV_SYN/SYN_REPORT as an event terminator,
956          * whereas 2.4.x sends EV_ABS/ABS_MISC. We have to support both.
957          */
958         if (!(  (event->type == EV_SYN && event->code == SYN_REPORT) ||
959                     (event->type == EV_ABS && event->code == ABS_MISC)
960              )) {
961             continue;
962         }
963
964         if (prox)
965         {
966 #if XORG_BOTCHED_INPUT
967             ConvertProc(local, 0, 3, x, y, 0, 0, 0, 0, &report_x, &report_y);
968 #else
969             report_x = x;
970             report_y = y;
971 #endif
972             if (!(priv->acecadOldProximity))
973                 if (!is_core_pointer)
974                 {
975                     xf86PostProximityEvent(local->dev, 1, 0, 3 , report_x, report_y, z);
976                 }
977
978
979             xf86PostMotionEvent(local->dev, 1, 0, 3, report_x, report_y, z);
980
981             if (priv->acecadOldButtons != buttons)
982             {
983                 int delta = buttons ^ priv->acecadOldButtons;
984                 while (delta)
985                 {
986                     int id = ffs(delta);
987                     delta &= ~(1 << (id-1));
988
989                     xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, report_x, report_y, z);
990                 }
991             }
992         }
993         else
994         {
995             if (!is_core_pointer)
996                 if (priv->acecadOldProximity)
997                 {
998                     xf86PostProximityEvent(local->dev, 0, 0, 3, report_x, report_y, z);
999                 }
1000             priv->acecadOldProximity = 0;
1001         }
1002
1003         priv->acecadOldButtons = buttons;
1004         priv->acecadOldX = x;
1005         priv->acecadOldY = y;
1006         priv->acecadOldZ = z;
1007         priv->acecadOldProximity = prox;
1008     }
1009     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
1010 }
1011 #endif
1012
1013 static void
1014 CloseProc (InputInfoPtr local)
1015 {
1016 }
1017
1018 /*
1019  * The ConvertProc function may need to be tailored for your device.
1020  * This function converts the device's valuator outputs to x and y coordinates
1021  * to simulate mouse events.
1022  */
1023 static Bool
1024 ConvertProc (InputInfoPtr local, int first, int num,
1025         int v0, int v1, int v2, int v3, int v4, int v5,
1026         int *x, int *y)
1027 {
1028     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1029
1030     /* TODO: should have a structure to hold which screen the
1031      * pointer is attached to? */
1032     // xf86Msg(X_INFO, "%s: coordinate conversion in : %d, %d\n", local->name, v0, v1);
1033     *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
1034     *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
1035     // xf86Msg(X_INFO, "%s: coordinate conversion out: %d, %d\n", local->name, *x, *y);
1036     return TRUE;
1037 }
1038
1039
1040 static Bool
1041 ReverseConvertProc (InputInfoPtr local,
1042         int x, int  y,
1043         int *valuators)
1044 {
1045     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1046
1047     // xf86Msg(X_INFO, "%s: reverse coordinate conversion in : %d, %d\n", local->name, x, y);
1048     valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
1049     valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
1050     // xf86Msg(X_INFO, "%s: reverse coordinate conversion out: %d, %d\n", local->name, valuators[0], valuators[1]);
1051
1052     return TRUE;
1053 }
1054
1055
1056 #define WriteString(str)\
1057     XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
1058
1059
1060 static Bool
1061 QueryHardware (AceCadPrivatePtr priv)
1062 {
1063
1064     /* Reset */
1065     WriteString("z0");
1066
1067     /* Wait */
1068     milisleep (250);
1069
1070     /* Prompt Mode in order to not be disturbed */
1071     WriteString(ACECAD_PROMPT_MODE);
1072
1073     /* Flush */
1074     while (XisbRead(priv->buffer) >= 0);
1075
1076     /* Ask for Config packet*/
1077     WriteString(ACECAD_CONFIG);
1078
1079     /* Read the packet */
1080     XisbBlockDuration (priv->buffer, 1000000);
1081     NewPacket (priv);
1082
1083     /*xf86Msg(X_CONFIG, "ACECAD Tablet init envoyé \n");*/
1084
1085     if ((AceCadGetPacket (priv) == Success))
1086     {
1087         priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
1088         priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
1089         priv->acecadMaxZ = 512;
1090         xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d\n", priv->acecadMaxX, priv->acecadMaxY);
1091     }
1092     else
1093         return !Success;
1094
1095     /*xf86Msg(X_INFO, "ACECAD Tablet query hardware fini \n");*/
1096     return Success;
1097 }
1098
1099 #define BITS_PER_LONG (sizeof(long) * 8)
1100 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
1101 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
1102 #define OFF(x)  ((x)%BITS_PER_LONG)
1103 #define LONG(x) ((x)/BITS_PER_LONG)
1104
1105 #ifdef HAVE_LINUX_INPUT_H
1106 static Bool
1107 USBQueryHardware (InputInfoPtr local)
1108 {
1109     AceCadPrivatePtr    priv = (AceCadPrivatePtr) local->private;
1110     unsigned long       bit[EV_MAX][NBITS(KEY_MAX)];
1111     int                 i, j;
1112     int                 abs[5];
1113     char                name[256] = "Unknown";
1114
1115     ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
1116     xf86MsgVerb(X_PROBED, 4, "Kernel Input device name: \"%s\"\n", name);
1117
1118     memset(bit, 0, sizeof(bit));
1119     ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
1120
1121     for (i = 0; i < EV_MAX; i++)
1122         if (test_bit(i, bit[0])) {
1123             ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
1124             for (j = 0; j < KEY_MAX; j++)
1125                 if (test_bit(j, bit[i])) {
1126                     if (i == EV_ABS) {
1127                         ioctl(local->fd, EVIOCGABS(j), abs);
1128                         switch (j) {
1129                             case ABS_X:
1130                                 priv->acecadMaxX = abs[2];
1131                                 break;
1132
1133                             case ABS_Y:
1134                                 priv->acecadMaxY = abs[2];
1135                                 break;
1136
1137                             case ABS_PRESSURE:
1138                                 priv->acecadMaxZ = abs[2];
1139                                 break;
1140                         }
1141                     }
1142                 }
1143         }
1144
1145     xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY, priv->acecadMaxZ);
1146     return Success;
1147 }
1148 #endif
1149
1150 static void
1151 NewPacket (AceCadPrivatePtr priv)
1152 {
1153     priv->packeti = 0;
1154 }
1155
1156 static Bool
1157 AceCadGetPacket (AceCadPrivatePtr priv)
1158 {
1159     int count = 0;
1160     int c = 0;
1161
1162     while((c = XisbRead(priv->buffer)) >= 0 )
1163     {
1164
1165         /*
1166          * fail after 500 bytes so the server doesn't hang forever if a
1167          * device sends bad data.
1168          */
1169         if (count++ > 500)
1170         {
1171             NewPacket (priv);
1172             return !Success;
1173         }
1174
1175         if (c & PHASING_BIT)
1176         {
1177             NewPacket(priv);
1178
1179             /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
1180             XisbBlockDuration (priv->buffer, 10000);
1181             priv->packet[priv->packeti++] = c;
1182             count = ACECAD_PACKET_SIZE - 1;
1183             while (count-- && (c = XisbRead(priv->buffer)) >= 0)
1184             {
1185                 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
1186                 priv->packet[priv->packeti++] = c;
1187             }
1188             XisbBlockDuration (priv->buffer, 0);
1189             if(c > 0)
1190                 return Success;
1191         }
1192     }
1193     return !Success;
1194 }