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_ansic.h>
52 #include <xf86_OSproc.h>
54 #include <xf86Xinput.h>
56 #include <xf86Module.h>
58 /*****************************************************************************
60 ****************************************************************************/
63 /*****************************************************************************
64 * Variables without includable headers
65 ****************************************************************************/
67 /*****************************************************************************
69 ****************************************************************************/
71 #define SYSCALL(call) while(((call) == -1) && (errno == EINTR))
73 #define read(a,b,c) xf86ReadSerial((a),(b),(c))
75 /* max number of input events to read in one read call */
79 _X_EXPORT InputDriverRec ACECAD =
91 static XF86ModuleVersionInfo VersionRec =
106 _X_EXPORT XF86ModuleData acecadModuleData = {
112 /*****************************************************************************
113 * Function Definitions
114 ****************************************************************************/
117 SetupProc( pointer module,
122 xf86AddInputDriver(&ACECAD, module, 0);
127 TearDownProc( pointer p )
130 LocalDevicePtr local = (LocalDevicePtr) p;
131 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
133 DeviceOff (local->dev);
135 xf86CloseSerial (local->fd);
136 XisbFree (priv->buffer);
144 static const char *default_options[] =
152 "FlowControl", "Xoff",
163 SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
166 xf86Msg(X_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
167 version >> 16, (version >> 8) & 0xff, version & 0xff);
176 AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
178 LocalDevicePtr local = xf86AllocateInput(drv, 0);
179 AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
183 if ((!local) || (!priv))
186 memset(priv,0,sizeof (AceCadPrivateRec));
188 local->name = dev->identifier;
189 local->type_name = "AceCad Tablet";
190 local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
191 local->motion_history_proc = xf86GetMotionEvents;
192 local->control_proc = NULL;
193 local->close_proc = CloseProc;
194 local->switch_mode = NULL;
195 local->conversion_proc = ConvertProc;
196 local->reverse_conversion_proc = ReverseConvertProc;
198 local->private = priv;
199 local->private_flags = 0;
200 local->conf_idev = dev;
201 local->device_control = DeviceControl;
202 /*local->always_core_feedback = 0;*/
204 xf86CollectInputOptions(local, default_options, NULL);
206 xf86OptionListReport(local->options);
208 priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
210 local->fd = xf86OpenSerial (local->options);
213 xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
216 xf86ErrorFVerb( 6, "tty port opened successfully\n" );
219 if(IsUSBLine(local->fd)){
222 local->read_input = USBReadInput;
224 if (USBQueryHardware(local) != Success)
226 ErrorF ("Unable to query/initialize AceCad hardware.\n");
234 local->read_input = ReadInput;
236 speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
241 priv->acecadReportSpeed = 'Q';
244 priv->acecadReportSpeed = 'R';
247 priv->acecadReportSpeed = 'S';
250 priv->acecadReportSpeed = 'T';
253 priv->acecadReportSpeed = 'R';
255 xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");
258 xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);
260 priv->buffer = XisbNew (local->fd, 200);
263 * Verify that hardware is attached and fuctional
265 if (QueryHardware(priv) != Success)
267 xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
272 s = xf86FindOptionValue(local->options, "Mode");
273 if (s && (xf86NameCmp(s, "Relative") == 0))
275 priv->flags = priv->flags & ~ABSOLUTE_FLAG;
279 priv->flags = priv->flags | ABSOLUTE_FLAG;
282 xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
283 DBG (9, XisbTrace (priv->buffer, 1));
285 local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
287 xf86ProcessCommonOptions(local, local->options);
289 local->flags |= XI86_CONFIGURED;
293 RemoveEnabledDevice (local->fd);
296 XisbFree(priv->buffer);
299 xf86CloseSerial(local->fd);
301 RemoveEnabledDevice (local->fd);
306 * If something went wrong, cleanup and return NULL
309 if ((local) && (local->fd))
310 xf86CloseSerial (local->fd);
311 if ((priv) && (priv->buffer))
312 XisbFree (priv->buffer);
319 DeviceControl (DeviceIntPtr dev, int mode)
330 RetValue = DeviceOn( dev );
333 RetValue = DeviceOff( dev );
336 RetValue = DeviceClose( dev );
346 DeviceOn (DeviceIntPtr dev)
349 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
350 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
352 xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
354 local->fd = xf86OpenSerial(local->options);
357 xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
362 if (priv->acecadUSB==0){
363 priv->buffer = XisbNew(local->fd, 200);
366 xf86CloseSerial(local->fd);
371 /*Rets qu'a l'envoyer a la tablette */
372 sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
373 XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
376 xf86FlushInput(local->fd);
377 xf86AddEnabledDevice (local);
378 dev->public.on = TRUE;
383 DeviceOff (DeviceIntPtr dev)
385 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
386 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
391 RemoveEnabledDevice (local->fd);
394 XisbFree(priv->buffer);
397 xf86CloseSerial(local->fd);
401 xf86RemoveEnabledDevice (local);
402 dev->public.on = FALSE;
407 DeviceClose (DeviceIntPtr dev)
409 xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
414 ControlProc(DeviceIntPtr device,
417 xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
421 DeviceInit (DeviceIntPtr dev)
424 LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
425 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
426 unsigned char map[] =
429 xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
432 if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
434 ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
438 if (InitFocusClassDeviceStruct (dev) == FALSE)
440 ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
444 if (InitPtrFeedbackClassDeviceStruct(dev,
445 ControlProc) == FALSE) {
446 ErrorF("unable to init ptr feedback\n");
452 if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
454 ((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
457 ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
463 InitValuatorAxisStruct(dev,
466 priv->acecadMaxX, /* max val */
467 1000, /* resolution */
470 InitValuatorAxisStruct(dev,
473 priv->acecadMaxY, /* max val */
474 1000, /* resolution */
477 InitValuatorAxisStruct(dev,
480 priv->acecadMaxZ, /* max val */
481 1000, /* resolution */
487 if (InitProximityClassDeviceStruct (dev) == FALSE)
489 ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
493 xf86MotionHistoryAllocate (local);
496 /* On ne peut pas calculer l'increment avant, faute d'ecran pour
497 connaitre la taille... */
499 if (priv->acecadInc > 95)
500 priv->acecadInc = 95;
501 if (priv->acecadInc < 1)
503 /* guess the best increment value given video mode */
504 rx=priv->acecadMaxX / screenInfo.screens[0]->width;
505 ry=priv->acecadMaxY / screenInfo.screens[0]->height;
507 priv->acecadInc = rx;
509 priv->acecadInc = ry;
510 if (priv->acecadInc < 1)
514 xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
520 ReadInput (LocalDevicePtr local)
524 int is_core_pointer, is_absolute;
525 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
527 /*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
529 is_absolute = (priv->flags & ABSOLUTE_FLAG);
530 is_core_pointer = xf86IsCorePointer(local->dev);
533 * set blocking to -1 on the first call because we know there is data to
534 * read. Xisb automatically clears it after one successful read so that
535 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
536 * read from blocking indefinately.
538 XisbBlockDuration (priv->buffer, -1);
540 while (AceCadGetPacket (priv) == Success)
542 x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
543 y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
545 if (!(priv->flags & ABSOLUTE_FLAG))
547 x = priv->packet[0] & XSIGN_BIT? x:-x;
548 y = priv->packet[0] & YSIGN_BIT? y:-y;
552 y = priv->acecadMaxY - y ;
556 z = ((int)priv->packet[5] << 2) |
557 (((int)priv->packet[6] & 0x01) << 1) |
558 (((int)priv->packet[6] & 0x10) >> 4);
560 buttons = ((int)priv->packet[0] & 0x07) |
561 ((int)priv->packet[6] & 0x02 << 2);
563 prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
567 if (!(priv->acecadOldProximity))
568 if (!is_core_pointer)
570 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
571 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
574 if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
575 || (!is_absolute && (x || y)))
577 if (is_absolute || priv->acecadOldProximity)
579 /*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
580 xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
584 if (priv->acecadOldButtons != buttons)
588 delta = buttons ^ priv->acecadOldButtons;
594 delta &= ~(1 << (id-1));
596 /*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
597 xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
601 priv->acecadOldButtons = buttons;
602 priv->acecadOldX = x;
603 priv->acecadOldY = y;
604 priv->acecadOldZ = z;
605 priv->acecadOldProximity = prox;
609 if (!is_core_pointer)
610 if (priv->acecadOldProximity)
612 /*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
613 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
615 priv->acecadOldProximity = 0;
618 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
622 #define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
624 USBReadInput (LocalDevicePtr local)
627 struct input_event * event;
628 char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
629 AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
630 int x = priv->acecadOldX;
631 int y = priv->acecadOldY;
632 int z = priv->acecadOldZ;
633 int prox = priv->acecadOldProximity;
634 int buttons = priv->acecadOldButtons;
637 is_core_pointer = xf86IsCorePointer(local->dev);
639 SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
642 ErrorF("Error reading wacom device : %s\n", strerror(errno));
646 for (event=(struct input_event *)eventbuf;
647 event<(struct input_event *)(eventbuf+len); event++) {
649 switch (event->type) {
651 switch (event->code) {
671 switch (event->code) {
677 buttons=set_bit(buttons,0,event->value);
681 buttons=set_bit(buttons,1,event->value);
685 buttons=set_bit(buttons,2,event->value);
690 xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
691 } /* switch event->type */
693 /* ABS_MISC is the event terminator */
694 if (event->type != EV_ABS || event->code != ABS_MISC) {
700 if (!(priv->acecadOldProximity))
701 if (!is_core_pointer)
703 xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
707 xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
709 if (priv->acecadOldButtons != buttons)
713 delta = buttons ^ priv->acecadOldButtons;
719 delta &= ~(1 << (id-1));
721 xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
727 if (!is_core_pointer)
728 if (priv->acecadOldProximity)
730 xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
732 priv->acecadOldProximity = 0;
735 priv->acecadOldButtons = buttons;
736 priv->acecadOldX = x;
737 priv->acecadOldY = y;
738 priv->acecadOldZ = z;
739 priv->acecadOldProximity = prox;
741 /*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
746 CloseProc (LocalDevicePtr local)
751 * The ConvertProc function may need to be tailored for your device.
752 * This function converts the device's valuator outputs to x and y coordinates
753 * to simulate mouse events.
756 ConvertProc (LocalDevicePtr local,
768 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
770 *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
771 *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
777 ReverseConvertProc( LocalDevicePtr local,
782 AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
784 valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
785 valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
791 #define WriteString(str)\
792 XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
796 QueryHardware (AceCadPrivatePtr priv)
805 /* Prompt Mode in order to not be disturbed */
806 WriteString(ACECAD_PROMPT_MODE);
809 while(XisbRead(priv->buffer)>=0);
811 /* Ask for Config packet*/
812 WriteString(ACECAD_CONFIG);
814 /* Read the packet */
815 XisbBlockDuration (priv->buffer, 1000000);
818 /*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/
820 if ((AceCadGetPacket (priv) == Success))
822 priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
823 priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
824 priv->acecadMaxZ = 512;
825 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
830 /*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
834 #define BITS_PER_LONG (sizeof(long) * 8)
835 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
836 #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
837 #define OFF(x) ((x)%BITS_PER_LONG)
838 #define LONG(x) ((x)/BITS_PER_LONG)
842 USBQueryHardware (LocalDevicePtr local)
844 AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
845 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
848 char name[256] = "Unknown";
850 ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
851 xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
853 memset(bit, 0, sizeof(bit));
854 ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
856 for (i = 0; i < EV_MAX; i++)
857 if (test_bit(i, bit[0])) {
858 ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
859 for (j = 0; j < KEY_MAX; j++)
860 if (test_bit(j, bit[i])) {
862 ioctl(local->fd, EVIOCGABS(j), abs);
865 priv->acecadMaxX = abs[2];
869 priv->acecadMaxY = abs[2];
873 priv->acecadMaxZ = abs[2];
880 xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
886 NewPacket (AceCadPrivatePtr priv)
892 AceCadGetPacket (AceCadPrivatePtr priv)
897 while((c = XisbRead(priv->buffer))>=0 )
901 * fail after 500 bytes so the server doesn't hang forever if a
902 * device sends bad data.
914 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
915 XisbBlockDuration (priv->buffer, 10000);
916 priv->packet[priv->packeti++] = c;
917 count=ACECAD_PACKET_SIZE-1;
918 while(count-- && (c = XisbRead(priv->buffer))>=0)
920 /*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
921 priv->packet[priv->packeti++] = c;
923 XisbBlockDuration (priv->buffer, 0);