aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar
diff options
context:
space:
mode:
authorLibravatar Hristo Venev <hristo@venev.name>2020-02-01 18:08:00 +0100
committerLibravatar Simon Ser <contact@emersion.fr>2020-02-10 18:58:09 +0100
commit7affe5c1bda53a2bb57295b7b6dbe4494e8c007b (patch)
treed55e64c43a785f15c6abf77d1891bb9d70b3e529 /swaybar
parentDo not truncate pointer coordinates to int. (diff)
downloadsway-7affe5c1bda53a2bb57295b7b6dbe4494e8c007b.tar.gz
sway-7affe5c1bda53a2bb57295b7b6dbe4494e8c007b.tar.zst
sway-7affe5c1bda53a2bb57295b7b6dbe4494e8c007b.zip
swaybar: fix i3bar relative coordinates when scaling is used
24e8ba048aef4751c6fa1d5982ee634f921e6cf6 did not take scaling into account. The hotspot size used pixel coordinates, the absolute coordinates were logical, and the relative coordinates were completely wrong. This commit makes all coordinates use logical values. If `"float_event_coords":true` is sent in the handshake message, coordinates are sent as floating-point values. The "scale" field is an integer containing the scale value.
Diffstat (limited to 'swaybar')
-rw-r--r--swaybar/i3bar.c26
-rw-r--r--swaybar/input.c35
-rw-r--r--swaybar/render.c12
-rw-r--r--swaybar/status_line.c7
-rw-r--r--swaybar/tray/item.c2
5 files changed, 47 insertions, 35 deletions
diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c
index 5c8b87a2..4bcd5843 100644
--- a/swaybar/i3bar.c
+++ b/swaybar/i3bar.c
@@ -267,8 +267,8 @@ bool i3bar_handle_readable(struct status_line *status) {
267} 267}
268 268
269enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, 269enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
270 struct i3bar_block *block, int x, int y, int rx, int ry, int w, int h, 270 struct i3bar_block *block, double x, double y, double rx, double ry,
271 uint32_t button) { 271 double w, double h, int scale, uint32_t button) {
272 sway_log(SWAY_DEBUG, "block %s clicked", block->name); 272 sway_log(SWAY_DEBUG, "block %s clicked", block->name);
273 if (!block->name || !status->click_events) { 273 if (!block->name || !status->click_events) {
274 return HOTSPOT_PROCESS; 274 return HOTSPOT_PROCESS;
@@ -285,12 +285,22 @@ enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
285 json_object_object_add(event_json, "button", 285 json_object_object_add(event_json, "button",
286 json_object_new_int(event_to_x11_button(button))); 286 json_object_new_int(event_to_x11_button(button)));
287 json_object_object_add(event_json, "event", json_object_new_int(button)); 287 json_object_object_add(event_json, "event", json_object_new_int(button));
288 json_object_object_add(event_json, "x", json_object_new_int(x)); 288 if (status->float_event_coords) {
289 json_object_object_add(event_json, "y", json_object_new_int(y)); 289 json_object_object_add(event_json, "x", json_object_new_double(x));
290 json_object_object_add(event_json, "relative_x", json_object_new_int(rx)); 290 json_object_object_add(event_json, "y", json_object_new_double(y));
291 json_object_object_add(event_json, "relative_y", json_object_new_int(ry)); 291 json_object_object_add(event_json, "relative_x", json_object_new_double(rx));
292 json_object_object_add(event_json, "width", json_object_new_int(w)); 292 json_object_object_add(event_json, "relative_y", json_object_new_double(ry));
293 json_object_object_add(event_json, "height", json_object_new_int(h)); 293 json_object_object_add(event_json, "width", json_object_new_double(w));
294 json_object_object_add(event_json, "height", json_object_new_double(h));
295 } else {
296 json_object_object_add(event_json, "x", json_object_new_int(x));
297 json_object_object_add(event_json, "y", json_object_new_int(y));
298 json_object_object_add(event_json, "relative_x", json_object_new_int(rx));
299 json_object_object_add(event_json, "relative_y", json_object_new_int(ry));
300 json_object_object_add(event_json, "width", json_object_new_int(w));
301 json_object_object_add(event_json, "height", json_object_new_int(h));
302 }
303 json_object_object_add(event_json, "scale", json_object_new_int(scale));
294 if (dprintf(status->write_fd, "%s%s\n", status->clicked ? "," : "", 304 if (dprintf(status->write_fd, "%s%s\n", status->clicked ? "," : "",
295 json_object_to_json_string(event_json)) < 0) { 305 json_object_to_json_string(event_json)) < 0) {
296 status_error(status, "[failed to write click event]"); 306 status_error(status, "[failed to write click event]");
diff --git a/swaybar/input.c b/swaybar/input.c
index aa6290fa..c0352300 100644
--- a/swaybar/input.c
+++ b/swaybar/input.c
@@ -138,21 +138,23 @@ static bool check_bindings(struct swaybar *bar, uint32_t button,
138 return false; 138 return false;
139} 139}
140 140
141static void process_hotspots(struct swaybar_output *output, 141static bool process_hotspots(struct swaybar_output *output,
142 double x, double y, uint32_t button) { 142 double x, double y, uint32_t button) {
143 x *= output->scale; 143 double px = x * output->scale;
144 y *= output->scale; 144 double py = y * output->scale;
145 struct swaybar_hotspot *hotspot; 145 struct swaybar_hotspot *hotspot;
146 wl_list_for_each(hotspot, &output->hotspots, link) { 146 wl_list_for_each(hotspot, &output->hotspots, link) {
147 if (x >= hotspot->x && y >= hotspot->y 147 if (px >= hotspot->x && py >= hotspot->y
148 && x < hotspot->x + hotspot->width 148 && px < hotspot->x + hotspot->width
149 && y < hotspot->y + hotspot->height) { 149 && py < hotspot->y + hotspot->height) {
150 if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, 150 if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, x, y,
151 x / output->scale, y / output->scale, button, hotspot->data)) { 151 button, hotspot->data)) {
152 return; 152 return true;
153 } 153 }
154 } 154 }
155 } 155 }
156
157 return false;
156} 158}
157 159
158static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, 160static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
@@ -229,19 +231,8 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
229 return; 231 return;
230 } 232 }
231 233
232 struct swaybar_hotspot *hotspot; 234 if (process_hotspots(output, pointer->x, pointer->y, button)) {
233 wl_list_for_each(hotspot, &output->hotspots, link) { 235 return;
234 double x = pointer->x * output->scale;
235 double y = pointer->y * output->scale;
236 if (x >= hotspot->x
237 && y >= hotspot->y
238 && x < hotspot->x + hotspot->width
239 && y < hotspot->y + hotspot->height) {
240 if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot,
241 pointer->x, pointer->y, button, hotspot->data)) {
242 return;
243 }
244 }
245 } 236 }
246 237
247 struct swaybar_config *config = seat->bar->config; 238 struct swaybar_config *config = seat->bar->config;
diff --git a/swaybar/render.c b/swaybar/render.c
index 06efb53c..ea5faef6 100644
--- a/swaybar/render.c
+++ b/swaybar/render.c
@@ -131,11 +131,15 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color,
131 131
132static enum hotspot_event_handling block_hotspot_callback( 132static enum hotspot_event_handling block_hotspot_callback(
133 struct swaybar_output *output, struct swaybar_hotspot *hotspot, 133 struct swaybar_output *output, struct swaybar_hotspot *hotspot,
134 int x, int y, uint32_t button, void *data) { 134 double x, double y, uint32_t button, void *data) {
135 struct i3bar_block *block = data; 135 struct i3bar_block *block = data;
136 struct status_line *status = output->bar->status; 136 struct status_line *status = output->bar->status;
137 return i3bar_block_send_click(status, block, x, y, x - hotspot->x, 137 return i3bar_block_send_click(status, block, x, y,
138 y - hotspot->y, hotspot->width, hotspot->height, button); 138 x - (double)hotspot->x / output->scale,
139 y - (double)hotspot->y / output->scale,
140 (double)hotspot->width / output->scale,
141 (double)hotspot->height / output->scale,
142 output->scale, button);
139} 143}
140 144
141static void i3bar_block_unref_callback(void *data) { 145static void i3bar_block_unref_callback(void *data) {
@@ -540,7 +544,7 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo,
540 544
541static enum hotspot_event_handling workspace_hotspot_callback( 545static enum hotspot_event_handling workspace_hotspot_callback(
542 struct swaybar_output *output, struct swaybar_hotspot *hotspot, 546 struct swaybar_output *output, struct swaybar_hotspot *hotspot,
543 int x, int y, uint32_t button, void *data) { 547 double x, double y, uint32_t button, void *data) {
544 if (button != BTN_LEFT) { 548 if (button != BTN_LEFT) {
545 return HOTSPOT_PROCESS; 549 return HOTSPOT_PROCESS;
546 } 550 }
diff --git a/swaybar/status_line.c b/swaybar/status_line.c
index 2a9e1da8..fb9271f8 100644
--- a/swaybar/status_line.c
+++ b/swaybar/status_line.c
@@ -85,6 +85,13 @@ bool status_handle_readable(struct status_line *status) {
85 } 85 }
86 } 86 }
87 87
88 json_object *float_event_coords;
89 if (json_object_object_get_ex(header, "float_event_coords", &float_event_coords)
90 && json_object_get_boolean(float_event_coords)) {
91 sway_log(SWAY_DEBUG, "Enabling floating-point coordinates.");
92 status->float_event_coords = true;
93 }
94
88 json_object *signal; 95 json_object *signal;
89 if (json_object_object_get_ex(header, "stop_signal", &signal)) { 96 if (json_object_object_get_ex(header, "stop_signal", &signal)) {
90 status->stop_signal = json_object_get_int(signal); 97 status->stop_signal = json_object_get_int(signal);
diff --git a/swaybar/tray/item.c b/swaybar/tray/item.c
index 06a8e5b0..b4238417 100644
--- a/swaybar/tray/item.c
+++ b/swaybar/tray/item.c
@@ -377,7 +377,7 @@ static int cmp_sni_id(const void *item, const void *cmp_to) {
377 377
378static enum hotspot_event_handling icon_hotspot_callback( 378static enum hotspot_event_handling icon_hotspot_callback(
379 struct swaybar_output *output, struct swaybar_hotspot *hotspot, 379 struct swaybar_output *output, struct swaybar_hotspot *hotspot,
380 int x, int y, uint32_t button, void *data) { 380 double x, double y, uint32_t button, void *data) {
381 sway_log(SWAY_DEBUG, "Clicked on %s", (char *)data); 381 sway_log(SWAY_DEBUG, "Clicked on %s", (char *)data);
382 382
383 struct swaybar_tray *tray = output->bar->tray; 383 struct swaybar_tray *tray = output->bar->tray;