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 const 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)
656 for (i = 0; i < LENGTH(styles); i++) {
657 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
659 return styles[i].style;
666 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
670 webkit_web_view_stop_loading(w);
671 arg = (Arg)PLUMB((char *)uri);
676 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
681 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
687 inspector(Client *c, const Arg *arg)
690 webkit_web_inspector_close(c->inspector);
692 webkit_web_inspector_show(c->inspector);
696 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
698 return WEBKIT_WEB_VIEW(webkit_web_view_new());
702 inspector_show(WebKitWebInspector *i, Client *c)
709 w = webkit_web_inspector_get_web_view(i);
710 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
711 gtk_widget_show(GTK_WIDGET(w));
712 c->isinspecting = true;
718 inspector_close(WebKitWebInspector *i, Client *c)
722 if (!c->isinspecting)
725 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
727 gtk_widget_destroy(w);
728 c->isinspecting = false;
734 inspector_finished(WebKitWebInspector *i, Client *c)
736 g_free(c->inspector);
740 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
744 gboolean processed = FALSE;
746 mods = CLEANMASK(mods);
747 key = gdk_keyval_to_lower(key);
749 for (i = 0; i < LENGTH(keys); i++) {
750 if (key == keys[i].keyval
751 && mods == keys[i].mod
753 keys[i].func(c, &(keys[i].arg));
762 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
765 c->linkhover = copystr(&c->linkhover, l);
766 } else if (c->linkhover) {
774 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
776 WebKitWebFrame *frame;
777 WebKitWebDataSource *src;
778 WebKitNetworkRequest *request;
779 WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
783 switch (webkit_web_view_get_load_status (c->view)) {
784 case WEBKIT_LOAD_COMMITTED:
786 if (strstr(uri, "https://") == uri) {
787 frame = webkit_web_view_get_main_frame(c->view);
788 src = webkit_web_frame_get_data_source(frame);
789 request = webkit_web_data_source_get_request(src);
790 msg = webkit_network_request_get_message(request);
791 c->sslfailed = !(soup_message_get_flags(msg)
792 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
794 setatom(c, AtomUri, uri);
797 g_object_set(G_OBJECT(set), "user-stylesheet-uri",
798 getstyle(uri), NULL);
801 case WEBKIT_LOAD_FINISHED:
805 soup_cache_flush(diskcache);
806 soup_cache_dump(diskcache);
815 loaduri(Client *c, const Arg *arg)
818 const char *uri = (char *)arg->v;
819 Arg a = { .b = FALSE };
822 if (strcmp(uri, "") == 0)
825 /* In case it's a file path. */
826 if (stat(uri, &st) == 0) {
827 rp = realpath(uri, NULL);
828 u = g_strdup_printf("file://%s", rp);
831 u = g_strrstr(uri, "://") ? g_strdup(uri)
832 : g_strdup_printf("http://%s", uri);
835 setatom(c, AtomUri, uri);
837 /* prevents endless loop */
838 if (strcmp(u, geturi(c)) == 0) {
841 webkit_web_view_load_uri(c->view, u);
843 c->title = copystr(&c->title, u);
850 navigate(Client *c, const Arg *arg)
852 int steps = *(int *)arg;
853 webkit_web_view_go_back_or_forward(c->view, steps);
860 WebKitWebSettings *settings;
861 WebKitWebFrame *frame;
862 GdkGeometry hints = { 1, 1 };
867 if (!(c = calloc(1, sizeof(Client))))
868 die("Cannot malloc!\n");
875 c->win = gtk_plug_new(embed);
877 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
879 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
880 * is always correct, so we should still call this function.
881 * But when doing so, we *must* differentiate between a
882 * WM_CLASS and a resource on the window. By convention, the
883 * window class (WM_CLASS) is capped, while the resource is in
884 * lowercase. Both these values come as a pair.
886 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
888 /* TA: 20091214: And set the role here as well -- so that
889 * sessions can pick this up.
891 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
893 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
894 g_signal_connect(G_OBJECT(c->win),
896 G_CALLBACK(destroywin), c);
897 g_signal_connect(G_OBJECT(c->win),
898 "leave_notify_event",
899 G_CALLBACK(titlechangeleave), c);
905 c->pane = gtk_vpaned_new();
908 c->vbox = gtk_vbox_new(FALSE, 0);
909 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
912 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
914 g_signal_connect(G_OBJECT(c->view),
916 G_CALLBACK(titlechange), c);
917 g_signal_connect(G_OBJECT(c->view),
918 "hovering-over-link",
919 G_CALLBACK(linkhover), c);
920 g_signal_connect(G_OBJECT(c->view),
921 "geolocation-policy-decision-requested",
922 G_CALLBACK(geopolicyrequested), c);
923 g_signal_connect(G_OBJECT(c->view),
925 G_CALLBACK(createwindow), c);
926 g_signal_connect(G_OBJECT(c->view),
927 "new-window-policy-decision-requested",
928 G_CALLBACK(decidewindow), c);
929 g_signal_connect(G_OBJECT(c->view),
930 "mime-type-policy-decision-requested",
931 G_CALLBACK(decidedownload), c);
932 g_signal_connect(G_OBJECT(c->view),
933 "window-object-cleared",
934 G_CALLBACK(windowobjectcleared), c);
935 g_signal_connect(G_OBJECT(c->view),
936 "notify::load-status",
937 G_CALLBACK(loadstatuschange), c);
938 g_signal_connect(G_OBJECT(c->view),
940 G_CALLBACK(progresschange), c);
941 g_signal_connect(G_OBJECT(c->view),
942 "download-requested",
943 G_CALLBACK(initdownload), c);
944 g_signal_connect(G_OBJECT(c->view),
945 "button-release-event",
946 G_CALLBACK(buttonrelease), c);
947 g_signal_connect(G_OBJECT(c->view),
949 G_CALLBACK(contextmenu), c);
950 g_signal_connect(G_OBJECT(c->view),
951 "resource-request-starting",
952 G_CALLBACK(beforerequest), c);
953 g_signal_connect(G_OBJECT(c->view),
954 "should-show-delete-interface-for-element",
955 G_CALLBACK(deletion_interface), c);
957 /* Scrolled Window */
958 c->scroll = gtk_scrolled_window_new(NULL, NULL);
960 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
961 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
962 G_CALLBACK(gtk_true), NULL);
964 if (!enablescrollbars) {
965 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
969 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
970 GTK_POLICY_AUTOMATIC,
971 GTK_POLICY_AUTOMATIC);
975 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
976 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
977 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
980 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
982 gtk_widget_grab_focus(GTK_WIDGET(c->view));
983 gtk_widget_show(c->pane);
984 gtk_widget_show(c->vbox);
985 gtk_widget_show(c->scroll);
986 gtk_widget_show(GTK_WIDGET(c->view));
987 gtk_widget_show(c->win);
988 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
990 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
991 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
992 webkit_web_view_set_full_content_zoom(c->view, TRUE);
996 settings = webkit_web_view_get_settings(c->view);
997 if (!(ua = getenv("SURF_USERAGENT")))
999 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
1001 g_object_set(G_OBJECT(settings),
1002 "user-stylesheet-uri", getstyle("about:blank"),
1005 g_object_set(G_OBJECT(settings),
1006 "auto-load-images", loadimages, NULL);
1007 g_object_set(G_OBJECT(settings),
1008 "enable-plugins", enableplugins, NULL);
1009 g_object_set(G_OBJECT(settings),
1010 "enable-scripts", enablescripts, NULL);
1011 g_object_set(G_OBJECT(settings),
1012 "enable-spatial-navigation", enablespatialbrowsing, NULL);
1013 g_object_set(G_OBJECT(settings),
1014 "enable-developer-extras", enableinspector, NULL);
1015 g_object_set(G_OBJECT(settings),
1016 "enable-default-context-menu", kioskmode ^ 1, NULL);
1017 g_object_set(G_OBJECT(settings),
1018 "default-font-size", defaultfontsize, NULL);
1019 g_object_set(G_OBJECT(settings),
1020 "resizable-text-areas", 1, NULL);
1023 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
1024 * This ensures websites are not unusably small with a high DPI screen.
1025 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
1028 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
1029 dpi = gdk_screen_get_resolution(screen);
1031 g_object_set(G_OBJECT(settings),
1032 "enforce-96-dpi", true, NULL);
1033 webkit_web_view_set_zoom_level(c->view, dpi/96);
1036 /* This might conflict with _zoomto96dpi_. */
1037 if (zoomlevel != 1.0)
1038 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1040 if (enableinspector) {
1041 c->inspector = webkit_web_view_get_inspector(c->view);
1042 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
1043 G_CALLBACK(inspector_new), c);
1044 g_signal_connect(G_OBJECT(c->inspector), "show-window",
1045 G_CALLBACK(inspector_show), c);
1046 g_signal_connect(G_OBJECT(c->inspector), "close-window",
1047 G_CALLBACK(inspector_close), c);
1048 g_signal_connect(G_OBJECT(c->inspector), "finished",
1049 G_CALLBACK(inspector_finished), c);
1050 c->isinspecting = false;
1053 if (runinfullscreen) {
1055 fullscreen(c, NULL);
1058 setatom(c, AtomFind, "");
1059 setatom(c, AtomUri, "about:blank");
1061 webkit_web_view_set_transparent(c->view, TRUE);
1067 gdk_display_sync(gtk_widget_get_display(c->win));
1069 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1071 if (fclose(stdout) != 0) {
1072 die("Error closing stdout");
1080 newwindow(Client *c, const Arg *arg, gboolean noembed)
1083 const char *cmd[18], *uri;
1084 const Arg a = { .v = (void *)cmd };
1089 cmd[i++] = cookiepolicies;
1090 if (!enablescrollbars)
1092 if (embed && !noembed) {
1094 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1097 if (!allowgeolocation)
1109 if (enablediskcache)
1112 cmd[i++] = cookiefile;
1114 uri = arg->v ? (char *)arg->v : c->linkhover;
1122 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1123 gboolean keyboard, Client *c)
1125 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1127 for (GList *l = items; l; l = l->next)
1128 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1135 menuactivate(GtkMenuItem *item, Client *c)
1138 * context-menu-action-2000 open link
1139 * context-menu-action-1 open link in window
1140 * context-menu-action-2 download linked file
1141 * context-menu-action-3 copy link location
1142 * context-menu-action-7 copy image address
1143 * context-menu-action-13 reload
1144 * context-menu-action-10 back
1145 * context-menu-action-11 forward
1146 * context-menu-action-12 stop
1149 GtkAction *a = NULL;
1150 const char *name, *uri;
1151 GtkClipboard *prisel, *clpbrd;
1153 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
1157 name = gtk_action_get_name(a);
1158 if (!g_strcmp0(name, "context-menu-action-3")) {
1159 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1160 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1161 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1162 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1163 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1164 uri = gtk_clipboard_wait_for_text(clpbrd);
1166 gtk_clipboard_set_text(prisel, uri, -1);
1171 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1173 Arg arg = {.v = text };
1175 loaduri((Client *) d, &arg);
1179 print(Client *c, const Arg *arg)
1181 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1185 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1187 Client *c = (Client *)d;
1191 if (((XEvent *)e)->type == PropertyNotify) {
1192 ev = &((XEvent *)e)->xproperty;
1193 if (ev->state == PropertyNewValue) {
1194 if (ev->atom == atoms[AtomFind]) {
1198 return GDK_FILTER_REMOVE;
1199 } else if (ev->atom == atoms[AtomGo]) {
1200 arg.v = getatom(c, AtomGo);
1203 return GDK_FILTER_REMOVE;
1207 return GDK_FILTER_CONTINUE;
1211 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1213 c->progress = webkit_web_view_get_progress(c->view) * 100;
1218 linkopen(Client *c, const Arg *arg)
1220 newwindow(NULL, arg, 1);
1224 linkopenembed(Client *c, const Arg *arg)
1226 newwindow(NULL, arg, 0);
1230 reload(Client *c, const Arg *arg)
1232 gboolean nocache = *(gboolean *)arg;
1234 webkit_web_view_reload_bypass_cache(c->view);
1236 webkit_web_view_reload(c->view);
1240 scroll_h(Client *c, const Arg *arg)
1242 scroll(gtk_scrolled_window_get_hadjustment(
1243 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1247 scroll_v(Client *c, const Arg *arg)
1249 scroll(gtk_scrolled_window_get_vadjustment(
1250 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1254 scroll(GtkAdjustment *a, const Arg *arg)
1258 v = gtk_adjustment_get_value(a);
1262 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1267 v += gtk_adjustment_get_step_increment(a) * arg->i;
1271 v = MIN(v, gtk_adjustment_get_upper(a) -
1272 gtk_adjustment_get_page_size(a));
1273 gtk_adjustment_set_value(a, v);
1277 setatom(Client *c, int a, const char *v)
1280 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1281 atoms[a], XA_STRING, 8, PropModeReplace,
1282 (unsigned char *)v, strlen(v) + 1);
1289 char *proxy, *new_proxy;
1290 char *styledirfile, *stylepath;
1293 GError *error = NULL;
1295 /* clean up any zombies immediately */
1297 gtk_init(NULL, NULL);
1299 dpy = GDK_DISPLAY();
1302 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1303 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1304 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1306 /* dirs and files */
1307 cookiefile = buildfile(cookiefile);
1308 scriptfile = buildfile(scriptfile);
1309 cachefolder = buildpath(cachefolder);
1310 if (stylefile == NULL) {
1311 styledir = buildpath(styledir);
1312 for (i = 0; i < LENGTH(styles); i++) {
1313 if (regcomp(&(styles[i].re), styles[i].regex,
1316 "Could not compile regex: %s\n",
1318 styles[i].regex = NULL;
1320 styledirfile = g_strconcat(styledir, "/",
1321 styles[i].style, NULL);
1322 stylepath = buildfile(styledirfile);
1323 styles[i].style = g_strconcat("file://", stylepath,
1325 g_free(styledirfile);
1330 stylepath = buildfile(stylefile);
1331 stylefile = g_strconcat("file://", stylepath, NULL);
1335 /* request handler */
1336 s = webkit_get_default_session();
1339 soup_session_add_feature(s,
1340 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1341 FALSE, cookiepolicy_get())));
1344 if (enablediskcache) {
1345 diskcache = soup_cache_new(cachefolder,
1346 SOUP_CACHE_SINGLE_USER);
1347 soup_cache_set_max_size(diskcache, diskcachebytes);
1348 soup_cache_load(diskcache);
1349 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1353 tlsdb = g_tls_file_database_new(cafile, &error);
1356 g_warning("Error loading SSL database %s: %s", cafile,
1358 g_error_free(error);
1360 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1361 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1364 if ((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1365 new_proxy = g_strrstr(proxy, "http://")
1366 || g_strrstr(proxy, "socks://")
1367 || g_strrstr(proxy, "socks4://")
1368 || g_strrstr(proxy, "socks5://")
1370 : g_strdup_printf("http://%s", proxy);
1371 puri = soup_uri_new(new_proxy);
1372 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1373 soup_uri_free(puri);
1382 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1383 die("Can't install SIGCHLD handler");
1384 while (0 < waitpid(-1, NULL, WNOHANG));
1388 source(Client *c, const Arg *arg)
1390 Arg a = { .b = FALSE };
1393 s = webkit_web_view_get_view_source_mode(c->view);
1394 webkit_web_view_set_view_source_mode(c->view, !s);
1399 spawn(Client *c, const Arg *arg)
1403 close(ConnectionNumber(dpy));
1405 execvp(((char **)arg->v)[0], (char **)arg->v);
1406 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1413 eval(Client *c, const Arg *arg)
1415 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1416 evalscript(webkit_web_frame_get_global_context(frame),
1417 ((char **)arg->v)[0], "");
1421 stop(Client *c, const Arg *arg)
1423 webkit_web_view_stop_loading(c->view);
1427 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1429 const gchar *t = webkit_web_view_get_title(view);
1431 c->title = copystr(&c->title, t);
1437 titlechangeleave(void *a, void *b, Client *c)
1439 c->linkhover = NULL;
1444 toggle(Client *c, const Arg *arg)
1446 WebKitWebSettings *settings;
1447 char *name = (char *)arg->v;
1449 Arg a = { .b = FALSE };
1451 settings = webkit_web_view_get_settings(c->view);
1452 g_object_get(G_OBJECT(settings), name, &value, NULL);
1453 g_object_set(G_OBJECT(settings), name, !value, NULL);
1459 togglecookiepolicy(Client *c, const Arg *arg)
1462 SoupCookieJarAcceptPolicy policy;
1464 jar = SOUP_COOKIE_JAR(soup_session_get_feature(
1465 webkit_get_default_session(),
1466 SOUP_TYPE_COOKIE_JAR));
1467 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1470 if (policysel >= strlen(cookiepolicies))
1473 g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);
1476 /* Do not reload. */
1480 togglegeolocation(Client *c, const Arg *arg)
1482 Arg a = { .b = FALSE };
1484 allowgeolocation ^= 1;
1489 twitch(Client *c, const Arg *arg)
1494 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1497 v = gtk_adjustment_get_value(a);
1502 v = MIN(v, gtk_adjustment_get_upper(a) -
1503 gtk_adjustment_get_page_size(a));
1504 gtk_adjustment_set_value(a, v);
1508 togglescrollbars(Client *c, const Arg *arg)
1510 GtkPolicyType vspolicy;
1513 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1516 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1517 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1521 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1522 GTK_POLICY_AUTOMATIC,
1523 GTK_POLICY_AUTOMATIC);
1532 togglestyle(Client *c, const Arg *arg)
1534 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1536 enablestyles = !enablestyles;
1537 g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
1538 enablestyles ? getstyle(geturi(c)) : "", NULL);
1544 gettogglestat(Client *c)
1548 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1550 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1552 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1554 togglestat[p++] = value? 'C': 'c';
1556 togglestat[p++] = allowgeolocation? 'G': 'g';
1558 togglestat[p++] = enablediskcache? 'D': 'd';
1560 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1561 togglestat[p++] = value? 'I': 'i';
1563 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1564 togglestat[p++] = value? 'S': 's';
1566 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1567 togglestat[p++] = value? 'V': 'v';
1569 togglestat[p++] = enablestyles ? 'M': 'm';
1571 togglestat[p] = '\0';
1575 getpagestat(Client *c)
1577 const char *uri = geturi(c);
1579 if (strstr(uri, "https://") == uri)
1580 pagestat[0] = c->sslfailed ? 'U' : 'T';
1584 pagestat[1] = usingproxy ? 'P' : '-';
1589 updatetitle(Client *c)
1593 if (showindicators) {
1598 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1600 } else if (c->progress != 100) {
1601 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1602 togglestat, pagestat,
1603 c->title == NULL ? "" : c->title);
1605 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1606 c->title == NULL ? "" : c->title);
1609 gtk_window_set_title(GTK_WINDOW(c->win), t);
1612 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1618 updatewinid(Client *c)
1620 snprintf(winid, LENGTH(winid), "%u",
1621 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1627 die("usage: %s [-bBfFgGiIkKnNpPsSvx] [-a cookiepolicies ] "
1628 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1629 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1633 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1634 JSObjectRef win, Client *c)
1640 zoom(Client *c, const Arg *arg)
1645 webkit_web_view_zoom_out(c->view);
1646 } else if (arg->i > 0) {
1648 webkit_web_view_zoom_in(c->view);
1652 webkit_web_view_set_zoom_level(c->view, 1.0);
1657 main(int argc, char *argv[])
1662 memset(&arg, 0, sizeof(arg));
1664 /* command line args */
1667 cookiepolicies = EARGF(usage());
1670 enablescrollbars = 0;
1673 enablescrollbars = 1;
1676 cookiefile = EARGF(usage());
1679 enablediskcache = 0;
1682 enablediskcache = 1;
1685 embed = strtol(EARGF(usage()), NULL, 0);
1688 runinfullscreen = 1;
1691 runinfullscreen = 0;
1694 allowgeolocation = 0;
1697 allowgeolocation = 1;
1718 enableinspector = 0;
1721 enableinspector = 1;
1730 scriptfile = EARGF(usage());
1739 stylefile = EARGF(usage());
1742 useragent = EARGF(usage());
1745 die("surf-"VERSION", ©2009-2015 surf engineers, "
1746 "see LICENSE for details\n");
1751 zoomlevel = strtof(EARGF(usage()), NULL);
1762 loaduri(clients, &arg);
1769 return EXIT_SUCCESS;