* Loads tree from ~/.i3/_restart.json (used for in-place restarts).
*
*/
-bool tree_restore();
+bool tree_restore(const char *path);
#endif
INIT_COLOR(config.bar.unfocused, "#333333", "#222222", "#888888");
INIT_COLOR(config.bar.urgent, "#2f343a", "#900000", "#ffffff");
- config.restart_state_path = "~/.i3/_restart.json";
config.default_border = BS_NORMAL;
parse_configuration(override_configpath);
int screens;
char *override_configpath = NULL;
bool autostart = true;
+ char *layout_path = NULL;
+ bool delete_layout_path;
bool only_check_config = false;
bool force_xinerama = false;
xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
{"config", required_argument, 0, 'c'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
+ {"layout", required_argument, 0, 'L'},
+ {"restart", required_argument, 0, 0},
{"force-xinerama", no_argument, 0, 0},
{0, 0, 0, 0}
};
start_argv = argv;
- while ((opt = getopt_long(argc, argv, "c:Cvahld:V", long_options, &option_index)) != -1) {
+ while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
switch (opt) {
case 'a':
LOG("Autostart disabled using -a\n");
autostart = false;
break;
+ case 'L':
+ layout_path = sstrdup(optarg);
+ delete_layout_path = false;
+ break;
case 'c':
override_configpath = sstrdup(optarg);
break;
"Please check if your driver really does not support RandR "
"and disable this option as soon as you can.\n");
break;
+ } else if (strcmp(long_options[option_index].name, "restart") == 0) {
+ layout_path = sstrdup(optarg);
+ delete_layout_path = true;
+ break;
}
/* fall-through */
default:
fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-a: disable autostart\n");
+ fprintf(stderr, "-L <layoutfile>: load the layout from <layoutfile>\n");
fprintf(stderr, "-v: display version and exit\n");
fprintf(stderr, "-V: enable verbose mode\n");
fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
#endif
}
- if (!tree_restore())
+ bool needs_tree_init = true;
+ if (layout_path) {
+ LOG("Trying to restore the layout from %s...", layout_path);
+ needs_tree_init = !tree_restore(layout_path);
+ if (delete_layout_path)
+ unlink(layout_path);
+ free(layout_path);
+ }
+ if (needs_tree_init)
tree_init();
tree_render();
* Loads tree from ~/.i3/_restart.json (used for in-place restarts).
*
*/
-bool tree_restore() {
- char *globbed = resolve_tilde(config.restart_state_path);
+bool tree_restore(const char *path) {
+ char *globbed = resolve_tilde(path);
if (!path_exists(globbed)) {
LOG("%s does not exist, not restoring tree\n", globbed);
tree_append_json(globbed);
- size_t path_len = strlen(config.restart_state_path);
- char *old_restart = smalloc(path_len + strlen(".old") + 1);
- strncpy(old_restart, config.restart_state_path, path_len + strlen(".old") + 1);
- strncat(old_restart, ".old", strlen(".old") + 1);
- unlink(old_restart);
- rename(globbed, old_restart);
- free(globbed);
- free(old_restart);
-
printf("appended tree, using new root\n");
croot = TAILQ_FIRST(&(croot->nodes_head));
printf("new root = %p\n", croot);
#if defined(__OpenBSD__)
#include <sys/cdefs.h>
#endif
-
#include <fcntl.h>
+#include <pwd.h>
#include "all.h"
return result;
}
+/*
+ * Returns the name of a temporary file with the specified prefix.
+ *
+ */
+char *get_process_filename(const char *prefix)
+{
+ struct passwd *pw = getpwuid(getuid());
+ const char *username = pw ? pw->pw_name : "unknown";
+ char *filename;
+ int res = asprintf(&filename, "/tmp/%s-%s.%d", prefix, username, getpid());
+ if (res == -1) {
+ perror("asprintf()");
+ return NULL;
+ }
+ else {
+ return filename;
+ }
+}
+
#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() {
+char *store_restart_layout() {
setlocale(LC_NUMERIC, "C");
yajl_gen gen = yajl_gen_alloc(NULL, NULL);
unsigned int length;
y(get_buf, &payload, &length);
- char *globbed = resolve_tilde(config.restart_state_path);
- int fd = open(globbed, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
- free(globbed);
+ /* create a temporary file if one hasn't been specified, or just
+ * resolve the tildes in the specified path */
+ char *filename;
+ if (config.restart_state_path == NULL) {
+ filename = get_process_filename("i3-restart-state");
+ if (!filename)
+ return NULL;
+ } else {
+ filename = resolve_tilde(config.restart_state_path);
+ }
+
+ int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("open()");
- return;
+ free(filename);
+ return NULL;
}
int written = 0;
/* TODO: correct error-handling */
if (n == -1) {
perror("write()");
- return;
+ free(filename);
+ return NULL;
}
if (n == 0) {
printf("write == 0?\n");
- return;
+ free(filename);
+ return NULL;
}
written += n;
printf("written: %d of %d\n", written, length);
printf("layout: %.*s\n", length, payload);
y(free);
+
+ return filename;
}
/*
*
*/
void i3_restart() {
- store_restart_layout();
+ char *restart_filename = store_restart_layout();
restore_geometry();
ipc_shutdown();
/* make sure -a is in the argument list or append it */
start_argv = append_argument(start_argv, "-a");
+ /* replace -r <file> so that the layout is restored */
+ if (restart_filename != NULL) {
+ /* create the new argv */
+ int num_args;
+ for (num_args = 0; start_argv[num_args] != NULL; num_args++);
+ char **new_argv = scalloc((num_args + 3) * sizeof(char*));
+
+ /* copy the arguments, but skip the ones we'll replace */
+ int write_index = 0;
+ bool skip_next = false;
+ for (int i = 0; i < num_args; ++i) {
+ if (skip_next)
+ skip_next = false;
+ else if (!strcmp(start_argv[i], "-r"))
+ skip_next = true;
+ else
+ new_argv[write_index++] = start_argv[i];
+ }
+
+ /* add the arguments we'll replace */
+ new_argv[write_index++] = "--restart";
+ new_argv[write_index++] = restart_filename;
+
+ /* swap the argvs */
+ start_argv = new_argv;
+ }
+
execvp(start_argv[0], start_argv);
/* not reached */
}