commit: f8d6696b46e5f0fe8516a0c77780809a0897de42
parent 9859ac3e8543a78682be88c55f0ccf2f8c610da9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 18 Apr 2024 01:18:43 +0200
Make `struct Window *window` a global variable
Simplifies the code a bit and should actually increase stability by not
ending up with invalid pointers being passed around.
Diffstat:
6 files changed, 60 insertions(+), 67 deletions(-)
diff --git a/badwolf.c b/badwolf.c
@@ -23,6 +23,8 @@
const gchar *homepage = "https://hacktivis.me/projects/badwolf";
const gchar *version = VERSION;
+struct Window *window = NULL;
+
static gchar *web_extensions_directory;
static uint64_t context_id_counter = 0;
GtkTreeModel *bookmarks_completion_model;
@@ -238,7 +240,7 @@ WebViewCb_notify__is__playing__audio(WebKitWebView *UNUSED(webView),
void
webView_tab_label_change(struct Client *browser, const gchar *title)
{
- GtkWidget *notebook = browser->window->notebook;
+ GtkWidget *notebook = window->notebook;
#define title_IS_EMPTY title == NULL || title[0] == '\0'
@@ -253,7 +255,7 @@ webView_tab_label_change(struct Client *browser, const gchar *title)
// Set the window title if the title change was on the current tab
if(gtk_notebook_page_num(GTK_NOTEBOOK(notebook), browser->box) ==
gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook)))
- gtk_window_set_title(GTK_WINDOW(browser->window->main_window), title);
+ gtk_window_set_title(GTK_WINDOW(window->main_window), title);
}
static gboolean
@@ -323,9 +325,9 @@ WebViewCb_create(WebKitWebView *related_web_view,
// shouldn't be needed but better be safe
old_browser->webView = related_web_view;
- browser = new_browser(old_browser->window, NULL, old_browser);
+ browser = new_browser(NULL, old_browser);
- if(badwolf_new_tab(GTK_NOTEBOOK(old_browser->window->notebook), browser, FALSE) < 0)
+ if(badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser, FALSE) < 0)
return NULL;
else
return browser->webView;
@@ -363,9 +365,9 @@ WebViewCb_decide_policy(WebKitWebView *UNUSED(web_view),
{
WebKitURIRequest *uri = webkit_navigation_action_get_request(navigation_action);
const gchar *target_url = webkit_uri_request_get_uri(uri);
- struct Client *browser = new_browser(old_browser->window, target_url, old_browser);
+ struct Client *browser = new_browser(target_url, old_browser);
- badwolf_new_tab(GTK_NOTEBOOK(browser->window->notebook), browser, FALSE);
+ badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser, FALSE);
webkit_web_view_load_uri(browser->webView, target_url);
webkit_policy_decision_ignore(decision);
}
@@ -452,7 +454,7 @@ WebViewCb_load_failed_with_tls_errors(WebKitWebView *UNUSED(web_view),
SoupURI *failing_uri = soup_uri_new(failing_text);
#endif
- GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(browser->window->main_window),
+ GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(window->main_window),
GTK_DIALOG_MODAL & GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_NONE,
@@ -506,15 +508,12 @@ web_contextCb_download_started(WebKitWebContext *UNUSED(web_context),
WebKitDownload *webkit_download,
gpointer user_data)
{
- struct Client *browser = (struct Client *)user_data;
struct Download *download = malloc(sizeof(struct Download));
assert(webkit_download);
if(download != NULL)
{
- download->window = browser->window;
-
download_new_entry(webkit_download, download);
g_signal_connect(
@@ -598,7 +597,7 @@ printCb_clicked(GtkButton *UNUSED(print), gpointer user_data)
WebKitPrintOperation *print_operation = webkit_print_operation_new(browser->webView);
- webkit_print_operation_run_dialog(print_operation, GTK_WINDOW(browser->window->main_window));
+ webkit_print_operation_run_dialog(print_operation, GTK_WINDOW(window->main_window));
}
static gboolean
@@ -654,7 +653,7 @@ widgetCb_drop_button3_event(GtkWidget *UNUSED(widget), GdkEvent *event, gpointer
}
struct Client *
-new_browser(struct Window *window, const gchar *target_url, struct Client *old_browser)
+new_browser(const gchar *target_url, struct Client *old_browser)
{
struct Client *browser = malloc(sizeof(struct Client));
target_url = badwolf_ensure_uri_scheme(target_url, (old_browser == NULL));
@@ -666,7 +665,6 @@ new_browser(struct Window *window, const gchar *target_url, struct Client *old_b
assert(window != NULL);
- browser->window = window;
browser->context_id = old_browser == NULL ? context_id_counter++ : old_browser->context_id;
browser->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_name(browser->box, "browser__box");
@@ -960,10 +958,9 @@ badwolf_new_tab(GtkNotebook *notebook, struct Client *browser, bool auto_switch)
}
static void
-new_tabCb_clicked(GtkButton *UNUSED(new_tab), gpointer user_data)
+new_tabCb_clicked(GtkButton *UNUSED(new_tab), gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
- struct Client *browser = new_browser(window, NULL, NULL);
+ struct Client *browser = new_browser(NULL, NULL);
badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser, TRUE);
}
@@ -980,20 +977,18 @@ static void
notebookCb_switch__page(GtkNotebook *notebook,
GtkWidget *page,
guint UNUSED(page_num),
- gpointer user_data)
+ gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
- GtkWidget *label = gtk_notebook_get_tab_label(notebook, page);
+ GtkWidget *label = gtk_notebook_get_tab_label(notebook, page);
// TODO: Maybe find a better way to store the title
gtk_window_set_title(GTK_WINDOW(window->main_window), gtk_widget_get_tooltip_text(label));
}
void
-content_managerCb_ready(GObject *UNUSED(store), GAsyncResult *result, gpointer user_data)
+content_managerCb_ready(GObject *UNUSED(store), GAsyncResult *result, gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
- GError *err = NULL;
+ GError *err = NULL;
WebKitUserContentFilter *filter =
webkit_user_content_filter_store_load_finish(window->content_store, result, &err);
@@ -1022,10 +1017,9 @@ content_managerCb_ready(GObject *UNUSED(store), GAsyncResult *result, gpointer u
static void
storeCb_finish(WebKitUserContentFilterStore *UNUSED(store),
GAsyncResult *result,
- gpointer user_data)
+ gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
- GError *err = NULL;
+ GError *err = NULL;
WebKitUserContentFilter *filter =
webkit_user_content_filter_store_save_finish(window->content_store, result, &err);
@@ -1057,7 +1051,6 @@ storeCb_finish(WebKitUserContentFilterStore *UNUSED(store),
int
main(int argc, char *argv[])
{
- struct Window *window = &(struct Window){NULL, NULL, NULL, NULL, NULL, NULL};
GApplication *application;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, DATADIR "/locale");
@@ -1111,22 +1104,26 @@ main(int argc, char *argv[])
bookmarks_completion_model = bookmarks_completion_init();
g_object_ref(bookmarks_completion_model);
- window->main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- window->notebook = gtk_notebook_new();
- window->new_tab = gtk_button_new_from_icon_name("tab-new-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
- window->downloads_tab = badwolf_downloads_tab_new();
- window->content_manager = webkit_user_content_manager_new();
+ gchar *filtersPath = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), "filters", NULL);
+
+ window = &(struct Window){
+ .main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL),
+ .notebook = gtk_notebook_new(),
+ .new_tab = gtk_button_new_from_icon_name("tab-new-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR),
+ .downloads_tab = badwolf_downloads_tab_new(),
+ .content_manager = webkit_user_content_manager_new(),
+ .content_store = webkit_user_content_filter_store_new(filtersPath),
+ };
load_userscripts(window->content_manager);
+ assert(window != NULL);
+
gchar *contentFilterPath =
g_build_filename(g_get_user_config_dir(), g_get_prgname(), "content-filters.json", NULL);
GFile *contentFilterFile = g_file_new_for_path(contentFilterPath);
fprintf(stderr, _("content-filters file set to: %s\n"), contentFilterPath);
- gchar *filtersPath = g_build_filename(g_get_user_cache_dir(), g_get_prgname(), "filters", NULL);
- window->content_store = webkit_user_content_filter_store_new(filtersPath);
-
webkit_user_content_filter_store_save_from_file(window->content_store,
"a",
contentFilterFile,
@@ -1176,23 +1173,23 @@ main(int argc, char *argv[])
gtk_container_add(GTK_CONTAINER(window->main_window), window->notebook);
gtk_widget_queue_draw(window->notebook);
- badwolf_downloads_tab_attach(window);
+ badwolf_downloads_tab_attach();
g_signal_connect(
- window->main_window, "key-press-event", G_CALLBACK(main_windowCb_key_press_event), window);
+ window->main_window, "key-press-event", G_CALLBACK(main_windowCb_key_press_event), NULL);
g_signal_connect(window->main_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
- g_signal_connect(window->new_tab, "clicked", G_CALLBACK(new_tabCb_clicked), window);
- g_signal_connect(window->notebook, "switch-page", G_CALLBACK(notebookCb_switch__page), window);
+ g_signal_connect(window->new_tab, "clicked", G_CALLBACK(new_tabCb_clicked), NULL);
+ g_signal_connect(window->notebook, "switch-page", G_CALLBACK(notebookCb_switch__page), NULL);
gtk_widget_show(window->new_tab);
gtk_widget_show_all(window->main_window);
if(argc == 0)
- badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(window, NULL, NULL), FALSE);
+ badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(NULL, NULL), FALSE);
else
for(int i = 0; i < argc; ++i)
- badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(window, argv[i], NULL), FALSE);
+ badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(argv[i], NULL), FALSE);
gtk_notebook_set_current_page(GTK_NOTEBOOK(window->notebook), 1);
diff --git a/badwolf.h b/badwolf.h
@@ -22,6 +22,7 @@
extern const gchar *homepage;
extern const gchar *version;
+extern struct Window *window;
struct Window
{
@@ -46,7 +47,6 @@ struct Client
uint64_t context_id;
WebKitWebView *webView;
- struct Window *window;
GtkWidget *statusbar;
GtkWidget *statuslabel;
@@ -55,8 +55,7 @@ struct Client
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, const gchar *target_url, struct Client *old_browser);
+struct Client *new_browser(const gchar *target_url, struct Client *old_browser);
int badwolf_new_tab(GtkNotebook *notebook, struct Client *browser, bool auto_switch);
gint badwolf_get_tab_position(GtkContainer *notebook, GtkWidget *child);
#endif /* BADWOLF_H_INCLUDED */
diff --git a/downloads.c b/downloads.c
@@ -7,6 +7,7 @@
#include "badwolf.h"
#include "config.h"
+#include <assert.h>
#include <glib/gi18n.h> /* _() and other internationalization/localization helpers */
static void
@@ -56,7 +57,7 @@ download_new_entry(WebKitDownload *webkit_download, struct Download *download)
gtk_box_pack_start(GTK_BOX(download->container), download->status, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(download->container), download->file_path, FALSE, FALSE, 0);
- gtk_list_box_insert(GTK_LIST_BOX(download->window->downloads_tab), download->container, -1);
+ gtk_list_box_insert(GTK_LIST_BOX(window->downloads_tab), download->container, -1);
gtk_widget_show_all(download->container);
}
@@ -79,11 +80,10 @@ downloadCb_created_destination(WebKitDownload *UNUSED(webkit_download),
gboolean
downloadCb_decide_destination(WebKitDownload *webkit_download,
gchar *suggested_filename,
- gpointer user_data)
+ gpointer UNUSED(user_data))
{
- struct Client *browser = (struct Client *)user_data;
gint chooser_response;
- GtkWindow *parent_window = GTK_WINDOW(browser->window->main_window);
+ GtkWindow *parent_window = GTK_WINDOW(window->main_window);
GtkFileChooserNative *file_dialog =
gtk_file_chooser_native_new(NULL, parent_window, GTK_FILE_CHOOSER_ACTION_SAVE, NULL, NULL);
@@ -187,8 +187,10 @@ badwolf_downloads_tab_new()
}
void
-badwolf_downloads_tab_attach(struct Window *window)
+badwolf_downloads_tab_attach()
{
+ assert(window != NULL);
+
GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_widget_set_name(scrolled_window, "browser__scrollwin_downloads");
gtk_container_add(GTK_CONTAINER(scrolled_window), window->downloads_tab);
diff --git a/downloads.h b/downloads.h
@@ -7,8 +7,6 @@
struct Download
{
- struct Window *window;
-
GtkWidget *container;
GtkWidget *icon;
GtkWidget *stop_icon;
@@ -28,4 +26,4 @@ void downloadCb_failed(WebKitDownload *webkit_download, GError *error, gpointer
void downloadCb_finished(WebKitDownload *download, gpointer user_data);
void downloadCb_received_data(WebKitDownload *download, guint64 data_lenght, gpointer user_data);
GtkWidget *badwolf_downloads_tab_new();
-void badwolf_downloads_tab_attach(struct Window *window);
+void badwolf_downloads_tab_attach();
diff --git a/keybindings.c b/keybindings.c
@@ -9,11 +9,9 @@
#include <glib/gi18n.h> /* _() */
static gboolean
-about_dialogCb_activate_link(GtkAboutDialog *about_dialog, gchar *uri, gpointer user_data)
+about_dialogCb_activate_link(GtkAboutDialog *about_dialog, gchar *uri, gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
-
- badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(window, uri, NULL), TRUE);
+ badwolf_new_tab(GTK_NOTEBOOK(window->notebook), new_browser(uri, NULL), TRUE);
gtk_window_close(GTK_WINDOW(about_dialog));
@@ -21,9 +19,8 @@ about_dialogCb_activate_link(GtkAboutDialog *about_dialog, gchar *uri, gpointer
}
static void
-badwolf_about_dialog(GtkWindow *main_window, gpointer user_data)
+badwolf_about_dialog(GtkWindow *main_window, gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
GtkWidget *about_dialog = gtk_about_dialog_new();
char *comments = NULL;
@@ -75,7 +72,7 @@ toggle_caret_browsing(WebKitWebView *webView)
* loosely follows https://developer.gnome.org/hig/guidelines/keyboard.html
*/
gboolean
-commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *browser)
+commonCb_key_press_event(GdkEvent *event, struct Client *browser)
{
GtkNotebook *notebook = GTK_NOTEBOOK(window->notebook);
@@ -115,7 +112,7 @@ commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *
return TRUE;
case GDK_KEY_p:
webkit_print_operation_run_dialog(webkit_print_operation_new(browser->webView),
- GTK_WINDOW(browser->window->main_window));
+ GTK_WINDOW(window->main_window));
return TRUE;
}
}
@@ -130,7 +127,7 @@ commonCb_key_press_event(struct Window *window, GdkEvent *event, struct Client *
gtk_notebook_prev_page(notebook);
return TRUE;
case GDK_KEY_t:
- badwolf_new_tab(notebook, new_browser(window, NULL, NULL), TRUE);
+ badwolf_new_tab(notebook, new_browser(NULL, NULL), TRUE);
return TRUE;
}
}
@@ -194,7 +191,7 @@ WebViewCb_key_press_event(WebKitWebView *UNUSED(webView), GdkEvent *event, gpoin
{
struct Client *browser = (struct Client *)user_data;
- if(commonCb_key_press_event(browser->window, event, browser)) return TRUE;
+ if(commonCb_key_press_event(event, browser)) return TRUE;
return FALSE;
}
@@ -204,17 +201,17 @@ boxCb_key_press_event(GtkWidget *UNUSED(widget), GdkEvent *event, gpointer user_
{
struct Client *browser = (struct Client *)user_data;
- if(commonCb_key_press_event(browser->window, event, browser)) return TRUE;
+ if(commonCb_key_press_event(event, browser)) return TRUE;
return FALSE;
}
gboolean
-main_windowCb_key_press_event(GtkWidget *UNUSED(widget), GdkEvent *event, gpointer user_data)
+main_windowCb_key_press_event(GtkWidget *UNUSED(widget),
+ GdkEvent *event,
+ gpointer UNUSED(user_data))
{
- struct Window *window = (struct Window *)user_data;
-
- if(commonCb_key_press_event(window, event, NULL)) return TRUE;
+ if(commonCb_key_press_event(event, NULL)) return TRUE;
return FALSE;
}
diff --git a/keybindings.h b/keybindings.h
@@ -9,7 +9,7 @@
#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 commonCb_key_press_event(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);
gboolean tab_boxCb_button_release_event(GtkWidget *widget, GdkEvent *event, gpointer user_data);