From 2445d279604d7be38c00db60ffde4279a3c75459 Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Sun, 2 Apr 2017 14:38:33 -0600 Subject: Impliment i3-style marks This commit adds three commands to sway: `show_marks`, `mark` and `unmark`. Marks are displayed right-aligned in the window border as i3 does. Marks may be found using criteria. Fixes #1007 --- sway/commands/mark.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ sway/commands/show_marks.c | 13 ++++++++ sway/commands/unmark.c | 31 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 sway/commands/mark.c create mode 100644 sway/commands/show_marks.c create mode 100644 sway/commands/unmark.c (limited to 'sway/commands') diff --git a/sway/commands/mark.c b/sway/commands/mark.c new file mode 100644 index 00000000..68a84af7 --- /dev/null +++ b/sway/commands/mark.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include "sway/commands.h" +#include "list.h" +#include "stringop.h" + +struct cmd_results *cmd_mark(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); + if ((error = checkarg(argc, "floating", EXPECTED_AT_LEAST, 1))) { + return error; + } + + swayc_t *view = get_focused_container(&root_container); + bool add = false; + bool toggle = false; + + if (strcmp(argv[0], "--add") == 0) { + --argc; ++argv; + add = true; + } else if (strcmp(argv[0], "--replace") == 0) { + --argc; ++argv; + } + + if (argc && strcmp(argv[0], "--toggle") == 0) { + --argc; ++argv; + toggle = true; + } + + if (argc) { + char *mark = join_args(argv, argc); + if (view->marks) { + if (add) { + int index; + if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { + if (toggle) { + free(view->marks->items[index]); + list_del(view->marks, index); + + if (0 == view->marks->length) { + list_free(view->marks); + view->marks = NULL; + } + } + free(mark); + } else { + list_add(view->marks, mark); + } + } else { + if (toggle && list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark) != -1) { + // Delete the list + list_foreach(view->marks, free); + list_free(view->marks); + view->marks = NULL; + } else { + // Delete and replace with a new list + list_foreach(view->marks, free); + list_free(view->marks); + + view->marks = create_list(); + list_add(view->marks, mark); + } + } + } else { + view->marks = create_list(); + list_add(view->marks, mark); + } + } else { + return cmd_results_new(CMD_FAILURE, "mark", + "Expected 'mark [--add|--replace] [--toggle] '"); + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/show_marks.c b/sway/commands/show_marks.c new file mode 100644 index 00000000..ed56d9e5 --- /dev/null +++ b/sway/commands/show_marks.c @@ -0,0 +1,13 @@ +#include +#include +#include "sway/commands.h" + +struct cmd_results *cmd_show_marks(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "show_marks", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + config->show_marks = !strcasecmp(argv[0], "on"); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c new file mode 100644 index 00000000..34a2ae44 --- /dev/null +++ b/sway/commands/unmark.c @@ -0,0 +1,31 @@ +#include +#include +#include "sway/commands.h" +#include "list.h" +#include "stringop.h" + +struct cmd_results *cmd_unmark(int argc, char **argv) { + swayc_t *view = get_focused_container(&root_container); + + if (view->marks) { + if (argc) { + char *mark = join_args(argv, argc); + int index; + if ((index = list_seq_find(view->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1) { + free(view->marks->items[index]); + list_del(view->marks, index); + + if (view->marks->length == 0) { + list_free(view->marks); + view->marks = NULL; + } + } + free(mark); + } else { + list_foreach(view->marks, free); + list_free(view->marks); + view->marks = NULL; + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} -- cgit v1.2.3 From 069d37f987c4e323cdb9396f0d80ac83d00566ff Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Tue, 4 Apr 2017 21:20:27 -0600 Subject: Improve criteria handling This commit changes how commands decide what container to act on. Commands get the current container though `current_container`, a global defined in sway/commands.c. If a criteria is given before a command, then the following command will be run once for every container the criteria matches with a reference to the matching container in 'current_container'. Commands should use this instead of `get_focused_container()` from now on. This commit also fixes a few (minor) mistakes made in implementing marks such as non-escaped arrows in sway(5) and calling the "mark" command "floating" by accident. It also cleans up `criteria.c` in a few places. --- sway/commands/border.c | 2 +- sway/commands/floating.c | 2 +- sway/commands/focus.c | 3 +++ sway/commands/fullscreen.c | 2 +- sway/commands/kill.c | 2 +- sway/commands/layout.c | 2 +- sway/commands/mark.c | 4 ++-- sway/commands/move.c | 4 ++-- sway/commands/resize.c | 12 ++++++------ sway/commands/split.c | 6 +++--- sway/commands/unmark.c | 2 +- 11 files changed, 22 insertions(+), 19 deletions(-) (limited to 'sway/commands') diff --git a/sway/commands/border.c b/sway/commands/border.c index 0211e40c..c888622e 100644 --- a/sway/commands/border.c +++ b/sway/commands/border.c @@ -20,7 +20,7 @@ struct cmd_results *cmd_border(int argc, char **argv) { "Expected 'border []"); } - swayc_t *view = get_focused_view(&root_container); + swayc_t *view = current_container; enum swayc_border_types border = view->border_type; int thickness = view->border_thickness; diff --git a/sway/commands/floating.c b/sway/commands/floating.c index 113c8b71..ccfde532 100644 --- a/sway/commands/floating.c +++ b/sway/commands/floating.c @@ -13,7 +13,7 @@ struct cmd_results *cmd_floating(int argc, char **argv) { if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) { return error; } - swayc_t *view = get_focused_container(&root_container); + swayc_t *view = current_container; bool wants_floating; if (strcasecmp(argv[0], "enable") == 0) { wants_floating = true; diff --git a/sway/commands/focus.c b/sway/commands/focus.c index 12c5d02c..defaba29 100644 --- a/sway/commands/focus.c +++ b/sway/commands/focus.c @@ -30,6 +30,9 @@ struct cmd_results *cmd_focus(int argc, char **argv) { } } return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } else if (argc == 0) { + set_focused_container(current_container); + return cmd_results_new(CMD_SUCCESS, NULL, NULL); } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) { return error; } diff --git a/sway/commands/fullscreen.c b/sway/commands/fullscreen.c index 321d6f59..bfff82f9 100644 --- a/sway/commands/fullscreen.c +++ b/sway/commands/fullscreen.c @@ -14,7 +14,7 @@ struct cmd_results *cmd_fullscreen(int argc, char **argv) { if ((error = checkarg(argc, "fullscreen", EXPECTED_AT_LEAST, 0))) { return error; } - swayc_t *container = get_focused_view(&root_container); + swayc_t *container = current_container; if(container->type != C_VIEW){ return cmd_results_new(CMD_INVALID, "fullscreen", "Only views can fullscreen"); } diff --git a/sway/commands/kill.c b/sway/commands/kill.c index 2e94fb10..742e2b86 100644 --- a/sway/commands/kill.c +++ b/sway/commands/kill.c @@ -6,7 +6,7 @@ struct cmd_results *cmd_kill(int argc, char **argv) { if (config->reading) return cmd_results_new(CMD_FAILURE, "kill", "Can't be used in config file."); if (!config->active) return cmd_results_new(CMD_FAILURE, "kill", "Can only be used when sway is running."); - swayc_t *container = get_focused_container(&root_container); + swayc_t *container = current_container; close_views(container); return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 570cd207..40ebd590 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -16,7 +16,7 @@ struct cmd_results *cmd_layout(int argc, char **argv) { if ((error = checkarg(argc, "layout", EXPECTED_MORE_THAN, 0))) { return error; } - swayc_t *parent = get_focused_container(&root_container); + swayc_t *parent = current_container; if (parent->is_floating) { return cmd_results_new(CMD_FAILURE, "layout", "Unable to change layout of floating windows"); } diff --git a/sway/commands/mark.c b/sway/commands/mark.c index 68a84af7..919883b0 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -8,11 +8,11 @@ struct cmd_results *cmd_mark(int argc, char **argv) { struct cmd_results *error = NULL; if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); - if ((error = checkarg(argc, "floating", EXPECTED_AT_LEAST, 1))) { + if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) { return error; } - swayc_t *view = get_focused_container(&root_container); + swayc_t *view = current_container; bool add = false; bool toggle = false; diff --git a/sway/commands/move.c b/sway/commands/move.c index 97e10f10..3c47cfe7 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -20,7 +20,7 @@ struct cmd_results *cmd_move(int argc, char **argv) { "'move to workspace ' or " "'move to output ' or " "'move position mouse'"; - swayc_t *view = get_focused_container(&root_container); + swayc_t *view = current_container; if (argc == 2 || (argc == 3 && strcasecmp(argv[2], "px") == 0 )) { char *inv; @@ -125,7 +125,7 @@ struct cmd_results *cmd_move(int argc, char **argv) { if (view->type != C_CONTAINER && view->type != C_VIEW) { return cmd_results_new(CMD_FAILURE, "move scratchpad", "Can only move containers and views."); } - swayc_t *view = get_focused_container(&root_container); + swayc_t *view = current_container; int i; for (i = 0; i < scratchpad->length; i++) { if (scratchpad->items[i] == view) { diff --git a/sway/commands/resize.c b/sway/commands/resize.c index 61af080c..ef52bb07 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -19,7 +19,7 @@ enum resize_dim_types { }; static bool set_size_floating(int new_dimension, bool use_width) { - swayc_t *view = get_focused_float(swayc_active_workspace()); + swayc_t *view = current_container; if (view) { if (use_width) { int current_width = view->width; @@ -50,7 +50,7 @@ static bool set_size_floating(int new_dimension, bool use_width) { } static bool resize_floating(int amount, bool use_width) { - swayc_t *view = get_focused_float(swayc_active_workspace()); + swayc_t *view = current_container; if (view) { if (use_width) { @@ -64,7 +64,7 @@ static bool resize_floating(int amount, bool use_width) { } static bool resize_tiled(int amount, bool use_width) { - swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *container = current_container; swayc_t *parent = container->parent; int idx_focused = 0; bool use_major = false; @@ -199,7 +199,7 @@ static bool resize_tiled(int amount, bool use_width) { static bool set_size_tiled(int amount, bool use_width) { int desired; - swayc_t *focused = get_focused_view(swayc_active_workspace()); + swayc_t *focused = current_container; if (use_width) { desired = amount - focused->width; @@ -211,7 +211,7 @@ static bool set_size_tiled(int amount, bool use_width) { } static bool set_size(int dimension, bool use_width) { - swayc_t *focused = get_focused_view_include_floating(swayc_active_workspace()); + swayc_t *focused = current_container; if (focused) { if (focused->is_floating) { @@ -225,7 +225,7 @@ static bool set_size(int dimension, bool use_width) { } static bool resize(int dimension, bool use_width, enum resize_dim_types dim_type) { - swayc_t *focused = get_focused_view_include_floating(swayc_active_workspace()); + swayc_t *focused = current_container; // translate "10 ppt" (10%) to appropriate # of pixels in case we need it float ppt_dim = (float)dimension / 100; diff --git a/sway/commands/split.c b/sway/commands/split.c index e7da93d7..e3045a4f 100644 --- a/sway/commands/split.c +++ b/sway/commands/split.c @@ -17,7 +17,7 @@ static struct cmd_results *_do_split(int argc, char **argv, int layout) { if ((error = checkarg(argc, name, EXPECTED_EQUAL_TO, 0))) { return error; } - swayc_t *focused = get_focused_container(&root_container); + swayc_t *focused = current_container; // Case of floating window, don't split if (focused->is_floating) { @@ -66,7 +66,7 @@ struct cmd_results *cmd_split(int argc, char **argv) { } else if (strcasecmp(argv[0], "h") == 0 || strcasecmp(argv[0], "horizontal") == 0) { _do_split(argc - 1, argv + 1, L_HORIZ); } else if (strcasecmp(argv[0], "t") == 0 || strcasecmp(argv[0], "toggle") == 0) { - swayc_t *focused = get_focused_container(&root_container); + swayc_t *focused = current_container; if (focused->parent->layout == L_VERT) { _do_split(argc - 1, argv + 1, L_HORIZ); } else { @@ -89,7 +89,7 @@ struct cmd_results *cmd_splith(int argc, char **argv) { } struct cmd_results *cmd_splitt(int argc, char **argv) { - swayc_t *focused = get_focused_container(&root_container); + swayc_t *focused = current_container; if (focused->parent->layout == L_VERT) { return _do_split(argc, argv, L_HORIZ); } else { diff --git a/sway/commands/unmark.c b/sway/commands/unmark.c index 34a2ae44..ac213261 100644 --- a/sway/commands/unmark.c +++ b/sway/commands/unmark.c @@ -5,7 +5,7 @@ #include "stringop.h" struct cmd_results *cmd_unmark(int argc, char **argv) { - swayc_t *view = get_focused_container(&root_container); + swayc_t *view = current_container; if (view->marks) { if (argc) { -- cgit v1.2.3 From 154c6718c1f0e34e0f217150ba2770ee100e5b38 Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Fri, 7 Apr 2017 11:37:51 -0600 Subject: Add `-t get_marks` and use more i3-like marks In i3 every mark is unique and one mark cannot be used in more than one window, sway behavior has been amended to match this. `swaymsg -t get_marks` will now return an array of all marks used in sway. See #98 --- sway/commands/mark.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'sway/commands') diff --git a/sway/commands/mark.c b/sway/commands/mark.c index 919883b0..c1d959df 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -5,6 +5,15 @@ #include "list.h" #include "stringop.h" +static void find_marks_callback(swayc_t *container, void *_mark) { + char *mark = (char *)_mark; + + int index; + if (container->marks && ((index = list_seq_find(container->marks, (int (*)(const void *, const void *))strcmp, mark)) != -1)) { + list_del(container->marks, index); + } +} + struct cmd_results *cmd_mark(int argc, char **argv) { struct cmd_results *error = NULL; if (config->reading) return cmd_results_new(CMD_FAILURE, "mark", "Can't be used in config file."); @@ -30,6 +39,10 @@ struct cmd_results *cmd_mark(int argc, char **argv) { if (argc) { char *mark = join_args(argv, argc); + + // Remove all existing marks of this type + container_map(&root_container, find_marks_callback, mark); + if (view->marks) { if (add) { int index; -- cgit v1.2.3