Bug Fix TIVI-840 and TIVI-987.
[profile/ivi/ico-uxf-homescreen.git] / ico-app-framework / ico_uxf_conf_sys.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   user experiance library(read configuration file in all system)
11  *
12  * @date    Feb-28-2013
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <sys/file.h>
23 #include <errno.h>
24
25 #include "ico_apf_log.h"
26 #include "ico_apf_apimacro.h"
27 #include "ico_uxf_conf_common.h"
28
29 static Ico_Uxf_Sys_Config   *reloadSysConfig(void);
30
31 static Ico_Uxf_Sys_Config   *_ico_sys_config = NULL;
32
33
34 /*--------------------------------------------------------------------------*/
35 /**
36  * @brief   ico_uxf_getSysConfig: get system configurations
37  *
38  * @param       none
39  * @return      system configuration table
40  * @retval      != NULL         success(system configuration table address)
41  * @retval      == NULL         error(can not read configuration file)
42  */
43 /*--------------------------------------------------------------------------*/
44 ICO_APF_API const Ico_Uxf_Sys_Config *
45 ico_uxf_getSysConfig(void)
46 {
47     if (_ico_sys_config) {
48         return _ico_sys_config;
49     }
50     _ico_sys_config = g_new0(Ico_Uxf_Sys_Config,1);
51     return reloadSysConfig();
52 }
53
54 /*--------------------------------------------------------------------------*/
55 /**
56  * @brief   ico_uxf_ifGetSysConfig: get system configurations
57  *
58  * @param       none
59  * @return      system configuration table
60  * @retval      != NULL         success(system configuration table address)
61  * @retval      == NULL         error(the configuration file is not yet read)
62  */
63 /*--------------------------------------------------------------------------*/
64 ICO_APF_API const Ico_Uxf_Sys_Config *
65 ico_uxf_ifGetSysConfig(void)
66 {
67     return _ico_sys_config;
68 }
69
70 /*--------------------------------------------------------------------------*/
71 /**
72  * @brief   pars_dispposition: calculate an expression about display size(static function)
73  *
74  * @param[in]   exp             expression
75  * @param[in]   display         display configuration table
76  * @return      calculation result
77  */
78 /*--------------------------------------------------------------------------*/
79 static int
80 pars_dispposition(const char *exp, Ico_Uxf_conf_display *display)
81 {
82     int     val = 0;
83     int     wval;
84     int     i, j, sign;
85     char    word[32];
86
87     j = 0;
88     sign = 0;
89     for (i = 0; ; i++)  {
90         if (exp[i] == ' ')  continue;
91         if ((exp[i] == 0) || (exp[i] == '+') || (exp[i] == '-') ||
92             (exp[i] == '*') || (exp[i] == '-') || (exp[i] == '/'))  {
93             if (j > 0)  {
94                 word[j] = 0;
95                 if ((strcasecmp(word, "dispw") == 0) ||
96                     (strcasecmp(word, "width") == 0))   {
97                     wval = display->width;
98                 }
99                 else if ((strcasecmp(word, "disph") == 0) ||
100                          (strcasecmp(word, "heigh") == 0))  {
101                     wval = display->height;
102                 }
103                 else    {
104                     wval = strtol(word, (char **)0, 0);
105                 }
106                 j = 0;
107                 if (sign >= 10) {
108                     wval = 0 - wval;
109                 }
110                 switch (sign % 10)  {
111                 case 0:                     /* assign       */
112                     val = wval;
113                     break;
114                 case 1:                     /* '+'          */
115                     val += wval;
116                     break;
117                 case 2:                     /* '-'          */
118                     val -= wval;
119                     break;
120                 case 3:                     /* '*'          */
121                     val *= wval;
122                     break;
123                 case 4:                     /* '/'          */
124                     val /= wval;
125                     break;
126                 default:
127                     break;
128                 }
129                 sign = 0;
130                 if (exp[i] == '+')      sign = 1;
131                 else if (exp[i] == '-') sign = 2;
132                 else if (exp[i] == '*') sign = 3;
133                 else if (exp[i] == '/') sign = 4;
134                 else                    sign = 0;
135             }
136             else    {
137                 if ((sign > 0) && (exp[i] == '-'))  {
138                     sign += 10;
139                 }
140                 else    {
141                     if (exp[i] == '+')      sign = 1;
142                     else if (exp[i] == '-') sign = 2;
143                     else if (exp[i] == '*') sign = 3;
144                     else if (exp[i] == '/') sign = 4;
145                     else                    sign = 0;
146                 }
147             }
148             if (exp[i] == 0)    break;
149         }
150         else    {
151             if (j < ((int)sizeof(word)-1))  {
152                 word[j++] = exp[i];
153             }
154         }
155     }
156     return val;
157 }
158
159 /*--------------------------------------------------------------------------*/
160 /**
161  * @brief   reloadSysConfig: load system configuration file(static function)
162  *
163  * @param       none
164  * @return      system configuration table
165  * @retval      != NULL         success(system configuration table address)
166  * @retval      == NULL         error(can not read configuration file)
167  */
168 /*--------------------------------------------------------------------------*/
169 static Ico_Uxf_Sys_Config *
170 reloadSysConfig(void)
171 {
172     GKeyFile *keyfile;
173     GKeyFileFlags flags;
174     GError *error = NULL;
175     gsize length;
176     int i, j, k;
177     int zoneidx;
178     GList*  idlist;
179     char*   dirp;
180     char*   strp;
181
182     apfw_trace("reloadSysConfig: Enter");
183
184     /* decide top directory in all configurations       */
185     dirp = getenv(ICO_UXF_TOP_ENV);
186     if (! dirp)    {
187         dirp = ICO_UXF_TOP_TOPDIR;
188     }
189     strncpy(_ico_sys_config->misc.topdir, dirp, ICO_UXF_CONF_DIR_MAX);
190
191     /* decide top directory in configuration file's     */
192     dirp = getenv(ICO_UXF_CONFIG_ENV);
193     if (dirp)  {
194         strncpy(_ico_sys_config->misc.confdir, dirp, ICO_UXF_CONF_DIR_MAX);
195     }
196     else    {
197         snprintf(_ico_sys_config->misc.confdir, ICO_UXF_CONF_DIR_MAX,
198                  "%s/%s", _ico_sys_config->misc.topdir, ICO_UXF_CONFIG_DIR);
199         dirp = _ico_sys_config->misc.confdir;
200     }
201
202     /* A system-wide definition         */
203     keyfile = g_key_file_new();
204     flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
205
206     GString* filepath = g_string_new("xx");
207     g_string_printf(filepath, "%s/%s", dirp, ICO_UXF_CONFIG_SYSTEM);
208
209     if (!g_key_file_load_from_file(keyfile, filepath->str, flags, &error)) {
210         fprintf(stderr, "%s: %s\n", (char *)filepath->str, error->message);
211         g_string_free(filepath, TRUE);
212         return NULL;
213     }
214     g_string_free(filepath, TRUE);
215
216     /* log                      */
217     _ico_sys_config->misc.loglevel
218              = g_key_file_get_integer(keyfile, "log", "loglevel", &error);
219     ico_uxf_conf_checkGerror(&error);
220     _ico_sys_config->misc.logflush =
221         ico_uxf_getSysBoolByName(g_key_file_get_string(keyfile, "log", "logflush", &error));
222     ico_uxf_conf_checkGerror(&error);
223
224     /* log setting              */
225     if (getenv("ICO_IVI_NO_LOG"))    {
226         ico_uxf_log_setlevel(0);
227     }
228     else    {
229         ico_uxf_log_setlevel(_ico_sys_config->misc.loglevel |
230                              ((_ico_sys_config->misc.logflush) ?
231                                 ICO_APF_LOG_FLUSH : ICO_APF_LOG_NOFLUSH));
232     }
233
234     /* [category] application category name and attributes          */
235     idlist = ico_uxf_conf_countNumericalKey(keyfile, "category");
236     length = g_list_length(idlist);
237     _ico_sys_config->categoryNum = length;
238     _ico_sys_config->category = g_malloc0(sizeof(Ico_Uxf_conf_category) * length);
239
240     for (i = 0; i < (int)length; i++) {
241         const char* key = (const char*) g_list_nth_data(idlist, i);
242         const char* g = "category";
243         _ico_sys_config->category[i].id = ico_uxf_conf_getUint(key);
244         ico_uxf_conf_checkGerror(&error);
245         _ico_sys_config->category[i].name = g_key_file_get_string(keyfile, g, key, &error);
246         ico_uxf_conf_checkGerror(&error);
247
248         strp = (char *)g_key_file_get_string(keyfile, g,
249                                              ico_uxf_conf_appendStr(key, ".view"), &error);
250         ico_uxf_conf_checkGerror(&error);
251         if (strncasecmp(strp, "alway", 5) == 0) {
252             _ico_sys_config->category[i].view = ICO_UXF_POLICY_ALWAYS;
253         }
254         else if (strncasecmp(strp, "run", 3) == 0)  {
255             _ico_sys_config->category[i].view = ICO_UXF_POLICY_RUNNING;
256         }
257         else if (strncasecmp(strp, "park", 4) == 0) {
258             _ico_sys_config->category[i].view = ICO_UXF_POLICY_PARKED;
259         }
260         else if (strncasecmp(strp, "shift_park", 10) == 0)  {
261             _ico_sys_config->category[i].view = ICO_UXF_POLICY_SHIFT_PARKING;
262         }
263         else if ((strncasecmp(strp, "shift_back", 10) == 0) ||
264                  (strncasecmp(strp, "shift_rev", 9) == 0))  {
265             _ico_sys_config->category[i].view = ICO_UXF_POLICY_SHIFT_REVERSES;
266         }
267         else if (strcasecmp(strp, "blinker_left") == 0)   {
268             _ico_sys_config->category[i].view = ICO_UXF_POLICY_BLINKER_LEFT;
269         }
270         else if (strcasecmp(strp, "blinker_right") == 0)  {
271             _ico_sys_config->category[i].view = ICO_UXF_POLICY_BLINKER_RIGHT;
272         }
273         else    {
274             fprintf(stderr, "[%s.view: %s] Unknown view mode\n", key, strp);
275             g_list_free_full(idlist,g_free);
276             g_key_file_free(keyfile);
277             return NULL;
278         }
279
280         strp = (char *)g_key_file_get_string(keyfile, g,
281                                              ico_uxf_conf_appendStr(key, ".sound"), &error);
282         ico_uxf_conf_checkGerror(&error);
283         if (strncasecmp(strp, "alway", 5) == 0) {
284             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_ALWAYS;
285         }
286         else if (strncasecmp(strp, "run", 3) == 0)  {
287             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_RUNNING;
288         }
289         else if (strncasecmp(strp, "park", 4) == 0) {
290             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_PARKED;
291         }
292         else if (strncasecmp(strp, "shift_park", 10) == 0)  {
293             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_SHIFT_PARKING;
294         }
295         else if ((strncasecmp(strp, "shift_back", 10) == 0) ||
296                  (strncasecmp(strp, "shift_rev", 9) == 0))  {
297             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_SHIFT_REVERSES;
298         }
299         else if (strcasecmp(strp, "blinker_left") == 0)   {
300             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_BLINKER_LEFT;
301         }
302         else if (strcasecmp(strp, "blinker_right") == 0)  {
303             _ico_sys_config->category[i].sound = ICO_UXF_POLICY_BLINKER_RIGHT;
304         }
305         else    {
306             fprintf(stderr, "[%s.view: %s] Unknown sound mode\n", key, strp);
307             g_list_free_full(idlist,g_free);
308             g_key_file_free(keyfile);
309             return NULL;
310         }
311
312         strp = (char *)g_key_file_get_string(keyfile, g,
313                                              ico_uxf_conf_appendStr(key, ".input"), &error);
314         ico_uxf_conf_checkGerror(&error);
315         if (strncasecmp(strp, "alway", 5) == 0) {
316             _ico_sys_config->category[i].input = ICO_UXF_POLICY_ALWAYS;
317         }
318         else if (strncasecmp(strp, "run", 3) == 0)  {
319             _ico_sys_config->category[i].input = ICO_UXF_POLICY_RUNNING;
320         }
321         else if (strncasecmp(strp, "park", 4) == 0) {
322             _ico_sys_config->category[i].input = ICO_UXF_POLICY_PARKED;
323         }
324         else if (strncasecmp(strp, "shift_park", 10) == 0)  {
325             _ico_sys_config->category[i].input = ICO_UXF_POLICY_SHIFT_PARKING;
326         }
327         else if ((strncasecmp(strp, "shift_back", 10) == 0) ||
328                  (strncasecmp(strp, "shift_rev", 9) == 0))  {
329             _ico_sys_config->category[i].input = ICO_UXF_POLICY_SHIFT_REVERSES;
330         }
331         else if (strcasecmp(strp, "blinker_left") == 0)   {
332             _ico_sys_config->category[i].input = ICO_UXF_POLICY_BLINKER_LEFT;
333         }
334         else if (strcasecmp(strp, "blinker_right") == 0)  {
335             _ico_sys_config->category[i].input = ICO_UXF_POLICY_BLINKER_RIGHT;
336         }
337         else    {
338             fprintf(stderr, "[%s.view: %s] Unknown input mode\n", key, strp);
339             g_list_free_full(idlist,g_free);
340             g_key_file_free(keyfile);
341             return NULL;
342         }
343         _ico_sys_config->category[i].priority =
344                                 g_key_file_get_integer(keyfile, g,
345                                 ico_uxf_conf_appendStr(key,".priority"), &error);
346         ico_uxf_conf_checkGerror(&error);
347
348         apfw_trace("Categry.%d id = %d name = %s view = %d sound = %d input = %d prio = %d",
349                    i, _ico_sys_config->category[i].id, _ico_sys_config->category[i].name,
350                    _ico_sys_config->category[i].view, _ico_sys_config->category[i].sound,
351                    _ico_sys_config->category[i].input,
352                    _ico_sys_config->category[i].priority);
353     }
354     g_list_free_full(idlist,g_free);
355
356     /* [kind] application kind name and attributes          */
357     idlist = ico_uxf_conf_countNumericalKey(keyfile, "kind");
358     length = g_list_length(idlist);
359     _ico_sys_config->kindNum = length;
360     _ico_sys_config->kind = g_malloc0(sizeof(Ico_Uxf_conf_kind) * length);
361
362     for (i = 0; i < (int)length; i++) {
363         const char* key = (const char*) g_list_nth_data(idlist, i);
364         const char* g = "kind";
365         _ico_sys_config->kind[i].id = ico_uxf_conf_getUint(key);
366         ico_uxf_conf_checkGerror(&error);
367         _ico_sys_config->kind[i].name = g_key_file_get_string(keyfile, g, key, &error);
368         ico_uxf_conf_checkGerror(&error);
369         strp = (char *)g_key_file_get_string(
370                             keyfile, g, ico_uxf_conf_appendStr(key, ".privilege"), &error);
371         ico_uxf_conf_checkGerror(&error);
372         if (strcasecmp(strp, "almighty") == 0)   {
373             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_ALMIGHTY;
374         }
375         else if (strcasecmp(strp, "system") == 0)   {
376             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_SYSTEM;
377         }
378         else if (strcasecmp(strp, "system.audio") == 0)   {
379             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_SYSTEM_AUDIO;
380         }
381         else if (strcasecmp(strp, "system.visible") == 0)   {
382             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_SYSTEM_VISIBLE;
383         }
384         else if (strcasecmp(strp, "maker") == 0)    {
385             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_MAKER;
386         }
387         else if (strcasecmp(strp, "certificate") == 0)  {
388             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_CERTIFICATE;
389         }
390         else if (strcasecmp(strp, "none") == 0) {
391             _ico_sys_config->kind[i].priv = ICO_UXF_PRIVILEGE_NONE;
392         }
393         else    {
394             fprintf(stderr, "[%s.view: %s] Unknown privilege mode\n", key, strp);
395             g_list_free_full(idlist,g_free);
396             g_key_file_free(keyfile);
397             return NULL;
398         }
399         apfw_trace("Kind.%d id = %d name = %s privilege = %d",
400                    i, _ico_sys_config->kind[i].id, _ico_sys_config->kind[i].name,
401                    _ico_sys_config->kind[i].priv);
402     }
403     g_list_free_full(idlist,g_free);
404
405     /* [port] port numbers              */
406     _ico_sys_config->misc.appscontrollerport
407         = g_key_file_get_integer(keyfile, "port", "appscontrollerport", &error);
408     ico_uxf_conf_checkGerror(&error);
409     _ico_sys_config->misc.soundpluginport
410         = g_key_file_get_integer(keyfile, "port", "soundpluginport", &error);
411     ico_uxf_conf_checkGerror(&error);
412     if (_ico_sys_config->misc.appscontrollerport <= 0)
413         _ico_sys_config->misc.appscontrollerport = 18081;
414     if (_ico_sys_config->misc.soundpluginport <= 0)
415         _ico_sys_config->misc.soundpluginport = 18082;
416
417     /* hosts                        */
418     idlist = ico_uxf_conf_countNumericalKey(keyfile, "host");
419     length = g_list_length(idlist);
420     _ico_sys_config->hostNum = length;
421     _ico_sys_config->host = g_malloc0(sizeof(Ico_Uxf_conf_host) * length);
422
423     for (i = 0; i < (int)length; i++)   {
424         char* key = (char*) g_list_nth_data(idlist, i);
425         const char* g = "host";
426         _ico_sys_config->host[i].id  = ico_uxf_conf_getUint(key);
427         _ico_sys_config->host[i].name = g_key_file_get_string(keyfile, g, key, &error);
428         ico_uxf_conf_checkGerror(&error);
429
430         _ico_sys_config->host[i].address
431             = g_key_file_get_string(keyfile, g, ico_uxf_conf_appendStr(key, ".ip"), &error);
432         ico_uxf_conf_checkGerror(&error);
433         if (g_hostname_is_ip_address(_ico_sys_config->host[i].address)!= TRUE){
434             apfw_error("host[%d] = %s is not ip address style",
435                         _ico_sys_config->host[i].id, _ico_sys_config->host[i].address);
436             exit(1);
437         }
438
439         _ico_sys_config->host[i].type =
440             ico_uxf_getSysHostTypeByName(
441                     g_key_file_get_string(keyfile, g,
442                                           ico_uxf_conf_appendStr(key, ".type"), &error));
443         ico_uxf_conf_checkGerror(&error);
444
445         _ico_sys_config->host[i].homescreen =
446             g_key_file_get_string(keyfile, g,
447                                   ico_uxf_conf_appendStr(key, ".homescreen"), &error);
448         ico_uxf_conf_checkGerror(&error);
449         apfw_trace("Host.%d: Id = %d Name = %s Type = %d Ip = %s HomeScreen = %s",
450             i, _ico_sys_config->host[i].id, (char *)_ico_sys_config->host[i].name,
451             _ico_sys_config->host[i].type, (char *)_ico_sys_config->host[i].address,
452             _ico_sys_config->host[i].homescreen);
453     }
454     g_list_free_full(idlist,g_free);
455
456     /* decide myhost        */
457     const char *penv = getenv(ICO_UXF_ENV_APPINF);
458     int hostid = -1;
459     int dmy1, dmy2, dmy3;
460     char cdmy4[64];
461     if (penv)  {
462         if (sscanf(penv, "%d %d %d %s %d", &dmy1, &dmy2, &dmy3, cdmy4, &hostid) < 5) {
463             hostid = -1;
464         }
465         apfw_trace("reloadSysConfig: Env(%s) = %s, hostId = %d",
466                     ICO_UXF_ENV_APPINF, penv, hostid);
467     }
468
469     if (hostid < 0)    {
470         if ((gethostname(cdmy4, sizeof(cdmy4)-1) >= 0) &&
471             (strncmp(cdmy4, "localhost", 9) != 0))   {
472             apfw_trace("hostname = %s", cdmy4);
473             for (j = 0; cdmy4[j]; j++) {
474                 if ((cdmy4[j] == ' ') || (cdmy4[j] == '(') ||
475                     (cdmy4[j] == '@') || (cdmy4[j] == ':'))    {
476                     cdmy4[j] = 0;
477                     break;
478                 }
479             }
480             for (j = 0; j < _ico_sys_config->hostNum; j++) {
481                 if (strcasecmp(_ico_sys_config->host[j].name, cdmy4) == 0)   {
482                     hostid = _ico_sys_config->host[j].id;
483                     break;
484                 }
485             }
486         }
487     }
488
489     if (hostid < 0)    {
490         FILE *fp = popen("/sbin/ifconfig", "r");
491         int foundeth = 0;
492         char sBuf[128];
493
494         if (fp)    {
495             while (fgets(sBuf, sizeof(sBuf)-1, fp))  {
496                 if ((! foundeth) || (sBuf[0] != ' '))  {
497                     foundeth = 0;
498                     /* skip to "eth" device     */
499                     if (sBuf[0] == ' ' || sBuf[0] == '\t') continue;
500                     if (strncmp(sBuf, "eth", 3) != 0)    continue;
501                 }
502                 foundeth = 1;
503
504                 for (j = 0; sBuf[j]; j++)  {
505                     if (strncmp(&sBuf[j], "inet6", 5) == 0)  continue;
506                     if (strncmp(&sBuf[j], "inet", 4) == 0)   break;
507                 }
508                 if (sBuf[j])   {
509                     /* forget "inet" device         */
510                     j += 4;
511                     for (; sBuf[j]; j++)   {
512                         if (sBuf[j] == ':')    break;
513                     }
514                     if (sBuf[j] )  {
515                         j++;
516                         int k;
517                         for (k = j +1; sBuf[k]; k++)   {
518                             if (sBuf[k] == ' ' || sBuf[k] == '\t' ||
519                                 sBuf[k] == '\n' || sBuf[k] == '\r')    {
520                                 sBuf[k] = 0;
521                                 break;
522                             }
523                         }
524                         apfw_trace("My Inet Addr = %s", &sBuf[j]);
525                         for (k = 0; k < _ico_sys_config->hostNum; k++) {
526                             if (strcmp(_ico_sys_config->host[k].address, &sBuf[j]) == 0) {
527                                 hostid = _ico_sys_config->host[k].id;
528                                 break;
529                             }
530                         }
531                     }
532                 }
533                 if (hostid >= 0)   break;
534             }
535             fclose(fp);
536         }
537     }
538     if (hostid >= 0)   {
539         _ico_sys_config->misc.myhostId = hostid;
540     }
541     else    {
542         apfw_trace("Default Host = 0, %s", ico_uxf_get_SysLocation(0));
543         _ico_sys_config->misc.myhostId = _ico_sys_config->host[0].id;
544     }
545
546     /* The validity of MyHost is checked.       */
547     Ico_Uxf_conf_host *myhost = (Ico_Uxf_conf_host *)
548             ico_uxf_getSysHostById(_ico_sys_config->misc.myhostId);
549     if (! myhost)  {
550         apfw_critical("MyHostId.%d dose not exist, default 0",
551                       _ico_sys_config->misc.myhostId);
552         myhost = &_ico_sys_config->host[0];
553         if (! myhost)  {
554             apfw_error("HostId.0 dose not exist, GiveUp!");
555             exit(1);
556         }
557         _ico_sys_config->misc.myhostId = myhost->id;
558     }
559
560     _ico_sys_config->misc.myaddress =
561             (char *)ico_uxf_get_SysLocation(_ico_sys_config->misc.myhostId);
562     _ico_sys_config->misc.myhostName = myhost->name;
563
564     _ico_sys_config->misc.homescreen = myhost->homescreen;
565
566     if (myhost->homescreen)    {
567         _ico_sys_config->misc.masterhostId = myhost->id;
568     }
569     else    {
570         _ico_sys_config->misc.masterhostId = _ico_sys_config->misc.myhostId;
571
572         for (i = 0; i < _ico_sys_config->hostNum; i++) {
573             if (_ico_sys_config->host[i].homescreen)   {
574                 _ico_sys_config->misc.masterhostId = _ico_sys_config->host[i].id;
575                 break;
576             }
577         }
578     }
579     _ico_sys_config->misc.masterhostaddress =
580             (char *)ico_uxf_get_SysLocation(_ico_sys_config->misc.masterhostId);
581
582     apfw_trace("MyHost %d[%s] Master Host %d(%s)", _ico_sys_config->misc.myhostId,
583                 _ico_sys_config->misc.myaddress, _ico_sys_config->misc.masterhostId,
584                 _ico_sys_config->misc.masterhostaddress);
585
586     _ico_sys_config->misc.default_hostId = ico_uxf_getSysHostByName(
587                             g_key_file_get_string(keyfile, "default", "host", &error));
588     ico_uxf_conf_checkGerror(&error);
589
590     /* display                  */
591     idlist = ico_uxf_conf_countNumericalKey(keyfile, "display");
592     length = g_list_length(idlist);
593     _ico_sys_config->displayNum = length;
594     _ico_sys_config->display = g_malloc0(sizeof(Ico_Uxf_conf_display) * length);
595     zoneidx = 0;
596
597     for (i = 0; i < (int)length; i++) {
598         const char* key = (const char*) g_list_nth_data(idlist, i);
599         const char* g = "display";
600         Ico_Uxf_conf_display *display = &_ico_sys_config->display[i];
601         display->id = ico_uxf_conf_getUint(key);
602         ico_uxf_conf_checkGerror(&error);
603         display->name = g_key_file_get_string(keyfile, g, key, &error);
604         ico_uxf_conf_checkGerror(&error);
605
606         display->hostId =
607             ico_uxf_getSysHostByName(
608                         g_key_file_get_string(keyfile, g,
609                                               ico_uxf_conf_appendStr(key,".host"), &error));
610         ico_uxf_conf_checkGerror(&error);
611         display->displayno =
612             g_key_file_get_integer(keyfile, g,
613                                    ico_uxf_conf_appendStr(key,".displayno"), &error);
614         if (error) {
615             display->displayno = -1;
616             g_clear_error(&error);
617         }
618         display->wayland =
619             g_key_file_get_string(keyfile, g,
620                                   ico_uxf_conf_appendStr(key,".wayland"), &error);
621         ico_uxf_conf_checkGerror(&error);
622
623         display->type =
624             ico_uxf_getSysDispTypeByName(g_key_file_get_string(keyfile, g,
625                                          ico_uxf_conf_appendStr(key,".type"), &error));
626         ico_uxf_conf_checkGerror(&error);
627
628         display->width =
629             g_key_file_get_integer(keyfile, g, ico_uxf_conf_appendStr(key,".width"), &error);
630         ico_uxf_conf_checkGerror(&error);
631         display->height =
632             g_key_file_get_integer(keyfile, g, ico_uxf_conf_appendStr(key,".height"), &error);
633         ico_uxf_conf_checkGerror(&error);
634         display->layerNum =
635             g_key_file_get_integer(keyfile, g, ico_uxf_conf_appendStr(key,".layer"), &error);
636         ico_uxf_conf_checkGerror(&error);
637
638         /* inch                     */
639         display->inch =
640             g_key_file_get_integer(keyfile,g,ico_uxf_conf_appendStr(key,".inch"),&error);
641         ico_uxf_conf_checkGerror(&error);
642
643         apfw_trace("Display.%d Id = %d Name = %s Host = %d DispNo = %d W/H = %d/%d Wayland = %s inch = %d",
644                    i, display->id, (char *)display->name, display->hostId,
645                    display->displayno, display->width, display->height,
646                    display->wayland, display->inch);
647
648         /* display layer            */
649         int displayer_length;
650         gchar **layerlist;
651         gsize layersize;
652         GString* layer_key = g_string_new("");
653         for (displayer_length = 0;
654              displayer_length < ICO_UXF_DISPLAY_LAYER_MAX; displayer_length++)  {
655             g_string_printf(layer_key,"%s.layer.%d",key,displayer_length);
656             if (g_key_file_has_key(keyfile,g,layer_key->str,NULL) != TRUE)   break;
657         }
658         apfw_trace("Display.%d layerNum = %d" ,i, displayer_length);
659         display->layerNum = displayer_length;
660         display->layer = g_malloc0(sizeof(Ico_Uxf_conf_display_layer)*displayer_length);
661
662         for (j = 0; j < displayer_length; j++)  {
663             g_string_printf(layer_key,"%s.layer.%d",key,j);
664             display->layer[j].id = j;
665             layerlist = g_key_file_get_string_list(keyfile, g, layer_key->str,
666                                                    &layersize, &error);
667             ico_uxf_conf_checkGerror(&error);
668             if (layersize <= 0) {
669                 apfw_error("Display.%d Layer.%d has no name", i, j);
670             }
671             else    {
672                 display->layer[j].name = strdup((char *)layerlist[0]);
673                 for (k = 1; k < (int)layersize; k++)    {
674                     if (strcasecmp((char *)layerlist[k], "menuoverlap") == 0)   {
675                         display->layer[j].menuoverlap = 1;
676                     }
677                 }
678                 apfw_trace("Display.%d Layer.%d id = %d name = %s overlap=%d", i, j,
679                            display->layer[j].id, display->layer[j].name,
680                            display->layer[j].menuoverlap);
681             }
682             if (layerlist)  g_strfreev(layerlist);
683         }
684         g_string_free(layer_key,TRUE);
685
686         /* display zone         */
687         int dispzone_length;
688         GString* zone_key = g_string_new("");
689         for (dispzone_length = 0;
690              dispzone_length < ICO_UXF_DISPLAY_ZONE_MAX; dispzone_length++) {
691             g_string_printf(zone_key,"%s.zone.%d",key,dispzone_length);
692             if (g_key_file_has_key(keyfile,g,zone_key->str,NULL) != TRUE)    break;
693         }
694         apfw_trace("Display.%d zoneNum = %d" ,i, dispzone_length);
695         display->zoneNum = dispzone_length;
696         display->zone = g_malloc0(sizeof(Ico_Uxf_conf_display_zone)*dispzone_length);
697
698         gsize zonesizes[ICO_UXF_DISPLAY_ZONE_MAX];
699         gchar **zonelists[ICO_UXF_DISPLAY_ZONE_MAX];
700         gchar **zonelist;
701         Ico_Uxf_conf_display_zone *zone;
702
703         for (j = 0; j < dispzone_length; j++)   {
704             g_string_printf(zone_key,"%s.zone.%d",key,j);
705             zonelists[j] = g_key_file_get_string_list(
706                                     keyfile, g, zone_key->str, &zonesizes[j], &error);
707             ico_uxf_conf_checkGerror(&error);
708             if ((! zonelists[j]) || (zonesizes[j] <= 0)) {
709                 apfw_error("reloadSysConfig: display.%d zone.%d dose not exist",i,j);
710                 zonesizes[j] = 0;
711             }
712         }
713         for (j = 0; j < dispzone_length; j++)   {
714             zone = &(display->zone[j]);
715             zone->id = j;
716             zone->zoneidx = zoneidx++;
717             zone->display = display;
718
719             if (zonesizes[j] <= 0)  continue;
720
721             zonelist = zonelists[j];
722             zone->name = strdup((char*)zonelist[0]);
723             if (zonesizes[j] < 5)   {
724                 /* default zone is full screen      */
725                 zone->x = 0;
726                 zone->y = 0;
727                 zone->width = display->width;
728                 zone->height = display->height;
729             }
730             else    {
731                 zone->x = pars_dispposition((char *)zonelist[1], display);
732                 zone->y = pars_dispposition((char *)zonelist[2], display);
733                 zone->width = pars_dispposition((char *)zonelist[3], display);
734                 zone->height = pars_dispposition((char *)zonelist[4], display);
735             }
736         }
737         for (j = 0; j < dispzone_length; j++)   {
738             int k, l, m, n;
739             if (zonesizes[j] <= 5)  continue;
740             zonelist = zonelists[j];
741             zone = &(display->zone[j]);
742             m = 0;
743             for (k = 5; (m < ZONE_MAX_OVERLAP) && (k < (int)zonesizes[j]); k++) {
744                 for (l = 0; l < dispzone_length; l++)   {
745                     if (l == j) continue;
746                     if (strcasecmp(zonelist[k], display->zone[l].name) == 0)    {
747                         for (n = 0; n < m; n++) {
748                             if (zone->overlap[n] == display->zone[l].id)    break;
749                         }
750                         if (n >= m) {
751                             zone->overlap[m++] = display->zone[l].id;
752                         }
753                         break;
754                     }
755                 }
756                 if (l >= dispzone_length)   {
757                     apfw_error("reloadSysConfig: disp.%d zone.%d overlap zone(%s) "
758                                "dose not exist", i, j, (char *)zonelist[k]);
759                 }
760             }
761             zone->overlapNum = m;
762         }
763         for (j = 0; j < dispzone_length; j++)   {
764             if (zonelists[j])   g_strfreev(zonelists[j]);
765             zone = &(display->zone[j]);
766             apfw_trace("Disp.%d zone.%d x/y = %d/%d w/h = %d/%d over = %d.%d %d %d %d %d",
767                        i, j, zone->x, zone->y, zone->width, zone->height,
768                        zone->overlapNum, zone->overlap[0], zone->overlap[1],
769                        zone->overlap[2], zone->overlap[3], zone->overlap[4]);
770         }
771         g_string_free(zone_key,TRUE);
772     }
773     g_list_free_full(idlist,g_free);
774
775     /* sound devices                        */
776     idlist = ico_uxf_conf_countNumericalKey(keyfile, "sound");
777     length = g_list_length(idlist);
778     _ico_sys_config->soundNum = length;
779     _ico_sys_config->sound = g_malloc0(sizeof(Ico_Uxf_conf_sound) * length);
780     zoneidx = 0;
781
782     for (i = 0; i < (int)length; i++) {
783         const char* key = (const char*) g_list_nth_data(idlist, i);
784         const char* g = "sound";
785         Ico_Uxf_conf_sound *sound = &_ico_sys_config->sound[i];
786         sound->id = ico_uxf_conf_getUint(key);
787         ico_uxf_conf_checkGerror(&error);
788         sound->name = g_key_file_get_string(keyfile, g, key, &error);
789         ico_uxf_conf_checkGerror(&error);
790
791         sound->hostId =
792             ico_uxf_getSysHostByName(g_key_file_get_string(
793                                     keyfile, g, ico_uxf_conf_appendStr(key,".host"), &error));
794         ico_uxf_conf_checkGerror(&error);
795         sound->soundno =
796             g_key_file_get_integer(keyfile, g,
797                                    ico_uxf_conf_appendStr(key,".soundno"), &error);
798         if (error) {
799             sound->soundno = -1;
800             g_clear_error(&error);
801         }
802
803         apfw_trace("Sound.%d Id = %d Name = %s Host = %d SoundNo = %d",
804                    i, sound->id, (char *)sound->name, sound->hostId, sound->soundno);
805
806         /* sound zone           */
807         int sound_length;
808         GString* sound_key = g_string_new("");
809         for (sound_length = 0;
810              sound_length < ICO_UXF_SOUND_ZONE_MAX; sound_length++) {
811             g_string_printf(sound_key,"%s.zone.%d", key, sound_length);
812             if (g_key_file_has_key(keyfile,g,sound_key->str,NULL) != TRUE)   break;
813         }
814         apfw_trace("Sound.%d zoneNum = %d" ,i, sound_length);
815         sound->zoneNum = sound_length;
816         sound->zone = g_malloc0(sizeof(Ico_Uxf_conf_sound_zone)*sound_length);
817
818         gsize zonesizes[ICO_UXF_SOUND_ZONE_MAX];
819         gchar **zonelists[ICO_UXF_SOUND_ZONE_MAX];
820         gchar **zonelist;
821         Ico_Uxf_conf_sound_zone *zone;
822
823         for (j = 0; j < sound_length; j++)  {
824             g_string_printf(sound_key,"%s.zone.%d",key,j);
825             zonelists[j] =
826                 g_key_file_get_string_list(keyfile,g,sound_key->str,&zonesizes[j],&error);
827             ico_uxf_conf_checkGerror(&error);
828             if ((! zonelists[j]) || (zonesizes[j] <= 0)) {
829                 apfw_error("reloadSysConfig: sound.%d zone.%d dose not exist",i,j);
830                 zonesizes[j] = 0;
831             }
832         }
833         for (j = 0; j < sound_length; j++)  {
834             zone = &(sound->zone[j]);
835             zone->id = j;
836             zone->zoneidx = zoneidx++;
837             zone->sound = sound;
838             if (zonesizes[j] <= 0)  continue;
839
840             zonelist = zonelists[j];
841             zone->name = strdup((char*)zonelist[0]);
842         }
843         for (j = 0; j < sound_length; j++)  {
844             int k, l, m, n;
845             if (zonesizes[j] <= 1)  continue;
846             zonelist = zonelists[j];
847             zone = &(sound->zone[j]);
848             m = 0;
849             for (k = 1; (m < ZONE_MAX_OVERLAP) && (k < (int)zonesizes[j]); k++) {
850                 for (l = 0; l < sound_length; l++)  {
851                     if (l == j) continue;
852                     if (strcasecmp((char*)zonelist[k], sound->zone[l].name) == 0)   {
853                         for (n = 0; n < m; n++) {
854                             if (zone->overlap[n] == sound->zone[l].id)  break;
855                         }
856                         if (n >= m) {
857                             zone->overlap[m++] = sound->zone[l].id;
858                         }
859                         break;
860                     }
861                 }
862                 if (l >= sound_length)  {
863                     apfw_error("reloadSysConfig: sound.%d zone.%d overlap zone(%s) "
864                                "dose not exist", i, j, (char *)zonelist[k]);
865                 }
866             }
867             zone->overlapNum = m;
868         }
869         for (j = 0; j < sound_length; j++)  {
870             if (zonelists[j])   g_strfreev(zonelists[j]);
871             zone = &(sound->zone[j]);
872             apfw_trace("Sound.%d zone.%d(%s) over = %d.%d %d %d %d %d",
873                        i, j, zone->name,
874                        zone->overlapNum, zone->overlap[0], zone->overlap[1],
875                        zone->overlap[2], zone->overlap[3], zone->overlap[4]);
876         }
877         g_string_free(sound_key,TRUE);
878     }
879     g_list_free_full(idlist,g_free);
880
881     /* input devices                        */
882     idlist = ico_uxf_conf_countNumericalKey(keyfile, "input");
883     length = g_list_length(idlist);
884     _ico_sys_config->inputdevNum = length;
885     _ico_sys_config->inputdev = g_malloc0(sizeof(Ico_Uxf_conf_inputdev) * length);
886
887     for (i = 0; i < (int)length; i++) {
888         const char* key = (const char*) g_list_nth_data(idlist, i);
889         const char* g = "input";
890         Ico_Uxf_conf_inputdev *inputdev = &_ico_sys_config->inputdev[i];
891         inputdev->id = ico_uxf_conf_getUint(key);
892         ico_uxf_conf_checkGerror(&error);
893         inputdev->name = g_key_file_get_string(keyfile, g, key, &error);
894         ico_uxf_conf_checkGerror(&error);
895
896         /* input sw             */
897         int sw_length;
898         GString* sw_key = g_string_new("");
899         for (sw_length = 0;; sw_length++)   {
900             g_string_printf(sw_key,"%s.sw.%d",key,sw_length);
901             if (g_key_file_has_key(keyfile,g,sw_key->str,NULL) != TRUE)  break;
902         }
903         apfw_trace("Input.%d swNum = %d" ,i, sw_length);
904         inputdev->inputswNum = sw_length;
905         inputdev->inputsw = g_malloc0(sizeof(Ico_Uxf_conf_inputsw)*sw_length);
906
907         /* input sw         */
908         gchar **swlist;
909         gsize   swsize;
910         for (j = 0; j < sw_length; j++) {
911             g_string_printf(sw_key,"%s.sw.%d",key,j);
912             swlist = g_key_file_get_string_list(keyfile,g,sw_key->str,&swsize,&error);
913             ico_uxf_conf_checkGerror(&error);
914             if ((!swlist) || (swsize <= 0))  {
915                 apfw_error("reloadSysConfig: input.%d sw.%d dose not exist",i,j);
916                 swsize = 0;
917             }
918             else    {
919                 inputdev->inputsw[j].inputdev = inputdev;
920                 inputdev->inputsw[j].name = strdup((char *)swlist[0]);
921                 if (swsize > 1) {
922                     inputdev->inputsw[j].appid = strdup((char *)swlist[1]);
923                 }
924             }
925             g_strfreev(swlist);
926             uifw_trace("reloadSysConfig: input %s.%s app = %s",
927                        inputdev->name,
928                        inputdev->inputsw[j].name ? inputdev->inputsw[j].name : " ",
929                        inputdev->inputsw[j].appid ? inputdev->inputsw[j].appid : " ");
930         }
931         g_string_free(sw_key,TRUE);
932     }
933     g_list_free_full(idlist,g_free);
934
935     _ico_sys_config->misc.default_kindId = ico_uxf_getSysKindByName(
936                         g_key_file_get_string(keyfile, "default", "kind", &error));
937     ico_uxf_conf_checkGerror(&error);
938     _ico_sys_config->misc.default_categoryId = ico_uxf_getSysCategoryByName(
939                         g_key_file_get_string(keyfile, "default", "category", &error));
940     ico_uxf_conf_checkGerror(&error);
941     _ico_sys_config->misc.default_displayId = ico_uxf_getSysDisplayByName(
942                         g_key_file_get_string(keyfile, "default", "display", &error));
943     ico_uxf_conf_checkGerror(&error);
944     _ico_sys_config->misc.default_layerId = ico_uxf_getSysLayerByName(
945                         _ico_sys_config->misc.default_displayId,
946                         g_key_file_get_string(keyfile, "default", "layer", &error));
947     ico_uxf_conf_checkGerror(&error);
948     _ico_sys_config->misc.default_dispzoneId = ico_uxf_getSysDispZoneByName(
949                         _ico_sys_config->misc.default_displayId,
950                         g_key_file_get_string(keyfile, "default", "displayzone", &error));
951     _ico_sys_config->misc.default_soundId = ico_uxf_getSysSoundByName(
952                         g_key_file_get_string(keyfile, "default", "sound", &error));
953     _ico_sys_config->misc.default_soundzoneId = ico_uxf_getSysSoundZoneByName(
954                         _ico_sys_config->misc.default_soundId,
955                         g_key_file_get_string(keyfile, "default", "soundzone", &error));
956     ico_uxf_conf_checkGerror(&error);
957     _ico_sys_config->misc.default_inputdevId = ico_uxf_getSysInputdevByName(
958                         g_key_file_get_string(keyfile, "default", "inputdev", &error));
959     _ico_sys_config->misc.default_inputswId = ico_uxf_getSysInputswByName(
960                         _ico_sys_config->misc.default_inputdevId,
961                         g_key_file_get_string(keyfile, "default", "inputsw", &error));
962     ico_uxf_conf_checkGerror(&error);
963
964     apfw_trace("Default host = %d kind = %d category = %d disp = %d layer = %d dispzone = %d "
965                "sound = %d soundzone = %d input = %d/%d",
966                _ico_sys_config->misc.default_hostId,
967                _ico_sys_config->misc.default_kindId,
968                _ico_sys_config->misc.default_categoryId,
969                _ico_sys_config->misc.default_displayId,
970                _ico_sys_config->misc.default_layerId,
971                _ico_sys_config->misc.default_dispzoneId,
972                _ico_sys_config->misc.default_soundId,
973                _ico_sys_config->misc.default_soundzoneId,
974                _ico_sys_config->misc.default_inputdevId,
975                _ico_sys_config->misc.default_inputswId);
976     g_key_file_free(keyfile);
977
978     apfw_trace("reloadSysConfig: Leave");
979     return _ico_sys_config;
980 }
981
982 /*--------------------------------------------------------------------------*/
983 /**
984  * @brief   ico_uxf_getSysHostById: get host configuration by host Id
985  *
986  * @param[in]   hostId      host Id
987  * @return      host configuration table
988  * @retval      != NULL         success(host configuration table address)
989  * @retval      == NULL         error(host Id dose not exist)
990  */
991 /*--------------------------------------------------------------------------*/
992 ICO_APF_API const Ico_Uxf_conf_host *
993 ico_uxf_getSysHostById(const int hostId)
994 {
995     int     i;
996
997     if (!_ico_sys_config) {
998         (void)ico_uxf_getSysConfig();
999     }
1000
1001     for (i = 0; i < _ico_sys_config->hostNum; i++) {
1002         if (_ico_sys_config->host[i].id == hostId) {
1003             return &_ico_sys_config->host[i];
1004         }
1005     }
1006     apfw_warn("ico_uxf_getSysHostById(%d) Illegal Host Number", hostId);
1007     return NULL;
1008 }
1009
1010 /*--------------------------------------------------------------------------*/
1011 /**
1012  * @brief   ico_uxf_get_SysLocation: get host location by host Id
1013  *
1014  * @param[in]   hostId      host Id
1015  * @return      host location name
1016  * @retval      != NULL         success(host location name)
1017  * @retval      == NULL         error(host Id dose not exist)
1018  */
1019 /*--------------------------------------------------------------------------*/
1020 ICO_APF_API const char *
1021 ico_uxf_get_SysLocation(const int hostId)
1022 {
1023     int     i;
1024
1025     if (!_ico_sys_config) {
1026         (void)ico_uxf_getSysConfig();
1027     }
1028
1029     for (i = 0; i < _ico_sys_config->hostNum; i++) {
1030         if (_ico_sys_config->host[i].id == hostId) {
1031             return _ico_sys_config->host[i].address;
1032         }
1033     }
1034     apfw_warn("Ico_uxf_get_SysLocation(%d) Illegal Host Number", hostId);
1035     return "127.0.0.1";
1036 }
1037
1038 /*--------------------------------------------------------------------------*/
1039 /**
1040  * @brief   ico_uxf_getSysDisplayById: get display configuration by display Id
1041  *
1042  * @param[in]   displayId       display Id
1043  * @return      display configuration table address
1044  * @retval      != NULL         success(host oy configuration tabel address)
1045  * @retval      == NULL         error(displayd dose not exist)
1046  */
1047 /*--------------------------------------------------------------------------*/
1048 ICO_APF_API const Ico_Uxf_conf_display *
1049 ico_uxf_getSysDisplayById(const int displayId)
1050 {
1051     int     i;
1052
1053     if (!_ico_sys_config) {
1054         (void)ico_uxf_getSysConfig();
1055     }
1056
1057     for (i = 0; i < _ico_sys_config->displayNum; i++)  {
1058         if (_ico_sys_config->display[i].id == displayId)   {
1059             return &_ico_sys_config->display[i];
1060         }
1061     }
1062     apfw_warn("ico_uxf_getSysDisplayById(%d) Illegal Display Number", displayId);
1063     return NULL;
1064 }
1065
1066 /*--------------------------------------------------------------------------*/
1067 /**
1068  * @brief   ico_uxf_getSysHostByName: get host Id bu host name
1069  *
1070  * @param[in]   shost       host name
1071  * @return      host Id number
1072  */
1073 /*--------------------------------------------------------------------------*/
1074 ICO_APF_API int
1075 ico_uxf_getSysHostByName(const char *shost)
1076 {
1077     int     i;
1078
1079     if (!_ico_sys_config) {
1080         (void)ico_uxf_getSysConfig();
1081     }
1082     if (_ico_sys_config->hostNum <= 0)  {
1083         apfw_critical("ico_uxf_getSysHostByName: can not read configuration file");
1084         return _ico_sys_config->misc.default_hostId;
1085     }
1086
1087     if ((! shost) || (*shost == 0))    {
1088         return _ico_sys_config->host[0].id;
1089     }
1090
1091     for (i = 0; i < _ico_sys_config->hostNum; i++) {
1092         if (strcasecmp(shost, _ico_sys_config->host[i].name) == 0)    {
1093             return _ico_sys_config->host[i].id;
1094         }
1095     }
1096     apfw_warn("ico_uxf_getSysHostByName(%s) Illegal Host Name", shost);
1097     return _ico_sys_config->misc.default_hostId;
1098 }
1099
1100 /*--------------------------------------------------------------------------*/
1101 /**
1102  * @brief   ico_uxf_getSysHostTypeByName: get host type number by name(internal function)
1103  *
1104  * @param[in]   stype       host type name
1105  * @return      host type number
1106  */
1107 /*--------------------------------------------------------------------------*/
1108 int
1109 ico_uxf_getSysHostTypeByName(const char *stype)
1110 {
1111     if ((! stype) || (*stype == 0))    {
1112         return ICO_UXF_HOSTTYPE_CENTER;
1113     }
1114     if (strcasecmp(stype, "center") == 0)    {
1115         return ICO_UXF_HOSTTYPE_CENTER;
1116     }
1117     else if (strcasecmp(stype, "meter") == 0)    {
1118         return ICO_UXF_HOSTTYPE_METER;
1119     }
1120     else if (strcasecmp(stype, "remote") == 0)   {
1121         return ICO_UXF_HOSTTYPE_REMOTE;
1122     }
1123     else if (strcasecmp(stype, "passenger") == 0)    {
1124         return ICO_UXF_HOSTTYPE_PASSENGER;
1125     }
1126     else if (strcasecmp(stype, "rear") == 0) {
1127         return ICO_UXF_HOSTTYPE_REAR;
1128     }
1129     else if (strcasecmp(stype, "rearleft") == 0) {
1130         return ICO_UXF_HOSTTYPE_REARLEFT;
1131     }
1132     else if (strcasecmp(stype, "rearright") == 0)    {
1133         return ICO_UXF_HOSTTYPE_REARRIGHT;
1134     }
1135     apfw_warn("ico_uxf_getSysHostTypeByName(%s) Illegal HostType Name", stype);
1136     return strtol(stype, (char **)0, 0);
1137 }
1138
1139 /*--------------------------------------------------------------------------*/
1140 /**
1141  * @brief   ico_uxf_getSysDispTypeByName: get display type number by name(internal function)
1142  *
1143  * @param[in]   stype       display type name
1144  * @return      display type number
1145  */
1146 /*--------------------------------------------------------------------------*/
1147 int
1148 ico_uxf_getSysDispTypeByName(const char *stype)
1149 {
1150     if ((! stype) || (*stype == 0))    {
1151         apfw_critical("ico_uxf_getSysDispTypeByName(NULL) Illegal HostType Name");
1152         return 0;
1153     }
1154     if (strcasecmp(stype, "center") == 0)    {
1155         return ICO_UXF_DISPLAYTYPE_CENTER;
1156     }
1157     else if (strcasecmp(stype, "meter") == 0)    {
1158         return ICO_UXF_DISPLAYTYPE_METER;
1159     }
1160     else if (strcasecmp(stype, "remote") == 0)   {
1161         return ICO_UXF_DISPLAYTYPE_REMOTE;
1162     }
1163     else if (strcasecmp(stype, "passenger") == 0)    {
1164         return ICO_UXF_DISPLAYTYPE_PASSENGER;
1165     }
1166     else if (strcasecmp(stype, "rear") == 0) {
1167         return ICO_UXF_DISPLAYTYPE_REAR;
1168     }
1169     else if (strcasecmp(stype, "rearleft") == 0) {
1170         return ICO_UXF_DISPLAYTYPE_REARLEFT;
1171     }
1172     else if (strcasecmp(stype, "rearright") == 0)    {
1173         return ICO_UXF_DISPLAYTYPE_REARRIGHT;
1174     }
1175     apfw_warn("ico_uxf_getSysDispTypeByName(%s) Illegal DisplayType Name", stype);
1176     return strtol(stype, (char **)0, 0);
1177 }
1178
1179 /*--------------------------------------------------------------------------*/
1180 /**
1181  * @brief   ico_uxf_getSysBoolByName: get boolian value(internal function)
1182  *
1183  * @param[in]   sbool       boolian value string
1184  * @return      boolian value
1185  * @retval      = 0             false
1186  * @retval      = 1             true
1187  */
1188 /*--------------------------------------------------------------------------*/
1189 int
1190 ico_uxf_getSysBoolByName(const char *sbool)
1191 {
1192     if ((! sbool) || (*sbool == 0))    {
1193         return 0;
1194     }
1195     if ((strcasecmp(sbool, "true") == 0) ||
1196         (strcasecmp(sbool, "yes") == 0) ||
1197         (strcasecmp(sbool, "1") == 0))  {
1198         return 1;
1199     }
1200     return 0;
1201 }
1202
1203 /*--------------------------------------------------------------------------*/
1204 /**
1205  * @brief   ico_uxf_getSysDisplayByName: get display Id by display name
1206  *
1207  * @param[in]   sdisplay        display name
1208  * @return      display Id
1209  */
1210 /*--------------------------------------------------------------------------*/
1211 ICO_APF_API int
1212 ico_uxf_getSysDisplayByName(const char *sdisplay)
1213 {
1214     int     i;
1215
1216     if (!_ico_sys_config) {
1217         (void)ico_uxf_getSysConfig();
1218     }
1219     if ((! sdisplay) || (*sdisplay == 0))  {
1220         apfw_trace("ico_uxf_getSysDisplayByName(NULL) Illegal Display Name");
1221         return _ico_sys_config->misc.default_displayId;
1222     }
1223
1224     for (i = 0; i < _ico_sys_config->displayNum; i++) {
1225         if (strcasecmp(sdisplay, _ico_sys_config->display[i].name) == 0)    {
1226             return _ico_sys_config->display[i].id;
1227         }
1228     }
1229     apfw_warn("ico_uxf_getSysDisplayByName(%s) Illegal Display Name", sdisplay);
1230     return _ico_sys_config->misc.default_displayId;
1231 }
1232
1233 /*--------------------------------------------------------------------------*/
1234 /**
1235  * @brief   ico_uxf_getSysLayerByName: get layer Id by layer name
1236  *
1237  * @param[in]   display     display Id
1238  * @param[in]   slayer      layer name
1239  * @return      layer Id
1240  */
1241 /*--------------------------------------------------------------------------*/
1242 ICO_APF_API int
1243 ico_uxf_getSysLayerByName(const int display, const char *slayer)
1244 {
1245     int     i;
1246     char    *errp;
1247
1248     if (!_ico_sys_config) {
1249         (void)ico_uxf_getSysConfig();
1250     }
1251
1252     if ((display < 0) || (display >= _ico_sys_config->displayNum)) {
1253         apfw_warn("ico_uxf_getSysLayerByName(%d,%s) Illegal Display Number",
1254                             display, slayer);
1255         return _ico_sys_config->misc.default_layerId;
1256     }
1257     if ((! slayer) || (*slayer == 0))  {
1258         apfw_trace("ico_uxf_getSysLayerByName(NULL) Illegal Layer Name");
1259         return _ico_sys_config->misc.default_layerId;
1260     }
1261
1262     for (i = 0; i < _ico_sys_config->display[display].layerNum; i++)    {
1263         if (strcasecmp(slayer, _ico_sys_config->display[display].layer[i].name) == 0)   {
1264             return _ico_sys_config->display[display].layer[i].id;
1265         }
1266     }
1267
1268     errp = NULL;
1269     i = strtol(slayer, &errp, 0);
1270     if ((errp) && (*errp != 0)) {
1271         apfw_warn("ico_uxf_getSysLayerByName(%d,%s) Illegal Layer Name", display, slayer);
1272         i = _ico_sys_config->misc.default_layerId;
1273     }
1274     return i;
1275 }
1276
1277 /*--------------------------------------------------------------------------*/
1278 /**
1279  * @brief   ico_uxf_getSysKindByName: get kind Id by kind name
1280  *
1281  * @param[in]   skind       kind name
1282  * @return      kind Id
1283  */
1284 /*--------------------------------------------------------------------------*/
1285 ICO_APF_API int
1286 ico_uxf_getSysKindByName(const char *skind)
1287 {
1288     int     i;
1289
1290     if (!_ico_sys_config) {
1291         (void)ico_uxf_getSysConfig();
1292     }
1293     if (_ico_sys_config->kindNum <= 0)  {
1294         apfw_critical("ico_uxf_getSysKindByName: can not read configuration file");
1295         return _ico_sys_config->misc.default_kindId;
1296     }
1297
1298     if ((skind) && (*skind != 0))  {
1299         for (i = 0; i < _ico_sys_config->kindNum; i++) {
1300             if (strcasecmp(skind, _ico_sys_config->kind[i].name) == 0)    {
1301                 return _ico_sys_config->kind[i].id;
1302             }
1303         }
1304     }
1305     apfw_warn("ico_uxf_getSysKindByName(%s) Illegal kind name", skind);
1306     return _ico_sys_config->misc.default_kindId;
1307 }
1308
1309 /*--------------------------------------------------------------------------*/
1310 /**
1311  * @brief   ico_uxf_getSysCategoryByName: get category Id by category name
1312  *
1313  * @param[in]   scategory   category name
1314  * @return      category Id
1315  */
1316 /*--------------------------------------------------------------------------*/
1317 ICO_APF_API int
1318 ico_uxf_getSysCategoryByName(const char *scategory)
1319 {
1320     int     i;
1321
1322     if (!_ico_sys_config) {
1323         (void)ico_uxf_getSysConfig();
1324     }
1325     if (_ico_sys_config->categoryNum <= 0)  {
1326         apfw_critical("ico_uxf_getSysCategoryByName: can not read configuration file");
1327         return _ico_sys_config->misc.default_categoryId;
1328     }
1329
1330     if ((scategory) && (*scategory != 0))  {
1331         for (i = 0; i < _ico_sys_config->categoryNum; i++) {
1332             if (strcasecmp(scategory, _ico_sys_config->category[i].name) == 0)    {
1333                 return _ico_sys_config->category[i].id;
1334             }
1335         }
1336     }
1337     apfw_warn("ico_uxf_getSysCategoryByName(%s) Illegal category name", scategory);
1338     return _ico_sys_config->misc.default_categoryId;
1339 }
1340
1341 /*--------------------------------------------------------------------------*/
1342 /**
1343  * @brief   ico_uxf_getSysDispZoneByName: get display zone Id by display zone name
1344  *
1345  * @param[in]   display     display Id
1346  * @param[in]   sdispzone   displahy zone name
1347  * @return      display zone Id
1348  */
1349 /*--------------------------------------------------------------------------*/
1350 ICO_APF_API int
1351 ico_uxf_getSysDispZoneByName(const int display, const char *sdispzone)
1352 {
1353     int     i;
1354
1355     if (!_ico_sys_config) {
1356         (void)ico_uxf_getSysConfig();
1357     }
1358     if ((display < 0) || (display >= _ico_sys_config->displayNum) ||
1359         (_ico_sys_config->display[display].zoneNum <= 0))   {
1360         apfw_critical("ico_uxf_getSysDispZoneByName: can not read configuration file");
1361         return _ico_sys_config->misc.default_dispzoneId;
1362     }
1363
1364     if ((sdispzone) && (*sdispzone != 0))  {
1365         for (i = 0; i < _ico_sys_config->display[display].zoneNum; i++) {
1366             if (strcasecmp(sdispzone, _ico_sys_config->display[display].zone[i].name) == 0) {
1367                 return _ico_sys_config->display[display].zone[i].id;
1368             }
1369         }
1370     }
1371     apfw_warn("ico_uxf_getSysDispZoneByName(%s) Illegal zone name", sdispzone);
1372     return _ico_sys_config->misc.default_dispzoneId;
1373 }
1374
1375 /*--------------------------------------------------------------------------*/
1376 /**
1377  * @brief   ico_uxf_getSysSoundByName: get sound Id by sound name
1378  *
1379  * @param[in]   ssound      sound name
1380  * @return      sound Id
1381  */
1382 /*--------------------------------------------------------------------------*/
1383 ICO_APF_API int
1384 ico_uxf_getSysSoundByName(const char *ssound)
1385 {
1386     int     i;
1387
1388     if (!_ico_sys_config) {
1389         (void)ico_uxf_getSysConfig();
1390     }
1391     if (_ico_sys_config->soundNum <= 0) {
1392         apfw_critical("ico_uxf_getSysSoundByName: can not read configuration file");
1393         return _ico_sys_config->misc.default_soundId;
1394     }
1395
1396     if ((ssound) && (*ssound != 0))    {
1397         for (i = 0; i < _ico_sys_config->soundNum; i++) {
1398             if (strcasecmp(ssound, _ico_sys_config->sound[i].name) == 0)    {
1399                 return _ico_sys_config->sound[i].id;
1400             }
1401         }
1402     }
1403     apfw_warn("ico_uxf_getSysSoundByName(%s) Illegal sound name", ssound);
1404     return _ico_sys_config->misc.default_soundId;
1405 }
1406
1407 /*--------------------------------------------------------------------------*/
1408 /**
1409  * @brief   ico_uxf_getSysSoundZoneByName: get sound zone Id by sound zone name
1410  *
1411  * @param[in]   sound       sound Id
1412  * @param[in]   ssoundzone  sound zone name
1413  * @return      sound zone Id
1414  */
1415 /*--------------------------------------------------------------------------*/
1416 ICO_APF_API int
1417 ico_uxf_getSysSoundZoneByName(const int sound, const char *ssoundzone)
1418 {
1419     int     i;
1420
1421     if (!_ico_sys_config) {
1422         (void)ico_uxf_getSysConfig();
1423     }
1424     if ((sound < 0) || (sound >= _ico_sys_config->soundNum) ||
1425         (_ico_sys_config->sound[sound].zoneNum <= 0))   {
1426         apfw_critical("ico_uxf_getSysSoundZoneByName: can not read configuration file");
1427         return _ico_sys_config->misc.default_soundzoneId;
1428     }
1429
1430     if ((ssoundzone) && (*ssoundzone != 0))    {
1431         for (i = 0; i < _ico_sys_config->sound[sound].zoneNum; i++) {
1432             if (strcasecmp(ssoundzone, _ico_sys_config->sound[sound].zone[i].name) == 0)    {
1433                 return _ico_sys_config->sound[sound].zone[i].id;
1434             }
1435         }
1436     }
1437     apfw_warn("ico_uxf_getSysSoundZoneByName(%s) Illegal zone name", ssoundzone);
1438     return _ico_sys_config->misc.default_soundzoneId;
1439 }
1440
1441 /*--------------------------------------------------------------------------*/
1442 /**
1443  * @brief   ico_uxf_getSysInputdevByName: get input device Id by input device name
1444  *
1445  * @param[in]   sinputdev   input device name
1446  * @return      input device Id
1447  */
1448 /*--------------------------------------------------------------------------*/
1449 ICO_APF_API int
1450 ico_uxf_getSysInputdevByName(const char *sinputdev)
1451 {
1452     int     i;
1453
1454     if (!_ico_sys_config) {
1455         (void)ico_uxf_getSysConfig();
1456     }
1457     if (_ico_sys_config->inputdevNum <= 0)  {
1458         apfw_critical("ico_uxf_getSysInputdevByName: can not read configuration file");
1459         return _ico_sys_config->misc.default_inputdevId;
1460     }
1461
1462     if ((! sinputdev) || (*sinputdev == 0))    {
1463         return _ico_sys_config->misc.default_inputdevId;
1464     }
1465
1466     for (i = 0; i < _ico_sys_config->inputdevNum; i++) {
1467         if (strcasecmp(sinputdev, _ico_sys_config->inputdev[i].name) == 0)    {
1468             return _ico_sys_config->inputdev[i].id;
1469         }
1470     }
1471     apfw_warn("ico_uxf_getSysInputdevByName(%s) Illegal device Name", sinputdev);
1472     return _ico_sys_config->misc.default_inputdevId;
1473 }
1474
1475 /*--------------------------------------------------------------------------*/
1476 /**
1477  * @brief   ico_uxf_getSysInputswByName: get input switch Id by input switch name
1478  *
1479  * @param[in]   inputdev    input device Id
1480  * @param[in]   sinputsw    input switch name
1481  * @return      input switch Id
1482  */
1483 /*--------------------------------------------------------------------------*/
1484 ICO_APF_API int
1485 ico_uxf_getSysInputswByName(const int inputdev, const char *sinputsw)
1486 {
1487     int     i;
1488
1489     if (!_ico_sys_config) {
1490         (void)ico_uxf_getSysConfig();
1491     }
1492     if ((inputdev < 0) || (inputdev >= _ico_sys_config->inputdevNum) ||
1493         (_ico_sys_config->inputdev[inputdev].inputswNum <= 0))  {
1494         apfw_critical("ico_uxf_getSysInputswByName: can not read configuration file");
1495         return 0;
1496     }
1497
1498     if ((sinputsw) && (*sinputsw != 0)) {
1499         for (i = 0; i < _ico_sys_config->inputdev[inputdev].inputswNum; i++)    {
1500             if (strcasecmp(sinputsw, _ico_sys_config->inputdev[inputdev].inputsw[i].name)
1501                     == 0)    {
1502                 return i;
1503             }
1504         }
1505     }
1506     apfw_warn("ico_uxf_getSysInputswByName(%s) Illegal sw name",
1507               sinputsw ? sinputsw : "(NULL)");
1508     return 0;
1509 }
1510
1511 /*--------------------------------------------------------------------------*/
1512 /**
1513  * @brief   ico_uxf_closeSysConfig: close and release system configuration table
1514  *
1515  * @param       none
1516  * @return      none
1517  */
1518 /*--------------------------------------------------------------------------*/
1519 ICO_APF_API void
1520 ico_uxf_closeSysConfig(void)
1521 {
1522     if (_ico_sys_config != NULL)    {
1523         g_free(_ico_sys_config->host);
1524         g_free(_ico_sys_config->display);
1525         g_free(_ico_sys_config);
1526         _ico_sys_config = NULL;
1527     }
1528 }
1529