i3
|
00001 /* 00002 * vim:ts=8:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * 00006 * © 2009 Michael Stapelberg and contributors 00007 * 00008 * See file LICENSE for license information. 00009 * 00010 */ 00011 #include <err.h> 00012 00013 #include "data.h" 00014 00015 #ifndef _UTIL_H 00016 #define _UTIL_H 00017 00018 #define die(...) errx(EXIT_FAILURE, __VA_ARGS__); 00019 #define exit_if_null(pointer, ...) { if (pointer == NULL) die(__VA_ARGS__); } 00020 #define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0) 00021 #define CIRCLEQ_NEXT_OR_NULL(head, elm, field) (CIRCLEQ_NEXT(elm, field) != CIRCLEQ_END(head) ? \ 00022 CIRCLEQ_NEXT(elm, field) : NULL) 00023 #define CIRCLEQ_PREV_OR_NULL(head, elm, field) (CIRCLEQ_PREV(elm, field) != CIRCLEQ_END(head) ? \ 00024 CIRCLEQ_PREV(elm, field) : NULL) 00025 #define FOR_TABLE(workspace) \ 00026 for (int cols = 0; cols < (workspace)->cols; cols++) \ 00027 for (int rows = 0; rows < (workspace)->rows; rows++) 00028 00029 #define NODES_FOREACH(head) \ 00030 for (Con *child = (Con*)-1; (child == (Con*)-1) && ((child = 0), true);) \ 00031 TAILQ_FOREACH(child, &((head)->nodes_head), nodes) 00032 00033 /* greps the ->nodes of the given head and returns the first node that matches the given condition */ 00034 #define GREP_FIRST(dest, head, condition) \ 00035 NODES_FOREACH(head) { \ 00036 if (!(condition)) \ 00037 continue; \ 00038 \ 00039 (dest) = child; \ 00040 break; \ 00041 } 00042 00043 #define FREE(pointer) do { \ 00044 if (pointer != NULL) { \ 00045 free(pointer); \ 00046 pointer = NULL; \ 00047 } \ 00048 } \ 00049 while (0) 00050 00051 #define CALL(obj, member, ...) obj->member(obj, ## __VA_ARGS__) 00052 00053 int min(int a, int b); 00054 int max(int a, int b); 00055 bool rect_contains(Rect rect, uint32_t x, uint32_t y); 00056 Rect rect_add(Rect a, Rect b); 00057 00063 bool update_if_necessary(uint32_t *destination, const uint32_t new_value); 00064 00070 void *smalloc(size_t size); 00071 00077 void *scalloc(size_t size); 00078 00084 void *srealloc(void *ptr, size_t size); 00085 00091 char *sstrdup(const char *str); 00092 00103 void start_application(const char *command); 00104 00120 void exec_i3_utility(char *name, char *argv[]); 00121 00127 void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, 00128 char *err_message); 00129 00137 char *convert_utf8_to_ucs2(char *input, int *real_strlen); 00138 00145 char *resolve_tilde(const char *path); 00146 00151 bool path_exists(const char *path); 00152 00153 00158 char *get_process_filename(const char *prefix); 00159 00165 void i3_restart(bool forget_layout); 00166 00167 #if defined(__OpenBSD__) || defined(__APPLE__) 00168 00169 /* 00170 * Taken from FreeBSD 00171 * Find the first occurrence of the byte string s in byte string l. 00172 * 00173 */ 00174 void *memmem(const void *l, size_t l_len, const void *s, size_t s_len); 00175 00176 #endif 00177 00178 #if defined(__APPLE__) 00179 00180 /* 00181 * Taken from FreeBSD 00182 * Returns a pointer to a new string which is a duplicate of the 00183 * string, but only copies at most n characters. 00184 * 00185 */ 00186 char *strndup(const char *str, size_t n); 00187 00188 #endif 00189 00190 #endif