00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "all.h"
00016
00017 extern xcb_connection_t *conn;
00018
00019 void floating_enable(Con *con, bool automatic) {
00020 bool set_focus = true;
00021
00022 if (con_is_floating(con)) {
00023 LOG("Container is already in floating mode, not doing anything.\n");
00024 return;
00025 }
00026
00027
00028
00029
00030
00031 if (con->type == CT_WORKSPACE) {
00032 LOG("This is a workspace, creating new container around content\n");
00033 if (con_num_children(con) == 0) {
00034 LOG("Workspace is empty, aborting\n");
00035 return;
00036 }
00037
00038 Con *new = con_new(NULL, NULL);
00039 new->parent = con;
00040 new->orientation = con->orientation;
00041
00042
00043
00044 memcpy(&(new->rect), &(con->rect), sizeof(Rect));
00045
00046 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
00047 if (old_focused == TAILQ_END(&(con->focus_head)))
00048 old_focused = NULL;
00049
00050
00051 DLOG("Moving cons\n");
00052 Con *child;
00053 while (!TAILQ_EMPTY(&(con->nodes_head))) {
00054 child = TAILQ_FIRST(&(con->nodes_head));
00055 con_detach(child);
00056 con_attach(child, new, true);
00057 }
00058
00059
00060 DLOG("Attaching new split to ws\n");
00061 con_attach(new, con, false);
00062
00063 if (old_focused)
00064 con_focus(old_focused);
00065
00066 con = new;
00067 set_focus = false;
00068 }
00069
00070
00071
00072 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
00073 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
00074
00075 con_fix_percent(con->parent);
00076
00077
00078
00079 Con *nc = con_new(NULL, NULL);
00080
00081
00082
00083 nc->parent = con_get_workspace(con);
00084
00085
00086 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) && con_num_children(con->parent) == 0) {
00087 DLOG("Old container empty after setting this child to floating, closing\n");
00088 tree_close(con->parent, DONT_KILL_WINDOW, false);
00089 }
00090
00091 char *name;
00092 asprintf(&name, "[i3 con] floatingcon around %p", con);
00093 x_set_name(nc, name);
00094 free(name);
00095
00096
00097 int deco_height = config.font.height + 5;
00098
00099 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
00100 Rect zero = { 0, 0, 0, 0 };
00101 nc->rect = con->geometry;
00102
00103
00104 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
00105 DLOG("Geometry not set, combining children\n");
00106 Con *child;
00107 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
00108 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
00109 nc->rect.width += child->geometry.width;
00110 nc->rect.height = max(nc->rect.height, child->geometry.height);
00111 }
00112 }
00113
00114 nc->rect.width = max(nc->rect.width, 75);
00115 nc->rect.height = max(nc->rect.height, 50);
00116
00117
00118
00119 nc->rect.height += deco_height + 4;
00120 nc->rect.width += 4;
00121 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
00122 nc->orientation = NO_ORIENTATION;
00123 nc->type = CT_FLOATING_CON;
00124 TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows);
00125 TAILQ_INSERT_TAIL(&(nc->parent->focus_head), nc, focused);
00126
00127
00128 con->parent = nc;
00129 con->percent = 1.0;
00130 con->floating = FLOATING_USER_ON;
00131
00132
00133
00134
00135 if (nc->rect.x == 0 && nc->rect.y == 0) {
00136 Con *leader;
00137 if (con->window && con->window->leader != XCB_NONE &&
00138 (leader = con_by_window_id(con->window->leader)) != NULL) {
00139 DLOG("Centering above leader\n");
00140 nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
00141 nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
00142 } else {
00143
00144 Con *ws = nc->parent;
00145 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
00146 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
00147 }
00148 }
00149
00150 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
00151 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
00152
00153
00154 render_con(nc, false);
00155 render_con(con, false);
00156
00157
00158 if (set_focus)
00159 con_focus(con);
00160
00161
00162
00163 if (floating_maybe_reassign_ws(nc))
00164 return;
00165
00166
00167 if (get_output_containing(nc->rect.x, nc->rect.y) != NULL)
00168 return;
00169
00170 ELOG("No output found at destination coordinates, centering floating window on current ws\n");
00171 Con *ws = nc->parent;
00172 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
00173 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
00174 }
00175
00176 void floating_disable(Con *con, bool automatic) {
00177 if (!con_is_floating(con)) {
00178 LOG("Container isn't floating, not doing anything.\n");
00179 return;
00180 }
00181
00182 Con *ws = con_get_workspace(con);
00183
00184
00185 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
00186 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
00187
00188
00189 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
00190 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
00191 tree_close(con->parent, DONT_KILL_WINDOW, false);
00192
00193
00194
00195 Con *focused = con_descend_tiling_focused(ws);
00196
00197
00198
00199 if (focused->type == CT_WORKSPACE)
00200 con->parent = focused;
00201 else con->parent = focused->parent;
00202
00203
00204 con->percent = 0.0;
00205
00206 TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
00207 TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
00208
00209 con->floating = FLOATING_USER_OFF;
00210
00211 con_fix_percent(con->parent);
00212
00213 con_focus(con);
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223 void toggle_floating_mode(Con *con, bool automatic) {
00224
00225 if (con_is_floating(con)) {
00226 LOG("already floating, re-setting to tiling\n");
00227
00228 floating_disable(con, automatic);
00229 return;
00230 }
00231
00232 floating_enable(con, automatic);
00233 }
00234
00235
00236
00237
00238
00239 void floating_raise_con(Con *con) {
00240 DLOG("Raising floating con %p / %s\n", con, con->name);
00241 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
00242 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
00243 }
00244
00245
00246
00247
00248
00249
00250 bool floating_maybe_reassign_ws(Con *con) {
00251 Output *output = get_output_containing(
00252 con->rect.x + (con->rect.width / 2),
00253 con->rect.y + (con->rect.height / 2));
00254
00255 if (!output) {
00256 ELOG("No output found at destination coordinates?\n");
00257 return false;
00258 }
00259
00260 if (con_get_output(con) == output->con) {
00261 DLOG("still the same ws\n");
00262 return false;
00263 }
00264
00265 DLOG("Need to re-assign!\n");
00266
00267 Con *content = output_get_content(output->con);
00268 Con *ws = TAILQ_FIRST(&(content->focus_head));
00269 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
00270 con_move_to_workspace(con, ws);
00271 con_focus(con_descend_focused(con));
00272 return true;
00273 }
00274
00275 DRAGGING_CB(drag_window_callback) {
00276 struct xcb_button_press_event_t *event = extra;
00277
00278
00279 con->rect.x = old_rect->x + (new_x - event->root_x);
00280 con->rect.y = old_rect->y + (new_y - event->root_y);
00281
00282 render_con(con, false);
00283 x_push_node(con);
00284 xcb_flush(conn);
00285
00286
00287 if (!floating_maybe_reassign_ws(con))
00288 return;
00289 tree_render();
00290 }
00291
00292
00293
00294
00295
00296
00297 void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
00298 DLOG("floating_drag_window\n");
00299
00300 drag_pointer(con, event, XCB_NONE, BORDER_TOP , drag_window_callback, event);
00301 tree_render();
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311 struct resize_window_callback_params {
00312 border_t corner;
00313 bool proportional;
00314 xcb_button_press_event_t *event;
00315 };
00316
00317 DRAGGING_CB(resize_window_callback) {
00318 struct resize_window_callback_params *params = extra;
00319 xcb_button_press_event_t *event = params->event;
00320 border_t corner = params->corner;
00321
00322 int32_t dest_x = con->rect.x;
00323 int32_t dest_y = con->rect.y;
00324 uint32_t dest_width;
00325 uint32_t dest_height;
00326
00327 double ratio = (double) old_rect->width / old_rect->height;
00328
00329
00330
00331 if (corner & BORDER_LEFT)
00332 dest_width = old_rect->width - (new_x - event->root_x);
00333 else dest_width = old_rect->width + (new_x - event->root_x);
00334
00335 if (corner & BORDER_TOP)
00336 dest_height = old_rect->height - (new_y - event->root_y);
00337 else dest_height = old_rect->height + (new_y - event->root_y);
00338
00339
00340 Rect minimum = con_minimum_size(con);
00341 dest_width = max(dest_width, minimum.width);
00342 dest_height = max(dest_height, minimum.height);
00343
00344
00345 if (params->proportional) {
00346 dest_width = max(dest_width, (int) (dest_height * ratio));
00347 dest_height = max(dest_height, (int) (dest_width / ratio));
00348 }
00349
00350
00351
00352 if (corner & BORDER_LEFT)
00353 dest_x = old_rect->x + (old_rect->width - dest_width);
00354
00355 if (corner & BORDER_TOP)
00356 dest_y = old_rect->y + (old_rect->height - dest_height);
00357
00358 con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
00359
00360
00361
00362 tree_render();
00363 x_push_changes(croot);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372 void floating_resize_window(Con *con, bool proportional,
00373 xcb_button_press_event_t *event) {
00374 DLOG("floating_resize_window\n");
00375
00376
00377
00378 border_t corner = 0;
00379
00380 if (event->event_x <= (con->rect.width / 2))
00381 corner |= BORDER_LEFT;
00382 else corner |= BORDER_RIGHT;
00383
00384 if (event->event_y <= (con->rect.height / 2))
00385 corner |= BORDER_TOP;
00386 else corner |= BORDER_BOTTOM;
00387
00388 struct resize_window_callback_params params = { corner, proportional, event };
00389
00390 drag_pointer(con, event, XCB_NONE, BORDER_TOP , resize_window_callback, ¶ms);
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t
00402 confine_to, border_t border, callback_t callback, void *extra)
00403 {
00404 uint32_t new_x, new_y;
00405 Rect old_rect;
00406 if (con != NULL)
00407 memcpy(&old_rect, &(con->rect), sizeof(Rect));
00408
00409
00410 xcb_grab_pointer_cookie_t cookie;
00411 xcb_grab_pointer_reply_t *reply;
00412 cookie = xcb_grab_pointer(conn,
00413 false,
00414 root,
00415 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
00416 XCB_GRAB_MODE_ASYNC,
00417 XCB_GRAB_MODE_ASYNC,
00418 confine_to,
00419 XCB_NONE,
00420 XCB_CURRENT_TIME);
00421
00422 if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) {
00423 ELOG("Could not grab pointer\n");
00424 return;
00425 }
00426
00427 free(reply);
00428
00429
00430 xcb_flush(conn);
00431
00432 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
00433 bool loop_done = false;
00434
00435 while (!loop_done && (inside_event = xcb_wait_for_event(conn))) {
00436
00437 do {
00438
00439 if (inside_event->response_type == 0) {
00440 free(inside_event);
00441 continue;
00442 }
00443
00444 int type = (inside_event->response_type & 0x7F);
00445
00446 switch (type) {
00447 case XCB_BUTTON_RELEASE:
00448 loop_done = true;
00449 break;
00450
00451 case XCB_MOTION_NOTIFY:
00452
00453 FREE(last_motion_notify);
00454 last_motion_notify = inside_event;
00455 break;
00456
00457 case XCB_UNMAP_NOTIFY:
00458 case XCB_KEY_PRESS:
00459 case XCB_KEY_RELEASE:
00460 DLOG("Unmap-notify, aborting\n");
00461 handle_event(type, inside_event);
00462 loop_done = true;
00463 break;
00464
00465 default:
00466 DLOG("Passing to original handler\n");
00467
00468 handle_event(type, inside_event);
00469 break;
00470 }
00471 if (last_motion_notify != inside_event)
00472 free(inside_event);
00473 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
00474
00475 if (last_motion_notify == NULL)
00476 continue;
00477
00478 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
00479 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
00480
00481 callback(con, &old_rect, new_x, new_y, extra);
00482 FREE(last_motion_notify);
00483 }
00484
00485 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
00486 xcb_flush(conn);
00487 }
00488
00489 #if 0
00490
00491
00492
00493
00494
00495
00496
00497 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
00498 DLOG("floating focus\n");
00499
00500 if (direction == D_LEFT || direction == D_RIGHT) {
00501
00502 Client *client;
00503
00504 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
00505 TAILQ_NEXT(currently_focused, floating_clients))) !=
00506 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
00507 if (!client->floating)
00508 continue;
00509 set_focus(conn, client, true);
00510 return;
00511 }
00512 }
00513 }
00514
00515
00516
00517
00518
00519 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
00520 DLOG("floating move\n");
00521
00522 Rect destination = currently_focused->rect;
00523 Rect *screen = &(currently_focused->workspace->output->rect);
00524
00525 switch (direction) {
00526 case D_LEFT:
00527 destination.x -= 10;
00528 break;
00529 case D_RIGHT:
00530 destination.x += 10;
00531 break;
00532 case D_UP:
00533 destination.y -= 10;
00534 break;
00535 case D_DOWN:
00536 destination.y += 10;
00537 break;
00538
00539 default:
00540 break;
00541 }
00542
00543
00544 if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
00545 (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
00546 (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
00547 (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
00548 DLOG("boundary check failed, not moving\n");
00549 return;
00550 }
00551
00552 currently_focused->rect = destination;
00553 reposition_client(conn, currently_focused);
00554
00555
00556
00557 fake_absolute_configure_notify(conn, currently_focused);
00558
00559 }
00560
00561
00562
00563
00564
00565
00566 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
00567 Client *client;
00568
00569 workspace->floating_hidden = !workspace->floating_hidden;
00570 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
00571 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
00572 if (workspace->floating_hidden)
00573 client_unmap(conn, client);
00574 else client_map(conn, client);
00575 }
00576
00577
00578
00579 if (workspace->floating_hidden)
00580 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
00581 if (client_is_floating(client))
00582 continue;
00583 set_focus(conn, client, true);
00584 return;
00585 }
00586
00587 xcb_flush(conn);
00588 }
00589 #endif