]> icculus.org git repositories - dana/openbox.git/blob - openbox/action_value.c
register filters on startup
[dana/openbox.git] / openbox / action_value.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    action_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 "action_value.h"
20 #include "action_list.h"
21 #include "geom.h"
22
23 #include "stdlib.h"
24
25 struct _ObActionValue {
26     gint ref;
27     enum {
28         OB_AV_STRING,
29         OB_AV_ACTIONLIST
30     } type;
31     union {
32         gchar *string;
33         gboolean boolean;
34         guint integer;
35         ObActionList *actions;
36     } v;
37 };
38
39 ObActionValue* action_value_new_string(const gchar *s)
40 {
41     return action_value_new_string_steal(g_strdup(s));
42 }
43
44 ObActionValue* action_value_new_string_steal(gchar *s)
45 {
46     ObActionValue *v = g_slice_new(ObActionValue);
47     v->ref = 1;
48     v->type = OB_AV_STRING;
49     v->v.string = s;
50     return v;
51 }
52
53 ObActionValue* action_value_new_action_list(ObActionList *al)
54 {
55     ObActionValue *v = g_slice_new(ObActionValue);
56     v->ref = 1;
57     v->type = OB_AV_ACTIONLIST;
58     v->v.actions = al;
59     action_list_ref(al);
60     return v;
61 }
62
63 void action_value_ref(ObActionValue *v)
64 {
65     ++v->ref;
66 }
67
68 void action_value_unref(ObActionValue *v)
69 {
70     if (v && --v->ref < 1) {
71         switch (v->type) {
72         case OB_AV_STRING:
73             g_free(v->v.string);
74             break;
75         case OB_AV_ACTIONLIST:
76             action_list_unref(v->v.actions);
77             break;
78         }
79         g_slice_free(ObActionValue, v);
80     }
81 }
82
83 gboolean action_value_is_string(ObActionValue *v)
84 {
85     return v->type == OB_AV_STRING;
86 }
87
88 gboolean action_value_is_action_list(ObActionValue *v)
89 {
90     return v->type == OB_AV_ACTIONLIST;
91 }
92
93 const gchar* action_value_string(ObActionValue *v)
94 {
95     g_return_val_if_fail(v->type == OB_AV_STRING, NULL);
96     return v->v.string;
97 }
98
99 gboolean action_value_bool(ObActionValue *v)
100 {
101     g_return_val_if_fail(v->type == OB_AV_STRING, FALSE);
102     if (g_strcasecmp(v->v.string, "true") == 0 ||
103         g_strcasecmp(v->v.string, "yes") == 0)
104         return TRUE;
105     else
106         return FALSE;
107 }
108
109 gint action_value_int(ObActionValue *v)
110 {
111     gchar *s;
112
113     g_return_val_if_fail(v->type == OB_AV_STRING, 0);
114     s = v->v.string;
115     return strtol(s, &s, 10);
116 }
117
118 void action_value_fraction(ObActionValue *v, gint *numer, gint *denom)
119 {
120     gchar *s;
121
122     *numer = *denom = 0;
123
124     g_return_if_fail(v->type == OB_AV_STRING);
125     s = v->v.string;
126
127     *numer = strtol(s, &s, 10);
128     if (*s == '%')
129         *denom = 100;
130     else if (*s == '/')
131         *denom = atoi(s+1);
132 }
133
134 void action_value_gravity_coord(ObActionValue *v, GravityCoord *c)
135 {
136     gchar *s;
137
138     c->center = FALSE;
139     c->pos = 0;
140     c->denom = 0;
141
142     g_return_if_fail(v->type == OB_AV_STRING);
143     s = v->v.string;
144
145     if (!g_ascii_strcasecmp(s, "center"))
146         c->center = TRUE;
147     else {
148         if (s[0] == '-')
149             c->opposite = TRUE;
150         if (s[0] == '-' || s[0] == '+')
151             ++s;
152
153         c->pos = strtol(s, &s, 10);
154
155         if (*s == '%')
156             c->denom = 100;
157         else if (*s == '/')
158             c->denom = atoi(s+1);
159     }
160 }
161
162 ObActionList* action_value_action_list(ObActionValue *v)
163 {
164     g_return_val_if_fail(v->type == OB_AV_ACTIONLIST, NULL);
165     return v->v.actions;
166 }