ef4ed4bf21cd1c2360a246f1e5bea080f43930ca
[mikachu/openbox.git] / openbox / actions / if.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    if.c for the Openbox window manager
4    Copyright (c) 2007        Mikael Magnusson
5    Copyright (c) 2007        Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "openbox/actions.h"
21 #include "openbox/misc.h"
22 #include "openbox/client.h"
23 #include "openbox/frame.h"
24 #include "openbox/screen.h"
25 #include "openbox/focus.h"
26 #include <glib.h>
27
28 typedef enum {
29     QUERY_TARGET_IS_ACTION_TARGET,
30     QUERY_TARGET_IS_FOCUS_TARGET,
31 } QueryTarget;
32
33 typedef enum {
34     MATCH_TYPE_NONE = 0,
35     MATCH_TYPE_PATTERN,
36     MATCH_TYPE_REGEX,
37     MATCH_TYPE_EXACT,
38 } MatchType;
39
40 typedef struct {
41     MatchType type;
42     union m {
43         GPatternSpec *pattern;
44         GRegex *regex;
45         gchar *exact;
46     } m;
47 } TypedMatch;
48
49 typedef struct {
50     QueryTarget target;
51     gboolean shaded_on;
52     gboolean shaded_off;
53     gboolean maxvert_on;
54     gboolean maxvert_off;
55     gboolean maxhorz_on;
56     gboolean maxhorz_off;
57     gboolean maxfull_on;
58     gboolean maxfull_off;
59     gboolean iconic_on;
60     gboolean iconic_off;
61     gboolean locked_on;
62     gboolean locked_off;
63     gboolean focused;
64     gboolean unfocused;
65     gboolean urgent_on;
66     gboolean urgent_off;
67     gboolean decor_off;
68     gboolean decor_on;
69     gboolean omnipresent_on;
70     gboolean omnipresent_off;
71     gboolean desktop_current;
72     gboolean desktop_other;
73     guint    desktop_number;
74     guint    screendesktop_number;
75     guint    client_monitor;
76     TypedMatch title;
77     TypedMatch class;
78     TypedMatch name;
79     TypedMatch role;
80     TypedMatch type;
81 } Query;
82
83 typedef struct {
84     GArray *queries;
85     GSList *thenacts;
86     GSList *elseacts;
87 } Options;
88
89 static gpointer setup_func(xmlNodePtr node);
90 static void     free_func(gpointer options);
91 static gboolean run_func_if(ObActionsData *data, gpointer options);
92 static gboolean run_func_stop(ObActionsData *data, gpointer options);
93 static gboolean run_func_foreach(ObActionsData *data, gpointer options);
94
95 static gboolean foreach_stop;
96
97 void action_if_startup(void)
98 {
99     actions_register("If", setup_func, free_func, run_func_if);
100     actions_register("Stop", NULL, NULL, run_func_stop);
101     actions_register("ForEach", setup_func, free_func, run_func_foreach);
102
103     actions_set_can_stop("Stop", TRUE);
104 }
105
106 static inline void set_bool(xmlNodePtr node,
107                             const char *name,
108                             gboolean *on,
109                             gboolean *off)
110 {
111     xmlNodePtr n;
112
113     if ((n = obt_xml_find_node(node, name))) {
114         if (obt_xml_node_bool(n))
115             *on = TRUE;
116         else
117             *off = TRUE;
118     }
119 }
120
121 static void setup_typed_match(TypedMatch *tm, xmlNodePtr n)
122 {
123     gchar *s;
124     if ((s = obt_xml_node_string(n))) {
125         gchar *type = NULL;
126         if (!obt_xml_attr_string(n, "type", &type) ||
127             !g_ascii_strcasecmp(type, "pattern"))
128         {
129             tm->type = MATCH_TYPE_PATTERN;
130             tm->m.pattern = g_pattern_spec_new(s);
131         } else if (type && !g_ascii_strcasecmp(type, "regex")) {
132             tm->type = MATCH_TYPE_REGEX;
133             tm->m.regex = g_regex_new(s, 0, 0, NULL);
134         } else if (type && !g_ascii_strcasecmp(type, "exact")) {
135             tm->type = MATCH_TYPE_EXACT;
136             tm->m.exact = g_strdup(s);
137         }
138         g_free(s);
139         g_free(type);
140     }
141 }
142
143 static void free_typed_match(TypedMatch *tm)
144 {
145     switch (tm->type) {
146     case MATCH_TYPE_PATTERN:
147         g_pattern_spec_free(tm->m.pattern);
148         break;
149     case MATCH_TYPE_REGEX:
150         g_regex_unref(tm->m.regex);
151         break;
152     case MATCH_TYPE_EXACT:
153         g_free(tm->m.exact);
154         break;
155     case MATCH_TYPE_NONE:
156         break;
157     }
158 }
159
160 static gboolean check_typed_match(TypedMatch *tm, const gchar *s)
161 {
162     switch (tm->type) {
163     case MATCH_TYPE_PATTERN:
164         return g_pattern_match_string(tm->m.pattern, s);
165     case MATCH_TYPE_REGEX:
166         return g_regex_match(tm->m.regex, s, 0, NULL);
167     case MATCH_TYPE_EXACT:
168         return !strcmp(tm->m.exact, s);
169     case MATCH_TYPE_NONE:
170         return TRUE;
171     }
172     g_assert_not_reached();
173 }
174
175 static void setup_query(Options* o, xmlNodePtr node, QueryTarget target) {
176     Query *q = g_slice_new0(Query);
177     g_array_append_val(o->queries, q);
178
179     q->target = target;
180
181     set_bool(node, "shaded", &q->shaded_on, &q->shaded_off);
182     set_bool(node, "maximized", &q->maxfull_on, &q->maxfull_off);
183     set_bool(node, "maximizedhorizontal", &q->maxhorz_on, &q->maxhorz_off);
184     set_bool(node, "maximizedvertical", &q->maxvert_on, &q->maxvert_off);
185     set_bool(node, "iconified", &q->iconic_on, &q->iconic_off);
186     set_bool(node, "locked", &q->locked_on, &q->locked_off);
187     set_bool(node, "focused", &q->focused, &q->unfocused);
188     set_bool(node, "urgent", &q->urgent_on, &q->urgent_off);
189     set_bool(node, "undecorated", &q->decor_off, &q->decor_on);
190     set_bool(node, "omnipresent", &q->omnipresent_on, &q->omnipresent_off);
191
192     xmlNodePtr n;
193     if ((n = obt_xml_find_node(node, "desktop"))) {
194         gchar *s;
195         if ((s = obt_xml_node_string(n))) {
196             if (!g_ascii_strcasecmp(s, "current"))
197                 q->desktop_current = TRUE;
198             if (!g_ascii_strcasecmp(s, "other"))
199                 q->desktop_other = TRUE;
200             else
201                 q->desktop_number = atoi(s);
202             g_free(s);
203         }
204     }
205     if ((n = obt_xml_find_node(node, "activedesktop"))) {
206         q->screendesktop_number = obt_xml_node_int(n);
207     }
208     if ((n = obt_xml_find_node(node, "title"))) {
209         setup_typed_match(&q->title, n);
210     }
211     if ((n = obt_xml_find_node(node, "class"))) {
212         setup_typed_match(&q->class, n);
213     }
214     if ((n = obt_xml_find_node(node, "name"))) {
215         setup_typed_match(&q->name, n);
216     }
217     if ((n = obt_xml_find_node(node, "role"))) {
218         setup_typed_match(&q->role, n);
219     }
220     if ((n = obt_xml_find_node(node, "type"))) {
221         setup_typed_match(&q->type, n);
222     }
223     if ((n = obt_xml_find_node(node, "monitor"))) {
224         q->client_monitor = obt_xml_node_int(n);
225     }
226 }
227
228 static gpointer setup_func(xmlNodePtr node)
229 {
230     Options *o = g_slice_new0(Options);
231
232     gboolean zero_terminated = FALSE;
233     gboolean clear_to_zero_on_alloc = FALSE;
234     o->queries = g_array_new(zero_terminated,
235                              clear_to_zero_on_alloc,
236                              sizeof(Query*));
237
238     xmlNodePtr n;
239     if ((n = obt_xml_find_node(node, "then"))) {
240         xmlNodePtr m;
241
242         m = obt_xml_find_node(n->children, "action");
243         while (m) {
244             ObActionsAct *action = actions_parse(m);
245             if (action) o->thenacts = g_slist_append(o->thenacts, action);
246             m = obt_xml_find_node(m->next, "action");
247         }
248     }
249     if ((n = obt_xml_find_node(node, "else"))) {
250         xmlNodePtr m;
251
252         m = obt_xml_find_node(n->children, "action");
253         while (m) {
254             ObActionsAct *action = actions_parse(m);
255             if (action) o->elseacts = g_slist_append(o->elseacts, action);
256             m = obt_xml_find_node(m->next, "action");
257         }
258     }
259
260     xmlNodePtr query_node = obt_xml_find_node(node, "query");
261     if (!query_node) {
262         /* The default query if none is specified. It uses the conditions
263            found in the action's node. */
264         setup_query(o,
265                     node,
266                     QUERY_TARGET_IS_ACTION_TARGET);
267     } else {
268         while (query_node) {
269             QueryTarget query_target = QUERY_TARGET_IS_ACTION_TARGET;
270             if (obt_xml_attr_contains(query_node, "target", "focus"))
271                 query_target = QUERY_TARGET_IS_FOCUS_TARGET;
272
273             setup_query(o, query_node->children, query_target);
274
275             query_node = obt_xml_find_node(query_node->next, "query");
276         }
277     }
278
279     return o;
280 }
281
282 static void free_func(gpointer options)
283 {
284     Options *o = options;
285
286     guint i;
287     for (i = 0; i < o->queries->len; ++i) {
288         Query *q = g_array_index(o->queries, Query*, i);
289
290         free_typed_match(&q->title);
291         free_typed_match(&q->class);
292         free_typed_match(&q->name);
293         free_typed_match(&q->role);
294         free_typed_match(&q->type);
295
296         g_slice_free(Query, q);
297     }
298
299     while (o->thenacts) {
300         actions_act_unref(o->thenacts->data);
301         o->thenacts = g_slist_delete_link(o->thenacts, o->thenacts);
302     }
303     while (o->elseacts) {
304         actions_act_unref(o->elseacts->data);
305         o->elseacts = g_slist_delete_link(o->elseacts, o->elseacts);
306     }
307
308     g_array_unref(o->queries);
309     g_slice_free(Options, o);
310 }
311
312 /* Always return FALSE because its not interactive */
313 static gboolean run_func_if(ObActionsData *data, gpointer options)
314 {
315     Options *o = options;
316     ObClient *action_target = data->client;
317     gboolean is_true = TRUE;
318
319     guint i;
320     for (i = 0; is_true && i < o->queries->len; ++i) {
321         Query *q = g_array_index(o->queries, Query*, i);
322         ObClient *query_target = NULL;
323
324         switch (q->target) {
325         case QUERY_TARGET_IS_ACTION_TARGET:
326             query_target = data->client;
327             break;
328         case QUERY_TARGET_IS_FOCUS_TARGET:
329             query_target = focus_client;
330             break;
331         }
332
333         /* If there's no client to query, then false. */
334         if (!query_target) {
335             is_true = FALSE;
336             break;
337         }
338
339         if (q->shaded_on)
340             is_true &= query_target->shaded;
341         if (q->shaded_off)
342             is_true &= !query_target->shaded;
343
344         if (q->iconic_on)
345             is_true &= query_target->iconic;
346         if (q->iconic_off)
347             is_true &= !query_target->iconic;
348
349         if (q->locked_on)
350             is_true &= query_target->locked;
351         if (q->locked_off)
352             is_true &= !query_target->locked;
353
354         if (q->maxhorz_on)
355             is_true &= query_target->max_horz;
356         if (q->maxhorz_off)
357             is_true &= !query_target->max_horz;
358
359         if (q->maxvert_on)
360             is_true &= query_target->max_vert;
361         if (q->maxvert_off)
362             is_true &= !query_target->max_vert;
363
364         gboolean is_max_full =
365             query_target->max_vert && query_target->max_horz;
366         if (q->maxfull_on)
367             is_true &= is_max_full;
368         if (q->maxfull_off)
369             is_true &= !is_max_full;
370
371         if (q->focused)
372             is_true &= query_target == focus_client;
373         if (q->unfocused)
374             is_true &= query_target != focus_client;
375
376         gboolean is_urgent =
377             query_target->urgent || query_target->demands_attention;
378         if (q->urgent_on)
379             is_true &= is_urgent;
380         if (q->urgent_off)
381             is_true &= !is_urgent;
382
383         gboolean has_visible_title_bar =
384             !query_target->undecorated &&
385             (query_target->decorations & OB_FRAME_DECOR_TITLEBAR);
386         if (q->decor_on)
387             is_true &= has_visible_title_bar;
388         if (q->decor_off)
389             is_true &= !has_visible_title_bar;
390
391         if (q->omnipresent_on)
392             is_true &= query_target->desktop == DESKTOP_ALL;
393         if (q->omnipresent_off)
394             is_true &= query_target->desktop != DESKTOP_ALL;
395
396         gboolean is_on_current_desktop =
397             query_target->desktop == screen_desktop ||
398             query_target->desktop == DESKTOP_ALL;
399         if (q->desktop_current)
400             is_true &= is_on_current_desktop;
401         if (q->desktop_other)
402             is_true &= !is_on_current_desktop;
403
404         if (q->desktop_number) {
405             gboolean is_on_desktop =
406                 query_target->desktop == q->desktop_number - 1 ||
407                 query_target->desktop == DESKTOP_ALL;
408             is_true &= is_on_desktop;
409         }
410
411         if (q->screendesktop_number)
412             is_true &= screen_desktop == q->screendesktop_number - 1;
413
414         is_true &= check_typed_match(&q->title, query_target->original_title);
415         is_true &= check_typed_match(&q->class, query_target->class);
416         is_true &= check_typed_match(&q->name, query_target->name);
417         is_true &= check_typed_match(&q->role, query_target->role);
418         is_true &= check_typed_match(&q->type,
419                                      client_type_to_string(query_target));
420
421         if (q->client_monitor)
422             is_true &= client_monitor(query_target) == q->client_monitor - 1;
423
424     }
425
426     GSList *acts;
427     if (is_true)
428         acts = o->thenacts;
429     else
430         acts = o->elseacts;
431
432     actions_run_acts(acts, data->uact, data->state,
433                      data->x, data->y, data->button,
434                      data->context, action_target);
435
436     return FALSE;
437 }
438
439 static gboolean run_func_foreach(ObActionsData *data, gpointer options)
440 {
441     GList *it;
442
443     foreach_stop = FALSE;
444
445     for (it = client_list; it; it = g_list_next(it)) {
446         data->client = it->data;
447         run_func_if(data, options);
448         if (foreach_stop) {
449             foreach_stop = FALSE;
450             break;
451         }
452     }
453
454     return FALSE;
455 }
456
457 static gboolean run_func_stop(ObActionsData *data, gpointer options)
458 {
459     /* This stops the loop above so we don't invoke actions on any more
460        clients */
461     foreach_stop = TRUE;
462
463     /* TRUE causes actions_run_acts to not run further actions on the current
464        client */
465     return TRUE;
466 }