1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 config_value.c for the Openbox window manager
4 Copyright (c) 2011 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "config_value.h"
20 #include "action_list.h"
25 struct _ObConfigValue {
35 ObActionList *actions;
39 void config_value_ref(ObConfigValue *v)
44 void config_value_unref(ObConfigValue *v)
46 if (v && --v->ref < 1) {
53 for (it = v->v.list; it; it = g_list_next(it))
54 config_value_unref(it->data);
55 g_list_free(v->v.list);
58 case OB_CV_ACTION_LIST:
59 action_list_unref(v->v.actions);
62 g_slice_free(ObConfigValue, v);
66 /*************************** describer functions ***************************/
68 gboolean config_value_is_string(const ObConfigValue *v)
70 g_return_val_if_fail(v != NULL, FALSE);
71 return v->type == OB_CV_STRING;
74 gboolean config_value_is_list(const ObConfigValue *v)
76 g_return_val_if_fail(v != NULL, FALSE);
77 return v->type == OB_CV_LIST;
80 gboolean config_value_is_action_list(const ObConfigValue *v)
82 g_return_val_if_fail(v != NULL, FALSE);
83 return v->type == OB_CV_ACTION_LIST;
86 /**************************** pointer functions ****************************/
88 void config_value_copy_ptr(ObConfigValue *v,
89 ObConfigValueDataType type,
90 ObConfigValueDataPtr p,
91 const ObConfigValueEnum e[])
94 case OB_CONFIG_VALUE_STRING:
95 *p.string = config_value_string(v);
97 case OB_CONFIG_VALUE_BOOLEAN:
98 *p.boolean = config_value_bool(v);
100 case OB_CONFIG_VALUE_INTEGER:
101 *p.integer = config_value_int(v);
103 case OB_CONFIG_VALUE_ENUM:
104 *p.enumeration = config_value_enum(v, e);
106 case OB_CONFIG_VALUE_FRACTION: {
108 config_value_fraction(v, &n, &d);
109 p.fraction->numer = n;
110 p.fraction->denom = d;
113 case OB_CONFIG_VALUE_GRAVITY_COORD: {
115 config_value_gravity_coord(v, &c);
119 case OB_CONFIG_VALUE_LIST:
120 *p.list = config_value_list(v);
122 case OB_CONFIG_VALUE_ACTIONLIST:
123 *p.actions = config_value_action_list(v);
125 case NUM_OB_CONFIG_VALUE_TYPES:
127 g_assert_not_reached();
132 /***************************** getter functions ****************************/
134 const gchar* config_value_string(ObConfigValue *v)
136 g_return_val_if_fail(v != NULL, NULL);
137 g_return_val_if_fail(config_value_is_string(v), NULL);
140 gboolean config_value_bool(ObConfigValue *v)
142 g_return_val_if_fail(v != NULL, FALSE);
143 g_return_val_if_fail(config_value_is_string(v), FALSE);
144 return (g_strcasecmp(v->v.string, "true") == 0 ||
145 g_strcasecmp(v->v.string, "yes") == 0);
147 guint config_value_int(ObConfigValue *v)
150 g_return_val_if_fail(v != NULL, FALSE);
151 g_return_val_if_fail(config_value_is_string(v), FALSE);
153 return strtol(s, &s, 10);
155 guint config_value_enum(ObConfigValue *v, const ObConfigValueEnum choices[])
157 const ObConfigValueEnum *e;
159 g_return_val_if_fail(v != NULL, (guint)-1);
160 g_return_val_if_fail(config_value_is_string(v), (guint)-1);
161 g_return_val_if_fail(choices != NULL, (guint)-1);
163 for (e = choices; e->name; ++e)
164 if (g_strcasecmp(v->v.string, e->name) == 0)
168 void config_value_fraction(ObConfigValue *v, gint *numer, gint *denom)
174 g_return_if_fail(v != NULL);
175 g_return_if_fail(config_value_is_string(v));
178 *numer = strtol(s, &s, 10);
186 void config_value_gravity_coord(ObConfigValue *v, GravityCoord *c)
194 g_return_if_fail(v != NULL);
195 g_return_if_fail(config_value_is_string(v));
198 if (!g_ascii_strcasecmp(s, "center"))
203 if (s[0] == '-' || s[0] == '+')
206 c->pos = strtol(s, &s, 10);
211 c->denom = atoi(s+1);
214 GList* config_value_list(ObConfigValue *v)
216 g_return_val_if_fail(v != NULL, NULL);
217 g_return_val_if_fail(config_value_is_list(v), NULL);
220 ObActionList* config_value_action_list(ObConfigValue *v)
222 g_return_val_if_fail(v != NULL, NULL);
223 g_return_val_if_fail(config_value_is_action_list(v), NULL);
227 /****************************** constructors ******************************/
229 ObConfigValue* config_value_new_string(const gchar *s)
231 g_return_val_if_fail(s != NULL, NULL);
232 return config_value_new_string_steal(g_strdup(s));
235 ObConfigValue* config_value_new_string_steal(gchar *s)
238 g_return_val_if_fail(s != NULL, NULL);
239 v = g_slice_new(ObConfigValue);
241 v->type = OB_CV_STRING;
246 ObConfigValue* config_value_new_list(GList *list)
248 GList *c = g_list_copy(list);
251 for (it = c; it; it = g_list_next(it))
252 config_value_ref(it->data);
253 return config_value_new_list_steal(c);
256 ObConfigValue* config_value_new_list_steal(GList *list)
258 ObConfigValue *v = g_slice_new(ObConfigValue);
260 v->type = OB_CV_LIST;
265 ObConfigValue* config_value_new_action_list(ObActionList *al)
267 ObConfigValue *v = g_slice_new(ObConfigValue);
269 v->type = OB_CV_ACTION_LIST;