Skip to content

Commit

Permalink
Tray: Implement dbusmenu
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Fan <[email protected]>
Co-authored-by: Nathan Schulte <[email protected]>

Signed-off-by: Felix Weilbach <[email protected]>
  • Loading branch information
Felix Weilbach committed Jan 16, 2023
1 parent 2f2cdd6 commit 69e2fe7
Show file tree
Hide file tree
Showing 11 changed files with 1,468 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/swaybar/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct swaybar {
struct zwlr_layer_shell_v1 *layer_shell;
struct zxdg_output_manager_v1 *xdg_output_manager;
struct wl_shm *shm;
struct xdg_wm_base *wm_base;

struct swaybar_config *config;
struct status_line *status;
Expand Down
5 changes: 3 additions & 2 deletions include/swaybar/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

struct swaybar;
struct swaybar_output;
struct swaybar_seat;

struct swaybar_pointer {
struct wl_pointer *pointer;
Expand Down Expand Up @@ -48,8 +49,8 @@ struct swaybar_hotspot {
struct wl_list link; // swaybar_output::hotspots
int x, y, width, height;
enum hotspot_event_handling (*callback)(struct swaybar_output *output,
struct swaybar_hotspot *hotspot, double x, double y, uint32_t button,
bool released, void *data);
struct swaybar_hotspot *hotspot, struct swaybar_seat *seat, uint32_t serial,
double x, double y, uint32_t button, bool released, void *data);
void (*destroy)(void *data);
void *data;
};
Expand Down
27 changes: 27 additions & 0 deletions include/swaybar/tray/dbusmenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _SWAYBAR_TRAY_DBUSMENU_H
#define _SWAYBAR_TRAY_DBUSMENU_H

#include "swaybar/bar.h"
#include "swaybar/tray/item.h"

void swaybar_dbusmenu_open(struct swaybar_sni *sni,
struct swaybar_output *output, struct swaybar_seat *seat, uint32_t serial,
int x, int y);

bool dbusmenu_pointer_button(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time_, uint32_t button, uint32_t state);

bool dbusmenu_pointer_motion(struct swaybar_seat *seat, struct wl_pointer *wl_pointer,
uint32_t time_, wl_fixed_t surface_x, wl_fixed_t surface_y);

bool dbusmenu_pointer_enter(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y);

bool dbusmenu_pointer_leave(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
struct wl_surface *surface);

bool dbusmenu_pointer_frame(struct swaybar_seat *data, struct wl_pointer *wl_pointer);

bool dbusmenu_pointer_axis(struct swaybar_seat *data, struct wl_pointer *wl_pointer);

#endif
2 changes: 2 additions & 0 deletions include/swaybar/tray/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct swaybar_pixmap {
struct swaybar_sni_slot {
struct wl_list link; // swaybar_sni::slots
struct swaybar_sni *sni;
int menu_id;
const char *prop;
const char *type;
void *dest;
Expand Down Expand Up @@ -48,6 +49,7 @@ struct swaybar_sni {
char *icon_theme_path; // non-standard KDE property

struct wl_list slots; // swaybar_sni_slot::link
char **menu_icon_theme_paths;
};

struct swaybar_sni *create_sni(char *id, struct swaybar_tray *tray);
Expand Down
3 changes: 3 additions & 0 deletions include/swaybar/tray/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ struct swaybar_tray {

list_t *basedirs; // char *
list_t *themes; // struct swaybar_theme *

struct swaybar_dbusmenu *menu;
struct swaybar_dbusmenu_menu *menu_pointer_focus;
};

struct swaybar_tray *create_tray(struct swaybar *bar);
Expand Down
4 changes: 4 additions & 0 deletions swaybar/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "pool-buffer.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"

void free_workspaces(struct wl_list *list) {
struct swaybar_workspace *ws, *tmp;
Expand Down Expand Up @@ -362,6 +363,8 @@ static void handle_global(void *data, struct wl_registry *registry,
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
bar->xdg_output_manager = wl_registry_bind(registry, name,
&zxdg_output_manager_v1_interface, 2);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
bar->wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
}
}

Expand Down Expand Up @@ -534,6 +537,7 @@ void bar_teardown(struct swaybar *bar) {
#if HAVE_TRAY
destroy_tray(bar->tray);
#endif
xdg_wm_base_destroy(bar->wm_base);
free_outputs(&bar->outputs);
free_outputs(&bar->unused_outputs);
free_seats(&bar->seats);
Expand Down
49 changes: 45 additions & 4 deletions swaybar/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include "swaybar/input.h"
#include "swaybar/ipc.h"

#if HAVE_TRAY
#include "swaybar/tray/dbusmenu.h"
#endif

void free_hotspots(struct wl_list *list) {
struct swaybar_hotspot *hotspot, *tmp;
wl_list_for_each_safe(hotspot, tmp, list, link) {
Expand Down Expand Up @@ -112,10 +116,23 @@ static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
}
}
update_cursor(seat);

#if HAVE_TRAY
if (dbusmenu_pointer_enter(data, wl_pointer, serial, surface, surface_x,
surface_y)) {
return;
}
#endif
}

static void wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface) {
#if HAVE_TRAY
if (dbusmenu_pointer_leave(data, wl_pointer, serial, surface)) {
return;
}
#endif

struct swaybar_seat *seat = data;
seat->pointer.current = NULL;
}
Expand All @@ -125,6 +142,11 @@ static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
struct swaybar_seat *seat = data;
seat->pointer.x = wl_fixed_to_double(surface_x);
seat->pointer.y = wl_fixed_to_double(surface_y);
#if HAVE_TRAY
if (dbusmenu_pointer_motion(data, wl_pointer, time, surface_x, surface_y)) {
return;
}
#endif
}

static bool check_bindings(struct swaybar *bar, uint32_t button,
Expand All @@ -141,14 +163,15 @@ static bool check_bindings(struct swaybar *bar, uint32_t button,
}

static bool process_hotspots(struct swaybar_output *output,
struct swaybar_seat *seat, uint32_t serial,
double x, double y, uint32_t button, uint32_t state) {
bool released = state == WL_POINTER_BUTTON_STATE_RELEASED;
struct swaybar_hotspot *hotspot;
wl_list_for_each(hotspot, &output->hotspots, link) {
if (x >= hotspot->x && y >= hotspot->y
&& x < hotspot->x + hotspot->width
&& y < hotspot->y + hotspot->height) {
if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, x, y,
if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, seat, serial, x, y,
button, released, hotspot->data)) {
return true;
}
Expand All @@ -161,13 +184,19 @@ static bool process_hotspots(struct swaybar_output *output,
static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
struct swaybar_seat *seat = data;
#if HAVE_TRAY
if (dbusmenu_pointer_button(seat, wl_pointer, serial, time, button,
state)) {
return;
}
#endif
struct swaybar_pointer *pointer = &seat->pointer;
struct swaybar_output *output = pointer->current;
if (!sway_assert(output, "button with no active output")) {
return;
}

if (process_hotspots(output, pointer->x, pointer->y, button, state)) {
if (process_hotspots(output, seat, serial, pointer->x, pointer->y, button, state)) {
return;
}

Expand Down Expand Up @@ -221,7 +250,7 @@ static void process_discrete_scroll(struct swaybar_seat *seat,
struct swaybar_output *output, struct swaybar_pointer *pointer,
uint32_t axis, wl_fixed_t value) {
uint32_t button = wl_axis_to_button(axis, value);
if (process_hotspots(output, pointer->x, pointer->y, button, WL_POINTER_BUTTON_STATE_PRESSED)) {
if (process_hotspots(output, seat, 0, pointer->x, pointer->y, button, WL_POINTER_BUTTON_STATE_PRESSED)) {
// (Currently hotspots don't do anything on release events, so no need to emit one)
return;
}
Expand Down Expand Up @@ -278,6 +307,12 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
return;
}

#if HAVE_TRAY
if (dbusmenu_pointer_axis(data, wl_pointer)) {
return;
}
#endif

// If there's a while since the last scroll event,
// set 'value' to zero as if to reset the "virtual scroll wheel"
if (seat->axis[axis].discrete_steps == 0 &&
Expand All @@ -294,6 +329,12 @@ static void wl_pointer_frame(void *data, struct wl_pointer *wl_pointer) {
struct swaybar_pointer *pointer = &seat->pointer;
struct swaybar_output *output = pointer->current;

#if HAVE_TRAY
if (dbusmenu_pointer_frame(data, wl_pointer)) {
return;
}
#endif

if (output == NULL) {
return;
}
Expand Down Expand Up @@ -401,7 +442,7 @@ static void wl_touch_up(void *data, struct wl_touch *wl_touch,
}
if (time - slot->time < 500) {
// Tap, treat it like a pointer click
process_hotspots(slot->output, slot->x, slot->y, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
process_hotspots(slot->output, seat, serial, slot->x, slot->y, BTN_LEFT, WL_POINTER_BUTTON_STATE_PRESSED);
// (Currently hotspots don't do anything on release events, so no need to emit one)
}
slot->output = NULL;
Expand Down
3 changes: 2 additions & 1 deletion swaybar/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ tray_files = have_tray ? [
'tray/icon.c',
'tray/item.c',
'tray/tray.c',
'tray/watcher.c'
'tray/watcher.c',
'tray/dbusmenu.c'
] : []

swaybar_deps = [
Expand Down
2 changes: 2 additions & 0 deletions swaybar/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color,

static enum hotspot_event_handling block_hotspot_callback(
struct swaybar_output *output, struct swaybar_hotspot *hotspot,
struct swaybar_seat *seat, uint32_t serial,
double x, double y, uint32_t button, bool released, void *data) {
struct i3bar_block *block = data;
struct status_line *status = output->bar->status;
Expand Down Expand Up @@ -599,6 +600,7 @@ static uint32_t render_binding_mode_indicator(struct render_context *ctx,

static enum hotspot_event_handling workspace_hotspot_callback(
struct swaybar_output *output, struct swaybar_hotspot *hotspot,
struct swaybar_seat *seat, uint32_t serial,
double x, double y, uint32_t button, bool released, void *data) {
if (button != BTN_LEFT) {
return HOTSPOT_PROCESS;
Expand Down
Loading

0 comments on commit 69e2fe7

Please sign in to comment.