remove unused variables
[platform/core/uifw/efl-assist.git] / src / lib / efl_assist_screen_reader.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  */
17
18
19 #include "efl_assist.h"
20 #include "efl_assist_private.h"
21
22 #include <Ecore.h>
23
24 #ifdef HAVE_X11
25 #include <Ecore_X.h>
26 #endif
27 #ifdef HAVE_WAYLAND
28 #include <Ecore_Wayland.h>
29 #endif
30
31 #include <vconf.h>
32 #include <tts.h>
33 #define UNAVAILABLE_TEXT "Screen reader is unavailable during using this application. You can press home or back key to go back to home screen."
34
35 static tts_h tts = NULL;
36
37 static void _tts_shutdown(void)
38 {
39    int ret = 0;
40    if (tts)
41      {
42         /* check current state */
43         tts_state_e state;
44         tts_get_state(tts, &state);
45         if (state == TTS_STATE_PLAYING || state == TTS_STATE_PAUSED)
46           {
47              ret = tts_stop(tts);
48              if (TTS_ERROR_NONE != ret)
49                {
50                   fprintf(stderr, "Fail to stop handle : result(%d)", ret);
51                   return;
52                }
53           }
54
55         /* it is possible to try to shutdown before the state is ready,
56            because tts_prepare(); works Asynchronously. see elm_modapi_init(): */
57         if (state == TTS_STATE_READY)
58           {
59              ret = tts_unprepare(tts);
60              if (TTS_ERROR_NONE != ret)
61                {
62                   fprintf(stderr, "Fail to unprepare handle : result(%d)", ret);
63                   return;
64                }
65
66              ret = tts_unset_state_changed_cb(tts);
67              if (TTS_ERROR_NONE != ret)
68                {
69                   fprintf(stderr, "Fail to set callback : result(%d)", ret);
70                   return;
71                }
72           }
73
74         ret = tts_destroy(tts);
75         if (TTS_ERROR_NONE != ret)
76           {
77              fprintf(stderr, "Fail to destroy handle : result(%d)", ret);
78              return;
79           }
80
81         if (tts) tts = NULL;
82      }
83 }
84
85 void _tts_state_changed_cb(tts_h tts, tts_state_e previous, tts_state_e current, void* data)
86 {
87    int ret = 0;
88    int u_id = 0;
89
90    if (TTS_STATE_CREATED == previous && TTS_STATE_READY == current)
91      {
92         ret = tts_add_text(tts, UNAVAILABLE_TEXT, NULL, TTS_VOICE_TYPE_AUTO,
93                            TTS_SPEED_AUTO, &u_id);
94         if (TTS_ERROR_NONE != ret)
95           {
96              fprintf(stderr, "Fail to add kept text : ret(%d)\n", ret);
97           }
98
99         ret = tts_play(tts);
100         if (TTS_ERROR_NONE != ret)
101           {
102              fprintf(stderr, "Fail to play TTS : ret(%d)\n", ret);
103           }
104      }
105 }
106
107 static void _tts_init(void)
108 {
109    int ret = 0;
110
111    ret = tts_create(&tts);
112    if (TTS_ERROR_NONE != ret)
113      {
114         fprintf(stderr, "Fail to get handle : result(%d)", ret);
115         return;
116      }
117
118    ret = tts_set_state_changed_cb(tts, _tts_state_changed_cb, NULL);
119    if (TTS_ERROR_NONE != ret)
120      {
121         fprintf(stderr, "Fail to set callback : result(%d)", ret);
122         return;
123      }
124
125    ret = tts_set_mode(tts, TTS_MODE_SCREEN_READER);
126    if (TTS_ERROR_NONE != ret)
127      {
128         fprintf(stderr, "Fail to set mode : result(%d)", ret);
129         return;
130      }
131
132    ret = tts_prepare(tts);
133    if (TTS_ERROR_NONE != ret)
134      {
135         fprintf(stderr, "Fail to prepare handle : result(%d)", ret);
136         return;
137      }
138 }
139
140 static void _timeout_cb(void *data, Evas_Object *obj, void *event_info)
141 {
142 #ifdef HAVE_X11
143    Ecore_X_Window xwin=0;
144    unsigned int val;
145 #endif
146 #ifdef HAVE_WAYLAND
147    Ecore_Wl_Window *wwin=0;
148 #endif
149
150 #ifdef HAVE_X11
151    xwin = elm_win_xwindow_get(data);
152    if (xwin>0) {
153      evas_object_del(obj); obj=0;
154      val = 2;
155      ecore_x_window_prop_card32_set
156        (xwin, ECORE_X_ATOM_E_ILLUME_ACCESS_CONTROL, &val, 1);
157    } else
158 #endif
159      {
160 #ifdef HAVE_WAYLAND
161      wwin = elm_win_wl_window_get(data);
162      if (wwin != NULL) {
163        evas_object_del(obj); obj=0;
164
165        fprintf(stderr, "TODO: workaround: disabled code from "
166                __FILE__ ":%d:", __LINE__);
167      }
168 #endif
169    }
170    _tts_shutdown();
171 }
172
173 EAPI Eina_Bool
174 ea_screen_reader_support_set(Evas_Object *win, Eina_Bool support)
175 {
176 #ifdef HAVE_X11
177    Ecore_X_Window w=0;
178    unsigned int val;
179 #elif defined HAVE_WAYLAND
180    Ecore_Wl_Window *w=0;
181 #endif
182    int tts_val;
183    Evas_Object *popup;
184
185    if (vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &tts_val) != 0)
186      return EINA_FALSE;
187
188    if (!tts_val) return EINA_FALSE;
189
190    if (!win) return EINA_FALSE;
191
192 #ifdef HAVE_WAYLAND
193    w = elm_win_wl_window_get(win);
194 #elif HAVE_X11
195    w = elm_win_xwindow_get(win);
196 #endif
197
198    if (!w) return EINA_FALSE;
199
200    if (support)
201      {
202         elm_config_access_set(EINA_TRUE);
203 #ifdef HAVE_X11
204         val = 0;
205         ecore_x_window_prop_card32_set
206           (w, ECORE_X_ATOM_E_ILLUME_ACCESS_CONTROL, &val, 1);
207 #else
208         fprintf(stderr, "TODO: workaround: disabled code from "
209                 __FILE__ ":%d:", __LINE__);
210 #endif
211      }
212    else
213      {
214         elm_config_access_set(EINA_FALSE);
215
216         popup = elm_popup_add(win);
217         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
218         elm_object_text_set(popup, UNAVAILABLE_TEXT);
219         elm_popup_timeout_set(popup, 12.0);
220         evas_object_smart_callback_add(popup, "timeout", _timeout_cb, win);
221
222         _tts_init();
223         evas_object_show(popup);
224      }
225
226    return EINA_TRUE;
227 }
228
229 EAPI Eina_Bool
230 ea_screen_reader_support_get()
231 {
232    return elm_config_access_get();
233 }