Browse Source

Make alpha work on window borders

Up to now, we always asked the X11 server for color allocation ("which
pixel value corresponds to (r,g,b)?", an AllocCollor request).

This commit adds direct support for TrueColor and DirectColor visuals.
In such a visual, the X11 server gives tells us where the red, green,
and blue color components are in a pixel value and we can then just
directly compute the pixel value.

Additionally, this commit adds code that assumes that in a depth=32
visual, the remaining values (after handling red, green, blue) is the
alpha channel for colors. Thus, this adds support for transparent client
borders.

This commit also touches code for the systray. However, the systray must
always use the X11 server's default visual and that one always(?) has
depth=24, i.e. does not support an alpha channel. Thus, the systray
background still cannot be transparent.

Also, in theory this commit should support visuals where some color
component does not have 8 bits, for example RGB565. However, this is
just theoretic and I have no idea how to actually test this (without
jumping through too many hoops).

Fixes: https://github.com/awesomeWM/awesome/issues/162
Signed-off-by: Uli Schlachter <psychon@znc.in>
Uli Schlachter 1 year ago
parent
commit
1acae2aa6c
4 changed files with 72 additions and 10 deletions
  1. 69
    7
      color.c
  2. 1
    1
      color.h
  3. 1
    1
      objects/window.c
  4. 1
    1
      systray.c

+ 69
- 7
color.c View File

@@ -39,15 +39,17 @@
39 39
  */
40 40
 static bool
41 41
 color_parse(const char *colstr, ssize_t len,
42
-            uint8_t *red, uint8_t *green, uint8_t *blue)
42
+            uint8_t *red, uint8_t *green, uint8_t *blue, uint8_t *alpha)
43 43
 {
44 44
     unsigned long colnum;
45 45
     char *p;
46 46
 
47
+    *alpha = 0xff;
48
+
47 49
     colnum = strtoul(colstr + 1, &p, 16);
48 50
     if(len == 9 && (p - colstr) == 9)
49 51
     {
50
-        /* We ignore the alpha component */
52
+        *alpha = colnum & 0xff;
51 53
         colnum >>= 8;
52 54
         len -= 2;
53 55
         p -= 2;
@@ -65,19 +67,58 @@ color_parse(const char *colstr, ssize_t len,
65 67
     return true;
66 68
 }
67 69
 
70
+/** Given a color component value in the range from 0x00 to 0xff and a mask that
71
+ * specifies where the component is, move the component into place. For example,
72
+ * component=0x12 and mask=0xff00 return 0x1200. Note that the mask can have a
73
+ * different width, for example component=0x12 and mask=0xf00 return 0x100.
74
+ * \param component The intensity of a color component.
75
+ * \param mask A mask containing a consecutive number of bits set to 1 defining
76
+ * where the component is.
77
+ * \return A pixel value containing the giving component in the given component.
78
+ */
79
+static uint32_t
80
+apply_mask(uint8_t component, uint32_t mask)
81
+{
82
+    unsigned int shift = 0;
83
+    unsigned int width = 0;
84
+
85
+    // Shift the mask right until the first set bit appears
86
+    while (mask != 0 && (mask & 0x1) == 0) {
87
+        shift++;
88
+        mask >>= 1;
89
+    }
90
+    // Shift the mask right until we saw all set bits
91
+    while ((mask & 0x1) == 1) {
92
+        width++;
93
+        mask >>= 1;
94
+    }
95
+
96
+    // The mask consists of [width] set bits followed by [shift] unset bits.
97
+    // Modify the component accordingly.
98
+    uint32_t result = component;
99
+
100
+    // Scale the result up to the desired width
101
+    if (width < 8)
102
+        result >>= (8 - width);
103
+    if (width > 8)
104
+        result <<= (width - 8);
105
+    return result << shift;
106
+}
107
+
68 108
 /** Send a request to initialize a X color.
69 109
  * If you are only interested in the rgba values and don't need the color's
70 110
  * pixel value, you should use color_init_unchecked() instead.
71 111
  * \param color color_t struct to store color into.
72 112
  * \param colstr Color specification.
73 113
  * \param len The length of colstr (which still MUST be NULL terminated).
114
+ * \param visual The visual for which the color is to be allocated.
74 115
  * \return request informations.
75 116
  */
76 117
 color_init_request_t
77
-color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
118
+color_init_unchecked(color_t *color, const char *colstr, ssize_t len, xcb_visualtype_t *visual)
78 119
 {
79 120
     color_init_request_t req;
80
-    uint8_t red, green, blue;
121
+    uint8_t red, green, blue, alpha;
81 122
 
82 123
     p_clear(&req, 1);
83 124
 
@@ -90,21 +131,40 @@ color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
90 131
     req.color = color;
91 132
 
92 133
     /* The color is given in RGB value */
93
-    if(!color_parse(colstr, len, &red, &green, &blue))
134
+    if(!color_parse(colstr, len, &red, &green, &blue, &alpha))
94 135
     {
95 136
         warn("awesome: error, invalid color '%s'", colstr);
96 137
         req.has_error = true;
97 138
         return req;
98 139
     }
99 140
 
141
+    req.colstr = colstr;
142
+    req.has_error = false;
143
+
144
+    if (visual->_class == XCB_VISUAL_CLASS_TRUE_COLOR || visual->_class == XCB_VISUAL_CLASS_DIRECT_COLOR) {
145
+        uint32_t pixel = 0;
146
+        pixel |= apply_mask(red, visual->red_mask);
147
+        pixel |= apply_mask(blue, visual->blue_mask);
148
+        pixel |= apply_mask(green, visual->green_mask);
149
+        if (draw_visual_depth(globalconf.screen, visual->visual_id) == 32) {
150
+            /* This is not actually in the X11 protocol, but we assume that this
151
+             * is an ARGB visual and everything unset in some mask is alpha.
152
+             */
153
+            pixel |= apply_mask(alpha, ~(visual->red_mask | visual->blue_mask | visual->green_mask));
154
+        }
155
+        req.color->pixel = pixel;
156
+        req.color->red   = RGB_8TO16(red);
157
+        req.color->green = RGB_8TO16(green);
158
+        req.color->blue  = RGB_8TO16(blue);
159
+        req.color->initialized = true;
160
+        return req;
161
+    }
100 162
     req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
101 163
                                                 globalconf.default_cmap,
102 164
                                                 RGB_8TO16(red),
103 165
                                                 RGB_8TO16(green),
104 166
                                                 RGB_8TO16(blue));
105 167
 
106
-    req.has_error = false;
107
-    req.colstr = colstr;
108 168
 
109 169
     return req;
110 170
 }
@@ -118,6 +178,8 @@ color_init_reply(color_init_request_t req)
118 178
 {
119 179
     if(req.has_error)
120 180
         return false;
181
+    if(req.color->initialized)
182
+        return true;
121 183
 
122 184
     xcb_alloc_color_reply_t *hexa_color;
123 185
 

+ 1
- 1
color.h View File

@@ -44,7 +44,7 @@ typedef struct
44 44
     const char *colstr;
45 45
 } color_init_request_t;
46 46
 
47
-color_init_request_t color_init_unchecked(color_t *, const char *, ssize_t);
47
+color_init_request_t color_init_unchecked(color_t *, const char *, ssize_t, xcb_visualtype_t *visual);
48 48
 bool color_init_reply(color_init_request_t);
49 49
 
50 50
 int luaA_pushcolor(lua_State *, const color_t);

+ 1
- 1
objects/window.c View File

@@ -194,7 +194,7 @@ luaA_window_set_border_color(lua_State *L, window_t *window)
194 194
     const char *color_name = luaL_checklstring(L, -1, &len);
195 195
 
196 196
     if(color_name &&
197
-       color_init_reply(color_init_unchecked(&window->border_color, color_name, len)))
197
+       color_init_reply(color_init_unchecked(&window->border_color, color_name, len, globalconf.visual)))
198 198
     {
199 199
         window->border_need_update = true;
200 200
         luaA_object_emit_signal(L, -3, "property::border_color", 0);

+ 1
- 1
systray.c View File

@@ -355,7 +355,7 @@ luaA_systray(lua_State *L)
355 355
         color_t bg_color;
356 356
         bool force_redraw = false;
357 357
 
358
-        if(color_init_reply(color_init_unchecked(&bg_color, bg, bg_len))
358
+        if(color_init_reply(color_init_unchecked(&bg_color, bg, bg_len, globalconf.default_visual))
359 359
                 && globalconf.systray.background_pixel != bg_color.pixel)
360 360
         {
361 361
             uint32_t config_back[] = { bg_color.pixel };

Loading…
Cancel
Save