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