summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/config.h2
-rw-r--r--include/sway/input/keyboard.h5
-rw-r--r--include/sway/input/seat.h20
-rw-r--r--include/sway/tree/layout.h1
-rw-r--r--sway/commands/bind.c25
-rw-r--r--sway/desktop/output.c40
-rw-r--r--sway/input/cursor.c62
-rw-r--r--sway/input/keyboard.c206
-rw-r--r--sway/input/seat.c98
-rw-r--r--sway/tree/layout.c1
-rw-r--r--swayidle/main.c3
11 files changed, 328 insertions, 135 deletions
diff --git a/include/sway/config.h b/include/sway/config.h
index 81e9c382..e75b0664 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -30,7 +30,7 @@ struct sway_binding {
30 bool release; 30 bool release;
31 bool locked; 31 bool locked;
32 bool bindcode; 32 bool bindcode;
33 list_t *keys; 33 list_t *keys; // sorted in ascending order
34 uint32_t modifiers; 34 uint32_t modifiers;
35 char *command; 35 char *command;
36}; 36};
diff --git a/include/sway/input/keyboard.h b/include/sway/input/keyboard.h
index e99a54b1..6713398e 100644
--- a/include/sway/input/keyboard.h
+++ b/include/sway/input/keyboard.h
@@ -21,7 +21,9 @@ struct sway_shortcut_state {
21 * including duplicates when a keycode generates multiple key ids. 21 * including duplicates when a keycode generates multiple key ids.
22 */ 22 */
23 uint32_t pressed_keycodes[SWAY_KEYBOARD_PRESSED_KEYS_CAP]; 23 uint32_t pressed_keycodes[SWAY_KEYBOARD_PRESSED_KEYS_CAP];
24 int last_key_index; 24 uint32_t last_keycode;
25 uint32_t last_raw_modifiers;
26 size_t npressed;
25}; 27};
26 28
27struct sway_keyboard { 29struct sway_keyboard {
@@ -36,7 +38,6 @@ struct sway_keyboard {
36 struct sway_shortcut_state state_keysyms_raw; 38 struct sway_shortcut_state state_keysyms_raw;
37 struct sway_shortcut_state state_keycodes; 39 struct sway_shortcut_state state_keycodes;
38 struct sway_binding *held_binding; 40 struct sway_binding *held_binding;
39 uint32_t last_modifiers;
40}; 41};
41 42
42struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, 43struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h
index 2e4da438..1f7792ba 100644
--- a/include/sway/input/seat.h
+++ b/include/sway/input/seat.h
@@ -21,6 +21,19 @@ struct sway_seat_container {
21 struct wl_listener destroy; 21 struct wl_listener destroy;
22}; 22};
23 23
24struct sway_drag_icon {
25 struct sway_seat *seat;
26 struct wlr_drag_icon *wlr_drag_icon;
27 struct wl_list link; // sway_root::drag_icons
28
29 double x, y; // in layout-local coordinates
30
31 struct wl_listener surface_commit;
32 struct wl_listener map;
33 struct wl_listener unmap;
34 struct wl_listener destroy;
35};
36
24struct sway_seat { 37struct sway_seat {
25 struct wlr_seat *wlr_seat; 38 struct wlr_seat *wlr_seat;
26 struct sway_cursor *cursor; 39 struct sway_cursor *cursor;
@@ -35,8 +48,13 @@ struct sway_seat {
35 // If exclusive_client is set, no other clients will receive input events 48 // If exclusive_client is set, no other clients will receive input events
36 struct wl_client *exclusive_client; 49 struct wl_client *exclusive_client;
37 50
51 // Last touch point
52 int32_t touch_id;
53 double touch_x, touch_y;
54
38 struct wl_listener focus_destroy; 55 struct wl_listener focus_destroy;
39 struct wl_listener new_container; 56 struct wl_listener new_container;
57 struct wl_listener new_drag_icon;
40 58
41 struct wl_list devices; // sway_seat_device::link 59 struct wl_list devices; // sway_seat_device::link
42 60
@@ -114,4 +132,6 @@ struct seat_config *seat_get_config(struct sway_seat *seat);
114 132
115bool seat_is_input_allowed(struct sway_seat *seat, struct wlr_surface *surface); 133bool seat_is_input_allowed(struct sway_seat *seat, struct wlr_surface *surface);
116 134
135void drag_icon_update_position(struct sway_drag_icon *icon);
136
117#endif 137#endif
diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h
index cd131056..ba265623 100644
--- a/include/sway/tree/layout.h
+++ b/include/sway/tree/layout.h
@@ -28,6 +28,7 @@ struct sway_root {
28 struct wl_listener output_layout_change; 28 struct wl_listener output_layout_change;
29 29
30 struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link 30 struct wl_list xwayland_unmanaged; // sway_xwayland_unmanaged::link
31 struct wl_list drag_icons; // sway_drag_icon::link
31 32
32 struct wlr_texture *debug_tree; 33 struct wlr_texture *debug_tree;
33 34
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index d0e3e22f..821f9cd1 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -32,7 +32,7 @@ void free_sway_binding(struct sway_binding *binding) {
32 * Note that keyboard layout is not considered, so the bindings might actually 32 * Note that keyboard layout is not considered, so the bindings might actually
33 * not be equivalent on some layouts. 33 * not be equivalent on some layouts.
34 */ 34 */
35bool binding_key_compare(struct sway_binding *binding_a, 35static bool binding_key_compare(struct sway_binding *binding_a,
36 struct sway_binding *binding_b) { 36 struct sway_binding *binding_b) {
37 if (binding_a->release != binding_b->release) { 37 if (binding_a->release != binding_b->release) {
38 return false; 38 return false;
@@ -50,18 +50,12 @@ bool binding_key_compare(struct sway_binding *binding_a,
50 return false; 50 return false;
51 } 51 }
52 52
53 // Keys are sorted
53 int keys_len = binding_a->keys->length; 54 int keys_len = binding_a->keys->length;
54 for (int i = 0; i < keys_len; ++i) { 55 for (int i = 0; i < keys_len; ++i) {
55 uint32_t key_a = *(uint32_t*)binding_a->keys->items[i]; 56 uint32_t key_a = *(uint32_t *)binding_a->keys->items[i];
56 bool found = false; 57 uint32_t key_b = *(uint32_t *)binding_b->keys->items[i];
57 for (int j = 0; j < keys_len; ++j) { 58 if (key_a != key_b) {
58 uint32_t key_b = *(uint32_t*)binding_b->keys->items[j];
59 if (key_b == key_a) {
60 found = true;
61 break;
62 }
63 }
64 if (!found) {
65 return false; 59 return false;
66 } 60 }
67 } 61 }
@@ -69,6 +63,12 @@ bool binding_key_compare(struct sway_binding *binding_a,
69 return true; 63 return true;
70} 64}
71 65
66static int key_qsort_cmp(const void *keyp_a, const void *keyp_b) {
67 uint32_t key_a = **(uint32_t **)keyp_a;
68 uint32_t key_b = **(uint32_t **)keyp_b;
69 return (key_a < key_b) ? -1 : ((key_a > key_b) ? 1 : 0);
70}
71
72static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv, 72static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
73 bool bindcode) { 73 bool bindcode) {
74 const char *bindtype = bindcode ? "bindcode" : "bindsym"; 74 const char *bindtype = bindcode ? "bindcode" : "bindsym";
@@ -169,6 +169,9 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
169 free_flat_list(split); 169 free_flat_list(split);
170 binding->order = binding_order++; 170 binding->order = binding_order++;
171 171
172 // sort ascending
173 list_qsort(binding->keys, key_qsort_cmp);
174
172 list_t *mode_bindings; 175 list_t *mode_bindings;
173 if (bindcode) { 176 if (bindcode) {
174 mode_bindings = config->current_mode->keycode_bindings; 177 mode_bindings = config->current_mode->keycode_bindings;
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index bd1d760d..d4115be8 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -161,6 +161,21 @@ static void unmanaged_for_each_surface(struct wl_list *unmanaged,
161 } 161 }
162} 162}
163 163
164static void drag_icons_for_each_surface(struct wl_list *drag_icons,
165 struct sway_output *output, struct root_geometry *geo,
166 wlr_surface_iterator_func_t iterator, void *user_data) {
167 struct sway_drag_icon *drag_icon;
168 wl_list_for_each(drag_icon, drag_icons, link) {
169 double ox = drag_icon->x - output->swayc->x;
170 double oy = drag_icon->y - output->swayc->y;
171
172 if (drag_icon->wlr_drag_icon->mapped) {
173 surface_for_each_surface(drag_icon->wlr_drag_icon->surface,
174 ox, oy, geo, iterator, user_data);
175 }
176 }
177}
178
164static void scale_box(struct wlr_box *box, float scale) { 179static void scale_box(struct wlr_box *box, float scale) {
165 box->x *= scale; 180 box->x *= scale;
166 box->y *= scale; 181 box->y *= scale;
@@ -225,7 +240,8 @@ static void render_surface_iterator(struct wlr_surface *surface, int sx, int sy,
225 pixman_region32_t *output_damage = data->damage; 240 pixman_region32_t *output_damage = data->damage;
226 float alpha = data->alpha; 241 float alpha = data->alpha;
227 242
228 if (!wlr_surface_has_buffer(surface)) { 243 struct wlr_texture *texture = wlr_surface_get_texture(surface);
244 if (texture == NULL) {
229 return; 245 return;
230 } 246 }
231 247
@@ -244,8 +260,7 @@ static void render_surface_iterator(struct wlr_surface *surface, int sx, int sy,
244 wlr_matrix_project_box(matrix, &box, transform, rotation, 260 wlr_matrix_project_box(matrix, &box, transform, rotation,
245 wlr_output->transform_matrix); 261 wlr_output->transform_matrix);
246 262
247 render_texture(wlr_output, output_damage, surface->texture, &box, matrix, 263 render_texture(wlr_output, output_damage, texture, &box, matrix, alpha);
248 alpha);
249} 264}
250 265
251static void render_layer(struct sway_output *output, 266static void render_layer(struct sway_output *output,
@@ -270,6 +285,17 @@ static void render_unmanaged(struct sway_output *output,
270 render_surface_iterator, &data); 285 render_surface_iterator, &data);
271} 286}
272 287
288static void render_drag_icons(struct sway_output *output,
289 pixman_region32_t *damage, struct wl_list *drag_icons) {
290 struct render_data data = {
291 .output = output,
292 .damage = damage,
293 .alpha = 1.0f,
294 };
295 drag_icons_for_each_surface(drag_icons, output, &data.root_geo,
296 render_surface_iterator, &data);
297}
298
273static void render_rect(struct wlr_output *wlr_output, 299static void render_rect(struct wlr_output *wlr_output,
274 pixman_region32_t *output_damage, const struct wlr_box *_box, 300 pixman_region32_t *output_damage, const struct wlr_box *_box,
275 float color[static 4]) { 301 float color[static 4]) {
@@ -911,6 +937,7 @@ static void render_output(struct sway_output *output, struct timespec *when,
911 } 937 }
912 render_layer(output, damage, 938 render_layer(output, damage,
913 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); 939 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
940 render_drag_icons(output, damage, &root_container.sway_root->drag_icons);
914 941
915renderer_end: 942renderer_end:
916 if (root_container.sway_root->debug_tree) { 943 if (root_container.sway_root->debug_tree) {
@@ -961,6 +988,12 @@ static void send_frame_done_unmanaged(struct send_frame_done_data *data,
961 send_frame_done_iterator, data); 988 send_frame_done_iterator, data);
962} 989}
963 990
991static void send_frame_done_drag_icons(struct send_frame_done_data *data,
992 struct wl_list *drag_icons) {
993 drag_icons_for_each_surface(drag_icons, data->output, &data->root_geo,
994 send_frame_done_iterator, data);
995}
996
964static void send_frame_done_container_iterator(struct sway_container *con, 997static void send_frame_done_container_iterator(struct sway_container *con,
965 void *_data) { 998 void *_data) {
966 struct send_frame_done_data *data = _data; 999 struct send_frame_done_data *data = _data;
@@ -1014,6 +1047,7 @@ static void send_frame_done(struct sway_output *output, struct timespec *when) {
1014 1047
1015 send_frame_done_layer(&data, 1048 send_frame_done_layer(&data,
1016 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); 1049 &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
1050 send_frame_done_drag_icons(&data, &root_container.sway_root->drag_icons);
1017} 1051}
1018 1052
1019static void damage_handle_frame(struct wl_listener *listener, void *data) { 1053static void damage_handle_frame(struct wl_listener *listener, void *data) {
diff --git a/sway/input/cursor.c b/sway/input/cursor.c
index 4100479c..37a87756 100644
--- a/sway/input/cursor.c
+++ b/sway/input/cursor.c
@@ -144,21 +144,22 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec,
144 time_msec = get_current_time_msec(); 144 time_msec = get_current_time_msec();
145 } 145 }
146 146
147 struct wlr_seat *seat = cursor->seat->wlr_seat; 147 struct sway_seat *seat = cursor->seat;
148 struct wlr_seat *wlr_seat = seat->wlr_seat;
148 struct wlr_surface *surface = NULL; 149 struct wlr_surface *surface = NULL;
149 double sx, sy; 150 double sx, sy;
150 151
151 // Find the container beneath the pointer's previous position 152 // Find the container beneath the pointer's previous position
152 struct sway_container *prev_c = container_at_coords(cursor->seat, 153 struct sway_container *prev_c = container_at_coords(seat,
153 cursor->previous.x, cursor->previous.y, &surface, &sx, &sy); 154 cursor->previous.x, cursor->previous.y, &surface, &sx, &sy);
154 // Update the stored previous position 155 // Update the stored previous position
155 cursor->previous.x = cursor->cursor->x; 156 cursor->previous.x = cursor->cursor->x;
156 cursor->previous.y = cursor->cursor->y; 157 cursor->previous.y = cursor->cursor->y;
157 158
158 struct sway_container *c = container_at_coords(cursor->seat, 159 struct sway_container *c = container_at_coords(seat,
159 cursor->cursor->x, cursor->cursor->y, &surface, &sx, &sy); 160 cursor->cursor->x, cursor->cursor->y, &surface, &sx, &sy);
160 if (c && config->focus_follows_mouse && allow_refocusing) { 161 if (c && config->focus_follows_mouse && allow_refocusing) {
161 struct sway_container *focus = seat_get_focus(cursor->seat); 162 struct sway_container *focus = seat_get_focus(seat);
162 if (focus && c->type == C_WORKSPACE) { 163 if (focus && c->type == C_WORKSPACE) {
163 // Only follow the mouse if it would move to a new output 164 // Only follow the mouse if it would move to a new output
164 // Otherwise we'll focus the workspace, which is probably wrong 165 // Otherwise we'll focus the workspace, which is probably wrong
@@ -170,20 +171,20 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec,
170 output = container_parent(c, C_OUTPUT); 171 output = container_parent(c, C_OUTPUT);
171 } 172 }
172 if (output != focus) { 173 if (output != focus) {
173 seat_set_focus_warp(cursor->seat, c, false); 174 seat_set_focus_warp(seat, c, false);
174 } 175 }
175 } else if (c->type == C_VIEW) { 176 } else if (c->type == C_VIEW) {
176 // Focus c if both of the following are true: 177 // Focus c if both of the following are true:
177 // - cursor is over a new view, i.e. entered a new window; and 178 // - cursor is over a new view, i.e. entered a new window; and
178 // - the new view is visible, i.e. not hidden in a stack or tab. 179 // - the new view is visible, i.e. not hidden in a stack or tab.
179 if (c != prev_c && view_is_visible(c->sway_view)) { 180 if (c != prev_c && view_is_visible(c->sway_view)) {
180 seat_set_focus_warp(cursor->seat, c, false); 181 seat_set_focus_warp(seat, c, false);
181 } else { 182 } else {
182 struct sway_container *next_focus = 183 struct sway_container *next_focus =
183 seat_get_focus_inactive(cursor->seat, &root_container); 184 seat_get_focus_inactive(seat, &root_container);
184 if (next_focus && next_focus->type == C_VIEW && 185 if (next_focus && next_focus->type == C_VIEW &&
185 view_is_visible(next_focus->sway_view)) { 186 view_is_visible(next_focus->sway_view)) {
186 seat_set_focus_warp(cursor->seat, next_focus, false); 187 seat_set_focus_warp(seat, next_focus, false);
187 } 188 }
188 } 189 }
189 } 190 }
@@ -202,12 +203,18 @@ void cursor_send_pointer_motion(struct sway_cursor *cursor, uint32_t time_msec,
202 203
203 // send pointer enter/leave 204 // send pointer enter/leave
204 if (surface != NULL) { 205 if (surface != NULL) {
205 if (seat_is_input_allowed(cursor->seat, surface)) { 206 if (seat_is_input_allowed(seat, surface)) {
206 wlr_seat_pointer_notify_enter(seat, surface, sx, sy); 207 wlr_seat_pointer_notify_enter(wlr_seat, surface, sx, sy);
207 wlr_seat_pointer_notify_motion(seat, time_msec, sx, sy); 208 wlr_seat_pointer_notify_motion(wlr_seat, time_msec, sx, sy);
208 } 209 }
209 } else { 210 } else {
210 wlr_seat_pointer_clear_focus(seat); 211 wlr_seat_pointer_clear_focus(wlr_seat);
212 }
213
214 struct wlr_drag_icon *wlr_drag_icon;
215 wl_list_for_each(wlr_drag_icon, &wlr_seat->drag_icons, link) {
216 struct sway_drag_icon *drag_icon = wlr_drag_icon->data;
217 drag_icon_update_position(drag_icon);
211 } 218 }
212} 219}
213 220
@@ -293,22 +300,27 @@ static void handle_touch_down(struct wl_listener *listener, void *data) {
293 wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); 300 wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat);
294 struct wlr_event_touch_down *event = data; 301 struct wlr_event_touch_down *event = data;
295 302
296 struct wlr_seat *seat = cursor->seat->wlr_seat; 303 struct sway_seat *seat = cursor->seat;
304 struct wlr_seat *wlr_seat = seat->wlr_seat;
297 struct wlr_surface *surface = NULL; 305 struct wlr_surface *surface = NULL;
298 306
299 double lx, ly; 307 double lx, ly;
300 wlr_cursor_absolute_to_layout_coords(cursor->cursor, event->device, 308 wlr_cursor_absolute_to_layout_coords(cursor->cursor, event->device,
301 event->x, event->y, &lx, &ly); 309 event->x, event->y, &lx, &ly);
302 double sx, sy; 310 double sx, sy;
303 container_at_coords(cursor->seat, lx, ly, &surface, &sx, &sy); 311 container_at_coords(seat, lx, ly, &surface, &sx, &sy);
312
313 seat->touch_id = event->touch_id;
314 seat->touch_x = lx;
315 seat->touch_y = ly;
304 316
305 if (!surface) { 317 if (!surface) {
306 return; 318 return;
307 } 319 }
308 320
309 // TODO: fall back to cursor simulation if client has not bound to touch 321 // TODO: fall back to cursor simulation if client has not bound to touch
310 if (seat_is_input_allowed(cursor->seat, surface)) { 322 if (seat_is_input_allowed(seat, surface)) {
311 wlr_seat_touch_notify_down(seat, surface, event->time_msec, 323 wlr_seat_touch_notify_down(wlr_seat, surface, event->time_msec,
312 event->touch_id, sx, sy); 324 event->touch_id, sx, sy);
313 cursor->image_client = NULL; 325 cursor->image_client = NULL;
314 wlr_cursor_set_image(cursor->cursor, NULL, 0, 0, 0, 0, 0, 0); 326 wlr_cursor_set_image(cursor->cursor, NULL, 0, 0, 0, 0, 0, 0);
@@ -330,7 +342,8 @@ static void handle_touch_motion(struct wl_listener *listener, void *data) {
330 wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat); 342 wlr_idle_notify_activity(cursor->seat->input->server->idle, cursor->seat->wlr_seat);
331 struct wlr_event_touch_motion *event = data; 343 struct wlr_event_touch_motion *event = data;
332 344
333 struct wlr_seat *seat = cursor->seat->wlr_seat; 345 struct sway_seat *seat = cursor->seat;
346 struct wlr_seat *wlr_seat = seat->wlr_seat;
334 struct wlr_surface *surface = NULL; 347 struct wlr_surface *surface = NULL;
335 348
336 double lx, ly; 349 double lx, ly;
@@ -339,14 +352,25 @@ static void handle_touch_motion(struct wl_listener *listener, void *data) {
339 double sx, sy; 352 double sx, sy;
340 container_at_coords(cursor->seat, lx, ly, &surface, &sx, &sy); 353 container_at_coords(cursor->seat, lx, ly, &surface, &sx, &sy);
341 354
355 if (seat->touch_id == event->touch_id) {
356 seat->touch_x = lx;
357 seat->touch_y = ly;
358
359 struct wlr_drag_icon *wlr_drag_icon;
360 wl_list_for_each(wlr_drag_icon, &wlr_seat->drag_icons, link) {
361 struct sway_drag_icon *drag_icon = wlr_drag_icon->data;
362 drag_icon_update_position(drag_icon);
363 }
364 }
365
342 if (!surface) { 366 if (!surface) {
343 return; 367 return;
344 } 368 }
345 369
346 // TODO: fall back to cursor simulation if client has not bound to touch 370 // TODO: fall back to cursor simulation if client has not bound to touch
347 if (seat_is_input_allowed(cursor->seat, surface)) { 371 if (seat_is_input_allowed(cursor->seat, surface)) {
348 wlr_seat_touch_notify_motion( 372 wlr_seat_touch_notify_motion(wlr_seat, event->time_msec,
349 seat, event->time_msec, event->touch_id, sx, sy); 373 event->touch_id, sx, sy);
350 } 374 }
351} 375}
352 376
diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c
index d90655f2..9e093828 100644
--- a/sway/input/keyboard.c
+++ b/sway/input/keyboard.c
@@ -10,85 +10,111 @@
10#include "log.h" 10#include "log.h"
11 11
12/** 12/**
13 * Remove all key ids associated to a keycode from the list of pressed keys
14 */
15static void state_erase_key(struct sway_shortcut_state *state,
16 uint32_t keycode) {
17 size_t j = 0;
18 for (size_t i = 0; i < state->npressed; ++i) {
19 if (i > j) {
20 state->pressed_keys[j] = state->pressed_keys[i];
21 state->pressed_keycodes[j] = state->pressed_keycodes[i];
22 }
23 if (state->pressed_keycodes[i] != keycode) {
24 ++j;
25 }
26 }
27 while(state->npressed > j) {
28 --state->npressed;
29 state->pressed_keys[state->npressed] = 0;
30 state->pressed_keycodes[state->npressed] = 0;
31 }
32}
33
34/**
35 * Add a key id (with associated keycode) to the list of pressed keys,
36 * if the list is not full.
37 */
38static void state_add_key(struct sway_shortcut_state *state,
39 uint32_t keycode, uint32_t key_id) {
40 if (state->npressed >= SWAY_KEYBOARD_PRESSED_KEYS_CAP) {
41 return;
42 }
43 size_t i = 0;
44 while (i < state->npressed && state->pressed_keys[i] < key_id) {
45 ++i;
46 }
47 size_t j = state->npressed;
48 while (j > i) {
49 state->pressed_keys[j] = state->pressed_keys[j - 1];
50 state->pressed_keycodes[j] = state->pressed_keycodes[j - 1];
51 --j;
52 }
53 state->pressed_keys[i] = key_id;
54 state->pressed_keycodes[i] = keycode;
55 state->npressed++;
56}
57
58/**
13 * Update the shortcut model state in response to new input 59 * Update the shortcut model state in response to new input
14 */ 60 */
15static void update_shortcut_state(struct sway_shortcut_state *state, 61static void update_shortcut_state(struct sway_shortcut_state *state,
16 struct wlr_event_keyboard_key *event, uint32_t new_key, 62 struct wlr_event_keyboard_key *event, uint32_t new_key,
17 bool last_key_was_a_modifier) { 63 uint32_t raw_modifiers) {
64 bool last_key_was_a_modifier = raw_modifiers != state->last_raw_modifiers;
65 state->last_raw_modifiers = raw_modifiers;
66
18 if (event->state == WLR_KEY_PRESSED) { 67 if (event->state == WLR_KEY_PRESSED) {
19 if (last_key_was_a_modifier && state->last_key_index >= 0) { 68 if (last_key_was_a_modifier && state->last_keycode) {
20 // Last pressed key before this one was a modifier 69 // Last pressed key before this one was a modifier
21 state->pressed_keycodes[state->last_key_index] = 0; 70 state_erase_key(state, state->last_keycode);
22 state->pressed_keys[state->last_key_index] = 0;
23 state->last_key_index = -1;
24 } 71 }
25 72
26 // Add current key to set; there may be duplicates 73 // Add current key to set; there may be duplicates
27 for (size_t i = 0; i < SWAY_KEYBOARD_PRESSED_KEYS_CAP; ++i) { 74 state_add_key(state, event->keycode, new_key);
28 if (!state->pressed_keys[i]) { 75 state->last_keycode = event->keycode;
29 state->pressed_keys[i] = new_key;
30 state->pressed_keycodes[i] = event->keycode;
31 state->last_key_index = i;
32 break;
33 }
34 }
35 } else { 76 } else {
36 for (size_t i = 0; i < SWAY_KEYBOARD_PRESSED_KEYS_CAP; ++i) { 77 state_erase_key(state, event->keycode);
37 // The same keycode may match multiple keysyms.
38 if (state->pressed_keycodes[i] == event->keycode) {
39 state->pressed_keys[i] = 0;
40 state->pressed_keycodes[i] = 0;
41 }
42 }
43 } 78 }
44} 79}
45 80
46/** 81/**
47 * 82 * If one exists, finds a binding which matches the shortcut model state,
48 * Returns a binding which matches the shortcut model state (ignoring the 83 * current modifiers, release state, and locked state.
49 * `release` flag).
50 */ 84 */
51static struct sway_binding *get_active_binding( 85static void get_active_binding(const struct sway_shortcut_state *state,
52 struct sway_shortcut_state *state, list_t *bindings, 86 list_t *bindings, struct sway_binding **current_binding,
53 uint32_t modifiers, bool locked) { 87 uint32_t modifiers, bool release, bool locked) {
54 int npressed_keys = 0;
55 for (size_t i = 0; i < SWAY_KEYBOARD_PRESSED_KEYS_CAP; ++i) {
56 if (state->pressed_keys[i]) {
57 ++npressed_keys;
58 }
59 }
60 for (int i = 0; i < bindings->length; ++i) { 88 for (int i = 0; i < bindings->length; ++i) {
61 struct sway_binding *binding = bindings->items[i]; 89 struct sway_binding *binding = bindings->items[i];
62 90
63 if (modifiers ^ binding->modifiers || 91 if (modifiers ^ binding->modifiers ||
64 npressed_keys != binding->keys->length || 92 state->npressed != (size_t)binding->keys->length ||
65 locked > binding->locked) { 93 locked > binding->locked ||
94 release != binding->release) {
66 continue; 95 continue;
67 } 96 }
68 97
69 bool match = true; 98 bool match = true;
70 for (int j = 0; j < binding->keys->length; ++j) { 99 for (size_t j = 0; j < state->npressed; j++) {
71 uint32_t key = *(uint32_t *)binding->keys->items[j]; 100 uint32_t key = *(uint32_t *)binding->keys->items[j];
72 101 if (key != state->pressed_keys[j]) {
73 bool key_found = false;
74 for (int k = 0; k < SWAY_KEYBOARD_PRESSED_KEYS_CAP; ++k) {
75 if (state->pressed_keys[k] == key) {
76 key_found = true;
77 break;
78 }
79 }
80 if (!key_found) {
81 match = false; 102 match = false;
82 break; 103 break;
83 } 104 }
84 } 105 }
106 if (!match) {
107 continue;
108 }
85 109
86 if (match) { 110 if (*current_binding && *current_binding != binding) {
87 return binding; 111 wlr_log(L_DEBUG, "encountered duplicate bindings %d and %d",
112 (*current_binding)->order, binding->order);
113 } else {
114 *current_binding = binding;
88 } 115 }
116 return;
89 } 117 }
90
91 return NULL;
92} 118}
93 119
94/** 120/**
@@ -204,69 +230,65 @@ static void handle_keyboard_key(struct wl_listener *listener, void *data) {
204 size_t raw_keysyms_len = 230 size_t raw_keysyms_len =
205 keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers); 231 keyboard_keysyms_raw(keyboard, keycode, &raw_keysyms, &raw_modifiers);
206 232
207 struct wlr_input_device *device = 233 uint32_t code_modifiers = wlr_keyboard_get_modifiers(wlr_device->keyboard);
208 keyboard->seat_device->input_device->wlr_device;
209 uint32_t code_modifiers = wlr_keyboard_get_modifiers(device->keyboard);
210
211 bool last_key_was_a_modifier = code_modifiers != keyboard->last_modifiers;
212 keyboard->last_modifiers = code_modifiers;
213 234
214 // Update shortcut model state 235 // Update shortcut model state
215 update_shortcut_state(&keyboard->state_keycodes, event, 236 update_shortcut_state(&keyboard->state_keycodes, event,
216 (uint32_t)keycode, last_key_was_a_modifier); 237 (uint32_t)keycode, code_modifiers);
217 for (size_t i = 0; i < translated_keysyms_len; ++i) { 238 for (size_t i = 0; i < translated_keysyms_len; ++i) {
218 update_shortcut_state(&keyboard->state_keysyms_translated, 239 update_shortcut_state(&keyboard->state_keysyms_translated,
219 event, (uint32_t)translated_keysyms[i], 240 event, (uint32_t)translated_keysyms[i],
220 last_key_was_a_modifier && i == 0); 241 code_modifiers);
221 } 242 }
222 for (size_t i = 0; i < raw_keysyms_len; ++i) { 243 for (size_t i = 0; i < raw_keysyms_len; ++i) {
223 update_shortcut_state(&keyboard->state_keysyms_raw, 244 update_shortcut_state(&keyboard->state_keysyms_raw,
224 event, (uint32_t)raw_keysyms[i], 245 event, (uint32_t)raw_keysyms[i],
225 last_key_was_a_modifier && i == 0); 246 code_modifiers);
226 } 247 }
227 248
228 // identify which binding should be executed.
229 struct sway_binding *binding = get_active_binding(
230 &keyboard->state_keycodes,
231 config->current_mode->keycode_bindings,
232 code_modifiers, input_inhibited);
233 struct sway_binding *translated_binding = get_active_binding(
234 &keyboard->state_keysyms_translated,
235 config->current_mode->keysym_bindings,
236 translated_modifiers, input_inhibited);
237 if (translated_binding && !binding) {
238 binding = translated_binding;
239 } else if (binding && translated_binding && binding != translated_binding) {
240 wlr_log(L_DEBUG, "encountered duplicate bindings %d and %d",
241 binding->order, translated_binding->order);
242 }
243 struct sway_binding *raw_binding = get_active_binding(
244 &keyboard->state_keysyms_raw,
245 config->current_mode->keysym_bindings,
246 raw_modifiers, input_inhibited);
247 if (raw_binding && !binding) {
248 binding = raw_binding;
249 } else if (binding && raw_binding && binding != raw_binding) {
250 wlr_log(L_DEBUG, "encountered duplicate bindings %d and %d",
251 binding->order, raw_binding->order);
252 }
253 249
254 bool handled = false; 250 bool handled = false;
255 251
256 // Execute the identified binding if need be. 252 // Identify active release binding
257 if (keyboard->held_binding && binding != keyboard->held_binding && 253 struct sway_binding *binding_released = NULL;
254 get_active_binding(&keyboard->state_keycodes,
255 config->current_mode->keycode_bindings, &binding_released,
256 code_modifiers, true, input_inhibited);
257 get_active_binding(&keyboard->state_keysyms_translated,
258 config->current_mode->keysym_bindings, &binding_released,
259 translated_modifiers, true, input_inhibited);
260 get_active_binding(&keyboard->state_keysyms_raw,
261 config->current_mode->keysym_bindings, &binding_released,
262 raw_modifiers, true, input_inhibited);
263
264 // Execute stored release binding once no longer active
265 if (keyboard->held_binding && binding_released != keyboard->held_binding &&
258 event->state == WLR_KEY_RELEASED) { 266 event->state == WLR_KEY_RELEASED) {
259 keyboard_execute_command(keyboard, keyboard->held_binding); 267 keyboard_execute_command(keyboard, keyboard->held_binding);
260 handled = true; 268 handled = true;
261 } 269 }
262 if (binding != keyboard->held_binding) { 270 if (binding_released != keyboard->held_binding) {
263 keyboard->held_binding = NULL; 271 keyboard->held_binding = NULL;
264 } 272 }
265 if (binding && event->state == WLR_KEY_PRESSED) { 273 if (binding_released && event->state == WLR_KEY_PRESSED) {
266 if (binding->release) { 274 keyboard->held_binding = binding_released;
267 keyboard->held_binding = binding; 275 }
268 } else { 276
269 keyboard_execute_command(keyboard, binding); 277 // Identify and execute active pressed binding
278 if (event->state == WLR_KEY_PRESSED) {
279 struct sway_binding *binding_pressed = NULL;
280 get_active_binding(&keyboard->state_keycodes,
281 config->current_mode->keycode_bindings, &binding_pressed,
282 code_modifiers, false, input_inhibited);
283 get_active_binding(&keyboard->state_keysyms_translated,
284 config->current_mode->keysym_bindings, &binding_pressed,
285 translated_modifiers, false, input_inhibited);
286 get_active_binding(&keyboard->state_keysyms_raw,
287 config->current_mode->keysym_bindings, &binding_pressed,
288 raw_modifiers, false, input_inhibited);
289
290 if (binding_pressed) {
291 keyboard_execute_command(keyboard, binding_pressed);
270 handled = true; 292 handled = true;
271 } 293 }
272 } 294 }
@@ -315,10 +337,6 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat,
315 wl_list_init(&keyboard->keyboard_key.link); 337 wl_list_init(&keyboard->keyboard_key.link);
316 wl_list_init(&keyboard->keyboard_modifiers.link); 338 wl_list_init(&keyboard->keyboard_modifiers.link);
317 339
318 keyboard->state_keycodes.last_key_index = -1;
319 keyboard->state_keysyms_raw.last_key_index = -1;
320 keyboard->state_keysyms_translated.last_key_index = -1;
321
322 return keyboard; 340 return keyboard;
323} 341}
324 342
diff --git a/sway/input/seat.c b/sway/input/seat.c
index 071ef020..1ea36466 100644
--- a/sway/input/seat.c
+++ b/sway/input/seat.c
@@ -6,21 +6,22 @@
6#include <wlr/types/wlr_cursor.h> 6#include <wlr/types/wlr_cursor.h>
7#include <wlr/types/wlr_output_layout.h> 7#include <wlr/types/wlr_output_layout.h>
8#include <wlr/types/wlr_xcursor_manager.h> 8#include <wlr/types/wlr_xcursor_manager.h>
9#include "log.h"
9#include "sway/debug.h" 10#include "sway/debug.h"
10#include "sway/tree/container.h" 11#include "sway/desktop.h"
11#include "sway/tree/workspace.h"
12#include "sway/input/seat.h"
13#include "sway/input/cursor.h" 12#include "sway/input/cursor.h"
14#include "sway/input/input-manager.h" 13#include "sway/input/input-manager.h"
15#include "sway/input/keyboard.h" 14#include "sway/input/keyboard.h"
15#include "sway/input/seat.h"
16#include "sway/ipc-server.h" 16#include "sway/ipc-server.h"
17#include "sway/layers.h" 17#include "sway/layers.h"
18#include "sway/output.h" 18#include "sway/output.h"
19#include "sway/tree/arrange.h" 19#include "sway/tree/arrange.h"
20#include "sway/tree/container.h" 20#include "sway/tree/container.h"
21#include "sway/tree/container.h"
21#include "sway/tree/view.h" 22#include "sway/tree/view.h"
22#include "sway/tree/workspace.h" 23#include "sway/tree/workspace.h"
23#include "log.h" 24#include "sway/tree/workspace.h"
24 25
25static void seat_device_destroy(struct sway_seat_device *seat_device) { 26static void seat_device_destroy(struct sway_seat_device *seat_device) {
26 if (!seat_device) { 27 if (!seat_device) {
@@ -40,6 +41,8 @@ void seat_destroy(struct sway_seat *seat) {
40 seat_device_destroy(seat_device); 41 seat_device_destroy(seat_device);
41 } 42 }
42 sway_cursor_destroy(seat->cursor); 43 sway_cursor_destroy(seat->cursor);
44 wl_list_remove(&seat->new_container.link);
45 wl_list_remove(&seat->new_drag_icon.link);
43 wl_list_remove(&seat->link); 46 wl_list_remove(&seat->link);
44 wlr_seat_destroy(seat->wlr_seat); 47 wlr_seat_destroy(seat->wlr_seat);
45} 48}
@@ -234,6 +237,90 @@ static void handle_new_container(struct wl_listener *listener, void *data) {
234 seat_container_from_container(seat, con); 237 seat_container_from_container(seat, con);
235} 238}
236 239
240static void drag_icon_damage_whole(struct sway_drag_icon *icon) {
241 if (!icon->wlr_drag_icon->mapped) {
242 return;
243 }
244 desktop_damage_surface(icon->wlr_drag_icon->surface, icon->x, icon->y, true);
245}
246
247void drag_icon_update_position(struct sway_drag_icon *icon) {
248 drag_icon_damage_whole(icon);
249
250 struct wlr_drag_icon *wlr_icon = icon->wlr_drag_icon;
251 struct sway_seat *seat = icon->seat;
252 struct wlr_cursor *cursor = seat->cursor->cursor;
253 if (wlr_icon->is_pointer) {
254 icon->x = cursor->x + wlr_icon->sx;
255 icon->y = cursor->y + wlr_icon->sy;
256 } else {
257 struct wlr_touch_point *point =
258 wlr_seat_touch_get_point(seat->wlr_seat, wlr_icon->touch_id);
259 if (point == NULL) {
260 return;
261 }
262 icon->x = seat->touch_x + wlr_icon->sx;
263 icon->y = seat->touch_y + wlr_icon->sy;
264 }
265
266 drag_icon_damage_whole(icon);
267}
268
269static void drag_icon_handle_surface_commit(struct wl_listener *listener,
270 void *data) {
271 struct sway_drag_icon *icon =
272 wl_container_of(listener, icon, surface_commit);
273 drag_icon_update_position(icon);
274}
275
276static void drag_icon_handle_map(struct wl_listener *listener, void *data) {
277 struct sway_drag_icon *icon = wl_container_of(listener, icon, map);
278 drag_icon_damage_whole(icon);
279}
280
281static void drag_icon_handle_unmap(struct wl_listener *listener, void *data) {
282 struct sway_drag_icon *icon = wl_container_of(listener, icon, unmap);
283 drag_icon_damage_whole(icon);
284}
285
286static void drag_icon_handle_destroy(struct wl_listener *listener,
287 void *data) {
288 struct sway_drag_icon *icon = wl_container_of(listener, icon, destroy);
289 icon->wlr_drag_icon->data = NULL;
290 wl_list_remove(&icon->link);
291 wl_list_remove(&icon->surface_commit.link);
292 wl_list_remove(&icon->unmap.link);
293 wl_list_remove(&icon->destroy.link);
294 free(icon);
295}
296
297static void handle_new_drag_icon(struct wl_listener *listener, void *data) {
298 struct sway_seat *seat = wl_container_of(listener, seat, new_drag_icon);
299 struct wlr_drag_icon *wlr_drag_icon = data;
300
301 struct sway_drag_icon *icon = calloc(1, sizeof(struct sway_drag_icon));
302 if (icon == NULL) {
303 wlr_log(L_ERROR, "Allocation failed");
304 return;
305 }
306 icon->seat = seat;
307 icon->wlr_drag_icon = wlr_drag_icon;
308 wlr_drag_icon->data = icon;
309
310 icon->surface_commit.notify = drag_icon_handle_surface_commit;
311 wl_signal_add(&wlr_drag_icon->surface->events.commit, &icon->surface_commit);
312 icon->unmap.notify = drag_icon_handle_unmap;
313 wl_signal_add(&wlr_drag_icon->events.unmap, &icon->unmap);
314 icon->map.notify = drag_icon_handle_map;
315 wl_signal_add(&wlr_drag_icon->events.map, &icon->map);
316 icon->destroy.notify = drag_icon_handle_destroy;
317 wl_signal_add(&wlr_drag_icon->events.destroy, &icon->destroy);
318
319 wl_list_insert(&root_container.sway_root->drag_icons, &icon->link);
320
321 drag_icon_update_position(icon);
322}
323
237static void collect_focus_iter(struct sway_container *con, void *data) { 324static void collect_focus_iter(struct sway_container *con, void *data) {
238 struct sway_seat *seat = data; 325 struct sway_seat *seat = data;
239 if (con->type > C_WORKSPACE) { 326 if (con->type > C_WORKSPACE) {
@@ -278,6 +365,9 @@ struct sway_seat *seat_create(struct sway_input_manager *input,
278 &seat->new_container); 365 &seat->new_container);
279 seat->new_container.notify = handle_new_container; 366 seat->new_container.notify = handle_new_container;
280 367
368 wl_signal_add(&seat->wlr_seat->events.new_drag_icon, &seat->new_drag_icon);
369 seat->new_drag_icon.notify = handle_new_drag_icon;
370
281 seat->input = input; 371 seat->input = input;
282 wl_list_init(&seat->devices); 372 wl_list_init(&seat->devices);
283 373
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index a8aba618..d1ad044d 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -37,6 +37,7 @@ void layout_init(void) {
37 root_container.sway_root->output_layout = wlr_output_layout_create(); 37 root_container.sway_root->output_layout = wlr_output_layout_create();
38 wl_list_init(&root_container.sway_root->outputs); 38 wl_list_init(&root_container.sway_root->outputs);
39 wl_list_init(&root_container.sway_root->xwayland_unmanaged); 39 wl_list_init(&root_container.sway_root->xwayland_unmanaged);
40 wl_list_init(&root_container.sway_root->drag_icons);
40 wl_signal_init(&root_container.sway_root->events.new_container); 41 wl_signal_init(&root_container.sway_root->events.new_container);
41 42
42 root_container.sway_root->output_layout_change.notify = 43 root_container.sway_root->output_layout_change.notify =
diff --git a/swayidle/main.c b/swayidle/main.c
index d83ab98d..7666578f 100644
--- a/swayidle/main.c
+++ b/swayidle/main.c
@@ -127,7 +127,8 @@ void acquire_sleep_lock() {
127 127
128static int prepare_for_sleep(sd_bus_message *msg, void *userdata, 128static int prepare_for_sleep(sd_bus_message *msg, void *userdata,
129 sd_bus_error *ret_error) { 129 sd_bus_error *ret_error) {
130 bool going_down = true; 130 /* "b" apparently reads into an int, not a bool */
131 int going_down = 1;
131 int ret = sd_bus_message_read(msg, "b", &going_down); 132 int ret = sd_bus_message_read(msg, "b", &going_down);
132 if (ret < 0) { 133 if (ret < 0) {
133 wlr_log(L_ERROR, "Failed to parse D-Bus response for Inhibit: %s", 134 wlr_log(L_ERROR, "Failed to parse D-Bus response for Inhibit: %s",