]> git.lizzy.rs Git - bspwm.git/blob - helpers.c
New setting: 'apply_shadow_property'
[bspwm.git] / helpers.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <xcb/xcb.h>
4 #include <xcb/xcb_event.h>
5 #include "bspwm.h"
6 #include "helpers.h"
7
8 void warn(char *fmt, ...)
9 {
10     va_list ap;
11     va_start(ap, fmt);
12     vfprintf(stderr, fmt, ap);
13     va_end(ap);
14 }
15
16 __attribute__((noreturn))
17 void err(char *fmt, ...)
18 {
19     va_list ap;
20     va_start(ap, fmt);
21     vfprintf(stderr, fmt, ap);
22     va_end(ap);
23     exit(EXIT_FAILURE);
24 }
25
26 uint32_t get_color(char *col)
27 {
28     xcb_colormap_t map = screen->default_colormap;
29     uint32_t pxl = 0;
30
31     if (col[0] == '#') {
32         unsigned int red, green, blue;
33         if (sscanf(col + 1, "%02x%02x%02x", &red, &green, &blue) == 3) {
34             /* 2**16 - 1 == 0xffff and 0x101 * 0xij == 0xijij */
35             red *= 0x101;
36             green *= 0x101;
37             blue *= 0x101;
38             xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(dpy, xcb_alloc_color(dpy, map, red, green, blue), NULL);
39             if (reply != NULL) {
40                 pxl = reply->pixel;
41                 free(reply);
42             }
43         }
44     } else {
45         xcb_alloc_named_color_reply_t *reply = xcb_alloc_named_color_reply(dpy, xcb_alloc_named_color(dpy, map, strlen(col), col), NULL);
46         if (reply != NULL) {
47             pxl = reply->pixel;
48             free(reply);
49         }
50     }
51
52     return pxl;
53 }