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