b6fc32331fcdf12bf90e62a29496b4abe15463d3
[apps/home/clock.git] / ring / src / ring.c
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   * 
4   * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #include "ring.h"
18 #include "ring_view_main.h"
19 #include "ring_fwk_util.h"
20 #include "ring_fwk_vconf.h"
21 #include <bundle.h>
22 #include <mm_session_private.h>
23 #include <sensor.h>
24 /**********************************************************************
25 ******************define, struct ,typedef, union, enum, global val *************************************
26 ***********************************************************************/
27
28 /**********************************************************************
29 ******************Local function declear, extern function declear*************************************
30 ***********************************************************************/
31 static void _ring_key_event_init(struct appdata *ad);
32 static void _ring_key_event_fini(struct appdata *ad);
33 static Eina_Bool _ring_key_down_cb(void *data, int type, void *event);
34
35 static int _ring_mgr_cb_func(alarm_id_t alarm_id, void *data);
36 void _ring_init_motion_sensor(struct appdata *ad);
37 void _ring_deinit_motion_sensor(struct appdata *ad);
38 static void _ring_motion_sensor_callback_func(unsigned int type,
39                                               sensor_event_data_t * event,
40                                               void *data);
41
42 static int ring_app_create(void *data);
43 static int ring_app_terminate(void *data);
44 static int ring_app_pause(void *data);
45 static int ring_app_resume(void *data);
46 static int ring_app_reset(bundle * b, void *data);
47 static int ring_app_low_memory(void *data);
48 static int ring_app_low_battery(void *data);
49 static int ring_app_lang_change(void *data);
50
51 /**********************************************************************
52 ******************Global val , static global val*************************************
53 ***********************************************************************/
54
55 static struct appdata g_alarm_ring_ad;
56
57 static struct text_part main_txt[] = {
58         {"txt_title", N_("Application template"),},
59         {"txt_mesg", N_("Click to exit"),},
60 };
61
62 /**********************************************************************
63 ******************Local function ref*************************************
64 ***********************************************************************/
65
66 /**
67 * send
68 * This function is  used to init key event
69 * @param           data[in]         pointer to struct appdata
70 * @return          void
71 * @exception
72 */
73 static void _ring_key_event_init(struct appdata *ad)
74 {
75         retm_if(!ad, "ad null");
76         Display *disp = ecore_x_display_get();
77         Window win = elm_win_xwindow_get(ad->win_main);
78         utilx_grab_key(disp, win, KEY_SEND, TOP_POSITION_GRAB);
79         utilx_grab_key(disp, win, KEY_SELECT, TOP_POSITION_GRAB);
80         utilx_grab_key(disp, win, KEY_END, TOP_POSITION_GRAB);
81         ad->ecore_handler =
82             ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _ring_key_down_cb,
83                                     ad);
84 }
85
86 static void _ring_key_event_fini(struct appdata *ad)
87 {
88         retm_if(!ad, "ad null");
89         Display *disp = ecore_x_display_get();
90         Window win = elm_win_xwindow_get(ad->win_main);
91         utilx_ungrab_key(disp, win, KEY_SELECT);
92         utilx_ungrab_key(disp, win, KEY_SEND);
93         utilx_ungrab_key(disp, win, KEY_END);
94         ECORE_EVENT_HANDLER_DELIF(ad->ecore_handler);
95 }
96
97 /**
98 * send
99 * This function is  used to handle key down cb
100 * @param           data[in]         pointer to data
101 * @param           type[in]         int type
102 * @param           event[in]        pointer to event
103 * @return          when success, return EINA_TRUE or EINA_FALSE if error
104 * @exception
105 */
106 static Eina_Bool _ring_key_down_cb(void *data, int type, void *event)
107 {
108         retvm_if(!data, ECORE_CALLBACK_CANCEL, "data is null");
109         retvm_if(!event, ECORE_CALLBACK_CANCEL, "event is null");
110         struct appdata *ad = (struct appdata *)data;
111         Evas_Event_Key_Up *ev = event;
112
113         retvm_if(!ev->keyname, ECORE_CALLBACK_CANCEL, "ev->keyname is null");
114         retvm_if(!ad->data_s, ECORE_CALLBACK_CANCEL, "ad->data_s is null");
115         if (IS_STR_EQUAL(ev->keyname, KEY_SEND)
116             || IS_STR_EQUAL(ev->keyname, KEY_SELECT)
117             || IS_STR_EQUAL(ev->keyname, KEY_END)) {
118                 ad->id_exit_type =
119                     ring_util_get_exit_type(ad->data_s->repeat_once,
120                                             ALARM_BTN_STOP);
121                 elm_exit();
122         }
123         return ECORE_CALLBACK_CANCEL;
124 }
125
126 /**
127 * send
128 * This function is  used to set alarmmgr_set_cb cb
129 * @param           alarm_id[in]     alarm_id_t
130 * @param           data[in]         pointer to data
131 * @return          when success, return SUCCESS or FAILED if error
132 * @exception
133 */
134 static int _ring_mgr_cb_func(alarm_id_t alarm_id, void *data)
135 {
136         retvm_if(!data, FAILED, "data null");
137         int ret = SUCCESS;
138         struct appdata *ad = (struct appdata *)data;
139         /*if ring have been created, just return SUCCESS */
140         retv_if(ad->is_create, SUCCESS);
141         /*ring have been created */
142         ad->is_create = EINA_TRUE;
143         //set data_s
144         ret = ring_util_data_get(ad, alarm_id);
145         if (ret != SUCCESS) {
146                 goto end;
147         }
148         //set i18n
149         appcore_set_i18n(PACKAGE, LOCALEDIR);
150         //show ui
151         ring_show_ui(ad);
152
153         ring_app_lang_change(ad);
154         appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY,
155                                    ring_app_low_battery, ad);
156         appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY,
157                                    ring_app_low_memory, ad);
158         appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE,
159                                    ring_app_lang_change, ad);
160         _ring_key_event_init(ad);
161         ring_noti_init(ad);
162  end:
163         if (SUCCESS != ret) {
164                 elm_exit();
165         }
166         return ret;
167 }
168
169 static void _ring_motion_sensor_callback_func(unsigned int type,
170                                               sensor_event_data_t * event,
171                                               void *data)
172 {
173         int *motion_detect = NULL;
174         struct appdata *ad = (struct appdata *)data;
175
176         retm_if(type != MOTION_ENGINE_EVENT_TOP_TO_BOTTOM,
177                 "motion event is not top to bottom");
178         retm_if(event == NULL, "sensor event data is NULL");
179
180         motion_detect = (int *)(event->event_data);
181
182         if (*motion_detect == MOTION_ENGIEN_TOP_TO_BOTTOM_DETECTION) {
183                 ad->id_exit_type =
184                     ring_util_get_exit_type(ad->data_s->repeat_once,
185                                             ALARM_BTN_STOP);
186                 elm_exit();
187         }
188
189         _ring_deinit_motion_sensor(ad);
190 }
191
192 void _ring_init_motion_sensor(struct appdata *ad)
193 {
194         int ret = -1;
195         int handle = -1;
196         int motion_active = -1;
197         int turnover_active = -1;
198
199         retm_if(ad->motion_sensor_handle >= 0, "already initialized");
200         retm_if(0 !=
201                 vconf_get_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION,
202                                &motion_active),
203                 "motion activation vconf key getting failed");
204         retm_if(0 !=
205                 vconf_get_bool(VCONFKEY_SETAPPL_USE_TURN_OVER,
206                                &turnover_active),
207                 "turn over activation vconf key getting failed");
208
209         if (motion_active == 1 && turnover_active == 1) {
210                 handle = sf_connect(MOTION_SENSOR);
211                 retm_if(handle < 0, "sf_connect failed");
212
213                 ret =
214                     sf_register_event(handle, MOTION_ENGINE_EVENT_TOP_TO_BOTTOM,
215                                       NULL, _ring_motion_sensor_callback_func,
216                                       ad);
217                 retm_if(ret < 0, "sf_register_event failed");
218
219                 ret = sf_start(handle, 0);
220                 retm_if(ret < 0, "sensor_start fail");
221
222                 ad->motion_sensor_handle = handle;
223         }
224 }
225
226 void _ring_deinit_motion_sensor(struct appdata *ad)
227 {
228         int ret = -1;
229
230         retm_if(ad->motion_sensor_handle < 0, "not initialized");
231
232         ret =
233             sf_unregister_event(ad->motion_sensor_handle,
234                                 MOTION_ENGINE_EVENT_TOP_TO_BOTTOM);
235         warn_if(ret < 0, "sf_unregister_event failed");
236
237         ret = sf_stop(ad->motion_sensor_handle);
238         warn_if(ret < 0, "sf_stop failed");
239
240         ret = sf_disconnect(ad->motion_sensor_handle);
241         warn_if(ret < 0, "sf_disconnect failed");
242 }
243
244 /**
245 * send
246 * This function is  used to handle when create
247 * @param           data[in]         pointer to data
248 * @return          when success, return SUCCESS or FAILED if error
249 * @exception
250 */
251 static int ring_app_create(void *data)
252 {
253         retvm_if(!data, FAILED, "data null");
254         int nErr = SUCCESS;
255         struct appdata *ad = (struct appdata *)data;
256
257         ad->is_create = EINA_FALSE;
258         nErr = alarmdb_init(NULL);
259         retvm_if(SUCCESS != nErr, FAILED, "alarmdb_init failed! return=%d\n",
260                  nErr);
261         nErr = alarmmgr_init(PKGNAME);
262         retvm_if(SUCCESS != nErr, FAILED, "alarmmgr_init failed! return=%d",
263                  nErr);
264         //mm
265         mm_session_init(MM_SESSION_TYPE_ALARM);
266         //
267         ring_util_state_get(ad);
268         nErr = alarmmgr_set_cb(_ring_mgr_cb_func, ad);
269         retvm_if(SUCCESS != nErr, FAILED, "alarmmgr_set_cb failed! return=%d",
270                  nErr);
271         _ring_init_motion_sensor(ad);
272         return SUCCESS;
273 }
274
275 /**
276 * send
277 * This function is  used to handle when terminate
278 * @param           data[in]         pointer to data
279 * @return          when success, return SUCCESS or FAILED if error
280 * @exception
281 */
282 static int ring_app_terminate(void *data)
283 {
284         struct appdata *ad = (struct appdata *)data;
285         ECORE_TIMER_DELIF(ad->timer);
286         ECORE_TIMER_DELIF(ad->lowbattery_timer);
287         //stop sound
288         ring_volume_stop_sound(ad);
289         //stop handle
290         ring_exit_data_handle(ad);
291         //delete res
292         EVAS_OBJECT_DELIF(ad->layout_main);
293         EVAS_OBJECT_DELIF(ad->win_main);
294         _ring_key_event_fini(ad);
295         FREEIF(ad->data_s);
296         alarmdb_fini();
297         alarmmgr_fini();
298         ring_noti_fini();
299         mm_session_finish();
300         ring_util_power_state(ALARMRING_UNLOCK);
301         return SUCCESS;
302 }
303
304 /**
305 * send
306 * This function is  used to handle when resume
307 * @param           data[in]         pointer to data
308 * @return          when success, return SUCCESS or FAILED if error
309 * @exception
310 */
311 static int ring_app_resume(void *data)
312 {
313         //struct appdata *ad = data;
314         return SUCCESS;
315 }
316
317 /**
318 * send
319 * This function is  used to handle when pause
320 * @param           data[in]         pointer to data
321 * @return          when success, return SUCCESS or FAILED if error
322 * @exception
323 */
324 static int ring_app_pause(void *data)
325 {
326         // struct appdata *ad = data;
327         return SUCCESS;
328 }
329
330 /**
331 * send
332 * This function is  used to handle when reset
333 * @param           b[in]         pointer to bundle
334 * @param           data[in]      pointer to data
335 * @return          when success, return SUCCESS or FAILED if error
336 * @exception
337 */
338 static int ring_app_reset(bundle * b, void *data)
339 {
340         struct appdata *ad = data;
341         if (ad->win_main) {
342                 elm_win_activate(ad->win_main);
343         }
344         return SUCCESS;
345 }
346
347 /**
348 * send
349 * This function is  used to handle when low memory
350 * @param           data[in]         pointer to data
351 * @return          when success, return SUCCESS or FAILED if error
352 * @exception
353 */
354 static int ring_app_low_memory(void *data)
355 {
356         return SUCCESS;
357 }
358
359 /**
360 * send
361 * This function is  used to handle when low battery
362 * @param           data[in]         pointer to data
363 * @return          when success, return SUCCESS or FAILED if error
364 * @exception
365 */
366 static int ring_app_low_battery(void *data)
367 {
368         return SUCCESS;
369 }
370
371 /**
372 * send
373 * This function is  used to handle when language changed
374 * @param           data[in]         pointer to data
375 * @return          when success, return SUCCESS or FAILED if error
376 * @exception
377 */
378 static int ring_app_lang_change(void *data)
379 {
380         struct appdata *ad = data;
381         if (ad->layout_main == NULL) {
382                 return SUCCESS;
383         }
384         update_ts(elm_layout_edje_get(ad->layout_main), main_txt,
385                   sizeof(main_txt) / sizeof(main_txt[0]));
386         return SUCCESS;
387 }
388
389 /**********************************************************************
390 ******************Global function ref*************************************
391 ***********************************************************************/
392
393 /**
394 * send
395 * This function is  used to main()
396 * @param           argc[in]         int
397 * @param           argv[in]         pointer to char*
398 * @return          when success, return SUCCESS or FAILED if error
399 * @exception
400 */
401 int main(int argc, char *argv[])
402 {
403         struct appcore_ops ops = {
404                 .create = ring_app_create,
405                 .terminate = ring_app_terminate,
406                 .pause = ring_app_pause,
407                 .resume = ring_app_resume,
408                 .reset = ring_app_reset,
409         };
410         memset(&g_alarm_ring_ad, 0x0, sizeof(struct appdata));
411         ops.data = &g_alarm_ring_ad;
412         g_alarm_ring_ad.motion_sensor_handle = -1;
413         return appcore_efl_main("alarm-ring", &argc, &argv, &ops);
414 }