2 #define I3__FILE__ "randr.c"
17 #include <xcb/randr.h>
25 xcb_randr_get_output_primary_reply_t *
primary;
55 strcasecmp(output->
name, name) == 0)
72 die(
"No usable outputs available.\n");
85 DLOG(
"comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
105 int lx = rect.
x, uy = rect.
y;
110 DLOG(
"comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
136 else if (direction ==
D_LEFT)
138 else if (direction ==
D_DOWN)
146 DLOG(
"current = %s, best = %s\n", current->
name, best->
name);
170 other = &(output->
rect);
172 if ((direction ==
D_RIGHT && other->x > cur->
x) ||
173 (direction ==
D_LEFT && other->x < cur->
x)) {
176 if ((other->y + other->height) <= cur->
y ||
177 (cur->
y + cur->
height) <= other->y)
179 }
else if ((direction ==
D_DOWN && other->y > cur->
y) ||
180 (direction ==
D_UP && other->y < cur->
y)) {
183 if ((other->x + other->width) <= cur->
x ||
184 (cur->
x + cur->
width) <= other->x)
198 if ((direction ==
D_RIGHT && other->x < best->
rect.
x) ||
199 (direction ==
D_LEFT && other->x > best->
rect.
x) ||
200 (direction ==
D_DOWN && other->y < best->
rect.
y) ||
201 (direction ==
D_UP && other->y > best->
rect.
y)) {
208 if ((direction ==
D_RIGHT && other->x > best->
rect.
x) ||
209 (direction ==
D_LEFT && other->x < best->
rect.
x) ||
210 (direction ==
D_DOWN && other->y > best->
rect.
y) ||
211 (direction ==
D_UP && other->y < best->
rect.
y)) {
218 DLOG(
"current = %s, best = %s\n", current->
name, (best ? best->
name :
"NULL"));
228 DLOG(
"RandR extension unusable, disabling.\n");
252 Con *con = NULL, *current;
255 DLOG(
"init_con for output %s\n", output->
name);
260 if (strcmp(current->name, output->
name) != 0)
265 DLOG(
"Using existing con %p / %s\n", con, con->
name);
273 con->
type = CT_OUTPUT;
286 DLOG(
"Not adding workspace, this was a reused con\n");
290 DLOG(
"Changing layout, adding top/bottom dockarea\n");
292 topdock->
type = CT_DOCKAREA;
297 match->
dock = M_DOCK_TOP;
312 DLOG(
"adding main content container\n");
314 content->
type = CT_CON;
326 bottomdock->
type = CT_DOCKAREA;
331 match->
dock = M_DOCK_BOTTOM;
359 if (strcmp(assignment->
output, output->
name) != 0)
363 Con *workspace = NULL, *out;
366 !strcasecmp(child->name, assignment->
name));
367 if (workspace == NULL)
373 if (workspace_out == output->
con) {
374 LOG(
"Workspace \"%s\" assigned to output \"%s\", but it is already "
375 "there. Do you have two assignment directives for the same "
376 "workspace in your configuration file?\n",
382 LOG(
"Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
389 Con *previous = NULL;
391 LOG(
"Switching to previously used workspace \"%s\" on output \"%s\"\n",
392 previous->
name, workspace_out->
name);
404 TAILQ_FOREACH (floating_con, &(workspace->floating_head), floating_windows)
415 if (visible && previous == NULL) {
416 LOG(
"There is no workspace left on \"%s\", re-initializing\n",
417 workspace_out->
name);
420 DLOG(
"Done re-initializing, continuing with \"%s\"\n", output->
name);
440 if (strcmp(assignment->
output, output->
name) != 0)
443 LOG(
"Initializing first assigned workspace \"%s\" for output \"%s\"\n",
451 DLOG(
"Now adding a workspace\n");
470 DLOG(
"Output mode changed, updating rect\n");
471 assert(output->
con != NULL);
474 Con *content, *workspace, *child;
482 TAILQ_FOREACH (child, &(workspace->floating_head), floating_windows) {
498 DLOG(
"Setting workspace [%d,%s]'s layout to %d.\n", workspace->
num, workspace->
name, workspace->
layout);
499 if ((child =
TAILQ_FIRST(&(workspace->nodes_head)))) {
502 DLOG(
"Setting child [%d,%s]'s layout to %d.\n", child->
num, child->
name, child->
layout);
517 xcb_randr_get_output_info_reply_t *
output,
523 bool existing = (
new != NULL);
530 xcb_randr_get_output_info_name_length(output),
531 xcb_randr_get_output_info_name(output));
533 DLOG(
"found output with name %s\n", new->name);
538 if (output->crtc == XCB_NONE) {
544 }
else if (new->active)
545 new->to_be_disabled =
true;
549 xcb_randr_get_crtc_info_cookie_t icookie;
550 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
551 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
552 DLOG(
"Skipping output %s: could not get CRTC (%p)\n",
563 new->active = (
new->rect.width != 0 &&
new->rect.height != 0);
565 DLOG(
"width/height 0/0, disabling output\n");
569 DLOG(
"mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
570 new->rect.x, new->rect.y);
575 if (!updated || !existing) {
594 xcb_randr_get_output_primary_cookie_t pcookie;
595 xcb_randr_get_screen_resources_current_cookie_t rcookie;
603 xcb_randr_output_t *randr_outputs;
609 rcookie = xcb_randr_get_screen_resources_current(
conn,
root);
610 pcookie = xcb_randr_get_output_primary(
conn,
root);
612 if ((
primary = xcb_randr_get_output_primary_reply(
conn, pcookie, NULL)) == NULL)
613 ELOG(
"Could not get RandR primary output\n");
616 if ((res = xcb_randr_get_screen_resources_current_reply(
conn, rcookie, NULL)) == NULL) {
620 cts = res->config_timestamp;
622 int len = xcb_randr_get_screen_resources_current_outputs_length(res);
623 randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
626 xcb_randr_get_output_info_cookie_t ocookie[len];
627 for (
int i = 0; i < len; i++)
628 ocookie[i] = xcb_randr_get_output_info(
conn, randr_outputs[i], cts);
631 for (
int i = 0; i < len; i++) {
632 xcb_randr_get_output_info_reply_t *
output;
634 if ((output = xcb_randr_get_output_info_reply(
conn, ocookie[i], NULL)) == NULL)
646 DLOG(
"output %p / %s, position (%d, %d), checking for clones\n",
659 DLOG(
"output %p has the same position, his mode = %d x %d\n",
671 DLOG(
"disabling output %p (%s)\n", other, other->
name);
674 DLOG(
"new output mode %d x %d, other mode %d x %d\n",
685 if (output->
active && output->
con == NULL) {
686 DLOG(
"Need to initialize a Con for output %s\n", output->
name);
697 DLOG(
"Output %s disabled, re-assigning workspaces/docks\n", output->
name);
706 if (output->
con != NULL) {
711 DLOG(
"This output (%p) was focused! Getting next\n", output->
con);
713 DLOG(
"next = %p\n", next);
722 if (current != next &&
TAILQ_EMPTY(&(current->focus_head))) {
724 DLOG(
"Getting rid of current = %p / %s (empty, unfocused)\n", current, current->
name);
728 DLOG(
"Detaching current = %p / %s\n", current, current->
name);
730 DLOG(
"Re-attaching current = %p / %s\n", current, current->
name);
732 DLOG(
"Fixing the coordinates of floating containers\n");
734 TAILQ_FOREACH (floating_con, &(current->floating_head), floating_windows)
736 DLOG(
"Done, next\n");
738 DLOG(
"re-attached all workspaces\n");
741 DLOG(
"now focusing next = %p\n", next);
749 if (child->
type != CT_DOCKAREA)
751 DLOG(
"Handling dock con %p\n", child);
758 DLOG(
"Moving dock client %p to nc %p\n", dock, nc);
760 DLOG(
"Re-attaching\n");
766 DLOG(
"destroying disappearing con %p\n", output->
con);
768 DLOG(
"Done. Should be fine now\n");
783 ELOG(
"No outputs found via RandR, disabling\n");
797 DLOG(
"Should add ws for output %s\n", output->
name);
806 DLOG(
"Focusing primary output %s\n", output->
name);
823 const xcb_query_extension_reply_t *extreply;
825 extreply = xcb_get_extension_data(
conn, &xcb_randr_id);
826 if (!extreply->present) {
832 if (event_base != NULL)
833 *event_base = extreply->first_event;
836 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
837 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
838 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
839 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
bool update_if_necessary(uint32_t *destination, const uint32_t new_value)
Updates *destination with new_value and returns true if it was changed or false if it was the same...
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
Output * get_output_by_name(const char *name)
Returns the output with the given name if it is active (!) or NULL.
char * name
Name of the output.
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
xcb_randr_get_crtc_info_reply_t crtc_info
Output * get_output_next(direction_t direction, Output *current, output_close_far_t close_far)
Gets the output which is the next one in the given direction.
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Stores which workspace (by name or number) goes to which output.
Stores a rectangle, for example the size of a window, the child window etc.
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
void init_ws_for_output(Output *output, Con *content)
Initializes at least one workspace for this output, trying the following steps until there is at leas...
void con_detach(Con *con)
Detaches the given container from its current parent.
xcb_randr_output_t id
Output id, so that we can requery the output directly later.
void workspace_show(Con *workspace)
Switches to the given workspace.
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Rect rect
x, y, width, height
xcb_screen_t * root_screen
#define TAILQ_FIRST(head)
static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id, xcb_randr_get_output_info_reply_t *output, xcb_timestamp_t cts, resources_reply *res)
Con * create_workspace_on_output(Output *output, Con *content)
An Output is a physical output on your graphics driver.
#define TAILQ_NEXT(elm, field)
Con * con_new(Con *parent, i3Window *window)
void workspace_show_by_name(const char *num)
Looks up the workspace by name and switches to it.
Output * get_output_next_wrap(direction_t direction, Output *current)
Like get_output_next with close_far == CLOSEST_OUTPUT, but wraps.
int default_orientation
Default orientation for new containers.
void disable_randr(xcb_connection_t *conn)
Disables RandR support by creating exactly one output with the size of the X11 screen.
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 randr_query_outputs(void)
Initializes the specified output, assigning the specified workspace to it.
bool tree_close(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus)
Closes the given container including all children.
struct outputs_head outputs
Con * con_for_window(Con *con, i3Window *window, Match **store_match)
Returns the first container below 'con' which wants to swallow this window TODO: priority.
#define TAILQ_INSERT_HEAD(head, elm, field)
static void output_change_mode(xcb_connection_t *conn, Output *output)
Con * con
Pointer to the Con which represents this output.
bool contained_by_output(Rect rect)
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
fullscreen_mode_t fullscreen_mode
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name...
void con_focus(Con *con)
Sets input focus to the given container.
#define TAILQ_EMPTY(head)
static bool randr_disabled
int con_num_children(Con *con)
Returns the number of children of this container.
Output * get_first_output(void)
Returns the first output which is active.
bool active
Whether the output is currently active (has a CRTC attached with a valid mode)
bool changed
Internal flags, necessary for querying RandR screens (happens in two stages)
static Output * get_output_by_id(xcb_randr_output_t id)
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
void randr_init(int *event_base)
We have just established a connection to the X server and need the initial XRandR information to setu...
#define GREP_FIRST(dest, head, condition)
A 'Con' represents everything from the X11 root window down to a single X11 window.
void * scalloc(size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
xcb_randr_get_output_primary_reply_t * primary
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
enum Match::@15 insert_where
#define TAILQ_INSERT_TAIL(head, elm, field)
#define TAILQ_HEAD_INITIALIZER(head)
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect)
Fixes the coordinates of the floating window whenever the window gets reassigned to a different outpu...
#define TAILQ_FOREACH(var, head, field)
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
xcb_randr_get_screen_resources_current_reply_t resources_reply
struct ws_assignments_head ws_assignments
void match_init(Match *match)
void output_init_con(Output *output)
Initializes a CT_OUTPUT Con (searches existing ones from inplace restart before) to use for the given...
void render_con(Con *con, bool render_fullscreen)
"Renders" the given container (and its children), meaning that all rects are updated correctly...