aboutsummaryrefslogtreecommitdiff
path: root/rosenrot.c
blob: c6e89135cd10716ab973bd18824ed32b2240fe25 (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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <gdk/gdk.h>
#include <stdlib.h>
#include <string.h>
#include <webkit2/webkit2.h>

// User config
#define WIDTH 1920 // 960 for half-width, 1920 for full width
#define HEIGHT 1080
#define BAR_SIZE 1000
#define SEARCH "https://lite.duckduckgo.com/html/?q=%s"
#define HOME "https://lite.duckduckgo.com/html"

// Minimal niceties
#define ZOOM 1 /* Starting zoom level.*/
#define ZOOM_VAL .1 /* Zooming value in zoomin/zoomout functions */
#define MAX_NUM_TABS 8

// Webkit settings
// See: https://webkitgtk.org/reference/webkit2gtk/stable/class.Settings.html
#define WEBKIT_DEFAULT_SETTINGS                                                 \
    "enable-back-forward-navigation-gestures", 1, "enable-developer-extras", 1, \
        "enable-smooth-scrolling", 0,                                           \
        "default-charset", "utf-8"

/* CACHE */
#define DATA_DIR "/home/nuno/.cache/rosenrot"
#define DATA_MANAGER_OPTS "base-cache-directory", DATA_DIR, "base-data-directory", DATA_DIR

// GTK
#define GTK_SETTINGS_CONFIG_H "gtk-application-prefer-dark-theme", 0, "gtk-enable-animations", 0
#define KEY(x) GDK_KEY_##x
#define SFT 1 << 0
#define CTRL 1 << 2
#define ALT 1 << 3

/* Global declarations */
static GtkNotebook* notebook;
static GtkWindow* window;
static struct {
    GtkHeaderBar* widget;
    GtkEntry* line;
    GtkEntryBuffer* line_text;
    enum { _SEARCH,
        _FIND,
        _HIDDEN } entry_mode;
} bar;
static int num_tabs = 0;

// Forward declarations
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri);

/* Utils */
WebKitWebView* notebook_get_webview(GtkNotebook* notebook)
{
    return WEBKIT_WEB_VIEW(gtk_notebook_get_nth_page(notebook, gtk_notebook_get_current_page(notebook)));
}

/* Load content*/

void load_uri(WebKitWebView* view, const char* uri)
{
    if (strlen(uri) == 0) {
        webkit_web_view_load_uri(view, "");
    } else if (g_str_has_prefix(uri, "http://") || g_str_has_prefix(uri, "https://") || g_str_has_prefix(uri, "file://") || g_str_has_prefix(uri, "about:")) {
        webkit_web_view_load_uri(view, uri);
    } else {
        char tmp[strlen(uri) + strlen(SEARCH)];
        snprintf(tmp, sizeof(tmp), SEARCH, uri);
        webkit_web_view_load_uri(view, tmp);
    }
}

void handle_signal_load_changed(WebKitWebView* self, WebKitLoadEvent load_event,
    GtkNotebook* notebook)
{
    switch (load_event) {
        case WEBKIT_LOAD_FINISHED: {
            /* Add gtk tab title */
            const char* webpage_title = webkit_web_view_get_title(self);
            const int max_length = 25;
            char tab_title[max_length + 1];
            if (webpage_title != NULL) {
                for (int i = 0; i < (max_length); i++) {
                    tab_title[i] = webpage_title[i];
                    if (webpage_title[i] == '\0') {
                        break;
                    }
                }
                tab_title[max_length] = '\0';
            }
            gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(self),
                webpage_title == NULL ? "—" : tab_title);
        }
    }
}

/* Create new tabs */
GtkWidget* handle_signal_create_new_tab(WebKitWebView* self,
    WebKitNavigationAction* navigation_action,
    GtkNotebook* notebook)
{
    if (num_tabs < MAX_NUM_TABS || num_tabs == 0) {
        WebKitURIRequest* uri_request = webkit_navigation_action_get_request(navigation_action);
        const char* uri = webkit_uri_request_get_uri(uri_request);
        notebook_create_new_tab(notebook, uri);
        gtk_notebook_set_show_tabs(notebook, 1);
    } else {
        webkit_web_view_evaluate_javascript(self, "alert('Too many tabs, not opening a new one')", -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL);
    }
    return NULL;
    /*
     WebKitGTK documentation recommends returning the new webview.
     I imagine that this might allow e.g., to go back in a new tab
     or generally to keep track of history.
     However, this would require either modifying notebook_create_new_tab
     or duplicating its contents, for unclear gain.
   */
}
WebKitWebView* create_new_webview()
{
    char* style;
    WebKitSettings* settings;
    WebKitWebContext* web_context;
    WebKitCookieManager* cookiemanager;
    WebKitUserContentManager* contentmanager;

    settings = webkit_settings_new_with_settings(WEBKIT_DEFAULT_SETTINGS, NULL);
    web_context = webkit_web_context_new_with_website_data_manager(webkit_website_data_manager_new(DATA_MANAGER_OPTS, NULL));
    contentmanager = webkit_user_content_manager_new();
    cookiemanager = webkit_web_context_get_cookie_manager(web_context);

    webkit_cookie_manager_set_persistent_storage(cookiemanager, DATA_DIR "/cookies.sqlite", WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
    webkit_cookie_manager_set_accept_policy(cookiemanager, WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);

    return g_object_new(WEBKIT_TYPE_WEB_VIEW, "settings", settings, "web-context", web_context, "user-content-manager", contentmanager, NULL);
}
void notebook_create_new_tab(GtkNotebook* notebook, const char* uri)
{
    if (num_tabs < MAX_NUM_TABS || MAX_NUM_TABS == 0) {
        WebKitWebView* view = create_new_webview();

        g_signal_connect(view, "load_changed", G_CALLBACK(handle_signal_load_changed), notebook);
        g_signal_connect(view, "create", G_CALLBACK(handle_signal_create_new_tab), notebook);

        int n = gtk_notebook_append_page(notebook, GTK_WIDGET(view), NULL);
        gtk_notebook_set_tab_reorderable(notebook, GTK_WIDGET(view), 1);
        gtk_widget_show_all(GTK_WIDGET(window));
        load_uri(view, (uri) ? uri : HOME);

        gtk_notebook_set_current_page(notebook, n);
        gtk_notebook_set_tab_label_text(notebook, GTK_WIDGET(view), "-");
        webkit_web_view_set_zoom_level(view, ZOOM);
        num_tabs += 1;
    } else {
        webkit_web_view_evaluate_javascript(notebook_get_webview(notebook), "alert('Too many tabs, not opening a new one')",
            -1, NULL, "rosenrot-alert-numtabs", NULL, NULL, NULL);
    }
}

// Handle what happens when the user is on the bar and presses enter
void handle_signal_bar_press_enter(GtkEntry* self, GtkNotebook* notebook)
{
    if (bar.entry_mode == _SEARCH)
        load_uri(notebook_get_webview(notebook), gtk_entry_buffer_get_text(bar.line_text));
    else if (bar.entry_mode == _FIND)
        webkit_find_controller_search(
            webkit_web_view_get_find_controller(notebook_get_webview(notebook)),
            gtk_entry_buffer_get_text(bar.line_text),
            WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE | WEBKIT_FIND_OPTIONS_WRAP_AROUND,
            G_MAXUINT);
}

/* Handle shortcuts */
int handle_signal_keypress(void* self, GdkEvent* event, GtkNotebook* notebook)
{
    // (void)self;

    guint event_keyval = 0;
    gdk_event_get_keyval(event, &event_keyval);
    GdkModifierType event_state = 0;
    gdk_event_get_state(event, &event_state);

    static double zoom = ZOOM;
    static bool is_fullscreen = 0;

    if (event_state & CTRL) {
        WebKitWebView* view = notebook_get_webview(notebook);
        switch (event_keyval) {
            case KEY(h): // go back
                webkit_web_view_go_back(view);
                break;
            case KEY(j): // go forward 
                webkit_web_view_go_forward(view);
                break;
            case KEY(r): // reload 
                webkit_web_view_reload(view);
                break;  
            case KEY(R): // force reload
                webkit_web_view_reload_bypass_cache(view);
                break;
            case KEY(H): // back to home 
                load_uri(view, HOME);
                break; 
            case KEY(equal): // zoom in 
                webkit_web_view_set_zoom_level(view, (zoom += ZOOM_VAL));
                break;
            case KEY(minus): // zoom out 
                webkit_web_view_set_zoom_level(view, (zoom -= ZOOM_VAL));
                break;
            case KEY(0): // restore zoom 
                webkit_web_view_set_zoom_level(view, (zoom = ZOOM));
                break;
            case KEY(KP_Page_Up):{
                int n = gtk_notebook_get_n_pages(notebook);
                int k = gtk_notebook_get_current_page(notebook);
                int l = (n + k - 1) % n;
                gtk_notebook_set_current_page(notebook, l);
                break;
            } // previous tab 
            case KEY(KP_Page_Down): { // next tab 
                int m = gtk_notebook_get_n_pages(notebook);
                int i = gtk_notebook_get_current_page(notebook);
                int j = (i + 1) % m;
                gtk_notebook_set_current_page(notebook, j);
                break;
            }
            case KEY(w): // close tab 
                gtk_notebook_remove_page(notebook, gtk_notebook_get_current_page(notebook));
                num_tabs -= 1;
                if (gtk_notebook_get_n_pages(notebook) == 0) {
                    exit(0);
                }
                break;
            case KEY(l): // show search bar
            case KEY(semicolon): { // fallthrough; same
                bar.entry_mode = _SEARCH;
                const char* url = webkit_web_view_get_uri(notebook_get_webview(notebook));
                gtk_entry_set_placeholder_text(bar.line, "Search");
                gtk_entry_buffer_set_text(bar.line_text, url, strlen(url));
                gtk_widget_show(GTK_WIDGET(bar.widget));
                gtk_window_set_focus