1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
12 #include <gdk/gdkkeysyms.h>
14 #include <sys/types.h>
20 #include <webkit/webkit.h>
21 #include <glib/gstdio.h>
22 #include <JavaScriptCore/JavaScript.h>
32 #define LENGTH(x) (sizeof x / sizeof x[0])
33 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
34 #define COOKIEJAR_TYPE (cookiejar_get_type ())
35 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
37 enum { AtomFind, AtomGo, AtomUri, AtomLast };
39 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
40 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
41 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
42 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
43 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
44 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
45 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
48 typedef union Arg Arg;
55 typedef struct Client {
56 GtkWidget *win, *scroll, *vbox, *pane;
58 WebKitWebInspector *inspector;
59 char *title, *linkhover;
63 gboolean zoomed, fullscreen, isinspecting, sslfailed;
69 void (*func)(Client *c, const Arg *arg);
77 void (*func)(Client *c, const Arg *arg);
82 SoupCookieJarText parent_instance;
87 SoupCookieJarTextClass parent_class;
90 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
99 static Atom atoms[AtomLast];
100 static Client *clients = NULL;
101 static GdkNativeWindow embed = 0;
102 static gboolean showxid = FALSE;
103 static char winid[64];
104 static gboolean usingproxy = 0;
105 static char togglestat[9];
106 static char pagestat[3];
107 static GTlsDatabase *tlsdb;
108 static int policysel = 0;
109 static char *stylefile = NULL;
110 static SoupCache *diskcache = NULL;
112 static void addaccelgroup(Client *c);
113 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
114 WebKitWebResource *r, WebKitNetworkRequest *req,
115 WebKitNetworkResponse *resp, Client *c);
116 static char *buildpath(const char *path);
117 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
118 static void cleanup(void);
119 static void clipboard(Client *c, const Arg *arg);
121 /* Cookiejar implementation */
122 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
123 SoupCookie *new_cookie);
124 static void cookiejar_finalize(GObject *self);
125 static SoupCookieJarAcceptPolicy cookiepolicy_get(void);
126 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only,
127 SoupCookieJarAcceptPolicy policy);
128 static void cookiejar_set_property(GObject *self, guint prop_id,
129 const GValue *value, GParamSpec *pspec);
130 static char cookiepolicy_set(const SoupCookieJarAcceptPolicy p);
132 static char *copystr(char **str, const char *src);
133 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
135 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
136 WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p,
138 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
139 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
140 WebKitWebPolicyDecision *p, Client *c);
141 static gboolean deletion_interface(WebKitWebView *view,
142 WebKitDOMHTMLElement *arg1, Client *c);
143 static void destroyclient(Client *c);
144 static void destroywin(GtkWidget* w, Client *c);
145 static void die(const char *errstr, ...);
146 static void eval(Client *c, const Arg *arg);
147 static void find(Client *c, const Arg *arg);
148 static void fullscreen(Client *c, const Arg *arg);
149 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
150 WebKitGeolocationPolicyDecision *d, Client *c);
151 static const char *getatom(Client *c, int a);
152 static void gettogglestat(Client *c);
153 static void getpagestat(Client *c);
154 static char *geturi(Client *c);
155 static gchar *getstyle(const char *uri);
157 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
159 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
161 static void inspector(Client *c, const Arg *arg);
162 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
164 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
165 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
166 static void inspector_finished(WebKitWebInspector *i, Client *c);
168 static gboolean keypress(GtkAccelGroup *group,
169 GObject *obj, guint key, GdkModifierType mods,
171 static void linkhover(WebKitWebView *v, const char* t, const char* l,
173 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
175 static void loaduri(Client *c, const Arg *arg);
176 static void navigate(Client *c, const Arg *arg);
177 static Client *newclient(void);
178 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
179 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
180 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
181 WebKitHitTestResult *target, gboolean keyboard, Client *c);
182 static void menuactivate(GtkMenuItem *item, Client *c);
183 static void print(Client *c, const Arg *arg);
184 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
186 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
187 static void linkopen(Client *c, const Arg *arg);
188 static void linkopenembed(Client *c, const Arg *arg);
189 static void reload(Client *c, const Arg *arg);
190 static void scroll_h(Client *c, const Arg *arg);
191 static void scroll_v(Client *c, const Arg *arg);
192 static void scroll(GtkAdjustment *a, const Arg *arg);
193 static void setatom(Client *c, int a, const char *v);
194 static void setup(void);
195 static void sigchld(int unused);
196 static void source(Client *c, const Arg *arg);
197 static void spawn(Client *c, const Arg *arg);
198 static void stop(Client *c, const Arg *arg);
199 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
200 static void titlechangeleave(void *a, void *b, Client *c);
201 static void toggle(Client *c, const Arg *arg);
202 static void togglecookiepolicy(Client *c, const Arg *arg);
203 static void togglegeolocation(Client *c, const Arg *arg);
204 static void togglescrollbars(Client *c, const Arg *arg);
205 static void togglestyle(Client *c, const Arg *arg);
206 static void updatetitle(Client *c);
207 static void updatewinid(Client *c);
208 static void usage(void);
209 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
210 JSContextRef js, JSObjectRef win, Client *c);
211 static void zoom(Client *c, const Arg *arg);
213 /* configuration, allows nested code to access above variables */
217 addaccelgroup(Client *c) {
219 GtkAccelGroup *group = gtk_accel_group_new();
222 for(i = 0; i < LENGTH(keys); i++) {
223 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
224 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod,
227 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
231 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
232 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
234 const gchar *uri = webkit_network_request_get_uri(req);
237 if(g_str_has_suffix(uri, "/favicon.ico"))
238 webkit_network_request_set_uri(req, "about:blank");
240 if(!g_str_has_prefix(uri, "http://") \
241 && !g_str_has_prefix(uri, "https://") \
242 && !g_str_has_prefix(uri, "about:") \
243 && !g_str_has_prefix(uri, "file://") \
244 && !g_str_has_prefix(uri, "data:") \
245 && !g_str_has_prefix(uri, "blob:") \
246 && strlen(uri) > 0) {
248 for(i = 0; i < strlen(uri); i++) {
249 if(!g_ascii_isprint(uri[i])) {
255 handleplumb(c, w, uri);
260 buildpath(const char *path) {
264 /* creating directory */
266 apath = g_strdup(path);
267 } else if(path[0] == '~') {
269 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
271 apath = g_strconcat(g_get_home_dir(), "/",
275 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
278 if((p = strrchr(apath, '/'))) {
280 g_mkdir_with_parents(apath, 0700);
281 g_chmod(apath, 0700); /* in case it existed */
284 /* creating file (gives error when apath ends with "/") */
285 if((f = fopen(apath, "a"))) {
286 g_chmod(apath, 0600); /* always */
294 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c) {
295 WebKitHitTestResultContext context;
296 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
301 g_object_get(result, "context", &context, NULL);
302 g_object_get(result, "link-uri", &arg.v, NULL);
303 printf("%d %d\n", context, e->button);
304 for(i = 0; i < LENGTH(buttons); i++) {
305 if(context & buttons[i].click && e->button == buttons[i].button &&
306 CLEANMASK(e->state) == CLEANMASK(buttons[i].mask) && buttons[i].func) {
307 buttons[i].func(c, buttons[i].click == ClkLink && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
317 soup_cache_flush(diskcache);
318 soup_cache_dump(diskcache);
321 destroyclient(clients);
328 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
329 SoupCookie *new_cookie) {
330 flock(COOKIEJAR(self)->lock, LOCK_EX);
331 if(new_cookie && !new_cookie->expires && sessiontime) {
332 soup_cookie_set_expires(new_cookie,
333 soup_date_new_from_now(sessiontime));
335 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
336 old_cookie, new_cookie);
337 flock(COOKIEJAR(self)->lock, LOCK_UN);
341 cookiejar_class_init(CookieJarClass *klass) {
342 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
343 G_OBJECT_CLASS(klass)->get_property =
344 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
345 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
346 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
347 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
351 cookiejar_finalize(GObject *self) {
352 close(COOKIEJAR(self)->lock);
353 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
357 cookiejar_init(CookieJar *self) {
358 self->lock = open(cookiefile, 0);
361 static SoupCookieJar *
362 cookiejar_new(const char *filename, gboolean read_only,
363 SoupCookieJarAcceptPolicy policy) {
364 return g_object_new(COOKIEJAR_TYPE,
365 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
366 SOUP_COOKIE_JAR_READ_ONLY, read_only,
367 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
371 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
373 flock(COOKIEJAR(self)->lock, LOCK_SH);
374 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
376 flock(COOKIEJAR(self)->lock, LOCK_UN);
379 static SoupCookieJarAcceptPolicy
380 cookiepolicy_get(void) {
381 switch(cookiepolicies[policysel]) {
383 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
385 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
391 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
395 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep) {
397 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
399 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
401 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
410 evalscript(JSContextRef js, char *script, char* scriptname) {
411 JSStringRef jsscript, jsscriptname;
412 JSValueRef exception = NULL;
414 jsscript = JSStringCreateWithUTF8CString(script);
415 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
416 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
417 jsscriptname, 0, &exception);
418 JSStringRelease(jsscript);
419 JSStringRelease(jsscriptname);
423 runscript(WebKitWebFrame *frame) {
427 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
428 evalscript(webkit_web_frame_get_global_context(frame),
434 clipboard(Client *c, const Arg *arg) {
435 gboolean paste = *(gboolean *)arg;
438 gtk_clipboard_request_text(
439 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
442 gtk_clipboard_set_text(
443 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
444 c->linkhover ? c->linkhover : geturi(c), -1);
449 copystr(char **str, const char *src) {
460 static WebKitWebView *
461 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
462 Client *n = newclient();
467 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
468 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
469 if(!webkit_web_view_can_show_mime_type(v, m)) {
470 webkit_web_policy_decision_download(p);
477 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
478 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
482 if(webkit_web_navigation_action_get_reason(n) ==
483 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
484 webkit_web_policy_decision_ignore(p);
485 arg.v = (void *)webkit_network_request_get_uri(r);
486 newwindow(NULL, &arg, 0);
493 deletion_interface(WebKitWebView *view,
494 WebKitDOMHTMLElement *arg1, Client *c) {
499 destroyclient(Client *c) {
502 webkit_web_view_stop_loading(c->view);
503 gtk_widget_destroy(GTK_WIDGET(c->view));
504 gtk_widget_destroy(c->scroll);
505 gtk_widget_destroy(c->vbox);
506 gtk_widget_destroy(c->win);
508 for(p = clients; p && p->next != c; p = p->next);
520 destroywin(GtkWidget* w, Client *c) {
525 die(const char *errstr, ...) {
528 va_start(ap, errstr);
529 vfprintf(stderr, errstr, ap);
535 find(Client *c, const Arg *arg) {
538 s = getatom(c, AtomFind);
539 gboolean forward = *(gboolean *)arg;
540 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
544 fullscreen(Client *c, const Arg *arg) {
546 gtk_window_unfullscreen(GTK_WINDOW(c->win));
548 gtk_window_fullscreen(GTK_WINDOW(c->win));
550 c->fullscreen = !c->fullscreen;
554 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
555 WebKitGeolocationPolicyDecision *d, Client *c) {
556 if(allowgeolocation) {
557 webkit_geolocation_policy_allow(d);
559 webkit_geolocation_policy_deny(d);
564 getatom(Client *c, int a) {
565 static char buf[BUFSIZ];
568 unsigned long ldummy;
569 unsigned char *p = NULL;
571 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
572 atoms[a], 0L, BUFSIZ, False, XA_STRING,
573 &adummy, &idummy, &ldummy, &ldummy, &p);
575 strncpy(buf, (char *)p, LENGTH(buf)-1);
588 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
594 getstyle(const char *uri) {
597 if(stylefile != NULL)
598 return g_strconcat("file://", stylefile, NULL);
600 for(i = 0; i < LENGTH(styles); i++) {
601 if(styles[i].regex && !regexec(&(styles[i].re), uri, 0,
603 return g_strconcat("file://", styles[i].style, NULL);
610 handleplumb(Client *c, WebKitWebView *w, const gchar *uri) {
613 webkit_web_view_stop_loading(w);
614 arg = (Arg)PLUMB((char *)uri);
619 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
623 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
629 inspector(Client *c, const Arg *arg) {
630 if(c->isinspecting) {
631 webkit_web_inspector_close(c->inspector);
633 webkit_web_inspector_show(c->inspector);
637 static WebKitWebView *
638 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
639 return WEBKIT_WEB_VIEW(webkit_web_view_new());
643 inspector_show(WebKitWebInspector *i, Client *c) {
649 w = webkit_web_inspector_get_web_view(i);
650 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
651 gtk_widget_show(GTK_WIDGET(w));
652 c->isinspecting = true;
658 inspector_close(WebKitWebInspector *i, Client *c) {
664 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
666 gtk_widget_destroy(w);
667 c->isinspecting = false;
673 inspector_finished(WebKitWebInspector *i, Client *c) {
674 g_free(c->inspector);
678 keypress(GtkAccelGroup *group, GObject *obj,
679 guint key, GdkModifierType mods, Client *c) {
681 gboolean processed = FALSE;
683 mods = CLEANMASK(mods);
684 key = gdk_keyval_to_lower(key);
686 for(i = 0; i < LENGTH(keys); i++) {
687 if(key == keys[i].keyval
688 && mods == keys[i].mod
690 keys[i].func(c, &(keys[i].arg));
699 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
701 c->linkhover = copystr(&c->linkhover, l);
702 } else if(c->linkhover) {
710 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
711 WebKitWebFrame *frame;
712 WebKitWebDataSource *src;
713 WebKitNetworkRequest *request;
714 WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
718 switch(webkit_web_view_get_load_status (c->view)) {
719 case WEBKIT_LOAD_COMMITTED:
721 if(strstr(uri, "https://") == uri) {
722 frame = webkit_web_view_get_main_frame(c->view);
723 src = webkit_web_frame_get_data_source(frame);
724 request = webkit_web_data_source_get_request(src);
725 msg = webkit_network_request_get_message(request);
726 c->sslfailed = !(soup_message_get_flags(msg)
727 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
729 setatom(c, AtomUri, uri);
732 g_object_set(G_OBJECT(set), "user-stylesheet-uri",
733 getstyle(uri), NULL);
736 case WEBKIT_LOAD_FINISHED:
740 soup_cache_flush(diskcache);
741 soup_cache_dump(diskcache);
750 loaduri(Client *c, const Arg *arg) {
752 const char *uri = (char *)arg->v;
753 Arg a = { .b = FALSE };
756 if(strcmp(uri, "") == 0)
759 /* In case it's a file path. */
760 if(stat(uri, &st) == 0) {
761 rp = realpath(uri, NULL);
762 u = g_strdup_printf("file://%s", rp);
765 u = g_strrstr(uri, "://") ? g_strdup(uri)
766 : g_strdup_printf("http://%s", uri);
769 setatom(c, AtomUri, uri);
771 /* prevents endless loop */
772 if(strcmp(u, geturi(c)) == 0) {
775 webkit_web_view_load_uri(c->view, u);
777 c->title = copystr(&c->title, u);
784 navigate(Client *c, const Arg *arg) {
785 int steps = *(int *)arg;
786 webkit_web_view_go_back_or_forward(c->view, steps);
792 WebKitWebSettings *settings;
793 WebKitWebFrame *frame;
794 GdkGeometry hints = { 1, 1 };
799 if(!(c = calloc(1, sizeof(Client))))
800 die("Cannot malloc!\n");
807 c->win = gtk_plug_new(embed);
809 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
811 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
812 * is always correct, so we should still call this function.
813 * But when doing so, we *must* differentiate between a
814 * WM_CLASS and a resource on the window. By convention, the
815 * window class (WM_CLASS) is capped, while the resource is in
816 * lowercase. Both these values come as a pair.
818 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
820 /* TA: 20091214: And set the role here as well -- so that
821 * sessions can pick this up.
823 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
825 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
826 g_signal_connect(G_OBJECT(c->win),
828 G_CALLBACK(destroywin), c);
829 g_signal_connect(G_OBJECT(c->win),
830 "leave_notify_event",
831 G_CALLBACK(titlechangeleave), c);
837 c->pane = gtk_vpaned_new();
840 c->vbox = gtk_vbox_new(FALSE, 0);
841 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
844 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
846 g_signal_connect(G_OBJECT(c->view),
848 G_CALLBACK(titlechange), c);
849 g_signal_connect(G_OBJECT(c->view),
850 "hovering-over-link",
851 G_CALLBACK(linkhover), c);
852 g_signal_connect(G_OBJECT(c->view),
853 "geolocation-policy-decision-requested",
854 G_CALLBACK(geopolicyrequested), c);
855 g_signal_connect(G_OBJECT(c->view),
857 G_CALLBACK(createwindow), c);
858 g_signal_connect(G_OBJECT(c->view),
859 "new-window-policy-decision-requested",
860 G_CALLBACK(decidewindow), c);
861 g_signal_connect(G_OBJECT(c->view),
862 "mime-type-policy-decision-requested",
863 G_CALLBACK(decidedownload), c);
864 g_signal_connect(G_OBJECT(c->view),
865 "window-object-cleared",
866 G_CALLBACK(windowobjectcleared), c);
867 g_signal_connect(G_OBJECT(c->view),
868 "notify::load-status",
869 G_CALLBACK(loadstatuschange), c);
870 g_signal_connect(G_OBJECT(c->view),
872 G_CALLBACK(progresschange), c);
873 g_signal_connect(G_OBJECT(c->view),
874 "download-requested",
875 G_CALLBACK(initdownload), c);
876 g_signal_connect(G_OBJECT(c->view),
877 "button-release-event",
878 G_CALLBACK(buttonrelease), c);
879 g_signal_connect(G_OBJECT(c->view),
881 G_CALLBACK(contextmenu), c);
882 g_signal_connect(G_OBJECT(c->view),
883 "resource-request-starting",
884 G_CALLBACK(beforerequest), c);
885 g_signal_connect(G_OBJECT(c->view),
886 "should-show-delete-interface-for-element",
887 G_CALLBACK(deletion_interface), c);
889 /* Scrolled Window */
890 c->scroll = gtk_scrolled_window_new(NULL, NULL);
892 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
893 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
894 G_CALLBACK(gtk_true), NULL);
896 if(!enablescrollbars) {
897 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
898 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
900 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
901 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
905 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
906 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
907 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
910 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
911 TRUE, 0, GTK_PACK_START);
912 gtk_widget_grab_focus(GTK_WIDGET(c->view));
913 gtk_widget_show(c->pane);
914 gtk_widget_show(c->vbox);
915 gtk_widget_show(c->scroll);
916 gtk_widget_show(GTK_WIDGET(c->view));
917 gtk_widget_show(c->win);
918 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
920 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
921 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
922 webkit_web_view_set_full_content_zoom(c->view, TRUE);
926 settings = webkit_web_view_get_settings(c->view);
927 if(!(ua = getenv("SURF_USERAGENT")))
929 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
931 g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
932 getstyle("about:blank"), NULL);
934 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
936 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
938 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
940 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
941 enablespatialbrowsing, NULL);
942 g_object_set(G_OBJECT(settings), "enable-developer-extras",
943 enableinspector, NULL);
944 g_object_set(G_OBJECT(settings), "enable-default-context-menu",
945 kioskmode ^ 1, NULL);
946 g_object_set(G_OBJECT(settings), "default-font-size",
947 defaultfontsize, NULL);
948 g_object_set(G_OBJECT(settings), "resizable-text-areas",
952 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
953 * This ensures websites are not unusably small with a high DPI screen.
954 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
957 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
958 dpi = gdk_screen_get_resolution(screen);
960 g_object_set(G_OBJECT(settings), "enforce-96-dpi", true,
962 webkit_web_view_set_zoom_level(c->view, dpi/96);
965 /* This might conflict with _zoomto96dpi_. */
967 webkit_web_view_set_zoom_level(c->view, zoomlevel);
969 if(enableinspector) {
970 c->inspector = WEBKIT_WEB_INSPECTOR(
971 webkit_web_view_get_inspector(c->view));
972 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
973 G_CALLBACK(inspector_new), c);
974 g_signal_connect(G_OBJECT(c->inspector), "show-window",
975 G_CALLBACK(inspector_show), c);
976 g_signal_connect(G_OBJECT(c->inspector), "close-window",
977 G_CALLBACK(inspector_close), c);
978 g_signal_connect(G_OBJECT(c->inspector), "finished",
979 G_CALLBACK(inspector_finished), c);
980 c->isinspecting = false;
983 if(runinfullscreen) {
988 setatom(c, AtomFind, "");
989 setatom(c, AtomUri, "about:blank");
991 webkit_web_view_set_transparent(c->view, TRUE);
997 gdk_display_sync(gtk_widget_get_display(c->win));
999 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1001 if (fclose(stdout) != 0) {
1002 die("Error closing stdout");
1010 newwindow(Client *c, const Arg *arg, gboolean noembed) {
1012 const char *cmd[18], *uri;
1013 const Arg a = { .v = (void *)cmd };
1018 cmd[i++] = cookiepolicies;
1019 if(!enablescrollbars)
1021 if(embed && !noembed) {
1023 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1026 if(!allowgeolocation)
1041 cmd[i++] = cookiefile;
1043 uri = arg->v ? (char *)arg->v : c->linkhover;
1051 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1052 gboolean keyboard, Client *c) {
1053 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1055 for(GList *l = items; l; l = l->next) {
1056 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1064 menuactivate(GtkMenuItem *item, Client *c) {
1066 * context-menu-action-2000 open link
1067 * context-menu-action-1 open link in window
1068 * context-menu-action-2 download linked file
1069 * context-menu-action-3 copy link location
1070 * context-menu-action-7 copy image address
1071 * context-menu-action-13 reload
1072 * context-menu-action-10 back
1073 * context-menu-action-11 forward
1074 * context-menu-action-12 stop
1077 GtkAction *a = NULL;
1078 const char *name, *uri;
1079 GtkClipboard *prisel, *clpbrd;
1081 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
1085 name = gtk_action_get_name(a);
1086 if(!g_strcmp0(name, "context-menu-action-3")) {
1087 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1088 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1089 } else if(!g_strcmp0(name, "context-menu-action-7")) {
1090 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1091 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1092 uri = gtk_clipboard_wait_for_text(clpbrd);
1094 gtk_clipboard_set_text(prisel, uri, -1);
1099 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
1100 Arg arg = {.v = text };
1102 loaduri((Client *) d, &arg);
1106 print(Client *c, const Arg *arg) {
1107 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1110 static GdkFilterReturn
1111 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
1112 Client *c = (Client *)d;
1116 if(((XEvent *)e)->type == PropertyNotify) {
1117 ev = &((XEvent *)e)->xproperty;
1118 if(ev->state == PropertyNewValue) {
1119 if(ev->atom == atoms[AtomFind]) {
1123 return GDK_FILTER_REMOVE;
1124 } else if(ev->atom == atoms[AtomGo]) {
1125 arg.v = getatom(c, AtomGo);
1128 return GDK_FILTER_REMOVE;
1132 return GDK_FILTER_CONTINUE;
1136 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
1137 c->progress = webkit_web_view_get_progress(c->view) * 100;
1142 linkopen(Client *c, const Arg *arg) {
1143 newwindow(NULL, arg, 1);
1147 linkopenembed(Client *c, const Arg *arg) {
1148 newwindow(NULL, arg, 0);
1152 reload(Client *c, const Arg *arg) {
1153 gboolean nocache = *(gboolean *)arg;
1155 webkit_web_view_reload_bypass_cache(c->view);
1157 webkit_web_view_reload(c->view);
1162 scroll_h(Client *c, const Arg *arg) {
1163 scroll(gtk_scrolled_window_get_hadjustment(
1164 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1168 scroll_v(Client *c, const Arg *arg) {
1169 scroll(gtk_scrolled_window_get_vadjustment(
1170 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1174 scroll(GtkAdjustment *a, const Arg *arg) {
1177 v = gtk_adjustment_get_value(a);
1181 v += gtk_adjustment_get_page_increment(a) *
1187 v += gtk_adjustment_get_step_increment(a) * arg->i;
1191 v = MIN(v, gtk_adjustment_get_upper(a) -
1192 gtk_adjustment_get_page_size(a));
1193 gtk_adjustment_set_value(a, v);
1197 setatom(Client *c, int a, const char *v) {
1199 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1200 atoms[a], XA_STRING, 8, PropModeReplace,
1201 (unsigned char *)v, strlen(v) + 1);
1211 GError *error = NULL;
1213 /* clean up any zombies immediately */
1215 gtk_init(NULL, NULL);
1217 dpy = GDK_DISPLAY();
1220 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1221 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1222 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1224 /* dirs and files */
1225 cookiefile = buildpath(cookiefile);
1226 scriptfile = buildpath(scriptfile);
1227 cachefolder = buildpath(cachefolder);
1228 styledir = buildpath(styledir);
1229 if(stylefile == NULL) {
1230 for(i = 0; i < LENGTH(styles); i++) {
1231 if(regcomp(&(styles[i].re), styles[i].regex,
1234 "Could not compile regex: %s\n",
1236 styles[i].regex = NULL;
1238 styles[i].style = buildpath(
1239 g_strconcat(styledir,
1240 styles[i].style, NULL));
1243 stylefile = buildpath(stylefile);
1246 /* request handler */
1247 s = webkit_get_default_session();
1250 soup_session_add_feature(s,
1251 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE,
1252 cookiepolicy_get())));
1255 if(enablediskcache) {
1256 diskcache = soup_cache_new(cachefolder, SOUP_CACHE_SINGLE_USER);
1257 soup_cache_set_max_size(diskcache, diskcachebytes);
1258 soup_cache_load(diskcache);
1259 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1263 tlsdb = g_tls_file_database_new(cafile, &error);
1266 g_warning("Error loading SSL database %s: %s", cafile, error->message);
1267 g_error_free(error);
1269 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1270 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1273 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1274 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1275 g_strdup_printf("http://%s", proxy);
1276 puri = soup_uri_new(new_proxy);
1277 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1278 soup_uri_free(puri);
1285 sigchld(int unused) {
1286 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1287 die("Can't install SIGCHLD handler");
1288 while(0 < waitpid(-1, NULL, WNOHANG));
1292 source(Client *c, const Arg *arg) {
1293 Arg a = { .b = FALSE };
1296 s = webkit_web_view_get_view_source_mode(c->view);
1297 webkit_web_view_set_view_source_mode(c->view, !s);
1302 spawn(Client *c, const Arg *arg) {
1305 close(ConnectionNumber(dpy));
1307 execvp(((char **)arg->v)[0], (char **)arg->v);
1308 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1315 eval(Client *c, const Arg *arg) {
1316 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1317 evalscript(webkit_web_frame_get_global_context(frame),
1318 ((char **)arg->v)[0], "");
1322 stop(Client *c, const Arg *arg) {
1323 webkit_web_view_stop_loading(c->view);
1327 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
1328 const gchar *t = webkit_web_view_get_title(view);
1330 c->title = copystr(&c->title, t);
1336 titlechangeleave(void *a, void *b, Client *c) {
1337 c->linkhover = NULL;
1342 toggle(Client *c, const Arg *arg) {
1343 WebKitWebSettings *settings;
1344 char *name = (char *)arg->v;
1346 Arg a = { .b = FALSE };
1348 settings = webkit_web_view_get_settings(c->view);
1349 g_object_get(G_OBJECT(settings), name, &value, NULL);
1350 g_object_set(G_OBJECT(settings), name, !value, NULL);
1356 togglecookiepolicy(Client *c, const Arg *arg) {
1358 SoupCookieJarAcceptPolicy policy;
1360 jar = SOUP_COOKIE_JAR(
1361 soup_session_get_feature(
1362 webkit_get_default_session(),
1363 SOUP_TYPE_COOKIE_JAR));
1364 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1367 if(policysel >= strlen(cookiepolicies))
1370 g_object_set(G_OBJECT(jar), "accept-policy",
1371 cookiepolicy_get(), NULL);
1374 /* Do not reload. */
1378 togglegeolocation(Client *c, const Arg *arg) {
1379 Arg a = { .b = FALSE };
1381 allowgeolocation ^= 1;
1387 twitch(Client *c, const Arg *arg) {
1391 a = gtk_scrolled_window_get_vadjustment(
1392 GTK_SCROLLED_WINDOW(c->scroll));
1394 v = gtk_adjustment_get_value(a);
1399 v = MIN(v, gtk_adjustment_get_upper(a) -
1400 gtk_adjustment_get_page_size(a));
1401 gtk_adjustment_set_value(a, v);
1405 togglescrollbars(Client *c, const Arg *arg) {
1406 GtkPolicyType vspolicy;
1409 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1411 if(vspolicy == GTK_POLICY_AUTOMATIC) {
1412 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1413 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1415 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1416 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1425 togglestyle(Client *c, const Arg *arg) {
1426 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1429 enablestyles = !enablestyles;
1430 uri = enablestyles ? getstyle(geturi(c)) : g_strdup("");
1431 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1437 gettogglestat(Client *c){
1440 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1442 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1444 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1446 togglestat[p++] = value? 'C': 'c';
1448 togglestat[p++] = allowgeolocation? 'G': 'g';
1450 togglestat[p++] = enablediskcache? 'D': 'd';
1452 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1453 togglestat[p++] = value? 'I': 'i';
1455 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1456 togglestat[p++] = value? 'S': 's';
1458 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1459 togglestat[p++] = value? 'V': 'v';
1461 togglestat[p++] = enablestyles ? 'M': 'm';
1463 togglestat[p] = '\0';
1467 getpagestat(Client *c) {
1468 const char *uri = geturi(c);
1470 if(strstr(uri, "https://") == uri) {
1471 pagestat[0] = c->sslfailed ? 'U' : 'T';
1476 pagestat[1] = usingproxy ? 'P' : '-';
1482 updatetitle(Client *c) {
1485 if(showindicators) {
1490 t = g_strdup_printf("%s:%s | %s", togglestat,
1491 pagestat, c->linkhover);
1492 } else if(c->progress != 100) {
1493 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1494 togglestat, pagestat,
1495 (c->title == NULL)? "" : c->title);
1497 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1498 (c->title == NULL)? "" : c->title);
1501 gtk_window_set_title(GTK_WINDOW(c->win), t);
1504 gtk_window_set_title(GTK_WINDOW(c->win),
1505 (c->title == NULL)? "" : c->title);
1510 updatewinid(Client *c) {
1511 snprintf(winid, LENGTH(winid), "%u",
1512 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1517 die("usage: %s [-bBfFgGiIkKnNpPsSvx]"
1518 " [-a cookiepolicies ] "
1519 " [-c cookiefile] [-e xid] [-r scriptfile]"
1520 " [-t stylefile] [-u useragent] [-z zoomlevel]"
1521 " [uri]\n", basename(argv0));
1525 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1526 JSObjectRef win, Client *c) {
1531 zoom(Client *c, const Arg *arg) {
1535 webkit_web_view_zoom_out(c->view);
1536 } else if(arg->i > 0) {
1538 webkit_web_view_zoom_in(c->view);
1542 webkit_web_view_set_zoom_level(c->view, 1.0);
1547 main(int argc, char *argv[]) {
1551 memset(&arg, 0, sizeof(arg));
1553 /* command line args */
1556 cookiepolicies = EARGF(usage());
1559 enablescrollbars = 0;
1562 enablescrollbars = 1;
1565 cookiefile = EARGF(usage());
1568 enablediskcache = 0;
1571 enablediskcache = 1;
1574 embed = strtol(EARGF(usage()), NULL, 0);
1577 runinfullscreen = 1;
1580 runinfullscreen = 0;
1583 allowgeolocation = 0;
1586 allowgeolocation = 1;
1607 enableinspector = 0;
1610 enableinspector = 1;
1619 scriptfile = EARGF(usage());
1628 stylefile = EARGF(usage());
1631 useragent = EARGF(usage());
1634 die("surf-"VERSION", ©2009-2014 surf engineers, "
1635 "see LICENSE for details\n");
1640 zoomlevel = strtof(EARGF(usage()), NULL);
1651 loaduri(clients, &arg);
1659 return EXIT_SUCCESS;