diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-08-09 09:23:10 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-08-09 09:23:10 -0400 |
commit | f97a48d5b75fd80a578af14f19d0b1d954f82fdd (patch) | |
tree | c99dcdd46754915d18445281d2778ea61be05f3b | |
parent | Handle unmanaged windows appropriately (diff) | |
download | sway-f97a48d5b75fd80a578af14f19d0b1d954f82fdd.tar.gz sway-f97a48d5b75fd80a578af14f19d0b1d954f82fdd.tar.zst sway-f97a48d5b75fd80a578af14f19d0b1d954f82fdd.zip |
Implement focus_follows_mouse
-rw-r--r-- | sway/commands.c | 60 | ||||
-rw-r--r-- | sway/config.c | 11 | ||||
-rw-r--r-- | sway/config.h | 4 | ||||
-rw-r--r-- | sway/handlers.c | 40 | ||||
-rw-r--r-- | sway/handlers.h | 4 | ||||
-rw-r--r-- | sway/layout.c | 19 | ||||
-rw-r--r-- | sway/layout.h | 2 | ||||
-rw-r--r-- | sway/main.c | 4 |
8 files changed, 118 insertions, 26 deletions
diff --git a/sway/commands.c b/sway/commands.c index 6e2a79f6..8e6d19ec 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -9,30 +9,6 @@ | |||
9 | #include "log.h" | 9 | #include "log.h" |
10 | #include "commands.h" | 10 | #include "commands.h" |
11 | 11 | ||
12 | int cmd_set(struct sway_config *config, int argc, char **argv) { | ||
13 | if (argc != 2) { | ||
14 | sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); | ||
15 | return 1; | ||
16 | } | ||
17 | struct sway_variable *var = malloc(sizeof(struct sway_variable)); | ||
18 | var->name = malloc(strlen(argv[0]) + 1); | ||
19 | strcpy(var->name, argv[0]); | ||
20 | var->value = malloc(strlen(argv[1]) + 1); | ||
21 | strcpy(var->value, argv[1]); | ||
22 | list_add(config->symbols, var); | ||
23 | return 0; | ||
24 | } | ||
25 | |||
26 | int cmd_exit(struct sway_config *config, int argc, char **argv) { | ||
27 | if (argc != 0) { | ||
28 | sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc); | ||
29 | return 1; | ||
30 | } | ||
31 | // TODO: Some kind of clean up is probably in order | ||
32 | exit(0); | ||
33 | return 0; | ||
34 | } | ||
35 | |||
36 | struct modifier_key { | 12 | struct modifier_key { |
37 | char *name; | 13 | char *name; |
38 | uint32_t mod; | 14 | uint32_t mod; |
@@ -94,10 +70,46 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) { | |||
94 | return 0; | 70 | return 0; |
95 | } | 71 | } |
96 | 72 | ||
73 | int cmd_exit(struct sway_config *config, int argc, char **argv) { | ||
74 | if (argc != 0) { | ||
75 | sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc); | ||
76 | return 1; | ||
77 | } | ||
78 | // TODO: Some kind of clean up is probably in order | ||
79 | exit(0); | ||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | |||
84 | int cmd_focus_follows_mouse(struct sway_config *config, int argc, char **argv) { | ||
85 | if (argc != 1) { | ||
86 | sway_log(L_ERROR, "Invalid focus_follows_mouse command (expected 1 arguments, got %d)", argc); | ||
87 | return 1; | ||
88 | } | ||
89 | |||
90 | config->focus_follows_mouse = !strcasecmp(argv[0], "yes"); | ||
91 | return 0; | ||
92 | } | ||
93 | |||
94 | int cmd_set(struct sway_config *config, int argc, char **argv) { | ||
95 | if (argc != 2) { | ||
96 | sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); | ||
97 | return 1; | ||
98 | } | ||
99 | struct sway_variable *var = malloc(sizeof(struct sway_variable)); | ||
100 | var->name = malloc(strlen(argv[0]) + 1); | ||
101 | strcpy(var->name, argv[0]); | ||
102 | var->value = malloc(strlen(argv[1]) + 1); | ||
103 | strcpy(var->value, argv[1]); | ||
104 | list_add(config->symbols, var); | ||
105 | return 0; | ||
106 | } | ||
107 | |||
97 | /* Keep alphabetized */ | 108 | /* Keep alphabetized */ |
98 | struct cmd_handler handlers[] = { | 109 | struct cmd_handler handlers[] = { |
99 | { "bindsym", cmd_bindsym }, | 110 | { "bindsym", cmd_bindsym }, |
100 | { "exit", cmd_exit }, | 111 | { "exit", cmd_exit }, |
112 | { "focus_follows_mouse", cmd_focus_follows_mouse }, | ||
101 | { "set", cmd_set }, | 113 | { "set", cmd_set }, |
102 | }; | 114 | }; |
103 | 115 | ||
diff --git a/sway/config.c b/sway/config.c index 7c610755..26dc88cb 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -8,14 +8,21 @@ | |||
8 | #include "commands.h" | 8 | #include "commands.h" |
9 | #include "config.h" | 9 | #include "config.h" |
10 | 10 | ||
11 | struct sway_config *read_config(FILE *file) { | 11 | void config_defaults(struct sway_config *config) { |
12 | struct sway_config *config = malloc(sizeof(struct sway_config)); | ||
13 | config->symbols = create_list(); | 12 | config->symbols = create_list(); |
14 | config->modes = create_list(); | 13 | config->modes = create_list(); |
15 | config->current_mode = malloc(sizeof(struct sway_mode)); | 14 | config->current_mode = malloc(sizeof(struct sway_mode)); |
16 | config->current_mode->name = NULL; | 15 | config->current_mode->name = NULL; |
17 | config->current_mode->bindings = create_list(); | 16 | config->current_mode->bindings = create_list(); |
18 | list_add(config->modes, config->current_mode); | 17 | list_add(config->modes, config->current_mode); |
18 | // Flags | ||
19 | config->focus_follows_mouse = true; | ||
20 | config->mouse_warping = true; | ||
21 | } | ||
22 | |||
23 | struct sway_config *read_config(FILE *file) { | ||
24 | struct sway_config *config = malloc(sizeof(struct sway_config)); | ||
25 | config_defaults(config); | ||
19 | 26 | ||
20 | bool success = true; | 27 | bool success = true; |
21 | 28 | ||
diff --git a/sway/config.h b/sway/config.h index 88225800..6802a341 100644 --- a/sway/config.h +++ b/sway/config.h | |||
@@ -25,6 +25,10 @@ struct sway_config { | |||
25 | list_t *symbols; | 25 | list_t *symbols; |
26 | list_t *modes; | 26 | list_t *modes; |
27 | struct sway_mode *current_mode; | 27 | struct sway_mode *current_mode; |
28 | |||
29 | // Flags | ||
30 | bool focus_follows_mouse; | ||
31 | bool mouse_warping; | ||
28 | }; | 32 | }; |
29 | 33 | ||
30 | struct sway_config *read_config(FILE *file); | 34 | struct sway_config *read_config(FILE *file); |
diff --git a/sway/handlers.c b/sway/handlers.c index f1098835..0c0fb85f 100644 --- a/sway/handlers.c +++ b/sway/handlers.c | |||
@@ -83,3 +83,43 @@ bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers | |||
83 | } | 83 | } |
84 | return ret; | 84 | return ret; |
85 | } | 85 | } |
86 | |||
87 | bool pointer_test(swayc_t *view, void *_origin) { | ||
88 | const struct wlc_origin *origin = _origin; | ||
89 | if (view->type == C_VIEW && origin->x >= view->x && origin->y >= view->y | ||
90 | && origin->x < view->x + view->width && origin->y < view->y + view->height) { | ||
91 | return true; | ||
92 | } | ||
93 | return false; | ||
94 | } | ||
95 | |||
96 | struct wlc_origin mouse_origin; | ||
97 | |||
98 | bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin) { | ||
99 | mouse_origin = *origin; | ||
100 | if (!config->focus_follows_mouse) { | ||
101 | return true; | ||
102 | } | ||
103 | swayc_t *c = find_container(&root_container, pointer_test, (void *)origin); | ||
104 | swayc_t *focused = get_focused_container(&root_container); | ||
105 | if (c && c != focused) { | ||
106 | sway_log(L_DEBUG, "Switching focus to %p", c); | ||
107 | focus_view(c); | ||
108 | } | ||
109 | return true; | ||
110 | } | ||
111 | |||
112 | bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, | ||
113 | uint32_t button, enum wlc_button_state state) { | ||
114 | if (state == WLC_BUTTON_STATE_PRESSED) { | ||
115 | swayc_t *c = find_container(&root_container, pointer_test, &mouse_origin); | ||
116 | swayc_t *focused = get_focused_container(&root_container); | ||
117 | if (c && c != focused) { | ||
118 | sway_log(L_DEBUG, "Switching focus to %p", c); | ||
119 | focus_view(c); | ||
120 | return false; | ||
121 | } | ||
122 | return true; | ||
123 | } | ||
124 | return true; | ||
125 | } | ||
diff --git a/sway/handlers.h b/sway/handlers.h index d0cc67c8..331be725 100644 --- a/sway/handlers.h +++ b/sway/handlers.h | |||
@@ -16,4 +16,8 @@ void handle_view_geometry_request(wlc_handle view, const struct wlc_geometry* ge | |||
16 | bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers | 16 | bool handle_key(wlc_handle view, uint32_t time, const struct wlc_modifiers |
17 | *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state); | 17 | *modifiers, uint32_t key, uint32_t sym, enum wlc_key_state state); |
18 | 18 | ||
19 | bool handle_pointer_motion(wlc_handle view, uint32_t time, const struct wlc_origin *origin); | ||
20 | bool handle_pointer_button(wlc_handle view, uint32_t time, const struct wlc_modifiers *modifiers, | ||
21 | uint32_t button, enum wlc_button_state state); | ||
22 | |||
19 | #endif | 23 | #endif |
diff --git a/sway/layout.c b/sway/layout.c index 2a57ccce..996be387 100644 --- a/sway/layout.c +++ b/sway/layout.c | |||
@@ -7,6 +7,25 @@ | |||
7 | 7 | ||
8 | swayc_t root_container; | 8 | swayc_t root_container; |
9 | 9 | ||
10 | swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data) { | ||
11 | if (!container->children) { | ||
12 | return NULL; | ||
13 | } | ||
14 | int i; | ||
15 | for (i = 0; i < container->children->length; ++i) { | ||
16 | swayc_t *child = container->children->items[i]; | ||
17 | if (test(child, data)) { | ||
18 | return child; | ||
19 | } else { | ||
20 | swayc_t *_ = find_container(child, test, data); | ||
21 | if (_) { | ||
22 | return _; | ||
23 | } | ||
24 | } | ||
25 | } | ||
26 | return NULL; | ||
27 | } | ||
28 | |||
10 | void arrange_windows(swayc_t *container, int width, int height) { | 29 | void arrange_windows(swayc_t *container, int width, int height) { |
11 | int i; | 30 | int i; |
12 | if (width == -1 || height == -1) { | 31 | if (width == -1 || height == -1) { |
diff --git a/sway/layout.h b/sway/layout.h index 801faf90..0e34cb5c 100644 --- a/sway/layout.h +++ b/sway/layout.h | |||
@@ -51,6 +51,8 @@ void destroy_view(swayc_t *view); | |||
51 | void add_view(wlc_handle view); | 51 | void add_view(wlc_handle view); |
52 | void focus_view(swayc_t *view); | 52 | void focus_view(swayc_t *view); |
53 | void arrange_windows(swayc_t *container, int width, int height); | 53 | void arrange_windows(swayc_t *container, int width, int height); |
54 | swayc_t *find_container(swayc_t *container, bool (*test)(swayc_t *view, void *data), void *data); | ||
55 | swayc_t *get_focused_container(swayc_t *parent); | ||
54 | 56 | ||
55 | swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); | 57 | swayc_t *get_swayc_for_handle(wlc_handle handle, swayc_t *parent); |
56 | 58 | ||
diff --git a/sway/main.c b/sway/main.c index e547ef4a..2f69506c 100644 --- a/sway/main.c +++ b/sway/main.c | |||
@@ -48,6 +48,10 @@ int main(int argc, char **argv) { | |||
48 | }, | 48 | }, |
49 | .keyboard = { | 49 | .keyboard = { |
50 | .key = handle_key | 50 | .key = handle_key |
51 | }, | ||
52 | .pointer = { | ||
53 | .motion = handle_pointer_motion, | ||
54 | .button = handle_pointer_button | ||
51 | } | 55 | } |
52 | }; | 56 | }; |
53 | 57 | ||