3956a402bb01e49c8edf8ddd930a32c2974d9693
[framework/uifw/elementary.git] / src / modules / access_output / mod.c
1 #include <Elementary.h>
2 #ifdef HAVE_CONFIG_H
3 # include "elementary_config.h"
4 #endif
5
6 /* to enable this module
7 export ELM_MODULES="access_output>access/api"
8 export ELM_ACCESS_MODE=1
9  */
10
11 static void (*cb_func) (void *data);
12 static void *cb_data;
13 static Ecore_Exe *espeak = NULL;
14 static Ecore_Event_Handler *exe_exit_handler = NULL;
15 static char *tmpf = NULL;
16 static int tmpfd = -1;
17
18 static Eina_Bool
19 _exe_del(void *data __UNUSED__, int type __UNUSED__, void *event)
20 {
21    Ecore_Exe_Event_Del *ev = event;
22
23    if ((espeak) && (ev->exe == espeak))
24      {
25         if (tmpf)
26           {
27              unlink(tmpf);
28              free(tmpf);
29              tmpf = NULL;
30              close(tmpfd);
31           }
32         espeak = NULL;
33         if (cb_func) cb_func(cb_data);
34      }
35    return ECORE_CALLBACK_RENEW;
36 }
37
38 // module api funcs needed
39 EAPI int
40 elm_modapi_init(void *m __UNUSED__)
41 {
42    exe_exit_handler =
43       ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
44                               _exe_del, NULL);
45    return 1; // succeed always
46 }
47
48 EAPI int
49 elm_modapi_shutdown(void *m __UNUSED__)
50 {
51    if (exe_exit_handler)
52      {
53         ecore_event_handler_del(exe_exit_handler);
54         exe_exit_handler = NULL;
55      }
56    return 1; // succeed always
57 }
58
59 // module fucns for the specific module type
60 EAPI void
61 out_read(const char *txt)
62 {
63    if (!tmpf)
64      {
65         char buf[PATH_MAX];
66
67         snprintf(buf, sizeof(buf), "/tmp/.elm-speak-XXXXXX");
68         tmpfd = mkstemp(buf);
69         if (tmpfd >= 0) tmpf = strdup(buf);
70         else return;
71      }
72    if (write(tmpfd, txt, strlen(txt)) < 0) perror("write to tmpfile (espeak)");
73 }
74
75 EAPI void
76 out_read_done(void)
77 {
78    char buf[PATH_MAX];
79
80    if (espeak)
81      {
82         ecore_exe_interrupt(espeak);
83         espeak = NULL;
84      }
85    if (tmpf)
86      {
87         // FIXME: espeak supporets -v XX for voice locale. should provide this
88         // based on actual lang/locale
89         close(tmpfd);
90         snprintf(buf, sizeof(buf), "espeak -p 2 -s 120 -k 10 -m -f %s", tmpf);
91         espeak = ecore_exe_pipe_run(buf,
92                                     ECORE_EXE_NOT_LEADER,
93                                     NULL);
94      }
95 }
96
97 EAPI void
98 out_cancel(void)
99 {
100    if (espeak)
101      {
102         ecore_exe_interrupt(espeak);
103         espeak = NULL;
104      }
105    if (tmpf)
106      {
107         unlink(tmpf);
108         free(tmpf);
109         tmpf = NULL;
110         close(tmpfd);
111      }
112 }
113
114 EAPI void
115 out_done_callback_set(void (*func) (void *data), const void *data)
116 {
117    cb_func = func;
118    cb_data = (void *)data;
119 }