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
|
|
|
|
2021-02-17 20:38:16 +00:00
|
|
|
/* state-machine variables for processing <item></item> */
|
|
|
|
|
static bool in_item = false;
|
|
|
|
|
static struct menuitem *current_item;
|
|
|
|
|
|
2021-08-07 08:35:46 +01:00
|
|
|
#define MENUWIDTH (110)
|
2021-08-20 20:20:49 +01:00
|
|
|
#define MENU_ITEM_PADDING_Y (4)
|
|
|
|
|
#define MENU_ITEM_PADDING_X (7)
|
2020-10-19 22:14:17 +01:00
|
|
|
|
2021-07-01 19:21:09 +01:00
|
|
|
static struct menuitem *
|
2021-02-17 20:38:16 +00:00
|
|
|
menuitem_create(struct server *server, 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;
|
|
|
|
|
}
|
2021-02-21 21:54:40 +00:00
|
|
|
struct theme *theme = server->theme;
|
2021-08-20 20:20:49 +01:00
|
|
|
struct font font = {
|
|
|
|
|
.name = rc.font_name_menuitem,
|
|
|
|
|
.size = rc.font_size_menuitem,
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-07 08:35:46 +01:00
|
|
|
menuitem->box.width = MENUWIDTH;
|
2021-08-20 20:20:49 +01:00
|
|
|
menuitem->box.height = font_height(&font) + 2 * MENU_ITEM_PADDING_Y;
|
2021-08-07 08:35:46 +01:00
|
|
|
|
2021-08-20 20:20:49 +01:00
|
|
|
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);
|
2021-08-07 08:35:46 +01:00
|
|
|
|
|
|
|
|
/* center align vertically */
|
|
|
|
|
menuitem->texture.offset_y =
|
|
|
|
|
(menuitem->box.height - menuitem->texture.active->height) / 2;
|
2021-08-20 20:20:49 +01:00
|
|
|
menuitem->texture.offset_x = MENU_ITEM_PADDING_X;
|
2021-08-07 08:35:46 +01:00
|
|
|
|
2020-10-19 22:14:17 +01:00
|
|
|
wl_list_insert(&menu->menuitems, &menuitem->link);
|
|
|
|
|
return menuitem;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 23:31:14 +00:00
|
|
|
static void fill_item(char *nodename, char *content, struct menu *menu)
|
2021-02-17 20:38:16 +00:00
|
|
|
{
|
|
|
|
|
string_truncate_at_pattern(nodename, ".item.menu");
|
|
|
|
|
|
|
|
|
|
if (getenv("LABWC_DEBUG_MENU_NODENAMES")) {
|
|
|
|
|
printf("%s: %s\n", nodename, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Handle the following:
|
|
|
|
|
* <item label="">
|
|
|
|
|
* <action name="">
|
|
|
|
|
* <command></command>
|
|
|
|
|
* </action>
|
|
|
|
|
* </item>
|
|
|
|
|
*/
|
|
|
|
|
if (!strcmp(nodename, "label")) {
|
2021-02-19 23:31:14 +00:00
|
|
|
current_item = menuitem_create(menu->server, menu, content);
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
assert(current_item);
|
|
|
|
|
if (!strcmp(nodename, "name.action")) {
|
|
|
|
|
current_item->action = strdup(content);
|
|
|
|
|
} else if (!strcmp(nodename, "command.action")) {
|
|
|
|
|
current_item->command = strdup(content);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2021-02-19 23:31:14 +00:00
|
|
|
entry(xmlNode *node, char *nodename, char *content, struct menu *menu)
|
2021-02-17 20:38:16 +00:00
|
|
|
{
|
|
|
|
|
static bool in_root_menu = false;
|
|
|
|
|
|
|
|
|
|
if (!nodename) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
string_truncate_at_pattern(nodename, ".openbox_menu");
|
|
|
|
|
if (!content) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!strcmp(nodename, "id.menu")) {
|
|
|
|
|
in_root_menu = !strcmp(content, "root-menu") ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We only handle the root-menu for the time being */
|
|
|
|
|
if (!in_root_menu) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (in_item) {
|
2021-02-19 23:31:14 +00:00
|
|
|
fill_item(nodename, content, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2021-02-19 23:31:14 +00:00
|
|
|
process_node(xmlNode *node, struct menu *menu)
|
2021-02-17 20:38:16 +00:00
|
|
|
{
|
|
|
|
|
char *content;
|
|
|
|
|
static char buffer[256];
|
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
|
|
content = (char *)node->content;
|
|
|
|
|
if (xmlIsBlankNode(node)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
name = nodename(node, buffer, sizeof(buffer));
|
2021-02-19 23:31:14 +00:00
|
|
|
entry(node, name, content, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-19 23:31:14 +00:00
|
|
|
static void xml_tree_walk(xmlNode *node, struct menu *menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
|
|
|
|
|
static void
|
2021-02-19 23:31:14 +00:00
|
|
|
traverse(xmlNode *n, struct menu *menu)
|
2021-02-17 20:38:16 +00:00
|
|
|
{
|
2021-02-19 23:31:14 +00:00
|
|
|
process_node(n, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
for (xmlAttr *attr = n->properties; attr; attr = attr->next) {
|
2021-02-19 23:31:14 +00:00
|
|
|
xml_tree_walk(attr->children, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
2021-02-19 23:31:14 +00:00
|
|
|
xml_tree_walk(n->children, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2021-02-19 23:31:14 +00:00
|
|
|
xml_tree_walk(xmlNode *node, struct menu *menu)
|
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, "item")) {
|
|
|
|
|
in_item = true;
|
2021-02-19 23:31:14 +00:00
|
|
|
traverse(n, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
in_item = false;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-02-19 23:31:14 +00:00
|
|
|
traverse(n, menu);
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2021-02-19 23:31:14 +00:00
|
|
|
parse_xml(const char *filename, struct menu *menu)
|
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) {
|
2021-07-23 21:15:55 +01:00
|
|
|
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) {
|
2021-07-23 21:15:55 +01:00
|
|
|
wlr_log(WLR_ERROR, "xmlParseMemory()");
|
2021-02-19 23:05:14 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
2021-02-19 23:31:14 +00:00
|
|
|
xml_tree_walk(xmlDocGetRootElement(d), menu);
|
2021-02-19 23:05:14 +00:00
|
|
|
xmlFreeDoc(d);
|
|
|
|
|
xmlCleanupParser();
|
2021-02-17 20:38:16 +00:00
|
|
|
free(b.buf);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 22:14:17 +01:00
|
|
|
void
|
2021-02-19 23:05:14 +00:00
|
|
|
menu_init_rootmenu(struct server *server, struct menu *menu)
|
2020-10-19 22:14:17 +01:00
|
|
|
{
|
2021-02-17 20:38:16 +00:00
|
|
|
static bool has_run;
|
|
|
|
|
|
2021-02-19 23:05:14 +00:00
|
|
|
if (!has_run) {
|
|
|
|
|
LIBXML_TEST_VERSION
|
|
|
|
|
wl_list_init(&menu->menuitems);
|
|
|
|
|
server->rootmenu = menu;
|
2021-02-19 23:31:14 +00:00
|
|
|
menu->server = server;
|
2021-02-17 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-19 23:31:14 +00:00
|
|
|
parse_xml("menu.xml", menu);
|
2021-02-19 23:05:14 +00:00
|
|
|
|
2021-02-17 20:38:16 +00:00
|
|
|
/* Default menu if no menu.xml found */
|
2021-02-19 23:05:14 +00:00
|
|
|
if (wl_list_empty(&menu->menuitems)) {
|
2021-02-17 20:38:16 +00:00
|
|
|
current_item = menuitem_create(server, menu, "Reconfigure");
|
|
|
|
|
current_item->action = strdup("Reconfigure");
|
|
|
|
|
current_item = menuitem_create(server, menu, "Exit");
|
|
|
|
|
current_item->action = strdup("Exit");
|
|
|
|
|
}
|
2020-10-19 22:14:17 +01:00
|
|
|
menu_move(menu, 100, 100);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-22 19:43:27 +01:00
|
|
|
void
|
|
|
|
|
menu_finish(struct menu *menu)
|
|
|
|
|
{
|
|
|
|
|
struct menuitem *menuitem, *next;
|
|
|
|
|
wl_list_for_each_safe(menuitem, next, &menu->menuitems, link) {
|
2021-07-01 19:21:09 +01:00
|
|
|
zfree(menuitem->action);
|
|
|
|
|
zfree(menuitem->command);
|
2020-10-22 19:43:27 +01:00
|
|
|
wl_list_remove(&menuitem->link);
|
|
|
|
|
free(menuitem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-07 08:35:46 +01:00
|
|
|
/*
|
|
|
|
|
* TODO: call this menu_configure and return the size of the menu so that
|
|
|
|
|
* a background color can be rendered
|
|
|
|
|
*/
|
2020-10-19 22:14:17 +01:00
|
|
|
void
|
|
|
|
|
menu_move(struct menu *menu, int x, int y)
|
|
|
|
|
{
|
2021-08-09 17:28:39 +01:00
|
|
|
menu->box.x = x;
|
|
|
|
|
menu->box.y = y;
|
2020-10-19 22:14:17 +01:00
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
struct menuitem *menuitem;
|
2020-10-21 20:32:08 +01:00
|
|
|
wl_list_for_each_reverse (menuitem, &menu->menuitems, link) {
|
2021-08-09 17:28:39 +01:00
|
|
|
menuitem->box.x = menu->box.x;
|
|
|
|
|
menuitem->box.y = menu->box.y + offset;
|
2021-08-07 08:35:46 +01:00
|
|
|
offset += menuitem->box.height;
|
2020-10-19 22:14:17 +01:00
|
|
|
}
|
2021-08-09 17:28:39 +01:00
|
|
|
|
|
|
|
|
menu->box.width = MENUWIDTH;
|
|
|
|
|
menu->box.height = offset;
|
2020-10-19 22:14:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
menu_set_selected(struct menu *menu, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
struct menuitem *menuitem;
|
|
|
|
|
wl_list_for_each (menuitem, &menu->menuitems, link) {
|
|
|
|
|
menuitem->selected =
|
2021-08-07 08:35:46 +01:00
|
|
|
wlr_box_contains_point(&menuitem->box, x, y);
|
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) {
|
|
|
|
|
action(server, menuitem->action, menuitem->command);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
{
|
2021-02-19 23:31:14 +00:00
|
|
|
menu_finish(menu);
|
|
|
|
|
menu_init_rootmenu(server, menu);
|
2021-02-19 23:05:14 +00:00
|
|
|
}
|