[SDL_Tizen] Fix build warnings
[platform/upstream/SDL.git] / src / video / tizen / indicator / SDL_tizenindicator.c
1 #include <app.h>
2 #include <Elementary.h>
3 #include <Ecore_Ipc.h>
4
5 //for 'tizen_policy_set_transient_for'
6 #include <Ecore_Wayland.h>
7 #include <tizen-extension-client-protocol.h>
8 #include <wayland-util.h>
9
10 #include "SDL_tizenindicator.h"
11 #include "dbg.h"
12
13 static Eina_Bool
14 _tizen_set_transient_for (appdata_s *ad, uint32_t child_id, uint32_t parent_id)
15 {
16         /*
17         child_id : indicator resource id
18         parent_id :  SDL window resource id,
19         */
20         Eina_Inlist *globals;
21         Ecore_Wl_Global *global;
22         struct wl_registry *registry;
23         if (!ad->tizen_policy)
24         {
25                 registry = ecore_wl_registry_get();
26                 globals = ecore_wl_globals_get();
27
28                 if (!registry || !globals)
29                         return EINA_FALSE;
30                 EINA_INLIST_FOREACH(globals, global)
31                 {
32                         if (!strcmp(global->interface, "tizen_policy"))
33                         ad->tizen_policy = wl_registry_bind(registry, global->id, &tizen_policy_interface, 7);
34                 }
35                 if (!ad->tizen_policy)
36                         return EINA_FALSE;
37         }
38
39         _INFO("[SDL] handle_server_data major : %d, minor : %d\n", child_id, parent_id);
40         tizen_policy_set_transient_for (ad->tizen_policy, child_id, parent_id);
41
42         return EINA_TRUE;
43 }
44
45 Eina_Bool
46 handler_server_add(void *data, int ev_type EINA_UNUSED, void *ev)
47 {
48         appdata_s *ad = (appdata_s *)data;
49         IPC_HEAD(ad , Add);
50
51         _INFO("[SDL] handler_server_add\n");
52         ad->isShowing = 1;
53         ecore_ipc_server_send(ad->IpcServer, OP_INDICATOR_INIT, ad->indicator_h, 0, 0, 0, NULL, 0);
54         return 1;
55 }
56
57 Eina_Bool
58 handler_server_del(void *data, int ev_type EINA_UNUSED, void *ev)
59 {
60         appdata_s *ad = (appdata_s *)data;
61         IPC_HEAD(ad, Del);
62         _INFO("[SDL] handler_server_del\n");
63         return 1;
64 }
65
66 void resetTimer(appdata_s *ad)
67 {
68         if(ad->timer_hide_effect)
69         {
70                 ecore_timer_freeze(ad->timer_hide_effect);
71                 ecore_timer_reset(ad->timer_hide_effect);
72                 ecore_timer_del(ad->timer_hide_effect);
73         }
74
75         if(ad->timer_hide_window)
76         {
77                 ecore_timer_freeze(ad->timer_hide_window);
78                 ecore_timer_reset(ad->timer_hide_window);
79                 ecore_timer_del(ad->timer_hide_window);
80         }
81
82         ad->timer_hide_effect = NULL;
83         ad->timer_hide_window = NULL;
84 }
85
86 Eina_Bool ecore_timer_cb_hide_effect(void *data) {
87         appdata_s *ad = (appdata_s *)data;
88
89         elm_object_signal_emit(ad->conform, "indicator,hide,effect", "elm");
90         ecore_ipc_server_send(ad->IpcServer, OP_INDICATOR_HIDE, 1, 0, 0, 0, NULL, 0);
91
92         return ECORE_CALLBACK_CANCEL;
93 }
94
95 Eina_Bool ecore_timer_cb_hide_window(void *data) {
96         appdata_s *ad = (appdata_s *)data;
97         ecore_ipc_server_send(ad->IpcServer, OP_INDICATOR_HIDE, 2, 0, 0, 0, NULL, 0);
98         resetTimer(ad);
99         elm_win_iconified_set(ad->win, EINA_TRUE);
100         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_HIDE);
101
102         return ECORE_CALLBACK_CANCEL;
103 }
104
105 void start_hide_timer(appdata_s *ad)
106 {
107
108         ad->timer_hide_effect = ecore_timer_add(2.4, ecore_timer_cb_hide_effect, ad);
109         ad->timer_hide_window = ecore_timer_add(2.5, ecore_timer_cb_hide_window, ad);
110 }
111
112 void setIndicatorGeometry(appdata_s *ad)
113 {
114         int idx = ad->rotation / 90;
115         elm_win_rotation_set(ad->win, ad->rotation);
116         evas_object_resize(ad->win, info_size[idx][0], info_size[idx][1]);
117         evas_object_move(ad->win, info_position[idx][0], info_position[idx][1]);
118
119         _INFO("[SDL] setIndicator - rotation:%d, x:%d, y:%d, w:%d, h:%d\n", ad->rotation,
120                         info_position[idx][0], info_position[idx][1], info_size[idx][0], info_size[idx][1]);
121
122 }
123
124 void setIndicatorOpacity(appdata_s *ad, int style)
125 {
126         switch(style)
127         {
128         case 1:
129                 elm_win_indicator_opacity_set(ad->win, SDL_INDICATOR_OPAQUE);
130         break;
131         case 2:
132                 elm_win_indicator_opacity_set(ad->win, SDL_INDICATOR_TRANSLUCENT);
133         break;
134         case 3:
135                 elm_win_indicator_opacity_set(ad->win, SDL_INDICATOR_TRANSPARENT);
136         break;
137         case 4:
138                 elm_win_indicator_opacity_set(ad->win, SDL_INDICATOR_OPAQUE);
139                 elm_object_signal_emit(ad->conform, "elm,state,indicator,overlap", "elm");
140         break;
141         }
142 }
143
144 void showIndicator(appdata_s *ad)
145 {
146         elm_win_iconified_set(ad->win, EINA_FALSE);
147         setIndicatorGeometry(ad);
148         elm_win_indicator_mode_set(ad->win, ELM_WIN_INDICATOR_SHOW);
149         elm_object_signal_emit(ad->conform, "indicator,show,effect", "elm");
150
151         if(ad->indicator_type == SDL_INDICATOR_TRANSPARENT || ad->rotation % 180)
152                 start_hide_timer(ad);
153 }
154
155 Eina_Bool handler_server_data(void *data, int ev_type EINA_UNUSED, void *ev)
156 {
157         appdata_s *ad = (appdata_s *)data;
158         IPC_HEAD(ad, Data);
159         Ecore_Ipc_Event_Server_Data* epcEvent = (Ecore_Ipc_Event_Server_Data*)ev;
160
161         switch(epcEvent->major)
162         {
163         case OP_INDICATOR_SHOW:
164         {
165                 if(!ad->isShowing)
166                         return EINA_TRUE;
167                 _INFO("[SDL] 1. call event OP_INDICATOR_SHOW");
168
169                 resetTimer(ad);
170
171                 ad->rotation = epcEvent->minor;
172                 ad->parent_res_id = epcEvent->ref;
173
174                 if(epcEvent->ref_to!=0)
175                 {
176                         ad->indicator_type = epcEvent->ref_to;
177                         setIndicatorOpacity(ad, ad->indicator_type);
178                 }
179
180                 showIndicator(ad);
181
182                 _tizen_set_transient_for(ad, ad->indicator_res_id, ad->parent_res_id);
183                 ecore_ipc_server_send(ad->IpcServer, OP_INDICATOR_SHOW, 0, 0, 0, 0, NULL, 0);
184
185         break;
186         }
187         case OP_RESUME:
188         {
189                 ad->timer_hide_window = ecore_timer_add(0.5, ecore_timer_cb_hide_window, ad);
190                 ad->isShowing = 1;
191         break;
192         }
193         case OP_PAUSE:
194         {
195                 ad->isShowing = 0;
196         break;
197         }
198         case OP_TERMINATE:
199         {
200                 ecore_ipc_server_send(ad->IpcServer, OP_TERMINATE, 0, 0, 0, 0, NULL, 0);
201                 ui_app_exit();
202         break;
203         }
204         }
205
206         return EINA_TRUE;
207 }
208
209 void create_ipcServer(appdata_s *ad)
210 {
211         ecore_ipc_init();
212         if((ad->IpcServer = ecore_ipc_server_connect(ECORE_IPC_LOCAL_USER, ECORE_IPC_INDICATOR, 0, NULL)))
213         {
214                 ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD, handler_server_add, ad);
215                 ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL, handler_server_del, NULL);
216                 ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA, handler_server_data, ad);
217         }
218         else
219         {
220                 _INFO("[SDL] Failed IPC Server\n");
221                 ui_app_exit();
222         }
223 }
224
225 Evas_Object *
226 elm_win_create(const char *name)
227 {
228         Evas_Object *win = elm_win_add(NULL, name, ELM_WIN_UTILITY);
229
230         elm_win_autodel_set(win, EINA_TRUE);
231         elm_win_alpha_set(win, EINA_TRUE);
232         elm_win_conformant_set(win, EINA_TRUE);
233
234         if (elm_win_wm_rotation_supported_get(win)) {
235                 int rots[4] = { 0, 90, 180, 270 };
236                 elm_win_wm_rotation_available_rotations_set(win, (const int *)(&rots), 4);
237         }
238
239         evas_object_show(win);
240         elm_win_prop_focus_skip_set(win, EINA_TRUE);
241         return win;
242 }
243
244 Evas_Object *
245 elm_conformand_create(Evas_Object *parent)
246 {
247         Evas_Object* conform = elm_conformant_add(parent);
248         evas_object_size_hint_align_set(conform,  EVAS_HINT_FILL, EVAS_HINT_FILL);
249         evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
250         elm_win_resize_object_add(parent, conform);
251         elm_win_indicator_mode_set(parent, ELM_WIN_INDICATOR_SHOW);
252         elm_win_indicator_opacity_set(parent, ELM_WIN_INDICATOR_TRANSPARENT);
253         evas_object_show(conform);
254         return conform;
255 }
256
257 static void
258 win_delete_request_cb(void *data, Evas_Object *obj, void *event_info)
259 {
260         ui_app_exit();
261 }
262
263 static Eina_Bool _window_show_cb(void *data, int type, void *event)
264 {
265         appdata_s *ad = data;
266         Ecore_Wl_Event_Window_Show *e;
267
268         uint32_t g_res_id = -1;
269
270         e = event;
271         if (e->data[0] > 0)
272                 g_res_id = e->data[0];
273
274         dlog_print(DLOG_INFO, LOG_TAG, "[SDL]Window resource Id : %d", g_res_id);
275
276         if(g_res_id!=0)
277                 ad->indicator_res_id = g_res_id;
278
279         return ECORE_CALLBACK_PASS_ON;
280 }
281
282 void init_info(appdata_s *ad)
283 {
284         int width=0;
285         int height=0;
286         elm_win_screen_size_get(ad->win, 0, 0, &width, &height);
287
288         int h;
289         edje_object_part_geometry_get(elm_layout_edje_get(ad->conform), "indicator_base", NULL, NULL, NULL, &(h));
290         ad->indicator_h = h;
291         evas_object_resize(ad->win, width, ad->indicator_h);
292
293         dlog_print(DLOG_INFO, LOG_TAG, "[SDL]indicator size : %d", ad->indicator_h);
294         for(int i=0; i<4; i++)
295                 info_position[i][0] = info_position[i][1] = 0;
296         info_position[2][1] = height - ad->indicator_h;
297         info_position[3][0] = width - ad->indicator_h;
298
299         for(int i=0; i<4; i++)
300         {
301                 if((i*90)%180)
302                 {
303                         info_size[i][0] = height;
304                         info_size[i][1] = ad->indicator_h;
305                 }
306                 else
307                 {
308                         info_size[i][0] = width;
309                         info_size[i][1] = ad->indicator_h;
310                 }
311         }
312 }
313
314 static void
315 create_base_gui(appdata_s *ad)
316 {
317         ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, _window_show_cb, ad);
318
319         ad->win = (Evas_Object *)elm_win_create("SDL_Indicator");
320         evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
321         ad->conform = (Evas_Object *)elm_conformand_create(ad->win);
322         init_info(ad);
323 }
324
325 static bool
326 app_create(void *data)
327 {
328         /* Hook to take necessary actions before main event loop starts
329                 Initialize UI resources and application's data
330                 If this function returns true, the main loop of application starts
331                 If this function returns false, the application is terminated */
332         appdata_s *ad = data;
333
334         create_base_gui(ad);
335         create_ipcServer(ad);
336
337         return true;
338 }
339
340 static void
341 app_control(app_control_h app_control, void *data)
342 {
343         /* Handle the launch request. */
344 }
345
346 static void
347 app_pause(void *data)
348 {
349         /* Take necessary actions when application becomes invisible. */
350 }
351
352 static void
353 app_resume(void *data)
354 {
355         /* Take necessary actions when application becomes visible. */
356 }
357
358 static void
359 app_terminate(void *data)
360 {
361         /* Release all resources. */
362 }
363
364 static void
365 ui_app_lang_changed(app_event_info_h event_info, void *user_data)
366 {
367         /*APP_EVENT_LANGUAGE_CHANGED*/
368
369         int ret;
370         char *language;
371
372         ret = app_event_get_language(event_info, &language);
373         if (ret != APP_ERROR_NONE) {
374                 dlog_print(DLOG_ERROR, LOG_TAG, "app_event_get_language() failed. Err = %d.", ret);
375                 return;
376         }
377
378         if (language != NULL) {
379                 elm_language_set(language);
380                 free(language);
381         }
382 }
383
384 static void
385 ui_app_orient_changed(app_event_info_h event_info, void *user_data)
386 {
387         /*APP_EVENT_DEVICE_ORIENTATION_CHANGED*/
388         return;
389 }
390
391 static void
392 ui_app_region_changed(app_event_info_h event_info, void *user_data)
393 {
394         /*APP_EVENT_REGION_FORMAT_CHANGED*/
395 }
396
397 static void
398 ui_app_low_battery(app_event_info_h event_info, void *user_data)
399 {
400         /*APP_EVENT_LOW_BATTERY*/
401 }
402
403 static void
404 ui_app_low_memory(app_event_info_h event_info, void *user_data)
405 {
406         /*APP_EVENT_LOW_MEMORY*/
407 }
408
409 int
410 main(int argc, char *argv[])
411 {
412         appdata_s ad = {0,};
413         ad.indicator_type = SDL_INDICATOR_TRANSPARENT;
414         int ret = 0;
415
416         ui_app_lifecycle_callback_s event_callback = {0,};
417         app_event_handler_h handlers[5] = {NULL, };
418
419         event_callback.create = app_create;
420         event_callback.terminate = app_terminate;
421         event_callback.pause = app_pause;
422         event_callback.resume = app_resume;
423         event_callback.app_control = app_control;
424
425
426         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
427         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
428         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
429         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
430         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
431
432         ret = ui_app_main(argc, argv, &event_callback, &ad);
433         if (ret != APP_ERROR_NONE) {
434                 dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);
435         }
436
437         return ret;
438 }