X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Futil.c;h=c8f4ee389492962b8817b8be39737dcbed20362a;hb=a0e33c1d683ffe3b67a3967980f61f828b41fef2;hp=dab3199bf777a5f2fbc3e55c799fbf6a2652ea6d;hpb=f1a011c014bfb14946840fb9f1528e3940c9916c;p=i3%2Fi3 diff --git a/src/util.c b/src/util.c index dab3199b..c8f4ee38 100644 --- a/src/util.c +++ b/src/util.c @@ -10,35 +10,18 @@ * util.c: Utility functions, which can be useful everywhere. * */ -#include -#include -#include -#include #include #include -#include #include #if defined(__OpenBSD__) #include #endif -#include +#include -#include "i3.h" -#include "data.h" -#include "table.h" -#include "layout.h" -#include "util.h" -#include "xcb.h" -#include "client.h" -#include "log.h" -#include "ewmh.h" -#include "manage.h" -#include "workspace.h" +#include "all.h" static iconv_t conversion_descriptor = 0; -struct keyvalue_table_head by_parent = TAILQ_HEAD_INITIALIZER(by_parent); -struct keyvalue_table_head by_child = TAILQ_HEAD_INITIALIZER(by_child); int min(int a, int b) { return (a < b ? a : b); @@ -48,6 +31,13 @@ int max(int a, int b) { return (a > b ? a : b); } +bool rect_contains(Rect rect, uint32_t x, uint32_t y) { + return (x >= rect.x && + x <= (rect.x + rect.width) && + y >= rect.y && + y <= (rect.y + rect.height)); +} + /* * Updates *destination with new_value and returns true if it was changed or false * if it was the same @@ -76,50 +66,18 @@ void *scalloc(size_t size) { return result; } +void *srealloc(void *ptr, size_t size) { + void *result = realloc(ptr, size); + exit_if_null(result, "Error: out memory (realloc(%zd))\n", size); + return result; +} + char *sstrdup(const char *str) { char *result = strdup(str); exit_if_null(result, "Error: out of memory (strdup())\n"); return result; } -/* - * The table_* functions emulate the behaviour of libxcb-wm, which in libxcb 0.3.4 suddenly - * vanished. Great. - * - */ -bool table_put(struct keyvalue_table_head *head, uint32_t key, void *value) { - struct keyvalue_element *element = scalloc(sizeof(struct keyvalue_element)); - element->key = key; - element->value = value; - - TAILQ_INSERT_TAIL(head, element, elements); - return true; -} - -void *table_remove(struct keyvalue_table_head *head, uint32_t key) { - struct keyvalue_element *element; - - TAILQ_FOREACH(element, head, elements) - if (element->key == key) { - void *value = element->value; - TAILQ_REMOVE(head, element, elements); - free(element); - return value; - } - - return NULL; -} - -void *table_get(struct keyvalue_table_head *head, uint32_t key) { - struct keyvalue_element *element; - - TAILQ_FOREACH(element, head, elements) - if (element->key == key) - return element->value; - - return NULL; -} - /* * Starts the given application by passing it through a shell. We use double fork * to avoid zombie processes. As the started application’s parent exits (immediately), @@ -131,6 +89,7 @@ void *table_get(struct keyvalue_table_head *head, uint32_t key) { * */ void start_application(const char *command) { + LOG("executing: %s\n", command); if (fork() == 0) { /* Child process */ if (fork() == 0) { @@ -207,6 +166,7 @@ char *convert_utf8_to_ucs2(char *input, int *real_strlen) { return buffer; } +#if 0 /* * Returns the client which comes next in focus stack (= was selected before) for @@ -481,6 +441,7 @@ done: FREE(to_title_ucs); return matching; } +#endif /* * Goes through the list of arguments (for exec()) and checks if the given argument @@ -505,13 +466,58 @@ static char **append_argument(char **original, char *argument) { return result; } +#define y(x, ...) yajl_gen_ ## x (gen, ##__VA_ARGS__) +#define ystr(str) yajl_gen_string(gen, (unsigned char*)str, strlen(str)) + +void store_restart_layout() { + yajl_gen gen = yajl_gen_alloc(NULL, NULL); + + dump_node(gen, croot, true); + + const unsigned char *payload; + unsigned int length; + y(get_buf, &payload, &length); + + char *globbed = glob_path("~/.i3/_restart.json"); + int fd = open(globbed, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + free(globbed); + if (fd == -1) { + perror("open()"); + return; + } + + int written = 0; + while (written < length) { + int n = write(fd, payload + written, length - written); + /* TODO: correct error-handling */ + if (n == -1) { + perror("write()"); + return; + } + if (n == 0) { + printf("write == 0?\n"); + return; + } + written += n; + printf("written: %d of %d\n", written, length); + } + close(fd); + + printf("layout: %.*s\n", length, payload); + + y(free); +} + /* * Restart i3 in-place * appends -a to argument list to disable autostart * */ void i3_restart() { - restore_geometry(global_conn); + store_restart_layout(); + restore_geometry(); + + //ipc_shutdown(); LOG("restarting \"%s\"...\n", start_argv[0]); /* make sure -a is in the argument list or append it */ @@ -521,6 +527,8 @@ void i3_restart() { /* not reached */ } +#if 0 + #if defined(__OpenBSD__) /* @@ -556,4 +564,4 @@ void *memmem(const void *l, size_t l_len, const void *s, size_t s_len) { } #endif - +#endif