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