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);
158 static void setstyle(Client *c, const char *style);
160 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
162 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
164 static void inspector(Client *c, const Arg *arg);
165 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
167 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
168 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
169 static void inspector_finished(WebKitWebInspector *i, Client *c);
171 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
172 GdkModifierType mods, Client *c);
173 static void linkhover(WebKitWebView *v, const char* t, const char* l,
175 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
177 static void loaduri(Client *c, const Arg *arg);
178 static void navigate(Client *c, const Arg *arg);
179 static Client *newclient(void);
180 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
181 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
182 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
183 WebKitHitTestResult *target, gboolean keyboard,
185 static void menuactivate(GtkMenuItem *item, Client *c);
186 static void print(Client *c, const Arg *arg);
187 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
189 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
190 static void linkopen(Client *c, const Arg *arg);
191 static void linkopenembed(Client *c, const Arg *arg);
192 static void reload(Client *c, const Arg *arg);
193 static void scroll_h(Client *c, const Arg *arg);
194 static void scroll_v(Client *c, const Arg *arg);
195 static void scroll(GtkAdjustment *a, const Arg *arg);
196 static void setatom(Client *c, int a, const char *v);
197 static void setup(void);
198 static void sigchld(int unused);
199 static void source(Client *c, const Arg *arg);
200 static void spawn(Client *c, const Arg *arg);
201 static void stop(Client *c, const Arg *arg);
202 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
203 static void titlechangeleave(void *a, void *b, Client *c);
204 static void toggle(Client *c, const Arg *arg);
205 static void togglecookiepolicy(Client *c, const Arg *arg);
206 static void togglegeolocation(Client *c, const Arg *arg);
207 static void togglescrollbars(Client *c, const Arg *arg);
208 static void togglestyle(Client *c, const Arg *arg);
209 static void updatetitle(Client *c);
210 static void updatewinid(Client *c);
211 static void usage(void);
212 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
213 JSContextRef js, JSObjectRef win, Client *c);
214 static void zoom(Client *c, const Arg *arg);
216 /* configuration, allows nested code to access above variables */
220 addaccelgroup(Client *c)
223 GtkAccelGroup *group = gtk_accel_group_new();
226 for (i = 0; i < LENGTH(keys); i++) {
227 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
228 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
231 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
235 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
236 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
239 const gchar *uri = webkit_network_request_get_uri(req);
242 if (g_str_has_suffix(uri, "/favicon.ico"))
243 webkit_network_request_set_uri(req, "about:blank");
245 if (!g_str_has_prefix(uri, "http://")
246 && !g_str_has_prefix(uri, "https://")
247 && !g_str_has_prefix(uri, "about:")
248 && !g_str_has_prefix(uri, "file://")
249 && !g_str_has_prefix(uri, "data:")
250 && !g_str_has_prefix(uri, "blob:")
251 && strlen(uri) > 0) {
252 for (i = 0; i < strlen(uri); i++) {
253 if (!g_ascii_isprint(uri[i])) {
259 handleplumb(c, w, uri);
264 buildfile(const char *path)
266 char *dname, *bname, *bpath, *fpath;
269 dname = g_path_get_dirname(path);
270 bname = g_path_get_basename(path);
272 bpath = buildpath(dname);
275 fpath = g_build_filename(bpath, bname, NULL);
278 if (!(f = fopen(fpath, "a")))
279 die("Could not open file: %s\n", fpath);
281 g_chmod(fpath, 0600); /* always */
288 buildpath(const char *path)
291 char *apath, *name, *p, *fpath;
293 if (path[0] == '~') {
294 if (path[1] == '/' || path[1] == '\0') {
295 p = (char *)&path[1];
296 pw = getpwuid(getuid());
298 if ((p = strchr(path, '/')))
299 name = g_strndup(&path[1], --p - path);
301 name = g_strdup(&path[1]);
303 if (!(pw = getpwnam(name))) {
304 die("Can't get user %s home directory: %s.\n",
309 apath = g_build_filename(pw->pw_dir, p, NULL);
311 apath = g_strdup(path);
314 /* creating directory */
315 if (g_mkdir_with_parents(apath, 0700) < 0)
316 die("Could not access directory: %s\n", apath);
318 fpath = realpath(apath, NULL);
325 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
327 WebKitHitTestResultContext context;
328 WebKitHitTestResult *result;
332 result = webkit_web_view_get_hit_test_result(web, e);
333 g_object_get(result, "context", &context, NULL);
334 g_object_get(result, "link-uri", &arg.v, NULL);
335 for (i = 0; i < LENGTH(buttons); i++) {
336 if (context & buttons[i].click
337 && e->button == buttons[i].button
338 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
339 && buttons[i].func) {
340 buttons[i].func(c, buttons[i].click == ClkLink
341 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
352 soup_cache_flush(diskcache);
353 soup_cache_dump(diskcache);
356 destroyclient(clients);
363 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
364 SoupCookie *new_cookie)
366 flock(COOKIEJAR(self)->lock, LOCK_EX);
367 if (new_cookie && !new_cookie->expires && sessiontime) {
368 soup_cookie_set_expires(new_cookie,
369 soup_date_new_from_now(sessiontime));
371 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
374 flock(COOKIEJAR(self)->lock, LOCK_UN);
378 cookiejar_class_init(CookieJarClass *klass)
380 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
381 G_OBJECT_CLASS(klass)->get_property =
382 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
383 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
384 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
385 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
389 cookiejar_finalize(GObject *self)
391 close(COOKIEJAR(self)->lock);
392 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
396 cookiejar_init(CookieJar *self)
398 self->lock = open(cookiefile, 0);
402 cookiejar_new(const char *filename, gboolean read_only,
403 SoupCookieJarAcceptPolicy policy)
405 return g_object_new(COOKIEJAR_TYPE,
406 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
407 SOUP_COOKIE_JAR_READ_ONLY, read_only,
408 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
412 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
415 flock(COOKIEJAR(self)->lock, LOCK_SH);
416 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
418 flock(COOKIEJAR(self)->lock, LOCK_UN);
421 SoupCookieJarAcceptPolicy
422 cookiepolicy_get(void)
424 switch (cookiepolicies[policysel]) {
426 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
428 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
434 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
438 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep)
441 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
443 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
445 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
454 evalscript(JSContextRef js, char *script, char* scriptname)
456 JSStringRef jsscript, jsscriptname;
457 JSValueRef exception = NULL;
459 jsscript = JSStringCreateWithUTF8CString(script);
460 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
461 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
462 jsscriptname, 0, &exception);
463 JSStringRelease(jsscript);
464 JSStringRelease(jsscriptname);
468 runscript(WebKitWebFrame *frame)
473 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
474 evalscript(webkit_web_frame_get_global_context(frame), script,
480 clipboard(Client *c, const Arg *arg)
482 gboolean paste = *(gboolean *)arg;
485 gtk_clipboard_request_text(gtk_clipboard_get(
486 GDK_SELECTION_PRIMARY),
489 gtk_clipboard_set_text(gtk_clipboard_get(
490 GDK_SELECTION_PRIMARY), c->linkhover
491 ? c->linkhover : geturi(c), -1);
496 copystr(char **str, const char *src)
509 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
511 Client *n = newclient();
516 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
517 gchar *m, WebKitWebPolicyDecision *p, Client *c)
519 if (!webkit_web_view_can_show_mime_type(v, m)) {
520 webkit_web_policy_decision_download(p);
527 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
528 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
533 if (webkit_web_navigation_action_get_reason(n)
534 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
535 webkit_web_policy_decision_ignore(p);
536 arg.v = (void *)webkit_network_request_get_uri(r);
537 newwindow(NULL, &arg, 0);
544 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
550 destroyclient(Client *c)
554 webkit_web_view_stop_loading(c->view);
555 gtk_widget_destroy(GTK_WIDGET(c->view));
556 gtk_widget_destroy(c->scroll);
557 gtk_widget_destroy(c->vbox);
558 gtk_widget_destroy(c->win);
560 for (p = clients; p && p->next != c; p = p->next)
572 destroywin(GtkWidget* w, Client *c)
578 die(const char *errstr, ...)
582 va_start(ap, errstr);
583 vfprintf(stderr, errstr, ap);
589 find(Client *c, const Arg *arg)
593 s = getatom(c, AtomFind);
594 gboolean forward = *(gboolean *)arg;
595 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
599 fullscreen(Client *c, const Arg *arg)
602 gtk_window_unfullscreen(GTK_WINDOW(c->win));
604 gtk_window_fullscreen(GTK_WINDOW(c->win));
605 c->fullscreen = !c->fullscreen;
609 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
610 WebKitGeolocationPolicyDecision *d, Client *c)
612 if (allowgeolocation)
613 webkit_geolocation_policy_allow(d);
615 webkit_geolocation_policy_deny(d);
619 getatom(Client *c, int a)
621 static char buf[BUFSIZ];
624 unsigned long ldummy;
625 unsigned char *p = NULL;
627 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
628 atoms[a], 0L, BUFSIZ, False, XA_STRING,
629 &adummy, &idummy, &ldummy, &ldummy, &p);
631 strncpy(buf, (char *)p, LENGTH(buf)-1);
644 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
650 getstyle(const char *uri)
654 if (stylefile != NULL)
657 for (i = 0; i < LENGTH(styles); i++) {
658 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
660 return styles[i].style;
667 setstyle(Client *c, const char *style)
669 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
671 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
675 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
679 webkit_web_view_stop_loading(w);
680 arg = (Arg)PLUMB((char *)uri);
685 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
690 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
696 inspector(Client *c, const Arg *arg)
699 webkit_web_inspector_close(c->inspector);
701 webkit_web_inspector_show(c->inspector);
705 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
707 return WEBKIT_WEB_VIEW(webkit_web_view_new());
711 inspector_show(WebKitWebInspector *i, Client *c)
718 w = webkit_web_inspector_get_web_view(i);
719 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
720 gtk_widget_show(GTK_WIDGET(w));
721 c->isinspecting = true;
727 inspector_close(WebKitWebInspector *i, Client *c)
731 if (!c->isinspecting)
734 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
736 gtk_widget_destroy(w);
737 c->isinspecting = false;
743 inspector_finished(WebKitWebInspector *i, Client *c)
745 g_free(c->inspector);
749 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
753 gboolean processed = FALSE;
755 mods = CLEANMASK(mods);
756 key = gdk_keyval_to_lower(key);
758 for (i = 0; i < LENGTH(keys); i++) {
759 if (key == keys[i].keyval
760 && mods == keys[i].mod
762 keys[i].func(c, &(keys[i].arg));
771 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
774 c->linkhover = copystr(&c->linkhover, l);
775 } else if (c->linkhover) {
783 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
785 WebKitWebFrame *frame;
786 WebKitWebDataSource *src;
787 WebKitNetworkRequest *request;
791 switch (webkit_web_view_get_load_status (c->view)) {
792 case WEBKIT_LOAD_COMMITTED:
794 if (strstr(uri, "https://") == uri) {
795 frame = webkit_web_view_get_main_frame(c->view);
796 src = webkit_web_frame_get_data_source(frame);
797 request = webkit_web_data_source_get_request(src);
798 msg = webkit_network_request_get_message(request);
799 c->sslfailed = !(soup_message_get_flags(msg)
800 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
802 setatom(c, AtomUri, uri);
805 setstyle(c, getstyle(uri));
807 case WEBKIT_LOAD_FINISHED:
811 soup_cache_flush(diskcache);
812 soup_cache_dump(diskcache);
821 loaduri(Client *c, const Arg *arg)
824 const char *uri = (char *)arg->v;
825 Arg a = { .b = FALSE };
828 if (strcmp(uri, "") == 0)
831 /* In case it's a file path. */
832 if (stat(uri, &st) == 0) {
833 rp = realpath(uri, NULL);
834 u = g_strdup_printf("file://%s", rp);
837 u = g_strrstr(uri, "://") ? g_strdup(uri)
838 : g_strdup_printf("http://%s", uri);
841 setatom(c, AtomUri, uri);
843 /* prevents endless loop */
844 if (strcmp(u, geturi(c)) == 0) {
847 webkit_web_view_load_uri(c->view, u);
849 c->title = copystr(&c->title, u);
856 navigate(Client *c, const Arg *arg)
858 int steps = *(int *)arg;
859 webkit_web_view_go_back_or_forward(c->view, steps);
866 WebKitWebSettings *settings;
867 WebKitWebFrame *frame;
868 GdkGeometry hints = { 1, 1 };
873 if (!(c = calloc(1, sizeof(Client))))
874 die("Cannot malloc!\n");
881 c->win = gtk_plug_new(embed);
883 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
885 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
886 * is always correct, so we should still call this function.
887 * But when doing so, we *must* differentiate between a
888 * WM_CLASS and a resource on the window. By convention, the
889 * window class (WM_CLASS) is capped, while the resource is in
890 * lowercase. Both these values come as a pair.
892 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
894 /* TA: 20091214: And set the role here as well -- so that
895 * sessions can pick this up.
897 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
899 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
900 g_signal_connect(G_OBJECT(c->win),
902 G_CALLBACK(destroywin), c);
903 g_signal_connect(G_OBJECT(c->win),
904 "leave_notify_event",
905 G_CALLBACK(titlechangeleave), c);
911 c->pane = gtk_vpaned_new();
914 c->vbox = gtk_vbox_new(FALSE, 0);
915 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
918 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
920 g_signal_connect(G_OBJECT(c->view),
922 G_CALLBACK(titlechange), c);
923 g_signal_connect(G_OBJECT(c->view),
924 "hovering-over-link",
925 G_CALLBACK(linkhover), c);
926 g_signal_connect(G_OBJECT(c->view),
927 "geolocation-policy-decision-requested",
928 G_CALLBACK(geopolicyrequested), c);
929 g_signal_connect(G_OBJECT(c->view),
931 G_CALLBACK(createwindow), c);
932 g_signal_connect(G_OBJECT(c->view),
933 "new-window-policy-decision-requested",
934 G_CALLBACK(decidewindow), c);
935 g_signal_connect(G_OBJECT(c->view),
936 "mime-type-policy-decision-requested",
937 G_CALLBACK(decidedownload), c);
938 g_signal_connect(G_OBJECT(c->view),
939 "window-object-cleared",
940 G_CALLBACK(windowobjectcleared), c);
941 g_signal_connect(G_OBJECT(c->view),
942 "notify::load-status",
943 G_CALLBACK(loadstatuschange), c);
944 g_signal_connect(G_OBJECT(c->view),
946 G_CALLBACK(progresschange), c);
947 g_signal_connect(G_OBJECT(c->view),
948 "download-requested",
949 G_CALLBACK(initdownload), c);
950 g_signal_connect(G_OBJECT(c->view),
951 "button-release-event",
952 G_CALLBACK(buttonrelease), c);
953 g_signal_connect(G_OBJECT(c->view),
955 G_CALLBACK(contextmenu), c);
956 g_signal_connect(G_OBJECT(c->view),
957 "resource-request-starting",
958 G_CALLBACK(beforerequest), c);
959 g_signal_connect(G_OBJECT(c->view),
960 "should-show-delete-interface-for-element",
961 G_CALLBACK(deletion_interface), c);
963 /* Scrolled Window */
964 c->scroll = gtk_scrolled_window_new(NULL, NULL);
966 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
967 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
968 G_CALLBACK(gtk_true), NULL);
970 if (!enablescrollbars) {
971 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
975 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
976 GTK_POLICY_AUTOMATIC,
977 GTK_POLICY_AUTOMATIC);
981 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
982 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
983 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
986 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
988 gtk_widget_grab_focus(GTK_WIDGET(c->view));
989 gtk_widget_show(c->pane);
990 gtk_widget_show(c->vbox);
991 gtk_widget_show(c->scroll);
992 gtk_widget_show(GTK_WIDGET(c->view));
993 gtk_widget_show(c->win);
994 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
996 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
997 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
998 webkit_web_view_set_full_content_zoom(c->view, TRUE);
1002 settings = webkit_web_view_get_settings(c->view);
1003 if (!(ua = getenv("SURF_USERAGENT")))
1005 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
1006 g_object_set(G_OBJECT(settings),
1007 "auto-load-images", loadimages, NULL);
1008 g_object_set(G_OBJECT(settings),
1009 "enable-plugins", enableplugins, NULL);
1010 g_object_set(G_OBJECT(settings),
1011 "enable-scripts", enablescripts, NULL);
1012 g_object_set(G_OBJECT(settings),
1013 "enable-spatial-navigation", enablespatialbrowsing, NULL);
1014 g_object_set(G_OBJECT(settings),
1015 "enable-developer-extras", enableinspector, NULL);
1016 g_object_set(G_OBJECT(settings),
1017 "enable-default-context-menu", kioskmode ^ 1, NULL);
1018 g_object_set(G_OBJECT(settings),
1019 "default-font-size", defaultfontsize, NULL);
1020 g_object_set(G_OBJECT(settings),
1021 "resizable-text-areas", 1, NULL);
1023 setstyle(c, getstyle("about:blank"));
1026 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
1027 * This ensures websites are not unusably small with a high DPI screen.
1028 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
1031 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
1032 dpi = gdk_screen_get_resolution(screen);
1034 g_object_set(G_OBJECT(settings),
1035 "enforce-96-dpi", true, NULL);
1036 webkit_web_view_set_zoom_level(c->view, dpi/96);
1039 /* This might conflict with _zoomto96dpi_. */
1040 if (zoomlevel != 1.0)
1041 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1043 if (enableinspector) {
1044 c->inspector = webkit_web_view_get_inspector(c->view);
1045 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
1046 G_CALLBACK(inspector_new), c);
1047 g_signal_connect(G_OBJECT(c->inspector), "show-window",
1048 G_CALLBACK(inspector_show), c);
1049 g_signal_connect(G_OBJECT(c->inspector), "close-window",
1050 G_CALLBACK(inspector_close), c);
1051 g_signal_connect(G_OBJECT(c->inspector), "finished",
1052 G_CALLBACK(inspector_finished), c);
1053 c->isinspecting = false;
1056 if (runinfullscreen)
1057 fullscreen(c, NULL);
1059 setatom(c, AtomFind, "");
1060 setatom(c, AtomUri, "about:blank");
1062 webkit_web_view_set_transparent(c->view, TRUE);
1068 gdk_display_sync(gtk_widget_get_display(c->win));
1070 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1072 if (fclose(stdout) != 0) {
1073 die("Error closing stdout");
1081 newwindow(Client *c, const Arg *arg, gboolean noembed)
1084 const char *cmd[18], *uri;
1085 const Arg a = { .v = (void *)cmd };
1090 cmd[i++] = cookiepolicies;
1091 if (!enablescrollbars)
1093 if (embed && !noembed) {
1095 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1098 if (!allowgeolocation)
1110 if (enablediskcache)
1113 cmd[i++] = cookiefile;
1115 uri = arg->v ? (char *)arg->v : c->linkhover;
1123 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1124 gboolean keyboard, Client *c)
1126 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1128 for (GList *l = items; l; l = l->next)
1129 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1136 menuactivate(GtkMenuItem *item, Client *c)
1139 * context-menu-action-2000 open link
1140 * context-menu-action-1 open link in window
1141 * context-menu-action-2 download linked file
1142 * context-menu-action-3 copy link location
1143 * context-menu-action-7 copy image address
1144 * context-menu-action-13 reload
1145 * context-menu-action-10 back
1146 * context-menu-action-11 forward
1147 * context-menu-action-12 stop
1150 GtkAction *a = NULL;
1151 const char *name, *uri;
1152 GtkClipboard *prisel, *clpbrd;
1154 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
1158 name = gtk_action_get_name(a);
1159 if (!g_strcmp0(name, "context-menu-action-3")) {
1160 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1161 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1162 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1163 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1164 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1165 uri = gtk_clipboard_wait_for_text(clpbrd);
1167 gtk_clipboard_set_text(prisel, uri, -1);
1172 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1174 Arg arg = {.v = text };
1176 loaduri((Client *) d, &arg);
1180 print(Client *c, const Arg *arg)
1182 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1186 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1188 Client *c = (Client *)d;
1192 if (((XEvent *)e)->type == PropertyNotify) {
1193 ev = &((XEvent *)e)->xproperty;
1194 if (ev->state == PropertyNewValue) {
1195 if (ev->atom == atoms[AtomFind]) {
1199 return GDK_FILTER_REMOVE;
1200 } else if (ev->atom == atoms[AtomGo]) {
1201 arg.v = getatom(c, AtomGo);
1204 return GDK_FILTER_REMOVE;
1208 return GDK_FILTER_CONTINUE;
1212 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1214 c->progress = webkit_web_view_get_progress(c->view) * 100;
1219 linkopen(Client *c, const Arg *arg)
1221 newwindow(NULL, arg, 1);
1225 linkopenembed(Client *c, const Arg *arg)
1227 newwindow(NULL, arg, 0);
1231 reload(Client *c, const Arg *arg)
1233 gboolean nocache = *(gboolean *)arg;
1235 webkit_web_view_reload_bypass_cache(c->view);
1237 webkit_web_view_reload(c->view);
1241 scroll_h(Client *c, const Arg *arg)
1243 scroll(gtk_scrolled_window_get_hadjustment(
1244 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1248 scroll_v(Client *c, const Arg *arg)
1250 scroll(gtk_scrolled_window_get_vadjustment(
1251 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1255 scroll(GtkAdjustment *a, const Arg *arg)
1259 v = gtk_adjustment_get_value(a);
1263 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1268 v += gtk_adjustment_get_step_increment(a) * arg->i;
1272 v = MIN(v, gtk_adjustment_get_upper(a) -
1273 gtk_adjustment_get_page_size(a));
1274 gtk_adjustment_set_value(a, v);
1278 setatom(Client *c, int a, const char *v)
1281 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1282 atoms[a], XA_STRING, 8, PropModeReplace,
1283 (unsigned char *)v, strlen(v) + 1);
1290 char *proxy, *new_proxy;
1291 char *styledirfile, *stylepath;
1294 GError *error = NULL;
1296 /* clean up any zombies immediately */
1298 gtk_init(NULL, NULL);
1300 dpy = GDK_DISPLAY();
1303 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1304 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1305 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1307 /* dirs and files */
1308 cookiefile = buildfile(cookiefile);
1309 scriptfile = buildfile(scriptfile);
1310 cachefolder = buildpath(cachefolder);
1311 if (stylefile == NULL) {
1312 styledir = buildpath(styledir);
1313 for (i = 0; i < LENGTH(styles); i++) {
1314 if (regcomp(&(styles[i].re), styles[i].regex,
1317 "Could not compile regex: %s\n",
1319 styles[i].regex = NULL;
1321 styledirfile = g_strconcat(styledir, "/",
1322 styles[i].style, NULL);
1323 stylepath = buildfile(styledirfile);
1324 styles[i].style = g_strconcat("file://", stylepath,
1326 g_free(styledirfile);
1331 stylepath = buildfile(stylefile);
1332 stylefile = g_strconcat("file://", stylepath, NULL);
1336 /* request handler */
1337 s = webkit_get_default_session();
1340 soup_session_add_feature(s,
1341 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1342 FALSE, cookiepolicy_get())));
1345 if (enablediskcache) {
1346 diskcache = soup_cache_new(cachefolder,
1347 SOUP_CACHE_SINGLE_USER);
1348 soup_cache_set_max_size(diskcache, diskcachebytes);
1349 soup_cache_load(diskcache);
1350 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1354 tlsdb = g_tls_file_database_new(cafile, &error);
1357 g_warning("Error loading SSL database %s: %s", cafile,
1359 g_error_free(error);
1361 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1362 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1365 if ((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1366 new_proxy = g_strrstr(proxy, "http://")
1367 || g_strrstr(proxy, "socks://")
1368 || g_strrstr(proxy, "socks4://")
1369 || g_strrstr(proxy, "socks5://")
1371 : g_strdup_printf("http://%s", proxy);
1372 puri = soup_uri_new(new_proxy);
1373 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1374 soup_uri_free(puri);
1383 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1384 die("Can't install SIGCHLD handler");
1385 while (0 < waitpid(-1, NULL, WNOHANG));
1389 source(Client *c, const Arg *arg)
1391 Arg a = { .b = FALSE };
1394 s = webkit_web_view_get_view_source_mode(c->view);
1395 webkit_web_view_set_view_source_mode(c->view, !s);
1400 spawn(Client *c, const Arg *arg)
1404 close(ConnectionNumber(dpy));
1406 execvp(((char **)arg->v)[0], (char **)arg->v);
1407 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1414 eval(Client *c, const Arg *arg)
1416 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1417 evalscript(webkit_web_frame_get_global_context(frame),
1418 ((char **)arg->v)[0], "");
1422 stop(Client *c, const Arg *arg)
1424 webkit_web_view_stop_loading(c->view);
1428 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1430 const gchar *t = webkit_web_view_get_title(view);
1432 c->title = copystr(&c->title, t);
1438 titlechangeleave(void *a, void *b, Client *c)
1440 c->linkhover = NULL;
1445 toggle(Client *c, const Arg *arg)
1447 WebKitWebSettings *settings;
1448 char *name = (char *)arg->v;
1450 Arg a = { .b = FALSE };
1452 settings = webkit_web_view_get_settings(c->view);
1453 g_object_get(G_OBJECT(settings), name, &value, NULL);
1454 g_object_set(G_OBJECT(settings), name, !value, NULL);
1460 togglecookiepolicy(Client *c, const Arg *arg)
1463 SoupCookieJarAcceptPolicy policy;
1465 jar = SOUP_COOKIE_JAR(soup_session_get_feature(
1466 webkit_get_default_session(),
1467 SOUP_TYPE_COOKIE_JAR));
1468 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1471 if (policysel >= strlen(cookiepolicies))
1474 g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);
1477 /* Do not reload. */
1481 togglegeolocation(Client *c, const Arg *arg)
1483 Arg a = { .b = FALSE };
1485 allowgeolocation ^= 1;
1490 twitch(Client *c, const Arg *arg)
1495 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1498 v = gtk_adjustment_get_value(a);
1503 v = MIN(v, gtk_adjustment_get_upper(a) -
1504 gtk_adjustment_get_page_size(a));
1505 gtk_adjustment_set_value(a, v);
1509 togglescrollbars(Client *c, const Arg *arg)
1511 GtkPolicyType vspolicy;
1514 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1517 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1518 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1522 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1523 GTK_POLICY_AUTOMATIC,
1524 GTK_POLICY_AUTOMATIC);
1533 togglestyle(Client *c, const Arg *arg)
1535 enablestyles = !enablestyles;
1536 setstyle(c, enablestyles ? getstyle(geturi(c)) : "");
1542 gettogglestat(Client *c)
1546 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1548 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1550 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1552 togglestat[p++] = value? 'C': 'c';
1554 togglestat[p++] = allowgeolocation? 'G': 'g';
1556 togglestat[p++] = enablediskcache? 'D': 'd';
1558 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1559 togglestat[p++] = value? 'I': 'i';
1561 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1562 togglestat[p++] = value? 'S': 's';
1564 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1565 togglestat[p++] = value? 'V': 'v';
1567 togglestat[p++] = enablestyles ? 'M': 'm';
1569 togglestat[p] = '\0';
1573 getpagestat(Client *c)
1575 const char *uri = geturi(c);
1577 if (strstr(uri, "https://") == uri)
1578 pagestat[0] = c->sslfailed ? 'U' : 'T';
1582 pagestat[1] = usingproxy ? 'P' : '-';
1587 updatetitle(Client *c)
1591 if (showindicators) {
1596 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1598 } else if (c->progress != 100) {
1599 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1600 togglestat, pagestat,
1601 c->title == NULL ? "" : c->title);
1603 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1604 c->title == NULL ? "" : c->title);
1607 gtk_window_set_title(GTK_WINDOW(c->win), t);
1610 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1616 updatewinid(Client *c)
1618 snprintf(winid, LENGTH(winid), "%u",
1619 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1625 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1626 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1627 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1631 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1632 JSObjectRef win, Client *c)
1638 zoom(Client *c, const Arg *arg)
1643 webkit_web_view_zoom_out(c->view);
1644 } else if (arg->i > 0) {
1646 webkit_web_view_zoom_in(c->view);
1650 webkit_web_view_set_zoom_level(c->view, 1.0);
1655 main(int argc, char *argv[])
1660 memset(&arg, 0, sizeof(arg));
1662 /* command line args */
1665 cookiepolicies = EARGF(usage());
1668 enablescrollbars = 0;
1671 enablescrollbars = 1;
1674 cookiefile = EARGF(usage());
1677 enablediskcache = 0;
1680 enablediskcache = 1;
1683 embed = strtol(EARGF(usage()), NULL, 0);
1686 runinfullscreen = 0;
1689 runinfullscreen = 1;
1692 allowgeolocation = 0;
1695 allowgeolocation = 1;
1716 enableinspector = 0;
1719 enableinspector = 1;
1728 scriptfile = EARGF(usage());
1737 stylefile = EARGF(usage());
1740 useragent = EARGF(usage());
1743 die("surf-"VERSION", ©2009-2015 surf engineers, "
1744 "see LICENSE for details\n");
1749 zoomlevel = strtof(EARGF(usage()), NULL);
1760 loaduri(clients, &arg);
1767 return EXIT_SUCCESS;