i3
assignments.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 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  bool needs_tree_render = false;
21 
22  /* Check if any assignments match */
23  Assignment *current;
25  if (current->type != A_COMMAND || !match_matches_window(&(current->match), window))
26  continue;
27 
28  bool skip = false;
29  for (uint32_t c = 0; c < window->nr_assignments; c++) {
30  if (window->ran_assignments[c] != current)
31  continue;
32 
33  DLOG("This assignment already ran for the given window, not executing it again.\n");
34  skip = true;
35  break;
36  }
37 
38  if (skip)
39  continue;
40 
41  /* Store that we ran this assignment to not execute it again. We have
42  * to do this before running the actual command to prevent infinite
43  * loops. */
44  window->nr_assignments++;
45  window->ran_assignments = srealloc(window->ran_assignments, sizeof(Assignment *) * window->nr_assignments);
46  window->ran_assignments[window->nr_assignments - 1] = current;
47 
48  DLOG("matching assignment, execute command %s\n", current->dest.command);
49  char *full_command;
50  sasprintf(&full_command, "[id=\"%d\"] %s", window->id, current->dest.command);
51  CommandResult *result = parse_command(full_command, NULL, NULL);
52  free(full_command);
53 
54  if (result->needs_tree_render)
55  needs_tree_render = true;
56 
57  command_result_free(result);
58  }
59 
60  /* If any of the commands required re-rendering, we will do that now. */
61  if (needs_tree_render)
62  tree_render();
63 }
64 
65 /*
66  * Returns the first matching assignment for the given window.
67  *
68  */
69 Assignment *assignment_for(i3Window *window, int type) {
70  Assignment *assignment;
71 
72  TAILQ_FOREACH (assignment, &assignments, assignments) {
73  if ((type != A_ANY && (assignment->type & type) == 0) ||
74  !match_matches_window(&(assignment->match), window))
75  continue;
76  DLOG("got a matching assignment\n");
77  return assignment;
78  }
79 
80  return NULL;
81 }
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:17
Assignment * assignment_for(i3Window *window, int type)
Returns the first matching assignment for the given window.
Definition: assignments.c:69
CommandResult * parse_command(const char *input, yajl_gen gen, ipc_client *client)
Parses and executes the given command.
void command_result_free(CommandResult *result)
Frees a CommandResult.
struct assignments_head assignments
Definition: main.c:97
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:89
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:451
#define DLOG(fmt,...)
Definition: libi3.h:105
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...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
A struct that contains useful information about the result of a command as a whole (e....
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:394
xcb_window_t id
Definition: data.h:395
Assignment ** ran_assignments
Definition: data.h:405
uint32_t nr_assignments
Pointers to the Assignments which were already ran for this Window (assignments run only once)
Definition: data.h:404
An Assignment makes specific windows go to a specific workspace/output or run a command for that wind...
Definition: data.h:564
Match match
the criteria to check if a window matches
Definition: data.h:586
union Assignment::@17 dest
destination workspace/command/output, depending on the type
char * command
Definition: data.h:590
enum Assignment::@16 type
type of this assignment: