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-2011 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     /* Check if any assignments match */
00021     Assignment *current;
00022     TAILQ_FOREACH(current, &assignments, assignments) {
00023         if (!match_matches_window(&(current->match), window))
00024             continue;
00025 
00026         bool skip = false;
00027         for (int c = 0; c < window->nr_assignments; c++) {
00028             if (window->ran_assignments[c] != current)
00029                 continue;
00030 
00031             DLOG("This assignment already ran for the given window, not executing it again.\n");
00032             skip = true;
00033             break;
00034         }
00035 
00036         if (skip)
00037             continue;
00038 
00039         DLOG("matching assignment, would do:\n");
00040         if (current->type == A_COMMAND) {
00041             DLOG("execute command %s\n", current->dest.command);
00042             char *full_command;
00043             sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
00044             char *json_result = parse_cmd(full_command);
00045             FREE(full_command);
00046             FREE(json_result);
00047         }
00048 
00049         /* Store that we ran this assignment to not execute it again */
00050         window->nr_assignments++;
00051         window->ran_assignments = srealloc(window->ran_assignments, sizeof(Assignment*) * window->nr_assignments);
00052         window->ran_assignments[window->nr_assignments-1] = current;
00053     }
00054 }
00055 
00056 /*
00057  * Returns the first matching assignment for the given window.
00058  *
00059  */
00060 Assignment *assignment_for(i3Window *window, int type) {
00061     Assignment *assignment;
00062 
00063     TAILQ_FOREACH(assignment, &assignments, assignments) {
00064         if ((type != A_ANY && (assignment->type & type) == 0) ||
00065             !match_matches_window(&(assignment->match), window))
00066             continue;
00067         DLOG("got a matching assignment (to %s)\n", assignment->dest.workspace);
00068         return assignment;
00069     }
00070 
00071     return NULL;
00072 }