From c0ee2a64065f3a9953e977e517a466361444c144 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 10 Aug 2015 13:53:43 -0500 Subject: [PATCH 1/5] Added in reload and exec_always handling --- sway/commands.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- sway/config.c | 9 ++++++++- sway/config.h | 4 +++- sway/main.c | 3 ++- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 64130fdcb..df394901e 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -39,7 +39,7 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) { binding->keys = create_list(); binding->modifiers = 0; binding->command = join_args(argv + 1, argc - 1); - + list_t *split = split_string(argv[0], "+"); int i; for (i = 0; i < split->length; ++i) { @@ -78,6 +78,28 @@ int cmd_exec(struct sway_config *config, int argc, char **argv) { sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc); return 1; } + + if (config->reloading) { + sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); + return 0; + } + + if (fork() == 0) { + char *args = join_args(argv, argc); + sway_log(L_DEBUG, "Executing %s", args); + execl("/bin/sh", "sh", "-c", args, (char *)NULL); + free(args); + exit(0); + } + return 0; +} + +int cmd_exec_always(struct sway_config *config, int argc, char **argv) { + if (argc < 1) { + sway_log(L_ERROR, "Invalid exec_always command (expected at least 1 argument, got %d)", argc); + return 1; + } + if (fork() == 0) { char *args = join_args(argv, argc); sway_log(L_DEBUG, "Executing %s", args); @@ -152,6 +174,31 @@ int cmd_layout(struct sway_config *config, int argc, char **argv) { return 0; } +int cmd_reload(struct sway_config *config, int argc, char **argv) { + if (argc != 0) { + sway_log(L_ERROR, "Invalid reload command (expected 1 arguments, got %d)", argc); + return 1; + } + + // TODO: Allow use of more config file locations + const char *name = "/.sway/config"; + const char *home = getenv("HOME"); + char *temp = malloc(strlen(home) + strlen(name) + 1); + strcpy(temp, home); + strcat(temp, name); + FILE *f = fopen(temp, "r"); + if (!f) { + fprintf(stderr, "Unable to open %s for reading", temp); + free(temp); + exit(1); + } + free(temp); + config = read_config(f, true); + fclose(f); + + return 0; +} + int cmd_set(struct sway_config *config, int argc, char **argv) { if (argc != 2) { sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); @@ -232,12 +279,14 @@ int cmd_fullscreen(struct sway_config *config, int argc, char **argv) { struct cmd_handler handlers[] = { { "bindsym", cmd_bindsym }, { "exec", cmd_exec }, + { "exec_always", cmd_exec_always }, { "exit", cmd_exit }, { "focus", cmd_focus }, { "focus_follows_mouse", cmd_focus_follows_mouse }, { "fullscreen", cmd_fullscreen }, { "layout", cmd_layout }, { "log_colors", cmd_log_colors }, + { "reload", cmd_reload }, { "set", cmd_set }, { "splith", cmd_splith }, { "splitv", cmd_splitv } diff --git a/sway/config.c b/sway/config.c index 26dc88cbb..ee6c324c0 100644 --- a/sway/config.c +++ b/sway/config.c @@ -18,12 +18,17 @@ void config_defaults(struct sway_config *config) { // Flags config->focus_follows_mouse = true; config->mouse_warping = true; + config->reloading = false; } -struct sway_config *read_config(FILE *file) { +struct sway_config *read_config(FILE *file, bool is_active) { struct sway_config *config = malloc(sizeof(struct sway_config)); config_defaults(config); + if (is_active) { + config->reloading = true; + } + bool success = true; int temp_depth = 0; // Temporary: skip all config sections with depth @@ -56,6 +61,8 @@ _continue: exit(1); } + config->reloading = false; + return config; } diff --git a/sway/config.h b/sway/config.h index 6802a3418..6a50c4454 100644 --- a/sway/config.h +++ b/sway/config.h @@ -29,9 +29,11 @@ struct sway_config { // Flags bool focus_follows_mouse; bool mouse_warping; + + bool reloading; }; -struct sway_config *read_config(FILE *file); +struct sway_config *read_config(FILE *file, bool is_active); char *do_var_replacement(struct sway_config *config, char *str); extern struct sway_config *config; diff --git a/sway/main.c b/sway/main.c index 248beae71..298e530d9 100644 --- a/sway/main.c +++ b/sway/main.c @@ -23,7 +23,7 @@ void load_config() { exit(1); } free(temp); - config = read_config(f); + config = read_config(f, false); fclose(f); } @@ -52,6 +52,7 @@ int main(int argc, char **argv) { .motion = handle_pointer_motion, .button = handle_pointer_button } + }; setenv("WLC_DIM", "0", 0); From 68beabda0382e63d2c0101d78893eb20967dbe43 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 10 Aug 2015 14:00:10 -0500 Subject: [PATCH 2/5] Style fixes and slight reload command alteration --- sway/commands.c | 8 ++++---- sway/config.c | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index df394901e..5ed5e0700 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -80,7 +80,7 @@ int cmd_exec(struct sway_config *config, int argc, char **argv) { } if (config->reloading) { - sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); + sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); return 0; } @@ -176,7 +176,7 @@ int cmd_layout(struct sway_config *config, int argc, char **argv) { int cmd_reload(struct sway_config *config, int argc, char **argv) { if (argc != 0) { - sway_log(L_ERROR, "Invalid reload command (expected 1 arguments, got %d)", argc); + sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc); return 1; } @@ -188,9 +188,9 @@ int cmd_reload(struct sway_config *config, int argc, char **argv) { strcat(temp, name); FILE *f = fopen(temp, "r"); if (!f) { - fprintf(stderr, "Unable to open %s for reading", temp); + sway_log(L_ERROR, "Sway config file not found, aborting reload!"); free(temp); - exit(1); + return 1; } free(temp); config = read_config(f, true); diff --git a/sway/config.c b/sway/config.c index ee6c324c0..da4fac84a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -18,7 +18,7 @@ void config_defaults(struct sway_config *config) { // Flags config->focus_follows_mouse = true; config->mouse_warping = true; - config->reloading = false; + config->reloading = false; } struct sway_config *read_config(FILE *file, bool is_active) { @@ -26,7 +26,7 @@ struct sway_config *read_config(FILE *file, bool is_active) { config_defaults(config); if (is_active) { - config->reloading = true; + config->reloading = true; } bool success = true; @@ -49,7 +49,7 @@ struct sway_config *read_config(FILE *file, bool is_active) { if (!temp_depth && handle_command(config, line) != 0) { success = false; } - + _continue: if (line && line[strlen(line) - 1] == '{') { temp_depth++; @@ -61,7 +61,9 @@ _continue: exit(1); } - config->reloading = false; + if (is_active) { + config->reloading = true; + } return config; } From 7c02a1967b3d6345754b69a716459534bd2e1620 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 10 Aug 2015 14:09:51 -0500 Subject: [PATCH 3/5] Spaces to Tabs --- sway/commands.c | 10 +++++----- sway/commands.h | 4 ++-- sway/config.c | 14 +++++++------- sway/config.h | 30 +++++++++++++++--------------- sway/layout.h | 48 ++++++++++++++++++++++++------------------------ sway/list.h | 6 +++--- sway/log.c | 2 +- sway/log.h | 8 ++++---- 8 files changed, 61 insertions(+), 61 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 5ed5e0700..af24cc95c 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -79,10 +79,10 @@ int cmd_exec(struct sway_config *config, int argc, char **argv) { return 1; } - if (config->reloading) { - sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); - return 0; - } + if (config->reloading) { + sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); + return 0; + } if (fork() == 0) { char *args = join_args(argv, argc); @@ -190,7 +190,7 @@ int cmd_reload(struct sway_config *config, int argc, char **argv) { if (!f) { sway_log(L_ERROR, "Sway config file not found, aborting reload!"); free(temp); - return 1; + return 1; } free(temp); config = read_config(f, true); diff --git a/sway/commands.h b/sway/commands.h index f99a5201c..42b39f27e 100644 --- a/sway/commands.h +++ b/sway/commands.h @@ -4,8 +4,8 @@ #include "config.h" struct cmd_handler { - char *command; - int (*handle)(struct sway_config *config, int argc, char **argv); + char *command; + int (*handle)(struct sway_config *config, int argc, char **argv); }; int handle_command(struct sway_config *config, char *command); diff --git a/sway/config.c b/sway/config.c index da4fac84a..280900ca9 100644 --- a/sway/config.c +++ b/sway/config.c @@ -18,16 +18,16 @@ void config_defaults(struct sway_config *config) { // Flags config->focus_follows_mouse = true; config->mouse_warping = true; - config->reloading = false; + config->reloading = false; } struct sway_config *read_config(FILE *file, bool is_active) { struct sway_config *config = malloc(sizeof(struct sway_config)); config_defaults(config); - if (is_active) { - config->reloading = true; - } + if (is_active) { + config->reloading = true; + } bool success = true; @@ -61,9 +61,9 @@ _continue: exit(1); } - if (is_active) { - config->reloading = true; - } + if (is_active) { + config->reloading = true; + } return config; } diff --git a/sway/config.h b/sway/config.h index 6a50c4454..733aaaaef 100644 --- a/sway/config.h +++ b/sway/config.h @@ -6,31 +6,31 @@ #include "list.h" struct sway_variable { - char *name; - char *value; + char *name; + char *value; }; struct sway_binding { - list_t *keys; - uint32_t modifiers; - char *command; + list_t *keys; + uint32_t modifiers; + char *command; }; struct sway_mode { - char *name; - list_t *bindings; + char *name; + list_t *bindings; }; struct sway_config { - list_t *symbols; - list_t *modes; - struct sway_mode *current_mode; + list_t *symbols; + list_t *modes; + struct sway_mode *current_mode; - // Flags - bool focus_follows_mouse; - bool mouse_warping; - - bool reloading; + // Flags + bool focus_follows_mouse; + bool mouse_warping; + + bool reloading; }; struct sway_config *read_config(FILE *file, bool is_active); diff --git a/sway/layout.h b/sway/layout.h index b0aec42f0..3a8f89025 100644 --- a/sway/layout.h +++ b/sway/layout.h @@ -5,38 +5,38 @@ #include "list.h" struct sway_container { - wlc_handle handle; + wlc_handle handle; - enum { - C_ROOT, - C_OUTPUT, - C_WORKSPACE, - C_CONTAINER, - C_VIEW - } type; + enum { + C_ROOT, + C_OUTPUT, + C_WORKSPACE, + C_CONTAINER, + C_VIEW + } type; - enum { - L_NONE, - L_HORIZ, - L_VERT, - L_STACKED, - L_TABBED, - L_FLOATING - } layout; + enum { + L_NONE, + L_HORIZ, + L_VERT, + L_STACKED, + L_TABBED, + L_FLOATING + } layout; - // Not including borders or margins - int width, height; + // Not including borders or margins + int width, height; - int x, y; + int x, y; - int weight; + int weight; - char *name; + char *name; - list_t *children; + list_t *children; - struct sway_container *parent; - struct sway_container *focused; + struct sway_container *parent; + struct sway_container *focused; }; typedef struct sway_container swayc_t; diff --git a/sway/list.h b/sway/list.h index 93649fd5b..e035ec4c0 100644 --- a/sway/list.h +++ b/sway/list.h @@ -2,9 +2,9 @@ #define _SWAY_LIST_H typedef struct { - int capacity; - int length; - void **items; + int capacity; + int length; + void **items; } list_t; list_t *create_list(); diff --git a/sway/log.c b/sway/log.c index f439e3b28..188461eba 100644 --- a/sway/log.c +++ b/sway/log.c @@ -18,7 +18,7 @@ void init_log(int verbosity) { } void sway_log_colors(int mode) { - colored = (mode == 1) ? 1 : 0; + colored = (mode == 1) ? 1 : 0; } void sway_abort(char *format, ...) { diff --git a/sway/log.h b/sway/log.h index 038365133..e5075a39f 100644 --- a/sway/log.h +++ b/sway/log.h @@ -2,10 +2,10 @@ #define _SWAY_LOG_H typedef enum { - L_SILENT = 0, - L_ERROR = 1, - L_INFO = 2, - L_DEBUG = 3, + L_SILENT = 0, + L_ERROR = 1, + L_INFO = 2, + L_DEBUG = 3, } log_importance_t; void init_log(int verbosity); From 508980e3ab930fd1ea16cbb769771126110aa329 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 10 Aug 2015 14:22:22 -0500 Subject: [PATCH 4/5] Abstracted load_config --- sway/commands.c | 3 +++ sway/config.c | 19 +++++++++++++++++++ sway/config.h | 1 + sway/main.c | 22 +++------------------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index af24cc95c..98786885d 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -195,6 +195,9 @@ int cmd_reload(struct sway_config *config, int argc, char **argv) { free(temp); config = read_config(f, true); fclose(f); + if (load_config()) { + + } return 0; } diff --git a/sway/config.c b/sway/config.c index 280900ca9..3c7badec5 100644 --- a/sway/config.c +++ b/sway/config.c @@ -8,6 +8,25 @@ #include "commands.h" #include "config.h" +bool load_config() { + // TODO: Allow use of more config file locations + const char *name = "/.sway/config"; + const char *home = getenv("HOME"); + char *temp = malloc(strlen(home) + strlen(name) + 1); + strcpy(temp, home); + strcat(temp, name); + FILE *f = fopen(temp, "r"); + if (!f) { + fprintf(stderr, "Unable to open %s for reading", temp); + free(temp); + return false; + } + free(temp); + config = read_config(f, false); + fclose(f); + return true; +} + void config_defaults(struct sway_config *config) { config->symbols = create_list(); config->modes = create_list(); diff --git a/sway/config.h b/sway/config.h index 733aaaaef..62b65723e 100644 --- a/sway/config.h +++ b/sway/config.h @@ -33,6 +33,7 @@ struct sway_config { bool reloading; }; +bool load_config(); struct sway_config *read_config(FILE *file, bool is_active); char *do_var_replacement(struct sway_config *config, char *str); diff --git a/sway/main.c b/sway/main.c index 298e530d9..900e6e5dc 100644 --- a/sway/main.c +++ b/sway/main.c @@ -9,24 +9,6 @@ struct sway_config *config; -void load_config() { - // TODO: Allow use of more config file locations - const char *name = "/.sway/config"; - const char *home = getenv("HOME"); - char *temp = malloc(strlen(home) + strlen(name) + 1); - strcpy(temp, home); - strcat(temp, name); - FILE *f = fopen(temp, "r"); - if (!f) { - fprintf(stderr, "Unable to open %s for reading", temp); - free(temp); - exit(1); - } - free(temp); - config = read_config(f, false); - fclose(f); -} - int main(int argc, char **argv) { init_log(L_DEBUG); // TODO: Control this with command line arg init_layout(); @@ -61,7 +43,9 @@ int main(int argc, char **argv) { } setenv("DISPLAY", ":1", 1); - load_config(); + if (load_config()) { + exit(1); + } wlc_run(); return 0; From 88997bbfe65c20ff2bbc28f49ee2d84a79972d61 Mon Sep 17 00:00:00 2001 From: Luminarys Date: Mon, 10 Aug 2015 14:24:31 -0500 Subject: [PATCH 5/5] Minor fix --- sway/commands.c | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 98786885d..4db254b65 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -179,24 +179,8 @@ int cmd_reload(struct sway_config *config, int argc, char **argv) { sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc); return 1; } - - // TODO: Allow use of more config file locations - const char *name = "/.sway/config"; - const char *home = getenv("HOME"); - char *temp = malloc(strlen(home) + strlen(name) + 1); - strcpy(temp, home); - strcat(temp, name); - FILE *f = fopen(temp, "r"); - if (!f) { - sway_log(L_ERROR, "Sway config file not found, aborting reload!"); - free(temp); - return 1; - } - free(temp); - config = read_config(f, true); - fclose(f); - if (load_config()) { - + if (!load_config()) { + return 1; } return 0;