forked from thenameisnigel-old/COT2-KitKat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.c
447 lines (399 loc) · 9.73 KB
/
touch.c
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
/* Touch functions */
/* Original by tdm/CyanogenMod Review:
* http://review.cyanogenmod.org/#/c/61359/1
*/
/* #define DEBUG_TOUCH_EVENTS */
/* Some extra input defines */
#ifndef ABS_MT_ANGLE
#define ABS_MT_ANGLE 0x38
#endif
/* Set colors if they aren't already defined */
#ifndef MENU_TEXT_COLOR
#define MENU_TEXT_COLOR 0, 191, 255, 255
#endif
#ifndef NORMAL_TEXT_COLOR
#define NORMAL_TEXT_COLOR 200, 200, 200, 255
#endif
#ifndef HEADER_TEXT_COLOR
#define HEADER_TEXT_COLOR NORMAL_TEXT_COLOR
#endif
#ifndef HILITE_TEXT_COLOR
#define HILITE_TEXT_COLOR 255, 255, 255, 255
#endif
#ifndef BACKGROUND_COLOR
#define BACKGROUND_COLOR 0, 0, 0, 255
#endif
#ifndef SEPARATOR_COLOR
#define SEPARATOR_COLOR 160, 160, 160, 255
#endif
int BATT_LINE, TIME_LINE, BATT_POS, TIME_POS;
struct point {
int x;
int y;
};
static struct point touch_min;
static struct point touch_max;
static int in_touch;
static int in_swipe;
static struct point touch_start;
static struct point touch_end;
static int touch_last_y;
static int min_x_swipe_px = 100;
static int min_y_swipe_px = 80;
static void touch_set_min_swipe_lengths() {
char value[PROPERTY_VALUE_MAX];
property_get("ro.sf.lcd_density", value, "0");
int screen_density = atoi(value);
if (screen_density > 0) {
min_x_swipe_px = (int)(0.5 * screen_density);
min_y_swipe_px = (int)(0.3 * screen_density);
}
}
int touch_handle_key(int key, int visible) {
if (visible) {
switch (key) {
case KEY_DOWN:
case KEY_VOLUMEDOWN:
case KEY_MENU:
return HIGHLIGHT_DOWN;
case KEY_UP:
case KEY_VOLUMEUP:
case KEY_HOME:
return HIGHLIGHT_UP;
case KEY_POWER:
if (ui_get_showing_back_button()) {
return SELECT_ITEM;
}
if (!get_allow_toggle_display() && !ui_root_menu) {
return GO_BACK;
}
break;
case KEY_BACK:
if (!ui_root_menu) {
return GO_BACK;
}
break;
case KEY_ENTER:
return SELECT_ITEM;
}
}
return NO_ACTION;
}
static void touch_draw_text_line(int row, const char *t, int align)
{
int col = 0;
if (t[0] != '\0') {
int length = strnlen(t, MENU_MAX_COLS) * CHAR_WIDTH;
switch(align)
{
case LEFT_ALIGN:
col = 1;
break;
case CENTER_ALIGN:
col = ((gr_fb_width() - length) / 2);
break;
case RIGHT_ALIGN:
col = gr_fb_width() - length - 1;
break;
}
gr_text(col, (row+1)*CHAR_HEIGHT-1, t, 0);
}
}
static void touch_draw_menu_item(int textrow, const char *text, int selected)
{
if (selected) {
gr_color(UICOLOR0, UICOLOR1, UICOLOR2, 255);
gr_fill(0, (textrow-1)*CHAR_HEIGHT,
gr_fb_width(), (textrow+2)*CHAR_HEIGHT-1);
gr_color(HILITE_TEXT_COLOR);
gr_text(0, (textrow+1)*CHAR_HEIGHT-1, text, 0);
}
else {
gr_color(BACKGROUND_COLOR);
gr_fill(0, (textrow-1)*CHAR_HEIGHT,
gr_fb_width(), (textrow+2)*CHAR_HEIGHT-1);
gr_color(UICOLOR0, UICOLOR1, UICOLOR2, 255);
gr_text(0, (textrow+1)*CHAR_HEIGHT-1, text, 0);
}
gr_color(SEPARATOR_COLOR);
gr_fill(0, (textrow+2)*CHAR_HEIGHT-1,
gr_fb_width(), (textrow+2)*CHAR_HEIGHT+1);
}
int draw_touch_menu(char menu[MENU_MAX_ROWS][MENU_MAX_COLS], int menu_items, int menu_top, int menu_sel, int menu_show_start)
{
int i = 0;
int j = 0;
int row = 0;
gr_color(UICOLOR0, UICOLOR1, UICOLOR2, 255);
//Show battery level
int batt_level = 0;
batt_level = get_batt_stats();
if (batt_level < 21) {
gr_color(255, 0, 0, 255);
}
char batt_text[40];
char time_gmt[40];
// Get a usable time
struct tm *current;
time_t now;
now = time(0);
current = localtime(&now);
sprintf(batt_text, "[%d%%]", batt_level);
sprintf(time_gmt, "[%02D:%02D GMT]", current->tm_hour, current->tm_min);
touch_draw_text_line(BATT_LINE, batt_text, BATT_POS);
touch_draw_text_line(TIME_LINE, time_gmt, TIME_POS);
gr_color(HEADER_TEXT_COLOR);
for (i = 0; i < menu_top; ++i) {
touch_draw_text_line(i*2, menu[i], LEFT_ALIGN);
row++;
}
if (menu_items - menu_show_start + menu_top >= max_menu_rows)
j = max_menu_rows - menu_top;
else
j = menu_items - menu_show_start;
for (i = menu_show_start; i < (menu_show_start + j); ++i) {
int textrow = menu_top + 3*(i - menu_show_start) + 1;
touch_draw_menu_item(textrow, menu[menu_top + i], (i == menu_sel));
row++;
if (row >= max_menu_rows)
break;
}
return row;
}
void touch_init()
{
in_touch = 0;
in_swipe = 0;
touch_start.x = touch_end.x = -1;
touch_start.y = touch_end.y = -1;
touch_last_y = -1;
}
int get_max_menu_rows(int max_menu_rows)
{
int textrows = gr_fb_height() / CHAR_HEIGHT;
return (textrows - menu_top) / 3;
}
static void touch_calibrate_axis(int fd, int axis, int *minp, int *maxp)
{
struct input_absinfo info;
memset(&info, 0, sizeof(info));
if (ioctl(fd, EVIOCGABS(axis), &info)) {
LOGE("touch_calibrate: EVIOCGABS failed\n");
return;
}
LOGI("calibrate %d: min=%d, max=%d\n", axis, info.minimum, info.maximum);
*minp = info.minimum;
*maxp = info.maximum;
}
static void touch_calibrate(int fd)
{
if (touch_min.x == touch_max.x) {
touch_calibrate_axis(fd, ABS_MT_POSITION_X, &touch_min.x, &touch_max.x);
}
if (touch_min.y == touch_max.y) {
touch_calibrate_axis(fd, ABS_MT_POSITION_Y, &touch_min.y, &touch_max.y);
}
}
static int touch_x_scale(int val)
{
int touch_width = (touch_max.x - touch_min.x);
if (touch_width) {
val = val * gr_fb_width() / touch_width;
}
return val;
}
static int touch_y_scale(int val)
{
int touch_height = (touch_max.y - touch_min.y);
if (touch_height) {
val = val * gr_fb_height() / touch_height;
}
return val;
}
static void show_event(struct input_event *ev)
{
#ifdef DEBUG_TOUCH_EVENTS
char typebuf[40];
char codebuf[40];
const char *evtypestr = NULL;
const char *evcodestr = NULL;
sprintf(typebuf, "0x%04x", ev->type);
evtypestr = typebuf;
sprintf(codebuf, "0x%04x", ev->code);
evcodestr = codebuf;
switch (ev->type) {
case EV_SYN:
evtypestr = "EV_SYN";
switch (ev->code) {
case SYN_REPORT:
evcodestr = "SYN_REPORT";
break;
case SYN_MT_REPORT:
evcodestr = "SYN_MT_REPORT";
break;
}
break;
case EV_KEY:
evtypestr = "EV_KEY";
switch (ev->code) {
case BTN_TOUCH:
evcodestr = "BTN_TOUCH";
break;
}
break;
case EV_ABS:
evtypestr = "EV_ABS";
switch (ev->code) {
case ABS_MT_TOUCH_MAJOR:
evcodestr = "ABS_MT_TOUCH_MAJOR";
break;
case ABS_MT_TOUCH_MINOR:
evcodestr = "ABS_MT_TOUCH_MINOR";
break;
case ABS_MT_WIDTH_MAJOR:
evcodestr = "ABS_MT_WIDTH_MAJOR";
break;
case ABS_MT_WIDTH_MINOR:
evcodestr = "ABS_MT_WIDTH_MINOR";
break;
case ABS_MT_POSITION_X:
evcodestr = "ABS_MT_POSITION_X";
break;
case ABS_MT_POSITION_Y:
evcodestr = "ABS_MT_POSITION_Y";
break;
case ABS_MT_TRACKING_ID:
evcodestr = "ABS_MT_TRACKING_ID";
break;
case ABS_MT_ANGLE:
evcodestr = "ABS_MT_ANGLE";
break;
}
break;
}
LOGI("show_event: type=%s, code=%s, val=%d\n", evtypestr, evcodestr, ev->value);
#endif
}
static void touch_handle_press(struct input_event *ev)
{
in_touch = 1;
in_swipe = 0;
touch_start.x = touch_end.x = -1;
touch_start.y = touch_end.y = -1;
touch_last_y = -1;
}
static int touch_handle_release(struct input_event *ev)
{
int rc = 1;
int dx = touch_end.x - touch_start.x;
int dy = touch_end.y - touch_start.y;
if (in_swipe) {
if (abs(dx) > abs(dy)) {
if (abs(dx) > gr_fb_width()/4) {
ev->type = EV_KEY;
ev->code = (dx > 0 ? KEY_ENTER : KEY_BACK);
ev->value = 2;
rc = 0;
}
}
else {
/* Vertical swipe, handled realtime */
}
}
else {
int sel;
sel = (touch_end.y - (menu_top*CHAR_HEIGHT))/(CHAR_HEIGHT*3) + menu_show_start;
if (sel >= 0 && sel < menu_items) {
int textrow = menu_top + 3*sel + 1;
touch_draw_menu_item(textrow, menu[menu_top + sel], 1);
gr_flip();
usleep(1000*50);
menu_sel = sel;
ev->type = EV_KEY;
ev->code = KEY_ENTER;
ev->value = 2;
rc = 0;
}
}
in_touch = 0;
in_swipe = 0;
touch_start.x = touch_end.x = -1;
touch_start.y = touch_end.y = -1;
touch_last_y = -1;
return rc;
}
int touch_handle_gestures(struct input_event *ev)
{
int rc = 1;
int dx = touch_end.x - touch_start.x;
int dy = touch_end.y - touch_start.y;
if (touch_end.x == -1 || touch_end.y == -1) {
return 1;
}
if (abs(dx) > abs(dy)) {
if (abs(dx) > min_x_swipe_px) {
/* Horizontal swipe, handle it on release */
in_swipe = 1;
}
}
else {
if (touch_last_y == -1) {
touch_last_y = touch_end.y;
}
dy = touch_end.y - touch_last_y;
if (abs(dy) > min_y_swipe_px) {
in_swipe = 1;
touch_last_y = touch_end.y;
ev->type = EV_KEY;
ev->code = (dy < 0) ? KEY_VOLUMEUP : KEY_VOLUMEDOWN;
ev->value = 2;
rc = 0;
}
}
return rc;
}
int touch_handle_input(int fd, struct input_event *ev)
{
int rc = 1;
int dx, dy;
touch_set_min_swipe_lengths();
show_event(ev);
touch_calibrate(fd);
if (ev->type == EV_ABS && ev->code == ABS_MT_TRACKING_ID) {
if (ev->value != -1) {
touch_handle_press(ev);
}
else {
rc = touch_handle_release(ev);
}
}
else if (ev->type == EV_KEY && ev->code == BTN_TOUCH) {
if (ev->value) {
touch_handle_press(ev);
}
else {
rc = touch_handle_release(ev);
}
}
else if (ev->type == EV_ABS) {
switch (ev->code) {
case ABS_MT_POSITION_X:
in_touch = 1;
touch_end.x = touch_x_scale(ev->value);
if (touch_start.x == -1) {
touch_start.x = touch_end.x;
}
rc = touch_handle_gestures(ev);
break;
case ABS_MT_POSITION_Y:
in_touch = 1;
touch_end.y = touch_y_scale(ev->value);
if (touch_start.y == -1) {
touch_start.y = touch_end.y;
}
rc = touch_handle_gestures(ev);
break;
}
}
return rc;
}