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