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"
24 #include "translate.h"
28 struct _ObConfigValue {
38 ObActionList *actions;
42 void config_value_ref(ObConfigValue *v)
47 void config_value_unref(ObConfigValue *v)
49 if (v && --v->ref < 1) {
55 g_strfreev(v->v.list);
57 case OB_CV_ACTION_LIST:
58 action_list_unref(v->v.actions);
61 g_slice_free(ObConfigValue, v);
65 /*************************** describer functions ***************************/
67 gboolean config_value_is_string(const ObConfigValue *v)
69 g_return_val_if_fail(v != NULL, FALSE);
70 return v->type == OB_CV_STRING;
73 gboolean config_value_is_string_list(const ObConfigValue *v)
75 g_return_val_if_fail(v != NULL, FALSE);
76 return v->type == OB_CV_LIST;
79 gboolean config_value_is_action_list(const ObConfigValue *v)
81 g_return_val_if_fail(v != NULL, FALSE);
82 return v->type == OB_CV_ACTION_LIST;
85 /**************************** pointer functions ****************************/
87 gboolean config_value_copy_ptr(ObConfigValue *v,
88 ObConfigValueDataType type,
89 ObConfigValueDataPtr p,
90 const ObConfigValueEnum e[])
95 case OB_CONFIG_VALUE_STRING:
96 *p.string = config_value_string(v);
98 case OB_CONFIG_VALUE_BOOLEAN:
99 *p.boolean = config_value_bool(v);
101 case OB_CONFIG_VALUE_INTEGER:
102 *p.integer = config_value_int(v);
104 case OB_CONFIG_VALUE_ENUM:
105 ok = config_value_enum(v, e, p.enumeration);
107 case OB_CONFIG_VALUE_FRACTION: {
109 config_value_fraction(v, &n, &d);
110 p.fraction->numer = n;
111 p.fraction->denom = d;
114 case OB_CONFIG_VALUE_GRAVITY_COORD: {
115 config_value_gravity_coord(v, p.coord);
118 case OB_CONFIG_VALUE_KEY:
119 ok = config_value_key(v, p.key);
121 case OB_CONFIG_VALUE_BUTTON:
122 ok = config_value_button(v, p.button);
124 case OB_CONFIG_VALUE_STRING_LIST: {
125 *p.list = config_value_string_list(v);
128 case OB_CONFIG_VALUE_ACTIONLIST:
129 *p.actions = config_value_action_list(v);
131 case NUM_OB_CONFIG_VALUE_TYPES:
132 g_assert_not_reached();
138 /***************************** getter functions ****************************/
140 const gchar* config_value_string(ObConfigValue *v)
142 g_return_val_if_fail(v != NULL, NULL);
143 g_return_val_if_fail(config_value_is_string(v), NULL);
146 gboolean config_value_bool(ObConfigValue *v)
148 g_return_val_if_fail(v != NULL, FALSE);
149 g_return_val_if_fail(config_value_is_string(v), FALSE);
150 return (g_strcasecmp(v->v.string, "true") == 0 ||
151 g_strcasecmp(v->v.string, "yes") == 0);
153 guint config_value_int(ObConfigValue *v)
156 g_return_val_if_fail(v != NULL, FALSE);
157 g_return_val_if_fail(config_value_is_string(v), FALSE);
159 return strtol(s, &s, 10);
161 gboolean config_value_enum(ObConfigValue *v, const ObConfigValueEnum choices[],
164 const ObConfigValueEnum *e;
166 g_return_val_if_fail(v != NULL, FALSE);
167 g_return_val_if_fail(config_value_is_string(v), FALSE);
168 g_return_val_if_fail(choices != NULL, FALSE);
170 for (e = choices; e->name; ++e)
171 if (g_strcasecmp(v->v.string, e->name) == 0) {
177 void config_value_fraction(ObConfigValue *v, gint *numer, gint *denom)
183 g_return_if_fail(v != NULL);
184 g_return_if_fail(config_value_is_string(v));
187 *numer = strtol(s, &s, 10);
195 void config_value_gravity_coord(ObConfigValue *v, GravityCoord *c)
203 g_return_if_fail(v != NULL);
204 g_return_if_fail(config_value_is_string(v));
207 if (!g_ascii_strcasecmp(s, "center"))
212 if (s[0] == '-' || s[0] == '+')
215 c->pos = strtol(s, &s, 10);
220 c->denom = atoi(s+1);
223 gboolean config_value_key(ObConfigValue *v, struct _ObKeyboardKey *k)
228 g_return_val_if_fail(v != NULL, FALSE);
229 g_return_val_if_fail(config_value_is_string(v), FALSE);
230 return translate_key(v->v.string, &k->modifiers, &k->keycode);
232 gboolean config_value_button(ObConfigValue *v, struct _ObMouseButton *b)
237 g_return_val_if_fail(v != NULL, FALSE);
238 g_return_val_if_fail(config_value_is_string(v), FALSE);
239 return translate_button(v->v.string, &b->modifiers, &b->button);
241 const gchar *const* config_value_string_list(ObConfigValue *v)
243 g_return_val_if_fail(v != NULL, NULL);
244 g_return_val_if_fail(config_value_is_string_list(v), NULL);
245 return (const gchar**)v->v.list;
247 ObActionList* config_value_action_list(ObConfigValue *v)
249 g_return_val_if_fail(v != NULL, NULL);
250 g_return_val_if_fail(config_value_is_action_list(v), NULL);
254 /****************************** constructors ******************************/
256 ObConfigValue* config_value_new_string(const gchar *s)
258 g_return_val_if_fail(s != NULL, NULL);
259 return config_value_new_string_steal(g_strdup(s));
262 ObConfigValue* config_value_new_string_steal(gchar *s)
265 g_return_val_if_fail(s != NULL, NULL);
266 v = g_slice_new(ObConfigValue);
268 v->type = OB_CV_STRING;
273 ObConfigValue* config_value_new_string_list(gchar **list)
275 return config_value_new_string_list_steal(g_strdupv(list));
278 ObConfigValue* config_value_new_string_list_steal(gchar **list)
280 ObConfigValue *v = g_slice_new(ObConfigValue);
282 v->type = OB_CV_LIST;
287 ObConfigValue* config_value_new_action_list(ObActionList *al)
289 ObConfigValue *v = g_slice_new(ObConfigValue);
291 v->type = OB_CV_ACTION_LIST;