unifdef XFree86LOADER, this isn't optional anymore anyway.
[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 #include "config.h"
28
29 #include <xorgVersion.h>
30 #define XORG_VERSION_BOTCHED XORG_VERSION_NUMERIC(1,4,0,0,0)
31 #if XORG_VERSION_CURRENT >= XORG_VERSION_BOTCHED
32 #define XORG_BOTCHED_INPUT 1
33 #else
34 #define XORG_BOTCHED_INPUT 0
35 #endif
36
37 /*****************************************************************************
38  *      Standard Headers
39  ****************************************************************************/
40
41 #ifdef LINUX_INPUT
42 #include <asm/types.h>
43 #include <linux/input.h>
44 #ifndef EV_SYN
45 #define EV_SYN EV_RST
46 #define SYN_REPORT 0
47 #endif
48 #ifdef BUS_PCI
49 #undef BUS_PCI
50 #endif
51 #ifdef BUS_ISA
52 #undef BUS_ISA
53 #endif
54 #endif
55
56 #include <misc.h>
57 #include <xf86.h>
58 #ifndef NEED_XF86_TYPES
59 #define NEED_XF86_TYPES
60 #endif
61 #include <xf86_OSproc.h>
62 #include <xisb.h>
63 #include <xf86Xinput.h>
64 #include <exevents.h>
65 #include <xf86Module.h>
66
67 #include <string.h>
68 #include <stdio.h>
69
70 #include <errno.h>
71 #ifdef LINUX_INPUT
72 #include <fcntl.h>
73 #ifdef LINUX_SYSFS
74 #include <sysfs/libsysfs.h>
75 #include <dlfcn.h>
76 #endif
77 #endif
78
79 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
80 #include <X11/Xatom.h>
81 #include <xserver-properties.h>
82 #endif
83
84 /* Previously found in xf86Xinput.h */
85 #ifdef DBG
86 #undef DBG
87 #endif
88 #define DBG(lvl, f) {if ((lvl) <= xf86GetVerbosity()) f;}
89
90 /*****************************************************************************
91  *      Local Headers
92  ****************************************************************************/
93 #include "acecad.h"
94
95 /*****************************************************************************
96  *      Variables without includable headers
97  ****************************************************************************/
98
99 /*****************************************************************************
100  *      Local Variables
101  ****************************************************************************/
102
103 #undef read
104 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
105
106 /* max number of input events to read in one read call */
107 #define MAX_EVENTS 50
108
109 _X_EXPORT InputDriverRec ACECAD =
110 {
111         1,
112         "acecad",
113         NULL,
114         AceCadPreInit,
115         NULL,
116         NULL,
117         0
118 };
119
120 static XF86ModuleVersionInfo VersionRec =
121 {
122         "acecad",
123         MODULEVENDORSTRING,
124         MODINFOSTRING1,
125         MODINFOSTRING2,
126         XORG_VERSION_CURRENT,
127         PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
128         ABI_CLASS_XINPUT,
129         ABI_XINPUT_VERSION,
130         MOD_CLASS_XINPUT,
131         {0, 0, 0, 0}
132 };
133
134
135 _X_EXPORT XF86ModuleData acecadModuleData = {
136         &VersionRec,
137         SetupProc,
138         TearDownProc
139 };
140
141 /*****************************************************************************
142  *      Function Definitions
143  ****************************************************************************/
144
145 static pointer
146 SetupProc(      pointer module,
147                 pointer options,
148                 int *errmaj,
149                 int *errmin )
150 {
151         xf86AddInputDriver(&ACECAD, module, 0);
152         return module;
153 }
154
155 static void
156 TearDownProc( pointer p )
157 {
158 #if 0
159         LocalDevicePtr local = (LocalDevicePtr) p;
160         AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
161
162         DeviceOff (local->dev);
163
164         xf86CloseSerial (local->fd);
165         XisbFree (priv->buffer);
166         xfree (priv);
167         xfree (local->name);
168         xfree (local);
169 #endif
170 }
171
172 static const char *default_options[] =
173 {
174         "BaudRate", "9600",
175         "StopBits", "1",
176         "DataBits", "8",
177         "Parity", "Odd",
178         "Vmin", "1",
179         "Vtime", "10",
180         "FlowControl", "Xoff",
181         NULL
182 };
183
184 #ifdef LINUX_INPUT
185 static int
186 IsUSBLine(int fd)
187 {
188     int version;
189     int err;
190
191     SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
192
193     if (!err) {
194         xf86MsgVerb(X_PROBED, 4, "Kernel Input driver version is %d.%d.%d\n",
195                 version >> 16, (version >> 8) & 0xff, version & 0xff);
196         return 1;
197     } else {
198         xf86MsgVerb(X_PROBED, 4, "No Kernel Input driver found\n");
199         return 0;
200     }
201 }
202
203 /* Heavily inspired by synaptics/eventcomm.c */
204
205 #define DEV_INPUT_EVENT "/dev/input/event"
206 #define EV_DEV_NAME_MAXLEN 64
207 #define SET_EVENT_NUM(str, num) \
208     snprintf(str, EV_DEV_NAME_MAXLEN, "%s%d", DEV_INPUT_EVENT, num)
209
210 static Bool
211 fd_query_acecad(int fd, char *ace_name) {
212     char name[256] = "Unknown";
213     int cmp_at = strlen(ace_name);
214     if (cmp_at > 255)
215         cmp_at = 255;
216     ioctl(fd, EVIOCGNAME(sizeof(name)), name);
217     name[cmp_at] = '\0';
218     if (xf86NameCmp(name, ace_name) == 0)
219         return TRUE;
220     return FALSE;
221 }
222
223 static char ace_name_default[7] = "acecad";
224
225 #ifdef LINUX_SYSFS
226 static char usb_bus_name[4] = "usb";
227 static char acecad_driver_name[11] = "usb_acecad";
228 #endif
229
230 static Bool
231 AceCadAutoDevProbe(LocalDevicePtr local, int verb)
232 {
233     /* We are trying to find the right eventX device */
234     int i = 0;
235     Bool have_evdev = FALSE;
236     int noent_cnt = 0;
237     const int max_skip = 10;
238     char *ace_name = xf86FindOptionValue(local->options, "Name");
239     char fname[EV_DEV_NAME_MAXLEN];
240     int np;
241
242 #ifdef LINUX_SYSFS
243     struct sysfs_bus *usb_bus = NULL;
244     struct sysfs_driver *acecad_driver = NULL;
245     struct sysfs_device *candidate = NULL;
246     char *link = NULL;
247     struct dlist *devs = NULL;
248     struct dlist *links = NULL;
249
250     xf86MsgVerb(X_INFO, verb, "%s: querying sysfs for Acecad tablets\n", local->name);
251     usb_bus = sysfs_open_bus(usb_bus_name);
252     if (usb_bus) {
253         xf86MsgVerb(X_PROBED, 4, "%s: usb bus opened\n", local->name);
254         acecad_driver = sysfs_get_bus_driver(usb_bus, acecad_driver_name);
255         if (acecad_driver) {
256             xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad driver opened\n", local->name);
257             devs = sysfs_get_driver_devices(acecad_driver);
258             if (devs) {
259                 xf86MsgVerb(X_PROBED, 4, "%s: usb_acecad devices retrieved\n", local->name);
260                 dlist_for_each_data(devs, candidate, struct sysfs_device) {
261                     xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s\n", local->name, candidate->name, candidate->path);
262                     links = sysfs_open_link_list(candidate->path);
263                     dlist_for_each_data(links, link, char) {
264                         if (sscanf(link, "input:event%d", &i) == 1) {
265                             xf86MsgVerb(X_PROBED, 4, "%s: device %s at %s: %s\n", local->name, candidate->name, candidate->path, link);
266                             break;
267                         }
268                     }
269                     sysfs_close_list(links);
270                     if (i > 0) /* We found something */
271                         break;
272                 }
273             } else
274                 xf86MsgVerb(X_WARNING, 4, "%s: no usb_acecad devices found\n", local->name);
275         } else
276             xf86MsgVerb(X_WARNING, 4, "%s: usb_acecad driver not found\n", local->name);
277     } else
278         xf86MsgVerb(X_WARNING, 4, "%s: usb bus not found\n", local->name);
279     sysfs_close_bus(usb_bus);
280
281     if (i > 0) {
282         /* We found something */
283         np = SET_EVENT_NUM(fname, i);
284         if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
285             xf86MsgVerb(X_WARNING, verb, "%s: unable to manage event device %d\n", local->name, i);
286         } else {
287             goto ProbeFound;
288         }
289     } else
290         xf86MsgVerb(X_WARNING, verb, "%s: no Acecad devices found via sysfs\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[] = {0, 1, 2, 3};
632 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
633     Atom btn_labels[3];
634     Atom axes_labels[3];
635
636     btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
637     btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
638     btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
639
640     if ((priv->flags & ABSOLUTE_FLAG))
641     {
642         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
643         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
644         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
645     } else
646     {
647         axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
648         axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
649         axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Z);
650     }
651 #endif
652
653     xf86MsgVerb(X_INFO, 4, "%s Init\n", local->name);
654
655     /* 3 boutons */
656     if (InitButtonClassDeviceStruct (dev, 3,
657 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
658                 btn_labels,
659 #endif
660                 map) == FALSE)
661     {
662         xf86Msg(X_ERROR, "%s: unable to allocate ButtonClassDeviceStruct\n", local->name);
663         return !Success;
664     }
665
666     if (InitFocusClassDeviceStruct (dev) == FALSE)
667     {
668         xf86Msg(X_ERROR, "%s: unable to allocate FocusClassDeviceStruct\n", local->name);
669         return !Success;
670     }
671
672     if (InitPtrFeedbackClassDeviceStruct(dev, ControlProc) == FALSE) {
673         xf86Msg(X_ERROR, "%s: unable to init ptr feedback\n", local->name);
674         return !Success;
675     }
676
677
678     /* 3 axes */
679     if (InitValuatorClassDeviceStruct (dev, 3,
680 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
681                 axes_labels,
682 #endif
683 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
684                 xf86GetMotionEvents,
685 #endif
686                 local->history_size,
687                 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
688             == FALSE)
689     {
690         xf86Msg(X_ERROR, "%s: unable to allocate ValuatorClassDeviceStruct\n", local->name);
691         return !Success;
692     }
693     else
694     {
695
696         InitValuatorAxisStruct(dev,
697                 0,
698 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
699                 axes_labels[0],
700 #endif
701                 0,                      /* min val */
702 #if XORG_BOTCHED_INPUT
703                 screenInfo.screens[0]->width,
704 #else
705                 priv->acecadMaxX,       /* max val */
706 #endif
707                 1000,                   /* resolution */
708                 0,                      /* min_res */
709                 1000);                  /* max_res */
710         InitValuatorAxisStruct(dev,
711                 1,
712 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
713                 axes_labels[1],
714 #endif
715                 0,                      /* min val */
716 #if XORG_BOTCHED_INPUT
717                 screenInfo.screens[0]->height,
718 #else
719                 priv->acecadMaxY,       /* max val */
720 #endif
721                 1000,                   /* resolution */
722                 0,                      /* min_res */
723                 1000);                  /* max_res */
724         InitValuatorAxisStruct(dev,
725                 2,
726 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
727                 axes_labels[2],
728 #endif
729                 0,                      /* min val */
730                 priv->acecadMaxZ,       /* max val */
731                 1000,                   /* resolution */
732                 0,                      /* min_res */
733                 1000);          /* max_res */
734
735     }
736
737     if (InitProximityClassDeviceStruct (dev) == FALSE)
738     {
739         xf86Msg(X_ERROR, "%s: unable to allocate ProximityClassDeviceStruct\n", local->name);
740         return !Success;
741     }
742
743     xf86MotionHistoryAllocate (local);
744
745
746     /* On ne peut pas calculer l'increment avant, faute d'ecran pour
747        connaitre la taille... */
748
749     if (priv->acecadInc > 95)
750         priv->acecadInc = 95;
751     if (priv->acecadInc < 1)
752     {
753         /* guess the best increment value given video mode */
754         rx = priv->acecadMaxX / screenInfo.screens[0]->width;
755         ry = priv->acecadMaxY / screenInfo.screens[0]->height;
756         if (rx < ry)
757             priv->acecadInc = rx;
758         else
759             priv->acecadInc = ry;
760         if (priv->acecadInc < 1)
761             priv->acecadInc = 1;
762     }
763
764     xf86Msg(X_INFO, "%s Increment: %d\n", local->name, priv->acecadInc);
765
766     return Success;
767 }
768
769 static void
770 ReadInput (LocalDevicePtr local)
771 {
772     int x, y, z;
773     int prox, buttons;
774     int is_core_pointer = 0, is_absolute;
775     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
776
777     /*xf86Msg(X_INFO, "ACECAD Tablet Read Input\n");*/
778
779     is_absolute = (priv->flags & ABSOLUTE_FLAG);
780 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
781     is_core_pointer = xf86IsCorePointer(local->dev);
782 #endif
783
784     /*
785      * set blocking to -1 on the first call because we know there is data to
786      * read. Xisb automatically clears it after one successful read so that
787      * succeeding reads are preceeded buy a select with a 0 timeout to prevent
788      * read from blocking indefinately.
789      */
790     XisbBlockDuration (priv->buffer, -1);
791
792     while (AceCadGetPacket (priv) == Success)
793     {
794         x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
795         y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
796
797         if (!(priv->flags & ABSOLUTE_FLAG))
798         {
799             x = priv->packet[0] & XSIGN_BIT? x:-x;
800             y = priv->packet[0] & YSIGN_BIT? y:-y;
801         }
802         else
803         {
804             y = priv->acecadMaxY - y ;
805         }
806
807
808         z = ((int)priv->packet[5] << 2) |
809             (((int)priv->packet[6] & 0x01) << 1) |
810             (((int)priv->packet[6] & 0x10) >> 4);
811
812         buttons = ((int)priv->packet[0] & 0x07) |
813             ((int)priv->packet[6] & 0x02 << 2);
814
815         prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
816
817         if (prox)
818         {
819             if (!(priv->acecadOldProximity))
820                 if (!is_core_pointer)
821                 {
822                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxIN %d %d %d\n",x, y, z);*/
823                     xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
824                 }
825
826             if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
827                     || (!is_absolute && (x || y)))
828             {
829                 if (is_absolute || priv->acecadOldProximity)
830                 {
831                     /*xf86Msg(X_INFO, "ACECAD Tablet Motion %d %d %d\n", x, y, z);*/
832                     xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
833                 }
834             }
835
836             if (priv->acecadOldButtons != buttons)
837             {
838                 int delta = buttons ^ priv->acecadOldButtons;
839                 while (delta)
840                 {
841                     int id = ffs(delta);
842                     delta &= ~(1 << (id-1));
843
844                     /*xf86Msg(X_INFO, "ACECAD Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
845                     xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
846                 }
847             }
848
849             priv->acecadOldButtons = buttons;
850             priv->acecadOldX = x;
851             priv->acecadOldY = y;
852             priv->acecadOldZ = z;
853             priv->acecadOldProximity = prox;
854         }
855         else
856         {
857             if (!is_core_pointer)
858                 if (priv->acecadOldProximity)
859                 {
860                     /*xf86Msg(X_INFO, "ACECAD Tablet ProxOUT %d %d %d\n",x, y, z);*/
861                     xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
862                 }
863             priv->acecadOldProximity = 0;
864         }
865     }
866     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
867 }
868
869 #ifdef LINUX_INPUT
870 #define set_bit(byte,nb,bit)    (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
871 static void
872 USBReadInput (LocalDevicePtr local)
873 {
874     int len;
875     struct input_event * event;
876     char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
877     AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
878     int x = priv->acecadOldX;
879     int y = priv->acecadOldY;
880     int z = priv->acecadOldZ;
881     int report_x, report_y;
882     int prox = priv->acecadOldProximity;
883     int buttons = priv->acecadOldButtons;
884     int is_core_pointer = 0;
885 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) == 0
886     is_core_pointer = xf86IsCorePointer(local->dev);
887 #endif
888     /* Is autodev active? */
889     int autodev = priv->flags & AUTODEV_FLAG;
890     /* Was the device available last time we checked? */
891     int avail = priv->flags & AVAIL_FLAG;
892
893     SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
894
895     if (len <= 0) {
896         if (avail) {
897             xf86Msg(X_ERROR, "%s: error reading device %s: %s\n", local->name, xf86FindOptionValue(local->options, "Device"), strerror(errno));
898         }
899         if (NOTAVAIL) {
900             priv->flags &= ~AVAIL_FLAG;
901             if(autodev) {
902                 if (AceCadAutoDevProbe(local, 4)) {
903                     DeviceOff(local->dev);
904                     DeviceOn(local->dev);
905                 }
906             }
907         }
908         return;
909     } else {
910         if (!avail) {
911             /* If the device wasn't available last time we checked */
912             xf86Msg(X_INFO, "%s: device %s is available again\n", local->name, xf86FindOptionValue(local->options, "Device"));
913             priv->flags |= AVAIL_FLAG;
914         }
915     }
916
917     for (event = (struct input_event *)eventbuf;
918             event < (struct input_event *)(eventbuf+len); event++) {
919
920         switch (event->type) {
921             case EV_SYN: /* 2.6.x */
922                 if (event->code != SYN_REPORT)
923                     xf86Msg(X_ERROR, "%s: unknown EV_SYN code %d\n", local->name, event->code);
924                 break;
925             case EV_ABS:
926                 switch (event->code) {
927                     case ABS_X:
928                         x = event->value;
929                         break;
930
931                     case ABS_Y:
932                         y = event->value;
933                         break;
934
935                     case ABS_PRESSURE:
936                         z = event->value;
937                         break;
938
939                     case ABS_MISC:
940                         break;
941
942                 }
943                 break; /* EV_ABS */
944
945             case EV_KEY:
946                 switch (event->code) {
947                     case BTN_TOOL_PEN:
948                         prox = event->value;
949                         break;
950
951                     case BTN_TOUCH:
952                         buttons = set_bit(buttons,0,event->value);
953                         break;
954
955                     case BTN_STYLUS:
956                         buttons = set_bit(buttons,1,event->value);
957                         break;
958
959                     case BTN_STYLUS2:
960                         buttons = set_bit(buttons,2,event->value);
961                         break;
962                 }
963                 break; /* EV_KEY */
964             default:
965                 xf86Msg(X_ERROR, "%s: unknown event type/code %d/%d\n", local->name, event->type, event->code);
966         } /* switch event->type */
967
968         /* Linux Kernel 2.6.x sends EV_SYN/SYN_REPORT as an event terminator,
969          * whereas 2.4.x sends EV_ABS/ABS_MISC. We have to support both.
970          */
971         if (!(  (event->type == EV_SYN && event->code == SYN_REPORT) ||
972                     (event->type == EV_ABS && event->code == ABS_MISC)
973              )) {
974             continue;
975         }
976
977         if (prox)
978         {
979 #if XORG_BOTCHED_INPUT
980             ConvertProc(local, 0, 3, x, y, 0, 0, 0, 0, &report_x, &report_y);
981 #else
982             report_x = x;
983             report_y = y;
984 #endif
985             if (!(priv->acecadOldProximity))
986                 if (!is_core_pointer)
987                 {
988                     xf86PostProximityEvent(local->dev, 1, 0, 3 , report_x, report_y, z);
989                 }
990
991
992             xf86PostMotionEvent(local->dev, 1, 0, 3, report_x, report_y, z);
993
994             if (priv->acecadOldButtons != buttons)
995             {
996                 int delta = buttons ^ priv->acecadOldButtons;
997                 while (delta)
998                 {
999                     int id = ffs(delta);
1000                     delta &= ~(1 << (id-1));
1001
1002                     xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, report_x, report_y, z);
1003                 }
1004             }
1005         }
1006         else
1007         {
1008             if (!is_core_pointer)
1009                 if (priv->acecadOldProximity)
1010                 {
1011                     xf86PostProximityEvent(local->dev, 0, 0, 3, report_x, report_y, z);
1012                 }
1013             priv->acecadOldProximity = 0;
1014         }
1015
1016         priv->acecadOldButtons = buttons;
1017         priv->acecadOldX = x;
1018         priv->acecadOldY = y;
1019         priv->acecadOldZ = z;
1020         priv->acecadOldProximity = prox;
1021     }
1022     /*xf86Msg(X_INFO, "ACECAD Tablet Sortie Read Input\n");*/
1023 }
1024 #endif
1025
1026 static void
1027 CloseProc (LocalDevicePtr local)
1028 {
1029 }
1030
1031 /*
1032  * The ConvertProc function may need to be tailored for your device.
1033  * This function converts the device's valuator outputs to x and y coordinates
1034  * to simulate mouse events.
1035  */
1036 static Bool
1037 ConvertProc (LocalDevicePtr local, int first, int num,
1038         int v0, int v1, int v2, int v3, int v4, int v5,
1039         int *x, int *y)
1040 {
1041     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1042
1043     /* TODO: should have a structure to hold which screen the
1044      * pointer is attached to? */
1045     // xf86Msg(X_INFO, "%s: coordinate conversion in : %d, %d\n", local->name, v0, v1);
1046     *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
1047     *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
1048     // xf86Msg(X_INFO, "%s: coordinate conversion out: %d, %d\n", local->name, *x, *y);
1049     return TRUE;
1050 }
1051
1052
1053 static Bool
1054 ReverseConvertProc (LocalDevicePtr local,
1055         int x, int  y,
1056         int *valuators)
1057 {
1058     AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
1059
1060     // xf86Msg(X_INFO, "%s: reverse coordinate conversion in : %d, %d\n", local->name, x, y);
1061     valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
1062     valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
1063     // xf86Msg(X_INFO, "%s: reverse coordinate conversion out: %d, %d\n", local->name, valuators[0], valuators[1]);
1064
1065     return TRUE;
1066 }
1067
1068
1069 #define WriteString(str)\
1070     XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
1071
1072
1073 static Bool
1074 QueryHardware (AceCadPrivatePtr priv)
1075 {
1076
1077     /* Reset */
1078     WriteString("z0");
1079
1080     /* Wait */
1081     milisleep (250);
1082
1083     /* Prompt Mode in order to not be disturbed */
1084     WriteString(ACECAD_PROMPT_MODE);
1085
1086     /* Flush */
1087     while (XisbRead(priv->buffer) >= 0);
1088
1089     /* Ask for Config packet*/
1090     WriteString(ACECAD_CONFIG);
1091
1092     /* Read the packet */
1093     XisbBlockDuration (priv->buffer, 1000000);
1094     NewPacket (priv);
1095
1096     /*xf86Msg(X_CONFIG, "ACECAD Tablet init envoyé \n");*/
1097
1098     if ((AceCadGetPacket (priv) == Success))
1099     {
1100         priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
1101         priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
1102         priv->acecadMaxZ = 512;
1103         xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d\n", priv->acecadMaxX, priv->acecadMaxY);
1104     }
1105     else
1106         return !Success;
1107
1108     /*xf86Msg(X_INFO, "ACECAD Tablet query hardware fini \n");*/
1109     return Success;
1110 }
1111
1112 #define BITS_PER_LONG (sizeof(long) * 8)
1113 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
1114 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
1115 #define OFF(x)  ((x)%BITS_PER_LONG)
1116 #define LONG(x) ((x)/BITS_PER_LONG)
1117
1118 #ifdef LINUX_INPUT
1119 static Bool
1120 USBQueryHardware (LocalDevicePtr local)
1121 {
1122     AceCadPrivatePtr    priv = (AceCadPrivatePtr) local->private;
1123     unsigned long       bit[EV_MAX][NBITS(KEY_MAX)];
1124     int                 i, j;
1125     int                 abs[5];
1126     char                name[256] = "Unknown";
1127
1128     ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
1129     xf86MsgVerb(X_PROBED, 4, "Kernel Input device name: \"%s\"\n", name);
1130
1131     memset(bit, 0, sizeof(bit));
1132     ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
1133
1134     for (i = 0; i < EV_MAX; i++)
1135         if (test_bit(i, bit[0])) {
1136             ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
1137             for (j = 0; j < KEY_MAX; j++)
1138                 if (test_bit(j, bit[i])) {
1139                     if (i == EV_ABS) {
1140                         ioctl(local->fd, EVIOCGABS(j), abs);
1141                         switch (j) {
1142                             case ABS_X:
1143                                 priv->acecadMaxX = abs[2];
1144                                 break;
1145
1146                             case ABS_Y:
1147                                 priv->acecadMaxY = abs[2];
1148                                 break;
1149
1150                             case ABS_PRESSURE:
1151                                 priv->acecadMaxZ = abs[2];
1152                                 break;
1153                         }
1154                     }
1155                 }
1156         }
1157
1158     xf86Msg(X_PROBED, "ACECAD Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY, priv->acecadMaxZ);
1159     return Success;
1160 }
1161 #endif
1162
1163 static void
1164 NewPacket (AceCadPrivatePtr priv)
1165 {
1166     priv->packeti = 0;
1167 }
1168
1169 static Bool
1170 AceCadGetPacket (AceCadPrivatePtr priv)
1171 {
1172     int count = 0;
1173     int c = 0;
1174
1175     while((c = XisbRead(priv->buffer)) >= 0 )
1176     {
1177
1178         /*
1179          * fail after 500 bytes so the server doesn't hang forever if a
1180          * device sends bad data.
1181          */
1182         if (count++ > 500)
1183         {
1184             NewPacket (priv);
1185             return !Success;
1186         }
1187
1188         if (c & PHASING_BIT)
1189         {
1190             NewPacket(priv);
1191
1192             /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
1193             XisbBlockDuration (priv->buffer, 10000);
1194             priv->packet[priv->packeti++] = c;
1195             count = ACECAD_PACKET_SIZE - 1;
1196             while (count-- && (c = XisbRead(priv->buffer)) >= 0)
1197             {
1198                 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
1199                 priv->packet[priv->packeti++] = c;
1200             }
1201             XisbBlockDuration (priv->buffer, 0);
1202             if(c > 0)
1203                 return Success;
1204         }
1205     }
1206     return !Success;
1207 }