aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_config/ecore_config.c
blob: e81538eb62d84cee64ed6bfc543559404342d1b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>

#include "Ecore_Config.h"
#include "ecore_config_private.h"
#include "ecore_config_ipc.h"

#include "ecore_config_util.h"
int  _ecore_config_log_dom = -1;
int                  DEBUG = 0;
EAPI Ecore_Config_Server *__ecore_config_server_global = NULL;
EAPI Ecore_Config_Server *__ecore_config_server_local = NULL;
EAPI Ecore_Config_Bundle *__ecore_config_bundle_local = NULL;
EAPI char                *__ecore_config_app_name = NULL;
int                  __ecore_config_system_init = 0;

static int           _ecore_config_system_init_no_load(void);
static int           _ecore_config_system_load(void);

static inline void  *__ecore_argb_to_long(int a, int r, int g, int b, long *v);
static inline void  *__ecore_argbstr_to_long(const char *argb, long *v);

static const char  *_ecore_config_type[] =
   { "undefined", "integer", "float", "string", "colour", "theme", "boolean", "structure" };

/**
 * @defgroup Ecore_Config_Property_Group Ecore Config Property Functions
 *
 * Functions that retrieve or set the attributes relating to a property.
 */

/**
 * Removes the given property from the local configuration and destroys it.
 * @param   e Property to destroy.
 * @return  @c NULL
 * @ingroup Ecore_Config_Property_Group
 */
EAPI Ecore_Config_Prop  *
ecore_config_dst(Ecore_Config_Prop * e)
{
   Ecore_Config_Bundle *t;
   Ecore_Config_Prop  *p, *c;
   Ecore_Config_Listener_List *l;

   p = NULL;
   t = __ecore_config_bundle_local;
   c = t->data;

   if (!e || !e->key)
      return NULL;
   if (t)
     {
	if (t->data == e)
	   t->data = e->next;
	else
	  {
	     do
	       {
		  p = c;
		  c = c->next;
	       }
	     while (c && (c != e));
	     if (c)		/* remove from list if even in there */
		p->next = c->next;
	  }
     }

   while (e->listeners)
     {
	l = e->listeners;
	e->listeners = e->listeners->next;
	free(l);
     }

   if (e->key)
      free(e->key);
   if (e->ptr && ((e->type == ECORE_CONFIG_STR) || (e->type == ECORE_CONFIG_THM)))
      free(e->ptr);

   memset(e, 0, sizeof(Ecore_Config_Prop));
   free(e);

   return NULL;
}

/**
 * @defgroup Ecore_Config_Get_Group Configuration Retrieve Functions
 *
 * Functions that retrieve configuration values, based on type.
 */

/**
 * Returns the property with the given key.
 * @param   key The unique name of the wanted property.
 * @return  The property that corresponds to the given key.  @c NULL if the
 *          key could not be found.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI Ecore_Config_Prop  *
ecore_config_get(const char *key)
{
   Ecore_Config_Bundle *t;
   Ecore_Config_Prop  *e;

   t = __ecore_config_bundle_local;
   if (!t || !key)
      return NULL;
   e = t->data;
   while (e)
     {
	if (!strcmp(key, e->key))
	   return e;
	e = e->next;
     }
   return NULL;
}

/**
 * Returns the type of the property.
 * @param   e Property to get the type of.
 * @returns The type of the property.  If the property is invalid, then the
 *          string "not found" is returned.
 * @ingroup Ecore_Config_Property_Group
 */
EAPI const char         *
ecore_config_type_get(const Ecore_Config_Prop * e)
{
   if (e)
     {
	return _ecore_config_type[e->type];
     }
   return "not found";
}

/**
 * Returns the specified property as a string.
 * @param   key The property key.
 * @return  The string value of the property.  The function returns @c NULL if
 *          the property is not a string or is not set.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI char               *
ecore_config_string_get(const char *key)
{
   return _ecore_config_string_get( ecore_config_get(key) );
}

char               *
_ecore_config_string_get(Ecore_Config_Prop *e)
{
   return (e && (e->type == ECORE_CONFIG_STR) && e->ptr) ? strdup(e->ptr) : NULL;
}

/**
 * Returns the specified property as an integer.
 * @param   key The property key.
 * @return  The value of the property.  The function returns -1 if the
 *          property is not an integer or is not set.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI int
ecore_config_boolean_get(const char *key)
{
   return _ecore_config_boolean_get( ecore_config_get(key) );
}

int
_ecore_config_boolean_get(Ecore_Config_Prop *e)
{
   return (e && ((e->type == ECORE_CONFIG_INT) || (e->type == ECORE_CONFIG_BLN))) ? (e->val != 0) : -1;
}

/**
 * Returns the specified property as a long integer.
 * @param   key The property key.
 * @return  The integer value of the property.  The function returns 0 if the
 *          property is not an integer or is not set.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI long
ecore_config_int_get(const char *key)
{
   return _ecore_config_int_get( ecore_config_get(key) );
}

long
_ecore_config_int_get(Ecore_Config_Prop *e)
{
   return (e && ((e->type == ECORE_CONFIG_INT) || (e->type == ECORE_CONFIG_RGB))) ? e->val : 0L;
}

/**
 * Returns the specified property as a float.
 * @param   key The property key.
 * @return  The float value of the property.  The function returns 0.0 if the
 *          property is not a float or is not set.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI float
ecore_config_float_get(const char *key)
{
   return _ecore_config_float_get( ecore_config_get(key) );
}

float
_ecore_config_float_get(Ecore_Config_Prop *e)
{
   return (e && (e->type == ECORE_CONFIG_FLT)) ? ((float)e->val / ECORE_CONFIG_FLOAT_PRECISION) : 0.0;
}

/**
 * Finds the alpha, red, green and blue values of a color property.
 * @param   key The property key.
 * @param   a   A pointer to an integer to store the alpha value into.
 * @param   r   A pointer to an integer to store the red value into.
 * @param   g   A pointer to an integer to store the green value into.
 * @param   b   A pointer to an integer to store the blue value into.
 * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_FAIL
 *          otherwise.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI int
ecore_config_argb_get(const char *key, int *a, int *r, int *g, int *b)
{
   return _ecore_config_argb_get( ecore_config_get(key), a, r, g, b);
}

int
_ecore_config_argb_get(Ecore_Config_Prop *e, int *a, int *r, int *g, int *b)
{
   if (e && ((e->type == ECORE_CONFIG_RGB)))
     {
	if(a) *a = (e->val >> 24) & 0xff;
	if(r) *r = (e->val >> 16) & 0xff;
	if(g) *g = (e->val >>  8) & 0xff;
	if(b) *b =  e->val        & 0xff;
	return ECORE_CONFIG_ERR_SUCC;
     }
   return ECORE_CONFIG_ERR_FAIL;
}

/**
 * Returns a color property as a long
 * @param   key The property key.
 * @return  ARGB data as long
 * @ingroup Ecore_Config_Get_Group
 */
EAPI long
ecore_config_argbint_get(const char *key)
{
   return _ecore_config_argbint_get( ecore_config_get(key) );
}

long
_ecore_config_argbint_get(Ecore_Config_Prop *e)
{
   if (e && ((e->type == ECORE_CONFIG_RGB)))
     {
	return e->val;
     }
   return 0L;
}

/**
 * Returns a color property as a string of hexadecimal characters.
 * @param   key The property key.
 * @return  A string of hexadecimal characters in the format #aarrggbb.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI char               *
ecore_config_argbstr_get(const char *key)
{
   return _ecore_config_argbstr_get( ecore_config_get(key) );
}

char               *
_ecore_config_argbstr_get(Ecore_Config_Prop *e)
{
   char               *r;

   r = NULL;
   esprintf(&r, "#%08x", _ecore_config_int_get(e));
   return r;
}

/**
 * Returns a theme property.
 * @param   key The property key.
 * @return  The name of the theme the property refers to.  The function returns
 *          @c NULL if the property is not a theme or is not set.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI char               *
ecore_config_theme_get(const char *key)
{
   return _ecore_config_theme_get( ecore_config_get(key) );
}

char               *
_ecore_config_theme_get(Ecore_Config_Prop *e)
{
   return (e && (e->type == ECORE_CONFIG_THM)) ? strdup(e->ptr) : NULL;
}

/**
 * Retrieves the key as a string.
 * @param   key The property key.
 * @return  Returns a character array in the form of 'key:type=value'.  @c NULL
 *          is returned if the property does not exist.
 * @ingroup Ecore_Config_Get_Group
 */
EAPI char               *
ecore_config_as_string_get(const char *key)
{
   Ecore_Config_Prop  *e;
   char               *val;
   char               *r;

   val = NULL;
   r = NULL;
   if (!(e = ecore_config_get(key)))
      ERR("no such property, \"%s\"...", key);
   else
     {
	switch (e->type)
	   {
	     case ECORE_CONFIG_NIL:
		val = strdup("<nil>");
		break;
	     case ECORE_CONFIG_INT:
		esprintf(&val, "%ld",    _ecore_config_int_get(e));
		break;
	     case ECORE_CONFIG_BLN:
		esprintf(&val, "%ld",    _ecore_config_boolean_get(e));
		break;
	     case ECORE_CONFIG_FLT:
		esprintf(&val, "%lf",    _ecore_config_float_get(e));
		break;
	     case ECORE_CONFIG_STR:
		esprintf(&val, "\"%s\"", _ecore_config_string_get(e));
		break;
	     case ECORE_CONFIG_RGB:
		esprintf(&val, "#%08x",  _ecore_config_int_get(e));
		break;
	     case ECORE_CONFIG_THM:
		esprintf(&val, "\"%s\"", _ecore_config_theme_get(e));
		break;
	     case ECORE_CONFIG_SCT:
		break;
	     default:
		esprintf(&r, "%s:unknown_type", key);
		break;
	   }
	if (val)
	   {
	     esprintf(&r, "%s:%s=%s", key, _ecore_config_type[e->type], val);
	     free(val);
	   }
     }
   return r;
}

EAPI int
ecore_config_bound(Ecore_Config_Prop * e)
{
   int                 ret;
   long                v;

   ret = ECORE_CONFIG_ERR_SUCC;

   if (!e)
      return ECORE_CONFIG_ERR_FAIL;
   if (e->flags & ECORE_CONFIG_FLAG_BOUNDS)
     {
	if ((e->val < e->lo))
	  {
	    WRN("ecore_config_bounds(\"%s\",%ld): value out of range; adjusted to %ld...",
	       e->key, e->val, e->lo);
	     e->val = e->lo;
	  }
	else if ((e->val > e->hi))
	  {
	    WRN("ecore_config_bounds(\"%s\",%ld): value out of range; adjusted to %ld...",
	       e->key, e->val, e->hi);
	     e->val = e->hi;
	  }
	else
	   ret = ECORE_CONFIG_ERR_IGNORED;
     }
   else
      ret = ECORE_CONFIG_ERR_IGNORED;

   if (e->step)
     {
	v = ((int)(e->val / e->step)) * e->step;
	if (v != e->val)
	  {
	     if (e->type == ECORE_CONFIG_FLT)
	       WRN("ecore_config_bound(\"%s\"): float value %f not a multiple of %f, adjusted to %f...",
		  e->key, ((double)e->val) / ECORE_CONFIG_FLOAT_PRECISION,
		  ((double)e->step) / ECORE_CONFIG_FLOAT_PRECISION,
		  ((double)v) / ECORE_CONFIG_FLOAT_PRECISION);
	     else
	       WRN("ecore_config_bound(\"%s\"): integer value %ld not a multiple of %ld, adjusted to %ld...",
		  e->key, e->val, e->step, v);
	     ret = ECORE_CONFIG_ERR_SUCC;
	     e->val = v;
	  }
     }

   return ret;
}

/**
 * Tries to guess the type of a property.
 *
 * This function first checks to see if the property exists.  If it does, then
 * the type of the stored property is returned.  Otherwise, the function tries
 * to guess the type of the property based on @p val.
 *
 * @param  key The property key.
 * @param  val The value in string form.
 * @return The type of the property determined by the function.  Note that if
 *         val is @c NULL, @c ECORE_CONFIG_NIL will be returned.
 */
EAPI int
ecore_config_type_guess(const char *key, const char *val)
{
   Ecore_Config_Prop  *p;
   char               *l;

   l = NULL;

   if (key && (p = ecore_config_get(key)) && p->type != ECORE_CONFIG_NIL)
      return p->type;

   if (!val)
      return ECORE_CONFIG_NIL;
   if (val[0] == '#')
      return ECORE_CONFIG_RGB;
   strtol(val, &l, 10);
   if (*l)
     {
	float               f;

	if (sscanf(val, "%f%*s", &f) != 1)
	   return ECORE_CONFIG_STR;
	return ECORE_CONFIG_FLT;
     }
   return ECORE_CONFIG_INT;
}

static int
ecore_config_typed_val(Ecore_Config_Prop * e, const void *val, int type)
{

   if (!e)
     return ECORE_CONFIG_ERR_NODATA;

   if (!(val) && (type != ECORE_CONFIG_NIL && type != ECORE_CONFIG_SCT))
      e->ptr = NULL;
   else
     {
	if (type == ECORE_CONFIG_INT || type == ECORE_CONFIG_BLN)
	  {
	     e->val = (long) *((int *)val);
	     e->type = type;
	  }
	else if (type == ECORE_CONFIG_STR || type == ECORE_CONFIG_THM)
	  {
	     if (!(e->ptr = strdup(val)))
		return ECORE_CONFIG_ERR_OOM;
	     if (e->type == ECORE_CONFIG_NIL)
		e->type = type;
	  }
	else if (type == ECORE_CONFIG_RGB)
	  {
	     __ecore_argbstr_to_long((char *)val, &e->val);
	     e->type = ECORE_CONFIG_RGB;
	  }
	else if (type == ECORE_CONFIG_FLT)
	  {
	     e->val = (long) ((*((float *)val)) * ECORE_CONFIG_FLOAT_PRECISION);
	     e->type = ECORE_CONFIG_FLT;
	  }
	else if (type == ECORE_CONFIG_SCT)
	  {
	     e->type = ECORE_CONFIG_SCT;
	  }
	else
	  {
	   e->type = ECORE_CONFIG_NIL;
	  }

	ecore_config_bound(e);
	e->flags |= ECORE_CONFIG_FLAG_MODIFIED;
	e->flags &= ~ECORE_CONFIG_FLAG_CMDLN;
	return ECORE_CONFIG_ERR_SUCC;
     }
   return ECORE_CONFIG_ERR_IGNORED;
}

static int
ecore_config_typed_add(const char *key, const void *val, int type)
{
   int error = ECORE_CONFIG_ERR_SUCC;
   Ecore_Config_Prop  *e = NULL;
   Ecore_Config_Bundle *t;

   t = __ecore_config_bundle_local;
   if (!key)
      return ECORE_CONFIG_ERR_NODATA;

   if (!(e = calloc(1, sizeof(Ecore_Config_Prop))))
     {
	return ECORE_CONFIG_ERR_OOM;
     }
   else if (!(e->key = strdup(key)))
     {
	error = ECORE_CONFIG_ERR_OOM;
     }
   else if ((error = ecore_config_typed_val(e, val, type)) == ECORE_CONFIG_ERR_SUCC)
     {
	if (t)
	   {
	     e->next = t->data;
	     t->data = e;
	   }
	return ECORE_CONFIG_ERR_SUCC;
     }

   if(e->key)
     free(e->key);
   if(e)
     free(e);

   if (error == ECORE_CONFIG_ERR_SUCC)
      error = ECORE_CONFIG_ERR_FAIL;

   return error;
}

static int
ecore_config_add(const char *key, const char *val)
{
   int                 type;

   type = ecore_config_type_guess(key, val);
   return ecore_config_typed_add(key, val, type);
}

/**
 * Sets the description field of the indicated property.
 * @param   key  The property key.
 * @param   desc Description string.
 * @note    The description string is copied for the property's use.  You can
 *          free @p desc once this function is called.
 * @ingroup Ecore_Config_Property_Group
 */
EAPI int
ecore_config_describe(const char *key, const char *desc)
{
   Ecore_Config_Prop  *e;

   if (!(e = ecore_config_get(key)))
      return ECORE_CONFIG_ERR_NODATA;
   e->description = strdup(desc);
   return ECORE_CONFIG_ERR_SUCC;
}

/**
 * Set the short option character of a property.
 * @param   key       The property key.
 * @param   short_opt Character used to indicate the value of a property
 *                    given on the command line.
 * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
 *          is returned if the property does not exist.
 * @ingroup Ecore_Config_Property_Group
 */
EAPI int
ecore_config_short_opt_set(const char *key, char short_opt)
{
   Ecore_Config_Prop  *e;

   if (!(e = ecore_config_get(key)))
      return ECORE_CONFIG_ERR_NODATA;
   e->short_opt = short_opt;
   return ECORE_CONFIG_ERR_SUCC;
}

/**
 * Set the long option string of the property.
 * @param   key      The property key.
 * @param   long_opt String used to indicate the value of a property given
 *                   on the command line.
 * @return  @c ECORE_CONFIG_ERR_SUCC on success.  @c ECORE_CONFIG_ERR_NODATA
 *          is returned if the property does not exist.
 * @ingroup Ecore_Config_Property_Group
 */
EAPI int
ecore_config_long_opt_set(const char *key, const char *long_opt)
{
   Ecore_Config_Prop  *e;

   if (!(e = ecore_config_get(key)))
      return ECORE_CONFIG_ERR_NODATA;
   if (e->long_opt)
      free(e->long_opt);
   if (long_opt)
      e->long_opt = strdup(long_opt);
   return ECORE_CONFIG_ERR_SUCC;
}

static void
_ecore_config_listener_fire(Ecore_Config_Prop *prop)
{
   Ecore_Config_Listener_List *l;
   for (l = prop->listeners; l; l = l->next)
     l->listener(prop->key, prop->type, l->tag, l->data);

   /* fire change listeners for the generic struct container etc */
   if (prop->parent)
     _ecore_config_listener_fire(prop->parent);
}

/**
 * Sets the indicated property to the given value and type.
 * @param   key  The property key.
 * @param   val  A pointer to the value to set the property to.
 * @param   type The type of the property.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Property_Group
 */
EAPI int
ecore_config_typed_set(const char *key, const void *val, int type)
{
   Ecore_Config_Prop  *e;
   int                 ret;

   if (!key)
      return ECORE_CONFIG_ERR_NODATA;
/*  if (!t) { * global prop *
    e=ecore_config_get(key);
    if (e)
      for(l=e->listeners;l;l=l->next)
        l->listener(e->key,e->type,l->tag,l->data,t);
    return ECORE_CONFIG_ERR_SUCC;
  }
*/
   if (!(e = ecore_config_get(key)))
      return ecore_config_typed_add(key, val, type);

   if ((ret = ecore_config_typed_val(e, val, type)) == ECORE_CONFIG_ERR_SUCC)
     {
       _ecore_config_listener_fire(e);
     }
   else
     {
	ERR("ecore_config_typed_set(\"%s\"): ecore_config_typed_val() failed: %d",
	    key, ret);
     }

   return ret;
}

/**
 * @defgroup Ecore_Config_Set_Group Ecore Config Setters
 *
 * Functions that set the value of a property.
 */

/**
 * Sets the indicated property to the value indicated by @a val.
 * @param   key The property key.
 * @param   val String representation of value to set.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_set(const char *key, const char *val)
{
   int                 type;
   int                 tmpi;
   float               tmpf;
   long                tmpl;

   type = ecore_config_type_guess(key, val);
   if (type == ECORE_CONFIG_INT || type == ECORE_CONFIG_BLN)
     {
	tmpi = atoi(val);
	return ecore_config_typed_set(key, &tmpi, type);
     }
   else if (type == ECORE_CONFIG_FLT)
     {
	tmpf = atof(val);
	return ecore_config_typed_set(key, &tmpf, type);
     }
   else if (type == ECORE_CONFIG_RGB)
     {
	__ecore_argbstr_to_long(val, &tmpl);
	return ecore_config_typed_set(key, &tmpl, type);
     }
   else
      return ecore_config_typed_set(key, val, type);
}

/**
 * Sets the indicated property to the value given in the string.
 * @param   key The property key.
 * @param   val String representation of the value.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_as_string_set(const char *key, const char *val)
{
   return ecore_config_set(key, val);
}

/**
 * Sets the indicated property to the given boolean.
 * @param   key The property key.
 * @param   val Boolean integer to set the property to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_boolean_set(const char *key, int val)
{
   val = val ? 1 : 0;
   return ecore_config_typed_set(key, &val, ECORE_CONFIG_BLN);
}

/**
 * Sets the indicated property to the given integer.
 * @param   key The property key.
 * @param   val Integer to set the property to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_int_set(const char *key, int val)
{
   return ecore_config_typed_set(key, &val, ECORE_CONFIG_INT);
}

/**
 * Sets the indicated property to the given string.
 * @param   key The property key.
 * @param   val String to set the property to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_string_set(const char *key, const char *val)
{
   return ecore_config_typed_set(key, val, ECORE_CONFIG_STR);
}

/**
 * Sets the indicated property to the given float value.
 * @param   key The property key.
 * @param   val Float to set the property to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_float_set(const char *key, float val)
{
   return ecore_config_typed_set(key, &val, ECORE_CONFIG_FLT);
}

/**
 * Sets the indicated property to a color value.
 * @param   key The property key
 * @param   a integer 0..255
 * @param   r integer 0..255
 * @param   g integer 0..255
 * @param   b integer 0..255
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_argb_set(const char *key, int a, int r, int g, int b)
{
   long v = 0;
   return ecore_config_typed_set(key, __ecore_argb_to_long(a,r,g,b, &v), ECORE_CONFIG_RGB);
}

/**
 * Sets the indicated property to a color value.
 * @param   key The property key
 * @param   argb ARGB data as long
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_argbint_set(const char *key, long argb)
{
   return ecore_config_typed_set(key, &argb, ECORE_CONFIG_RGB);
}

/**
 * Sets the indicated property to a color value.
 * @param   key The property key
 * @param   val Color value in ARGB format.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_argbstr_set(const char *key, const char *val)
{
   long v = 0;
   return ecore_config_typed_set(key, __ecore_argbstr_to_long(val, &v), ECORE_CONFIG_RGB);
}

/**
 * Sets the indicated property to a theme name.
 * @param   key The property key.
 * @param   val String giving the name of the theme.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_theme_set(const char *key, const char *val)
{
   return ecore_config_typed_set(key, val, ECORE_CONFIG_THM);
}

/**
 * Sets the theme preview group of an indicated property.
 * @param   key   The property key.
 * @param   group The group name.
 * @return  @c ECORE_CONFIG_ERR_SUCC on success.
 * @ingroup Ecore_Config_Set_Group
 */
EAPI int
ecore_config_theme_preview_group_set(const char *key, const char *group)
{
   int                 ret;
   Ecore_Config_Prop  *e;

   ret = ECORE_CONFIG_ERR_SUCC;
   if (!(e = ecore_config_get(key)))
     {				/* prop doesn't exist yet */
	if ((ret = ecore_config_typed_add(key, "", ECORE_CONFIG_THM)) != ECORE_CONFIG_ERR_SUCC)	/* try to add it */
	   return ret;		/* ...failed */
	if (!(e = ecore_config_get(key)))	/* get handle */
	   return ECORE_CONFIG_ERR_FAIL;
     }
   if (e->data)
      free(e->data);
   e->data = strdup(group);

   return ret;
}

EAPI int
ecore_config_typed_default(const char *key, const void *val, int type)
{
   int                 ret;
   Ecore_Config_Prop  *e;

   ret = ECORE_CONFIG_ERR_SUCC;

   if (!(e = ecore_config_get(key)))
     {				/* prop doesn't exist yet */
	if ((ret = ecore_config_typed_add(key, val, type)) != ECORE_CONFIG_ERR_SUCC)	/* try to add it */
	   return ret;		/* ...failed */
	if (!(e = ecore_config_get(key)))	/* get handle */
	   return ECORE_CONFIG_ERR_FAIL;
	e->flags = e->flags & ~ECORE_CONFIG_FLAG_MODIFIED;
     }
   else if (!(e->flags & ECORE_CONFIG_FLAG_MODIFIED) && !(e->flags & ECORE_CONFIG_FLAG_SYSTEM))
     {
	ecore_config_typed_set(key, val, type);
	if (!(e = ecore_config_get(key)))       /* get handle */
	   return ECORE_CONFIG_ERR_FAIL;
	e->flags = e->flags & ~ECORE_CONFIG_FLAG_MODIFIED;
     }
   return ret;
}

/**
 * @defgroup Ecore_Config_Default_Group Ecore Config Defaults
 *
 * Functions that are used to set the default values of properties.
 */

/**
 * Sets the indicated property if it has not already been set or loaded.
 * @param   key  The property key.
 * @param   val  Default value of the key.
 * @param   lo   Lowest valid value for the key.
 * @param   hi   Highest valid value for the key.
 * @param   step Used by integer and float values.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there are no errors.
 * @note    The @p lo, @p hi and @p step parameters are only used when storing
 *          integer and float properties.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_default(const char *key, const char *val, float lo, float hi, float step)
{
   int                 ret, type;
   Ecore_Config_Prop  *e;

   type = ecore_config_type_guess(key, val);
   ret = ecore_config_typed_default(key, val, type);
   e = ecore_config_get(key);
   if (e)
     {
	if (type == ECORE_CONFIG_INT)
	  {
	     e->step = step;
	     e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
	     e->lo = lo;
	     e->hi = hi;
	     ecore_config_bound(e);
	  }
	else if (type == ECORE_CONFIG_FLT)
	  {
	     e->step = (int)(step * ECORE_CONFIG_FLOAT_PRECISION);
	     e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
	     e->lo = (int)(lo * ECORE_CONFIG_FLOAT_PRECISION);
	     e->hi = (int)(hi * ECORE_CONFIG_FLOAT_PRECISION);
	     ecore_config_bound(e);
	  }
     }

   return ret;
}

/**
 * Sets the indicated property to the given boolean if the property has not yet
 * been set.
 * @param   key The property key.
 * @param   val Boolean Integer to set the value to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there are no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_boolean_default(const char *key, int val)
{
   val = val ? 1 : 0;
   return ecore_config_typed_default(key, &val, ECORE_CONFIG_BLN);
}

/**
 * Sets the indicated property to the given integer if the property has not yet
 * been set.
 * @param   key The property key.
 * @param   val Integer to set the value to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there are no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_int_default(const char *key, int val)
{
   return ecore_config_typed_default(key, &val, ECORE_CONFIG_INT);
}

/**
 * Sets the indicated property to the given integer if the property has not yet
 * been set.
 *
 * The bounds and step values are set regardless.
 *
 * @param   key  The property key.
 * @param   val  Integer to set the property to.
 * @param   low  Lowest valid integer value for the property.
 * @param   high Highest valid integer value for the property.
 * @param   step Increment value for the property.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there were no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_int_default_bound(const char *key, int val, int low, int high,
			       int step)
{
   Ecore_Config_Prop  *e;
   int                 ret;

   ret = ecore_config_typed_default(key, &val, ECORE_CONFIG_INT);
   e = ecore_config_get(key);
   if (e)
     {
	e->step = step;
	e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
	e->lo = low;
	e->hi = high;
	ecore_config_bound(e);
     }

   return ret;
}

/**
 * Sets the indicated property to the given string if the property has not yet
 * been set.
 * @param   key The property key.
 * @param   val String to set the property to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there were no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_string_default(const char *key, const char *val)
{
   return ecore_config_typed_default(key, val, ECORE_CONFIG_STR);
}

/**
 * Sets the indicated property to the given float if the property has not yet
 * been set.
 * @param   key The property key.
 * @param   val Float to set the property to.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there were no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_float_default(const char *key, float val)
{
   return ecore_config_typed_default(key, &val, ECORE_CONFIG_FLT);
}

/**
 * Sets the indicated property to the given float if the property has not yet
 * been set.
 *
 * The bounds and step values are set regardless.
 *
 * @param   key  The property key.
 * @param   val  Float to set the property to.
 * @param   low  Lowest valid integer value for the property.
 * @param   high Highest valid float value for the property.
 * @param   step Increment value for the property.
 * @return  @c ECORE_CONFIG_ERR_SUCC if there were no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_float_default_bound(const char *key, float val, float low,
				 float high, float step)
{
   Ecore_Config_Prop  *e;
   int                 ret;

   ret = ecore_config_typed_default(key, &val, ECORE_CONFIG_FLT);
   e = ecore_config_get(key);
   if (e)
     {
	e->step = (int)(step * ECORE_CONFIG_FLOAT_PRECISION);
	e->flags |= ECORE_CONFIG_FLAG_BOUNDS;
	e->lo = (int)(low * ECORE_CONFIG_FLOAT_PRECISION);
	e->hi = (int)(high * ECORE_CONFIG_FLOAT_PRECISION);
	ecore_config_bound(e);
     }

   return ret;
}

/**
 * Sets the indicated property to a color value if the property has not yet
 * been set.
 * @param  key The property key.
 * @param  a integer 0..255
 * @param  r integer 0..255
 * @param  g integer 0..255
 * @param  b integer 0..255
 * @return @c ECORE_CONFIG_ERR_SUCC if there are no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_argb_default(const char *key, int a, int r, int g, int b)
{
   long v = 0;
   return ecore_config_typed_default(key, __ecore_argb_to_long(a,r,g,b, &v), ECORE_CONFIG_RGB);
}

/**
 * Sets the indicated property to a color value if the property has not yet
 * been set.
 * @param  key The property key.
 * @param  argb ARGB data as long
 * @return @c ECORE_CONFIG_ERR_SUCC if there are no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_argbint_default(const char *key, long argb)
{
   return ecore_config_typed_default(key, &argb, ECORE_CONFIG_RGB);
}

/**
 * Sets the indicated property to a color value if the property has not yet
 * been set.
 * @param  key The property key.
 * @param  val Color value in ARGB format.
 * @return @c ECORE_CONFIG_ERR_SUCC if there are no problems.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_argbstr_default(const char *key, const char *val)
{
   long v = 0;
   return ecore_config_typed_default(key, __ecore_argbstr_to_long(val, &v), ECORE_CONFIG_RGB);
}

/**
 * Sets the indicated property to a theme name if the property has not yet
 * been set.
 * @param   key The property key.
 * @param   val String giving the name of the theme.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Default_Group
 */
EAPI int
ecore_config_theme_default(const char *key, const char *val)
{
   return ecore_config_typed_default(key, val, ECORE_CONFIG_THM);
}

/**
 * @defgroup Ecore_Config_Struct_Group Ecore Config Structures
 *
 * Functions that are used to create structures of properties.
 */

/**
 * Sets the indicated property to a structure if the property has not yet
 * been set.
 * @param   key The property key.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_create(const char *key)
{
   WRN("you are using ecore_config structures. These are very young");
   WRN(" and not complete - you have been warned");

   return ecore_config_typed_default(key, NULL, ECORE_CONFIG_SCT);
}

static int
_ecore_config_struct_append(Ecore_Config_Prop *sct, Ecore_Config_Prop *add)
{
   Eina_List *l;

   if (!sct || !add || sct->type != ECORE_CONFIG_SCT)
     return ECORE_CONFIG_ERR_IGNORED;

   l = sct->data;
   sct->data = eina_list_append(l, add);
   add->parent = sct;

   return ECORE_CONFIG_ERR_SUCC;
}

static int
_ecore_config_struct_typed_add(const char *key, const char *name, const void *val,
    int type)
{
   char *subkey;
   int ret;

   subkey = malloc((strlen(key) + strlen(name) + 2) * sizeof(char));
   strcpy(subkey, key);
   strcat(subkey, ".");
   strcat(subkey, name);

   ecore_config_typed_default(subkey, val, type);
   ret = _ecore_config_struct_append(ecore_config_get(key),
                                     ecore_config_get(subkey));
   free(subkey);
   return ret;
}

/**
 * Add an int property to the named structure. The property is set if it has not
 * yet been set.
 * @param   key The key of the structure to add to.
 * @param   name The name of the item to add - this will be appended to the key
 * @param   val the int to default to
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_int_add(const char *key, const char *name, int val)
{
   return _ecore_config_struct_typed_add(key, name, &val, ECORE_CONFIG_INT);
}

/**
 * Add a float property to the named structure. The property is set if it has
 * not yet been set.
 * @param   key The key of the structure to add to.
 * @param   name The name of the item to add - this will be appended to the key
 * @param   val The float to default to
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_float_add(const char *key, const char *name, float val)
{
   return _ecore_config_struct_typed_add(key, name, &val, ECORE_CONFIG_FLT);
}

/**
 * Add a string property to the named structure. The property is set if it has
 * not yet been set.
 * @param   key The key of the structure to add to.
 * @param   name The name of the item to add - this will be appended to the key
 * @param   val The string to default to
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_string_add(const char *key, const char *name, const char* val)
{
   return _ecore_config_struct_typed_add(key, name, val, ECORE_CONFIG_STR);
}

/**
 * Add an argb property to the named structure. The property is set if it has
 * not yet been set.
 * @param   key The key of the structure to add to.
 * @param   name The name of the item to add - this will be appended to the key
 * @param   a The alpha to default to
 * @param   r The red to default to
 * @param   g The green to default to
 * @param   b The blue to default to
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_argb_add(const char *key, const char *name, int a, int r,
                             int g, int b)
{
   long argb;

   __ecore_argb_to_long(a, r, g, b, &argb);
   return _ecore_config_struct_typed_add(key, name, &argb, ECORE_CONFIG_RGB);
}

/**
 * Add a theme property to the named structure. The property is set if it has
 * not yet been set.
 * @param   key The key of the structure to add to.
 * @param   name The name of the item to add - this will be appended to the key
 * @param   val The theme name to default to
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_theme_add(const char *key, const char *name, const char* val)
{
   return _ecore_config_struct_typed_add(key, name, val, ECORE_CONFIG_THM);
}

/**
 * Add a boolean property to the named structure. The property is set if it has
 * not yet been set.
 * @param   key The key of the structure to add to.
 * @param   name The name of the item to add - this will be appended to the key
 * @param   val The boolean to default to
 * @return  @c ECORE_CONFIG_ERR_SUCC if the property is set successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_boolean_add(const char *key, const char *name, int val)
{
   val = val ? 1 : 0;
   return _ecore_config_struct_typed_add(key, name, &val, ECORE_CONFIG_BLN);
}

/**
 * Get the contents of a defined structure property and load it into the passed
 * C struct
 * @param   key The name of the structure property to look up.
 * @param   data The struct to write into.
 * @return  @c ECORE_CONFIG_ERR_SUCC if the structure is written successfully.
 * @ingroup Ecore_Config_Struct_Group
 */
EAPI int
ecore_config_struct_get(const char *key, void *data)
{
   Ecore_Config_Prop *e, *f;
   Eina_List *l;
   unsigned char *ptr;
   long argb;

   e = ecore_config_get(key);
   if (!e)
     return ECORE_CONFIG_ERR_NODATA;

   l = e->data;
   ptr = data;
   while (l)
     {
	f = (Ecore_Config_Prop *) l->data;
	switch (f->type)
	  {
	     case ECORE_CONFIG_INT:
	       *((int *) ptr) = _ecore_config_int_get(f);
	       ptr += sizeof(int);
	     break;
	     case ECORE_CONFIG_BLN:
	       *((int *) ptr) = _ecore_config_boolean_get(f);
	       ptr += sizeof(int);
	     break;
	     case ECORE_CONFIG_FLT:
	       *((float *) ptr) = _ecore_config_float_get(f);
	       ptr += sizeof(float);
	     break;
	     case ECORE_CONFIG_STR:
	     case ECORE_CONFIG_THM:
	       *((char **) ptr) = _ecore_config_string_get(f);
	       ptr += sizeof(char *);
	     break;
	     case ECORE_CONFIG_RGB:
	       argb = _ecore_config_argbint_get(f);
	       *((int *) ptr) = (argb >> 24) & 0xff;
	       ptr += sizeof(int);
	       *((int *) ptr) = (argb >> 16) & 0xff;
	       ptr += sizeof(int);
	       *((int *) ptr) = (argb >> 8) & 0xff;
	       ptr += sizeof(int);
	       *((int *) ptr) = argb & 0xff;
	       ptr += sizeof(int);
	     break;
	     default:
	       WRN("ARGH - STRUCT coding not implemented yet");
	  }
	l = eina_list_next(l);
     }
   return ECORE_CONFIG_ERR_SUCC;
}

/**
 * @defgroup Ecore_Config_Listeners_Group Ecore Config Listeners
 *
 * Functions that set and unset property listener callbacks.
 */

/**
 * Adds a callback function to the list of functions called when a property
 * changes.
 * @param   name     Name of the callback.
 * @param   key      The key of the property to listen to.
 * @param   listener Listener callback function.
 * @param   tag      Tag to pass to @p listener when it is called.
 * @param   data     Data to pass to @p listener when it is called.
 * @return  @c ECORE_CONFIG_ERR_SUCC if successful in setting up the callback.
 * @ingroup Ecore_Config_Listeners_Group
 */
EAPI int
ecore_config_listen(const char *name, const char *key,
		    Ecore_Config_Listener listener, int tag, void *data)
{
   Ecore_Config_Prop  *e;
   Ecore_Config_Listener_List *l;

   if (!key)
      return ECORE_CONFIG_ERR_NODATA;

   if (!(e = ecore_config_get(key)))
     {
	int                 ret = ecore_config_add(key, "");

	if (ret != ECORE_CONFIG_ERR_SUCC)
	  {
	     ERR("ecore_config_listen: ecore_config_add(\"%s\") failed: %d",
		 key, ret);
	     return ret;
	  }
	if (!(e = ecore_config_get(key)))
	  {
	     ERR("ecore_config_listen: list of properties corrupted!?");
	     return ECORE_CONFIG_ERR_FAIL;
	  }
     }

   for (l = e->listeners; l; l = l->next)
      if (!strcmp(l->name, name) || (l->listener == listener))
	{
	   ERR("ecore_config_listen: %s is already listening for changes of %s...",
	     name, key);
	   return ECORE_CONFIG_ERR_IGNORED;
	}

   if (!(l = malloc(sizeof(Ecore_Config_Listener_List))))
      return ECORE_CONFIG_ERR_OOM;

   ERR("registering listener \"%s\" for \"%s\" (%d)...", name, key, e->type);

   memset(l, 0, sizeof(Ecore_Config_Listener_List));

   l->listener = listener;
   l->name = name;
   l->data = data;
   l->tag = tag;
   l->next = e->listeners;
   e->listeners = l;

   if (e->type != ECORE_CONFIG_NIL)	/* call right on creation if prop exists and has val */
      listener(key, e->type, tag, data);

   return ECORE_CONFIG_ERR_SUCC;
}

/**
 * Removes a listener callback.
 * @param  name     Name of the callback to remove.
 * @param  key      The property key the callback is listening to.
 * @param  listener The callback function to remove.
 * @return @c ECORE_CONFIG_ERR_SUCC if successful in removing the callback.
 *         If no callback matches the given parameters, then
 *         @c ECORE_CONFIG_ERR_NOTFOUND is returned.  If @c NULL is passed
 *         for the key pointer, @c ECORE_CONFIG_ERR_NODATA is returned.
 * @ingroup Ecore_Config_Listeners_Group
 */
EAPI int
ecore_config_deaf(const char *name, const char *key,
		  Ecore_Config_Listener listener)
{
   Ecore_Config_Prop  *e;
   Ecore_Config_Listener_List *l, *p;
   int                 ret;

   ret = ECORE_CONFIG_ERR_NOTFOUND;

   if (!key)
      return ECORE_CONFIG_ERR_NODATA;

   if (!(e = ecore_config_get(key)))
      return ECORE_CONFIG_ERR_NOTFOUND;

   for (p = NULL, l = e->listeners; l; p = l)
     {
	Ecore_Config_Listener_List *nl;

	nl = l->next;
	if ((name && !strcmp(l->name, name)) || (l->listener == listener))
	  {
	     ret = ECORE_CONFIG_ERR_SUCC;
	     if (!p)
		e->listeners = e->listeners->next;
	     else
		p->next = l->next;
	     memset(l, 0, sizeof(Ecore_Config_Listener));
	     free(l);
	  }
	l = nl;
     }

   return ret;
}

/**
 * Locates the first configuration bundle on the given server.
 * @param  srv The configuration server.
 * @return Pointer to the first configuration bundle.
 */
EAPI Ecore_Config_Bundle *
ecore_config_bundle_1st_get(Ecore_Config_Server * srv)
{				/* anchor: global, but read-only */
   return srv->bundles;
}

/**
 * Locates the configuration bundle after the given one.
 * @param  ns  The configuration bundle.
 * @return The next configuration bundle.
 */
EAPI Ecore_Config_Bundle *
ecore_config_bundle_next_get(Ecore_Config_Bundle * ns)
{
   return ns ? ns->next : NULL;
}

/**
 * Locates a configuration bundle on a configuration server based on its serial
 * number.
 * @param  srv    The configuration server.
 * @param  serial Serial number.
 * @return The configuration bundle with the given serial number.
 */
EAPI Ecore_Config_Bundle *
ecore_config_bundle_by_serial_get(Ecore_Config_Server * srv, long serial)
{
   Ecore_Config_Bundle *eb;

   eb = srv->bundles;

   if (serial < 0)
      return NULL;
   else if (serial == 0)
     {
	Ecore_Config_Bundle *r = eb;

	return r;
     }

   while (eb)
     {
	if (eb->serial == serial)
	   return eb;
	eb = eb->next;
     }
   return NULL;
}

/**
 * Gets the Ecore_Config_Bundle with the given identifier from the given
 * server.
 * @param  srv   The configuration server.
 * @param  label The bundle's identifier string.
 * @return The bundle with the given identifier string, or @c NULL if it
 *         could not be found.
 */
EAPI Ecore_Config_Bundle *
ecore_config_bundle_by_label_get(Ecore_Config_Server * srv, const char *label)
{
   Ecore_Config_Bundle *ns;

   ns = srv->bundles;

   while (ns)
     {
	if (ns->identifier && !strcmp(ns->identifier, label))
	   return ns;
	ns = ns->next;
     }
   return NULL;
}

/**
 * Retrieves the bundle's serial number.
 * @param  ns The configuration bundle.
 * @return The bundle's identifier string, or -1 if ns is @c NULL.
 */
EAPI long
ecore_config_bundle_serial_get(Ecore_Config_Bundle * ns)
{
   return ns ? ns->serial : -1;
}

/**
 * Retrieves the bundle's identifier.
 * @param  ns The configuration bundle.
 * @return The bundle's identifer string.
 */
EAPI char               *
ecore_config_bundle_label_get(Ecore_Config_Bundle * ns)
{
   return ns ? ns->identifier : NULL;
}

/**
 * Creates a new Ecore_Config_Bundle.
 * @param  srv        Config server.
 * @param  identifier Identifier string for the new bundle.
 * @return A pointer to a new Ecore_Config_Bundle.  @c NULL is returned if the
 *         structure couldn't be allocated.
 */
EAPI Ecore_Config_Bundle *
ecore_config_bundle_new(Ecore_Config_Server * srv, const char *identifier)
{
   Ecore_Config_Bundle *t;
   static long         ss;

   ss = 0;			/* bundle unique serial */

   if ((t = malloc(sizeof(Ecore_Config_Bundle))))
     {
	memset(t, 0, sizeof(Ecore_Config_Bundle));

	t->identifier = (char *)identifier;
	t->serial = ++ss;
	t->owner = srv->name;
	t->next = srv->bundles;
	srv->bundles = t;
     }
   return t;
}

static Ecore_Config_Server *
do_init(const char *name)
{
   return _ecore_config_ipc_init(name);
}

static Ecore_Config_Server *
ecore_config_init_local(const char *name)
{
   char               *p;
   char               *buf;

   if ((p = getenv("HOME")))
     {				/* debug-only ### FIXME */
	if (!(buf = malloc(PATH_MAX * sizeof(char))))
	   return NULL;
	snprintf(buf, PATH_MAX, "%s/.ecore/%s/.global", p, name);
	unlink(buf);

	free(buf);
     }

   return do_init(name);
}

static Ecore_Config_Server *
ecore_config_init_global(const char *name)
{
   char               *p;
   int global;
   char               *buf;

   if ((p = getenv("HOME")))
     {				/* debug-only ### FIXME */
	if (!(buf = malloc(PATH_MAX * sizeof(char))))
	   return NULL;
	snprintf(buf, PATH_MAX, "%s/.ecore/%s/.global", p, name);
	global = creat(buf, S_IRWXU);

	if (global >= 0)
	   close(global);

	free(buf);
     }

   return do_init(name);
}

/**
 * @defgroup Ecore_Config_App_Lib_Group Ecore Config App Library Functions
 *
 * Functions that are used to start up and shutdown the Enlightened
 * Property Library when used directly by an application.
 */

/**
 * Initializes the Enlightened Property Library.
 *
 * Either this function or @ref ecore_config_system_init must be run
 * before any other function in the Enlightened Property Library, even
 * if you have run @ref ecore_init .  The name given is used to
 * determine the default configuration to load.
 *
 * @param  name Application name
 * @return @c ECORE_CONFIG_ERR_SUCC if the library is successfully set up.
 *         @c ECORE_CONFIG_ERR_FAIL otherwise.
 * @ingroup Ecore_Config_App_Lib_Group
 */
EAPI int
ecore_config_init(const char *name)
{
   char                *path;
   Ecore_Config_Prop   *list;
   _ecore_config_log_dom = eina_log_domain_register
     ("ecore_config", ECORE_CONFIG_DEFAULT_LOG_COLOR);
   if(_ecore_config_log_dom < 0)
     {
       EINA_LOG_ERR("Impossible to create a log domain for the Ecore config module.");
       return -1;
     }
   _ecore_config_system_init_no_load();

   __ecore_config_app_name = strdup(name);
   __ecore_config_server_local = ecore_config_init_local(name);
   if (!__ecore_config_server_local)
      return ECORE_CONFIG_ERR_FAIL;

   list = __ecore_config_bundle_local->data;
   free( __ecore_config_bundle_local );
   __ecore_config_bundle_local =
      ecore_config_bundle_new(__ecore_config_server_local, "config");
   __ecore_config_bundle_local->data = list;

   path = ecore_config_theme_default_path_get();
   ecore_config_string_default("/e/themes/search_path", path);
   if (path)
      free(path);

   list = ecore_config_get("/e/themes/search_path");
   if (list)
     {
	list->flags |= ECORE_CONFIG_FLAG_SYSTEM;
	list->flags &= ~ECORE_CONFIG_FLAG_MODIFIED;
     }

   return _ecore_config_system_load();
}

/**
 * Frees memory and shuts down the library for an application.
 * @return @c ECORE_CONFIG_ERR_IGNORED .
 * @ingroup Ecore_Config_App_Lib_Group
 */
EAPI int
ecore_config_shutdown(void)
{
   return ecore_config_system_shutdown();
}

/**
 * @defgroup Ecore_Config_Lib_Lib_Group Ecore Config Library Functions
 *
 * Functions that are used to start up and shutdown the Enlightened
 * Property Library when used directly by an application.
 */

/**
 * Initializes the Enlightened Property Library.
 *
 * This function is meant to be run from other programming libraries.
 * It should not be called from applications.
 *
 * This function (or @ref ecore_config_init )
 * must be run before any other function in the
 * Enlightened Property Library, even if you have run @ref ecore_init .
 *
 * @return @c ECORE_CONFIG_ERR_SUCC if the library is successfully set up.
 *         @c ECORE_CONFIG_ERR_FAIL otherwise.
 * @ingroup Ecore_Config_Lib_Lib_Group
 */
EAPI int
ecore_config_system_init(void)
{
   _ecore_config_system_init_no_load();
   return _ecore_config_system_load();
}

static            int
_ecore_config_system_init_no_load(void)
{
   char               *p;

   __ecore_config_system_init++;
   if (__ecore_config_system_init > 1)
      return ECORE_CONFIG_ERR_IGNORED;

   DEBUG = -1;
   if ((p = getenv("ECORE_CONFIG_DEBUG")) && p[0] != 0)
     {
	DEBUG = atoi(p);
     }

   __ecore_config_server_global =
      ecore_config_init_global(ECORE_CONFIG_GLOBAL_ID);
   if (!__ecore_config_server_global)
	return ECORE_CONFIG_ERR_FAIL;

   __ecore_config_bundle_local =
      ecore_config_bundle_new(__ecore_config_server_global, "system");

   /* set up a simple default path */
   ecore_config_string_default("/e/themes/search_path", PACKAGE_DATA_DIR "../ewl/themes");

   return ECORE_CONFIG_ERR_SUCC;
}


static             int
_ecore_config_system_load(void)
{
   char               *buf, *p;
   Ecore_Config_Prop  *sys;

   if (__ecore_config_system_init != 1)
	return ECORE_CONFIG_ERR_FAIL;

   if ((p = getenv("HOME")))
     {                          /* debug-only ### FIXME */
	if ((buf = malloc(PATH_MAX * sizeof(char))))
	  {
	     snprintf(buf, PATH_MAX, "%s/.e/config.eet", p);
	     if (ecore_config_file_load(buf) != 0) {
		/* even if this file (system.eet) doesn't exist we can
		 * continue without it as it isn't striclty necessary.
		*/
		ecore_config_file_load(PACKAGE_DATA_DIR "/system.eet");
	     }
	     sys = __ecore_config_bundle_local->data;
	     while (sys)
	       {
		  /* unmark it modified - modification will mean it has been overridden */
		  sys->flags &= ~ECORE_CONFIG_FLAG_MODIFIED;
		  /* mark as system so that examine can hide them */
		  sys->flags |= ECORE_CONFIG_FLAG_SYSTEM;
		  sys = sys->next;
	       }
	  }
	free(buf);
     }

   return ECORE_CONFIG_ERR_SUCC;
}


/**
 * Frees memory and shuts down the library for other programming libraries.
 * @return @c ECORE_CONFIG_ERR_IGNORED
 * @ingroup Ecore_Config_Lib_Lib_Group
 */
EAPI int
ecore_config_system_shutdown(void)
{
   int                 ret;

   __ecore_config_system_init--;
   if (__ecore_config_system_init > 0)
      return ECORE_CONFIG_ERR_IGNORED;

   ret = _ecore_config_ipc_exit();
   if (__ecore_config_app_name)
      free(__ecore_config_app_name);
   while(__ecore_config_bundle_local->data)
     ecore_config_dst(__ecore_config_bundle_local->data);
   free(__ecore_config_bundle_local);
   free(__ecore_config_server_local);
   free(__ecore_config_server_global);
   eina_log_domain_unregister(_ecore_config_log_dom);
   _ecore_config_log_dom = -1;
   return ret;
}

static inline void *
__ecore_argb_to_long(int a, int r, int g, int b, long *v)
{
   *v = ((a << 24) & 0xff000000 )
      | ((r << 16) &   0xff0000 )
      | ((g <<  8) &     0xff00 )
      | ( b        &       0xff );

   return v;
}

static inline void *
__ecore_argbstr_to_long(const char *argb, long *v)
{
   char *l = NULL;

   // convert hexadecimal string #..., #0x..., 0x..., ... to long
   if(*argb == '#')
     argb++;
   *v = (long)strtoul( argb, &l, 16);

   if(*l)
     {
	ERR("ecore_config_val: value \"%s\" not a valid hexadecimal RGB value?", argb);
	return NULL;
     }

   return v;
}