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