]> icculus.org git repositories - dana/openbox.git/blob - openbox/config_value.c
Make warnings about parse problems in .desktop files "debug" messages. Most people...
[dana/openbox.git] / openbox / config_value.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    config_value.c for the Openbox window manager
4    Copyright (c) 2011        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "config_value.h"
20 #include "action_list.h"
21 #include "geom.h"
22
23 #include "stdlib.h"
24
25 struct _ObConfigValue {
26     gint ref;
27     enum {
28         OB_CV_STRING,
29         OB_CV_LIST,
30         OB_CV_ACTION_LIST
31     } type;
32     union {
33         gchar *string;
34         gchar **list;
35         ObActionList *actions;
36     } v;
37 };
38
39 void config_value_ref(ObConfigValue *v)
40 {
41     ++v->ref;
42 }
43
44 void config_value_unref(ObConfigValue *v)
45 {
46     if (v && --v->ref < 1) {
47         switch (v->type) {
48         case OB_CV_STRING:
49             g_free(v->v.string);
50             break;
51         case OB_CV_LIST:
52             g_strfreev(v->v.list);
53             break;
54         case OB_CV_ACTION_LIST:
55             action_list_unref(v->v.actions);
56             break;
57         }
58         g_slice_free(ObConfigValue, v);
59     }
60 }
61
62 /*************************** describer functions ***************************/
63
64 gboolean config_value_is_string(const ObConfigValue *v)
65 {
66     g_return_val_if_fail(v != NULL, FALSE);
67     return v->type == OB_CV_STRING;
68 }
69
70 gboolean config_value_is_string_list(const ObConfigValue *v)
71 {
72     g_return_val_if_fail(v != NULL, FALSE);
73     return v->type == OB_CV_LIST;
74 }
75
76 gboolean config_value_is_action_list(const ObConfigValue *v)
77 {
78     g_return_val_if_fail(v != NULL, FALSE);
79     return v->type == OB_CV_ACTION_LIST;
80 }
81
82 /***************************** getter functions ****************************/
83
84 const gchar* config_value_string(ObConfigValue *v)
85 {
86     g_return_val_if_fail(v != NULL, NULL);
87     g_return_val_if_fail(config_value_is_string(v), NULL);
88     return v->v.string;
89 }
90 gboolean config_value_bool(ObConfigValue *v)
91 {
92     g_return_val_if_fail(v != NULL, FALSE);
93     g_return_val_if_fail(config_value_is_string(v), FALSE);
94     return (g_strcasecmp(v->v.string, "true") == 0 ||
95             g_strcasecmp(v->v.string, "yes") == 0);
96 }
97 guint config_value_int(ObConfigValue *v)
98 {
99     gchar *s;
100     g_return_val_if_fail(v != NULL, FALSE);
101     g_return_val_if_fail(config_value_is_string(v), FALSE);
102     s = v->v.string;
103     return strtol(s, &s, 10);
104 }
105 void config_value_fraction(ObConfigValue *v, gint *numer, gint *denom)
106 {
107     gchar *s;
108
109     *numer = *denom = 0;
110
111     g_return_if_fail(v != NULL);
112     g_return_if_fail(config_value_is_string(v));
113
114     s = v->v.string;
115     *numer = strtol(s, &s, 10);
116     if (*s == '%')
117         *denom = 100;
118     else if (*s == '/')
119         *denom = atoi(s+1);
120     else
121         *denom = 0;
122 }
123 void config_value_gravity_coord(ObConfigValue *v, GravityCoord *c)
124 {
125     gchar *s;
126
127     c->center = FALSE;
128     c->pos = 0;
129     c->denom = 0;
130
131     g_return_if_fail(v != NULL);
132     g_return_if_fail(config_value_is_string(v));
133
134     s = v->v.string;
135     if (!g_ascii_strcasecmp(s, "center"))
136         c->center = TRUE;
137     else {
138         if (s[0] == '-')
139             c->opposite = TRUE;
140         if (s[0] == '-' || s[0] == '+')
141             ++s;
142
143         c->pos = strtol(s, &s, 10);
144
145         if (*s == '%')
146             c->denom = 100;
147         else if (*s == '/')
148             c->denom = atoi(s+1);
149     }
150 }
151 const gchar *const* config_value_string_list(ObConfigValue *v)
152 {
153     g_return_val_if_fail(v != NULL, NULL);
154     g_return_val_if_fail(config_value_is_string_list(v), NULL);
155     return (const gchar**)v->v.list;
156 }
157 ObActionList* config_value_action_list(ObConfigValue *v)
158 {
159     g_return_val_if_fail(v != NULL, NULL);
160     g_return_val_if_fail(config_value_is_action_list(v), NULL);
161     return v->v.actions;
162 }
163
164 /****************************** constructors ******************************/
165
166 ObConfigValue* config_value_new_string(const gchar *s)
167 {
168     g_return_val_if_fail(s != NULL, NULL);
169     return config_value_new_string_steal(g_strdup(s));
170 }
171
172 ObConfigValue* config_value_new_string_steal(gchar *s)
173 {
174     ObConfigValue *v;
175     g_return_val_if_fail(s != NULL, NULL);
176     v = g_slice_new(ObConfigValue);
177     v->ref = 1;
178     v->type = OB_CV_STRING;
179     v->v.string = s;
180     return v;
181 }
182
183 ObConfigValue* config_value_new_string_list(gchar **list)
184 {
185     return config_value_new_string_list_steal(g_strdupv(list));
186 }
187
188 ObConfigValue* config_value_new_string_list_steal(gchar **list)
189 {
190     ObConfigValue *v = g_slice_new(ObConfigValue);
191     v->ref = 1;
192     v->type = OB_CV_LIST;
193     v->v.list = list;
194     return v;
195 }
196
197 ObConfigValue* config_value_new_action_list(ObActionList *al)
198 {
199     ObConfigValue *v = g_slice_new(ObConfigValue);
200     v->ref = 1;
201     v->type = OB_CV_ACTION_LIST;
202     v->v.actions = al;
203     action_list_ref(al);
204     return v;
205 }