aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/bar.c
diff options
context:
space:
mode:
authorLibravatar Ian Fan <ianfan0@gmail.com>2018-10-17 20:21:27 +0100
committerLibravatar Ian Fan <ianfan0@gmail.com>2018-10-18 14:19:00 +0100
commit499150a91b706b9829ca763ede9b97c573b51cb7 (patch)
tree09ff2da2b266f152e590db839f0c5e2016888552 /swaybar/bar.c
parentMerge pull request #2871 from RyanDwyer/untangle-cursor-warp (diff)
downloadsway-499150a91b706b9829ca763ede9b97c573b51cb7.tar.gz
sway-499150a91b706b9829ca763ede9b97c573b51cb7.tar.zst
sway-499150a91b706b9829ca763ede9b97c573b51cb7.zip
swaybar: separate input code to new file
Diffstat (limited to 'swaybar/bar.c')
-rw-r--r--swaybar/bar.c248
1 files changed, 1 insertions, 247 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c
index 0deba72d..5e9767b2 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -11,14 +11,10 @@
11#include <wayland-client.h> 11#include <wayland-client.h>
12#include <wayland-cursor.h> 12#include <wayland-cursor.h>
13#include <wlr/util/log.h> 13#include <wlr/util/log.h>
14#ifdef __FreeBSD__
15#include <dev/evdev/input-event-codes.h>
16#else
17#include <linux/input-event-codes.h>
18#endif
19#include "swaybar/bar.h" 14#include "swaybar/bar.h"
20#include "swaybar/config.h" 15#include "swaybar/config.h"
21#include "swaybar/i3bar.h" 16#include "swaybar/i3bar.h"
17#include "swaybar/input.h"
22#include "swaybar/ipc.h" 18#include "swaybar/ipc.h"
23#include "swaybar/status_line.h" 19#include "swaybar/status_line.h"
24#include "swaybar/render.h" 20#include "swaybar/render.h"
@@ -36,17 +32,6 @@ static void bar_init(struct swaybar *bar) {
36 wl_list_init(&bar->outputs); 32 wl_list_init(&bar->outputs);
37} 33}
38 34
39void free_hotspots(struct wl_list *list) {
40 struct swaybar_hotspot *hotspot, *tmp;
41 wl_list_for_each_safe(hotspot, tmp, list, link) {
42 wl_list_remove(&hotspot->link);
43 if (hotspot->destroy) {
44 hotspot->destroy(hotspot->data);
45 }
46 free(hotspot);
47 }
48}
49
50void free_workspaces(struct wl_list *list) { 35void free_workspaces(struct wl_list *list) {
51 struct swaybar_workspace *ws, *tmp; 36 struct swaybar_workspace *ws, *tmp;
52 wl_list_for_each_safe(ws, tmp, list, link) { 37 wl_list_for_each_safe(ws, tmp, list, link) {
@@ -107,237 +92,6 @@ struct zwlr_layer_surface_v1_listener layer_surface_listener = {
107 .closed = layer_surface_closed, 92 .closed = layer_surface_closed,
108}; 93};
109 94
110static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
111 uint32_t serial, struct wl_surface *surface,
112 wl_fixed_t surface_x, wl_fixed_t surface_y) {
113 struct swaybar *bar = data;
114 struct swaybar_pointer *pointer = &bar->pointer;
115 struct swaybar_output *output;
116 wl_list_for_each(output, &bar->outputs, link) {
117 if (output->surface == surface) {
118 pointer->current = output;
119 break;
120 }
121 }
122 int max_scale = 1;
123 struct swaybar_output *_output;
124 wl_list_for_each(_output, &bar->outputs, link) {
125 if (_output->scale > max_scale) {
126 max_scale = _output->scale;
127 }
128 }
129 wl_surface_set_buffer_scale(pointer->cursor_surface, max_scale);
130 wl_surface_attach(pointer->cursor_surface,
131 wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
132 wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
133 pointer->cursor_image->hotspot_x / max_scale,
134 pointer->cursor_image->hotspot_y / max_scale);
135 wl_surface_commit(pointer->cursor_surface);
136}
137
138static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
139 uint32_t serial, struct wl_surface *surface) {
140 struct swaybar *bar = data;
141 bar->pointer.current = NULL;
142}
143
144static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
145 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
146 struct swaybar *bar = data;
147 bar->pointer.x = wl_fixed_to_int(surface_x);
148 bar->pointer.y = wl_fixed_to_int(surface_y);
149}
150
151static bool check_bindings(struct swaybar *bar, uint32_t x11_button,
152 uint32_t state) {
153 bool released = state == WL_POINTER_BUTTON_STATE_RELEASED;
154 for (int i = 0; i < bar->config->bindings->length; i++) {
155 struct swaybar_binding *binding = bar->config->bindings->items[i];
156 if (binding->button == x11_button && binding->release == released) {
157 ipc_execute_binding(bar, binding);
158 return true;
159 }
160 }
161 return false;
162}
163
164static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
165 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
166 struct swaybar *bar = data;
167 struct swaybar_pointer *pointer = &bar->pointer;
168 struct swaybar_output *output = pointer->current;
169 if (!sway_assert(output, "button with no active output")) {
170 return;
171 }
172
173 if (check_bindings(bar, wl_button_to_x11_button(button), state)) {
174 return;
175 }
176
177 if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
178 return;
179 }
180 struct swaybar_hotspot *hotspot;
181 wl_list_for_each(hotspot, &output->hotspots, link) {
182 double x = pointer->x * output->scale;
183 double y = pointer->y * output->scale;
184 if (x >= hotspot->x
185 && y >= hotspot->y
186 && x < hotspot->x + hotspot->width
187 && y < hotspot->y + hotspot->height) {
188 if (HOTSPOT_IGNORE == hotspot->callback(output, pointer->x, pointer->y,
189 wl_button_to_x11_button(button), hotspot->data)) {
190 return;
191 }
192 }
193 }
194}
195
196static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
197 uint32_t time, uint32_t axis, wl_fixed_t value) {
198 struct swaybar *bar = data;
199 struct swaybar_pointer *pointer = &bar->pointer;
200 struct swaybar_output *output = bar->pointer.current;
201 if (!sway_assert(output, "axis with no active output")) {
202 return;
203 }
204
205 // If there is a button press binding, execute it, skip default behavior,
206 // and check button release bindings
207 if (check_bindings(bar, wl_axis_to_x11_button(axis, value),
208 WL_POINTER_BUTTON_STATE_PRESSED)) {
209 check_bindings(bar, wl_axis_to_x11_button(axis, value),
210 WL_POINTER_BUTTON_STATE_RELEASED);
211 return;
212 }
213
214 struct swaybar_hotspot *hotspot;
215 wl_list_for_each(hotspot, &output->hotspots, link) {
216 double x = pointer->x * output->scale;
217 double y = pointer->y * output->scale;
218 if (x >= hotspot->x
219 && y >= hotspot->y
220 && x < hotspot->x + hotspot->width
221 && y < hotspot->y + hotspot->height) {
222 if (HOTSPOT_IGNORE == hotspot->callback(
223 output, pointer->x, pointer->y,
224 wl_axis_to_x11_button(axis, value), hotspot->data)) {
225 return;
226 }
227 }
228 }
229
230 double amt = wl_fixed_to_double(value);
231 if (amt == 0.0) {
232 return;
233 }
234
235 // last doesn't actually need initialization,
236 // but gcc (7.3.1) is too dumb to figure it out
237 struct swaybar_workspace *first = NULL;
238 struct swaybar_workspace *active = NULL;
239 struct swaybar_workspace *last = NULL;
240
241 struct swaybar_workspace *iter;
242 wl_list_for_each(iter, &output->workspaces, link) {
243 if (!first) {
244 first = iter;
245 }
246
247 if (iter->visible) {
248 active = iter;
249 }
250
251 last = iter;
252 }
253
254 if (!sway_assert(active, "axis with null workspace")) {
255 return;
256 }
257
258 struct swaybar_workspace *new;
259
260 if (amt > 0.0) {
261 if (active == first) {
262 if (!bar->config->wrap_scroll) {
263 return;
264 }
265 new = last;
266 }
267
268 new = wl_container_of(active->link.prev, new, link);
269 } else {
270 if (active == last) {
271 if (!bar->config->wrap_scroll) {
272 return;
273 }
274 new = first;
275 }
276
277 new = wl_container_of(active->link.next, new, link);
278 }
279
280 ipc_send_workspace_command(bar, new->name);
281
282 // Check button release bindings
283 check_bindings(bar, wl_axis_to_x11_button(axis, value),
284 WL_POINTER_BUTTON_STATE_RELEASED);
285}
286
287static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
288 // Who cares
289}
290
291static void wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
292 uint32_t axis_source) {
293 // Who cares
294}
295
296static void wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
297 uint32_t time, uint32_t axis) {
298 // Who cares
299}
300
301static void wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
302 uint32_t axis, int32_t discrete) {
303 // Who cares
304}
305
306struct wl_pointer_listener pointer_listener = {
307 .enter = wl_pointer_enter,
308 .leave = wl_pointer_leave,
309 .motion = wl_pointer_motion,
310 .button = wl_pointer_button,
311 .axis = wl_pointer_axis,
312 .frame = wl_pointer_frame,
313 .axis_source = wl_pointer_axis_source,
314 .axis_stop = wl_pointer_axis_stop,
315 .axis_discrete = wl_pointer_axis_discrete,
316};
317
318static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
319 enum wl_seat_capability caps) {
320 struct swaybar *bar = data;
321 if (bar->pointer.pointer != NULL) {
322 wl_pointer_release(bar->pointer.pointer);
323 bar->pointer.pointer = NULL;
324 }
325 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
326 bar->pointer.pointer = wl_seat_get_pointer(wl_seat);
327 wl_pointer_add_listener(bar->pointer.pointer, &pointer_listener, bar);
328 }
329}
330
331static void seat_handle_name(void *data, struct wl_seat *wl_seat,
332 const char *name) {
333 // Who cares
334}
335
336const struct wl_seat_listener seat_listener = {
337 .capabilities = seat_handle_capabilities,
338 .name = seat_handle_name,
339};
340
341static void add_layer_surface(struct swaybar_output *output) { 95static void add_layer_surface(struct swaybar_output *output) {
342 if (output->layer_surface) { 96 if (output->layer_surface) {
343 return; 97 return;