-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgregg_touch.c
382 lines (348 loc) · 12.6 KB
/
gregg_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
/*============================[[ beg-code ]]============================*/
#include "gregg.h"
static char s_event [LEN_FULL];
static FILE *s_file = NULL;
static int s_line = 0;
static char s_touch = MODE_CURSOR;
static int s_pres = 0;
static int s_xpad = 0;
static int s_ypad = 0;
static float s_xrel = 0;
static float s_yrel = 0;
static int s_xpos = 0;
static int s_ypos = 0;
static int s_new_x = 0;
static int s_new_y = 0;
static int s_new_r = 0;
static int s_fileno = 0;
static int s_flags = 0;
/*---(struct.re)--------+-----------+-*//*-+----------------------------------*/
#define ABS_MODE 3
#define ABS_X 0x00
#define ABS_Y 0x01
#define ABS_PRES 0x18
/*---(struct.re)--------+-----------+-*//*-+----------------------------------*/
typedef struct cEVENT tEVENT;
struct cEVENT {
int64_t one;
int64_t two;
int16_t type;
int16_t code;
int32_t value;
};
/*====================------------------------------------====================*/
/*===---- program level ----===*/
/*====================------------------------------------====================*/
static void o___PROGRAM_________o () { return; }
char
TOUCH_init (void)
{
snprintf (s_event , LEN_FULL, "%s%s", "/dev/input/", "event12");
printf ("s_event = å%sæ\n", s_event);
TOUCH__open ();
return 0;
}
char
TOUCH_wrap (void)
{
TOUCH__close ();
return 0;
}
/*====================------------------------------------====================*/
/*===---- device file ----===*/
/*====================------------------------------------====================*/
static void o___DEVICE__________o () { return; }
char /*--> open touch device event file ----------[ leaf [ ------ ]-*/
TOUCH__open (void)
{
/*---(locals)-----------+-----------+-*/
char rce = -10; /* return code for errors */
/*---(header)-------------------------*/
DEBUG_TOUCH yLOG_enter (__FUNCTION__);
/*---(open)---------------------------*/
DEBUG_TOUCH yLOG_info ("name" , s_event);
s_file = fopen (s_event, "r");
DEBUG_TOUCH yLOG_point ("*file" , s_file);
--rce; if (s_file == NULL) {
/*> DEBUG_TOUCH yLOG_fatal ("device" , "event file can not be openned"); <*/
DEBUG_TOUCH yLOG_exit (__FUNCTION__);
return rce;
}
DEBUG_TOUCH yLOG_note ("succesfully opened");
/*---(complete)-----------------------*/
DEBUG_TOUCH yLOG_exit (__FUNCTION__);
return 0;
}
char /*--> open touch device event file ----------[ leaf [ ------ ]-*/
TOUCH__normal (void)
{
fcntl (s_fileno, F_SETFL, s_flags);
return 0;
}
char /*--> open touch device event file ----------[ leaf [ ------ ]-*/
TOUCH__check (void)
{
/*---(locals)-----------*-------------*/
char rce = -10; /* return code for errors */
int rc = 0;
int x_flags = 0;
int x_ch = 0;
/*---(header)-------------------------*/
DEBUG_TOUCH yLOG_senter (__FUNCTION__);
/*---(prepare)------------------------*/
s_fileno = 0;
s_flags = 0;
/*---(set to non-blocking)------------*/
DEBUG_TOUCH yLOG_snote ("non-blocking");
s_fileno = fileno (s_file);
s_flags = fcntl (s_fileno, F_GETFL, 0);
fcntl (s_fileno, F_SETFL, s_flags | O_NONBLOCK);
/*---(test for input)-----------------*/
x_ch = fgetc (s_file);
DEBUG_TOUCH yLOG_sint (x_ch);
--rce; if (x_ch < 0) {
DEBUG_TOUCH yLOG_snote ("nothing in queue");
TOUCH__normal ();
DEBUG_TOUCH yLOG_sexit (__FUNCTION__);
return rce;
}
/*---(put the char back)--------------*/
rc = ungetc (x_ch, s_file);
--rce; if (rc != x_ch) {
DEBUG_TOUCH yLOG_snote ("could not unget");
TOUCH__normal ();
DEBUG_TOUCH yLOG_sexit (__FUNCTION__);
return rce;
}
DEBUG_TOUCH yLOG_snote ("ready to read");
/*---(complete)-----------------------*/
DEBUG_TOUCH yLOG_sexit (__FUNCTION__);
return 0;
}
char
TOUCH__close (void)
{
int rc = 0;
/*---(open)---------------------------*/
rc = fclose (s_file);
if (rc < 0) {
printf ("BLOODY CAN'T CLOSE EVENT FILE\n");
exit (-2);
}
/*---(complete)-----------------------*/
return 0;
}
char /* [------] read input event -------------------------------*/
TOUCH_control (int a_x, int a_y)
{
/*---(locals)-----------+-----+-----+-*/
int x_xpos = 0;
int x_xinc = 0;
int i = 0;
int x_total = 0;
/*---(defense)------------------------*/
if (s_touch != MODE_CONTROL) return 0;
printf ("touched controls\n");
/*---(complete)-----------------------*/
return 0;
}
char /* [------] read input event -------------------------------*/
TOUCH_player (int a_x, int a_y)
{
/*---(locals)-----------+-----+-----+-*/
int x_inc = 0;
int x_slot = 0;
int x_wide;
int x_xmin, x_xmax, x_xlen, x_ymin, x_ymax, x_ylen;
/*---(defense)------------------------*/
if (s_touch != MODE_PLAYER) return 0;
/*> yVIKEYS_view_size (YVIKEYS_MAIN, NULL , NULL , &x_wide, NULL , NULL); <*/
/*> yVIKEYS_view_bounds (YVIKEYS_MAIN, &x_xmin, NULL, &x_xlen, &x_ymin, NULL, &x_ylen); <*/
x_xmax = x_xmin + x_xlen;
x_ymax = x_ymin + x_ylen;
x_inc = x_wide / 35.0;
x_slot = trunc ((a_x - x_xmin - 5) / 35);
/*> printf ("player x=%4d, w=%4d, i=%4d, s=%4d\n", a_x, x_xfull, x_inc, x_slot); <*/
/*> printf ("o.curr = %4d, o.total = %4d\n", o.curr, o.total); <*/
switch (x_slot) {
case 5 :
o.curr = 0;
break;
case 6 :
--o.curr;
break;
case 7 :
++o.curr;
break;
case 8 :
o.curr = o.total;
break;
}
/*> printf ("o.curr = %4d, o.total = %4d\n", o.curr, o.total); <*/
if (o.curr < 0 ) o.curr = 0;
if (o.curr >= o.total) o.curr = o.total - 1;
/*> printf ("o.curr = %4d, o.total = %4d\n", o.curr, o.total); <*/
OUT_pick (o.curr);
/*---(complete)-----------------------*/
return 0;
}
char /* [------] read input event -------------------------------*/
TOUCH_slider (int a_x, int a_y)
{
/*---(locals)-----------+-----+-----+-*/
int x_xpos = 0;
int x_xinc = 0;
int i = 0;
int x_total = 0;
int x_xmin, x_xfull;
/*---(defense)------------------------*/
if (s_touch != MODE_SLIDER) return 0;
if (o.navg <= 0) return 0;
/*---(process)------------------------*/
/*> yVIKEYS_view_bounds (YVIKEYS_MAIN, &x_xmin, NULL, &x_xfull, NULL, NULL, NULL); <*/
x_xpos = a_x - x_xmin;
/*> printf ("a_x %4d, x_xpos %4d\n", a_x, x_xpos); <*/
x_xinc = x_xfull / (o.navg - 2);
/*> printf ("navg %4d, x_inc %4d\n", o.navg, x_xinc); <*/
o.cavg = -1;
for (i = 1; i < o.navg - 1; ++i) {
/*> printf ("%2d x_xpos %4d, x_total %4d\n", i, x_xpos, x_total); <*/
if (x_xpos > x_total) {
x_total += x_xinc;
/*> printf (" next\n"); <*/
continue;
}
/*> printf (" CAUGHT\n"); <*/
o.cavg = i;
break;
}
if (o.cavg < 0) o.cavg = o.navg;
/*---(complete)-----------------------*/
return 0;
}
/*====================------------------------------------====================*/
/*===---- data reading ----===*/
/*====================------------------------------------====================*/
static void o___READING_________o () { return; }
char /* [------] read input event -------------------------------*/
TOUCH_read (void)
{
/*---(locals)-----------+-----+-----+-*/
char rce = -10; /* return code for errors */
int rc = 0;
tEVENT x_event;
char x_real = '-';
int x_mwide = 0;
int x_rwide = 0;
/*---(check)--------------------------*/
rc = TOUCH__check ();
if (rc < 0) return 0;
/*---(header)-------------------------*/
DEBUG_TOUCH yLOG_enter (__FUNCTION__);
/*---(prepare)------------------------*/
/*> yVIKEYS_view_bounds (YVIKEYS_MAIN , &(my.x_min), &(my.x_max ), NULL, &(my.y_min), &(my.y_max ), NULL); <*/
/*> yVIKEYS_view_bounds (YVIKEYS_MAIN , NULL , NULL , &x_mwide , NULL , NULL , &(my.y_tall)); <*/
/*> yVIKEYS_view_bounds (YVIKEYS_RIBBON, NULL , NULL , &x_rwide , NULL , NULL , &(my.y_tall)); <*/
my.x_wide = x_mwide + x_rwide;
my.ratio = my.x_scale / my.x_wide;
/*---(read)---------------------------*/
DEBUG_TOUCH yLOG_note ("read event");
fread (&x_event, sizeof (x_event), 1, s_file);
if (x_event.type != ABS_MODE) {
DEBUG_TOUCH yLOG_note ("not ABS mode, ignoring");
DEBUG_TOUCH yLOG_exit (__FUNCTION__);
return 0;
}
/*---(touch and pressure)-------------*/
if (x_event.code == ABS_PRES) {
DEBUG_TOUCH yLOG_note ("pressure event");
s_pres = x_event.value;
DEBUG_TOUCH yLOG_value ("s_pres" , s_pres);
/*---(touch)-----------------------*/
if (s_touch == MODE_CURSOR && s_pres >= 25) {
DEBUG_TOUCH yLOG_note ("new touch");
s_touch = MODE_TOUCH;
RAW_touch (s_xpad, s_ypad);
}
/*---(release)---------------------*/
else if (s_touch != MODE_CURSOR && s_pres < 25) {
DEBUG_TOUCH yLOG_note ("lifted existing touch");
if (s_touch == MODE_TOUCH) RAW_lift (s_xpad, s_ypad);
s_touch = MODE_CURSOR;
}
++s_line;
DEBUG_TOUCH yLOG_char ("s_touch" , s_touch);
DEBUG_TOUCH yLOG_exit (__FUNCTION__);
return 0;
}
/*---(position)-----------------------*/
switch (x_event.code) {
case ABS_X :
DEBUG_TOUCH yLOG_note ("X-movement event");
x_real = 'y';
if (x_event.value <= my.x_scale * 0.01) {
DEBUG_TOUCH yLOG_note ("lifted, i.e., coord went too low");
s_touch = MODE_CURSOR;
x_real = '-';
}
if (x_event.value >= my.x_scale * 0.99) {
DEBUG_TOUCH yLOG_note ("lifted, i.e., coord went too high");
s_touch = MODE_CURSOR;
x_real = '-';
}
if (x_real == 'y') {
s_xpad = my.x_scale - x_event.value;
s_xrel = s_xpad / (float) my.x_scale;
s_xpos = my.x_min + (s_xrel * my.x_wide);
}
break;
case ABS_Y :
DEBUG_TOUCH yLOG_note ("Y-movement event");
x_real = 'y';
if (x_event.value <= my.y_scale * 0.01) {
DEBUG_TOUCH yLOG_note ("lifted, i.e., coord went too low");
s_touch = MODE_CURSOR;
x_real = '-';
}
if (x_event.value >= my.y_scale * 0.99) {
DEBUG_TOUCH yLOG_note ("lifted, i.e., coord went too high");
s_touch = MODE_CURSOR;
x_real = '-';
}
if (x_real == 'y') {
s_ypad = x_event.value;
s_yrel = s_ypad / (float) my.y_scale;
s_ypos = my.y_min + (s_yrel * my.y_tall);
}
break;
default :
DEBUG_TOUCH yLOG_note ("event to be ignored");
DEBUG_TOUCH yLOG_exit (__FUNCTION__);
return 0;
break;
}
my.touch = s_touch;
my.xpos = s_xpos;
my.ypos = s_ypos;
if (s_touch == MODE_TOUCH ) RAW_normal (s_xpad, s_ypad);
if (s_touch == MODE_SLIDER ) TOUCH_slider (s_xpos, s_ypos);
++s_line;
/*---(headers and line breaks)--------*/
RPTG_TOUCH {
if ((s_line % 15) == 0) {
printf ("\n");
printf ("count t -pad- -rel- -scr- t -pad- -rel- -scr-\n");
}
if ((s_line % 3) == 0) {
printf ("\n");
}
printf ("%5d X %5d %5.3f %5d Y %5d %5.3f %5d\n",
s_line, s_xpad, s_xrel, s_xpos, s_ypad, s_yrel, s_ypos);
}
/*---(reset values)-------------*/
TOUCH__normal ();
/*---(complete)-------------------------*/
DEBUG_TOUCH yLOG_exit (__FUNCTION__);
return rc;
}
/*============================[[ end-of-code ]]===============================*/