~sircmpwn/visurf-devel

nsvi: about:bindings fetcher to show key bindings v1 APPLIED

Steef Hegeman: 1
 nsvi: about:bindings fetcher to show key bindings

 4 files changed, 180 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/visurf-devel/patches/29459/mbox | git am -3
Learn more about email & git

[PATCH] nsvi: about:bindings fetcher to show key bindings Export this patch

about:bindings now shows a table with all currently bound key bindings.
(Updates on refresh.)

It submits the rows one by one instead of buffering them like e.g.
about:config, but it seems fast enough (0.0s or 0.1s on default config).
---
Hello,

One thing I liked about dwb was being able to see the key bindings from
the browser. I tried to add it here.

It does not seem possible for a frontend to add fetchers, so I did it
this way. It also seems involved to add new schemes, so I settled for
about:bindings for now. about:visurf/bindings would also be possible.

I'm not sure whether I handled the copyright headers correctly. I edited
copies of config.c/config.h so I kept the notice and added my name.

Kind regards,
Steef

 content/fetchers/about/Makefile   |   1 +
 content/fetchers/about/about.c    |   8 ++
 content/fetchers/about/bindings.c | 135 ++++++++++++++++++++++++++++++
 content/fetchers/about/bindings.h |  36 ++++++++
 4 files changed, 180 insertions(+)
 create mode 100644 content/fetchers/about/bindings.c
 create mode 100644 content/fetchers/about/bindings.h

diff --git a/content/fetchers/about/Makefile b/content/fetchers/about/Makefile
index 4f12a0626..0a1cedf3e 100644
--- a/content/fetchers/about/Makefile
+++ b/content/fetchers/about/Makefile
@@ -2,6 +2,7 @@

S_FETCHER_ABOUT := \
	about.c \
	bindings.c \
	blank.c \
	certificate.c \
	chart.c \
diff --git a/content/fetchers/about/about.c b/content/fetchers/about/about.c
index 651894249..acca3c6d8 100644
--- a/content/fetchers/about/about.c
+++ b/content/fetchers/about/about.c
@@ -46,6 +46,7 @@

#include "private.h"
#include "about.h"
#include "bindings.h"
#include "blank.h"
#include "certificate.h"
#include "config.h"
@@ -404,6 +405,13 @@ struct about_handlers about_handler_list[] = {
		fetch_about_about_handler,
		true
	},
	{
		"bindings",
		SLEN("bindings"),
		NULL,
		fetch_about_bindings_handler,
		false
	},
	{
		"nscolours.css",
		SLEN("nscolours.css"),
diff --git a/content/fetchers/about/bindings.c b/content/fetchers/about/bindings.c
new file mode 100644
index 000000000..9f27d6a2a
--- /dev/null
+++ b/content/fetchers/about/bindings.c
@@ -0,0 +1,135 @@
/*
 * Copyright 2020 Vincent Sanders <vince@netsurf-browser.org>
 * Copyright 2022 Steef Hegeman <mail@steefhegeman.com>
 *
 * This file is part of NetSurf.
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * content generator for the about scheme bindings page
 */

#include <stdbool.h>
#include <stdio.h>
#include <xkbcommon/xkbcommon.h>

#include "utils/errors.h"
#include "utils/utils.h"
#include "visurf/visurf.h"

#include "private.h"
#include "bindings.h"

static int snprint_binding(char *buf, size_t size, struct nsvi_binding *binding)
{
	size_t len = 0;
	int klen;
	char tmp[64];
	do {
		klen = xkb_keysym_get_name(binding->keysym, tmp, sizeof tmp);
		if (klen <= 0) {
			klen = snprintf(tmp, sizeof tmp, "<i>invalid</i>");
		}

		if (binding->mask) {
			klen++;
		}

		if (klen == 1) {
			len += snprintf(buf + len, size - len, tmp);
		} else {
			len += snprintf(buf + len, size - len, "&lt;");
			// TODO: Other modifiers?
			if (binding->mask & MOD_CTRL) {
				len += snprintf(buf + len, size - len, "C-");
			}
			len += snprintf(buf + len, size - len, "%s&gt;", tmp);
		}
	} while ((binding = binding->next));
	return len;
}

/**
 * Handler to generate about scheme bindings page
 *
 * \param ctx The fetcher context.
 * \return true if handled false if aborted.
 */
bool fetch_about_bindings_handler(struct fetch_about_context *ctx)
{
	char buf[256];
	nserror res;
	bool even = false;
	const char * const ODD_EVEN[] = {"odd", "even"};
	struct nsvi_bindings *bindings;

	if (global_state == NULL) {
		goto fetch_about_bindings_handler_aborted;
	}
	bindings = &global_state->bindings;

	fetch_about_set_http_code(ctx, 200);
	if (fetch_about_send_header(ctx, "Content-Type: text/html")) {
		goto fetch_about_bindings_handler_aborted;
	}

	res = fetch_about_ssenddataf(ctx,
			"<html>\n<head>\n"
			"<title>ViSurf Key Bindings</title>\n"
			"<link rel=\"stylesheet\" type=\"text/css\" "
			"href=\"resource:internal.css\">\n"
			"</head>\n"
			"<body "
				"id =\"configlist\" "
				"class=\"ns-even-bg ns-even-fg ns-border\" "
				"style=\"overflow: hidden;\">\n"
			"<h1 class=\"ns-border\">ViSurf Key Bindings</h1>\n"
			"<table class=\"config\">\n"
			"<tr><th>Key(s)</th>"
			"<th>Command</th></tr>\n");
	if (res != NSERROR_OK) {
		goto fetch_about_bindings_handler_aborted;
	}

	for (size_t i = 0; i < bindings->nbindings; ++i, even = !even) {
		struct nsvi_binding *binding = &bindings->bindings[i];
		// TODO: Gently handle length > sizeof buf (realloc?)
		if (snprint_binding(buf, sizeof buf, binding) < 0) {
			goto fetch_about_bindings_handler_aborted;
		}

		res = fetch_about_ssenddataf(ctx,
				"<tr class=\"ns-%s-bg\">"
					"<th class=\"ns-border\">%s</th>"
					"<td class=\"ns-border\">%s</td>"
				"</tr>\n",
				ODD_EVEN[even], buf, binding->command);
		if (res != NSERROR_OK) {
			goto fetch_about_bindings_handler_aborted;
		}
	}

	res = fetch_about_ssenddataf(ctx, "</table>\n</body>\n</html>\n");
	if (res != NSERROR_OK) {
		goto fetch_about_bindings_handler_aborted;
	}
	fetch_about_send_finished(ctx);

	return true;

fetch_about_bindings_handler_aborted:
	return false;
}
diff --git a/content/fetchers/about/bindings.h b/content/fetchers/about/bindings.h
new file mode 100644
index 000000000..06147f2d1
--- /dev/null
+++ b/content/fetchers/about/bindings.h
@@ -0,0 +1,36 @@
/*
 * Copyright 2020 Vincent Sanders <vince@netsurf-browser.org>
 * Copyright 2022 Steef Hegeman <mail@steefhegeman.com>
 *
 * This file is part of NetSurf.
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * about scheme bindings handler interface
 */

#ifndef NETSURF_CONTENT_FETCHERS_ABOUT_BINDINGS_H
#define NETSURF_CONTENT_FETCHERS_ABOUT_BINDINGS_H

/**
 * Handler to generate about scheme bindings page.
 *
 * \param ctx The fetcher context.
 * \return true if handled false if aborted.
 */
bool fetch_about_bindings_handler(struct fetch_about_context *ctx);

#endif
-- 
2.35.1
Neat. Thanks!

To git@git.sr.ht:~sircmpwn/visurf
   3518302d7..7de863576  master -> master