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