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