From da36745e98d6ab317f45c9f8331409775fb516b4 Mon Sep 17 00:00:00 2001 From: fenze Date: Tue, 10 May 2022 19:52:56 +0200 Subject: [PATCH] Provide config for keybinds --- config.h | 12 +++++++++++- keyconf.h | 17 +++++++++++++++++ rose.c | 7 +++++++ window.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 keyconf.h diff --git a/config.h b/config.h index 9abedf9..1566f0b 100644 --- a/config.h +++ b/config.h @@ -1,5 +1,15 @@ #pragma once +#include "keyconf.h" -static const char *cachedir = "/home/fenze/.cache/rose/"; static const int dark_mode = 1; +static const char *cachedir = "/home/fenze/.cache/rose/"; static const char *homepage = "https://duckduckgo.com"; + +#define MODKEY GDK_CONTROL_MASK + +static Key keys[] = { + { MODKEY, GDK_KEY_h, goback }, + { MODKEY, GDK_KEY_l, goforward }, + { MODKEY, GDK_KEY_y, copy_url }, + { MODKEY, GDK_KEY_p, paste_url } +}; diff --git a/keyconf.h b/keyconf.h new file mode 100644 index 0000000..a435972 --- /dev/null +++ b/keyconf.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +typedef struct { + unsigned modkey; + unsigned keycod; + unsigned funcid; +} Key; + +enum { + goback, + goforward, + copy_url, + paste_url +}; diff --git a/rose.c b/rose.c index 8fbba49..f0d63e0 100644 --- a/rose.c +++ b/rose.c @@ -12,6 +12,8 @@ static void run(GtkApplication *app) if (dark_mode) { g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", true, NULL); + GdkRGBA color = { 0, 0, 0, 1 }; + webkit_web_view_get_background_color(WEBKIT_WEB_VIEW(webview), &color); } rose_window_set_webview(window, webview); @@ -23,6 +25,11 @@ static void run(GtkApplication *app) int main(int argc, char **argv) { + if (argc == 2) { + homepage = argv[1]; + argv++; argc--; + } + GtkApplication *app = gtk_application_new("org.gtk.rose", G_APPLICATION_FLAGS_NONE); g_signal_connect(app, "activate", G_CALLBACK(run), NULL); g_application_run(G_APPLICATION(app), argc, argv); diff --git a/window.c b/window.c index b0ccfa9..167f006 100644 --- a/window.c +++ b/window.c @@ -3,6 +3,8 @@ #include "config.h" #include "webview.h" +#define LENGTH(x) (sizeof(x) / sizeof(x[0])) + struct _RoseWindow { GtkApplicationWindow parent_instance; @@ -19,26 +21,50 @@ enum { G_DEFINE_TYPE(RoseWindow, rose_window, GTK_TYPE_APPLICATION_WINDOW) +static void read_clipboard(GObject *object, + GAsyncResult *res, + gpointer webview) +{ + GdkDisplay *dpy = gdk_display_get_default(); + GdkClipboard *clipboard = gdk_display_get_clipboard(dpy); + webkit_web_view_load_uri( + WEBKIT_WEB_VIEW(webview), + gdk_clipboard_read_text_finish(clipboard, res, NULL) + ); +} + static gboolean key_press_callback(WebKitWebView *webview, guint keyval, guint keycode, GdkModifierType state) { - if (state & GDK_CONTROL_MASK) { - switch (keycode) { - case 43: { - webkit_web_view_go_back(webview); - } break; - case 46: { - webkit_web_view_go_forward(webview); - } break; - case 26: { - rose_webview_load_url(webview, homepage); - } break; + for (int i = 0; i < LENGTH(keys); i++) { + if (keys[i].modkey == GDK_CONTROL_MASK + && keys[i].keycod == keyval) { + switch (keys[i].funcid) { + case goback: + webkit_web_view_go_back(webview); + break; + case goforward: + webkit_web_view_go_forward(webview); + break; + case copy_url: { + GdkDisplay *dpy = gdk_display_get_default(); + gdk_clipboard_set_text( + gdk_display_get_clipboard(dpy), + webkit_web_view_get_uri(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, webview); + } break; + } } } - + } return GDK_EVENT_PROPAGATE; }