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