From 51287bc4200d2eac66bbac964f9309b1759e6bdd Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Mon, 22 Jul 2024 21:54:36 -0400 Subject: [PATCH] backport some small tweaks to rosenrot3.c & cleanup --- config.h | 2 +- plugins/style/style.c | 2 +- plugins/style/style.h | 2 +- rosenrot3.c | 168 +++++++++++++++++++++--------------------- rosenrot4.c | 70 ++++++++++-------- 5 files changed, 128 insertions(+), 116 deletions(-) diff --git a/config.h b/config.h index 368c1ce..29ba7fa 100644 --- a/config.h +++ b/config.h @@ -30,7 +30,7 @@ To disable plugins: 2. recompile To remove plugins completely; -1. Remove the corresponding code in rosenrot.c by looking for the variables above. +1. Remove the corresponding code in rosenrot.c by looking for the variables above, as well as custom_style_enabled 2. Remove PLUGIN and $(PLUGIN) from the makefile 3. Recompile diff --git a/plugins/style/style.c b/plugins/style/style.c index f3d8195..adc2c7c 100644 --- a/plugins/style/style.c +++ b/plugins/style/style.c @@ -1,7 +1,7 @@ #include #include #include -#define STYLE_N 7877 + 1000 +#define STYLE_N 8049 + 1000 void read_style_js(char* string) { diff --git a/plugins/style/style.h b/plugins/style/style.h index d718187..c0f3d4e 100644 --- a/plugins/style/style.h +++ b/plugins/style/style.h @@ -1,5 +1,5 @@ #pragma once -#define STYLE_N 7877 + 1000 +#define STYLE_N 8049 + 1000 void read_style_js(char* string); diff --git a/rosenrot3.c b/rosenrot3.c index a5e1be5..42e11bf 100644 --- a/rosenrot3.c +++ b/rosenrot3.c @@ -32,30 +32,40 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook) /* Load content*/ void load_uri(WebKitWebView* view, const char* uri) { - if (strlen(uri) == 0) { + bool is_empty_uri = (strlen(uri) == 0); + if (is_empty_uri) { webkit_web_view_load_uri(view, ""); toggle_bar(notebook, _SEARCH); - } else if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) { + return; + } + + bool has_direct_uri_prefix = g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:"); + if (has_direct_uri_prefix){ webkit_web_view_load_uri(view, uri); - } else if (strstr(uri, ".com") || strstr(uri, ".org")) { + return; + } + + bool has_common_domain_extension = (strstr(uri, ".com") || strstr(uri, ".org")); + if (has_common_domain_extension){ char tmp[strlen("https://") + strlen(uri) + 1]; snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); webkit_web_view_load_uri(view, tmp); - } else { - // Check for shortcuts - int l = SHORTCUT_N + strlen(uri) + 1; - char uri_expanded[l]; - str_init(uri_expanded, l); - int check = shortcut_expand(uri, uri_expanded); - if (check == 2) { - webkit_web_view_load_uri(view, uri_expanded); - } else { - // Feed into search engine. - char tmp[strlen(uri) + strlen(SEARCH)]; - snprintf(tmp, sizeof(tmp), SEARCH, uri); - webkit_web_view_load_uri(view, tmp); - } - } + return; + } + + int l = SHORTCUT_N + strlen(uri) + 1; + char uri_expanded[l]; + str_init(uri_expanded, l); + int check = shortcut_expand(uri, uri_expanded); + bool has_shortcut = (check == 2); + if (has_shortcut){ + webkit_web_view_load_uri(view, uri_expanded); + return; + } + + char tmp[strlen(uri) + strlen(SEARCH)]; + snprintf(tmp, sizeof(tmp), SEARCH, uri); + webkit_web_view_load_uri(view, tmp); } /* Deal with new load or changed load */ @@ -111,7 +121,35 @@ void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event, } } -/* Create new tabs */ +/* New tabs */ +WebKitWebView* create_new_webview() +{ + char* style; + + WebKitSettings* settings = webkit_settings_new_with_settings(WEBKIT_DEFAULT_SETTINGS, NULL); + if (CUSTOM_USER_AGENT) { + webkit_settings_set_user_agent( + settings, + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, " + "like Gecko) Chrome/120.0.0.0 Safari/537.3"); + // https://www.useragents.me + } + WebKitWebContext* web_context = webkit_web_context_new_with_website_data_manager(webkit_website_data_manager_new(DATA_MANAGER_OPTS, NULL)); + WebKitUserContentManager* contentmanager = webkit_user_content_manager_new(); + + WebKitCookieManager* cookiemanager = webkit_web_context_get_cookie_manager(web_context); + webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE); + webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS); + + if (g_file_get_contents("~/opt/rosenrot/style.css", &style, NULL, NULL)) { + webkit_user_content_manager_add_style_sheet( + contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL)); + } + + WebKitWebView* view = g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, "user-content-manager", contentmanager, NULL); + + return view; +} GtkWidget* handle_signal_create_new_tab(WebKitWebView* self, WebKitNavigationAction* navigation_action, GtkNotebook* notebook) @@ -125,46 +163,9 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self, } else { webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); } - return NULL; - /* - WebKitGTK documentation recommends returning the new webview. - I imagine that this might allow e.g., to go back in a new tab - or generally to keep track of history. - However, this would require either modifying notebook_create_new_tab - or duplicating its contents, for unclear gain. - */ + return ABORT_REQUEST_ON_CURRENT_TAB; } -WebKitWebView* create_new_webview() -{ - char* style; - WebKitSettings* settings; - WebKitWebContext* web_context; - WebKitCookieManager* cookiemanager; - WebKitUserContentManager* contentmanager; - settings = webkit_settings_new_with_settings(WEBKIT_DEFAULT_SETTINGS, NULL); - if (CUSTOM_USER_AGENT) { - webkit_settings_set_user_agent( - settings, - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, " - "like Gecko) Chrome/120.0.0.0 Safari/537.3"); - // https://www.useragents.me - } - web_context = webkit_web_context_new_with_website_data_manager(webkit_website_data_manager_new(DATA_MANAGER_OPTS, NULL)); - contentmanager = webkit_user_content_manager_new(); - cookiemanager = webkit_web_context_get_cookie_manager(web_context); - - webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE); - - webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS); - - if (g_file_get_contents("~/opt/rosenrot/style.css", &style, NULL, NULL)) { - webkit_user_content_manager_add_style_sheet( - contentmanager, webkit_user_style_sheet_new(style, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL)); - } - - return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, "user-content-manager", contentmanager, NULL); -} void notebook_create_new_tab(GtkNotebook* notebook, const char* uri) { if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) { @@ -236,8 +237,7 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) gtk_widget_hide(GTK_WIDGET(bar.widget)); } -/* Handle shortcuts */ -// Act when a particular shortcut is detected +/* Shortcuts */ int handle_shortcut(func id, GtkNotebook* notebook) { static double zoom = ZOOM_START_LEVEL; @@ -254,11 +254,8 @@ int handle_shortcut(func id, GtkNotebook* notebook) break; case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */ - if (custom_style_enabled) - custom_style_enabled = 0; - else - custom_style_enabled = 1; - // break; passthrough + custom_style_enabled ^= 1; + // fallthrough case refresh: webkit_web_view_reload(view); break; @@ -297,18 +294,17 @@ int handle_shortcut(func id, GtkNotebook* notebook) gtk_notebook_set_current_page(notebook, j); break; case close_tab: - gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); num_tabs -= 1; - - switch (gtk_notebook_get_n_pages(notebook)) { + switch(num_tabs){ case 0: exit(0); break; case 1: gtk_notebook_set_show_tabs(notebook, false); - break; + // fallthrough + default: + gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); } - break; case toggle_fullscreen: if (is_fullscreen) @@ -341,6 +337,13 @@ int handle_shortcut(func id, GtkNotebook* notebook) toggle_bar(notebook, _HIDDEN); break; + case halve_window: + gtk_window_resize(window, FULL_WIDTH/2, HEIGHT); + break; + case rebig_window: + gtk_window_resize(window, FULL_WIDTH, HEIGHT); + break; + case prettify: { if (READABILITY_ENABLED) { char* readability_js = malloc(READABILITY_N + 1); @@ -380,7 +383,6 @@ int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook) - https://docs.gtk.org/gdk3/union.Event.html - https://docs.gtk.org/gdk3/struct.EventButton.html */ - // This API is deprecated in GTK4 :(. return 0; } @@ -391,33 +393,35 @@ int main(int argc, char** argv) g_object_set(gtk_settings_get_default(), GTK_SETTINGS_CONFIG_H, NULL); // https://docs.gtk.org/gobject/method.Object.set.html GtkCssProvider* css = gtk_css_provider_new(); gtk_css_provider_load_from_path(css, "/opt/rosenrot/style-gtk3.css", NULL); - gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), 800); /* might change with GTK4/webkitgtk6.0 */ + gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(css), 800); /* Initialize GTK objects. These are declared as static globals at the top of this file */ - // Notebook - notebook = GTK_NOTEBOOK(gtk_notebook_new()); - gtk_notebook_set_show_tabs(notebook, false); - gtk_notebook_set_show_border(notebook, false); // Window window = GTK_WINDOW(gtk_window_new(0)); gtk_window_set_default_size(window, WIDTH, HEIGHT_GTK3); - g_signal_connect(window, "key-press-event", G_CALLBACK(handle_signal_keypress), notebook); - g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook); - - gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook)); /* deprecated in GTK */ + // Notebook + notebook = GTK_NOTEBOOK(gtk_notebook_new()); + gtk_notebook_set_show_tabs(notebook, false); + gtk_notebook_set_show_border(notebook, false); + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook)); // Bar bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0)); bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text)); gtk_entry_set_alignment(bar.line, 0.48); - gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_SIZE, -1); - g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook); + gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_WIDTH, -1); bar.widget = GTK_HEADER_BAR(gtk_header_bar_new()); gtk_header_bar_set_custom_title(bar.widget, GTK_WIDGET(bar.line)); gtk_window_set_titlebar(window, GTK_WIDGET(bar.widget)); + // Signals + g_signal_connect(window, "key-press-event", G_CALLBACK(handle_signal_keypress), notebook); + g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook); + g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook); + + /* Load first tab */ char* first_uri = argc > 1 ? argv[1] : HOME; notebook_create_new_tab(notebook, first_uri); @@ -434,5 +438,5 @@ int main(int argc, char** argv) } } - gtk_main(); /* deprecated in GKT4: https://docs.gtk.org/gtk4/migrating-3to4.html#stop-using-gtk_main-and-related-apis */ + gtk_main(); } diff --git a/rosenrot4.c b/rosenrot4.c index d45e062..6b609c1 100644 --- a/rosenrot4.c +++ b/rosenrot4.c @@ -1,7 +1,6 @@ #include #include #include -#include #include "config.h" #include "plugins/plugins.h" @@ -37,32 +36,39 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook) /* TODO: Think throug void load_uri(WebKitWebView* view, const char* uri) { bool is_empty_uri = (strlen(uri) == 0); + if (is_empty_uri) { + webkit_web_view_load_uri(view, ""); + toggle_bar(notebook, _SEARCH); + return + } + bool has_direct_uri_prefix = g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:"); + if (has_direct_uri_prefix){ + webkit_web_view_load_uri(view, uri); + return; + } + bool has_common_domain_extension = (strstr(uri, ".com") || strstr(uri, ".org")); - bool has_shortcut; + if (has_common_domain_extension){ + char tmp[strlen("https://") + strlen(uri) + 1]; + snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); + webkit_web_view_load_uri(view, tmp); + return + } int l = SHORTCUT_N + strlen(uri) + 1; char uri_expanded[l]; str_init(uri_expanded, l); int check = shortcut_expand(uri, uri_expanded); - has_shortcut = (check == 2); - - if (is_empty_uri) { - webkit_web_view_load_uri(view, ""); - toggle_bar(notebook, _SEARCH); - } else if (has_direct_uri_prefix){ - webkit_web_view_load_uri(view, uri); - } else if (has_common_domain_extension){ - char tmp[strlen("https://") + strlen(uri) + 1]; - snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); - webkit_web_view_load_uri(view, tmp); - } else if (has_shortcut){ + bool has_shortcut = (check == 2); + if (has_shortcut){ webkit_web_view_load_uri(view, uri_expanded); - } else { - char tmp[strlen(uri) + strlen(SEARCH)]; - snprintf(tmp, sizeof(tmp), SEARCH, uri); - webkit_web_view_load_uri(view, tmp); - } + return + } + + char tmp[strlen(uri) + strlen(SEARCH)]; + snprintf(tmp, sizeof(tmp), SEARCH, uri); + webkit_web_view_load_uri(view, tmp); } /* Deal with new load or changed load */ @@ -241,8 +247,7 @@ void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) /* con gtk_widget_hide(GTK_WIDGET(bar.widget)); } -/* Handle shortcuts */ - +/* Shortcuts */ int handle_shortcut(func id) { static double zoom = ZOOM_START_LEVEL; @@ -301,12 +306,16 @@ int handle_shortcut(func id) gtk_notebook_set_current_page(notebook, p); break; case close_tab: - gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); num_tabs -= 1; - if (!gtk_notebook_get_n_pages(notebook)) { - exit(0); - } else { - gtk_notebook_set_show_tabs(notebook, false); + switch(num_tabs){ + case 0: + exit(0); + break; + case 1: + gtk_notebook_set_show_tabs(notebook, false); + // fallthrough + default: + gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook)); } break; case toggle_fullscreen: @@ -342,10 +351,10 @@ int handle_shortcut(func id) break; case halve_window: - gtk_window_set_default_size(window, FULL_WIDTH/2, HEIGHT_GTK4); + gtk_window_set_default_size(window, FULL_WIDTH/2, HEIGHT); break; case rebig_window: - gtk_window_set_default_size(window, FULL_WIDTH, HEIGHT_GTK4); + gtk_window_set_default_size(window, FULL_WIDTH, HEIGHT); break; case prettify: { @@ -395,7 +404,7 @@ int main(int argc, char** argv) // Create the main window window = GTK_WINDOW(gtk_window_new()); - gtk_window_set_default_size(window, WIDTH, HEIGHT_GTK4); + gtk_window_set_default_size(window, WIDTH, HEIGHT); // Set up notebook notebook = GTK_NOTEBOOK(gtk_notebook_new()); @@ -421,7 +430,7 @@ int main(int argc, char** argv) g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook); g_signal_connect(GTK_WIDGET(window), "destroy", G_CALLBACK(exit), notebook); - // Parse first tab + // Load first tab char* first_uri = argc > 1 ? argv[1] : HOME; notebook_create_new_tab(notebook, first_uri); @@ -440,7 +449,6 @@ int main(int argc, char** argv) } // Enter the main event loop, and wait for user interaction - printf("Entering main loop\n"); while (g_list_model_get_n_items(gtk_window_get_toplevels()) > 0 && num_tabs > 0) g_main_context_iteration(NULL, TRUE);