diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ecore/src/examples/ecore_fd_handler_example.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/libraries/ecore/src/examples/ecore_fd_handler_example.c b/libraries/ecore/src/examples/ecore_fd_handler_example.c new file mode 100644 index 0000000..d80b290 --- /dev/null +++ b/libraries/ecore/src/examples/ecore_fd_handler_example.c | |||
@@ -0,0 +1,86 @@ | |||
1 | #include <Ecore.h> | ||
2 | #include <unistd.h> | ||
3 | |||
4 | struct context { | ||
5 | Ecore_Fd_Handler *handler; | ||
6 | Ecore_Timer *timer; | ||
7 | }; | ||
8 | |||
9 | static void | ||
10 | _fd_prepare_cb(void *data, Ecore_Fd_Handler *handler) | ||
11 | { | ||
12 | printf("prepare_cb called.\n"); | ||
13 | } | ||
14 | |||
15 | static Eina_Bool | ||
16 | _fd_handler_cb(void *data, Ecore_Fd_Handler *handler) | ||
17 | { | ||
18 | struct context *ctxt = data; | ||
19 | char buf[1024]; | ||
20 | size_t nbytes; | ||
21 | int fd; | ||
22 | |||
23 | if (ecore_main_fd_handler_active_get(handler, ECORE_FD_ERROR)) | ||
24 | { | ||
25 | printf("An error has occurred. Stop watching this fd and quit.\n"); | ||
26 | ecore_main_loop_quit(); | ||
27 | ctxt->handler = NULL; | ||
28 | return ECORE_CALLBACK_CANCEL; | ||
29 | } | ||
30 | |||
31 | fd = ecore_main_fd_handler_fd_get(handler); | ||
32 | nbytes = read(fd, buf, sizeof(buf)); | ||
33 | if (nbytes == 0) | ||
34 | { | ||
35 | printf("Nothing to read, exiting...\n"); | ||
36 | ecore_main_loop_quit(); | ||
37 | ctxt->handler = NULL; | ||
38 | return ECORE_CALLBACK_CANCEL; | ||
39 | } | ||
40 | buf[nbytes - 1] = '\0'; | ||
41 | |||
42 | printf("Read %zd bytes from input: \"%s\"\n", nbytes - 1, buf); | ||
43 | |||
44 | return ECORE_CALLBACK_RENEW; | ||
45 | } | ||
46 | |||
47 | static Eina_Bool | ||
48 | _timer_cb(void *data) | ||
49 | { | ||
50 | printf("Timer expired after 5 seconds...\n"); | ||
51 | |||
52 | return ECORE_CALLBACK_RENEW; | ||
53 | } | ||
54 | |||
55 | int main(int argc, char **argv) | ||
56 | { | ||
57 | struct context ctxt = {0}; | ||
58 | |||
59 | if (!ecore_init()) | ||
60 | { | ||
61 | printf("ERROR: Cannot init Ecore!\n"); | ||
62 | return -1; | ||
63 | } | ||
64 | |||
65 | ctxt.handler = ecore_main_fd_handler_add(STDIN_FILENO, | ||
66 | ECORE_FD_READ | ECORE_FD_ERROR, | ||
67 | _fd_handler_cb, | ||
68 | &ctxt, NULL, NULL); | ||
69 | ecore_main_fd_handler_prepare_callback_set(ctxt.handler, _fd_prepare_cb, &ctxt); | ||
70 | ctxt.timer = ecore_timer_add(5, _timer_cb, &ctxt); | ||
71 | |||
72 | printf("Starting the main loop. Type anything and hit <enter> to " | ||
73 | "activate the fd_handler callback, or CTRL+d to shutdown.\n"); | ||
74 | |||
75 | ecore_main_loop_begin(); | ||
76 | |||
77 | if (ctxt.handler) | ||
78 | ecore_main_fd_handler_del(ctxt.handler); | ||
79 | |||
80 | if (ctxt.timer) | ||
81 | ecore_timer_del(ctxt.timer); | ||
82 | |||
83 | ecore_shutdown(); | ||
84 | |||
85 | return 0; | ||
86 | } | ||