2015-08-05 11:30:40 +10:00
|
|
|
#include <stdio.h>
|
2015-08-06 07:30:40 +10:00
|
|
|
#include <stdbool.h>
|
2015-08-05 11:30:40 +10:00
|
|
|
#include <stdlib.h>
|
2015-08-16 10:51:23 +10:00
|
|
|
#include <unistd.h>
|
2015-08-05 11:30:40 +10:00
|
|
|
#include "readline.h"
|
|
|
|
#include "stringop.h"
|
|
|
|
#include "list.h"
|
2015-08-09 09:24:18 +10:00
|
|
|
#include "log.h"
|
2015-08-06 07:30:40 +10:00
|
|
|
#include "commands.h"
|
2015-08-05 11:30:40 +10:00
|
|
|
#include "config.h"
|
|
|
|
|
2015-08-11 10:50:22 +10:00
|
|
|
struct sway_config *config;
|
|
|
|
|
2015-08-16 10:51:23 +10:00
|
|
|
static bool exists(const char *path) {
|
|
|
|
return access(path, R_OK) != -1;
|
|
|
|
}
|
|
|
|
|
2015-08-18 21:19:20 +10:00
|
|
|
static char *get_config_path() {
|
2015-08-16 10:51:23 +10:00
|
|
|
char *name = "/.sway/config";
|
2015-08-11 05:22:22 +10:00
|
|
|
const char *home = getenv("HOME");
|
2015-08-16 10:51:23 +10:00
|
|
|
|
|
|
|
// Check home dir
|
2015-08-16 11:03:33 +10:00
|
|
|
sway_log(L_DEBUG, "Trying to find config in ~/.sway/config");
|
2015-08-11 05:22:22 +10:00
|
|
|
char *temp = malloc(strlen(home) + strlen(name) + 1);
|
|
|
|
strcpy(temp, home);
|
|
|
|
strcat(temp, name);
|
2015-08-16 10:51:23 +10:00
|
|
|
if (exists(temp)) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check XDG_CONFIG_HOME with fallback to ~/.config/
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/sway/config");
|
|
|
|
char *xdg_config_home = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (xdg_config_home == NULL) {
|
|
|
|
sway_log(L_DEBUG, "Falling back to ~/.config/sway/config");
|
|
|
|
name = "/.config/sway/config";
|
|
|
|
temp = malloc(strlen(home) + strlen(name) + 1);
|
|
|
|
strcpy(temp, home);
|
|
|
|
strcat(temp, name);
|
|
|
|
} else {
|
|
|
|
name = "/sway/config";
|
|
|
|
temp = malloc(strlen(xdg_config_home) + strlen(name) + 1);
|
2015-08-18 20:53:02 +10:00
|
|
|
strcpy(xdg_config_home, home);
|
2015-08-16 10:51:23 +10:00
|
|
|
strcat(temp, name);
|
|
|
|
}
|
|
|
|
if (exists(temp)) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check /etc/
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in /etc/sway/config");
|
|
|
|
strcpy(temp, "/etc/sway/config");
|
|
|
|
if (exists(temp)) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check XDG_CONFIG_DIRS
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
|
|
|
|
char *xdg_config_dirs = getenv("XDG_CONFIG_DIRS");
|
|
|
|
if (xdg_config_dirs != NULL) {
|
|
|
|
list_t *paths = split_string(xdg_config_dirs, ":");
|
|
|
|
name = "/sway/config";
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < paths->length; i++ ) {
|
|
|
|
temp = malloc(strlen(paths->items[i]) + strlen(name) + 1);
|
|
|
|
strcpy(temp, paths->items[i]);
|
|
|
|
strcat(temp, name);
|
|
|
|
if (exists(temp)) {
|
|
|
|
free_flat_list(paths);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_flat_list(paths);
|
|
|
|
}
|
|
|
|
|
2015-08-16 11:03:33 +10:00
|
|
|
//Now fall back to i3 paths and try the same thing
|
|
|
|
name = "/.i3/config";
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in ~/.i3/config");
|
2015-08-16 11:13:18 +10:00
|
|
|
temp = malloc(strlen(home) + strlen(name) + 1);
|
2015-08-16 11:03:33 +10:00
|
|
|
strcpy(temp, home);
|
|
|
|
strcat(temp, name);
|
|
|
|
if (exists(temp)) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_HOME/i3/config");
|
|
|
|
if (xdg_config_home == NULL) {
|
|
|
|
sway_log(L_DEBUG, "Falling back to ~/.config/i3/config");
|
|
|
|
name = "/.config/i3/config";
|
|
|
|
temp = malloc(strlen(home) + strlen(name) + 1);
|
|
|
|
strcpy(temp, home);
|
|
|
|
strcat(temp, name);
|
|
|
|
} else {
|
|
|
|
name = "/i3/config";
|
|
|
|
temp = malloc(strlen(xdg_config_home) + strlen(name) + 1);
|
2015-08-18 20:53:02 +10:00
|
|
|
strcpy(xdg_config_home, home);
|
2015-08-16 11:03:33 +10:00
|
|
|
strcat(temp, name);
|
|
|
|
}
|
|
|
|
if (exists(temp)) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in /etc/i3/config");
|
|
|
|
strcpy(temp, "/etc/i3/config");
|
|
|
|
if (exists(temp)) {
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
sway_log(L_DEBUG, "Trying to find config in XDG_CONFIG_DIRS");
|
|
|
|
if (xdg_config_dirs != NULL) {
|
|
|
|
list_t *paths = split_string(xdg_config_dirs, ":");
|
|
|
|
name = "/i3/config";
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < paths->length; i++ ) {
|
|
|
|
temp = malloc(strlen(paths->items[i]) + strlen(name) + 1);
|
|
|
|
strcpy(temp, paths->items[i]);
|
|
|
|
strcat(temp, name);
|
|
|
|
if (exists(temp)) {
|
|
|
|
free_flat_list(paths);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_flat_list(paths);
|
|
|
|
}
|
|
|
|
|
2015-08-16 10:51:23 +10:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:48:41 +10:00
|
|
|
bool load_config(void) {
|
2015-08-16 10:51:23 +10:00
|
|
|
sway_log(L_INFO, "Loading config");
|
|
|
|
|
|
|
|
char *path = get_config_path();
|
|
|
|
|
|
|
|
if (path == NULL) {
|
|
|
|
sway_log(L_ERROR, "Unable to find a config file!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fopen(path, "r");
|
2015-08-11 05:22:22 +10:00
|
|
|
if (!f) {
|
2015-08-16 10:51:23 +10:00
|
|
|
fprintf(stderr, "Unable to open %s for reading", path);
|
|
|
|
free(path);
|
2015-08-11 05:22:22 +10:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-16 10:51:23 +10:00
|
|
|
free(path);
|
2015-08-14 05:41:29 +10:00
|
|
|
|
|
|
|
bool config_load_success;
|
|
|
|
if (config) {
|
|
|
|
config_load_success = read_config(f, true);
|
|
|
|
} else {
|
|
|
|
config_load_success = read_config(f, false);
|
|
|
|
}
|
2015-08-11 05:22:22 +10:00
|
|
|
fclose(f);
|
2015-08-14 05:41:29 +10:00
|
|
|
|
|
|
|
return config_load_success;
|
2015-08-11 05:22:22 +10:00
|
|
|
}
|
|
|
|
|
2015-08-09 23:23:10 +10:00
|
|
|
void config_defaults(struct sway_config *config) {
|
2015-08-05 11:30:40 +10:00
|
|
|
config->symbols = create_list();
|
|
|
|
config->modes = create_list();
|
2015-08-14 03:32:43 +10:00
|
|
|
config->cmd_queue = create_list();
|
2015-08-18 10:34:53 +10:00
|
|
|
config->workspace_outputs = create_list();
|
2015-08-06 12:10:56 +10:00
|
|
|
config->current_mode = malloc(sizeof(struct sway_mode));
|
|
|
|
config->current_mode->name = NULL;
|
|
|
|
config->current_mode->bindings = create_list();
|
|
|
|
list_add(config->modes, config->current_mode);
|
2015-08-09 23:23:10 +10:00
|
|
|
// Flags
|
|
|
|
config->focus_follows_mouse = true;
|
|
|
|
config->mouse_warping = true;
|
2015-08-11 05:09:51 +10:00
|
|
|
config->reloading = false;
|
2015-08-14 05:41:29 +10:00
|
|
|
config->active = false;
|
|
|
|
config->failed = false;
|
2015-08-09 23:23:10 +10:00
|
|
|
}
|
|
|
|
|
2015-08-14 05:41:29 +10:00
|
|
|
bool read_config(FILE *file, bool is_active) {
|
|
|
|
struct sway_config *temp_config = malloc(sizeof(struct sway_config));
|
|
|
|
config_defaults(temp_config);
|
2015-08-11 05:09:51 +10:00
|
|
|
if (is_active) {
|
2015-08-14 05:41:29 +10:00
|
|
|
sway_log(L_DEBUG, "Performing configuration file reload");
|
|
|
|
temp_config->reloading = true;
|
|
|
|
temp_config->active = true;
|
2015-08-11 05:09:51 +10:00
|
|
|
}
|
2015-08-11 04:53:43 +10:00
|
|
|
|
2015-08-06 12:10:56 +10:00
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
int temp_depth = 0; // Temporary: skip all config sections with depth
|
2015-08-06 07:30:40 +10:00
|
|
|
|
2015-08-05 11:30:40 +10:00
|
|
|
while (!feof(file)) {
|
2015-08-18 22:39:26 +10:00
|
|
|
int _;
|
2015-08-05 11:30:40 +10:00
|
|
|
char *line = read_line(file);
|
2015-08-18 22:39:26 +10:00
|
|
|
line = strip_comments(line);
|
|
|
|
line = strip_whitespace(line, &_);
|
2015-08-05 11:30:40 +10:00
|
|
|
if (!line[0]) {
|
|
|
|
goto _continue;
|
|
|
|
}
|
2015-08-06 12:10:56 +10:00
|
|
|
if (temp_depth && line[0] == '}') {
|
|
|
|
temp_depth--;
|
2015-08-06 07:30:40 +10:00
|
|
|
goto _continue;
|
|
|
|
}
|
|
|
|
|
2015-08-14 03:32:43 +10:00
|
|
|
// Any command which would require wlc to be initialized
|
2015-08-14 03:56:08 +10:00
|
|
|
// should be queued for later execution
|
2015-08-14 03:32:43 +10:00
|
|
|
list_t *args = split_string(line, " ");
|
2015-08-14 05:41:29 +10:00
|
|
|
if (!is_active && (
|
|
|
|
strcmp("exec", args->items[0]) == 0 ||
|
|
|
|
strcmp("exec_always", args->items[0]) == 0 )) {
|
2015-08-14 03:32:43 +10:00
|
|
|
sway_log(L_DEBUG, "Deferring command %s", line);
|
2015-08-14 05:41:29 +10:00
|
|
|
|
2015-08-14 03:32:43 +10:00
|
|
|
char *cmd = malloc(strlen(line) + 1);
|
|
|
|
strcpy(cmd, line);
|
2015-08-14 05:41:29 +10:00
|
|
|
list_add(temp_config->cmd_queue, cmd);
|
|
|
|
} else if (!temp_depth && !handle_command(temp_config, line)) {
|
2015-08-14 03:32:43 +10:00
|
|
|
sway_log(L_DEBUG, "Config load failed for line %s", line);
|
2015-08-06 12:10:56 +10:00
|
|
|
success = false;
|
2015-08-14 05:41:29 +10:00
|
|
|
temp_config->failed = true;
|
2015-08-16 10:51:23 +10:00
|
|
|
}
|
2015-08-14 03:32:43 +10:00
|
|
|
list_free(args);
|
2015-08-11 05:00:10 +10:00
|
|
|
|
2015-08-05 11:30:40 +10:00
|
|
|
_continue:
|
2015-08-06 07:30:40 +10:00
|
|
|
if (line && line[strlen(line) - 1] == '{') {
|
2015-08-06 12:10:56 +10:00
|
|
|
temp_depth++;
|
2015-08-06 07:30:40 +10:00
|
|
|
}
|
2015-08-05 11:30:40 +10:00
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
2015-08-11 05:09:51 +10:00
|
|
|
if (is_active) {
|
2015-08-14 05:41:29 +10:00
|
|
|
temp_config->reloading = false;
|
2015-08-11 05:09:51 +10:00
|
|
|
}
|
2015-08-14 05:41:29 +10:00
|
|
|
config = temp_config;
|
2015-08-11 04:53:43 +10:00
|
|
|
|
2015-08-14 05:41:29 +10:00
|
|
|
return success;
|
2015-08-05 11:30:40 +10:00
|
|
|
}
|
2015-08-06 12:40:38 +10:00
|
|
|
|
|
|
|
char *do_var_replacement(struct sway_config *config, char *str) {
|
|
|
|
// 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;
|
|
|
|
}
|