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