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