]> git.sur5r.net Git - i3/i3/blobdiff - src/floating.c
Implement setting the WM_NAME of i3 container windows for debugging
[i3/i3] / src / floating.c
index 2afe1b8218876bd9be0b12278805fe8c2f8246b9..756102b7afe67fef59660087ad4a7eb6c7462d00 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * vim:ts=8:expandtab
+ * vim:ts=4:sw=4:expandtab
  *
  * i3 - an improved dynamic tiling window manager
  *
 
 extern xcb_connection_t *conn;
 
+void floating_enable(Con *con, bool automatic) {
+    if (con_is_floating(con)) {
+        LOG("Container is already in floating mode, not doing anything.\n");
+        return;
+    }
+
+    /* 1: detach the container from its parent */
+    /* TODO: refactor this with tree_close() */
+    TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
+    TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
+
+    con_fix_percent(con->parent, WINDOW_REMOVE);
+
+    /* 2: create a new container to render the decoration on, add
+     * it as a floating window to the workspace */
+    Con *nc = con_new(NULL);
+    /* we need to set the parent afterwards instead of passing it as an
+     * argument to con_new() because nc would be inserted into the tiling layer
+     * otherwise. */
+    nc->parent = con_get_workspace(con);
+
+    char *name;
+    asprintf(&name, "[i3 con] floatingcon around %p", con);
+    x_set_name(nc, name);
+    free(name);
+
+    nc->rect = con->rect;
+    /* add pixels for the decoration */
+    /* TODO: don’t add them when the user automatically puts new windows into
+     * 1pixel/borderless mode */
+    nc->rect.height += 17 + 2;
+    nc->rect.width += 4;
+    nc->orientation = NO_ORIENTATION;
+    nc->type = CT_FLOATING_CON;
+    TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows);
+    TAILQ_INSERT_TAIL(&(nc->parent->focus_head), nc, focused);
+
+    /* 3: attach the child to the new parent container */
+    con->old_parent = con->parent;
+    con->parent = nc;
+    con->floating = FLOATING_USER_ON;
+
+    /* Some clients (like GIMP’s color picker window) get mapped
+     * to (0, 0), so we push them to a reasonable position
+     * (centered over their leader) */
+    if (nc->rect.x == 0 && nc->rect.y == 0) {
+        Con *leader;
+        if (con->window && con->window->leader != XCB_NONE &&
+            (leader = con_by_window_id(con->window->leader)) != NULL) {
+            DLOG("Centering above leader\n");
+            nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
+            nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
+        } else {
+            /* center the window on workspace as fallback */
+            Con *ws = nc->parent;
+            nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
+            nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
+        }
+    }
+
+    TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
+    TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
+}
+
+void floating_disable(Con *con, bool automatic) {
+    if (!con_is_floating(con)) {
+        LOG("Container isn't floating, not doing anything.\n");
+        return;
+    }
+
+    assert(con->old_parent != NULL);
+
+    /* 1: detach from parent container */
+    TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
+    TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
+
+    /* 2: kill parent container */
+    TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
+    TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
+    tree_close(con->parent, false);
+
+    /* 3: re-attach to previous parent */
+    con->parent = con->old_parent;
+    TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
+    TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
+
+    con->floating = FLOATING_USER_OFF;
+
+    con_fix_percent(con->parent, WINDOW_ADD);
+}
+
+
 /*
  * Toggles floating mode for the given container.
  *
@@ -29,59 +121,12 @@ void toggle_floating_mode(Con *con, bool automatic) {
         /* see if the client is already floating */
         if (con_is_floating(con)) {
                 LOG("already floating, re-setting to tiling\n");
-                assert(con->old_parent != NULL);
-
-                /* 1: detach from parent container */
-                TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
-                TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
-
-                /* 2: kill parent container */
-                TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
-                tree_close(con->parent, false);
-
-                /* 3: re-attach to previous parent */
-                con->parent = con->old_parent;
-                TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
-                TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
 
+                floating_disable(con, automatic);
                 return;
         }
 
-        /* 1: detach the container from its parent */
-        /* TODO: refactor this with tree_close() */
-        TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
-        TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
-
-        Con *child;
-    int children = 0;
-    TAILQ_FOREACH(child, &(con->parent->nodes_head), nodes)
-        children++;
-    /* TODO: better document why this math works */
-    double fix = 1.0 / (1.0 - (1.0 / (children+1)));
-    TAILQ_FOREACH(child, &(con->parent->nodes_head), nodes) {
-        if (child->percent <= 0.0)
-            continue;
-        child->percent *= fix;
-    }
-
-        /* 2: create a new container to render the decoration on, add
-         * it as a floating window to the workspace */
-        Con *nc = con_new(NULL);
-        nc->parent = con_get_workspace(con);
-        nc->rect = con->rect;
-        nc->orientation = NO_ORIENTATION;
-        nc->type = CT_FLOATING_CON;
-        TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows);
-
-        /* 3: attach the child to the new parent container */
-        con->old_parent = con->parent;
-        con->parent = nc;
-        con->floating = FLOATING_USER_ON;
-        nc->rect.x = 400;
-        nc->rect.y = 400;
-        TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
-        TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
-
+        floating_enable(con, automatic);
 
 
 #if 0
@@ -310,11 +355,10 @@ DRAGGING_CB(drag_window_callback) {
         /* Reposition the client correctly while moving */
         con->rect.x = old_rect->x + (new_x - event->root_x);
         con->rect.y = old_rect->y + (new_y - event->root_y);
-        //reposition_client(conn, con);
-        /* Because reposition_client does not send a faked configure event (only resize does),
-         * we need to initiate that on our own */
-        //fake_absolute_configure_notify(conn, client);
-        /* fake_absolute_configure_notify flushes */
+        /* TODO: don’t re-render the whole tree just because we change
+         * coordinates of a floating window */
+        tree_render();
+        x_push_changes(croot);
 }
 
 /*
@@ -329,7 +373,6 @@ void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
         tree_render();
 }
 
-#if 0
 /*
  * This is an ugly data structure which we need because there is no standard
  * way of having nested functions (only available as a gcc extension at the
@@ -338,55 +381,60 @@ void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
  *
  */
 struct resize_window_callback_params {
-        border_t corner;
-        bool proportional;
-        xcb_button_press_event_t *event;
+    border_t corner;
+    bool proportional;
+    xcb_button_press_event_t *event;
 };
 
 DRAGGING_CB(resize_window_callback) {
-        struct resize_window_callback_params *params = extra;
-        xcb_button_press_event_t *event = params->event;
-        border_t corner = params->corner;
+    struct resize_window_callback_params *params = extra;
+    xcb_button_press_event_t *event = params->event;
+    border_t corner = params->corner;
 
-        int32_t dest_x = client->rect.x;
-        int32_t dest_y = client->rect.y;
-        uint32_t dest_width;
-        uint32_t dest_height;
+    int32_t dest_x = con->rect.x;
+    int32_t dest_y = con->rect.y;
+    uint32_t dest_width;
+    uint32_t dest_height;
 
-        double ratio = (double) old_rect->width / old_rect->height;
+    double ratio = (double) old_rect->width / old_rect->height;
 
-        /* First guess: We resize by exactly the amount the mouse moved,
-         * taking into account in which corner the client was grabbed */
-        if (corner & BORDER_LEFT)
-                dest_width = old_rect->width - (new_x - event->root_x);
-        else dest_width = old_rect->width + (new_x - event->root_x);
+    /* First guess: We resize by exactly the amount the mouse moved,
+     * taking into account in which corner the client was grabbed */
+    if (corner & BORDER_LEFT)
+            dest_width = old_rect->width - (new_x - event->root_x);
+    else dest_width = old_rect->width + (new_x - event->root_x);
 
-        if (corner & BORDER_TOP)
-                dest_height = old_rect->height - (new_y - event->root_y);
-        else dest_height = old_rect->height + (new_y - event->root_y);
+    if (corner & BORDER_TOP)
+            dest_height = old_rect->height - (new_y - event->root_y);
+    else dest_height = old_rect->height + (new_y - event->root_y);
 
-        /* Obey minimum window size */
-        dest_width = max(dest_width, client_min_width(client));
-        dest_height = max(dest_height, client_min_height(client));
+    /* TODO: minimum window size */
+#if 0
+    /* Obey minimum window size */
+    dest_width = max(dest_width, client_min_width(client));
+    dest_height = max(dest_height, client_min_height(client));
+#endif
 
-        /* User wants to keep proportions, so we may have to adjust our values */
-        if (params->proportional) {
-                dest_width = max(dest_width, (int) (dest_height * ratio));
-                dest_height = max(dest_height, (int) (dest_width / ratio));
-        }
+    /* User wants to keep proportions, so we may have to adjust our values */
+    if (params->proportional) {
+            dest_width = max(dest_width, (int) (dest_height * ratio));
+            dest_height = max(dest_height, (int) (dest_width / ratio));
+    }
 
-        /* If not the lower right corner is grabbed, we must also reposition
-         * the client by exactly the amount we resized it */
-        if (corner & BORDER_LEFT)
-                dest_x = old_rect->x + (old_rect->width - dest_width);
+    /* If not the lower right corner is grabbed, we must also reposition
+     * the client by exactly the amount we resized it */
+    if (corner & BORDER_LEFT)
+            dest_x = old_rect->x + (old_rect->width - dest_width);
 
-        if (corner & BORDER_TOP)
-                dest_y = old_rect->y + (old_rect->height - dest_height);
+    if (corner & BORDER_TOP)
+            dest_y = old_rect->y + (old_rect->height - dest_height);
 
-        client->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
+    con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
 
-        /* resize_client flushes */
-        resize_client(conn, client);
+    /* TODO: don’t re-render the whole tree just because we change
+     * coordinates of a floating window */
+    tree_render();
+    x_push_changes(croot);
 }
 
 /*
@@ -395,28 +443,27 @@ DRAGGING_CB(resize_window_callback) {
  * Calls the drag_pointer function with the resize_window callback
  *
  */
-void floating_resize_window(xcb_connection_t *conn, Client *client,
-                            bool proportional, xcb_button_press_event_t *event) {
-        DLOG("floating_resize_window\n");
+void floating_resize_window(Con *con, bool proportional,
+                            xcb_button_press_event_t *event) {
+    DLOG("floating_resize_window\n");
 
-        /* corner saves the nearest corner to the original click. It contains
-         * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
-        border_t corner = 0;
+    /* corner saves the nearest corner to the original click. It contains
+     * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
+    border_t corner = 0;
 
-        if (event->event_x <= (client->rect.width / 2))
-                corner |= BORDER_LEFT;
-        else corner |= BORDER_RIGHT;
+    if (event->event_x <= (con->rect.width / 2))
+            corner |= BORDER_LEFT;
+    else corner |= BORDER_RIGHT;
 
-        if (event->event_y <= (client->rect.height / 2))
-                corner |= BORDER_TOP;
-        else corner |= BORDER_RIGHT;
+    if (event->event_y <= (con->rect.height / 2))
+            corner |= BORDER_TOP;
+    else corner |= BORDER_RIGHT;
 
-        struct resize_window_callback_params params = { corner, proportional, event };
+    struct resize_window_callback_params params = { corner, proportional, event };
 
-        drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, &params);
+    drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, &params);
 }
 
-#endif
 /*
  * This function grabs your pointer and lets you drag stuff around (borders).
  * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received