summaryrefslogtreecommitdiff
path: root/src/lib/ecore_fb/ecore_fb_ts.c
blob: b9880ec63e68d8282177f8c48b86bb0c4220935f (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_TSLIB
# include <tslib.h>
# include <errno.h>
#endif

#include "Ecore_Fb.h"
#include "ecore_fb_private.h"

typedef struct _Ecore_Fb_Ts_Event Ecore_Fb_Ts_Event;
typedef struct _Ecore_Fb_Ts_Calibrate Ecore_Fb_Ts_Calibrate;
typedef struct _Ecore_Fb_Ts_Backlight Ecore_Fb_Ts_Backlight;
typedef struct _Ecore_Fb_Ts_Contrast Ecore_Fb_Ts_Contrast;
typedef struct _Ecore_Fb_Ts_Led Ecore_Fb_Ts_Led;
typedef struct _Ecore_Fb_Ts_Flite Ecore_Fb_Ts_Flite;

struct _Ecore_Fb_Ts_Event
{
   unsigned short pressure;
   unsigned short x;
   unsigned short y;
   unsigned short _unused;
};

struct _Ecore_Fb_Ts_Calibrate
{
   int xscale;
   int xtrans;
   int yscale;
   int ytrans;
   int xyswap;
};

struct _Ecore_Fb_Ts_Backlight
{
   int           on;
   unsigned char brightness;
};

struct _Ecore_Fb_Ts_Contrast
{
   unsigned char contrast;
};

struct _Ecore_Fb_Ts_Led
{
   unsigned char on;
   unsigned char blink_time;
   unsigned char on_time;
   unsigned char off_time;
};

struct _Ecore_Fb_Ts_Flite
{
   unsigned char mode;
   unsigned char pwr;
   unsigned char brightness;
};

static Eina_Bool _ecore_fb_ts_fd_handler(void *data, Ecore_Fd_Handler *fd_handler);
static int _ecore_fb_ts_fd = -1;
static int _ecore_fb_ts_apply_cal = 0;
#ifndef HAVE_TSLIB
static int _ecore_fb_ts_event_byte_count = 0;
static Ecore_Fb_Ts_Event _ecore_fb_ts_event;
#endif
static Ecore_Fb_Ts_Calibrate _ecore_fb_ts_cal = {1,1,0,0,0};
static Ecore_Fd_Handler *_ecore_fb_ts_fd_handler_handle = NULL;

#ifdef HAVE_TSLIB
struct tsdev *_ecore_fb_tslib_tsdev = NULL;
struct ts_sample _ecore_fb_tslib_event;
#endif

static double _ecore_fb_double_click_time = 0.25;
static void *_ecore_fb_ts_event_window = NULL;

EAPI int
ecore_fb_ts_init(void)
{
#ifdef HAVE_TSLIB
   char *tslib_tsdevice = NULL;
   if (
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
       (getuid() == geteuid()) &&
#endif
       ((tslib_tsdevice = getenv("TSLIB_TSDEVICE"))))
     {
        printf( "ECORE_FB: TSLIB_TSDEVICE = '%s'\n", tslib_tsdevice );
        _ecore_fb_tslib_tsdev = ts_open( tslib_tsdevice, 1 ); /* 1 = nonblocking, 0 = blocking */

        if ( !_ecore_fb_tslib_tsdev )
          {
             printf( "ECORE_FB: Can't ts_open (%s)\n", strerror( errno ) );
             return 0;
          }

        if ( ts_config( _ecore_fb_tslib_tsdev ) )
          {
             printf( "ECORE_FB: Can't ts_config (%s)\n", strerror( errno ) );
             return 0;
          }
        _ecore_fb_ts_fd = ts_fd( _ecore_fb_tslib_tsdev );
        if ( _ecore_fb_ts_fd < 0 )
          {
             printf( "ECORE_FB: Can't open touchscreen (%s)\n", strerror( errno ) );
             return 0;
          }
     }
#else
   _ecore_fb_ts_fd = open("/dev/touchscreen/0", O_RDONLY);
#endif
   if (_ecore_fb_ts_fd >= 0)
     {
        _ecore_fb_ts_fd_handler_handle = ecore_main_fd_handler_add(_ecore_fb_ts_fd,
                                                                   ECORE_FD_READ,
                                                                   _ecore_fb_ts_fd_handler, NULL,
                                                                   NULL, NULL);
        if (!_ecore_fb_ts_fd_handler_handle)
          {
             close(_ecore_fb_ts_fd);
             return 0;
          }
        // FIXME _ecore_fb_kbd_fd = open("/dev/touchscreen/key", O_RDONLY);
        return 1;
     }
   return 0;
}

EAPI void
ecore_fb_ts_shutdown(void)
{
   if (_ecore_fb_ts_fd_handler_handle)
      ecore_main_fd_handler_del(_ecore_fb_ts_fd_handler_handle);
   if (_ecore_fb_ts_fd >= 0) close(_ecore_fb_ts_fd);
   _ecore_fb_ts_fd = -1;
   _ecore_fb_ts_fd_handler_handle = NULL;
   _ecore_fb_ts_event_window = NULL;
}

EAPI void
ecore_fb_ts_event_window_set(void *window)
{
   _ecore_fb_ts_event_window = window;
}

EAPI void *
ecore_fb_ts_event_window_get(void)
{
    return _ecore_fb_ts_event_window;
}

/**
 * @defgroup Ecore_FB_Calibrate_Group Framebuffer Calibration Functions
 * @ingroup Ecore_FB_Group
 *
 * Functions that calibrate the screen.
 */


/**
 * Calibrates the touschreen using the given parameters.
 * @param   xscale X scaling, where 256 = 1.0
 * @param   xtrans X translation.
 * @param   yscale Y scaling.
 * @param   ytrans Y translation.
 * @param   xyswap Swap X & Y flag.
 * @ingroup Ecore_FB_Calibrate_Group
 */
EAPI void
ecore_fb_touch_screen_calibrate_set(int xscale, int xtrans, int yscale, int ytrans, int xyswap)
{
   Ecore_Fb_Ts_Calibrate cal;

   if (_ecore_fb_ts_fd < 0) return;
   cal.xscale = xscale;
   cal.xtrans = xtrans;
   cal.yscale = yscale;
   cal.ytrans = ytrans;
   cal.xyswap = xyswap;
   if (ioctl(_ecore_fb_ts_fd, TS_SET_CAL, (void *)&cal))
     {
        _ecore_fb_ts_cal = cal;
        _ecore_fb_ts_apply_cal = 1;
     }
}

/**
 * Retrieves the calibration parameters of the touchscreen.
 * @param   xscale Pointer to an integer in which to store the X scaling.
 *                 Note that 256 = 1.0.
 * @param   xtrans Pointer to an integer in which to store the X translation.
 * @param   yscale Pointer to an integer in which to store the Y scaling.
 * @param   ytrans Pointer to an integer in which to store the Y translation.
 * @param   xyswap Pointer to an integer in which to store the Swap X & Y flag.
 * @ingroup Ecore_FB_Calibrate_Group
 */
EAPI void
ecore_fb_touch_screen_calibrate_get(int *xscale, int *xtrans, int *yscale, int *ytrans, int *xyswap)
{
   Ecore_Fb_Ts_Calibrate cal;

   if (_ecore_fb_ts_fd < 0) return;
   if (!_ecore_fb_ts_apply_cal)
     {
        if (ioctl(_ecore_fb_ts_fd, TS_GET_CAL, (void *)&cal))
           _ecore_fb_ts_cal = cal;
     }
   else
      cal = _ecore_fb_ts_cal;
   if (xscale) *xscale = cal.xscale;
   if (xtrans) *xtrans = cal.xtrans;
   if (yscale) *yscale = cal.yscale;
   if (ytrans) *ytrans = cal.ytrans;
   if (xyswap) *xyswap = cal.xyswap;
}

static Eina_Bool
_ecore_fb_ts_fd_handler(void *data EINA_UNUSED, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
{
   static int prev_x = 0, prev_y = 0, prev_pressure = 0;
   static double last_time = 0;
   static double last_last_time = 0;
   int v = 0;

   do
     {
        int x, y, pressure;
        int num;
        double t = 0.0;
        static int did_double = 0;
        static int did_triple = 0;

#ifdef HAVE_TSLIB
        if (_ecore_fb_ts_apply_cal)
           num = ts_read_raw(_ecore_fb_tslib_tsdev, &_ecore_fb_tslib_event, 1);
        else
           num = ts_read(_ecore_fb_tslib_tsdev, &_ecore_fb_tslib_event, 1);
        if (num != 1) return 1; /* no more samples at this time */
        x = _ecore_fb_tslib_event.x;
        y = _ecore_fb_tslib_event.y;
        pressure = _ecore_fb_tslib_event.pressure;
        v = 1; /* loop, there might be more samples */
#else
        char *ptr;
        ptr = (char *)&(_ecore_fb_ts_event);
        ptr += _ecore_fb_ts_event_byte_count;
        num = sizeof(Ecore_Fb_Ts_Event) - _ecore_fb_ts_event_byte_count;
        v = read(_ecore_fb_ts_fd, ptr, num);
        if (v < 0) return 1;
        _ecore_fb_ts_event_byte_count += v;
        if (v < num) return 1;
        _ecore_fb_ts_event_byte_count = 0;
        if (_ecore_fb_ts_apply_cal)
          {
             x = ((_ecore_fb_ts_cal.xscale * _ecore_fb_ts_event.x) >> 8) + _ecore_fb_ts_cal.xtrans;
             y = ((_ecore_fb_ts_cal.yscale * _ecore_fb_ts_event.y) >> 8) + _ecore_fb_ts_cal.ytrans;
          }
        else
          {
             x = _ecore_fb_ts_event.x;
             y = _ecore_fb_ts_event.y;
          }
        pressure = _ecore_fb_ts_event.pressure;
#endif
        t = ecore_loop_time_get();
        /* add event to queue */
        /* always add a move event */
        if ((pressure) || (prev_pressure))
          {
             /* MOVE: mouse is down and was */
             Ecore_Event_Mouse_Move *e;

             e = calloc(1, sizeof(Ecore_Event_Mouse_Move));
             if (!e) goto retry;
             e->x = x;
             e->y = y;
             e->root.x = e->x;
             e->root.y = e->y;
             e->window = 1;
             e->event_window = e->window;
             e->root_window = e->window;
             e->same_screen = 1;
             e->timestamp = ecore_loop_time_get() * 1000.0;
             ecore_event_add(ECORE_EVENT_MOUSE_MOVE, e, NULL, NULL);
          }
        if ((pressure) && (!prev_pressure))
          {
             /* DOWN: mouse is down, but was not now */
             Ecore_Event_Mouse_Button *e;

             e = calloc(1, sizeof(Ecore_Event_Mouse_Button));
             if (!e) goto retry;
             e->x = x;
             e->y = y;
             e->root.x = e->x;
             e->root.y = e->y;
             e->buttons = 1;
             if ((t - last_time) <= _ecore_fb_double_click_time)
               {
                  e->double_click = 1;
                  did_double = 1;
               }
             else
               {
                  did_double = 0;
                  did_triple = 0;
               }
             if ((t - last_last_time) <= (2 * _ecore_fb_double_click_time))
               {
                  did_triple = 1;
                  e->triple_click = 1;
               }
             else
               {
                  did_triple = 0;
               }
             e->window = 1;
             e->event_window = e->window;
             e->root_window = e->window;
             e->same_screen = 1;
             e->timestamp = ecore_loop_time_get() * 1000.0;
             ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, e, NULL, NULL);
          }
        else if ((!pressure) && (prev_pressure))
          {
             /* UP: mouse was down, but is not now */
             Ecore_Event_Mouse_Button *e;

             e = calloc(1, sizeof(Ecore_Event_Mouse_Button));
             if (!e) goto retry;
             e->x = prev_x;
             e->y = prev_y;
             e->root.x = e->x;
             e->root.y = e->y;
             e->buttons = 1;
             if (did_double)
                e->double_click = 1;
             if (did_triple)
                e->triple_click = 1;
             e->window = 1;
             e->event_window = e->window;
             e->root_window = e->window;
             e->same_screen = 1;
             e->timestamp = ecore_loop_time_get() * 1000.0;
             ecore_event_add(ECORE_EVENT_MOUSE_BUTTON_UP, e, NULL, NULL);
          }
        if (did_triple)
          {
             last_time = 0;
             last_last_time = 0;
          }
        else
          {
             last_last_time = last_time;
             last_time = t;
          }
retry:
        prev_x = x;
        prev_y = y;
        prev_pressure = pressure;
     }
   while (v > 0);
   return 1;
}