aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/floating.c
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
committerLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
commitb374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch)
tree04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands/floating.c
parentMerge pull request #874 from yohanesu75/ipc-client-fix (diff)
downloadsway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.gz
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.zst
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.zip
refactor commands.c
Diffstat (limited to 'sway/commands/floating.c')
-rw-r--r--sway/commands/floating.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/sway/commands/floating.c b/sway/commands/floating.c
new file mode 100644
index 00000000..2323902d
--- /dev/null
+++ b/sway/commands/floating.c
@@ -0,0 +1,80 @@
1#include <string.h>
2#include "commands.h"
3#include "container.h"
4#include "ipc-server.h"
5#include "layout.h"
6#include "list.h"
7#include "log.h"
8
9struct cmd_results *cmd_floating(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if (config->reading) return cmd_results_new(CMD_FAILURE, "floating", "Can't be used in config file.");
12 if ((error = checkarg(argc, "floating", EXPECTED_EQUAL_TO, 1))) {
13 return error;
14 }
15 swayc_t *view = get_focused_container(&root_container);
16 bool wants_floating;
17 if (strcasecmp(argv[0], "enable") == 0) {
18 wants_floating = true;
19 } else if (strcasecmp(argv[0], "disable") == 0) {
20 wants_floating = false;
21 } else if (strcasecmp(argv[0], "toggle") == 0) {
22 wants_floating = !view->is_floating;
23 } else {
24 return cmd_results_new(CMD_FAILURE, "floating",
25 "Expected 'floating <enable|disable|toggle>");
26 }
27
28 // Prevent running floating commands on things like workspaces
29 if (view->type != C_VIEW) {
30 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
31 }
32
33 // Change from nonfloating to floating
34 if (!view->is_floating && wants_floating) {
35 // Remove view from its current location
36 destroy_container(remove_child(view));
37
38 // and move it into workspace floating
39 add_floating(swayc_active_workspace(), view);
40 view->x = (swayc_active_workspace()->width - view->width)/2;
41 view->y = (swayc_active_workspace()->height - view->height)/2;
42 if (view->desired_width != -1) {
43 view->width = view->desired_width;
44 }
45 if (view->desired_height != -1) {
46 view->height = view->desired_height;
47 }
48 arrange_windows(swayc_active_workspace(), -1, -1);
49
50 } else if (view->is_floating && !wants_floating) {
51 // Delete the view from the floating list and unset its is_floating flag
52 remove_child(view);
53 view->is_floating = false;
54 // Get the properly focused container, and add in the view there
55 swayc_t *focused = container_under_pointer();
56 // If focused is null, it's because the currently focused container is a workspace
57 if (focused == NULL || focused->is_floating) {
58 focused = swayc_active_workspace();
59 }
60 set_focused_container(focused);
61
62 sway_log(L_DEBUG, "Non-floating focused container is %p", focused);
63
64 // Case of focused workspace, just create as child of it
65 if (focused->type == C_WORKSPACE) {
66 add_child(focused, view);
67 }
68 // Regular case, create as sibling of current container
69 else {
70 add_sibling(focused, view);
71 }
72 // Refocus on the view once its been put back into the layout
73 view->width = view->height = 0;
74 arrange_windows(swayc_active_workspace(), -1, -1);
75 remove_view_from_scratchpad(view);
76 ipc_event_window(view, "floating");
77 }
78 set_focused_container(view);
79 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
80}