1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 if.c for the Openbox window manager
4 Copyright (c) 2007 Mikael Magnusson
5 Copyright (c) 2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
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"
29 QUERY_TARGET_IS_ACTION_TARGET,
30 QUERY_TARGET_IS_FOCUS_TARGET,
43 GPatternSpec *pattern;
61 gboolean fullscreen_on;
62 gboolean fullscreen_off;
71 gboolean omnipresent_on;
72 gboolean omnipresent_off;
73 gboolean desktop_current;
74 gboolean desktop_other;
75 gboolean desktop_last;
77 guint screendesktop_number;
92 static gpointer setup_func(xmlNodePtr node);
93 static void free_func(gpointer options);
94 static gboolean run_func_if(ObActionsData *data, gpointer options);
95 static gboolean run_func_stop(ObActionsData *data, gpointer options);
96 static gboolean run_func_foreach(ObActionsData *data, gpointer options);
98 static gboolean foreach_stop;
100 void action_if_startup(void)
102 actions_register("If", setup_func, free_func, run_func_if);
103 actions_register("Stop", NULL, NULL, run_func_stop);
104 actions_register("ForEach", setup_func, free_func, run_func_foreach);
106 actions_set_can_stop("Stop", TRUE);
109 static inline void set_bool(xmlNodePtr node,
116 if ((n = obt_xml_find_node(node, name))) {
117 if (obt_xml_node_bool(n))
124 static void setup_typed_match(TypedMatch *tm, xmlNodePtr n)
127 if ((s = obt_xml_node_string(n))) {
129 if (!obt_xml_attr_string(n, "type", &type) ||
130 !g_ascii_strcasecmp(type, "pattern"))
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);
146 static void free_typed_match(TypedMatch *tm)
149 case MATCH_TYPE_PATTERN:
150 g_pattern_spec_free(tm->m.pattern);
152 case MATCH_TYPE_REGEX:
153 g_regex_unref(tm->m.regex);
155 case MATCH_TYPE_EXACT:
158 case MATCH_TYPE_NONE:
163 static gboolean check_typed_match(TypedMatch *tm, const gchar *s)
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:
175 g_assert_not_reached();
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);
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, "fullscreen", &q->fullscreen_on, &q->fullscreen_off);
190 set_bool(node, "locked", &q->locked_on, &q->locked_off);
191 set_bool(node, "focused", &q->focused, &q->unfocused);
192 set_bool(node, "urgent", &q->urgent_on, &q->urgent_off);
193 set_bool(node, "undecorated", &q->decor_off, &q->decor_on);
194 set_bool(node, "omnipresent", &q->omnipresent_on, &q->omnipresent_off);
197 if ((n = obt_xml_find_node(node, "desktop"))) {
199 if ((s = obt_xml_node_string(n))) {
200 if (!g_ascii_strcasecmp(s, "current"))
201 q->desktop_current = TRUE;
202 else if (!g_ascii_strcasecmp(s, "other"))
203 q->desktop_other = TRUE;
204 else if (!g_ascii_strcasecmp(s, "last"))
205 q->desktop_last = TRUE;
207 q->desktop_number = atoi(s);
211 if ((n = obt_xml_find_node(node, "activedesktop"))) {
212 q->screendesktop_number = obt_xml_node_int(n);
214 if ((n = obt_xml_find_node(node, "title"))) {
215 setup_typed_match(&q->title, n);
217 if ((n = obt_xml_find_node(node, "class"))) {
218 setup_typed_match(&q->class, n);
220 if ((n = obt_xml_find_node(node, "name"))) {
221 setup_typed_match(&q->name, n);
223 if ((n = obt_xml_find_node(node, "role"))) {
224 setup_typed_match(&q->role, n);
226 if ((n = obt_xml_find_node(node, "type"))) {
227 setup_typed_match(&q->type, n);
229 if ((n = obt_xml_find_node(node, "monitor"))) {
230 q->client_monitor = obt_xml_node_int(n);
234 static gpointer setup_func(xmlNodePtr node)
236 Options *o = g_slice_new0(Options);
238 gboolean zero_terminated = FALSE;
239 gboolean clear_to_zero_on_alloc = FALSE;
240 o->queries = g_array_new(zero_terminated,
241 clear_to_zero_on_alloc,
245 if ((n = obt_xml_find_node(node, "then"))) {
248 m = obt_xml_find_node(n->children, "action");
250 ObActionsAct *action = actions_parse(m);
251 if (action) o->thenacts = g_slist_append(o->thenacts, action);
252 m = obt_xml_find_node(m->next, "action");
255 if ((n = obt_xml_find_node(node, "else"))) {
258 m = obt_xml_find_node(n->children, "action");
260 ObActionsAct *action = actions_parse(m);
261 if (action) o->elseacts = g_slist_append(o->elseacts, action);
262 m = obt_xml_find_node(m->next, "action");
266 xmlNodePtr query_node = obt_xml_find_node(node, "query");
268 /* The default query if none is specified. It uses the conditions
269 found in the action's node. */
272 QUERY_TARGET_IS_ACTION_TARGET);
275 QueryTarget query_target = QUERY_TARGET_IS_ACTION_TARGET;
276 if (obt_xml_attr_contains(query_node, "target", "focus"))
277 query_target = QUERY_TARGET_IS_FOCUS_TARGET;
279 setup_query(o, query_node->children, query_target);
281 query_node = obt_xml_find_node(query_node->next, "query");
288 static void free_func(gpointer options)
290 Options *o = options;
293 for (i = 0; i < o->queries->len; ++i) {
294 Query *q = g_array_index(o->queries, Query*, i);
296 free_typed_match(&q->title);
297 free_typed_match(&q->class);
298 free_typed_match(&q->name);
299 free_typed_match(&q->role);
300 free_typed_match(&q->type);
302 g_slice_free(Query, q);
305 while (o->thenacts) {
306 actions_act_unref(o->thenacts->data);
307 o->thenacts = g_slist_delete_link(o->thenacts, o->thenacts);
309 while (o->elseacts) {
310 actions_act_unref(o->elseacts->data);
311 o->elseacts = g_slist_delete_link(o->elseacts, o->elseacts);
314 g_array_unref(o->queries);
315 g_slice_free(Options, o);
318 /* Always return FALSE because its not interactive */
319 static gboolean run_func_if(ObActionsData *data, gpointer options)
321 Options *o = options;
322 ObClient *action_target = data->client;
323 gboolean is_true = TRUE;
326 for (i = 0; is_true && i < o->queries->len; ++i) {
327 Query *q = g_array_index(o->queries, Query*, i);
328 ObClient *query_target = NULL;
331 case QUERY_TARGET_IS_ACTION_TARGET:
332 query_target = data->client;
334 case QUERY_TARGET_IS_FOCUS_TARGET:
335 query_target = focus_client;
339 /* If there's no client to query, then false. */
346 is_true &= query_target->shaded;
348 is_true &= !query_target->shaded;
351 is_true &= query_target->iconic;
353 is_true &= !query_target->iconic;
355 if (q->fullscreen_on)
356 is_true &= query_target->fullscreen;
357 if (q->fullscreen_off)
358 is_true &= !query_target->fullscreen;
361 is_true &= query_target->locked;
363 is_true &= !query_target->locked;
366 is_true &= query_target->max_horz;
368 is_true &= !query_target->max_horz;
371 is_true &= query_target->max_vert;
373 is_true &= !query_target->max_vert;
375 gboolean is_max_full =
376 query_target->max_vert && query_target->max_horz;
378 is_true &= is_max_full;
380 is_true &= !is_max_full;
383 is_true &= query_target == focus_client;
385 is_true &= query_target != focus_client;
388 query_target->urgent || query_target->demands_attention;
390 is_true &= is_urgent;
392 is_true &= !is_urgent;
394 gboolean has_visible_title_bar =
395 !query_target->undecorated &&
396 (query_target->decorations & OB_FRAME_DECOR_TITLEBAR);
398 is_true &= has_visible_title_bar;
400 is_true &= !has_visible_title_bar;
402 if (q->omnipresent_on)
403 is_true &= query_target->desktop == DESKTOP_ALL;
404 if (q->omnipresent_off)
405 is_true &= query_target->desktop != DESKTOP_ALL;
407 gboolean is_on_current_desktop =
408 query_target->desktop == screen_desktop ||
409 query_target->desktop == DESKTOP_ALL;
410 if (q->desktop_current)
411 is_true &= is_on_current_desktop;
412 if (q->desktop_other)
413 is_true &= !is_on_current_desktop;
415 is_true &= query_target->desktop == screen_last_desktop;
417 if (q->desktop_number) {
418 gboolean is_on_desktop =
419 query_target->desktop == q->desktop_number - 1 ||
420 query_target->desktop == DESKTOP_ALL;
421 is_true &= is_on_desktop;
424 if (q->screendesktop_number)
425 is_true &= screen_desktop == q->screendesktop_number - 1;
427 is_true &= check_typed_match(&q->title, query_target->original_title);
428 is_true &= check_typed_match(&q->class, query_target->class);
429 is_true &= check_typed_match(&q->name, query_target->name);
430 is_true &= check_typed_match(&q->role, query_target->role);
431 is_true &= check_typed_match(&q->type,
432 client_type_to_string(query_target));
434 if (q->client_monitor)
435 is_true &= client_monitor(query_target) == q->client_monitor - 1;
445 actions_run_acts(acts, data->uact, data->state,
446 data->x, data->y, data->button,
447 data->context, action_target);
452 static gboolean run_func_foreach(ObActionsData *data, gpointer options)
456 foreach_stop = FALSE;
458 for (it = client_list; it; it = g_list_next(it)) {
459 data->client = it->data;
460 run_func_if(data, options);
462 foreach_stop = FALSE;
470 static gboolean run_func_stop(ObActionsData *data, gpointer options)
472 /* This stops the loop above so we don't invoke actions on any more
476 /* TRUE causes actions_run_acts to not run further actions on the current