~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/programs/Xserver/hw/xfree86/input/magictouch/xf86MagicTouch.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $XFree86: xc/programs/Xserver/hw/xfree86/input/magictouch/xf86MagicTouch.c,v 1.3 2003/01/12 03:55:50 tsi Exp $
 
3
 */
 
4
 
 
5
#include <xf86Version.h>
 
6
#if XF86_VERSION_CURRENT >= XF86_VERSION_NUMERIC(3,9,0,0,0)
 
7
#define XFREE86_V4
 
8
#endif
 
9
 
 
10
#ifdef XFREE86_V4
 
11
 
 
12
#ifndef XFree86LOADER
 
13
#include <errno.h>
 
14
#include <string.h>
 
15
#include <unistd.h>
 
16
#endif
 
17
 
 
18
#include <misc.h>
 
19
#include <xf86.h>
 
20
#if !defined(DGUX)
 
21
#include <xf86_ansic.h>
 
22
#endif
 
23
#include <xf86_OSproc.h>
 
24
#include <xf86Xinput.h>
 
25
#include <exevents.h>
 
26
 
 
27
#ifdef XFree86LOADER
 
28
#include <xf86Module.h>
 
29
#endif
 
30
 
 
31
#else /* XFREE86_V4 */
 
32
 
 
33
#include "Xos.h"
 
34
#include <signal.h>
 
35
#include <stdio.h>
 
36
#define  NEED_EVENTS
 
37
#include "X.h"
 
38
#include "Xproto.h"
 
39
#include "inputstr.h"
 
40
#include "scrnintstr.h"
 
41
 
 
42
#include "XI.h"
 
43
#include "XIproto.h"
 
44
 
 
45
#if defined(sun) && !defined(i386)
 
46
#include <errno.h>
 
47
#include <termio.h>
 
48
#include <fcntl.h>
 
49
#include <ctype.h>
 
50
 
 
51
#include "extio.h"
 
52
#else /* defined(sun) && !defined(i386) */
 
53
#include "compiler.h"
 
54
 
 
55
#include "xf86.h"
 
56
#include "xf86Procs.h"
 
57
#include "xf86_OSlib.h"
 
58
#include "xf86_Config.h"
 
59
#include "xf86Xinput.h"
 
60
#include "xf86Version.h"
 
61
#endif /* defined(sun) && !defined(i386) */
 
62
 
 
63
#if !defined(sun) || defined(i386)
 
64
#include "os.h"
 
65
#include "osdep.h"
 
66
#include "exevents.h"
 
67
 
 
68
#include "extnsionst.h"
 
69
#include "extinit.h"
 
70
#endif /* !defined(sun) || defined(i386) */
 
71
 
 
72
#endif /* XFREE86_V4 */
 
73
 
 
74
 
 
75
#ifndef XFREE86_V4
 
76
#if !defined(sun) || defined(i386)
 
77
/*
 
78
 ***************************************************************************
 
79
 *
 
80
 * Configuration descriptor.
 
81
 *
 
82
 ***************************************************************************
 
83
 */
 
84
 
 
85
#define PORT            1
 
86
#define MAGIC_DEVICE_NAME       2
 
87
#define SCREEN_NO       3
 
88
#define MAXX            4
 
89
#define MAXY            5
 
90
#define MINX            6
 
91
#define MINY            7
 
92
#define DEBUG_LEVEL     8
 
93
#define HISTORY_SIZE    9
 
94
#define LINK_SPEED      10
 
95
#define ALWAYS_CORE     11
 
96
#define SWAP_AXES       12
 
97
#define N_SAMPLE_X      13
 
98
#define N_SAMPLE_Y      14
 
99
 
 
100
static SymTabRec MagicTab[] = {
 
101
  { ENDSUBSECTION,     "endsubsection" },
 
102
  { PORT,              "port" },
 
103
  { MAGIC_DEVICE_NAME,   "devicename" },
 
104
  { SCREEN_NO,         "screenno" },
 
105
  { MAXX,              "maximumxposition" },
 
106
  { MAXY,              "maximumyposition" },
 
107
  { MINX,              "minimumxposition" },
 
108
  { MINY,              "minimumyposition" },
 
109
  { DEBUG_LEVEL,       "debuglevel" },
 
110
  { HISTORY_SIZE,      "historysize" },
 
111
  { LINK_SPEED,        "linkspeed" },
 
112
  { ALWAYS_CORE,       "alwayscore" },
 
113
  { SWAP_AXES,         "swapxy" },
 
114
  { N_SAMPLE_X,        "numsamplex" },
 
115
  { N_SAMPLE_Y,        "numsampley" },
 
116
  { -1,                "" },
 
117
};
 
118
 
 
119
 
 
120
#define LS1200          1
 
121
#define LS2400          2
 
122
#define LS4800          3
 
123
#define LS9600          4
 
124
 
 
125
static SymTabRec LinkSpeedTab[] = {
 
126
  { LS1200,     "b1200" },
 
127
  { LS2400,     "b2400" },
 
128
  { LS4800,     "b4800" },
 
129
  { LS9600,     "b9600" }
 
130
};
 
131
#endif /* !defined(sun) || defined(i386) */
 
132
 
 
133
/*
 
134
 * This struct connects a line speed with
 
135
 * a compatible motion packet delay. The
 
136
 * driver will attempt to enforce a correct
 
137
 * delay (according to this table) in order to
 
138
 * avoid losing data in the touchscreen controller.
 
139
 * LinkSpeedValues should be kept in sync with
 
140
 * LinkSpeedTab.
 
141
 */
 
142
typedef struct {
 
143
  int   speed;
 
144
  int   delay;
 
145
} LinkParameterStruct;
 
146
 
 
147
static LinkParameterStruct      LinkSpeedValues[] = {
 
148
  { B1200, 8 },
 
149
  { B2400, 4 },
 
150
  { B4800, 2 },
 
151
  { B9600, 1 }
 
152
};
 
153
#endif /* XFREE86_V4 */
 
154
 
 
155
 
 
156
/*
 
157
 ***************************************************************************
 
158
 *
 
159
 * Default constants.
 
160
 *
 
161
 ***************************************************************************
 
162
 */
 
163
#define MAGIC_PACKET_SIZE       5
 
164
#define MAGIC_PORT              "/dev/magictouch"
 
165
#define MAGIC_LINK_SPEED        B9600
 
166
 
 
167
/* First byte of the packet */
 
168
#define MGCT_TOUCH            0x01
 
169
#define MGCT_RKEY                     0x02
 
170
#define MGCT_LKEY                     0x04
 
171
#define MGCT_MKEY                     0x08
 
172
#define MGCT_CLICK_STATUS       0x10
 
173
 
 
174
#define MEDIE_X         20
 
175
#define MEDIE_Y         20
 
176
 
 
177
 
 
178
/*
 
179
 ***************************************************************************
 
180
 *
 
181
 * Usefull macros.
 
182
 *
 
183
 ***************************************************************************
 
184
 */
 
185
#define WORD_ASSEMBLY(byte1, byte2)     (((byte2) << 8) | (byte1))
 
186
#define SYSCALL(call)                   while(((call) == -1) && (errno == EINTR))
 
187
 
 
188
/* This one is handy, thanx Fred ! */
 
189
#ifdef DBG
 
190
#undef DBG
 
191
#endif
 
192
#ifdef DEBUG
 
193
#undef DEBUG
 
194
#endif
 
195
 
 
196
static int      debug_level = 0;
 
197
#define DEBUG 1
 
198
#if DEBUG
 
199
#define DBG(lvl, f) {if ((lvl) == debug_level) { f; } }
 
200
#else
 
201
#define DBG(lvl, f)
 
202
#endif
 
203
 
 
204
 
 
205
#ifdef XFREE86_V4
 
206
#undef SYSCALL
 
207
#undef read
 
208
#undef write
 
209
#undef close
 
210
#undef strdup
 
211
#define SYSCALL(call) call
 
212
#define read(fd, ptr, num) xf86ReadSerial(fd, ptr, num)
 
213
#define write(fd, ptr, num) xf86WriteSerial(fd, ptr, num)
 
214
#define close(fd) xf86CloseSerial(fd)
 
215
#define strdup(str) xf86strdup(str)
 
216
#endif
 
217
 
 
218
 
 
219
                                        
 
220
 
 
221
 
 
222
 
 
223
/*
 
224
 ***************************************************************************
 
225
 *
 
226
 * Device private records.
 
227
 *
 
228
 ***************************************************************************
 
229
 */
 
230
typedef struct _MagicPrivateRec {
 
231
  char  *input_dev;                             /* The touchscreen input tty                    */
 
232
  int           min_x;                          /* Minimum x reported by calibration            */
 
233
  int           max_x;                          /* Maximum x                                    */
 
234
  int           min_y;                          /* Minimum y reported by calibration            */
 
235
  int           max_y;                          /* Maximum y                                    */
 
236
#ifndef XFREE86_V4
 
237
  int           link_speed;                     /* Speed of the RS232 link connecting the ts.   */
 
238
#endif
 
239
  int           screen_no;                      /* Screen associated with the device            */
 
240
  int           screen_width;                   /* Width of the associated X screen             */
 
241
  int           screen_height;                  /* Height of the screen                         */
 
242
  int           swap_axes;                      /* Swap X an Y axes if != 0 */
 
243
  unsigned char packet_buf[MAGIC_PACKET_SIZE];  /* Assembly buffer                              */
 
244
  int           packet_pos;
 
245
  int           buf_x[MEDIE_X], i_x, num_medie_x;
 
246
  int           buf_y[MEDIE_Y], i_y, num_medie_y;
 
247
  Bool          first_x, first_y;
 
248
  Bool          first_entry;
 
249
  Bool          e_presente;
 
250
  Bool          click_on;
 
251
} MagicPrivateRec, *MagicPrivatePtr;
 
252
 
 
253
 
 
254
#ifndef XFREE86_V4
 
255
#if !defined(sun) || defined(i386)
 
256
/*
 
257
 ****************************************************************************
 
258
 * xf86MagicConfig --
 
259
 *      Configure the device driver from configuration data
 
260
 ****************************************************************************
 
261
 */
 
262
static Bool
 
263
xf86MagicConfig(LocalDevicePtr    *array,
 
264
              int               inx,
 
265
              int               max,
 
266
              LexPtr            val)
 
267
{
 
268
  LocalDevicePtr        local = array[inx];
 
269
  MagicPrivatePtr       priv = (MagicPrivatePtr)(local->private);
 
270
  int                   token;
 
271
  
 
272
  while ((token = xf86GetToken(MagicTab)) != ENDSUBSECTION) {
 
273
    switch(token) {
 
274
      
 
275
    case PORT:
 
276
      if (xf86GetToken(NULL) != STRING) {
 
277
        xf86ConfigError("MagicTouch input port expected");
 
278
      }
 
279
      priv->input_dev = strdup(val->str);       
 
280
      if (xf86Verbose) {
 
281
        ErrorF("%s MagicTouch input port: %s\n",
 
282
               XCONFIG_GIVEN, priv->input_dev);
 
283
      }
 
284
      break;
 
285
 
 
286
    case MAGIC_DEVICE_NAME:
 
287
      if (xf86GetToken(NULL) != STRING) {
 
288
        xf86ConfigError("Magictouch device name expected");
 
289
      }
 
290
      local->name = strdup(val->str);
 
291
      if (xf86Verbose) {
 
292
        ErrorF("%s MagicTouch X device name: %s\n",
 
293
               XCONFIG_GIVEN, local->name);
 
294
      }
 
295
      break;
 
296
 
 
297
    case SCREEN_NO:
 
298
      if (xf86GetToken(NULL) != NUMBER) {
 
299
        xf86ConfigError("MagicTouch screen number expected");
 
300
      }
 
301
      priv->screen_no = val->num;
 
302
      if (xf86Verbose) {
 
303
        ErrorF("%s MagicTouch associated screen: %d\n",
 
304
               XCONFIG_GIVEN, priv->screen_no);
 
305
      }
 
306
      break;
 
307
 
 
308
    case LINK_SPEED:
 
309
      {
 
310
        int     ltoken = xf86GetToken(LinkSpeedTab);
 
311
        if (ltoken == EOF ||
 
312
            ltoken == STRING ||
 
313
            ltoken == NUMBER) {
 
314
          xf86ConfigError("MagicTouch link speed expected");
 
315
        }
 
316
        priv->link_speed = LinkSpeedValues[ltoken-1].speed;
 
317
        if (xf86Verbose) {
 
318
          ErrorF("%s MagicTouch link speed: %s bps\n",
 
319
                 XCONFIG_GIVEN, (LinkSpeedTab[ltoken-1].name)+1);
 
320
        }
 
321
      }
 
322
      break;
 
323
      
 
324
    case MAXX:
 
325
      if (xf86GetToken(NULL) != NUMBER) {
 
326
        xf86ConfigError("MagicTouch maximum x position expected");
 
327
      }
 
328
      priv->max_x = val->num;
 
329
      if (xf86Verbose) {
 
330
        ErrorF("%s MagicTouch maximum x position: %d\n",
 
331
               XCONFIG_GIVEN, priv->max_x);
 
332
      }
 
333
     break;
 
334
      
 
335
    case MAXY:
 
336
      if (xf86GetToken(NULL) != NUMBER) {
 
337
        xf86ConfigError("MagicTouch maximum y position expected");
 
338
      }
 
339
      priv->max_y = val->num;
 
340
      if (xf86Verbose) {
 
341
        ErrorF("%s MagicTouch maximum y position: %d\n",
 
342
               XCONFIG_GIVEN, priv->max_y);
 
343
      }
 
344
     break;
 
345
      
 
346
    case MINX:
 
347
      if (xf86GetToken(NULL) != NUMBER) {
 
348
        xf86ConfigError("MagicTouch minimum x position expected");
 
349
      }
 
350
      priv->min_x = val->num;
 
351
      if (xf86Verbose) {
 
352
        ErrorF("%s MagicTouch minimum x position: %d\n",
 
353
               XCONFIG_GIVEN, priv->min_x);
 
354
      }
 
355
     break;
 
356
      
 
357
    case MINY:
 
358
      if (xf86GetToken(NULL) != NUMBER) {
 
359
        xf86ConfigError("MagicTouch minimum y position expected");
 
360
      }
 
361
      priv->min_y = val->num;
 
362
      if (xf86Verbose) {
 
363
        ErrorF("%s MagicTouch minimum y position: %d\n",
 
364
               XCONFIG_GIVEN, priv->min_y);
 
365
      }
 
366
     break;
 
367
      
 
368
    case DEBUG_LEVEL:
 
369
      if (xf86GetToken(NULL) != NUMBER) {
 
370
        xf86ConfigError("MagicTouch driver debug level expected");
 
371
      }
 
372
      debug_level = val->num;
 
373
      if (xf86Verbose) {
 
374
#if DEBUG
 
375
        ErrorF("%s MagicTouch debug level sets to %d\n", XCONFIG_GIVEN,
 
376
               debug_level);      
 
377
#else
 
378
        ErrorF("%s MagicTouch debug not available\n",
 
379
                XCONFIG_GIVEN, debug_level);      
 
380
#endif
 
381
      }
 
382
      break;
 
383
 
 
384
    case HISTORY_SIZE:
 
385
      if (xf86GetToken(NULL) != NUMBER) {
 
386
        xf86ConfigError("MagicTouch motion history size expected");
 
387
      }
 
388
      local->history_size = val->num;
 
389
      if (xf86Verbose) {
 
390
        ErrorF("%s MagicTouch motion history size is %d\n", XCONFIG_GIVEN,
 
391
               local->history_size);
 
392
      }
 
393
      break;
 
394
            
 
395
    case ALWAYS_CORE:
 
396
      xf86AlwaysCore(local, TRUE);
 
397
      if (xf86Verbose) {
 
398
        ErrorF("%s MagicTouch device will always stays core pointer\n",
 
399
               XCONFIG_GIVEN);
 
400
      }
 
401
      break;
 
402
 
 
403
    case SWAP_AXES:
 
404
      priv->swap_axes = 1;
 
405
      if (xf86Verbose) {
 
406
        ErrorF("%s MagicTouch device will work with X and Y axes swapped\n",
 
407
               XCONFIG_GIVEN);
 
408
      }      
 
409
      break;
 
410
 
 
411
    case N_SAMPLE_X:
 
412
      if (xf86GetToken(NULL) != NUMBER) {
 
413
        xf86ConfigError("MagicTouch driver NumSamplesX expected");
 
414
      }
 
415
      priv->num_medie_x = val->num;
 
416
      
 
417
      
 
418
      if (priv->num_medie_x>MEDIE_X) 
 
419
          priv->num_medie_x=MEDIE_X;
 
420
      
 
421
      DBG(4,
 
422
        ErrorF("NumSampleX = %d\n", priv->num_medie_x)
 
423
        );
 
424
      
 
425
      break;
 
426
      
 
427
    case N_SAMPLE_Y:
 
428
      if (xf86GetToken(NULL) != NUMBER) {
 
429
        xf86ConfigError("MagicTouch driver NumSamplesY expected");
 
430
      }
 
431
      priv->num_medie_y = val->num;
 
432
      
 
433
      if (priv->num_medie_y>MEDIE_Y) 
 
434
          priv->num_medie_y=MEDIE_Y;
 
435
          
 
436
      DBG(4,
 
437
        ErrorF("NumSampleY = %d\n", priv->num_medie_y)
 
438
        );
 
439
      break;
 
440
 
 
441
    case EOF:
 
442
      FatalError("Unexpected EOF (missing EndSubSection)");
 
443
      break;
 
444
 
 
445
    default:
 
446
      xf86ConfigError("MagicTouch subsection keyword expected");
 
447
      break;
 
448
    }
 
449
  }
 
450
 
 
451
  if (priv->max_x - priv->min_x <= 0) {
 
452
    ErrorF("%s MagicTouch: reverse x mode (minimum x position >= maximum x position)\n",
 
453
           XCONFIG_GIVEN);
 
454
  }  
 
455
  if (priv->max_y - priv->min_y <= 0) {
 
456
    ErrorF("%s MagicTouch: reverse y mode (minimum y position >= maximum y position)\n",
 
457
           XCONFIG_GIVEN);
 
458
  }
 
459
    
 
460
  DBG(2, ErrorF("xf86MagicConfig port name=%s\n", priv->input_dev))
 
461
 
 
462
  return Success;
 
463
}
 
464
 
 
465
#endif /* !defined(sun) || defined(i386) */
 
466
#endif /* XFREE86_V4 */
 
467
 
 
468
/****************************************************************************
 
469
 *
 
470
 * xf86MagicQueryOK --
 
471
 *      Testa la presenza del touch controller. 
 
472
 *      Si osserva che al primo accesso al touch dopo l'accensione e' 
 
473
 *      presente nel buffer di ricezione il codice 0xF che identifica la
 
474
 *      vera presenza del touch controller.
 
475
 *      Dal secondo accesso in poi bisogna interrogare il touch controller
 
476
 *      per verificarne l'esistenza. 
 
477
 ****************************************************************************
 
478
 */
 
479
static Bool
 
480
xf86MagicQueryOK(int fd)
 
481
{
 
482
        Bool    ok;
 
483
        int     result;
 
484
        char    buf;
 
485
 
 
486
        ok = Success;
 
487
 
 
488
        /* Provo a leggere un byte dal buffer di ricezione */
 
489
        SYSCALL( result = read(fd, &buf, 1) );
 
490
        
 
491
        DBG(4, ErrorF("<<%s[%d]>> QueryOK: read --> %d\n", __FILE__, __LINE__, result) );
 
492
        
 
493
        /* Se result e' -1 vuol dire che non c'e' nessun carattere nel
 
494
         buffer. Allora X/Window e' stato avviato almeno una volta */
 
495
        if (result<0) {
 
496
                DBG(4,
 
497
                        ErrorF("Avvio n-esimo di X/Windows\n");
 
498
                        ErrorF("Controllo presenza Touch Controller\n")
 
499
                );
 
500
                
 
501
                /* Cerco il touch controller. Invio il carattere 0x00. */
 
502
                buf = 0;
 
503
                SYSCALL( result = write(fd, &buf, 1) );
 
504
                
 
505
                /* Attendo 20 ms per dare il tempo al touch controller di
 
506
                capire il comando */
 
507
                usleep(20000);
 
508
                
 
509
                /* Leggo la risposta */
 
510
                SYSCALL( result = read(fd, &buf, 1) );
 
511
 
 
512
                DBG(4,
 
513
                        ErrorF("QueryOK: buf==%X,  result==%d\n", buf, result)
 
514
                );
 
515
        }
 
516
                
 
517
        /* Se result<0 allora il touch controller non e' presente sul 
 
518
        disposito. Non posso proseguire */
 
519
        if (result<0) {
 
520
                DBG(4,
 
521
                        ErrorF("<<%s[%d]>> result<0\n", __FILE__, __LINE__)
 
522
                );
 
523
                ok = !Success;
 
524
        }
 
525
        /* Se il touch controller ha risposto allora controllo cosa ha 
 
526
        risposto */
 
527
        else {
 
528
                ok = (buf==0xF ? Success : !Success);
 
529
                DBG(4,
 
530
                        ErrorF("<<%s[%d]>> QueryOK buf==%x\n", __FILE__, __LINE__, buf)
 
531
                );
 
532
        }
 
533
 
 
534
        return ok;
 
535
}
 
536
 
 
537
/*
 
538
 ***********************************************************************
 
539
 *
 
540
 * xf86MagicControl
 
541
 *
 
542
 ***********************************************************************
 
543
 */
 
544
static Bool
 
545
xf86MagicControl(DeviceIntPtr dev,
 
546
                int mode)
 
547
{
 
548
        LocalDevicePtr  local = (LocalDevicePtr) dev->public.devicePrivate;
 
549
        MagicPrivatePtr priv = (MagicPrivatePtr)(local->private);
 
550
        unsigned char   map[] = { 0, 1 };
 
551
        unsigned char   req[MAGIC_PACKET_SIZE], replay[MAGIC_PACKET_SIZE];
 
552
        int            status_line;
 
553
        
 
554
        switch (mode) {
 
555
                case DEVICE_INIT:
 
556
                        DBG(2, ErrorF("MagicTouch init...\n") );
 
557
                        
 
558
                        /* Controlla il numero di schermo selezionato */
 
559
                        if (priv->screen_no >= screenInfo.numScreens || priv->screen_no<0)
 
560
                                priv->screen_no = 0;
 
561
                        /* Legge le dimensioni dello schermo */
 
562
                        priv->screen_width = screenInfo.screens[priv->screen_no]->width;
 
563
                        priv->screen_height = screenInfo.screens[priv->screen_no]->height;
 
564
                        
 
565
                        if (InitButtonClassDeviceStruct(dev, 1, map)==FALSE) {
 
566
                                ErrorF("Impossibile allocare ButtonClassDeviceStruct per MagicTouch\n");
 
567
                                return !Success;
 
568
                        }
 
569
                        
 
570
                        if (InitFocusClassDeviceStruct(dev)==FALSE) {
 
571
                                ErrorF("Impossibile allocare FocusClassDeviceStruct per MagicTouch\n");
 
572
                                return !Success;
 
573
                        }
 
574
                        
 
575
                        /*
 
576
                         * Il movimento viene eseguito su due assi in coordinate assolute.
 
577
                         */
 
578
                        if (InitValuatorClassDeviceStruct(dev, 2, xf86GetMotionEvents, local->history_size, Absolute) == FALSE ) 
 
579
                        {
 
580
                                ErrorF("MagicTouch ValuatorClassDeviceStruct: ERRORE\n");
 
581
                                return !Success;
 
582
                        }
 
583
                        else {
 
584
                                InitValuatorAxisStruct(dev, 0, priv->min_x, priv->max_x, 
 
585
                                                        9500, 
 
586
                                                        0,      /* min res */
 
587
                                                        9500    /* max res */);
 
588
                                                        
 
589
                                InitValuatorAxisStruct(dev, 1, priv->min_y, priv->max_y,
 
590
                                                        10500,
 
591
                                                        0,
 
592
                                                        10500);
 
593
                        }
 
594
                        
 
595
                        if (InitFocusClassDeviceStruct(dev)==FALSE) {
 
596
                                ErrorF("Impossibile allocare FocusClassDeviceStruct per MagicTouch\n");
 
597
                        }
 
598
                        
 
599
                        /*
 
600
                         * Alloca il buffer degli eventi spostamento
 
601
                         */
 
602
                        xf86MotionHistoryAllocate(local);
 
603
                        
 
604
#ifndef XFREE86_V4
 
605
                        AssignTypeAndName(dev, local->atom, local->name);
 
606
#endif /* XFREE86_V4 */
 
607
                        
 
608
                        DBG(2, ErrorF("MagicTouch INIT OK\n") );
 
609
                        
 
610
                        break; /* DEVICE_INIT*/
 
611
                        
 
612
                case DEVICE_ON:
 
613
                        DBG(2, ErrorF("MagicTouch ON\n") );
 
614
                        if (local->fd<0) {
 
615
#ifndef XFREE86_V4
 
616
                        struct termios termios_tty;
 
617
                        int             i,result;
 
618
#endif
 
619
                        
 
620
                        DBG(2, ErrorF("Opening device...\n") );
 
621
                        
 
622
#ifdef XFREE86_V4
 
623
                        local->fd = xf86OpenSerial(local->options);
 
624
                        if (local->fd<0) {
 
625
                                ErrorF("Impossibile aprire MagicTouch\n");
 
626
                                return !Success;
 
627
                        }
 
628
#else
 
629
                        SYSCALL( local->fd = open(priv->input_dev, O_RDWR | O_NDELAY, 0) );
 
630
                        if (local->fd<0) {
 
631
                                Error("Impossibile aprire MagicTouch\n");
 
632
                                return !Success;
 
633
                        }
 
634
                        
 
635
                        DBG(3, ErrorF("Provo a configurare il MagicTouch\n") );
 
636
                        
 
637
                        
 
638
                        memset(&termios_tty, 0, sizeof(termios_tty) );
 
639
                        termios_tty.c_iflag = 0;
 
640
                        termios_tty.c_cflag = priv->link_speed | CS8 | CREAD | CLOCAL;
 
641
                        termios_tty.c_oflag = 0;
 
642
                        termios_tty.c_lflag = 0;
 
643
                        termios_tty.c_cc[VTIME]=0;
 
644
                        termios_tty.c_cc[VMIN]=1;
 
645
                        
 
646
                        /*
 
647
                         * Attivo l'RTS per abilitare il touch controller
 
648
                         */
 
649
#if 0
 
650
         SYSCALL( result = ioctl(local->fd, TIOCMGET, &status_line) );
 
651
                        if (result<0) 
 
652
                        {
 
653
                                Error("Impossibile leggere stato linee seriale\n");
 
654
                                close(local->fd);
 
655
                                return !Success;
 
656
                        }
 
657
                        status_line |= TIOCM_RTS;
 
658
                        SYSCALL( result = ioctl(local->fd, TIOCMSET, &status_line) );
 
659
                        if (result<0) 
 
660
                        {
 
661
                                Error("Impossibile settare stato linee seriale\n");
 
662
                                close(local->fd);
 
663
                                return !Success;
 
664
                        }
 
665
#endif
 
666
                        
 
667
                        SYSCALL( result = tcsetattr(local->fd, TCSANOW, &termios_tty) );
 
668
                        if (result<0) 
 
669
                        {
 
670
                                Error("Impossibile configurare MagicTouch\n");
 
671
                                close(local->fd);
 
672
                                return !Success;
 
673
                        }
 
674
#endif
 
675
                        
 
676
                                                                
 
677
                        /* Controlla se e' presente il touch controller.*/
 
678
                        req[0] = 0x00;
 
679
                        if (xf86MagicQueryOK(local->fd)!=Success) {
 
680
                                ErrorF("MagicTouch not present\n");
 
681
                                close(local->fd);
 
682
                                return !Success;
 
683
                        }
 
684
 
 
685
                        priv->e_presente = TRUE;                        
 
686
                        
 
687
                        AddEnabledDevice(local->fd);
 
688
                        dev->public.on = TRUE;                  
 
689
                        } /* if (local->fd<0) */
 
690
                        break; /* DEVICE_ON */
 
691
                        
 
692
                case DEVICE_CLOSE:
 
693
                case DEVICE_OFF:
 
694
                        DBG(2, ErrorF("MagicTouch OFF\n") );
 
695
                        dev->public.on = FALSE;
 
696
                        if (local->fd>=0)
 
697
                           emoveEnabledDevice(local->fd);
 
698
                                
 
699
                        SYSCALL( close(local->fd) );
 
700
                        local->fd = -1;
 
701
                        DBG(2, ErrorF("OK\n") );
 
702
                        break; /* DEVICE_OFF*/
 
703
                        
 
704
                default:
 
705
                        ErrorF("unsupported mode %d\n", mode);
 
706
                        return !Success;
 
707
        } /* switch (mode) */
 
708
        
 
709
        return Success;
 
710
}
 
711
 
 
712
 
 
713
 
 
714
/*
 
715
 ***************************************************************************
 
716
 *
 
717
 * GetPacket --
 
718
 *
 
719
 ***************************************************************************
 
720
 */
 
721
static Bool
 
722
GetPacket(LocalDevicePtr local,  unsigned char *buffer, int *n_rx, int fd)
 
723
{
 
724
        int     num_bytes;
 
725
        int     i;
 
726
        Bool    ok;
 
727
        MagicPrivatePtr  priv=(MagicPrivatePtr) local->private;
 
728
 
 
729
        DBG(6, ErrorF("Entering GetPacket with packet_pos == %d\n", *n_rx) );
 
730
        
 
731
        SYSCALL( 
 
732
                num_bytes=read(fd, buffer+*n_rx, MAGIC_PACKET_SIZE-*n_rx) 
 
733
        );
 
734
        
 
735
        /* Se e' il primo ingresso nella procedura e ho letto un solo byte,
 
736
           allora e' arrivato lo 0x0F di risposta all-inizializzazione del
 
737
           touch controlloer */
 
738
        /* Sto gia' leggendo un pacchetto normale */            
 
739
        *n_rx += num_bytes;
 
740
        
 
741
        DBG(8,
 
742
                for (i=0; i<*n_rx; i++)
 
743
                        ErrorF("%3X", buffer[i]);
 
744
                ErrorF("\n")
 
745
        );
 
746
 
 
747
        ok = (*n_rx==MAGIC_PACKET_SIZE ? Success : !Success );
 
748
        
 
749
        if (ok==Success)
 
750
                *n_rx = 0;
 
751
        
 
752
        DBG(6, 
 
753
                if(ok==Success) 
 
754
                        ErrorF("GetPacket OK\n");
 
755
                else
 
756
                        ErrorF("GetPacket FAIL\n")
 
757
        );
 
758
          
 
759
        return ok;
 
760
}
 
761
 
 
762
/*
 
763
 ************************************************************************
 
764
 *
 
765
 * xf86MagicReadInput
 
766
 *
 
767
 ************************************************************************
 
768
 */
 
769
static 
 
770
int medie_x(LocalDevicePtr local, int x)
 
771
{
 
772
        int i,res;
 
773
        float medie;
 
774
        MagicPrivatePtr priv = (MagicPrivatePtr)(local->private);
 
775
        
 
776
        DBG(6, 
 
777
                ErrorF("Medie in X = %d\n", priv->num_medie_x)
 
778
        );
 
779
        
 
780
        if (priv->first_x) {
 
781
                priv->first_x = FALSE;
 
782
                for (i=0; i<priv->num_medie_x; i++)
 
783
                        priv->buf_x[i] = x;
 
784
                        
 
785
                res = x;
 
786
        }
 
787
        else {
 
788
                priv->buf_x[priv->i_x] = x;
 
789
                priv->i_x++;
 
790
                if (priv->i_x>=priv->num_medie_x)
 
791
                        priv->i_x = 0;
 
792
                        
 
793
                medie = 0.0;    
 
794
                for (i=0; i<priv->num_medie_x; i++)
 
795
                        medie += priv->buf_x[i];
 
796
                
 
797
                res = (int)(medie/priv->num_medie_x);
 
798
        }
 
799
        
 
800
        return res;
 
801
}
 
802
 
 
803
static
 
804
int medie_y(LocalDevicePtr local, int y)
 
805
{
 
806
        int i,res;
 
807
        float medie;
 
808
        MagicPrivatePtr priv = (MagicPrivatePtr)(local->private);
 
809
        
 
810
        DBG(6, 
 
811
                ErrorF("Medie in Y = %d\n", priv->num_medie_y)
 
812
        );
 
813
        
 
814
        if (priv->first_y) {
 
815
                priv->first_y = FALSE;
 
816
                for (i=0; i<priv->num_medie_y; i++)
 
817
                        priv->buf_y[i] = y;
 
818
                        
 
819
                res = y;
 
820
        }
 
821
        else {
 
822
                priv->buf_y[priv->i_y] = y;
 
823
                priv->i_y++;
 
824
                if (priv->i_y>=priv->num_medie_y)
 
825
                        priv->i_y = 0;
 
826
                        
 
827
                medie = 0.0;    
 
828
                for (i=0; i<priv->num_medie_y; i++)
 
829
                        medie += priv->buf_y[i];
 
830
                
 
831
                res = (int)(medie/priv->num_medie_y);
 
832
        }
 
833
        
 
834
        return res;
 
835
}
 
836
 
 
837
/*
 
838
static
 
839
int MAX(int x, int y)
 
840
{
 
841
  return (x>=y ? x : y);
 
842
}
 
843
*/
 
844
 
 
845
#define MAX(x,y) (x>=y ? x : y)
 
846
 
 
847
static void
 
848
xf86MagicReadInput(LocalDevicePtr       local)
 
849
{
 
850
        MagicPrivatePtr priv = (MagicPrivatePtr)(local->private);
 
851
        int             cur_x, cur_y;
 
852
        Bool            touch_now;
 
853
        static int      n_coms = 0;
 
854
 
 
855
        if (!priv->e_presente) {
 
856
                DBG(4,
 
857
                        ErrorF("<<%s[%d]>> ReadInput: Touch Controller non inizializzato\n")
 
858
                );
 
859
                return;
 
860
        }
 
861
                
 
862
        DBG(4, ErrorF("Entering ReadInput\n"));
 
863
        /*
 
864
         * Try to get a packet.
 
865
         */
 
866
        if (GetPacket(local, priv->packet_buf, &priv->packet_pos, local->fd)==Success) 
 
867
        {
 
868
                /* Calculate the (x,y) coord of pointer */
 
869
                cur_x = priv->packet_buf[1];
 
870
                cur_x <<= 6;
 
871
                cur_x |= priv->packet_buf[2];
 
872
                
 
873
                cur_y = priv->packet_buf[3];
 
874
                cur_y <<= 6;
 
875
                cur_y |= priv->packet_buf[4];
 
876
                
 
877
                touch_now = priv->packet_buf[0] & MGCT_TOUCH == MGCT_TOUCH;
 
878
                
 
879
                /* Se c'e' pressione sul touch inizio a calcolare la posizione
 
880
                   e a spostare il cursore grafico */
 
881
                if (touch_now) {
 
882
                        DBG(6, 
 
883
                                ErrorF("Touch premuto: medio i valori di posizione\n")
 
884
                        );
 
885
                        cur_x = medie_x(local, cur_x);
 
886
                        cur_y = medie_y(local, cur_y);
 
887
                }
 
888
                else {
 
889
                        DBG(6, 
 
890
                                ErrorF("Touch rilasciato:\n"
 
891
                                        "\tazzeramento buffer memoria\n"
 
892
                                        "\tposizionamento immediato\n")
 
893
                        );
 
894
                        
 
895
                        /* Se non ho pressione allora comando lo spostamento
 
896
                        del cursore senza mediare. Svuoto il buffer delle medie */
 
897
                        priv->first_x = TRUE;
 
898
                        priv->first_y = TRUE;
 
899
                }
 
900
                /* Comando lo spostamento */
 
901
                xf86PostMotionEvent(local->dev, TRUE, 0, 2, cur_x, cur_y);              
 
902
                /* comanda la pressione del tasto */
 
903
                
 
904
                DBG(9,
 
905
                        ErrorF("touch_now==%s\n", (touch_now==TRUE ? "TRUE" : "FALSE") )
 
906
                );
 
907
                if (touch_now!=priv->click_on) {
 
908
                        DBG(9,
 
909
                                ErrorF("Bottone == %s\n", (touch_now==TRUE ? "PREMUTO" : "RILASCAITO") )
 
910
                        );
 
911
                        priv->click_on = touch_now;
 
912
                        xf86PostButtonEvent(local->dev, TRUE, 1, touch_now, 0, 2, cur_x, cur_y);
 
913
                }
 
914
        } /* GetPacket */
 
915
}
 
916
 
 
917
 
 
918
/*
 
919
 ************************************************************************
 
920
 *
 
921
 * xf86MagicConvert
 
922
 *
 
923
 ************************************************************************
 
924
 */
 
925
static Bool
 
926
xf86MagicConvert(LocalDevicePtr local,
 
927
               int              first,
 
928
               int              num,
 
929
               int              v0,
 
930
               int              v1,
 
931
               int              v2,
 
932
               int              v3,
 
933
               int              v4,
 
934
               int              v5,
 
935
               int              *x,
 
936
               int              *y)
 
937
{
 
938
  MagicPrivatePtr       priv = (MagicPrivatePtr) local->private;
 
939
  int           width = priv->max_x - priv->min_x;
 
940
  int           height = priv->max_y - priv->min_y;
 
941
  int           input_x, input_y;
 
942
  
 
943
  if (first != 0 || num != 2) {
 
944
    return FALSE;
 
945
  }
 
946
 
 
947
  DBG(3, ErrorF("MagicConvert: v0(%d), v1(%d)\n",       v0, v1));
 
948
 
 
949
  if (priv->swap_axes) {
 
950
    input_x = v1;
 
951
    input_y = v0;
 
952
  }
 
953
  else {
 
954
    input_x = v0;
 
955
    input_y = v1;
 
956
  }
 
957
  *x = (priv->screen_width * (input_x - priv->min_x)) / width;
 
958
  *y = (priv->screen_height - (priv->screen_height * (input_y - priv->min_y)) / height);
 
959
  
 
960
#ifdef XFREE86_V4
 
961
  /*
 
962
   * Need to check if still on the correct screen.
 
963
   * This call is here so that this work can be done after
 
964
   * calib and before posting the event.
 
965
   */
 
966
  xf86XInputSetScreen(local, priv->screen_no, *x, *y);
 
967
#endif
 
968
  
 
969
  DBG(3, ErrorF("MagicConvert: x(%d), y(%d)\n", *x, *y));
 
970
 
 
971
  return TRUE;
 
972
}
 
973
 
 
974
 
 
975
 
 
976
/*
 
977
 ************************************************************************
 
978
 *
 
979
 * xf86MagicAllocate
 
980
 * 
 
981
 ************************************************************************
 
982
 */
 
983
static LocalDevicePtr
 
984
#ifndef XFREE86_V4
 
985
xf86MagicAllocate(void)
 
986
#else
 
987
xf86MagicAllocate(InputDriverPtr drv)
 
988
#endif
 
989
{
 
990
#ifndef XFREE86_V4
 
991
        LocalDevicePtr  local = (LocalDevicePtr) xalloc( sizeof(LocalDeviceRec) );
 
992
#else
 
993
        LocalDevicePtr  local = xf86AllocateInput(drv, 0);
 
994
#endif
 
995
 
 
996
        MagicPrivatePtr priv = (MagicPrivatePtr) xalloc( sizeof(MagicPrivateRec) );
 
997
 
 
998
        /* Controlla la corretta allocazione di buffers. Se uno dei buffers non
 
999
                e' stato allocato correttamente termina l'inizializzazione
 
1000
        */
 
1001
        if (!local) {
 
1002
                if (priv)
 
1003
                        xfree(priv);
 
1004
                return NULL;
 
1005
        }
 
1006
        
 
1007
        if (!priv) {
 
1008
                if (local)
 
1009
                        xfree(local);
 
1010
                return NULL;
 
1011
        }
 
1012
        
 
1013
        /* I buffers sono allocati correttamente */
 
1014
#ifdef XFREE86_V4
 
1015
        priv->input_dev = strdup(MAGIC_PORT);
 
1016
#else
 
1017
        priv->input_dev = MAGIC_PORT;
 
1018
        priv->link_speed = MAGIC_LINK_SPEED;
 
1019
#endif
 
1020
        
 
1021
        priv->min_x = 60;
 
1022
        priv->max_x = 960;
 
1023
        priv->min_y = 60;
 
1024
        priv->max_y = 960;
 
1025
        priv->screen_no = 0;
 
1026
        priv->screen_width = -1;
 
1027
        priv->screen_height = -1;
 
1028
        priv->swap_axes = 0;
 
1029
        priv->first_x = 
 
1030
        priv->first_y = TRUE;
 
1031
        priv->first_entry = TRUE;
 
1032
        priv->e_presente = FALSE;
 
1033
        priv->click_on = FALSE;
 
1034
        priv->i_x = 
 
1035
        priv->i_y = 0;
 
1036
        priv->packet_pos = 0;
 
1037
        bzero(priv->buf_x, MEDIE_X);
 
1038
        bzero(priv->buf_y, MEDIE_Y);
 
1039
        priv->num_medie_x = MEDIE_X;
 
1040
        priv->num_medie_y = MEDIE_Y;
 
1041
        
 
1042
        local->name = XI_TOUCHSCREEN;
 
1043
        local->flags = 0;
 
1044
        
 
1045
#ifndef XFREE86_V4
 
1046
#if !defined(sun) || defined(i386)
 
1047
        local->device_config = xf86MagicConfig; 
 
1048
#endif /* !defined(sun) || defined(i386) */     
 
1049
#endif /* XFREE86_V4*/
 
1050
        
 
1051
        local->device_control = xf86MagicControl;
 
1052
        local->read_input = xf86MagicReadInput;
 
1053
        local->control_proc = NULL;
 
1054
        local->close_proc = NULL;
 
1055
        local->switch_mode = NULL;
 
1056
        local->conversion_proc = xf86MagicConvert;
 
1057
        local->reverse_conversion_proc = NULL;
 
1058
        local->fd = -1;
 
1059
        local->atom = 0;
 
1060
        local->dev = NULL;
 
1061
        local->private = priv;
 
1062
        local->type_name = "MagicTouch";
 
1063
        local->history_size = 0;
 
1064
        
 
1065
        return local;
 
1066
        
 
1067
} /* xf86MagicAllocae */
 
1068
 
 
1069
 
 
1070
 
 
1071
#ifndef XFREE86_V4
 
1072
 
 
1073
/*
 
1074
 * Sezione relativa a X < 4.0.0
 
1075
 */
 
1076
 
 
1077
DeviceAssocRec magictouch_assoc = {
 
1078
        "magictouch",
 
1079
        xf86MagicAllocate
 
1080
};
 
1081
 
 
1082
#ifdef DYNAMIC_MODULE
 
1083
 
 
1084
#ifndef DLSYM_BUG
 
1085
int init_module(unsigned long server_version)
 
1086
#else
 
1087
int init_xf86Magic(unsigned long server_version)
 
1088
#endif
 
1089
{
 
1090
        /* Aggiunge l'assiocazione per il touchscreen */
 
1091
        xf86AddDeviceAssoc(&magictouch_assoc);
 
1092
        
 
1093
        /* Controlla la versione */ 
 
1094
        if (server_version != XF86_VERSION_CURRENT) {
 
1095
                ErrorF("Warining: MagicTouch module compiled for version %s\n", XF86_VERSION);
 
1096
                return 0;
 
1097
        }
 
1098
        return 1;
 
1099
}
 
1100
#endif /* DYNAMIC_MODULE */
 
1101
 
 
1102
#else
 
1103
/*
 
1104
 * Sezione relativa a X >= 4.0.0
 
1105
 */
 
1106
 
 
1107
#endif /* XFREE86_V4 */