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