From d6456e4357a1d2f1480f4ac14e1e00ddba2644b4 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Thu, 14 Mar 2024 15:47:36 -0300 Subject: cleanup & change binary name to rosenrot --- TODO.md | 6 +- config.h | 2 +- makefile | 44 +- rose | Bin 40176 -> 0 bytes rose-mklink | 17 - rose.c | 454 --------------------- rosenrot | Bin 0 -> 40176 bytes rosenrot-mklink | 17 + rosenrot.c | 454 +++++++++++++++++++++ .../debian-12/install-with-dependencies.sh | 4 +- user-scripts/debian-12/rose.desktop | 9 - user-scripts/debian-12/rosenrot.desktop | 9 + user-scripts/ubuntu-20.04/README.md | 2 +- 13 files changed, 509 insertions(+), 509 deletions(-) delete mode 100755 rose delete mode 100755 rose-mklink delete mode 100644 rose.c create mode 100755 rosenrot create mode 100755 rosenrot-mklink create mode 100644 rosenrot.c delete mode 100755 user-scripts/debian-12/rose.desktop create mode 100755 user-scripts/debian-12/rosenrot.desktop diff --git a/TODO.md b/TODO.md index 63ec9f2..4092c47 100644 --- a/TODO.md +++ b/TODO.md @@ -3,21 +3,21 @@ ## Quality of life: - [ ] Document creating new applications, e.g., as in [Asana for Linux](https://git.nunosempere.com/NunoSempere/asana-for-linux) -- [ ] Set [`webkit_web_context_set_sandbox_enabled`](), as recommended [here]() - [ ] Use something other than Whatsapp as an example syslink. - [ ] Fix bug about distorted audio. Maybe related to [this pipewire issue]()? -- [ ] Upgrade to GTK-4 / Webkitgtk 6.0? Will take a fair amount of time, since these are not available on Ubuntu 20.04. +- [ ] Upgrade to GTK-4 / Webkitgtk 6.0? Will take a fair amount of time, since GTK4 redesigns the application model somewhat. - Instructions for webkit-6.0 [here](https://github.com/WebKit/WebKit/blob/ed1422596dce5ff012e64a38faf402ac1674fc7e/Source/WebKit/gtk/migrating-to-webkitgtk-6.0.md) - Instructions for GTK-4 [here](https://docs.gtk.org/gtk4/migrating-3to4.html) - [ ] Prepare for GTK-3 to GTK-4 transition - [ ] Understand wtf is going on with signals and events: . - [ ] Remove webkit2gtk-4.1 and download webkit2gtk-6.0 - [ ] Attempt to compile - - [ ] Change README and point to last Ubuntu 20.04 commit # Previously done +- ~~[ ] Set [`webkit_web_context_set_sandbox_enabled`](), as recommended [here]()~~. Irrelevant with upgrade to libsoup3. - [x] Update to webkit2gtk-4.1 + - [x] Change README and point to last Ubuntu 20.04 commit - [x] Add list of similar projects: - [x] Add comparisons against rose & surf - [x] Compare against rose diff --git a/config.h b/config.h index 4dc8ca6..b29cc02 100644 --- a/config.h +++ b/config.h @@ -43,7 +43,7 @@ To remove plugins completely; "enable-smooth-scrolling", false, \ "default-charset", "utf-8" /* CACHE */ -#define DATA_DIR "/home/nuno/.cache/rose" +#define DATA_DIR "/home/nuno/.cache/rosenrot" #define DATA_MANAGER_OPTS "base-cache-directory", DATA_DIR, "base-data-directory", DATA_DIR // GTK diff --git a/makefile b/makefile index 03c8129..a6d3a93 100644 --- a/makefile +++ b/makefile @@ -12,7 +12,7 @@ INCS=`pkg-config --cflags ${DEPS}` LIBS=`pkg-config --libs ${DEPS}` # Code -SRC=rose.c +SRC=rosenrot.c CONFIG=config.h ## Plugins @@ -31,8 +31,8 @@ FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT) # Change hardcoded paths when building ## Data dirs USER=`whoami` -DEFAULT_DATA_DIR=/home/nuno/.cache/rose -USER_DATA_DIR=/home/$(USER)/.cache/rose +DEFAULT_DATA_DIR=/home/nuno/.cache/rosenrot +USER_DATA_DIR=/home/$(USER)/.cache/rosenrot ## Startup image dir DEFAULT_DIR=/home/nuno/Documents/workspace/rosenrot CURRENT_DIR=`pwd` @@ -51,50 +51,50 @@ build: $(SRC) $(PLUGINS) $(CONFIG) sed -i "s|$(DEFAULT_DIR)|$(CURRENT_DIR)|g" {} + # Compile rosenrot GIO_MODULE_DIR=/usr/lib/x86_64-linux-gnu/gio/modules/ - $(CC) $(WARNINGS) $(OPTIMIZED_MORE) $(DEBUG) $(INCS) $(PLUGINS) $(SRC) -o rose $(LIBS) $(ADBLOCK) + $(CC) $(WARNINGS) $(OPTIMIZED_MORE) $(DEBUG) $(INCS) $(PLUGINS) $(SRC) -o rosenrot $(LIBS) $(ADBLOCK) -install: rose +install: rosenrot GIO_MODULE_DIR=/usr/lib/x86_64-linux-gnu/gio/modules/ - cp -f rose /usr/bin - mkdir -p /usr/share/themes/rose - cp style.css /usr/share/themes/rose/ - cp rose-mklink /usr/bin - sudo mkdir -p /usr/bin/rose-browser - sudo cp rose /usr/bin/rose-browser/twitter # custom twitter tweaks + cp -f rosenrot /usr/bin + mkdir -p /usr/share/themes/rosenrot + cp style.css /usr/share/themes/rosenrot/ + cp rosenrot-mklink /usr/bin + sudo mkdir -p /usr/bin/rosenrot-browser + sudo cp rosenrot /usr/bin/rosenrot-browser/twitter # custom twitter tweaks uninstall: - rm -r /usr/share/themes/rose - rm /usr/bin/rose - rm /usr/bin/rose-mklink + rm -r /usr/share/themes/rosenrot + rm /usr/bin/rosenrot + rm /usr/bin/rosenrot-mklink rm $(DATA_DIR) clean: - rm rose + rm rosenrot rm $(DATA_DIR) format: $(SRC) $(PLUGINS) - $(FORMATTER) $(SRC) $(PLUGINS) $(rose.h) + $(FORMATTER) $(SRC) $(PLUGINS) $(rosenrot.h) lint: - clang-tidy $(SRC) $(PLUGINS) -- -Wall -O3 `pkg-config --cflags 'webkit2gtk-4.1'` -o rose `pkg-config --libs 'webkit2gtk-4.1'` + clang-tidy $(SRC) $(PLUGINS) -- -Wall -O3 `pkg-config --cflags 'webkit2gtk-4.1'` -o rosenrot `pkg-config --libs 'webkit2gtk-4.1'` ## A few more commands: fast: $(SRC) $(PLUGINS) $(CONFIG) rm -f *.gcda GIO_MODULE_DIR=/usr/lib/x86_64-linux-gnu/gio/modules/ - $(CC) $(WARNINGS) $(OPTIMIZED_MORE) -fprofile-generate $(INCS) $(PLUGINS) $(SRC) -o rose $(LIBS) $(ADBLOCK) + $(CC) $(WARNINGS) $(OPTIMIZED_MORE) -fprofile-generate $(INCS) $(PLUGINS) $(SRC) -o rosenrot $(LIBS) $(ADBLOCK) @echo "Now use the browser for a while to gather some profiling data" sleep 2 - ./rose - $(CC) $(WARNINGS) $(OPTIMIZED_MORE) -fprofile-use $(INCS) $(PLUGINS) $(SRC) -o rose $(LIBS) $(ADBLOCK) + ./rosenrot + $(CC) $(WARNINGS) $(OPTIMIZED_MORE) -fprofile-use $(INCS) $(PLUGINS) $(SRC) -o rosenrot $(LIBS) $(ADBLOCK) rm -f *.gcda inspect: build - GTK_DEBUG=interactive ./rose + GTK_DEBUG=interactive ./rosenrot diagnose_deprecations: - G_ENABLE_DIAGNOSTIC=1 ./rose + G_ENABLE_DIAGNOSTIC=1 ./rosenrot view-gtk3-version: dpkg -l libgtk-3-0 diff --git a/rose b/rose deleted file mode 100755 index f3c043b..0000000 Binary files a/rose and /dev/null differ diff --git a/rose-mklink b/rose-mklink deleted file mode 100755 index f016aae..0000000 --- a/rose-mklink +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh - -test "$1" = "--help" || test -z "$1" && { - printf "%s\n" "usage: rose-mklink " \ - "Create a /usr/bin link to a website." - exit -} - -test -z "$2" || { - test -f "/usr/bin/$1" && { - echo "/usr/bin/$1 already exists, remove it first" - exit 1 - } - - printf "#!/bin/sh\n\nrose %s" "$2" > /usr/bin/$1 - chmod +x /usr/bin/$1 -} diff --git a/rose.c b/rose.c deleted file mode 100644 index 34a74ad..0000000 --- a/rose.c +++ /dev/null @@ -1,454 +0,0 @@ -#include -#include -#include - -#include "config.h" -#include "plugins/plugins.h" - -/* Global declarations */ -static GtkNotebook* notebook; -static GtkWindow* window; -static struct { - GtkHeaderBar* widget; - GtkEntry* line; - GtkEntryBuffer* line_text; - enum { _SEARCH, - _FIND, - _HIDDEN } entry_mode; -} bar; -static int num_tabs = 0; - -/* Utils */ -WebKitWebView* notebook_get_webview(GtkNotebook* notebook) -{ - return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page( - notebook, gtk_notebook_get_current_page(notebook))); -} - -/* Load content*/ -void load_uri(WebKitWebView* view, const char* uri) -{ - 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:")) { - webkit_web_view_load_uri(view, uri); - } 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); - } - } -} - -/* Deal with new load or changed load */ -void redirect_if_annoying(WebKitWebView* view, const char* uri) -{ - if (LIBRE_REDIRECT_ENABLED) { - int l = LIBRE_N + strlen(uri) + 1; - char uri_filtered[l]; - str_init(uri_filtered, l); - - int check = libre_redirect(uri, uri_filtered); - - if (check == 2) { - webkit_web_view_load_uri(view, uri_filtered); - } - } -} -void set_custom_style(WebKitWebView* view) -{ - if (CUSTOM_STYLE_ENABLED) { - char* style_js = malloc(STYLE_N + 1); - read_style_js(style_js); - webkit_web_view_evaluate_javascript(view, style_js, -1, NULL, "rosenrot-style-plugin", NULL, NULL, NULL); - free(style_js); - } -} -void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event, - GtkNotebook* notebook) -{ - switch (load_event) { - /* see - */ - case WEBKIT_LOAD_STARTED: - set_custom_style(self); - redirect_if_annoying(self, webkit_web_view_get_uri(self)); - break; - case WEBKIT_LOAD_REDIRECTED: - redirect_if_annoying(self, webkit_web_view_get_uri(self)); - break; - case WEBKIT_LOAD_COMMITTED: - redirect_if_annoying(self, webkit_web_view_get_uri(self)); - set_custom_style(self); - break; - case WEBKIT_LOAD_FINISHED: { - /* Add gtk tab title */ - const char* webpage_title = webkit_web_view_get_title(self); - const int max_length = 25; - char tab_title[max_length + 1]; - if (webpage_title != NULL) { - for (int i = 0; i < (max_length); i++) { - tab_title[i] = webpage_title[i]; - if (webpage_title[i] == '\0') { - break; - } - } - tab_title[max_length] = '\0'; - } - - gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self), - webpage_title == NULL ? "—" : tab_title); - // gtk_widget_hide(GTK_WIDGET(bar)); - } - } -} - -/* Create new tabs */ -void notebook_create_new_tab(GtkNotebook* notebook, const char* uri); -// handle_signal_create_new_tab is bound to a signal inside notebook_create_new_tab -// and itself calls notebook_create_new_tab -// therefore we need to do a forward declaration for one of them. -GtkWidget* handle_signal_create_new_tab(WebKitWebView* self, - WebKitNavigationAction* navigation_action, - GtkNotebook* notebook) -{ - if (num_tabs < MAX_NUM_TABS || num_tabs == 0) { - WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action); - const char* uri = webkit_uri_request_get_uri(uri_request); - printf("Creating new window: %s\n", uri); - notebook_create_new_tab(notebook, uri); - gtk_notebook_set_show_tabs(notebook, true); - return NULL; - } 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. - */ -} -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/110.0.0.0 Safari/537.36"); - // See: for some common user agents - } - 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("~/.config/rose/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) { - - WebKitWebView* view = create_new_webview(); - - g_signal_connect(view, "load_changed", G_CALLBACK(handle_signal_load_changed), notebook); - g_signal_connect(view, "create", G_CALLBACK(handle_signal_create_new_tab), notebook); - - int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL); - gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true); - gtk_widget_show_all(GTK_WIDGET(window)); - gtk_widget_hide(GTK_WIDGET(bar.widget)); - load_uri(view, (uri) ? uri : HOME); - - set_custom_style(view); - - gtk_notebook_set_current_page(notebook, n); - gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-"); - webkit_web_view_set_zoom_level(view, ZOOM); - num_tabs += 1; - } else { - - webkit_web_view_evaluate_javascript(notebook_get_webview(notebook), "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); - } -} - -/* Top bar */ -void show_bar(GtkNotebook* notebook) -{ - if (bar.entry_mode == _SEARCH) { - const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook)); - gtk_entry_set_placeholder_text(bar.line, "Search"); - gtk_entry_buffer_set_text(bar.line_text, url, strlen(url)); - gtk_widget_show(GTK_WIDGET(bar.widget)); - gtk_window_set_focus(window, GTK_WIDGET(bar.line)); - } else if (bar.entry_mode == _HIDDEN) { - gtk_widget_hide(GTK_WIDGET(bar.widget)); - } else { - const char* search_text = webkit_find_controller_get_search_text( - webkit_web_view_get_find_controller(notebook_get_webview(notebook))); - - if (search_text != NULL) - gtk_entry_buffer_set_text(bar.line_text, search_text, strlen(search_text)); - - gtk_entry_set_placeholder_text(bar.line, "Find"); - gtk_widget_show(GTK_WIDGET(bar.widget)); - gtk_window_set_focus(window, GTK_WIDGET(bar.line)); - } -} -// Handle what happens when the user is on the bar and presses enter -void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) -{ - if (bar.entry_mode == _SEARCH) - load_uri(notebook_get_webview(notebook), - gtk_entry_buffer_get_text(bar.line_text)); - else if (bar.entry_mode == _FIND) - webkit_find_controller_search( - webkit_web_view_get_find_controller(notebook_get_webview(notebook)), - gtk_entry_buffer_get_text(bar.line_text), - WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, - G_MAXUINT); - - gtk_widget_hide(GTK_WIDGET(bar.widget)); -} - -/* Handle shortcuts */ -// Act when a particular shortcut is detected -int handle_shortcut(func id, GtkNotebook* notebook) -{ - static double zoom = ZOOM; - static bool is_fullscreen = 0; - - WebKitWebView* view = notebook_get_webview(notebook); - - switch (id) { - case goback: - webkit_web_view_go_back(view); - break; - case goforward: - webkit_web_view_go_forward(view); - break; - - case refresh: - webkit_web_view_reload(view); - break; - case refresh_force: - webkit_web_view_reload_bypass_cache(view); - break; - - case back_to_home: - load_uri(view, HOME); - break; - - case zoomin: - webkit_web_view_set_zoom_level(view, - (zoom += ZOOM_VAL)); - break; - - case zoomout: - webkit_web_view_set_zoom_level(view, - (zoom -= ZOOM_VAL)); - break; - - case zoom_reset: - webkit_web_view_set_zoom_level(view, - (zoom = ZOOM)); - break; - - case prev_tab:; // declarations aren't statements - // - int n = gtk_notebook_get_n_pages(notebook); - int k = gtk_notebook_get_current_page(notebook); - int l = (n + k - 1) % n; - gtk_notebook_set_current_page(notebook, l); - break; - - case next_tab:; - int m = gtk_notebook_get_n_pages(notebook); - int i = gtk_notebook_get_current_page(notebook); - int j = (i + 1) % m; - 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)) { - case 0: - exit(0); - break; - case 1: - gtk_notebook_set_show_tabs(notebook, false); - break; - } - - break; - - case toggle_fullscreen: - if (is_fullscreen) - gtk_window_unfullscreen(window); - else - gtk_window_fullscreen(window); - - is_fullscreen = !is_fullscreen; - break; - - case show_searchbar: - bar.entry_mode = _SEARCH; - show_bar(notebook); - break; - - case show_finder: - bar.entry_mode = _FIND; - show_bar(notebook); - break; - - case finder_next: - webkit_find_controller_search_next( - webkit_web_view_get_find_controller(view)); - break; - - case finder_prev: - webkit_find_controller_search_previous( - webkit_web_view_get_find_controller(view)); - break; - - case new_tab: - notebook_create_new_tab(notebook, NULL); - gtk_notebook_set_show_tabs(notebook, true); - bar.entry_mode = _SEARCH; - show_bar(notebook); - break; - - case hide_bar: - bar.entry_mode = _HIDDEN; - show_bar(notebook); - break; - - case prettify: { - if (READABILITY_ENABLED) { - char* readability_js = malloc(READABILITY_N + 1); - read_readability_js(readability_js); - webkit_web_view_evaluate_javascript(view, readability_js, -1, NULL, "rosenrot-readability-plugin", NULL, NULL, NULL); - free(readability_js); - } - break; - } - } - - return 1; -} -// Listen to key presses and call shortcuts if needed. -int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook) -{ - (void)self; - - guint event_keyval = 0; - gdk_event_get_keyval(event, &event_keyval); - GdkModifierType event_state = 0; - gdk_event_get_state(event, &event_state); - - int debug_shortcuts = 0; - if (debug_shortcuts) { - printf("Keypress state: %d\n", event_state); - if (event_state & GDK_CONTROL_MASK) { - printf("Keypress state is: CONTROL\n"); - } - printf("Keypress value: %d\n", event_keyval); - } - - for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++) - if ((event_state & shortcut[i].mod || shortcut[i].mod == 0x0) && event_keyval == shortcut[i].key) - return handle_shortcut(shortcut[i].id, notebook); - /* - If I wanted to bind button presses, like the extra button in the mouse, - I would have to bind the button-press-event signal instead. - Some links in case I go down that road: - - - - - - https://docs.gtk.org/gdk3/struct.EventButton.html - */ - // This API is deprecated in GTK4 :( - return 0; -} - -int main(int argc, char** argv) -{ - /* Initialize GTK in general */ - gtk_init(NULL, NULL); // - g_object_set(gtk_settings_get_default(), GTK_SETTINGS_CONFIG_H, NULL); // - GtkCssProvider* css = gtk_css_provider_new(); - gtk_css_provider_load_from_path(css, "/usr/share/themes/rose/style.css", NULL); - 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); - 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)); - - // 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); - - 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)); - - /* Load first tab */ - char* first_uri = argc > 1 ? argv[1] : HOME; - notebook_create_new_tab(notebook, first_uri); - - /* Show to user */ - gtk_widget_show_all(GTK_WIDGET(window)); - gtk_widget_hide(GTK_WIDGET(bar.widget)); - - /* Deal with more tabs */ - if (argc > 2) { - gtk_notebook_set_show_tabs(notebook, true); - for (int i = 2; i < argc; i++) { - notebook_create_new_tab(notebook, argv[i]); - } - } - - gtk_main(); -} diff --git a/rosenrot b/rosenrot new file mode 100755 index 0000000..cb3f6a0 Binary files /dev/null and b/rosenrot differ diff --git a/rosenrot-mklink b/rosenrot-mklink new file mode 100755 index 0000000..d8f5be6 --- /dev/null +++ b/rosenrot-mklink @@ -0,0 +1,17 @@ +#!/bin/sh + +test "$1" = "--help" || test -z "$1" && { + printf "%s\n" "usage: rosenrot-mklink " \ + "Create a /usr/bin link to a website." + exit +} + +test -z "$2" || { + test -f "/usr/bin/$1" && { + echo "/usr/bin/$1 already exists, remove it first" + exit 1 + } + + printf "#!/bin/sh\n\nrosenrot %s" "$2" > /usr/bin/$1 + chmod +x /usr/bin/$1 +} diff --git a/rosenrot.c b/rosenrot.c new file mode 100644 index 0000000..34a74ad --- /dev/null +++ b/rosenrot.c @@ -0,0 +1,454 @@ +#include +#include +#include + +#include "config.h" +#include "plugins/plugins.h" + +/* Global declarations */ +static GtkNotebook* notebook; +static GtkWindow* window; +static struct { + GtkHeaderBar* widget; + GtkEntry* line; + GtkEntryBuffer* line_text; + enum { _SEARCH, + _FIND, + _HIDDEN } entry_mode; +} bar; +static int num_tabs = 0; + +/* Utils */ +WebKitWebView* notebook_get_webview(GtkNotebook* notebook) +{ + return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page( + notebook, gtk_notebook_get_current_page(notebook))); +} + +/* Load content*/ +void load_uri(WebKitWebView* view, const char* uri) +{ + 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:")) { + webkit_web_view_load_uri(view, uri); + } 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); + } + } +} + +/* Deal with new load or changed load */ +void redirect_if_annoying(WebKitWebView* view, const char* uri) +{ + if (LIBRE_REDIRECT_ENABLED) { + int l = LIBRE_N + strlen(uri) + 1; + char uri_filtered[l]; + str_init(uri_filtered, l); + + int check = libre_redirect(uri, uri_filtered); + + if (check == 2) { + webkit_web_view_load_uri(view, uri_filtered); + } + } +} +void set_custom_style(WebKitWebView* view) +{ + if (CUSTOM_STYLE_ENABLED) { + char* style_js = malloc(STYLE_N + 1); + read_style_js(style_js); + webkit_web_view_evaluate_javascript(view, style_js, -1, NULL, "rosenrot-style-plugin", NULL, NULL, NULL); + free(style_js); + } +} +void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event, + GtkNotebook* notebook) +{ + switch (load_event) { + /* see + */ + case WEBKIT_LOAD_STARTED: + set_custom_style(self); + redirect_if_annoying(self, webkit_web_view_get_uri(self)); + break; + case WEBKIT_LOAD_REDIRECTED: + redirect_if_annoying(self, webkit_web_view_get_uri(self)); + break; + case WEBKIT_LOAD_COMMITTED: + redirect_if_annoying(self, webkit_web_view_get_uri(self)); + set_custom_style(self); + break; + case WEBKIT_LOAD_FINISHED: { + /* Add gtk tab title */ + const char* webpage_title = webkit_web_view_get_title(self); + const int max_length = 25; + char tab_title[max_length + 1]; + if (webpage_title != NULL) { + for (int i = 0; i < (max_length); i++) { + tab_title[i] = webpage_title[i]; + if (webpage_title[i] == '\0') { + break; + } + } + tab_title[max_length] = '\0'; + } + + gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self), + webpage_title == NULL ? "—" : tab_title); + // gtk_widget_hide(GTK_WIDGET(bar)); + } + } +} + +/* Create new tabs */ +void notebook_create_new_tab(GtkNotebook* notebook, const char* uri); +// handle_signal_create_new_tab is bound to a signal inside notebook_create_new_tab +// and itself calls notebook_create_new_tab +// therefore we need to do a forward declaration for one of them. +GtkWidget* handle_signal_create_new_tab(WebKitWebView* self, + WebKitNavigationAction* navigation_action, + GtkNotebook* notebook) +{ + if (num_tabs < MAX_NUM_TABS || num_tabs == 0) { + WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action); + const char* uri = webkit_uri_request_get_uri(uri_request); + printf("Creating new window: %s\n", uri); + notebook_create_new_tab(notebook, uri); + gtk_notebook_set_show_tabs(notebook, true); + return NULL; + } 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. + */ +} +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/110.0.0.0 Safari/537.36"); + // See: for some common user agents + } + 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("~/.config/rose/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) { + + WebKitWebView* view = create_new_webview(); + + g_signal_connect(view, "load_changed", G_CALLBACK(handle_signal_load_changed), notebook); + g_signal_connect(view, "create", G_CALLBACK(handle_signal_create_new_tab), notebook); + + int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL); + gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), true); + gtk_widget_show_all(GTK_WIDGET(window)); + gtk_widget_hide(GTK_WIDGET(bar.widget)); + load_uri(view, (uri) ? uri : HOME); + + set_custom_style(view); + + gtk_notebook_set_current_page(notebook, n); + gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-"); + webkit_web_view_set_zoom_level(view, ZOOM); + num_tabs += 1; + } else { + + webkit_web_view_evaluate_javascript(notebook_get_webview(notebook), "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL); + } +} + +/* Top bar */ +void show_bar(GtkNotebook* notebook) +{ + if (bar.entry_mode == _SEARCH) { + const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook)); + gtk_entry_set_placeholder_text(bar.line, "Search"); + gtk_entry_buffer_set_text(bar.line_text, url, strlen(url)); + gtk_widget_show(GTK_WIDGET(bar.widget)); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + } else if (bar.entry_mode == _HIDDEN) { + gtk_widget_hide(GTK_WIDGET(bar.widget)); + } else { + const char* search_text = webkit_find_controller_get_search_text( + webkit_web_view_get_find_controller(notebook_get_webview(notebook))); + + if (search_text != NULL) + gtk_entry_buffer_set_text(bar.line_text, search_text, strlen(search_text)); + + gtk_entry_set_placeholder_text(bar.line, "Find"); + gtk_widget_show(GTK_WIDGET(bar.widget)); + gtk_window_set_focus(window, GTK_WIDGET(bar.line)); + } +} +// Handle what happens when the user is on the bar and presses enter +void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook) +{ + if (bar.entry_mode == _SEARCH) + load_uri(notebook_get_webview(notebook), + gtk_entry_buffer_get_text(bar.line_text)); + else if (bar.entry_mode == _FIND) + webkit_find_controller_search( + webkit_web_view_get_find_controller(notebook_get_webview(notebook)), + gtk_entry_buffer_get_text(bar.line_text), + WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND, + G_MAXUINT); + + gtk_widget_hide(GTK_WIDGET(bar.widget)); +} + +/* Handle shortcuts */ +// Act when a particular shortcut is detected +int handle_shortcut(func id, GtkNotebook* notebook) +{ + static double zoom = ZOOM; + static bool is_fullscreen = 0; + + WebKitWebView* view = notebook_get_webview(notebook); + + switch (id) { + case goback: + webkit_web_view_go_back(view); + break; + case goforward: + webkit_web_view_go_forward(view); + break; + + case refresh: + webkit_web_view_reload(view); + break; + case refresh_force: + webkit_web_view_reload_bypass_cache(view); + break; + + case back_to_home: + load_uri(view, HOME); + break; + + case zoomin: + webkit_web_view_set_zoom_level(view, + (zoom += ZOOM_VAL)); + break; + + case zoomout: + webkit_web_view_set_zoom_level(view, + (zoom -= ZOOM_VAL)); + break; + + case zoom_reset: + webkit_web_view_set_zoom_level(view, + (zoom = ZOOM)); + break; + + case prev_tab:; // declarations aren't statements + // + int n = gtk_notebook_get_n_pages(notebook); + int k = gtk_notebook_get_current_page(notebook); + int l = (n + k - 1) % n; + gtk_notebook_set_current_page(notebook, l); + break; + + case next_tab:; + int m = gtk_notebook_get_n_pages(notebook); + int i = gtk_notebook_get_current_page(notebook); + int j = (i + 1) % m; + 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)) { + case 0: + exit(0); + break; + case 1: + gtk_notebook_set_show_tabs(notebook, false); + break; + } + + break; + + case toggle_fullscreen: + if (is_fullscreen) + gtk_window_unfullscreen(window); + else + gtk_window_fullscreen(window); + + is_fullscreen = !is_fullscreen; + break; + + case show_searchbar: + bar.entry_mode = _SEARCH; + show_bar(notebook); + break; + + case show_finder: + bar.entry_mode = _FIND; + show_bar(notebook); + break; + + case finder_next: + webkit_find_controller_search_next( + webkit_web_view_get_find_controller(view)); + break; + + case finder_prev: + webkit_find_controller_search_previous( + webkit_web_view_get_find_controller(view)); + break; + + case new_tab: + notebook_create_new_tab(notebook, NULL); + gtk_notebook_set_show_tabs(notebook, true); + bar.entry_mode = _SEARCH; + show_bar(notebook); + break; + + case hide_bar: + bar.entry_mode = _HIDDEN; + show_bar(notebook); + break; + + case prettify: { + if (READABILITY_ENABLED) { + char* readability_js = malloc(READABILITY_N + 1); + read_readability_js(readability_js); + webkit_web_view_evaluate_javascript(view, readability_js, -1, NULL, "rosenrot-readability-plugin", NULL, NULL, NULL); + free(readability_js); + } + break; + } + } + + return 1; +} +// Listen to key presses and call shortcuts if needed. +int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook) +{ + (void)self; + + guint event_keyval = 0; + gdk_event_get_keyval(event, &event_keyval); + GdkModifierType event_state = 0; + gdk_event_get_state(event, &event_state); + + int debug_shortcuts = 0; + if (debug_shortcuts) { + printf("Keypress state: %d\n", event_state); + if (event_state & GDK_CONTROL_MASK) { + printf("Keypress state is: CONTROL\n"); + } + printf("Keypress value: %d\n", event_keyval); + } + + for (int i = 0; i < sizeof(shortcut) / sizeof(shortcut[0]); i++) + if ((event_state & shortcut[i].mod || shortcut[i].mod == 0x0) && event_keyval == shortcut[i].key) + return handle_shortcut(shortcut[i].id, notebook); + /* + If I wanted to bind button presses, like the extra button in the mouse, + I would have to bind the button-press-event signal instead. + Some links in case I go down that road: + - + - + - https://docs.gtk.org/gdk3/struct.EventButton.html + */ + // This API is deprecated in GTK4 :( + return 0; +} + +int main(int argc, char** argv) +{ + /* Initialize GTK in general */ + gtk_init(NULL, NULL); // + g_object_set(gtk_settings_get_default(), GTK_SETTINGS_CONFIG_H, NULL); // + GtkCssProvider* css = gtk_css_provider_new(); + gtk_css_provider_load_from_path(css, "/usr/share/themes/rose/style.css", NULL); + 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); + 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)); + + // 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); + + 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)); + + /* Load first tab */ + char* first_uri = argc > 1 ? argv[1] : HOME; + notebook_create_new_tab(notebook, first_uri); + + /* Show to user */ + gtk_widget_show_all(GTK_WIDGET(window)); + gtk_widget_hide(GTK_WIDGET(bar.widget)); + + /* Deal with more tabs */ + if (argc > 2) { + gtk_notebook_set_show_tabs(notebook, true); + for (int i = 2; i < argc; i++) { + notebook_create_new_tab(notebook, argv[i]); + } + } + + gtk_main(); +} diff --git a/user-scripts/debian-12/install-with-dependencies.sh b/user-scripts/debian-12/install-with-dependencies.sh index 60e716e..aa68f09 100644 --- a/user-scripts/debian-12/install-with-dependencies.sh +++ b/user-scripts/debian-12/install-with-dependencies.sh @@ -21,5 +21,5 @@ sudo make install cd - # Debian desktop icon -chmod +x rose.desktop -sudo cp rose.desktop /usr/share/applications +chmod +x rosenrot.desktop +sudo cp rosenrot.desktop /usr/share/applications diff --git a/user-scripts/debian-12/rose.desktop b/user-scripts/debian-12/rose.desktop deleted file mode 100755 index c970b7a..0000000 --- a/user-scripts/debian-12/rose.desktop +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env xdg-open -[Desktop Entry] -Version=1.0 -Type=Application -Terminal=false -Exec=/bin/rose %u -Name=Rose -Comment=Minimalistic browser -Icon=/home/nuno/Documents/workspace/rosenrot/user-scripts/debian-12/rose-images/rose-desktop-icon.png diff --git a/user-scripts/debian-12/rosenrot.desktop b/user-scripts/debian-12/rosenrot.desktop new file mode 100755 index 0000000..c970b7a --- /dev/null +++ b/user-scripts/debian-12/rosenrot.desktop @@ -0,0 +1,9 @@ +#!/usr/bin/env xdg-open +[Desktop Entry] +Version=1.0 +Type=Application +Terminal=false +Exec=/bin/rose %u +Name=Rose +Comment=Minimalistic browser +Icon=/home/nuno/Documents/workspace/rosenrot/user-scripts/debian-12/rose-images/rose-desktop-icon.png diff --git a/user-scripts/ubuntu-20.04/README.md b/user-scripts/ubuntu-20.04/README.md index 5fcc6f4..c0070c9 100644 --- a/user-scripts/ubuntu-20.04/README.md +++ b/user-scripts/ubuntu-20.04/README.md @@ -1 +1 @@ -Code here uses the libwebkit2gtk-4.0 api. It is deprecated. See the debian version for how to use the -4.1 version instead. Package names might vary. +Code in this directory refers to a previous version of rosenrot which used the libwebkit2gtk-4.0 api. It is deprecated. See the debian folder for how to use the -4.1 version instead. Package names might vary. -- cgit v1.2.3