librui: Exit event loop upon INT signal 34/146634/7
authorŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 29 Aug 2017 12:52:40 +0000 (14:52 +0200)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 21 Sep 2017 08:08:55 +0000 (10:08 +0200)
Change-Id: I93fc569ef2b2f482553a0acd8243278a69625a77
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
[ Applied Tizen Coding style ]
Signed-off-by: Karol Lewandowski <k.lewandowsk@samsung.com>
src/librui/input-events.c

index fd312e7..c841ed4 100644 (file)
 #include <errno.h>
 #include <stdio.h>
 #include <dirent.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdbool.h>
 
 #include <sys/poll.h>
+#include <sys/signalfd.h>
 
 #include <linux/input.h>
 
 #include "input-events.h"
 
-#define MAX_DEVICES     16
-#define MAX_MISC_FDS    16
+#define MAX_INPUTS      32
 
 #define BITS_PER_LONG           (sizeof(unsigned long) * 8)
 #define BITS_TO_LONGS(x)        (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG)
 #define test_bit(bit, array)                                            \
        ((array)[(bit) / BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG)))
 
-static struct pollfd ev_fds[MAX_DEVICES + MAX_MISC_FDS];
+static struct pollfd ev_fds[MAX_INPUTS];
 
 static unsigned ev_count = 0;
-static unsigned ev_misc_count = 0;
 
-bool ev_init(void)
+static unsigned signalfd_idx = -1;
+
+#define IS_SIGNAL_EVENT(x) (x == signalfd_idx)
+
+static bool ev_signal_init(void)
+{
+       sigset_t mask;
+
+       if (ev_count >= MAX_INPUTS)
+               return false;
+
+       sigemptyset(&mask);
+       sigaddset(&mask, SIGINT);
+       if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
+               perror("sigprocmask");
+               return false;
+       }
+
+       ev_fds[ev_count].fd = signalfd(-1, &mask, 0);
+       ev_fds[ev_count].events = POLLIN;
+       if (ev_fds[ev_count].fd == -1) {
+               perror("signalfd");
+               return false;
+       }
+       signalfd_idx = ev_count++;
+
+       return true;
+}
+
+static bool ev_input_init(void)
 {
        DIR *dir;
        struct dirent *de;
        int fd;
+       int t = ev_count;
 
        dir = opendir("/dev/input");
        if (!dir) {
@@ -64,7 +94,7 @@ bool ev_init(void)
        while ((de = readdir(dir))) {
                unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
 
-               /*              fprintf(stderr,"/dev/input/%s\n", de->d_name);*/
+               /*              fprintf(stderr,"/dev/input/%s\n", de->d_name);*/
                if (strncmp(de->d_name, "event", 5))
                        continue;
 
@@ -89,12 +119,12 @@ bool ev_init(void)
                ev_fds[ev_count].fd = fd;
                ev_fds[ev_count].events = POLLIN;
                ev_count++;
-               if (ev_count == MAX_DEVICES)
+               if (ev_count == MAX_INPUTS)
                        break;
        }
 
        closedir(dir);
-       if (ev_count == 0) {
+       if (ev_count == t) {
                printf("No input devices found.\n");
                return false;
        }
@@ -102,12 +132,15 @@ bool ev_init(void)
        return true;
 }
 
+bool ev_init(void)
+{
+       return ev_input_init() && ev_signal_init();
+}
+
 void ev_exit(void)
 {
        while (ev_count > 0)
                close(ev_fds[--ev_count].fd);
-
-       ev_misc_count = 0;
 }
 
 static user_action last_action = ACTION_NONE;
@@ -211,6 +244,18 @@ bool ev_dispatch(input_callback callback)
                        int r;
                        struct input_event ev;
 
+                       if (IS_SIGNAL_EVENT(n)) {   /* signalfd */
+                               struct signalfd_siginfo fdsi;
+                               r = read(ev_fds[n].fd, &fdsi, sizeof(fdsi));
+                               if (r != sizeof(fdsi)) {
+                                       perror("read");
+                                       return false;
+                               }
+                               if (fdsi.ssi_signo == SIGINT)
+                                       return false;
+                               continue;
+                       }
+
                        r = read(ev_fds[n].fd, &ev, sizeof(ev));
                        if (r != sizeof(ev))
                                continue;