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