]> git.sur5r.net Git - i3/i3/commitdiff
Implement handling the size hints so that aspect ratio is used correctly, fix renderi...
authorMichael Stapelberg <michael+x200@stapelberg.de>
Fri, 6 Mar 2009 05:06:19 +0000 (06:06 +0100)
committerMichael Stapelberg <michael+x200@stapelberg.de>
Fri, 6 Mar 2009 05:06:19 +0000 (06:06 +0100)
include/data.h
include/handlers.h
src/handlers.c
src/layout.c
src/mainx.c

index 16db7d51fac6aceb41b75d84a8fd5fb47f42f004..e2d59f889e38f00aa4577c85de896939ceaa7125 100644 (file)
@@ -204,6 +204,11 @@ struct Client {
         /* x, y, width, height of the child (relative to its frame) */
         Rect child_rect;
 
+        /* contains the size calculated from the hints set by the window or 0 if the client
+           did not send any hints */
+        int proportional_height;
+        int proportional_width;
+
         /* Height which was determined by reading the _NET_WM_STRUT_PARTIAL top/bottom of the screen
            reservation */
         int desired_height;
index 82b149e6b4cb65cf0c49b020e19f2aa1e1db29fc..22e61693acf08e6c825a89c067cb0b5c62a7c921 100644 (file)
@@ -22,5 +22,7 @@ int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event);
 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event);
 int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *property);
+int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
+                        xcb_atom_t name, xcb_get_property_reply_t *reply);
 
 #endif
index f7ddc5870cf8884ca44ae9657a60025cffa95024..2fc96f83bff9c670a8f1ce2a1dc68f2ecc0952d8 100644 (file)
@@ -15,6 +15,7 @@
 #include <xcb/xcb.h>
 
 #include <xcb/xcb_wm.h>
+#include <xcb/xcb_icccm.h>
 #include <X11/XKBlib.h>
 
 #include "i3.h"
@@ -353,6 +354,7 @@ int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_n
         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
 
         printf("handle_configure_event\n");
+        printf("event->type = %d, \n", event->response_type);
         printf("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
         printf("sequence = %d\n", event->sequence);
 
@@ -531,11 +533,28 @@ int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *
         if (client->container->mode != MODE_STACK)
                 decorate_window(conn, client, client->frame, client->titlegc, 0);
         else {
-                xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND,
-                        get_colorpixel(conn, "#285577"));
-
-                xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
-                xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
+                uint32_t background_color;
+                /* Distinguish if the window is currently focused… */
+                if (CUR_CELL->currently_focused == client)
+                        background_color = get_colorpixel(conn, "#285577");
+                /* …or if it is the focused window in a not focused container */
+                else background_color = get_colorpixel(conn, "#555555");
+
+                /* Set foreground color to current focused color, line width to 2 */
+                uint32_t values[] = {background_color, 2};
+                xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
+
+                /* Draw the border, the ±1 is for line width = 2 */
+                xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
+                                        {1, client->rect.height-1},                       /* left bottom edge */
+                                        {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
+                                        {client->rect.width-1, 0}};                       /* right upper edge */
+                xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
+
+                /* Draw a black background */
+                xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
+                xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
+                xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
 
                 xcb_flush(conn);
         }
@@ -582,3 +601,86 @@ int window_type_handler(void *data, xcb_connection_t *conn, uint8_t state, xcb_w
         printf("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
         return 0;
 }
+
+/*
+ * Handles the size hints set by a window, but currently only the part necessary for displaying
+ * clients proportionally inside their frames (mplayer for example)
+ *
+ * See ICCCM 4.1.2.3 for more details
+ *
+ */
+int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
+                        xcb_atom_t name, xcb_get_property_reply_t *reply) {
+        printf("handle_normal_hints\n");
+        Client *client = table_get(byChild, window);
+        if (client == NULL) {
+                printf("No such client\n");
+                return;
+        }
+        xcb_size_hints_t size_hints;
+
+        /* If the hints were already in this event, use them, if not, request them */
+        if (reply != NULL)
+                xcb_get_wm_size_hints_from_reply(&size_hints, reply);
+        else
+                xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
+
+        /* If no aspect ratio was set or if it was invalid, we ignore the hints */
+        if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
+            (size_hints.min_aspect_num <= 0) ||
+            (size_hints.min_aspect_den <= 0)) {
+                printf("No aspect ratio set, ignoring\n");
+                return;
+        }
+
+        printf("window is %08x / %s\n", client->child, client->name);
+
+        int base_width = 0, base_height = 0,
+            min_width = 0, min_height = 0;
+
+        /* base_width/height are the desired size of the window.
+           We check if either the program-specified size or the program-specified
+           min-size is available */
+        if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
+                base_width = size_hints.base_width;
+                base_height = size_hints.base_height;
+        } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
+                base_width = size_hints.min_width;
+                base_height = size_hints.min_height;
+        }
+
+        if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
+                min_width = size_hints.min_width;
+                min_height = size_hints.min_height;
+        } else if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
+                min_width = size_hints.base_width;
+                min_height = size_hints.base_height;
+        }
+
+        double width = client->rect.width - base_width;
+        double height = client->rect.height - base_height;
+        /* Convert numerator/denominator to a double */
+        double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
+        double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
+
+        printf("min_aspect = %f, max_aspect = %f\n", min_aspect, max_aspect);
+        printf("width = %f, height = %f\n", width, height);
+
+        /* Sanity checks, this is user-input, in a way */
+        if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
+                return;
+
+        /* Check if we need to set proportional_* variables using the correct ratio */
+        if ((width / height) < min_aspect) {
+                client->proportional_width = width;
+                client->proportional_height = width / min_aspect;
+        } else if ((width / height) > max_aspect) {
+                client->proportional_width = width;
+                client->proportional_height = width / max_aspect;
+        } else return;
+
+        client->force_reconfigure = true;
+
+        if (client->container != NULL)
+                render_container(conn, client->container);
+}
index 0c9279a1f180c24c530099743feceacccd630ec2..c01e45550dadb0358f72ba79e38effcb85e98ca4 100644 (file)
@@ -89,9 +89,12 @@ int get_unoccupied_y(Workspace *workspace, int col) {
  *
  */
 void redecorate_window(xcb_connection_t *conn, Client *client) {
-        if (client->container->mode == MODE_STACK)
+        if (client->container->mode == MODE_STACK) {
                 render_container(conn, client->container);
-        else decorate_window(conn, client, client->frame, client->titlegc, 0);
+                /* We clear the frame to generate exposure events, because the color used
+                   in drawing may be different */
+                xcb_clear_area(conn, true, client->frame, 0, 0, client->rect.width, client->rect.height);
+        } else decorate_window(conn, client, client->frame, client->titlegc, 0);
         xcb_flush(conn);
 }
 
@@ -102,6 +105,7 @@ void redecorate_window(xcb_connection_t *conn, Client *client) {
  */
 void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t drawable, xcb_gcontext_t gc, int offset) {
         i3Font *font = load_font(conn, config.font);
+        int decoration_height = font->height + 2 + 2;
         uint32_t background_color,
                  text_color,
                  border_color;
@@ -134,11 +138,24 @@ void decorate_window(xcb_connection_t *conn, Client *client, xcb_drawable_t draw
         /* Draw a rectangle in background color around the window */
         xcb_change_gc_single(conn, gc, XCB_GC_FOREGROUND, background_color);
 
-        /* We need to use the container’s width because it is the more recent value - when
-           in stacking mode, clients get reconfigured only on demand (the not active client
-           is not reconfigured), so the client’s rect.width would be wrong */
-        xcb_rectangle_t rect = {0, offset, client->container->width, offset + client->rect.height};
-        xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
+        /* In stacking mode, we only render the rect for this specific decoration */
+        if (client->container->mode == MODE_STACK) {
+                xcb_rectangle_t rect = {0, offset, client->container->width, offset + decoration_height };
+                xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
+        } else {
+                /* We need to use the container’s width because it is the more recent value - when
+                   in stacking mode, clients get reconfigured only on demand (the not active client
+                   is not reconfigured), so the client’s rect.width would be wrong */
+                xcb_rectangle_t rect = {0, 0, client->container->width, client->rect.height};
+                xcb_poly_fill_rectangle(conn, drawable, gc, 1, &rect);
+
+                /* Draw the inner background to have a black frame around clients (such as mplayer)
+                   which cannot be resized exactly in our frames and therefore are centered */
+                xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
+                xcb_rectangle_t crect = {2, decoration_height,
+                                         client->rect.width - (2 + 2), client->rect.height - 2 - decoration_height};
+                xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
+        }
 
         /* Draw the lines */
         xcb_draw_line(conn, drawable, gc, border_color, 2, offset, client->rect.width, offset);
@@ -210,6 +227,28 @@ static void resize_client(xcb_connection_t *conn, Client *client) {
                         break;
         }
 
+        /* Obey the ratio, if any */
+        if (client->proportional_height != 0 &&
+            client->proportional_width != 0) {
+                printf("proportional height = %d, width = %d\n", client->proportional_height, client->proportional_width);
+                double new_height = rect->height + 1;
+                int new_width = rect->width;
+
+                while (new_height > rect->height) {
+                        new_height = ((double)client->proportional_height / client->proportional_width) * new_width;
+
+                        if (new_height > rect->height)
+                                new_width--;
+                }
+                /* Center the window */
+                rect->y += (rect->height / 2) - (new_height / 2);
+                rect->x += (rect->width / 2) - (new_width / 2);
+
+                rect->height = new_height;
+                rect->width = new_width;
+                printf("new_height = %f, new_width = %d\n", new_height, new_width);
+        }
+
         printf("child will be at %dx%d with size %dx%d\n", rect->x, rect->y, rect->width, rect->height);
 
         xcb_configure_window(conn, client->child, mask, &(rect->x));
index a29cd54a2354895bbb3390a0966fac8599216130..143f0ab4f2b212ed363dca307167cf5d805e7a2e 100644 (file)
@@ -398,6 +398,9 @@ int main(int argc, char *argv[], char *env[]) {
         /* Watch the WM_NAME (= title of the window) property */
         xcb_watch_wm_name(&prophs, 128, handle_windowname_change, 0);
 
+        /* Watch size hints (to obey correct aspect ratio) */
+        xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
+
         /* Get the root window and set the event mask */
         root = xcb_aux_get_screen(conn, screens)->root;