07a0573100231157d765793b68870cf99eeed454
[profile/ivi/ico-uxf-homescreen.git] / src / on_screen.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   onscreen application
11  *
12  * @date    Feb-15-2013
13  */
14
15 #include <unistd.h>
16 #include <Eina.h>
17 #include <Evas.h>
18 #include <Ecore.h>
19 #include <Ecore_Wayland.h>
20 #include <Ecore_Evas.h>
21 #include <Edje.h>
22
23 #include "ico_uxf.h"
24 #include "ico_uxf_conf.h"
25 #include "ico_uxf_conf_ecore.h"
26
27 #include "home_screen.h"
28 #include "home_screen_res.h"
29 #include "home_screen_conf.h"
30
31 #include <libwebsockets.h>
32
33 /*============================================================================*/
34 /* Define data types                                                          */
35 /*============================================================================*/
36 #define ICO_ONS_WS_TIMEOUT 0.1
37 #define ICO_ONS_WS_ADDRESS "127.0.0.1"
38 #define ICO_ONS_WS_PROTOCOL_NAME ICO_HS_PROTOCOL_OS
39 #define ICO_ONS_BUF_SIZE    (1024)
40 #define ICO_ONS_APPLI_NUM    (15)        /* only for applist */
41
42 #define ICO_ONS_VERTICAL    (1)
43 #define ICO_ONS_HORIZONTAL  (2)
44
45 #define ICO_ONS_CMD_WAIT    (1)
46 #define ICO_ONS_NO_WAIT     (2)
47
48 typedef struct _ons_msg ons_msg_t;
49 struct _ons_msg {
50     ons_msg_t *next;
51     char *data;
52     int len;
53 };
54
55 /*============================================================================*/
56 /* static(internal) functions prototype                                       */
57 /*============================================================================*/
58 static int ons_loadinons_edje_file(const char *edje_file);
59 static void ons_event_message(char *format, ...);
60 static ons_msg_t *ons_alloc_seendmsg(char *data, int len);
61 static ons_msg_t *ons_get_sendmsg(void);
62 static int ons_put_sendmsg(ons_msg_t *send);
63 static char *ons_edje_parse_str(void *in, int arg_num);
64 static int ons_callback_http(
65               struct libwebsocket_context *context, struct libwebsocket *wsi,
66               enum libwebsocket_callback_reasons reason, void *user, void *in,
67               size_t len);
68 static int ons_callback_onscreen(
69                   struct libwebsocket_context *context,
70                   struct libwebsocket *wsi,
71                   enum libwebsocket_callback_reasons reason, void *user,
72                   void *in, size_t len);
73 static void ons_create_context(void);
74 static Eina_Bool ons_ecore_event(void *data);
75 static void ons_on_destroy(Ecore_Evas *ee);
76 static void ons_touch_up_edje(void *data, Evas *evas,
77                               Evas_Object *obj, void *event_info);
78 static void ons_touch_up_next(void *data, Evas *evas, Evas_Object *obj,
79                               void *event_info);
80 static const char *ons_get_fname(const char *filepath);
81 static int ons_get_appindex(int idx);
82 static void ons_set_appicon(Evas *evas, Evas_Object *edje, Evas_Object* part,
83                             const char *partname);
84 static void ons_load_config(void);
85 static void ons_config_event(const char *appid, int type);
86
87 /*============================================================================*/
88 /* variabe & table                                                            */
89 /*============================================================================*/
90 static int ons_ws_port = ICO_HS_WS_PORT;
91 static int ons_ws_connected = 0;
92 static struct libwebsocket_context *ons_ws_context = NULL;
93 static struct libwebsocket *ons_wsi_mirror = NULL;
94 static char ons_edje_str[ICO_ONS_BUF_SIZE];
95
96 static Ecore_Evas *ons_window; /* ecore-evas object */
97 static Evas *ons_evas = NULL; /* evas object */
98 static Evas_Object *ons_edje = NULL; /* loaded edje objects */
99 static Eina_List *ons_img_list = NULL;
100 static int ons_width, ons_height;
101 static int ons_applist_idx = 0; /* only for applist, it's index */
102 static int ons_app_cnt = 0; /* only for applist. a number of app to listed */
103
104 static ons_msg_t *ons_free_msg = NULL;
105 static ons_msg_t *ons_send_msg = NULL;
106
107 static int ons_command_wait = ICO_ONS_NO_WAIT;
108
109 static struct libwebsocket_protocols ws_protocols[] = {
110     {
111         "http-only",
112         ons_callback_http,
113         0
114     },
115     {
116         "onscreen-protocol",
117         ons_callback_onscreen,
118         0,
119     },
120     {
121         /* end of list */
122         NULL,
123         NULL,
124         0
125     }
126 };
127
128 /*============================================================================*/
129 /* functions                                                                  */
130 /*============================================================================*/
131 /*--------------------------------------------------------------------------*/
132 /**
133  * @brief   ons_event_message
134  *          send message
135  *
136  * @param[in]   wsi                 libwebsockets management table to send
137  * @param[in]   fromat              message to send
138  * @return      none
139  */
140 /*--------------------------------------------------------------------------*/
141 static void
142 ons_event_message(char *format, ...)
143 {
144     va_list list;
145     char message[ICO_HS_TEMP_BUF_SIZE];
146     ons_msg_t *send;
147
148     va_start(list, format);
149     vsnprintf(message, sizeof(message), format, list);
150     va_end(list);
151
152     uifw_trace("OnScreen: ons_event_message %s", message);
153
154     send = ons_alloc_seendmsg(message, strlen(message));
155     if (!send) {
156         uifw_warn("ons_event_message: ERROR(allocate send msg)");
157     }
158     else {
159         ons_put_sendmsg(send);
160     }
161
162     return;
163 }
164
165 /*--------------------------------------------------------------------------*/
166 /**
167  * @brief   ons_get_sendmsg
168  *          get the send message from the send buffer.
169  *
170  * @param       none
171  * @return      send buffer address
172  * @retval      > 0                 success
173  * @retval      NULL                error
174  */
175 /*--------------------------------------------------------------------------*/
176 static ons_msg_t *
177 ons_get_sendmsg(void)
178 {
179     ons_msg_t *msg;
180
181     msg = ons_send_msg;
182     if (msg) {
183         ons_send_msg = msg->next;
184         msg->next = NULL;
185     }
186
187     return msg;
188 }
189
190 /*--------------------------------------------------------------------------*/
191 /**
192  * @brief   ons_put_sendmsg
193  *          put the send message to the send queue end.
194  *
195  * @param[in]   data                send message
196  * @return      result
197  * @retval      ICO_HS_OK           success
198  * @retval      ICO_HS_ERR          error
199  */
200 /*--------------------------------------------------------------------------*/
201 static int
202 ons_put_sendmsg(ons_msg_t *send)
203 {
204     ons_msg_t *msg;
205
206     uifw_trace("ons_put_sendmsg: Enter");
207
208     msg = ons_send_msg;
209     while (msg) {
210         if (!msg->next) {
211             break;
212         }
213         msg = msg->next;
214     }
215     if (!msg) {
216         ons_send_msg = send;
217     }
218     else {
219         msg->next = send;
220     }
221
222     if (ons_ws_context && ons_wsi_mirror) {
223         uifw_trace("ons_put_sendmsg: libwebsocket_callback_on_writable"
224             "(wsi_ctx=%x, wsi=%x", ons_ws_context, ons_wsi_mirror);
225         libwebsocket_callback_on_writable(ons_ws_context, ons_wsi_mirror);
226     }
227     else {
228         uifw_trace("ons_put_sendmsg: Leave(do not have wsi)");
229         return ICO_HS_ERR;
230     }
231
232     uifw_trace("ons_put_sendmsg: Leave");
233
234     return ICO_HS_OK;
235 }
236
237 /*--------------------------------------------------------------------------*/
238 /**
239  * @brief   ons_alloc_seendmsg
240  *          Allocate a send message buffer.
241  *
242  * @param[in]   data                data
243  * @param[in]   len                 data length
244  * @return      address
245  * @retval      > 0                 success
246  * @retval      NULL                error
247  */
248 /*--------------------------------------------------------------------------*/
249 static ons_msg_t *
250 ons_alloc_seendmsg(char *data, int len)
251 {
252     ons_msg_t *msg;
253
254     if (!ons_free_msg) {
255         msg = malloc(sizeof(ons_msg_t));
256         if (!msg) {
257             return NULL;
258         }
259     }
260     else {
261         msg = ons_free_msg;
262         ons_free_msg = ons_free_msg->next;
263     }
264     memset(msg, 0, sizeof(ons_msg_t));
265
266     msg->len = len;
267     msg->data = strdup(data);
268     if (!msg->data) {
269         free(msg);
270         return NULL;
271     }
272
273     return msg;
274 }
275
276 static char *
277 ons_edje_parse_str(void *in, int arg_num)
278 {
279     int i;
280     char *data;
281
282     uifw_trace("ons_edje_parse_str %s, arg = %d", in, arg_num);
283     data = strtok(in, " ");
284     /* arg_num : 0 to n */
285     for (i = 0; i < arg_num; i++) {
286         data = strtok(NULL, " ");
287     }
288     uifw_trace("ons_edje_parse_str data: %s", data);
289     return data;
290 }
291
292 /*--------------------------------------------------------------------------*/
293 /*
294  * @brief   ons_callback_http
295  *          Connection status is notified from libwebsockets.
296  *
297  * @param[in]   context             libwebsockets context
298  * @param[in]   wsi                 libwebsockets management table
299  * @param[in]   reason              event type
300  * @param[in]   user                intact
301  * @param[in]   in                  receive message
302  * @param[in]   len                 message size[BYTE]
303  * @return      result
304  * @retval      =0                  success
305  * @retval      =1                  error
306  */
307 /*--------------------------------------------------------------------------*/
308 static int
309 ons_callback_http(struct libwebsocket_context *context, struct libwebsocket *wsi,
310               enum libwebsocket_callback_reasons reason, void *user, void *in,
311               size_t len)
312 {
313     uifw_trace("ons_callback_http %p", context);
314     uifw_trace("OS-REASON %d", reason);
315     return 0;
316 }
317
318 /*--------------------------------------------------------------------------*/
319 /*
320  * @brief   ons_callback_onscreen
321  *          this callback function is notified from libwebsockets
322  *          onscreen protocol
323  *
324  * @param[in]   context             libwebsockets context
325  * @param[in]   wsi                 libwebsockets management table
326  * @param[in]   reason              event type
327  * @param[in]   user                intact
328  * @param[in]   in                  receive message
329  * @param[in]   len                 message size[BYTE]
330  * @return      result
331  * @retval      =0                  success
332  * @retval      =1                  error
333  */
334 /*--------------------------------------------------------------------------*/
335 static int
336 ons_callback_onscreen(struct libwebsocket_context *context,
337                   struct libwebsocket *wsi,
338                   enum libwebsocket_callback_reasons reason, void *user,
339                   void *in, size_t len)
340 {
341     int n = 0;
342     unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512
343             + LWS_SEND_BUFFER_POST_PADDING];
344     unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
345     ons_msg_t *msg;
346
347     uifw_trace("ons_callback_onscreen %p", context);
348
349     switch (reason) {
350     case LWS_CALLBACK_CLIENT_ESTABLISHED:
351         uifw_trace("OS-ESTABLISHED %x", wsi);
352         ons_wsi_mirror = wsi;
353         ons_event_message("ANS HELLO");
354         break;
355
356     case LWS_CALLBACK_CLIENT_RECEIVE:
357         uifw_trace("OS-RECEIVE[%d] %s", len, in);
358
359         if (strlen(in) == 0)
360             break;
361         ons_command_wait = ICO_ONS_NO_WAIT;
362         /* onscreen activate request */
363         if (strncmp("OPEN", in, 4) == 0) {
364             uifw_trace("%s", in);
365             strncpy(ons_edje_str, ons_edje_parse_str(in, 1), sizeof(ons_edje_str));
366             uifw_trace("ons_loadinons_edje_file: %s", &ons_edje_str[0]);
367             if (ons_loadinons_edje_file(&ons_edje_str[0]) == 0) {
368                 ons_event_message("RESULT SUCCESS");
369             }
370             else {
371                 ons_event_message("RESULT FAILED");
372             }
373         }
374         break;
375
376     case LWS_CALLBACK_CLOSED:
377         uifw_trace("OS-CLOSE");
378         ons_wsi_mirror = NULL;
379         break;
380
381     case LWS_CALLBACK_CLIENT_WRITEABLE:
382         uifw_trace("LWS_CALLBACK_CLIENT_WRITEABLE:(wsi=%x)", wsi);
383
384         msg = ons_get_sendmsg();
385         if (msg) {
386             strcpy((char *)p, msg->data);
387             n = libwebsocket_write(wsi, p, strlen((char *)p), LWS_WRITE_TEXT);
388             if (n < 0) {
389                 uifw_warn("ons_callback_onscreen: ERROR(fail to write ws)");
390             }
391         }
392         if (ons_send_msg) {
393             libwebsocket_callback_on_writable(context, wsi);
394         }
395
396         usleep(200);
397         break;
398
399     default:
400         uifw_trace("OS-REASON %d", reason);
401         break;
402     }
403
404     uifw_trace("ons_callback_onscreen: Leave");
405
406     return 0;
407 }
408
409 /*--------------------------------------------------------------------------*/
410 /**
411  * @brief   ons_create_context
412  *          connect to the homescreen using websocket.
413  *
414  * @param[in]   none
415  * @return      none
416  */
417 /*--------------------------------------------------------------------------*/
418 static void
419 ons_create_context(void)
420 {
421     ons_ws_context
422             = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
423                                           ws_protocols,
424                                           libwebsocket_internal_extensions,
425                                           NULL, NULL, -1, -1, 0);
426     uifw_trace("OnScreen: ons_create_context ctx = %p", ons_ws_context);
427
428     ons_ws_connected = 0;
429     if (ons_ws_context == NULL) {
430         uifw_trace("OnScreen: libwebsocket_create_context failed.");
431     }
432     else {
433         ons_wsi_mirror = libwebsocket_client_connect(ons_ws_context,
434                                                  ICO_ONS_WS_ADDRESS, ons_ws_port, 0, "/",
435                                                  ICO_ONS_WS_ADDRESS, NULL,
436                                                  ICO_ONS_WS_PROTOCOL_NAME, -1);
437         uifw_trace("OnScreen: ons_create_context wsi = %p", ons_wsi_mirror);
438         if (ons_wsi_mirror != NULL) {
439             ons_ws_connected = 1;
440         }
441     }
442 }
443
444 /*--------------------------------------------------------------------------*/
445 /**
446  * @brief   ons_ecore_event
447  *          timer handler called by Ecore.
448  *
449  * @param[in]   data                user data
450  * @return      call back setting
451  * @retval      ECORE_CALLBACK_RENEW    set callback
452  */
453 /*--------------------------------------------------------------------------*/
454 static Eina_Bool
455 ons_ecore_event(void *data)
456 {
457     if (ons_ws_connected) {
458         libwebsocket_service(ons_ws_context, 0);
459     }
460     else {
461         if (ons_ws_context != NULL) {
462             libwebsocket_context_destroy(ons_ws_context);
463         }
464         ons_ws_context
465                 = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
466                                               ws_protocols,
467                                               libwebsocket_internal_extensions,
468                                               NULL, NULL, -1, -1, 0);
469         if (ons_ws_context == NULL) {
470             uifw_trace("OnScreen: libwebsocket_create_context failed.");
471         }
472         else {
473             ons_wsi_mirror = libwebsocket_client_connect(ons_ws_context,
474                                                      ICO_ONS_WS_ADDRESS, ons_ws_port, 0,
475                                                      "/", ICO_ONS_WS_ADDRESS, NULL,
476                                                      ICO_ONS_WS_PROTOCOL_NAME, -1);
477             if (ons_wsi_mirror != NULL) {
478                 ons_ws_connected = 1;
479             }
480         }
481     }
482
483     return ECORE_CALLBACK_RENEW;
484 }
485
486 /*--------------------------------------------------------------------------*/
487 /*
488  * @brief   ons_on_destroy
489  *          callback function called by EFL when ecore destroyed.
490  *          exit ecore main loop.
491  *
492  * @param[in]   ee                  ecore evas object
493  * @return      none
494  */
495 /*--------------------------------------------------------------------------*/
496 static void
497 ons_on_destroy(Ecore_Evas *ee)
498 {
499     uifw_trace("ons_on_destroy: Enter");
500
501     ecore_main_loop_quit();
502     libwebsocket_context_destroy(ons_ws_context);
503     edje_shutdown();
504 }
505
506 static void
507 ons_touch_up_edje(void *data, Evas *evas, Evas_Object *obj, void *event_info)
508 {
509     if (ons_command_wait == ICO_ONS_CMD_WAIT) return;
510     ons_command_wait = ICO_ONS_CMD_WAIT;
511
512     /* get name from userdata */
513     if (data != NULL) {
514         uifw_trace("OnScreen: user data is %s", (const char *)data);
515     }
516     else {
517         uifw_trace("OnScreen: user data is NULL");
518     }
519     ons_event_message("TOUCH %s %s", ons_edje_str, data);
520
521     /* operation sound */
522     hs_snd_play(hs_snd_get_filename(ICO_HS_SND_TYPE_DEFAULT));
523 }
524
525 /*--------------------------------------------------------------------------*/
526 /**
527  * @brief   ons_touch_up_next
528  *          processing when next button touch up.
529  *
530  * @param[in]   data                user data
531  * @param[in]   obj                 evas object of the button
532  * @param[in]   event_info          evas event infomation
533  * @return      none
534  */
535 /*--------------------------------------------------------------------------*/
536 static void
537 ons_touch_up_next(void *data, Evas *evas, Evas_Object *obj, void *event_info)
538 {
539     int listcnt;
540
541     if (ons_command_wait == ICO_ONS_CMD_WAIT) return;
542     ons_command_wait = ICO_ONS_CMD_WAIT;
543
544     if (ons_app_cnt > 0) {
545         listcnt = ((ons_app_cnt - 1) / ICO_ONS_APPLI_NUM) + 1;
546     }
547     else {
548         listcnt = 1;
549     }
550     ons_applist_idx += 1;
551     if (ons_applist_idx >= listcnt) {
552         ons_applist_idx = 0;
553     }
554
555     /* get name from userdata */
556     if (data != NULL) {
557         uifw_trace("OnScreen: user data is %s", (const char *)data);
558     }
559     else {
560         uifw_trace("OnScreen: user data is NULL");
561     }
562     ons_event_message("TOUCH %s %s", ons_edje_str, data);
563
564     /* operation sound */
565     hs_snd_play(hs_snd_get_filename(ICO_HS_SND_TYPE_DEFAULT));
566 }
567
568 /*--------------------------------------------------------------------------*/
569 /**
570  * @brief   ons_get_fname
571  *          get filename from the full path
572  *
573  * @param[in]   filepath            file path
574  * @return      filename
575  */
576 /*--------------------------------------------------------------------------*/
577 static const char *
578 ons_get_fname(const char *filepath)
579 {
580     int ii;
581     const char *name = filepath;
582
583     for (ii = 0; ii < ICO_ONS_BUF_SIZE - 1; ii++) {
584         if (filepath[ii] == 0)
585             break;
586         if (filepath[ii] == '/')
587             name = &filepath[ii + 1];
588     }
589
590     return name;
591 }
592
593 /*--------------------------------------------------------------------------*/
594 /**
595  * @brief   ons_get_appindex
596  *          return a application index that to be set indicated index.
597  *
598  * @param[in]   filepath            file path
599  * @return      filename
600  */
601 /*--------------------------------------------------------------------------*/
602 static int
603 ons_get_appindex(int idx)
604 {
605     int ii;
606     int appidx = idx + ons_applist_idx * ICO_ONS_APPLI_NUM + 1;
607     int cnt = 0;
608     Ico_Uxf_App_Config *appconf = (Ico_Uxf_App_Config *)ico_uxf_getAppConfig();
609
610     uifw_trace("ons_get_appindex: idx=%d appidx=%d appcnt=%d", idx, appidx, ons_app_cnt);
611
612     for (ii = 0; ii < appconf->applicationNum; ii++) {
613         if ((! appconf->application[ii].noicon) &&
614             (strcmp(appconf->application[ii].type, ICO_HS_GROUP_SPECIAL) != 0)) {
615             cnt++;
616         }
617         if (cnt == appidx) {
618             return ii;
619         }
620     }
621
622     return -1;
623 }
624
625 /* set App Icon on rect */
626 static void
627 ons_set_appicon(Evas *evas, Evas_Object *edje, Evas_Object* part,
628                 const char *partname)
629 {
630     int x, y, w, h;
631     int idx; /* rect index */
632     int appidx; /* appli index */
633     char imgfile[ICO_ONS_BUF_SIZE];
634     Ico_Uxf_App_Config *appconf;
635     Evas_Object *img = NULL;
636
637     memset(imgfile, 0, sizeof(imgfile));
638     /* check name, if part is rect/next_bt/cancel_bt */
639     if (strncmp(partname, ICO_HS_ONS_PART_RECT_NAME,
640                 sizeof(ICO_HS_ONS_PART_RECT_NAME) - 1) == 0) {
641         img = evas_object_image_filled_add(ons_evas);
642         /* get rect index from partname(rect_01, rect_02, ...) */
643         sscanf(partname, ICO_HS_ONS_PART_RECT_NAME"%d", &idx);
644         idx -= 1;
645
646         appconf = (Ico_Uxf_App_Config *)ico_uxf_getAppConfig();
647
648         appidx = ons_get_appindex(idx);
649         uifw_trace("ons_set_appicon: idx=%d appidx=%d", idx, appidx);
650         if ((appidx < 0) || (appidx > appconf->applicationNum)) {
651             evas_object_del(img);
652             return;
653         }
654
655         /* set icon file name */
656         snprintf(imgfile, ICO_ONS_BUF_SIZE, "%s",
657                  appconf->application[appidx].icon_key_name);
658         uifw_trace("ons_set_appicon: set image = %s(%d/%d), app=%s, group=%s",
659                    imgfile, appidx, ons_app_cnt,
660                    appconf->application[appidx].appid,
661                    appconf->application[appidx].group);
662         /* set mouse call back function */
663         evas_object_event_callback_add(img, EVAS_CALLBACK_MOUSE_UP,
664                                        ons_touch_up_edje,
665                                        appconf->application[appidx].appid);
666     }
667     else if (strcmp(partname, ICO_HS_ONS_PART_NEXT_NAME) == 0) {
668         evas_object_event_callback_add(part, EVAS_CALLBACK_MOUSE_UP,
669                                        ons_touch_up_next, partname);
670     }
671     else if (strcmp(partname, ICO_HS_ONS_PART_CANCEL_NAME) == 0) {
672         evas_object_event_callback_add(part, EVAS_CALLBACK_MOUSE_UP,
673                                        ons_touch_up_edje, partname);
674     }
675     else {
676         return;
677     }
678
679     if (strlen(imgfile) > 0) {
680         /* calculation icon pos */
681         edje_object_part_geometry_get(ons_edje, partname, &x, &y, &w, &h);
682         x += (ons_width - ICO_HS_SIZE_LAYOUT_WIDTH) / 2;
683         y += (ons_height - ICO_HS_SIZE_LAYOUT_HEIGHT) / 2;
684
685         evas_object_image_file_set(img, imgfile, NULL);
686         evas_object_move(img, x, y);
687         evas_object_resize(img, w, h);
688         evas_object_show(img);
689     }
690
691     ons_img_list = eina_list_append(ons_img_list, img);
692 }
693
694 static int
695 ons_loadinons_edje_file(const char *edje_file)
696 {
697     Evas_Object *part; /* part handle */
698     Eina_List *group; /* edje group list */
699     Eina_List *list; /* part list in edje */
700     int group_count = 0; /* group counter */
701     int name_count = 0; /* name counter */
702     int moveX, moveY; /* move list to center */
703     Eina_List *l, *l_next;
704     Evas_Object *data;
705     Evas_Object *canvas;
706
707     if (!ons_evas) {
708         ons_evas = ecore_evas_get(ons_window);
709         if (!ons_evas) {
710             uifw_trace("OnScreen: could not create evas.");
711             return -1;
712         }
713         /* set color */
714         canvas = evas_object_rectangle_add(ons_evas);
715         evas_object_color_set(canvas, 0, 0, 0, 191);
716         evas_object_move(canvas, 0, 0);
717         evas_object_resize(canvas, ons_width, ons_height);
718         evas_object_show(canvas);
719     }
720
721     /* delete pre image */
722     uifw_trace("OnScreen: img list is %08x", ons_img_list);
723     EINA_LIST_FOREACH_SAFE(ons_img_list, l, l_next, data) {
724         uifw_trace("OnScreen: del data is %08x", data);
725         evas_object_del(data);
726         ons_img_list = eina_list_remove_list(ons_img_list, l);
727     }
728
729     /* delete edje */
730     if (ons_edje) {
731         evas_object_del(ons_edje);
732     }
733
734     /* create and add object in canvas from edje */
735     ons_edje = edje_object_add(ons_evas);
736     if (!ons_edje) {
737         uifw_trace("OnScreen: could not create edje object!");
738         return -1;
739     }
740
741     uifw_trace("OnScreen: w=%d h=%d", ons_width, ons_height);
742
743     /* calc move */
744     moveX = (ons_width - ICO_HS_SIZE_LAYOUT_WIDTH) / 2;
745     moveY = (ons_height - ICO_HS_SIZE_LAYOUT_HEIGHT) / 2;
746
747     /* Put in the image */
748     evas_object_move(ons_edje, moveX, moveY);
749     /* Resize the image */
750     evas_object_resize(ons_edje, ons_width, ons_height);
751     /* Show the image */
752     evas_object_show(ons_edje);
753
754     /* get group list */
755     group = edje_file_collection_list(edje_file);
756     while (group != NULL) {
757         /* Set the edj file */
758         if (!edje_object_file_set(ons_edje, edje_file, (const char *)group->data)) {
759             int err = edje_object_load_error_get(ons_edje);
760             const char *errmsg = edje_load_error_str(err);
761             uifw_trace("OnScreen: could not load %s: %s", edje_file, errmsg);
762
763             edje_file_collection_list_free(group);
764             evas_object_del(ons_edje);
765             return -1;
766         }
767         uifw_trace("OnScreen: group[%d] data : %s", group_count,
768                    (const char *)group->data);
769
770         /* get list */
771         list = edje_object_access_part_list_get(ons_edje);
772         while (list != NULL) {
773             uifw_trace("OnScreen: list[%d] data : %s", name_count,
774                        (const char *)list->data);
775
776             /* set callback for part name */
777             part = (Evas_Object *)edje_object_part_object_get(ons_edje,
778                                                               (const char *)list->data);
779             if (part != NULL) {
780                 uifw_trace("OnScreen: list[%d] name : %s", name_count,
781                            (const char *)list->data);
782
783                 /* if not applist */
784                 if (strncmp(ons_get_fname(edje_file), ICO_HS_ONS_APPLI_LIST_NAME,
785                             sizeof(ICO_HS_ONS_APPLI_LIST_NAME) - 1) != 0) {
786                     evas_object_event_callback_add(part,
787                                                    EVAS_CALLBACK_MOUSE_UP,
788                                                    ons_touch_up_edje, list->data);
789                 }
790                 /* if applist */
791                 else {
792                     ons_set_appicon(ons_evas, ons_edje, part, (const char *)list->data);
793                 }
794             }
795             else {
796                 uifw_trace("OnScreen: list[%d] is NULL", name_count);
797             }
798             /* to next list */
799             list = list->next;
800             name_count++;
801         }
802         /* to next group */
803         group = group->next;
804         group_count++;
805     }
806     uifw_trace("OnScreen: group num is %d", group_count);
807     uifw_trace("OnScreen: name num is %d", name_count);
808
809     return 0;
810 }
811
812 /*--------------------------------------------------------------------------*/
813 /**
814  * @brief   ons_load_config
815  *          load/reload configuration.
816  *
817  * @param       none
818  * @return      none
819  */
820 /*--------------------------------------------------------------------------*/
821 static void
822 ons_load_config(void)
823 {
824     Ico_Uxf_App_Config *appconf;
825     int appcnt;
826     int appidx, idx, cnt;
827
828     uifw_trace("ons_load_config: Enter");
829
830     appconf = (Ico_Uxf_App_Config *)ico_uxf_getAppConfig();
831     appcnt = appconf->applicationNum;
832     for (appidx = 0; appidx < appconf->applicationNum; appidx++) {
833         if ((appconf->application[appidx].noicon) ||
834             (strcmp(appconf->application[appidx].type, ICO_HS_GROUP_SPECIAL) == 0)) {
835             appcnt--;
836             uifw_trace("ons_load_config: No Need appid=%s noicon=%d type=%s",
837                        appconf->application[appidx].appid,
838                        appconf->application[appidx].noicon,
839                        appconf->application[appidx].type);
840         }
841     }
842     cnt = 0;
843     for (idx = 0; idx < appcnt; idx++) {
844         appidx = ons_get_appindex(idx);
845         if (appidx > 0) {
846             uifw_trace("ons_load_config: appid=%s seat=%d idx=%d seatcnt=%d",
847                        appconf->application[appidx].appid, cnt
848                                / ICO_ONS_APPLI_NUM, idx - ICO_ONS_APPLI_NUM
849                                * (cnt / ICO_ONS_APPLI_NUM), ((appcnt - 1)
850                                / ICO_ONS_APPLI_NUM) + 1);
851             cnt++;
852         }
853     }
854
855     ons_app_cnt = appcnt;
856     ons_applist_idx = 0;
857
858     uifw_trace("ons_load_config: Leave(appcnt=%d)", appcnt);
859
860     return;
861 }
862
863 /*--------------------------------------------------------------------------*/
864 /**
865  * @brief   ons_config_event
866  *          This is a callback function called when the configurations
867  *          is updata.
868  *
869  * @param[in]   appid               application id
870  * @param[in]   type                event type(install/uninstall)
871  * @return      none
872  */
873 /*--------------------------------------------------------------------------*/
874 static void
875 ons_config_event(const char *appid, int type)
876 {
877     uifw_trace("ons_config_event: Enter(appid=%s, type=%d)", appid, type);
878
879     ons_load_config();
880
881     uifw_trace("ons_config_event: Leave");
882
883     return;
884 }
885
886 /*--------------------------------------------------------------------------*/
887 /*
888  * @brief   onscreen application
889  *          main routine
890  *
891  * @param   main() finction's standard parameter (argc,argv)
892  * @return  result
893  * @retval  0       success
894  * @retval  1       failed
895  */
896 /*--------------------------------------------------------------------------*/
897 int
898 main(int argc, char *argv[])
899 {
900     int width, height;
901     int orientation = ICO_ONS_HORIZONTAL;
902     int ii;
903     int ret;
904
905     /* configure */
906     if (getenv("PKG_NAME")) {
907         ico_uxf_log_open(getenv("PKG_NAME"));
908     }
909     else {
910         ico_uxf_log_open(ICO_HS_APPID_DEFAULT_ONS);
911     }
912
913     /* get argment */
914     for (ii = 1; ii < argc; ii++) {
915         if (argv[ii][0] != '-')
916             continue;
917         if (strncasecmp(argv[ii], "-orientaion=", strlen("-orientaion=")) == 0) {
918             if (strcmp(&argv[ii][strlen("-orientaion=")], "vertical") == 0) {
919                 orientation = ICO_ONS_VERTICAL;
920             }
921             else if (strcmp(&argv[ii][strlen("-orientaion=")], "horizontal")
922                     == 0) {
923                 orientation = ICO_ONS_HORIZONTAL;
924             }
925         }
926     }
927
928     /* load configuration */
929     ret = initHomeScreenConfig(ICO_ONSCREEN_CONFIG_FILE);
930     if (ret == ICO_HS_OK) {
931         ons_ws_port = hs_conf_get_integer(ICO_HS_CONFIG_ONSCREEN,
932                                          ICO_HS_CONFIG_WS_PORT,
933                                          ICO_HS_WS_PORT);
934         orientation = hs_conf_get_integer(ICO_HS_CONFIG_ONSCREEN,
935                                           ICO_HS_CONFIG_ORIENTAION,
936                                           orientation);
937     }
938     ons_load_config();
939     hs_snd_init();
940
941     /* Reset a ecore_evas */
942     ecore_evas_init();
943
944     /* Initialize a edje */
945     edje_init();
946
947     /* Make a new ecore_evas */
948     ons_window = ecore_evas_new(NULL, 0, 0, 1, 1, "frame=0");
949
950     /* if not for a window, return NULL */
951     if (!ons_window) {
952         EINA_LOG_CRIT("OnScreen: could not create ons_window.");
953         return -1;
954     }
955     ecore_evas_callback_destroy_set(ons_window, ons_on_destroy);
956
957     /* resize window */
958     ecore_main_loop_iterate();
959     ecore_wl_screen_size_get(&width, &height);
960     if (orientation == ICO_ONS_VERTICAL) {
961         ons_width = width > height ? height : width;
962         ons_height = width > height ? width : height;
963     }
964     else {
965         ons_width = width < height ? height : width;
966         ons_height = width < height ? width : height;
967     }
968     ecore_evas_resize(ons_window, ons_width, ons_height);
969
970     /* Show the window */
971     /* evas_output_framespace_set(ecore_evas_get(ons_window), 0, 0, 0, 0);  */
972     ecore_evas_alpha_set(ons_window, EINA_TRUE);
973     ecore_evas_show(ons_window);
974
975     /* Init websockets */
976     ons_create_context();
977     ecore_timer_add(ICO_ONS_WS_TIMEOUT, ons_ecore_event, NULL);
978
979     /* add callback to app configuration */
980     ico_uxf_econf_setAppUpdateCb(ons_config_event);
981
982     /* Start main loop */
983     ecore_main_loop_begin();
984
985     /* end the ecore_evas */
986     ecore_evas_shutdown();
987
988     return 0;
989 }