i3
src/assignments.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * assignments.c: Assignments for specific windows (for_window).
00008  *
00009  */
00010 #include "all.h"
00011 
00012 /*
00013  * Checks the list of assignments for the given window and runs all matching
00014  * ones (unless they have already been run for this specific window).
00015  *
00016  */
00017 void run_assignments(i3Window *window) {
00018     DLOG("Checking if any assignments match this window\n");
00019 
00020     bool needs_tree_render = false;
00021 
00022     /* Check if any assignments match */
00023     Assignment *current;
00024     TAILQ_FOREACH(current, &assignments, assignments) {
00025         if (!match_matches_window(&(current->match), window))
00026             continue;
00027 
00028         bool skip = false;
00029         for (int c = 0; c < window->nr_assignments; c++) {
00030             if (window->ran_assignments[c] != current)
00031                 continue;
00032 
00033             DLOG("This assignment already ran for the given window, not executing it again.\n");
00034             skip = true;
00035             break;
00036         }
00037 
00038         if (skip)
00039             continue;
00040 
00041         DLOG("matching assignment, would do:\n");
00042         if (current->type == A_COMMAND) {
00043             DLOG("execute command %s\n", current->dest.command);
00044             char *full_command;
00045             sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
00046             struct CommandResult *command_output = parse_command(full_command);
00047             free(full_command);
00048 
00049             if (command_output->needs_tree_render)
00050                 needs_tree_render = true;
00051 
00052             free(command_output->json_output);
00053         }
00054 
00055         /* Store that we ran this assignment to not execute it again */
00056         window->nr_assignments++;
00057         window->ran_assignments = srealloc(window->ran_assignments, sizeof(Assignment*) * window->nr_assignments);
00058         window->ran_assignments[window->nr_assignments-1] = current;
00059     }
00060 
00061     /* If any of the commands required re-rendering, we will do that now. */
00062     if (needs_tree_render)
00063         tree_render();
00064 }
00065 
00066 /*
00067  * Returns the first matching assignment for the given window.
00068  *
00069  */
00070 Assignment *assignment_for(i3Window *window, int type) {
00071     Assignment *assignment;
00072 
00073     TAILQ_FOREACH(assignment, &assignments, assignments) {
00074         if ((type != A_ANY && (assignment->type & type) == 0) ||
00075             !match_matches_window(&(assignment->match), window))
00076             continue;
00077         DLOG("got a matching assignment (to %s)\n", assignment->dest.workspace);
00078         return assignment;
00079     }
00080 
00081     return NULL;
00082 }