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