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