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)
85 DLOG(
"comparing x=%d y=%d with x=%d and y=%d width %d height %d\n",
104 Output *output, *candidate = NULL;
111 #define WIN(variable, condition) \
112 if (variable condition) { \
113 candidate = output; \
114 position = variable; \
118 if (((direction ==
D_UP) || (direction ==
D_DOWN)) &&
138 assert(candidate != NULL);
148 Output *output, *candidate = NULL;
154 if (((direction ==
D_UP) || (direction ==
D_DOWN)) &&
165 (!candidate || output->
rect.
y < candidate->
rect.
y))
170 (!candidate || output->
rect.
y > candidate->
rect.
y))
175 (!candidate || output->
rect.
x > candidate->
rect.
x))
180 (!candidate || output->
rect.
x < candidate->
rect.
x))
195 DLOG(
"RandR extension unusable, disabling.\n");
219 Con *con = NULL, *current;
222 DLOG(
"init_con for output %s\n", output->
name);
227 if (strcmp(current->name, output->
name) != 0)
232 DLOG(
"Using existing con %p / %s\n", con, con->
name);
240 con->
type = CT_OUTPUT;
253 DLOG(
"Not adding workspace, this was a reused con\n");
257 DLOG(
"Changing layout, adding top/bottom dockarea\n");
259 topdock->
type = CT_DOCKAREA;
260 topdock->
layout = L_DOCKAREA;
264 match->
dock = M_DOCK_TOP;
279 DLOG(
"adding main content container\n");
281 content->
type = CT_CON;
282 content->
layout = L_SPLITH;
293 bottomdock->
type = CT_DOCKAREA;
294 bottomdock->
layout = L_DOCKAREA;
298 match->
dock = M_DOCK_BOTTOM;
326 if (strcmp(assignment->
output, output->
name) != 0)
330 Con *workspace = NULL, *out;
333 !strcasecmp(child->name, assignment->
name));
334 if (workspace == NULL)
340 if (workspace_out == output->
con) {
341 LOG(
"Workspace \"%s\" assigned to output \"%s\", but it is already "
342 "there. Do you have two assignment directives for the same "
343 "workspace in your configuration file?\n",
349 LOG(
"Moving workspace \"%s\" from output \"%s\" to \"%s\" due to assignment\n",
356 Con *previous = NULL;
358 LOG(
"Switching to previously used workspace \"%s\" on output \"%s\"\n",
359 previous->
name, workspace_out->
name);
371 TAILQ_FOREACH(floating_con, &(workspace->floating_head), floating_windows)
382 if (visible && previous == NULL) {
383 LOG(
"There is no workspace left on \"%s\", re-initializing\n",
384 workspace_out->
name);
387 DLOG(
"Done re-initializing, continuing with \"%s\"\n", output->
name);
407 if (strcmp(assignment->
output, output->
name) != 0)
410 LOG(
"Initializing first assigned workspace \"%s\" for output \"%s\"\n",
418 DLOG(
"Now adding a workspace\n");
437 DLOG(
"Output mode changed, updating rect\n");
438 assert(output->
con != NULL);
441 Con *content, *workspace, *child;
449 TAILQ_FOREACH(child, &(workspace->floating_head), floating_windows) {
465 DLOG(
"Setting workspace [%d,%s]'s layout to %d.\n", workspace->
num, workspace->
name, workspace->
layout);
466 if ((child =
TAILQ_FIRST(&(workspace->nodes_head)))) {
467 if (child->
layout == L_SPLITV || child->
layout == L_SPLITH)
469 DLOG(
"Setting child [%d,%s]'s layout to %d.\n", child->
num, child->
name, child->
layout);
484 xcb_randr_get_output_info_reply_t *
output,
490 bool existing = (
new != NULL);
497 xcb_randr_get_output_info_name_length(output),
498 xcb_randr_get_output_info_name(output));
500 DLOG(
"found output with name %s\n", new->name);
505 if (output->crtc == XCB_NONE) {
510 }
else if (new->active)
511 new->to_be_disabled =
true;
515 xcb_randr_get_crtc_info_cookie_t icookie;
516 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts);
517 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) {
518 DLOG(
"Skipping output %s: could not get CRTC (%p)\n",
529 new->active = (
new->rect.width != 0 &&
new->rect.height != 0);
531 DLOG(
"width/height 0/0, disabling output\n");
535 DLOG(
"mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height,
536 new->rect.x, new->rect.y);
541 if (!updated || !existing) {
559 xcb_randr_get_output_primary_cookie_t pcookie;
560 xcb_randr_get_screen_resources_current_cookie_t rcookie;
568 xcb_randr_output_t *randr_outputs;
574 rcookie = xcb_randr_get_screen_resources_current(
conn,
root);
575 pcookie = xcb_randr_get_output_primary(
conn,
root);
577 if ((
primary = xcb_randr_get_output_primary_reply(
conn, pcookie, NULL)) == NULL)
578 ELOG(
"Could not get RandR primary output\n");
579 else DLOG(
"primary output is %08x\n",
primary->output);
580 if ((res = xcb_randr_get_screen_resources_current_reply(
conn, rcookie, NULL)) == NULL) {
584 cts = res->config_timestamp;
586 int len = xcb_randr_get_screen_resources_current_outputs_length(res);
587 randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
590 xcb_randr_get_output_info_cookie_t ocookie[len];
591 for (
int i = 0; i < len; i++)
592 ocookie[i] = xcb_randr_get_output_info(
conn, randr_outputs[i], cts);
595 for (
int i = 0; i < len; i++) {
596 xcb_randr_get_output_info_reply_t *
output;
598 if ((output = xcb_randr_get_output_info_reply(
conn, ocookie[i], NULL)) == NULL)
610 DLOG(
"output %p / %s, position (%d, %d), checking for clones\n",
623 DLOG(
"output %p has the same position, his mode = %d x %d\n",
635 DLOG(
"disabling output %p (%s)\n", other, other->
name);
638 DLOG(
"new output mode %d x %d, other mode %d x %d\n",
649 if (output->
active && output->
con == NULL) {
650 DLOG(
"Need to initialize a Con for output %s\n", output->
name);
661 DLOG(
"Output %s disabled, re-assigning workspaces/docks\n", output->
name);
664 die(
"No usable outputs available\n");
671 if (output->
con != NULL) {
676 DLOG(
"This output (%p) was focused! Getting next\n", output->
con);
678 DLOG(
"next = %p\n", next);
687 if (current != next &&
TAILQ_EMPTY(&(current->focus_head))) {
689 DLOG(
"Getting rid of current = %p / %s (empty, unfocused)\n", current, current->
name);
693 DLOG(
"Detaching current = %p / %s\n", current, current->
name);
695 DLOG(
"Re-attaching current = %p / %s\n", current, current->
name);
697 DLOG(
"Fixing the coordinates of floating containers\n");
699 TAILQ_FOREACH(floating_con, &(current->floating_head), floating_windows)
701 DLOG(
"Done, next\n");
703 DLOG(
"re-attached all workspaces\n");
706 DLOG(
"now focusing next = %p\n", next);
714 if (child->
type != CT_DOCKAREA)
716 DLOG(
"Handling dock con %p\n", child);
723 DLOG(
"Moving dock client %p to nc %p\n", dock, nc);
725 DLOG(
"Re-attaching\n");
731 DLOG(
"destroying disappearing con %p\n", output->
con);
733 DLOG(
"Done. Should be fine now\n");
748 ELOG(
"No outputs found via RandR, disabling\n");
759 DLOG(
"Should add ws for output %s\n", output->
name);
768 DLOG(
"Focusing primary output %s\n", output->
name);
785 const xcb_query_extension_reply_t *extreply;
787 extreply = xcb_get_extension_data(
conn, &xcb_randr_id);
788 if (!extreply->present) {
793 if (event_base != NULL)
794 *event_base = extreply->first_event;
797 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
798 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
799 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
800 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);