Initialize Tizen 2.3
[framework/uifw/elementary.git] / mobile / 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         mode_t cur_umask;
67
68         snprintf(buf, sizeof(buf), "/tmp/.elm-speak-XXXXXX");
69         cur_umask = umask(S_IRWXO | S_IRWXG);
70         tmpfd = mkstemp(buf);
71         umask(cur_umask);
72         if (tmpfd >= 0) tmpf = strdup(buf);
73         else return;
74      }
75    if (write(tmpfd, txt, strlen(txt)) < 0) perror("write to tmpfile (espeak)");
76 }
77
78 EAPI void
79 out_read_done(void)
80 {
81    char buf[PATH_MAX];
82
83    if (espeak)
84      {
85         ecore_exe_interrupt(espeak);
86         espeak = NULL;
87      }
88    if (tmpf)
89      {
90         // FIXME: espeak supporets -v XX for voice locale. should provide this
91         // based on actual lang/locale
92         close(tmpfd);
93         snprintf(buf, sizeof(buf), "espeak -p 2 -s 120 -k 10 -m -f %s", tmpf);
94         espeak = ecore_exe_pipe_run(buf,
95                                     ECORE_EXE_NOT_LEADER,
96                                     NULL);
97      }
98 }
99
100 EAPI void
101 out_cancel(void)
102 {
103    if (espeak)
104      {
105         ecore_exe_interrupt(espeak);
106         espeak = NULL;
107      }
108    if (tmpf)
109      {
110         unlink(tmpf);
111         free(tmpf);
112         tmpf = NULL;
113         close(tmpfd);
114      }
115 }
116
117 EAPI void
118 out_done_callback_set(void (*func) (void *data), const void *data)
119 {
120    cb_func = func;
121    cb_data = (void *)data;
122 }