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 $ */
33 /*****************************************************************************
35 ****************************************************************************/
38 #include <asm/types.h>
39 #include <linux/input.h>
50 #define NEED_XF86_TYPES
51 #include <xf86_OSproc.h>
53 #include <xf86Xinput.h>
55 #include <xf86Module.h>
60 /*****************************************************************************
62 ****************************************************************************/
65 /*****************************************************************************
66 * Variables without includable headers
67 ****************************************************************************/
69 /*****************************************************************************
71 ****************************************************************************/
73 #define SYSCALL(call) while(((call) == -1) && (errno == EINTR))
75 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
77 /* max number of input events to read in one read call */
80 _X_EXPORT InputDriverRec ACECAD =
92 static XF86ModuleVersionInfo VersionRec =
107 _X_EXPORT XF86ModuleData acecadModuleData = {
113 /*****************************************************************************
114 * Function Definitions
115 ****************************************************************************/
118 SetupProc( pointer module,
123 xf86AddInputDriver(&ACECAD, module, 0);
128 TearDownProc( pointer p )
131 LocalDevicePtr local = (LocalDevicePtr) p;
132 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
134 DeviceOff (local->dev);
136 xf86CloseSerial (local->fd);
137 XisbFree (priv->buffer);
145 static const char *default_options[] =
153 "FlowControl", "Xoff",
164 SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
167 xf86Msg(X_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
168 version >> 16, (version >> 8) & 0xff, version & 0xff);
177 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
179 LocalDevicePtr local = xf86AllocateInput(drv, 0);
180 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
184 if ((!local) || (!priv))
187 memset(priv,0,sizeof (AceCadPrivateRec));
189 local->name = dev->identifier;
190 local->type_name = "AceCad Tablet";
191 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
192 local->motion_history_proc = xf86GetMotionEvents;
193 local->control_proc = NULL;
194 local->close_proc = CloseProc;
195 local->switch_mode = NULL;
196 local->conversion_proc = ConvertProc;
197 local->reverse_conversion_proc = ReverseConvertProc;
199 local->private = priv;
200 local->private_flags = 0;
201 local->conf_idev = dev;
202 local->device_control = DeviceControl;
203 /*local->always_core_feedback = 0;*/
205 xf86CollectInputOptions(local, default_options, NULL);
207 xf86OptionListReport(local->options);
209 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
211 local->fd = xf86OpenSerial (local->options);
214 xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
217 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
220 if(IsUSBLine(local->fd)){
223 local->read_input = USBReadInput;
225 if (USBQueryHardware(local) != Success)
227 ErrorF ("Unable to query/initialize AceCad hardware.\n");
235 local->read_input = ReadInput;
237 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
242 priv->acecadReportSpeed = 'Q';
245 priv->acecadReportSpeed = 'R';
248 priv->acecadReportSpeed = 'S';
251 priv->acecadReportSpeed = 'T';
254 priv->acecadReportSpeed = 'R';
256 xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");
259 xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);
261 priv->buffer = XisbNew (local->fd, 200);
264 * Verify that hardware is attached and fuctional
266 if (QueryHardware(priv) != Success)
268 xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
273 s = xf86FindOptionValue(local->options, "Mode");
274 if (s && (xf86NameCmp(s, "Relative") == 0))
276 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
280 priv->flags = priv->flags | ABSOLUTE_FLAG;
283 xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
284 DBG (9, XisbTrace (priv->buffer, 1));
286 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
288 xf86ProcessCommonOptions(local, local->options);
290 local->flags |= XI86_CONFIGURED;
294 RemoveEnabledDevice (local->fd);
297 XisbFree(priv->buffer);
300 xf86CloseSerial(local->fd);
302 RemoveEnabledDevice (local->fd);
307 * If something went wrong, cleanup and return NULL
310 if ((local) && (local->fd))
311 xf86CloseSerial (local->fd);
312 if ((priv) && (priv->buffer))
313 XisbFree (priv->buffer);
320 DeviceControl (DeviceIntPtr dev, int mode)
331 RetValue = DeviceOn( dev );
334 RetValue = DeviceOff( dev );
337 RetValue = DeviceClose( dev );
347 DeviceOn (DeviceIntPtr dev)
350 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
351 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
353 xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
355 local->fd = xf86OpenSerial(local->options);
358 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
363 if (priv->acecadUSB==0){
364 priv->buffer = XisbNew(local->fd, 200);
367 xf86CloseSerial(local->fd);
372 /*Rets qu'a l'envoyer a la tablette */
373 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
374 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
377 xf86FlushInput(local->fd);
378 xf86AddEnabledDevice (local);
379 dev->public.on = TRUE;
384 DeviceOff (DeviceIntPtr dev)
386 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
387 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
392 RemoveEnabledDevice (local->fd);
395 XisbFree(priv->buffer);
398 xf86CloseSerial(local->fd);
402 xf86RemoveEnabledDevice (local);
403 dev->public.on = FALSE;
408 DeviceClose (DeviceIntPtr dev)
410 xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
415 ControlProc(DeviceIntPtr device,
418 xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
422 DeviceInit (DeviceIntPtr dev)
425 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
426 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
427 unsigned char map[] =
430 xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
433 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
435 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
439 if (InitFocusClassDeviceStruct (dev) == FALSE)
441 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
445 if (InitPtrFeedbackClassDeviceStruct(dev,
446 ControlProc) == FALSE) {
447 ErrorF("unable to init ptr feedback\n");
453 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
455 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
458 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
464 InitValuatorAxisStruct(dev,
467 priv->acecadMaxX, /* max val */
468 1000, /* resolution */
471 InitValuatorAxisStruct(dev,
474 priv->acecadMaxY, /* max val */
475 1000, /* resolution */
478 InitValuatorAxisStruct(dev,
481 priv->acecadMaxZ, /* max val */
482 1000, /* resolution */
488 if (InitProximityClassDeviceStruct (dev) == FALSE)
490 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
494 xf86MotionHistoryAllocate (local);
497 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
498 connaitre la taille... */
500 if (priv->acecadInc > 95)
501 priv->acecadInc = 95;
502 if (priv->acecadInc < 1)
504 /* guess the best increment value given video mode */
505 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
506 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
508 priv->acecadInc = rx;
510 priv->acecadInc = ry;
511 if (priv->acecadInc < 1)
515 xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
521 ReadInput (LocalDevicePtr local)
525 int is_core_pointer, is_absolute;
526 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
528 /*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
530 is_absolute = (priv->flags & ABSOLUTE_FLAG);
531 is_core_pointer = xf86IsCorePointer(local->dev);
534 * set blocking to -1 on the first call because we know there is data to
535 * read. Xisb automatically clears it after one successful read so that
536 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
537 * read from blocking indefinately.
539 XisbBlockDuration (priv->buffer, -1);
541 while (AceCadGetPacket (priv) == Success)
543 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
544 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
546 if (!(priv->flags & ABSOLUTE_FLAG))
548 x = priv->packet[0] & XSIGN_BIT? x:-x;
549 y = priv->packet[0] & YSIGN_BIT? y:-y;
553 y = priv->acecadMaxY - y ;
557 z = ((int)priv->packet[5] << 2) |
558 (((int)priv->packet[6] & 0x01) << 1) |
559 (((int)priv->packet[6] & 0x10) >> 4);
561 buttons = ((int)priv->packet[0] & 0x07) |
562 ((int)priv->packet[6] & 0x02 << 2);
564 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
568 if (!(priv->acecadOldProximity))
569 if (!is_core_pointer)
571 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
572 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
575 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
576 || (!is_absolute && (x || y)))
578 if (is_absolute || priv->acecadOldProximity)
580 /*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
581 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
585 if (priv->acecadOldButtons != buttons)
589 delta = buttons ^ priv->acecadOldButtons;
595 delta &= ~(1 << (id-1));
597 /*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
598 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
602 priv->acecadOldButtons = buttons;
603 priv->acecadOldX = x;
604 priv->acecadOldY = y;
605 priv->acecadOldZ = z;
606 priv->acecadOldProximity = prox;
610 if (!is_core_pointer)
611 if (priv->acecadOldProximity)
613 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
614 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
616 priv->acecadOldProximity = 0;
619 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
623 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
625 USBReadInput (LocalDevicePtr local)
628 struct input_event * event;
629 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
630 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
631 int x = priv->acecadOldX;
632 int y = priv->acecadOldY;
633 int z = priv->acecadOldZ;
634 int prox = priv->acecadOldProximity;
635 int buttons = priv->acecadOldButtons;
638 is_core_pointer = xf86IsCorePointer(local->dev);
640 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
643 ErrorF("Error reading wacom device : %s\n", strerror(errno));
647 for (event=(struct input_event *)eventbuf;
648 event<(struct input_event *)(eventbuf+len); event++) {
650 switch (event->type) {
652 switch (event->code) {
672 switch (event->code) {
678 buttons=set_bit(buttons,0,event->value);
682 buttons=set_bit(buttons,1,event->value);
686 buttons=set_bit(buttons,2,event->value);
691 xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
692 } /* switch event->type */
694 /* ABS_MISC is the event terminator */
695 if (event->type != EV_ABS || event->code != ABS_MISC) {
701 if (!(priv->acecadOldProximity))
702 if (!is_core_pointer)
704 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
708 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
710 if (priv->acecadOldButtons != buttons)
714 delta = buttons ^ priv->acecadOldButtons;
720 delta &= ~(1 << (id-1));
722 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
728 if (!is_core_pointer)
729 if (priv->acecadOldProximity)
731 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
733 priv->acecadOldProximity = 0;
736 priv->acecadOldButtons = buttons;
737 priv->acecadOldX = x;
738 priv->acecadOldY = y;
739 priv->acecadOldZ = z;
740 priv->acecadOldProximity = prox;
742 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
747 CloseProc (LocalDevicePtr local)
752 * The ConvertProc function may need to be tailored for your device.
753 * This function converts the device's valuator outputs to x and y coordinates
754 * to simulate mouse events.
757 ConvertProc (LocalDevicePtr local,
769 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
771 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
772 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
778 ReverseConvertProc( LocalDevicePtr local,
783 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
785 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
786 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
792 #define WriteString(str)\
793 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
797 QueryHardware (AceCadPrivatePtr priv)
806 /* Prompt Mode in order to not be disturbed */
807 WriteString(ACECAD_PROMPT_MODE);
810 while(XisbRead(priv->buffer)>=0);
812 /* Ask for Config packet*/
813 WriteString(ACECAD_CONFIG);
815 /* Read the packet */
816 XisbBlockDuration (priv->buffer, 1000000);
819 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
821 if ((AceCadGetPacket (priv) == Success))
823 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
824 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
825 priv->acecadMaxZ = 512;
826 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
831 /*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
835 #define BITS_PER_LONG (sizeof(long) * 8)
836 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
837 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
838 #define OFF(x) ((x)%BITS_PER_LONG)
839 #define LONG(x) ((x)/BITS_PER_LONG)
843 USBQueryHardware (LocalDevicePtr local)
845 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
846 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
849 char name[256] = "Unknown";
851 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
852 xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
854 memset(bit, 0, sizeof(bit));
855 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
857 for (i = 0; i < EV_MAX; i++)
858 if (test_bit(i, bit[0])) {
859 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
860 for (j = 0; j < KEY_MAX; j++)
861 if (test_bit(j, bit[i])) {
863 ioctl(local->fd, EVIOCGABS(j), abs);
866 priv->acecadMaxX = abs[2];
870 priv->acecadMaxY = abs[2];
874 priv->acecadMaxZ = abs[2];
881 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
887 NewPacket (AceCadPrivatePtr priv)
893 AceCadGetPacket (AceCadPrivatePtr priv)
898 while((c = XisbRead(priv->buffer))>=0 )
902 * fail after 500 bytes so the server doesn't hang forever if a
903 * device sends bad data.
915 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
916 XisbBlockDuration (priv->buffer, 10000);
917 priv->packet[priv->packeti++] = c;
918 count=ACECAD_PACKET_SIZE-1;
919 while(count-- && (c = XisbRead(priv->buffer))>=0)
921 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
922 priv->packet[priv->packeti++] = c;
924 XisbBlockDuration (priv->buffer, 0);