tizen 2.0 beta
[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
30 #include "ug-client.h"
31
32 #define POPUP_TITLE_MAX (128)
33
34 #ifdef LOG_TAG
35 #undef LOG_TAG
36 #endif
37
38 #define LOG_TAG "UGClient"
39
40 static void prt_usage(const char *cmd)
41 {
42         fprintf(stderr, "Usage: %s [-f] [-F] -n <UG NAME> [-d <Arguments>]\n",
43                 cmd);
44         fprintf(stderr, "   Options:\n");
45         fprintf(stderr, "            -d argument\n");
46         fprintf(stderr, "            -F Fullview mode (Default)\n");
47         fprintf(stderr, "            -f Frameview mode\n");
48         fprintf(stderr, "   Example:\n");
49         fprintf(stderr,
50                 "            %s -F -n helloUG-efl -d \"name,John Doe\" -d \"age,30\"\n",
51                 cmd);
52
53 }
54
55 static void win_del(void *data, Evas_Object *obj, void *event)
56 {
57         elm_exit();
58 }
59
60 static void main_quit_cb(void *data, Evas_Object *obj,
61                          const char *emission, const char *source)
62 {
63         elm_exit();
64 }
65
66 static int rotate(enum appcore_rm m, void *data)
67 {
68         struct appdata *ad = data;
69         int r;
70
71         if (ad == NULL || ad->win == NULL)
72                 return 0;
73
74         switch (m) {
75         case APPCORE_RM_PORTRAIT_NORMAL:
76                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
77                 r = 0;
78                 break;
79         case APPCORE_RM_PORTRAIT_REVERSE:
80                 r = 180;
81                 ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
82                 break;
83         case APPCORE_RM_LANDSCAPE_NORMAL:
84                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
85                 r = 270;
86                 break;
87         case APPCORE_RM_LANDSCAPE_REVERSE:
88                 ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
89                 r = 90;
90                 break;
91         default:
92                 r = -1;
93                 break;
94         }
95
96         if (r >= 0)
97                 elm_win_rotation_with_resize_set(ad->win, r);
98
99         return 0;
100 }
101
102 static void close_popup(void *data, Evas_Object *obj, void *event_info)
103 {
104         evas_object_del(obj);
105         elm_exit();
106 }
107
108 static void show_popup(Evas_Object *win, const char *name)
109 {
110         Evas_Object *pu;
111         char buf[POPUP_TITLE_MAX];
112
113         if (!name || !win)
114                 return;
115
116         pu = elm_popup_add(win);
117         if (!pu)
118                 return;
119
120         evas_object_size_hint_weight_set(pu, EVAS_HINT_EXPAND,
121                                          EVAS_HINT_EXPAND);
122 #if 0
123         elm_popup_mode_set(pu, ELM_POPUP_TYPE_ALERT);
124         elm_popup_timeout_set(pu, 2);
125         snprintf(buf, POPUP_TITLE_MAX, "Received result from %s", name);
126         elm_popup_title_label_set(pu, buf);
127         evas_object_smart_callback_add(pu, "response", close_popup, NULL);
128 #endif
129         evas_object_show(pu);
130 }
131
132 void layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
133 {
134         struct appdata *ad;
135         Evas_Object *base;
136
137         if (!ug || !priv)
138                 return;
139
140         ad = priv;
141
142         base = ug_get_layout(ug);
143         if (!base)
144                 return;
145
146         switch (mode) {
147         case UG_MODE_FULLVIEW:
148                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
149                                                  EVAS_HINT_EXPAND);
150                 elm_win_resize_object_add(ad->win, base);
151                 ug_disable_effect(ug);
152                 evas_object_show(base);
153                 break;
154         case UG_MODE_FRAMEVIEW:
155                 elm_object_part_content_set(ad->ly_main, "content", base);
156                 break;
157         default:
158                 break;
159         }
160 }
161
162 void result_cb(ui_gadget_h ug, service_h result, void *priv)
163 {
164         struct appdata *ad;
165         int ret;
166
167         if (!ug || !priv)
168                 return;
169         ad = priv;
170
171         ret = service_reply_to_launch_request(result, ad->request, SERVICE_RESULT_SUCCEEDED);
172         if (ret != SERVICE_ERROR_NONE)
173                 LOGE("service_reply_to_launch_request failed, %d\n", ret);
174 }
175
176 void destroy_cb(ui_gadget_h ug, void *priv)
177 {
178         if (!ug)
179                 return;
180
181         ug_destroy(ug);
182         elm_exit();
183 }
184
185 static Evas_Object *create_win(const char *name)
186 {
187         Evas_Object *eo;
188         int w, h;
189
190         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
191         if (eo) {
192                 elm_win_title_set(eo, name);
193                 elm_win_borderless_set(eo, EINA_TRUE);
194                 evas_object_smart_callback_add(eo, "delete,request",
195                                                win_del, NULL);
196                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
197                                         &w, &h);
198                 evas_object_resize(eo, w, h);
199         }
200
201         return eo;
202 }
203
204 static Evas_Object *load_edj(Evas_Object *parent, const char *file,
205                              const char *group)
206 {
207         Evas_Object *eo;
208         int r;
209
210         eo = elm_layout_add(parent);
211         if (eo) {
212                 r = elm_layout_file_set(eo, file, group);
213                 if (!r) {
214                         evas_object_del(eo);
215                         return NULL;
216                 }
217
218                 evas_object_size_hint_weight_set(eo,
219                                                  EVAS_HINT_EXPAND,
220                                                  EVAS_HINT_EXPAND);
221         }
222
223         return eo;
224 }
225
226 static int low_memory(void *data)
227 {
228         return ug_send_event(UG_EVENT_LOW_MEMORY);
229 }
230
231 static int low_battery(void *data)
232 {
233         return ug_send_event(UG_EVENT_LOW_BATTERY);
234 }
235
236 static int lang_changed(void *data)
237 {
238         return ug_send_event(UG_EVENT_LANG_CHANGE);
239 }
240
241 static int app_create(void *data)
242 {
243         struct appdata *ad = data;
244         Evas_Object *win;
245         Evas_Object *ly;
246
247         /* create window */
248         win = create_win(PACKAGE);
249         if (win == NULL)
250                 return -1;
251         ad->win = win;
252         UG_INIT_EFL(ad->win, UG_OPT_INDICATOR_ENABLE);
253
254         /* load edje */
255         ly = load_edj(win, EDJ_FILE, GRP_MAIN);
256         if (ly == NULL)
257                 return -1;
258         elm_win_resize_object_add(win, ly);
259         edje_object_signal_callback_add(elm_layout_edje_get(ly),
260                                         "EXIT", "*", main_quit_cb, NULL);
261         ad->ly_main = ly;
262
263         lang_changed(ad);
264
265         appcore_set_rotation_cb(rotate, ad);
266         appcore_set_event_callback(APPCORE_EVENT_LOW_MEMORY, low_memory, ad);
267         appcore_set_event_callback(APPCORE_EVENT_LOW_BATTERY, low_battery, ad);
268         appcore_set_event_callback(APPCORE_EVENT_LANG_CHANGE, lang_changed, ad);
269
270         return 0;
271 }
272
273 static int app_terminate(void *data)
274 {
275         struct appdata *ad = data;
276
277         ug_destroy_all();
278         if (ad->ly_main)
279                 evas_object_del(ad->ly_main);
280
281         if (ad->win)
282                 evas_object_del(ad->win);
283
284         return 0;
285 }
286
287 static int app_pause(void *data)
288 {
289         ug_pause();
290         return 0;
291 }
292
293 static int app_resume(void *data)
294 {
295         ug_resume();
296         return 0;
297 }
298
299 static int svc_cb(void *data)
300 {
301         LOGD("svc_cb called\n");
302         return 0;
303 }
304
305 extern int service_create_event(bundle *data, service_h *service);
306 extern int appsvc_request_transient_app(bundle *b, Ecore_X_Window callee_id, appsvc_host_res_fn cbfunc, void *data);
307
308 static int app_reset(bundle *b, void *data)
309 {
310         struct appdata *ad = data;
311         struct ug_cbs cbs = { 0, };
312         service_h service;
313         enum ug_mode mode = UG_MODE_FULLVIEW;
314
315         Ecore_X_Window id2 = elm_win_xwindow_get(ad->win);
316         appsvc_request_transient_app(b, id2, svc_cb, "svc test");
317
318         if (ad->win) {
319                 elm_win_activate(ad->win);
320                 evas_object_show(ad->win);
321         }
322
323         if (ad->data)   /* ug-launcher */
324                 service_create_event(ad->data, &service);
325         else
326                 service_create_event(b, &service);
327
328         service_clone(&ad->request, service);
329
330         cbs.layout_cb = layout_cb;
331         cbs.destroy_cb = destroy_cb;
332         cbs.result_cb = result_cb;
333         cbs.priv = ad;
334
335         ad->ug = ug_create(NULL, ad->name, mode, service, &cbs);
336
337         return 0;
338 }
339
340 static int update_argument(const char *optarg, struct appdata *ad)
341 {
342         const char *key;
343         const char *val;
344         key = strtok((char *)optarg, ",");
345         if (!key)
346                 return -1;
347
348         val = optarg + strlen(key) + 1;
349         if (!val)
350                 return -1;
351
352         if (!ad->data)
353                 ad->data = bundle_create();
354         if (!ad->data)
355                 return -1;
356         bundle_add(ad->data, key, val);
357         return 0;
358 }
359
360 int main(int argc, char *argv[])
361 {
362         int opt;
363         struct appdata ad;
364         struct appcore_ops ops = {
365                 .create = app_create,
366                 .terminate = app_terminate,
367                 .pause = app_pause,
368                 .resume = app_resume,
369                 .reset = app_reset,
370         };
371         int cmdlen = 0;
372
373         setenv("ELM_ENGINE", "gl", 1); //enabling the OpenGL as the backend of the EFL.
374
375         memset(&ad, 0x0, sizeof(struct appdata));
376         ops.data = &ad;
377
378         cmdlen = strlen(argv[0]);
379         if (strncmp(argv[0], "ug-launcher", cmdlen) == 0
380                 || strncmp(argv[0], "/usr/bin/ug-launcher", cmdlen) == 0) {
381                 while ((opt = getopt(argc, argv, "n:d:")) != -1) {
382                         switch (opt) {
383                         case 'n':
384                                 if (optarg)
385                                         ad.name = strdup(optarg);
386                                 break;
387                         case 'd':
388                                 if (update_argument(optarg, &ad)) {
389                                         if (ad.data)
390                                                 bundle_free(ad.data);
391                                         prt_usage(argv[0]);
392                                         return -1;
393                                 }
394                                 break;
395                         default:
396                                 prt_usage(argv[0]);
397                                 return -1;
398                         }
399                 }
400
401                 if (!ad.name) {
402                         prt_usage(argv[0]);
403                         return -1;
404                 }
405                 argc = 1; // remove appsvc bundle
406         } else {        /* ug-client */
407                 char *name = NULL;
408                 name = strrchr(argv[0], '/');
409                 if (name == NULL)
410                         return -1;
411                 /* .../bin/{name} */
412                 ad.name = strdup(&name[1]);
413         }
414         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
415 }