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.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 /* $XFree86: xc/programs/Xserver/hw/xfree86/input/acecad/acecad.c,v 1.4 2003/10/30 00:40:45 dawes Exp $ */
29 /*****************************************************************************
31 ****************************************************************************/
34 #include <asm/types.h>
35 #include <linux/input.h>
46 #define NEED_XF86_TYPES
47 #include <xf86_ansic.h>
48 #include <xf86_OSproc.h>
50 #include <xf86Xinput.h>
52 #include <xf86Module.h>
54 /*****************************************************************************
56 ****************************************************************************/
59 /*****************************************************************************
60 * Variables without includable headers
61 ****************************************************************************/
63 /*****************************************************************************
65 ****************************************************************************/
67 #define SYSCALL(call) while(((call) == -1) && (errno == EINTR))
69 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
71 /* max number of input events to read in one read call */
75 _X_EXPORT InputDriverRec ACECAD =
87 static XF86ModuleVersionInfo VersionRec =
102 _X_EXPORT XF86ModuleData acecadModuleData = {
108 /*****************************************************************************
109 * Function Definitions
110 ****************************************************************************/
113 SetupProc( pointer module,
118 xf86AddInputDriver(&ACECAD, module, 0);
123 TearDownProc( pointer p )
126 LocalDevicePtr local = (LocalDevicePtr) p;
127 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
129 DeviceOff (local->dev);
131 xf86CloseSerial (local->fd);
132 XisbFree (priv->buffer);
140 static const char *default_options[] =
148 "FlowControl", "Xoff",
159 SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
162 xf86Msg(X_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
163 version >> 16, (version >> 8) & 0xff, version & 0xff);
172 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
174 LocalDevicePtr local = xf86AllocateInput(drv, 0);
175 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
179 if ((!local) || (!priv))
182 memset(priv,0,sizeof (AceCadPrivateRec));
184 local->name = dev->identifier;
185 local->type_name = "AceCad Tablet";
186 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
187 local->motion_history_proc = xf86GetMotionEvents;
188 local->control_proc = NULL;
189 local->close_proc = CloseProc;
190 local->switch_mode = NULL;
191 local->conversion_proc = ConvertProc;
192 local->reverse_conversion_proc = ReverseConvertProc;
194 local->private = priv;
195 local->private_flags = 0;
196 local->conf_idev = dev;
197 local->device_control = DeviceControl;
198 /*local->always_core_feedback = 0;*/
200 xf86CollectInputOptions(local, default_options, NULL);
202 xf86OptionListReport(local->options);
204 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
206 local->fd = xf86OpenSerial (local->options);
209 xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
212 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
215 if(IsUSBLine(local->fd)){
218 local->read_input = USBReadInput;
220 if (USBQueryHardware(local) != Success)
222 ErrorF ("Unable to query/initialize AceCad hardware.\n");
230 local->read_input = ReadInput;
232 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
237 priv->acecadReportSpeed = 'Q';
240 priv->acecadReportSpeed = 'R';
243 priv->acecadReportSpeed = 'S';
246 priv->acecadReportSpeed = 'T';
249 priv->acecadReportSpeed = 'R';
251 xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");
254 xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);
256 priv->buffer = XisbNew (local->fd, 200);
259 * Verify that hardware is attached and fuctional
261 if (QueryHardware(priv) != Success)
263 xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
268 s = xf86FindOptionValue(local->options, "Mode");
269 if (s && (xf86NameCmp(s, "Relative") == 0))
271 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
275 priv->flags = priv->flags | ABSOLUTE_FLAG;
278 xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
279 DBG (9, XisbTrace (priv->buffer, 1));
281 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
283 xf86ProcessCommonOptions(local, local->options);
285 local->flags |= XI86_CONFIGURED;
289 RemoveEnabledDevice (local->fd);
292 XisbFree(priv->buffer);
295 xf86CloseSerial(local->fd);
297 RemoveEnabledDevice (local->fd);
302 * If something went wrong, cleanup and return NULL
305 if ((local) && (local->fd))
306 xf86CloseSerial (local->fd);
307 if ((priv) && (priv->buffer))
308 XisbFree (priv->buffer);
315 DeviceControl (DeviceIntPtr dev, int mode)
326 RetValue = DeviceOn( dev );
329 RetValue = DeviceOff( dev );
332 RetValue = DeviceClose( dev );
342 DeviceOn (DeviceIntPtr dev)
345 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
346 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
348 xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
350 local->fd = xf86OpenSerial(local->options);
353 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
358 if (priv->acecadUSB==0){
359 priv->buffer = XisbNew(local->fd, 200);
362 xf86CloseSerial(local->fd);
367 /*Rets qu'a l'envoyer a la tablette */
368 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
369 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
372 xf86FlushInput(local->fd);
373 xf86AddEnabledDevice (local);
374 dev->public.on = TRUE;
379 DeviceOff (DeviceIntPtr dev)
381 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
382 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
387 RemoveEnabledDevice (local->fd);
390 XisbFree(priv->buffer);
393 xf86CloseSerial(local->fd);
397 xf86RemoveEnabledDevice (local);
398 dev->public.on = FALSE;
403 DeviceClose (DeviceIntPtr dev)
405 xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
410 ControlProc(DeviceIntPtr device,
413 xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
417 DeviceInit (DeviceIntPtr dev)
420 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
421 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
422 unsigned char map[] =
425 xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
428 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
430 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
434 if (InitFocusClassDeviceStruct (dev) == FALSE)
436 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
440 if (InitPtrFeedbackClassDeviceStruct(dev,
441 ControlProc) == FALSE) {
442 ErrorF("unable to init ptr feedback\n");
448 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
450 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
453 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
459 InitValuatorAxisStruct(dev,
462 priv->acecadMaxX, /* max val */
463 1000, /* resolution */
466 InitValuatorAxisStruct(dev,
469 priv->acecadMaxY, /* max val */
470 1000, /* resolution */
473 InitValuatorAxisStruct(dev,
476 priv->acecadMaxZ, /* max val */
477 1000, /* resolution */
483 if (InitProximityClassDeviceStruct (dev) == FALSE)
485 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
489 xf86MotionHistoryAllocate (local);
492 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
493 connaitre la taille... */
495 if (priv->acecadInc > 95)
496 priv->acecadInc = 95;
497 if (priv->acecadInc < 1)
499 /* guess the best increment value given video mode */
500 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
501 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
503 priv->acecadInc = rx;
505 priv->acecadInc = ry;
506 if (priv->acecadInc < 1)
510 xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
516 ReadInput (LocalDevicePtr local)
520 int is_core_pointer, is_absolute;
521 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
523 /*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
525 is_absolute = (priv->flags & ABSOLUTE_FLAG);
526 is_core_pointer = xf86IsCorePointer(local->dev);
529 * set blocking to -1 on the first call because we know there is data to
530 * read. Xisb automatically clears it after one successful read so that
531 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
532 * read from blocking indefinately.
534 XisbBlockDuration (priv->buffer, -1);
536 while (AceCadGetPacket (priv) == Success)
538 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
539 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
541 if (!(priv->flags & ABSOLUTE_FLAG))
543 x = priv->packet[0] & XSIGN_BIT? x:-x;
544 y = priv->packet[0] & YSIGN_BIT? y:-y;
548 y = priv->acecadMaxY - y ;
552 z = ((int)priv->packet[5] << 2) |
553 (((int)priv->packet[6] & 0x01) << 1) |
554 (((int)priv->packet[6] & 0x10) >> 4);
556 buttons = ((int)priv->packet[0] & 0x07) |
557 ((int)priv->packet[6] & 0x02 << 2);
559 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
563 if (!(priv->acecadOldProximity))
564 if (!is_core_pointer)
566 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
567 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
570 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
571 || (!is_absolute && (x || y)))
573 if (is_absolute || priv->acecadOldProximity)
575 /*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
576 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
580 if (priv->acecadOldButtons != buttons)
584 delta = buttons ^ priv->acecadOldButtons;
590 delta &= ~(1 << (id-1));
592 /*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
593 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
597 priv->acecadOldButtons = buttons;
598 priv->acecadOldX = x;
599 priv->acecadOldY = y;
600 priv->acecadOldZ = z;
601 priv->acecadOldProximity = prox;
605 if (!is_core_pointer)
606 if (priv->acecadOldProximity)
608 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
609 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
611 priv->acecadOldProximity = 0;
614 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
618 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
620 USBReadInput (LocalDevicePtr local)
623 struct input_event * event;
624 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
625 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
626 int x = priv->acecadOldX;
627 int y = priv->acecadOldY;
628 int z = priv->acecadOldZ;
629 int prox = priv->acecadOldProximity;
630 int buttons = priv->acecadOldButtons;
633 is_core_pointer = xf86IsCorePointer(local->dev);
635 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
638 ErrorF("Error reading wacom device : %s\n", strerror(errno));
642 for (event=(struct input_event *)eventbuf;
643 event<(struct input_event *)(eventbuf+len); event++) {
645 switch (event->type) {
647 switch (event->code) {
667 switch (event->code) {
673 buttons=set_bit(buttons,0,event->value);
677 buttons=set_bit(buttons,1,event->value);
681 buttons=set_bit(buttons,2,event->value);
686 xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
687 } /* switch event->type */
689 /* ABS_MISC is the event terminator */
690 if (event->type != EV_ABS || event->code != ABS_MISC) {
696 if (!(priv->acecadOldProximity))
697 if (!is_core_pointer)
699 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
703 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
705 if (priv->acecadOldButtons != buttons)
709 delta = buttons ^ priv->acecadOldButtons;
715 delta &= ~(1 << (id-1));
717 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
723 if (!is_core_pointer)
724 if (priv->acecadOldProximity)
726 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
728 priv->acecadOldProximity = 0;
731 priv->acecadOldButtons = buttons;
732 priv->acecadOldX = x;
733 priv->acecadOldY = y;
734 priv->acecadOldZ = z;
735 priv->acecadOldProximity = prox;
737 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
742 CloseProc (LocalDevicePtr local)
747 * The ConvertProc function may need to be tailored for your device.
748 * This function converts the device's valuator outputs to x and y coordinates
749 * to simulate mouse events.
752 ConvertProc (LocalDevicePtr local,
764 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
766 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
767 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
773 ReverseConvertProc( LocalDevicePtr local,
778 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
780 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
781 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
787 #define WriteString(str)\
788 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
792 QueryHardware (AceCadPrivatePtr priv)
801 /* Prompt Mode in order to not be disturbed */
802 WriteString(ACECAD_PROMPT_MODE);
805 while(XisbRead(priv->buffer)>=0);
807 /* Ask for Config packet*/
808 WriteString(ACECAD_CONFIG);
810 /* Read the packet */
811 XisbBlockDuration (priv->buffer, 1000000);
814 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
816 if ((AceCadGetPacket (priv) == Success))
818 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
819 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
820 priv->acecadMaxZ = 512;
821 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
826 /*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
830 #define BITS_PER_LONG (sizeof(long) * 8)
831 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
832 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
833 #define OFF(x) ((x)%BITS_PER_LONG)
834 #define LONG(x) ((x)/BITS_PER_LONG)
838 USBQueryHardware (LocalDevicePtr local)
840 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
841 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
844 char name[256] = "Unknown";
846 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
847 xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
849 memset(bit, 0, sizeof(bit));
850 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
852 for (i = 0; i < EV_MAX; i++)
853 if (test_bit(i, bit[0])) {
854 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
855 for (j = 0; j < KEY_MAX; j++)
856 if (test_bit(j, bit[i])) {
858 ioctl(local->fd, EVIOCGABS(j), abs);
861 priv->acecadMaxX = abs[2];
865 priv->acecadMaxY = abs[2];
869 priv->acecadMaxZ = abs[2];
876 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
882 NewPacket (AceCadPrivatePtr priv)
888 AceCadGetPacket (AceCadPrivatePtr priv)
893 while((c = XisbRead(priv->buffer))>=0 )
897 * fail after 500 bytes so the server doesn't hang forever if a
898 * device sends bad data.
910 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
911 XisbBlockDuration (priv->buffer, 10000);
912 priv->packet[priv->packeti++] = c;
913 count=ACECAD_PACKET_SIZE-1;
914 while(count-- && (c = XisbRead(priv->buffer))>=0)
916 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
917 priv->packet[priv->packeti++] = c;
919 XisbBlockDuration (priv->buffer, 0);