db8856e69c71ee9172a9057079bfff06998424be
[dana/openbox.git] / openbox / config.c
1 #include "config.h"
2 #include "parse.h"
3
4 gboolean config_focus_new;
5 gboolean config_focus_follow;
6 gboolean config_focus_last;
7 gboolean config_focus_last_on_desktop;
8
9 char *config_theme;
10
11 int config_desktops_num;
12 GSList *config_desktops_names;
13
14 static void parse_focus(char *name, ParseToken *value)
15 {
16     if (!g_ascii_strcasecmp(name, "focusnew")) {
17         if (value->type != TOKEN_BOOL)
18             yyerror("invalid value");
19         else {
20             config_focus_new = value->data.bool;
21         }
22     } else if (!g_ascii_strcasecmp(name, "followmouse")) {
23         if (value->type != TOKEN_BOOL)
24             yyerror("invalid value");
25         else {
26             config_focus_follow = value->data.bool;
27         }
28     } else if (!g_ascii_strcasecmp(name, "focuslast")) {
29         if (value->type != TOKEN_BOOL)
30             yyerror("invalid value");
31         else {
32             config_focus_last = value->data.bool;
33         }
34     } else if (!g_ascii_strcasecmp(name, "focuslastondesktop")) {
35         if (value->type != TOKEN_BOOL)
36             yyerror("invalid value");
37         else {
38             config_focus_last_on_desktop = value->data.bool;
39         }
40     } else
41         yyerror("invalid option");
42     parse_free_token(value);
43 }
44
45 static void parse_theme(char *name, ParseToken *value)
46 {
47     if (!g_ascii_strcasecmp(name, "theme")) {
48         if (value->type != TOKEN_STRING)
49             yyerror("invalid value");
50         else {
51             g_free(config_theme);
52             config_theme = g_strdup(value->data.string);
53         }
54     } else
55         yyerror("invalid option");
56     parse_free_token(value);
57 }
58
59 static void parse_desktops(char *name, ParseToken *value)
60 {
61     GList *it;
62
63     if (!g_ascii_strcasecmp(name, "number")) {
64         if (value->type != TOKEN_INTEGER)
65             yyerror("invalid value");
66         else {
67             config_desktops_num = value->data.integer;
68         }
69     } else if (!g_ascii_strcasecmp(name, "names")) {
70         if (value->type == TOKEN_LIST) {
71             for (it = value->data.list; it; it = it->next)
72                 if (((ParseToken*)it->data)->type != TOKEN_STRING) break;
73             if (it == NULL) {
74                 /* build a string list */
75                 g_free(config_desktops_names);
76                 for (it = value->data.list; it; it = it->next)
77                     config_desktops_names =
78                         g_slist_append(config_desktops_names,
79                                        g_strdup
80                                        (((ParseToken*)it->data)->data.string));
81             } else {
82                 yyerror("invalid string in names list");
83             }
84         } else {
85             yyerror("syntax error (expected list of strings)");
86         }
87     } else
88         yyerror("invalid option");
89     parse_free_token(value);
90 }
91
92 void config_startup()
93 {
94     config_focus_new = TRUE;
95     config_focus_follow = FALSE;
96     config_focus_last = TRUE;
97     config_focus_last_on_desktop = TRUE;
98
99     parse_reg_section("focus", NULL, parse_focus);
100
101     config_theme = NULL;
102
103     parse_reg_section("theme", NULL, parse_theme);
104
105     config_desktops_num = 4;
106     config_desktops_names = NULL;
107
108     parse_reg_section("desktops", NULL, parse_desktops);
109 }
110
111 void config_shutdown()
112 {
113     GSList *it;
114
115     g_free(config_theme);
116
117     for (it = config_desktops_names; it; it = it->next)
118         g_free(it->data);
119     g_slist_free(config_desktops_names);
120 }