]> git.sur5r.net Git - i3/i3/blobdiff - i3bar/src/xcb.c
Merge branch 'master' into next
[i3/i3] / i3bar / src / xcb.c
index 861925b96925f1aea7687dddd4eee8b97342c023..e0abfadd746ca5fed1c5a6a94988f71c035e0cf9 100644 (file)
@@ -9,7 +9,6 @@
  */
 #include <xcb/xcb.h>
 #include <xcb/xproto.h>
-#include <xcb/xcb_atom.h>
 #include <xcb/xcb_aux.h>
 
 #ifdef XCB_COMPAT
@@ -50,6 +49,10 @@ int              screen;
 xcb_screen_t     *root_screen;
 xcb_window_t     xcb_root;
 
+/* selection window for tray support */
+static xcb_window_t selwin = XCB_NONE;
+static xcb_intern_atom_reply_t *tray_reply = NULL;
+
 /* This is needed for integration with libi3 */
 xcb_connection_t *conn;
 
@@ -74,6 +77,9 @@ ev_check   *xcb_chk;
 ev_io      *xcb_io;
 ev_io      *xkb_io;
 
+/* The name of current binding mode */
+static mode binding;
+
 /* The parsed colors */
 struct xcb_colors_t {
     uint32_t bar_fg;
@@ -120,10 +126,31 @@ void refresh_statusline(void) {
             continue;
 
         block->width = predict_text_width(block->full_text);
+
+        /* Compute offset and append for text aligment in min_width. */
+        if (block->min_width <= block->width) {
+            block->x_offset = 0;
+            block->x_append = 0;
+        } else {
+            uint32_t padding_width = block->min_width - block->width;
+            switch (block->align) {
+                case ALIGN_LEFT:
+                    block->x_append = padding_width;
+                    break;
+                case ALIGN_RIGHT:
+                    block->x_offset = padding_width;
+                    break;
+                case ALIGN_CENTER:
+                    block->x_offset = padding_width / 2;
+                    block->x_append = padding_width / 2 + padding_width % 2;
+                    break;
+            }
+        }
+
         /* If this is not the last block, add some pixels for a separator. */
         if (TAILQ_NEXT(block, blocks) != NULL)
             block->width += 9;
-        statusline_width += block->width;
+        statusline_width += block->width + block->x_offset + block->x_append;
     }
 
     /* If the statusline is bigger than our screen we need to make sure that
@@ -144,8 +171,8 @@ void refresh_statusline(void) {
 
         uint32_t colorpixel = (block->color ? get_colorpixel(block->color) : colors.bar_fg);
         set_font_colors(statusline_ctx, colorpixel, colors.bar_bg);
-        draw_text(block->full_text, statusline_pm, statusline_ctx, x, 0, block->width);
-        x += block->width;
+        draw_text(block->full_text, statusline_pm, statusline_ctx, x + block->x_offset, 0, block->width);
+        x += block->width + block->x_offset + block->x_append;
 
         if (TAILQ_NEXT(block, blocks) != NULL) {
             /* This is not the last block, draw a separator. */
@@ -245,6 +272,9 @@ void init_colors(const struct xcb_color_strings_t *new_colors) {
     PARSE_COLOR(focus_ws_bg, "#285577");
     PARSE_COLOR(focus_ws_border, "#4c7899");
 #undef PARSE_COLOR
+
+    init_tray_colors();
+    xcb_flush(xcb_connection);
 }
 
 /*
@@ -302,16 +332,24 @@ void handle_button(xcb_button_press_event_t *event) {
             }
             break;
         case 4:
-            /* Mouse wheel down. We select the next ws */
-            if (cur_ws != TAILQ_FIRST(walk->workspaces)) {
-                cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
-            }
+            /* Mouse wheel up. We select the previous ws, if any.
+             * If there is no more workspace, don’t even send the workspace
+             * command, otherwise (with workspace auto_back_and_forth) we’d end
+             * up on the wrong workspace. */
+            if (cur_ws == TAILQ_FIRST(walk->workspaces))
+                return;
+
+            cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
             break;
         case 5:
-            /* Mouse wheel up. We select the previos ws */
-            if (cur_ws != TAILQ_LAST(walk->workspaces, ws_head)) {
-                cur_ws = TAILQ_NEXT(cur_ws, tailq);
-            }
+            /* Mouse wheel down. We select the next ws, if any.
+             * If there is no more workspace, don’t even send the workspace
+             * command, otherwise (with workspace auto_back_and_forth) we’d end
+             * up on the wrong workspace. */
+            if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
+                return;
+
+            cur_ws = TAILQ_NEXT(cur_ws, tailq);
             break;
     }
 
@@ -531,7 +569,7 @@ static void handle_client_message(xcb_client_message_event_t* event) {
             /* Trigger an update to copy the statusline text to the appropriate
              * position */
             configure_trayclients();
-            draw_bars();
+            draw_bars(false);
         }
     }
 }
@@ -559,7 +597,7 @@ static void handle_unmap_notify(xcb_unmap_notify_event_t* event) {
 
             /* Trigger an update, we now have more space for the statusline */
             configure_trayclients();
-            draw_bars();
+            draw_bars(false);
             return;
         }
     }
@@ -624,13 +662,13 @@ static void handle_property_notify(xcb_property_notify_event_t *event) {
             xcb_unmap_window(xcb_connection, trayclient->win);
             trayclient->mapped = map_it;
             configure_trayclients();
-            draw_bars();
+            draw_bars(false);
         } else if (!trayclient->mapped && map_it) {
             /* need to map the window */
             xcb_map_window(xcb_connection, trayclient->win);
             trayclient->mapped = map_it;
             configure_trayclients();
-            draw_bars();
+            draw_bars(false);
         }
         free(xembedr);
     }
@@ -960,6 +998,31 @@ void init_xcb_late(char *fontname) {
     }
 }
 
+/*
+ * Inform clients waiting for a new _NET_SYSTEM_TRAY that we took the
+ * selection.
+ *
+ */
+static void send_tray_clientmessage(void) {
+    uint8_t buffer[32] = { 0 };
+    xcb_client_message_event_t *ev = (xcb_client_message_event_t*)buffer;
+
+    ev->response_type = XCB_CLIENT_MESSAGE;
+    ev->window = xcb_root;
+    ev->type = atoms[MANAGER];
+    ev->format = 32;
+    ev->data.data32[0] = XCB_CURRENT_TIME;
+    ev->data.data32[1] = tray_reply->atom;
+    ev->data.data32[2] = selwin;
+
+    xcb_send_event(xcb_connection,
+                   0,
+                   xcb_root,
+                   0xFFFFFF,
+                   (char*)buffer);
+}
+
+
 /*
  * Initializes tray support by requesting the appropriate _NET_SYSTEM_TRAY atom
  * for the X11 display we are running on, then acquiring the selection for this
@@ -972,11 +1035,11 @@ void init_tray(void) {
     char atomname[strlen("_NET_SYSTEM_TRAY_S") + 11];
     snprintf(atomname, strlen("_NET_SYSTEM_TRAY_S") + 11, "_NET_SYSTEM_TRAY_S%d", screen);
     xcb_intern_atom_cookie_t tray_cookie;
-    xcb_intern_atom_reply_t *tray_reply;
-    tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
+    if (tray_reply == NULL)
+        tray_cookie = xcb_intern_atom(xcb_connection, 0, strlen(atomname), atomname);
 
     /* tray support: we need a window to own the selection */
-    xcb_window_t selwin = xcb_generate_id(xcb_connection);
+    selwin = xcb_generate_id(xcb_connection);
     uint32_t selmask = XCB_CW_OVERRIDE_REDIRECT;
     uint32_t selval[] = { 1 };
     xcb_create_window(xcb_connection,
@@ -1002,9 +1065,13 @@ void init_tray(void) {
                         1,
                         &orientation);
 
-    if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
-        ELOG("Could not get atom %s\n", atomname);
-        exit(EXIT_FAILURE);
+    init_tray_colors();
+
+    if (tray_reply == NULL) {
+        if (!(tray_reply = xcb_intern_atom_reply(xcb_connection, tray_cookie, NULL))) {
+            ELOG("Could not get atom %s\n", atomname);
+            exit(EXIT_FAILURE);
+        }
     }
 
     xcb_set_selection_owner(xcb_connection,
@@ -1031,23 +1098,48 @@ void init_tray(void) {
         return;
     }
 
-    /* Inform clients waiting for a new _NET_SYSTEM_TRAY that we are here */
-    void *event = scalloc(32);
-    xcb_client_message_event_t *ev = event;
-    ev->response_type = XCB_CLIENT_MESSAGE;
-    ev->window = xcb_root;
-    ev->type = atoms[MANAGER];
-    ev->format = 32;
-    ev->data.data32[0] = XCB_CURRENT_TIME;
-    ev->data.data32[1] = tray_reply->atom;
-    ev->data.data32[2] = selwin;
-    xcb_send_event(xcb_connection,
-                   0,
-                   xcb_root,
-                   0xFFFFFF,
-                   (char*)ev);
-    free(event);
-    free(tray_reply);
+    send_tray_clientmessage();
+}
+
+/*
+ * We need to set the _NET_SYSTEM_TRAY_COLORS atom on the tray selection window
+ * to make GTK+ 3 applets with Symbolic Icons visible. If the colors are unset,
+ * they assume a light background.
+ * See also https://bugzilla.gnome.org/show_bug.cgi?id=679591
+ *
+ */
+void init_tray_colors(void) {
+    /* Convert colors.bar_fg (#rrggbb) to 16-bit RGB */
+    const char *bar_fg = (config.colors.bar_fg ? config.colors.bar_fg : "#FFFFFF");
+
+    DLOG("Setting bar_fg = %s as _NET_SYSTEM_TRAY_COLORS\n", bar_fg);
+
+    char strgroups[3][3] = {{bar_fg[1], bar_fg[2], '\0'},
+                            {bar_fg[3], bar_fg[4], '\0'},
+                            {bar_fg[5], bar_fg[6], '\0'}};
+    const uint8_t r = strtol(strgroups[0], NULL, 16);
+    const uint8_t g = strtol(strgroups[1], NULL, 16);
+    const uint8_t b = strtol(strgroups[2], NULL, 16);
+
+    const uint16_t r16 = ((uint16_t)r << 8) | r;
+    const uint16_t g16 = ((uint16_t)g << 8) | g;
+    const uint16_t b16 = ((uint16_t)b << 8) | b;
+
+    const uint32_t tray_colors[12] = {
+        r16, g16, b16, /* foreground color */
+        r16, g16, b16, /* error color */
+        r16, g16, b16, /* warning color */
+        r16, g16, b16, /* success color */
+    };
+
+    xcb_change_property(xcb_connection,
+                        XCB_PROP_MODE_REPLACE,
+                        selwin,
+                        atoms[_NET_SYSTEM_TRAY_COLORS],
+                        XCB_ATOM_CARDINAL,
+                        32,
+                        12,
+                        tray_colors);
 }
 
 /*
@@ -1107,6 +1199,9 @@ void get_atoms(void) {
  *
  */
 void kick_tray_clients(i3_output *output) {
+    if (TAILQ_EMPTY(output->trayclients))
+        return;
+
     trayclient *trayclient;
     while (!TAILQ_EMPTY(output->trayclients)) {
         trayclient = TAILQ_FIRST(output->trayclients);
@@ -1122,6 +1217,20 @@ void kick_tray_clients(i3_output *output) {
          * event afterwards, but better safe than sorry. */
         TAILQ_REMOVE(output->trayclients, trayclient, tailq);
     }
+
+    /* Fake a DestroyNotify so that Qt re-adds tray icons.
+     * We cannot actually destroy the window because then Qt will not restore
+     * its event mask on the new window. */
+    uint8_t buffer[32] = { 0 };
+    xcb_destroy_notify_event_t *event = (xcb_destroy_notify_event_t*)buffer;
+
+    event->response_type = XCB_DESTROY_NOTIFY;
+    event->event = selwin;
+    event->window = selwin;
+
+    xcb_send_event(conn, false, selwin, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (char*)event);
+
+    send_tray_clientmessage();
 }
 
 /*
@@ -1367,7 +1476,7 @@ void reconfig_windows(void) {
             values[3] = font.height + 6;
             values[4] = XCB_STACK_MODE_ABOVE;
 
-            DLOG("Destroying buffer for output %s", walk->name);
+            DLOG("Destroying buffer for output %s\n", walk->name);
             xcb_free_pixmap(xcb_connection, walk->buffer);
 
             DLOG("Reconfiguring Window for output %s to %d,%d\n", walk->name, values[0], values[1]);
@@ -1376,7 +1485,7 @@ void reconfig_windows(void) {
                                                                         mask,
                                                                         values);
 
-            DLOG("Recreating buffer for output %s", walk->name);
+            DLOG("Recreating buffer for output %s\n", walk->name);
             xcb_void_cookie_t pm_cookie = xcb_create_pixmap_checked(xcb_connection,
                                                                     root_screen->root_depth,
                                                                     walk->buffer,
@@ -1398,12 +1507,15 @@ void reconfig_windows(void) {
  * Render the bars, with buttons and statusline
  *
  */
-void draw_bars(void) {
+void draw_bars(bool unhide) {
     DLOG("Drawing Bars...\n");
     int i = 0;
 
     refresh_statusline();
 
+    static char *last_urgent_ws = NULL;
+    bool walks_away = true;
+
     i3_output *outputs_walk;
     SLIST_FOREACH(outputs_walk, outputs, slist) {
         if (!outputs_walk->active) {
@@ -1460,8 +1572,6 @@ void draw_bars(void) {
         }
 
         i3_ws *ws_walk;
-        static char *last_urgent_ws = NULL;
-        bool has_urgent = false, walks_away = true;
 
         TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
             DLOG("Drawing Button for WS %s at x = %d, len = %d\n", i3string_as_utf8(ws_walk->name), i, ws_walk->name_width);
@@ -1486,13 +1596,11 @@ void draw_bars(void) {
                 fg_color = colors.urgent_ws_fg;
                 bg_color = colors.urgent_ws_bg;
                 border_color = colors.urgent_ws_border;
-                has_urgent = true;
+                unhide = true;
                 if (!ws_walk->focused) {
                     FREE(last_urgent_ws);
                     last_urgent_ws = sstrdup(i3string_as_utf8(ws_walk->name));
                 }
-                /* The urgent-hint should get noticed, so we unhide the bars shortly */
-                unhide_bars();
             }
             uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
             uint32_t vals_border[] = { border_color, border_color };
@@ -1520,16 +1628,56 @@ void draw_bars(void) {
             set_font_colors(outputs_walk->bargc, fg_color, bg_color);
             draw_text(ws_walk->name, outputs_walk->buffer, outputs_walk->bargc, i + 5, 2, ws_walk->name_width);
             i += 10 + ws_walk->name_width + 1;
+
         }
 
-        if (!has_urgent && !mod_pressed && walks_away) {
-            FREE(last_urgent_ws);
-            hide_bars();
+        if (binding.name) {
+
+            uint32_t fg_color = colors.urgent_ws_fg;
+            uint32_t bg_color = colors.urgent_ws_bg;
+            uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
+
+            uint32_t vals_border[] = { colors.urgent_ws_border, colors.urgent_ws_border };
+            xcb_change_gc(xcb_connection,
+                          outputs_walk->bargc,
+                          mask,
+                          vals_border);
+            xcb_rectangle_t rect_border = { i, 0, binding.width + 10, font.height + 4 };
+            xcb_poly_fill_rectangle(xcb_connection,
+                                    outputs_walk->buffer,
+                                    outputs_walk->bargc,
+                                    1,
+                                    &rect_border);
+
+            uint32_t vals[] = { bg_color, bg_color };
+            xcb_change_gc(xcb_connection,
+                          outputs_walk->bargc,
+                          mask,
+                          vals);
+            xcb_rectangle_t rect = { i + 1, 1, binding.width + 8, font.height + 2 };
+            xcb_poly_fill_rectangle(xcb_connection,
+                                    outputs_walk->buffer,
+                                    outputs_walk->bargc,
+                                    1,
+                                    &rect);
+
+            set_font_colors(outputs_walk->bargc, fg_color, bg_color);
+            draw_text(binding.name, outputs_walk->buffer, outputs_walk->bargc, i + 5, 2, binding.width);
         }
 
         i = 0;
     }
 
+    if (!mod_pressed) {
+        if (unhide) {
+            /* The urgent-hint should get noticed, so we unhide the bars shortly */
+            unhide_bars();
+        } else if (walks_away) {
+            FREE(last_urgent_ws);
+            hide_bars();
+        }
+    }
+
     redraw_bars();
 }
 
@@ -1554,3 +1702,13 @@ void redraw_bars(void) {
         xcb_flush(xcb_connection);
     }
 }
+
+/*
+ * Set the current binding mode
+ *
+ */
+void set_current_mode(struct mode *current) {
+    I3STRING_FREE(binding.name);
+    binding = *current;
+    return;
+}