diff --git a/.travis.yml b/.travis.yml index a8e292ba..2bb17d40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,7 @@ arch: - cairo - gdk-pixbuf2 - wlc-git + - libcap script: - "cmake ." - "make" diff --git a/CMake/FindLibcap.cmake b/CMake/FindLibcap.cmake new file mode 100644 index 00000000..b34e5e37 --- /dev/null +++ b/CMake/FindLibcap.cmake @@ -0,0 +1,56 @@ +#.rst: +# FindLibcap +# ------- +# +# Find Libcap library +# +# Try to find Libcap library. The following values are defined +# +# :: +# +# Libcap_FOUND - True if Libcap is available +# Libcap_INCLUDE_DIRS - Include directories for Libcap +# Libcap_LIBRARIES - List of libraries for Libcap +# Libcap_DEFINITIONS - List of definitions for Libcap +# +# and also the following more fine grained variables +# +# :: +# +# Libcap_VERSION +# Libcap_VERSION_MAJOR +# Libcap_VERSION_MINOR +# +#============================================================================= +# Copyright (c) 2017 Jerzi Kaminsky +# +# Distributed under the OSI-approved BSD License (the "License"); +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= + +include(FeatureSummary) +set_package_properties(Libcap PROPERTIES + URL "https://www.kernel.org/pub/linux/libs/security/linux-privs/libcap2" + DESCRIPTION "Library for getting and setting POSIX.1e capabilities") + +find_package(PkgConfig) +pkg_check_modules(PC_CAP QUIET Libcap) +find_library(Libcap_LIBRARIES NAMES cap HINTS ${PC_CAP_LIBRARY_DIRS}) +find_path(Libcap_INCLUDE_DIRS sys/capability.h HINTS ${PC_CAP_INCLUDE_DIRS}) + +set(Libcap_VERSION ${PC_CAP_VERSION}) +string(REPLACE "." ";" VERSION_LIST "${PC_CAP_VERSION}") + +LIST(LENGTH VERSION_LIST n) +if (n EQUAL 2) + list(GET VERSION_LIST 0 Libcap_VERSION_MAJOR) + list(GET VERSION_LIST 1 Libcap_VERSION_MINOR) +endif () + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Libcap DEFAULT_MSG Libcap_INCLUDE_DIRS Libcap_LIBRARIES) +mark_as_advanced(Libcap_INCLUDE_DIRS Libcap_LIBRARIES Libcap_DEFINITIONS + Libcap_VERSION Libcap_VERSION_MAJOR Libcap_VERSION_MICRO Libcap_VERSION_MINOR) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec2c73a2..017b0994 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,6 +66,7 @@ find_package(GdkPixbuf) find_package(PAM) find_package(LibInput REQUIRED) +find_package(Libcap REQUIRED) if (CMAKE_SYSTEM_NAME STREQUAL FreeBSD) find_package(EpollShim REQUIRED) diff --git a/include/sway/commands.h b/include/sway/commands.h index 511bee4d..91f2ae01 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -5,6 +5,9 @@ #include #include "config.h" +// Container that a called command should act upon. Only valid in command functions. +extern swayc_t *current_container; + /** * Indicates the result of a command's execution. */ @@ -126,6 +129,7 @@ sway_cmd cmd_ipc; sway_cmd cmd_kill; sway_cmd cmd_layout; sway_cmd cmd_log_colors; +sway_cmd cmd_mark; sway_cmd cmd_mode; sway_cmd cmd_mouse_warping; sway_cmd cmd_move; @@ -140,12 +144,14 @@ sway_cmd cmd_resize; sway_cmd cmd_scratchpad; sway_cmd cmd_seamless_mouse; sway_cmd cmd_set; +sway_cmd cmd_show_marks; sway_cmd cmd_smart_gaps; sway_cmd cmd_split; sway_cmd cmd_splith; sway_cmd cmd_splitt; sway_cmd cmd_splitv; sway_cmd cmd_sticky; +sway_cmd cmd_unmark; sway_cmd cmd_workspace; sway_cmd cmd_ws_auto_back_and_forth; sway_cmd cmd_workspace_layout; diff --git a/include/sway/config.h b/include/sway/config.h index d77fbd51..2de90434 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -275,6 +275,7 @@ struct sway_config { bool reading; bool auto_back_and_forth; bool seamless_mouse; + bool show_marks; bool edge_gaps; bool smart_gaps; diff --git a/include/sway/container.h b/include/sway/container.h index 46925589..37192ce3 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -165,6 +165,11 @@ struct sway_container { * Number of slave groups (e.g. columns) in auto layouts. */ size_t nb_slave_groups; + + /** + * Marks applied to the container, list_t of char*. + */ + list_t *marks; }; enum visibility_mask { diff --git a/include/sway/criteria.h b/include/sway/criteria.h index 5c71d172..022c48a8 100644 --- a/include/sway/criteria.h +++ b/include/sway/criteria.h @@ -33,4 +33,7 @@ char *extract_crit_tokens(list_t *tokens, const char *criteria); // been set with `for_window` commands and have an associated cmdlist. list_t *criteria_for(swayc_t *cont); +// Returns a list of all containers that match the given list of tokens. +list_t *container_for(list_t *tokens); + #endif diff --git a/sway/border.c b/sway/border.c index d79029a9..10ad92c2 100644 --- a/sway/border.c +++ b/sway/border.c @@ -1,8 +1,11 @@ +#define _XOPEN_SOURCE 500 #include #include #include #include #include +#include +#include #include #include "sway/border.h" #include "sway/container.h" @@ -190,6 +193,26 @@ static void render_title_bar(swayc_t *view, cairo_t *cr, struct wlc_geometry *b, cairo_set_source_u32(cr, colors->text); pango_printf(cr, config->font, 1, false, "%s", view->name); } + // Marks + if (config->show_marks && view->marks) { + int total_len = 0; + + for(int i = view->marks->length - 1; i >= 0; --i) { + char *mark = (char *)view->marks->items[i]; + if (*mark != '_') { + int width, height; + get_text_size(cr, config->font, &width, &height, 1, false, "[%s]", mark); + total_len += width; + if ((int)tb->size.w + x - (total_len + 2) < x + 2) { + break; + } else { + cairo_move_to(cr, (int)tb->size.w + x - (total_len + 2), y + 2); + cairo_set_source_u32(cr, colors->text); + pango_printf(cr, config->font, 1, false, "[%s]", mark); + } + } + } + } // titlebars has a border all around for tabbed layouts if (view->parent->layout == L_TABBED) { diff --git a/sway/commands.c b/sway/commands.c index c330ebee..17c7d717 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -43,6 +43,8 @@ struct cmd_handler { int sp_index = 0; +swayc_t *current_container = NULL; + // Returns error object, or NULL if check succeeds. struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { struct cmd_results *error = NULL; @@ -190,6 +192,7 @@ static struct cmd_handler handlers[] = { { "kill", cmd_kill }, { "layout", cmd_layout }, { "log_colors", cmd_log_colors }, + { "mark", cmd_mark }, { "mode", cmd_mode }, { "mouse_warping", cmd_mouse_warping }, { "move", cmd_move }, @@ -203,12 +206,14 @@ static struct cmd_handler handlers[] = { { "scratchpad", cmd_scratchpad }, { "seamless_mouse", cmd_seamless_mouse }, { "set", cmd_set }, + { "show_marks", cmd_show_marks }, { "smart_gaps", cmd_smart_gaps }, { "split", cmd_split }, { "splith", cmd_splith }, { "splitt", cmd_splitt }, { "splitv", cmd_splitv }, { "sticky", cmd_sticky }, + { "unmark", cmd_unmark }, { "workspace", cmd_workspace }, { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, { "workspace_layout", cmd_workspace_layout }, @@ -368,42 +373,37 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { char *head = exec; char *cmdlist; char *cmd; - char *criteria __attribute__((unused)); + list_t *containers = NULL; head = exec; do { // Extract criteria (valid for this command list only). - criteria = NULL; if (*head == '[') { ++head; - criteria = argsep(&head, "]"); + char *criteria_string = argsep(&head, "]"); if (head) { ++head; - // TODO handle criteria + list_t *tokens = create_list(); + char *error; + + if ((error = extract_crit_tokens(tokens, criteria_string))) { + results = cmd_results_new(CMD_INVALID, criteria_string, + "Can't parse criteria string: %s", error); + free(error); + free(tokens); + goto cleanup; + } + containers = container_for(tokens); + + free(tokens); } else { if (!results) { - results = cmd_results_new(CMD_INVALID, criteria, "Unmatched ["); + results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched ["); } goto cleanup; } // Skip leading whitespace head += strspn(head, whitespace); - - // TODO: it will yield unexpected results to execute commands - // (on any view) that where meant for certain views only. - if (!results) { - int len = strlen(criteria) + strlen(head) + 4; - char *tmp = malloc(len); - if (tmp) { - snprintf(tmp, len, "[%s] %s", criteria, head); - } else { - sway_log(L_DEBUG, "Unable to allocate criteria string for cmd result"); - } - results = cmd_results_new(CMD_INVALID, tmp, - "Can't handle criteria string: Refusing to execute command"); - free(tmp); - } - goto cleanup; } // Split command list cmdlist = argsep(&head, ";"); @@ -447,21 +447,43 @@ struct cmd_results *handle_command(char *_exec, enum command_context context) { free_argv(argc, argv); goto cleanup; } - struct cmd_results *res = handler->handle(argc-1, argv+1); - if (res->status != CMD_SUCCESS) { - free_argv(argc, argv); - if (results) { - free_cmd_results(results); + int i = 0; + do { + if (!containers) { + current_container = get_focused_container(&root_container); + } else if (containers->length == 0) { + break; + } else { + current_container = (swayc_t *)containers->items[i]; } - results = res; - goto cleanup; - } + sway_log(L_INFO, "Running on container '%s'", current_container->name); + + struct cmd_results *res = handler->handle(argc-1, argv+1); + if (res->status != CMD_SUCCESS) { + free_argv(argc, argv); + if (results) { + free_cmd_results(results); + } + results = res; + goto cleanup; + } + free_cmd_results(res); + ++i; + } while(containers && i < containers->length); + free_argv(argc, argv); - free_cmd_results(res); } while(cmdlist); + + if (containers) { + list_free(containers); + containers = NULL; + } } while(head); cleanup: free(exec); + if (containers) { + free(containers); + } if (!results) { results = cmd_results_new(CMD_SUCCESS, NULL, NULL); } 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 new file mode 100644 index 00000000..c1d959df --- /dev/null +++ b/sway/commands/mark.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include "sway/commands.h" +#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."); + if ((error = checkarg(argc, "mark", EXPECTED_AT_LEAST, 1))) { + return error; + } + + swayc_t *view = current_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); + + // Remove all existing marks of this type + container_map(&root_container, find_marks_callback, mark); + + 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/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/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/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 new file mode 100644 index 00000000..ac213261 --- /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 = current_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); +} diff --git a/sway/config.c b/sway/config.c index 46faf643..c8432a2a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -329,6 +329,7 @@ static void config_defaults(struct sway_config *config) { config->auto_back_and_forth = false; config->seamless_mouse = true; config->reading = false; + config->show_marks = true; config->edge_gaps = true; config->smart_gaps = false; diff --git a/sway/container.c b/sway/container.c index 707aa4d8..08aa77a8 100644 --- a/sway/container.c +++ b/sway/container.c @@ -61,6 +61,10 @@ static void free_swayc(swayc_t *cont) { } list_free(cont->floating); } + if (cont->marks) { + list_foreach(cont->marks, free); + list_free(cont->marks); + } if (cont->parent) { remove_child(cont); } diff --git a/sway/criteria.c b/sway/criteria.c index bc0523ce..ee6d4d1c 100644 --- a/sway/criteria.c +++ b/sway/criteria.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include "sway/criteria.h" #include "sway/container.h" #include "sway/config.h" @@ -12,6 +12,7 @@ enum criteria_type { // *must* keep in sync with criteria_strings[] CRIT_CLASS, + CRIT_CON_MARK, CRIT_ID, CRIT_INSTANCE, CRIT_TITLE, @@ -22,16 +23,16 @@ enum criteria_type { // *must* keep in sync with criteria_strings[] CRIT_LAST }; -// this *must* match the ordering in criteria_type enum -static const char * const criteria_strings[] = { - "class", - "id", - "instance", - "title", - "urgent", // either "latest" or "oldest" ... - "window_role", - "window_type", - "workspace" +static const char * const criteria_strings[CRIT_LAST] = { + [CRIT_CLASS] = "class", + [CRIT_CON_MARK] = "con_mark", + [CRIT_ID] = "id", + [CRIT_INSTANCE] = "instance", + [CRIT_TITLE] = "title", + [CRIT_URGENT] = "urgent", // either "latest" or "oldest" ... + [CRIT_WINDOW_ROLE] = "window_role", + [CRIT_WINDOW_TYPE] = "window_type", + [CRIT_WORKSPACE] = "workspace" }; /** @@ -40,18 +41,13 @@ static const char * const criteria_strings[] = { */ struct crit_token { enum criteria_type type; - regex_t *regex; + pcre *regex; char *raw; }; static void free_crit_token(struct crit_token *crit) { - if (crit->regex) { - regfree(crit->regex); - free(crit->regex); - } - if (crit->raw) { - free(crit->raw); - } + pcre_free(crit->regex); + free(crit->raw); free(crit); } @@ -188,18 +184,17 @@ static char *parse_criteria_name(enum criteria_type *type, char *name) { } // Returns error string on failure or NULL otherwise. -static char *generate_regex(regex_t **regex, char *value) { - *regex = calloc(1, sizeof(regex_t)); - int err = regcomp(*regex, value, REG_NOSUB); - if (err != 0) { - char *reg_err = malloc(64); - regerror(err, *regex, reg_err, 64); +static char *generate_regex(pcre **regex, char *value) { + const char *reg_err; + int offset; + + *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL); + if (!*regex) { const char *fmt = "Regex compilation (for '%s') failed: %s"; int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; char *error = malloc(len); snprintf(error, len, fmt, value, reg_err); - free(reg_err); return error; } return NULL; @@ -243,6 +238,10 @@ ect_cleanup: return error; } +static int regex_cmp(const char *item, const pcre *regex) { + return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0); +} + // test a single view if it matches list of criteria tokens (all of them). static bool criteria_test(swayc_t *cont, list_t *tokens) { if (cont->type != C_VIEW) { @@ -260,26 +259,34 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->class && strcmp(cont->class, focused->class) == 0) { matches++; } - } else if (crit->regex && regexec(crit->regex, cont->class, 0, NULL, 0) == 0) { + } else if (crit->regex && regex_cmp(cont->class, crit->regex) == 0) { matches++; } break; + case CRIT_CON_MARK: + if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) { + // Make sure it isn't matching the NUL string + if ((strcmp(crit->raw, "") == 0) == (list_seq_find(cont->marks, (int (*)(const void *, const void *))strcmp, "") != -1)) { + ++matches; + } + } + break; case CRIT_ID: if (!cont->app_id) { // ignore - } else if (crit->regex && regexec(crit->regex, cont->app_id, 0, NULL, 0) == 0) { + } else if (crit->regex && regex_cmp(cont->app_id, crit->regex) == 0) { matches++; } break; case CRIT_INSTANCE: if (!cont->instance) { // ignore - } else if (strcmp(crit->raw, "focused") == 0) { + } else if (crit_is_focused(crit->raw)) { swayc_t *focused = get_focused_view(&root_container); if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { matches++; } - } else if (crit->regex && regexec(crit->regex, cont->instance, 0, NULL, 0) == 0) { + } else if (crit->regex && regex_cmp(cont->instance, crit->regex) == 0) { matches++; } break; @@ -291,7 +298,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused->name && strcmp(cont->name, focused->name) == 0) { matches++; } - } else if (crit->regex && regexec(crit->regex, cont->name, 0, NULL, 0) == 0) { + } else if (crit->regex && regex_cmp(cont->name, crit->regex) == 0) { matches++; } break; @@ -311,7 +318,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) { if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { matches++; } - } else if (crit->regex && regexec(crit->regex, cont_ws->name, 0, NULL, 0) == 0) { + } else if (crit->regex && regex_cmp(cont_ws->name, crit->regex) == 0) { matches++; } break; @@ -362,3 +369,21 @@ list_t *criteria_for(swayc_t *cont) { } return matches; } + +struct list_tokens { + list_t *list; + list_t *tokens; +}; + +static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) { + if (criteria_test(container, list_tokens->tokens)) { + list_add(list_tokens->list, container); + } +} +list_t *container_for(list_t *tokens) { + struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens}; + + container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens); + + return list_tokens.list; +} diff --git a/sway/ipc-server.c b/sway/ipc-server.c index b5f4bb16..67a3cdc8 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -63,6 +63,7 @@ void ipc_client_handle_command(struct ipc_client *client); bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); void ipc_get_workspaces_callback(swayc_t *workspace, void *data); void ipc_get_outputs_callback(swayc_t *container, void *data); +static void ipc_get_marks_callback(swayc_t *container, void *data); void ipc_init(void) { ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); @@ -161,7 +162,8 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) { } int flags; - if ((flags=fcntl(client_fd, F_GETFD)) == -1 || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { + if ((flags = fcntl(client_fd, F_GETFD)) == -1 + || fcntl(client_fd, F_SETFD, flags|FD_CLOEXEC) == -1) { sway_log_errno(L_ERROR, "Unable to set CLOEXEC on IPC client socket"); close(client_fd); return 0; @@ -193,13 +195,12 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { if (mask & WLC_EVENT_ERROR) { sway_log(L_ERROR, "IPC Client socket error, removing client"); - client->fd = -1; ipc_client_disconnect(client); return 0; } if (mask & WLC_EVENT_HANGUP) { - client->fd = -1; + sway_log(L_DEBUG, "Client %d hung up", client->fd); ipc_client_disconnect(client); return 0; } @@ -456,6 +457,19 @@ void ipc_client_handle_command(struct ipc_client *client) { goto exit_cleanup; } + case IPC_GET_MARKS: + { + if (!(client->security_policy & IPC_FEATURE_GET_MARKS)) { + goto exit_denied; + } + json_object *marks = json_object_new_array(); + container_map(&root_container, ipc_get_marks_callback, marks); + const char *json_string = json_object_to_json_string(marks); + ipc_send_reply(client, json_string, (uint32_t) strlen(json_string)); + json_object_put(marks); + goto exit_cleanup; + } + case IPC_GET_VERSION: { json_object *version = ipc_json_get_version(); @@ -609,6 +623,16 @@ void ipc_get_outputs_callback(swayc_t *container, void *data) { } } +static void ipc_get_marks_callback(swayc_t *container, void *data) { + json_object *object = (json_object *)data; + if (container->marks) { + for (int i = 0; i < container->marks->length; ++i) { + char *mark = (char *)container->marks->items[i]; + json_object_array_add(object, json_object_new_string(mark)); + } + } +} + void ipc_send_event(const char *json_string, enum ipc_command_type event) { static struct { enum ipc_command_type event; diff --git a/sway/main.c b/sway/main.c index 55b71fa4..b9f8936f 100644 --- a/sway/main.c +++ b/sway/main.c @@ -11,8 +11,8 @@ #include #include #include -#include #ifdef __linux__ +#include #include #endif #include "sway/extensions.h" diff --git a/sway/sway.1.txt b/sway/sway.1.txt index 32519d0e..1a77611d 100644 --- a/sway/sway.1.txt +++ b/sway/sway.1.txt @@ -48,8 +48,8 @@ providing such a great piece of software, so good that your users would rather write an entirely new window manager from scratch that behaved _exactly_ like i3 rather than switch to something else. -You may run sway from an ongoing x11 session to run it within x. Otherwise, you -can run sway on a tty and it will use your outputs directly. +Launch sway directly from a tty or via your favorite Wayland-compatible login +manager. *Important note for nvidia users*: The proprietary nvidia driver does _not_ have support for Wayland as of 2016-06-10. Use nouveau. diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 5d143d97..d76951b5 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -316,6 +316,14 @@ The default colors are: If smart_gaps are _on_ then gaps will only be enabled if a workspace has more than one child container. +**mark** \<--add|--replace> \<--toggle> :: + Marks are arbitrary labels that can be used to identify certain windows and + then jump to them at a later time. By default, the **mark** command sets + _identifier_ as the only mark on a window. By specifying _--add_, mark will + add _identifier_ to the list of current marks. If _--toggle_ is specified mark + will remove _identifier_ if it is already a label. Marks may be found by using + a criteria. See the **Criteria** section below. + **mode** :: Switches to the given mode_name. The default mode is simply _default_. To create a new mode in config append _{_ to this command, the following lines @@ -368,6 +376,15 @@ The default colors are: be configured with perfectly aligned adjacent positions for this option to have any effect. +**show_marks** :: + If **show_marks** is on then marks will be showed in the window decoration. + However, any mark that starts with an underscore will not be drawn even if the + option is on. The default option is _on_. + +**unmark** :: + **Unmark** will remove _identifier_ from the list of current marks on a window. If + no _identifier_ is specified then **unmark** will remove all marks. + **workspace** [number] :: Switches to the specified workspace. The string "number" is optional. The worspace _name_, if unquoted, may not contain the string "output", as sway @@ -409,6 +426,20 @@ The string contains one or more (space separated) attribute/value pairs and they are used by some commands filter which views to execute actions on. All attributes must match for the criteria string to match. +Criteria may be used with either the **for_window** or **assign** commands to +specify operations to perform on new views. A criteria may also be used to +perform specific commands (ones that normally act upon one window) on all views +that match that criteria. For example: + +Focus on a window with the mark "IRC": + [con_mark="IRC"] focus + +Kill all windows with the title "Emacs": + [class="Emacs"] kill + +Mark all Firefox windows with "Browser": + [class="Firefox"] mark Browser + Currently supported attributes: **class**:: @@ -416,6 +447,9 @@ Currently supported attributes: is _focused_ then the window class must be the same as that of the currently focused window. +**con_mark**:: + Compare against the window marks. Can be a regular expression. + **id**:: Compare value against the app id. Can be a regular expression.