From dd1ce83521403d81ebfe46f7815a10220e4f3a96 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Sat, 26 Apr 2025 23:27:07 +0200 Subject: Add about:keys to show keyboard shortcuts Fixes: https://github.com/dillo-browser/dillo/issues/66 --- src/keys.cc | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-) (limited to 'src/keys.cc') diff --git a/src/keys.cc b/src/keys.cc index 7346706e..bbbd30bb 100644 --- a/src/keys.cc +++ b/src/keys.cc @@ -2,7 +2,7 @@ * Key parser * * Copyright (C) 2009 Jorge Arellano Cid - * Copyright (C) 2024 Rodrigo Arias Mallo + * Copyright (C) 2024-2025 Rodrigo Arias Mallo * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,8 @@ #include "keys.hh" #include "utf8.hh" #include "msg.h" +#include "cache.h" +#include "misc.h" /* * Local data types @@ -260,6 +262,26 @@ int Keys::getKeyCode(char *keyName) return -1; } +const char *Keys::getKeyName(int key) +{ + static char buf[128]; + + uint_t i; + for (i = 0; i < sizeof(keyNames) / sizeof(keyNames[0]); i++) { + if (keyNames[i].value == key) + return keyNames[i].name; + } + + if (d_isascii(key)) { + sprintf(buf, "%c", key); + } else { + /* Otherwise print hexadecimal */ + sprintf(buf, "0x%x", key); + } + + return buf; +} + /** * Takes a command name and searches it in the mapping table. * Return value: command code if found, -1 otherwise @@ -398,3 +420,94 @@ void Keys::parse(FILE *fp) } fclose(fp); } + +void Keys::genAboutKeys(void) +{ + int len = dList_length(bindings); + Dstr *table = dStr_new(""); + + dStr_sprintfa(table, + "\n" + "\n" + "\n" + " Keyboard shortcuts\n" + " \n" + "\n" + "\n" + "
\n" + "\n" + "

Keyboard shortcuts

\n" + "\n" + "

The following table contains the current key bindings in Dillo.\n" + "To change them, edit the configuration file ~/.dillo/keysrc \n" + "and restart the browser.

\n" + "\n" + "\n"); + + for (int i = 0; i < len; i++) { + KeyBinding_t *node = (KeyBinding_t*)dList_nth_data(bindings, i); + const char *key = Keys::getKeyName(node->key); + + dStr_sprintfa(table, "", key); + dStr_sprintfa(table, "", node->name); + dStr_sprintfa(table, "\n"); + } + + dStr_sprintfa(table, + "
ShortcutAction
"); + + for (uint_t j = 0; j < sizeof(modifierNames) / sizeof(modifierNames[0]); j++) { + if (modifierNames[j].value & node->modifier) { + dStr_sprintfa(table, "%s ", + modifierNames[j].name); + } + } + + dStr_sprintfa(table, "%s%s
\n" + "
\n" + "\n" + "\n"); + + /* inject keymaps after loading them */ + DilloUrl *url = a_Url_new("about:keys", NULL); + a_Cache_entry_inject(url, table); + dStr_free(table, 1); + a_Url_free(url); +} -- cgit v1.2.3