move to appcore_get_rotation_state in the last of app_create
[framework/appfw/ui-gadget-1.git] / client / ug-client.c
1 /*
2  *  UI Gadget
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <appcore-efl.h>
24 #include <ui-gadget.h>
25 #include <Ecore_X.h>
26 #include <dlog.h>
27 #include <aul.h>
28 #include <appsvc.h>
29 #include <runtime_info.h>
30
31 #include "ug-client.h"
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "UGClient"
38
39 static void prt_usage(const char *cmd)
40 {
41         fprintf(stderr, "Usage: %s [-f] [-F] -n <UG NAME> [-d <Arguments>]\n",
42                 cmd);
43         fprintf(stderr, "   Options:\n");
44         fprintf(stderr, "            -d argument\n");
45         fprintf(stderr, "            -F Fullview mode (Default)\n");
46         fprintf(stderr, "            -f Frameview mode\n");
47         fprintf(stderr, "   Example:\n");
48         fprintf(stderr,
49                 "            %s -F -n helloUG-efl -d \"name,John Doe\" -d \"age,30\"\n",
50                 cmd);
51
52 }
53
54 static void win_del(void *data, Evas_Object *obj, void *event)
55 {
56         elm_exit();
57 }
58
59 static void main_quit_cb(void *data, Evas_Object *obj,
60                          const char *emission, const char *source)
61 {
62         elm_exit();
63 }
64
65 static int rotate(enum appcore_rm m, void *data)
66 {
67         struct appdata *ad = data;
68         int r;
69         bool is_rotation_lock = false;
70
71         if (ad == NULL || ad->win == NULL)
72                 return 0;
73
74         /* rotation lock */
75         r = runtime_info_get_value_bool(RUNTIME_INFO_KEY_ROTATION_LOCK_ENABLED, &is_rotation_lock);
76         if ( !r && is_rotation_lock)
77                 m = APPCORE_RM_PORTRAIT_NORMAL;
78
79         switch (m) {
80         case APPCORE_RM_PORTRAIT_NORMAL:
81                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
82                 r = 0;
83                 break;
84         case APPCORE_RM_PORTRAIT_REVERSE:
85                 r = 180;
86                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
87                 break;
88         case APPCORE_RM_LANDSCAPE_NORMAL:
89                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
90                 r = 270;
91                 break;
92         case APPCORE_RM_LANDSCAPE_REVERSE:
93                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
94                 r = 90;
95                 break;
96         default:
97                 r = -1;
98                 break;
99         }
100
101         return 0;
102 }
103
104 void layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
105 {
106         struct appdata *ad;
107         Evas_Object *base;
108
109         if (!ug || !priv)
110                 return;
111
112         ad = priv;
113
114         base = ug_get_layout(ug);
115         if (!base)
116                 return;
117
118         switch (mode) {
119         case UG_MODE_FULLVIEW:
120                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
121                                                  EVAS_HINT_EXPAND);
122                 elm_win_resize_object_add(ad->win, base);
123                 ug_disable_effect(ug);
124                 evas_object_show(base);
125                 break;
126         case UG_MODE_FRAMEVIEW:
127                 elm_object_part_content_set(ad->ly_main, "content", base);
128                 break;
129         default:
130                 break;
131         }
132 }
133
134 void result_cb(ui_gadget_h ug, service_h result, void *priv)
135 {
136         struct appdata *ad;
137         int ret;
138
139         if (!ug || !priv)
140                 return;
141         ad = priv;
142
143         ret = service_reply_to_launch_request(result, ad->request, SERVICE_RESULT_SUCCEEDED);
144         if (ret != SERVICE_ERROR_NONE)
145                 LOGE("service_reply_to_launch_request failed, %d\n", ret);
146 }
147
148 void destroy_cb(ui_gadget_h ug, void *priv)
149 {
150         if (!ug)
151                 return;
152
153         ug_destroy(ug);
154         elm_exit();
155 }
156
157 static Evas_Object *create_win(const char *name)
158 {
159         Evas_Object *eo;
160         int w, h;
161
162         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
163         if (eo) {
164                 elm_win_title_set(eo, name);
165                 elm_win_borderless_set(eo, EINA_TRUE);
166                 evas_object_smart_callback_add(eo, "delete,request",
167                                                win_del, NULL);
168                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
169                                         &w, &h);
170                 evas_object_resize(eo, w, h);
171         }
172
173         return eo;
174 }
175
176 static Evas_Object *load_edj(Evas_Object *parent, const char *file,
177                              const char *group)
178 {
179         Evas_Object *eo;
180         int r;
181
182         eo = elm_layout_add(parent);
183         if (eo) {
184                 r = elm_layout_file_set(eo, file, group);
185                 if (!r) {
186                         evas_object_del(eo);
187                         return NULL;
188                 }
189
190                 evas_object_size_hint_weight_set(eo,
191                                                  EVAS_HINT_EXPAND,
192                                                  EVAS_HINT_EXPAND);
193         }
194
195         return eo;
196 }
197
198 static int low_memory(void *data)
199 {
200         return ug_send_event(UG_EVENT_LOW_MEMORY);
201 }
202
203 static int low_battery(void *data)
204 {
205         return ug_send_event(UG_EVENT_LOW_BATTERY);
206 }
207
208 static int lang_changed(void *data)
209 {
210         return ug_send_event(UG_EVENT_LANG_CHANGE);
211 }
212
213 static int app_create(void *data)
214 {
215         struct appdata *ad = data;
216         enum appcore_rm rm;
217         Evas_Object *win;
218         Evas_Object *ly;
219
220         /* create window */
221         win = create_win(PACKAGE);
222         if (win == NULL)
223                 return -1;
224         ad->win = win;
225         UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_ENABLE);
226
227         /* load edje */
228         ly = load_edj(win, EDJ_FILE, GRP_MAIN);
229         if (ly == NULL)
230                 return -1;
231         elm_win_resize_object_add(win, ly);
232         edje_object_signal_callback_add(elm_layout_edje_get(ly),
233                                         "EXIT", "*", main_quit_cb, NULL);
234         ad->ly_main = ly;
235
236         lang_changed(ad);
237
238         appcore_set_rotation_cb(rotate, ad);
239         appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, low_memory, ad);
240         appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, low_battery, ad);
241         appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, lang_changed, ad);
242
243         if (appcore_get_rotation_state(&rm) == 0)
244                 rotate(rm, ad);
245
246         return 0;
247 }
248
249 static int app_terminate(void *data)
250 {
251         struct appdata *ad = data;
252
253         ug_destroy_all();
254         if (ad->ly_main)
255                 evas_object_del(ad->ly_main);
256
257         if (ad->win)
258                 evas_object_del(ad->win);
259
260         return 0;
261 }
262
263 static int app_pause(void *data)
264 {
265         ug_pause();
266         return 0;
267 }
268
269 static int app_resume(void *data)
270 {
271         ug_resume();
272         return 0;
273 }
274
275 static int svc_cb(void *data)
276 {
277         LOGD("svc_cb called\n");
278         return 0;
279 }
280
281 extern int service_create_event(bundle *data, service_h *service);
282 extern int appsvc_request_transient_app(bundle *b, Ecore_X_Window callee_id, appsvc_host_res_fn cbfunc, void *data);
283
284 static int app_reset(bundle *b, void *data)
285 {
286         struct appdata *ad = data;
287         struct ug_cbs cbs = { 0, };
288         service_h service;
289         enum ug_mode mode = UG_MODE_FULLVIEW;
290
291         Ecore_X_Window id2 = elm_win_xwindow_get(ad->win);
292         appsvc_request_transient_app(b, id2, svc_cb, "svc test");
293
294         if (ad->win) {
295                 elm_win_activate(ad->win);
296                 evas_object_show(ad->win);
297         }
298
299         if (ad->data)   /* ug-launcher */
300                 service_create_event(ad->data, &service);
301         else
302                 service_create_event(b, &service);
303
304         service_clone(&ad->request, service);
305
306         cbs.layout_cb = layout_cb;
307         cbs.destroy_cb = destroy_cb;
308         cbs.result_cb = result_cb;
309         cbs.priv = ad;
310
311         ad->ug = ug_create(NULL, ad->name, mode, service, &cbs);
312         if (ad->ug == NULL) {
313                 LOGE("ug_create fail: %s\n", ad->name);
314                 elm_exit();
315         }
316
317         return 0;
318 }
319
320 static int update_argument(const char *optarg, struct appdata *ad)
321 {
322         const char *key;
323         const char *val;
324         key = strtok((char *)optarg, ",");
325         if (!key)
326                 return -1;
327
328         val = optarg + strlen(key) + 1;
329         if (!val)
330                 return -1;
331
332         if (!ad->data)
333                 ad->data = bundle_create();
334         if (!ad->data)
335                 return -1;
336         bundle_add(ad->data, key, val);
337         return 0;
338 }
339
340 int main(int argc, char *argv[])
341 {
342         int opt;
343         struct appdata ad;
344         struct appcore_ops ops = {
345                 .create = app_create,
346                 .terminate = app_terminate,
347                 .pause = app_pause,
348                 .resume = app_resume,
349                 .reset = app_reset,
350         };
351         int cmdlen = 0;
352
353         setenv("ELM_ENGINE", "gl", 1); //enabling the OpenGL as the backend of the EFL.
354
355         memset(&ad, 0x0, sizeof(struct appdata));
356         ops.data = &ad;
357
358         cmdlen = strlen(argv[0]);
359         if (strncmp(argv[0], "ug-launcher", cmdlen) == 0
360                 || strncmp(argv[0], "/usr/bin/ug-launcher", cmdlen) == 0) {
361                 while ((opt = getopt(argc, argv, "n:d:")) != -1) {
362                         switch (opt) {
363                         case 'n':
364                                 if (optarg)
365                                         ad.name = strdup(optarg);
366                                 break;
367                         case 'd':
368                                 if (update_argument(optarg, &ad)) {
369                                         if (ad.data)
370                                                 bundle_free(ad.data);
371                                         prt_usage(argv[0]);
372                                         return -1;
373                                 }
374                                 break;
375                         default:
376                                 prt_usage(argv[0]);
377                                 return -1;
378                         }
379                 }
380
381                 if (!ad.name) {
382                         prt_usage(argv[0]);
383                         return -1;
384                 }
385                 argc = 1; // remove appsvc bundle
386         } else {        /* ug-client */
387                 char *name = NULL;
388                 name = strrchr(argv[0], '/');
389                 if (name == NULL)
390                         return -1;
391                 /* .../bin/{name} */
392                 ad.name = strdup(&name[1]);
393         }
394         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
395 }