tizen 2.4 release
[framework/uifw/voice/voice-control-panel.git] / src / voice_control_panel_main.c
1 /*
2 *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved 
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <stdbool.h>
17
18 #include <voice_control_setting.h>
19
20 #include "voice_control_panel_main.h"
21 #include "voice_control_panel_view.h"
22 #include "voice_control_panel_vc.h"
23
24 static void __vc_enabled_changed_cb(bool enabled, void* user_data)
25 {
26         LOGD("Voice control enabled changed to (%d)", enabled);
27
28         if (false == enabled) {
29                 ui_app_exit();
30         }
31 }
32
33 static bool app_create(void *data)
34 {
35         LOGD("");
36
37         appdata *ad = (appdata *)data;
38
39         /* Check voice control enabled */
40         if (0 != vc_setting_initialize()) {
41                 LOGE("Fail to init");
42         }
43
44         bool enabled;
45         if (0 != vc_setting_get_enabled(&enabled)) {
46                 LOGE("Fail to get enabled");
47                 vc_setting_deinitialize();
48                 return false;
49         }
50         if (false == enabled) {
51                 LOGE("Voice control is disabled");
52                 vc_setting_deinitialize();
53                 return false;
54         }
55         
56         if (0 != vc_setting_set_enabled_changed_cb(__vc_enabled_changed_cb, NULL)) {
57                 LOGE("Fail to set enabled cb");
58         }
59
60         char* lang = NULL;
61         if (0 != vc_setting_get_language(&lang)) {
62                 LOGE("Fail to get language");
63                 return false;
64         }
65
66         char loc[64] = {'\0',};
67         snprintf(loc, 64, "%s.UTF-8", lang);
68         
69         setlocale(LC_ALL, loc);
70         
71         bindtextdomain("org.tizen.voice-control-panel", "/usr/apps/org.tizen.voice-control-panel/res/locale");
72         textdomain("org.tizen.voice-control-panel");
73
74         if (NULL != lang) {
75             free(lang);
76             lang = NULL;
77         }
78
79         /* Create View */
80         if (0 != vc_panel_view_create(ad)) {
81                 LOGE( "[ERROR] Fail to create view");
82                 return -1;
83         }
84
85         /* Initialize Voice Control */
86         if (0 != vc_panel_vc_init(ad)) {
87                 LOGE( "[ERROR] Fail to vc init");
88                 return -1;
89         }
90
91         return true;
92 }
93
94 static void app_control(app_control_h app_control, void *data)
95 {
96         LOGD("");
97
98         appdata *ad = (appdata *)data;
99         LOGD("state - %d", ad->app_state);
100
101         if (0 != ad->app_state) {
102                 ui_app_exit();
103                 return;
104         }
105
106         if (ad->win) {
107                 elm_win_activate(ad->win);
108         }
109         ad->app_state = APP_STATE_INIT;
110 }
111
112 static void app_pause(void *data)
113 {
114         LOGD("");
115
116         appdata *ad = (appdata *)data;
117         ad->app_state = APP_STATE_PAUSE;
118         ui_app_exit();
119 }
120
121 static void app_resume(void *data)
122 {
123         LOGD("");
124
125         appdata *ad = (appdata *)data;
126         if(ad->app_state == APP_STATE_PAUSE) {
127                 if (ad->win) {
128                         elm_win_activate(ad->win);
129                 }
130         }
131         ad->app_state = APP_STATE_SERVICE;
132
133         vc_panel_vc_start(ad);
134 }
135
136 static void app_terminate(void *data)
137 {
138         LOGD("");
139
140         appdata *ad = (appdata *)data;
141         ad->app_state = APP_STATE_TERMINATE;
142         
143         vc_panel_vc_cancel(ad);
144
145         vc_panel_view_destroy(ad);
146
147         vc_setting_deinitialize();
148
149         if (0 != vc_panel_vc_deinit(data)) {
150                 LOGE( "[ERROR] Fail to vc deinit");
151         }
152
153 }
154
155 static void ui_app_lang_changed(app_event_info_h event_info, void *user_data)
156 {
157         LOGD("");
158 }
159
160 static void ui_app_orient_changed(app_event_info_h event_info, void *user_data)
161 {
162         LOGD("");
163 }
164
165 static void ui_app_region_changed(app_event_info_h event_info, void *user_data)
166 {
167         LOGD("");
168 }
169
170 static void ui_app_low_battery(app_event_info_h event_info, void *user_data)
171 {
172         LOGD("");
173 }
174
175 static void ui_app_low_memory(app_event_info_h event_info, void *user_data)
176 {
177         LOGD("");
178 }
179
180 int main (int argc, char *argv[])
181 {
182         appdata ad = {0,};
183         int ret = 0;
184
185         ui_app_lifecycle_callback_s event_callback = {0,};
186         app_event_handler_h handlers[5] = {NULL, };
187
188         event_callback.create = app_create;
189         event_callback.terminate = app_terminate;
190         event_callback.pause = app_pause;
191         event_callback.resume = app_resume;
192         event_callback.app_control = app_control;
193
194         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
195         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
196         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
197         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
198         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
199
200         ret = ui_app_main(argc, argv, &event_callback, &ad);
201         if (ret != APP_ERROR_NONE) {
202                 LOGW("ui_app_main failed, Err=%d\n", ret);
203         }
204
205         return ret;
206 }
207
208 /*
209 vi:ts=4:ai:nowrap:expandtab
210 */