2015-08-05 01:30:40 +00:00
|
|
|
#include <stdio.h>
|
2015-08-05 21:30:40 +00:00
|
|
|
#include <stdbool.h>
|
2015-08-05 01:30:40 +00:00
|
|
|
#include <stdlib.h>
|
2015-08-16 00:51:23 +00:00
|
|
|
#include <unistd.h>
|
2015-08-05 01:30:40 +00:00
|
|
|
#include "readline.h"
|
|
|
|
#include "stringop.h"
|
|
|
|
#include "list.h"
|
2015-08-08 23:24:18 +00:00
|
|
|
#include "log.h"
|
2015-08-05 21:30:40 +00:00
|
|
|
#include "commands.h"
|
2015-08-05 01:30:40 +00:00
|
|
|
#include "config.h"
|
2015-08-19 02:05:58 +00:00
|
|
|
#include "layout.h"
|
2015-08-20 21:14:26 +00:00
|
|
|
#include "input_state.h"
|
2015-08-05 01:30:40 +00:00
|
|
|
|
2015-09-07 06:22:02 +00:00
|
|
|
struct sway_config *config = NULL;
|
2015-08-11 00:50:22 +00:00
|
|
|
|
2015-09-07 21:29:40 +00:00
|
|
|
|
2015-09-07 13:52:27 +00:00
|
|
|
static void free_variable(struct sway_variable *var) {
|
|
|
|
free(var->name);
|
|
|
|
free(var->value);
|
|
|
|
free(var);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_binding(struct sway_binding *bind) {
|
|
|
|
free_flat_list(bind->keys);
|
|
|
|
free(bind->command);
|
|
|
|
free(bind);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_mode(struct sway_mode *mode) {
|
|
|
|
free(mode->name);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < mode->bindings->length; ++i) {
|
|
|
|
free_binding(mode->bindings->items[i]);
|
|
|
|
}
|
|
|
|
list_free(mode->bindings);
|
|
|
|
free(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_outut_config(struct output_config *oc) {
|
|
|
|
free(oc->name);
|
|
|
|
free(oc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_workspace_output(struct workspace_output *wo) {
|
|
|
|
free(wo->output);
|
|
|
|
free(wo->workspace);
|
|
|
|
free(wo);
|
|
|
|
}
|
|
|
|
|
2015-09-07 21:29:40 +00:00
|
|
|
static void free_config(struct sway_config *config) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < config->symbols->length; ++i) {
|
|
|
|
free_variable(config->symbols->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->symbols);
|
|
|
|
|
|
|
|
for (i = 0; i < config->modes->length; ++i) {
|
|
|
|
free_mode(config->modes->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->modes);
|
|
|
|
|
|
|
|
free_flat_list(config->cmd_queue);
|
|
|
|
|
|
|
|
for (i = 0; i < config->workspace_outputs->length; ++i) {
|
|
|
|
free_workspace_output(config->workspace_outputs->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->workspace_outputs);
|
|
|
|
|
|
|
|
for (i = 0; i < config->output_configs->length; ++i) {
|
|
|
|
free_outut_config(config->output_configs->items[i]);
|
|
|
|
}
|
|
|
|
list_free(config->output_configs);
|
|
|
|
free(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-07 05:48:43 +00:00
|
|
|
static bool file_exists(const char *path) {
|
2015-08-16 00:51:23 +00:00
|
|
|
return access(path, R_OK) != -1;
|
|
|
|
}
|
|
|
|
|
2015-09-07 05:48:43 +00:00
|
|
|
static void config_defaults(struct sway_config *config) {
|
2015-08-19 11:50:27 +00:00
|
|
|
config->symbols = create_list();
|
|
|
|
config->modes = create_list();
|
|
|
|
config->workspace_outputs = create_list();
|
2015-08-22 15:18:55 +00:00
|
|
|
config->output_configs = create_list();
|
2015-09-07 05:48:43 +00:00
|
|
|
|
|
|
|
config->cmd_queue = create_list();
|
|
|
|
|
2015-08-19 11:50:27 +00:00
|
|
|
config->current_mode = malloc(sizeof(struct sway_mode));
|
2015-09-07 21:29:40 +00:00
|
|
|
config->current_mode->name = malloc(sizeof("default"));
|
|
|
|
strcpy(config->current_mode->name, "default");
|
2015-08-19 11:50:27 +00:00
|
|
|
config->current_mode->bindings = create_list();
|
|
|
|
list_add(config->modes, config->current_mode);
|
2015-09-07 05:48:43 +00:00
|
|
|
|
2015-08-28 21:45:40 +00:00
|
|
|
config->floating_mod = 0;
|
2015-08-28 02:52:59 +00:00
|
|
|
config->default_layout = L_NONE;
|
|
|
|
config->default_orientation = L_NONE;
|
2015-08-19 11:50:27 +00:00
|
|
|
// Flags
|
|
|
|
config->focus_follows_mouse = true;
|
|
|
|
config->mouse_warping = true;
|
|
|
|
config->reloading = false;
|
|
|
|
config->active = false;
|
|
|
|
config->failed = false;
|
2015-08-31 02:34:10 +00:00
|
|
|
config->auto_back_and_forth = false;
|
2015-08-28 21:45:40 +00:00
|
|
|
|
2015-08-19 11:50:27 +00:00
|
|
|
config->gaps_inner = 0;
|
|
|
|
config->gaps_outer = 0;
|
|
|
|
}
|
2015-08-16 00:51:23 +00:00
|
|
|
|
2015-09-07 05:48:43 +00:00
|
|
|
static char *get_config_path(void) {
|
|
|
|
char *config_path = NULL;
|
|
|
|
char *paths[3] = {getenv("HOME"), getenv("XDG_CONFIG_HOME"), ""};
|
|
|
|
int pathlen[3] = {0, 0, 0};
|
|
|
|
int i;
|
|
|
|
#define home paths[0]
|
|
|
|
#define conf paths[1]
|
|
|
|
// Get home and config directories
|
|
|
|
home = home ? strdup(home) : NULL;
|
|
|
|
if (conf) {
|
|
|
|
conf = strdup(conf);
|
2015-08-19 14:33:30 +00:00
|
|
|
} else if (home) {
|
|
|
|
const char *def = "/.config";
|
2015-09-07 05:48:43 +00:00
|
|
|
conf = malloc(strlen(home) + strlen(def) + 1);
|
|
|
|
strcpy(conf, home);
|
|
|
|
strcat(conf, def);
|
2015-08-19 14:33:30 +00:00
|
|
|
} else {
|
|
|
|
home = strdup("");
|
2015-09-07 05:48:43 +00:00
|
|
|
conf = strdup("");
|
2015-08-16 01:03:33 +00:00
|
|
|
}
|
2015-09-07 05:48:43 +00:00
|
|
|
pathlen[0] = strlen(home);
|
|
|
|
pathlen[1] = strlen(conf);
|
|
|
|
#undef home
|
|
|
|
#undef conf
|
|
|
|
// Search for config file from search paths
|
|
|
|
static const char *search_paths[] = {
|
|
|
|
"/.sway/config", // Prepend with $home
|
|
|
|
"/sway/config", // Prepend with $config
|
|
|
|
"/etc/sway/config",
|
|
|
|
"/.i3/config", // $home
|
|
|
|
"/.i3/config", // $config
|
|
|
|
"/etc/i3/config"
|
|
|
|
};
|
2015-08-21 14:53:11 +00:00
|
|
|
for (i = 0; i < (int)(sizeof(search_paths) / sizeof(char *)); ++i) {
|
2015-09-07 05:48:43 +00:00
|
|
|
char *test = malloc(pathlen[i%3] + strlen(search_paths[i]) + 1);
|
|
|
|
strcpy(test, paths[i%3]);
|
|
|
|
strcat(test, search_paths[i]);
|
2015-08-19 11:50:27 +00:00
|
|
|
sway_log(L_DEBUG, "Checking for config at %s", test);
|
2015-09-07 05:48:43 +00:00
|
|
|
if (file_exists(test)) {
|
|
|
|
config_path = test;
|
|
|
|
goto cleanup;
|
2015-08-19 11:50:27 +00:00
|
|
|
}
|
|
|
|
free(test);
|
2015-08-16 01:03:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
|
2015-08-19 11:50:27 +00:00
|
|
|
char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
|
2015-09-07 05:48:43 +00:00
|
|
|
if (xdg_config_dirs) {
|
2015-08-16 01:03:33 +00:00
|
|
|
list_t *paths = split_string(xdg_config_dirs, ":");
|
2015-09-07 05:48:43 +00:00
|
|
|
const char *name = "/sway/config";
|
2015-08-16 01:03:33 +00:00
|
|
|
for (i = 0; i < paths->length; i++ ) {
|
2015-09-07 05:48:43 +00:00
|
|
|
char *test = malloc(strlen(paths->items[i]) + strlen(name) + 1);
|
2015-08-19 11:50:27 +00:00
|
|
|
strcpy(test, paths->items[i]);
|
|
|
|
strcat(test, name);
|
2015-09-07 05:48:43 +00:00
|
|
|
if (file_exists(test)) {
|
|
|
|
config_path = test;
|
|
|
|
break;
|
2015-08-16 01:03:33 +00:00
|
|
|
}
|
2015-08-19 11:50:27 +00:00
|
|
|
free(test);
|
2015-08-16 01:03:33 +00:00
|
|
|
}
|
|
|
|
free_flat_list(paths);
|
|
|
|
}
|
|
|
|
|
2015-09-07 05:48:43 +00:00
|
|
|
cleanup:
|
|
|
|
free(paths[0]);
|
|
|
|
free(paths[1]);
|
|
|
|
return config_path;
|
2015-08-16 00:51:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 12:37:09 +00:00
|
|
|
bool load_config(const char *file) {
|
2015-08-16 00:51:23 +00:00
|
|
|
sway_log(L_INFO, "Loading config");
|
|
|
|
|
2015-08-20 21:14:26 +00:00
|
|
|
input_init();
|
|
|
|
|
2015-08-20 12:37:09 +00:00
|
|
|
char *path;
|
|
|
|
if (file != NULL) {
|
|
|
|
path = strdup(file);
|
|
|
|
} else {
|
|
|
|
path = get_config_path();
|
|
|
|
}
|
2015-08-16 00:51:23 +00:00
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
sway_log(L_ERROR, "Unable to find a config file!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fopen(path, "r");
|
2015-08-10 19:22:22 +00:00
|
|
|
if (!f) {
|
2015-08-16 00:51:23 +00:00
|
|
|
fprintf(stderr, "Unable to open %s for reading", path);
|
|
|
|
free(path);
|
2015-08-10 19:22:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-16 00:51:23 +00:00
|
|
|
free(path);
|
2015-08-13 19:41:29 +00:00
|
|
|
|
|
|
|
bool config_load_success;
|
|
|
|
if (config) {
|
|
|
|
config_load_success = read_config(f, true);
|
|
|
|
} else {
|
|
|
|
config_load_success = read_config(f, false);
|
|
|
|
}
|
2015-08-10 19:22:22 +00:00
|
|
|
fclose(f);
|
2015-08-13 19:41:29 +00:00
|
|
|
|
|
|
|
return config_load_success;
|
2015-08-10 19:22:22 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 19:41:29 +00:00
|
|
|
bool read_config(FILE *file, bool is_active) {
|
2015-09-07 21:29:40 +00:00
|
|
|
struct sway_config *old_config = config;
|
|
|
|
struct sway_mode *default_mode;
|
|
|
|
config = malloc(sizeof(struct sway_config));
|
|
|
|
|
|
|
|
config_defaults(config);
|
|
|
|
default_mode = config->current_mode;
|
|
|
|
|
2015-08-10 19:09:51 +00:00
|
|
|
if (is_active) {
|
2015-08-13 19:41:29 +00:00
|
|
|
sway_log(L_DEBUG, "Performing configuration file reload");
|
2015-09-07 21:29:40 +00:00
|
|
|
config->reloading = true;
|
|
|
|
config->active = true;
|
2015-08-10 19:09:51 +00:00
|
|
|
}
|
2015-08-06 02:10:56 +00:00
|
|
|
bool success = true;
|
|
|
|
|
2015-09-07 21:29:40 +00:00
|
|
|
char *line;
|
2015-08-05 01:30:40 +00:00
|
|
|
while (!feof(file)) {
|
2015-09-07 21:29:40 +00:00
|
|
|
line = read_line(file);
|
2015-09-07 21:40:23 +00:00
|
|
|
line = strip_whitespace(line);
|
2015-08-18 22:03:38 +00:00
|
|
|
line = strip_comments(line);
|
2015-09-07 21:29:40 +00:00
|
|
|
if (line[0] == '\0') {
|
2015-08-05 01:30:40 +00:00
|
|
|
goto _continue;
|
|
|
|
}
|
2015-09-07 21:29:40 +00:00
|
|
|
if (line[0] == '}') {
|
|
|
|
config->current_mode = default_mode;
|
2015-08-05 21:30:40 +00:00
|
|
|
goto _continue;
|
|
|
|
}
|
|
|
|
|
2015-08-13 17:32:43 +00:00
|
|
|
// Any command which would require wlc to be initialized
|
2015-08-13 17:56:08 +00:00
|
|
|
// should be queued for later execution
|
2015-09-07 21:29:40 +00:00
|
|
|
list_t *args = split_string(line, whitespace);
|
2015-09-04 23:57:03 +00:00
|
|
|
struct cmd_handler *handler;
|
|
|
|
if ((handler = find_handler(args->items[0]))) {
|
2015-09-05 00:09:07 +00:00
|
|
|
if (handler->config_type == CMD_KEYBIND) {
|
2015-09-04 23:57:03 +00:00
|
|
|
sway_log(L_ERROR, "Invalid command during config ``%s''", line);
|
2015-09-05 00:09:07 +00:00
|
|
|
} else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) {
|
2015-09-04 23:57:03 +00:00
|
|
|
sway_log(L_DEBUG, "Deferring command ``%s''", line);
|
|
|
|
char *cmd = malloc(strlen(line) + 1);
|
|
|
|
strcpy(cmd, line);
|
2015-09-07 21:29:40 +00:00
|
|
|
list_add(config->cmd_queue, cmd);
|
|
|
|
} else if (!handle_command(line)) {
|
2015-09-04 23:57:03 +00:00
|
|
|
sway_log(L_DEBUG, "Config load failed for line ``%s''", line);
|
|
|
|
success = false;
|
2015-09-07 21:29:40 +00:00
|
|
|
config->failed = true;
|
2015-09-04 23:57:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-05 00:02:02 +00:00
|
|
|
sway_log(L_ERROR, "Invalid command ``%s''", line);
|
2015-08-16 00:51:23 +00:00
|
|
|
}
|
2015-08-24 02:09:18 +00:00
|
|
|
free_flat_list(args);
|
2015-08-10 19:00:10 +00:00
|
|
|
|
2015-08-05 01:30:40 +00:00
|
|
|
_continue:
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
2015-08-10 19:09:51 +00:00
|
|
|
if (is_active) {
|
2015-09-07 21:29:40 +00:00
|
|
|
config->reloading = false;
|
2015-08-19 02:05:58 +00:00
|
|
|
arrange_windows(&root_container, -1, -1);
|
2015-08-10 19:09:51 +00:00
|
|
|
}
|
2015-09-07 21:29:40 +00:00
|
|
|
if (old_config) {
|
|
|
|
free_config(old_config);
|
2015-09-07 06:22:02 +00:00
|
|
|
}
|
2015-08-10 18:53:43 +00:00
|
|
|
|
2015-08-13 19:41:29 +00:00
|
|
|
return success;
|
2015-08-05 01:30:40 +00:00
|
|
|
}
|
2015-08-06 02:40:38 +00:00
|
|
|
|
2015-09-07 21:29:40 +00:00
|
|
|
char *do_var_replacement(char *str) {
|
2015-08-06 02:40:38 +00:00
|
|
|
// TODO: Handle escaping $ and using $ in string literals
|
|
|
|
int i;
|
|
|
|
for (i = 0; str[i]; ++i) {
|
|
|
|
if (str[i] == '$') {
|
|
|
|
// Try for match (note: this could be faster)
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < config->symbols->length; ++j) {
|
|
|
|
struct sway_variable *var = config->symbols->items[j];
|
|
|
|
if (strstr(str + i, var->name) == str + i) {
|
|
|
|
// Match, do replacement
|
|
|
|
char *new_string = malloc(
|
|
|
|
strlen(str) -
|
|
|
|
strlen(var->name) +
|
|
|
|
strlen(var->value) + 1);
|
|
|
|
strncpy(new_string, str, i);
|
|
|
|
new_string[i] = 0;
|
|
|
|
strcat(new_string, var->value);
|
|
|
|
strcat(new_string, str + i + strlen(var->name));
|
|
|
|
free(str);
|
|
|
|
str = new_string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|