1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
11 #include <gdk/gdkkeysyms.h>
13 #include <sys/types.h>
19 #include <webkit/webkit.h>
20 #include <glib/gstdio.h>
21 #include <JavaScriptCore/JavaScript.h>
33 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
34 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
35 #define COOKIEJAR_TYPE (cookiejar_get_type ())
36 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
38 enum { AtomFind, AtomGo, AtomUri, AtomLast };
40 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
41 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
42 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
43 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
44 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
45 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
46 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
49 typedef union Arg Arg;
56 typedef struct Client {
57 GtkWidget *win, *scroll, *vbox, *pane;
59 WebKitWebInspector *inspector;
60 char *title, *linkhover;
64 gboolean zoomed, fullscreen, isinspecting, sslfailed;
70 void (*func)(Client *c, const Arg *arg);
78 void (*func)(Client *c, const Arg *arg);
83 SoupCookieJarText parent_instance;
88 SoupCookieJarTextClass parent_class;
91 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
100 static Atom atoms[AtomLast];
101 static Client *clients = NULL;
102 static GdkNativeWindow embed = 0;
103 static gboolean showxid = FALSE;
104 static char winid[64];
105 static gboolean usingproxy = 0;
106 static char togglestat[9];
107 static char pagestat[3];
108 static GTlsDatabase *tlsdb;
109 static int policysel = 0;
110 static char *stylefile = NULL;
111 static SoupCache *diskcache = NULL;
113 static void addaccelgroup(Client *c);
114 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
115 WebKitWebResource *r, WebKitNetworkRequest *req,
116 WebKitNetworkResponse *resp, Client *c);
117 static char *buildfile(const char *path);
118 static char *buildpath(const char *path);
119 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
120 static void cleanup(void);
121 static void clipboard(Client *c, const Arg *arg);
123 /* Cookiejar implementation */
124 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
125 SoupCookie *new_cookie);
126 static void cookiejar_finalize(GObject *self);
127 static SoupCookieJarAcceptPolicy cookiepolicy_get(void);
128 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only,
129 SoupCookieJarAcceptPolicy policy);
130 static void cookiejar_set_property(GObject *self, guint prop_id,
131 const GValue *value, GParamSpec *pspec);
132 static char cookiepolicy_set(const SoupCookieJarAcceptPolicy p);
134 static char *copystr(char **str, const char *src);
135 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
137 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
138 WebKitNetworkRequest *r, gchar *m,
139 WebKitWebPolicyDecision *p, Client *c);
140 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
141 WebKitNetworkRequest *r, WebKitWebNavigationAction
142 *n, WebKitWebPolicyDecision *p, Client *c);
143 static gboolean deletion_interface(WebKitWebView *view,
144 WebKitDOMHTMLElement *arg1, Client *c);
145 static void destroyclient(Client *c);
146 static void destroywin(GtkWidget* w, Client *c);
147 static void die(const char *errstr, ...);
148 static void eval(Client *c, const Arg *arg);
149 static void find(Client *c, const Arg *arg);
150 static void fullscreen(Client *c, const Arg *arg);
151 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
152 WebKitGeolocationPolicyDecision *d, Client *c);
153 static const char *getatom(Client *c, int a);
154 static void gettogglestat(Client *c);
155 static void getpagestat(Client *c);
156 static char *geturi(Client *c);
157 static gchar *getstyle(const char *uri);
159 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
161 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
163 static void inspector(Client *c, const Arg *arg);
164 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
166 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
167 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
168 static void inspector_finished(WebKitWebInspector *i, Client *c);
170 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
171 GdkModifierType mods, Client *c);
172 static void linkhover(WebKitWebView *v, const char* t, const char* l,
174 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
176 static void loaduri(Client *c, const Arg *arg);
177 static void navigate(Client *c, const Arg *arg);
178 static Client *newclient(void);
179 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
180 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
181 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
182 WebKitHitTestResult *target, gboolean keyboard,
184 static void menuactivate(GtkMenuItem *item, Client *c);
185 static void print(Client *c, const Arg *arg);
186 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
188 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
189 static void linkopen(Client *c, const Arg *arg);
190 static void linkopenembed(Client *c, const Arg *arg);
191 static void reload(Client *c, const Arg *arg);
192 static void scroll_h(Client *c, const Arg *arg);
193 static void scroll_v(Client *c, const Arg *arg);
194 static void scroll(GtkAdjustment *a, const Arg *arg);
195 static void setatom(Client *c, int a, const char *v);
196 static void setup(void);
197 static void sigchld(int unused);
198 static void source(Client *c, const Arg *arg);
199 static void spawn(Client *c, const Arg *arg);
200 static void stop(Client *c, const Arg *arg);
201 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
202 static void titlechangeleave(void *a, void *b, Client *c);
203 static void toggle(Client *c, const Arg *arg);
204 static void togglecookiepolicy(Client *c, const Arg *arg);
205 static void togglegeolocation(Client *c, const Arg *arg);
206 static void togglescrollbars(Client *c, const Arg *arg);
207 static void togglestyle(Client *c, const Arg *arg);
208 static void updatetitle(Client *c);
209 static void updatewinid(Client *c);
210 static void usage(void);
211 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
212 JSContextRef js, JSObjectRef win, Client *c);
213 static void zoom(Client *c, const Arg *arg);
215 /* configuration, allows nested code to access above variables */
219 addaccelgroup(Client *c)
222 GtkAccelGroup *group = gtk_accel_group_new();
225 for (i = 0; i < LENGTH(keys); i++) {
226 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
227 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
230 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
234 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
235 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
238 const gchar *uri = webkit_network_request_get_uri(req);
241 if (g_str_has_suffix(uri, "/favicon.ico"))
242 webkit_network_request_set_uri(req, "about:blank");
244 if (!g_str_has_prefix(uri, "http://")
245 && !g_str_has_prefix(uri, "https://")
246 && !g_str_has_prefix(uri, "about:")
247 && !g_str_has_prefix(uri, "file://")
248 && !g_str_has_prefix(uri, "data:")
249 && !g_str_has_prefix(uri, "blob:")
250 && strlen(uri) > 0) {
251 for (i = 0; i < strlen(uri); i++) {
252 if (!g_ascii_isprint(uri[i])) {
258 handleplumb(c, w, uri);
263 buildfile(const char *path)
265 char *dname, *bname, *bpath, *fpath;
268 dname = g_path_get_dirname(path);
269 bname = g_path_get_basename(path);
271 bpath = buildpath(dname);
274 fpath = g_build_filename(bpath, bname, NULL);
277 if (!(f = fopen(fpath, "a")))
278 die("Could not open file: %s\n", fpath);
280 g_chmod(fpath, 0600); /* always */
287 buildpath(const char *path)
290 char *apath, *name, *p, *fpath;
292 if (path[0] == '~') {
293 if (path[1] == '/' || path[1] == '\0') {
294 p = (char *)&path[1];
295 pw = getpwuid(getuid());
297 if ((p = strchr(path, '/')))
298 name = g_strndup(&path[1], --p - path);
300 name = g_strdup(&path[1]);
302 if (!(pw = getpwnam(name))) {
303 die("Can't get user %s home directory: %s.\n",
308 apath = g_build_filename(pw->pw_dir, p, NULL);
310 apath = g_strdup(path);
313 /* creating directory */
314 if (g_mkdir_with_parents(apath, 0700) < 0)
315 die("Could not access directory: %s\n", apath);
317 fpath = realpath(apath, NULL);
324 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
326 WebKitHitTestResultContext context;
327 WebKitHitTestResult *result;
331 result = webkit_web_view_get_hit_test_result(web, e);
332 g_object_get(result, "context", &context, NULL);
333 g_object_get(result, "link-uri", &arg.v, NULL);
334 for (i = 0; i < LENGTH(buttons); i++) {
335 if (context & buttons[i].click
336 && e->button == buttons[i].button
337 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
338 && buttons[i].func) {
339 buttons[i].func(c, buttons[i].click == ClkLink
340 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
351 soup_cache_flush(diskcache);
352 soup_cache_dump(diskcache);
355 destroyclient(clients);
362 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
363 SoupCookie *new_cookie)
365 flock(COOKIEJAR(self)->lock, LOCK_EX);
366 if (new_cookie && !new_cookie->expires && sessiontime) {
367 soup_cookie_set_expires(new_cookie,
368 soup_date_new_from_now(sessiontime));
370 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
373 flock(COOKIEJAR(self)->lock, LOCK_UN);
377 cookiejar_class_init(CookieJarClass *klass)
379 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
380 G_OBJECT_CLASS(klass)->get_property =
381 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
382 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
383 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
384 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
388 cookiejar_finalize(GObject *self)
390 close(COOKIEJAR(self)->lock);
391 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
395 cookiejar_init(CookieJar *self)
397 self->lock = open(cookiefile, 0);
401 cookiejar_new(const char *filename, gboolean read_only,
402 SoupCookieJarAcceptPolicy policy)
404 return g_object_new(COOKIEJAR_TYPE,
405 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
406 SOUP_COOKIE_JAR_READ_ONLY, read_only,
407 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
411 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
414 flock(COOKIEJAR(self)->lock, LOCK_SH);
415 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
417 flock(COOKIEJAR(self)->lock, LOCK_UN);
420 SoupCookieJarAcceptPolicy
421 cookiepolicy_get(void)
423 switch (cookiepolicies[policysel]) {
425 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
427 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
433 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
437 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep)
440 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
442 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
444 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
453 evalscript(JSContextRef js, char *script, char* scriptname)
455 JSStringRef jsscript, jsscriptname;
456 JSValueRef exception = NULL;
458 jsscript = JSStringCreateWithUTF8CString(script);
459 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
460 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
461 jsscriptname, 0, &exception);
462 JSStringRelease(jsscript);
463 JSStringRelease(jsscriptname);
467 runscript(WebKitWebFrame *frame)
472 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
473 evalscript(webkit_web_frame_get_global_context(frame), script,
479 clipboard(Client *c, const Arg *arg)
481 gboolean paste = *(gboolean *)arg;
484 gtk_clipboard_request_text(gtk_clipboard_get(
485 GDK_SELECTION_PRIMARY),
488 gtk_clipboard_set_text(gtk_clipboard_get(
489 GDK_SELECTION_PRIMARY), c->linkhover
490 ? c->linkhover : geturi(c), -1);
495 copystr(char **str, const char *src)
508 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
510 Client *n = newclient();
515 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
516 gchar *m, WebKitWebPolicyDecision *p, Client *c)
518 if (!webkit_web_view_can_show_mime_type(v, m)) {
519 webkit_web_policy_decision_download(p);
526 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
527 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
532 if (webkit_web_navigation_action_get_reason(n)
533 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
534 webkit_web_policy_decision_ignore(p);
535 arg.v = (void *)webkit_network_request_get_uri(r);
536 newwindow(NULL, &arg, 0);
543 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
549 destroyclient(Client *c)
553 webkit_web_view_stop_loading(c->view);
554 gtk_widget_destroy(GTK_WIDGET(c->view));
555 gtk_widget_destroy(c->scroll);
556 gtk_widget_destroy(c->vbox);
557 gtk_widget_destroy(c->win);
559 for (p = clients; p && p->next != c; p = p->next)
571 destroywin(GtkWidget* w, Client *c)
577 die(const char *errstr, ...)
581 va_start(ap, errstr);
582 vfprintf(stderr, errstr, ap);
588 find(Client *c, const Arg *arg)
592 s = getatom(c, AtomFind);
593 gboolean forward = *(gboolean *)arg;
594 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
598 fullscreen(Client *c, const Arg *arg)
601 gtk_window_unfullscreen(GTK_WINDOW(c->win));
603 gtk_window_fullscreen(GTK_WINDOW(c->win));
604 c->fullscreen = !c->fullscreen;
608 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
609 WebKitGeolocationPolicyDecision *d, Client *c)
611 if (allowgeolocation)
612 webkit_geolocation_policy_allow(d);
614 webkit_geolocation_policy_deny(d);
618 getatom(Client *c, int a)
620 static char buf[BUFSIZ];
623 unsigned long ldummy;
624 unsigned char *p = NULL;
626 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
627 atoms[a], 0L, BUFSIZ, False, XA_STRING,
628 &adummy, &idummy, &ldummy, &ldummy, &p);
630 strncpy(buf, (char *)p, LENGTH(buf)-1);
643 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
649 getstyle(const char *uri)
653 if (stylefile != NULL)
654 return g_strconcat("file://", stylefile, NULL);
656 for (i = 0; i < LENGTH(styles); i++) {
657 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
659 return g_strconcat("file://", styles[i].style, NULL);
665 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
669 webkit_web_view_stop_loading(w);
670 arg = (Arg)PLUMB((char *)uri);
675 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
680 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
686 inspector(Client *c, const Arg *arg)
689 webkit_web_inspector_close(c->inspector);
691 webkit_web_inspector_show(c->inspector);
695 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
697 return WEBKIT_WEB_VIEW(webkit_web_view_new());
701 inspector_show(WebKitWebInspector *i, Client *c)
708 w = webkit_web_inspector_get_web_view(i);
709 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
710 gtk_widget_show(GTK_WIDGET(w));
711 c->isinspecting = true;
717 inspector_close(WebKitWebInspector *i, Client *c)
721 if (!c->isinspecting)
724 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
726 gtk_widget_destroy(w);
727 c->isinspecting = false;
733 inspector_finished(WebKitWebInspector *i, Client *c)
735 g_free(c->inspector);
739 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
743 gboolean processed = FALSE;
745 mods = CLEANMASK(mods);
746 key = gdk_keyval_to_lower(key);
748 for (i = 0; i < LENGTH(keys); i++) {
749 if (key == keys[i].keyval
750 && mods == keys[i].mod
752 keys[i].func(c, &(keys[i].arg));
761 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
764 c->linkhover = copystr(&c->linkhover, l);
765 } else if (c->linkhover) {
773 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
775 WebKitWebFrame *frame;
776 WebKitWebDataSource *src;
777 WebKitNetworkRequest *request;
778 WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
782 switch (webkit_web_view_get_load_status (c->view)) {
783 case WEBKIT_LOAD_COMMITTED:
785 if (strstr(uri, "https://") == uri) {
786 frame = webkit_web_view_get_main_frame(c->view);
787 src = webkit_web_frame_get_data_source(frame);
788 request = webkit_web_data_source_get_request(src);
789 msg = webkit_network_request_get_message(request);
790 c->sslfailed = !(soup_message_get_flags(msg)
791 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
793 setatom(c, AtomUri, uri);
796 g_object_set(G_OBJECT(set), "user-stylesheet-uri",
797 getstyle(uri), NULL);
800 case WEBKIT_LOAD_FINISHED:
804 soup_cache_flush(diskcache);
805 soup_cache_dump(diskcache);
814 loaduri(Client *c, const Arg *arg)
817 const char *uri = (char *)arg->v;
818 Arg a = { .b = FALSE };
821 if (strcmp(uri, "") == 0)
824 /* In case it's a file path. */
825 if (stat(uri, &st) == 0) {
826 rp = realpath(uri, NULL);
827 u = g_strdup_printf("file://%s", rp);
830 u = g_strrstr(uri, "://") ? g_strdup(uri)
831 : g_strdup_printf("http://%s", uri);
834 setatom(c, AtomUri, uri);
836 /* prevents endless loop */
837 if (strcmp(u, geturi(c)) == 0) {
840 webkit_web_view_load_uri(c->view, u);
842 c->title = copystr(&c->title, u);
849 navigate(Client *c, const Arg *arg)
851 int steps = *(int *)arg;
852 webkit_web_view_go_back_or_forward(c->view, steps);
859 WebKitWebSettings *settings;
860 WebKitWebFrame *frame;
861 GdkGeometry hints = { 1, 1 };
866 if (!(c = calloc(1, sizeof(Client))))
867 die("Cannot malloc!\n");
874 c->win = gtk_plug_new(embed);
876 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
878 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
879 * is always correct, so we should still call this function.
880 * But when doing so, we *must* differentiate between a
881 * WM_CLASS and a resource on the window. By convention, the
882 * window class (WM_CLASS) is capped, while the resource is in
883 * lowercase. Both these values come as a pair.
885 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
887 /* TA: 20091214: And set the role here as well -- so that
888 * sessions can pick this up.
890 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
892 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
893 g_signal_connect(G_OBJECT(c->win),
895 G_CALLBACK(destroywin), c);
896 g_signal_connect(G_OBJECT(c->win),
897 "leave_notify_event",
898 G_CALLBACK(titlechangeleave), c);
904 c->pane = gtk_vpaned_new();
907 c->vbox = gtk_vbox_new(FALSE, 0);
908 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
911 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
913 g_signal_connect(G_OBJECT(c->view),
915 G_CALLBACK(titlechange), c);
916 g_signal_connect(G_OBJECT(c->view),
917 "hovering-over-link",
918 G_CALLBACK(linkhover), c);
919 g_signal_connect(G_OBJECT(c->view),
920 "geolocation-policy-decision-requested",
921 G_CALLBACK(geopolicyrequested), c);
922 g_signal_connect(G_OBJECT(c->view),
924 G_CALLBACK(createwindow), c);
925 g_signal_connect(G_OBJECT(c->view),
926 "new-window-policy-decision-requested",
927 G_CALLBACK(decidewindow), c);
928 g_signal_connect(G_OBJECT(c->view),
929 "mime-type-policy-decision-requested",
930 G_CALLBACK(decidedownload), c);
931 g_signal_connect(G_OBJECT(c->view),
932 "window-object-cleared",
933 G_CALLBACK(windowobjectcleared), c);
934 g_signal_connect(G_OBJECT(c->view),
935 "notify::load-status",
936 G_CALLBACK(loadstatuschange), c);
937 g_signal_connect(G_OBJECT(c->view),
939 G_CALLBACK(progresschange), c);
940 g_signal_connect(G_OBJECT(c->view),
941 "download-requested",
942 G_CALLBACK(initdownload), c);
943 g_signal_connect(G_OBJECT(c->view),
944 "button-release-event",
945 G_CALLBACK(buttonrelease), c);
946 g_signal_connect(G_OBJECT(c->view),
948 G_CALLBACK(contextmenu), c);
949 g_signal_connect(G_OBJECT(c->view),
950 "resource-request-starting",
951 G_CALLBACK(beforerequest), c);
952 g_signal_connect(G_OBJECT(c->view),
953 "should-show-delete-interface-for-element",
954 G_CALLBACK(deletion_interface), c);
956 /* Scrolled Window */
957 c->scroll = gtk_scrolled_window_new(NULL, NULL);
959 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
960 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
961 G_CALLBACK(gtk_true), NULL);
963 if (!enablescrollbars) {
964 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
968 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
969 GTK_POLICY_AUTOMATIC,
970 GTK_POLICY_AUTOMATIC);
974 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
975 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
976 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
979 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
981 gtk_widget_grab_focus(GTK_WIDGET(c->view));
982 gtk_widget_show(c->pane);
983 gtk_widget_show(c->vbox);
984 gtk_widget_show(c->scroll);
985 gtk_widget_show(GTK_WIDGET(c->view));
986 gtk_widget_show(c->win);
987 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
989 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
990 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
991 webkit_web_view_set_full_content_zoom(c->view, TRUE);
995 settings = webkit_web_view_get_settings(c->view);
996 if (!(ua = getenv("SURF_USERAGENT")))
998 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
1000 g_object_set(G_OBJECT(settings),
1001 "user-stylesheet-uri", getstyle("about:blank"),
1004 g_object_set(G_OBJECT(settings),
1005 "auto-load-images", loadimages, NULL);
1006 g_object_set(G_OBJECT(settings),
1007 "enable-plugins", enableplugins, NULL);
1008 g_object_set(G_OBJECT(settings),
1009 "enable-scripts", enablescripts, NULL);
1010 g_object_set(G_OBJECT(settings),
1011 "enable-spatial-navigation", enablespatialbrowsing, NULL);
1012 g_object_set(G_OBJECT(settings),
1013 "enable-developer-extras", enableinspector, NULL);
1014 g_object_set(G_OBJECT(settings),
1015 "enable-default-context-menu", kioskmode ^ 1, NULL);
1016 g_object_set(G_OBJECT(settings),
1017 "default-font-size", defaultfontsize, NULL);
1018 g_object_set(G_OBJECT(settings),
1019 "resizable-text-areas", 1, NULL);
1022 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
1023 * This ensures websites are not unusably small with a high DPI screen.
1024 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
1027 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
1028 dpi = gdk_screen_get_resolution(screen);
1030 g_object_set(G_OBJECT(settings),
1031 "enforce-96-dpi", true, NULL);
1032 webkit_web_view_set_zoom_level(c->view, dpi/96);
1035 /* This might conflict with _zoomto96dpi_. */
1036 if (zoomlevel != 1.0)
1037 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1039 if (enableinspector) {
1040 c->inspector = webkit_web_view_get_inspector(c->view);
1041 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
1042 G_CALLBACK(inspector_new), c);
1043 g_signal_connect(G_OBJECT(c->inspector), "show-window",
1044 G_CALLBACK(inspector_show), c);
1045 g_signal_connect(G_OBJECT(c->inspector), "close-window",
1046 G_CALLBACK(inspector_close), c);
1047 g_signal_connect(G_OBJECT(c->inspector), "finished",
1048 G_CALLBACK(inspector_finished), c);
1049 c->isinspecting = false;
1052 if (runinfullscreen) {
1054 fullscreen(c, NULL);
1057 setatom(c, AtomFind, "");
1058 setatom(c, AtomUri, "about:blank");
1060 webkit_web_view_set_transparent(c->view, TRUE);
1066 gdk_display_sync(gtk_widget_get_display(c->win));
1068 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1070 if (fclose(stdout) != 0) {
1071 die("Error closing stdout");
1079 newwindow(Client *c, const Arg *arg, gboolean noembed)
1082 const char *cmd[18], *uri;
1083 const Arg a = { .v = (void *)cmd };
1088 cmd[i++] = cookiepolicies;
1089 if (!enablescrollbars)
1091 if (embed && !noembed) {
1093 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1096 if (!allowgeolocation)
1108 if (enablediskcache)
1111 cmd[i++] = cookiefile;
1113 uri = arg->v ? (char *)arg->v : c->linkhover;
1121 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1122 gboolean keyboard, Client *c)
1124 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1126 for (GList *l = items; l; l = l->next)
1127 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1134 menuactivate(GtkMenuItem *item, Client *c)
1137 * context-menu-action-2000 open link
1138 * context-menu-action-1 open link in window
1139 * context-menu-action-2 download linked file
1140 * context-menu-action-3 copy link location
1141 * context-menu-action-7 copy image address
1142 * context-menu-action-13 reload
1143 * context-menu-action-10 back
1144 * context-menu-action-11 forward
1145 * context-menu-action-12 stop
1148 GtkAction *a = NULL;
1149 const char *name, *uri;
1150 GtkClipboard *prisel, *clpbrd;
1152 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
1156 name = gtk_action_get_name(a);
1157 if (!g_strcmp0(name, "context-menu-action-3")) {
1158 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1159 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1160 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1161 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1162 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1163 uri = gtk_clipboard_wait_for_text(clpbrd);
1165 gtk_clipboard_set_text(prisel, uri, -1);
1170 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1172 Arg arg = {.v = text };
1174 loaduri((Client *) d, &arg);
1178 print(Client *c, const Arg *arg)
1180 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1184 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1186 Client *c = (Client *)d;
1190 if (((XEvent *)e)->type == PropertyNotify) {
1191 ev = &((XEvent *)e)->xproperty;
1192 if (ev->state == PropertyNewValue) {
1193 if (ev->atom == atoms[AtomFind]) {
1197 return GDK_FILTER_REMOVE;
1198 } else if (ev->atom == atoms[AtomGo]) {
1199 arg.v = getatom(c, AtomGo);
1202 return GDK_FILTER_REMOVE;
1206 return GDK_FILTER_CONTINUE;
1210 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1212 c->progress = webkit_web_view_get_progress(c->view) * 100;
1217 linkopen(Client *c, const Arg *arg)
1219 newwindow(NULL, arg, 1);
1223 linkopenembed(Client *c, const Arg *arg)
1225 newwindow(NULL, arg, 0);
1229 reload(Client *c, const Arg *arg)
1231 gboolean nocache = *(gboolean *)arg;
1233 webkit_web_view_reload_bypass_cache(c->view);
1235 webkit_web_view_reload(c->view);
1239 scroll_h(Client *c, const Arg *arg)
1241 scroll(gtk_scrolled_window_get_hadjustment(
1242 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1246 scroll_v(Client *c, const Arg *arg)
1248 scroll(gtk_scrolled_window_get_vadjustment(
1249 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1253 scroll(GtkAdjustment *a, const Arg *arg)
1257 v = gtk_adjustment_get_value(a);
1261 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1266 v += gtk_adjustment_get_step_increment(a) * arg->i;
1270 v = MIN(v, gtk_adjustment_get_upper(a) -
1271 gtk_adjustment_get_page_size(a));
1272 gtk_adjustment_set_value(a, v);
1276 setatom(Client *c, int a, const char *v)
1279 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1280 atoms[a], XA_STRING, 8, PropModeReplace,
1281 (unsigned char *)v, strlen(v) + 1);
1292 GError *error = NULL;
1294 /* clean up any zombies immediately */
1296 gtk_init(NULL, NULL);
1298 dpy = GDK_DISPLAY();
1301 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1302 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1303 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1305 /* dirs and files */
1306 cookiefile = buildfile(cookiefile);
1307 scriptfile = buildfile(scriptfile);
1308 cachefolder = buildpath(cachefolder);
1309 styledir = buildpath(styledir);
1310 if (stylefile == NULL) {
1311 for (i = 0; i < LENGTH(styles); i++) {
1312 if (regcomp(&(styles[i].re), styles[i].regex,
1315 "Could not compile regex: %s\n",
1317 styles[i].regex = NULL;
1319 styles[i].style = buildfile(g_strconcat(styledir, "/",
1320 styles[i].style, NULL));
1323 stylefile = buildfile(stylefile);
1326 /* request handler */
1327 s = webkit_get_default_session();
1330 soup_session_add_feature(s,
1331 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1332 FALSE, cookiepolicy_get())));
1335 if (enablediskcache) {
1336 diskcache = soup_cache_new(cachefolder,
1337 SOUP_CACHE_SINGLE_USER);
1338 soup_cache_set_max_size(diskcache, diskcachebytes);
1339 soup_cache_load(diskcache);
1340 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1344 tlsdb = g_tls_file_database_new(cafile, &error);
1347 g_warning("Error loading SSL database %s: %s", cafile,
1349 g_error_free(error);
1351 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1352 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1355 if ((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1356 new_proxy = g_strrstr(proxy, "http://")
1357 || g_strrstr(proxy, "socks://")
1358 || g_strrstr(proxy, "socks4://")
1359 || g_strrstr(proxy, "socks5://")
1361 : g_strdup_printf("http://%s", proxy);
1362 puri = soup_uri_new(new_proxy);
1363 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1364 soup_uri_free(puri);
1373 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1374 die("Can't install SIGCHLD handler");
1375 while (0 < waitpid(-1, NULL, WNOHANG));
1379 source(Client *c, const Arg *arg)
1381 Arg a = { .b = FALSE };
1384 s = webkit_web_view_get_view_source_mode(c->view);
1385 webkit_web_view_set_view_source_mode(c->view, !s);
1390 spawn(Client *c, const Arg *arg)
1394 close(ConnectionNumber(dpy));
1396 execvp(((char **)arg->v)[0], (char **)arg->v);
1397 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1404 eval(Client *c, const Arg *arg)
1406 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1407 evalscript(webkit_web_frame_get_global_context(frame),
1408 ((char **)arg->v)[0], "");
1412 stop(Client *c, const Arg *arg)
1414 webkit_web_view_stop_loading(c->view);
1418 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1420 const gchar *t = webkit_web_view_get_title(view);
1422 c->title = copystr(&c->title, t);
1428 titlechangeleave(void *a, void *b, Client *c)
1430 c->linkhover = NULL;
1435 toggle(Client *c, const Arg *arg)
1437 WebKitWebSettings *settings;
1438 char *name = (char *)arg->v;
1440 Arg a = { .b = FALSE };
1442 settings = webkit_web_view_get_settings(c->view);
1443 g_object_get(G_OBJECT(settings), name, &value, NULL);
1444 g_object_set(G_OBJECT(settings), name, !value, NULL);
1450 togglecookiepolicy(Client *c, const Arg *arg)
1453 SoupCookieJarAcceptPolicy policy;
1455 jar = SOUP_COOKIE_JAR(soup_session_get_feature(
1456 webkit_get_default_session(),
1457 SOUP_TYPE_COOKIE_JAR));
1458 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1461 if (policysel >= strlen(cookiepolicies))
1464 g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);
1467 /* Do not reload. */
1471 togglegeolocation(Client *c, const Arg *arg)
1473 Arg a = { .b = FALSE };
1475 allowgeolocation ^= 1;
1480 twitch(Client *c, const Arg *arg)
1485 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1488 v = gtk_adjustment_get_value(a);
1493 v = MIN(v, gtk_adjustment_get_upper(a) -
1494 gtk_adjustment_get_page_size(a));
1495 gtk_adjustment_set_value(a, v);
1499 togglescrollbars(Client *c, const Arg *arg)
1501 GtkPolicyType vspolicy;
1504 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1507 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1508 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1512 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1513 GTK_POLICY_AUTOMATIC,
1514 GTK_POLICY_AUTOMATIC);
1523 togglestyle(Client *c, const Arg *arg)
1525 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1528 enablestyles = !enablestyles;
1529 uri = enablestyles ? getstyle(geturi(c)) : g_strdup("");
1530 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1536 gettogglestat(Client *c)
1540 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1542 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1544 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1546 togglestat[p++] = value? 'C': 'c';
1548 togglestat[p++] = allowgeolocation? 'G': 'g';
1550 togglestat[p++] = enablediskcache? 'D': 'd';
1552 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1553 togglestat[p++] = value? 'I': 'i';
1555 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1556 togglestat[p++] = value? 'S': 's';
1558 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1559 togglestat[p++] = value? 'V': 'v';
1561 togglestat[p++] = enablestyles ? 'M': 'm';
1563 togglestat[p] = '\0';
1567 getpagestat(Client *c)
1569 const char *uri = geturi(c);
1571 if (strstr(uri, "https://") == uri)
1572 pagestat[0] = c->sslfailed ? 'U' : 'T';
1576 pagestat[1] = usingproxy ? 'P' : '-';
1581 updatetitle(Client *c)
1585 if (showindicators) {
1590 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1592 } else if (c->progress != 100) {
1593 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1594 togglestat, pagestat,
1595 c->title == NULL ? "" : c->title);
1597 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1598 c->title == NULL ? "" : c->title);
1601 gtk_window_set_title(GTK_WINDOW(c->win), t);
1604 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1610 updatewinid(Client *c)
1612 snprintf(winid, LENGTH(winid), "%u",
1613 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1619 die("usage: %s [-bBfFgGiIkKnNpPsSvx] [-a cookiepolicies ] "
1620 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1621 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1625 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1626 JSObjectRef win, Client *c)
1632 zoom(Client *c, const Arg *arg)
1637 webkit_web_view_zoom_out(c->view);
1638 } else if (arg->i > 0) {
1640 webkit_web_view_zoom_in(c->view);
1644 webkit_web_view_set_zoom_level(c->view, 1.0);
1649 main(int argc, char *argv[])
1654 memset(&arg, 0, sizeof(arg));
1656 /* command line args */
1659 cookiepolicies = EARGF(usage());
1662 enablescrollbars = 0;
1665 enablescrollbars = 1;
1668 cookiefile = EARGF(usage());
1671 enablediskcache = 0;
1674 enablediskcache = 1;
1677 embed = strtol(EARGF(usage()), NULL, 0);
1680 runinfullscreen = 1;
1683 runinfullscreen = 0;
1686 allowgeolocation = 0;
1689 allowgeolocation = 1;
1710 enableinspector = 0;
1713 enableinspector = 1;
1722 scriptfile = EARGF(usage());
1731 stylefile = EARGF(usage());
1734 useragent = EARGF(usage());
1737 die("surf-"VERSION", ©2009-2015 surf engineers, "
1738 "see LICENSE for details\n");
1743 zoomlevel = strtof(EARGF(usage()), NULL);
1754 loaduri(clients, &arg);
1761 return EXIT_SUCCESS;