logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://hacktivis.me/git/badwolf.git
commit: 4d30c11f4c7ff59805c770ca972a24088c9f2604
parent 2216742794e48bda3898813acbb9e1d68d38b241
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  6 Jul 2019 15:53:06 +0200

Move keybindings to a separate file

Diffstat:

MMakefile15+++++++++++----
Mbadwolf.c156+------------------------------------------------------------------------------
Mbadwolf.h7+++++++
Mconfig.h4++++
Akeybindings.c152+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Akeybindings.h12++++++++++++
6 files changed, 187 insertions(+), 159 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,7 +7,8 @@ DATADIR = $(PREFIX)/share/badwolf PACKAGE = Badwolf DEPS = gtk+-3.0 webkit2gtk-4.0 -OBJS = badwolf +OBJS = keybindings.o +EXE = badwolf TRANS = fr.mo CC = cc @@ -15,7 +16,7 @@ CFLAGS = -g -Wall -Wextra -Wconversion -Wsign-conversion CDEPS = `pkg-config --cflags $(DEPS)` -DDATADIR=\"$(DATADIR)\" -DPACKAGE=\"$(PACKAGE)\" -D_DEFAULT_SOURCE LIBS = `pkg-config --libs $(DEPS)` -all: $(OBJS) $(TRANS) +all: $(EXE) $(TRANS) po/messages.pot: badwolf.c xgettext --keyword=_ --language=C -o $@ --add-comments --sort-output -j badwolf.c @@ -27,8 +28,14 @@ ${TRANS}: po/${@:.mo=.po} mkdir -p locale/${@:.mo=}/LC_MESSAGES msgfmt -o locale/${@:.mo=}/LC_MESSAGES/$(PACKAGE).mo po/${@:.mo=.po} +${EXE}: $(OBJS) + $(CC) -std=c11 $(CFLAGS) $(CDEPS) $(LDFLAGS) $(LIBS) -o $@ $(OBJS) $@.c + .c: - $(CC) -std=c11 $(CFLAGS) $(CDEPS) -o $@ $< $(LDFLAGS) $(LIBS) + $(CC) -std=c11 $(CFLAGS) $(CDEPS) $(LDFLAGS) $(LIBS) -o $@ $< + +.c.o: + $(CC) -std=c11 $(CFLAGS) $(CDEPS) $(LDFLAGS) $(LIBS) -c -o $@ $< install: all mkdir -p $(DESTDIR)$(BINDIR) @@ -39,7 +46,7 @@ install: all cp -r locale/ $(DESTDIR)$(DATADIR) clean: - rm -fr locale $(OBJS) + rm -fr locale $(OBJS) $(EXE) format: *.c *.h clang-format -style=file -assume-filename=.clang-format -i *.c *.h diff --git a/badwolf.c b/badwolf.c @@ -5,6 +5,7 @@ #include "badwolf.h" #include "config.h" +#include "keybindings.h" #include <glib/gi18n.h> /* _() and other internationalization/localization helpers */ #include <glib/gprintf.h> /* g_fprintf() */ @@ -15,13 +16,6 @@ const gchar *homepage = "https://hacktivis.me/projects/badwolf"; const gchar *version = "0.3.0"; static gboolean WebViewCb_close(WebKitWebView *webView, gpointer user_data); -static gboolean -commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser); -static gboolean -WebViewCb_key_press_event(WebKitWebView *webView, GdkEvent *event, gpointer user_data); -static gboolean -main_windowCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data); -static gboolean boxCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data); static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, WebKitWebProcessTerminationReason reason, gpointer user_data); @@ -91,23 +85,6 @@ badwolf_ensure_uri_scheme(gchar *text, gboolean try_file) return g_strdup_printf("http://%s", text); } -static void -badwolf_about_dialog(GtkWindow *main_window) -{ - // clang-format off - gtk_show_about_dialog( - main_window, - "license", "SPDX-License-Identifier: BSD-3-Clause", - "copyright", "2019 Haelwenn (lanodan) Monnier <contact+badwolf@hacktivis.me>", - "website", homepage, - "comments", "Minimalist and privacy-oriented WebKitGTK+ browser", - "version", version, - //FIXME: "logo-icon-name", g_get_application_name(), - NULL - ); - // clang-format on -} - static gboolean WebViewCb_close(WebKitWebView *webView, gpointer user_data) { @@ -125,137 +102,6 @@ WebViewCb_close(WebKitWebView *webView, gpointer user_data) return TRUE; } -static void -toggle_caret_browsing(WebKitWebView *webView) -{ - WebKitSettings *settings = webkit_web_view_get_settings(webView); - - webkit_settings_set_enable_caret_browsing(settings, - !webkit_settings_get_enable_caret_browsing(settings)); - - webkit_web_view_set_settings(webView, settings); -} - -/* commonCb_key_press_event: Global callback for keybindings - * - * Theses shortcuts should be avoided as much as possible: - * - Single key shortcuts (ie. backspace and space) - * - Triple key shortcuts (except for Ctrl+Shift) - * - Unix Terminal shortcuts (specially Ctrl-W) - * - * loosely follows https://developer.gnome.org/hig/stable/keyboard-input.html - */ -static gboolean -commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser) -{ - GtkNotebook *notebook = GTK_NOTEBOOK(window->notebook); - - if(((GdkEventKey *)event)->state & GDK_CONTROL_MASK) - { - if(browser != NULL) - { - switch(((GdkEventKey *)event)->keyval) - { - case GDK_KEY_F4: webkit_web_view_try_close(browser->webView); return TRUE; - case GDK_KEY_r: - if(((GdkEventKey *)event)->state & GDK_SHIFT_MASK) - webkit_web_view_reload_bypass_cache(browser->webView); - else - webkit_web_view_reload(browser->webView); - - return TRUE; - case GDK_KEY_f: gtk_widget_grab_focus(browser->search); return TRUE; - case GDK_KEY_l: gtk_widget_grab_focus(browser->location); return TRUE; - case GDK_KEY_bracketleft: webkit_web_view_go_back(browser->webView); return TRUE; - case GDK_KEY_bracketright: webkit_web_view_go_forward(browser->webView); return TRUE; - case GDK_KEY_0: - webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(browser->webView), 1); - return TRUE; - } - } - else - { - switch(((GdkEventKey *)event)->keyval) - { - case GDK_KEY_Page_Down: gtk_notebook_next_page(notebook); return TRUE; - case GDK_KEY_Page_Up: gtk_notebook_prev_page(notebook); return TRUE; - case GDK_KEY_t: badwolf_new_tab(notebook, new_browser(window, NULL, NULL)); return TRUE; - } - } - } - - if((((GdkEventKey *)event)->state & GDK_MOD1_MASK)) - { - if((browser != NULL) && (((GdkEventKey *)event)->keyval == GDK_KEY_d)) - { - webkit_web_view_try_close(browser->webView); - return TRUE; - } - - switch(((GdkEventKey *)event)->keyval) - { - case GDK_KEY_Left: gtk_notebook_prev_page(notebook); return TRUE; - case GDK_KEY_Right: gtk_notebook_next_page(notebook); return TRUE; - } - if((((GdkEventKey *)event)->keyval >= GDK_KEY_0) && - (((GdkEventKey *)event)->keyval <= GDK_KEY_9)) - gtk_notebook_set_current_page(notebook, (gint)(((GdkEventKey *)event)->keyval - GDK_KEY_1)); - } - - if(browser != NULL) - { - switch(((GdkEventKey *)event)->keyval) - { - case GDK_KEY_F5: webkit_web_view_reload(browser->webView); return TRUE; - case GDK_KEY_F7: toggle_caret_browsing(browser->webView); return TRUE; - case GDK_KEY_F12: - webkit_web_inspector_show(webkit_web_view_get_inspector(browser->webView)); - return TRUE; - } - } - else - { - switch(((GdkEventKey *)event)->keyval) - { - case GDK_KEY_F1: badwolf_about_dialog(GTK_WINDOW(window->main_window)); return TRUE; - } - } - - return FALSE; -} - -static gboolean -WebViewCb_key_press_event(WebKitWebView *webView, GdkEvent *event, gpointer user_data) -{ - (void)webView; - struct Client *browser = (struct Client *)user_data; - - if(commonCb_key_press_event(browser->window, event, browser)) return TRUE; - - return FALSE; -} - -static gboolean -boxCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) -{ - (void)widget; - struct Client *browser = (struct Client *)user_data; - - if(commonCb_key_press_event(browser->window, event, browser)) return TRUE; - - return FALSE; -} - -static gboolean -main_windowCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) -{ - (void)widget; - struct Window *window = (struct Window *)user_data; - - if(commonCb_key_press_event(window, event, NULL)) return TRUE; - - return FALSE; -} static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, WebKitWebProcessTerminationReason reason, diff --git a/badwolf.h b/badwolf.h @@ -1,6 +1,12 @@ +#ifndef BADWOLF_H_INCLUDED +#define BADWOLF_H_INCLUDED + #include <gtk/gtk.h> #include <webkit2/webkit2.h> +const gchar *homepage; +const gchar *version; + struct Window { GtkWidget *main_window; @@ -42,3 +48,4 @@ struct Client * new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view); int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser); gint badwolf_get_tab_position(GtkContainer *notebook, GtkWidget *child); +#endif /* BADWOLF_H_INCLUDED */ diff --git a/config.h b/config.h @@ -1,3 +1,5 @@ +#ifndef CONFIG_H_INCLUDED +#define CONFIG_H_INCLUDED /* BADWOLF_TAB_POSITION: Position of the tab listing, can be one of: * - GTK_POS_TOP * - GTK_POS_BOTTOM @@ -79,3 +81,5 @@ * See https://developer.gnome.org/pango/stable/pango-Layout-Objects.html#PangoEllipsizeMode */ #define BADWOLF_STATUSLABEL_ELLIPSIZE PANGO_ELLIPSIZE_MIDDLE + +#endif /* CONFIG_H_INCLUDED */ diff --git a/keybindings.c b/keybindings.c @@ -0,0 +1,152 @@ +#include "keybindings.h" + +#include "badwolf.h" + +static void +badwolf_about_dialog(GtkWindow *main_window) +{ + // clang-format off + gtk_show_about_dialog( + main_window, + "license", "SPDX-License-Identifier: BSD-3-Clause", + "copyright", "2019 Haelwenn (lanodan) Monnier <contact+badwolf@hacktivis.me>", + "website", homepage, + "comments", "Minimalist and privacy-oriented WebKitGTK+ browser", + "version", version, + //FIXME: "logo-icon-name", g_get_application_name(), + NULL + ); + // clang-format on +} + +static void +toggle_caret_browsing(WebKitWebView *webView) +{ + WebKitSettings *settings = webkit_web_view_get_settings(webView); + + webkit_settings_set_enable_caret_browsing(settings, + !webkit_settings_get_enable_caret_browsing(settings)); + + webkit_web_view_set_settings(webView, settings); +} + +/* commonCb_key_press_event: Global callback for keybindings + * + * Theses shortcuts should be avoided as much as possible: + * - Single key shortcuts (ie. backspace and space) + * - Triple key shortcuts (except for Ctrl+Shift) + * - Unix Terminal shortcuts (specially Ctrl-W) + * + * loosely follows https://developer.gnome.org/hig/stable/keyboard-input.html + */ +gboolean +commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser) +{ + GtkNotebook *notebook = GTK_NOTEBOOK(window->notebook); + + if(((GdkEventKey *)event)->state & GDK_CONTROL_MASK) + { + if(browser != NULL) + { + switch(((GdkEventKey *)event)->keyval) + { + case GDK_KEY_F4: webkit_web_view_try_close(browser->webView); return TRUE; + case GDK_KEY_r: + if(((GdkEventKey *)event)->state & GDK_SHIFT_MASK) + webkit_web_view_reload_bypass_cache(browser->webView); + else + webkit_web_view_reload(browser->webView); + + return TRUE; + case GDK_KEY_f: gtk_widget_grab_focus(browser->search); return TRUE; + case GDK_KEY_l: gtk_widget_grab_focus(browser->location); return TRUE; + case GDK_KEY_bracketleft: webkit_web_view_go_back(browser->webView); return TRUE; + case GDK_KEY_bracketright: webkit_web_view_go_forward(browser->webView); return TRUE; + case GDK_KEY_0: + webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(browser->webView), 1); + return TRUE; + } + } + else + { + switch(((GdkEventKey *)event)->keyval) + { + case GDK_KEY_Page_Down: gtk_notebook_next_page(notebook); return TRUE; + case GDK_KEY_Page_Up: gtk_notebook_prev_page(notebook); return TRUE; + case GDK_KEY_t: badwolf_new_tab(notebook, new_browser(window, NULL, NULL)); return TRUE; + } + } + } + + if((((GdkEventKey *)event)->state & GDK_MOD1_MASK)) + { + if((browser != NULL) && (((GdkEventKey *)event)->keyval == GDK_KEY_d)) + { + webkit_web_view_try_close(browser->webView); + return TRUE; + } + + switch(((GdkEventKey *)event)->keyval) + { + case GDK_KEY_Left: gtk_notebook_prev_page(notebook); return TRUE; + case GDK_KEY_Right: gtk_notebook_next_page(notebook); return TRUE; + } + if((((GdkEventKey *)event)->keyval >= GDK_KEY_0) && + (((GdkEventKey *)event)->keyval <= GDK_KEY_9)) + gtk_notebook_set_current_page(notebook, (gint)(((GdkEventKey *)event)->keyval - GDK_KEY_1)); + } + + if(browser != NULL) + { + switch(((GdkEventKey *)event)->keyval) + { + case GDK_KEY_F5: webkit_web_view_reload(browser->webView); return TRUE; + case GDK_KEY_F7: toggle_caret_browsing(browser->webView); return TRUE; + case GDK_KEY_F12: + webkit_web_inspector_show(webkit_web_view_get_inspector(browser->webView)); + return TRUE; + } + } + else + { + switch(((GdkEventKey *)event)->keyval) + { + case GDK_KEY_F1: badwolf_about_dialog(GTK_WINDOW(window->main_window)); return TRUE; + } + } + + return FALSE; +} + +gboolean +WebViewCb_key_press_event(WebKitWebView *webView, GdkEvent *event, gpointer user_data) +{ + (void)webView; + struct Client *browser = (struct Client *)user_data; + + if(commonCb_key_press_event(browser->window, event, browser)) return TRUE; + + return FALSE; +} + +gboolean +boxCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) +{ + (void)widget; + struct Client *browser = (struct Client *)user_data; + + if(commonCb_key_press_event(browser->window, event, browser)) return TRUE; + + return FALSE; +} + +gboolean +main_windowCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data) +{ + (void)widget; + struct Window *window = (struct Window *)user_data; + + if(commonCb_key_press_event(window, event, NULL)) return TRUE; + + return FALSE; +} diff --git a/keybindings.h b/keybindings.h @@ -0,0 +1,12 @@ +#ifndef KEYBINDINGS_H_INCLUDED +#define KEYBINDINGS_H_INCLUDED +#include "badwolf.h" + +#include <gtk/gtk.h> +#include <webkit2/webkit2.h> + +gboolean boxCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data); +gboolean commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser); +gboolean main_windowCb_key_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data); +gboolean WebViewCb_key_press_event(WebKitWebView *webView, GdkEvent *event, gpointer user_data); +#endif /* KEYBINDINGS_H_INCLUDED */