summaryrefslogtreecommitdiffstats
path: root/sway/commands/focus.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands/focus.c')
-rw-r--r--sway/commands/focus.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/sway/commands/focus.c b/sway/commands/focus.c
new file mode 100644
index 00000000..8442305f
--- /dev/null
+++ b/sway/commands/focus.c
@@ -0,0 +1,91 @@
1#include <string.h>
2#include <wlc/wlc.h>
3#include "sway/commands.h"
4#include "sway/container.h"
5#include "sway/focus.h"
6#include "sway/input_state.h"
7#include "sway/output.h"
8#include "sway/workspace.h"
9
10struct cmd_results *cmd_focus(int argc, char **argv) {
11 if (config->reading) return cmd_results_new(CMD_FAILURE, "focus", "Can't be used in config file.");
12 struct cmd_results *error = NULL;
13 if (argc > 0 && strcasecmp(argv[0], "output") == 0) {
14 swayc_t *output = NULL;
15 struct wlc_point abs_pos;
16 get_absolute_center_position(get_focused_container(&root_container), &abs_pos);
17 if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 2))) {
18 return error;
19 } else if (!(output = output_by_name(argv[1], &abs_pos))) {
20 return cmd_results_new(CMD_FAILURE, "focus output",
21 "Can't find output with name/at direction '%s' @ (%i,%i)", argv[1], abs_pos.x, abs_pos.y);
22 } else if (!workspace_switch(swayc_active_workspace_for(output))) {
23 return cmd_results_new(CMD_FAILURE, "focus output",
24 "Switching to workspace on output '%s' was blocked", argv[1]);
25 } else if (config->mouse_warping) {
26 swayc_t *focused = get_focused_view(output);
27 if (focused && focused->type == C_VIEW) {
28 center_pointer_on(focused);
29 }
30 }
31 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
32 } else if ((error = checkarg(argc, "focus", EXPECTED_EQUAL_TO, 1))) {
33 return error;
34 }
35 static int floating_toggled_index = 0;
36 static int tiled_toggled_index = 0;
37 if (strcasecmp(argv[0], "left") == 0) {
38 move_focus(MOVE_LEFT);
39 } else if (strcasecmp(argv[0], "right") == 0) {
40 move_focus(MOVE_RIGHT);
41 } else if (strcasecmp(argv[0], "up") == 0) {
42 move_focus(MOVE_UP);
43 } else if (strcasecmp(argv[0], "down") == 0) {
44 move_focus(MOVE_DOWN);
45 } else if (strcasecmp(argv[0], "parent") == 0) {
46 move_focus(MOVE_PARENT);
47 } else if (strcasecmp(argv[0], "child") == 0) {
48 move_focus(MOVE_CHILD);
49 } else if (strcasecmp(argv[0], "mode_toggle") == 0) {
50 int i;
51 swayc_t *workspace = swayc_active_workspace();
52 swayc_t *focused = get_focused_view(workspace);
53 if (focused->is_floating) {
54 if (workspace->children->length > 0) {
55 for (i = 0;i < workspace->floating->length; i++) {
56 if (workspace->floating->items[i] == focused) {
57 floating_toggled_index = i;
58 break;
59 }
60 }
61 if (workspace->children->length > tiled_toggled_index) {
62 set_focused_container(get_focused_view(workspace->children->items[tiled_toggled_index]));
63 } else {
64 set_focused_container(get_focused_view(workspace->children->items[0]));
65 tiled_toggled_index = 0;
66 }
67 }
68 } else {
69 if (workspace->floating->length > 0) {
70 for (i = 0;i < workspace->children->length; i++) {
71 if (workspace->children->items[i] == focused) {
72 tiled_toggled_index = i;
73 break;
74 }
75 }
76 if (workspace->floating->length > floating_toggled_index) {
77 swayc_t *floating = workspace->floating->items[floating_toggled_index];
78 set_focused_container(get_focused_view(floating));
79 } else {
80 swayc_t *floating = workspace->floating->items[workspace->floating->length - 1];
81 set_focused_container(get_focused_view(floating));
82 tiled_toggled_index = workspace->floating->length - 1;
83 }
84 }
85 }
86 } else {
87 return cmd_results_new(CMD_INVALID, "focus",
88 "Expected 'focus <direction|parent|child|mode_toggle>' or 'focus output <direction|name>'");
89 }
90 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
91}