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) {
52 g_strfreev(v->v.list);
54 case OB_CV_ACTION_LIST:
55 action_list_unref(v->v.actions);
58 g_slice_free(ObConfigValue, v);
62 /*************************** describer functions ***************************/
64 gboolean config_value_is_string(const ObConfigValue *v)
66 g_return_val_if_fail(v != NULL, FALSE);
67 return v->type == OB_CV_STRING;
70 gboolean config_value_is_string_list(const ObConfigValue *v)
72 g_return_val_if_fail(v != NULL, FALSE);
73 return v->type == OB_CV_LIST;
76 gboolean config_value_is_action_list(const ObConfigValue *v)
78 g_return_val_if_fail(v != NULL, FALSE);
79 return v->type == OB_CV_ACTION_LIST;
82 /***************************** getter functions ****************************/
84 const gchar* config_value_string(ObConfigValue *v)
86 g_return_val_if_fail(v != NULL, NULL);
87 g_return_val_if_fail(config_value_is_string(v), NULL);
90 gboolean config_value_bool(ObConfigValue *v)
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);
97 guint config_value_int(ObConfigValue *v)
100 g_return_val_if_fail(v != NULL, FALSE);
101 g_return_val_if_fail(config_value_is_string(v), FALSE);
103 return strtol(s, &s, 10);
105 void config_value_fraction(ObConfigValue *v, gint *numer, gint *denom)
111 g_return_if_fail(v != NULL);
112 g_return_if_fail(config_value_is_string(v));
115 *numer = strtol(s, &s, 10);
123 void config_value_gravity_coord(ObConfigValue *v, GravityCoord *c)
131 g_return_if_fail(v != NULL);
132 g_return_if_fail(config_value_is_string(v));
135 if (!g_ascii_strcasecmp(s, "center"))
140 if (s[0] == '-' || s[0] == '+')
143 c->pos = strtol(s, &s, 10);
148 c->denom = atoi(s+1);
151 const gchar *const* config_value_string_list(ObConfigValue *v)
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;
157 ObActionList* config_value_action_list(ObConfigValue *v)
159 g_return_val_if_fail(v != NULL, NULL);
160 g_return_val_if_fail(config_value_is_action_list(v), NULL);
164 /****************************** constructors ******************************/
166 ObConfigValue* config_value_new_string(const gchar *s)
168 g_return_val_if_fail(s != NULL, NULL);
169 return config_value_new_string_steal(g_strdup(s));
172 ObConfigValue* config_value_new_string_steal(gchar *s)
175 g_return_val_if_fail(s != NULL, NULL);
176 v = g_slice_new(ObConfigValue);
178 v->type = OB_CV_STRING;
183 ObConfigValue* config_value_new_string_list(gchar **list)
185 return config_value_new_string_list_steal(g_strdupv(list));
188 ObConfigValue* config_value_new_string_list_steal(gchar **list)
190 ObConfigValue *v = g_slice_new(ObConfigValue);
192 v->type = OB_CV_LIST;
197 ObConfigValue* config_value_new_action_list(ObActionList *al)
199 ObConfigValue *v = g_slice_new(ObConfigValue);
201 v->type = OB_CV_ACTION_LIST;