twebext-surf.c - surf - [fork] customized build of surf, the suckless webkit browser
(HTM) git clone git://src.adamsgaard.dk/surf
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
twebext-surf.c (2472B)
---
1 #include <sys/socket.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <inttypes.h>
5 #include <limits.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include <gio/gio.h>
10 #include <webkit2/webkit-web-extension.h>
11 #include <webkitdom/webkitdom.h>
12 #include <webkitdom/WebKitDOMDOMWindowUnstable.h>
13
14 #include "common.h"
15
16 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
17
18 static WebKitWebExtension *webext;
19 static int sock;
20
21 static void
22 msgsurf(guint64 pageid, const char *s)
23 {
24 static char msg[MSGBUFSZ];
25 size_t sln = strlen(s);
26 int ret;
27
28 if ((ret = snprintf(msg, sizeof(msg), "%c%s", pageid, s))
29 >= sizeof(msg)) {
30 fprintf(stderr, "webext: msg: message too long: %d\n", ret);
31 return;
32 }
33
34 if (send(sock, msg, ret, 0) < 0)
35 fprintf(stderr, "webext: error sending: %s\n", msg+1);
36 }
37
38 static gboolean
39 readsock(GIOChannel *s, GIOCondition c, gpointer unused)
40 {
41 static char js[48], msg[MSGBUFSZ];
42 WebKitWebPage *page;
43 JSCContext *jsc;
44 GError *gerr = NULL;
45 gsize msgsz;
46
47 if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) !=
48 G_IO_STATUS_NORMAL) {
49 if (gerr) {
50 fprintf(stderr, "webext: error reading socket: %s\n",
51 gerr->message);
52 g_error_free(gerr);
53 }
54 return TRUE;
55 }
56
57 if (msgsz < 2) {
58 fprintf(stderr, "webext: readsock: message too short: %d\n",
59 msgsz);
60 return TRUE;
61 }
62
63 if (!(page = webkit_web_extension_get_page(webext, msg[0])))
64 return TRUE;
65
66 jsc = webkit_frame_get_js_context(webkit_web_page_get_main_frame(page));
67
68 switch (msg[1]) {
69 case 'h':
70 if (msgsz != 3)
71 return TRUE;
72 snprintf(js, sizeof(js),
73 "window.scrollBy(window.innerWidth/100*%d,0);",
74 msg[2]);
75 jsc_context_evaluate(jsc, js, -1);
76 break;
77 case 'v':
78 if (msgsz != 3)
79 return TRUE;
80 snprintf(js, sizeof(js),
81 "window.scrollBy(0,window.innerHeight/100*%d);",
82 msg[2]);
83 jsc_context_evaluate(jsc, js, -1);
84 break;
85 }
86
87 return TRUE;
88 }
89
90 G_MODULE_EXPORT void
91 webkit_web_extension_initialize_with_user_data(WebKitWebExtension *e,
92 const GVariant *gv)
93 {
94 GIOChannel *gchansock;
95
96 webext = e;
97
98 g_variant_get(gv, "i", &sock);
99
100 gchansock = g_io_channel_unix_new(sock);
101 g_io_channel_set_encoding(gchansock, NULL, NULL);
102 g_io_channel_set_flags(gchansock, g_io_channel_get_flags(gchansock)
103 | G_IO_FLAG_NONBLOCK, NULL);
104 g_io_channel_set_close_on_unref(gchansock, TRUE);
105 g_io_add_watch(gchansock, G_IO_IN, readsock, NULL);
106 }