]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Support a very simple root-menu
authorJohan Malm <jgm323@gmail.com>
Mon, 19 Oct 2020 21:14:17 +0000 (22:14 +0100)
committerJohan Malm <jgm323@gmail.com>
Mon, 19 Oct 2020 21:14:17 +0000 (22:14 +0100)
include/labwc.h
include/menu/menu.h [new file with mode: 0644]
src/action.c
src/cursor.c
src/main.c
src/menu/menu.c [new file with mode: 0644]
src/menu/meson.build [new file with mode: 0644]
src/meson.build
src/output.c

index 974381d172ba9441a935f4e056498be0f0849150..a28e14e98793de9fa4c7d6399cf3ee84aa639544 100644 (file)
 #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 (file)
index 0000000..bc583c1
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef __LABWC_MENU_H
+#define __LABWC_MENU_H
+
+#include <wayland-server.h>
+#include <wlr/render/wlr_renderer.h>
+
+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 */
index 853640375f1e9163b2c7803da9922d0e9572d29f..100fb9af4f7fd816222db325c3e68e0853762cc1 100644 (file)
@@ -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);
index ab909e2426cf94b921d70e7fa318130911ac9bf6..a7e555d8aabdb53318c0f1aa922729eab7a9505e 100644 (file)
@@ -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;
        }
 }
 
index 6bc9fcf47b6229bed9b61819ac12ea7fbfc92189..90528ae4e92cf06cb8c89c8401d42af69741473a 100644 (file)
@@ -3,6 +3,7 @@
 #include "labwc.h"
 #include "theme/theme.h"
 #include "xbm/xbm.h"
+#include "menu/menu.h"
 
 #include <cairo.h>
 #include <pango/pangocairo.h>
@@ -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 (file)
index 0000000..c6c772b
--- /dev/null
@@ -0,0 +1,117 @@
+#define _POSIX_C_SOURCE 200809L
+#include <cairo/cairo.h>
+#include <pango/pangocairo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#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 (file)
index 0000000..51306a4
--- /dev/null
@@ -0,0 +1,3 @@
+labwc_sources += files(
+  'menu.c',
+)
index eb0e8b0eea3bf722dc8b257680b14de5dd03e9c6..03eaf6708629fae582bac903a6619d6d69b4b4cd 100644 (file)
@@ -21,3 +21,4 @@ subdir('common')
 subdir('config')
 subdir('theme')
 subdir('xbm')
+subdir('menu')
index c121c35d15fc2f26043b9b352876227811fcbf7e..97d0d5a6f5ac3052913bf1315a65216ed9b86519 100644 (file)
@@ -1,6 +1,7 @@
 #define _POSIX_C_SOURCE 200809L
 #include <wlr/types/wlr_xdg_output_v1.h>
 #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);