From ab7570d311d65ff03fd14627ec3157fa37995ced Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Mon, 3 Apr 2017 07:46:11 -0400
Subject: [PATCH 1/9] Update man page

---
 sway/sway.1.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

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.

From 2445d279604d7be38c00db60ffde4279a3c75459 Mon Sep 17 00:00:00 2001
From: Calvin Lee <cyrus296@gmail.com>
Date: Sun, 2 Apr 2017 14:38:33 -0600
Subject: [PATCH 2/9] 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
---
 include/sway/commands.h    |  3 ++
 include/sway/config.h      |  1 +
 include/sway/container.h   |  5 +++
 sway/border.c              | 23 ++++++++++++
 sway/commands.c            |  3 ++
 sway/commands/mark.c       | 74 ++++++++++++++++++++++++++++++++++++++
 sway/commands/show_marks.c | 13 +++++++
 sway/commands/unmark.c     | 31 ++++++++++++++++
 sway/config.c              |  1 +
 sway/container.c           |  4 +++
 sway/criteria.c            | 11 ++++++
 sway/sway.5.txt            | 20 +++++++++++
 12 files changed, 189 insertions(+)
 create mode 100644 sway/commands/mark.c
 create mode 100644 sway/commands/show_marks.c
 create mode 100644 sway/commands/unmark.c

diff --git a/include/sway/commands.h b/include/sway/commands.h
index 511bee4d..35a2f92a 100644
--- a/include/sway/commands.h
+++ b/include/sway/commands.h
@@ -126,6 +126,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 +141,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/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 <wlc/wlc-render.h>
 #include <cairo/cairo.h>
 #include <pango/pangocairo.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
+#include <strings.h>
 #include <arpa/inet.h>
 #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..971ff505 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -190,6 +190,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 +204,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 },
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 <string.h>
+#include <strings.h>
+#include <stdbool.h>
+#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] <mark>'");
+	}
+	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 <string.h>
+#include <strings.h>
+#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 <string.h>
+#include <strings.h>
+#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);
+}
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..3ffc48f0 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -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,
@@ -25,6 +26,7 @@ enum criteria_type { // *must* keep in sync with criteria_strings[]
 // this *must* match the ordering in criteria_type enum
 static const char * const criteria_strings[] = {
 	"class",
+	"con_mark",
 	"id",
 	"instance",
 	"title",
@@ -243,6 +245,10 @@ ect_cleanup:
 	return error;
 }
 
+int regex_cmp(const char *item, const regex_t *regex) {
+    return regexec(regex, item, 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) {
@@ -264,6 +270,11 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
 				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)) {
+				++matches;
+			}
+			break;
 		case CRIT_ID:
 			if (!cont->app_id) {
 				// ignore
diff --git a/sway/sway.5.txt b/sway/sway.5.txt
index 5d143d97..3cccdfd5 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> <identifier>::
+	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** <mode_name>::
 	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** <on|off>::
+	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** <identifier>::
+	**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] <name>::
 	Switches to the specified workspace. The string "number" is optional. The
 	worspace _name_, if unquoted, may not contain the string "output", as sway
@@ -416,6 +433,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.
 

From 069d37f987c4e323cdb9396f0d80ac83d00566ff Mon Sep 17 00:00:00 2001
From: Calvin Lee <cyrus296@gmail.com>
Date: Tue, 4 Apr 2017 21:20:27 -0600
Subject: [PATCH 3/9] 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.
---
 include/sway/commands.h    |  3 ++
 include/sway/criteria.h    |  3 ++
 sway/commands.c            | 79 +++++++++++++++++++++++---------------
 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 +-
 sway/criteria.c            | 27 +++++++++++--
 sway/sway.5.txt            | 16 +++++++-
 16 files changed, 116 insertions(+), 53 deletions(-)

diff --git a/include/sway/commands.h b/include/sway/commands.h
index 35a2f92a..91f2ae01 100644
--- a/include/sway/commands.h
+++ b/include/sway/commands.h
@@ -5,6 +5,9 @@
 #include <wlc/wlc.h>
 #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.
  */
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/commands.c b/sway/commands.c
index 971ff505..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;
@@ -371,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, ";");
@@ -450,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 <normal|pixel|none|toggle> [<n>]");
 	}
 
-	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 <container|window> to workspace <name>' or "
 		"'move <container|window|workspace> to output <name|direction>' 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) {
diff --git a/sway/criteria.c b/sway/criteria.c
index 3ffc48f0..bd99461d 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -245,7 +245,7 @@ ect_cleanup:
 	return error;
 }
 
-int regex_cmp(const char *item, const regex_t *regex) {
+static int regex_cmp(const char *item, const regex_t *regex) {
     return regexec(regex, item, 0, NULL, 0);
 }
 
@@ -272,7 +272,10 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
 			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)) {
-				++matches;
+				// 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:
@@ -285,7 +288,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
 		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++;
@@ -373,3 +376,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/sway.5.txt b/sway/sway.5.txt
index 3cccdfd5..d76951b5 100644
--- a/sway/sway.5.txt
+++ b/sway/sway.5.txt
@@ -316,7 +316,7 @@ 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> <identifier>::
+**mark** \<--add|--replace> \<--toggle> <identifier>::
 	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
@@ -426,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**::

From fe54a6725ede65020dc8fb71e8d453c1c51dc3fe Mon Sep 17 00:00:00 2001
From: Scott Anderson <ascent12@hotmail.com>
Date: Fri, 7 Apr 2017 00:16:06 +1200
Subject: [PATCH 4/9] Changed regular expressions to use PCRE for i3
 compatibility

---
 sway/criteria.c | 40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/sway/criteria.c b/sway/criteria.c
index bd99461d..706da04f 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
-#include <regex.h>
+#include <pcre.h>
 #include "sway/criteria.h"
 #include "sway/container.h"
 #include "sway/config.h"
@@ -42,18 +42,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);
 }
 
@@ -190,18 +185,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, &reg_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;
@@ -245,8 +239,8 @@ ect_cleanup:
 	return error;
 }
 
-static int regex_cmp(const char *item, const regex_t *regex) {
-    return regexec(regex, item, 0, NULL, 0);
+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).
@@ -266,7 +260,7 @@ 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;
@@ -281,7 +275,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
 		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;
@@ -293,7 +287,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
 				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;
@@ -305,7 +299,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;
@@ -325,7 +319,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;

From 3a32be67ed89bf667ec840eca3191ed5f207df70 Mon Sep 17 00:00:00 2001
From: Scott Anderson <ascent12@hotmail.com>
Date: Fri, 7 Apr 2017 00:19:14 +1200
Subject: [PATCH 5/9] Added designated initaliser, to prevent any possible
 problem with ordering

---
 sway/criteria.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/sway/criteria.c b/sway/criteria.c
index 706da04f..ee6d4d1c 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -23,17 +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",
-	"con_mark",
-	"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"
 };
 
 /**
@@ -240,7 +239,7 @@ ect_cleanup:
 }
 
 static int regex_cmp(const char *item, const pcre *regex) {
-    return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
+	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).

From aa6bd85da147291728acb0828f5c987a02a66193 Mon Sep 17 00:00:00 2001
From: JerziKaminsky <JerziKaminsky@users.noreply.github.com>
Date: Thu, 6 Apr 2017 02:42:25 +0300
Subject: [PATCH 6/9] Add libcap check to CMake

- Moved ``<sys/capability.h>`` include inside `__linux__` guard,
  because all uses are similarly guarded.
- <sys/capability.h> is part of an optional devel package, at least
  in fedora. CMake now explicitly checks that libcap devel files
  are available.
- Added libcap to the list of install packages in .travis.yml, to
  make the dependency explicit. travis-ci installs the package by
  default, which is why this hasn't surfaced previously.
---
 .travis.yml            |  1 +
 CMake/FindLibcap.cmake | 56 ++++++++++++++++++++++++++++++++++++++++++
 CMakeLists.txt         |  1 +
 sway/main.c            |  2 +-
 4 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 CMake/FindLibcap.cmake

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/sway/main.c b/sway/main.c
index 55b71fa4..b9f8936f 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -11,8 +11,8 @@
 #include <signal.h>
 #include <unistd.h>
 #include <getopt.h>
-#include <sys/capability.h>
 #ifdef __linux__
+#include <sys/capability.h>
 #include <sys/prctl.h>
 #endif
 #include "sway/extensions.h"

From 50a31a6c88cf29d0ce43ded49ec5c10e6f59f4bf Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Fri, 7 Apr 2017 07:55:35 -0400
Subject: [PATCH 7/9] Fix dangling file descriptors (fixes #1152)

---
 sway/ipc-server.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 0cacc515..3600cab2 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -161,7 +161,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;
@@ -199,6 +200,8 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
 	}
 
 	if (mask & WLC_EVENT_HANGUP) {
+		sway_log(L_DEBUG, "Client %d hung up", client->fd);
+		close(client->fd);
 		client->fd = -1;
 		ipc_client_disconnect(client);
 		return 0;

From 1b2cd93212508913451fe6b9549f4b172925a4f6 Mon Sep 17 00:00:00 2001
From: Scott Anderson <ascent12@hotmail.com>
Date: Sat, 8 Apr 2017 00:45:31 +1200
Subject: [PATCH 8/9] Removed explicitly setting file descriptors to -1

---
 sway/ipc-server.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 3600cab2..984e6754 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -194,15 +194,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) {
 		sway_log(L_DEBUG, "Client %d hung up", client->fd);
-		close(client->fd);
-		client->fd = -1;
 		ipc_client_disconnect(client);
 		return 0;
 	}

From 154c6718c1f0e34e0f217150ba2770ee100e5b38 Mon Sep 17 00:00:00 2001
From: Calvin Lee <cyrus296@gmail.com>
Date: Fri, 7 Apr 2017 11:37:51 -0600
Subject: [PATCH 9/9] 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 +++++++++++++
 sway/ipc-server.c    | 24 ++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

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;
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 984e6754..6554098b 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);
@@ -464,6 +465,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();
@@ -617,6 +631,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;