00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00041
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
00047 struct outputs_head outputs = TAILQ_HEAD_INITIALIZER(outputs);
00048
00049 static bool randr_disabled = false;
00050
00051
00052
00053
00054
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
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
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
00096
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
00116
00117
00118
00119
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
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
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
00173 memcpy(&(workspace->rect), &(output->rect), sizeof(Rect));
00174
00175
00176 workspace_map_clients(conn, workspace);
00177
00178
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
00213
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
00237
00238
00239
00240
00241
00242
00243
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
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
00269
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
00279 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
00280
00281
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
00297
00298
00299
00300
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
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
00322
00323
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
00356
00357
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
00368
00369
00370
00371 void randr_query_outputs(xcb_connection_t *conn) {
00372 Workspace *ws;
00373 Output *output, *other, *first;
00374 xcb_randr_get_screen_resources_current_cookie_t rcookie;
00375 resources_reply *res;
00376
00377
00378 xcb_timestamp_t cts;
00379
00380
00381 xcb_randr_output_t *randr_outputs;
00382
00383 if (randr_disabled)
00384 return;
00385
00386
00387 rcookie = xcb_randr_get_screen_resources_current(conn, root);
00388 if ((res = xcb_randr_get_screen_resources_current_reply(conn, rcookie, NULL)) == NULL) {
00389 disable_randr(conn);
00390 return;
00391 }
00392 cts = res->config_timestamp;
00393
00394 int len = xcb_randr_get_screen_resources_current_outputs_length(res);
00395 randr_outputs = xcb_randr_get_screen_resources_current_outputs(res);
00396
00397
00398 xcb_randr_get_output_info_cookie_t ocookie[len];
00399 for (int i = 0; i < len; i++)
00400 ocookie[i] = xcb_randr_get_output_info(conn, randr_outputs[i], cts);
00401
00402
00403 for (int i = 0; i < len; i++) {
00404 xcb_randr_get_output_info_reply_t *output;
00405
00406 if ((output = xcb_randr_get_output_info_reply(conn, ocookie[i], NULL)) == NULL)
00407 continue;
00408
00409 handle_output(conn, randr_outputs[i], output, cts, res);
00410 free(output);
00411 }
00412
00413 free(res);
00414
00415
00416 TAILQ_FOREACH(output, &outputs, outputs) {
00417 if (!output->active || output->to_be_disabled)
00418 continue;
00419 DLOG("output %p, position (%d, %d), checking for clones\n",
00420 output, output->rect.x, output->rect.y);
00421
00422 for (other = output;
00423 other != TAILQ_END(&outputs);
00424 other = TAILQ_NEXT(other, outputs)) {
00425 if (other == output || !other->active || other->to_be_disabled)
00426 continue;
00427
00428 if (other->rect.x != output->rect.x ||
00429 other->rect.y != output->rect.y)
00430 continue;
00431
00432 DLOG("output %p has the same position, his mode = %d x %d\n",
00433 other, other->rect.width, other->rect.height);
00434 uint32_t width = min(other->rect.width, output->rect.width);
00435 uint32_t height = min(other->rect.height, output->rect.height);
00436
00437 if (update_if_necessary(&(output->rect.width), width) |
00438 update_if_necessary(&(output->rect.height), height))
00439 output->changed = true;
00440
00441 update_if_necessary(&(other->rect.width), width);
00442 update_if_necessary(&(other->rect.height), height);
00443
00444 DLOG("disabling output %p (%s)\n", other, other->name);
00445 other->to_be_disabled = true;
00446
00447 DLOG("new output mode %d x %d, other mode %d x %d\n",
00448 output->rect.width, output->rect.height,
00449 other->rect.width, other->rect.height);
00450 }
00451 }
00452
00453
00454
00455 TAILQ_FOREACH(output, &outputs, outputs) {
00456 if (output->to_be_disabled) {
00457 output->active = false;
00458 DLOG("Output %s disabled, re-assigning workspaces/docks\n", output->name);
00459
00460 if ((first = get_first_output()) == NULL)
00461 die("No usable outputs available\n");
00462
00463 bool needs_init = (first->current_workspace == NULL);
00464
00465 TAILQ_FOREACH(ws, workspaces, workspaces) {
00466 if (ws->output != output)
00467 continue;
00468
00469 workspace_assign_to(ws, first, true);
00470 if (!needs_init)
00471 continue;
00472 initialize_output(conn, first, ws);
00473 needs_init = false;
00474 }
00475
00476 Client *dock;
00477 while (!SLIST_EMPTY(&(output->dock_clients))) {
00478 dock = SLIST_FIRST(&(output->dock_clients));
00479 SLIST_REMOVE_HEAD(&(output->dock_clients), dock_clients);
00480 SLIST_INSERT_HEAD(&(first->dock_clients), dock, dock_clients);
00481 }
00482 output->current_workspace = NULL;
00483 output->to_be_disabled = false;
00484 } else if (output->changed) {
00485 output_change_mode(conn, output);
00486 output->changed = false;
00487 }
00488 }
00489
00490 if (TAILQ_EMPTY(&outputs)) {
00491 ELOG("No outputs found via RandR, disabling\n");
00492 disable_randr(conn);
00493 }
00494
00495 ewmh_update_workarea();
00496
00497
00498 TAILQ_FOREACH(output, &outputs, outputs) {
00499 if (!output->active || output->current_workspace != NULL)
00500 continue;
00501 ws = get_first_workspace_for_output(output);
00502 initialize_output(conn, output, ws);
00503 }
00504
00505
00506 render_layout(conn);
00507 }
00508
00509
00510
00511
00512
00513
00514 void initialize_randr(xcb_connection_t *conn, int *event_base) {
00515 const xcb_query_extension_reply_t *extreply;
00516
00517 extreply = xcb_get_extension_data(conn, &xcb_randr_id);
00518 if (!extreply->present)
00519 disable_randr(conn);
00520 else randr_query_outputs(conn);
00521
00522 if (event_base != NULL)
00523 *event_base = extreply->first_event;
00524
00525 xcb_randr_select_input(conn, root,
00526 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
00527 XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
00528 XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
00529 XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
00530
00531 xcb_flush(conn);
00532 }