~ubuntu-branches/ubuntu/gutsy/xf86-input-evtouch/gutsy

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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
/*
 * Copyright 2004 by Kenan Esau <kenan.esau@conan.de>, Baltmannsweiler, 
 * Germany.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of copyright holders not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  The copyright holders
 * make no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define _evdev_touch_C_
/*****************************************************************************
 *        Standard Headers
 ****************************************************************************/
#define SYSCALL(call)        while(((call) == -1) && (errno == EINTR))

#include <X11/Xos.h>
#include <misc.h>
#include <xf86.h>
#define NEED_XF86_TYPES
#include <xf86_ansic.h>
#include <xf86_OSproc.h>
#include <xf86Xinput.h>
#include <xisb.h>
#include <exevents.h>
#include <string.h>


/*****************************************************************************
 *        Local Headers
 ****************************************************************************/
#include "libtouch.h"
#include "evtouch.h"

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

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


static InputInfoPtr
EVTouchPreInit(InputDriverPtr drv, IDevPtr dev, int flags);

static int debug_level = 0;

InputDriverRec EVTOUCH = {
        1,
        "evtouch",
        NULL,
        EVTouchPreInit,
        /*EVTouchUnInit*/ NULL,
        NULL,
        0
};

#ifdef XFree86LOADER
ModuleInfoRec EVTouchInfo = {
        1,
        "EVTOUCH",
        NULL,
        0,
        NULL /*EVTouchAvailableOptions*/,
};


static XF86ModuleVersionInfo VersionRec =
{
        "evtouch",
        "Kenan Esau",
        MODINFOSTRING1,
        MODINFOSTRING2,
        XF86_VERSION_CURRENT,
        0, 8, 3,
        ABI_CLASS_XINPUT,
        ABI_XINPUT_VERSION,
        MOD_CLASS_XINPUT,
        {0, 0, 0, 0}                                /* signature, to be patched into the file by
                                                     * a tool */
};


static pointer
Plug( pointer module,
      pointer options,
      int *errmaj,
      int *errmin )
{
        xf86AddModuleInfo(&EVTouchInfo, module);
        xf86AddInputDriver(&EVTOUCH, module, 0);
        return module;
}


static void
Unplug(pointer        p)
{
        DBGOUT(1, "EVTouch: Unplug\n");
}


XF86ModuleData evtouchModuleData = {&VersionRec, Plug, Unplug };

#endif /* XFree86LOADER */


static const char *default_options[] =
{
        "BaudRate", "9600",
        "StopBits", "1",
        "DataBits", "8",
        "Parity", "None",
        "Vmin", "5",
        "Vtime", "1",
        "FlowControl", "None"
};

static void
ControlProc(DeviceIntPtr device, PtrCtrl *ctrl);

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


static CARD32
emulate3Timer(OsTimerPtr timer, CARD32 now, pointer _local)
{
        int sigstate;

        DBGOUT(2,"EVTouch: %s\n", __FUNCTION__);

        LocalDevicePtr local = (LocalDevicePtr)_local;
        EVTouchPrivatePtr priv = (EVTouchPrivatePtr) local->private;

        sigstate = xf86BlockSIGIO();

        xf86PostMotionEvent(local->dev, TRUE, 0, 2, 
                            priv->cur_x, 
                            priv->cur_y);

        /* 
         * Emit a button press -- release is handled in EVTouchLBRBEvent
         */
        if ( ( priv->touch_flags & LB_STAT ) &&
             !( priv->touch_flags & RB_STAT ) ) {
                DBGOUT(2, "EVTouch: Left Press\n");
                xf86PostButtonEvent (local->dev, TRUE,
                                     1, 1, 0, 2, 
                                     priv->cur_x, 
                                     priv->cur_y);
        }

        if ( ( priv->touch_flags & RB_STAT ) &&
             !( priv->touch_flags & LB_STAT ) ) {
                DBGOUT(2, "EVTouch: Right Press\n");
                xf86PostButtonEvent (local->dev, TRUE,
                                     3, 1, 0, 2, 
                                     priv->cur_x, 
                                     priv->cur_y);
        }

        /*
          Handling "middle" button press
        */
        if ( ( priv->touch_flags & RB_STAT ) &&
             ( priv->touch_flags & LB_STAT ) ) {
                DBGOUT(2, "EVTouch: Middle Press\n");
                xf86PostButtonEvent (local->dev, TRUE,
                                     2, 1, 0, 2, 
                                     priv->cur_x, 
                                     priv->cur_y);
        }


        priv->emulate3_timer_expired = TRUE;
        xf86UnblockSIGIO(sigstate);
             
        return 0;
}




void EVTouchProcessAbs(EVTouchPrivatePtr priv)
{
        struct input_event *ev; /* packet being/just read */

        ev = &priv->ev;
        priv->old_x = priv->cur_x;
        priv->old_y = priv->cur_y;

        if (ev->code == ABS_X) {
                priv->cur_x = ev->value;
		libtouchSetXPos(priv->libtouch, priv->cur_x);
	}

        if (ev->code == ABS_Y) {
                priv->cur_y = ev->value;
		libtouchSetYPos(priv->libtouch, priv->cur_y);
	}

	if (ev->code == ABS_WHEEL) {
		LocalDevicePtr local = priv->local;

		if (ev->value > 0) {
			for (; ev->value > 0; ev->value--) {
				xf86PostButtonEvent (local->dev, TRUE,
						     4, 1, 0, 2, 
						     priv->cur_x, 
						     priv->cur_y);
				xf86PostButtonEvent (local->dev, TRUE,
						     4, 0, 0, 2, 
						     priv->cur_x, 
						     priv->cur_y);
			}
		} else if (ev->value < 0) {
			for (ev->value = -ev->value; ev->value > 0; ev->value--) {
				xf86PostButtonEvent (local->dev, TRUE,
						     5, 1, 0, 2, 
						     priv->cur_x, 
						     priv->cur_y);
				xf86PostButtonEvent (local->dev, TRUE,
						     5, 0, 0, 2, 
						     priv->cur_x, 
						     priv->cur_y);
			}
		}			
	}
}




void EVTouchProcessRel(EVTouchPrivatePtr priv)
{
        struct input_event *ev; /* packet being/just read */

        ev = &priv->ev;
        priv->old_x = priv->cur_x;
        priv->old_y = priv->cur_y;
        if ( ev->code == REL_X ) {
                priv->cur_x += ev->value;
                if (priv->cur_x > priv->max_x)
                        priv->cur_x = priv->max_x;
                if (priv->cur_x < priv->min_x)
                        priv->cur_x = priv->min_x;
                return;
        } 
        if ( ev->code == REL_Y ) {
                priv->cur_y += ev->value;
                if (priv->cur_y > priv->max_y)
                        priv->cur_y = priv->max_y;
                if (priv->cur_y < priv->min_y)
                        priv->cur_y = priv->min_y;
                return;
        }
	
        libtouchSetPos(priv->libtouch, priv->cur_x, priv->cur_y);
}




void EVTouchLBRBEvent(EVTouchPrivatePtr priv)
{
        struct input_event *ev; /* packet being/just read */
        LocalDevicePtr local = priv->local;
        static OsTimerPtr emulate3_timer = NULL;
        int btn = 0;  

        ev = &priv->ev;
        DBGOUT(2, "EVTouch: %s\n", __FUNCTION__);

        if (priv->emulate3) {
                if ( (ev->value==1) && (emulate3_timer==NULL) )
                        emulate3_timer = TimerSet(emulate3_timer, 0,
                                                  priv->emulate3_timeout,
                                                  emulate3Timer,
                                                  local);

                if ( (ev->value == 1) && (ev->code == BTN_LEFT) )
                        priv->touch_flags |= LB_STAT;
                if ( (ev->value == 1) && (ev->code == BTN_RIGHT) )
                        priv->touch_flags |= RB_STAT;

                if ( (ev->value == 0) && 
                     (priv->touch_flags & RB_STAT) && 
                     (priv->touch_flags & LB_STAT) ) {
                        DBGOUT(2, "EVTouch: Middle Release\n");
                        priv->touch_flags &= ~LB_STAT;
                        priv->touch_flags &= ~RB_STAT;
                        btn = 2;                  
                } else if ( (ev->value == 0) && (ev->code == BTN_LEFT) &&
                     (priv->touch_flags & LB_STAT) ) {
                        DBGOUT(2, "EVTouch: Left Release\n");
                        priv->touch_flags &= ~LB_STAT;
                        btn = 1;
                } else if ( (ev->value == 0) && (ev->code == BTN_RIGHT) &&
                     (priv->touch_flags & RB_STAT) ) {
                        DBGOUT(2, "EVTouch: Right Release\n");
                        priv->touch_flags &= ~RB_STAT;
                        btn = 3;
                }                
                if (ev->value == 0) {
                        TimerFree(emulate3_timer);
                        emulate3_timer=NULL;
                        priv->emulate3_timer_expired = FALSE;
                        xf86PostButtonEvent(local->dev, TRUE,
                                            btn, ev->value, 0, 2, 
                                            priv->cur_x, 
                                            priv->cur_y);
                }
                
        } else {
                if (ev->code == BTN_LEFT) {
                        xf86PostButtonEvent(local->dev, TRUE,
                                            1, ev->value, 0, 2, 
                                            priv->cur_x, 
                                            priv->cur_y);
                }

		if (ev->code == BTN_MIDDLE) {
			xf86PostButtonEvent(local->dev, TRUE,
					    2, ev->value, 0, 2,
					    priv->cur_x,
					    priv->cur_y);
		}

                if (ev->code == BTN_RIGHT) {
                        xf86PostButtonEvent (local->dev, TRUE,
                                             3, ev->value, 0, 2,
                                             priv->cur_x, 
                                             priv->cur_y);
                }
        }
}




void EVTouchProcessKey(EVTouchPrivatePtr priv)
{
        struct input_event *ev; /* packet being/just read */

        DBGOUT(2, "EVTouch: %s\n", __FUNCTION__);
        ev = &priv->ev;
        if ( (ev->code == BTN_LEFT) || 
             (ev->code == BTN_RIGHT) ||
             (ev->code == BTN_MIDDLE) ) {

                /* give lb and rb-events some special treatment 
                   (emulate3 or not, ...) */
                EVTouchLBRBEvent(priv);
                return;
        }

#ifdef _0_
        switch (ev->code) {
        case BTN_SIDE:
                break;
        case BTN_EXTRA:
                break;
        case BTN_FORWARD:
                break;
        case BTN_BACK:
                break;
        case BTN_TASK:
                break;
        default:
                return;
        }
        xf86PostButtonEvent(local->dev, TRUE,
                            btn, ev->value, 0, 2, 
                            priv->cur_x, 
                            priv->cur_y);
#endif

        return;
}




static Bool
DeviceOn (DeviceIntPtr dev)
{
        LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
        EVTouchPrivatePtr priv = (EVTouchPrivatePtr) (local->private);
        
        local->fd = xf86OpenSerial(local->options);

        DBGOUT(2, "EVTouch: Device ON\n");

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

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


        if (!priv->buffer) 
        {
                xf86CloseSerial(local->fd);
                local->fd = -1;
                return (!Success);
        }
        xf86FlushInput(local->fd);

#ifndef XFREE86_V4
        xf86AddEnabledDevice(local);
#else
        AddEnabledDevice(local->fd);
#endif


        dev->public.on = TRUE;

        return (Success);
}




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

        DBGOUT(2, "EVTouch: Device OFF\n");

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

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

        if ( (priv->calibrate) && (priv->fifo>0) ){
                SYSCALL(close (priv->fifo));
        }

        return (Success);
}




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

   
        /* 
         * these have to be here instead of in the SetupProc, because when the
         * SetupProc is run at server startup, screenInfo is not setup yet
         */

        ScrnInfoPtr   pScrn = xf86Screens[priv->screen_num];

        priv->phys_width = pScrn->currentMode->HDisplay;  /* physical screen resolution */
        priv->phys_height = pScrn->currentMode->VDisplay;
        priv->screen_width  = pScrn->virtualX;     /* It's the virtual screen size ! */
        priv->screen_height = pScrn->virtualY;
        priv->pViewPort_X0  = &(pScrn->frameX0);   /* initialize the pointers to the viewport coords */
        if ( (priv->screen_width != priv->phys_width) ||
             (priv->screen_height != priv->phys_height) ) 
              priv->virtual = 1;
        else  
                priv->virtual = 0;

        priv->pViewPort_Y0  = &(pScrn->frameY0);
        priv->pViewPort_X1  = &(pScrn->frameX1);
        priv->pViewPort_Y1  = &(pScrn->frameY1);

        DBGOUT(2, "EVTouch: DeviceInit\n");
        DBGOUT(2, "EVTouch: Display X,Y: %d %d\n", priv->phys_width, priv->phys_height);
        DBGOUT(2, "EVTouch: Virtual X,Y: %d %d\n", priv->screen_width, priv->screen_height);
        DBGOUT(2, "EVTouch: DriverName, Rev.: %s %d\n", pScrn->driverName, pScrn->driverVersion);
        DBGOUT(2, "EVTouch: Viewport X0,Y0: %d %d\n", *priv->pViewPort_X0, *priv->pViewPort_Y0);
        DBGOUT(2, "EVTouch: Viewport X1,Y1: %d %d\n", *priv->pViewPort_X1, *priv->pViewPort_Y1);
        DBGOUT(2, "EVTouch: MaxValue H,V: %d %d\n", pScrn->maxHValue, pScrn->maxVValue);



        priv->screen_width = screenInfo.screens[priv->screen_num]->width;
        priv->screen_height = screenInfo.screens[priv->screen_num]->height;


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

        /* 
         * Device reports button press for 5 buttons.
         */
        if (InitButtonClassDeviceStruct (dev, 5, map) == FALSE)
        {
                ErrorF("Unable to allocate EVTouch touchscreen ButtonClassDeviceStruct\n");
                return !Success;
        }

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

        /* 
         * Device reports motions on 2 axes in absolute coordinates.
         * Axes min and max values are reported in raw coordinates.
         */
        if (InitValuatorClassDeviceStruct(dev, 2, xf86GetMotionEvents,
                                          local->history_size, Absolute) == FALSE)
        {
                ErrorF ("Unable to allocate EVTouch touchscreen ValuatorClassDeviceStruct\n");
                return !Success;
        }
        else
        {
                InitValuatorAxisStruct (dev, 0, priv->min_x, priv->max_x,
                                        1024,
                                        EV_AXIS_MIN_RES /* min_res */ ,
                                        EV_AXIS_MAX_RES /* max_res */ );
                InitValuatorAxisStruct (dev, 1, priv->min_y, priv->max_y,
                                        1024,
                                        EV_AXIS_MIN_RES /* min_res */ ,
                                        EV_AXIS_MAX_RES /* max_res */ );
        }

        /* Initial position of pointer on screen: Centered */
        priv->cur_x=(priv->max_x - priv->min_x)/2;
        priv->cur_y=(priv->max_y - priv->min_y)/2;
        libtouchSetPos(priv->libtouch, priv->cur_x, priv->cur_y);

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

        /* 
         * Allocate the motion events buffer.
         */
        xf86MotionHistoryAllocate (local);


        return (Success);
}




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:
        case DEVICE_CLOSE:
                RetValue = DeviceOff(dev);
                break;
        default:
                RetValue = BadValue;
        }

        return( RetValue );
}




static void
EVTouchNewPacket (EVTouchPrivatePtr priv)
{
        xf86memset(&priv->ev, 0, sizeof(struct input_event));
        priv->packeti = 0;
        priv->binary_pkt = FALSE;
}




static unsigned char
EVTouchRead(EVTouchPrivatePtr priv)
{
        unsigned char c;
        XisbBlockDuration (priv->buffer, EV_TIMEOUT);
        c = XisbRead(priv->buffer);
        return (c);
}



static Bool
EVTouchGetPacket (EVTouchPrivatePtr priv)
{
        static int count = 0;
        int c;
	CARD32 now;

        if (count==0) EVTouchNewPacket(priv);

        while ((c = XisbRead(priv->buffer)) >= 0) {
	        if (count == 0) {
		        now = GetTimeInMillis();
		        libtouchSetTime(priv->libtouch, now);
	        }

                ((char *)&priv->ev)[count] = c;
                count ++;

                if (sizeof(priv->ev) == count) {
                        count = 0;
                        EVTouchDumpPacketToLog(priv);
                        
                        return Success;
                }
        }
        return (!Success);
}




static void ReadInput (LocalDevicePtr local)
{
        struct input_event *ev; /* packet being/just read */

        EVTouchPrivatePtr priv = (EVTouchPrivatePtr) (local->private);


        /* 
         * 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 infinately.
         */
        XisbBlockDuration (priv->buffer, -1);
        while (EVTouchGetPacket (priv) == Success)
        {
                ev = &priv->ev;

                xf86XInputSetScreen(local, 
                                    priv->screen_num, 
                                    priv->cur_x, 
                                    priv->cur_y);
                
                xf86PostProximityEvent(local->dev, 1, 0, 2, 
                                       priv->cur_x, 
                                       priv->cur_y);

                priv->old_x = priv->cur_x;
                priv->old_y = priv->cur_y;
                switch (ev->type) {
                case EV_ABS:
                        EVTouchProcessAbs(priv);
                        break;
                case EV_REL:
                        EVTouchProcessRel(priv);
                        break;
		case EV_KEY:
			if (priv->ev.code == BTN_TOUCH) {
				if (priv->ev.value == 1) {
					priv->touch_flags |= TOUCHED;
					DBGOUT(2, "EVTouch: TOUCHED\n");
				}
				else {
					priv->touch_flags &= ~TOUCHED;
					DBGOUT(2, "EVTouch: UNTOUCHED\n");
				}
			}
			break;
		case EV_SYN:
			if ( priv->touch_flags & TOUCHED )
				libtouchTriggerSM(priv->libtouch, PEN_TOUCHED);
			else
				libtouchTriggerSM(priv->libtouch, PEN_UNTOUCHED);
                        break;
                }

                DBGOUT( 2, "EVTouch: setting (x/y)=(%d/%d)\n", 
                        priv->cur_x, priv->cur_y);

                /* 
                 * Send events.
                 *
                 * We *must* generate a motion before a button change if pointer
                 * location has changed as DIX assumes this. This is why we always
                 * emit a motion, regardless of the kind of packet processed.
                 */
                xf86PostMotionEvent (local->dev, TRUE, 0, 2, 
                                     priv->cur_x, 
                                     priv->cur_y);

                if (ev->type == EV_KEY)
                        EVTouchProcessKey(priv);


        }
}




static void
ControlProc(DeviceIntPtr device, PtrCtrl *ctrl)
{
        DBGOUT(2, "EVTouch: ControlProc\n");
}




static void
CloseProc (LocalDevicePtr local)
{
        xf86ErrorFVerb(2, "CLOSEPROC\n" );
}




static int
SwitchMode (ClientPtr client, DeviceIntPtr dev, int mode)
{
        LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
        EVTouchPrivatePtr priv = (EVTouchPrivatePtr) (local->private);


        if ((mode == TS_Raw) || (mode == TS_Scaled))
        {
                priv->reporting_mode = mode;
                return (Success);
        }
        else if ((mode == SendCoreEvents) || (mode == DontSendCoreEvents))
        {
                xf86XInputSetSendCoreEvents (local, (mode == SendCoreEvents));
                return (Success);
        }
        else
                return (!Success);
}




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 )
{
        /*
          correction factors depending on current position of pointer
        */
        float cx[3];
        float cy[3];
        float dx = 0, dy = 0;

        int max_x, max_y;
        int xc, yc;
        int screen_width  = 0;
        int screen_height = 0;
#ifdef EVDBG
        int i = 0;
#endif

        EVTouchPrivatePtr priv = (EVTouchPrivatePtr) (local->private);  

        DBGOUT(2, "FIRST: v0=%d   v1=%d\n", v0, v1);

        /*correction of raw coordinates*/
        if ( (priv->fifo>0) && (priv->calibrate) ){
                DBGOUT(2, "writing to FIFO\n");
                SYSCALL(write (priv->fifo, &v0, sizeof(v0)));
                SYSCALL(write (priv->fifo, &v1, sizeof(v1)));
        }

        if (!priv->calibrate) {
                xc = v0 - priv->min_x;
                yc = v1 - priv->min_y;
        
                max_x = priv->max_x - priv->min_x;
                max_y = priv->max_y - priv->min_y;

                if (priv->rotate == EV_ROTATE_NONE) {
                        screen_width  = priv->phys_width;
                        screen_height = priv->phys_height;
                } else {
                        screen_width  = priv->phys_height;
                        screen_height = priv->phys_width;
                }


                if (xc < (max_x / 2)) {
                        /*
                          left
                        */
                        if (yc>(max_y / 2)) {
                                /*
                                  upper
                                */
                                cx[1] = ((float) xc / (max_x / 2) );
                                cx[0] = (float) 1 - cx[1];
                                cy[0] = ((float) (yc-(max_y/2)) / (max_y/2) );
                                cy[1] = (float) 1 - cy[0];

                                dx = ((float) (cx[1] * cy[0] * priv->diff[1][0]) +
                                      (float)(cx[0] * cy[0] * priv->diff[0][0]) +
                                      (float)(cx[1] * cy[1] * priv->diff[4][0]) +
                                      (float)(cx[0] * cy[1] * priv->diff[3][0]));
                                
                                dy = ((float) (cx[1] * cy[0] * priv->diff[1][1]) +
                                      (float)(cx[0] * cy[0] * priv->diff[0][1]) +
                                      (float)(cx[1] * cy[1] * priv->diff[4][1]) +
                                      (float)(cx[0] * cy[1] * priv->diff[3][1]));
                        }
                        else {
                                /*
                                  lower
                                */
                                cx[1] = ((float) xc / (max_x/2) );
                                cx[0] = (float) 1 - cx[1];
                                cy[0] = ((float) yc / (max_y/2) );
                                cy[1] = (float) 1 - cy[0];

                                dx = ((float) (cx[1] * cy[0] * priv->diff[4][0]) +
                                      (float)(cx[0] * cy[0] * priv->diff[3][0]) +
                                      (float)(cx[1] * cy[1] * priv->diff[7][0]) +
                                      (float)(cx[0] * cy[1] * priv->diff[6][0]));
                                
                                dy = ((float) (cx[1] * cy[0] * priv->diff[4][1]) +
                                      (float)(cx[0] * cy[0] * priv->diff[3][1]) +
                                      (float)(cx[1] * cy[1] * priv->diff[7][1]) +
                                      (float)(cx[0] * cy[1] * priv->diff[6][1]));

                        }
                } else {
                        /*
                          right
                        */
                        if (yc>(max_y/2)) {
                                /*
                                  upper
                                */
                                cx[1] = ((float) (xc-(max_x/2)) / (max_x/2) );
                                cx[0] = (float)1 - cx[1];
                                cy[0] = ((float) (yc-(max_y/2)) / (max_y/2) );
                                cy[1] = (float)1 - cy[0];

                                dx = ((float) (cx[1] * cy[0] * priv->diff[2][0]) +
				      (float)(cx[0] * cy[0] * priv->diff[1][0]) +
				      (float)(cx[1] * cy[1] * priv->diff[5][0]) +
				      (float)(cx[0] * cy[1] * priv->diff[4][0]));
                        
                                dy = ((float) (cx[1] * cy[0] * priv->diff[2][1]) +
				      (float)(cx[0] * cy[0] * priv->diff[1][1]) +
                                      (float)(cx[1] * cy[1] * priv->diff[5][1]) +
                                      (float)(cx[0] * cy[1] * priv->diff[4][1]));

                                DBGOUT(2, "TEST: dx=%f   dy=%f\n", dx, dy);
                        } else {
                                /*
                                  lower
                                */
                                cx[1] = ((float) (xc-(max_x/2)) / (max_x/2) );
                                cx[0] = (float) 1 - cx[1];
                                cy[0] = ((float) yc / (max_y/2) );
                                cy[1] = (float) 1 - cy[0];

                                dx = ((float) (cx[1] * cy[0] * priv->diff[5][0]) +
                                      (float)(cx[0] * cy[0] * priv->diff[4][0]) +
                                      (float)(cx[1] * cy[1] * priv->diff[8][0]) +
                                      (float)(cx[0] * cy[1] * priv->diff[7][0]));
                                
                                dy = ((float) (cx[1] * cy[0] * priv->diff[5][1]) +
                                      (float)(cx[0] * cy[0] * priv->diff[4][1]) +
                                      (float)(cx[1] * cy[1] * priv->diff[8][1]) +
                                      (float)(cx[0] * cy[1] * priv->diff[7][1]));
                        }
                }


#ifdef EVDBG
                for (i=0; i<3; i++) 
                        xf86ErrorFVerb(2, "cx[%d]=%f   cy[%d]=%f\n", i, cx[i]
                                       ,i, cy[i]);
                
                DBGOUT(2, "ViewPort_X0=%d   ViewPort_Y0=%d\n", 
                    *(priv->pViewPort_X0), 
                    *(priv->pViewPort_Y0));
                DBGOUT(2, "dx=%f   dy=%f\n", dx, dy);
#endif

                xc = ( ((float)xc / max_x) * screen_width ) + dx;
                yc = ( ((float)yc / max_y) * screen_height) + dy;

                if (priv->swap_y == TRUE)
                        yc = screen_height - yc;

                /* ususally we DON'T swap x -- but if swap_x is 1 
                   => go on and swap */
                if (priv->swap_x == TRUE)
                        xc = screen_width - xc;

                /* rotation mixes x and y up a bit */
                if (priv->rotate == EV_ROTATE_CW) {
                        v0 = yc;
                        v1 = screen_width - xc;
                } else if (priv->rotate == EV_ROTATE_CCW) {
                        v0 = screen_height - yc;
                        v1 = xc;
                } else {
                        v0 = xc;
                        v1 = yc;
                }
        }

        DBGOUT(2, "FINAL: v0=%d   v1=%d\n", v0, v1);

        *x = v0;
        *y = v1;

        return (TRUE);
}




static Bool
QueryHardware (LocalDevicePtr local)
{
        xf86ErrorFVerb(2, "QUERY HARDWARE\n" );

        return Success;
}




static InputInfoPtr
EVTouchPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
{
        /* LocalDevicePtr local; */
        InputInfoPtr local;
        EVTouchPrivatePtr priv;

        int i = 0;
        char *s;
        char tmp_str[8];
        int timeo = 0;

        priv = xcalloc (1, sizeof (EVTouchPrivateRec));
        if (!priv)
                return NULL;

        local = xf86AllocateInput(drv, 0);
        if (!local) {
                xfree(priv);
                return NULL;
        }

        local->name = dev->identifier;
        local->type_name = XI_TOUCHSCREEN;
        local->device_control = DeviceControl;
        local->read_input = ReadInput;
        local->control_proc = NULL; /* ControlProc; */
        local->close_proc = CloseProc;
        local->switch_mode = SwitchMode;
        local->conversion_proc = ConvertProc;
        local->reverse_conversion_proc = NULL;
        local->fd = -1;
        local->dev = NULL;
        local->private = priv;
        priv->local = local;
        local->private_flags = 0;
        local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
        local->conf_idev = dev;

        xf86CollectInputOptions(local, default_options, NULL);

        xf86OptionListReport(local->options);

        local->fd = xf86OpenSerial (local->options);
        if (local->fd == -1)
        {
                ErrorF ("EVTouch driver unable to open device\n");
                goto SetupProc_fail;
        }
        xf86ErrorFVerb( 3, "evdev device opened successfully\n" );

	priv->libtouch = xcalloc(1, sizeof(LibTouchRec));
	libtouchInit(priv->libtouch, local);
        priv->calibrate = xf86SetIntOption(local->options, "Calibrate", 0);
        priv->min_x = xf86SetIntOption(local->options, "MinX", 0 );
        priv->max_x = xf86SetIntOption(local->options, "MaxX", EV_SCREEN_WIDTH );
        priv->min_y = xf86SetIntOption(local->options, "MinY", 0 );
        priv->max_y = xf86SetIntOption(local->options, "MaxY", EV_SCREEN_HEIGHT );
        priv->screen_num    = xf86SetIntOption(local->options, "ScreenNumber", 0 );
        priv->button_number = xf86SetIntOption(local->options, "ButtonNumber", 2 );

        priv->emulate3      = xf86SetBoolOption(local->options, "Emulate3Buttons", TRUE);
        priv->emulate3_timeout = xf86SetIntOption(local->options, 
                                                  "Emulate3Timeout", 50);
        priv->buttonless     = xf86SetBoolOption(local->options, "ButtonlessDevice", FALSE);

        debug_level = xf86SetIntOption(local->options, "DebugLevel", 0);
        libtouchSetDebugLevel(debug_level);

        timeo = xf86SetIntOption(local->options, "TapTimer", 90);
        libtouchSetTapTimeo(priv->libtouch, timeo);

        timeo = xf86SetIntOption(local->options, "LongtouchTimer", 160);
        libtouchSetLongtouchTimeo(priv->libtouch, timeo);

        libtouchSetMoveLimit(priv->libtouch, 
                             xf86SetIntOption( local->options, 
                                               "MoveLimit", 180 ));

        priv->rotate     = EV_ROTATE_NONE;
        s = xf86FindOptionValue(local->options, "Rotate");
        if (s) {
                if (xf86NameCmp(s, "CW") == 0) {
                        priv->rotate = EV_ROTATE_CW;                           
                } else if (xf86NameCmp(s, "CCW") == 0 ) {
                        priv->rotate = EV_ROTATE_CCW;
                } 
        }

        if (priv->rotate == EV_ROTATE_NONE) {
                priv->max_rel_x = priv->max_x;
                priv->max_rel_y = priv->max_y;
                priv->min_rel_x = priv->min_x;
                priv->min_rel_y = priv->min_y;             
        } else {
                priv->max_rel_x = priv->max_y;
                priv->max_rel_y = priv->max_x;
                priv->min_rel_x = priv->min_y;
                priv->min_rel_y = priv->min_x;             
        }

        priv->swap_y = xf86SetBoolOption(local->options, "SwapY", FALSE);
        priv->swap_x = xf86SetBoolOption(local->options, "SwapX", FALSE);

        /* 
           get calibration parameters from XF86Config 
        */
        for (i=0; i<9; i++){
                sprintf(tmp_str, "x%d", i);
                priv->diff[i][0] = xf86SetIntOption( local->options,
                                                     tmp_str, 0 );
                sprintf(tmp_str, "y%d", i);
                priv->diff[i][1] = xf86SetIntOption( local->options,
                                                     tmp_str, 0 );
                DBGOUT(2, "(diff[%d][0]/diff[%d][1])=(%d/%d)\n", i, i, 
                    priv->diff[i][0], priv->diff[i][1]);
        }
        
        xf86AlwaysCore(local, TRUE);

        s = xf86FindOptionValue (local->options, "ReportingMode");
        if ((s) && (xf86NameCmp (s, "raw") == 0))
                priv->reporting_mode = TS_Raw;
        else
                priv->reporting_mode = TS_Scaled;

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

        DBG (9, XisbTrace (priv->buffer, 1));

        if (QueryHardware(local) != Success)
        {
                ErrorF ("Unable to query/initialize EVTouch hardware.\n");
                goto SetupProc_fail;
        }

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

        /* prepare to process touch packets */
        EVTouchNewPacket (priv);

        /* 
           if started in calibration-mode:
           - open named pipe 
        */
        if (priv->calibrate) {
                SYSCALL(priv->fifo=open("/tmp/ev_calibrate", O_RDWR, 0));
                if (priv->fifo < 0)
                        xf86ErrorFVerb(2, "open FIFO FAILED\n");
        }

        /* this results in an xstrdup that must be freed later */
        local->name = xf86SetStrOption( local->options, "DeviceName", "EVTouch TouchScreen" );
        xf86ProcessCommonOptions(local, local->options);
        local->flags |= XI86_CONFIGURED;

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

 SetupProc_fail:
        if ((local) && (local->fd))
                xf86CloseSerial (local->fd);
        if ((local) && (local->name))
                xfree (local->name);

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