Initial pepper-based widget framework commit
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
18 #include <stdlib.h>
19
20 #include <bundle.h>
21 #include <aul.h>
22 #include <dlog.h>
23 #include <glib.h>
24 #include <glib-object.h>
25 #include <stdlib.h>
26 #include <app_control.h>
27 #include <app_control_internal.h>
28 #include <Elementary.h>
29 #include <widget_errno.h>
30 #include <widget_instance.h>
31 #include <widget_service_internal.h>
32
33 #include "widget_app.h"
34 #include "widget-log.h"
35 #include "widget-private.h"
36 #include "widget_app_internal.h"
37
38 #ifdef LOG_TAG
39 #undef LOG_TAG
40 #endif
41
42 #define STR_MAX_BUF 128
43 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
44 #define K_REASON    "__WC_K_REASON__"
45
46 typedef enum _widget_obj_state_e {
47         WC_READY = 0,
48         WC_RUNNING = 1,
49         WC_PAUSED = 2,
50         WC_TERMINATED = 3
51 } widget_obj_state_e;
52
53 struct app_event_handler {
54         app_event_type_e type;
55         app_event_cb cb;
56         void *data;
57 };
58
59 struct app_event_info {
60         app_event_type_e type;
61         void *value;
62 };
63
64 struct _widget_class {
65         void *user_data;
66         widget_instance_lifecycle_callback_s ops;
67         char *classid;
68         struct _widget_class *next;
69         struct _widget_class *prev;
70 };
71
72 struct _widget_context {
73         char *id;
74         struct _widget_class *provider;
75         int state;
76         void *tag;
77         Evas_Object *win;
78         bundle *content;
79         widget_instance_lifecycle_callback_s ops;
80 };
81
82 typedef struct _widget_class widget_class_s;
83 typedef struct _widget_context widget_context_s;
84
85 static int caller_pid = 0;
86 static widget_app_lifecycle_callback_s *app_ops;
87 static void *app_user_data = NULL;
88 static char *appid = NULL;
89 static widget_class_h class_provider = NULL;
90 static GList *contexts = NULL;
91 static char *viewer_endpoint = NULL;
92
93 static gint __comp_by_id(gconstpointer a, gconstpointer b)
94 {
95         widget_context_s *wc = (widget_context_s*)a;
96
97         return strcmp(wc->id, (const char*)b);
98 }
99
100 static widget_context_s* __find_context_by_id(const char *id)
101 {
102         GList* ret = g_list_find_custom(contexts, id, __comp_by_id);
103
104         if (ret == NULL)
105                 return NULL;
106
107         return ret->data;
108 }
109
110 static int __send_update_status(const char *class_id, const char *instance_id,
111         int status, bundle *extra, int internal_only)
112 {
113         bundle *b = extra;
114
115         if (b == NULL)
116                 b = bundle_create();
117
118         bundle_add_str(b, WIDGET_K_ID, class_id);
119         bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
120         bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
121
122         aul_app_com_send(viewer_endpoint, b);
123
124         if (extra == NULL)
125                 bundle_free(b);
126 }
127
128 static int __instance_create(widget_class_h handle, const char *id, bundle *b)
129 {
130         widget_context_s *wc = NULL;
131         int w = 0, h = 0;
132         char *w_str = NULL, *h_str = NULL;
133         char *remain = NULL;
134         int ret = 0;
135
136         wc = (widget_context_s *)malloc(sizeof(widget_context_s));
137         if (!wc)
138                 return WIDGET_ERROR_OUT_OF_MEMORY;
139
140         wc->state = WC_READY;
141         wc->id = g_strdup(id);
142         wc->provider = handle;
143         wc->win = NULL;
144
145         wc->content = bundle_dup(b);
146         bundle_get_str(b, WIDGET_K_WIDTH, &w_str);
147         bundle_get_str(b, WIDGET_K_HEIGHT, &h_str);
148
149         if (w_str) {
150                 w = (int)g_ascii_strtoll(w_str, &remain, 10);
151         }
152
153         if (h_str) {
154                 h = (int)g_ascii_strtoll(h_str, &remain, 10);
155         }
156
157         contexts = g_list_append(contexts, wc);
158
159         handle->ops.create(wc, b, w, h, handle->user_data);
160
161         ret = __send_update_status(handle->classid, wc->id, WIDGET_INSTANCE_EVENT_CREATE, b, 0);
162
163         return ret;
164 }
165
166 static int __instance_destroy(widget_class_h handle, const char *id, widget_destroy_type_e reason, bundle *b)
167 {
168         widget_context_s *wc = __find_context_by_id(id);
169         int ret = 0;
170
171         if (wc) {
172                 wc->state = WC_TERMINATED;
173                 handle->ops.destroy(wc, (widget_app_destroy_type_e)reason, b, handle->user_data);
174
175                 ret = __send_update_status(handle->classid, id, WIDGET_INSTANCE_EVENT_TERMINATE, b, 0);
176
177                 contexts = g_list_remove(contexts, wc);
178
179                 if (wc->id)
180                         free(wc->id);
181                 free(wc);
182         } else {
183                 _E("could not find widget obj: %s", id);
184                 ret = WIDGET_ERROR_INVALID_PARAMETER;
185         }
186
187         return ret;
188 }
189
190 static widget_class_h __find_class_handler(const char *class_id, widget_class_h handle)
191 {
192         if (!class_id || !handle)
193                 return NULL;
194
195         widget_class_h head = handle;
196
197         while (head) {
198                 if (head->classid && strcmp(head->classid, class_id) == 0)
199                         return head;
200
201                 head = head->next;
202         }
203
204         return NULL;
205 }
206
207 static void __control(bundle *b)
208 {
209         char *class_id = NULL;
210         char *id = NULL;
211         char *operation = NULL;
212         char *reason = NULL;
213         char *remain = NULL;
214         int destroy_type = WIDGET_DESTROY_TYPE_DEFAULT;
215
216         widget_class_h handle = NULL;
217         bundle_get_str(b, WIDGET_K_CLASS, &class_id);
218         /* for previous version compatibility, use appid for default class id */
219         if (class_id == NULL)
220                 class_id = appid;
221
222         bundle_get_str(b, WIDGET_K_INSTANCE, &id);
223         bundle_get_str(b, WIDGET_K_OPERATION, &operation);
224
225         handle = __find_class_handler(class_id, class_provider);
226         if (!handle) {
227                 _E("no handle provided: %s", class_id);
228                 goto error;
229         }
230
231         if (!operation) {
232                 _E("no operation provided");
233                 goto error;
234         }
235
236         if (strcmp(operation, "create") == 0) {
237                 __instance_create(handle, id, b);
238         } else if (strcmp(operation, "resize") == 0) {
239                 /* TODO */
240         } else if (strcmp(operation, "update") == 0) {
241                 /* TODO */
242         } else if (strcmp(operation, "destroy") == 0) {
243                 bundle_get_str(b, WIDGET_K_REASON, &reason);
244                 if (reason)
245                         destroy_type = (int)g_ascii_strtoll(reason, &remain, 10);
246
247                 __instance_destroy(handle, id, destroy_type, b);
248         } else if (strcmp(operation, "resume") == 0) {
249                 /* TODO */
250         } else if (strcmp(operation, "pause") == 0) {
251                 /* TODO */
252         }
253
254         return;
255 error:
256         LOGD("error on control");
257         return;
258 }
259
260 static void __show_all()
261 {
262         LOGD("resume");
263 }
264
265 static int __aul_handler(aul_type type, bundle *b, void *data)
266 {
267         char *caller = NULL;
268         char *remain = NULL;
269
270         switch (type) {
271         case AUL_START:
272                 if (b) {
273                         bundle_get_str(b, WIDGET_K_CALLER, &caller);
274                         if (caller)
275                                 caller_pid = g_ascii_strtoll(caller, &remain, 10);
276                         else {
277                                 /* using caller appid and query pid using caller appid? */
278                                 _E("no caller pid");
279                         }
280                 }
281
282                 __control(b);
283                 break;
284         case AUL_RESUME:
285                 __show_all();
286                 break;
287         case AUL_TERMINATE:
288                 widget_app_exit();
289                 break;
290         default:
291                 break;
292         }
293
294         return 0;
295 }
296
297
298 static int __before_loop(int argc, char **argv)
299 {
300         int r;
301         bundle *kb = NULL;
302         char *wayland_display = NULL;
303         char *xdg_runtime_dir = NULL;
304
305 #if !(GLIB_CHECK_VERSION(2, 36, 0))
306         g_type_init();
307 #endif
308
309         kb = bundle_import_from_argv(argc, argv);
310         if (kb) {
311                 bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
312                 bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
313                 bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
314
315                 if (xdg_runtime_dir)
316                         setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
317
318                 if (wayland_display)
319                         setenv("WAYLAND_DISPLAY", wayland_display, 1);
320
321                 bundle_free(kb);
322                 kb = NULL;
323         } else {
324                 _E("failed to get launch argv");
325         }
326
327         elm_init(argc, argv);
328
329         r = aul_launch_init(__aul_handler, NULL);
330         if (r < 0) {
331                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
332                                         "Fail to call the aul_launch_init");
333         }
334
335         r = aul_launch_argv_handler(argc, argv);
336         if (r < 0) {
337                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
338                                         "Fail to call the aul_launch_argv_handler");
339         }
340
341         r = app_get_id(&appid);
342         if (r != APP_ERROR_NONE)
343                 return r;
344
345         class_provider = app_ops->create(app_user_data);
346         if (class_provider == NULL) {
347                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__,
348                                         "widget_class is NULL");
349         }
350
351         return WIDGET_ERROR_NONE;
352 }
353
354 static void __after_loop()
355 {
356         if (app_ops->terminate)
357                 app_ops->terminate(app_user_data);
358
359         elm_shutdown();
360 }
361
362 EXPORT_API int widget_app_main(int argc, char **argv,
363                                 widget_app_lifecycle_callback_s *callback, void *user_data)
364 {
365         int r;
366
367         if (argc <= 0 || argv == NULL || callback == NULL)
368                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
369
370         if (callback->create == NULL)
371                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, "widget_app_create_cb() callback must be registered");
372
373         app_ops = callback;
374         app_user_data = user_data;
375         r = __before_loop(argc, argv);
376         if (r < 0)
377                 return r;
378
379         ecore_main_loop_begin();
380         aul_status_update(STATUS_DYING);
381         __after_loop();
382
383         return WIDGET_ERROR_NONE;
384 }
385
386 EXPORT_API int widget_app_exit(void)
387 {
388         ecore_main_loop_quit();
389
390         return WIDGET_ERROR_NONE;
391 }
392
393 static gboolean __finish_event_cb(gpointer user_data)
394 {
395         if (user_data == NULL)
396                 return FALSE;
397
398         widget_context_s *wc = (widget_context_s *)user_data;
399
400         switch (wc->state) {
401         case WC_READY:
402
403                 break;
404         case WC_RUNNING:
405
406                 break;
407         case WC_PAUSED:
408
409                 break;
410         default:
411                 break;
412         }
413
414
415         return FALSE;
416 }
417
418 EXPORT_API int widget_app_terminate_context(widget_context_h context)
419 {
420         if (context == NULL)
421                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
422
423         g_idle_add(__finish_event_cb, context);
424         return WIDGET_ERROR_NONE;
425 }
426
427 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
428 {
429         return WIDGET_ERROR_NONE;
430 }
431
432 EXPORT_API int widget_app_add_event_handler(app_event_handler_h *event_handler,
433                                         app_event_type_e event_type, app_event_cb callback,
434                                         void *user_data)
435 {
436         /* TODO */
437         return 0;
438 }
439
440 EXPORT_API int widget_app_remove_event_handler(app_event_handler_h
441                                                 event_handler)
442 {
443         /* TODO */
444         return 0;
445 }
446
447 EXPORT_API const char* widget_app_get_id(widget_context_h context)
448 {
449         return context->id;
450 }
451
452 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
453                                         Evas_Object **win)
454 {
455         if (context == NULL || win == NULL)
456                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
457
458         widget_context_s *cxt = (widget_context_s*)context;
459         Evas_Object *ret_win = NULL;
460
461         ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
462         if (ret_win == NULL) {
463                 _E("failed to create window");
464                 return WIDGET_ERROR_FAULT;
465         }
466         elm_win_title_set(ret_win, cxt->id);
467
468         *win = ret_win;
469         cxt->win = ret_win;
470
471         return WIDGET_ERROR_NONE;
472 }
473
474 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
475                 widget_instance_lifecycle_callback_s callback, void *user_data)
476 {
477         widget_class_s *wc = (widget_class_s*)malloc(sizeof(widget_class_s));
478
479         if (wc == NULL) {
480                 _E("failed to malloc : %s", __FUNCTION__);
481                 set_last_result(WIDGET_ERROR_OUT_OF_MEMORY);
482                 return NULL;
483         }
484
485         wc->classid = strdup(class_id);
486         wc->user_data = user_data;
487         wc->ops = callback;
488         wc->next = prev;
489         wc->prev = NULL;
490
491         set_last_result(WIDGET_ERROR_NONE);
492
493         if (prev)
494                 prev->prev = wc;
495
496         return wc;
497 }
498
499 EXPORT_API widget_class_h widget_app_class_add(widget_class_h widget_class, const char *class_id,
500                 widget_instance_lifecycle_callback_s callback, void *user_data)
501 {
502         if (class_id == NULL) {
503                 set_last_result(WIDGET_ERROR_INVALID_PARAMETER);
504                 return NULL;
505         }
506
507         return _widget_class_create(widget_class, class_id, callback, user_data);
508 }
509
510 EXPORT_API widget_class_h widget_app_class_create(widget_instance_lifecycle_callback_s callback, void *user_data)
511 {
512         return _widget_class_create(class_provider, appid, callback, user_data);
513 }
514
515 EXPORT_API int widget_app_context_set_tag(widget_context_h context, void *tag)
516 {
517         if (context == NULL)
518                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
519
520         context->tag = tag;
521
522         return WIDGET_ERROR_NONE;
523 }
524
525 EXPORT_API int widget_app_context_get_tag(widget_context_h context, void **tag)
526 {
527         if (context == NULL || tag == NULL)
528                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
529
530         *tag = context->tag;
531
532         return WIDGET_ERROR_NONE;
533 }
534
535 EXPORT_API int widget_app_context_set_content_info(widget_context_h context, bundle *content_info)
536 {
537         const char *class_id = NULL;
538         int ret = 0;
539         if (context == NULL || content_info == NULL)
540                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
541
542         if (context->provider == NULL)
543                 return widget_app_error(WIDGET_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);
544
545         class_id = context->provider->classid;
546
547         if (class_id == NULL)
548                 return widget_app_error(WIDGET_ERROR_FAULT, __FUNCTION__, NULL);
549
550         ret = __send_update_status(class_id, context->id, WIDGET_INSTANCE_EVENT_UPDATE, content_info, true);
551
552         if (ret < 0) {
553                 _E("failed to send content info: %s of %s (%d)", context->id, class_id, ret);
554                 return widget_app_error(WIDGET_ERROR_IO_ERROR, __FUNCTION__, NULL);
555         }
556
557         return WIDGET_ERROR_NONE;
558 }
559
560 EXPORT_API int widget_app_context_set_title(widget_context_h context, const char *title)
561 {
562         /* TODO
563          may use widget status update, or use surface title set.
564          */
565         return 0;
566 }
567