]> git.lizzy.rs Git - bspwm.git/blob - helpers.c
Generalize window commands to nodes
[bspwm.git] / helpers.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <math.h>
35 #include "bspwm.h"
36
37 void warn(char *fmt, ...)
38 {
39         va_list ap;
40         va_start(ap, fmt);
41         vfprintf(stderr, fmt, ap);
42         va_end(ap);
43 }
44
45 __attribute__((noreturn))
46 void err(char *fmt, ...)
47 {
48         va_list ap;
49         va_start(ap, fmt);
50         vfprintf(stderr, fmt, ap);
51         va_end(ap);
52         exit(EXIT_FAILURE);
53 }
54
55 char *read_string(const char *file_path, size_t *tlen)
56 {
57         if (file_path == NULL) {
58                 return NULL;
59         }
60
61         int fd = open(file_path, O_RDONLY);
62
63         if (fd == -1) {
64                 perror("Read file: open");
65                 return NULL;
66         }
67
68         char buf[BUFSIZ], *content;
69         size_t len = sizeof(buf);
70
71         if ((content = malloc(len * sizeof(char))) == NULL) {
72                 perror("Read file: malloc");
73                 return NULL;
74         }
75
76         int nb;
77         *tlen = 0;
78
79         while (true) {
80                 nb = read(fd, buf, sizeof(buf));
81                 if (nb < 0) {
82                         perror("Restore tree: read");
83                         free(content);
84                         return NULL;
85                 } else if (nb == 0) {
86                         break;
87                 } else {
88                         *tlen += nb;
89                         if (*tlen > len) {
90                                 len *= 2;
91                                 char *rcontent = realloc(content, len * sizeof(char));
92                                 if (rcontent == NULL) {
93                                         perror("Read file: realloc");
94                                         free(content);
95                                         return NULL;
96                                 } else {
97                                         content = rcontent;
98                                 }
99                         }
100                         strncpy(content + (*tlen - nb), buf, nb);
101                 }
102         }
103
104         return content;
105 }
106
107
108 /* Adapted from i3wm */
109 uint32_t get_color_pixel(const char *color)
110 {
111         unsigned int red, green, blue;
112         if (sscanf(color + 1, "%02x%02x%02x", &red, &green, &blue) == 3) {
113                 /* We set the first 8 bits high to have 100% opacity in case of a 32 bit
114                  * color depth visual. */
115                 return (0xFF << 24) | (red << 16 | green << 8 | blue);
116         } else {
117                 return screen->black_pixel;
118         }
119 }
120
121 bool is_hex_color(const char *color)
122 {
123         if (color[0] != '#' || strlen(color) != 7) {
124                 return false;
125         }
126         for (int i = 1; i < 7; i++) {
127                 if (!isxdigit(color[i])) {
128                         return false;
129                 }
130         }
131         return true;
132 }
133
134 double distance(xcb_point_t a, xcb_point_t b)
135 {
136         return hypot(a.x - b.x, a.y - b.y);
137 }