From b374c35758777f98e5ddbe4b0dc43bd7c80f36d7 Mon Sep 17 00:00:00 2001 From: Zandr Martin Date: Thu, 1 Sep 2016 21:39:08 -0500 Subject: refactor commands.c --- sway/commands/scratchpad.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 sway/commands/scratchpad.c (limited to 'sway/commands/scratchpad.c') diff --git a/sway/commands/scratchpad.c b/sway/commands/scratchpad.c new file mode 100644 index 00000000..8cde85a0 --- /dev/null +++ b/sway/commands/scratchpad.c @@ -0,0 +1,65 @@ +#include +#include +#include "commands.h" +#include "container.h" +#include "focus.h" +#include "layout.h" + +static swayc_t *fetch_view_from_scratchpad() { + if (sp_index >= scratchpad->length) { + sp_index = 0; + } + swayc_t *view = scratchpad->items[sp_index++]; + + if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) { + wlc_view_set_output(view->handle, swayc_active_output()->handle); + } + if (!view->is_floating) { + view->width = swayc_active_workspace()->width/2; + view->height = swayc_active_workspace()->height/2; + view->x = (swayc_active_workspace()->width - view->width)/2; + view->y = (swayc_active_workspace()->height - view->height)/2; + } + if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) { + view->x = (swayc_active_workspace()->width - view->width)/2; + } + if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) { + view->y = (swayc_active_workspace()->height - view->height)/2; + } + + add_floating(swayc_active_workspace(), view); + wlc_view_set_mask(view->handle, VISIBLE); + view->visible = true; + arrange_windows(swayc_active_workspace(), -1, -1); + set_focused_container(view); + return view; +} + +struct cmd_results *cmd_scratchpad(int argc, char **argv) { + struct cmd_results *error = NULL; + if (config->reading) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can't be used in config file."); + if (!config->active) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can only be used when sway is running."); + if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) { + if (!sp_view) { + sp_view = fetch_view_from_scratchpad(); + } else { + if (swayc_active_workspace() != sp_view->parent) { + hide_view_in_scratchpad(sp_view); + if (sp_index == 0) { + sp_index = scratchpad->length; + } + sp_index--; + sp_view = fetch_view_from_scratchpad(); + } else { + hide_view_in_scratchpad(sp_view); + sp_view = NULL; + } + } + return cmd_results_new(CMD_SUCCESS, NULL, NULL); + } + return cmd_results_new(CMD_FAILURE, "scratchpad", "Expected 'scratchpad show' when scratchpad is not empty."); +} -- cgit v1.2.3-54-g00ecf