* util.c: Utility functions, which can be useful everywhere.
*
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
#include <sys/wait.h>
#include <stdarg.h>
-#include <assert.h>
#include <iconv.h>
#if defined(__OpenBSD__)
#include <sys/cdefs.h>
#endif
-#include <xcb/xcb_icccm.h>
-
-#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 "ipc.h"
+#include <fcntl.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);
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
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),
*
*/
void start_application(const char *command) {
+ LOG("executing: %s\n", command);
if (fork() == 0) {
/* Child process */
if (fork() == 0) {
return buffer;
}
+#if 0
/*
* Returns the client which comes next in focus stack (= was selected before) for
FREE(to_title_ucs);
return matching;
}
+#endif
/*
* Goes through the list of arguments (for exec()) and checks if the given 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();
+ //ipc_shutdown();
LOG("restarting \"%s\"...\n", start_argv[0]);
/* make sure -a is in the argument list or append it */
/* not reached */
}
+#if 0
+
#if defined(__OpenBSD__)
/*
}
#endif
-
+#endif