diff options
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | config.def.h | 34 | ||||
| -rw-r--r-- | main.c | 34 |
3 files changed, 69 insertions, 1 deletions
@@ -12,6 +12,8 @@ Linux, BSD (Windows/Mac are untested, try at your own risk) ## Dependencies +**Notice:** On some systems, you may need GStreamer plugins (e.g. gst-plugins-pulse/gst-plugins-pipewire) for some websites to work. Please refer to the correct packages baesd on your distro. + - webkit2gtk-4.1 - gtk3 - clang diff --git a/config.def.h b/config.def.h index 587e85b..3e2b792 100644 --- a/config.def.h +++ b/config.def.h @@ -18,6 +18,8 @@ extern void set_mode(Cinnamon* cinnamon, int new_mode); /* default window width/height, can be resized after. */ #define WINDOW_WIDTH 1024 #define WINDOW_HEIGHT 768 +/* searchengine/homepage */ +#define SEARCH_ENGINE "https://duckduckgo.com/?q=" #define HOMEPAGE "https://start.duckduckgo.com" /* if a keybind is unbound, still don't send when not in insert mode */ #define NO_SEND_UNBOUND_KEYBINDS @@ -100,4 +102,36 @@ static const Keybind keybinds[] = { { "<A-0>", &_kbd_tabsel, (void*)(intptr_t)-1 }, }; +/* Command definitions */ + +/* as mentioned above, prefixes like _cmd and _kbd are for good practice but in your modified configs you can do what you want. i don't care, i just make the software, do what you want with your config */ +static void _cmd_open(Cinnamon* cinnamon, const char* args) { + GtkWidget *webview = gtk_notebook_get_nth_page(GTK_NOTEBOOK(cinnamon->notebook), gtk_notebook_get_current_page(GTK_NOTEBOOK(cinnamon->notebook))); + + char uri[2048]; + if (strncmp(args, "http://", 7) == 0 || strncmp(args, "https://", 8) == 0) { + snprintf(uri, sizeof(uri), "%s", args); + } else if (strchr(args, '.') && !strchr(args, ' ')) { + /* has a dot and no spaces, probably a domain */ + /* if it's not, oh well, too bad so sad */ + snprintf(uri, sizeof(uri), "https://%s", args); + } else { + /* urlencode magic */ + char encoded[1024]; + int j = 0; + for (int i = 0; args[i] && j < (int)sizeof(encoded)-1; i++) { + if (args[i] == ' ') encoded[j++] = '+'; + else encoded[j++] = args[i]; + } + encoded[j] = '\0'; + snprintf(uri, sizeof(uri), SEARCH_ENGINE "%s", encoded); + } + + webkit_web_view_load_uri(WEBKIT_WEB_VIEW(webview), uri); +} + +static const Command commands[] = { + { "open", &_cmd_open } +}; + #endif @@ -59,6 +59,35 @@ static void on_title_changed(WebKitWebView *webview, GParamSpec *pspec, GtkNoteb gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(webview), title ? title : "New Tab"); } +static gboolean on_cmdbar_activate(GtkEntry *entry, gpointer data) { + Cinnamon* cinnamon = (Cinnamon*)data; + const char *input = gtk_entry_get_text(entry); + + char cmd[64]; + const char *args = ""; + const char *space = strchr(input, ' '); + + if (space) { + snprintf(cmd, sizeof(cmd), "%.*s", (int)(space - input), input); + args = space + 1; + } else { + snprintf(cmd, sizeof(cmd), "%s", input); + } + + /* commands array defined in config.h */ + for (int i = 0; i < sizeof(commands) / sizeof(commands[0]); i++) { + if (strcmp(commands[i].name, cmd) == 0) { + commands[i].fptr(cinnamon, args); + break; + } + } + + gtk_entry_set_text(entry, ""); + gtk_widget_hide(cinnamon->cmdbar); + cmdbar_focused = false; + set_mode(cinnamon, 0); +} + static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) { Cinnamon* cinnamon = (Cinnamon*)data; GdkModifierType relevant = GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK; @@ -83,12 +112,12 @@ static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer dat return TRUE; } + /* keybinds array defined in config.h */ for (int i = 0; i < sizeof(keybinds) / sizeof(keybinds[0]); i++) { guint keyval; GdkModifierType mods; parse_keybind(keybinds[i].key, &keyval, &mods); 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].arg); return TRUE; } @@ -174,6 +203,9 @@ int main(int argc, char *argv[]) { /* handle keypresses */ g_signal_connect(cinnamon.window, "key-press-event", G_CALLBACK(on_key_press), &cinnamon); + /* handle commands from cmdbar */ + g_signal_connect(cinnamon.cmdbar, "activate", G_CALLBACK(on_cmdbar_activate), &cinnamon); + gtk_widget_show_all(cinnamon.window); gtk_widget_hide(cinnamon.cmdbar); #ifdef HIDE_TAB_BAR |
