logo

badwolf

Minimalist and privacy-oriented WebKitGTK+ browser
commit: 99364d71485e8d2e33d0bf6234c72b79f3a06608
parent: 3743ac4704e0c566716167f4f69d2a35a4a7f8f7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 11 Mar 2019 08:32:14 +0100

clang-format

done on NightmareMoon

Diffstat:

Mbadwolf.c135+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 81 insertions(+), 54 deletions(-)

diff --git a/badwolf.c b/badwolf.c @@ -2,12 +2,12 @@ // Copyright © 2019 Haelwenn (lanodan) Monnier <contact@hacktivis.me> // SPDX-License-Identifier: BSD-3-Clause -#include <gtk/gtk.h> +#include "config.h" + #include <glib/gprintf.h> /* g_fprintf() */ +#include <gtk/gtk.h> #include <webkit2/webkit2.h> -#include "config.h" - struct Window { GtkWidget *main_window; @@ -31,19 +31,30 @@ struct Client }; static gboolean WebViewCb_close(WebKitWebView *webView, GtkWidget *window); -static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, WebKitWebProcessTerminationReason reason); -static gboolean WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); -static gboolean WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); -static gboolean WebViewCb_notify__estimated_load_progress(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); -static gboolean WebViewCb_mouse_target_changed(WebKitWebView *webView, WebKitHitTestResult *hit, guint modifiers, gpointer user_data); -static WebKitWebView *WebViewCb_create(WebKitWebView *webView, WebKitNavigationAction *navigation_action, gpointer user_data); +static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, + WebKitWebProcessTerminationReason reason); +static gboolean +WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); +static gboolean +WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data); +static gboolean WebViewCb_notify__estimated_load_progress(WebKitWebView *webView, + GParamSpec *pspec, + gpointer user_data); +static gboolean WebViewCb_mouse_target_changed(WebKitWebView *webView, + WebKitHitTestResult *hit, + guint modifiers, + gpointer user_data); +static WebKitWebView *WebViewCb_create(WebKitWebView *webView, + WebKitNavigationAction *navigation_action, + gpointer user_data); static gboolean locationCb_activate(GtkEntry *location, gpointer user_data); static gboolean javascriptCb_toggled(GtkButton *javascript, gpointer user_data); static gboolean SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_data); 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); +struct Client * +new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view); void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data); void closeCb_clicked(GtkButton *close, gpointer user_data); int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser); @@ -65,14 +76,11 @@ static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView, switch(reason) { - case WEBKIT_WEB_PROCESS_CRASHED: - str_reason = "the web process crashed."; - break; + case WEBKIT_WEB_PROCESS_CRASHED: str_reason = "the web process crashed."; break; case WEBKIT_WEB_PROCESS_EXCEEDED_MEMORY_LIMIT: str_reason = "the web process exceeded the memory limit."; break; - default: - str_reason = "the web process terminated for an unknown reason."; + default: str_reason = "the web process terminated for an unknown reason."; } g_fprintf(stderr, "BadWolf [signal: web-process-terminated]: %s\n", str_reason); @@ -94,9 +102,10 @@ static gboolean WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, return TRUE; } -GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser) { +GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser) +{ (void)browser; - GtkWidget *label = gtk_label_new(title); + GtkWidget *label = gtk_label_new(title); gtk_label_set_width_chars(GTK_LABEL(label), 25); gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_MIDDLE); @@ -123,7 +132,8 @@ WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user if(title == NULL) title = "BadWolf"; - gtk_notebook_set_tab_label(GTK_NOTEBOOK(browser->window->notebook), browser->box, badwolf_new_tab_box(title, browser)); + gtk_notebook_set_tab_label( + GTK_NOTEBOOK(browser->window->notebook), browser->box, badwolf_new_tab_box(title, browser)); return TRUE; } @@ -162,15 +172,20 @@ static gboolean WebViewCb_mouse_target_changed(WebKitWebView *webView, return TRUE; } -static WebKitWebView *WebViewCb_create(WebKitWebView *related_web_view, WebKitNavigationAction *navigation_action, gpointer user_data) +static WebKitWebView *WebViewCb_create(WebKitWebView *related_web_view, + WebKitNavigationAction *navigation_action, + gpointer user_data) { (void)navigation_action; - struct Window *window = (struct Window *)user_data; + struct Window *window = (struct Window *)user_data; struct Client *browser = new_browser(window, NULL, related_web_view); - if(badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser) < 0) { + if(badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser) < 0) + { return NULL; - } else { + } + else + { return browser->webView; } } @@ -202,11 +217,10 @@ static gboolean javascriptCb_toggled(GtkButton *javascript, gpointer user_data) return TRUE; } - static gboolean SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_data) { (void)search; - struct Client *browser = (struct Client *)user_data; + struct Client *browser = (struct Client *)user_data; WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView); webkit_find_controller_search_next(findController); @@ -217,7 +231,7 @@ static gboolean SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_ static gboolean SearchEntryCb_previous__match(GtkSearchEntry *search, gpointer user_data) { (void)search; - struct Client *browser = (struct Client *)user_data; + struct Client *browser = (struct Client *)user_data; WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView); webkit_find_controller_search_previous(findController); @@ -227,9 +241,9 @@ static gboolean SearchEntryCb_previous__match(GtkSearchEntry *search, gpointer u static gboolean SearchEntryCb_search__changed(GtkSearchEntry *search, gpointer user_data) { - struct Client *browser = (struct Client *)user_data; + struct Client *browser = (struct Client *)user_data; WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView); - const gchar *search_text = gtk_entry_get_text(GTK_ENTRY(search)); + const gchar *search_text = gtk_entry_get_text(GTK_ENTRY(search)); webkit_find_controller_search(findController, search_text, 0, 0); @@ -239,7 +253,7 @@ static gboolean SearchEntryCb_search__changed(GtkSearchEntry *search, gpointer u static gboolean SearchEntryCb_stop__search(GtkSearchEntry *search, gpointer user_data) { (void)search; - struct Client *browser = (struct Client *)user_data; + struct Client *browser = (struct Client *)user_data; WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView); webkit_find_controller_search_finish(findController); @@ -247,28 +261,36 @@ static gboolean SearchEntryCb_stop__search(GtkSearchEntry *search, gpointer user return TRUE; } -struct Client *new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view) +struct Client * +new_browser(struct Window *window, gchar *target_url, WebKitWebView *related_web_view) { struct Client *browser = g_malloc(sizeof(struct Client)); - browser->window = window; - browser->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); - browser->toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - browser->location = gtk_entry_new(); - browser->search = gtk_search_entry_new(); - browser->javascript = gtk_check_button_new(); - browser->close = gtk_button_new_from_icon_name("window-close", GTK_ICON_SIZE_SMALL_TOOLBAR); - browser->statusbar = gtk_label_new(NULL); + browser->window = window; + browser->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + browser->toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + browser->location = gtk_entry_new(); + browser->search = gtk_search_entry_new(); + browser->javascript = gtk_check_button_new(); + browser->close = gtk_button_new_from_icon_name("window-close", GTK_ICON_SIZE_SMALL_TOOLBAR); + browser->statusbar = gtk_label_new(NULL); - if(related_web_view == NULL) { + if(related_web_view == NULL) + { WebKitWebContext *web_context = webkit_web_context_new_ephemeral(); - webkit_web_context_set_process_model(web_context, WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); + webkit_web_context_set_process_model(web_context, + WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); browser->webView = WEBKIT_WEB_VIEW(webkit_web_view_new_with_context(web_context)); - if(target_url == NULL) { target_url = "about:blank"; } - } else { + if(target_url == NULL) + { + target_url = "about:blank"; + } + } + else + { browser->webView = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(related_web_view)); } @@ -317,8 +339,10 @@ struct Client *new_browser(struct Window *window, gchar *target_url, WebKitWebVi g_signal_connect(browser->webView, "close", G_CALLBACK(WebViewCb_close), browser); g_signal_connect(browser->search, "next-match", G_CALLBACK(SearchEntryCb_next__match), browser); - g_signal_connect(browser->search, "previous-match", G_CALLBACK(SearchEntryCb_previous__match), browser); - g_signal_connect(browser->search, "search-changed", G_CALLBACK(SearchEntryCb_search__changed), browser); + g_signal_connect( + browser->search, "previous-match", G_CALLBACK(SearchEntryCb_previous__match), browser); + g_signal_connect( + browser->search, "search-changed", G_CALLBACK(SearchEntryCb_search__changed), browser); g_signal_connect(browser->search, "stop-search", G_CALLBACK(SearchEntryCb_stop__search), browser); WebKitSettings *settings = webkit_web_view_get_settings(browser->webView); @@ -326,13 +350,15 @@ struct Client *new_browser(struct Window *window, gchar *target_url, WebKitWebVi webkit_settings_set_default_charset(settings, "utf-8"); webkit_settings_set_enable_caret_browsing(settings, TRUE); webkit_settings_set_enable_developer_extras(settings, TRUE); - webkit_settings_set_enable_java(settings, FALSE); /* dangerous */ - webkit_settings_set_enable_javascript(settings, FALSE); /* sane default 😏 */ - webkit_settings_set_enable_plugins(settings, FALSE); /* dangerous */ + webkit_settings_set_enable_java(settings, FALSE); /* dangerous */ + webkit_settings_set_enable_javascript(settings, FALSE); /* sane default 😏 */ + webkit_settings_set_enable_plugins(settings, FALSE); /* dangerous */ webkit_settings_set_javascript_can_access_clipboard(settings, FALSE); /* dangerous */ webkit_settings_set_javascript_can_open_windows_automatically(settings, FALSE); /* dangerous */ - webkit_settings_set_media_playback_requires_user_gesture(settings, TRUE); /* typically breaks notification sounds */ - webkit_settings_set_minimum_font_size(settings, 9); /* text needs to be readable; screw broken pages */ + webkit_settings_set_media_playback_requires_user_gesture( + settings, TRUE); /* typically breaks notification sounds */ + webkit_settings_set_minimum_font_size(settings, + 9); /* text needs to be readable; screw broken pages */ webkit_web_view_set_settings(browser->webView, settings); @@ -344,11 +370,12 @@ struct Client *new_browser(struct Window *window, gchar *target_url, WebKitWebVi int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser) { gint current_page = gtk_notebook_get_current_page(notebook); - gchar *title = "New tab"; + gchar *title = "New tab"; gtk_widget_show_all(browser->box); - if(gtk_notebook_insert_page(notebook, browser->box, NULL, (current_page+2)) == -1) { + if(gtk_notebook_insert_page(notebook, browser->box, NULL, (current_page + 2)) == -1) + { return -1; } @@ -363,7 +390,7 @@ int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser) void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data) { (void)new_tab; - struct Window *window = (struct Window *)user_data; + struct Window *window = (struct Window *)user_data; struct Client *browser = new_browser(window, NULL, NULL); badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser); @@ -373,7 +400,7 @@ void closeCb_clicked(GtkButton *close, gpointer user_data) { (void)close; struct Client *browser = (struct Client *)user_data; - GtkNotebook *notebook = GTK_NOTEBOOK(browser->window->notebook); + GtkNotebook *notebook = GTK_NOTEBOOK(browser->window->notebook); gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); @@ -382,8 +409,8 @@ void closeCb_clicked(GtkButton *close, gpointer user_data) int main(int argc, char *argv[]) { - struct Window *window = g_malloc(sizeof(struct Client)); - gchar *target_url = NULL; + struct Window *window = g_malloc(sizeof(struct Client)); + gchar *target_url = NULL; gtk_init(&argc, &argv);