aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: d5321a04dfd851eb706d54e3715ed14bcb804ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* 
Copyright (c) 2026 Arslaan Pathan
This software is licensed under the ARPL. See LICENSE for details.
*/
#include <webkit2/webkit2.h>
#include <gtk/gtk.h>
#include "config.h"

/* struct so we can easily pass objects around. will actually use this in code later */
typedef struct {
	GtkWidget *window;
	GtkWidget *notebook;
	GtkWidget *cmdbar;
} Cinnamon;

/* 0 = normal mode, 1 = insert, 2 = passthrough */
static int mode = 0;

static void parse_keybind(const char *key, guint *keyval, GdkModifierType *mods) {
	char name[32];
	const char *start;
	const char *end;

	*mods = 0;
	if (key[0] != '<') {
		*keyval = gdk_keyval_from_name(key);
		return;
	}

	if (strstr(key, "S-")) *mods |= GDK_SHIFT_MASK;
	if (strstr(key, "C-")) *mods |= GDK_CONTROL_MASK;
	if (strstr(key, "A-")) *mods |= GDK_MOD1_MASK;

	/* everything after the last '-' and before '>' */
	start = strrchr(key, '-') + 1;
	end = strchr(key, '>');
	snprintf(name, sizeof(name), "%.*s", (int)(end - start), start);
	*keyval = gdk_keyval_from_name(name);
}

static void on_title_changed(WebKitWebView *webview, GParamSpec *pspec, GtkNotebook *notebook) {
	const char *title = webkit_web_view_get_title(webview);
	int page = gtk_notebook_page_num(notebook, GTK_WIDGET(webview));
	gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(webview), title ? title : "New Tab");
}

static gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data) {
	if (event->keyval == GDK_KEY_colon) {
		/* we dont have command bar yet so pretend we did something and printf */
		printf("this should show the command bar\n");
		return TRUE;
	}

	for (int i = 0; i < sizeof(keybinds) / sizeof(keybinds[0]); i++) {
		guint keyval;
		GdkModifierType mods;
		parse_keybind(keybinds[i].key, &keyval, &mods);
		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 */
			printf("%s\n", keybinds[i].command);
			return TRUE;
		}
	}

	/* event not consumed pass to webview */
	return FALSE;
}

int main(int argc, char *argv[]) {
#ifndef ObamaPrism
	return 1;
#endif
	gtk_init(&argc, &argv);
    
	Cinnamon cinnamon = {0};

	/* Create a new window and set default size */
	cinnamon.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	/* Size constants defined in config.h */
	gtk_window_set_default_size(GTK_WINDOW(cinnamon.window), WINDOW_WIDTH, WINDOW_HEIGHT);
	g_signal_connect(cinnamon.window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	cinnamon.notebook = gtk_notebook_new();
	gtk_container_add(GTK_CONTAINER(cinnamon.window), cinnamon.notebook);


	/* 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);

	/* 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_main();

	return 0;
}