~sircmpwn/visurf-devel

nsvi: middle-click to close tab v2 APPLIED

Pranjal Kole: 1
 nsvi: middle-click to close tab

 7 files changed, 87 insertions(+), 41 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/27443/mbox | git am -3
Learn more about email & git

[PATCH v2] nsvi: middle-click to close tab Export this patch

The nsvi_undo code from main.c, window.c, commands.c, and visurf.h was
refactored into undo.c and undo.h

An nsvi_undo_tab or nsvi_undo_window function was not created, since it
is only required by cmd_undo in commands.c. It may be created if
required in the future

Two functions, nsvi_undo_tab_new and nsvi_undo_window_new were created,
instead of a single function, nsvi_undo_tab. This is so that commands.c
and others can NSLOG whether the nsvi_undo_kind was UNDO_TAB or
UNDO_WIN.

Implements: https://todo.sr.ht/~sircmpwn/visurf/72
---
 frontends/visurf/Makefile   |  3 ++-
 frontends/visurf/commands.c | 23 +++++++----------------
 frontends/visurf/main.c     | 13 ++-----------
 frontends/visurf/undo.c     | 32 ++++++++++++++++++++++++++++++++
 frontends/visurf/undo.h     | 26 ++++++++++++++++++++++++++
 frontends/visurf/visurf.h   | 12 ------------
 frontends/visurf/window.c   | 19 ++++++++++++++++++-
 7 files changed, 87 insertions(+), 41 deletions(-)
 create mode 100644 frontends/visurf/undo.c
 create mode 100644 frontends/visurf/undo.h

diff --git a/frontends/visurf/Makefile b/frontends/visurf/Makefile
index ce8ff3678..810af1f2d 100644
--- a/frontends/visurf/Makefile
+++ b/frontends/visurf/Makefile
@@ -47,7 +47,8 @@ $(OBJROOT)/xdg-decoration.h: $(XDG_DECORATION_PROTOCOL)
S_WAYLAND := $(OBJROOT)/xdg-shell.c $(OBJROOT)/xdg-decoration.c

S_FRONTEND := main.c bitmap.c commands.c fetch.c getopt.c keybindings.c \
		layout.c pool-buffer.c plotters.c window.c settings.c
		layout.c pool-buffer.c plotters.c window.c settings.c \
		undo.c

SOURCES = $(S_COMMON) $(S_IMAGE) $(S_BROWSER) $(S_FRONTEND) $(S_WAYLAND)

diff --git a/frontends/visurf/commands.c b/frontends/visurf/commands.c
index d80003107..7dbe6b2d3 100644
--- a/frontends/visurf/commands.c
+++ b/frontends/visurf/commands.c
@@ -23,6 +23,7 @@
#include "visurf/commands.h"
#include "visurf/getopt.h"
#include "visurf/settings.h"
#include "visurf/undo.h"
#include "visurf/visurf.h"
#include "visurf/window.h"
#include "utils/log.h"
@@ -92,19 +93,14 @@ cmd_close(struct nsvi_state *state, int argc, char *argv[])
		NSLOG(netsurf, ERROR, "close: no active window");
		return 1;
	}

	nsurl *url;
	struct gui_window *gw = win->tabs[win->tab];
	nserror err = browser_window_get_url(gw->bw, true, &url);
	if (err == NSERROR_OK) {
		struct nsvi_undo *undo = calloc(1, sizeof(struct nsvi_undo));
		undo->kind = UNDO_TAB;
		undo->urls = calloc(1, sizeof(nsurl *));
		undo->nurl = 1;
		undo->urls[0] = url;
		nsurl_ref(url);
		undo->next = state->undo;
		state->undo = undo;

	if (browser_window_get_url(gw->bw, true, &url) == NSERROR_OK) {
		nsvi_undo_tab_new(url);
	}

	browser_window_destroy(gw->bw);
	return 0;
}
@@ -998,12 +994,7 @@ cmd_undo(struct nsvi_state *state, int argc, char *argv[])
		assert(0); // TODO
	}

	for (size_t i = 0; i < undo->nurl; ++i) {
		nsurl_unref(undo->urls[i]);
	}
	free(undo->urls);
	state->undo = undo->next;
	free(undo);
	nsvi_undo_free(undo);
	return 0;
}

diff --git a/frontends/visurf/main.c b/frontends/visurf/main.c
index 576e1cc9a..ae986a066 100644
--- a/frontends/visurf/main.c
+++ b/frontends/visurf/main.c
@@ -40,6 +40,7 @@
#include "visurf/plotters.h"
#include "visurf/settings.h"
#include "visurf/timespec-util.h"
#include "visurf/undo.h"
#include "visurf/visurf.h"
#include "visurf/window.h"
#include "xdg-shell.h"
@@ -1425,17 +1426,7 @@ main(int argc, char *argv[]) {
		kill(state.filter.pid, SIGTERM);
	}

	struct nsvi_undo *undo = state.undo;
	while (undo) {
		for (size_t i = 0; i < undo->nurl; ++i) {
			nsurl_unref(undo->urls[i]);
		}
		free(undo->urls);
		struct nsvi_undo *next = undo->next;
		free(undo);
		undo = next;
	}

	nsvi_undo_finish();
	nsvi_fetch_filetype_fini();
	urldb_save_cookies(nsoption_charp(cookie_jar));
	urldb_save(nsoption_charp(url_file));
diff --git a/frontends/visurf/undo.c b/frontends/visurf/undo.c
new file mode 100644
index 000000000..60eb30177
--- /dev/null
+++ b/frontends/visurf/undo.c
@@ -0,0 +1,32 @@
#include <stdlib.h>
#include "visurf/undo.h"

void nsvi_undo_tab_new(nsurl *url) {
	struct nsvi_undo *undo = calloc(1, sizeof(struct nsvi_undo));
	undo->kind = UNDO_TAB;
	undo->urls = calloc(1, sizeof(nsurl *));
	undo->nurl = 1;
	undo->urls[0] = url;
	undo->next = global_state->undo;
	global_state->undo = undo;
}

void nsvi_undo_window_new(void) {
	assert(0); // TODO
}

void nsvi_undo_free(struct nsvi_undo *undo) {
	for (size_t i = 0; i < undo->nurl; ++i) {
		nsurl_unref(undo->urls[i]);
	}

	free(undo->urls);
	global_state->undo = undo->next;
	free(undo);
}

void nsvi_undo_finish(void) {
	while (global_state->undo) {
		nsvi_undo_free(global_state->undo);
	}
}
diff --git a/frontends/visurf/undo.h b/frontends/visurf/undo.h
new file mode 100644
index 000000000..e4e17ffe7
--- /dev/null
+++ b/frontends/visurf/undo.h
@@ -0,0 +1,26 @@
#ifndef NETSURF_VI_UNDO_H
#define NETSURF_VI_UNDO_H

#include "visurf/visurf.h"

enum nsvi_undo_kind {
        UNDO_TAB,
        UNDO_WIN,
};

struct nsvi_undo {
        enum nsvi_undo_kind kind;
        nsurl **urls;
        size_t nurl;
        struct nsvi_undo *next;
};

void nsvi_undo_tab_new(nsurl *url);

void nsvi_undo_window_new(void);

void nsvi_undo_free(struct nsvi_undo *undo);

void nsvi_undo_finish(void);

#endif
diff --git a/frontends/visurf/visurf.h b/frontends/visurf/visurf.h
index 921cb5cb3..55aa5f327 100644
--- a/frontends/visurf/visurf.h
+++ b/frontends/visurf/visurf.h
@@ -62,18 +62,6 @@ struct touch_event {
	struct touch_point points[10];
};

enum nsvi_undo_kind {
	UNDO_TAB,
	UNDO_WIN,
};

struct nsvi_undo {
	enum nsvi_undo_kind kind;
	nsurl **urls;
	size_t nurl;
	struct nsvi_undo *next;
};

struct nsvi_filter {
	FILE *in;
	FILE *out;
diff --git a/frontends/visurf/window.c b/frontends/visurf/window.c
index 460b266fb..94d420f36 100644
--- a/frontends/visurf/window.c
+++ b/frontends/visurf/window.c
@@ -22,6 +22,7 @@
#include "visurf/plotters.h"
#include "visurf/pool-buffer.h"
#include "visurf/settings.h"
#include "visurf/undo.h"
#include "visurf/visurf.h"
#include "visurf/window.h"
#include "xdg-shell.h"
@@ -1627,7 +1628,17 @@ nsvi_window_pointer_button_press_event(struct nsvi_window *win,
		// TODO: This could be more sophisticated
		// grep for 'XXX: tabwidth' for related code
		int tabwidth = win->width / win->ntab;
		win->tab = win->mouse.pressed_x / tabwidth;
		int tab = win->mouse.pressed_x / tabwidth;
		if (event->button == BTN_MIDDLE) {
			nsurl *url;
			if (browser_window_get_url(win->tabs[tab]->bw,
			true, &url) == NSERROR_OK) {
				nsvi_undo_tab_new(url);
			}
			browser_window_destroy(win->tabs[tab]->bw);
			return;
		}
		win->tab = tab;
		request_frame(win);
	}

@@ -1709,6 +1720,12 @@ nsvi_window_pointer_event(struct nsvi_window *win, struct pointer_event *event)
			nsvi_window_pointer_button_release_event(win, event);
		}
	}

	// Exit early if gw->bw has been browser_window_destroy()-ed
	if (gw != win->tabs[win->tab]) {
		return;
	}

	if (event->event_mask & POINTER_EVENT_ENTER
			|| event->event_mask & POINTER_EVENT_MOTION) {
		nsvi_window_pointer_motion_event(win, event);
-- 
2.34.1
Thanks!

To git@git.sr.ht:~sircmpwn/visurf
   93c207a51..06af14be9  master -> master