Merge pull request #2281 from pvsr/X11_click

Send clicks to swaybar blocks as X11 button ids
This commit is contained in:
emersion 2018-07-18 00:16:15 +01:00 committed by GitHub
commit 48b911a459
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 6 deletions

View file

@ -16,11 +16,24 @@ struct swaybar_pointer {
int x, y; int x, y;
}; };
enum x11_button {
NONE,
LEFT,
MIDDLE,
RIGHT,
SCROLL_UP,
SCROLL_DOWN,
SCROLL_LEFT,
SCROLL_RIGHT,
BACK,
FORWARD,
};
struct swaybar_hotspot { struct swaybar_hotspot {
struct wl_list link; struct wl_list link;
int x, y, width, height; int x, y, width, height;
void (*callback)(struct swaybar_output *output, void (*callback)(struct swaybar_output *output,
int x, int y, uint32_t button, void *data); int x, int y, enum x11_button button, void *data);
void (*destroy)(void *data); void (*destroy)(void *data);
void *data; void *data;
}; };

View file

@ -72,7 +72,9 @@ bool status_handle_readable(struct status_line *status);
void status_line_free(struct status_line *status); void status_line_free(struct status_line *status);
bool i3bar_handle_readable(struct status_line *status); bool i3bar_handle_readable(struct status_line *status);
void i3bar_block_send_click(struct status_line *status, void i3bar_block_send_click(struct status_line *status,
struct i3bar_block *block, int x, int y, uint32_t button); struct i3bar_block *block, int x, int y, enum x11_button button);
void i3bar_block_free(struct i3bar_block *block); void i3bar_block_free(struct i3bar_block *block);
enum x11_button wl_button_to_x11_button(uint32_t button);
enum x11_button wl_axis_to_x11_button(uint32_t axis, wl_fixed_t value);
#endif #endif

View file

@ -147,7 +147,7 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
&& x < hotspot->x + hotspot->width && x < hotspot->x + hotspot->width
&& y < hotspot->y + hotspot->height) { && y < hotspot->y + hotspot->height) {
hotspot->callback(output, pointer->x, pointer->y, hotspot->callback(output, pointer->x, pointer->y,
button, hotspot->data); wl_button_to_x11_button(button), hotspot->data);
} }
} }
} }
@ -155,11 +155,26 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis, wl_fixed_t value) { uint32_t time, uint32_t axis, wl_fixed_t value) {
struct swaybar *bar = data; struct swaybar *bar = data;
struct swaybar_pointer *pointer = &bar->pointer;
struct swaybar_output *output = bar->pointer.current; struct swaybar_output *output = bar->pointer.current;
if (!sway_assert(output, "axis with no active output")) { if (!sway_assert(output, "axis with no active output")) {
return; return;
} }
struct swaybar_hotspot *hotspot;
wl_list_for_each(hotspot, &output->hotspots, link) {
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) {
hotspot->callback(output, pointer->x, pointer->y,
wl_axis_to_x11_button(axis, value), hotspot->data);
return;
}
}
double amt = wl_fixed_to_double(value); double amt = wl_fixed_to_double(value);
if (amt == 0.0) { if (amt == 0.0) {
return; return;

View file

@ -1,5 +1,6 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <json-c/json.h> #include <json-c/json.h>
#include <linux/input-event-codes.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -192,7 +193,7 @@ bool i3bar_handle_readable(struct status_line *status) {
} }
void i3bar_block_send_click(struct status_line *status, void i3bar_block_send_click(struct status_line *status,
struct i3bar_block *block, int x, int y, uint32_t button) { struct i3bar_block *block, int x, int y, enum x11_button button) {
wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)"); wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)");
if (!block->name || !status->i3bar_state.click_events) { if (!block->name || !status->i3bar_state.click_events) {
return; return;
@ -215,3 +216,32 @@ void i3bar_block_send_click(struct status_line *status,
} }
json_object_put(event_json); json_object_put(event_json);
} }
enum x11_button wl_button_to_x11_button(uint32_t button) {
switch (button) {
case BTN_LEFT:
return LEFT;
case BTN_MIDDLE:
return MIDDLE;
case BTN_RIGHT:
return RIGHT;
case BTN_SIDE:
return BACK;
case BTN_EXTRA:
return FORWARD;
default:
return NONE;
}
}
enum x11_button wl_axis_to_x11_button(uint32_t axis, wl_fixed_t value) {
switch (axis) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
return wl_fixed_to_double(value) < 0 ? SCROLL_UP : SCROLL_DOWN;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
return wl_fixed_to_double(value) < 0 ? SCROLL_LEFT : SCROLL_RIGHT;
default:
wlr_log(WLR_DEBUG, "Unexpected axis value on mouse scroll");
return NONE;
}
}

View file

@ -109,7 +109,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color,
} }
static void block_hotspot_callback(struct swaybar_output *output, static void block_hotspot_callback(struct swaybar_output *output,
int x, int y, uint32_t button, void *data) { int x, int y, enum x11_button 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;
i3bar_block_send_click(status, block, x, y, button); i3bar_block_send_click(status, block, x, y, button);
@ -349,7 +349,7 @@ static const char *strip_workspace_number(const char *ws_name) {
} }
static void workspace_hotspot_callback(struct swaybar_output *output, static void workspace_hotspot_callback(struct swaybar_output *output,
int x, int y, uint32_t button, void *data) { int x, int y, enum x11_button button, void *data) {
ipc_send_workspace_command(output->bar, (const char *)data); ipc_send_workspace_command(output->bar, (const char *)data);
} }