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);
308 xf86DeleteInput (local, 0);
310 if ((priv) && (priv->buffer))
311 XisbFree (priv->buffer);
318 DeviceControl (DeviceIntPtr dev, int mode)
329 RetValue = DeviceOn( dev );
332 RetValue = DeviceOff( dev );
335 RetValue = DeviceClose( dev );
345 DeviceOn (DeviceIntPtr dev)
348 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
349 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
351 xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
353 local->fd = xf86OpenSerial(local->options);
356 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
361 if (priv->acecadUSB==0){
362 priv->buffer = XisbNew(local->fd, 200);
365 xf86CloseSerial(local->fd);
370 /*Rets qu'a l'envoyer a la tablette */
371 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
372 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
375 xf86FlushInput(local->fd);
376 xf86AddEnabledDevice (local);
377 dev->public.on = TRUE;
382 DeviceOff (DeviceIntPtr dev)
384 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
385 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
390 RemoveEnabledDevice (local->fd);
393 XisbFree(priv->buffer);
396 xf86CloseSerial(local->fd);
400 xf86RemoveEnabledDevice (local);
401 dev->public.on = FALSE;
406 DeviceClose (DeviceIntPtr dev)
408 xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
413 ControlProc(DeviceIntPtr device,
416 xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
420 DeviceInit (DeviceIntPtr dev)
423 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
424 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
425 unsigned char map[] =
428 xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
431 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
433 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
437 if (InitFocusClassDeviceStruct (dev) == FALSE)
439 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
443 if (InitPtrFeedbackClassDeviceStruct(dev,
444 ControlProc) == FALSE) {
445 ErrorF("unable to init ptr feedback\n");
451 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
453 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
456 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
462 InitValuatorAxisStruct(dev,
465 priv->acecadMaxX, /* max val */
466 1000, /* resolution */
469 InitValuatorAxisStruct(dev,
472 priv->acecadMaxY, /* max val */
473 1000, /* resolution */
476 InitValuatorAxisStruct(dev,
479 priv->acecadMaxZ, /* max val */
480 1000, /* resolution */
486 if (InitProximityClassDeviceStruct (dev) == FALSE)
488 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
492 xf86MotionHistoryAllocate (local);
495 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
496 connaitre la taille... */
498 if (priv->acecadInc > 95)
499 priv->acecadInc = 95;
500 if (priv->acecadInc < 1)
502 /* guess the best increment value given video mode */
503 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
504 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
506 priv->acecadInc = rx;
508 priv->acecadInc = ry;
509 if (priv->acecadInc < 1)
513 xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
519 ReadInput (LocalDevicePtr local)
523 int is_core_pointer, is_absolute;
524 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
526 /*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
528 is_absolute = (priv->flags & ABSOLUTE_FLAG);
529 is_core_pointer = xf86IsCorePointer(local->dev);
532 * set blocking to -1 on the first call because we know there is data to
533 * read. Xisb automatically clears it after one successful read so that
534 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
535 * read from blocking indefinately.
537 XisbBlockDuration (priv->buffer, -1);
539 while (AceCadGetPacket (priv) == Success)
541 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
542 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
544 if (!(priv->flags & ABSOLUTE_FLAG))
546 x = priv->packet[0] & XSIGN_BIT? x:-x;
547 y = priv->packet[0] & YSIGN_BIT? y:-y;
551 y = priv->acecadMaxY - y ;
555 z = ((int)priv->packet[5] << 2) |
556 (((int)priv->packet[6] & 0x01) << 1) |
557 (((int)priv->packet[6] & 0x10) >> 4);
559 buttons = ((int)priv->packet[0] & 0x07) |
560 ((int)priv->packet[6] & 0x02 << 2);
562 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
566 if (!(priv->acecadOldProximity))
567 if (!is_core_pointer)
569 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
570 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
573 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
574 || (!is_absolute && (x || y)))
576 if (is_absolute || priv->acecadOldProximity)
578 /*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
579 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
583 if (priv->acecadOldButtons != buttons)
587 delta = buttons ^ priv->acecadOldButtons;
593 delta &= ~(1 << (id-1));
595 /*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
596 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
600 priv->acecadOldButtons = buttons;
601 priv->acecadOldX = x;
602 priv->acecadOldY = y;
603 priv->acecadOldZ = z;
604 priv->acecadOldProximity = prox;
608 if (!is_core_pointer)
609 if (priv->acecadOldProximity)
611 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
612 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
614 priv->acecadOldProximity = 0;
617 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
621 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
623 USBReadInput (LocalDevicePtr local)
626 struct input_event * event;
627 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
628 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
629 int x = priv->acecadOldX;
630 int y = priv->acecadOldY;
631 int z = priv->acecadOldZ;
632 int prox = priv->acecadOldProximity;
633 int buttons = priv->acecadOldButtons;
636 is_core_pointer = xf86IsCorePointer(local->dev);
638 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
641 ErrorF("Error reading wacom device : %s\n", strerror(errno));
645 for (event=(struct input_event *)eventbuf;
646 event<(struct input_event *)(eventbuf+len); event++) {
648 switch (event->type) {
650 switch (event->code) {
670 switch (event->code) {
676 buttons=set_bit(buttons,0,event->value);
680 buttons=set_bit(buttons,1,event->value);
684 buttons=set_bit(buttons,2,event->value);
689 xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
690 } /* switch event->type */
692 /* ABS_MISC is the event terminator */
693 if (event->type != EV_ABS || event->code != ABS_MISC) {
699 if (!(priv->acecadOldProximity))
700 if (!is_core_pointer)
702 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
706 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
708 if (priv->acecadOldButtons != buttons)
712 delta = buttons ^ priv->acecadOldButtons;
718 delta &= ~(1 << (id-1));
720 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
726 if (!is_core_pointer)
727 if (priv->acecadOldProximity)
729 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
731 priv->acecadOldProximity = 0;
734 priv->acecadOldButtons = buttons;
735 priv->acecadOldX = x;
736 priv->acecadOldY = y;
737 priv->acecadOldZ = z;
738 priv->acecadOldProximity = prox;
740 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
745 CloseProc (LocalDevicePtr local)
750 * The ConvertProc function may need to be tailored for your device.
751 * This function converts the device's valuator outputs to x and y coordinates
752 * to simulate mouse events.
755 ConvertProc (LocalDevicePtr local,
767 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
769 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
770 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
776 ReverseConvertProc( LocalDevicePtr local,
781 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
783 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
784 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
790 #define WriteString(str)\
791 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
795 QueryHardware (AceCadPrivatePtr priv)
804 /* Prompt Mode in order to not be disturbed */
805 WriteString(ACECAD_PROMPT_MODE);
808 while(XisbRead(priv->buffer)>=0);
810 /* Ask for Config packet*/
811 WriteString(ACECAD_CONFIG);
813 /* Read the packet */
814 XisbBlockDuration (priv->buffer, 1000000);
817 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
819 if ((AceCadGetPacket (priv) == Success))
821 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
822 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
823 priv->acecadMaxZ = 512;
824 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
829 /*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
833 #define BITS_PER_LONG (sizeof(long) * 8)
834 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
835 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
836 #define OFF(x) ((x)%BITS_PER_LONG)
837 #define LONG(x) ((x)/BITS_PER_LONG)
841 USBQueryHardware (LocalDevicePtr local)
843 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
844 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
847 char name[256] = "Unknown";
849 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
850 xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
852 memset(bit, 0, sizeof(bit));
853 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
855 for (i = 0; i < EV_MAX; i++)
856 if (test_bit(i, bit[0])) {
857 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
858 for (j = 0; j < KEY_MAX; j++)
859 if (test_bit(j, bit[i])) {
861 ioctl(local->fd, EVIOCGABS(j), abs);
864 priv->acecadMaxX = abs[2];
868 priv->acecadMaxY = abs[2];
872 priv->acecadMaxZ = abs[2];
879 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
885 NewPacket (AceCadPrivatePtr priv)
891 AceCadGetPacket (AceCadPrivatePtr priv)
896 while((c = XisbRead(priv->buffer))>=0 )
900 * fail after 500 bytes so the server doesn't hang forever if a
901 * device sends bad data.
913 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
914 XisbBlockDuration (priv->buffer, 10000);
915 priv->packet[priv->packeti++] = c;
916 count=ACECAD_PACKET_SIZE-1;
917 while(count-- && (c = XisbRead(priv->buffer))>=0)
919 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
920 priv->packet[priv->packeti++] = c;
922 XisbBlockDuration (priv->buffer, 0);