diff options
Diffstat (limited to 'sway/commands/scratchpad.c')
-rw-r--r-- | sway/commands/scratchpad.c | 65 |
1 files changed, 65 insertions, 0 deletions
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 @@ | |||
1 | #include <string.h> | ||
2 | #include <wlc/wlc.h> | ||
3 | #include "commands.h" | ||
4 | #include "container.h" | ||
5 | #include "focus.h" | ||
6 | #include "layout.h" | ||
7 | |||
8 | static swayc_t *fetch_view_from_scratchpad() { | ||
9 | if (sp_index >= scratchpad->length) { | ||
10 | sp_index = 0; | ||
11 | } | ||
12 | swayc_t *view = scratchpad->items[sp_index++]; | ||
13 | |||
14 | if (wlc_view_get_output(view->handle) != swayc_active_output()->handle) { | ||
15 | wlc_view_set_output(view->handle, swayc_active_output()->handle); | ||
16 | } | ||
17 | if (!view->is_floating) { | ||
18 | view->width = swayc_active_workspace()->width/2; | ||
19 | view->height = swayc_active_workspace()->height/2; | ||
20 | view->x = (swayc_active_workspace()->width - view->width)/2; | ||
21 | view->y = (swayc_active_workspace()->height - view->height)/2; | ||
22 | } | ||
23 | if (swayc_active_workspace()->width < view->x + 20 || view->x + view->width < 20) { | ||
24 | view->x = (swayc_active_workspace()->width - view->width)/2; | ||
25 | } | ||
26 | if (swayc_active_workspace()->height < view->y + 20 || view->y + view->height < 20) { | ||
27 | view->y = (swayc_active_workspace()->height - view->height)/2; | ||
28 | } | ||
29 | |||
30 | add_floating(swayc_active_workspace(), view); | ||
31 | wlc_view_set_mask(view->handle, VISIBLE); | ||
32 | view->visible = true; | ||
33 | arrange_windows(swayc_active_workspace(), -1, -1); | ||
34 | set_focused_container(view); | ||
35 | return view; | ||
36 | } | ||
37 | |||
38 | struct cmd_results *cmd_scratchpad(int argc, char **argv) { | ||
39 | struct cmd_results *error = NULL; | ||
40 | if (config->reading) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can't be used in config file."); | ||
41 | if (!config->active) return cmd_results_new(CMD_FAILURE, "scratchpad", "Can only be used when sway is running."); | ||
42 | if ((error = checkarg(argc, "scratchpad", EXPECTED_EQUAL_TO, 1))) { | ||
43 | return error; | ||
44 | } | ||
45 | |||
46 | if (strcasecmp(argv[0], "show") == 0 && scratchpad->length > 0) { | ||
47 | if (!sp_view) { | ||
48 | sp_view = fetch_view_from_scratchpad(); | ||
49 | } else { | ||
50 | if (swayc_active_workspace() != sp_view->parent) { | ||
51 | hide_view_in_scratchpad(sp_view); | ||
52 | if (sp_index == 0) { | ||
53 | sp_index = scratchpad->length; | ||
54 | } | ||
55 | sp_index--; | ||
56 | sp_view = fetch_view_from_scratchpad(); | ||
57 | } else { | ||
58 | hide_view_in_scratchpad(sp_view); | ||
59 | sp_view = NULL; | ||
60 | } | ||
61 | } | ||
62 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
63 | } | ||
64 | return cmd_results_new(CMD_FAILURE, "scratchpad", "Expected 'scratchpad show' when scratchpad is not empty."); | ||
65 | } | ||