]> git.sur5r.net Git - i3/i3/blob - src/assignments.c
a0f5d5a747b79f02c23d1dfe478b5ef59c9e03ae
[i3/i3] / src / assignments.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * assignments.c: Assignments for specific windows (for_window).
8  *
9  */
10 #include "all.h"
11
12 /*
13  * Checks the list of assignments for the given window and runs all matching
14  * ones (unless they have already been run for this specific window).
15  *
16  */
17 void run_assignments(i3Window *window) {
18     DLOG("Checking if any assignments match this window\n");
19
20     bool needs_tree_render = false;
21
22     /* Check if any assignments match */
23     Assignment *current;
24     TAILQ_FOREACH(current, &assignments, assignments) {
25         if (!match_matches_window(&(current->match), window))
26             continue;
27
28         bool skip = false;
29         for (uint32_t c = 0; c < window->nr_assignments; c++) {
30             if (window->ran_assignments[c] != current)
31                 continue;
32
33             DLOG("This assignment already ran for the given window, not executing it again.\n");
34             skip = true;
35             break;
36         }
37
38         if (skip)
39             continue;
40
41         /* Store that we ran this assignment to not execute it again. We have
42          * to do this before running the actual command to prevent infinite
43          * loops. */
44         window->nr_assignments++;
45         window->ran_assignments = srealloc(window->ran_assignments, sizeof(Assignment *) * window->nr_assignments);
46         window->ran_assignments[window->nr_assignments - 1] = current;
47
48         DLOG("matching assignment, would do:\n");
49         if (current->type == A_COMMAND) {
50             DLOG("execute command %s\n", current->dest.command);
51             char *full_command;
52             sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
53             CommandResult *result = parse_command(full_command, NULL);
54             free(full_command);
55
56             if (result->needs_tree_render)
57                 needs_tree_render = true;
58
59             command_result_free(result);
60         }
61     }
62
63     /* If any of the commands required re-rendering, we will do that now. */
64     if (needs_tree_render)
65         tree_render();
66 }
67
68 /*
69  * Returns the first matching assignment for the given window.
70  *
71  */
72 Assignment *assignment_for(i3Window *window, int type) {
73     Assignment *assignment;
74
75     TAILQ_FOREACH(assignment, &assignments, assignments) {
76         if ((type != A_ANY && (assignment->type & type) == 0) ||
77             !match_matches_window(&(assignment->match), window))
78             continue;
79         DLOG("got a matching assignment\n");
80         return assignment;
81     }
82
83     return NULL;
84 }