swaybar: fix i3bar relative coordinates when scaling is used

24e8ba048a 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.
master
Hristo Venev 5 years ago committed by Simon Ser
parent fca32b6334
commit 7affe5c1bd

@ -28,7 +28,7 @@ struct i3bar_block {
void i3bar_block_unref(struct i3bar_block *block); void i3bar_block_unref(struct i3bar_block *block);
bool i3bar_handle_readable(struct status_line *status); bool i3bar_handle_readable(struct status_line *status);
enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
struct i3bar_block *block, int x, int y, int rx, int ry, int w, int h, struct i3bar_block *block, double x, double y, double rx, double ry,
uint32_t button); double w, double h, int scale, uint32_t button);
#endif #endif

@ -44,7 +44,7 @@ struct swaybar_hotspot {
struct wl_list link; // swaybar_output::hotspots struct wl_list link; // swaybar_output::hotspots
int x, y, width, height; int x, y, width, height;
enum hotspot_event_handling (*callback)(struct swaybar_output *output, enum hotspot_event_handling (*callback)(struct swaybar_output *output,
struct swaybar_hotspot *hotspot, int x, int y, uint32_t button, struct swaybar_hotspot *hotspot, double x, double y, uint32_t button,
void *data); void *data);
void (*destroy)(void *data); void (*destroy)(void *data);
void *data; void *data;

@ -28,6 +28,7 @@ struct status_line {
int cont_signal; int cont_signal;
bool click_events; bool click_events;
bool float_event_coords;
bool clicked; bool clicked;
char *buffer; char *buffer;
size_t buffer_size; size_t buffer_size;

@ -267,8 +267,8 @@ bool i3bar_handle_readable(struct status_line *status) {
} }
enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
struct i3bar_block *block, int x, int y, int rx, int ry, int w, int h, struct i3bar_block *block, double x, double y, double rx, double ry,
uint32_t button) { double w, double h, int scale, uint32_t button) {
sway_log(SWAY_DEBUG, "block %s clicked", block->name); sway_log(SWAY_DEBUG, "block %s clicked", block->name);
if (!block->name || !status->click_events) { if (!block->name || !status->click_events) {
return HOTSPOT_PROCESS; return HOTSPOT_PROCESS;
@ -285,12 +285,22 @@ enum hotspot_event_handling i3bar_block_send_click(struct status_line *status,
json_object_object_add(event_json, "button", json_object_object_add(event_json, "button",
json_object_new_int(event_to_x11_button(button))); json_object_new_int(event_to_x11_button(button)));
json_object_object_add(event_json, "event", json_object_new_int(button)); json_object_object_add(event_json, "event", json_object_new_int(button));
json_object_object_add(event_json, "x", json_object_new_int(x)); if (status->float_event_coords) {
json_object_object_add(event_json, "y", json_object_new_int(y)); json_object_object_add(event_json, "x", json_object_new_double(x));
json_object_object_add(event_json, "relative_x", json_object_new_int(rx)); json_object_object_add(event_json, "y", json_object_new_double(y));
json_object_object_add(event_json, "relative_y", json_object_new_int(ry)); json_object_object_add(event_json, "relative_x", json_object_new_double(rx));
json_object_object_add(event_json, "width", json_object_new_int(w)); json_object_object_add(event_json, "relative_y", json_object_new_double(ry));
json_object_object_add(event_json, "height", json_object_new_int(h)); json_object_object_add(event_json, "width", json_object_new_double(w));
json_object_object_add(event_json, "height", json_object_new_double(h));
} else {
json_object_object_add(event_json, "x", json_object_new_int(x));
json_object_object_add(event_json, "y", json_object_new_int(y));
json_object_object_add(event_json, "relative_x", json_object_new_int(rx));
json_object_object_add(event_json, "relative_y", json_object_new_int(ry));
json_object_object_add(event_json, "width", json_object_new_int(w));
json_object_object_add(event_json, "height", json_object_new_int(h));
}
json_object_object_add(event_json, "scale", json_object_new_int(scale));
if (dprintf(status->write_fd, "%s%s\n", status->clicked ? "," : "", if (dprintf(status->write_fd, "%s%s\n", status->clicked ? "," : "",
json_object_to_json_string(event_json)) < 0) { json_object_to_json_string(event_json)) < 0) {
status_error(status, "[failed to write click event]"); status_error(status, "[failed to write click event]");

@ -138,21 +138,23 @@ static bool check_bindings(struct swaybar *bar, uint32_t button,
return false; return false;
} }
static void process_hotspots(struct swaybar_output *output, static bool process_hotspots(struct swaybar_output *output,
double x, double y, uint32_t button) { double x, double y, uint32_t button) {
x *= output->scale; double px = x * output->scale;
y *= output->scale; double py = y * output->scale;
struct swaybar_hotspot *hotspot; struct swaybar_hotspot *hotspot;
wl_list_for_each(hotspot, &output->hotspots, link) { wl_list_for_each(hotspot, &output->hotspots, link) {
if (x >= hotspot->x && y >= hotspot->y if (px >= hotspot->x && py >= hotspot->y
&& x < hotspot->x + hotspot->width && px < hotspot->x + hotspot->width
&& y < hotspot->y + hotspot->height) { && py < hotspot->y + hotspot->height) {
if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, x, y,
x / output->scale, y / output->scale, button, hotspot->data)) { button, hotspot->data)) {
return; return true;
} }
} }
} }
return false;
} }
static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, static 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,
return; return;
} }
struct swaybar_hotspot *hotspot; if (process_hotspots(output, pointer->x, pointer->y, button)) {
wl_list_for_each(hotspot, &output->hotspots, link) { return;
double x = pointer->x * output->scale;
double y = pointer->y * output->scale;
if (x >= hotspot->x
&& y >= hotspot->y
&& x < hotspot->x + hotspot->width
&& y < hotspot->y + hotspot->height) {
if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot,
pointer->x, pointer->y, button, hotspot->data)) {
return;
}
}
} }
struct swaybar_config *config = seat->bar->config; struct swaybar_config *config = seat->bar->config;

@ -131,11 +131,15 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color,
static enum hotspot_event_handling block_hotspot_callback( static enum hotspot_event_handling block_hotspot_callback(
struct swaybar_output *output, struct swaybar_hotspot *hotspot, struct swaybar_output *output, struct swaybar_hotspot *hotspot,
int x, int y, uint32_t button, void *data) { double x, double y, uint32_t button, void *data) {
struct i3bar_block *block = data; struct i3bar_block *block = data;
struct status_line *status = output->bar->status; struct status_line *status = output->bar->status;
return i3bar_block_send_click(status, block, x, y, x - hotspot->x, return i3bar_block_send_click(status, block, x, y,
y - hotspot->y, hotspot->width, hotspot->height, button); x - (double)hotspot->x / output->scale,
y - (double)hotspot->y / output->scale,
(double)hotspot->width / output->scale,
(double)hotspot->height / output->scale,
output->scale, button);
} }
static void i3bar_block_unref_callback(void *data) { static void i3bar_block_unref_callback(void *data) {
@ -540,7 +544,7 @@ static uint32_t render_binding_mode_indicator(cairo_t *cairo,
static enum hotspot_event_handling workspace_hotspot_callback( static enum hotspot_event_handling workspace_hotspot_callback(
struct swaybar_output *output, struct swaybar_hotspot *hotspot, struct swaybar_output *output, struct swaybar_hotspot *hotspot,
int x, int y, uint32_t button, void *data) { double x, double y, uint32_t button, void *data) {
if (button != BTN_LEFT) { if (button != BTN_LEFT) {
return HOTSPOT_PROCESS; return HOTSPOT_PROCESS;
} }

@ -85,6 +85,13 @@ bool status_handle_readable(struct status_line *status) {
} }
} }
json_object *float_event_coords;
if (json_object_object_get_ex(header, "float_event_coords", &float_event_coords)
&& json_object_get_boolean(float_event_coords)) {
sway_log(SWAY_DEBUG, "Enabling floating-point coordinates.");
status->float_event_coords = true;
}
json_object *signal; json_object *signal;
if (json_object_object_get_ex(header, "stop_signal", &signal)) { if (json_object_object_get_ex(header, "stop_signal", &signal)) {
status->stop_signal = json_object_get_int(signal); status->stop_signal = json_object_get_int(signal);

@ -377,7 +377,7 @@ static int cmp_sni_id(const void *item, const void *cmp_to) {
static enum hotspot_event_handling icon_hotspot_callback( static enum hotspot_event_handling icon_hotspot_callback(
struct swaybar_output *output, struct swaybar_hotspot *hotspot, struct swaybar_output *output, struct swaybar_hotspot *hotspot,
int x, int y, uint32_t button, void *data) { double x, double y, uint32_t button, void *data) {
sway_log(SWAY_DEBUG, "Clicked on %s", (char *)data); sway_log(SWAY_DEBUG, "Clicked on %s", (char *)data);
struct swaybar_tray *tray = output->bar->tray; struct swaybar_tray *tray = output->bar->tray;

Loading…
Cancel
Save