+#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);
+}
+