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