1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
12 #include <gdk/gdkkeysyms.h>
14 #include <sys/types.h>
20 #include <webkit2/webkit2.h>
21 #include <glib/gstdio.h>
22 #include <JavaScriptCore/JavaScript.h>
34 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
35 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
36 #define COOKIEJAR_TYPE (cookiejar_get_type ())
37 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
39 enum { AtomFind, AtomGo, AtomUri, AtomLast };
41 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
42 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
43 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
44 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
45 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
46 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
47 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
50 typedef union Arg Arg;
57 typedef struct Client {
58 GtkWidget *win, *scroll, *vbox, *pane;
61 WebKitWebInspector *inspector;
62 char *title, *linkhover;
66 gboolean zoomed, fullscreen, isinspecting, sslfailed;
72 void (*func)(Client *c, const Arg *arg);
80 void (*func)(Client *c, const Arg *arg);
85 SoupCookieJarText parent_instance;
90 SoupCookieJarTextClass parent_class;
93 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
102 static Atom atoms[AtomLast];
103 static Client *clients = NULL;
104 static Window embed = 0;
105 static gboolean showxid = FALSE;
106 static char winid[64];
107 static char togglestat[9];
108 static char pagestat[3];
109 static GTlsDatabase *tlsdb;
110 static int policysel = 0;
111 static char *stylefile = NULL;
112 static SoupCache *diskcache = NULL;
114 static void addaccelgroup(Client *c);
115 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
116 WebKitWebResource *r, WebKitNetworkRequest *req,
117 WebKitNetworkResponse *resp, Client *c);
118 static char *buildfile(const char *path);
119 static char *buildpath(const char *path);
120 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
121 static void cleanup(void);
122 static void clipboard(Client *c, const Arg *arg);
124 /* Cookiejar implementation */
125 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
126 SoupCookie *new_cookie);
127 static void cookiejar_finalize(GObject *self);
128 static SoupCookieJarAcceptPolicy cookiepolicy_get(void);
129 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only,
130 SoupCookieJarAcceptPolicy policy);
131 static void cookiejar_set_property(GObject *self, guint prop_id,
132 const GValue *value, GParamSpec *pspec);
133 static char cookiepolicy_set(const SoupCookieJarAcceptPolicy p);
135 static char *copystr(char **str, const char *src);
136 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
138 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
139 WebKitNetworkRequest *r, gchar *m,
140 WebKitWebPolicyDecision *p, Client *c);
141 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
142 WebKitNetworkRequest *r, WebKitWebNavigationAction
143 *n, WebKitWebPolicyDecision *p, Client *c);
144 static gboolean deletion_interface(WebKitWebView *view,
145 WebKitDOMHTMLElement *arg1, Client *c);
146 static void destroyclient(Client *c);
147 static void destroywin(GtkWidget* w, Client *c);
148 static void die(const char *errstr, ...);
149 static void eval(Client *c, const Arg *arg);
150 static void find(Client *c, const Arg *arg);
151 static void fullscreen(Client *c, const Arg *arg);
152 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
153 WebKitGeolocationPolicyDecision *d, Client *c);
154 static const char *getatom(Client *c, int a);
155 static void gettogglestat(Client *c);
156 static void getpagestat(Client *c);
157 static char *geturi(Client *c);
158 static const gchar *getstyle(const char *uri);
159 static void setstyle(Client *c, const char *style);
161 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
163 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
165 static void inspector(Client *c, const Arg *arg);
166 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
168 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
169 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
170 static void inspector_finished(WebKitWebInspector *i, Client *c);
172 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
173 GdkModifierType mods, Client *c);
174 static void linkhover(WebKitWebView *v, const char* t, const char* l,
176 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
178 static void loaduri(Client *c, const Arg *arg);
179 static void navigate(Client *c, const Arg *arg);
180 static Client *newclient(void);
181 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
182 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
183 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
184 WebKitHitTestResult *target, gboolean keyboard,
186 static void menuactivate(GtkMenuItem *item, Client *c);
187 static void print(Client *c, const Arg *arg);
188 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
190 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
191 static void linkopen(Client *c, const Arg *arg);
192 static void linkopenembed(Client *c, const Arg *arg);
193 static void reload(Client *c, const Arg *arg);
194 static void scroll_h(Client *c, const Arg *arg);
195 static void scroll_v(Client *c, const Arg *arg);
196 static void scroll(GtkAdjustment *a, const Arg *arg);
197 static void setatom(Client *c, int a, const char *v);
198 static void setup(void);
199 static void sigchld(int unused);
200 static void source(Client *c, const Arg *arg);
201 static void spawn(Client *c, const Arg *arg);
202 static void stop(Client *c, const Arg *arg);
203 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
204 static void titlechangeleave(void *a, void *b, Client *c);
205 static void toggle(Client *c, const Arg *arg);
206 static void togglecookiepolicy(Client *c, const Arg *arg);
207 static void togglegeolocation(Client *c, const Arg *arg);
208 static void togglescrollbars(Client *c, const Arg *arg);
209 static void togglestyle(Client *c, const Arg *arg);
210 static void updatetitle(Client *c);
211 static void updatewinid(Client *c);
212 static void usage(void);
213 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
214 JSContextRef js, JSObjectRef win, Client *c);
215 static void zoom(Client *c, const Arg *arg);
217 /* configuration, allows nested code to access above variables */
221 addaccelgroup(Client *c)
224 GtkAccelGroup *group = gtk_accel_group_new();
227 for (i = 0; i < LENGTH(keys); i++) {
228 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
229 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
232 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
236 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
237 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
240 const gchar *uri = webkit_network_request_get_uri(req);
243 if (g_str_has_suffix(uri, "/favicon.ico"))
244 webkit_network_request_set_uri(req, "about:blank");
246 if (!g_str_has_prefix(uri, "http://")
247 && !g_str_has_prefix(uri, "https://")
248 && !g_str_has_prefix(uri, "about:")
249 && !g_str_has_prefix(uri, "file://")
250 && !g_str_has_prefix(uri, "data:")
251 && !g_str_has_prefix(uri, "blob:")
252 && strlen(uri) > 0) {
253 for (i = 0; i < strlen(uri); i++) {
254 if (!g_ascii_isprint(uri[i])) {
260 handleplumb(c, w, uri);
265 buildfile(const char *path)
267 char *dname, *bname, *bpath, *fpath;
270 dname = g_path_get_dirname(path);
271 bname = g_path_get_basename(path);
273 bpath = buildpath(dname);
276 fpath = g_build_filename(bpath, bname, NULL);
280 if (!(f = fopen(fpath, "a")))
281 die("Could not open file: %s\n", fpath);
283 g_chmod(fpath, 0600); /* always */
290 buildpath(const char *path)
293 char *apath, *name, *p, *fpath;
295 if (path[0] == '~') {
296 if (path[1] == '/' || path[1] == '\0') {
297 p = (char *)&path[1];
298 pw = getpwuid(getuid());
300 if ((p = strchr(path, '/')))
301 name = g_strndup(&path[1], --p - path);
303 name = g_strdup(&path[1]);
305 if (!(pw = getpwnam(name))) {
306 die("Can't get user %s home directory: %s.\n",
311 apath = g_build_filename(pw->pw_dir, p, NULL);
313 apath = g_strdup(path);
316 /* creating directory */
317 if (g_mkdir_with_parents(apath, 0700) < 0)
318 die("Could not access directory: %s\n", apath);
320 fpath = realpath(apath, NULL);
327 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
329 WebKitHitTestResultContext context;
330 WebKitHitTestResult *result;
334 result = webkit_web_view_get_hit_test_result(web, e);
335 g_object_get(result, "context", &context, NULL);
336 g_object_get(result, "link-uri", &arg.v, NULL);
337 for (i = 0; i < LENGTH(buttons); i++) {
338 if (context & buttons[i].click
339 && e->button == buttons[i].button
340 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
341 && buttons[i].func) {
342 buttons[i].func(c, buttons[i].click == ClkLink
343 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
354 soup_cache_flush(diskcache);
355 soup_cache_dump(diskcache);
358 destroyclient(clients);
365 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
366 SoupCookie *new_cookie)
368 flock(COOKIEJAR(self)->lock, LOCK_EX);
369 if (new_cookie && !new_cookie->expires && sessiontime) {
370 soup_cookie_set_expires(new_cookie,
371 soup_date_new_from_now(sessiontime));
373 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
376 flock(COOKIEJAR(self)->lock, LOCK_UN);
380 cookiejar_class_init(CookieJarClass *klass)
382 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
383 G_OBJECT_CLASS(klass)->get_property =
384 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
385 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
386 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
387 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
391 cookiejar_finalize(GObject *self)
393 close(COOKIEJAR(self)->lock);
394 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
398 cookiejar_init(CookieJar *self)
400 self->lock = open(cookiefile, 0);
404 cookiejar_new(const char *filename, gboolean read_only,
405 SoupCookieJarAcceptPolicy policy)
407 return g_object_new(COOKIEJAR_TYPE,
408 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
409 SOUP_COOKIE_JAR_READ_ONLY, read_only,
410 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
414 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
417 flock(COOKIEJAR(self)->lock, LOCK_SH);
418 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
420 flock(COOKIEJAR(self)->lock, LOCK_UN);
423 SoupCookieJarAcceptPolicy
424 cookiepolicy_get(void)
426 switch (cookiepolicies[policysel]) {
428 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
430 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
436 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
440 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep)
443 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
445 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
447 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
456 evalscript(JSContextRef js, char *script, char* scriptname)
458 JSStringRef jsscript, jsscriptname;
459 JSValueRef exception = NULL;
461 jsscript = JSStringCreateWithUTF8CString(script);
462 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
463 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
464 jsscriptname, 0, &exception);
465 JSStringRelease(jsscript);
466 JSStringRelease(jsscriptname);
470 runscript(WebKitWebFrame *frame)
475 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
476 evalscript(webkit_web_frame_get_global_context(frame), script,
482 clipboard(Client *c, const Arg *arg)
484 gboolean paste = *(gboolean *)arg;
487 gtk_clipboard_request_text(gtk_clipboard_get(
488 GDK_SELECTION_PRIMARY),
491 gtk_clipboard_set_text(gtk_clipboard_get(
492 GDK_SELECTION_PRIMARY), c->linkhover
493 ? c->linkhover : geturi(c), -1);
498 copystr(char **str, const char *src)
511 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
513 Client *n = newclient();
518 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
519 gchar *m, WebKitWebPolicyDecision *p, Client *c)
521 if (!webkit_web_view_can_show_mime_type(v, m)) {
522 webkit_web_policy_decision_download(p);
529 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
530 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
535 if (webkit_web_navigation_action_get_reason(n)
536 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
537 webkit_web_policy_decision_ignore(p);
538 arg.v = (void *)webkit_network_request_get_uri(r);
539 newwindow(NULL, &arg, 0);
546 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
552 destroyclient(Client *c)
556 webkit_web_view_stop_loading(c->view);
557 gtk_widget_destroy(GTK_WIDGET(c->view));
558 gtk_widget_destroy(c->scroll);
559 gtk_widget_destroy(c->vbox);
560 gtk_widget_destroy(c->win);
562 for (p = clients; p && p->next != c; p = p->next)
574 destroywin(GtkWidget* w, Client *c)
580 die(const char *errstr, ...)
584 va_start(ap, errstr);
585 vfprintf(stderr, errstr, ap);
591 find(Client *c, const Arg *arg)
595 s = getatom(c, AtomFind);
596 gboolean forward = *(gboolean *)arg;
597 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
601 fullscreen(Client *c, const Arg *arg)
604 gtk_window_unfullscreen(GTK_WINDOW(c->win));
606 gtk_window_fullscreen(GTK_WINDOW(c->win));
607 c->fullscreen = !c->fullscreen;
611 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
612 WebKitGeolocationPolicyDecision *d, Client *c)
614 if (allowgeolocation)
615 webkit_geolocation_policy_allow(d);
617 webkit_geolocation_policy_deny(d);
621 getatom(Client *c, int a)
623 static char buf[BUFSIZ];
626 unsigned long ldummy;
627 unsigned char *p = NULL;
629 XGetWindowProperty(dpy, c->xid,
630 atoms[a], 0L, BUFSIZ, False, XA_STRING,
631 &adummy, &idummy, &ldummy, &ldummy, &p);
633 strncpy(buf, (char *)p, LENGTH(buf)-1);
646 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
652 getstyle(const char *uri)
656 if (stylefile != NULL)
659 for (i = 0; i < LENGTH(styles); i++) {
660 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
662 return styles[i].style;
669 setstyle(Client *c, const char *style)
671 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
673 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
677 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
681 webkit_web_view_stop_loading(w);
682 arg = (Arg)PLUMB((char *)uri);
687 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
692 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
698 inspector(Client *c, const Arg *arg)
700 if (enableinspector) {
702 webkit_web_inspector_close(c->inspector);
704 webkit_web_inspector_show(c->inspector);
709 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
711 return WEBKIT_WEB_VIEW(webkit_web_view_new());
715 inspector_show(WebKitWebInspector *i, Client *c)
722 w = webkit_web_inspector_get_web_view(i);
723 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
724 gtk_widget_show(GTK_WIDGET(w));
725 c->isinspecting = true;
731 inspector_close(WebKitWebInspector *i, Client *c)
735 if (!c->isinspecting)
738 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
740 gtk_widget_destroy(w);
741 c->isinspecting = false;
747 inspector_finished(WebKitWebInspector *i, Client *c)
749 g_free(c->inspector);
753 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
757 gboolean processed = FALSE;
759 mods = CLEANMASK(mods);
760 key = gdk_keyval_to_lower(key);
762 for (i = 0; i < LENGTH(keys); i++) {
763 if (key == keys[i].keyval
764 && mods == keys[i].mod
766 keys[i].func(c, &(keys[i].arg));
775 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
778 c->linkhover = copystr(&c->linkhover, l);
779 } else if (c->linkhover) {
787 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
789 WebKitWebFrame *frame;
790 WebKitWebDataSource *src;
791 WebKitNetworkRequest *request;
795 switch (webkit_web_view_get_load_status (c->view)) {
796 case WEBKIT_LOAD_COMMITTED:
798 if (strstr(uri, "https://") == uri) {
799 frame = webkit_web_view_get_main_frame(c->view);
800 src = webkit_web_frame_get_data_source(frame);
801 request = webkit_web_data_source_get_request(src);
802 msg = webkit_network_request_get_message(request);
803 c->sslfailed = !(soup_message_get_flags(msg)
804 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
806 setatom(c, AtomUri, uri);
809 setstyle(c, getstyle(uri));
811 case WEBKIT_LOAD_FINISHED:
815 soup_cache_flush(diskcache);
816 soup_cache_dump(diskcache);
825 loaduri(Client *c, const Arg *arg)
828 const char *uri = (char *)arg->v;
829 Arg a = { .b = FALSE };
832 if (strcmp(uri, "") == 0)
835 /* In case it's a file path. */
836 if (stat(uri, &st) == 0) {
837 rp = realpath(uri, NULL);
838 u = g_strdup_printf("file://%s", rp);
841 u = g_strrstr(uri, "://") ? g_strdup(uri)
842 : g_strdup_printf("http://%s", uri);
845 setatom(c, AtomUri, uri);
847 /* prevents endless loop */
848 if (strcmp(u, geturi(c)) == 0) {
851 webkit_web_view_load_uri(c->view, u);
853 c->title = copystr(&c->title, u);
860 navigate(Client *c, const Arg *arg)
862 int steps = *(int *)arg;
863 webkit_web_view_go_back_or_forward(c->view, steps);
870 WebKitWebSettings *settings;
871 WebKitWebFrame *frame;
872 GdkGeometry hints = { 1, 1 };
878 if (!(c = calloc(1, sizeof(Client))))
879 die("Cannot malloc!\n");
886 c->win = gtk_plug_new(embed);
888 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
890 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
891 * is always correct, so we should still call this function.
892 * But when doing so, we *must* differentiate between a
893 * WM_CLASS and a resource on the window. By convention, the
894 * window class (WM_CLASS) is capped, while the resource is in
895 * lowercase. Both these values come as a pair.
897 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
899 /* TA: 20091214: And set the role here as well -- so that
900 * sessions can pick this up.
902 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
904 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
905 g_signal_connect(G_OBJECT(c->win),
907 G_CALLBACK(destroywin), c);
908 g_signal_connect(G_OBJECT(c->win),
909 "leave_notify_event",
910 G_CALLBACK(titlechangeleave), c);
916 c->pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
919 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
920 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
923 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
925 g_signal_connect(G_OBJECT(c->view),
927 G_CALLBACK(titlechange), c);
928 g_signal_connect(G_OBJECT(c->view),
929 "hovering-over-link",
930 G_CALLBACK(linkhover), c);
931 g_signal_connect(G_OBJECT(c->view),
932 "geolocation-policy-decision-requested",
933 G_CALLBACK(geopolicyrequested), c);
934 g_signal_connect(G_OBJECT(c->view),
936 G_CALLBACK(createwindow), c);
937 g_signal_connect(G_OBJECT(c->view),
938 "new-window-policy-decision-requested",
939 G_CALLBACK(decidewindow), c);
940 g_signal_connect(G_OBJECT(c->view),
941 "mime-type-policy-decision-requested",
942 G_CALLBACK(decidedownload), c);
943 g_signal_connect(G_OBJECT(c->view),
944 "window-object-cleared",
945 G_CALLBACK(windowobjectcleared), c);
946 g_signal_connect(G_OBJECT(c->view),
947 "notify::load-status",
948 G_CALLBACK(loadstatuschange), c);
949 g_signal_connect(G_OBJECT(c->view),
951 G_CALLBACK(progresschange), c);
952 g_signal_connect(G_OBJECT(c->view),
953 "download-requested",
954 G_CALLBACK(initdownload), c);
955 g_signal_connect(G_OBJECT(c->view),
956 "button-release-event",
957 G_CALLBACK(buttonrelease), c);
958 g_signal_connect(G_OBJECT(c->view),
960 G_CALLBACK(contextmenu), c);
961 g_signal_connect(G_OBJECT(c->view),
962 "resource-request-starting",
963 G_CALLBACK(beforerequest), c);
964 g_signal_connect(G_OBJECT(c->view),
965 "should-show-delete-interface-for-element",
966 G_CALLBACK(deletion_interface), c);
968 /* Scrolled Window */
969 c->scroll = gtk_scrolled_window_new(NULL, NULL);
971 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
972 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
973 G_CALLBACK(gtk_true), NULL);
975 if (!enablescrollbars) {
976 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
980 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
981 GTK_POLICY_AUTOMATIC,
982 GTK_POLICY_AUTOMATIC);
986 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
987 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
988 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
991 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
993 gtk_widget_grab_focus(GTK_WIDGET(c->view));
994 gtk_widget_show(c->pane);
995 gtk_widget_show(c->vbox);
996 gtk_widget_show(c->scroll);
997 gtk_widget_show(GTK_WIDGET(c->view));
998 gtk_widget_show(c->win);
999 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1000 c->xid = gdk_x11_window_get_xid(gwin);
1001 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1003 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1004 gdk_window_add_filter(gwin, processx, c);
1005 webkit_web_view_set_full_content_zoom(c->view, TRUE);
1009 settings = webkit_web_view_get_settings(c->view);
1010 if (!(ua = getenv("SURF_USERAGENT")))
1012 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
1013 g_object_set(G_OBJECT(settings),
1014 "auto-load-images", loadimages, NULL);
1015 g_object_set(G_OBJECT(settings),
1016 "enable-plugins", enableplugins, NULL);
1017 g_object_set(G_OBJECT(settings),
1018 "enable-scripts", enablescripts, NULL);
1019 g_object_set(G_OBJECT(settings),
1020 "enable-spatial-navigation", enablespatialbrowsing, NULL);
1021 g_object_set(G_OBJECT(settings),
1022 "enable-developer-extras", enableinspector, NULL);
1023 g_object_set(G_OBJECT(settings),
1024 "enable-default-context-menu", kioskmode ^ 1, NULL);
1025 g_object_set(G_OBJECT(settings),
1026 "default-font-size", defaultfontsize, NULL);
1027 g_object_set(G_OBJECT(settings),
1028 "resizable-text-areas", 1, NULL);
1030 setstyle(c, getstyle("about:blank"));
1033 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
1034 * This ensures websites are not unusably small with a high DPI screen.
1035 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
1038 screen = gdk_window_get_screen(gwin);
1039 dpi = gdk_screen_get_resolution(screen);
1041 g_object_set(G_OBJECT(settings),
1042 "enforce-96-dpi", true, NULL);
1043 webkit_web_view_set_zoom_level(c->view, dpi/96);
1046 /* This might conflict with _zoomto96dpi_. */
1047 if (zoomlevel != 1.0)
1048 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1050 if (enableinspector) {
1051 c->inspector = webkit_web_view_get_inspector(c->view);
1052 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
1053 G_CALLBACK(inspector_new), c);
1054 g_signal_connect(G_OBJECT(c->inspector), "show-window",
1055 G_CALLBACK(inspector_show), c);
1056 g_signal_connect(G_OBJECT(c->inspector), "close-window",
1057 G_CALLBACK(inspector_close), c);
1058 g_signal_connect(G_OBJECT(c->inspector), "finished",
1059 G_CALLBACK(inspector_finished), c);
1060 c->isinspecting = false;
1063 if (runinfullscreen)
1064 fullscreen(c, NULL);
1066 setatom(c, AtomFind, "");
1067 setatom(c, AtomUri, "about:blank");
1069 webkit_web_view_set_transparent(c->view, TRUE);
1075 gdk_display_sync(gtk_widget_get_display(c->win));
1076 printf("%lu\n", c->xid);
1078 if (fclose(stdout) != 0) {
1079 die("Error closing stdout");
1087 newwindow(Client *c, const Arg *arg, gboolean noembed)
1090 const char *cmd[18], *uri;
1091 const Arg a = { .v = (void *)cmd };
1096 cmd[i++] = cookiepolicies;
1097 if (!enablescrollbars)
1099 if (embed && !noembed) {
1101 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1104 if (!allowgeolocation)
1116 if (enablediskcache)
1119 cmd[i++] = cookiefile;
1121 uri = arg->v ? (char *)arg->v : c->linkhover;
1129 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1130 gboolean keyboard, Client *c)
1132 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1134 for (GList *l = items; l; l = l->next)
1135 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1142 menuactivate(GtkMenuItem *item, Client *c)
1145 * context-menu-action-2000 open link
1146 * context-menu-action-1 open link in window
1147 * context-menu-action-2 download linked file
1148 * context-menu-action-3 copy link location
1149 * context-menu-action-7 copy image address
1150 * context-menu-action-13 reload
1151 * context-menu-action-10 back
1152 * context-menu-action-11 forward
1153 * context-menu-action-12 stop
1156 const gchar *name, *uri;
1157 GtkClipboard *prisel, *clpbrd;
1159 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1163 if (!g_strcmp0(name, "context-menu-action-3")) {
1164 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1165 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1166 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1167 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1168 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1169 uri = gtk_clipboard_wait_for_text(clpbrd);
1171 gtk_clipboard_set_text(prisel, uri, -1);
1176 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1178 Arg arg = {.v = text };
1180 loaduri((Client *) d, &arg);
1184 print(Client *c, const Arg *arg)
1186 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1190 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1192 Client *c = (Client *)d;
1196 if (((XEvent *)e)->type == PropertyNotify) {
1197 ev = &((XEvent *)e)->xproperty;
1198 if (ev->state == PropertyNewValue) {
1199 if (ev->atom == atoms[AtomFind]) {
1203 return GDK_FILTER_REMOVE;
1204 } else if (ev->atom == atoms[AtomGo]) {
1205 arg.v = getatom(c, AtomGo);
1208 return GDK_FILTER_REMOVE;
1212 return GDK_FILTER_CONTINUE;
1216 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1218 c->progress = webkit_web_view_get_progress(c->view) * 100;
1223 linkopen(Client *c, const Arg *arg)
1225 newwindow(NULL, arg, 1);
1229 linkopenembed(Client *c, const Arg *arg)
1231 newwindow(NULL, arg, 0);
1235 reload(Client *c, const Arg *arg)
1237 gboolean nocache = *(gboolean *)arg;
1239 webkit_web_view_reload_bypass_cache(c->view);
1241 webkit_web_view_reload(c->view);
1245 scroll_h(Client *c, const Arg *arg)
1247 scroll(gtk_scrolled_window_get_hadjustment(
1248 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1252 scroll_v(Client *c, const Arg *arg)
1254 scroll(gtk_scrolled_window_get_vadjustment(
1255 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1259 scroll(GtkAdjustment *a, const Arg *arg)
1263 v = gtk_adjustment_get_value(a);
1267 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1272 v += gtk_adjustment_get_step_increment(a) * arg->i;
1276 v = MIN(v, gtk_adjustment_get_upper(a) -
1277 gtk_adjustment_get_page_size(a));
1278 gtk_adjustment_set_value(a, v);
1282 setatom(Client *c, int a, const char *v)
1285 XChangeProperty(dpy, c->xid,
1286 atoms[a], XA_STRING, 8, PropModeReplace,
1287 (unsigned char *)v, strlen(v) + 1);
1294 char *styledirfile, *stylepath;
1296 GError *error = NULL;
1298 /* clean up any zombies immediately */
1300 gtk_init(NULL, NULL);
1302 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1305 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1306 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1307 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1309 /* dirs and files */
1310 cookiefile = buildfile(cookiefile);
1311 scriptfile = buildfile(scriptfile);
1312 cachefolder = buildpath(cachefolder);
1313 if (stylefile == NULL) {
1314 styledir = buildpath(styledir);
1315 for (i = 0; i < LENGTH(styles); i++) {
1316 if (regcomp(&(styles[i].re), styles[i].regex,
1319 "Could not compile regex: %s\n",
1321 styles[i].regex = NULL;
1323 styledirfile = g_strconcat(styledir, "/",
1324 styles[i].style, NULL);
1325 stylepath = buildfile(styledirfile);
1326 styles[i].style = g_strconcat("file://", stylepath,
1328 g_free(styledirfile);
1333 stylepath = buildfile(stylefile);
1334 stylefile = g_strconcat("file://", stylepath, NULL);
1338 /* request handler */
1339 s = webkit_get_default_session();
1342 soup_session_add_feature(s,
1343 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1344 FALSE, cookiepolicy_get())));
1347 if (enablediskcache) {
1348 diskcache = soup_cache_new(cachefolder,
1349 SOUP_CACHE_SINGLE_USER);
1350 soup_cache_set_max_size(diskcache, diskcachebytes);
1351 soup_cache_load(diskcache);
1352 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1356 tlsdb = g_tls_file_database_new(cafile, &error);
1359 g_warning("Error loading SSL database %s: %s", cafile,
1361 g_error_free(error);
1363 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1364 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1370 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1371 die("Can't install SIGCHLD handler");
1372 while (0 < waitpid(-1, NULL, WNOHANG));
1376 source(Client *c, const Arg *arg)
1378 Arg a = { .b = FALSE };
1381 s = webkit_web_view_get_view_source_mode(c->view);
1382 webkit_web_view_set_view_source_mode(c->view, !s);
1387 spawn(Client *c, const Arg *arg)
1391 close(ConnectionNumber(dpy));
1393 execvp(((char **)arg->v)[0], (char **)arg->v);
1394 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1401 eval(Client *c, const Arg *arg)
1403 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1404 evalscript(webkit_web_frame_get_global_context(frame),
1405 ((char **)arg->v)[0], "");
1409 stop(Client *c, const Arg *arg)
1411 webkit_web_view_stop_loading(c->view);
1415 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1417 const gchar *t = webkit_web_view_get_title(view);
1419 c->title = copystr(&c->title, t);
1425 titlechangeleave(void *a, void *b, Client *c)
1427 c->linkhover = NULL;
1432 toggle(Client *c, const Arg *arg)
1434 WebKitWebSettings *settings;
1435 char *name = (char *)arg->v;
1437 Arg a = { .b = FALSE };
1439 settings = webkit_web_view_get_settings(c->view);
1440 g_object_get(G_OBJECT(settings), name, &value, NULL);
1441 g_object_set(G_OBJECT(settings), name, !value, NULL);
1447 togglecookiepolicy(Client *c, const Arg *arg)
1450 SoupCookieJarAcceptPolicy policy;
1452 jar = SOUP_COOKIE_JAR(soup_session_get_feature(
1453 webkit_get_default_session(),
1454 SOUP_TYPE_COOKIE_JAR));
1455 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1458 if (policysel >= strlen(cookiepolicies))
1461 g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);
1464 /* Do not reload. */
1468 togglegeolocation(Client *c, const Arg *arg)
1470 Arg a = { .b = FALSE };
1472 allowgeolocation ^= 1;
1477 twitch(Client *c, const Arg *arg)
1482 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1485 v = gtk_adjustment_get_value(a);
1490 v = MIN(v, gtk_adjustment_get_upper(a) -
1491 gtk_adjustment_get_page_size(a));
1492 gtk_adjustment_set_value(a, v);
1496 togglescrollbars(Client *c, const Arg *arg)
1498 GtkPolicyType vspolicy;
1501 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1504 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1505 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1509 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1510 GTK_POLICY_AUTOMATIC,
1511 GTK_POLICY_AUTOMATIC);
1520 togglestyle(Client *c, const Arg *arg)
1522 enablestyle = !enablestyle;
1523 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1529 gettogglestat(Client *c)
1533 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1535 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1537 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1539 togglestat[p++] = value? 'C': 'c';
1541 togglestat[p++] = allowgeolocation? 'G': 'g';
1543 togglestat[p++] = enablediskcache? 'D': 'd';
1545 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1546 togglestat[p++] = value? 'I': 'i';
1548 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1549 togglestat[p++] = value? 'S': 's';
1551 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1552 togglestat[p++] = value? 'V': 'v';
1554 togglestat[p++] = enablestyle ? 'M': 'm';
1556 togglestat[p] = '\0';
1560 getpagestat(Client *c)
1562 const char *uri = geturi(c);
1564 if (strstr(uri, "https://") == uri)
1565 pagestat[0] = c->sslfailed ? 'U' : 'T';
1573 updatetitle(Client *c)
1577 if (showindicators) {
1582 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1584 } else if (c->progress != 100) {
1585 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1586 togglestat, pagestat,
1587 c->title == NULL ? "" : c->title);
1589 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1590 c->title == NULL ? "" : c->title);
1593 gtk_window_set_title(GTK_WINDOW(c->win), t);
1596 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1602 updatewinid(Client *c)
1604 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1610 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1611 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1612 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1616 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1617 JSObjectRef win, Client *c)
1623 zoom(Client *c, const Arg *arg)
1628 webkit_web_view_zoom_out(c->view);
1629 } else if (arg->i > 0) {
1631 webkit_web_view_zoom_in(c->view);
1635 webkit_web_view_set_zoom_level(c->view, 1.0);
1640 main(int argc, char *argv[])
1645 memset(&arg, 0, sizeof(arg));
1647 /* command line args */
1650 cookiepolicies = EARGF(usage());
1653 enablescrollbars = 0;
1656 enablescrollbars = 1;
1659 cookiefile = EARGF(usage());
1662 enablediskcache = 0;
1665 enablediskcache = 1;
1668 embed = strtol(EARGF(usage()), NULL, 0);
1671 runinfullscreen = 0;
1674 runinfullscreen = 1;
1677 allowgeolocation = 0;
1680 allowgeolocation = 1;
1701 enableinspector = 0;
1704 enableinspector = 1;
1713 scriptfile = EARGF(usage());
1722 stylefile = EARGF(usage());
1725 useragent = EARGF(usage());
1728 die("surf-"VERSION", ©2009-2015 surf engineers, "
1729 "see LICENSE for details\n");
1734 zoomlevel = strtof(EARGF(usage()), NULL);
1745 loaduri(clients, &arg);
1752 return EXIT_SUCCESS;