labwc/src/menu/menu.c

418 lines
9.4 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 <ctype.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>
2021-07-22 21:30:17 +01:00
#include <wlr/util/log.h>
2021-02-17 20:38:16 +00:00
#include "common/buf.h"
#include "common/dir.h"
2020-10-21 20:32:08 +01:00
#include "common/font.h"
2021-02-17 20:38:16 +00:00
#include "common/nodename.h"
#include "common/string-helpers.h"
2021-07-01 19:21:09 +01:00
#include "common/zfree.h"
2020-10-19 22:14:17 +01:00
#include "labwc.h"
#include "menu/menu.h"
2021-02-21 22:18:34 +00:00
#include "theme.h"
2020-10-19 22:14:17 +01:00
#define MENUWIDTH (110)
#define MENU_ITEM_PADDING_Y (4)
#define MENU_ITEM_PADDING_X (7)
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 menu *current_menu;
/* vector for <menu id="" label=""> elements */
static struct menu *menus;
static int nr_menus, alloc_menus;
static struct menu *
menu_create(struct server *server, const char *id, const char *label)
{
if (nr_menus == alloc_menus) {
alloc_menus = (alloc_menus + 16) * 2;
menus = realloc(menus, alloc_menus * sizeof(struct menu));
}
struct menu *menu = menus + nr_menus;
memset(menu, 0, sizeof(*menu));
nr_menus++;
wl_list_init(&menu->menuitems);
menu->id = strdup(id);
menu->label = strdup(label);
menu->server = server;
return menu;
}
static struct menu *
get_menu_by_id(const char *id)
{
struct menu *menu;
for (int i = 0; i < nr_menus; ++i) {
menu = menus + i;
if (!strcmp(menu->id, id)) {
return menu;
}
}
return NULL;
}
2020-10-19 22:14:17 +01:00
2021-07-01 19:21:09 +01:00
static struct menuitem *
item_create(struct menu *menu, const char *text)
2020-10-19 22:14:17 +01:00
{
struct menuitem *menuitem = calloc(1, sizeof(struct menuitem));
if (!menuitem) {
return NULL;
}
struct server *server = menu->server;
2021-02-21 21:54:40 +00:00
struct theme *theme = server->theme;
struct font font = {
.name = rc.font_name_menuitem,
.size = rc.font_size_menuitem,
};
menuitem->box.width = MENUWIDTH;
menuitem->box.height = font_height(&font) + 2 * MENU_ITEM_PADDING_Y;
int item_max_width = MENUWIDTH - 2 * MENU_ITEM_PADDING_X;
font_texture_create(server, &menuitem->texture.active, item_max_width,
text, &font, theme->menu_items_active_text_color);
font_texture_create(server, &menuitem->texture.inactive, item_max_width,
text, &font, theme->menu_items_text_color);
/* center align vertically */
menuitem->texture.offset_y =
(menuitem->box.height - menuitem->texture.active->height) / 2;
menuitem->texture.offset_x = MENU_ITEM_PADDING_X;
2020-10-19 22:14:17 +01:00
wl_list_insert(&menu->menuitems, &menuitem->link);
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")) {
current_item = item_create(current_menu, content);
}
if (!current_item) {
wlr_log(WLR_ERROR, "expect <item label=\"\"> element first");
return;
2021-02-17 20:38:16 +00:00
}
if (!strcmp(nodename, "name.action")) {
current_item->action = strdup(content);
} else if (!strcmp(nodename, "command.action")) {
current_item->command = strdup(content);
}
}
static void
entry(xmlNode *node, char *nodename, char *content)
2021-02-17 20:38:16 +00:00
{
if (!nodename || !content) {
2021-02-17 20:38:16 +00:00
return;
}
string_truncate_at_pattern(nodename, ".openbox_menu");
if (in_item) {
fill_item(nodename, content);
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);
}
/*
* <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) {
current_menu = menu_create(server, id, label);
/* TODO: deal with nested menu definitions */
} else if (id) {
struct menu *menu = get_menu_by_id(id);
if (menu) {
current_item = item_create(current_menu, menu->label);
current_item->submenu = menu;
} else {
wlr_log(WLR_ERROR, "no menu with id '%s'", id);
}
2021-02-17 20:38:16 +00:00
}
zfree(label);
zfree(execute);
zfree(id);
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);
traverse(n, server);
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
}
}
static void
parse_xml(const char *filename, struct server *server)
2021-02-17 20:38:16 +00:00
{
FILE *stream;
char *line = NULL;
size_t len = 0;
struct buf b;
static char menuxml[4096] = { 0 };
if (!strlen(config_dir())) {
return;
}
2021-02-19 23:05:14 +00:00
snprintf(menuxml, sizeof(menuxml), "%s/%s", config_dir(), filename);
2021-02-17 20:38:16 +00:00
stream = fopen(menuxml, "r");
if (!stream) {
wlr_log(WLR_ERROR, "cannot read %s", menuxml);
2021-02-17 20:38:16 +00:00
return;
}
2021-07-22 21:30:17 +01:00
wlr_log(WLR_INFO, "read menu file %s", menuxml);
2021-02-17 20:38:16 +00:00
buf_init(&b);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p)
*p = '\0';
buf_add(&b, line);
}
free(line);
fclose(stream);
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
menu_configure(struct menu *menu, int x, int y)
2020-10-19 22:14:17 +01:00
{
menu->box.x = x;
menu->box.y = y;
2021-02-17 20:38:16 +00:00
int offset = 0;
struct menuitem *menuitem;
wl_list_for_each_reverse (menuitem, &menu->menuitems, link) {
menuitem->box.x = menu->box.x;
menuitem->box.y = menu->box.y + offset;
offset += menuitem->box.height;
if (menuitem->submenu) {
/* TODO: add offset to rc.xml */
menu_configure(menuitem->submenu,
menuitem->box.x + MENUWIDTH + 10,
menuitem->box.y);
}
2021-02-17 20:38:16 +00:00
}
menu->box.width = MENUWIDTH;
menu->box.height = offset;
}
void
menu_init_rootmenu(struct server *server)
{
parse_xml("menu.xml", server);
server->rootmenu = get_menu_by_id("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 */
if (!server->rootmenu) {
server->rootmenu = menu_create(server, "root-menu", "");
}
if (wl_list_empty(&server->rootmenu->menuitems)) {
current_item = item_create(server->rootmenu, "Reconfigure");
2021-02-17 20:38:16 +00:00
current_item->action = strdup("Reconfigure");
current_item = item_create(server->rootmenu, "Exit");
2021-02-17 20:38:16 +00:00
current_item->action = strdup("Exit");
}
server->rootmenu->visible = true;
menu_configure(server->rootmenu, 100, 100);
2020-10-19 22:14:17 +01:00
}
2020-10-22 19:43:27 +01:00
void
menu_finish(void)
2020-10-22 19:43:27 +01:00
{
struct menu *menu;
for (int i = 0; i < nr_menus; ++i) {
menu = menus + i;
struct menuitem *item, *next;
wl_list_for_each_safe(item, next, &menu->menuitems, link) {
zfree(item->action);
zfree(item->command);
wl_list_remove(&item->link);
free(item);
}
}
zfree(menus);
alloc_menus = 0;
nr_menus = 0;
}
static void
close_all_submenus(struct menu *menu)
{
struct menuitem *item;
wl_list_for_each (item, &menu->menuitems, link) {
if (item->submenu) {
item->submenu->visible = false;
close_all_submenus(item->submenu);
}
2020-10-22 19:43:27 +01:00
}
}
2020-10-19 22:14:17 +01:00
void
menu_move(struct menu *menu, int x, int y)
{
assert(menu);
close_all_submenus(menu);
menu_configure(menu, x, y);
}
2020-10-19 22:14:17 +01:00
/* TODO: consider renaming function to menu_process_cursor_motion */
void
menu_set_selected(struct menu *menu, int x, int y)
{
if (!menu->visible) {
return;
2020-10-19 22:14:17 +01:00
}
2021-08-09 17:28:39 +01:00
struct menuitem *item;
wl_list_for_each (item, &menu->menuitems, link) {
item->selected = wlr_box_contains_point(&item->box, x, y);
if (!item->selected) {
if (item->submenu && item->submenu->visible) {
/*
* Handle the case where a submenu is already
* open.
*/
item->selected = true;
menu_set_selected(item->submenu, x, y);
}
continue;
}
/* We're now on an item that has mouse-focus */
if (item->submenu) {
if (item->submenu->visible) {
/* do nothing - submenu already open */
} else {
/* open submenu */
close_all_submenus(menu);
item->submenu->visible = true;
menu_set_selected(item->submenu, x, y);
}
} else {
close_all_submenus(menu);
}
}
2020-10-19 22:14:17 +01:00
}
static void
menu_clear_selection(struct menu *menu)
2020-10-19 22:14:17 +01:00
{
struct menuitem *item;
wl_list_for_each (item, &menu->menuitems, link) {
item->selected = false;
if (item->submenu) {
menu_clear_selection(item->submenu);
}
2020-10-19 22:14:17 +01:00
}
}
void
menu_action_selected(struct server *server, struct menu *menu)
{
struct menuitem *menuitem;
wl_list_for_each (menuitem, &menu->menuitems, link) {
if (menuitem->selected && !menuitem->submenu) {
2020-10-19 22:14:17 +01:00
action(server, menuitem->action, menuitem->command);
break;
}
if (menuitem->submenu) {
menu_action_selected(server, menuitem->submenu);
}
2020-10-19 22:14:17 +01:00
}
menu_clear_selection(menu);
2020-10-19 22:14:17 +01:00
}
2021-02-19 23:05:14 +00:00
void
2021-02-19 23:31:14 +00:00
menu_reconfigure(struct server *server, struct menu *menu)
2021-02-19 23:05:14 +00:00
{
menu_finish();
menu_init_rootmenu(server);
2021-02-19 23:05:14 +00:00
}