summaryrefslogtreecommitdiff
path: root/src/acecad.c
blob: 49e09e82fd99f04b351d85d7990a297e29a837bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/* 
 * Copyright (c) 2001 Edouard TISSERANT <tissered@esstin.u-nancy.fr>
 * Parts inspired from Shane Watts <shane@bofh.asn.au> XFree86 3 Acecad Driver
 * Thanks to Emily, from AceCad, For giving me documents.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *
 */
/* $XFree86: xc/programs/Xserver/hw/xfree86/input/acecad/acecad.c,v 1.4 2003/10/30 00:40:45 dawes Exp $ */

#define _ACECAD_C_
/*****************************************************************************
 *	Standard Headers
 ****************************************************************************/

#ifdef LINUX_INPUT
#include <asm/types.h>
#include <linux/input.h>
#ifdef BUS_PCI
#undef BUS_PCI
#endif
#ifdef BUS_ISA
#undef BUS_ISA
#endif
#endif

#include <misc.h>
#include <xf86.h>
#define NEED_XF86_TYPES
#include <xf86_ansic.h>
#include <xf86_OSproc.h>
#include <xisb.h>
#include <xf86Xinput.h>
#include <exevents.h>
#include <xf86Module.h>

/*****************************************************************************
 *	Local Headers
 ****************************************************************************/
#include "acecad.h"

/*****************************************************************************
 *	Variables without includable headers
 ****************************************************************************/

/*****************************************************************************
 *	Local Variables
 ****************************************************************************/

#define SYSCALL(call) while(((call) == -1) && (errno == EINTR))
#undef read
#define read(a,b,c) xf86ReadSerial((a),(b),(c))

/* max number of input events to read in one read call */
#define MAX_EVENTS 50


InputDriverRec ACECAD = 
{
	1,
	"acecad",
	NULL,
	AceCadPreInit,
	NULL,
	NULL,
	0
};

#ifdef XFree86LOADER
static XF86ModuleVersionInfo VersionRec =
{
	"acecad",
	MODULEVENDORSTRING,
	MODINFOSTRING1,
	MODINFOSTRING2,
	XORG_VERSION_CURRENT,
	1, 0, 0,
	ABI_CLASS_XINPUT,
	ABI_XINPUT_VERSION,
	MOD_CLASS_XINPUT,
	{0, 0, 0, 0}
};


XF86ModuleData acecadModuleData = { &VersionRec, SetupProc, TearDownProc};

/*****************************************************************************
 *	Function Definitions
 ****************************************************************************/

static pointer
SetupProc(	pointer module,
		pointer options,
		int *errmaj,
		int *errmin )
{
	xf86AddInputDriver(&ACECAD, module, 0);
	return module;
}

static void
TearDownProc( pointer p )
{
#if 0
	LocalDevicePtr local = (LocalDevicePtr) p;
	AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;

	DeviceOff (local->dev);

	xf86CloseSerial (local->fd);
	XisbFree (priv->buffer);
	xfree (priv);
	xfree (local->name);
	xfree (local);
#endif
}
#endif

static const char *default_options[] =
{
	"BaudRate", "9600",
	"StopBits", "1",
	"DataBits", "8",
	"Parity", "Odd",
	"Vmin", "1",
	"Vtime", "10",
	"FlowControl", "Xoff",
	NULL
};

#ifdef LINUX_INPUT
static int
IsUSBLine(int fd)
{
    int version;
    int err;

    SYSCALL(err = ioctl(fd, EVIOCGVERSION, &version));
    
    if (!err) {
	xf86Msg(X_CONFIG,"Kernel Input driver version is %d.%d.%d\n",
	       version >> 16, (version >> 8) & 0xff, version & 0xff);
	return 1;
    } else {
	return 0;
    }
}
#endif

static InputInfoPtr
AceCadPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
{
	LocalDevicePtr local = xf86AllocateInput(drv, 0);
	AceCadPrivatePtr priv = xcalloc (1, sizeof (AceCadPrivateRec));
	int speed;
	char *s;

	if ((!local) || (!priv))
		goto SetupProc_fail;

	memset(priv,0,sizeof (AceCadPrivateRec));

	local->name = dev->identifier;
	local->type_name = "AceCad Tablet";
	local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
	local->motion_history_proc = xf86GetMotionEvents;
	local->control_proc = NULL;
	local->close_proc = CloseProc;
	local->switch_mode = NULL;
	local->conversion_proc = ConvertProc;
	local->reverse_conversion_proc = ReverseConvertProc;
	local->dev = NULL;
	local->private = priv;
	local->private_flags = 0;
	local->conf_idev = dev;
	local->device_control = DeviceControl;
	/*local->always_core_feedback = 0;*/
	
	xf86CollectInputOptions(local, default_options, NULL);

	xf86OptionListReport(local->options);

	priv->acecadInc = xf86SetIntOption(local->options, "Increment", 0 );

	local->fd = xf86OpenSerial (local->options);
	if (local->fd == -1)
	{
		xf86Msg(X_ERROR,"AceCad driver unable to open device\n");
		goto SetupProc_fail;
	}
	xf86ErrorFVerb( 6, "tty port opened successfully\n" );

#ifdef LINUX_INPUT
	if(IsUSBLine(local->fd)){
		priv->acecadUSB=1;

		local->read_input = USBReadInput;

		if (USBQueryHardware(local) != Success)
		{
			ErrorF ("Unable to query/initialize AceCad hardware.\n");
			goto SetupProc_fail;
		}
	} else
#endif
	{
		priv->acecadUSB=0;

		local->read_input = ReadInput;
		
		speed = xf86SetIntOption(local->options, "ReportSpeed", 85 );

		switch (speed)
		{
		case 120:
			priv->acecadReportSpeed = 'Q';
			break;
		case 85:
			priv->acecadReportSpeed = 'R';
			break;
		case 10:
			priv->acecadReportSpeed = 'S';
			break;
		case 2:
			priv->acecadReportSpeed = 'T';
			break;
		default:
			priv->acecadReportSpeed = 'R';
			speed = 85;
			xf86Msg(X_CONFIG, "Acecad Tablet: ReportSpeed possible values:\n 120, 85, 10, 2 \n");         
		}

		xf86Msg(X_CONFIG, "Acecad Tablet report %d points/s\n", speed);         

		priv->buffer = XisbNew (local->fd, 200);

		/* 
		 * Verify that hardware is attached and fuctional
		 */
		if (QueryHardware(priv) != Success)
		{
			xf86Msg(X_ERROR,"Unable to query/initialize AceCad hardware.\n");
			goto SetupProc_fail;
		}
	}

	s = xf86FindOptionValue(local->options, "Mode");
	if (s && (xf86NameCmp(s, "Relative") == 0))
	{
		priv->flags = priv->flags & ~ABSOLUTE_FLAG;
	}
	else 
	{
		priv->flags = priv->flags | ABSOLUTE_FLAG;
	}

	xf86Msg(X_CONFIG, "Acecad Tablet is in %s mode\n",(priv->flags & ABSOLUTE_FLAG) ? "absolute" : "relative");         
	DBG (9, XisbTrace (priv->buffer, 1));

	local->history_size = xf86SetIntOption(local->options , "HistorySize", 0);

	xf86ProcessCommonOptions(local, local->options);

	local->flags |= XI86_CONFIGURED;
	
	if (local->fd != -1)
	{ 
		RemoveEnabledDevice (local->fd);
		if (priv->buffer)
		{
			XisbFree(priv->buffer);
			priv->buffer = NULL;
		}
		xf86CloseSerial(local->fd);
	}
	RemoveEnabledDevice (local->fd);
	local->fd = -1;
	return (local);

	/* 
	 * If something went wrong, cleanup and return NULL
	 */
  SetupProc_fail:
	if ((local) && (local->fd))
		xf86CloseSerial (local->fd);
	if (local)
		xf86DeleteInput (local, 0);

	if ((priv) && (priv->buffer))
		XisbFree (priv->buffer);
	if (priv)
		xfree (priv);
	return (NULL);
}

static Bool
DeviceControl (DeviceIntPtr dev, int mode)
{
	Bool	RetValue;

	switch (mode)
	{
	case DEVICE_INIT:
		DeviceInit (dev);
		RetValue = Success;
		break;
	case DEVICE_ON:
		RetValue = DeviceOn( dev );
		break;
	case DEVICE_OFF:
		RetValue = DeviceOff( dev );
		break;
	case DEVICE_CLOSE:
		RetValue = DeviceClose( dev );
		break;
	default:
		RetValue = BadValue;
	}

	return( RetValue );
}

static Bool
DeviceOn (DeviceIntPtr dev)
{
	char buffer[256];
	LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
	AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);

	xf86Msg(X_CONFIG, "Acecad Tablet Device On\n");

	local->fd = xf86OpenSerial(local->options);
	if (local->fd == -1)
	{
		xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
		return (!Success);
	}

	
	if (priv->acecadUSB==0){
		priv->buffer = XisbNew(local->fd, 200);
		if (!priv->buffer) 
			{
				xf86CloseSerial(local->fd);
				local->fd = -1;
				return (!Success);
			}

		/*Rets qu'a l'envoyer a la tablette */
		sprintf(buffer, "%s%c%c%c%c", acecad_initstr, priv->acecadReportSpeed ,ACECAD_INCREMENT, 32 + priv->acecadInc, (priv->flags & ABSOLUTE_FLAG)? ACECAD_ABSOLUTE: ACECAD_RELATIVE);
		XisbWrite (priv->buffer, (unsigned char *)buffer, strlen(buffer));
	}
	
	xf86FlushInput(local->fd);
	xf86AddEnabledDevice (local);
	dev->public.on = TRUE;
	return (Success);
}

static Bool
DeviceOff (DeviceIntPtr dev)
{
	LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
	AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);


	if (local->fd != -1)
        { 
	RemoveEnabledDevice (local->fd);
		if (priv->buffer)
		{
			XisbFree(priv->buffer);
			priv->buffer = NULL;
		}
		xf86CloseSerial(local->fd);
	}


	xf86RemoveEnabledDevice (local);
	dev->public.on = FALSE;
	return (Success);
}

static Bool
DeviceClose (DeviceIntPtr dev)
{
	xf86Msg(X_CONFIG, "Acecad Tablet Device Close\n");
	return (Success);
}

static void 
ControlProc(DeviceIntPtr	device,
		   PtrCtrl	*ctrl)
{
	xf86Msg(X_CONFIG, "Acecad Tablet Control Proc\n");
}

static Bool
DeviceInit (DeviceIntPtr dev)
{
	int rx, ry;
	LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
	AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
	unsigned char map[] =
	{0, 1, 2, 3};

	xf86Msg(X_CONFIG, "Acecad Tablet Device Init\n");

	/* 3 boutons */
	if (InitButtonClassDeviceStruct (dev, 3, map) == FALSE)
	{
		ErrorF ("Unable to allocate AceCad ButtonClassDeviceStruct\n");
		return !Success;
	}

	if (InitFocusClassDeviceStruct (dev) == FALSE)
	{
		ErrorF("Unable to allocate AceCad FocusClassDeviceStruct\n");
		return !Success;
	}

	if (InitPtrFeedbackClassDeviceStruct(dev,
	    				     ControlProc) == FALSE) {
		ErrorF("unable to init ptr feedback\n");
		return !Success;
	}


	/* 3 axes */
	if (InitValuatorClassDeviceStruct (dev, 3, xf86GetMotionEvents,
			local->history_size,
			((priv->flags & ABSOLUTE_FLAG)? Absolute: Relative)|OutOfProximity)
			 == FALSE)
	{
		ErrorF ("Unable to allocate AceCad ValuatorClassDeviceStruct\n");
		return !Success;
	}
	else
	{

		InitValuatorAxisStruct(dev,
			   0,
			   0,			/* min val */
			   priv->acecadMaxX,	/* max val */
			   1000,		/* resolution */
			   0,			/* min_res */
			   1000);		/* max_res */
		InitValuatorAxisStruct(dev,
			   1,
			   0,			/* min val */
			   priv->acecadMaxY,	/* max val */
			   1000,		/* resolution */
			   0,			/* min_res */
			   1000);		/* max_res */
		InitValuatorAxisStruct(dev,
			   2,
			   0,			/* min val */
			   priv->acecadMaxZ,	/* max val */
			   1000,		/* resolution */
			   0,			/* min_res */
			   1000);		/* max_res */

	}

        if (InitProximityClassDeviceStruct (dev) == FALSE)
        {
                ErrorF ("Unable to allocate ProximityClassDeviceStruct\n");
                return !Success;
        }

	xf86MotionHistoryAllocate (local);


	/* On ne peut pas calculer l'increment avant, faute d'ecran pour
	connaitre la taille... */

        if (priv->acecadInc > 95)
		priv->acecadInc = 95;
	if (priv->acecadInc < 1)
	{
		/* guess the best increment value given video mode */
		rx=priv->acecadMaxX / screenInfo.screens[0]->width;
		ry=priv->acecadMaxY / screenInfo.screens[0]->height;
		if (rx < ry)
			priv->acecadInc = rx;
		else
			priv->acecadInc = ry;
		if (priv->acecadInc < 1)
			priv->acecadInc = 1;
	}

	xf86Msg(X_CONFIG, "Acecad Tablet Increment: %d\n",priv->acecadInc);

	return (Success);
}

static void
ReadInput (LocalDevicePtr local)
{
	int x, y, z;
	int prox, buttons;
	int is_core_pointer, is_absolute;
	AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);

	/*xf86Msg(X_CONFIG, "Acecad Tablet Read Input\n");*/

	is_absolute = (priv->flags & ABSOLUTE_FLAG);
	is_core_pointer = xf86IsCorePointer(local->dev);

	/* 
	 * set blocking to -1 on the first call because we know there is data to
	 * read. Xisb automatically clears it after one successful read so that
	 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
	 * read from blocking indefinately.
	 */
	XisbBlockDuration (priv->buffer, -1);
	
	while (AceCadGetPacket (priv) == Success)
	{
		x = (int)priv->packet[1] | ((int)priv->packet[2] << 7);
		y = (int)priv->packet[3] | ((int)priv->packet[4] << 7);

	    if (!(priv->flags & ABSOLUTE_FLAG))
		{
			x = priv->packet[0] & XSIGN_BIT? x:-x;
			y = priv->packet[0] & YSIGN_BIT? y:-y;
		}
		else
		{
		y = priv->acecadMaxY - y ;
		}
		

		z = ((int)priv->packet[5] << 2) |
			(((int)priv->packet[6] & 0x01) << 1) |
			(((int)priv->packet[6] & 0x10) >> 4);

		buttons = ((int)priv->packet[0] & 0x07) |
			((int)priv->packet[6] & 0x02 << 2);

		prox = (priv->packet[0] & PROXIMITY_BIT)? 0: 1;

		if (prox)
		{
			if (!(priv->acecadOldProximity))
				if (!is_core_pointer)
				{
					/*xf86Msg(X_CONFIG, "Acecad Tablet ProxIN %d %d %d\n",x, y, z);*/
					xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
				}

			if ((is_absolute && ((priv->acecadOldX != x) || (priv->acecadOldY != y) || (priv->acecadOldZ != z)))
				|| (!is_absolute && (x || y)))
			{
				if (is_absolute || priv->acecadOldProximity)
				{
					/*xf86Msg(X_CONFIG, "Acecad Tablet Motion %d %d %d\n", x, y, z);*/
					xf86PostMotionEvent(local->dev, is_absolute, 0, 3, x, y, z);
				}
			}

			if (priv->acecadOldButtons != buttons)
			{
				int	delta;

				delta = buttons ^ priv->acecadOldButtons;
				while(delta)
				{
					int id;

					id=ffs(delta);
					delta &= ~(1 << (id-1));

					/*xf86Msg(X_CONFIG, "Acecad Tablet Button %d 0x%x\n",id,(buttons&(1<<(id-1))));*/
					xf86PostButtonEvent(local->dev, is_absolute, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
				}
			}

			priv->acecadOldButtons = buttons;
			priv->acecadOldX = x;
			priv->acecadOldY = y;
			priv->acecadOldZ = z;
			priv->acecadOldProximity = prox;
		}
		else
		{
			if (!is_core_pointer)
				if (priv->acecadOldProximity)
				{
					/*xf86Msg(X_CONFIG, "Acecad Tablet ProxOUT %d %d %d\n",x, y, z);*/
					xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
				}
			priv->acecadOldProximity = 0;
		}
	}
	/*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
}

#ifdef LINUX_INPUT
#define set_bit(byte,nb,bit)	(bit ? byte | (1<<nb) : byte & (~(1<<nb)))
static void
USBReadInput (LocalDevicePtr local)
{
	int len;
	struct input_event * event;
	char eventbuf[sizeof(struct input_event) * MAX_EVENTS];
	AceCadPrivatePtr priv = (AceCadPrivatePtr) (local->private);
	int x = priv->acecadOldX;
	int y = priv->acecadOldY;
	int z = priv->acecadOldZ;
	int prox = priv->acecadOldProximity;
	int buttons = priv->acecadOldButtons;
	int is_core_pointer;

	is_core_pointer = xf86IsCorePointer(local->dev);

	SYSCALL(len = read(local->fd, eventbuf, sizeof(eventbuf)));

	if (len <= 0) {
		ErrorF("Error reading wacom device : %s\n", strerror(errno));
		return;
	}

	for (event=(struct input_event *)eventbuf;
	 event<(struct input_event *)(eventbuf+len); event++) {
	 
		switch (event->type) {
		case EV_ABS:
		    switch (event->code) {
		    case ABS_X:
			x = event->value;
			break;

		    case ABS_Y:
			y = event->value;
			break;

		    case ABS_PRESSURE:
			z = event->value;
			break;

		    case ABS_MISC:
			break;

		    }
		    break; /* EV_ABS */

		case EV_KEY:
		    switch (event->code) {
		    case BTN_TOOL_PEN:
			prox = event->value;
			break;

		    case BTN_TOUCH:
			buttons=set_bit(buttons,0,event->value);
			break;

		    case BTN_STYLUS:
			buttons=set_bit(buttons,1,event->value);
			break;

		    case BTN_STYLUS2:
			buttons=set_bit(buttons,2,event->value);
			break;
		    }
		    break; /* EV_KEY */
		default:
		    xf86Msg(X_ERROR, "UNKNOWN event->code=%d\n", event->code);
		} /* switch event->type */

		/* ABS_MISC is the event terminator */
		if (event->type != EV_ABS || event->code != ABS_MISC) {
		    continue;
		}

		if (prox)
		{
			if (!(priv->acecadOldProximity))
				if (!is_core_pointer)
				{
					xf86PostProximityEvent(local->dev, 1, 0, 3 , x, y, z);
				}


			xf86PostMotionEvent(local->dev, 1, 0, 3, x, y, z);

			if (priv->acecadOldButtons != buttons)
			{
				int	delta;

				delta = buttons ^ priv->acecadOldButtons;
				while(delta)
				{
					int id;

					id=ffs(delta);
					delta &= ~(1 << (id-1));

					xf86PostButtonEvent(local->dev, 1, id, (buttons&(1<<(id-1))), 0, 3, x, y,z);
				}
			}
		}
		else
		{
			if (!is_core_pointer)
				if (priv->acecadOldProximity)
				{
					xf86PostProximityEvent(local->dev, 0, 0, 3, x,y,z);
				}
			priv->acecadOldProximity = 0;
		}

		priv->acecadOldButtons = buttons;
		priv->acecadOldX = x;
		priv->acecadOldY = y;
		priv->acecadOldZ = z;
		priv->acecadOldProximity = prox;
	}
	/*xf86Msg(X_CONFIG, "Acecad Tablet Sortie Read Input\n");*/
}
#endif

static void
CloseProc (LocalDevicePtr local)
{
}

/* 
 * The ConvertProc function may need to be tailored for your device.
 * This function converts the device's valuator outputs to x and y coordinates
 * to simulate mouse events.
 */
static Bool
ConvertProc (LocalDevicePtr local,
			 int first,
			 int num,
			 int v0,
			 int v1,
			 int v2,
			 int v3,
			 int v4,
			 int v5,
			 int *x,
			 int *y)
{
    AceCadPrivatePtr	priv = (AceCadPrivatePtr)(local->private);

    *x = v0 * screenInfo.screens[0]->width / priv->acecadMaxX;
    *y = v1 * screenInfo.screens[0]->height / priv->acecadMaxY;
    return TRUE;
}


static Bool
ReverseConvertProc(		LocalDevicePtr	local,
			int		  x,
			int		  y,
			int		  *valuators)
{
    AceCadPrivatePtr	priv = (AceCadPrivatePtr)(local->private);

    valuators[0] = x * priv->acecadMaxX / screenInfo.screens[0]->width;
    valuators[1] = y * priv->acecadMaxY / screenInfo.screens[0]->height;

    return TRUE;
}


#define WriteString(str)\
XisbWrite (priv->buffer, (unsigned char *)(str), strlen(str))


static Bool
QueryHardware (AceCadPrivatePtr priv)
{	
	
	/* Reset */
	WriteString("z0");
	
	/* Wait */
	milisleep (250);
	
	/* Prompt Mode in order to not be disturbed */
	WriteString(ACECAD_PROMPT_MODE);
	
	/* Flush */
	while(XisbRead(priv->buffer)>=0);
	
	/* Ask for Config packet*/
	WriteString(ACECAD_CONFIG);
	
	/* Read the packet */
	XisbBlockDuration (priv->buffer, 1000000);
	NewPacket (priv);

	/*xf86Msg(X_CONFIG, "Acecad Tablet init envoyé \n");*/

	if ((AceCadGetPacket (priv) == Success))
	{
		priv->acecadMaxX = (int)priv->packet[1] + ((int)priv->packet[2] << 7);
		priv->acecadMaxY = (int)priv->packet[3] + ((int)priv->packet[4] << 7);
		priv->acecadMaxZ = 512;
		xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d\n",priv->acecadMaxX,priv->acecadMaxY);
	}
	else
		return (!Success);
		
	/*xf86Msg(X_CONFIG, "Acecad Tablet query hardware fini \n");*/
	return (Success);
}

#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define test_bit(bit, array)	((array[LONG(bit)] >> OFF(bit)) & 1)
#define OFF(x)  ((x)%BITS_PER_LONG)
#define LONG(x) ((x)/BITS_PER_LONG)

#ifdef LINUX_INPUT
static Bool
USBQueryHardware (LocalDevicePtr local)
{	
	AceCadPrivatePtr priv = (AceCadPrivatePtr) local->private;
	unsigned long	bit[EV_MAX][NBITS(KEY_MAX)];
	int			i, j;
	int			abs[5];
	char		name[256] = "Unknown";

	ioctl(local->fd, EVIOCGNAME(sizeof(name)), name);
	xf86Msg(X_CONFIG, "Kernel Input device name: \"%s\"\n", name);

	memset(bit, 0, sizeof(bit));
	ioctl(local->fd, EVIOCGBIT(0, EV_MAX), bit[0]);

    for (i = 0; i < EV_MAX; i++)
	if (test_bit(i, bit[0])) {
	    ioctl(local->fd, EVIOCGBIT(i, KEY_MAX), bit[i]);
	    for (j = 0; j < KEY_MAX; j++) 
		if (test_bit(j, bit[i])) {
		    if (i == EV_ABS) {
			ioctl(local->fd, EVIOCGABS(j), abs);
			switch (j) {
			case ABS_X:
				priv->acecadMaxX = abs[2];
			break;
			    
			case ABS_Y:
				priv->acecadMaxY = abs[2];
			break;
			    
			case ABS_PRESSURE:
				priv->acecadMaxZ = abs[2];
			break;
			}
		    }
		}
	}
    
	xf86Msg(X_CONFIG, "Acecad Tablet MaxX:%d MaxY:%d MaxZ:%d\n",priv->acecadMaxX,priv->acecadMaxY,priv->acecadMaxZ);
	return (Success);
}
#endif

static void
NewPacket (AceCadPrivatePtr priv)
{
    priv->packeti = 0;
}

static Bool
AceCadGetPacket (AceCadPrivatePtr priv)
{
	int count = 0;
	int c = 0;

	while((c = XisbRead(priv->buffer))>=0 )
	{
	
		/* 
		 * fail after 500 bytes so the server doesn't hang forever if a
		 * device sends bad data.
		 */
		if (count++ > 500)
		{
			NewPacket (priv);
			return (!Success);
		}

		if (c & PHASING_BIT)
		{
			NewPacket(priv);			
			
			/*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
			XisbBlockDuration (priv->buffer, 10000);
			priv->packet[priv->packeti++] = c;
			count=ACECAD_PACKET_SIZE-1;
			while(count-- && (c = XisbRead(priv->buffer))>=0)
				{
				/*xf86Msg(X_CONFIG, "Push %2.2x\n",(char) c);*/
				priv->packet[priv->packeti++] = c;
				}
			XisbBlockDuration (priv->buffer, 0);
			if(c > 0)
				return (Success);
		}
	}
	return (!Success);
}