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 InputDriverRec ACECAD =
87 static XF86ModuleVersionInfo VersionRec =
102 XF86ModuleData acecadModuleData = { &VersionRec, SetupProc, TearDownProc};
104 /*****************************************************************************
105 * Function Definitions
106 ****************************************************************************/
109 SetupProc( pointer module,
114 xf86AddInputDriver(&ACECAD, module, 0);
119 TearDownProc( pointer p )
122 LocalDevicePtr local = (LocalDevicePtr) p;
123 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
125 DeviceOff (local->dev);
127 xf86CloseSerial (local->fd);
128 XisbFree (priv->buffer);
136 static const char *default_options[] =
144 "FlowControl", "Xoff",
155 SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
158 xf86Msg(X_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
159 version >> 16, (version >> 8) & 0xff, version & 0xff);
168 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
170 LocalDevicePtr local = xf86AllocateInput(drv, 0);
171 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
175 if ((!local) || (!priv))
178 memset(priv,0,sizeof (AceCadPrivateRec));
180 local->name = dev->identifier;
181 local->type_name = "AceCad Tablet";
182 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
183 local->motion_history_proc = xf86GetMotionEvents;
184 local->control_proc = NULL;
185 local->close_proc = CloseProc;
186 local->switch_mode = NULL;
187 local->conversion_proc = ConvertProc;
188 local->reverse_conversion_proc = ReverseConvertProc;
190 local->private = priv;
191 local->private_flags = 0;
192 local->conf_idev = dev;
193 local->device_control = DeviceControl;
194 /*local->always_core_feedback = 0;*/
196 xf86CollectInputOptions(local, default_options, NULL);
198 xf86OptionListReport(local->options);
200 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
202 local->fd = xf86OpenSerial (local->options);
205 xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
208 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
211 if(IsUSBLine(local->fd)){
214 local->read_input = USBReadInput;
216 if (USBQueryHardware(local) != Success)
218 ErrorF ("Unable to query/initialize AceCad hardware.\n");
226 local->read_input = ReadInput;
228 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
233 priv->acecadReportSpeed = 'Q';
236 priv->acecadReportSpeed = 'R';
239 priv->acecadReportSpeed = 'S';
242 priv->acecadReportSpeed = 'T';
245 priv->acecadReportSpeed = 'R';
247 xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");
250 xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);
252 priv->buffer = XisbNew (local->fd, 200);
255 * Verify that hardware is attached and fuctional
257 if (QueryHardware(priv) != Success)
259 xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
264 s = xf86FindOptionValue(local->options, "Mode");
265 if (s && (xf86NameCmp(s, "Relative") == 0))
267 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
271 priv->flags = priv->flags | ABSOLUTE_FLAG;
274 xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
275 DBG (9, XisbTrace (priv->buffer, 1));
277 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
279 xf86ProcessCommonOptions(local, local->options);
281 local->flags |= XI86_CONFIGURED;
285 RemoveEnabledDevice (local->fd);
288 XisbFree(priv->buffer);
291 xf86CloseSerial(local->fd);
293 RemoveEnabledDevice (local->fd);
298 * If something went wrong, cleanup and return NULL
301 if ((local) && (local->fd))
302 xf86CloseSerial (local->fd);
304 xf86DeleteInput (local, 0);
306 if ((priv) && (priv->buffer))
307 XisbFree (priv->buffer);
314 DeviceControl (DeviceIntPtr dev, int mode)
325 RetValue = DeviceOn( dev );
328 RetValue = DeviceOff( dev );
331 RetValue = DeviceClose( dev );
341 DeviceOn (DeviceIntPtr dev)
344 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
345 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
347 xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
349 local->fd = xf86OpenSerial(local->options);
352 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
357 if (priv->acecadUSB==0){
358 priv->buffer = XisbNew(local->fd, 200);
361 xf86CloseSerial(local->fd);
366 /*Rets qu'a l'envoyer a la tablette */
367 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
368 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
371 xf86FlushInput(local->fd);
372 xf86AddEnabledDevice (local);
373 dev->public.on = TRUE;
378 DeviceOff (DeviceIntPtr dev)
380 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
381 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
386 RemoveEnabledDevice (local->fd);
389 XisbFree(priv->buffer);
392 xf86CloseSerial(local->fd);
396 xf86RemoveEnabledDevice (local);
397 dev->public.on = FALSE;
402 DeviceClose (DeviceIntPtr dev)
404 xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
409 ControlProc(DeviceIntPtr device,
412 xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
416 DeviceInit (DeviceIntPtr dev)
419 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
420 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
421 unsigned char map[] =
424 xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
427 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
429 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
433 if (InitFocusClassDeviceStruct (dev) == FALSE)
435 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
439 if (InitPtrFeedbackClassDeviceStruct(dev,
440 ControlProc) == FALSE) {
441 ErrorF("unable to init ptr feedback\n");
447 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
449 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
452 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
458 InitValuatorAxisStruct(dev,
461 priv->acecadMaxX, /* max val */
462 1000, /* resolution */
465 InitValuatorAxisStruct(dev,
468 priv->acecadMaxY, /* max val */
469 1000, /* resolution */
472 InitValuatorAxisStruct(dev,
475 priv->acecadMaxZ, /* max val */
476 1000, /* resolution */
482 if (InitProximityClassDeviceStruct (dev) == FALSE)
484 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
488 xf86MotionHistoryAllocate (local);
491 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
492 connaitre la taille... */
494 if (priv->acecadInc > 95)
495 priv->acecadInc = 95;
496 if (priv->acecadInc < 1)
498 /* guess the best increment value given video mode */
499 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
500 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
502 priv->acecadInc = rx;
504 priv->acecadInc = ry;
505 if (priv->acecadInc < 1)
509 xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
515 ReadInput (LocalDevicePtr local)
519 int is_core_pointer, is_absolute;
520 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
522 /*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
524 is_absolute = (priv->flags & ABSOLUTE_FLAG);
525 is_core_pointer = xf86IsCorePointer(local->dev);
528 * set blocking to -1 on the first call because we know there is data to
529 * read. Xisb automatically clears it after one successful read so that
530 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
531 * read from blocking indefinately.
533 XisbBlockDuration (priv->buffer, -1);
535 while (AceCadGetPacket (priv) == Success)
537 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
538 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
540 if (!(priv->flags & ABSOLUTE_FLAG))
542 x = priv->packet[0] & XSIGN_BIT? x:-x;
543 y = priv->packet[0] & YSIGN_BIT? y:-y;
547 y = priv->acecadMaxY - y ;
551 z = ((int)priv->packet[5] << 2) |
552 (((int)priv->packet[6] & 0x01) << 1) |
553 (((int)priv->packet[6] & 0x10) >> 4);
555 buttons = ((int)priv->packet[0] & 0x07) |
556 ((int)priv->packet[6] & 0x02 << 2);
558 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
562 if (!(priv->acecadOldProximity))
563 if (!is_core_pointer)
565 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
566 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
569 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
570 || (!is_absolute && (x || y)))
572 if (is_absolute || priv->acecadOldProximity)
574 /*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
575 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
579 if (priv->acecadOldButtons != buttons)
583 delta = buttons ^ priv->acecadOldButtons;
589 delta &= ~(1 << (id-1));
591 /*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
592 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
596 priv->acecadOldButtons = buttons;
597 priv->acecadOldX = x;
598 priv->acecadOldY = y;
599 priv->acecadOldZ = z;
600 priv->acecadOldProximity = prox;
604 if (!is_core_pointer)
605 if (priv->acecadOldProximity)
607 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
608 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
610 priv->acecadOldProximity = 0;
613 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
617 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
619 USBReadInput (LocalDevicePtr local)
622 struct input_event * event;
623 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
624 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
625 int x = priv->acecadOldX;
626 int y = priv->acecadOldY;
627 int z = priv->acecadOldZ;
628 int prox = priv->acecadOldProximity;
629 int buttons = priv->acecadOldButtons;
632 is_core_pointer = xf86IsCorePointer(local->dev);
634 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
637 ErrorF("Error reading wacom device : %s\n", strerror(errno));
641 for (event=(struct input_event *)eventbuf;
642 event<(struct input_event *)(eventbuf+len); event++) {
644 switch (event->type) {
646 switch (event->code) {
666 switch (event->code) {
672 buttons=set_bit(buttons,0,event->value);
676 buttons=set_bit(buttons,1,event->value);
680 buttons=set_bit(buttons,2,event->value);
685 xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
686 } /* switch event->type */
688 /* ABS_MISC is the event terminator */
689 if (event->type != EV_ABS || event->code != ABS_MISC) {
695 if (!(priv->acecadOldProximity))
696 if (!is_core_pointer)
698 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
702 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
704 if (priv->acecadOldButtons != buttons)
708 delta = buttons ^ priv->acecadOldButtons;
714 delta &= ~(1 << (id-1));
716 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
722 if (!is_core_pointer)
723 if (priv->acecadOldProximity)
725 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
727 priv->acecadOldProximity = 0;
730 priv->acecadOldButtons = buttons;
731 priv->acecadOldX = x;
732 priv->acecadOldY = y;
733 priv->acecadOldZ = z;
734 priv->acecadOldProximity = prox;
736 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
741 CloseProc (LocalDevicePtr local)
746 * The ConvertProc function may need to be tailored for your device.
747 * This function converts the device's valuator outputs to x and y coordinates
748 * to simulate mouse events.
751 ConvertProc (LocalDevicePtr local,
763 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
765 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
766 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
772 ReverseConvertProc( LocalDevicePtr local,
777 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
779 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
780 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
786 #define WriteString(str)\
787 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
791 QueryHardware (AceCadPrivatePtr priv)
800 /* Prompt Mode in order to not be disturbed */
801 WriteString(ACECAD_PROMPT_MODE);
804 while(XisbRead(priv->buffer)>=0);
806 /* Ask for Config packet*/
807 WriteString(ACECAD_CONFIG);
809 /* Read the packet */
810 XisbBlockDuration (priv->buffer, 1000000);
813 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
815 if ((AceCadGetPacket (priv) == Success))
817 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
818 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
819 priv->acecadMaxZ = 512;
820 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
825 /*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
829 #define BITS_PER_LONG (sizeof(long) * 8)
830 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
831 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
832 #define OFF(x) ((x)%BITS_PER_LONG)
833 #define LONG(x) ((x)/BITS_PER_LONG)
837 USBQueryHardware (LocalDevicePtr local)
839 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
840 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
843 char name[256] = "Unknown";
845 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
846 xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
848 memset(bit, 0, sizeof(bit));
849 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
851 for (i = 0; i < EV_MAX; i++)
852 if (test_bit(i, bit[0])) {
853 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
854 for (j = 0; j < KEY_MAX; j++)
855 if (test_bit(j, bit[i])) {
857 ioctl(local->fd, EVIOCGABS(j), abs);
860 priv->acecadMaxX = abs[2];
864 priv->acecadMaxY = abs[2];
868 priv->acecadMaxZ = abs[2];
875 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
881 NewPacket (AceCadPrivatePtr priv)
887 AceCadGetPacket (AceCadPrivatePtr priv)
892 while((c = XisbRead(priv->buffer))>=0 )
896 * fail after 500 bytes so the server doesn't hang forever if a
897 * device sends bad data.
909 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
910 XisbBlockDuration (priv->buffer, 10000);
911 priv->packet[priv->packeti++] = c;
912 count=ACECAD_PACKET_SIZE-1;
913 while(count-- && (c = XisbRead(priv->buffer))>=0)
915 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
916 priv->packet[priv->packeti++] = c;
918 XisbBlockDuration (priv->buffer, 0);