labwc/src/menu/menu.c

1057 lines
27 KiB
C
Raw Normal View History

2021-09-24 21:45:48 +01:00
// SPDX-License-Identifier: GPL-2.0-only
2020-10-19 22:14:17 +01:00
#define _POSIX_C_SOURCE 200809L
2021-02-17 20:38:16 +00:00
#include <assert.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
2020-10-19 22:14:17 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-02-17 20:38:16 +00:00
#include <strings.h>
#include <wayland-server-core.h>
2021-07-22 21:30:17 +01:00
#include <wlr/util/log.h>
#include "action.h"
2021-02-17 20:38:16 +00:00
#include "common/buf.h"
2020-10-21 20:32:08 +01:00
#include "common/font.h"
#include "common/list.h"
#include "common/match.h"
#include "common/mem.h"
2021-02-17 20:38:16 +00:00
#include "common/nodename.h"
#include "common/scaled_font_buffer.h"
#include "common/scene-helpers.h"
2021-02-17 20:38:16 +00:00
#include "common/string-helpers.h"
2020-10-19 22:14:17 +01:00
#include "labwc.h"
#include "menu/menu.h"
2022-03-03 04:33:33 +01:00
#include "node.h"
#include "theme.h"
2020-10-19 22:14:17 +01:00
2021-02-17 20:38:16 +00:00
/* state-machine variables for processing <item></item> */
2021-09-24 22:14:04 +01:00
static bool in_item;
2021-02-17 20:38:16 +00:00
static struct menuitem *current_item;
static struct action *current_item_action;
2021-02-17 20:38:16 +00:00
static int menu_level;
static struct menu *current_menu;
/* TODO: split this whole file into parser.c and actions.c*/
static struct menu *
menu_create(struct server *server, const char *id, const char *label)
{
struct menu *menu = znew(*menu);
wl_list_append(&server->menus, &menu->link);
wl_list_init(&menu->menuitems);
menu->id = xstrdup(id);
menu->label = xstrdup(label ? label : id);
menu->parent = current_menu;
menu->server = server;
menu->size.width = server->theme->menu_min_width;
2022-02-19 02:05:38 +01:00
/* menu->size.height will be kept up to date by adding items */
menu->scene_tree = wlr_scene_tree_create(server->menu_tree);
2022-02-19 02:05:38 +01:00
wlr_scene_node_set_enabled(&menu->scene_tree->node, false);
return menu;
}
2022-02-19 02:05:38 +01:00
struct menu *
menu_get_by_id(struct server *server, const char *id)
{
2022-02-19 02:05:38 +01:00
if (!id) {
return NULL;
}
struct menu *menu;
wl_list_for_each(menu, &server->menus, link) {
if (!strcmp(menu->id, id)) {
return menu;
}
}
return NULL;
}
2020-10-19 22:14:17 +01:00
static void
menu_update_width(struct menu *menu)
{
struct menuitem *item;
struct theme *theme = menu->server->theme;
int max_width = theme->menu_min_width;
/* Get widest menu item, clamped by menu_max_width */
wl_list_for_each(item, &menu->menuitems, link) {
if (item->native_width > max_width) {
max_width = item->native_width < theme->menu_max_width
? item->native_width : theme->menu_max_width;
}
}
menu->size.width = max_width + 2 * theme->menu_item_padding_x;
/* Update all items for the new size */
wl_list_for_each(item, &menu->menuitems, link) {
wlr_scene_rect_set_size(
2023-12-03 16:36:20 +08:00
wlr_scene_rect_from_node(item->normal.background),
menu->size.width, item->height);
if (!item->selected.background) {
/* This is a separator. They don't have a selected background. */
wlr_scene_rect_set_size(
2023-12-03 16:36:20 +08:00
wlr_scene_rect_from_node(item->normal.text),
menu->size.width - 2 * theme->menu_separator_padding_width,
theme->menu_separator_line_thickness);
} else {
/* Usual menu item */
wlr_scene_rect_set_size(
2023-12-03 16:36:20 +08:00
wlr_scene_rect_from_node(item->selected.background),
menu->size.width, item->height);
if (item->native_width > max_width || item->submenu) {
scaled_font_buffer_set_max_width(item->normal.buffer,
max_width);
scaled_font_buffer_set_max_width(item->selected.buffer,
max_width);
}
}
}
}
static void
post_processing(struct server *server)
{
struct menu *menu;
wl_list_for_each(menu, &server->menus, link) {
menu_update_width(menu);
}
}
static void
validate_menu(struct menu *menu)
{
struct menuitem *item;
struct action *action, *action_tmp;
wl_list_for_each(item, &menu->menuitems, link) {
wl_list_for_each_safe(action, action_tmp, &item->actions, link) {
if (!action_is_valid(action)) {
wl_list_remove(&action->link);
action_free(action);
wlr_log(WLR_ERROR, "Removed invalid menu action");
}
}
}
}
static void
validate(struct server *server)
{
struct menu *menu;
wl_list_for_each(menu, &server->menus, link) {
validate_menu(menu);
}
}
2021-07-01 19:21:09 +01:00
static struct menuitem *
2022-08-02 22:00:24 +01:00
item_create(struct menu *menu, const char *text, bool show_arrow)
2020-10-19 22:14:17 +01:00
{
2022-09-18 15:22:26 -04:00
struct menuitem *menuitem = znew(*menuitem);
2022-03-03 04:33:33 +01:00
menuitem->parent = menu;
menuitem->selectable = true;
struct server *server = menu->server;
2021-02-21 21:54:40 +00:00
struct theme *theme = server->theme;
const char *arrow = show_arrow ? "" : NULL;
2022-02-19 02:05:38 +01:00
if (!menu->item_height) {
menu->item_height = font_height(&rc.font_menuitem)
+ 2 * theme->menu_item_padding_y;
2022-02-19 02:05:38 +01:00
}
menuitem->height = menu->item_height;
2022-02-19 02:05:38 +01:00
int x, y;
menuitem->native_width = font_width(&rc.font_menuitem, text);
if (arrow) {
menuitem->native_width += font_width(&rc.font_menuitem, arrow);
}
2022-02-19 02:05:38 +01:00
2022-06-05 14:42:06 +02:00
/* Menu item root node */
menuitem->tree = wlr_scene_tree_create(menu->scene_tree);
2022-06-05 14:42:06 +02:00
node_descriptor_create(&menuitem->tree->node,
LAB_NODE_DESC_MENUITEM, menuitem);
/* Tree for each state to hold background and text buffer */
menuitem->normal.tree = wlr_scene_tree_create(menuitem->tree);
menuitem->selected.tree = wlr_scene_tree_create(menuitem->tree);
2022-06-05 14:42:06 +02:00
2022-02-19 02:05:38 +01:00
/* Item background nodes */
2022-06-05 14:42:06 +02:00
menuitem->normal.background = &wlr_scene_rect_create(
menuitem->normal.tree,
menu->size.width, menu->item_height,
2022-02-19 02:05:38 +01:00
theme->menu_items_bg_color)->node;
2022-06-05 14:42:06 +02:00
menuitem->selected.background = &wlr_scene_rect_create(
menuitem->selected.tree,
menu->size.width, menu->item_height,
2022-02-19 02:05:38 +01:00
theme->menu_items_active_bg_color)->node;
/* Font nodes */
menuitem->normal.buffer = scaled_font_buffer_create(menuitem->normal.tree);
menuitem->selected.buffer = scaled_font_buffer_create(menuitem->selected.tree);
if (!menuitem->normal.buffer || !menuitem->selected.buffer) {
wlr_log(WLR_ERROR, "Failed to create menu item '%s'", text);
2023-10-09 20:46:23 +01:00
/*
* Destroying the root node will destroy everything,
* including the node descriptor and scaled_font_buffers.
*/
wlr_scene_node_destroy(&menuitem->tree->node);
free(menuitem);
return NULL;
}
menuitem->normal.text = &menuitem->normal.buffer->scene_buffer->node;
menuitem->selected.text = &menuitem->selected.buffer->scene_buffer->node;
/* Font buffers */
scaled_font_buffer_update(menuitem->normal.buffer, text, menuitem->native_width,
&rc.font_menuitem, theme->menu_items_text_color, arrow);
scaled_font_buffer_update(menuitem->selected.buffer, text, menuitem->native_width,
&rc.font_menuitem, theme->menu_items_active_text_color, arrow);
2022-03-03 04:33:33 +01:00
2022-02-19 02:05:38 +01:00
/* Center font nodes */
x = theme->menu_item_padding_x;
y = (menu->item_height - menuitem->normal.buffer->height) / 2;
2022-02-19 02:05:38 +01:00
wlr_scene_node_set_position(menuitem->normal.text, x, y);
y = (menu->item_height - menuitem->selected.buffer->height) / 2;
2022-02-19 02:05:38 +01:00
wlr_scene_node_set_position(menuitem->selected.text, x, y);
/* Position the item in relation to its menu */
wlr_scene_node_set_position(&menuitem->tree->node, 0, menu->size.height);
2022-02-19 02:05:38 +01:00
/* Hide selected state */
2022-06-05 14:42:06 +02:00
wlr_scene_node_set_enabled(&menuitem->selected.tree->node, false);
2022-02-19 02:05:38 +01:00
2022-03-03 04:33:33 +01:00
/* Update menu extents */
menu->size.height += menuitem->height;
wl_list_append(&menu->menuitems, &menuitem->link);
wl_list_init(&menuitem->actions);
2020-10-19 22:14:17 +01:00
return menuitem;
}
static struct menuitem *
separator_create(struct menu *menu, const char *label)
{
2022-09-18 15:22:26 -04:00
struct menuitem *menuitem = znew(*menuitem);
menuitem->parent = menu;
menuitem->selectable = false;
struct server *server = menu->server;
struct theme *theme = server->theme;
menuitem->height = theme->menu_separator_line_thickness +
2 * theme->menu_separator_padding_height;
menuitem->tree = wlr_scene_tree_create(menu->scene_tree);
node_descriptor_create(&menuitem->tree->node,
LAB_NODE_DESC_MENUITEM, menuitem);
menuitem->normal.tree = wlr_scene_tree_create(menuitem->tree);
menuitem->normal.background = &wlr_scene_rect_create(
menuitem->normal.tree,
menu->size.width, menuitem->height,
theme->menu_items_bg_color)->node;
int width = menu->size.width - 2 * theme->menu_separator_padding_width;
menuitem->normal.text = &wlr_scene_rect_create(
menuitem->normal.tree,
width > 0 ? width : 0,
theme->menu_separator_line_thickness,
theme->menu_separator_color)->node;
wlr_scene_node_set_position(&menuitem->tree->node, 0, menu->size.height);
/* Vertically center-align separator line */
wlr_scene_node_set_position(menuitem->normal.text,
theme->menu_separator_padding_width,
theme->menu_separator_padding_height);
menu->size.height += menuitem->height;
wl_list_append(&menu->menuitems, &menuitem->link);
wl_list_init(&menuitem->actions);
return menuitem;
}
/*
* Handle the following:
* <item label="">
* <action name="">
* <command></command>
* </action>
* </item>
*/
static void
fill_item(char *nodename, char *content)
2021-02-17 20:38:16 +00:00
{
string_truncate_at_pattern(nodename, ".item.menu");
/* <item label=""> defines the start of a new item */
2021-02-17 20:38:16 +00:00
if (!strcmp(nodename, "label")) {
2022-08-02 22:00:24 +01:00
current_item = item_create(current_menu, content, false);
2022-01-06 03:24:32 +01:00
current_item_action = NULL;
} else if (!current_item) {
wlr_log(WLR_ERROR, "expect <item label=\"\"> element first. "
"nodename: '%s' content: '%s'", nodename, content);
} else if (!strcmp(nodename, "icon")) {
/*
* Do nothing as we don't support menu icons - just avoid
* logging errors if a menu.xml file contains icon="" entries.
*/
} else if (!strcmp(nodename, "name.action")) {
current_item_action = action_create(content);
if (current_item_action) {
wl_list_append(&current_item->actions,
&current_item_action->link);
}
} else if (!current_item_action) {
wlr_log(WLR_ERROR, "expect <action name=\"\"> element first. "
"nodename: '%s' content: '%s'", nodename, content);
} else {
action_arg_from_xml_node(current_item_action, nodename, content);
2021-02-17 20:38:16 +00:00
}
}
static void
item_destroy(struct menuitem *item)
{
wl_list_remove(&item->link);
action_list_free(&item->actions);
wlr_scene_node_destroy(&item->tree->node);
free(item);
}
/*
* We support XML CDATA for <command> in menu.xml in order to provide backward
* compatibility with obmenu-generator. For example:
*
* <menu id="" label="">
* <item label="">
* <action name="Execute">
* <command><![CDATA[xdg-open .]]></command>
* </action>
* </item>
* </menu>
*
* <execute> is an old, deprecated openbox variety of <command>. We support it
* for backward compatibility with old openbox-menu generators. It has the same
* function and <command>
*
* The match_glob() wildcard allows for nested menus giving nodenames with
* ...menu.menu... or ...menu.menu.menu... and so on.
*/
static bool
nodename_supports_cdata(char *nodename)
{
return match_glob("command.action.item.*menu.openbox_menu", nodename)
|| match_glob("execute.action.item.*menu.openbox_menu", nodename);
}
2021-02-17 20:38:16 +00:00
static void
entry(xmlNode *node, char *nodename, char *content)
2021-02-17 20:38:16 +00:00
{
if (!nodename) {
return;
}
xmlChar *cdata = NULL;
if (!content && nodename_supports_cdata(nodename)) {
cdata = xmlNodeGetContent(node);
}
if (!content && !cdata) {
2021-02-17 20:38:16 +00:00
return;
}
string_truncate_at_pattern(nodename, ".openbox_menu");
if (getenv("LABWC_DEBUG_MENU_NODENAMES")) {
printf("%s: %s\n", nodename, content ? content : (char *)cdata);
}
2021-02-17 20:38:16 +00:00
if (in_item) {
fill_item(nodename, content ? content : (char *)cdata);
2021-02-17 20:38:16 +00:00
}
xmlFree(cdata);
2021-02-17 20:38:16 +00:00
}
static void
process_node(xmlNode *node)
2021-02-17 20:38:16 +00:00
{
static char buffer[256];
char *content = (char *)node->content;
2021-02-17 20:38:16 +00:00
if (xmlIsBlankNode(node)) {
return;
}
char *name = nodename(node, buffer, sizeof(buffer));
entry(node, name, content);
2021-02-17 20:38:16 +00:00
}
static void xml_tree_walk(xmlNode *node, struct server *server);
2021-02-17 20:38:16 +00:00
static void
traverse(xmlNode *n, struct server *server)
2021-02-17 20:38:16 +00:00
{
2021-09-24 22:14:04 +01:00
xmlAttr *attr;
process_node(n);
2021-09-24 22:14:04 +01:00
for (attr = n->properties; attr; attr = attr->next) {
xml_tree_walk(attr->children, server);
}
xml_tree_walk(n->children, server);
}
static int
nr_parents(xmlNode *n)
{
assert(n);
int i = 0;
for (xmlNode *node = n->parent; node && i < INT_MAX; ++i) {
node = node->parent;
}
return i;
}
/*
* <menu> elements have three different roles:
* * Definition of (sub)menu - has ID, LABEL and CONTENT
* * Menuitem of pipemenu type - has EXECUTE and LABEL
* * Menuitem of submenu type - has ID only
*/
static void
handle_menu_element(xmlNode *n, struct server *server)
{
char *label = (char *)xmlGetProp(n, (const xmlChar *)"label");
char *execute = (char *)xmlGetProp(n, (const xmlChar *)"execute");
char *id = (char *)xmlGetProp(n, (const xmlChar *)"id");
if (execute) {
wlr_log(WLR_ERROR, "we do not support pipemenus");
} else if ((label && id) || (id && nr_parents(n) == 2)) {
/*
* (label && id) refers to <menu id="" label=""> which is an
* inline menu definition.
*
* (id && nr_parents(n) == 2) refers to:
* <openbox_menu>
* <menu id="">
* </menu>
* </openbox>
*
* which is the highest level a menu can be defined at.
*
* Openbox spec requires a label="" defined here, but it is
* actually pointless so we handle it with or without the label
* attritute to make it easier for users to define "root-menu"
* and "client-menu".
*/
struct menu **submenu = NULL;
if (menu_level > 0) {
/*
* In a nested (inline) menu definition we need to
* create an item pointing to the new submenu
*/
2022-08-02 22:00:24 +01:00
current_item = item_create(current_menu, label, true);
if (current_item) {
submenu = &current_item->submenu;
} else {
submenu = NULL;
}
}
++menu_level;
current_menu = menu_create(server, id, label);
if (submenu) {
*submenu = current_menu;
}
traverse(n, server);
current_menu = current_menu->parent;
--menu_level;
} else if (id) {
/*
* <menu id=""> creates an entry which points to a menu
* defined elsewhere
*/
struct menu *menu = menu_get_by_id(server, id);
if (menu) {
2022-08-02 22:00:24 +01:00
current_item = item_create(current_menu, menu->label, true);
if (current_item) {
current_item->submenu = menu;
}
} else {
wlr_log(WLR_ERROR, "no menu with id '%s'", id);
}
2021-02-17 20:38:16 +00:00
}
2023-10-09 20:46:23 +01:00
free(label);
free(execute);
free(id);
2021-02-17 20:38:16 +00:00
}
/* This can be one of <separator> and <separator label=""> */
static void
handle_separator_element(xmlNode *n)
{
char *label = (char *)xmlGetProp(n, (const xmlChar *)"label");
current_item = separator_create(current_menu, label);
2023-10-09 20:46:23 +01:00
free(label);
}
2021-02-17 20:38:16 +00:00
static void
xml_tree_walk(xmlNode *node, struct server *server)
2021-02-17 20:38:16 +00:00
{
for (xmlNode *n = node; n && n->name; n = n->next) {
if (!strcasecmp((char *)n->name, "comment")) {
continue;
}
if (!strcasecmp((char *)n->name, "menu")) {
handle_menu_element(n, server);
continue;
}
if (!strcasecmp((char *)n->name, "separator")) {
handle_separator_element(n);
continue;
}
2021-02-17 20:38:16 +00:00
if (!strcasecmp((char *)n->name, "item")) {
in_item = true;
traverse(n, server);
2021-02-17 20:38:16 +00:00
in_item = false;
continue;
}
traverse(n, server);
2021-02-17 20:38:16 +00:00
}
}
/*
* @stream can come from either of the following:
* - fopen() in the case of reading a file such as menu.xml
* - popen() when processing pipemenus
*/
2021-02-17 20:38:16 +00:00
static void
parse(struct server *server, FILE *stream)
2021-02-17 20:38:16 +00:00
{
char *line = NULL;
size_t len = 0;
struct buf b;
buf_init(&b);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
2022-01-09 05:48:27 +01:00
if (p) {
2021-02-17 20:38:16 +00:00
*p = '\0';
2022-01-09 05:48:27 +01:00
}
2021-02-17 20:38:16 +00:00
buf_add(&b, line);
}
free(line);
2021-02-19 23:05:14 +00:00
xmlDoc *d = xmlParseMemory(b.buf, b.len);
if (!d) {
wlr_log(WLR_ERROR, "xmlParseMemory()");
goto err;
2021-02-19 23:05:14 +00:00
}
xml_tree_walk(xmlDocGetRootElement(d), server);
2021-02-19 23:05:14 +00:00
xmlFreeDoc(d);
xmlCleanupParser();
err:
2021-02-17 20:38:16 +00:00
free(b.buf);
}
static void
parse_xml(const char *filename, struct server *server)
{
static char buf[4096] = { 0 };
if (!rc.config_dir) {
return;
}
snprintf(buf, sizeof(buf), "%s/%s", rc.config_dir, filename);
FILE *stream = fopen(buf, "r");
if (!stream) {
wlr_log(WLR_ERROR, "cannot read %s", buf);
return;
}
wlr_log(WLR_INFO, "read menu file %s", buf);
parse(server, stream);
fclose(stream);
}
2022-02-19 02:05:38 +01:00
static int
menu_get_full_width(struct menu *menu)
{
int width = menu->size.width - menu->server->theme->menu_overlap_x;
int child_width;
int max_child_width = 0;
struct menuitem *item;
wl_list_for_each(item, &menu->menuitems, link) {
2022-02-19 02:05:38 +01:00
if (!item->submenu) {
continue;
}
child_width = menu_get_full_width(item->submenu);
if (child_width > max_child_width) {
max_child_width = child_width;
}
}
return width + max_child_width;
}
static void
2022-02-19 02:05:38 +01:00
menu_configure(struct menu *menu, int lx, int ly, enum menu_align align)
2020-10-19 22:14:17 +01:00
{
2021-11-08 17:36:39 +00:00
struct theme *theme = menu->server->theme;
2022-02-19 02:05:38 +01:00
/* Get output local coordinates + output usable area */
double ox = lx;
double oy = ly;
struct wlr_output *wlr_output = wlr_output_layout_output_at(
menu->server->output_layout, lx, ly);
struct output *output = wlr_output ? output_from_wlr_output(
menu->server, wlr_output) : NULL;
if (!output) {
wlr_log(WLR_ERROR,
"Failed to position menu %s (%s) and its submenus: "
"Not enough screen space", menu->id, menu->label);
return;
}
2022-02-19 02:05:38 +01:00
wlr_output_layout_output_coords(menu->server->output_layout,
wlr_output, &ox, &oy);
if (align == LAB_MENU_OPEN_AUTO) {
int full_width = menu_get_full_width(menu);
if (ox + full_width > output->usable_area.width) {
2022-02-19 02:05:38 +01:00
align = LAB_MENU_OPEN_LEFT;
} else {
align = LAB_MENU_OPEN_RIGHT;
}
}
if (oy + menu->size.height > output->usable_area.height) {
2022-02-19 02:05:38 +01:00
align &= ~LAB_MENU_OPEN_BOTTOM;
align |= LAB_MENU_OPEN_TOP;
} else {
align &= ~LAB_MENU_OPEN_TOP;
align |= LAB_MENU_OPEN_BOTTOM;
}
if (align & LAB_MENU_OPEN_LEFT) {
lx -= menu->size.width - theme->menu_overlap_x;
2022-02-19 02:05:38 +01:00
}
if (align & LAB_MENU_OPEN_TOP) {
ly -= menu->size.height;
if (menu->parent) {
/* For submenus adjust y to bottom left corner */
ly += menu->item_height;
}
2021-02-17 20:38:16 +00:00
}
2022-02-19 02:05:38 +01:00
wlr_scene_node_set_position(&menu->scene_tree->node, lx, ly);
2021-02-17 20:38:16 +00:00
2022-02-19 02:05:38 +01:00
int rel_y;
int new_lx, new_ly;
struct menuitem *item;
wl_list_for_each(item, &menu->menuitems, link) {
2022-02-19 02:05:38 +01:00
if (!item->submenu) {
continue;
}
if (align & LAB_MENU_OPEN_RIGHT) {
new_lx = lx + menu->size.width - theme->menu_overlap_x;
2022-02-19 02:05:38 +01:00
} else {
new_lx = lx;
}
rel_y = item->tree->node.y;
2022-02-19 02:05:38 +01:00
new_ly = ly + rel_y - theme->menu_overlap_y;
menu_configure(item->submenu, new_lx, new_ly, align);
}
}
static void
menu_hide_submenu(struct server *server, const char *id)
{
struct menu *menu, *hide_menu;
hide_menu = menu_get_by_id(server, id);
if (!hide_menu) {
return;
}
wl_list_for_each(menu, &server->menus, link) {
bool should_reposition = false;
struct menuitem *item, *next;
wl_list_for_each_safe(item, next, &menu->menuitems, link) {
if (item->submenu == hide_menu) {
item_destroy(item);
should_reposition = true;
}
}
if (!should_reposition) {
continue;
}
/* Re-position items vertically */
menu->size.height = 0;
wl_list_for_each(item, &menu->menuitems, link) {
wlr_scene_node_set_position(&item->tree->node, 0,
menu->size.height);
menu->size.height += item->height;
}
}
}
static void
init_rootmenu(struct server *server)
{
struct menu *menu = menu_get_by_id(server, "root-menu");
2021-02-19 23:05:14 +00:00
2021-02-17 20:38:16 +00:00
/* Default menu if no menu.xml found */
2022-02-19 02:05:38 +01:00
if (!menu) {
2022-01-26 00:07:10 +01:00
current_menu = NULL;
2022-02-19 02:05:38 +01:00
menu = menu_create(server, "root-menu", "");
}
2022-02-19 02:05:38 +01:00
if (wl_list_empty(&menu->menuitems)) {
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Reconfigure"), false);
fill_item("name.action", "Reconfigure");
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Exit"), false);
fill_item("name.action", "Exit");
2021-02-17 20:38:16 +00:00
}
2020-10-19 22:14:17 +01:00
}
static void
init_windowmenu(struct server *server)
2022-01-26 00:07:10 +01:00
{
struct menu *menu = menu_get_by_id(server, "client-menu");
2022-01-26 00:07:10 +01:00
/* Default menu if no menu.xml found */
2022-02-19 02:05:38 +01:00
if (!menu) {
2022-01-26 00:07:10 +01:00
current_menu = NULL;
2022-02-19 02:05:38 +01:00
menu = menu_create(server, "client-menu", "");
2022-01-26 00:07:10 +01:00
}
2022-02-19 02:05:38 +01:00
if (wl_list_empty(&menu->menuitems)) {
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Minimize"), false);
2022-01-26 00:07:10 +01:00
fill_item("name.action", "Iconify");
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Maximize"), false);
2022-01-26 00:07:10 +01:00
fill_item("name.action", "ToggleMaximize");
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Fullscreen"), false);
2022-01-26 00:07:10 +01:00
fill_item("name.action", "ToggleFullscreen");
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Decorations"), false);
2022-01-26 00:07:10 +01:00
fill_item("name.action", "ToggleDecorations");
current_item = item_create(menu, _("Always on Top"), false);
2022-04-09 01:16:09 +02:00
fill_item("name.action", "ToggleAlwaysOnTop");
/* Workspace sub-menu */
struct menu *workspace_menu = menu_create(server, "workspaces", "");
2022-08-02 22:00:24 +01:00
current_item = item_create(workspace_menu, _("Move left"), false);
/*
* <action name="SendToDesktop"><follow> is true by default so
* GoToDesktop will be called as part of the action.
*/
fill_item("name.action", "SendToDesktop");
fill_item("to.action", "left");
2022-08-02 22:00:24 +01:00
current_item = item_create(workspace_menu, _("Move right"), false);
fill_item("name.action", "SendToDesktop");
fill_item("to.action", "right");
current_item = separator_create(workspace_menu, "");
current_item = item_create(workspace_menu,
_("Always on Visible Workspace"), false);
fill_item("name.action", "ToggleOmnipresent");
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Workspace"), true);
current_item->submenu = workspace_menu;
2022-08-02 22:00:24 +01:00
current_item = item_create(menu, _("Close"), false);
2022-01-26 00:07:10 +01:00
fill_item("name.action", "Close");
}
if (wl_list_length(&rc.workspace_config.workspaces) == 1) {
menu_hide_submenu(server, "workspaces");
}
2022-01-26 00:07:10 +01:00
}
void
menu_init(struct server *server)
{
wl_list_init(&server->menus);
parse_xml("menu.xml", server);
init_rootmenu(server);
init_windowmenu(server);
post_processing(server);
validate(server);
}
2020-10-22 19:43:27 +01:00
void
menu_finish(struct server *server)
2020-10-22 19:43:27 +01:00
{
struct menu *menu, *tmp_menu;
wl_list_for_each_safe(menu, tmp_menu, &server->menus, link) {
struct menuitem *item, *next;
wl_list_for_each_safe(item, next, &menu->menuitems, link) {
item_destroy(item);
}
/**
* Destroying the root node will destroy everything,
* including node descriptors and scaled_font_buffers.
*/
2022-02-19 02:05:38 +01:00
wlr_scene_node_destroy(&menu->scene_tree->node);
wl_list_remove(&menu->link);
zfree(menu);
}
}
2022-02-19 02:05:38 +01:00
/* Sets selection (or clears selection if passing NULL) */
static void
menu_set_selection(struct menu *menu, struct menuitem *item)
{
/* Clear old selection */
if (menu->selection.item) {
wlr_scene_node_set_enabled(
2022-06-05 14:42:06 +02:00
&menu->selection.item->normal.tree->node, true);
2022-02-19 02:05:38 +01:00
wlr_scene_node_set_enabled(
2022-06-05 14:42:06 +02:00
&menu->selection.item->selected.tree->node, false);
2022-02-19 02:05:38 +01:00
}
/* Set new selection */
if (item) {
2022-06-05 14:42:06 +02:00
wlr_scene_node_set_enabled(&item->normal.tree->node, false);
wlr_scene_node_set_enabled(&item->selected.tree->node, true);
2022-02-19 02:05:38 +01:00
}
menu->selection.item = item;
}
static void
close_all_submenus(struct menu *menu)
{
struct menuitem *item;
2022-11-03 19:58:21 +00:00
wl_list_for_each(item, &menu->menuitems, link) {
if (item->submenu) {
2022-04-04 20:53:36 +01:00
wlr_scene_node_set_enabled(
&item->submenu->scene_tree->node, false);
close_all_submenus(item->submenu);
}
2020-10-22 19:43:27 +01:00
}
2022-02-19 02:05:38 +01:00
menu->selection.menu = NULL;
2020-10-22 19:43:27 +01:00
}
static void
menu_close(struct menu *menu)
{
if (!menu) {
wlr_log(WLR_ERROR, "Trying to close non exiting menu");
return;
}
wlr_scene_node_set_enabled(&menu->scene_tree->node, false);
menu_set_selection(menu, NULL);
if (menu->selection.menu) {
menu_close(menu->selection.menu);
menu->selection.menu = NULL;
}
}
2020-10-19 22:14:17 +01:00
void
2022-02-19 02:05:38 +01:00
menu_open(struct menu *menu, int x, int y)
2020-10-19 22:14:17 +01:00
{
assert(menu);
2022-02-19 02:05:38 +01:00
if (menu->server->menu_current) {
menu_close(menu->server->menu_current);
}
close_all_submenus(menu);
2022-02-19 02:05:38 +01:00
menu_set_selection(menu, NULL);
menu_configure(menu, x, y, LAB_MENU_OPEN_AUTO);
wlr_scene_node_set_enabled(&menu->scene_tree->node, true);
menu->server->menu_current = menu;
menu->server->input_mode = LAB_INPUT_STATE_MENU;
}
2020-10-19 22:14:17 +01:00
static void
menu_process_item_selection(struct menuitem *item)
{
assert(item);
if (!item->selectable) {
return;
}
2022-02-19 02:05:38 +01:00
2022-03-03 04:33:33 +01:00
/* We are on an item that has new mouse-focus */
menu_set_selection(item->parent, item);
if (item->parent->selection.menu) {
/* Close old submenu tree */
menu_close(item->parent->selection.menu);
}
2021-08-09 17:28:39 +01:00
2022-03-03 04:33:33 +01:00
if (item->submenu) {
/* Sync the triggering view */
item->submenu->triggered_by_view = item->parent->triggered_by_view;
/* Ensure the submenu has its parent set correctly */
item->submenu->parent = item->parent;
/* And open the new submenu tree */
2022-03-03 04:33:33 +01:00
wlr_scene_node_set_enabled(
&item->submenu->scene_tree->node, true);
}
2022-03-03 04:33:33 +01:00
item->parent->selection.menu = item->submenu;
2020-10-19 22:14:17 +01:00
}
/* Get the deepest submenu with active item selection or the root menu itself */
static struct menu *
get_selection_leaf(struct server *server)
2020-10-19 22:14:17 +01:00
{
struct menu *menu = server->menu_current;
if (!menu) {
return NULL;
}
2022-02-19 02:05:38 +01:00
while (menu->selection.menu) {
if (!menu->selection.menu->selection.item) {
return menu;
}
menu = menu->selection.menu;
}
return menu;
}
/* Selects the next or previous sibling of the currently selected item */
static void
menu_item_select(struct server *server, bool forward)
{
struct menu *menu = get_selection_leaf(server);
if (!menu) {
return;
}
struct menuitem *item = NULL;
struct menuitem *selection = menu->selection.item;
struct wl_list *start = selection ? &selection->link : &menu->menuitems;
struct wl_list *current = start;
while (!item || !item->selectable) {
current = forward ? current->next : current->prev;
if (current == start) {
return;
}
if (current == &menu->menuitems) {
/* Allow wrap around */
item = NULL;
continue;
}
item = wl_container_of(current, item, link);
}
menu_process_item_selection(item);
}
static bool
menu_execute_item(struct menuitem *item)
{
assert(item);
if (item->submenu || !item->selectable) {
/* We received a click on a separator or item that just opens a submenu */
2022-02-19 02:05:38 +01:00
return false;
}
2022-03-03 04:33:33 +01:00
2023-10-09 20:46:23 +01:00
/*
* We close the menu here to provide a faster feedback to the user.
* We do that without resetting the input state so src/cursor.c
* can do its own clean up on the following RELEASE event.
*/
2022-03-03 04:33:33 +01:00
menu_close(item->parent->server->menu_current);
item->parent->server->menu_current = NULL;
struct server *server = item->parent->server;
menu_close_root(server);
cursor_update_focus(server);
/*
* We call the actions after menu_close_root() so that virtual keyboard
* input is sent to the focused_surface instead of being absorbed by the
* menu. Consider for example: `wlrctl keyboard type abc`
*/
actions_run(item->parent->triggered_by_view, server, &item->actions, 0);
2022-03-03 04:33:33 +01:00
return true;
2020-10-19 22:14:17 +01:00
}
/* Keyboard based selection */
void
menu_item_select_next(struct server *server)
{
menu_item_select(server, /* forward */ true);
}
void
menu_item_select_previous(struct server *server)
{
menu_item_select(server, /* forward */ false);
}
bool
menu_call_selected_actions(struct server *server)
{
struct menu *menu = get_selection_leaf(server);
if (!menu || !menu->selection.item) {
return false;
}
return menu_execute_item(menu->selection.item);
}
/* Selects the first item on the submenu attached to the current selection */
void
menu_submenu_enter(struct server *server)
{
struct menu *menu = get_selection_leaf(server);
if (!menu || !menu->selection.menu) {
return;
}
struct wl_list *start = &menu->selection.menu->menuitems;
struct wl_list *current = start;
struct menuitem *item = NULL;
while (!item || !item->selectable) {
current = current->next;
if (current == start) {
return;
}
item = wl_container_of(current, item, link);
}
menu_process_item_selection(item);
}
/* Re-selects the selected item on the parent menu of the current selection */
void
menu_submenu_leave(struct server *server)
{
struct menu *menu = get_selection_leaf(server);
if (!menu || !menu->parent || !menu->parent->selection.item) {
return;
}
menu_process_item_selection(menu->parent->selection.item);
}
/* Mouse based selection */
void
menu_process_cursor_motion(struct wlr_scene_node *node)
{
assert(node && node->data);
struct menuitem *item = node_menuitem_from_node(node);
if (item->selectable && node == &item->selected.tree->node) {
/* We are on an already selected item */
return;
}
menu_process_item_selection(item);
}
bool
menu_call_actions(struct wlr_scene_node *node)
{
assert(node && node->data);
struct menuitem *item = node_menuitem_from_node(node);
return menu_execute_item(item);
}
2020-10-19 22:14:17 +01:00
void
menu_close_root(struct server *server)
2020-10-19 22:14:17 +01:00
{
assert(server->input_mode == LAB_INPUT_STATE_MENU);
if (server->menu_current) {
menu_close(server->menu_current);
server->menu_current = NULL;
2020-10-19 22:14:17 +01:00
}
server->input_mode = LAB_INPUT_STATE_PASSTHROUGH;
2020-10-19 22:14:17 +01:00
}
2021-02-19 23:05:14 +00:00
void
2022-02-19 02:05:38 +01:00
menu_reconfigure(struct server *server)
2021-02-19 23:05:14 +00:00
{
menu_finish(server);
server->menu_current = NULL;
menu_init(server);
2021-02-19 23:05:14 +00:00
}