i3
assignments.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "assignments.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * assignments.c: Assignments for specific windows (for_window).
10  *
11  */
12 #include "all.h"
13 
14 /*
15  * Checks the list of assignments for the given window and runs all matching
16  * ones (unless they have already been run for this specific window).
17  *
18  */
19 void run_assignments(i3Window *window) {
20  DLOG("Checking if any assignments match this window\n");
21 
22  bool needs_tree_render = false;
23 
24  /* Check if any assignments match */
25  Assignment *current;
27  if (!match_matches_window(&(current->match), window))
28  continue;
29 
30  bool skip = false;
31  for (uint32_t c = 0; c < window->nr_assignments; c++) {
32  if (window->ran_assignments[c] != current)
33  continue;
34 
35  DLOG("This assignment already ran for the given window, not executing it again.\n");
36  skip = true;
37  break;
38  }
39 
40  if (skip)
41  continue;
42 
43  DLOG("matching assignment, would do:\n");
44  if (current->type == A_COMMAND) {
45  DLOG("execute command %s\n", current->dest.command);
46  char *full_command;
47  sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
48  CommandResult *result = parse_command(full_command, NULL);
49  free(full_command);
50 
51  if (result->needs_tree_render)
52  needs_tree_render = true;
53 
54  command_result_free(result);
55  }
56 
57  /* Store that we ran this assignment to not execute it again */
58  window->nr_assignments++;
59  window->ran_assignments = srealloc(window->ran_assignments, sizeof(Assignment *) * window->nr_assignments);
60  window->ran_assignments[window->nr_assignments - 1] = current;
61  }
62 
63  /* If any of the commands required re-rendering, we will do that now. */
64  if (needs_tree_render)
65  tree_render();
66 }
67 
68 /*
69  * Returns the first matching assignment for the given window.
70  *
71  */
72 Assignment *assignment_for(i3Window *window, int type) {
73  Assignment *assignment;
74 
75  TAILQ_FOREACH (assignment, &assignments, assignments) {
76  if ((type != A_ANY && (assignment->type & type) == 0) ||
77  !match_matches_window(&(assignment->match), window))
78  continue;
79  DLOG("got a matching assignment (to %s)\n", assignment->dest.workspace);
80  return assignment;
81  }
82 
83  return NULL;
84 }
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition: tree.c:507
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:440
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
union Assignment::@17 dest
destination workspace/output/command, depending on the type
void command_result_free(CommandResult *result)
Frees a CommandResult.
A &#39;Window&#39; is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:332
#define DLOG(fmt,...)
Definition: libi3.h:86
A struct that contains useful information about the result of a command as a whole (e...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
uint32_t nr_assignments
Pointers to the Assignments which were already ran for this Window (assignments run only once) ...
Definition: data.h:342
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:84
void run_assignments(i3Window *window)
Checks the list of assignments for the given window and runs all matching ones (unless they have alre...
Definition: assignments.c:19
struct assignments_head assignments
Definition: main.c:89
char * workspace
Definition: data.h:465
enum Assignment::@16 type
type of this assignment:
Assignment * assignment_for(i3Window *window, int type)
Returns the first matching assignment for the given window.
Definition: assignments.c:72
xcb_window_t id
Definition: data.h:333
Match match
the criteria to check if a window matches
Definition: data.h:460
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:334
char * command
Definition: data.h:464
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
Assignment ** ran_assignments
Definition: data.h:343