]> git.lizzy.rs Git - bspwm.git/blob - helpers.c
Cosmetic improvements
[bspwm.git] / helpers.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <math.h>
4 #include <xcb/xcb.h>
5 #include <xcb/xcb_event.h>
6 #include "bspwm.h"
7 #include "helpers.h"
8
9 void warn(char *fmt, ...)
10 {
11     va_list ap;
12     va_start(ap, fmt);
13     vfprintf(stderr, fmt, ap);
14     va_end(ap);
15 }
16
17 __attribute__((noreturn))
18 void err(char *fmt, ...)
19 {
20     va_list ap;
21     va_start(ap, fmt);
22     vfprintf(stderr, fmt, ap);
23     va_end(ap);
24     exit(EXIT_FAILURE);
25 }
26
27 bool get_color(char *col, uint32_t *pxl)
28 {
29     xcb_colormap_t map = screen->default_colormap;
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                 return true;
43             }
44         }
45     } else {
46         xcb_alloc_named_color_reply_t *reply = xcb_alloc_named_color_reply(dpy, xcb_alloc_named_color(dpy, map, strlen(col), col), NULL);
47         if (reply != NULL) {
48             *pxl = reply->pixel;
49             free(reply);
50             return true;
51         }
52     }
53     return false;
54 }
55
56 double distance(xcb_point_t a, xcb_point_t b)
57 {
58     return hypot(a.x - b.x, a.y - b.y);
59 }