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