diff options
| -rw-r--r-- | config.def.h | 70 | ||||
| -rw-r--r-- | main.c | 44 |
2 files changed, 79 insertions, 35 deletions
diff --git a/config.def.h b/config.def.h index 485c3b5..6bda445 100644 --- a/config.def.h +++ b/config.def.h @@ -4,10 +4,10 @@ #ifndef CONFIG_H #define CONFIG_H -/* forward declarations */ -int mode; +/* forward declarations from main.c */ +extern int mode; /* Cinnamon is declared in main.c before including config.h. Yes, I know this code is held together by hopes and dreams, but good enough. */ -void tabopen(Cinnamon* cinnamon); +extern void tabopen(Cinnamon* cinnamon); /* default window width/height, can be resized after. */ #define WINDOW_WIDTH 1024 @@ -17,32 +17,60 @@ void tabopen(Cinnamon* cinnamon); if its not there the browser will exit with code 1 do not ask any questions */ #define ObamaPrism +/* if a keybind is unbound, still don't send when not in insert mode */ +#define NO_SEND_UNBOUND_KEYBINDS /* Keybind definitions */ -typedef struct { - const char* key; - void (*fptr)(Cinnamon*); -} Keybind; - -static void _insmode(Cinnamon* cinnamon) { +static void _kbd_insmode(Cinnamon* cinnamon, void* arg) { mode = 1; } +static void _kbd_tabopen(Cinnamon* cinnamon, void* arg) { + /* wrapper to satisfy the arg + keep keybinds isolated */ + tabopen(cinnamon); +} + +static void _kbd_scroll(Cinnamon* cinnamon, void* arg) { + GtkWidget *webview = gtk_notebook_get_nth_page(GTK_NOTEBOOK(cinnamon->notebook), gtk_notebook_get_current_page(GTK_NOTEBOOK(cinnamon->notebook))); + const char* js; + if (strcmp((const char*)arg, "left") == 0) { + js = "window.scrollBy(-100, 0);"; + } else if (strcmp((const char*)arg, "right") == 0) { + js = "window.scrollBy(100, 0);"; + } else if (strcmp((const char*)arg, "up") == 0) { + js = "window.scrollBy(0, -100);"; + } else if (strcmp((const char*)arg, "down") == 0) { + js = "window.scrollBy(0, 100);"; + } else { + js = "alert(\"an unexpected error occurred in the scroll handler\");"; + } + + webkit_web_view_evaluate_javascript(WEBKIT_WEB_VIEW(webview), js, -1, NULL, NULL, NULL, NULL, NULL); +} + +static void _kbd_tabsel(Cinnamon* cinnamon, void* arg) { + gtk_notebook_set_current_page(GTK_NOTEBOOK(cinnamon->notebook), (int)(intptr_t)arg); +} + static const Keybind keybinds[] = { - { "<S-o>", &tabopen }, + { "<S-o>", &_kbd_tabopen, NULL }, // { "d", ":tab_close" }, // { "o", ":commandline_show \":open\"" }, - { "i", &_insmode }, - // { "<A-1>", ":tab_select 1" }, - // { "<A-2>", ":tab_select 2" }, - // { "<A-3>", ":tab_select 3" }, - // { "<A-4>", ":tab_select 4" }, - // { "<A-5>", ":tab_select 5" }, - // { "<A-6>", ":tab_select 6" }, - // { "<A-7>", ":tab_select 7" }, - // { "<A-8>", ":tab_select 8" }, - // { "<A-9>", ":tab_select 9" }, - // { "<A-0>", ":tab_select 10" }, + { "i", &_kbd_insmode, NULL }, + { "j", &_kbd_scroll, "down" }, + { "k", &_kbd_scroll, "up" }, + { "h", &_kbd_scroll, "left" }, + { "l", &_kbd_scroll, "right" }, + { "<A-1>", &_kbd_tabsel, (void*)(intptr_t)0 }, + { "<A-2>", &_kbd_tabsel, (void*)(intptr_t)1 }, + { "<A-3>", &_kbd_tabsel, (void*)(intptr_t)2 }, + { "<A-4>", &_kbd_tabsel, (void*)(intptr_t)3 }, + { "<A-5>", &_kbd_tabsel, (void*)(intptr_t)4 }, + { "<A-6>", &_kbd_tabsel, (void*)(intptr_t)5 }, + { "<A-7>", &_kbd_tabsel, (void*)(intptr_t)6 }, + { "<A-8>", &_kbd_tabsel, (void*)(intptr_t)7 }, + { "<A-9>", &_kbd_tabsel, (void*)(intptr_t)8 }, + { "<A-0>", &_kbd_tabsel, (void*)(intptr_t)-1 }, }; #endif @@ -4,23 +4,31 @@ This software is licensed under the ARPL. See LICENSE for details. */ #include <webkit2/webkit2.h> #include <gtk/gtk.h> -/* declare BEFORE including config.h so that config.h can reference it */ + +/* forward declare BEFORE including config.h so that config.h can reference them */ typedef struct { GtkWidget *window; GtkWidget *notebook; GtkWidget *cmdbar; } Cinnamon; -#include "config.h" +typedef struct { + const char* key; + void (*fptr)(Cinnamon*, void* arg); + void* arg; +} Keybind; + +typedef struct { + const char* name; + void (*fptr)(Cinnamon*, const char *args); +} Command; + +#include "config.h" /* 0 = normal mode, 1 = insert, 2 = passthrough */ int mode = 0; bool cmdbar_focused = false; -void tabopen(Cinnamon* cinnamon) { - printf("this should open a tab, will implement later\n"); -} - static void parse_keybind(const char *key, guint *keyval, GdkModifierType *mods) { char name[32]; const char *start; @@ -76,16 +84,30 @@ static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer dat GdkModifierType relevant = GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK; if (gdk_keyval_to_lower(event->keyval) == keyval && (event->state & relevant) == mods) { /* we dont know what to do yet, printf */ - keybinds[i].fptr(cinnamon); + keybinds[i].fptr(cinnamon, keybinds[i].arg); return TRUE; } } +#ifdef NO_SEND_UNBOUND_KEYBINDS + /* make sure that outside of insert/passthrough, we still don't send unbound keybinds to the webview */ + return TRUE; +#endif } /* event not consumed pass to webview */ return FALSE; } +void tabopen(Cinnamon* cinnamon) { + GtkWidget *webview = webkit_web_view_new(); + gtk_notebook_append_page(GTK_NOTEBOOK(cinnamon->notebook), webview, gtk_label_new("New Tab")); + g_signal_connect(webview, "notify::title", G_CALLBACK(on_title_changed), cinnamon->notebook); + /* HOMEPAGE defined in config.h */ + webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), HOMEPAGE); + gtk_widget_show_all(cinnamon->notebook); + gtk_notebook_set_current_page(GTK_NOTEBOOK(cinnamon->notebook), -1); +} + int main(int argc, char *argv[]) { #ifndef ObamaPrism return 1; @@ -109,10 +131,7 @@ int main(int argc, char *argv[]) { gtk_box_pack_start(GTK_BOX(vbox), cinnamon.notebook, TRUE, TRUE, 0); /* Create initial tab */ - GtkWidget *webview = webkit_web_view_new(); - gtk_notebook_append_page(GTK_NOTEBOOK(cinnamon.notebook), webview, gtk_label_new("New Tab")); - /* HOMEPAGE defined in config.h */ - webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), HOMEPAGE); + tabopen(&cinnamon); /* Create cmdbar */ /* im sleepy so just add a placeholder commented-out pack for future reference */ @@ -122,9 +141,6 @@ int main(int argc, char *argv[]) { /* handle keypresses */ g_signal_connect(cinnamon.window, "key-press-event", G_CALLBACK(on_key_press), &cinnamon); - /* handle tab changes */ - g_signal_connect(webview, "notify::title", G_CALLBACK(on_title_changed), cinnamon.notebook); - gtk_widget_show_all(cinnamon.window); gtk_widget_hide(cinnamon.cmdbar); gtk_main(); |
