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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*
Copyright (c) 2026 Arslaan Pathan
This software is licensed under the ARPL. See LICENSE for details.
*/
#include <webkit2/webkit2.h>
#include <gtk/gtk.h>
/* forward declare BEFORE including config.h so that config.h can reference them */
typedef struct {
GtkWidget *window;
GtkWidget *notebook;
GtkWidget *cmdbar;
GtkWidget *indicator;
WebKitWebContext *web_context;
} Cinnamon;
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 */
static int mode = 0;
bool cmdbar_focused = false;
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 '>' */
/* this means we can't have things like <S-A-z> for example, but too bad so sad. cannot be bothered to implement that at the moment */
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_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);
return TRUE;
}
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;
if (mode == 1 || mode == 2) {
if (mode == 1 && event->keyval == GDK_KEY_Escape) {
set_mode(cinnamon, 0);
if (cmdbar_focused) {
gtk_widget_hide(cinnamon->cmdbar);
cmdbar_focused = false;
}
}
if (mode == 2 && event->keyval == GDK_KEY_Escape && (event->state & relevant) == GDK_SHIFT_MASK) {
set_mode(cinnamon, 0);
}
}
else {
if (event->keyval == GDK_KEY_colon) {
gtk_widget_show(cinnamon->cmdbar);
gtk_widget_grab_focus(cinnamon->cmdbar);
cmdbar_focused = true;
set_mode(cinnamon, 1);
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) {
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 set_mode(Cinnamon* cinnamon, int new_mode) {
mode = new_mode;
switch (new_mode) {
case 0: gtk_label_set_text(GTK_LABEL(cinnamon->indicator), "-- NORMAL --"); break;
case 1: gtk_label_set_text(GTK_LABEL(cinnamon->indicator), "-- INSERT --"); break;
case 2: gtk_label_set_text(GTK_LABEL(cinnamon->indicator), "-- PASSTHROUGH --"); break;
}
}
void tabopen(Cinnamon* cinnamon) {
GtkWidget *webview = webkit_web_view_new_with_context(cinnamon->web_context);
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 and USERAGENT defined in config.h */
webkit_web_view_set_settings(WEBKIT_WEB_VIEW(webview), g_object_new(WEBKIT_TYPE_SETTINGS, "user-agent", USERAGENT, NULL));
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);
}
void tabclose(Cinnamon* cinnamon) {
int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(cinnamon->notebook));
/* dont close last tab */
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(cinnamon->notebook)) > 1) {
gtk_notebook_remove_page(GTK_NOTEBOOK(cinnamon->notebook), page);
} else {
#ifdef NO_QUIT_ON_CLOSE_LAST_TAB
tabopen(cinnamon);
gtk_notebook_remove_page(GTK_NOTEBOOK(cinnamon->notebook), page);
return;
#endif
gtk_main_quit();
}
}
int main(int argc, char *argv[]) {
#ifndef CINNAMON_ENABLED
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);
/* Create a vertical GtkBox for layout stuff */
GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(cinnamon.window), vbox);
cinnamon.notebook = gtk_notebook_new();
// gtk_container_add(GTK_CONTAINER(cinnamon.window), cinnamon.notebook);
gtk_box_pack_start(GTK_BOX(vbox), cinnamon.notebook, TRUE, TRUE, 0);
/* initialize web context to save cookies */
WebKitWebsiteDataManager *data_manager = webkit_website_data_manager_new("base-data-directory", g_build_filename(g_get_user_data_dir(), "cinnamon", NULL),"base-cache-directory", g_build_filename(g_get_user_cache_dir(), "cinnamon", NULL), NULL);
cinnamon.web_context = webkit_web_context_new_with_website_data_manager(data_manager);
WebKitCookieManager *cookie_manager = webkit_web_context_get_cookie_manager(cinnamon.web_context);
webkit_cookie_manager_set_persistent_storage(cookie_manager, g_build_filename(g_get_user_data_dir(), "cinnamon", "cookies.sqlite", NULL), WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
/* Create initial tab */
tabopen(&cinnamon);
/* Indicator label */
cinnamon.indicator = gtk_label_new("-- NORMAL --");
gtk_box_pack_end(GTK_BOX(vbox), cinnamon.indicator, FALSE, FALSE, 0);
/* Create cmdbar */
cinnamon.cmdbar = gtk_entry_new();
gtk_box_pack_end(GTK_BOX(vbox), cinnamon.cmdbar, FALSE, FALSE, 0);
/* 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
gtk_notebook_set_show_tabs(GTK_NOTEBOOK(cinnamon.notebook), FALSE);
#endif
user_start(&cinnamon);
gtk_main();
return 0;
}
|