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_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
173 version >> 16, (version >> 8) & 0xff, version & 0xff);
182 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
184 LocalDevicePtr local = xf86AllocateInput(drv, 0);
185 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
189 if ((!local) || (!priv))
192 memset(priv,0,sizeof (AceCadPrivateRec));
194 local->name = dev->identifier;
195 local->type_name = "AceCad Tablet";
196 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
197 local->motion_history_proc = xf86GetMotionEvents;
198 local->control_proc = NULL;
199 local->close_proc = CloseProc;
200 local->switch_mode = NULL;
201 local->conversion_proc = ConvertProc;
202 local->reverse_conversion_proc = ReverseConvertProc;
204 local->private = priv;
205 local->private_flags = 0;
206 local->conf_idev = dev;
207 local->device_control = DeviceControl;
208 /*local->always_core_feedback = 0;*/
210 xf86CollectInputOptions(local, default_options, NULL);
212 xf86OptionListReport(local->options);
214 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
216 local->fd = xf86OpenSerial (local->options);
219 xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
222 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
225 if(IsUSBLine(local->fd)){
228 local->read_input = USBReadInput;
230 if (USBQueryHardware(local) != Success)
232 ErrorF ("Unable to query/initialize AceCad hardware.\n");
240 local->read_input = ReadInput;
242 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
247 priv->acecadReportSpeed = 'Q';
250 priv->acecadReportSpeed = 'R';
253 priv->acecadReportSpeed = 'S';
256 priv->acecadReportSpeed = 'T';
259 priv->acecadReportSpeed = 'R';
261 xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");
264 xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);
266 priv->buffer = XisbNew (local->fd, 200);
269 * Verify that hardware is attached and fuctional
271 if (QueryHardware(priv) != Success)
273 xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
278 s = xf86FindOptionValue(local->options, "Mode");
279 if (s && (xf86NameCmp(s, "Relative") == 0))
281 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
285 priv->flags = priv->flags | ABSOLUTE_FLAG;
288 xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
289 DBG (9, XisbTrace (priv->buffer, 1));
291 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
293 xf86ProcessCommonOptions(local, local->options);
295 local->flags |= XI86_CONFIGURED;
299 RemoveEnabledDevice (local->fd);
302 XisbFree(priv->buffer);
305 xf86CloseSerial(local->fd);
307 RemoveEnabledDevice (local->fd);
312 * If something went wrong, cleanup and return NULL
315 if ((local) && (local->fd))
316 xf86CloseSerial (local->fd);
317 if ((priv) && (priv->buffer))
318 XisbFree (priv->buffer);
325 DeviceControl (DeviceIntPtr dev, int mode)
336 RetValue = DeviceOn( dev );
339 RetValue = DeviceOff( dev );
342 RetValue = DeviceClose( dev );
352 DeviceOn (DeviceIntPtr dev)
355 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
356 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
358 xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
360 local->fd = xf86OpenSerial(local->options);
363 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
368 if (priv->acecadUSB==0){
369 priv->buffer = XisbNew(local->fd, 200);
372 xf86CloseSerial(local->fd);
377 /*Rets qu'a l'envoyer a la tablette */
378 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
379 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
382 xf86FlushInput(local->fd);
383 xf86AddEnabledDevice (local);
384 dev->public.on = TRUE;
389 DeviceOff (DeviceIntPtr dev)
391 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
392 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
397 RemoveEnabledDevice (local->fd);
400 XisbFree(priv->buffer);
403 xf86CloseSerial(local->fd);
407 xf86RemoveEnabledDevice (local);
408 dev->public.on = FALSE;
413 DeviceClose (DeviceIntPtr dev)
415 xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
420 ControlProc(DeviceIntPtr device,
423 xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
427 DeviceInit (DeviceIntPtr dev)
430 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
431 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
432 unsigned char map[] =
435 xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
438 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
440 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
444 if (InitFocusClassDeviceStruct (dev) == FALSE)
446 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
450 if (InitPtrFeedbackClassDeviceStruct(dev,
451 ControlProc) == FALSE) {
452 ErrorF("unable to init ptr feedback\n");
458 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
460 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
463 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
469 InitValuatorAxisStruct(dev,
472 priv->acecadMaxX, /* max val */
473 1000, /* resolution */
476 InitValuatorAxisStruct(dev,
479 priv->acecadMaxY, /* max val */
480 1000, /* resolution */
483 InitValuatorAxisStruct(dev,
486 priv->acecadMaxZ, /* max val */
487 1000, /* resolution */
493 if (InitProximityClassDeviceStruct (dev) == FALSE)
495 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
499 xf86MotionHistoryAllocate (local);
502 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
503 connaitre la taille... */
505 if (priv->acecadInc > 95)
506 priv->acecadInc = 95;
507 if (priv->acecadInc < 1)
509 /* guess the best increment value given video mode */
510 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
511 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
513 priv->acecadInc = rx;
515 priv->acecadInc = ry;
516 if (priv->acecadInc < 1)
520 xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
526 ReadInput (LocalDevicePtr local)
530 int is_core_pointer, is_absolute;
531 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
533 /*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
535 is_absolute = (priv->flags & ABSOLUTE_FLAG);
536 is_core_pointer = xf86IsCorePointer(local->dev);
539 * set blocking to -1 on the first call because we know there is data to
540 * read. Xisb automatically clears it after one successful read so that
541 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
542 * read from blocking indefinately.
544 XisbBlockDuration (priv->buffer, -1);
546 while (AceCadGetPacket (priv) == Success)
548 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
549 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
551 if (!(priv->flags & ABSOLUTE_FLAG))
553 x = priv->packet[0] & XSIGN_BIT? x:-x;
554 y = priv->packet[0] & YSIGN_BIT? y:-y;
558 y = priv->acecadMaxY - y ;
562 z = ((int)priv->packet[5] << 2) |
563 (((int)priv->packet[6] & 0x01) << 1) |
564 (((int)priv->packet[6] & 0x10) >> 4);
566 buttons = ((int)priv->packet[0] & 0x07) |
567 ((int)priv->packet[6] & 0x02 << 2);
569 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
573 if (!(priv->acecadOldProximity))
574 if (!is_core_pointer)
576 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
577 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
580 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
581 || (!is_absolute && (x || y)))
583 if (is_absolute || priv->acecadOldProximity)
585 /*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
586 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
590 if (priv->acecadOldButtons != buttons)
594 delta = buttons ^ priv->acecadOldButtons;
600 delta &= ~(1 << (id-1));
602 /*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
603 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
607 priv->acecadOldButtons = buttons;
608 priv->acecadOldX = x;
609 priv->acecadOldY = y;
610 priv->acecadOldZ = z;
611 priv->acecadOldProximity = prox;
615 if (!is_core_pointer)
616 if (priv->acecadOldProximity)
618 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
619 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
621 priv->acecadOldProximity = 0;
624 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
628 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
630 USBReadInput (LocalDevicePtr local)
633 struct input_event * event;
634 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
635 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
636 int x = priv->acecadOldX;
637 int y = priv->acecadOldY;
638 int z = priv->acecadOldZ;
639 int prox = priv->acecadOldProximity;
640 int buttons = priv->acecadOldButtons;
643 is_core_pointer = xf86IsCorePointer(local->dev);
645 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
648 ErrorF("Error reading wacom device : %s\n", strerror(errno));
652 for (event=(struct input_event *)eventbuf;
653 event<(struct input_event *)(eventbuf+len); event++) {
655 switch (event->type) {
657 switch (event->code) {
677 switch (event->code) {
683 buttons=set_bit(buttons,0,event->value);
687 buttons=set_bit(buttons,1,event->value);
691 buttons=set_bit(buttons,2,event->value);
696 xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
697 } /* switch event->type */
699 /* ABS_MISC is the event terminator */
700 if (event->type != EV_ABS || event->code != ABS_MISC) {
706 if (!(priv->acecadOldProximity))
707 if (!is_core_pointer)
709 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
713 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
715 if (priv->acecadOldButtons != buttons)
719 delta = buttons ^ priv->acecadOldButtons;
725 delta &= ~(1 << (id-1));
727 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
733 if (!is_core_pointer)
734 if (priv->acecadOldProximity)
736 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
738 priv->acecadOldProximity = 0;
741 priv->acecadOldButtons = buttons;
742 priv->acecadOldX = x;
743 priv->acecadOldY = y;
744 priv->acecadOldZ = z;
745 priv->acecadOldProximity = prox;
747 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
752 CloseProc (LocalDevicePtr local)
757 * The ConvertProc function may need to be tailored for your device.
758 * This function converts the device's valuator outputs to x and y coordinates
759 * to simulate mouse events.
762 ConvertProc (LocalDevicePtr local,
774 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
776 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
777 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
783 ReverseConvertProc( LocalDevicePtr local,
788 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
790 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
791 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
797 #define WriteString(str)\
798 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
802 QueryHardware (AceCadPrivatePtr priv)
811 /* Prompt Mode in order to not be disturbed */
812 WriteString(ACECAD_PROMPT_MODE);
815 while(XisbRead(priv->buffer)>=0);
817 /* Ask for Config packet*/
818 WriteString(ACECAD_CONFIG);
820 /* Read the packet */
821 XisbBlockDuration (priv->buffer, 1000000);
824 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
826 if ((AceCadGetPacket (priv) == Success))
828 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
829 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
830 priv->acecadMaxZ = 512;
831 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
836 /*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
840 #define BITS_PER_LONG (sizeof(long) * 8)
841 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
842 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
843 #define OFF(x) ((x)%BITS_PER_LONG)
844 #define LONG(x) ((x)/BITS_PER_LONG)
848 USBQueryHardware (LocalDevicePtr local)
850 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
851 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
854 char name[256] = "Unknown";
856 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
857 xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
859 memset(bit, 0, sizeof(bit));
860 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
862 for (i = 0; i < EV_MAX; i++)
863 if (test_bit(i, bit[0])) {
864 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
865 for (j = 0; j < KEY_MAX; j++)
866 if (test_bit(j, bit[i])) {
868 ioctl(local->fd, EVIOCGABS(j), abs);
871 priv->acecadMaxX = abs[2];
875 priv->acecadMaxY = abs[2];
879 priv->acecadMaxZ = abs[2];
886 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
892 NewPacket (AceCadPrivatePtr priv)
898 AceCadGetPacket (AceCadPrivatePtr priv)
903 while((c = XisbRead(priv->buffer))>=0 )
907 * fail after 500 bytes so the server doesn't hang forever if a
908 * device sends bad data.
920 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
921 XisbBlockDuration (priv->buffer, 10000);
922 priv->packet[priv->packeti++] = c;
923 count=ACECAD_PACKET_SIZE-1;
924 while(count-- && (c = XisbRead(priv->buffer))>=0)
926 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
927 priv->packet[priv->packeti++] = c;
929 XisbBlockDuration (priv->buffer, 0);