backport some small tweaks to rosenrot3.c & cleanup

This commit is contained in:
NunoSempere 2024-07-22 21:54:36 -04:00
parent 56ed98703d
commit 51287bc420
5 changed files with 128 additions and 116 deletions

View File

@ -30,7 +30,7 @@ To disable plugins:
2. recompile 2. recompile
To remove plugins completely; 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 2. Remove PLUGIN and $(PLUGIN) from the makefile
3. Recompile 3. Recompile

View File

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define STYLE_N 7877 + 1000 #define STYLE_N 8049 + 1000
void read_style_js(char* string) void read_style_js(char* string)
{ {

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#define STYLE_N 7877 + 1000 #define STYLE_N 8049 + 1000
void read_style_js(char* string); void read_style_js(char* string);

View File

@ -32,30 +32,40 @@ WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
/* Load content*/ /* Load content*/
void load_uri(WebKitWebView* view, const char* uri) 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, ""); webkit_web_view_load_uri(view, "");
toggle_bar(notebook, _SEARCH); 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); 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]; char tmp[strlen("https://") + strlen(uri) + 1];
snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri); snprintf(tmp, sizeof(tmp) + 1, "https://%s", uri);
webkit_web_view_load_uri(view, tmp); webkit_web_view_load_uri(view, tmp);
} else { return;
// Check for shortcuts }
int l = SHORTCUT_N + strlen(uri) + 1; int l = SHORTCUT_N + strlen(uri) + 1;
char uri_expanded[l]; char uri_expanded[l];
str_init(uri_expanded, l); str_init(uri_expanded, l);
int check = shortcut_expand(uri, uri_expanded); int check = shortcut_expand(uri, uri_expanded);
if (check == 2) { bool has_shortcut = (check == 2);
if (has_shortcut){
webkit_web_view_load_uri(view, uri_expanded); webkit_web_view_load_uri(view, uri_expanded);
} else { return;
// Feed into search engine. }
char tmp[strlen(uri) + strlen(SEARCH)]; char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri); snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp); webkit_web_view_load_uri(view, tmp);
}
}
} }
/* Deal with new load or changed load */ /* 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, GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
WebKitNavigationAction* navigation_action, WebKitNavigationAction* navigation_action,
GtkNotebook* notebook) GtkNotebook* notebook)
@ -125,46 +163,9 @@ GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
} else { } else {
webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); 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; return ABORT_REQUEST_ON_CURRENT_TAB;
/*
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.
*/
} }
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) void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
{ {
if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) { 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)); gtk_widget_hide(GTK_WIDGET(bar.widget));
} }
/* Handle shortcuts */ /* Shortcuts */
// Act when a particular shortcut is detected
int handle_shortcut(func id, GtkNotebook* notebook) int handle_shortcut(func id, GtkNotebook* notebook)
{ {
static double zoom = ZOOM_START_LEVEL; static double zoom = ZOOM_START_LEVEL;
@ -254,11 +254,8 @@ int handle_shortcut(func id, GtkNotebook* notebook)
break; break;
case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */ case toggle_custom_style: /* Ctrl s + Ctrl Shift R to reload */
if (custom_style_enabled) custom_style_enabled ^= 1;
custom_style_enabled = 0; // fallthrough
else
custom_style_enabled = 1;
// break; passthrough
case refresh: case refresh:
webkit_web_view_reload(view); webkit_web_view_reload(view);
break; break;
@ -297,18 +294,17 @@ int handle_shortcut(func id, GtkNotebook* notebook)
gtk_notebook_set_current_page(notebook, j); gtk_notebook_set_current_page(notebook, j);
break; break;
case close_tab: case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1; num_tabs -= 1;
switch(num_tabs){
switch (gtk_notebook_get_n_pages(notebook)) {
case 0: case 0:
exit(0); exit(0);
break; break;
case 1: case 1:
gtk_notebook_set_show_tabs(notebook, false); gtk_notebook_set_show_tabs(notebook, false);
break; // fallthrough
default:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
} }
break; break;
case toggle_fullscreen: case toggle_fullscreen:
if (is_fullscreen) if (is_fullscreen)
@ -341,6 +337,13 @@ int handle_shortcut(func id, GtkNotebook* notebook)
toggle_bar(notebook, _HIDDEN); toggle_bar(notebook, _HIDDEN);
break; 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: { case prettify: {
if (READABILITY_ENABLED) { if (READABILITY_ENABLED) {
char* readability_js = malloc(READABILITY_N + 1); 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/union.Event.html
- https://docs.gtk.org/gdk3/struct.EventButton.html - https://docs.gtk.org/gdk3/struct.EventButton.html
*/ */
// This API is deprecated in GTK4 :(.
return 0; 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 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(); GtkCssProvider* css = gtk_css_provider_new();
gtk_css_provider_load_from_path(css, "/opt/rosenrot/style-gtk3.css", NULL); 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 */ /* 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
window = GTK_WINDOW(gtk_window_new(0)); window = GTK_WINDOW(gtk_window_new(0));
gtk_window_set_default_size(window, WIDTH, HEIGHT_GTK3); gtk_window_set_default_size(window, WIDTH, HEIGHT_GTK3);
g_signal_connect(window, "key-press-event", G_CALLBACK(handle_signal_keypress), notebook); // Notebook
g_signal_connect(window, "destroy", G_CALLBACK(exit), notebook); notebook = GTK_NOTEBOOK(gtk_notebook_new());
gtk_notebook_set_show_tabs(notebook, false);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook)); /* deprecated in GTK */ gtk_notebook_set_show_border(notebook, false);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(notebook));
// Bar // Bar
bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0)); bar.line_text = GTK_ENTRY_BUFFER(gtk_entry_buffer_new("", 0));
bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text)); bar.line = GTK_ENTRY(gtk_entry_new_with_buffer(bar.line_text));
gtk_entry_set_alignment(bar.line, 0.48); gtk_entry_set_alignment(bar.line, 0.48);
gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_SIZE, -1); gtk_widget_set_size_request(GTK_WIDGET(bar.line), BAR_WIDTH, -1);
g_signal_connect(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
bar.widget = GTK_HEADER_BAR(gtk_header_bar_new()); bar.widget = GTK_HEADER_BAR(gtk_header_bar_new());
gtk_header_bar_set_custom_title(bar.widget, GTK_WIDGET(bar.line)); gtk_header_bar_set_custom_title(bar.widget, GTK_WIDGET(bar.line));
gtk_window_set_titlebar(window, GTK_WIDGET(bar.widget)); 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 */ /* Load first tab */
char* first_uri = argc > 1 ? argv[1] : HOME; char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_new_tab(notebook, first_uri); 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();
} }

View File

@ -1,7 +1,6 @@
#include <gdk/gdk.h> #include <gdk/gdk.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <webkit/webkit.h>
#include "config.h" #include "config.h"
#include "plugins/plugins.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) void load_uri(WebKitWebView* view, const char* uri)
{ {
bool is_empty_uri = (strlen(uri) == 0); 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:"); 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_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; int l = SHORTCUT_N + strlen(uri) + 1;
char uri_expanded[l]; char uri_expanded[l];
str_init(uri_expanded, l); str_init(uri_expanded, l);
int check = shortcut_expand(uri, uri_expanded); int check = shortcut_expand(uri, uri_expanded);
has_shortcut = (check == 2); bool has_shortcut = (check == 2);
if (has_shortcut){
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){
webkit_web_view_load_uri(view, uri_expanded); webkit_web_view_load_uri(view, uri_expanded);
} else { return
}
char tmp[strlen(uri) + strlen(SEARCH)]; char tmp[strlen(uri) + strlen(SEARCH)];
snprintf(tmp, sizeof(tmp), SEARCH, uri); snprintf(tmp, sizeof(tmp), SEARCH, uri);
webkit_web_view_load_uri(view, tmp); webkit_web_view_load_uri(view, tmp);
}
} }
/* Deal with new load or changed load */ /* 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)); gtk_widget_hide(GTK_WIDGET(bar.widget));
} }
/* Handle shortcuts */ /* Shortcuts */
int handle_shortcut(func id) int handle_shortcut(func id)
{ {
static double zoom = ZOOM_START_LEVEL; static double zoom = ZOOM_START_LEVEL;
@ -301,12 +306,16 @@ int handle_shortcut(func id)
gtk_notebook_set_current_page(notebook, p); gtk_notebook_set_current_page(notebook, p);
break; break;
case close_tab: case close_tab:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
num_tabs -= 1; num_tabs -= 1;
if (!gtk_notebook_get_n_pages(notebook)) { switch(num_tabs){
case 0:
exit(0); exit(0);
} else { break;
case 1:
gtk_notebook_set_show_tabs(notebook, false); gtk_notebook_set_show_tabs(notebook, false);
// fallthrough
default:
gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
} }
break; break;
case toggle_fullscreen: case toggle_fullscreen:
@ -342,10 +351,10 @@ int handle_shortcut(func id)
break; break;
case halve_window: 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; break;
case rebig_window: case rebig_window:
gtk_window_set_default_size(window, FULL_WIDTH, HEIGHT_GTK4); gtk_window_set_default_size(window, FULL_WIDTH, HEIGHT);
break; break;
case prettify: { case prettify: {
@ -395,7 +404,7 @@ int main(int argc, char** argv)
// Create the main window // Create the main window
window = GTK_WINDOW(gtk_window_new()); 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 // Set up notebook
notebook = GTK_NOTEBOOK(gtk_notebook_new()); 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(bar.line, "activate", G_CALLBACK(handle_signal_bar_press_enter), notebook);
g_signal_connect(GTK_WIDGET(window), "destroy", G_CALLBACK(exit), 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; char* first_uri = argc > 1 ? argv[1] : HOME;
notebook_create_new_tab(notebook, first_uri); 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 // 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) while (g_list_model_get_n_items(gtk_window_get_toplevels()) > 0 && num_tabs > 0)
g_main_context_iteration(NULL, TRUE); g_main_context_iteration(NULL, TRUE);