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 #ifndef NEED_XF86_TYPES
51 #define NEED_XF86_TYPES
53 #include <xf86_OSproc.h>
55 #include <xf86Xinput.h>
57 #include <xf86Module.h>
66 /*****************************************************************************
68 ****************************************************************************/
71 /*****************************************************************************
72 * Variables without includable headers
73 ****************************************************************************/
75 /*****************************************************************************
77 ****************************************************************************/
80 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
82 /* max number of input events to read in one read call */
85 _X_EXPORT InputDriverRec ACECAD =
97 static XF86ModuleVersionInfo VersionRec =
103 XORG_VERSION_CURRENT,
112 _X_EXPORT XF86ModuleData acecadModuleData = {
118 /*****************************************************************************
119 * Function Definitions
120 ****************************************************************************/
123 SetupProc( pointer module,
128 xf86AddInputDriver(&ACECAD, module, 0);
133 TearDownProc( pointer p )
136 LocalDevicePtr local = (LocalDevicePtr) p;
137 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
139 DeviceOff (local->dev);
141 xf86CloseSerial (local->fd);
142 XisbFree (priv->buffer);
150 static const char *default_options[] =
158 "FlowControl", "Xoff",
169 SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
172 xf86Msg(X_PROBED, "Kernel Input driver version is %d.%d.%d\n",
173 version >> 16, (version >> 8) & 0xff, version & 0xff);
176 xf86Msg(X_PROBED, "No Kernel Input driver found\n");
183 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
185 LocalDevicePtr local = xf86AllocateInput(drv, 0);
186 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
191 if ((!local) || (!priv))
194 memset(priv,0,sizeof (AceCadPrivateRec));
196 local->name = dev->identifier;
197 local->type_name = "AceCad Tablet";
198 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
199 local->motion_history_proc = xf86GetMotionEvents;
200 local->control_proc = NULL;
201 local->close_proc = CloseProc;
202 local->switch_mode = NULL;
203 local->conversion_proc = ConvertProc;
204 local->reverse_conversion_proc = ReverseConvertProc;
206 local->private = priv;
207 local->private_flags = 0;
208 local->conf_idev = dev;
209 local->device_control = DeviceControl;
210 /*local->always_core_feedback = 0;*/
212 xf86CollectInputOptions(local, default_options, NULL);
214 xf86OptionListReport(local->options);
216 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
218 local->fd = xf86OpenSerial (local->options);
221 xf86Msg(X_ERROR, "AceCad driver unable to open device\n");
224 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
227 if(IsUSBLine(local->fd)){
230 local->read_input = USBReadInput;
232 if (USBQueryHardware(local) != Success)
234 ErrorF ("Unable to query/initialize AceCad hardware.\n");
242 local->read_input = ReadInput;
245 if (xf86FindOptionValue(local->options, "ReportSpeed")) {
247 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
255 priv->acecadReportSpeed = 'Q';
258 priv->acecadReportSpeed = 'R';
261 priv->acecadReportSpeed = 'S';
264 priv->acecadReportSpeed = 'T';
267 priv->acecadReportSpeed = 'R';
269 xf86Msg(X_ERROR, "Acecad Tablet: ReportSpeed value %d invalid. Possible values: 120, 85, 10, 2. Defaulting to 85\n", speed);
273 xf86Msg(msgtype, "Acecad Tablet report %d points/s\n", speed);
275 priv->buffer = XisbNew (local->fd, 200);
278 * Verify that hardware is attached and fuctional
280 if (QueryHardware(priv) != Success)
282 xf86Msg(X_ERROR, "Unable to query/initialize AceCad hardware.\n");
287 s = xf86FindOptionValue(local->options, "Mode");
288 msgtype = s ? X_CONFIG : X_DEFAULT;
289 if (s && (xf86NameCmp(s, "Relative") == 0))
291 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
295 priv->flags = priv->flags | ABSOLUTE_FLAG;
298 xf86Msg(msgtype, "Acecad Tablet is in %s mode\n", (priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
299 DBG (9, XisbTrace (priv->buffer, 1));
301 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
303 xf86ProcessCommonOptions(local, local->options);
305 local->flags |= XI86_CONFIGURED;
309 RemoveEnabledDevice (local->fd);
312 XisbFree(priv->buffer);
315 xf86CloseSerial(local->fd);
317 RemoveEnabledDevice (local->fd);
322 * If something went wrong, cleanup and return NULL
325 if ((local) && (local->fd))
326 xf86CloseSerial (local->fd);
327 if ((priv) && (priv->buffer))
328 XisbFree (priv->buffer);
335 DeviceControl (DeviceIntPtr dev, int mode)
346 RetValue = DeviceOn( dev );
349 RetValue = DeviceOff( dev );
352 RetValue = DeviceClose( dev );
362 DeviceOn (DeviceIntPtr dev)
365 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
366 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
368 xf86Msg(X_INFO, "Acecad Tablet Device On\n");
370 local->fd = xf86OpenSerial(local->options);
373 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
378 if (priv->acecadUSB==0){
379 priv->buffer = XisbNew(local->fd, 200);
382 xf86CloseSerial(local->fd);
387 /*Rets qu'a l'envoyer a la tablette */
388 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed, ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
389 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
392 xf86FlushInput(local->fd);
393 xf86AddEnabledDevice (local);
394 dev->public.on = TRUE;
399 DeviceOff (DeviceIntPtr dev)
401 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
402 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
407 RemoveEnabledDevice (local->fd);
410 XisbFree(priv->buffer);
413 xf86CloseSerial(local->fd);
417 xf86RemoveEnabledDevice (local);
418 dev->public.on = FALSE;
423 DeviceClose (DeviceIntPtr dev)
425 xf86Msg(X_INFO, "Acecad Tablet Device Close\n");
430 ControlProc(DeviceIntPtr device,
433 xf86Msg(X_INFO, "Acecad Tablet Control Proc\n");
437 DeviceInit (DeviceIntPtr dev)
440 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
441 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
442 unsigned char map[] =
445 xf86Msg(X_INFO, "Acecad Tablet Device Init\n");
448 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
450 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
454 if (InitFocusClassDeviceStruct (dev) == FALSE)
456 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
460 if (InitPtrFeedbackClassDeviceStruct(dev,
461 ControlProc) == FALSE) {
462 ErrorF("unable to init ptr feedback\n");
468 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
470 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
473 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
479 InitValuatorAxisStruct(dev,
482 priv->acecadMaxX, /* max val */
483 1000, /* resolution */
486 InitValuatorAxisStruct(dev,
489 priv->acecadMaxY, /* max val */
490 1000, /* resolution */
493 InitValuatorAxisStruct(dev,
496 priv->acecadMaxZ, /* max val */
497 1000, /* resolution */
503 if (InitProximityClassDeviceStruct (dev) == FALSE)
505 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
509 xf86MotionHistoryAllocate (local);
512 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
513 connaitre la taille... */
515 if (priv->acecadInc > 95)
516 priv->acecadInc = 95;
517 if (priv->acecadInc < 1)
519 /* guess the best increment value given video mode */
520 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
521 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
523 priv->acecadInc = rx;
525 priv->acecadInc = ry;
526 if (priv->acecadInc < 1)
530 xf86Msg(X_INFO, "Acecad Tablet Increment: %d\n", priv->acecadInc);
536 ReadInput (LocalDevicePtr local)
540 int is_core_pointer, is_absolute;
541 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
543 /*xf86Msg(X_INFO, "Acecad Tablet Read Input\n");*/
545 is_absolute = (priv->flags & ABSOLUTE_FLAG);
546 is_core_pointer = xf86IsCorePointer(local->dev);
549 * set blocking to -1 on the first call because we know there is data to
550 * read. Xisb automatically clears it after one successful read so that
551 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
552 * read from blocking indefinately.
554 XisbBlockDuration (priv->buffer, -1);
556 while (AceCadGetPacket (priv) == Success)
558 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
559 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
561 if (!(priv->flags & ABSOLUTE_FLAG))
563 x = priv->packet[0] & XSIGN_BIT? x:-x;
564 y = priv->packet[0] & YSIGN_BIT? y:-y;
568 y = priv->acecadMaxY - y ;
572 z = ((int)priv->packet[5] << 2) |
573 (((int)priv->packet[6] & 0x01) << 1) |
574 (((int)priv->packet[6] & 0x10) >> 4);
576 buttons = ((int)priv->packet[0] & 0x07) |
577 ((int)priv->packet[6] & 0x02 << 2);
579 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
583 if (!(priv->acecadOldProximity))
584 if (!is_core_pointer)
586 /*xf86Msg(X_INFO, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
587 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
590 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
591 || (!is_absolute && (x || y)))
593 if (is_absolute || priv->acecadOldProximity)
595 /*xf86Msg(X_INFO, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
596 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
600 if (priv->acecadOldButtons != buttons)
604 delta = buttons ^ priv->acecadOldButtons;
610 delta &= ~(1 << (id-1));
612 /*xf86Msg(X_INFO, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
613 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
617 priv->acecadOldButtons = buttons;
618 priv->acecadOldX = x;
619 priv->acecadOldY = y;
620 priv->acecadOldZ = z;
621 priv->acecadOldProximity = prox;
625 if (!is_core_pointer)
626 if (priv->acecadOldProximity)
628 /*xf86Msg(X_INFO, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
629 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
631 priv->acecadOldProximity = 0;
634 /*xf86Msg(X_INFO, "Acecad Tablet Sortie Read Input\n");*/
638 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
640 USBReadInput (LocalDevicePtr local)
643 struct input_event * event;
644 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
645 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
646 int x = priv->acecadOldX;
647 int y = priv->acecadOldY;
648 int z = priv->acecadOldZ;
649 int prox = priv->acecadOldProximity;
650 int buttons = priv->acecadOldButtons;
653 is_core_pointer = xf86IsCorePointer(local->dev);
655 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
658 ErrorF("Error reading wacom device : %s\n", strerror(errno));
662 for (event=(struct input_event *)eventbuf;
663 event<(struct input_event *)(eventbuf+len); event++) {
665 switch (event->type) {
667 switch (event->code) {
687 switch (event->code) {
693 buttons=set_bit(buttons,0,event->value);
697 buttons=set_bit(buttons,1,event->value);
701 buttons=set_bit(buttons,2,event->value);
706 xf86Msg(X_ERROR, "UNKNOWN event type/code=%d/%d\n", event->type, event->code);
707 } /* switch event->type */
709 /* ABS_MISC is the event terminator */
710 if (event->type != EV_ABS || event->code != ABS_MISC) {
716 if (!(priv->acecadOldProximity))
717 if (!is_core_pointer)
719 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
723 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
725 if (priv->acecadOldButtons != buttons)
729 delta = buttons ^ priv->acecadOldButtons;
735 delta &= ~(1 << (id-1));
737 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
743 if (!is_core_pointer)
744 if (priv->acecadOldProximity)
746 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
748 priv->acecadOldProximity = 0;
751 priv->acecadOldButtons = buttons;
752 priv->acecadOldX = x;
753 priv->acecadOldY = y;
754 priv->acecadOldZ = z;
755 priv->acecadOldProximity = prox;
757 /*xf86Msg(X_INFO, "Acecad Tablet Sortie Read Input\n");*/
762 CloseProc (LocalDevicePtr local)
767 * The ConvertProc function may need to be tailored for your device.
768 * This function converts the device's valuator outputs to x and y coordinates
769 * to simulate mouse events.
772 ConvertProc (LocalDevicePtr local,
784 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
786 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
787 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
793 ReverseConvertProc( LocalDevicePtr local,
798 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
800 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
801 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
807 #define WriteString(str)\
808 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
812 QueryHardware (AceCadPrivatePtr priv)
821 /* Prompt Mode in order to not be disturbed */
822 WriteString(ACECAD_PROMPT_MODE);
825 while(XisbRead(priv->buffer)>=0);
827 /* Ask for Config packet*/
828 WriteString(ACECAD_CONFIG);
830 /* Read the packet */
831 XisbBlockDuration (priv->buffer, 1000000);
834 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
836 if ((AceCadGetPacket (priv) == Success))
838 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
839 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
840 priv->acecadMaxZ = 512;
841 xf86Msg(X_INFO, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
846 /*xf86Msg(X_INFO, "Acecad Tablet query hardware fini \n");*/
850 #define BITS_PER_LONG (sizeof(long) * 8)
851 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
852 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
853 #define OFF(x) ((x)%BITS_PER_LONG)
854 #define LONG(x) ((x)/BITS_PER_LONG)
858 USBQueryHardware (LocalDevicePtr local)
860 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
861 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
864 char name[256] = "Unknown";
866 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
867 xf86Msg(X_PROBED, "Kernel Input device name: \"%s\"\n", name);
869 memset(bit, 0, sizeof(bit));
870 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
872 for (i = 0; i < EV_MAX; i++)
873 if (test_bit(i, bit[0])) {
874 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
875 for (j = 0; j < KEY_MAX; j++)
876 if (test_bit(j, bit[i])) {
878 ioctl(local->fd, EVIOCGABS(j), abs);
881 priv->acecadMaxX = abs[2];
885 priv->acecadMaxY = abs[2];
889 priv->acecadMaxZ = abs[2];
896 xf86Msg(X_PROBED, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n", priv->acecadMaxX, priv->acecadMaxY,priv->acecadMaxZ);
902 NewPacket (AceCadPrivatePtr priv)
908 AceCadGetPacket (AceCadPrivatePtr priv)
913 while((c = XisbRead(priv->buffer))>=0 )
917 * fail after 500 bytes so the server doesn't hang forever if a
918 * device sends bad data.
930 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
931 XisbBlockDuration (priv->buffer, 10000);
932 priv->packet[priv->packeti++] = c;
933 count=ACECAD_PACKET_SIZE-1;
934 while(count-- && (c = XisbRead(priv->buffer))>=0)
936 /*xf86Msg(X_INFO, "Push %2.2x\n",(char) c);*/
937 priv->packet[priv->packeti++] = c;
939 XisbBlockDuration (priv->buffer, 0);