Skip to content

Commit

Permalink
Tray: Implement dbusmenu
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Weilbach <[email protected]>
  • Loading branch information
Felix Weilbach committed Jul 31, 2021
1 parent 6ffa4b1 commit e212e13
Show file tree
Hide file tree
Showing 10 changed files with 1,425 additions and 26 deletions.
10 changes: 8 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,13 @@ 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,
void *data);
struct swaybar_hotspot *hotspot,
struct swaybar_seat *seat,
uint32_t serial,
double x,
double y,
uint32_t button,
void *data);
void (*destroy)(void *data);
void *data;
};
Expand Down
25 changes: 25 additions & 0 deletions include/swaybar/tray/dbusmenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _SWAYBAR_TRAY_DBUSMENU_H
#define _SWAYBAR_TRAY_DBUSMENU_H

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

struct swaybar_dbusmenu *swaybar_dbusmenu_create(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);

#endif
2 changes: 2 additions & 0 deletions include/swaybar/tray/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -47,6 +48,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
5 changes: 4 additions & 1 deletion include/swaybar/tray/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ struct swaybar_tray {
struct swaybar_watcher *watcher_kde;

list_t *basedirs; // char *
list_t *themes; // struct swaybar_theme *
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
55 changes: 47 additions & 8 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 @@ -97,8 +101,14 @@ void update_cursor(struct swaybar_seat *seat) {
}

static void wl_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) {
uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x,
wl_fixed_t surface_y) {
#if HAVE_TRAY
if (dbusmenu_pointer_enter(data, wl_pointer, serial, surface, surface_x,
surface_y)) {
return;
}
#endif
struct swaybar_seat *seat = data;
struct swaybar_pointer *pointer = &seat->pointer;
seat->pointer.x = wl_fixed_to_double(surface_x);
Expand All @@ -116,15 +126,27 @@ static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,

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;
}

static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
#if HAVE_TRAY
struct swaybar_seat *seat = data;
seat->pointer.x = wl_fixed_to_double(surface_x);
seat->pointer.y = wl_fixed_to_double(surface_y);

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,16 +163,26 @@ static bool check_bindings(struct swaybar *bar, uint32_t button,
}

static bool process_hotspots(struct swaybar_output *output,
double x, double y, uint32_t button) {
struct swaybar_seat *seat,
uint32_t serial,
double x,
double y,
uint32_t button) {
double px = x * output->scale;
double py = y * output->scale;
struct swaybar_hotspot *hotspot;
wl_list_for_each(hotspot, &output->hotspots, link) {
if (px >= hotspot->x && py >= hotspot->y
&& px < hotspot->x + hotspot->width
&& py < hotspot->y + hotspot->height) {
if (HOTSPOT_IGNORE == hotspot->callback(output, hotspot, x, y,
button, hotspot->data)) {
if (HOTSPOT_IGNORE == hotspot->callback(output,
hotspot,
seat,
serial,
x,
y,
button,
hotspot->data)) {
return true;
}
}
Expand All @@ -162,6 +194,12 @@ 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")) {
Expand All @@ -175,7 +213,7 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
return;
}
process_hotspots(output, pointer->x, pointer->y, button);
process_hotspots(output, seat, serial, pointer->x, pointer->y, button);
}

static void workspace_next(struct swaybar *bar, struct swaybar_output *output,
Expand Down Expand Up @@ -232,7 +270,7 @@ static void process_discrete_scroll(struct swaybar_seat *seat,
return;
}

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

Expand Down Expand Up @@ -405,7 +443,8 @@ 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);
process_hotspots(
slot->output, seat, serial, slot->x, slot->y, BTN_LEFT);
}
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
29 changes: 21 additions & 8 deletions swaybar/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,14 @@ 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,
double x, double y, uint32_t button, void *data) {
struct swaybar_output *output,
struct swaybar_hotspot *hotspot,
struct swaybar_seat *seat,
uint32_t serial,
double x,
double y,
uint32_t button,
void *data) {
struct i3bar_block *block = data;
struct status_line *status = output->bar->status;
return i3bar_block_send_click(status, block, x, y,
Expand Down Expand Up @@ -296,7 +302,7 @@ static uint32_t render_status_block(struct render_context *ctx,
} else if (strncmp(block->align, "right", 5) == 0) {
offset = x_pos + width - text_width;
} else if (strncmp(block->align, "center", 6) == 0) {
offset = x_pos + (width - text_width) / 2;
offset = x_pos + (width - text_width) / 2.0;
}
double text_y = height / 2.0 - text_height / 2.0;
cairo_move_to(cairo, offset, (int)floor(text_y));
Expand Down Expand Up @@ -325,7 +331,7 @@ static uint32_t render_status_block(struct render_context *ctx,
}
cairo_set_source_u32(cairo, color);
if (config->sep_symbol) {
offset = x_pos + (sep_block_width - sep_width) / 2;
offset = x_pos + (sep_block_width - sep_width) / 2.0;
double sep_y = height / 2.0 - sep_height / 2.0;
cairo_move_to(cairo, offset, (int)floor(sep_y));
choose_text_aa_mode(ctx, color);
Expand All @@ -334,7 +340,7 @@ static uint32_t render_status_block(struct render_context *ctx,
} else {
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_line_width(cairo, 1);
cairo_move_to(cairo, x_pos + sep_block_width / 2, margin);
cairo_move_to(cairo, x_pos + sep_block_width / 2.0, margin);
cairo_line_to(cairo, x_pos + sep_block_width / 2, height - margin);
cairo_stroke(cairo);
}
Expand Down Expand Up @@ -596,8 +602,14 @@ 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,
double x, double y, uint32_t button, void *data) {
struct swaybar_output *output,
struct swaybar_hotspot *hotspot,
struct swaybar_seat *seat,
uint32_t serial,
double x,
double y,
uint32_t button,
void *data) {
if (button != BTN_LEFT) {
return HOTSPOT_PROCESS;
}
Expand Down Expand Up @@ -662,7 +674,8 @@ static uint32_t render_workspace_button(struct render_context *ctx,

double text_y = height / 2.0 - text_height / 2.0;
cairo_set_source_u32(cairo, box_colors.text);
cairo_move_to(cairo, *x + width / 2 - text_width / 2, (int)floor(text_y));
cairo_move_to(
cairo, *x + width / 2.0 - text_width / 2.0, (int)floor(text_y));
choose_text_aa_mode(ctx, box_colors.text);
pango_printf(cairo, config->font, output->scale, config->pango_markup,
"%s", ws->label);
Expand Down
Loading

0 comments on commit e212e13

Please sign in to comment.