From 88efc157049e0eae532a820e8da8aadc5c4e1cd1 Mon Sep 17 00:00:00 2001 From: fenze Date: Fri, 13 May 2022 20:37:50 +0200 Subject: [PATCH] add history support --- config.h | 13 +++++++------ keyconf.h | 4 ++-- scripts/dmenu_rose.sh | 17 ++++++++++++++--- webview.c | 24 ++++++++++++++++++++++++ webview.h | 3 +++ window.c | 28 ++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 11 deletions(-) diff --git a/config.h b/config.h index f5f80b5..0934854 100644 --- a/config.h +++ b/config.h @@ -1,14 +1,14 @@ #include "keyconf.h" OPTIONS { - [CACHE] = DEFAULT, - [HOMEPAGE] = DEFAULT, + [CACHE] = DEFAULT, + [HOMEPAGE] = DEFAULT, }; APPERANCE { - [HEIGHT] = 600, - [WIDTH] = 800, - [DARKMODE] = true + [HEIGHT] = DEFAULT, + [WIDTH] = DEFAULT, + [DARKMODE] = TRUE }; KEYBINDS { @@ -28,5 +28,6 @@ KEYBINDS { { MODSFT, GDK_KEY_N, findprev }, { MODKEY, GDK_KEY_r, reload }, { MODSFT, GDK_KEY_R, reloadforce }, - { NOMODK, GDK_KEY_F11, fullscreen } + { NOMODK, GDK_KEY_F11, fullscreen }, + { MODSFT, GDK_KEY_H, history } }; diff --git a/keyconf.h b/keyconf.h index b3de8ea..36adfba 100644 --- a/keyconf.h +++ b/keyconf.h @@ -11,7 +11,7 @@ #define CACHE 0 #define HOMEPAGE 1 -#define DEFAULT NULL +#define DEFAULT 0 #define HEIGHT 0 #define WIDTH 1 @@ -46,5 +46,5 @@ enum { up, reload, reloadforce, - LAST_FUNC + history }; diff --git a/scripts/dmenu_rose.sh b/scripts/dmenu_rose.sh index dc0d44f..8cd60e0 100755 --- a/scripts/dmenu_rose.sh +++ b/scripts/dmenu_rose.sh @@ -21,7 +21,7 @@ search() [ -z "$SEARCH" ] && exit - ROSE_GO=$(xprop -id "$CURRENT_ID" | grep _ROSE_URI) + ROSE_GO=$(xprop -id "$CURRENT_ID" | grep rose) [ -z "$1" ] || ROSE_GO='' @@ -31,7 +31,7 @@ search() exit } || { FULL_URL=$(printf "$BOOKMARKS" | grep -w "$SEARCH" | sed 's/ /\./g') - xprop -id "$CURRENT_ID" -f "_ROSE_GO" 8u -set "_ROSE_GO" $FULL_URL + xprop -id "$CURRENT_ID" -f "_ROSE_GO" 8u -set "_ROSE_GO" https://$FULL_URL exit } } @@ -48,11 +48,21 @@ find() { FIND=$(printf "" | dmenu -p Find:) CURRENT_ID=$(xdotool getactivewindow) - ROSE_FIND=$(xprop -id "$CURRENT_ID" | grep _ROSE_URI) + ROSE_FIND=$(xprop -id "$CURRENT_ID" | grep rose) [ -z "$ROSE_FIND" ] || xprop -id "$CURRENT_ID" -f "_ROSE_FIND" 8u -set "_ROSE_FIND" "$FIND" } +history() +{ + CHOOSE=$(tac ~/.cache/rose/history | dmenu -p History:); + + CURRENT_ID=$(xdotool getactivewindow) + ROSE_GO=$(xprop -id "$CURRENT_ID" | grep rose) + + [ -z "$ROSE_GO" ] || xprop -id "$CURRENT_ID" -f "_ROSE_GO" 8u -set "_ROSE_GO" "$CHOOSE" +} + [ -z "$1" ] && search || { INSTALL_PATH=/usr/local/bin/dmenu_rose case "$1" in @@ -61,5 +71,6 @@ find() add_bookmark) echo "$2" >> $BOOKMARKS_PATH;; new_window) search new;; find) find;; + history) history; esac } diff --git a/webview.c b/webview.c index bb976df..89abcd5 100644 --- a/webview.c +++ b/webview.c @@ -106,6 +106,30 @@ void rose_webview_load_url(WebKitWebView *webview, const char *url) setatom(AtomUri, url); } +void rose_load_changed_callback(WebKitWebView *webview, + WebKitLoadEvent event) +{ + if (event == WEBKIT_LOAD_FINISHED) { + const char *uri = webkit_web_view_get_uri(webview); + char *cookiefile = calloc(1, sizeof(char) * (strlen(options[CACHE]) + 32) + 1); + sprintf(cookiefile, "%s/history", options[CACHE]); + FILE *f = fopen(cookiefile, "r"); + + if (!f) { + fclose(f); + FILE *f = fopen(cookiefile, "w"); + fclose(f); + } else { + fclose(f); + } + + FILE *cookie = fopen(cookiefile, "a"); + fprintf(f, "%s\n", uri); + fclose(cookie); + free(cookiefile); + } +} + static void rose_download(const char* uri) { char *cmd = calloc(1, sizeof(char) * strlen(uri) + 1); diff --git a/webview.h b/webview.h index 21c9aad..983e57a 100644 --- a/webview.h +++ b/webview.h @@ -25,4 +25,7 @@ RoseWebViewNavigationFlags rose_webview_get_navigation_flags(RoseWebView *webvie void rose_webview_go_back(RoseWebView *window); void rose_webview_go_forward(RoseWebView *window); +void rose_load_changed_callback(WebKitWebView *webview, + WebKitLoadEvent event); + const char* rose_webview_get_address(RoseWebView *webview); diff --git a/window.c b/window.c index 8fb401e..184a688 100644 --- a/window.c +++ b/window.c @@ -162,6 +162,25 @@ static gboolean key_press_callback(RoseWindow *window, case reloadforce: webkit_web_view_reload_bypass_cache(window->webview); break; + + case history: { + int id = fork(); + if (id == 0) { + close(spair[0]); + close(spair[1]); + setsid(); + char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose\thistory", NULL}; + execvp("/bin/sh", argument_list); + perror(" failed"); + exit(1); + } else { + wait(&id); + char *uri; + if (strcmp((uri = (char*)getatom(AtomGo)), "")) + rose_webview_load_url(window->webview, uri); + } + } break; + } } } @@ -202,8 +221,17 @@ guint rose_window_show(GtkApplication *app, RoseWindow *window, const char *url) g_signal_connect(G_OBJECT(window->webview), "web-process-terminated", G_CALLBACK(destroy), window); + g_signal_connect(G_OBJECT(window->webview), "load-changed", + G_CALLBACK(rose_load_changed_callback), window); + if (url) { rose_webview_load_url(WEBKIT_WEB_VIEW(webview), url); + setatom(AtomUri, url); + } + + if (!(appearance[WIDTH] && appearance[HEIGHT])) { + appearance[WIDTH] = 1280; + appearance[HEIGHT] = 720; } gtk_window_set_default_size(GTK_WINDOW(w), appearance[WIDTH], appearance[HEIGHT]);