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
|
#include "parse.h"
#include <glib.h>
struct Callback {
char *tag;
ParseCallback func;
void *data;
};
static GHashTable *callbacks;
static xmlDocPtr doc_config = NULL;
static void destfunc(struct Callback *c)
{
g_free(c->tag);
g_free(c);
}
void parse_startup()
{
callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
(GDestroyNotify)destfunc);
}
void parse_shutdown()
{
xmlFree(doc_config);
doc_config = NULL;
g_hash_table_destroy(callbacks);
}
void parse_register(const char *tag, ParseCallback func, void *data)
{
struct Callback *c;
if ((c = g_hash_table_lookup(callbacks, tag))) {
g_warning("tag '%s' already registered", tag);
return;
}
c = g_new(struct Callback, 1);
c->tag = g_strdup(tag);
c->func = func;
c->data = data;
g_hash_table_insert(callbacks, c->tag, c);
}
void parse_config()
{
char *path;
xmlNodePtr node = NULL;
xmlLineNumbersDefault(1);
path = g_build_filename(g_get_home_dir(), ".openbox", "rc3", NULL);
if ((doc_config = xmlParseFile(path))) {
node = xmlDocGetRootElement(doc_config);
if (!node) {
xmlFreeDoc(doc_config);
doc_config = NULL;
g_warning("%s is an empty document", path);
} else {
if (xmlStrcasecmp(node->name, (const xmlChar*)"openbox_config")) {
xmlFreeDoc(doc_config);
doc_config = NULL;
g_warning("document %s is of wrong type. root node is "
"not 'openbox_config'", path);
}
}
}
g_free(path);
if (!doc_config) {
path = g_build_filename(RCDIR, "rc3", NULL);
if ((doc_config = xmlParseFile(path))) {
node = xmlDocGetRootElement(doc_config);
if (!node) {
xmlFreeDoc(doc_config);
doc_config = NULL;
g_warning("%s is an empty document", path);
} else {
if (xmlStrcasecmp(node->name,
(const xmlChar*)"openbox_config")) {
xmlFreeDoc(doc_config);
doc_config = NULL;
g_warning("document %s is of wrong type. root node is "
"not 'openbox_config'", path);
}
}
}
g_free(path);
}
if (!doc_config) {
g_message("unable to find a valid config file, using defaults");
} else {
parse_tree(doc_config, node->xmlChildrenNode, NULL);
}
}
void parse_tree(xmlDocPtr doc, xmlNodePtr node, void *nothing)
{
while (node) {
struct Callback *c = g_hash_table_lookup(callbacks, node->name);
if (c)
c->func(doc, node->xmlChildrenNode, c->data);
node = node->next;
}
}
char *parse_string(xmlDocPtr doc, xmlNodePtr node)
{
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
char *s = g_strdup((char*)c);
xmlFree(c);
return s;
}
int parse_int(xmlDocPtr doc, xmlNodePtr node)
{
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
int i = atoi((char*)c);
xmlFree(c);
return i;
}
gboolean parse_bool(xmlDocPtr doc, xmlNodePtr node)
{
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
gboolean b = FALSE;
if (!xmlStrcasecmp(c, (const xmlChar*) "true"))
b = TRUE;
else if (!xmlStrcasecmp(c, (const xmlChar*) "yes"))
b = TRUE;
else if (!xmlStrcasecmp(c, (const xmlChar*) "on"))
b = TRUE;
xmlFree(c);
return b;
}
gboolean parse_contains(const char *val, xmlDocPtr doc, xmlNodePtr node)
{
xmlChar *c = xmlNodeListGetString(doc, node->xmlChildrenNode, TRUE);
gboolean r;
r = !xmlStrcasecmp(c, (const xmlChar*) val);
xmlFree(c);
return r;
}
xmlNodePtr parse_find_node(const char *tag, xmlNodePtr node)
{
while (node) {
if (!xmlStrcasecmp(node->name, (const xmlChar*) tag))
return node;
node = node->next;
}
return NULL;
}
gboolean parse_attr_int(const char *name, xmlNodePtr node, int *value)
{
xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
gboolean r = FALSE;
if (c) {
*value = atoi((char*)c);
r = TRUE;
}
xmlFree(c);
return r;
}
gboolean parse_attr_string(const char *name, xmlNodePtr node, char **value)
{
xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
gboolean r = FALSE;
if (c) {
*value = g_strdup((char*)c);
r = TRUE;
}
xmlFree(c);
return r;
}
Action *parse_action(xmlDocPtr doc, xmlNodePtr node)
{
char *actname;
Action *act = NULL;
xmlNodePtr n;
if (parse_attr_string("name", node, &actname)) {
if ((act = action_from_string(actname))) {
if (act->func == action_execute || act->func == action_restart) {
if ((n = parse_find_node("execute", node->xmlChildrenNode)))
act->data.execute.path = parse_string(doc, n);
} else if (act->func == action_showmenu) {
if ((n = parse_find_node("menu", node->xmlChildrenNode)))
act->data.showmenu.name = parse_string(doc, n);
} else if (act->func == action_desktop) {
if ((n = parse_find_node("desktop", node->xmlChildrenNode)))
act->data.desktop.desk = parse_int(doc, n);
if (act->data.desktop.desk > 0) act->data.desktop.desk--;
} else if (act->func == action_send_to_desktop) {
if ((n = parse_find_node("desktop", node->xmlChildrenNode)))
act->data.sendto.desk = parse_int(doc, n);
if (act->data.sendto.desk > 0) act->data.sendto.desk--;
} else if (act->func == action_move_relative_horz ||
act->func == action_move_relative_vert ||
act->func == action_resize_relative_horz ||
act->func == action_resize_relative_vert) {
if ((n = parse_find_node("delta", node->xmlChildrenNode)))
act->data.relative.delta = parse_int(doc, n);
} else if (act->func == action_desktop_right ||
act->func == action_desktop_left ||
act->func == action_desktop_up ||
act->func == action_desktop_down) {
if ((n = parse_find_node("wrap", node->xmlChildrenNode))) {
g_message("WRAP %d", parse_bool(doc, n));
act->data.desktopdir.wrap = parse_bool(doc, n);
}
} else if (act->func == action_send_to_desktop_right ||
act->func == action_send_to_desktop_left ||
act->func == action_send_to_desktop_up ||
act->func == action_send_to_desktop_down) {
if ((n = parse_find_node("wrap", node->xmlChildrenNode)))
act->data.sendtodir.wrap = parse_bool(doc, n);
if ((n = parse_find_node("follow", node->xmlChildrenNode)))
act->data.sendtodir.follow = parse_bool(doc, n);
}
}
}
return act;
}
gboolean parse_attr_contains(const char *val, xmlNodePtr node,
const char *name)
{
xmlChar *c = xmlGetProp(node, (const xmlChar*) name);
gboolean r;
r = !xmlStrcasecmp(c, (const xmlChar*) val);
xmlFree(c);
return r;
}
|