From 0f387483fd6b4ce40514578ab008bfabc84b015d Mon Sep 17 00:00:00 2001 From: Taiyu Date: Wed, 12 Aug 2015 20:59:43 -0700 Subject: moving stuff around --- sway/handlers.c | 49 +++++++++++++++++++++++++++++++++++++------------ sway/handlers.h | 17 +---------------- sway/main.c | 25 ------------------------- 3 files changed, 38 insertions(+), 53 deletions(-) diff --git a/sway/handlers.c b/sway/handlers.c index 78ca1363..fe7de75b 100644 --- a/sway/handlers.c +++ b/sway/handlers.c @@ -9,16 +9,16 @@ #include "commands.h" #include "handlers.h" -bool handle_output_created(wlc_handle output) { +static bool handle_output_created(wlc_handle output) { add_output(output); return true; } -void handle_output_destroyed(wlc_handle output) { +static void handle_output_destroyed(wlc_handle output) { destroy_output(output); } -void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { +static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) { sway_log(L_DEBUG, "Output %d resolution changed to %d x %d", output, to->w, to->h); swayc_t *c = get_swayc_for_handle(output, &root_container); if (!c) return; @@ -27,7 +27,7 @@ void handle_output_resolution_change(wlc_handle output, const struct wlc_size *f arrange_windows(&root_container, -1, -1); } -void handle_output_focused(wlc_handle output, bool focus) { +static void handle_output_focused(wlc_handle output, bool focus) { swayc_t *c = get_swayc_for_handle(output, &root_container); if (!c) return; if (focus) { @@ -36,27 +36,26 @@ void handle_output_focused(wlc_handle output, bool focus) { } } -bool handle_view_created(wlc_handle view) { +static bool handle_view_created(wlc_handle view) { add_view(view); return true; } -void handle_view_destroyed(wlc_handle view) { +static void handle_view_destroyed(wlc_handle view) { sway_log(L_DEBUG, "Destroying window %d", view); destroy_view(get_swayc_for_handle(view, &root_container)); - return true; } -void handle_view_focus(wlc_handle view, bool focus) { +static void handle_view_focus(wlc_handle view, bool focus) { return; } -void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry) { +static void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry) { // deny that shit } -bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers +static bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state) { enum { QSIZE = 32 }; static uint8_t head = 0; @@ -133,7 +132,7 @@ bool pointer_test(swayc_t *view, void *_origin) { struct wlc_origin mouse_origin; -bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) { +static bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) { mouse_origin = *origin; if (!config->focus_follows_mouse) { return true; @@ -148,7 +147,7 @@ bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_orig return true; } -bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, +static bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, uint32_t button, enum wlc_button_state state) { if (state == WLC_BUTTON_STATE_PRESSED) { swayc_t *c = find_container(&root_container, pointer_test, &mouse_origin); @@ -163,3 +162,29 @@ bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modi } return true; } + + +struct wlc_interface interface = { + .output = { + .created = handle_output_created, + .destroyed = handle_output_destroyed, + .resolution = handle_output_resolution_change, + .focus = handle_output_focused + }, + .view = { + .created = handle_view_created, + .destroyed = handle_view_destroyed, + .focus = handle_view_focus, + .request = { + .geometry = handle_view_geometry_request + } + }, + .keyboard = { + .key = handle_key + }, + .pointer = { + .motion = handle_pointer_motion, + .button = handle_pointer_button + } +}; + diff --git a/sway/handlers.h b/sway/handlers.h index 9792b6d7..798b3b50 100644 --- a/sway/handlers.h +++ b/sway/handlers.h @@ -4,21 +4,6 @@ #include #include -bool handle_output_created(wlc_handle output); -void handle_output_destroyed(wlc_handle output); -void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to); -void handle_output_focused(wlc_handle output, bool focus); - -bool handle_view_created(wlc_handle view); -void handle_view_destroyed(wlc_handle view); -void handle_view_focus(wlc_handle view, bool focus); -void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* geometry); - -bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers - *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state); - -bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin); -bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, - uint32_t button, enum wlc_button_state state); +extern struct wlc_interface interface; #endif diff --git a/sway/main.c b/sway/main.c index a7814364..7661551d 100644 --- a/sway/main.c +++ b/sway/main.c @@ -12,31 +12,6 @@ int main(int argc, char **argv) { init_log(L_DEBUG); // TODO: Control this with command line arg init_layout(); - static struct wlc_interface interface = { - .output = { - .created = handle_output_created, - .destroyed = handle_output_destroyed, - .resolution = handle_output_resolution_change, - .focus = handle_output_focused - }, - .view = { - .created = handle_view_created, - .destroyed = handle_view_destroyed, - .focus = handle_view_focus, - .request = { - .geometry = handle_view_geometry_request - } - }, - .keyboard = { - .key = handle_key - }, - .pointer = { - .motion = handle_pointer_motion, - .button = handle_pointer_button - } - - }; - setenv("WLC_DIM", "0", 0); if (!wlc_init(&interface, argc, argv)) { return 1; -- cgit v1.2.3-54-g00ecf From 65406cb61baed6ac24bcb6c307ed5a95922b2fc1 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Wed, 12 Aug 2015 21:06:09 -0700 Subject: safer forking, reduce duplicate code, cleanup zombie processes --- sway/commands.c | 74 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 870a2377..f4de72d8 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "stringop.h" #include "layout.h" @@ -29,6 +30,8 @@ struct modifier_key modifiers[] = { { "Mod5", WLC_BIT_MOD_MOD5 }, }; + + bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { if (argc < 2) { sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); @@ -73,44 +76,55 @@ bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_exec(struct sway_config *config, int argc, char **argv) { +static void cmd_exec_cleanup(int signal) { + while (waitpid((pid_t)-1, 0, WNOHANG) > 0){}; +} + +static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { + /* setup signal handler to cleanup dead proccesses */ + /* TODO: replace this with a function that has constructor attribute? */ + static bool cleanup = false; + if(cleanup == false) { + signal(SIGCHLD, cmd_exec_cleanup); + cleanup = true; + } + if (argc < 1) { sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc); return false; } - if (config->reloading) { - sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); - return true; + pid_t pid = fork(); + /* Failed to fork */ + if (pid < 0) { + sway_log(L_ERROR, "exec command failed, sway did not fork"); + return false; } - - if (fork() == 0) { + /* Child process */ + if (pid == 0) { char *args = join_args(argv, argc); sway_log(L_DEBUG, "Executing %s", args); execl("/bin/sh", "sh", "-c", args, (char *)NULL); + /* Execl doesnt return unless failure */ + sway_log(L_ERROR, "could not find /bin/sh"); free(args); - exit(0); + exit(-1); } + /* Parent */ return true; } -bool 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 false; - } - - if (fork() == 0) { +static bool cmd_exec(struct sway_config *config, int argc, char **argv) { + if (config->reloading) { char *args = join_args(argv, argc); - sway_log(L_DEBUG, "Executing %s", args); - execl("/bin/sh", "sh", "-c", args, (char *)NULL); + sway_log(L_DEBUG, "Ignoring exec %s due to reload", args); free(args); - exit(0); + return true; } - return true; + return cmd_exec_always(config, argc, argv); } -bool cmd_exit(struct sway_config *config, int argc, char **argv) { +static bool cmd_exit(struct sway_config *config, int argc, char **argv) { if (argc != 0) { sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc); return false; @@ -120,7 +134,7 @@ bool cmd_exit(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_focus(struct sway_config *config, int argc, char **argv) { +static bool cmd_focus(struct sway_config *config, int argc, char **argv) { if (argc != 1) { sway_log(L_ERROR, "Invalid focus command (expected 1 arguments, got %d)", argc); return false; @@ -139,7 +153,7 @@ bool cmd_focus(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { +static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { if (argc != 1) { sway_log(L_ERROR, "Invalid focus_follows_mouse command (expected 1 arguments, got %d)", argc); return false; @@ -149,7 +163,7 @@ bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) return true; } -bool cmd_layout(struct sway_config *config, int argc, char **argv) { +static bool cmd_layout(struct sway_config *config, int argc, char **argv) { if (argc < 1) { sway_log(L_ERROR, "Invalid layout command (expected at least 1 argument, got %d)", argc); return false; @@ -174,7 +188,7 @@ bool cmd_layout(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_reload(struct sway_config *config, int argc, char **argv) { +static bool cmd_reload(struct sway_config *config, int argc, char **argv) { if (argc != 0) { sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc); return false; @@ -186,7 +200,7 @@ bool cmd_reload(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_set(struct sway_config *config, int argc, char **argv) { +static bool 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); return false; @@ -200,7 +214,7 @@ bool cmd_set(struct sway_config *config, int argc, char **argv) { return true; } -bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { +static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { if (argc != 0) { sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc); return false; @@ -225,15 +239,15 @@ bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { return true; } -bool cmd_splitv(struct sway_config *config, int argc, char **argv) { +static bool cmd_splitv(struct sway_config *config, int argc, char **argv) { return _do_split(config, argc, argv, L_VERT); } -bool cmd_splith(struct sway_config *config, int argc, char **argv) { +static bool cmd_splith(struct sway_config *config, int argc, char **argv) { return _do_split(config, argc, argv, L_HORIZ); } -bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { +static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { if (argc != 1) { sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc); return false; @@ -248,7 +262,7 @@ bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { +static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { if (argc != 1) { sway_log(L_ERROR, "Invalid fullscreen command (expected 1 arguments, got %d)", argc); return false; @@ -262,7 +276,7 @@ bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { return true; } -bool cmd_workspace(struct sway_config *config, int argc, char **argv) { +static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { if (argc != 1) { sway_log(L_ERROR, "Invalid workspace command (expected 1 arguments, got %d)", argc); return false; -- cgit v1.2.3-54-g00ecf From dc9efcd79f7564e730abc6bc5923341e904b7688 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Wed, 12 Aug 2015 21:51:38 -0700 Subject: better error handling --- sway/commands.c | 84 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index f4de72d8..96245c8a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -30,13 +30,44 @@ struct modifier_key modifiers[] = { { "Mod5", WLC_BIT_MOD_MOD5 }, }; +enum expected_args { + EXPECTED_MORE_THEN, + EXPECTED_LESS_THEN, + EXPECTED_SAME_AS +}; +static bool checkarg(int argc, char *name, enum expected_args type, int val) { + switch(type) { + case EXPECTED_MORE_THEN: + if (argc > val) { + return true; + } + sway_log(L_ERROR, "Invalid %s command." + "(expected more then %d arguments, got %d", name, val, argc); + break; + case EXPECTED_LESS_THEN: + if (argc < val) { + return true; + }; + sway_log(L_ERROR, "Invalid %s command." + "(expected less then %d arguments, got %d", name, val, argc); + break; + case EXPECTED_SAME_AS: + if (argc == val) { + return true; + }; + sway_log(L_ERROR, "Invalid %s command." + "(expected %d arguments, got %d", name, val, argc); + break; + } + return false; +} -bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { - if (argc < 2) { - sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); + +static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { + if (checkarg(argc, "bindsym", EXPECTED_MORE_THEN, 1) == false) { return false; - } + }; struct sway_binding *binding = malloc(sizeof(struct sway_binding)); binding->keys = create_list(); @@ -89,8 +120,7 @@ static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { cleanup = true; } - if (argc < 1) { - sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc); + if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { return false; } @@ -125,8 +155,7 @@ static bool cmd_exec(struct sway_config *config, int argc, char **argv) { } static bool cmd_exit(struct sway_config *config, int argc, char **argv) { - if (argc != 0) { - sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc); + if (checkarg(argc, "exit", EXPECTED_SAME_AS, 0) == false) { return false; } // TODO: Some kind of clean up is probably in order @@ -135,8 +164,7 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { } static bool cmd_focus(struct sway_config *config, int argc, char **argv) { - if (argc != 1) { - sway_log(L_ERROR, "Invalid focus command (expected 1 arguments, got %d)", argc); + if (checkarg(argc, "focus", EXPECTED_SAME_AS, 1) == false) { return false; } if (strcasecmp(argv[0], "left") == 0) { @@ -154,8 +182,7 @@ static bool cmd_focus(struct sway_config *config, int argc, char **argv) { } static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { - if (argc != 1) { - sway_log(L_ERROR, "Invalid focus_follows_mouse command (expected 1 arguments, got %d)", argc); + if (checkarg(argc, "focus_follows_mouse", EXPECTED_SAME_AS, 1) == false) { return false; } @@ -164,8 +191,7 @@ static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char * } static bool cmd_layout(struct sway_config *config, int argc, char **argv) { - if (argc < 1) { - sway_log(L_ERROR, "Invalid layout command (expected at least 1 argument, got %d)", argc); + if (checkarg(argc, "layout", EXPECTED_MORE_THEN, 0) == false) { return false; } swayc_t *parent = get_focused_container(&root_container); @@ -189,8 +215,7 @@ static bool cmd_layout(struct sway_config *config, int argc, char **argv) { } static bool cmd_reload(struct sway_config *config, int argc, char **argv) { - if (argc != 0) { - sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc); + if (checkarg(argc, "reload", EXPECTED_SAME_AS, 0) == false) { return false; } if (!load_config()) { @@ -201,8 +226,7 @@ static bool cmd_reload(struct sway_config *config, int argc, char **argv) { } static bool 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); + if (checkarg(argc, "set", EXPECTED_SAME_AS, 2) == false) { return false; } struct sway_variable *var = malloc(sizeof(struct sway_variable)); @@ -215,8 +239,10 @@ static bool cmd_set(struct sway_config *config, int argc, char **argv) { } static bool _do_split(struct sway_config *config, int argc, char **argv, int layout) { - if (argc != 0) { - sway_log(L_ERROR, "Invalid splitv command (expected 0 arguments, got %d)", argc); + char *name = layout == L_VERT ? "splitv": + layout == L_HORIZ ? "splith": + "split"; + if (checkarg(argc, name, EXPECTED_SAME_AS, 0) == false) { return false; } swayc_t *focused = get_focused_container(&root_container); @@ -248,11 +274,9 @@ static bool cmd_splith(struct sway_config *config, int argc, char **argv) { } static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { - if (argc != 1) { - sway_log(L_ERROR, "Invalid log_colors command (expected 1 argument, got %d)", argc); + if (checkarg(argc, "log_colors", EXPECTED_SAME_AS, 1) == false) { return false; } - if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { sway_log(L_ERROR, "Invalid log_colors command (expected `yes` or `no`, got '%s')", argv[0]); return false; @@ -263,8 +287,7 @@ static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { } static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { - if (argc != 1) { - sway_log(L_ERROR, "Invalid fullscreen command (expected 1 arguments, got %d)", argc); + if (checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 1) == false) { return false; } @@ -277,8 +300,7 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { } static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { - if (argc != 1) { - sway_log(L_ERROR, "Invalid workspace command (expected 1 arguments, got %d)", argc); + if (checkarg(argc, "workspace", EXPECTED_SAME_AS, 1) == false) { return false; } @@ -292,7 +314,7 @@ static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { } /* Keep alphabetized */ -struct cmd_handler handlers[] = { +static struct cmd_handler handlers[] = { { "bindsym", cmd_bindsym }, { "exec", cmd_exec }, { "exec_always", cmd_exec_always }, @@ -309,7 +331,7 @@ struct cmd_handler handlers[] = { { "workspace", cmd_workspace } }; -char **split_directive(char *line, int *argc) { +static char **split_directive(char *line, int *argc) { const char *delimiters = " "; *argc = 0; while (isspace(*line) && *line) ++line; @@ -361,13 +383,13 @@ char **split_directive(char *line, int *argc) { return parts; } -int handler_compare(const void *_a, const void *_b) { +static int handler_compare(const void *_a, const void *_b) { const struct cmd_handler *a = _a; const struct cmd_handler *b = _b; return strcasecmp(a->command, b->command); } -struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { +static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { struct cmd_handler d = { .command=line }; struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare); return res; -- cgit v1.2.3-54-g00ecf From ac1c2d31bff37b0f0e1c3cce063121a1674f31c3 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Wed, 12 Aug 2015 22:58:15 -0700 Subject: no more output from programs called with exec, fixed focus return values --- sway/commands.c | 19 ++++++++++++++++++- sway/movement.c | 10 +++++----- sway/movement.h | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 96245c8a..7721c6fb 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "stringop.h" #include "layout.h" @@ -118,6 +119,22 @@ static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { if(cleanup == false) { signal(SIGCHLD, cmd_exec_cleanup); cleanup = true; + /* Set it so filedescriptors are closed for executed programs */ + int flag_out = fcntl(STDOUT_FILENO, F_GETFD); + int flag_in = fcntl(STDIN_FILENO, F_GETFD); + int flag_err = fcntl(STDERR_FILENO, F_GETFD); + if (flag_out != -1) { + flag_out |= FD_CLOEXEC; + fcntl(STDOUT_FILENO, F_SETFD, flag_out); + } + if (flag_in != -1) { + flag_in |= FD_CLOEXEC; + fcntl(STDIN_FILENO, F_SETFD, flag_in); + } + if (flag_err != -1) { + flag_err |= FD_CLOEXEC; + fcntl(STDERR_FILENO, F_SETFD, flag_err); + } } if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { @@ -287,7 +304,7 @@ static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { } static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 1) == false) { + if (checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 0) == false) { return false; } diff --git a/sway/movement.c b/sway/movement.c index a55d0350..166e6508 100644 --- a/sway/movement.c +++ b/sway/movement.c @@ -5,7 +5,7 @@ #include "layout.h" #include "movement.h" -int move_focus(enum movement_direction direction) { +bool move_focus(enum movement_direction direction) { swayc_t *current = get_focused_container(&root_container); swayc_t *parent = current->parent; @@ -14,12 +14,12 @@ int move_focus(enum movement_direction direction) { parent = parent->parent; if (parent->type == C_ROOT) { sway_log(L_DEBUG, "Focus cannot move to parent"); - return 1; + return false; } else { sway_log(L_DEBUG, "Moving focus away from %p", current); unfocus_all(parent); focus_view(parent); - return 0; + return true; } } @@ -56,7 +56,7 @@ int move_focus(enum movement_direction direction) { } else { unfocus_all(&root_container); focus_view(parent->children->items[desired]); - return 0; + return true; } } if (!can_move) { @@ -65,7 +65,7 @@ int move_focus(enum movement_direction direction) { parent = parent->parent; if (parent->type == C_ROOT) { // Nothing we can do - return 1; + return false; } } } diff --git a/sway/movement.h b/sway/movement.h index a527674c..dd701877 100644 --- a/sway/movement.h +++ b/sway/movement.h @@ -12,6 +12,6 @@ enum movement_direction { MOVE_PARENT }; -int move_focus(enum movement_direction direction); +bool move_focus(enum movement_direction direction); #endif -- cgit v1.2.3-54-g00ecf From f798e9bb0bc667d07283f32d1d83b6223d375a03 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Thu, 13 Aug 2015 00:24:03 -0700 Subject: moved fd modifying stuff to log.c --- sway/commands.c | 31 ++++++++----------------------- sway/log.c | 12 ++++++++++++ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 7721c6fb..edf9db7a 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -6,7 +6,6 @@ #include #include #include -#include #include #include "stringop.h" #include "layout.h" @@ -20,7 +19,7 @@ struct modifier_key { uint32_t mod; }; -struct modifier_key modifiers[] = { +static struct modifier_key modifiers[] = { { XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT }, { XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS }, { XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL }, @@ -38,20 +37,22 @@ enum expected_args { }; static bool checkarg(int argc, char *name, enum expected_args type, int val) { - switch(type) { + switch (type) { case EXPECTED_MORE_THEN: if (argc > val) { return true; } sway_log(L_ERROR, "Invalid %s command." - "(expected more then %d arguments, got %d", name, val, argc); + "(expected more then %d argument%s, got %d", + name, val, (char*[2]){"s", ""}[argc==1], argc); break; case EXPECTED_LESS_THEN: if (argc < val) { return true; }; sway_log(L_ERROR, "Invalid %s command." - "(expected less then %d arguments, got %d", name, val, argc); + "(expected less then %d argument%s, got %d", + name, val, (char*[2]){"s", ""}[argc==1], argc); break; case EXPECTED_SAME_AS: if (argc == val) { @@ -116,25 +117,9 @@ static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { /* setup signal handler to cleanup dead proccesses */ /* TODO: replace this with a function that has constructor attribute? */ static bool cleanup = false; - if(cleanup == false) { + if (cleanup == false) { signal(SIGCHLD, cmd_exec_cleanup); cleanup = true; - /* Set it so filedescriptors are closed for executed programs */ - int flag_out = fcntl(STDOUT_FILENO, F_GETFD); - int flag_in = fcntl(STDIN_FILENO, F_GETFD); - int flag_err = fcntl(STDERR_FILENO, F_GETFD); - if (flag_out != -1) { - flag_out |= FD_CLOEXEC; - fcntl(STDOUT_FILENO, F_SETFD, flag_out); - } - if (flag_in != -1) { - flag_in |= FD_CLOEXEC; - fcntl(STDIN_FILENO, F_SETFD, flag_in); - } - if (flag_err != -1) { - flag_err |= FD_CLOEXEC; - fcntl(STDERR_FILENO, F_SETFD, flag_err); - } } if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { @@ -448,7 +433,7 @@ bool handle_command(struct sway_config *config, char *exec) { sway_log(L_ERROR, "Command failed: %s", cmd); } } - if(ptr) { + if (ptr) { free(cmd); } return exec_success; diff --git a/sway/log.c b/sway/log.c index 188461eb..b9048b34 100644 --- a/sway/log.c +++ b/sway/log.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include int colored = 1; int v = 0; @@ -15,6 +17,16 @@ const char *verbosity_colors[] = { void init_log(int verbosity) { v = verbosity; + /* set FD_CLOEXEC flag to prevent programs called with exec to write into + * logs */ + int i, flag; + int fd[] = { STDOUT_FILENO, STDIN_FILENO, STDERR_FILENO }; + for (i = 0; i < 3; ++i) { + flag = fcntl(fd[i], F_GETFD); + if (flag != -1) { + fcntl(fd[i], F_SETFD, flag | FD_CLOEXEC); + } + } } void sway_log_colors(int mode) { -- cgit v1.2.3-54-g00ecf From 5df5b0098971e4d7e278bd7dfc8d5a611a97f467 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Thu, 13 Aug 2015 00:44:56 -0700 Subject: moved signal handling to main --- sway/commands.c | 13 ------------- sway/main.c | 12 ++++++++++++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index edf9db7a..e82462bf 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "stringop.h" #include "layout.h" @@ -109,19 +108,7 @@ static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { return true; } -static void cmd_exec_cleanup(int signal) { - while (waitpid((pid_t)-1, 0, WNOHANG) > 0){}; -} - static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { - /* setup signal handler to cleanup dead proccesses */ - /* TODO: replace this with a function that has constructor attribute? */ - static bool cleanup = false; - if (cleanup == false) { - signal(SIGCHLD, cmd_exec_cleanup); - cleanup = true; - } - if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { return false; } diff --git a/sway/main.c b/sway/main.c index 7661551d..7477b08c 100644 --- a/sway/main.c +++ b/sway/main.c @@ -2,16 +2,23 @@ #include #include #include +#include +#include #include "layout.h" #include "config.h" #include "log.h" #include "handlers.h" +static void sigchld_handle(int signal); int main(int argc, char **argv) { init_log(L_DEBUG); // TODO: Control this with command line arg init_layout(); + /* Signal handling */ + signal(SIGCHLD, sigchld_handle); + + setenv("WLC_DIM", "0", 0); if (!wlc_init(&interface, argc, argv)) { return 1; @@ -25,3 +32,8 @@ int main(int argc, char **argv) { wlc_run(); return 0; } + +static void sigchld_handle(int signal) { + (void) signal; + while (waitpid((pid_t)-1, 0, WNOHANG) > 0); +} -- cgit v1.2.3-54-g00ecf From ad21281540a0461c75d6a87c27c8e65c2dc6f002 Mon Sep 17 00:00:00 2001 From: Taiyu Date: Thu, 13 Aug 2015 07:48:03 -0700 Subject: style, spelling --- sway/commands.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index e82462bf..c2458278 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -30,14 +30,14 @@ static struct modifier_key modifiers[] = { }; enum expected_args { - EXPECTED_MORE_THEN, - EXPECTED_LESS_THEN, + EXPECTED_MORE_THAN, + EXPECTED_LESS_THAN, EXPECTED_SAME_AS }; static bool checkarg(int argc, char *name, enum expected_args type, int val) { switch (type) { - case EXPECTED_MORE_THEN: + case EXPECTED_MORE_THAN: if (argc > val) { return true; } @@ -45,7 +45,7 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) { "(expected more then %d argument%s, got %d", name, val, (char*[2]){"s", ""}[argc==1], argc); break; - case EXPECTED_LESS_THEN: + case EXPECTED_LESS_THAN: if (argc < val) { return true; }; @@ -66,7 +66,7 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) { static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "bindsym", EXPECTED_MORE_THEN, 1) == false) { + if (!checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1)) { return false; }; @@ -109,7 +109,7 @@ static bool cmd_bindsym(struct sway_config *config, int argc, char **argv) { } static bool cmd_exec_always(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "exec_always", EXPECTED_MORE_THEN, 0) == false) { + if (!checkarg(argc, "exec_always", EXPECTED_MORE_THAN, 0)) { return false; } @@ -144,7 +144,7 @@ static bool cmd_exec(struct sway_config *config, int argc, char **argv) { } static bool cmd_exit(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "exit", EXPECTED_SAME_AS, 0) == false) { + if (!checkarg(argc, "exit", EXPECTED_SAME_AS, 0)) { return false; } // TODO: Some kind of clean up is probably in order @@ -153,7 +153,7 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { } static bool cmd_focus(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "focus", EXPECTED_SAME_AS, 1) == false) { + if (!checkarg(argc, "focus", EXPECTED_SAME_AS, 1)) { return false; } if (strcasecmp(argv[0], "left") == 0) { @@ -171,7 +171,7 @@ static bool cmd_focus(struct sway_config *config, int argc, char **argv) { } static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "focus_follows_mouse", EXPECTED_SAME_AS, 1) == false) { + if (!checkarg(argc, "focus_follows_mouse", EXPECTED_SAME_AS, 1)) { return false; } @@ -180,7 +180,7 @@ static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char * } static bool cmd_layout(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "layout", EXPECTED_MORE_THEN, 0) == false) { + if (!checkarg(argc, "layout", EXPECTED_MORE_THAN, 0)) { return false; } swayc_t *parent = get_focused_container(&root_container); @@ -204,7 +204,7 @@ static bool cmd_layout(struct sway_config *config, int argc, char **argv) { } static bool cmd_reload(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "reload", EXPECTED_SAME_AS, 0) == false) { + if (!checkarg(argc, "reload", EXPECTED_SAME_AS, 0)) { return false; } if (!load_config()) { @@ -215,7 +215,7 @@ static bool cmd_reload(struct sway_config *config, int argc, char **argv) { } static bool cmd_set(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "set", EXPECTED_SAME_AS, 2) == false) { + if (!checkarg(argc, "set", EXPECTED_SAME_AS, 2)) { return false; } struct sway_variable *var = malloc(sizeof(struct sway_variable)); @@ -231,7 +231,7 @@ static bool _do_split(struct sway_config *config, int argc, char **argv, int lay char *name = layout == L_VERT ? "splitv": layout == L_HORIZ ? "splith": "split"; - if (checkarg(argc, name, EXPECTED_SAME_AS, 0) == false) { + if (!checkarg(argc, name, EXPECTED_SAME_AS, 0)) { return false; } swayc_t *focused = get_focused_container(&root_container); @@ -263,7 +263,7 @@ static bool cmd_splith(struct sway_config *config, int argc, char **argv) { } static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "log_colors", EXPECTED_SAME_AS, 1) == false) { + if (!checkarg(argc, "log_colors", EXPECTED_SAME_AS, 1)) { return false; } if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { @@ -276,7 +276,7 @@ static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { } static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 0) == false) { + if (!checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 0)) { return false; } @@ -289,7 +289,7 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { } static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { - if (checkarg(argc, "workspace", EXPECTED_SAME_AS, 1) == false) { + if (!checkarg(argc, "workspace", EXPECTED_SAME_AS, 1)) { return false; } -- cgit v1.2.3-54-g00ecf From 0a205776d7023cb8dea0d62994dff7b01d19dbcf Mon Sep 17 00:00:00 2001 From: Taiyu Date: Thu, 13 Aug 2015 07:50:46 -0700 Subject: small change --- sway/commands.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index c2458278..da33c5de 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -32,7 +32,7 @@ static struct modifier_key modifiers[] = { enum expected_args { EXPECTED_MORE_THAN, EXPECTED_LESS_THAN, - EXPECTED_SAME_AS + EXPECTED_EQUAL_TO }; static bool checkarg(int argc, char *name, enum expected_args type, int val) { @@ -53,7 +53,7 @@ static bool checkarg(int argc, char *name, enum expected_args type, int val) { "(expected less then %d argument%s, got %d", name, val, (char*[2]){"s", ""}[argc==1], argc); break; - case EXPECTED_SAME_AS: + case EXPECTED_EQUAL_TO: if (argc == val) { return true; }; @@ -144,7 +144,7 @@ static bool cmd_exec(struct sway_config *config, int argc, char **argv) { } static bool cmd_exit(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "exit", EXPECTED_SAME_AS, 0)) { + if (!checkarg(argc, "exit", EXPECTED_EQUAL_TO, 0)) { return false; } // TODO: Some kind of clean up is probably in order @@ -153,7 +153,7 @@ static bool cmd_exit(struct sway_config *config, int argc, char **argv) { } static bool cmd_focus(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "focus", EXPECTED_SAME_AS, 1)) { + if (!checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1)) { return false; } if (strcasecmp(argv[0], "left") == 0) { @@ -171,7 +171,7 @@ static bool cmd_focus(struct sway_config *config, int argc, char **argv) { } static bool cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "focus_follows_mouse", EXPECTED_SAME_AS, 1)) { + if (!checkarg(argc, "focus_follows_mouse", EXPECTED_EQUAL_TO, 1)) { return false; } @@ -204,7 +204,7 @@ static bool cmd_layout(struct sway_config *config, int argc, char **argv) { } static bool cmd_reload(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "reload", EXPECTED_SAME_AS, 0)) { + if (!checkarg(argc, "reload", EXPECTED_EQUAL_TO, 0)) { return false; } if (!load_config()) { @@ -215,7 +215,7 @@ static bool cmd_reload(struct sway_config *config, int argc, char **argv) { } static bool cmd_set(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "set", EXPECTED_SAME_AS, 2)) { + if (!checkarg(argc, "set", EXPECTED_EQUAL_TO, 2)) { return false; } struct sway_variable *var = malloc(sizeof(struct sway_variable)); @@ -231,7 +231,7 @@ static bool _do_split(struct sway_config *config, int argc, char **argv, int lay char *name = layout == L_VERT ? "splitv": layout == L_HORIZ ? "splith": "split"; - if (!checkarg(argc, name, EXPECTED_SAME_AS, 0)) { + if (!checkarg(argc, name, EXPECTED_EQUAL_TO, 0)) { return false; } swayc_t *focused = get_focused_container(&root_container); @@ -263,7 +263,7 @@ static bool cmd_splith(struct sway_config *config, int argc, char **argv) { } static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "log_colors", EXPECTED_SAME_AS, 1)) { + if (!checkarg(argc, "log_colors", EXPECTED_EQUAL_TO, 1)) { return false; } if (strcasecmp(argv[0], "no") != 0 && strcasecmp(argv[0], "yes") != 0) { @@ -276,7 +276,7 @@ static bool cmd_log_colors(struct sway_config *config, int argc, char **argv) { } static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "fullscreen", EXPECTED_SAME_AS, 0)) { + if (!checkarg(argc, "fullscreen", EXPECTED_EQUAL_TO, 0)) { return false; } @@ -289,7 +289,7 @@ static bool cmd_fullscreen(struct sway_config *config, int argc, char **argv) { } static bool cmd_workspace(struct sway_config *config, int argc, char **argv) { - if (!checkarg(argc, "workspace", EXPECTED_SAME_AS, 1)) { + if (!checkarg(argc, "workspace", EXPECTED_EQUAL_TO, 1)) { return false; } -- cgit v1.2.3-54-g00ecf