6ee2bdd9586b4b897838f95047f0f79e1ebad008
[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     gboolean stop;
88 } Options;
89
90 static gpointer setup_func(xmlNodePtr node);
91 static void     free_func(gpointer options);
92 static gboolean run_func_if(ObActionsData *data, gpointer options);
93 static gboolean run_func_continue(ObActionsData *data, gpointer options);
94 static gboolean run_func_stop(ObActionsData *data, gpointer options);
95 static gboolean run_func_foreach(ObActionsData *data, gpointer options);
96 static gboolean run_func_group(ObActionsData *data, gpointer options);
97
98 static gboolean foreach_stop;
99
100 void action_if_startup(void)
101 {
102     actions_register("If", setup_func, free_func, run_func_if);
103     actions_register("Stop", NULL, NULL, run_func_stop);
104     actions_register("Continue", NULL, NULL, run_func_continue);
105     actions_register("ForEach", setup_func, free_func, run_func_foreach);
106     //actions_register("GroupMembers", setup_func, free_func, run_func_group);
107 }
108
109 static inline void set_bool(xmlNodePtr node,
110                             const char *name,
111                             gboolean *on,
112                             gboolean *off)
113 {
114     xmlNodePtr n;
115
116     if ((n = obt_xml_find_node(node, name))) {
117         if (obt_xml_node_bool(n))
118             *on = TRUE;
119         else
120             *off = TRUE;
121     }
122 }
123
124 static void setup_typed_match(TypedMatch *tm, xmlNodePtr n)
125 {
126     gchar *s;
127     if ((s = obt_xml_node_string(n))) {
128         gchar *type = NULL;
129         if (!obt_xml_attr_string(n, "type", &type) ||
130             !g_ascii_strcasecmp(type, "pattern"))
131         {
132             tm->type = MATCH_TYPE_PATTERN;
133             tm->m.pattern = g_pattern_spec_new(s);
134         } else if (type && !g_ascii_strcasecmp(type, "regex")) {
135             tm->type = MATCH_TYPE_REGEX;
136             tm->m.regex = g_regex_new(s, 0, 0, NULL);
137         } else if (type && !g_ascii_strcasecmp(type, "exact")) {
138             tm->type = MATCH_TYPE_EXACT;
139             tm->m.exact = g_strdup(s);
140         }
141         g_free(s);
142         g_free(type);
143     }
144 }
145
146 static void free_typed_match(TypedMatch *tm)
147 {
148     switch (tm->type) {
149     case MATCH_TYPE_PATTERN:
150         g_pattern_spec_free(tm->m.pattern);
151         break;
152     case MATCH_TYPE_REGEX:
153         g_regex_unref(tm->m.regex);
154         break;
155     case MATCH_TYPE_EXACT:
156         g_free(tm->m.exact);
157         break;
158     case MATCH_TYPE_NONE:
159         break;
160     }
161 }
162
163 static gboolean check_typed_match(TypedMatch *tm, const gchar *s)
164 {
165     switch (tm->type) {
166     case MATCH_TYPE_PATTERN:
167         return g_pattern_match_string(tm->m.pattern, s);
168     case MATCH_TYPE_REGEX:
169         return g_regex_match(tm->m.regex, s, 0, NULL);
170     case MATCH_TYPE_EXACT:
171         return !strcmp(tm->m.exact, s);
172     case MATCH_TYPE_NONE:
173         return TRUE;
174     }
175     g_assert_not_reached();
176 }
177
178 static void setup_query(Options* o, xmlNodePtr node, QueryTarget target) {
179     Query *q = g_slice_new0(Query);
180     g_array_append_val(o->queries, q);
181
182     q->target = target;
183
184     set_bool(node, "shaded", &q->shaded_on, &q->shaded_off);
185     set_bool(node, "maximized", &q->maxfull_on, &q->maxfull_off);
186     set_bool(node, "maximizedhorizontal", &q->maxhorz_on, &q->maxhorz_off);
187     set_bool(node, "maximizedvertical", &q->maxvert_on, &q->maxvert_off);
188     set_bool(node, "iconified", &q->iconic_on, &q->iconic_off);
189     set_bool(node, "locked", &q->locked_on, &q->locked_off);
190     set_bool(node, "focused", &q->focused, &q->unfocused);
191     set_bool(node, "urgent", &q->urgent_on, &q->urgent_off);
192     set_bool(node, "undecorated", &q->decor_off, &q->decor_on);
193     set_bool(node, "omnipresent", &q->omnipresent_on, &q->omnipresent_off);
194
195     xmlNodePtr n;
196     if ((n = obt_xml_find_node(node, "desktop"))) {
197         gchar *s;
198         if ((s = obt_xml_node_string(n))) {
199             if (!g_ascii_strcasecmp(s, "current"))
200                 q->desktop_current = TRUE;
201             if (!g_ascii_strcasecmp(s, "other"))
202                 q->desktop_other = TRUE;
203             else
204                 q->desktop_number = atoi(s);
205             g_free(s);
206         }
207     }
208     if ((n = obt_xml_find_node(node, "screendesktop"))) {
209         gchar *s;
210         if ((s = obt_xml_node_string(n))) {
211           q->screendesktop_number = atoi(s);
212           g_free(s);
213         }
214     }
215     if ((n = obt_xml_find_node(node, "activedesktop"))) {
216         q->screendesktop_number = obt_xml_node_int(n);
217     }
218     if ((n = obt_xml_find_node(node, "title"))) {
219         setup_typed_match(&q->title, n);
220     }
221     if ((n = obt_xml_find_node(node, "class"))) {
222         setup_typed_match(&q->class, n);
223     }
224     if ((n = obt_xml_find_node(node, "name"))) {
225         setup_typed_match(&q->name, n);
226     }
227     if ((n = obt_xml_find_node(node, "role"))) {
228         setup_typed_match(&q->role, n);
229     }
230     if ((n = obt_xml_find_node(node, "type"))) {
231         setup_typed_match(&q->type, n);
232     }
233     if ((n = obt_xml_find_node(node, "monitor"))) {
234         q->client_monitor = obt_xml_node_int(n);
235     }
236 }
237
238 static gpointer setup_func(xmlNodePtr node)
239 {
240     Options *o = g_slice_new0(Options);
241
242     gboolean zero_terminated = FALSE;
243     gboolean clear_to_zero_on_alloc = FALSE;
244     o->queries = g_array_new(zero_terminated,
245                              clear_to_zero_on_alloc,
246                              sizeof(Query*));
247
248     xmlNodePtr n;
249     if ((n = obt_xml_find_node(node, "then"))) {
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->thenacts = g_slist_append(o->thenacts, action);
256             m = obt_xml_find_node(m->next, "action");
257         }
258     }
259     if ((n = obt_xml_find_node(node, "else"))) {
260         xmlNodePtr m;
261
262         m = obt_xml_find_node(n->children, "action");
263         while (m) {
264             ObActionsAct *action = actions_parse(m);
265             if (action) o->elseacts = g_slist_append(o->elseacts, action);
266             m = obt_xml_find_node(m->next, "action");
267         }
268     }
269
270     xmlNodePtr query_node = obt_xml_find_node(node, "query");
271     if (!query_node) {
272         /* The default query if none is specified. It uses the conditions
273            found in the action's node. */
274         setup_query(o,
275                     node,
276                     QUERY_TARGET_IS_ACTION_TARGET);
277     } else {
278         while (query_node) {
279             QueryTarget query_target = QUERY_TARGET_IS_ACTION_TARGET;
280             if (obt_xml_attr_contains(query_node, "target", "focus"))
281                 query_target = QUERY_TARGET_IS_FOCUS_TARGET;
282
283             setup_query(o, query_node->children, query_target);
284
285             query_node = obt_xml_find_node(query_node->next, "query");
286         }
287     }
288
289     return o;
290 }
291
292 static void free_func(gpointer options)
293 {
294     Options *o = options;
295
296     guint i;
297     for (i = 0; i < o->queries->len; ++i) {
298         Query *q = g_array_index(o->queries, Query*, i);
299
300         free_typed_match(&q->title);
301         free_typed_match(&q->class);
302         free_typed_match(&q->name);
303         free_typed_match(&q->role);
304         free_typed_match(&q->type);
305
306         g_slice_free(Query, q);
307     }
308
309     while (o->thenacts) {
310         actions_act_unref(o->thenacts->data);
311         o->thenacts = g_slist_delete_link(o->thenacts, o->thenacts);
312     }
313     while (o->elseacts) {
314         actions_act_unref(o->elseacts->data);
315         o->elseacts = g_slist_delete_link(o->elseacts, o->elseacts);
316     }
317
318     g_array_unref(o->queries);
319     g_slice_free(Options, o);
320 }
321
322 /* Always return FALSE because its not interactive */
323 static gboolean run_func_if(ObActionsData *data, gpointer options)
324 {
325     Options *o = options;
326     ObClient *action_target = data->client;
327     gboolean is_true = TRUE;
328
329     guint i;
330     for (i = 0; is_true && i < o->queries->len; ++i) {
331         Query *q = g_array_index(o->queries, Query*, i);
332         ObClient *query_target = NULL;
333
334         switch (q->target) {
335         case QUERY_TARGET_IS_ACTION_TARGET:
336             query_target = data->client;
337             break;
338         case QUERY_TARGET_IS_FOCUS_TARGET:
339             query_target = focus_client;
340             break;
341         }
342
343         /* If there's no client to query, then false. */
344         if (!query_target) {
345             is_true = FALSE;
346             break;
347         }
348
349         if (q->shaded_on)
350             is_true &= query_target->shaded;
351         if (q->shaded_off)
352             is_true &= !query_target->shaded;
353
354         if (q->iconic_on)
355             is_true &= query_target->iconic;
356         if (q->iconic_off)
357             is_true &= !query_target->iconic;
358
359         if (q->locked_on)
360             is_true &= query_target->locked;
361         if (q->locked_off)
362             is_true &= !query_target->locked;
363
364         if (q->maxhorz_on)
365             is_true &= query_target->max_horz;
366         if (q->maxhorz_off)
367             is_true &= !query_target->max_horz;
368
369         if (q->maxvert_on)
370             is_true &= query_target->max_vert;
371         if (q->maxvert_off)
372             is_true &= !query_target->max_vert;
373
374         gboolean is_max_full =
375             query_target->max_vert && query_target->max_horz;
376         if (q->maxfull_on)
377             is_true &= is_max_full;
378         if (q->maxfull_off)
379             is_true &= !is_max_full;
380
381         if (q->focused)
382             is_true &= query_target == focus_client;
383         if (q->unfocused)
384             is_true &= query_target != focus_client;
385
386         gboolean is_urgent =
387             query_target->urgent || query_target->demands_attention;
388         if (q->urgent_on)
389             is_true &= is_urgent;
390         if (q->urgent_off)
391             is_true &= !is_urgent;
392
393         gboolean has_visible_title_bar =
394             !query_target->undecorated &&
395             (query_target->decorations & OB_FRAME_DECOR_TITLEBAR);
396         if (q->decor_on)
397             is_true &= has_visible_title_bar;
398         if (q->decor_off)
399             is_true &= !has_visible_title_bar;
400
401         if (q->omnipresent_on)
402             is_true &= query_target->desktop == DESKTOP_ALL;
403         if (q->omnipresent_off)
404             is_true &= query_target->desktop != DESKTOP_ALL;
405
406         gboolean is_on_current_desktop =
407             query_target->desktop == screen_desktop ||
408             query_target->desktop == DESKTOP_ALL;
409         if (q->desktop_current)
410             is_true &= is_on_current_desktop;
411         if (q->desktop_other)
412             is_true &= !is_on_current_desktop;
413
414         if (q->desktop_number) {
415             gboolean is_on_desktop =
416                 query_target->desktop == q->desktop_number - 1 ||
417                 query_target->desktop == DESKTOP_ALL;
418             is_true &= is_on_desktop;
419         }
420
421         if (q->screendesktop_number)
422             is_true &= screen_desktop == q->screendesktop_number - 1;
423
424         is_true &= check_typed_match(&q->title, query_target->original_title);
425         is_true &= check_typed_match(&q->class, query_target->class);
426         is_true &= check_typed_match(&q->name, query_target->name);
427         is_true &= check_typed_match(&q->role, query_target->role);
428         is_true &= check_typed_match(&q->type,
429                                      client_type_to_string(query_target));
430
431         if (q->client_monitor)
432             is_true &= client_monitor(query_target) == q->client_monitor - 1;
433
434     }
435
436     GSList *acts;
437     if (is_true)
438         acts = o->thenacts;
439     else
440         acts = o->elseacts;
441
442     actions_run_acts(acts, data->uact, data->state,
443                      data->x, data->y, data->button,
444                      data->context, action_target);
445
446     return FALSE;
447 }
448
449 static gboolean run_func_foreach(ObActionsData *data, gpointer options)
450 {
451     GList *it;
452     Options *o = options;
453
454     o->stop = FALSE;
455
456     for (it = client_list; it; it = g_list_next(it)) {
457         data->client = it->data;
458         run_func_if(data, options);
459         if (o->stop) {
460             break;
461         }
462     }
463
464     return FALSE;
465 }
466
467 static gboolean run_func_continue(ObActionsData *data, gpointer options)
468 {
469     actions_stop_running();
470 }
471
472 /*
473 static gboolean run_func_group(ObActionsData *data, gpointer acts)
474 {
475     GSList *it, *a = acts;
476     ObClient *c = data->client;
477
478     if (a && c)
479         for (it = c->group->members; it; it = g_slist_next(it)) {
480             ObClient *member = it->data;
481             if (actions_run_acts(a, data->uact, data->state,
482                                  data->x, data->y, data->button,
483                                  data->context, member))
484                 return TRUE;
485         }
486
487     return FALSE;
488 }
489 */
490
491 static gboolean run_func_stop(ObActionsData *data, gpointer options)
492 {
493     Options *o = options;
494
495     /* This stops the loop above so we don't invoke actions on any more
496        clients */
497     o->stop = TRUE;
498
499     /* TRUE causes actions_run_acts to not run further actions on the current
500        client */
501     return TRUE;
502 }