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.2 2001/11/26 16:25:52 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
static InputDriverRec ACECAD =
87
static XF86ModuleVersionInfo VersionRec =
102
static const char *default_options[] =
110
"FlowControl", "Xoff",
114
XF86ModuleData acecadModuleData = { &VersionRec, SetupProc, TearDownProc};
116
/*****************************************************************************
117
* Function Definitions
118
****************************************************************************/
121
SetupProc( pointer module,
126
xf86AddInputDriver(&ACECAD, module, 0);
131
TearDownProc( pointer p )
134
LocalDevicePtr local = (LocalDevicePtr) p;
135
AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
137
DeviceOff (local->dev);
139
xf86CloseSerial (local->fd);
140
XisbFree (priv->buffer);
153
SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
156
xf86Msg(X_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
157
version >> 16, (version >> 8) & 0xff, version & 0xff);
166
AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
168
LocalDevicePtr local = xf86AllocateInput(drv, 0);
169
AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
173
if ((!local) || (!priv))
176
memset(priv,0,sizeof (AceCadPrivateRec));
178
local->name = dev->identifier;
179
local->type_name = "AceCad Tablet";
180
local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
181
local->motion_history_proc = xf86GetMotionEvents;
182
local->control_proc = NULL;
183
local->close_proc = CloseProc;
184
local->switch_mode = NULL;
185
local->conversion_proc = ConvertProc;
186
local->reverse_conversion_proc = ReverseConvertProc;
188
local->private = priv;
189
local->private_flags = 0;
190
local->conf_idev = dev;
191
local->device_control = DeviceControl;
192
/*local->always_core_feedback = 0;*/
194
xf86CollectInputOptions(local, default_options, NULL);
196
xf86OptionListReport(local->options);
198
priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );
200
local->fd = xf86OpenSerial (local->options);
203
xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
206
xf86ErrorFVerb( 6, "tty port opened successfully\n" );
208
if(IsUSBLine(local->fd)){
211
local->read_input = USBReadInput;
213
if (USBQueryHardware(local) != Success)
215
ErrorF ("Unable to query/initialize AceCad hardware.\n");
222
local->read_input = ReadInput;
224
speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );
229
priv->acecadReportSpeed = 'Q';
232
priv->acecadReportSpeed = 'R';
235
priv->acecadReportSpeed = 'S';
238
priv->acecadReportSpeed = 'T';
241
priv->acecadReportSpeed = 'R';
243
xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");
246
xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);
248
priv->buffer = XisbNew (local->fd, 200);
251
* Verify that hardware is attached and fuctional
253
if (QueryHardware(priv) != Success)
255
xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
260
s = xf86FindOptionValue(local->options, "Mode");
261
if (s && (xf86NameCmp(s, "Relative") == 0))
263
priv->flags = priv->flags & ~ABSOLUTE_FLAG;
267
priv->flags = priv->flags | ABSOLUTE_FLAG;
270
xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");
271
DBG (9, XisbTrace (priv->buffer, 1));
273
local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);
275
xf86ProcessCommonOptions(local, local->options);
277
local->flags |= XI86_CONFIGURED;
281
RemoveEnabledDevice (local->fd);
284
XisbFree(priv->buffer);
287
xf86CloseSerial(local->fd);
289
RemoveEnabledDevice (local->fd);
294
* If something went wrong, cleanup and return NULL
297
if ((local) && (local->fd))
298
xf86CloseSerial (local->fd);
302
if ((priv) && (priv->buffer))
303
XisbFree (priv->buffer);
310
DeviceControl (DeviceIntPtr dev, int mode)
321
RetValue = DeviceOn( dev );
324
RetValue = DeviceOff( dev );
327
RetValue = DeviceClose( dev );
337
DeviceOn (DeviceIntPtr dev)
340
LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
341
AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
343
xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");
345
local->fd = xf86OpenSerial(local->options);
348
xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
353
if (priv->acecadUSB==0){
354
priv->buffer = XisbNew(local->fd, 200);
357
xf86CloseSerial(local->fd);
362
/*Rets qu'a l'envoyer a la tablette */
363
sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
364
XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
367
xf86FlushInput(local->fd);
368
xf86AddEnabledDevice (local);
369
dev->public.on = TRUE;
374
DeviceOff (DeviceIntPtr dev)
376
LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
377
AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
382
RemoveEnabledDevice (local->fd);
385
XisbFree(priv->buffer);
388
xf86CloseSerial(local->fd);
392
xf86RemoveEnabledDevice (local);
393
dev->public.on = FALSE;
398
DeviceClose (DeviceIntPtr dev)
400
xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
405
ControlProc(DeviceIntPtr device,
408
xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
412
DeviceInit (DeviceIntPtr dev)
415
LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
416
AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
417
unsigned char map[] =
420
xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");
423
if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
425
ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
429
if (InitFocusClassDeviceStruct (dev) == FALSE)
431
ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
435
if (InitPtrFeedbackClassDeviceStruct(dev,
436
ControlProc) == FALSE) {
437
ErrorF("unable to init ptr feedback\n");
443
if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
445
((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
448
ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
454
InitValuatorAxisStruct(dev,
457
priv->acecadMaxX, /* max val */
458
1000, /* resolution */
461
InitValuatorAxisStruct(dev,
464
priv->acecadMaxY, /* max val */
465
1000, /* resolution */
468
InitValuatorAxisStruct(dev,
471
priv->acecadMaxZ, /* max val */
472
1000, /* resolution */
478
if (InitProximityClassDeviceStruct (dev) == FALSE)
480
ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
484
xf86MotionHistoryAllocate (local);
487
/* On ne peut pas calculer l'increment avant, faute d'ecran pour
488
connaitre la taille... */
490
if (priv->acecadInc > 95)
491
priv->acecadInc = 95;
492
if (priv->acecadInc < 1)
494
/* guess the best increment value given video mode */
495
rx=priv->acecadMaxX / screenInfo.screens[0]->width;
496
ry=priv->acecadMaxY / screenInfo.screens[0]->height;
498
priv->acecadInc = rx;
500
priv->acecadInc = ry;
501
if (priv->acecadInc < 1)
505
xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);
511
ReadInput (LocalDevicePtr local)
515
int is_core_pointer, is_absolute;
516
AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
518
/*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/
520
is_absolute = (priv->flags & ABSOLUTE_FLAG);
521
is_core_pointer = xf86IsCorePointer(local->dev);
524
* set blocking to -1 on the first call because we know there is data to
525
* read. Xisb automatically clears it after one successful read so that
526
* succeeding reads are preceeded buy a select with a 0 timeout to prevent
527
* read from blocking indefinately.
529
XisbBlockDuration (priv->buffer, -1);
531
while (AceCadGetPacket (priv) == Success)
533
x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
534
y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);
536
if (!(priv->flags & ABSOLUTE_FLAG))
538
x = priv->packet[0] & XSIGN_BIT? x:-x;
539
y = priv->packet[0] & YSIGN_BIT? y:-y;
543
y = priv->acecadMaxY - y ;
547
z = ((int)priv->packet[5] << 2) |
548
(((int)priv->packet[6] & 0x01) << 1) |
549
(((int)priv->packet[6] & 0x10) >> 4);
551
buttons = ((int)priv->packet[0] & 0x07) |
552
((int)priv->packet[6] & 0x02 << 2);
554
prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;
558
if (!(priv->acecadOldProximity))
559
if (!is_core_pointer)
561
/*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
562
xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
565
if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
566
|| (!is_absolute && (x || y)))
568
if (is_absolute || priv->acecadOldProximity)
570
/*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
571
xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
575
if (priv->acecadOldButtons != buttons)
579
delta = buttons ^ priv->acecadOldButtons;
585
delta &= ~(1 << (id-1));
587
/*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
588
xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
592
priv->acecadOldButtons = buttons;
593
priv->acecadOldX = x;
594
priv->acecadOldY = y;
595
priv->acecadOldZ = z;
596
priv->acecadOldProximity = prox;
600
if (!is_core_pointer)
601
if (priv->acecadOldProximity)
603
/*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
604
xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
606
priv->acecadOldProximity = 0;
609
/*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
612
#define set_bit(byte,nb,bit) (bit ? byte | (1<<nb) : byte & (~(1<<nb)))
614
USBReadInput (LocalDevicePtr local)
617
struct input_event * event;
618
char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
619
AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
620
int x = priv->acecadOldX;
621
int y = priv->acecadOldY;
622
int z = priv->acecadOldZ;
623
int prox = priv->acecadOldProximity;
624
int buttons = priv->acecadOldButtons;
627
is_core_pointer = xf86IsCorePointer(local->dev);
629
SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));
632
ErrorF("Error reading wacom device : %s\n", strerror(errno));
636
for (event=(struct input_event *)eventbuf;
637
event<(struct input_event *)(eventbuf+len); event++) {
639
switch (event->type) {
641
switch (event->code) {
661
switch (event->code) {
667
buttons=set_bit(buttons,0,event->value);
671
buttons=set_bit(buttons,1,event->value);
675
buttons=set_bit(buttons,2,event->value);
680
xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
681
} /* switch event->type */
683
/* ABS_MISC is the event terminator */
684
if (event->type != EV_ABS || event->code != ABS_MISC) {
690
if (!(priv->acecadOldProximity))
691
if (!is_core_pointer)
693
xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
697
xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);
699
if (priv->acecadOldButtons != buttons)
703
delta = buttons ^ priv->acecadOldButtons;
709
delta &= ~(1 << (id-1));
711
xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
717
if (!is_core_pointer)
718
if (priv->acecadOldProximity)
720
xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
722
priv->acecadOldProximity = 0;
725
priv->acecadOldButtons = buttons;
726
priv->acecadOldX = x;
727
priv->acecadOldY = y;
728
priv->acecadOldZ = z;
729
priv->acecadOldProximity = prox;
731
/*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
735
CloseProc (LocalDevicePtr local)
740
* The ConvertProc function may need to be tailored for your device.
741
* This function converts the device's valuator outputs to x and y coordinates
742
* to simulate mouse events.
745
ConvertProc (LocalDevicePtr local,
757
AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
759
*x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
760
*y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
766
ReverseConvertProc( LocalDevicePtr local,
771
AceCadPrivatePtr priv = (AceCadPrivatePtr)(local->private);
773
valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
774
valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;
780
#define WriteString(str)\
781
XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))
785
QueryHardware (AceCadPrivatePtr priv)
794
/* Prompt Mode in order to not be disturbed */
795
WriteString(ACECAD_PROMPT_MODE);
798
while(XisbRead(priv->buffer)>=0);
800
/* Ask for Config packet*/
801
WriteString(ACECAD_CONFIG);
803
/* Read the packet */
804
XisbBlockDuration (priv->buffer, 1000000);
807
/*xf86Msg(X_CONFIG, "Acecad Tablet init envoy� \n");*/
809
if ((AceCadGetPacket (priv) == Success))
811
priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
812
priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
813
priv->acecadMaxZ = 512;
814
xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
819
/*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
823
#define BITS_PER_LONG (sizeof(long) * 8)
824
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
825
#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
826
#define OFF(x) ((x)%BITS_PER_LONG)
827
#define LONG(x) ((x)/BITS_PER_LONG)
830
USBQueryHardware (LocalDevicePtr local)
832
AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
833
unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
836
char name[256] = "Unknown";
838
ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
839
xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);
841
memset(bit, 0, sizeof(bit));
842
ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);
844
for (i = 0; i < EV_MAX; i++)
845
if (test_bit(i, bit[0])) {
846
ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
847
for (j = 0; j < KEY_MAX; j++)
848
if (test_bit(j, bit[i])) {
850
ioctl(local->fd, EVIOCGABS(j), abs);
853
priv->acecadMaxX = abs[2];
857
priv->acecadMaxY = abs[2];
861
priv->acecadMaxZ = abs[2];
868
xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
873
NewPacket (AceCadPrivatePtr priv)
879
AceCadGetPacket (AceCadPrivatePtr priv)
884
while((c = XisbRead(priv->buffer))>=0 )
888
* fail after 500 bytes so the server doesn't hang forever if a
889
* device sends bad data.
901
/*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
902
XisbBlockDuration (priv->buffer, 10000);
903
priv->packet[priv->packeti++] = c;
904
count=ACECAD_PACKET_SIZE-1;
905
while(count-- && (c = XisbRead(priv->buffer))>=0)
907
/*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
908
priv->packet[priv->packeti++] = c;
910
XisbBlockDuration (priv->buffer, 0);