logo

badwolf

Minimalist and privacy-oriented WebKitGTK+ browser
commit: c5c20e8f0d45a13503826062a0db508ad5eebf2d
parent: f2fd43fd7335ceb6acdfd3f7483be616ae611406
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 18 Jul 2019 10:52:11 +0200

Merge branch 'chores/files-separation' into develop

Diffstat:

MMakefile15+++++++++++----
Mbadwolf.c218+++++--------------------------------------------------------------------------
Abadwolf.h51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mconfig.h4++++
Akeybindings.c152+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Akeybindings.h12++++++++++++
6 files changed, 242 insertions(+), 210 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 @@ -2,61 +2,29 @@ // Copyright © 2019 Haelwenn (lanodan) Monnier <contact@hacktivis.me> // SPDX-License-Identifier: BSD-3-Clause +#include "badwolf.h" + #include "config.h" +#include "keybindings.h" #include <glib/gi18n.h> /* _() and other internationalization/localization helpers */ #include <glib/gprintf.h> /* g_fprintf() */ -#include <gtk/gtk.h> -#include <locale.h> /* LC_* */ -#include <stdlib.h> /* realpath(), malloc() */ -#include <webkit2/webkit2.h> +#include <locale.h> /* LC_* */ +#include <stdlib.h> /* realpath(), malloc() */ const gchar *homepage = "https://hacktivis.me/projects/badwolf"; const gchar *version = "0.3.0"; -struct Window -{ - GtkWidget *main_window; - GtkWidget *notebook; - GtkWidget *new_tab; -}; - -struct Client -{ - GtkWidget *box; - - GtkWidget *toolbar; - GtkWidget *javascript; - GtkWidget *location; - - WebKitWebView *webView; - struct Window *window; - - GtkWidget *statusbar; - GtkWidget *statuslabel; - GtkWidget *search; -}; - -gchar *ensure_uri_scheme(gchar *text, gboolean try_file); 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); static gboolean WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); -GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser); static gboolean WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); static gboolean WebViewCb_notify__is__playing__audio(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); -void webView_tab_label_change(struct Client *browser, const gchar *title); static gboolean WebViewCb_notify__estimated_load_progress(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); @@ -86,28 +54,13 @@ static gboolean SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_ static gboolean SearchEntryCb_previous__match(GtkSearchEntry *search, gpointer user_data); static gboolean SearchEntryCb_search__changed(GtkSearchEntry *search, gpointer user_data); static gboolean SearchEntryCb_stop__search(GtkSearchEntry *search, gpointer user_data); -struct Client * -new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view); -int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser); static void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data); static void closeCb_clicked(GtkButton *close, gpointer user_data); static void notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data); -gint get_tab_position(GtkContainer *notebook, GtkWidget *child); -/* ensure_uri_scheme: tries to add a scheme on a pseudo-URL missing a scheme - * - gchar text: pseudo-URL missing a scheme - * - gboolean try_file: when TRUE check try first if it can be a file:// path - * - * When `text` isn't exploitable (ie. NULL), returns "about:blank", - * when the URL seems to be valid, return it, - * if try_file is TRUE, check if it can be file:// path, - * some other checks might be added. - * In the end use the fallback (`http://` for now, might get configuration), - * might get some safeguard. - */ gchar * -ensure_uri_scheme(gchar *text, gboolean try_file) +badwolf_ensure_uri_scheme(gchar *text, gboolean try_file) { if(text == NULL) return "about:blank"; @@ -132,23 +85,6 @@ 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) { @@ -156,7 +92,8 @@ WebViewCb_close(WebKitWebView *webView, gpointer user_data) struct Client *browser = (struct Client *)user_data; GtkNotebook *notebook = GTK_NOTEBOOK(browser->window->notebook); - gtk_notebook_remove_page(notebook, get_tab_position(GTK_CONTAINER(notebook), browser->box)); + gtk_notebook_remove_page(notebook, + badwolf_get_tab_position(GTK_CONTAINER(notebook), browser->box)); gtk_widget_destroy(browser->box); @@ -165,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, @@ -420,7 +226,7 @@ webView_tab_label_change(struct Client *browser, const gchar *title) gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(notebook), browser->box, title); // Set the window title if the title change was on the current tab - if(get_tab_position(GTK_CONTAINER(notebook), browser->box) == + if(badwolf_get_tab_position(GTK_CONTAINER(notebook), browser->box) == gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook))) gtk_window_set_title(GTK_WINDOW(browser->window->main_window), title); } @@ -600,7 +406,7 @@ locationCb_activate(GtkEntry *location, gpointer user_data) char *target_url; struct Client *browser = (struct Client *)user_data; - target_url = ensure_uri_scheme((char *)gtk_entry_get_text(location), TRUE); + target_url = badwolf_ensure_uri_scheme((char *)gtk_entry_get_text(location), TRUE); if(target_url != NULL) webkit_web_view_load_uri(browser->webView, target_url); @@ -674,7 +480,7 @@ struct Client * new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view) { struct Client *browser = malloc(sizeof(struct Client)); - target_url = ensure_uri_scheme(target_url, (related_web_view == NULL)); + target_url = badwolf_ensure_uri_scheme(target_url, (related_web_view == NULL)); char *badwolf_l10n = NULL; if(browser == NULL) return NULL; @@ -880,7 +686,7 @@ notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num, } gint -get_tab_position(GtkContainer *notebook, GtkWidget *child) +badwolf_get_tab_position(GtkContainer *notebook, GtkWidget *child) { GValue position = G_VALUE_INIT; g_value_init(&position, G_TYPE_INT); diff --git a/badwolf.h b/badwolf.h @@ -0,0 +1,51 @@ +#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; + GtkWidget *notebook; + GtkWidget *new_tab; +}; + +struct Client +{ + GtkWidget *box; + + GtkWidget *toolbar; + GtkWidget *javascript; + GtkWidget *location; + + WebKitWebView *webView; + struct Window *window; + + GtkWidget *statusbar; + GtkWidget *statuslabel; + GtkWidget *search; +}; + +/* badwolf_ensure_uri_scheme: tries to add a scheme on a pseudo-URL missing a scheme + * - gchar text: pseudo-URL missing a scheme + * - gboolean try_file: when TRUE check try first if it can be a file:// path + * + * When `text` isn't exploitable (ie. NULL), returns "about:blank", + * when the URL seems to be valid, return it, + * if try_file is TRUE, check if it can be file:// path, + * some other checks might be added. + * In the end use the fallback (`http://` for now, might get configuration), + * might get some safeguard. + */ +gchar *badwolf_ensure_uri_scheme(gchar *text, gboolean try_file); +GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser); +void webView_tab_label_change(struct Client *browser, const gchar *title); +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 */