Some interfaces were added for SystemController
[profile/ivi/ico-uxf-homescreen.git] / lib / apps-framework / ico_syc_appresctl.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   Application Resource Control API
11  *          for general applications
12  *
13  * @date    Aug-7-2013
14  */
15
16 #include <string.h>
17 #include <unistd.h>
18
19 #include <aul/aul.h>
20
21 #include "ico_syc_application.h"
22 #include "ico_syc_msg_cmd_def.h"
23 #include "ico_syc_msg.h"
24 #include "ico_syc_private.h"
25
26 /*============================================================================*/
27 /* define static function prototype                                           */
28 /*============================================================================*/
29 static ico_syc_res_window_t *_create_res_window(const char *ECU,
30                                                 const char *display,
31                                                 const char *layer,
32                                                 const char *layout,
33                                                 const char *area,
34                                                 const char *dispatchApp,
35                                                 const char *role,
36                                                 uint32_t resourceId);
37 static ico_syc_res_sound_t *_create_res_sound(char *zone, char *name, char *id,
38                                               int adjust);
39 static ico_syc_res_input_t *_create_res_input(char *name, int event);
40 static void _free_res_window(ico_syc_res_window_t *window);
41 static void _free_res_sound(ico_syc_res_sound_t *sound);
42 static void _free_res_input(ico_syc_res_input_t *input);
43 static struct ico_syc_res_context * _create_context(
44                                     char *appid,
45                                     const ico_syc_res_window_t *window,
46                                     const ico_syc_res_sound_t *sound,
47                                     const ico_syc_res_input_t *input,
48                                     int type);
49 /* create send message */
50 static JsonObject *_create_window_msg(ico_syc_res_window_t *window);
51 static JsonObject *_create_sound_msg(ico_syc_res_sound_t *sound);
52 static JsonObject *_create_input_msg(ico_syc_res_input_t *input);
53 static msg_t _create_acquire_res_msg(const struct ico_syc_res_context *context);
54 static msg_t _create_release_res_msg(const struct ico_syc_res_context *context);
55 static msg_t _create_set_region_msg(const char *appid,
56                                     const ico_syc_input_region_t *input,
57                                     int attr);
58 static msg_t _create_unset_region_msg(const char *appid,
59                                       const ico_syc_input_region_t *input);
60
61 /*============================================================================*/
62 /* static function                                                            */
63 /*============================================================================*/
64 /*--------------------------------------------------------------------------*/
65 /**
66  * @brief   _create_res_window
67  *          Allocate memory for window resource information.
68  *
69  * @param[in]   zone                    window zone
70  * @param[in]   name                    windo name
71  * @param[in]   id                      window id
72  * @return      resource info
73  * @retval      address                 success
74  * @retval      NULL                    error
75  */
76 /*--------------------------------------------------------------------------*/
77 static ico_syc_res_window_t *
78     _create_res_window(const char *ECU, const char *display, const char *layer,
79                        const char *layout, const char *area,
80                        const char *dispatchApp, const char *role,
81                        uint32_t resourceId)
82 {
83     ico_syc_res_window_t *info  = NULL;
84
85     if ((NULL == ECU) || (NULL == display) || (NULL == layer) ||
86         (NULL == layout) || (NULL == area) || (NULL == dispatchApp) ||
87         (NULL == role)) {
88         _ERR("invalid parameter (zone, name is NULL)");
89         return NULL;
90     }
91
92     /* alloc memory */
93     info = calloc(1, sizeof(ico_syc_res_window_t));
94     if (info == NULL) {
95         _ERR("calloc failed");
96         return NULL;
97     }
98
99     /* set element */
100     info->ECU         = strdup(ECU);
101     info->display     = strdup(display);
102     info->layer       = strdup(layer);
103     info->layout      = strdup(layout);
104     info->area        = strdup(area);
105     info->dispatchApp = strdup(dispatchApp);
106     info->role        = strdup(role);
107     info->resourceId  = resourceId;
108
109     return info;
110 }
111
112 /*--------------------------------------------------------------------------*/
113 /**
114  * @brief   _create_res_sound
115  *          Allocate memory for sound resource information.
116  *
117  * @param[in]   zone                    sound zone
118  * @param[in]   name                    sound name
119  * @param[in]   id                      sound id
120  * @param[in]   adjust                  adjust type
121  * @return      resource info
122  * @retval      address                 success
123  * @retval      NULL                    error
124  */
125 /*--------------------------------------------------------------------------*/
126 static ico_syc_res_sound_t *
127 _create_res_sound(char *zone, char *name, char *id, int adjust)
128 {
129     ico_syc_res_sound_t *info   = NULL;
130
131     if (zone == NULL || name == NULL) {
132         _ERR("invalid parameter (zone, name is NULL)");
133         return NULL;
134     }
135
136     /* alloc memory */
137     info = calloc(1, sizeof(ico_syc_res_sound_t));
138     if (info == NULL) {
139         _ERR("calloc failed");
140         return NULL;
141     }
142
143     /* set element */
144     info->zone = strdup(zone);
145     info->name = strdup(name);
146     if (id != NULL) {
147         info->id = strdup(id);
148     }
149     info->adjust = adjust;
150
151     return info;
152 }
153
154 /*--------------------------------------------------------------------------*/
155 /**
156  * @brief   _create_res_input
157  *          Allocate memory for input resource information.
158  *
159  * @param[in]   name                    input device name
160  * @param[in]   event                   input event
161  * @return      resource info
162  * @retval      address                 success
163  * @retval      NULL                    error
164  */
165 /*--------------------------------------------------------------------------*/
166 static ico_syc_res_input_t *
167 _create_res_input(char *name, int event)
168 {
169     ico_syc_res_input_t *info   = NULL;
170
171     if (name == NULL) {
172         _ERR("invalid parameter (name is NULL)");
173         return NULL;
174     }
175
176     /* alloc memory */
177     info = calloc(1, sizeof(ico_syc_res_input_t));
178     if (info == NULL) {
179         _ERR("calloc failed");
180         return NULL;
181     }
182
183     /* set element */
184     info->name = strdup(name);
185     info->event = event;
186
187     return info;
188 }
189
190 /*--------------------------------------------------------------------------*/
191 /**
192  * @brief   _free_res_window
193  *          Free memory of window resource information.
194  *
195  * @param[in]   window                  window resource's information
196  * @return      none
197  */
198 /*--------------------------------------------------------------------------*/
199 static void
200 _free_res_window(ico_syc_res_window_t *w)
201 {
202     if (w == NULL) {
203         return;
204     }
205
206     /* free element */
207     if (NULL != w->ECU)         free(w->ECU);
208     if (NULL != w->display)     free(w->display);
209     if (NULL != w->layer)       free(w->layer);
210     if (NULL != w->layout)      free(w->layout);
211     if (NULL != w->area)        free(w->area);
212     if (NULL != w->dispatchApp) free(w->dispatchApp);
213     if (NULL != w->role)        free(w->role);
214     /* free */
215     free(w);
216
217     return;
218 }
219
220 /*--------------------------------------------------------------------------*/
221 /**
222  * @brief   _free_res_sound
223  *          Free memory of sound resource information.
224  *
225  * @param[in]   sound                   sound resource's information
226  * @return      none
227  */
228 /*--------------------------------------------------------------------------*/
229 static void
230 _free_res_sound(ico_syc_res_sound_t *sound)
231 {
232     if (sound == NULL) {
233         return;
234     }
235
236     /* free element */
237     if (sound->zone != NULL) free(sound->zone);
238     if (sound->name != NULL) free(sound->name);
239     if (sound->id != NULL) free(sound->id);
240     /* free */
241     free(sound);
242
243     return;
244 }
245
246 /*--------------------------------------------------------------------------*/
247 /**
248  * @brief   _free_res_input
249  *          Free memory of input resource information.
250  *
251  * @param[in]   input                   input resource's information
252  * @return      none
253  */
254 /*--------------------------------------------------------------------------*/
255 static void
256 _free_res_input(ico_syc_res_input_t *input)
257 {
258     if (input == NULL) {
259         return;
260     }
261
262     /* free element */
263     if (input->name != NULL) free(input->name);
264     /* free */
265     free(input);
266
267     return;
268 }
269
270 /*--------------------------------------------------------------------------*/
271 /**
272  * @brief   _create_context
273  *          Create resource information context.
274  *
275  * @param[in]   appid                   application id
276  * @param[in]   window                  window resource's information
277  * @param[in]   sound                   sound resource's information
278  * @param[in]   input                   input resource's information
279  * @param[in]   type                    window and sound resource type
280  *                                      (basic or interruption)
281  * @return      resource's context address
282  * @retval      address                 success
283  * @retval      NULL                    error
284  */
285 /*--------------------------------------------------------------------------*/
286 static struct ico_syc_res_context *
287 _create_context(char *appid, const ico_syc_res_window_t *w,
288                 const ico_syc_res_sound_t *sound,
289                 const ico_syc_res_input_t *input,
290                 int type)
291 {
292     struct ico_syc_res_context *context = NULL;
293
294     context = (struct ico_syc_res_context *)
295                               calloc(1, sizeof(struct ico_syc_res_context));
296     if (context == NULL) {
297         _ERR("calloc failed");
298         return NULL;
299     }
300
301     /* set appid */
302     strcpy(context->appid, appid);
303
304     /* set window info */
305     if (w != NULL) {
306         context->window = _create_res_window(w->ECU, w->display, w->layer,
307                                              w->layout, w->area,
308                                              w->dispatchApp, w->role,
309                                              w->resourceId);
310     }
311     /* set sound info */
312     if (sound != NULL) {
313         context->sound = _create_res_sound(sound->zone, sound->name,
314                                            sound->id, sound->adjust);
315     }
316     /* set input info */
317     if (input != NULL) {
318         context->input = _create_res_input(input->name, input->event);
319     }
320
321     /* set resource type */
322     if (w != NULL || sound != NULL) {
323         context->type = type;
324     }
325
326     return context;
327 }
328
329 /*--------------------------------------------------------------------------*/
330 /**
331  * @brief   _create_window_msg
332  *          Create the JsonObject to set the window resource information.
333  *
334  * @param[in]   window                  window resource information
335  * @return      json object
336  * @retval      json object             success
337  * @retval      NULL                    error
338  */
339 /*--------------------------------------------------------------------------*/
340 static JsonObject *
341 _create_window_msg(ico_syc_res_window_t *w)
342 {
343     JsonObject *info    = NULL;
344
345     if (w == NULL) {
346         _ERR("invalid parameter (window is NULL)");
347         return NULL;
348     }
349
350     /* create json object */
351     info = json_object_new();
352     if (info == NULL) {
353         _ERR("json_object_new failed");
354         return NULL;
355     }
356
357     /* set member */
358     json_object_set_string_member(info, MSG_PRMKEY_ECU, w->ECU);
359     json_object_set_string_member(info, MSG_PRMKEY_DISPLAY, w->display);
360     json_object_set_string_member(info, MSG_PRMKEY_LAYER, w->layer);
361     json_object_set_string_member(info, MSG_PRMKEY_LAYOUT, w->layout);
362     json_object_set_string_member(info, MSG_PRMKEY_AREA, w->area);
363     json_object_set_string_member(info, MSG_PRMKEY_DISPATCHAPP, w->dispatchApp);
364     json_object_set_string_member(info, MSG_PRMKEY_ROLE, w->role);
365     json_object_set_int_member(info, MSG_PRMKEY_RESOURCEID, w->resourceId);
366
367     return info;
368 }
369
370 /*--------------------------------------------------------------------------*/
371 /**
372  * @brief   _create_sound_msg
373  *          Create the JsonObject to set the sound resource information.
374  *
375  * @param[in]   sound                   sound resource information
376  * @return      json object
377  * @retval      json object             success
378  * @retval      NULL                    error
379  */
380 /*--------------------------------------------------------------------------*/
381 static JsonObject *
382 _create_sound_msg(ico_syc_res_sound_t *sound)
383 {
384     JsonObject *info    = NULL;
385
386     if (sound == NULL) {
387         _ERR("invalid parameter (sound is NULL)");
388         return NULL;
389     }
390
391     /* create json object */
392     info = json_object_new();
393     if (info == NULL) {
394         _ERR("json_object_new failed");
395         return NULL;
396     }
397
398     /* set member */
399     json_object_set_string_member(info, MSG_PRMKEY_RES_ZONE, sound->zone);
400     json_object_set_string_member(info, MSG_PRMKEY_RES_NAME, sound->name);
401     json_object_set_string_member(info, MSG_PRMKEY_RES_ID, sound->id);
402     json_object_set_int_member(info, MSG_PRMKEY_RES_ADJUST, sound->adjust);
403
404     return info;
405 }
406
407 /*--------------------------------------------------------------------------*/
408 /**
409  * @brief   _create_input_msg
410  *          Create the JsonObject to set the input resource information.
411  *
412  * @param[in]   input                   input resource information
413  * @return      json object
414  * @retval      json object             success
415  * @retval      NULL                    error
416  */
417 /*--------------------------------------------------------------------------*/
418 static JsonObject *
419 _create_input_msg(ico_syc_res_input_t *input)
420 {
421     JsonObject *info    = NULL;
422
423     if (input == NULL) {
424         _ERR("invalid parameter (input is NULL)");
425         return NULL;
426     }
427
428     /* create json object */
429     info = json_object_new();
430     if (info == NULL) {
431         _ERR("json_object_new failed");
432         return NULL;
433     }
434
435     /* set member */
436     json_object_set_string_member(info, MSG_PRMKEY_RES_NAME, input->name);
437     json_object_set_int_member(info, MSG_PRMKEY_RES_EV, input->event);
438
439     return info;
440 }
441
442
443 /*--------------------------------------------------------------------------*/
444 /**
445  * @brief   _create_acquire_res_msg
446  *          Create the message to acquire the resource (window/sound/input).
447  *
448  * @param[in]   context                 resource context
449  * @return      json generator
450  * @retval      json generator          success
451  * @retval      NULL                    error
452  */
453 /*--------------------------------------------------------------------------*/
454 static msg_t
455 _create_acquire_res_msg(const struct ico_syc_res_context *context)
456 {
457     JsonObject *obj     = NULL;
458     JsonObject *resobj  = NULL;
459     JsonObject *info    = NULL;
460     JsonGenerator *gen  = NULL;
461     JsonNode *root      = NULL;
462
463     /* create json object */
464     obj = json_object_new();
465     resobj = json_object_new();
466     if (obj == NULL || resobj == NULL) {
467         _ERR("json_object_new failed");
468         return NULL;
469     }
470
471     /* set message */
472     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_ACQUIRE_RES);
473     json_object_set_string_member(obj, MSG_PRMKEY_APPID, context->appid);
474     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
475
476     /* window resource */
477     if (context->window != NULL) {
478         info = _create_window_msg(context->window);
479         /* set object */
480         json_object_set_object_member(resobj, MSG_PRMKEY_RES_WINDOW, info);
481     }
482
483     /* sound resource */
484     if (context->sound != NULL) {
485         info = _create_sound_msg(context->sound);
486         /* set object */
487         json_object_set_object_member(resobj, MSG_PRMKEY_RES_SOUND, info);
488     }
489
490     /* input resource */
491     if (context->input != NULL) {
492         info = _create_input_msg(context->input);
493         /* set object */
494         json_object_set_object_member(resobj, MSG_PRMKEY_RES_INPUT, info);
495     }
496
497     /* resource type */
498     if (context->window != NULL || context->sound != NULL) {
499         json_object_set_int_member(resobj, MSG_PRMKEY_RES_TYPE, context->type);
500     }
501     /* set resource object */
502     json_object_set_object_member(obj, MSG_PRMKEY_RES, resobj);
503
504     /* create root object */
505     root = json_node_new(JSON_NODE_OBJECT);
506     json_node_take_object(root, obj);
507
508     /* create generator object */
509     gen = json_generator_new();
510     json_generator_set_root(gen, root);
511
512     return gen;
513 }
514
515 /*--------------------------------------------------------------------------*/
516 /**
517  * @brief   _create_release_res_msg
518  *          Create the message to release the resource (window/sound/input).
519  *
520  * @param[in]   context                 resource context
521  * @return      json generator
522  * @retval      json generator          success
523  * @retval      NULL                    error
524  */
525 /*--------------------------------------------------------------------------*/
526 static msg_t
527 _create_release_res_msg(const struct ico_syc_res_context *context)
528 {
529     JsonObject *obj     = NULL;
530     JsonObject *resobj  = NULL;
531     JsonObject *info    = NULL;
532     JsonGenerator *gen  = NULL;
533     JsonNode *root      = NULL;
534
535     /* create json object */
536     obj = json_object_new();
537     resobj = json_object_new();
538     if (obj == NULL || resobj == NULL) {
539         _ERR("json_object_new failed");
540         return NULL;
541     }
542
543     /* set message */
544     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_RELEASE_RES);
545     json_object_set_string_member(obj, MSG_PRMKEY_APPID, context->appid);
546     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
547
548     /* window resource */
549     if (context->window != NULL) {
550         info = _create_window_msg(context->window);
551         /* set object */
552         json_object_set_object_member(resobj, MSG_PRMKEY_RES_WINDOW, info);
553     }
554
555     /* sound resource */
556     if (context->sound != NULL) {
557         info = _create_sound_msg(context->sound);
558         /* set object */
559         json_object_set_object_member(resobj, MSG_PRMKEY_RES_SOUND, info);
560     }
561
562     /* input resource */
563     if (context->input != NULL) {
564         info = _create_input_msg(context->input);
565         /* set object */
566         json_object_set_object_member(resobj, MSG_PRMKEY_RES_INPUT, info);
567     }
568     /* resource type */
569     if (context->window != NULL || context->sound != NULL) {
570         json_object_set_int_member(resobj, MSG_PRMKEY_RES_TYPE, context->type);
571     }
572     /* set resource object */
573     json_object_set_object_member(obj, MSG_PRMKEY_RES, resobj);
574
575     /* create root object */
576     root = json_node_new(JSON_NODE_OBJECT);
577     json_node_take_object(root, obj);
578
579     /* create generator object */
580     gen = json_generator_new();
581     json_generator_set_root(gen, root);
582
583     return gen;
584 }
585
586 /*--------------------------------------------------------------------------*/
587 /**
588  * @brief   _create_set_region_msg
589  *          Create the message to set the input region.
590  *
591  * @param[in]   appid                   application id
592  * @param[in]   input                   input region's information
593  * @param[in]   attr                    input region's attribute
594  * @return      json generator
595  * @retval      json generator          success
596  * @retval      NULL                    error
597  */
598 /*--------------------------------------------------------------------------*/
599 static msg_t
600 _create_set_region_msg(const char *appid, const ico_syc_input_region_t *input,
601                        int attr)
602 {
603     JsonObject *obj     = NULL;
604     JsonObject *resobj  = NULL;
605     JsonGenerator *gen  = NULL;
606     JsonNode *root      = NULL;
607
608     /* create json object */
609     obj = json_object_new();
610     resobj = json_object_new();
611     if (obj == NULL || resobj == NULL) {
612         _ERR("json_object_new failed");
613         return NULL;
614     }
615
616     /* set message */
617     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_SET_REGION);
618     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
619     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
620
621     json_object_set_string_member(resobj, MSG_PRMKEY_WINNAME, input->winname);
622     json_object_set_int_member(resobj, MSG_PRMKEY_RES_POS_X, input->pos_x);
623     json_object_set_int_member(resobj, MSG_PRMKEY_RES_POS_Y, input->pos_y);
624     json_object_set_int_member(resobj, MSG_PRMKEY_RES_WIDTH, input->width);
625     json_object_set_int_member(resobj, MSG_PRMKEY_RES_HEIGHT, input->height);
626     json_object_set_int_member(resobj, MSG_PRMKEY_RES_HOT_X, input->hotspot_x);
627     json_object_set_int_member(resobj, MSG_PRMKEY_RES_HOT_Y, input->hotspot_y);
628     json_object_set_int_member(resobj, MSG_PRMKEY_RES_CUR_X, input->cursor_x);
629     json_object_set_int_member(resobj, MSG_PRMKEY_RES_CUR_Y, input->cursor_y);
630     json_object_set_int_member(resobj, MSG_PRMKEY_RES_CUR_WIDTH, input->cursor_width);
631     json_object_set_int_member(resobj, MSG_PRMKEY_RES_CUR_HEIGHT, input->cursor_height);
632     json_object_set_int_member(resobj, MSG_PRMKEY_RES_ATTR, attr);
633     json_object_set_object_member(obj, MSG_PRMKEY_REGION, resobj);
634
635     /* create root object */
636     root = json_node_new(JSON_NODE_OBJECT);
637     json_node_take_object(root, obj);
638
639     /* create generator object */
640     gen = json_generator_new();
641     json_generator_set_root(gen, root);
642
643     return gen;
644 }
645
646 /*--------------------------------------------------------------------------*/
647 /**
648  * @brief   _create_unset_region_msg
649  *          Create the message to unset the input region.
650  *
651  * @param[in]   appid                   application id
652  * @param[in]   input                   input region's information
653  * @return      json generator
654  * @retval      json generator          success
655  * @retval      NULL                    error
656  */
657 /*--------------------------------------------------------------------------*/
658 static msg_t
659 _create_unset_region_msg(const char *appid, const ico_syc_input_region_t *input)
660 {
661     JsonObject *obj     = NULL;
662     JsonObject *resobj  = NULL;
663     JsonGenerator *gen  = NULL;
664     JsonNode *root      = NULL;
665
666     /* create json object */
667     obj = json_object_new();
668     resobj = json_object_new();
669     if (obj == NULL || resobj == NULL) {
670         _ERR("json_object_new failed");
671         return NULL;
672     }
673
674     /* set message */
675     json_object_set_int_member(obj, MSG_PRMKEY_CMD, MSG_CMD_UNSET_REGION);
676     json_object_set_string_member(obj, MSG_PRMKEY_APPID, appid);
677     json_object_set_int_member(obj, MSG_PRMKEY_PID, getpid());
678
679     if (input)  {
680         json_object_set_string_member(resobj, MSG_PRMKEY_WINNAME, input->winname);
681         json_object_set_int_member(resobj, MSG_PRMKEY_RES_POS_X, input->pos_x);
682         json_object_set_int_member(resobj, MSG_PRMKEY_RES_POS_Y, input->pos_y);
683         json_object_set_int_member(resobj, MSG_PRMKEY_RES_WIDTH, input->width);
684         json_object_set_int_member(resobj, MSG_PRMKEY_RES_HEIGHT, input->height);
685     }
686     else    {
687         json_object_set_string_member(resobj, MSG_PRMKEY_WINNAME, " ");
688         json_object_set_int_member(resobj, MSG_PRMKEY_RES_POS_X, 0);
689         json_object_set_int_member(resobj, MSG_PRMKEY_RES_POS_Y, 0);
690         json_object_set_int_member(resobj, MSG_PRMKEY_RES_WIDTH, 0);
691         json_object_set_int_member(resobj, MSG_PRMKEY_RES_HEIGHT, 0);
692     }
693     json_object_set_object_member(obj, MSG_PRMKEY_REGION, resobj);
694
695     /* create root object */
696     root = json_node_new(JSON_NODE_OBJECT);
697     json_node_take_object(root, obj);
698
699     /* create generator object */
700     gen = json_generator_new();
701     json_generator_set_root(gen, root);
702
703     return gen;
704 }
705
706 /*============================================================================*/
707 /* internal common function                                                   */
708 /*============================================================================*/
709 /*--------------------------------------------------------------------------*/
710 /**
711  * @internal
712  * @brief   ico_syc_cb_res
713  *          Execute callback function. (ICO_SYC_EV_RES_ACQUIRE
714  *                                      ICO_SYC_EV_RES_DEPRIVE
715  *                                      ICO_SYC_EV_RES_WAITING
716  *                                      ICO_SYC_EV_RES_REVERT
717  *                                      ICO_SYC_EV_RES_RELEASE)
718  *
719  * @param[in]   callback                callback function
720  * @param[in]   user_data               pased data on called callback function
721  * @param[in]   event                   event code
722  * @param[in]   data                    message data
723  * @param[in]   len                     length of data
724  * @return      none
725  */
726 /*--------------------------------------------------------------------------*/
727 void
728 ico_syc_cb_res(ico_syc_callback_t callback, void *user_data,
729                int event, const void *data, size_t len)
730 {
731     JsonParser *parser  = NULL;
732     GError *error       = NULL;
733     gboolean gbool      = FALSE;
734     JsonNode *root      = NULL;
735     JsonObject *obj     = NULL;
736     JsonObject *resobj  = NULL;
737     JsonObject *info    = NULL;
738
739     ico_syc_res_info_t *res_info    = NULL;
740     char *zone, *name, *id;
741     int adjust, input_ev;
742     char *ECU, *display, *layer, *layout, *area, *dispatchApp, *role;
743     uint32_t resourceId;
744
745
746     /* alloc memory */
747     res_info = calloc(1, sizeof(ico_syc_res_info_t));
748     if (res_info == NULL) {
749         _ERR("calloc failed");
750         return;
751     }
752
753     /* start parser */
754     parser = json_parser_new();
755     gbool = json_parser_load_from_data(parser, data, len, &error);
756     if (gbool == FALSE) {
757         g_object_unref(parser);
758         free(res_info);
759         _ERR("json_parser_load_from_data failed");
760         return;
761     }
762
763     /* get root node */
764     root = json_parser_get_root(parser);
765     if (root == NULL) {
766         g_object_unref(parser);
767         free(res_info);
768         _ERR("json_parser_get_root failed (root is NULL)");
769         return;
770     }
771
772     /* get object from root */
773     obj = json_node_get_object(root);
774     /* check object */
775     if (json_object_has_member(obj, MSG_PRMKEY_RES) == FALSE) {
776         _ERR("invalid message");
777         return;
778     }
779     resobj = json_object_get_object_member(obj, MSG_PRMKEY_RES);
780
781     /* window resource */
782     if (json_object_has_member(resobj, MSG_PRMKEY_RES_WINDOW) == TRUE) {
783         info = json_object_get_object_member(resobj, MSG_PRMKEY_RES_WINDOW);
784         ECU         = ico_syc_get_str_member(info, MSG_PRMKEY_ECU);
785         display     = ico_syc_get_str_member(info, MSG_PRMKEY_DISPLAY);
786         layer       = ico_syc_get_str_member(info, MSG_PRMKEY_LAYER);
787         layout      = ico_syc_get_str_member(info, MSG_PRMKEY_LAYOUT);
788         area        = ico_syc_get_str_member(info, MSG_PRMKEY_AREA);
789         dispatchApp = ico_syc_get_str_member(info, MSG_PRMKEY_DISPATCHAPP);
790         role        = ico_syc_get_str_member(info, MSG_PRMKEY_ROLE);
791         resourceId  = (uint32_t)
792                       ico_syc_get_int_member(info, MSG_PRMKEY_RESOURCEID);
793         res_info->window = _create_res_window(ECU, display, layer, layout,
794                                               area, dispatchApp, role,
795                                               resourceId);
796     }
797
798     /* sound resource */
799     if (json_object_has_member(resobj, MSG_PRMKEY_RES_SOUND) == TRUE) {
800         info = json_object_get_object_member(resobj, MSG_PRMKEY_RES_SOUND);
801         zone = ico_syc_get_str_member(info, MSG_PRMKEY_RES_ZONE);
802         name = ico_syc_get_str_member(info, MSG_PRMKEY_RES_NAME);
803         id = ico_syc_get_str_member(info, MSG_PRMKEY_RES_ID);
804         adjust = ico_syc_get_int_member(info, MSG_PRMKEY_RES_ADJUST);
805
806         res_info->sound = _create_res_sound(zone, name, id, adjust);
807     }
808
809     /* input resource */
810     if (json_object_has_member(resobj, MSG_PRMKEY_RES_INPUT) == TRUE) {
811         info = json_object_get_object_member(resobj, MSG_PRMKEY_RES_INPUT);
812         name = ico_syc_get_str_member(info, MSG_PRMKEY_RES_NAME);
813         input_ev = ico_syc_get_int_member(info, MSG_PRMKEY_RES_EV);
814
815         res_info->input = _create_res_input(name, input_ev);
816     }
817
818     /* exec callback */
819     callback(event, res_info, user_data);
820
821     /* free memory */
822     g_object_unref(parser);
823     _free_res_window(res_info->window);
824     _free_res_sound(res_info->sound);
825     _free_res_input(res_info->input);
826     free(res_info);
827
828     return;
829 }
830
831 /*--------------------------------------------------------------------------*/
832 /**
833  * @internal
834  * @brief   ico_syc_cb_region
835  *          Execute callback function.  (ICO_SYC_EV_INPUT_SET
836  *                                       ICO_SYC_EV_INPUT_UNSET)
837  *
838  * @param[in]   callback                callback function
839  * @param[in]   user_data               pased data on called callback function
840  * @param[in]   event                   event code
841  * @param[in]   data                    message data
842  * @param[in]   len                     length of data
843  * @return      none
844  */
845 /*--------------------------------------------------------------------------*/
846 void
847 ico_syc_cb_region(ico_syc_callback_t callback, void *user_data,
848                   int event, const void *data, size_t len)
849 {
850     JsonParser *parser  = NULL;
851     GError *error       = NULL;
852     gboolean gbool      = FALSE;
853     JsonNode *root      = NULL;
854     JsonObject *obj     = NULL;
855     JsonObject *resobj  = NULL;
856
857     ico_syc_input_region_t *region  = NULL;
858
859     /* alloc memory */
860     region = calloc(1, sizeof(ico_syc_input_region_t));
861     if (region == NULL) {
862         _ERR("calloc failed");
863         return;
864     }
865
866     /* start parser */
867     parser = json_parser_new();
868     gbool = json_parser_load_from_data(parser, data, len, &error);
869     if (gbool == FALSE) {
870         g_object_unref(parser);
871         free(region);
872         _ERR("json_parser_load_from_data failed");
873         return;
874     }
875
876     /* get root node */
877     root = json_parser_get_root(parser);
878     if (root == NULL) {
879         g_object_unref(parser);
880         free(region);
881         _ERR("json_parser_get_root failed (root is NULL)");
882         return;
883     }
884
885     /* get object from root */
886     obj = json_node_get_object(root);
887     /* check object */
888     if (json_object_has_member(obj, MSG_PRMKEY_REGION) == FALSE) {
889         _ERR("invalid message");
890         return;
891     }
892     resobj = json_object_get_object_member(obj, MSG_PRMKEY_REGION);
893
894     /* get input region information */
895     char *p = ico_syc_get_str_member(resobj, MSG_PRMKEY_WINNAME);
896     if (p)  {
897         strncpy(region->winname, p, ICO_SYC_MAX_WINNAME_LEN-1);
898     }
899     region->pos_x = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_POS_X);
900     region->pos_y = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_POS_Y);
901     region->width = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_WIDTH);
902     region->height = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_HEIGHT);
903     region->hotspot_x = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_HOT_X);
904     region->hotspot_y = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_HOT_Y);
905     region->cursor_x = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_CUR_X);
906     region->cursor_y = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_CUR_Y);
907     region->cursor_width = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_CUR_WIDTH);
908     region->cursor_height = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_CUR_HEIGHT);
909     region->attr = ico_syc_get_int_member(resobj, MSG_PRMKEY_RES_ATTR);
910
911     /* exec callback */
912     callback(event, region, user_data);
913
914     /* free memory */
915     g_object_unref(parser);
916     free(region);
917
918     return;
919 }
920
921 /*============================================================================*/
922 /* public interface function                                                  */
923 /*============================================================================*/
924 /*--------------------------------------------------------------------------*/
925 /**
926  * @brief   ico_syc_acquire_res
927  *          Acquire the resources(window/sound/input).
928  *          User can choose window and sound resource's type
929  *          "basic" or "interruption".
930  *          If user want to acquire the sound resource only,
931  *          user sets NULL to argument "window" and  "input".
932  *
933  * @param[in]   window                  window resource's information
934  * @param[in]   sound                   sound resource's information
935  * @param[in]   input                   input resource's information
936  * @param[in]   type                    window and sound resource's type
937  *                                      (basic or interruption)
938  * @return      resource's context address
939  * @retval      address                 success
940  * @retval      NULL                    error
941  * @see         ico_syc_res_type_e
942  */
943 /*--------------------------------------------------------------------------*/
944 ICO_API struct ico_syc_res_context *
945 ico_syc_acquire_res(const ico_syc_res_window_t *window,
946                     const ico_syc_res_sound_t *sound,
947                     const ico_syc_res_input_t *input,
948                     int type)
949 {
950     msg_t msg;
951     char *appid;
952     struct ico_syc_res_context *context = NULL;
953
954     /* check argument */
955     if (window == NULL && sound == NULL && input == NULL) {
956         _ERR("invalid parameter (all resources is NULL)");
957         return NULL;
958     }
959
960     /* get appid */
961     appid = ico_syc_get_appid();
962
963     /* create context */
964     context = _create_context(appid, window, sound, input, type);
965     if (context == NULL) {
966         _ERR("context is NULL");
967         return NULL;
968     }
969
970     /* make message */
971     msg = _create_acquire_res_msg(context);
972     /* send message */
973     (void)ico_syc_send_msg(msg);
974     /* free send message */
975     ico_syc_free_msg(msg);
976
977     return context;
978 }
979
980 /*--------------------------------------------------------------------------*/
981 /**
982  * @brief   ico_syc_release_res
983  *          Release the resources(window/sound/input).
984  *
985  * @param[in]   context                 resource context
986  * @return      result
987  * @retval      0                       success
988  * @retval      not 0                   error
989  */
990 /*--------------------------------------------------------------------------*/
991 ICO_API int
992 ico_syc_release_res(struct ico_syc_res_context *context)
993 {
994     msg_t msg;
995
996     /* check argument */
997     if (context == NULL) {
998         _ERR("invalid parameter (context is NULL)");
999         return ICO_SYC_ERR_INVALID_PARAM;
1000     }
1001
1002     /* make message */
1003     msg = _create_release_res_msg(context);
1004     /* send message */
1005     (void)ico_syc_send_msg(msg);
1006     /* free send message */
1007     ico_syc_free_msg(msg);
1008
1009     /* free resource info */
1010     _free_res_window(context->window);
1011     _free_res_sound(context->sound);
1012     _free_res_input(context->input);
1013     /* free context */
1014     free(context);
1015
1016     return ICO_SYC_ERR_NONE;
1017 }
1018
1019 /*--------------------------------------------------------------------------*/
1020 /**
1021  * @brief   ico_syc_set_input_region
1022  *          Set the input region. (for haptic device)
1023  *          Callback function notifies the result of setting the input region.
1024  *
1025  * @param[in]   input                   input region's information
1026  * @param[in]   attr                    input region's attribute
1027  * @return      result
1028  * @retval      0                       success
1029  * @retval      not 0                   error
1030  */
1031 /*--------------------------------------------------------------------------*/
1032 ICO_API int
1033 ico_syc_set_input_region(const ico_syc_input_region_t *input, int attr)
1034 {
1035     msg_t msg;
1036     char *appid;
1037
1038     /* check argument */
1039     if (input == NULL) {
1040         _ERR("invalid parameter (ico_syc_input_region_t is NULL)");
1041         return ICO_SYC_ERR_INVALID_PARAM;
1042     }
1043
1044     /* get appid */
1045     appid = ico_syc_get_appid();
1046
1047     /* make message */
1048     msg = _create_set_region_msg(appid, input, attr);
1049     /* send message */
1050     (void)ico_syc_send_msg(msg);
1051     /* free send message */
1052     ico_syc_free_msg(msg);
1053
1054     return ICO_SYC_ERR_NONE;
1055 }
1056
1057 /*--------------------------------------------------------------------------*/
1058 /**
1059  * @brief   ico_syc_unset_input_region
1060  *          Unset the input region. (for haptic device)
1061  *          Callback function notifies the result of unsetting the input region.
1062  *
1063  * @param[in]   input                   input region's information
1064  * @return      result
1065  * @retval      0                       success
1066  * @retval      not 0                   error
1067  */
1068 /*--------------------------------------------------------------------------*/
1069 ICO_API int
1070 ico_syc_unset_input_region(const ico_syc_input_region_t *input)
1071 {
1072     msg_t msg;
1073     char *appid;
1074
1075     /* check argument */
1076     if (input == NULL) {
1077         _ERR("invalid parameter (ico_syc_input_region_t is NULL)");
1078         return ICO_SYC_ERR_INVALID_PARAM;
1079     }
1080
1081     /* get appid */
1082     appid = ico_syc_get_appid();
1083
1084     /* make message */
1085     msg = _create_unset_region_msg(appid, input);
1086     /* send message */
1087     (void)ico_syc_send_msg(msg);
1088     /* free send message */
1089     ico_syc_free_msg(msg);
1090
1091     return ICO_SYC_ERR_NONE;
1092 }
1093
1094 /* vim: set expandtab ts=4 sw=4: */