i3
|
00001 /* 00002 * vim:ts=8:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * 00006 * © 2009-2010 Michael Stapelberg and contributors 00007 * 00008 * See file LICENSE for license information. 00009 * 00010 * For more information on RandR, please see the X.org RandR specification at 00011 * http://cgit.freedesktop.org/xorg/proto/randrproto/tree/randrproto.txt 00012 * (take your time to read it completely, it answers all questions). 00013 * 00014 */ 00015 #include <stdio.h> 00016 #include <stdlib.h> 00017 #include <string.h> 00018 #include <stdbool.h> 00019 #include <assert.h> 00020 #include <time.h> 00021 #include <unistd.h> 00022 00023 #include <xcb/xcb.h> 00024 #include <xcb/randr.h> 00025 00026 #include "queue.h" 00027 #include "i3.h" 00028 #include "data.h" 00029 #include "table.h" 00030 #include "util.h" 00031 #include "layout.h" 00032 #include "xcb.h" 00033 #include "config.h" 00034 #include "workspace.h" 00035 #include "log.h" 00036 #include "ewmh.h" 00037 #include "ipc.h" 00038 #include "client.h" 00039 00040 /* While a clean namespace is usually a pretty good thing, we really need 00041 * to use shorter names than the whole xcb_randr_* default names. */ 00042 typedef xcb_randr_get_crtc_info_reply_t crtc_info; 00043 typedef xcb_randr_mode_info_t mode_info; 00044 typedef xcb_randr_get_screen_resources_current_reply_t resources_reply; 00045 00046 /* Stores all outputs available in your current session. */ 00047 struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs); 00048 00049 static bool randr_disabled = false; 00050 00051 /* 00052 * Get a specific output by its internal X11 id. Used by randr_query_outputs 00053 * to check if the output is new (only in the first scan) or if we are 00054 * re-scanning. 00055 * 00056 */ 00057 static Output *get_output_by_id(xcb_randr_output_t id) { 00058 Output *output; 00059 TAILQ_FOREACH(output, &outputs, outputs) 00060 if (output->id == id) 00061 return output; 00062 00063 return NULL; 00064 } 00065 00066 /* 00067 * Returns the output with the given name if it is active (!) or NULL. 00068 * 00069 */ 00070 Output *get_output_by_name(const char *name) { 00071 Output *output; 00072 TAILQ_FOREACH(output, &outputs, outputs) 00073 if (output->active && 00074 strcasecmp(output->name, name) == 0) 00075 return output; 00076 00077 return NULL; 00078 } 00079 00080 /* 00081 * Returns the first output which is active. 00082 * 00083 */ 00084 Output *get_first_output() { 00085 Output *output; 00086 00087 TAILQ_FOREACH(output, &outputs, outputs) 00088 if (output->active) 00089 return output; 00090 00091 return NULL; 00092 } 00093 00094 /* 00095 * Returns the active (!) output which contains the coordinates x, y or NULL 00096 * if there is no output which contains these coordinates. 00097 * 00098 */ 00099 Output *get_output_containing(int x, int y) { 00100 Output *output; 00101 TAILQ_FOREACH(output, &outputs, outputs) { 00102 if (!output->active) 00103 continue; 00104 DLOG("comparing x=%d y=%d with x=%d and y=%d width %d height %d\n", 00105 x, y, output->rect.x, output->rect.y, output->rect.width, output->rect.height); 00106 if (x >= output->rect.x && x < (output->rect.x + output->rect.width) && 00107 y >= output->rect.y && y < (output->rect.y + output->rect.height)) 00108 return output; 00109 } 00110 00111 return NULL; 00112 } 00113 00114 /* 00115 * Gets the output which is the last one in the given direction, for example 00116 * the output on the most bottom when direction == D_DOWN, the output most 00117 * right when direction == D_RIGHT and so on. 00118 * 00119 * This function always returns a output. 00120 * 00121 */ 00122 Output *get_output_most(direction_t direction, Output *current) { 00123 Output *output, *candidate = NULL; 00124 int position = 0; 00125 TAILQ_FOREACH(output, &outputs, outputs) { 00126 if (!output->active) 00127 continue; 00128 00129 /* Repeated calls of WIN determine the winner of the comparison */ 00130 #define WIN(variable, condition) \ 00131 if (variable condition) { \ 00132 candidate = output; \ 00133 position = variable; \ 00134 } \ 00135 break; 00136 00137 if (((direction == D_UP) || (direction == D_DOWN)) && 00138 (current->rect.x != output->rect.x)) 00139 continue; 00140 00141 if (((direction == D_LEFT) || (direction == D_RIGHT)) && 00142 (current->rect.y != output->rect.y)) 00143 continue; 00144 00145 switch (direction) { 00146 case D_UP: 00147 WIN(output->rect.y, <= position); 00148 case D_DOWN: 00149 WIN(output->rect.y, >= position); 00150 case D_LEFT: 00151 WIN(output->rect.x, <= position); 00152 case D_RIGHT: 00153 WIN(output->rect.x, >= position); 00154 } 00155 } 00156 00157 assert(candidate != NULL); 00158 00159 return candidate; 00160 } 00161 00162 /* 00163 * Initializes the specified output, assigning the specified workspace to it. 00164 * 00165 */ 00166 void initialize_output(xcb_connection_t *conn, Output *output, Workspace *workspace) { 00167 i3Font *font = load_font(conn, config.font); 00168 00169 workspace->output = output; 00170 output->current_workspace = workspace; 00171 00172 /* Copy rect for the workspace */ 00173 memcpy(&(workspace->rect), &(output->rect), sizeof(Rect)); 00174 00175 /* Map clients on the workspace, if any */ 00176 workspace_map_clients(conn, workspace); 00177 00178 /* Create a bar window on each output */ 00179 if (!config.disable_workspace_bar) { 00180 Rect bar_rect = {output->rect.x, 00181 output->rect.y + output->rect.height - (font->height + 6), 00182 output->rect.x + output->rect.width, 00183 font->height + 6}; 00184 uint32_t mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK; 00185 uint32_t values[] = {1, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS}; 00186 output->bar = create_window(conn, bar_rect, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_CURSOR_LEFT_PTR, true, mask, values); 00187 output->bargc = xcb_generate_id(conn); 00188 xcb_create_gc(conn, output->bargc, output->bar, 0, 0); 00189 } 00190 00191 SLIST_INIT(&(output->dock_clients)); 00192 00193 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}"); 00194 DLOG("initialized output at (%d, %d) with %d x %d\n", 00195 output->rect.x, output->rect.y, output->rect.width, output->rect.height); 00196 00197 DLOG("assigning configured workspaces to this output...\n"); 00198 Workspace *ws; 00199 TAILQ_FOREACH(ws, workspaces, workspaces) { 00200 if (ws == workspace) 00201 continue; 00202 if (ws->preferred_output == NULL || 00203 get_output_by_name(ws->preferred_output) != output) 00204 continue; 00205 00206 DLOG("assigning ws %d\n", ws->num + 1); 00207 workspace_assign_to(ws, output, true); 00208 } 00209 } 00210 00211 /* 00212 * Disables RandR support by creating exactly one output with the size of the 00213 * X11 screen. 00214 * 00215 */ 00216 void disable_randr(xcb_connection_t *conn) { 00217 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data; 00218 00219 DLOG("RandR extension unusable, disabling.\n"); 00220 00221 Output *s = scalloc(sizeof(Output)); 00222 00223 s->active = true; 00224 s->rect.x = 0; 00225 s->rect.y = 0; 00226 s->rect.width = root_screen->width_in_pixels; 00227 s->rect.height = root_screen->height_in_pixels; 00228 s->name = "xroot-0"; 00229 00230 TAILQ_INSERT_TAIL(&outputs, s, outputs); 00231 00232 randr_disabled = true; 00233 } 00234 00235 /* 00236 * This function needs to be called when changing the mode of an output when 00237 * it already has some workspaces (or a bar window) assigned. 00238 * 00239 * It reconfigures the bar window for the new mode, copies the new rect into 00240 * each workspace on this output and forces all windows on the affected 00241 * workspaces to be reconfigured. 00242 * 00243 * It is necessary to call render_layout() afterwards. 00244 * 00245 */ 00246 static void output_change_mode(xcb_connection_t *conn, Output *output) { 00247 i3Font *font = load_font(conn, config.font); 00248 Workspace *ws; 00249 Client *client; 00250 00251 DLOG("Output mode changed, reconfiguring bar, updating workspaces\n"); 00252 Rect bar_rect = {output->rect.x, 00253 output->rect.y + output->rect.height - (font->height + 6), 00254 output->rect.x + output->rect.width, 00255 font->height + 6}; 00256 00257 xcb_set_window_rect(conn, output->bar, bar_rect); 00258 00259 /* go through all workspaces and set force_reconfigure */ 00260 TAILQ_FOREACH(ws, workspaces, workspaces) { 00261 if (ws->output != output) 00262 continue; 00263 00264 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) { 00265 client->force_reconfigure = true; 00266 if (!client_is_floating(client)) 00267 continue; 00268 /* For floating clients we need to translate the 00269 * coordinates (old workspace to new workspace) */ 00270 DLOG("old: (%x, %x)\n", client->rect.x, client->rect.y); 00271 client->rect.x -= ws->rect.x; 00272 client->rect.y -= ws->rect.y; 00273 client->rect.x += ws->output->rect.x; 00274 client->rect.y += ws->output->rect.y; 00275 DLOG("new: (%x, %x)\n", client->rect.x, client->rect.y); 00276 } 00277 00278 /* Update dimensions from output */ 00279 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect)); 00280 00281 /* Update the dimensions of a fullscreen client, if any */ 00282 if (ws->fullscreen_client != NULL) { 00283 DLOG("Updating fullscreen client size\n"); 00284 client = ws->fullscreen_client; 00285 Rect r = ws->rect; 00286 xcb_set_window_rect(conn, client->frame, r); 00287 00288 r.x = 0; 00289 r.y = 0; 00290 xcb_set_window_rect(conn, client->child, r); 00291 } 00292 } 00293 } 00294 00295 /* 00296 * Gets called by randr_query_outputs() for each output. The function adds new 00297 * outputs to the list of outputs, checks if the mode of existing outputs has 00298 * been changed or if an existing output has been disabled. It will then change 00299 * either the "changed" or the "to_be_deleted" flag of the output, if 00300 * appropriate. 00301 * 00302 */ 00303 static void handle_output(xcb_connection_t *conn, xcb_randr_output_t id, 00304 xcb_randr_get_output_info_reply_t *output, 00305 xcb_timestamp_t cts, resources_reply *res) { 00306 /* each CRT controller has a position in which we are interested in */ 00307 crtc_info *crtc; 00308 00309 Output *new = get_output_by_id(id); 00310 bool existing = (new != NULL); 00311 if (!existing) 00312 new = scalloc(sizeof(Output)); 00313 new->id = id; 00314 FREE(new->name); 00315 asprintf(&new->name, "%.*s", 00316 xcb_randr_get_output_info_name_length(output), 00317 xcb_randr_get_output_info_name(output)); 00318 00319 DLOG("found output with name %s\n", new->name); 00320 00321 /* Even if no CRTC is used at the moment, we store the output so that 00322 * we do not need to change the list ever again (we only update the 00323 * position/size) */ 00324 if (output->crtc == XCB_NONE) { 00325 if (!existing) 00326 TAILQ_INSERT_TAIL(&outputs, new, outputs); 00327 else if (new->active) 00328 new->to_be_disabled = true; 00329 return; 00330 } 00331 00332 xcb_randr_get_crtc_info_cookie_t icookie; 00333 icookie = xcb_randr_get_crtc_info(conn, output->crtc, cts); 00334 if ((crtc = xcb_randr_get_crtc_info_reply(conn, icookie, NULL)) == NULL) { 00335 DLOG("Skipping output %s: could not get CRTC (%p)\n", 00336 new->name, crtc); 00337 free(new); 00338 return; 00339 } 00340 00341 bool updated = update_if_necessary(&(new->rect.x), crtc->x) | 00342 update_if_necessary(&(new->rect.y), crtc->y) | 00343 update_if_necessary(&(new->rect.width), crtc->width) | 00344 update_if_necessary(&(new->rect.height), crtc->height); 00345 free(crtc); 00346 new->active = (new->rect.width != 0 && new->rect.height != 0); 00347 if (!new->active) { 00348 DLOG("width/height 0/0, disabling output\n"); 00349 return; 00350 } 00351 00352 DLOG("mode: %dx%d+%d+%d\n", new->rect.width, new->rect.height, 00353 new->rect.x, new->rect.y); 00354 00355 /* If we don’t need to change an existing output or if the output 00356 * does not exist in the first place, the case is simple: we either 00357 * need to insert the new output or we are done. */ 00358 if (!updated || !existing) { 00359 if (!existing) 00360 TAILQ_INSERT_TAIL(&outputs, new, outputs); 00361 return; 00362 } 00363 00364 new->changed = true; 00365 } 00366 00367 static void init_workspaces() { 00368 Output *output; 00369 Workspace *ws; 00370 00371 /* Just go through each active output and associate one workspace */ 00372 TAILQ_FOREACH(output, &outputs, outputs) { 00373 if (!output->active || output->current_workspace != NULL) 00374 continue; 00375 ws = get_first_workspace_for_output(output); 00376 initialize_output(global_conn, output, ws); 00377 } 00378 00379 /* render_layout flushes */ 00380 render_layout(global_conn); 00381 } 00382 00383 /* 00384 * (Re-)queries the outputs via RandR and stores them in the list of outputs. 00385 * 00386 */ 00387 void randr_query_outputs(xcb_connection_t *conn) { 00388 Workspace *ws; 00389 Output *output, *other, *first; 00390 xcb_randr_get_screen_resources_current_cookie_t rcookie; 00391 resources_reply *res; 00392 /* timestamp of the configuration so that we get consistent replies to all 00393 * requests (if the configuration changes between our different calls) */ 00394 xcb_timestamp_t cts; 00395 00396 /* an output is VGA-1, LVDS-1, etc. (usually physical video outputs) */ 00397 xcb_randr_output_t *randr_outputs; 00398 00399 if (randr_disabled) 00400 return; 00401 00402 /* Get screen resources (crtcs, outputs, modes) */ 00403 rcookie = xcb_randr_get_screen_resources_current(conn, root); 00404 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) { 00405 disable_randr(conn); 00406 init_workspaces(); 00407 return; 00408 } 00409 cts = res->config_timestamp; 00410 00411 int len = xcb_randr_get_screen_resources_current_outputs_length(res); 00412 randr_outputs = xcb_randr_get_screen_resources_current_outputs(res); 00413 00414 /* Request information for each output */ 00415 xcb_randr_get_output_info_cookie_t ocookie[len]; 00416 for (int i = 0; i < len; i++) 00417 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts); 00418 00419 /* Loop through all outputs available for this X11 screen */ 00420 for (int i = 0; i < len; i++) { 00421 xcb_randr_get_output_info_reply_t *output; 00422 00423 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL) 00424 continue; 00425 00426 handle_output(conn, randr_outputs[i], output, cts, res); 00427 free(output); 00428 } 00429 00430 free(res); 00431 /* Check for clones, disable the clones and reduce the mode to the 00432 * lowest common mode */ 00433 TAILQ_FOREACH(output, &outputs, outputs) { 00434 if (!output->active || output->to_be_disabled) 00435 continue; 00436 DLOG("output %p, position (%d, %d), checking for clones\n", 00437 output, output->rect.x, output->rect.y); 00438 00439 for (other = output; 00440 other != TAILQ_END(&outputs); 00441 other = TAILQ_NEXT(other, outputs)) { 00442 if (other == output || !other->active || other->to_be_disabled) 00443 continue; 00444 00445 if (other->rect.x != output->rect.x || 00446 other->rect.y != output->rect.y) 00447 continue; 00448 00449 DLOG("output %p has the same position, his mode = %d x %d\n", 00450 other, other->rect.width, other->rect.height); 00451 uint32_t width = min(other->rect.width, output->rect.width); 00452 uint32_t height = min(other->rect.height, output->rect.height); 00453 00454 if (update_if_necessary(&(output->rect.width), width) | 00455 update_if_necessary(&(output->rect.height), height)) 00456 output->changed = true; 00457 00458 update_if_necessary(&(other->rect.width), width); 00459 update_if_necessary(&(other->rect.height), height); 00460 00461 DLOG("disabling output %p (%s)\n", other, other->name); 00462 other->to_be_disabled = true; 00463 00464 DLOG("new output mode %d x %d, other mode %d x %d\n", 00465 output->rect.width, output->rect.height, 00466 other->rect.width, other->rect.height); 00467 } 00468 } 00469 00470 /* Handle outputs which have a new mode or are disabled now (either 00471 * because the user disabled them or because they are clones) */ 00472 TAILQ_FOREACH(output, &outputs, outputs) { 00473 if (output->to_be_disabled) { 00474 output->active = false; 00475 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name); 00476 00477 if ((first = get_first_output()) == NULL) 00478 die("No usable outputs available\n"); 00479 00480 bool needs_init = (first->current_workspace == NULL); 00481 00482 TAILQ_FOREACH(ws, workspaces, workspaces) { 00483 if (ws->output != output) 00484 continue; 00485 00486 workspace_assign_to(ws, first, true); 00487 if (!needs_init) 00488 continue; 00489 initialize_output(conn, first, ws); 00490 needs_init = false; 00491 } 00492 00493 Client *dock; 00494 while (!SLIST_EMPTY(&(output->dock_clients))) { 00495 dock = SLIST_FIRST(&(output->dock_clients)); 00496 SLIST_REMOVE_HEAD(&(output->dock_clients), dock_clients); 00497 SLIST_INSERT_HEAD(&(first->dock_clients), dock, dock_clients); 00498 } 00499 output->current_workspace = NULL; 00500 output->to_be_disabled = false; 00501 } else if (output->changed) { 00502 output_change_mode(conn, output); 00503 output->changed = false; 00504 } 00505 } 00506 00507 if (TAILQ_EMPTY(&outputs)) { 00508 ELOG("No outputs found via RandR, disabling\n"); 00509 disable_randr(conn); 00510 } 00511 00512 ewmh_update_workarea(); 00513 00514 init_workspaces(); 00515 } 00516 00517 /* 00518 * We have just established a connection to the X server and need the initial 00519 * XRandR information to setup workspaces for each screen. 00520 * 00521 */ 00522 void initialize_randr(xcb_connection_t *conn, int *event_base) { 00523 const xcb_query_extension_reply_t *extreply; 00524 00525 extreply = xcb_get_extension_data(conn, &xcb_randr_id); 00526 if (!extreply->present) { 00527 disable_randr(conn); 00528 init_workspaces(); 00529 return; 00530 } 00531 00532 randr_query_outputs(conn); 00533 00534 if (event_base != NULL) 00535 *event_base = extreply->first_event; 00536 00537 xcb_randr_select_input(conn, root, 00538 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE | 00539 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE | 00540 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE | 00541 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY); 00542 00543 xcb_flush(conn); 00544 }