3d33909d9efb7396f443efb4592453e9af7964ce
[profile/ivi/ico-uxf-homescreen.git] / lib / apps-framework / ico_syc_winctl.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   Window Control API
11  *          for privilege applications
12  *
13  * @date    Feb-21-2014
14  */
15
16 #include <string.h>
17
18 #include "ico_syc_msg_cmd_def.h"
19 #include "ico_syc_msg.h"
20 #include "ico_syc_private.h"
21 #include "ico_syc_privilege.h"
22
23 /*============================================================================*/
24 /* definition                                                                 */
25 /*============================================================================*/
26 #define _CMD_SHOW_WIN           0
27 #define _CMD_HIDE_WIN           1
28 #define _CMD_SHOW_LAYER         10
29 #define _CMD_HIDE_LAYER         11
30 #define _CMD_SET_LAYER_ATTR     12
31
32 /*============================================================================*/
33 /* define static function prototype                                           */
34 /*============================================================================*/
35 static msg_t _create_win_msg(const char *appid, int surface,
36                              const ico_syc_animation_t *animation,
37                              int type);
38 static msg_t _create_win_move_msg(const char *appid, int surface,
39                                   const ico_syc_win_move_t *move,
40                                   const ico_syc_animation_t *animation);
41 static msg_t _create_active_win_msg(const char *appid, int surface);
42 static msg_t _create_change_layer_msg(const char *appid, int surface, int layer);
43 static msg_t _create_map_get_msg(const char *appid, int surface, const char *filepath);
44 static msg_t _create_map_thumb_msg(const char *appid, int surface, int framerate,
45                                    const char *filepath);
46 static msg_t _create_unmap_thumb_msg(const char *appid, int surface);
47 static msg_t _create_layer_msg(const char *appid, int layer, int type);
48
49 /*============================================================================*/
50 /* static function                                                            */
51 /*============================================================================*/
52 /*--------------------------------------------------------------------------*/
53 /**
54  * @brief   _create_win_msg
55  *          Create the message to show/hide the application window.
56  *
57  * @param[in]   appid                   application id
58  * @param[in]   surface                 window's surface id
59  * @param[in]   animation               animation information
60  * @param[in]   type                    type of command
61  * @return      json generator
62  * @retval      json generator          success
63  * @retval      NULL                    error
64  */
65 /*--------------------------------------------------------------------------*/
66 static msg_t
67 _create_win_msg(const char *appid, int surface,
68                 const ico_syc_animation_t *animation, int type)
69 {
70     JsonObject *obj     = NULL;
71     JsonObject *argobj  = NULL;
72     JsonGenerator *gen  = NULL;
73     JsonNode *root      = NULL;
74     int cmd             = -1;
75
76     /* create json object */
77     obj = json_object_new();
78     argobj = json_object_new();
79     if (obj == NULL || argobj == NULL) {
80         _ERR("json_object_new failed");
81         return NULL;
82     }
83
84     /* set command */
85     if (type == _CMD_SHOW_WIN) {
86         cmd = MSG_CMD_SHOW;
87     }
88     else if (type == _CMD_HIDE_WIN) {
89         cmd = MSG_CMD_HIDE;
90     }
91
92     /* set message */
93     json_object_set_int_member(obj, MSG_PRMKEY_CMD, cmd);
94     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
95     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
96
97     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
98     /* set animation info */
99     if (animation != NULL) {
100         json_object_set_string_member(argobj, MSG_PRMKEY_ANIM_NAME,
101                                       animation->name);
102         json_object_set_int_member(argobj, MSG_PRMKEY_ANIM_TIME,
103                                    animation->time);
104     }
105     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
106
107     /* create root object */
108     root = json_node_new(JSON_NODE_OBJECT);
109     json_node_take_object(root, obj);
110
111     /* create generator object */
112     gen = json_generator_new();
113     json_generator_set_root(gen, root);
114
115     return gen;
116 }
117
118 /*--------------------------------------------------------------------------*/
119 /**
120  * @brief   _create_win_move_msg
121  *          Create the message to move the application window.
122  *
123  * @param[in]   appid                   application id
124  * @param[in]   surface                 window's surface id
125  * @param[in]   move                    move information (zone/position/size)
126  * @param[in]   animation               animation information
127  * @return      json generator
128  * @retval      json generator          success
129  * @retval      NULL                    error
130  */
131 /*--------------------------------------------------------------------------*/
132 static msg_t
133 _create_win_move_msg(const char *appid, int surface,
134                      const ico_syc_win_move_t *move,
135                      const ico_syc_animation_t *animation)
136 {
137     JsonObject *obj     = NULL;
138     JsonObject *argobj  = NULL;
139     JsonGenerator *gen  = NULL;
140     JsonNode *root      = NULL;
141
142     /* create json object */
143     obj = json_object_new();
144     argobj = json_object_new();
145     if (obj == NULL || argobj == NULL) {
146         _ERR("json_object_new failed");
147         return NULL;
148     }
149
150     /* set message */
151     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_MOVE);
152     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
153     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
154
155     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
156     /* set move info */
157     if (move->zone != NULL) {
158         json_object_set_string_member(argobj, MSG_PRMKEY_ZONE, move->zone);
159         json_object_set_int_member(argobj, MSG_PRMKEY_LAYER, move->layer);
160     }
161     else {
162         json_object_set_int_member(argobj, MSG_PRMKEY_POS_X, move->pos_x);
163         json_object_set_int_member(argobj, MSG_PRMKEY_POS_Y, move->pos_y);
164         json_object_set_int_member(argobj, MSG_PRMKEY_LAYER, -1);
165     }
166     json_object_set_int_member(argobj, MSG_PRMKEY_WIDTH, move->width);
167     json_object_set_int_member(argobj, MSG_PRMKEY_HEIGHT, move->height);
168     /* set animation info */
169     if (animation != NULL) {
170         json_object_set_string_member(argobj, MSG_PRMKEY_ANIM_NAME,
171                                       animation->name);
172         json_object_set_int_member(argobj, MSG_PRMKEY_ANIM_TIME,
173                                    animation->time);
174     }
175     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
176
177     /* create root object */
178     root = json_node_new(JSON_NODE_OBJECT);
179     json_node_take_object(root, obj);
180
181     /* create generator object */
182     gen = json_generator_new();
183     json_generator_set_root(gen, root);
184
185     return gen;
186 }
187
188 /*--------------------------------------------------------------------------*/
189 /**
190  * @brief   _create_active_win_msg
191  *          Create the message to change active window.
192  *
193  * @param[in]   appid                   application id
194  * @param[in]   surface                 window's surface id
195  * @return      json generator
196  * @retval      json generator          success
197  * @retval      NULL                    error
198  */
199 /*--------------------------------------------------------------------------*/
200 static msg_t
201 _create_active_win_msg(const char *appid, int surface)
202 {
203     JsonObject *obj     = NULL;
204     JsonObject *argobj  = NULL;
205     JsonGenerator *gen  = NULL;
206     JsonNode *root      = NULL;
207
208     /* create json object */
209     obj = json_object_new();
210     argobj = json_object_new();
211     if (obj == NULL || argobj == NULL) {
212         _ERR("json_object_new failed");
213         return NULL;
214     }
215
216     /* set message */
217     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_CHANGE_ACTIVE);
218     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
219     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
220
221     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
222     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
223
224     /* create root object */
225     root = json_node_new(JSON_NODE_OBJECT);
226     json_node_take_object(root, obj);
227
228     /* create generator object */
229     gen = json_generator_new();
230     json_generator_set_root(gen, root);
231
232     return gen;
233 }
234
235 /*--------------------------------------------------------------------------*/
236 /**
237  * @brief   _create_change_layer_msg
238  *          Create the message to change application window's layer.
239  *
240  * @param[in]   appid                   application id
241  * @param[in]   surface                 window's surface id
242  * @param[in]   layer                   window's layer id
243  * @return      json generator
244  * @retval      json generator          success
245  * @retval      NULL                    error
246  */
247 /*--------------------------------------------------------------------------*/
248 static msg_t
249 _create_change_layer_msg(const char *appid, int surface, int layer)
250 {
251     JsonObject *obj     = NULL;
252     JsonObject *argobj  = NULL;
253     JsonGenerator *gen  = NULL;
254     JsonNode *root      = NULL;
255
256     /* create json object */
257     obj = json_object_new();
258     argobj = json_object_new();
259     if (obj == NULL || argobj == NULL) {
260         _ERR("json_object_new failed");
261         return NULL;
262     }
263
264     /* set message */
265     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_CHANGE_LAYER);
266     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
267     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
268
269     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
270     json_object_set_int_member(argobj, MSG_PRMKEY_LAYER, layer);
271     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
272
273     /* create root object */
274     root = json_node_new(JSON_NODE_OBJECT);
275     json_node_take_object(root, obj);
276
277     /* create generator object */
278     gen = json_generator_new();
279     json_generator_set_root(gen, root);
280
281     return gen;
282 }
283
284 /*--------------------------------------------------------------------------*/
285 /**
286  * @brief   _create_map_get_msg
287  *          Create the message to map get.
288  *
289  * @param[in]   appid                   application id
290  * @param[in]   surface                 window's surface id
291  * @param[in]   filepath                pixel image file path
292  * @return      json generator
293  * @retval      json generator          success
294  * @retval      NULL                    error
295  */
296 /*--------------------------------------------------------------------------*/
297 static msg_t
298 _create_map_get_msg(const char *appid, int surface, const char *filepath)
299 {
300     JsonObject *obj     = NULL;
301     JsonObject *argobj  = NULL;
302     JsonGenerator *gen  = NULL;
303     JsonNode *root      = NULL;
304
305     /* create json object */
306     obj = json_object_new();
307     argobj = json_object_new();
308     if (obj == NULL || argobj == NULL) {
309         _ERR("json_object_new failed");
310         return NULL;
311     }
312
313     /* set message */
314     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_MAP_GET);
315     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
316     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
317
318     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
319     json_object_set_string_member(argobj, MSG_PRMKEY_ANIM_NAME, filepath);
320     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
321
322     /* create root object */
323     root = json_node_new(JSON_NODE_OBJECT);
324     json_node_take_object(root, obj);
325
326     /* create generator object */
327     gen = json_generator_new();
328     json_generator_set_root(gen, root);
329
330     return gen;
331 }
332
333 /*--------------------------------------------------------------------------*/
334 /**
335  * @brief   _create_map_thumb_msg
336  *          Create the message to map thumbnail.
337  *
338  * @param[in]   appid                   application id
339  * @param[in]   surface                 window's surface id
340  * @param[in]   framerate               notify cycle [frames par sec]
341  * @param[in]   filepath                pixel image file path
342  * @return      json generator
343  * @retval      json generator          success
344  * @retval      NULL                    error
345  */
346 /*--------------------------------------------------------------------------*/
347 static msg_t
348 _create_map_thumb_msg(const char *appid, int surface, int framerate, const char *filepath)
349 {
350     JsonObject *obj     = NULL;
351     JsonObject *argobj  = NULL;
352     JsonGenerator *gen  = NULL;
353     JsonNode *root      = NULL;
354
355     /* create json object */
356     obj = json_object_new();
357     argobj = json_object_new();
358     if (obj == NULL || argobj == NULL) {
359         _ERR("json_object_new failed");
360         return NULL;
361     }
362
363     /* set message */
364     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_MAP_THUMB);
365     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
366     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
367
368     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
369     json_object_set_int_member(argobj, MSG_PRMKEY_RATE, framerate);
370     if ((filepath != NULL) && (*filepath != 0) && (*filepath != ' '))   {
371         json_object_set_string_member(argobj, MSG_PRMKEY_ANIM_NAME, filepath);
372     }
373     else    {
374         json_object_set_string_member(argobj, MSG_PRMKEY_ANIM_NAME, " ");
375     }
376     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
377
378     /* create root object */
379     root = json_node_new(JSON_NODE_OBJECT);
380     json_node_take_object(root, obj);
381
382     /* create generator object */
383     gen = json_generator_new();
384     json_generator_set_root(gen, root);
385
386     return gen;
387 }
388
389 /*--------------------------------------------------------------------------*/
390 /**
391  * @brief   _create_unmap_thumb_msg
392  *          Create the message to unmap thumbnail.
393  *
394  * @param[in]   appid                   application id
395  * @param[in]   surface                 window's surface id
396  * @return      json generator
397  * @retval      json generator          success
398  * @retval      NULL                    error
399  */
400 /*--------------------------------------------------------------------------*/
401 static msg_t
402 _create_unmap_thumb_msg(const char *appid, int surface)
403 {
404     JsonObject *obj     = NULL;
405     JsonObject *argobj  = NULL;
406     JsonGenerator *gen  = NULL;
407     JsonNode *root      = NULL;
408
409     /* create json object */
410     obj = json_object_new();
411     argobj = json_object_new();
412     if (obj == NULL || argobj == NULL) {
413         _ERR("json_object_new failed");
414         return NULL;
415     }
416
417     /* set message */
418     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_UNMAP_THUMB);
419     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
420     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
421
422     json_object_set_int_member(argobj, MSG_PRMKEY_SURFACE, surface);
423     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
424
425     /* create root object */
426     root = json_node_new(JSON_NODE_OBJECT);
427     json_node_take_object(root, obj);
428
429     /* create generator object */
430     gen = json_generator_new();
431     json_generator_set_root(gen, root);
432
433     return gen;
434 }
435
436 /*--------------------------------------------------------------------------*/
437 /**
438  * @brief   _create_layer_msg
439  *          Create the message to show/hide layer or to set layer attribute.
440  *
441  * @param[in]   appid                   application id
442  * @param[in]   layer                   window's layer id
443  * @param[in]   type                    type of command
444  * @return      json generator
445  * @retval      json generator          success
446  * @retval      NULL                    error
447  */
448 /*--------------------------------------------------------------------------*/
449 static msg_t
450 _create_layer_msg(const char *appid, int layer, int type)
451 {
452     JsonObject *obj     = NULL;
453     JsonObject *argobj  = NULL;
454     JsonGenerator *gen  = NULL;
455     JsonNode *root      = NULL;
456     int cmd             = -1;
457
458     /* create json object */
459     obj = json_object_new();
460     argobj = json_object_new();
461     if (obj == NULL || argobj == NULL) {
462         _ERR("json_object_new failed");
463         return NULL;
464     }
465
466     /* set command */
467     if (type == _CMD_SHOW_LAYER) {
468         cmd = MSG_CMD_SHOW_LAYER;
469     }
470     else if (type == _CMD_HIDE_LAYER) {
471         cmd = MSG_CMD_HIDE_LAYER;
472     }
473
474     /* set message */
475     json_object_set_int_member(obj, MSG_PRMKEY_CMD, cmd);
476     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
477     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
478
479     json_object_set_int_member(argobj, MSG_PRMKEY_LAYER, layer);
480     json_object_set_object_member(obj, MSG_PRMKEY_ARG, argobj);
481
482     /* create root object */
483     root = json_node_new(JSON_NODE_OBJECT);
484     json_node_take_object(root, obj);
485
486     /* create generator object */
487     gen = json_generator_new();
488     json_generator_set_root(gen, root);
489
490     return gen;
491 }
492
493 /*============================================================================*/
494 /* internal common function                                                   */
495 /*============================================================================*/
496 /*--------------------------------------------------------------------------*/
497 /**
498  * @internal
499  * @brief   ico_syc_cb_win
500  *          Execute callback function. (ICO_SYC_EV_WIN_ACTIVE
501  *                                      ICO_SYC_EV_WIN_CREATE
502  *                                      ICO_SYC_EV_WIN_DESTROY)
503  *
504  * @param[in]   callback                callback function
505  * @param[in]   user_data               passed data on called callback function
506  * @param[in]   event                   event code
507  * @param[in]   data                    message data
508  * @param[in]   len                     length of data
509  * @return      none
510  */
511 /*--------------------------------------------------------------------------*/
512 void
513 ico_syc_cb_win(ico_syc_callback_t callback, void *user_data,
514                int event, const void *data, size_t len)
515 {
516     JsonParser *parser  = NULL;
517     GError *error       = NULL;
518     gboolean gbool      = FALSE;
519     JsonNode *root      = NULL;
520     JsonObject *obj     = NULL;
521     JsonObject *argobj  = NULL;
522
523     ico_syc_win_info_t *win_info    = NULL;
524
525     /* alloc memory */
526     win_info = calloc(1, sizeof(ico_syc_win_info_t));
527     if (win_info == NULL) {
528         _ERR("calloc failed");
529         return;
530     }
531
532     /* start parser */
533     parser = json_parser_new();
534     gbool = json_parser_load_from_data(parser, data, len, &error);
535     if (gbool == FALSE) {
536         g_object_unref(parser);
537         free(win_info);
538         _ERR("json_parser_load_from_data failed");
539         return;
540     }
541
542     /* get root node */
543     root = json_parser_get_root(parser);
544     if (root == NULL) {
545         g_object_unref(parser);
546         free(win_info);
547         _ERR("json_parser_get_root failed (root is NULL)");
548         return;
549     }
550
551     /* get object from root */
552     obj = json_node_get_object(root);
553     /* check message */
554     if (json_object_has_member(obj, MSG_PRMKEY_ARG) == FALSE) {
555         g_object_unref(parser);
556         free(win_info);
557         _ERR("invalid message" );
558         return;
559     }
560     /* get object from obj */
561     argobj = json_object_get_object_member(obj, MSG_PRMKEY_ARG);
562
563     /* set data */
564     win_info->appid = strdup(ico_syc_get_str_member(obj, MSG_PRMKEY_APPID));
565     win_info->name = strdup(ico_syc_get_str_member(argobj, MSG_PRMKEY_WINNAME));
566     win_info->surface = ico_syc_get_int_member(argobj, MSG_PRMKEY_SURFACE);
567
568     /* exec callback */
569     callback(event, win_info, user_data);
570
571     /* free memory */
572     g_object_unref(parser);
573     free(win_info->appid);
574     free(win_info->name);
575     free(win_info);
576
577     return;
578 }
579
580 /*--------------------------------------------------------------------------*/
581 /**
582  * @internal
583  * @brief   ico_syc_cb_win_attr
584  *          Execute callback function. (ICO_SYC_EV_WIN_ATTR_CHANGE)
585  *
586  * @param[in]   callback                callback function
587  * @param[in]   user_data               :assed data on called callback function
588  * @param[in]   event                   event code
589  * @param[in]   data                    message data
590  * @param[in]   len                     length of data
591  * @return      none
592  */
593 /*--------------------------------------------------------------------------*/
594 void
595 ico_syc_cb_win_attr(ico_syc_callback_t callback, void *user_data,
596                     int event, const void *data, size_t len)
597 {
598     JsonParser *parser  = NULL;
599     GError *error       = NULL;
600     gboolean gbool      = FALSE;
601     JsonNode *root      = NULL;
602     JsonObject *obj     = NULL;
603     JsonObject *argobj  = NULL;
604
605     ico_syc_win_attr_t *win_attr    = NULL;
606
607     /* alloc memory */
608     win_attr = calloc(1, sizeof(ico_syc_win_attr_t));
609     if (win_attr == NULL) {
610         _ERR("calloc failed");
611         return;
612     }
613
614     /* start parser */
615     parser = json_parser_new();
616     gbool = json_parser_load_from_data(parser, data, len, &error);
617     if (gbool == FALSE) {
618         g_object_unref(parser);
619         free(win_attr);
620         _ERR("json_parser_load_from_data failed");
621         return;
622     }
623
624     /* get root node */
625     root = json_parser_get_root(parser);
626     if (root == NULL) {
627         g_object_unref(parser);
628         free(win_attr);
629         _ERR("json_parser_get_root failed (root is NULL)");
630         return;
631     }
632
633     /* get object from root */
634     obj = json_node_get_object(root);
635     /* check message */
636     if (json_object_has_member(obj, MSG_PRMKEY_ARG) == FALSE) {
637         g_object_unref(parser);
638         free(win_attr);
639         _ERR("invalid message" );
640         return;
641     }
642     /* get object from obj */
643     argobj = json_object_get_object_member(obj, MSG_PRMKEY_ARG);
644
645     /* set data */
646     win_attr->appid = strdup(ico_syc_get_str_member(obj, MSG_PRMKEY_APPID));
647     win_attr->name = strdup(ico_syc_get_str_member(argobj, MSG_PRMKEY_WINNAME));
648     win_attr->zone = strdup(ico_syc_get_str_member(argobj, MSG_PRMKEY_ZONE));
649     win_attr->surface = ico_syc_get_int_member(argobj, MSG_PRMKEY_SURFACE);
650     win_attr->nodeid = ico_syc_get_int_member(argobj, MSG_PRMKEY_NODE);
651     win_attr->layer = ico_syc_get_int_member(argobj, MSG_PRMKEY_LAYER);
652     win_attr->pos_x = ico_syc_get_int_member(argobj, MSG_PRMKEY_POS_X);
653     win_attr->pos_y = ico_syc_get_int_member(argobj, MSG_PRMKEY_POS_Y);
654     win_attr->width = ico_syc_get_int_member(argobj, MSG_PRMKEY_WIDTH);
655     win_attr->height = ico_syc_get_int_member(argobj, MSG_PRMKEY_HEIGHT);
656     win_attr->raise = ico_syc_get_int_member(argobj, MSG_PRMKEY_RAISE);
657     win_attr->visible = ico_syc_get_int_member(argobj, MSG_PRMKEY_VISIBLE);
658     win_attr->active = ico_syc_get_int_member(argobj, MSG_PRMKEY_ACTIVE);
659
660     /* exec callback */
661     callback(event, win_attr, user_data);
662
663     /* free memory */
664     g_object_unref(parser);
665     free(win_attr->appid);
666     free(win_attr->name);
667     free(win_attr->zone);
668     free(win_attr);
669
670     return;
671 }
672
673 /*--------------------------------------------------------------------------*/
674 /**
675  * @internal
676  * @brief   ico_syc_cb_thumb
677  *          Execute callback function. (ICO_SYC_EV_THUMB_ERROR
678  *                                      ICO_SYC_EV_THUMB_CHANGE
679  *                                      ICO_SYC_EV_THUMB_UNMAP)
680  *
681  * @param[in]   callback                callback function
682  * @param[in]   user_data               passed data on called callback function
683  * @param[in]   event                   event code
684  * @param[in]   data                    message data
685  * @param[in]   len                     length of data
686  * @return      none
687  */
688 /*--------------------------------------------------------------------------*/
689 void
690 ico_syc_cb_thumb(ico_syc_callback_t callback, void *user_data,
691                  int event, const void *data, size_t len)
692 {
693     JsonParser *parser  = NULL;
694     GError *error       = NULL;
695     gboolean gbool      = FALSE;
696     JsonNode *root      = NULL;
697     JsonObject *obj     = NULL;
698     JsonObject *argobj  = NULL;
699
700     ico_syc_thumb_info_t *thumb_info    = NULL;
701
702     /* alloc memory */
703     thumb_info = calloc(1, sizeof(ico_syc_thumb_info_t));
704     if (thumb_info == NULL) {
705         _ERR("calloc failed");
706         return;
707     }
708
709     /* start parser */
710     parser = json_parser_new();
711     gbool = json_parser_load_from_data(parser, data, len, &error);
712     if (gbool == FALSE) {
713         g_object_unref(parser);
714         free(thumb_info);
715         _ERR("json_parser_load_from_data failed");
716         return;
717     }
718
719     /* get root node */
720     root = json_parser_get_root(parser);
721     if (root == NULL) {
722         g_object_unref(parser);
723         free(thumb_info);
724         _ERR("json_parser_get_root failed (root is NULL)");
725         return;
726     }
727
728     /* get object from root */
729     obj = json_node_get_object(root);
730     /* check message */
731     if (json_object_has_member(obj, MSG_PRMKEY_ARG) == FALSE) {
732         g_object_unref(parser);
733         free(thumb_info);
734         _ERR("invalid message" );
735         return;
736     }
737     /* get object from obj */
738     argobj = json_object_get_object_member(obj, MSG_PRMKEY_ARG);
739
740     /* set data */
741     thumb_info->appid = strdup(ico_syc_get_str_member(obj, MSG_PRMKEY_APPID));
742
743     thumb_info->surface = ico_syc_get_int_member(argobj,
744                                                  MSG_PRMKEY_SURFACE);
745     thumb_info->type = ico_syc_get_int_member(argobj, MSG_PRMKEY_ATTR);
746     thumb_info->width = ico_syc_get_int_member(argobj, MSG_PRMKEY_WIDTH);
747     thumb_info->height = ico_syc_get_int_member(argobj, MSG_PRMKEY_HEIGHT);
748     thumb_info->stride = ico_syc_get_int_member(argobj, MSG_PRMKEY_STRIDE);
749     thumb_info->format = ico_syc_get_int_member(argobj, MSG_PRMKEY_FORMAT);
750
751     /* exec callback */
752     callback(event, thumb_info, user_data);
753
754     /* free memory */
755     g_object_unref(parser);
756     free(thumb_info->appid);
757     free(thumb_info);
758
759     return;
760 }
761
762 /*--------------------------------------------------------------------------*/
763 /**
764  * @internal
765  * @brief   ico_syc_cb_layer
766  *          Execute callback function. (ICO_SYC_EV_LAYER_ATTR_CHANGE)
767  *
768  * @param[in]   callback                callback function
769  * @param[in]   user_data               passed data on called callback function
770  * @param[in]   event                   event code
771  * @param[in]   data                    message data
772  * @param[in]   len                     length of data
773  * @return      none
774  */
775 /*--------------------------------------------------------------------------*/
776 void
777 ico_syc_cb_layer(ico_syc_callback_t callback, void *user_data,
778                  int event, const void *data, size_t len)
779 {
780     JsonParser *parser  = NULL;
781     GError *error       = NULL;
782     gboolean gbool      = FALSE;
783     JsonNode *root      = NULL;
784     JsonObject *obj     = NULL;
785     JsonObject *argobj  = NULL;
786
787     ico_syc_layer_attr_t *layer_attr    = NULL;
788
789     /* alloc memory */
790     layer_attr = calloc(1, sizeof(ico_syc_layer_attr_t));
791     if (layer_attr == NULL) {
792         _ERR("calloc failed");
793         return;
794     }
795
796     /* start parser */
797     parser = json_parser_new();
798     gbool = json_parser_load_from_data(parser, data, len, &error);
799     if (gbool == FALSE) {
800         g_object_unref(parser);
801         free(layer_attr);
802         _ERR("json_parser_load_from_data failed");
803         return;
804     }
805
806     /* get root node */
807     root = json_parser_get_root(parser);
808     if (root == NULL) {
809         g_object_unref(parser);
810         free(layer_attr);
811         _ERR("json_parser_get_root failed (root is NULL)");
812         return;
813     }
814
815     /* get object from root */
816     obj = json_node_get_object(root);
817     /* check message */
818     if (json_object_has_member(obj, MSG_PRMKEY_ARG) == FALSE) {
819         g_object_unref(parser);
820         free(layer_attr);
821         _ERR("invalid message" );
822         return;
823     }
824     /* get object from obj */
825     argobj = json_object_get_object_member(obj, MSG_PRMKEY_ARG);
826     /* set data */
827     layer_attr->layer = ico_syc_get_int_member(argobj, MSG_PRMKEY_LAYER);
828     layer_attr->visible = ico_syc_get_int_member(argobj,
829                                                      MSG_PRMKEY_VISIBLE);
830
831     /* exec callback */
832     callback(event, layer_attr, user_data);
833
834     /* free memory */
835     g_object_unref(parser);
836     free(layer_attr);
837
838     return;
839 }
840
841 /*============================================================================*/
842 /* public interface function                                                  */
843 /*============================================================================*/
844 /*--------------------------------------------------------------------------*/
845 /**
846  * @brief   ico_syc_show
847  *          Show the application window with animation.
848  *          If user sets argument surface "ICO_SYC_WIN_SHOW_ALL",
849  *          show all of the application windows.
850  *
851  * @param[in]   appid                   application id
852  * @param[in]   surface                 window's surface id
853  * @param[in]   animation               animation information
854  * @return      result
855  * @retval      0                       success
856  * @retval      not 0                   error
857  */
858 /*--------------------------------------------------------------------------*/
859 ICO_API int
860 ico_syc_show(const char *appid, int surface,
861              const ico_syc_animation_t *animation)
862 {
863     int ret = ICO_SYC_ERR_NONE;
864     msg_t msg;
865
866     /* check argument */
867     if (appid == NULL) {
868         _ERR("invalid parameter (appid is NULL)");
869         return ICO_SYC_ERR_INVALID_PARAM;
870     }
871
872     /* make message */
873     msg = _create_win_msg(appid, surface, animation, _CMD_SHOW_WIN);
874     /* send message */
875     ret = ico_syc_send_msg(msg);
876     /* free send message */
877     ico_syc_free_msg(msg);
878
879     return ret;
880 }
881
882 /*--------------------------------------------------------------------------*/
883 /**
884  * @brief   ico_syc_hide
885  *          Hide the application window with animation.
886  *
887  * @param[in]   appid                   application id
888  * @param[in]   surface                 window's surface id
889  * @param[in]   animation               animation information
890  * @return      result
891  * @retval      0                       success
892  * @retval      not 0                   error
893  */
894 /*--------------------------------------------------------------------------*/
895 ICO_API int
896 ico_syc_hide(const char *appid, int surface,
897              const ico_syc_animation_t *animation)
898 {
899     int ret = ICO_SYC_ERR_NONE;
900     msg_t msg;
901
902     /* check argument */
903     if (appid == NULL) {
904         _ERR("invalid parameter (appid is NULL)");
905         return ICO_SYC_ERR_INVALID_PARAM;
906     }
907
908     /* make message */
909     msg = _create_win_msg(appid, surface, animation, _CMD_HIDE_WIN);
910     /* send message */
911     ret = ico_syc_send_msg(msg);
912     /* free send message */
913     ico_syc_free_msg(msg);
914
915     return ret;
916 }
917
918 /*--------------------------------------------------------------------------*/
919 /**
920  * @brief   ico_syc_move
921  *          Move the application window with animation.
922  *
923  * @param[in]   appid                   application id
924  * @param[in]   surface                 window's surface id
925  * @param[in]   move                    move information (zone/position/size)
926  * @param[in]   animation               animation information
927  * @return      result
928  * @retval      0                       success
929  * @retval      not 0                   error
930  */
931 /*--------------------------------------------------------------------------*/
932 ICO_API int
933 ico_syc_move(const char *appid, int surface,
934              const ico_syc_win_move_t *move,
935              const ico_syc_animation_t *animation)
936 {
937     int ret = ICO_SYC_ERR_NONE;
938     msg_t msg;
939
940     /* check argument */
941     if (appid == NULL) {
942         _ERR("invalid parameter (appid is NULL)");
943         return ICO_SYC_ERR_INVALID_PARAM;
944     }
945
946     /* make message */
947     msg = _create_win_move_msg(appid, surface, move, animation);
948     /* send message */
949     ret = ico_syc_send_msg(msg);
950     /* free send message */
951     ico_syc_free_msg(msg);
952
953     return ret;
954 }
955
956 /*--------------------------------------------------------------------------*/
957 /**
958  * @brief   ico_syc_change_active
959  *          Change the active window which receives the win-event notification
960  *          from System Controller.
961  *
962  * @param[in]   appid                   application id
963  * @param[in]   surface                 window's surface id
964  * @return      result
965  * @retval      0                       success
966  * @retval      not 0                   error
967  */
968 /*--------------------------------------------------------------------------*/
969 ICO_API int
970 ico_syc_change_active(const char *appid, int surface)
971 {
972     int ret = ICO_SYC_ERR_NONE;
973     msg_t msg;
974
975     /* check argument */
976     if (appid == NULL) {
977         _ERR("invalid parameter (appid is NULL)");
978         return ICO_SYC_ERR_INVALID_PARAM;
979     }
980
981     /* make message */
982     msg = _create_active_win_msg(appid, surface);
983     /* send message */
984     ret = ico_syc_send_msg(msg);
985     /* free send message */
986     ico_syc_free_msg(msg);
987
988     return ret;
989 }
990
991 /*--------------------------------------------------------------------------*/
992 /**
993  * @brief   ico_syc_change_layer
994  *          Change the window's layer.
995  *
996  * @param[in]   appid                   application id
997  * @param[in]   surface                 window's surface id
998  * @param[in]   layer                   window's layer id
999  * @return      result
1000  * @retval      0                       success
1001  * @retval      not 0                   error
1002  */
1003 /*--------------------------------------------------------------------------*/
1004 ICO_API int
1005 ico_syc_change_layer(const char *appid, int surface, int layer)
1006 {
1007     int ret = ICO_SYC_ERR_NONE;
1008     msg_t msg;
1009
1010     /* check argument */
1011     if (appid == NULL) {
1012         _ERR("invalid parameter (appid is NULL)");
1013         return ICO_SYC_ERR_INVALID_PARAM;
1014     }
1015
1016     /* make message */
1017     msg = _create_change_layer_msg(appid, surface, layer);
1018     /* send message */
1019     ret = ico_syc_send_msg(msg);
1020     /* free send message */
1021     ico_syc_free_msg(msg);
1022
1023     return ret;
1024 }
1025
1026 /*--------------------------------------------------------------------------*/
1027 /**
1028  * @brief   ico_syc_map_get
1029  *          Get the surface thumbnail pixel image to the file.
1030  *
1031  * @param[in]   surface                 window's surface id
1032  * @param[in]   filepath                surface image pixel file path
1033  * @return      result
1034  * @retval      0                       success
1035  * @retval      not 0                   error
1036  */
1037 /*--------------------------------------------------------------------------*/
1038 ICO_API int
1039 ico_syc_map_get(int surface, const char *filepath)
1040 {
1041     int ret = ICO_SYC_ERR_NONE;
1042     msg_t msg;
1043     char *appid;
1044
1045     /* get appid */
1046     appid = ico_syc_get_appid();
1047
1048     /* make message */
1049     msg = _create_map_get_msg(appid, surface, filepath);
1050     /* send message */
1051     ret = ico_syc_send_msg(msg);
1052     /* free send message */
1053     ico_syc_free_msg(msg);
1054
1055     return ret;
1056 }
1057
1058 /*--------------------------------------------------------------------------*/
1059 /**
1060  * @brief   ico_syc_map_thumb
1061  *          Prepare the thumbnail data
1062  *
1063  * @param[in]   surface                 window's surface id
1064  * @param[in]   framerate               notify cycle [ms]
1065  * @param[in]   filepath                pixel image file path
1066  * @return      result
1067  * @retval      0                       success
1068  * @retval      not 0                   error
1069  */
1070 /*--------------------------------------------------------------------------*/
1071 ICO_API int
1072 ico_syc_map_thumb(int surface, int framerate, const char *filepath)
1073 {
1074     int ret = ICO_SYC_ERR_NONE;
1075     msg_t msg;
1076     char *appid;
1077
1078     /* get appid */
1079     appid = ico_syc_get_appid();
1080
1081     /* make message */
1082     msg = _create_map_thumb_msg(appid, surface, framerate, filepath);
1083     /* send message */
1084     ret = ico_syc_send_msg(msg);
1085     /* free send message */
1086     ico_syc_free_msg(msg);
1087
1088     return ret;
1089 }
1090
1091 /*--------------------------------------------------------------------------*/
1092 /**
1093  * @brief   ico_syc_unmap_thumb
1094  *          Unmap the thumbnail data.
1095  *          User calls this API when receiving the notification that
1096  *          terminated the application.
1097  *
1098  * @param[in]   surface                 window's surface id
1099  * @return      result
1100  * @retval      0                       success
1101  * @retval      not 0                   error
1102  */
1103 /*--------------------------------------------------------------------------*/
1104 ICO_API int
1105 ico_syc_unmap_thumb(int surface)
1106 {
1107     int ret = ICO_SYC_ERR_NONE;
1108     msg_t msg;
1109     char *appid;
1110
1111     /* get appid */
1112     appid = ico_syc_get_appid();
1113
1114     /* make message */
1115     msg = _create_unmap_thumb_msg(appid, surface);
1116     /* send message */
1117     ret = ico_syc_send_msg(msg);
1118     /* free send message */
1119     ico_syc_free_msg(msg);
1120
1121     return ret;
1122 }
1123
1124 /*--------------------------------------------------------------------------*/
1125 /**
1126  * @brief   ico_syc_show_layer
1127  *          Show the layer.
1128  *
1129  * @param[in]   layer                   window's layer id
1130  * @return      result
1131  * @retval      0                       success
1132  * @retval      not 0                   error
1133  */
1134 /*--------------------------------------------------------------------------*/
1135 ICO_API int
1136 ico_syc_show_layer(int layer)
1137 {
1138     int ret = ICO_SYC_ERR_NONE;
1139     msg_t msg;
1140     char *appid;
1141
1142     /* get appid */
1143     appid = ico_syc_get_appid();
1144
1145     /* make message */
1146     msg = _create_layer_msg(appid, layer, _CMD_SHOW_LAYER);
1147     /* send message */
1148     ret = ico_syc_send_msg(msg);
1149     /* free send message */
1150     ico_syc_free_msg(msg);
1151
1152     return ret;
1153 }
1154
1155 /*--------------------------------------------------------------------------*/
1156 /**
1157  * @brief   ico_syc_hide_layer
1158  *          Hide the layer.
1159  *
1160  * @param[in]   layer                   window's layer id
1161  * @return      result
1162  * @retval      0                       success
1163  * @retval      not 0                   error
1164  */
1165 /*--------------------------------------------------------------------------*/
1166 ICO_API int
1167 ico_syc_hide_layer(int layer)
1168 {
1169     int ret = ICO_SYC_ERR_NONE;
1170     msg_t msg;
1171     char *appid;
1172
1173     /* get appid */
1174     appid = ico_syc_get_appid();
1175
1176     /* make message */
1177     msg = _create_layer_msg(appid, layer, _CMD_HIDE_LAYER);
1178     /* send message */
1179     ret = ico_syc_send_msg(msg);
1180     /* free send message */
1181     ico_syc_free_msg(msg);
1182
1183     return ret;
1184 }
1185 /* vim: set expandtab ts=4 sw=4: */