From 9eb0c2207fa0bad399eb47d79a888c9b8bf7e66d Mon Sep 17 00:00:00 2001 From: fenze Date: Fri, 13 May 2022 12:30:15 +0200 Subject: [PATCH] added find keybinds --- config.h | 3 +++ keyconf.h | 5 ++++- makefile | 6 +++--- rose.c | 6 ++---- rose.h | 1 + webview.c | 1 + webview.h | 2 ++ window.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 8 files changed, 64 insertions(+), 14 deletions(-) diff --git a/config.h b/config.h index b8f5e23..4be78eb 100644 --- a/config.h +++ b/config.h @@ -18,5 +18,8 @@ static Key keys[] = { { MODKEY, GDK_KEY_i, inspector }, { MODKEY, GDK_KEY_j, down }, { MODKEY, GDK_KEY_k, up }, + { MODKEY, GDK_KEY_f, find }, + { MODKEY, GDK_KEY_n, findnext }, + { MODSHIFT, GDK_KEY_N, findprev }, { 0, GDK_KEY_F11, fullscreen } }; diff --git a/keyconf.h b/keyconf.h index a0c6e61..272b6c9 100644 --- a/keyconf.h +++ b/keyconf.h @@ -3,7 +3,7 @@ #include #include -#define SHIFT GDK_SHIFT_MASK +#define MODSHIFT 5 typedef struct { unsigned modkey; @@ -19,6 +19,9 @@ enum { fullscreen, inspector, search, + find, + findnext, + findprev, zoomin, zoomout, down, diff --git a/makefile b/makefile index 9d1f1fe..f6b5d13 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ CC = clang -CFLAGS = `pkg-config --cflags gtk4 webkit2gtk-5.0 x11 ` -LIBS = `pkg-config --libs gtk4 webkit2gtk-5.0 x11` +CFLAGS = `pkg-config --cflags gtk4 webkit2gtk-5.0 x11 gstreamer-gl-1.0` +LIBS = `pkg-config --libs gtk4 webkit2gtk-5.0 x11 gstreamer-gl-1.0` OPTIONS = -Dgtk_doc=false -Dintrospection=false \ -DG_DISABLE_CAST_CHECKS \ -Dcloudproviders=false \ @@ -22,7 +22,7 @@ all: strip ./rose install: all - cp -uf ./rose /usr/local/bin/rose + su -c "cp -uf ./rose /usr/local/bin/rose" clean: rm -f rose compile_flags.txt diff --git a/rose.c b/rose.c index f73bcce..ad8fbf5 100644 --- a/rose.c +++ b/rose.c @@ -5,8 +5,7 @@ guint xid; -void -setatom(int a, const char *v) +void setatom(int a, const char *v) { XChangeProperty(dpy, xid, atoms[a], atoms[AtomUTF8], 8, PropModeReplace, @@ -14,8 +13,7 @@ setatom(int a, const char *v) XSync(dpy, False); } -const char * -getatom(int a) +const char* getatom(int a) { static char buf[BUFSIZ]; Atom adummy; diff --git a/rose.h b/rose.h index 8f29f0b..2c0c48b 100644 --- a/rose.h +++ b/rose.h @@ -19,4 +19,5 @@ enum { AtomFind, AtomGo, AtomUri, AtomUTF8, AtomLast }; static Atom atoms[AtomLast]; static Display *dpy; const char* getatom(int a); +void setatom(int a, const char *v); static int spair[2]; diff --git a/webview.c b/webview.c index 5590cf0..9167e5b 100644 --- a/webview.c +++ b/webview.c @@ -104,6 +104,7 @@ static void rose_webview_init(RoseWebView *webview) void rose_webview_load_url(WebKitWebView *webview, const char *url) { webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), url); + setatom(AtomUri, url); } GtkWidget* rose_webview_new() diff --git a/webview.h b/webview.h index 697ea19..21c9aad 100644 --- a/webview.h +++ b/webview.h @@ -1,5 +1,7 @@ #pragma once +#include "rose.h" + #include #include diff --git a/window.c b/window.c index c2f1552..7610486 100644 --- a/window.c +++ b/window.c @@ -12,6 +12,7 @@ struct _RoseWindow { GtkWindow *window; WebKitWebView *webview; WebKitWebInspector *inspector; + WebKitFindOptions *findopts; GHashTable *action_groups; GHashTable *action_labels; }; @@ -40,9 +41,6 @@ static gboolean key_press_callback(RoseWindow *window, guint keycode, GdkModifierType state) { - if (!state) - return GDK_EVENT_PROPAGATE; - for (int i = 0; i < LENGTH(keys); i++) { if (keys[i].modkey == state && keys[i].keycod == keyval) { @@ -50,9 +48,11 @@ static gboolean key_press_callback(RoseWindow *window, case goback: webkit_web_view_go_back(window->webview); break; + case goforward: webkit_web_view_go_forward(window->webview); break; + case copy_url: { GdkDisplay *dpy = gdk_display_get_default(); gdk_clipboard_set_text( @@ -60,17 +60,20 @@ static gboolean key_press_callback(RoseWindow *window, webkit_web_view_get_uri(window->webview) ); } break; + case paste_url: { GdkDisplay *dpy = gdk_display_get_default(); GdkClipboard *clipboard = gdk_display_get_clipboard(dpy); gdk_clipboard_read_text_async(clipboard, NULL, read_clipboard, window->webview); } break; + case fullscreen: if (gtk_window_is_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview))))) gtk_window_unfullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview)))); else gtk_window_fullscreen(GTK_WINDOW(gtk_widget_get_root(GTK_WIDGET(window->webview)))); break; + case search: { int id = fork(); if (id == 0) { @@ -79,23 +82,59 @@ static gboolean key_press_callback(RoseWindow *window, close(spair[0]); close(spair[1]); setsid(); - char* argument_list[] = { "/bin/sh", "-c", "surf-open", NULL}; + char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose", NULL}; execvp("/bin/sh", argument_list); perror(" failed"); exit(1); } else { wait(&id); - webkit_web_view_load_uri(window->webview, getatom(AtomGo)); + rose_webview_load_url(window->webview, getatom(AtomGo)); } - } + } break; + case find: { + + int id = fork(); + if (id == 0) { + if (dpy) + close(ConnectionNumber(dpy)); + close(spair[0]); + close(spair[1]); + setsid(); + char* argument_list[] = { "/bin/sh", "-c", "dmenu_rose\tfind", NULL}; + execvp("/bin/sh", argument_list); + perror(" failed"); + exit(1); + } else { + wait(&id); + WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview); + webkit_find_controller_search( + finder, + getatom(AtomFind), + WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | + WEBKIT_FIND_OPTIONS_WRAP_AROUND, G_MAXUINT); + } + } break; + + case findnext: { + WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview); + webkit_find_controller_search_next(finder); + } break; + + case findprev: { + WebKitFindController *finder = webkit_web_view_get_find_controller(window->webview); + webkit_find_controller_search_previous(finder); + } break; + case zoomin: zoom += 0.1; webkit_web_view_set_zoom_level(window->webview, zoom); break; + case zoomout: zoom -= 0.1; webkit_web_view_set_zoom_level(window->webview, zoom); break; + case inspector: window->inspector = webkit_web_view_get_inspector(window->webview); if (webkit_web_inspector_is_attached(window->inspector)) @@ -104,9 +143,11 @@ static gboolean key_press_callback(RoseWindow *window, webkit_web_inspector_show(window->inspector); return GDK_EVENT_STOP; break; + case up: webkit_web_view_run_javascript(window->webview, "window.scrollBy(0,-100);", NULL, NULL, NULL); break; + case down: webkit_web_view_run_javascript(window->webview, "window.scrollBy(0,100);", NULL, NULL, NULL); break; @@ -114,6 +155,7 @@ static gboolean key_press_callback(RoseWindow *window, } } + return GDK_EVENT_PROPAGATE; }