Abolished the touch panel (include the calibration) support.
[profile/ivi/ico-uxf-device-input-controller.git] / tests / test-send_event.c
1 /*
2  * Copyright © 2013 TOYOTA MOTOR CORPORATION.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 /**
23  * @brief   System Test Tool for send device input event
24  *
25  * @date    Feb-20-2013
26  */
27
28 #include    <stdio.h>
29 #include    <stdlib.h>
30 #include    <unistd.h>
31 #include    <string.h>
32 #include    <errno.h>
33 #include    <pthread.h>
34 #include    <sys/ioctl.h>
35 #include    <sys/ipc.h>
36 #include    <sys/msg.h>
37 #include    <sys/time.h>
38 #include    <sys/types.h>
39 #include    <sys/stat.h>
40 #include    <signal.h>
41 #include    <fcntl.h>
42 #include    <linux/input.h>
43 #include    <linux/uinput.h>
44 #include    <linux/joystick.h>
45 #include    "test-common.h"
46
47 #define DEV_TOUCH   0
48 #define DEV_JS      1
49 #define SPECIALTYPE_XY  9991
50
51 static const struct {
52     char    *prop;
53     short   devtype;
54     short   type;
55     short   code;
56     short   value;
57 }               event_key[] = {
58     { "X", DEV_TOUCH, EV_ABS, ABS_X, -1 },
59     { "Y", DEV_TOUCH, EV_ABS, ABS_Y, -1 },
60     { "Down", DEV_TOUCH, EV_KEY, BTN_TOUCH, 1 },
61     { "Up", DEV_TOUCH, EV_KEY, BTN_TOUCH, 0 },
62     { "Touch", DEV_TOUCH, EV_KEY, BTN_TOUCH, -1 },
63     { "XY", DEV_TOUCH, SPECIALTYPE_XY, 0, -1 },
64     { "SYN", DEV_TOUCH, 0, 0, 0 },
65     { "Button", DEV_TOUCH, EV_KEY, BTN_LEFT, -1 },
66     { "ButtonOn", DEV_TOUCH, EV_KEY, BTN_LEFT, 1 },
67     { "ButtonOff", DEV_TOUCH, EV_KEY, BTN_LEFT, 0 },
68
69     { "UpDown", DEV_JS, 2, 3, 1 },
70     { "UD", DEV_JS, 2, 3, 1 },
71     { "LeftRight", DEV_JS, 2, 2, 2 },
72     { "LR", DEV_JS, 2, 2, 2 },
73     { "Cross", DEV_JS, 1, 0, 3 },
74     { "Squere", DEV_JS, 1, 1, 4 },
75     { "Circle", DEV_JS, 1, 2, 5 },
76     { "Triangle", DEV_JS, 1, 3, 6 },
77     { "\0", 0, 0, 0, 0 } };
78
79 static int  uifd = -1;
80 static int  mqid = -1;
81 static int  mDebug = 0;
82 static int  mRun = 1;
83 static int  mTouch = 1;
84
85 static void
86 term_signal(const int signo)
87 {
88     mRun = 0;
89 }
90
91 static void
92 init_mq(const int mqkey)
93 {
94     char    dummy[256];
95
96     if (mqkey == 0) {
97         mqid = -1;
98     }
99     else    {
100         mqid = msgget(mqkey, 0);
101         if (mqid < 0)   {
102             mqid = msgget(mqkey, IPC_CREAT);
103         }
104         if (mqid < 0)   {
105             print_log("Can not create message queue(%d(0x%x))[%d]",
106                       mqkey, mqkey, errno);
107             fflush(stderr);
108             return;
109         }
110         while (msgrcv(mqid, dummy, sizeof(dummy)-sizeof(long), 0, IPC_NOWAIT) > 0)  ;
111     }
112 }
113
114 static void
115 init_device(const char *device)
116 {
117     int     fd;
118     int     ii;
119     char    devFile[64];
120     char    devName[64];
121     struct uinput_user_dev  uinputDevice;
122     uifd = open("/dev/uinput", O_RDWR);
123
124     if (uifd < 0)   {
125         print_log("/dev/uinput open error[%d]", errno);
126         fflush(stderr);
127         exit(1);
128     }
129
130     memset(&uinputDevice, 0, sizeof(uinputDevice));
131     strcpy(uinputDevice.name, device);
132     uinputDevice.absmax[ABS_X] = 1920;
133     uinputDevice.absmax[ABS_Y] = 1080;
134
135     /* uinput device configuration  */
136     if (write(uifd, &uinputDevice, sizeof(uinputDevice)) < (int)sizeof(uinputDevice)) {
137         print_log("/dev/uinput regist error[%d]", errno);
138         fflush(stderr);
139         close(uifd);
140         exit(1);
141     }
142
143     /* uinput set event bits        */
144     ioctl(uifd, UI_SET_EVBIT, EV_SYN);
145
146     if ((mTouch != 0) && (mTouch != 3)) {
147         ioctl(uifd, UI_SET_EVBIT, EV_ABS);
148         ioctl(uifd, UI_SET_ABSBIT, ABS_X);
149         ioctl(uifd, UI_SET_ABSBIT, ABS_Y);
150         ioctl(uifd, UI_SET_EVBIT, EV_KEY);
151         if (mTouch == 1)    {
152             ioctl(uifd, UI_SET_KEYBIT, BTN_LEFT);
153         }
154         else    {
155             ioctl(uifd, UI_SET_KEYBIT, BTN_TOUCH);
156             ioctl(uifd, UI_SET_KEYBIT, BTN_TOOL_PEN);
157         }
158     }
159     else    {
160         ioctl(uifd, UI_SET_EVBIT, EV_REL);
161         ioctl(uifd, UI_SET_RELBIT, REL_X);
162         ioctl(uifd, UI_SET_RELBIT, REL_Y);
163         ioctl(uifd, UI_SET_RELBIT, REL_Z);
164         ioctl(uifd, UI_SET_RELBIT, REL_RX);
165         ioctl(uifd, UI_SET_RELBIT, REL_RY);
166         ioctl(uifd, UI_SET_RELBIT, REL_RZ);
167         ioctl(uifd, UI_SET_EVBIT, EV_KEY);
168         ioctl(uifd, UI_SET_KEYBIT, KEY_RESERVED);
169         ioctl(uifd, UI_SET_KEYBIT, KEY_ESC);
170         ioctl(uifd, UI_SET_KEYBIT, KEY_1);
171         ioctl(uifd, UI_SET_KEYBIT, KEY_2);
172         ioctl(uifd, UI_SET_KEYBIT, KEY_3);
173         ioctl(uifd, UI_SET_KEYBIT, KEY_4);
174         ioctl(uifd, UI_SET_KEYBIT, KEY_5);
175         ioctl(uifd, UI_SET_KEYBIT, KEY_6);
176         ioctl(uifd, UI_SET_KEYBIT, KEY_7);
177         ioctl(uifd, UI_SET_KEYBIT, KEY_8);
178         ioctl(uifd, UI_SET_KEYBIT, KEY_9);
179         ioctl(uifd, UI_SET_KEYBIT, KEY_0);
180     }
181
182     ioctl(uifd, UI_SET_EVBIT, EV_MSC);
183     ioctl(uifd, UI_SET_MSCBIT, MSC_SCAN);
184
185     /* create event device          */
186     if (ioctl(uifd, UI_DEV_CREATE, NULL) < 0)   {
187         print_log("/dev/uinput create error[%d]", errno);
188         fflush(stderr);
189         close(uifd);
190         exit(1);
191     }
192     print_log("## created event device %s", device);
193
194     for (ii = 0; ii < 16; ii++) {
195         snprintf(devFile, 64, "/dev/input/event%d", ii);
196         fd = open(devFile, O_RDONLY);
197         if (fd < 0)     continue;
198
199         memset(devName, 0, sizeof(devName));
200         ioctl(fd, EVIOCGNAME(sizeof(devName)), devName);
201         close(fd);
202         print_log("%d.event device(%s) is %s", ii+1, devFile, devName);
203     }
204 }
205
206 static int
207 convert_value(const char *value, char **errp, int base)
208 {
209     int i;
210
211     for (i = 0; value[i]; i++)  {
212         if ((value[i] == ',') || (value[i] == ';') ||
213             (value[i] == ';') || (value[i] == ' ')) {
214             break;
215         }
216     }
217     if (errp)   {
218         *errp = (char *)&value[i];
219     }
220
221     if ((strncasecmp(value, "on", i) == 0) ||
222         (strncasecmp(value, "true", i) == 0) ||
223         (strncasecmp(value, "push", i) == 0) ||
224         (strncasecmp(value, "down", i) == 0) ||
225         (strncasecmp(value, "right", i) == 0))  {
226         return 1;
227     }
228     else if ((strncasecmp(value, "off", i) == 0) ||
229              (strncasecmp(value, "false", i) == 0) ||
230              (strncasecmp(value, "pop", i) == 0) ||
231              (strncasecmp(value, "up", i) == 0) ||
232              (strncasecmp(value, "left", i) == 0))  {
233         return 0;
234     }
235     return strtol(value, (char **)0, 0);
236 }
237
238 static void
239 send_event(const char *cmd)
240 {
241     int     i, j;
242     int     key;
243     char    prop[64];
244     char    value[128];
245     int     sec, msec;
246     char    *errp;
247     struct input_event  event;
248     struct js_event     js;
249
250     j = 0;
251     for (i = 0; cmd[i]; i++)    {
252         if ((cmd[i] == '=') || (cmd[i] == ' ')) break;
253         if (j < (int)(sizeof(prop)-1))  {
254             prop[j++] = cmd[i];
255         }
256     }
257
258     prop[j] = 0;
259     j = 0;
260     if (cmd[i] != 0)    {
261         for (i++; cmd[i]; i++)  {
262             if (cmd[i] == ' ')  continue;
263             if (j < (int)(sizeof(value)-1)) {
264                 value[j++] = cmd[i];
265             }
266         }
267     }
268     value[j] = 0;
269
270     if (strcasecmp(prop, "sleep") == 0) {
271         sec = 0;
272         msec = 0;
273         for (i = 0; value[i]; i++)  {
274             if (value[i] == '.')        break;
275             sec = sec * 10 + (value[i] & 0x0f);
276         }
277         if (value[i] == '.')    {
278             i++;
279             if (value[i] != 0)  {
280                 msec = (value[i] & 0x0f) * 100;
281                 i++;
282             }
283             if (value[i] != 0)  {
284                 msec = msec + (value[i] & 0x0f) * 10;
285                 i++;
286             }
287             if (value[i] != 0)  {
288                 msec = msec + (value[i] & 0x0f);
289             }
290         }
291         if (sec > 0)    sleep(sec);
292         if (msec > 0)   usleep(msec * 1000);
293
294         return;
295     }
296
297     for (key = 0; event_key[key].prop[0]; key++)    {
298         if (strcasecmp(prop, event_key[key].prop) == 0) break;
299     }
300     if (! event_key[key].prop[0])   {
301         print_log("UnKnown Event name[%s]", prop);
302         return;
303     }
304
305     if (mTouch != 0)    {
306         memset(&event, 0, sizeof(event));
307         gettimeofday(&event.time, NULL);
308         if (event_key[key].type == SPECIALTYPE_XY)  {
309             event.type = EV_ABS;
310             event.code = ABS_X;
311             event.value = convert_value(value, &errp, 0);
312             if (mDebug) {
313                 print_log("Send Event ABS_X=%d\t# %d.%03d", event.value,
314                           (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
315                 fflush(stderr);
316             }
317             if (write(uifd, &event, sizeof(struct input_event)) < 0)    {
318                 print_log("event write error 1[%d]", errno);
319                 fflush(stderr);
320                 return;
321             }
322             event.code = ABS_Y;
323             if (*errp == ',')   {
324                 event.value = convert_value(errp + 1, (char **)0, 0);
325             }
326             else    {
327                 event.value = 0;
328             }
329             event.time.tv_usec += 200;
330             if (event.time.tv_usec >= 1000000)  {
331                 event.time.tv_sec ++;
332                 event.time.tv_usec -= 1000000;
333             }
334             if (mDebug) {
335                 print_log("Send Event ABS_Y=%d\t# %d.%03d", event.value,
336                           (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
337                 fflush(stderr);
338             }
339         }
340         else    {
341             event.type = event_key[key].type;
342
343             if (event_key[key].code == -1)   {
344                 event.code = convert_value(value, (char **)0, 0);
345             }
346             else    {
347                 event.code = event_key[key].code;
348                 event.value = convert_value(value, (char **)0, 0);
349             }
350             if (mDebug) {
351                 if ((event.type == EV_ABS) && (event.code == ABS_X))    {
352                     print_log("Send Event X=%d\t# %d.%03d", event.value,
353                               (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
354                 }
355                 else if ((event.type == EV_ABS) && (event.code == ABS_Y))    {
356                     print_log("Send Event Y=%d\t %d.%03d", event.value,
357                               (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
358                 }
359                 else if ((event.type == EV_KEY) &&
360                          (event.code == BTN_LEFT) && (event.value == 1))    {
361                     print_log("Send Event BTN_LEFT=Down\t# %d.%03d",
362                               (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
363                 }
364                 else if ((event.type == EV_KEY) &&
365                          (event.code == BTN_LEFT) && (event.value == 0))   {
366                     print_log("Send Event BTN_LEFT=Up\t# %d.%03d",
367                               (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
368                 }
369                 else    {
370                     if ((event.type == EV_REL) && (event.value == 0))   {
371                         event.value = 9;
372                     }
373                     else if ((event.type == EV_KEY) && (event.code == 0))   {
374                         event.code = 9;
375                     }
376                     print_log("Send Event type=%d code=%d value=%d\t# %d.%03d",
377                               event.type, event.code, event.value,
378                               (int)event.time.tv_sec, (int)(event.time.tv_usec/1000));
379                 }
380                 fflush(stderr);
381             }
382         }
383         if (write(uifd, &event, sizeof(struct input_event)) < 0)    {
384             print_log("event write error 2[%d]", errno);
385             fflush(stderr);
386         }
387         else    {
388             /* send EV_SYN */
389             memset(&event, 0, sizeof(event));
390             gettimeofday(&event.time, NULL);
391             event.type = EV_SYN;
392             event.code = SYN_REPORT;
393             if (write(uifd, &event, sizeof(struct input_event)) < 0)    {
394                 print_log("syn event write error 3[%d]", errno);
395             }
396         }
397     }
398     else    {
399         memset(&js, 0, sizeof(js));
400         gettimeofday(&event.time, NULL);
401         js.time = (event.time.tv_sec * 1000) + (event.time.tv_usec / 1000);
402         js.type = event_key[key].type;
403         js.number = event_key[key].code;
404         js.value = convert_value(value, (char **)0, 0);
405         if (mDebug) {
406             print_log("Send Event JS=%d,%d,%d\t# %d",
407                       (int)js.type, (int)js.number, (int)js.value, (int)js.time);
408         }
409         if (write(uifd, &js, sizeof(struct js_event)) < 0)  {
410             print_log("event write error 4[%d]", errno);
411             fflush(stderr);
412         }
413     }
414 }
415
416 static void
417 usage(const char *prog)
418 {
419     fprintf(stderr, "Usage: %s [-device=device] [{-m/-t/-j}] [-mq[=key]] "
420             "[-d] [event=value] [event=value] ...\n", prog);
421     exit(0);
422 }
423
424 int
425 main(int argc, char *argv[])
426 {
427     int     i, j, k;
428     int     mqkey = 0;
429     struct {
430         long    mtype;
431         char    buf[240];
432     }       mqbuf;
433     char    buf[240];
434
435     j = 0;
436     strcpy(buf, "ico_test_device");
437     for (i = 1; i < argc; i++)  {
438         if (argv[i][0] == '-')  {
439             if (strncasecmp(argv[i], "-device=", 8) == 0)   {
440                 strcpy(buf, &argv[i][8]);
441             }
442             else if (strcasecmp(argv[i], "-m") == 0)   {
443                 mTouch = 1;                 /* Simulate mouse               */
444             }
445             else if (strcasecmp(argv[i], "-t") == 0)   {
446                 mTouch = 2;                 /* Simulate touch-panel         */
447             }
448             else if (strcmp(argv[i], "-j") == 0)   {
449                 mTouch = 0;                 /* Simulate joystick            */
450             }
451             else if (strcmp(argv[i], "-J") == 0)   {
452                 mTouch = 3;                 /* Simulate joystick, but event is mouse    */
453             }
454             else if (strncasecmp(argv[i], "-mq", 3) == 0)   {
455                 if (argv[i][3] == '=')  {
456                     mqkey = strtol(&argv[i][4], (char **)0, 0);
457                 }
458                 else    {
459                     mqkey = 55551;          /* default message queue key    */
460                 }
461             }
462             else if (strcasecmp(argv[i], "-d") == 0)   {
463                 mDebug = 1;
464             }
465             else    {
466                 usage(argv[0]);
467             }
468         }
469         else    {
470             j++;
471         }
472     }
473
474     init_mq(mqkey);
475
476     init_device(buf);
477
478     mRun = 1;
479
480     signal(SIGTERM, term_signal);
481     signal(SIGINT, term_signal);
482
483     if (mqid >= 0)  {
484         while (mRun)  {
485             memset(&mqbuf, 0, sizeof(mqbuf));
486             if (msgrcv(mqid, &mqbuf, sizeof(mqbuf)-sizeof(long), 0, 0) < 0) {
487                 if (errno == EINTR) continue;
488                 print_log("test-send_event: mq(%d) receive error[%d]",
489                           mqkey, errno);
490                 fflush(stderr);
491                 break;
492             }
493             k = 0;
494             j = -1;
495             for (i = 0; mqbuf.buf[i]; i++)    {
496                 if ((mqbuf.buf[i] == '#') || (mqbuf.buf[i] == '\n')
497                     || (mqbuf.buf[i] == '\r'))    break;
498                 if (mqbuf.buf[i] == '\t') buf[k++] = ' ';
499                 else                        buf[k++] = mqbuf.buf[i];
500                 if ((j < 0) && (mqbuf.buf[i] != ' ')) j = i;
501             }
502             if (j < 0)  continue;
503             buf[k] = 0;
504             send_event(&buf[j]);
505         }
506         msgctl(mqid, IPC_RMID, NULL);
507     }
508     else if (j <= 0) {
509         while ((mRun != 0) && (fgets(buf, sizeof(buf), stdin) != NULL))  {
510             j = -1;
511             for (i = 0; buf[i]; i++)    {
512                 if ((buf[i] == '#') || (buf[i] == '\n') || (buf[i] == '\r'))    break;
513                 if (buf[i] == '\t') buf[i] = ' ';
514                 if ((j < 0) && (buf[i] != ' ')) j = i;
515             }
516             if (j < 0)  continue;
517             buf[i] = 0;
518             send_event(&buf[j]);
519         }
520     }
521     else    {
522         for (i = 1; i < argc; i++)  {
523             if (argv[i][0] == '-')  continue;
524             if (mRun == 0)  break;
525             send_event(argv[i]);
526         }
527     }
528     exit(0);
529 }
530