]> Dogcows Code - chaz/openbox/blob - openbox/actions/if.c
Add some more conditions to the If action
[chaz/openbox] / openbox / actions / if.c
1 #include "openbox/actions.h"
2 #include "openbox/misc.h"
3 #include "openbox/client.h"
4 #include "openbox/frame.h"
5 #include "openbox/screen.h"
6 #include "openbox/focus.h"
7 #include <glib.h>
8
9 typedef struct {
10 gboolean shaded_on;
11 gboolean shaded_off;
12 gboolean maxvert_on;
13 gboolean maxvert_off;
14 gboolean maxhorz_on;
15 gboolean maxhorz_off;
16 gboolean maxfull_on;
17 gboolean maxfull_off;
18 gboolean iconic_on;
19 gboolean iconic_off;
20 gboolean focused;
21 gboolean unfocused;
22 gboolean urgent_on;
23 gboolean urgent_off;
24 gboolean decor_off;
25 gboolean decor_on;
26 gboolean omnipresent_on;
27 gboolean omnipresent_off;
28 gboolean desktop_current;
29 gboolean desktop_other;
30 guint desktop_number;
31 GPatternSpec *matchtitle;
32 GSList *thenacts;
33 GSList *elseacts;
34 } Options;
35
36 static gpointer setup_func(xmlNodePtr node);
37 static void free_func(gpointer options);
38 static gboolean run_func(ObActionsData *data, gpointer options);
39
40 void action_if_startup(void)
41 {
42 actions_register("If", setup_func, free_func, run_func);
43 }
44
45 static gpointer setup_func(xmlNodePtr node)
46 {
47 xmlNodePtr n;
48 Options *o;
49
50 o = g_slice_new0(Options);
51
52 if ((n = obt_xml_find_node(node, "shaded"))) {
53 if (obt_xml_node_bool(n))
54 o->shaded_on = TRUE;
55 else
56 o->shaded_off = TRUE;
57 }
58 if ((n = obt_xml_find_node(node, "maximized"))) {
59 if (obt_xml_node_bool(n))
60 o->maxfull_on = TRUE;
61 else
62 o->maxfull_off = TRUE;
63 }
64 if ((n = obt_xml_find_node(node, "maximizedhorizontal"))) {
65 if (obt_xml_node_bool(n))
66 o->maxhorz_on = TRUE;
67 else
68 o->maxhorz_off = TRUE;
69 }
70 if ((n = obt_xml_find_node(node, "maximizedvertical"))) {
71 if (obt_xml_node_bool(n))
72 o->maxvert_on = TRUE;
73 else
74 o->maxvert_off = TRUE;
75 }
76 if ((n = obt_xml_find_node(node, "iconified"))) {
77 if (obt_xml_node_bool(n))
78 o->iconic_on = TRUE;
79 else
80 o->iconic_off = TRUE;
81 }
82 if ((n = obt_xml_find_node(node, "focused"))) {
83 if (obt_xml_node_bool(n))
84 o->focused = TRUE;
85 else
86 o->unfocused = TRUE;
87 }
88 if ((n = obt_xml_find_node(node, "urgent"))) {
89 if (obt_xml_node_bool(n))
90 o->urgent_on = TRUE;
91 else
92 o->urgent_off = TRUE;
93 }
94 if ((n = obt_xml_find_node(node, "undecorated"))) {
95 if (obt_xml_node_bool(n))
96 o->decor_off = TRUE;
97 else
98 o->decor_on = TRUE;
99 }
100 if ((n = obt_xml_find_node(node, "desktop"))) {
101 gchar *s = obt_xml_node_string(n);
102 if (!g_ascii_strcasecmp(s, "current"))
103 o->desktop_current = TRUE;
104 if (!g_ascii_strcasecmp(s, "other"))
105 o->desktop_other = TRUE;
106 else
107 o->desktop_number = atoi(s);
108 }
109 if ((n = obt_xml_find_node(node, "omnipresent"))) {
110 if (obt_xml_node_bool(n))
111 o->omnipresent_on = TRUE;
112 else
113 o->omnipresent_off = TRUE;
114 }
115 if ((n = obt_xml_find_node(node, "title"))) {
116 gchar *s;
117 if ((s = obt_xml_node_string(n))) {
118 o->matchtitle = g_pattern_spec_new(s);
119 g_free(s);
120 }
121 }
122
123 if ((n = obt_xml_find_node(node, "then"))) {
124 xmlNodePtr m;
125
126 m = obt_xml_find_node(n->children, "action");
127 while (m) {
128 ObActionsAct *action = actions_parse(m);
129 if (action) o->thenacts = g_slist_append(o->thenacts, action);
130 m = obt_xml_find_node(m->next, "action");
131 }
132 }
133 if ((n = obt_xml_find_node(node, "else"))) {
134 xmlNodePtr m;
135
136 m = obt_xml_find_node(n->children, "action");
137 while (m) {
138 ObActionsAct *action = actions_parse(m);
139 if (action) o->elseacts = g_slist_append(o->elseacts, action);
140 m = obt_xml_find_node(m->next, "action");
141 }
142 }
143
144 return o;
145 }
146
147 static void free_func(gpointer options)
148 {
149 Options *o = options;
150
151 while (o->thenacts) {
152 actions_act_unref(o->thenacts->data);
153 o->thenacts = g_slist_delete_link(o->thenacts, o->thenacts);
154 }
155 while (o->elseacts) {
156 actions_act_unref(o->elseacts->data);
157 o->elseacts = g_slist_delete_link(o->elseacts, o->elseacts);
158 }
159 if (o->matchtitle)
160 g_pattern_spec_free(o->matchtitle);
161
162 g_slice_free(Options, o);
163 }
164
165 /* Always return FALSE because its not interactive */
166 static gboolean run_func(ObActionsData *data, gpointer options)
167 {
168 Options *o = options;
169 GSList *acts;
170 ObClient *c = data->client;
171
172 if (c &&
173 (!o->shaded_on || c->shaded) &&
174 (!o->shaded_off || !c->shaded) &&
175 (!o->iconic_on || c->iconic) &&
176 (!o->iconic_off || !c->iconic) &&
177 (!o->maxhorz_on || c->max_horz) &&
178 (!o->maxhorz_off || !c->max_horz) &&
179 (!o->maxvert_on || c->max_vert) &&
180 (!o->maxvert_off || !c->max_vert) &&
181 (!o->maxfull_on || (c->max_vert && c->max_horz)) &&
182 (!o->maxfull_off || !(c->max_vert && c->max_horz)) &&
183 (!o->focused || (c == focus_client)) &&
184 (!o->unfocused || !(c == focus_client)) &&
185 (!o->urgent_on || (c->urgent || c->demands_attention)) &&
186 (!o->urgent_off || !(c->urgent || c->demands_attention)) &&
187 (!o->decor_off || (c->undecorated || !(c->decorations & OB_FRAME_DECOR_TITLEBAR))) &&
188 (!o->decor_on || (!c->undecorated && (c->decorations & OB_FRAME_DECOR_TITLEBAR))) &&
189 (!o->omnipresent_on || (c->desktop == DESKTOP_ALL)) &&
190 (!o->omnipresent_off || (c->desktop != DESKTOP_ALL)) &&
191 (!o->desktop_current || ((c->desktop == screen_desktop) ||
192 (c->desktop == DESKTOP_ALL))) &&
193 (!o->desktop_other || ((c->desktop != screen_desktop) &&
194 (c->desktop != DESKTOP_ALL))) &&
195 (!o->desktop_number || ((c->desktop == o->desktop_number - 1) ||
196 (c->desktop == DESKTOP_ALL))) &&
197 (!o->matchtitle ||
198 (g_pattern_match_string(o->matchtitle, c->original_title))))
199 {
200 acts = o->thenacts;
201 }
202 else
203 acts = o->elseacts;
204
205 actions_run_acts(acts, data->uact, data->state,
206 data->x, data->y, data->button,
207 data->context, data->client);
208
209 return FALSE;
210 }
This page took 0.048622 seconds and 5 git commands to generate.