From dc5d1ab976cd887a7315deb43739ac1a30b0327e Mon Sep 17 00:00:00 2001 From: Johan Malm Date: Mon, 19 Oct 2020 22:14:17 +0100 Subject: [PATCH] Support a very simple root-menu --- include/labwc.h | 5 +- include/menu/menu.h | 35 +++++++++++++ src/action.c | 2 +- src/cursor.c | 94 +++++++++++++++++++++------------- src/main.c | 5 ++ src/menu/menu.c | 117 +++++++++++++++++++++++++++++++++++++++++++ src/menu/meson.build | 3 ++ src/meson.build | 1 + src/output.c | 29 +++++++++++ 9 files changed, 255 insertions(+), 36 deletions(-) create mode 100644 include/menu/menu.h create mode 100644 src/menu/menu.c create mode 100644 src/menu/meson.build diff --git a/include/labwc.h b/include/labwc.h index 974381d1..a28e14e9 100644 --- a/include/labwc.h +++ b/include/labwc.h @@ -38,9 +38,10 @@ #define XCURSOR_MOVE "grabbing" enum cursor_mode { - LAB_CURSOR_PASSTHROUGH, + LAB_CURSOR_PASSTHROUGH = 0, LAB_CURSOR_MOVE, LAB_CURSOR_RESIZE, + LAB_INPUT_STATE_MENU, }; struct input { @@ -106,6 +107,8 @@ struct server { /* Set when in cycle (alt-tab) mode */ struct view *cycle_view; + + struct menu *rootmenu; }; struct output { diff --git a/include/menu/menu.h b/include/menu/menu.h new file mode 100644 index 00000000..bc583c10 --- /dev/null +++ b/include/menu/menu.h @@ -0,0 +1,35 @@ +#ifndef __LABWC_MENU_H +#define __LABWC_MENU_H + +#include +#include + +struct menuitem { + char *action; + char *command; + struct wlr_box geo_box; + struct wlr_texture *active_texture; + struct wlr_texture *inactive_texture; + bool selected; + struct wl_list link; +}; + +struct menu { + int x; + int y; + struct wl_list menuitems; +}; + +/* menu_create - create menu */ +void menu_init(struct server *server, struct menu *menu); + +/* menu_move - move to position (x, y) */ +void menu_move(struct menu *menu, int x, int y); + +/* menu_set_selected - select item at (x, y) */ +void menu_set_selected(struct menu *menu, int x, int y); + +/* menu_action_selected - select item at (x, y) */ +void menu_action_selected(struct server *server, struct menu *menu); + +#endif /* __LABWC_MENU_H */ diff --git a/src/action.c b/src/action.c index 85364037..100fb9af 100644 --- a/src/action.c +++ b/src/action.c @@ -25,7 +25,7 @@ action(struct server *server, const char *action, const char *command) desktop_next_view(server, server->cycle_view); } else if (!strcasecmp(action, "Reconfigure")) { reconfigure(); - } else if (!strcasecmp(action, "debug-views")) { + } else if (!strcasecmp(action, "Debug")) { dbg_show_views(server); } else { warn("action (%s) not supported", action); diff --git a/src/cursor.c b/src/cursor.c index ab909e24..a7e555d8 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -1,4 +1,5 @@ #include "labwc.h" +#include "menu/menu.h" static void request_cursor_notify(struct wl_listener *listener, void *data) @@ -108,6 +109,10 @@ process_cursor_motion(struct server *server, uint32_t time) } else if (server->cursor_mode == LAB_CURSOR_RESIZE) { process_cursor_resize(server, time); return; + } else if (server->cursor_mode == LAB_INPUT_STATE_MENU) { + menu_set_selected(server->rootmenu, + server->seat.cursor->x, server->seat.cursor->y); + return; } /* Otherwise, find the view under the pointer and send the event along. @@ -240,42 +245,63 @@ cursor_button(struct wl_listener *listener, void *data) double sx, sy; struct wlr_surface *surface; int view_area; - struct view *view = - desktop_view_at(server, server->seat.cursor->x, server->seat.cursor->y, - &surface, &sx, &sy, &view_area); + struct view *view = desktop_view_at(server, server->seat.cursor->x, + server->seat.cursor->y, &surface, &sx, &sy, &view_area); + + /* handle _release_ */ if (event->state == WLR_BUTTON_RELEASED) { - /* Exit interactive move/resize mode. */ - server->cursor_mode = LAB_CURSOR_PASSTHROUGH; - } else { - /* Focus that client if the button was _pressed_ */ - desktop_focus_view(&server->seat, view); - switch (view_area) { - case LAB_DECO_BUTTON_CLOSE: - view->impl->close(view); - break; - case LAB_DECO_BUTTON_ICONIFY: - view_minimize(view); - break; - case LAB_DECO_PART_TITLE: - interactive_begin(view, LAB_CURSOR_MOVE, 0); - break; - case LAB_DECO_PART_TOP: - interactive_begin(view, LAB_CURSOR_RESIZE, - WLR_EDGE_TOP); - break; - case LAB_DECO_PART_RIGHT: - interactive_begin(view, LAB_CURSOR_RESIZE, - WLR_EDGE_RIGHT); - break; - case LAB_DECO_PART_BOTTOM: - interactive_begin(view, LAB_CURSOR_RESIZE, - WLR_EDGE_BOTTOM); - break; - case LAB_DECO_PART_LEFT: - interactive_begin(view, LAB_CURSOR_RESIZE, - WLR_EDGE_LEFT); - break; + if (server->cursor_mode == LAB_INPUT_STATE_MENU) { + return; } + /* Exit interactive move/resize/menu mode. */ + server->cursor_mode = LAB_CURSOR_PASSTHROUGH; + return; + } + + if (server->cursor_mode == LAB_INPUT_STATE_MENU) { + menu_action_selected(server, server->rootmenu); + server->cursor_mode = LAB_CURSOR_PASSTHROUGH; + return; + } + + /* handle _press_ on desktop */ + if (!view) { + /* launch root-menu */ + server->cursor_mode = LAB_INPUT_STATE_MENU; + menu_move(server->rootmenu, server->seat.cursor->x, + server->seat.cursor->y); + return; + } + + + /* Handle _press_ on view */ + desktop_focus_view(&server->seat, view); + switch (view_area) { + case LAB_DECO_BUTTON_CLOSE: + view->impl->close(view); + break; + case LAB_DECO_BUTTON_ICONIFY: + view_minimize(view); + break; + case LAB_DECO_PART_TITLE: + interactive_begin(view, LAB_CURSOR_MOVE, 0); + break; + case LAB_DECO_PART_TOP: + interactive_begin(view, LAB_CURSOR_RESIZE, + WLR_EDGE_TOP); + break; + case LAB_DECO_PART_RIGHT: + interactive_begin(view, LAB_CURSOR_RESIZE, + WLR_EDGE_RIGHT); + break; + case LAB_DECO_PART_BOTTOM: + interactive_begin(view, LAB_CURSOR_RESIZE, + WLR_EDGE_BOTTOM); + break; + case LAB_DECO_PART_LEFT: + interactive_begin(view, LAB_CURSOR_RESIZE, + WLR_EDGE_LEFT); + break; } } diff --git a/src/main.c b/src/main.c index 6bc9fcf4..90528ae4 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include "labwc.h" #include "theme/theme.h" #include "xbm/xbm.h" +#include "menu/menu.h" #include #include @@ -61,6 +62,10 @@ main(int argc, char *argv[]) theme_read(rc.theme_name); xbm_load(server.renderer); + struct menu menu = { 0 }; + menu_init(&server, &menu); + server.rootmenu = &menu; + session_autostart_init(); if (startup_cmd) { spawn_async_no_shell(startup_cmd); diff --git a/src/menu/menu.c b/src/menu/menu.c new file mode 100644 index 00000000..c6c772bc --- /dev/null +++ b/src/menu/menu.c @@ -0,0 +1,117 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include "labwc.h" +#include "menu/menu.h" + +static float background[4] = { 0.3f, 0.1f, 0.1f, 1.0f }; +static float foreground[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; +static const char font[] = "Sans 11"; + +struct wlr_texture * +texture_create(struct server *server, struct wlr_box *geo, const char *text, + float *bg, float *fg) +{ + cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, + geo->width, geo->height); + cairo_t *cairo = cairo_create(surf); + + cairo_set_source_rgb(cairo, bg[0], bg[1], bg[2]); + cairo_paint(cairo); + cairo_set_source_rgba(cairo, fg[0], fg[1], fg[2], fg[3]); + cairo_move_to(cairo, 0, 0); + + PangoLayout *layout = pango_cairo_create_layout(cairo); + pango_layout_set_width(layout, geo->width * PANGO_SCALE); + pango_layout_set_text(layout, text, -1); + + PangoFontDescription *desc = pango_font_description_from_string(font); + pango_layout_set_font_description(layout, desc); + pango_font_description_free(desc); + pango_cairo_update_layout(cairo, layout); + pango_cairo_show_layout(cairo, layout); + g_object_unref(layout); + + cairo_surface_flush(surf); + unsigned char *data = cairo_image_surface_get_data(surf); + struct wlr_texture *texture = wlr_texture_from_pixels(server->renderer, + WL_SHM_FORMAT_ARGB8888, cairo_image_surface_get_stride(surf), + geo->width, geo->height, data); + + cairo_destroy(cairo); + cairo_surface_destroy(surf); + return texture; +} + +#define MENUWIDTH (100) +#define MENUHEIGHT (25) + +struct menuitem * +menuitem_create(struct server *server, struct menu *menu, const char *text, + const char *action, const char *command) +{ + struct menuitem *menuitem = calloc(1, sizeof(struct menuitem)); + if (!menuitem) { + return NULL; + } + menuitem->action = action ? strdup(action) : NULL; + menuitem->command = command ? strdup(command) : NULL; + menuitem->geo_box.width = MENUWIDTH; + menuitem->geo_box.height = MENUHEIGHT; + menuitem->active_texture = texture_create(server, &menuitem->geo_box, + text, background, foreground); + menuitem->inactive_texture = texture_create(server, &menuitem->geo_box, + text, foreground, background); + wl_list_insert(&menu->menuitems, &menuitem->link); + return menuitem; +} + +void +menu_init(struct server *server, struct menu *menu) +{ + wl_list_init(&menu->menuitems); + menuitem_create(server, menu, "Terminal", "Execute", "sakura"); + menuitem_create(server, menu, "Reconfigure", "Reconfigure", NULL); + menuitem_create(server, menu, "Exit", "Exit", NULL); + menu_move(menu, 100, 100); +} + +void +menu_move(struct menu *menu, int x, int y) +{ + menu->x = x; + menu->y = y; + + int offset = 0; + struct menuitem *menuitem; + wl_list_for_each (menuitem, &menu->menuitems, link) { + menuitem->geo_box.x = menu->x; + menuitem->geo_box.y = menu->y + offset; + offset += menuitem->geo_box.height; + } +} + +void +menu_set_selected(struct menu *menu, int x, int y) +{ + struct menuitem *menuitem; + wl_list_for_each (menuitem, &menu->menuitems, link) { + menuitem->selected = + wlr_box_contains_point(&menuitem->geo_box, x, y); + } +} + +void +menu_action_selected(struct server *server, struct menu *menu) +{ + struct menuitem *menuitem; + wl_list_for_each (menuitem, &menu->menuitems, link) { + if (menuitem->selected) { + action(server, menuitem->action, menuitem->command); + break; + } + } +} diff --git a/src/menu/meson.build b/src/menu/meson.build new file mode 100644 index 00000000..51306a44 --- /dev/null +++ b/src/menu/meson.build @@ -0,0 +1,3 @@ +labwc_sources += files( + 'menu.c', +) diff --git a/src/meson.build b/src/meson.build index eb0e8b0e..03eaf670 100644 --- a/src/meson.build +++ b/src/meson.build @@ -21,3 +21,4 @@ subdir('common') subdir('config') subdir('theme') subdir('xbm') +subdir('menu') diff --git a/src/output.c b/src/output.c index c121c35d..97d0d5a6 100644 --- a/src/output.c +++ b/src/output.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include #include "labwc.h" +#include "menu/menu.h" #include "theme/theme.h" #include "layers.h" @@ -80,6 +81,29 @@ render_it: draw_rect_unfilled(&dd, box); } +static void +render_rootmenu(struct output *output) +{ + struct server *server = output->server; + struct draw_data ddata = { + .renderer = server->renderer, + .transform_matrix = output->wlr_output->transform_matrix, + }; + float matrix[9]; + + ddata.rgba = (float[4]){ 0.9, 0.3, 0.3, 0.5 }; + struct menuitem *menuitem; + wl_list_for_each (menuitem, &server->rootmenu->menuitems, link) { + struct wlr_texture *t; + t = menuitem->selected ? menuitem->active_texture : + menuitem->inactive_texture; + wlr_matrix_project_box(matrix, &menuitem->geo_box, + WL_OUTPUT_TRANSFORM_NORMAL, 0, ddata.transform_matrix); + wlr_render_texture_with_matrix(ddata.renderer, t, matrix, 1); + } + +} + static void render_icon(struct draw_data *d, struct wlr_box box, struct wlr_texture *texture) @@ -337,6 +361,11 @@ output_frame_notify(struct wl_listener *listener, void *data) render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]); render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); + + if (output->server->cursor_mode == LAB_INPUT_STATE_MENU) { + render_rootmenu(output); + } + /* Just in case hardware cursors not supported by GPU */ wlr_output_render_software_cursors(output->wlr_output, NULL); -- 2.52.0