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 typedef union Arg Arg;
46 typedef struct Client {
47 GtkWidget *win, *scroll, *vbox, *pane;
49 WebKitWebInspector *inspector;
50 char *title, *linkhover;
54 gboolean zoomed, fullscreen, isinspecting, sslfailed;
60 void (*func)(Client *c, const Arg *arg);
65 SoupCookieJarText parent_instance;
70 SoupCookieJarTextClass parent_class;
73 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
82 static Atom atoms[AtomLast];
83 static Client *clients = NULL;
84 static GdkNativeWindow embed = 0;
85 static gboolean showxid = FALSE;
86 static char winid[64];
87 static gboolean usingproxy = 0;
88 static char togglestat[9];
89 static char pagestat[3];
90 static GTlsDatabase *tlsdb;
91 static int policysel = 0;
92 static char *stylefile = NULL;
93 static SoupCache *diskcache = NULL;
95 static void addaccelgroup(Client *c);
96 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
97 WebKitWebResource *r, WebKitNetworkRequest *req,
98 WebKitNetworkResponse *resp, Client *c);
99 static char *buildpath(const char *path);
100 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
102 static void cleanup(void);
103 static void clipboard(Client *c, const Arg *arg);
105 /* Cookiejar implementation */
106 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
107 SoupCookie *new_cookie);
108 static void cookiejar_finalize(GObject *self);
109 static SoupCookieJarAcceptPolicy cookiepolicy_get(void);
110 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only,
111 SoupCookieJarAcceptPolicy policy);
112 static void cookiejar_set_property(GObject *self, guint prop_id,
113 const GValue *value, GParamSpec *pspec);
114 static char cookiepolicy_set(const SoupCookieJarAcceptPolicy p);
116 static char *copystr(char **str, const char *src);
117 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
119 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
120 WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p,
122 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
123 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
124 WebKitWebPolicyDecision *p, Client *c);
125 static gboolean deletion_interface(WebKitWebView *view,
126 WebKitDOMHTMLElement *arg1, Client *c);
127 static void destroyclient(Client *c);
128 static void destroywin(GtkWidget* w, Client *c);
129 static void die(const char *errstr, ...);
130 static void eval(Client *c, const Arg *arg);
131 static void find(Client *c, const Arg *arg);
132 static void fullscreen(Client *c, const Arg *arg);
133 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
134 WebKitGeolocationPolicyDecision *d, Client *c);
135 static const char *getatom(Client *c, int a);
136 static void gettogglestat(Client *c);
137 static void getpagestat(Client *c);
138 static char *geturi(Client *c);
139 static gchar *getstyle(const char *uri);
141 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
143 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
145 static void inspector(Client *c, const Arg *arg);
146 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
148 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
149 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
150 static void inspector_finished(WebKitWebInspector *i, Client *c);
152 static gboolean keypress(GtkAccelGroup *group,
153 GObject *obj, guint key, GdkModifierType mods,
155 static void linkhover(WebKitWebView *v, const char* t, const char* l,
157 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
159 static void loaduri(Client *c, const Arg *arg);
160 static void navigate(Client *c, const Arg *arg);
161 static Client *newclient(void);
162 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
163 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
164 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
165 WebKitHitTestResult *target, gboolean keyboard, Client *c);
166 static void menuactivate(GtkMenuItem *item, Client *c);
167 static void print(Client *c, const Arg *arg);
168 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
170 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
171 static void reload(Client *c, const Arg *arg);
172 static void scroll_h(Client *c, const Arg *arg);
173 static void scroll_v(Client *c, const Arg *arg);
174 static void scroll(GtkAdjustment *a, const Arg *arg);
175 static void setatom(Client *c, int a, const char *v);
176 static void setup(void);
177 static void sigchld(int unused);
178 static void source(Client *c, const Arg *arg);
179 static void spawn(Client *c, const Arg *arg);
180 static void stop(Client *c, const Arg *arg);
181 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
182 static void toggle(Client *c, const Arg *arg);
183 static void togglecookiepolicy(Client *c, const Arg *arg);
184 static void togglegeolocation(Client *c, const Arg *arg);
185 static void togglescrollbars(Client *c, const Arg *arg);
186 static void togglestyle(Client *c, const Arg *arg);
187 static void updatetitle(Client *c);
188 static void updatewinid(Client *c);
189 static void usage(void);
190 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
191 JSContextRef js, JSObjectRef win, Client *c);
192 static void zoom(Client *c, const Arg *arg);
194 /* configuration, allows nested code to access above variables */
198 addaccelgroup(Client *c) {
200 GtkAccelGroup *group = gtk_accel_group_new();
203 for(i = 0; i < LENGTH(keys); i++) {
204 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
205 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod,
208 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
212 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
213 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
215 const gchar *uri = webkit_network_request_get_uri(req);
218 if(g_str_has_suffix(uri, "/favicon.ico"))
219 webkit_network_request_set_uri(req, "about:blank");
221 if(!g_str_has_prefix(uri, "http://") \
222 && !g_str_has_prefix(uri, "https://") \
223 && !g_str_has_prefix(uri, "about:") \
224 && !g_str_has_prefix(uri, "file://") \
225 && !g_str_has_prefix(uri, "data:") \
226 && !g_str_has_prefix(uri, "blob:") \
227 && strlen(uri) > 0) {
229 for(i = 0; i < strlen(uri); i++) {
230 if(!g_ascii_isprint(uri[i])) {
236 handleplumb(c, w, uri);
241 buildpath(const char *path) {
245 /* creating directory */
247 apath = g_strdup(path);
248 } else if(path[0] == '~') {
250 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
252 apath = g_strconcat(g_get_home_dir(), "/",
256 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
259 if((p = strrchr(apath, '/'))) {
261 g_mkdir_with_parents(apath, 0700);
262 g_chmod(apath, 0700); /* in case it existed */
265 /* creating file (gives error when apath ends with "/") */
266 if((f = fopen(apath, "a"))) {
267 g_chmod(apath, 0600); /* always */
275 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
276 WebKitHitTestResultContext context;
277 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
281 g_object_get(result, "context", &context, NULL);
282 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
284 (e->button == 1 && CLEANMASK(e->state) == CLEANMASK(MODKEY))) {
285 g_object_get(result, "link-uri", &arg.v, NULL);
286 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
296 soup_cache_flush(diskcache);
297 soup_cache_dump(diskcache);
300 destroyclient(clients);
307 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
308 SoupCookie *new_cookie) {
309 flock(COOKIEJAR(self)->lock, LOCK_EX);
310 if(new_cookie && !new_cookie->expires && sessiontime) {
311 soup_cookie_set_expires(new_cookie,
312 soup_date_new_from_now(sessiontime));
314 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
315 old_cookie, new_cookie);
316 flock(COOKIEJAR(self)->lock, LOCK_UN);
320 cookiejar_class_init(CookieJarClass *klass) {
321 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
322 G_OBJECT_CLASS(klass)->get_property =
323 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
324 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
325 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
326 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
330 cookiejar_finalize(GObject *self) {
331 close(COOKIEJAR(self)->lock);
332 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
336 cookiejar_init(CookieJar *self) {
337 self->lock = open(cookiefile, 0);
340 static SoupCookieJar *
341 cookiejar_new(const char *filename, gboolean read_only,
342 SoupCookieJarAcceptPolicy policy) {
343 return g_object_new(COOKIEJAR_TYPE,
344 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
345 SOUP_COOKIE_JAR_READ_ONLY, read_only,
346 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
350 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
352 flock(COOKIEJAR(self)->lock, LOCK_SH);
353 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
355 flock(COOKIEJAR(self)->lock, LOCK_UN);
358 static SoupCookieJarAcceptPolicy
359 cookiepolicy_get(void) {
360 switch(cookiepolicies[policysel]) {
362 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
364 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
370 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
374 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep) {
376 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
378 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
380 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
389 evalscript(JSContextRef js, char *script, char* scriptname) {
390 JSStringRef jsscript, jsscriptname;
391 JSValueRef exception = NULL;
393 jsscript = JSStringCreateWithUTF8CString(script);
394 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
395 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
396 jsscriptname, 0, &exception);
397 JSStringRelease(jsscript);
398 JSStringRelease(jsscriptname);
402 runscript(WebKitWebFrame *frame) {
406 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
407 evalscript(webkit_web_frame_get_global_context(frame),
413 clipboard(Client *c, const Arg *arg) {
414 gboolean paste = *(gboolean *)arg;
417 gtk_clipboard_request_text(
418 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
421 gtk_clipboard_set_text(
422 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
423 c->linkhover ? c->linkhover : geturi(c), -1);
428 copystr(char **str, const char *src) {
439 static WebKitWebView *
440 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
441 Client *n = newclient();
446 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
447 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
448 if(!webkit_web_view_can_show_mime_type(v, m)) {
449 webkit_web_policy_decision_download(p);
456 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
457 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
461 if(webkit_web_navigation_action_get_reason(n) ==
462 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
463 webkit_web_policy_decision_ignore(p);
464 arg.v = (void *)webkit_network_request_get_uri(r);
465 newwindow(NULL, &arg, 0);
472 deletion_interface(WebKitWebView *view,
473 WebKitDOMHTMLElement *arg1, Client *c) {
478 destroyclient(Client *c) {
481 webkit_web_view_stop_loading(c->view);
482 gtk_widget_destroy(GTK_WIDGET(c->view));
483 gtk_widget_destroy(c->scroll);
484 gtk_widget_destroy(c->vbox);
485 gtk_widget_destroy(c->win);
487 for(p = clients; p && p->next != c; p = p->next);
499 destroywin(GtkWidget* w, Client *c) {
504 die(const char *errstr, ...) {
507 va_start(ap, errstr);
508 vfprintf(stderr, errstr, ap);
514 find(Client *c, const Arg *arg) {
517 s = getatom(c, AtomFind);
518 gboolean forward = *(gboolean *)arg;
519 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
523 fullscreen(Client *c, const Arg *arg) {
525 gtk_window_unfullscreen(GTK_WINDOW(c->win));
527 gtk_window_fullscreen(GTK_WINDOW(c->win));
529 c->fullscreen = !c->fullscreen;
533 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
534 WebKitGeolocationPolicyDecision *d, Client *c) {
535 if(allowgeolocation) {
536 webkit_geolocation_policy_allow(d);
538 webkit_geolocation_policy_deny(d);
543 getatom(Client *c, int a) {
544 static char buf[BUFSIZ];
547 unsigned long ldummy;
548 unsigned char *p = NULL;
550 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
551 atoms[a], 0L, BUFSIZ, False, XA_STRING,
552 &adummy, &idummy, &ldummy, &ldummy, &p);
554 strncpy(buf, (char *)p, LENGTH(buf)-1);
567 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
573 getstyle(const char *uri) {
576 if(stylefile != NULL)
577 return g_strconcat("file://", stylefile, NULL);
579 for(i = 0; i < LENGTH(styles); i++) {
580 if(styles[i].regex && !regexec(&(styles[i].re), uri, 0,
582 return g_strconcat("file://", styles[i].style, NULL);
589 handleplumb(Client *c, WebKitWebView *w, const gchar *uri) {
592 webkit_web_view_stop_loading(w);
593 arg = (Arg)PLUMB((char *)uri);
598 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
602 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
608 inspector(Client *c, const Arg *arg) {
609 if(c->isinspecting) {
610 webkit_web_inspector_close(c->inspector);
612 webkit_web_inspector_show(c->inspector);
616 static WebKitWebView *
617 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
618 return WEBKIT_WEB_VIEW(webkit_web_view_new());
622 inspector_show(WebKitWebInspector *i, Client *c) {
628 w = webkit_web_inspector_get_web_view(i);
629 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
630 gtk_widget_show(GTK_WIDGET(w));
631 c->isinspecting = true;
637 inspector_close(WebKitWebInspector *i, Client *c) {
643 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
645 gtk_widget_destroy(w);
646 c->isinspecting = false;
652 inspector_finished(WebKitWebInspector *i, Client *c) {
653 g_free(c->inspector);
657 keypress(GtkAccelGroup *group, GObject *obj,
658 guint key, GdkModifierType mods, Client *c) {
660 gboolean processed = FALSE;
662 mods = CLEANMASK(mods);
663 key = gdk_keyval_to_lower(key);
665 for(i = 0; i < LENGTH(keys); i++) {
666 if(key == keys[i].keyval
667 && mods == keys[i].mod
669 keys[i].func(c, &(keys[i].arg));
678 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
680 c->linkhover = copystr(&c->linkhover, l);
681 } else if(c->linkhover) {
689 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
690 WebKitWebFrame *frame;
691 WebKitWebDataSource *src;
692 WebKitNetworkRequest *request;
693 WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
697 switch(webkit_web_view_get_load_status (c->view)) {
698 case WEBKIT_LOAD_COMMITTED:
700 if(strstr(uri, "https://") == uri) {
701 frame = webkit_web_view_get_main_frame(c->view);
702 src = webkit_web_frame_get_data_source(frame);
703 request = webkit_web_data_source_get_request(src);
704 msg = webkit_network_request_get_message(request);
705 c->sslfailed = !(soup_message_get_flags(msg)
706 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
708 setatom(c, AtomUri, uri);
711 g_object_set(G_OBJECT(set), "user-stylesheet-uri",
712 getstyle(uri), NULL);
715 case WEBKIT_LOAD_FINISHED:
719 soup_cache_flush(diskcache);
720 soup_cache_dump(diskcache);
729 loaduri(Client *c, const Arg *arg) {
731 const char *uri = (char *)arg->v;
732 Arg a = { .b = FALSE };
735 if(strcmp(uri, "") == 0)
738 /* In case it's a file path. */
739 if(stat(uri, &st) == 0) {
740 rp = realpath(uri, NULL);
741 u = g_strdup_printf("file://%s", rp);
744 u = g_strrstr(uri, "://") ? g_strdup(uri)
745 : g_strdup_printf("http://%s", uri);
748 setatom(c, AtomUri, uri);
750 /* prevents endless loop */
751 if(strcmp(u, geturi(c)) == 0) {
754 webkit_web_view_load_uri(c->view, u);
756 c->title = copystr(&c->title, u);
763 navigate(Client *c, const Arg *arg) {
764 int steps = *(int *)arg;
765 webkit_web_view_go_back_or_forward(c->view, steps);
771 WebKitWebSettings *settings;
772 WebKitWebFrame *frame;
773 GdkGeometry hints = { 1, 1 };
778 if(!(c = calloc(1, sizeof(Client))))
779 die("Cannot malloc!\n");
786 c->win = gtk_plug_new(embed);
788 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
790 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
791 * is always correct, so we should still call this function.
792 * But when doing so, we *must* differentiate between a
793 * WM_CLASS and a resource on the window. By convention, the
794 * window class (WM_CLASS) is capped, while the resource is in
795 * lowercase. Both these values come as a pair.
797 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
799 /* TA: 20091214: And set the role here as well -- so that
800 * sessions can pick this up.
802 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
804 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
805 g_signal_connect(G_OBJECT(c->win),
807 G_CALLBACK(destroywin), c);
813 c->pane = gtk_vpaned_new();
816 c->vbox = gtk_vbox_new(FALSE, 0);
817 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
820 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
822 g_signal_connect(G_OBJECT(c->view),
824 G_CALLBACK(titlechange), c);
825 g_signal_connect(G_OBJECT(c->view),
826 "hovering-over-link",
827 G_CALLBACK(linkhover), c);
828 g_signal_connect(G_OBJECT(c->view),
829 "geolocation-policy-decision-requested",
830 G_CALLBACK(geopolicyrequested), c);
831 g_signal_connect(G_OBJECT(c->view),
833 G_CALLBACK(createwindow), c);
834 g_signal_connect(G_OBJECT(c->view),
835 "new-window-policy-decision-requested",
836 G_CALLBACK(decidewindow), c);
837 g_signal_connect(G_OBJECT(c->view),
838 "mime-type-policy-decision-requested",
839 G_CALLBACK(decidedownload), c);
840 g_signal_connect(G_OBJECT(c->view),
841 "window-object-cleared",
842 G_CALLBACK(windowobjectcleared), c);
843 g_signal_connect(G_OBJECT(c->view),
844 "notify::load-status",
845 G_CALLBACK(loadstatuschange), c);
846 g_signal_connect(G_OBJECT(c->view),
848 G_CALLBACK(progresschange), c);
849 g_signal_connect(G_OBJECT(c->view),
850 "download-requested",
851 G_CALLBACK(initdownload), c);
852 g_signal_connect(G_OBJECT(c->view),
853 "button-release-event",
854 G_CALLBACK(buttonrelease), c);
855 g_signal_connect(G_OBJECT(c->view),
857 G_CALLBACK(contextmenu), c);
858 g_signal_connect(G_OBJECT(c->view),
859 "resource-request-starting",
860 G_CALLBACK(beforerequest), c);
861 g_signal_connect(G_OBJECT(c->view),
862 "should-show-delete-interface-for-element",
863 G_CALLBACK(deletion_interface), c);
865 /* Scrolled Window */
866 c->scroll = gtk_scrolled_window_new(NULL, NULL);
868 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
869 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
870 G_CALLBACK(gtk_true), NULL);
872 if(!enablescrollbars) {
873 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
874 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
876 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
877 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
881 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
882 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
883 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
886 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
887 TRUE, 0, GTK_PACK_START);
888 gtk_widget_grab_focus(GTK_WIDGET(c->view));
889 gtk_widget_show(c->pane);
890 gtk_widget_show(c->vbox);
891 gtk_widget_show(c->scroll);
892 gtk_widget_show(GTK_WIDGET(c->view));
893 gtk_widget_show(c->win);
894 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
896 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
897 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
898 webkit_web_view_set_full_content_zoom(c->view, TRUE);
902 settings = webkit_web_view_get_settings(c->view);
903 if(!(ua = getenv("SURF_USERAGENT")))
905 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
907 g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
908 getstyle("about:blank"), NULL);
910 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
912 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
914 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
916 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
917 enablespatialbrowsing, NULL);
918 g_object_set(G_OBJECT(settings), "enable-developer-extras",
919 enableinspector, NULL);
920 g_object_set(G_OBJECT(settings), "enable-default-context-menu",
921 kioskmode ^ 1, NULL);
922 g_object_set(G_OBJECT(settings), "default-font-size",
923 defaultfontsize, NULL);
924 g_object_set(G_OBJECT(settings), "resizable-text-areas",
928 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
929 * This ensures websites are not unusably small with a high DPI screen.
930 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
933 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
934 dpi = gdk_screen_get_resolution(screen);
936 g_object_set(G_OBJECT(settings), "enforce-96-dpi", true,
938 webkit_web_view_set_zoom_level(c->view, dpi/96);
941 /* This might conflict with _zoomto96dpi_. */
943 webkit_web_view_set_zoom_level(c->view, zoomlevel);
945 if(enableinspector) {
946 c->inspector = WEBKIT_WEB_INSPECTOR(
947 webkit_web_view_get_inspector(c->view));
948 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
949 G_CALLBACK(inspector_new), c);
950 g_signal_connect(G_OBJECT(c->inspector), "show-window",
951 G_CALLBACK(inspector_show), c);
952 g_signal_connect(G_OBJECT(c->inspector), "close-window",
953 G_CALLBACK(inspector_close), c);
954 g_signal_connect(G_OBJECT(c->inspector), "finished",
955 G_CALLBACK(inspector_finished), c);
956 c->isinspecting = false;
959 if(runinfullscreen) {
964 setatom(c, AtomFind, "");
965 setatom(c, AtomUri, "about:blank");
967 webkit_web_view_set_transparent(c->view, TRUE);
973 gdk_display_sync(gtk_widget_get_display(c->win));
975 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
977 if (fclose(stdout) != 0) {
978 die("Error closing stdout");
986 newwindow(Client *c, const Arg *arg, gboolean noembed) {
988 const char *cmd[18], *uri;
989 const Arg a = { .v = (void *)cmd };
994 cmd[i++] = cookiepolicies;
995 if(!enablescrollbars)
997 if(embed && !noembed) {
999 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1002 if(!allowgeolocation)
1017 cmd[i++] = cookiefile;
1019 uri = arg->v ? (char *)arg->v : c->linkhover;
1027 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1028 gboolean keyboard, Client *c) {
1029 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1031 for(GList *l = items; l; l = l->next) {
1032 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1040 menuactivate(GtkMenuItem *item, Client *c) {
1042 * context-menu-action-2000 open link
1043 * context-menu-action-1 open link in window
1044 * context-menu-action-2 download linked file
1045 * context-menu-action-3 copy link location
1046 * context-menu-action-7 copy image address
1047 * context-menu-action-13 reload
1048 * context-menu-action-10 back
1049 * context-menu-action-11 forward
1050 * context-menu-action-12 stop
1053 GtkAction *a = NULL;
1054 const char *name, *uri;
1055 GtkClipboard *prisel, *clpbrd;
1057 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
1061 name = gtk_action_get_name(a);
1062 if(!g_strcmp0(name, "context-menu-action-3")) {
1063 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1064 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1065 } else if(!g_strcmp0(name, "context-menu-action-7")) {
1066 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1067 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1068 uri = gtk_clipboard_wait_for_text(clpbrd);
1070 gtk_clipboard_set_text(prisel, uri, -1);
1075 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
1076 Arg arg = {.v = text };
1078 loaduri((Client *) d, &arg);
1082 print(Client *c, const Arg *arg) {
1083 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1086 static GdkFilterReturn
1087 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
1088 Client *c = (Client *)d;
1092 if(((XEvent *)e)->type == PropertyNotify) {
1093 ev = &((XEvent *)e)->xproperty;
1094 if(ev->state == PropertyNewValue) {
1095 if(ev->atom == atoms[AtomFind]) {
1099 return GDK_FILTER_REMOVE;
1100 } else if(ev->atom == atoms[AtomGo]) {
1101 arg.v = getatom(c, AtomGo);
1104 return GDK_FILTER_REMOVE;
1108 return GDK_FILTER_CONTINUE;
1112 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
1113 c->progress = webkit_web_view_get_progress(c->view) * 100;
1118 reload(Client *c, const Arg *arg) {
1119 gboolean nocache = *(gboolean *)arg;
1121 webkit_web_view_reload_bypass_cache(c->view);
1123 webkit_web_view_reload(c->view);
1128 scroll_h(Client *c, const Arg *arg) {
1129 scroll(gtk_scrolled_window_get_hadjustment(
1130 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1134 scroll_v(Client *c, const Arg *arg) {
1135 scroll(gtk_scrolled_window_get_vadjustment(
1136 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1140 scroll(GtkAdjustment *a, const Arg *arg) {
1143 v = gtk_adjustment_get_value(a);
1147 v += gtk_adjustment_get_page_increment(a) *
1153 v += gtk_adjustment_get_step_increment(a) * arg->i;
1157 v = MIN(v, gtk_adjustment_get_upper(a) -
1158 gtk_adjustment_get_page_size(a));
1159 gtk_adjustment_set_value(a, v);
1163 setatom(Client *c, int a, const char *v) {
1165 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1166 atoms[a], XA_STRING, 8, PropModeReplace,
1167 (unsigned char *)v, strlen(v) + 1);
1177 GError *error = NULL;
1179 /* clean up any zombies immediately */
1181 gtk_init(NULL, NULL);
1183 dpy = GDK_DISPLAY();
1186 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1187 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1188 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1190 /* dirs and files */
1191 cookiefile = buildpath(cookiefile);
1192 scriptfile = buildpath(scriptfile);
1193 cachefolder = buildpath(cachefolder);
1194 styledir = buildpath(styledir);
1195 if(stylefile == NULL) {
1196 for(i = 0; i < LENGTH(styles); i++) {
1197 if(regcomp(&(styles[i].re), styles[i].regex,
1200 "Could not compile regex: %s\n",
1202 styles[i].regex = NULL;
1204 styles[i].style = buildpath(
1205 g_strconcat(styledir,
1206 styles[i].style, NULL));
1209 stylefile = buildpath(stylefile);
1212 /* request handler */
1213 s = webkit_get_default_session();
1216 soup_session_add_feature(s,
1217 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE,
1218 cookiepolicy_get())));
1221 if(enablediskcache) {
1222 diskcache = soup_cache_new(cachefolder, SOUP_CACHE_SINGLE_USER);
1223 soup_cache_set_max_size(diskcache, diskcachebytes);
1224 soup_cache_load(diskcache);
1225 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1229 tlsdb = g_tls_file_database_new(cafile, &error);
1232 g_warning("Error loading SSL database %s: %s", cafile, error->message);
1233 g_error_free(error);
1235 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1236 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1239 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1240 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1241 g_strdup_printf("http://%s", proxy);
1242 puri = soup_uri_new(new_proxy);
1243 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1244 soup_uri_free(puri);
1251 sigchld(int unused) {
1252 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1253 die("Can't install SIGCHLD handler");
1254 while(0 < waitpid(-1, NULL, WNOHANG));
1258 source(Client *c, const Arg *arg) {
1259 Arg a = { .b = FALSE };
1262 s = webkit_web_view_get_view_source_mode(c->view);
1263 webkit_web_view_set_view_source_mode(c->view, !s);
1268 spawn(Client *c, const Arg *arg) {
1271 close(ConnectionNumber(dpy));
1273 execvp(((char **)arg->v)[0], (char **)arg->v);
1274 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1281 eval(Client *c, const Arg *arg) {
1282 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1283 evalscript(webkit_web_frame_get_global_context(frame),
1284 ((char **)arg->v)[0], "");
1288 stop(Client *c, const Arg *arg) {
1289 webkit_web_view_stop_loading(c->view);
1293 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
1294 const gchar *t = webkit_web_view_get_title(view);
1296 c->title = copystr(&c->title, t);
1302 toggle(Client *c, const Arg *arg) {
1303 WebKitWebSettings *settings;
1304 char *name = (char *)arg->v;
1306 Arg a = { .b = FALSE };
1308 settings = webkit_web_view_get_settings(c->view);
1309 g_object_get(G_OBJECT(settings), name, &value, NULL);
1310 g_object_set(G_OBJECT(settings), name, !value, NULL);
1316 togglecookiepolicy(Client *c, const Arg *arg) {
1318 SoupCookieJarAcceptPolicy policy;
1320 jar = SOUP_COOKIE_JAR(
1321 soup_session_get_feature(
1322 webkit_get_default_session(),
1323 SOUP_TYPE_COOKIE_JAR));
1324 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1327 if(policysel >= strlen(cookiepolicies))
1330 g_object_set(G_OBJECT(jar), "accept-policy",
1331 cookiepolicy_get(), NULL);
1334 /* Do not reload. */
1338 togglegeolocation(Client *c, const Arg *arg) {
1339 Arg a = { .b = FALSE };
1341 allowgeolocation ^= 1;
1347 twitch(Client *c, const Arg *arg) {
1351 a = gtk_scrolled_window_get_vadjustment(
1352 GTK_SCROLLED_WINDOW(c->scroll));
1354 v = gtk_adjustment_get_value(a);
1359 v = MIN(v, gtk_adjustment_get_upper(a) -
1360 gtk_adjustment_get_page_size(a));
1361 gtk_adjustment_set_value(a, v);
1365 togglescrollbars(Client *c, const Arg *arg) {
1366 GtkPolicyType vspolicy;
1369 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1371 if(vspolicy == GTK_POLICY_AUTOMATIC) {
1372 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1373 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1375 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1376 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1385 togglestyle(Client *c, const Arg *arg) {
1386 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1389 enablestyles = !enablestyles;
1390 uri = enablestyles ? getstyle(geturi(c)) : g_strdup("");
1391 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1397 gettogglestat(Client *c){
1400 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1402 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1404 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1406 togglestat[p++] = value? 'C': 'c';
1408 togglestat[p++] = allowgeolocation? 'G': 'g';
1410 togglestat[p++] = enablediskcache? 'D': 'd';
1412 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1413 togglestat[p++] = value? 'I': 'i';
1415 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1416 togglestat[p++] = value? 'S': 's';
1418 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1419 togglestat[p++] = value? 'V': 'v';
1421 togglestat[p++] = enablestyles ? 'M': 'm';
1423 togglestat[p] = '\0';
1427 getpagestat(Client *c) {
1428 const char *uri = geturi(c);
1430 if(strstr(uri, "https://") == uri) {
1431 pagestat[0] = c->sslfailed ? 'U' : 'T';
1436 pagestat[1] = usingproxy ? 'P' : '-';
1442 updatetitle(Client *c) {
1445 if(showindicators) {
1450 t = g_strdup_printf("%s:%s | %s", togglestat,
1451 pagestat, c->linkhover);
1452 } else if(c->progress != 100) {
1453 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1454 togglestat, pagestat,
1455 (c->title == NULL)? "" : c->title);
1457 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1458 (c->title == NULL)? "" : c->title);
1461 gtk_window_set_title(GTK_WINDOW(c->win), t);
1464 gtk_window_set_title(GTK_WINDOW(c->win),
1465 (c->title == NULL)? "" : c->title);
1470 updatewinid(Client *c) {
1471 snprintf(winid, LENGTH(winid), "%u",
1472 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1477 die("usage: %s [-bBfFgGiIkKnNpPsSvx]"
1478 " [-a cookiepolicies ] "
1479 " [-c cookiefile] [-e xid] [-r scriptfile]"
1480 " [-t stylefile] [-u useragent] [-z zoomlevel]"
1481 " [uri]\n", basename(argv0));
1485 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1486 JSObjectRef win, Client *c) {
1491 zoom(Client *c, const Arg *arg) {
1495 webkit_web_view_zoom_out(c->view);
1496 } else if(arg->i > 0) {
1498 webkit_web_view_zoom_in(c->view);
1502 webkit_web_view_set_zoom_level(c->view, 1.0);
1507 main(int argc, char *argv[]) {
1511 memset(&arg, 0, sizeof(arg));
1513 /* command line args */
1516 cookiepolicies = EARGF(usage());
1519 enablescrollbars = 0;
1522 enablescrollbars = 1;
1525 cookiefile = EARGF(usage());
1528 enablediskcache = 0;
1531 enablediskcache = 1;
1534 embed = strtol(EARGF(usage()), NULL, 0);
1537 runinfullscreen = 1;
1540 runinfullscreen = 0;
1543 allowgeolocation = 0;
1546 allowgeolocation = 1;
1567 enableinspector = 0;
1570 enableinspector = 1;
1579 scriptfile = EARGF(usage());
1588 stylefile = EARGF(usage());
1591 useragent = EARGF(usage());
1594 die("surf-"VERSION", ©2009-2014 surf engineers, "
1595 "see LICENSE for details\n");
1600 zoomlevel = strtof(EARGF(usage()), NULL);
1611 loaduri(clients, &arg);
1619 return EXIT_SUCCESS;