]> git.sur5r.net Git - i3/i3/blob - src/assignments.c
Refactor the interface of commands.c
[i3/i3] / src / assignments.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2012 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     /* Check if any assignments match */
21     Assignment *current;
22     TAILQ_FOREACH(current, &assignments, assignments) {
23         if (!match_matches_window(&(current->match), window))
24             continue;
25
26         bool skip = false;
27         for (int c = 0; c < window->nr_assignments; c++) {
28             if (window->ran_assignments[c] != current)
29                 continue;
30
31             DLOG("This assignment already ran for the given window, not executing it again.\n");
32             skip = true;
33             break;
34         }
35
36         if (skip)
37             continue;
38
39         DLOG("matching assignment, would do:\n");
40         if (current->type == A_COMMAND) {
41             DLOG("execute command %s\n", current->dest.command);
42             char *full_command;
43             sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
44             struct CommandResult *command_output = parse_command(full_command);
45             free(full_command);
46
47             if (command_output->needs_tree_render)
48                 tree_render();
49
50             free(command_output->json_output);
51         }
52
53         /* Store that we ran this assignment to not execute it again */
54         window->nr_assignments++;
55         window->ran_assignments = srealloc(window->ran_assignments, sizeof(Assignment*) * window->nr_assignments);
56         window->ran_assignments[window->nr_assignments-1] = current;
57     }
58 }
59
60 /*
61  * Returns the first matching assignment for the given window.
62  *
63  */
64 Assignment *assignment_for(i3Window *window, int type) {
65     Assignment *assignment;
66
67     TAILQ_FOREACH(assignment, &assignments, assignments) {
68         if ((type != A_ANY && (assignment->type & type) == 0) ||
69             !match_matches_window(&(assignment->match), window))
70             continue;
71         DLOG("got a matching assignment (to %s)\n", assignment->dest.workspace);
72         return assignment;
73     }
74
75     return NULL;
76 }