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 titlechangeleave(void *a, void *b, Client *c);
183 static void toggle(Client *c, const Arg *arg);
184 static void togglecookiepolicy(Client *c, const Arg *arg);
185 static void togglegeolocation(Client *c, const Arg *arg);
186 static void togglescrollbars(Client *c, const Arg *arg);
187 static void togglestyle(Client *c, const Arg *arg);
188 static void updatetitle(Client *c);
189 static void updatewinid(Client *c);
190 static void usage(void);
191 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
192 JSContextRef js, JSObjectRef win, Client *c);
193 static void zoom(Client *c, const Arg *arg);
195 /* configuration, allows nested code to access above variables */
199 addaccelgroup(Client *c) {
201 GtkAccelGroup *group = gtk_accel_group_new();
204 for(i = 0; i < LENGTH(keys); i++) {
205 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
206 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod,
209 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
213 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
214 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
216 const gchar *uri = webkit_network_request_get_uri(req);
219 if(g_str_has_suffix(uri, "/favicon.ico"))
220 webkit_network_request_set_uri(req, "about:blank");
222 if(!g_str_has_prefix(uri, "http://") \
223 && !g_str_has_prefix(uri, "https://") \
224 && !g_str_has_prefix(uri, "about:") \
225 && !g_str_has_prefix(uri, "file://") \
226 && !g_str_has_prefix(uri, "data:") \
227 && !g_str_has_prefix(uri, "blob:") \
228 && strlen(uri) > 0) {
230 for(i = 0; i < strlen(uri); i++) {
231 if(!g_ascii_isprint(uri[i])) {
237 handleplumb(c, w, uri);
242 buildpath(const char *path) {
246 /* creating directory */
248 apath = g_strdup(path);
249 } else if(path[0] == '~') {
251 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
253 apath = g_strconcat(g_get_home_dir(), "/",
257 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
260 if((p = strrchr(apath, '/'))) {
262 g_mkdir_with_parents(apath, 0700);
263 g_chmod(apath, 0700); /* in case it existed */
266 /* creating file (gives error when apath ends with "/") */
267 if((f = fopen(apath, "a"))) {
268 g_chmod(apath, 0600); /* always */
276 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
277 WebKitHitTestResultContext context;
278 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
282 g_object_get(result, "context", &context, NULL);
283 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
285 (e->button == 1 && CLEANMASK(e->state) == CLEANMASK(MODKEY))) {
286 g_object_get(result, "link-uri", &arg.v, NULL);
287 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
297 soup_cache_flush(diskcache);
298 soup_cache_dump(diskcache);
301 destroyclient(clients);
308 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
309 SoupCookie *new_cookie) {
310 flock(COOKIEJAR(self)->lock, LOCK_EX);
311 if(new_cookie && !new_cookie->expires && sessiontime) {
312 soup_cookie_set_expires(new_cookie,
313 soup_date_new_from_now(sessiontime));
315 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
316 old_cookie, new_cookie);
317 flock(COOKIEJAR(self)->lock, LOCK_UN);
321 cookiejar_class_init(CookieJarClass *klass) {
322 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
323 G_OBJECT_CLASS(klass)->get_property =
324 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
325 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
326 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
327 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
331 cookiejar_finalize(GObject *self) {
332 close(COOKIEJAR(self)->lock);
333 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
337 cookiejar_init(CookieJar *self) {
338 self->lock = open(cookiefile, 0);
341 static SoupCookieJar *
342 cookiejar_new(const char *filename, gboolean read_only,
343 SoupCookieJarAcceptPolicy policy) {
344 return g_object_new(COOKIEJAR_TYPE,
345 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
346 SOUP_COOKIE_JAR_READ_ONLY, read_only,
347 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
351 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
353 flock(COOKIEJAR(self)->lock, LOCK_SH);
354 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
356 flock(COOKIEJAR(self)->lock, LOCK_UN);
359 static SoupCookieJarAcceptPolicy
360 cookiepolicy_get(void) {
361 switch(cookiepolicies[policysel]) {
363 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
365 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
371 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
375 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep) {
377 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
379 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
381 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
390 evalscript(JSContextRef js, char *script, char* scriptname) {
391 JSStringRef jsscript, jsscriptname;
392 JSValueRef exception = NULL;
394 jsscript = JSStringCreateWithUTF8CString(script);
395 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
396 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
397 jsscriptname, 0, &exception);
398 JSStringRelease(jsscript);
399 JSStringRelease(jsscriptname);
403 runscript(WebKitWebFrame *frame) {
407 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
408 evalscript(webkit_web_frame_get_global_context(frame),
414 clipboard(Client *c, const Arg *arg) {
415 gboolean paste = *(gboolean *)arg;
418 gtk_clipboard_request_text(
419 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
422 gtk_clipboard_set_text(
423 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
424 c->linkhover ? c->linkhover : geturi(c), -1);
429 copystr(char **str, const char *src) {
440 static WebKitWebView *
441 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
442 Client *n = newclient();
447 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
448 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
449 if(!webkit_web_view_can_show_mime_type(v, m)) {
450 webkit_web_policy_decision_download(p);
457 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
458 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
462 if(webkit_web_navigation_action_get_reason(n) ==
463 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
464 webkit_web_policy_decision_ignore(p);
465 arg.v = (void *)webkit_network_request_get_uri(r);
466 newwindow(NULL, &arg, 0);
473 deletion_interface(WebKitWebView *view,
474 WebKitDOMHTMLElement *arg1, Client *c) {
479 destroyclient(Client *c) {
482 webkit_web_view_stop_loading(c->view);
483 gtk_widget_destroy(GTK_WIDGET(c->view));
484 gtk_widget_destroy(c->scroll);
485 gtk_widget_destroy(c->vbox);
486 gtk_widget_destroy(c->win);
488 for(p = clients; p && p->next != c; p = p->next);
500 destroywin(GtkWidget* w, Client *c) {
505 die(const char *errstr, ...) {
508 va_start(ap, errstr);
509 vfprintf(stderr, errstr, ap);
515 find(Client *c, const Arg *arg) {
518 s = getatom(c, AtomFind);
519 gboolean forward = *(gboolean *)arg;
520 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
524 fullscreen(Client *c, const Arg *arg) {
526 gtk_window_unfullscreen(GTK_WINDOW(c->win));
528 gtk_window_fullscreen(GTK_WINDOW(c->win));
530 c->fullscreen = !c->fullscreen;
534 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
535 WebKitGeolocationPolicyDecision *d, Client *c) {
536 if(allowgeolocation) {
537 webkit_geolocation_policy_allow(d);
539 webkit_geolocation_policy_deny(d);
544 getatom(Client *c, int a) {
545 static char buf[BUFSIZ];
548 unsigned long ldummy;
549 unsigned char *p = NULL;
551 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
552 atoms[a], 0L, BUFSIZ, False, XA_STRING,
553 &adummy, &idummy, &ldummy, &ldummy, &p);
555 strncpy(buf, (char *)p, LENGTH(buf)-1);
568 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
574 getstyle(const char *uri) {
577 if(stylefile != NULL)
578 return g_strconcat("file://", stylefile, NULL);
580 for(i = 0; i < LENGTH(styles); i++) {
581 if(styles[i].regex && !regexec(&(styles[i].re), uri, 0,
583 return g_strconcat("file://", styles[i].style, NULL);
590 handleplumb(Client *c, WebKitWebView *w, const gchar *uri) {
593 webkit_web_view_stop_loading(w);
594 arg = (Arg)PLUMB((char *)uri);
599 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
603 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
609 inspector(Client *c, const Arg *arg) {
610 if(c->isinspecting) {
611 webkit_web_inspector_close(c->inspector);
613 webkit_web_inspector_show(c->inspector);
617 static WebKitWebView *
618 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
619 return WEBKIT_WEB_VIEW(webkit_web_view_new());
623 inspector_show(WebKitWebInspector *i, Client *c) {
629 w = webkit_web_inspector_get_web_view(i);
630 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
631 gtk_widget_show(GTK_WIDGET(w));
632 c->isinspecting = true;
638 inspector_close(WebKitWebInspector *i, Client *c) {
644 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
646 gtk_widget_destroy(w);
647 c->isinspecting = false;
653 inspector_finished(WebKitWebInspector *i, Client *c) {
654 g_free(c->inspector);
658 keypress(GtkAccelGroup *group, GObject *obj,
659 guint key, GdkModifierType mods, Client *c) {
661 gboolean processed = FALSE;
663 mods = CLEANMASK(mods);
664 key = gdk_keyval_to_lower(key);
666 for(i = 0; i < LENGTH(keys); i++) {
667 if(key == keys[i].keyval
668 && mods == keys[i].mod
670 keys[i].func(c, &(keys[i].arg));
679 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
681 c->linkhover = copystr(&c->linkhover, l);
682 } else if(c->linkhover) {
690 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
691 WebKitWebFrame *frame;
692 WebKitWebDataSource *src;
693 WebKitNetworkRequest *request;
694 WebKitWebSettings *set = webkit_web_view_get_settings(c->view);
698 switch(webkit_web_view_get_load_status (c->view)) {
699 case WEBKIT_LOAD_COMMITTED:
701 if(strstr(uri, "https://") == uri) {
702 frame = webkit_web_view_get_main_frame(c->view);
703 src = webkit_web_frame_get_data_source(frame);
704 request = webkit_web_data_source_get_request(src);
705 msg = webkit_network_request_get_message(request);
706 c->sslfailed = !(soup_message_get_flags(msg)
707 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
709 setatom(c, AtomUri, uri);
712 g_object_set(G_OBJECT(set), "user-stylesheet-uri",
713 getstyle(uri), NULL);
716 case WEBKIT_LOAD_FINISHED:
720 soup_cache_flush(diskcache);
721 soup_cache_dump(diskcache);
730 loaduri(Client *c, const Arg *arg) {
732 const char *uri = (char *)arg->v;
733 Arg a = { .b = FALSE };
736 if(strcmp(uri, "") == 0)
739 /* In case it's a file path. */
740 if(stat(uri, &st) == 0) {
741 rp = realpath(uri, NULL);
742 u = g_strdup_printf("file://%s", rp);
745 u = g_strrstr(uri, "://") ? g_strdup(uri)
746 : g_strdup_printf("http://%s", uri);
749 setatom(c, AtomUri, uri);
751 /* prevents endless loop */
752 if(strcmp(u, geturi(c)) == 0) {
755 webkit_web_view_load_uri(c->view, u);
757 c->title = copystr(&c->title, u);
764 navigate(Client *c, const Arg *arg) {
765 int steps = *(int *)arg;
766 webkit_web_view_go_back_or_forward(c->view, steps);
772 WebKitWebSettings *settings;
773 WebKitWebFrame *frame;
774 GdkGeometry hints = { 1, 1 };
779 if(!(c = calloc(1, sizeof(Client))))
780 die("Cannot malloc!\n");
787 c->win = gtk_plug_new(embed);
789 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
791 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
792 * is always correct, so we should still call this function.
793 * But when doing so, we *must* differentiate between a
794 * WM_CLASS and a resource on the window. By convention, the
795 * window class (WM_CLASS) is capped, while the resource is in
796 * lowercase. Both these values come as a pair.
798 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
800 /* TA: 20091214: And set the role here as well -- so that
801 * sessions can pick this up.
803 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
805 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
806 g_signal_connect(G_OBJECT(c->win),
808 G_CALLBACK(destroywin), c);
809 g_signal_connect(G_OBJECT(c->win),
810 "leave_notify_event",
811 G_CALLBACK(titlechangeleave), c);
817 c->pane = gtk_vpaned_new();
820 c->vbox = gtk_vbox_new(FALSE, 0);
821 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
824 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
826 g_signal_connect(G_OBJECT(c->view),
828 G_CALLBACK(titlechange), c);
829 g_signal_connect(G_OBJECT(c->view),
830 "hovering-over-link",
831 G_CALLBACK(linkhover), c);
832 g_signal_connect(G_OBJECT(c->view),
833 "geolocation-policy-decision-requested",
834 G_CALLBACK(geopolicyrequested), c);
835 g_signal_connect(G_OBJECT(c->view),
837 G_CALLBACK(createwindow), c);
838 g_signal_connect(G_OBJECT(c->view),
839 "new-window-policy-decision-requested",
840 G_CALLBACK(decidewindow), c);
841 g_signal_connect(G_OBJECT(c->view),
842 "mime-type-policy-decision-requested",
843 G_CALLBACK(decidedownload), c);
844 g_signal_connect(G_OBJECT(c->view),
845 "window-object-cleared",
846 G_CALLBACK(windowobjectcleared), c);
847 g_signal_connect(G_OBJECT(c->view),
848 "notify::load-status",
849 G_CALLBACK(loadstatuschange), c);
850 g_signal_connect(G_OBJECT(c->view),
852 G_CALLBACK(progresschange), c);
853 g_signal_connect(G_OBJECT(c->view),
854 "download-requested",
855 G_CALLBACK(initdownload), c);
856 g_signal_connect(G_OBJECT(c->view),
857 "button-release-event",
858 G_CALLBACK(buttonrelease), c);
859 g_signal_connect(G_OBJECT(c->view),
861 G_CALLBACK(contextmenu), c);
862 g_signal_connect(G_OBJECT(c->view),
863 "resource-request-starting",
864 G_CALLBACK(beforerequest), c);
865 g_signal_connect(G_OBJECT(c->view),
866 "should-show-delete-interface-for-element",
867 G_CALLBACK(deletion_interface), c);
869 /* Scrolled Window */
870 c->scroll = gtk_scrolled_window_new(NULL, NULL);
872 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
873 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
874 G_CALLBACK(gtk_true), NULL);
876 if(!enablescrollbars) {
877 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
878 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
880 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
881 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
885 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
886 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
887 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
890 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
891 TRUE, 0, GTK_PACK_START);
892 gtk_widget_grab_focus(GTK_WIDGET(c->view));
893 gtk_widget_show(c->pane);
894 gtk_widget_show(c->vbox);
895 gtk_widget_show(c->scroll);
896 gtk_widget_show(GTK_WIDGET(c->view));
897 gtk_widget_show(c->win);
898 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
900 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
901 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
902 webkit_web_view_set_full_content_zoom(c->view, TRUE);
906 settings = webkit_web_view_get_settings(c->view);
907 if(!(ua = getenv("SURF_USERAGENT")))
909 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
911 g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
912 getstyle("about:blank"), NULL);
914 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
916 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
918 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
920 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
921 enablespatialbrowsing, NULL);
922 g_object_set(G_OBJECT(settings), "enable-developer-extras",
923 enableinspector, NULL);
924 g_object_set(G_OBJECT(settings), "enable-default-context-menu",
925 kioskmode ^ 1, NULL);
926 g_object_set(G_OBJECT(settings), "default-font-size",
927 defaultfontsize, NULL);
928 g_object_set(G_OBJECT(settings), "resizable-text-areas",
932 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
933 * This ensures websites are not unusably small with a high DPI screen.
934 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
937 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
938 dpi = gdk_screen_get_resolution(screen);
940 g_object_set(G_OBJECT(settings), "enforce-96-dpi", true,
942 webkit_web_view_set_zoom_level(c->view, dpi/96);
945 /* This might conflict with _zoomto96dpi_. */
947 webkit_web_view_set_zoom_level(c->view, zoomlevel);
949 if(enableinspector) {
950 c->inspector = WEBKIT_WEB_INSPECTOR(
951 webkit_web_view_get_inspector(c->view));
952 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
953 G_CALLBACK(inspector_new), c);
954 g_signal_connect(G_OBJECT(c->inspector), "show-window",
955 G_CALLBACK(inspector_show), c);
956 g_signal_connect(G_OBJECT(c->inspector), "close-window",
957 G_CALLBACK(inspector_close), c);
958 g_signal_connect(G_OBJECT(c->inspector), "finished",
959 G_CALLBACK(inspector_finished), c);
960 c->isinspecting = false;
963 if(runinfullscreen) {
968 setatom(c, AtomFind, "");
969 setatom(c, AtomUri, "about:blank");
971 webkit_web_view_set_transparent(c->view, TRUE);
977 gdk_display_sync(gtk_widget_get_display(c->win));
979 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
981 if (fclose(stdout) != 0) {
982 die("Error closing stdout");
990 newwindow(Client *c, const Arg *arg, gboolean noembed) {
992 const char *cmd[18], *uri;
993 const Arg a = { .v = (void *)cmd };
998 cmd[i++] = cookiepolicies;
999 if(!enablescrollbars)
1001 if(embed && !noembed) {
1003 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1006 if(!allowgeolocation)
1021 cmd[i++] = cookiefile;
1023 uri = arg->v ? (char *)arg->v : c->linkhover;
1031 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1032 gboolean keyboard, Client *c) {
1033 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1035 for(GList *l = items; l; l = l->next) {
1036 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1044 menuactivate(GtkMenuItem *item, Client *c) {
1046 * context-menu-action-2000 open link
1047 * context-menu-action-1 open link in window
1048 * context-menu-action-2 download linked file
1049 * context-menu-action-3 copy link location
1050 * context-menu-action-7 copy image address
1051 * context-menu-action-13 reload
1052 * context-menu-action-10 back
1053 * context-menu-action-11 forward
1054 * context-menu-action-12 stop
1057 GtkAction *a = NULL;
1058 const char *name, *uri;
1059 GtkClipboard *prisel, *clpbrd;
1061 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
1065 name = gtk_action_get_name(a);
1066 if(!g_strcmp0(name, "context-menu-action-3")) {
1067 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1068 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1069 } else if(!g_strcmp0(name, "context-menu-action-7")) {
1070 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1071 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1072 uri = gtk_clipboard_wait_for_text(clpbrd);
1074 gtk_clipboard_set_text(prisel, uri, -1);
1079 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
1080 Arg arg = {.v = text };
1082 loaduri((Client *) d, &arg);
1086 print(Client *c, const Arg *arg) {
1087 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1090 static GdkFilterReturn
1091 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
1092 Client *c = (Client *)d;
1096 if(((XEvent *)e)->type == PropertyNotify) {
1097 ev = &((XEvent *)e)->xproperty;
1098 if(ev->state == PropertyNewValue) {
1099 if(ev->atom == atoms[AtomFind]) {
1103 return GDK_FILTER_REMOVE;
1104 } else if(ev->atom == atoms[AtomGo]) {
1105 arg.v = getatom(c, AtomGo);
1108 return GDK_FILTER_REMOVE;
1112 return GDK_FILTER_CONTINUE;
1116 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
1117 c->progress = webkit_web_view_get_progress(c->view) * 100;
1122 reload(Client *c, const Arg *arg) {
1123 gboolean nocache = *(gboolean *)arg;
1125 webkit_web_view_reload_bypass_cache(c->view);
1127 webkit_web_view_reload(c->view);
1132 scroll_h(Client *c, const Arg *arg) {
1133 scroll(gtk_scrolled_window_get_hadjustment(
1134 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1138 scroll_v(Client *c, const Arg *arg) {
1139 scroll(gtk_scrolled_window_get_vadjustment(
1140 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1144 scroll(GtkAdjustment *a, const Arg *arg) {
1147 v = gtk_adjustment_get_value(a);
1151 v += gtk_adjustment_get_page_increment(a) *
1157 v += gtk_adjustment_get_step_increment(a) * arg->i;
1161 v = MIN(v, gtk_adjustment_get_upper(a) -
1162 gtk_adjustment_get_page_size(a));
1163 gtk_adjustment_set_value(a, v);
1167 setatom(Client *c, int a, const char *v) {
1169 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1170 atoms[a], XA_STRING, 8, PropModeReplace,
1171 (unsigned char *)v, strlen(v) + 1);
1181 GError *error = NULL;
1183 /* clean up any zombies immediately */
1185 gtk_init(NULL, NULL);
1187 dpy = GDK_DISPLAY();
1190 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1191 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1192 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1194 /* dirs and files */
1195 cookiefile = buildpath(cookiefile);
1196 scriptfile = buildpath(scriptfile);
1197 cachefolder = buildpath(cachefolder);
1198 styledir = buildpath(styledir);
1199 if(stylefile == NULL) {
1200 for(i = 0; i < LENGTH(styles); i++) {
1201 if(regcomp(&(styles[i].re), styles[i].regex,
1204 "Could not compile regex: %s\n",
1206 styles[i].regex = NULL;
1208 styles[i].style = buildpath(
1209 g_strconcat(styledir,
1210 styles[i].style, NULL));
1213 stylefile = buildpath(stylefile);
1216 /* request handler */
1217 s = webkit_get_default_session();
1220 soup_session_add_feature(s,
1221 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE,
1222 cookiepolicy_get())));
1225 if(enablediskcache) {
1226 diskcache = soup_cache_new(cachefolder, SOUP_CACHE_SINGLE_USER);
1227 soup_cache_set_max_size(diskcache, diskcachebytes);
1228 soup_cache_load(diskcache);
1229 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1233 tlsdb = g_tls_file_database_new(cafile, &error);
1236 g_warning("Error loading SSL database %s: %s", cafile, error->message);
1237 g_error_free(error);
1239 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1240 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1243 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1244 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1245 g_strdup_printf("http://%s", proxy);
1246 puri = soup_uri_new(new_proxy);
1247 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1248 soup_uri_free(puri);
1255 sigchld(int unused) {
1256 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1257 die("Can't install SIGCHLD handler");
1258 while(0 < waitpid(-1, NULL, WNOHANG));
1262 source(Client *c, const Arg *arg) {
1263 Arg a = { .b = FALSE };
1266 s = webkit_web_view_get_view_source_mode(c->view);
1267 webkit_web_view_set_view_source_mode(c->view, !s);
1272 spawn(Client *c, const Arg *arg) {
1275 close(ConnectionNumber(dpy));
1277 execvp(((char **)arg->v)[0], (char **)arg->v);
1278 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1285 eval(Client *c, const Arg *arg) {
1286 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1287 evalscript(webkit_web_frame_get_global_context(frame),
1288 ((char **)arg->v)[0], "");
1292 stop(Client *c, const Arg *arg) {
1293 webkit_web_view_stop_loading(c->view);
1297 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
1298 const gchar *t = webkit_web_view_get_title(view);
1300 c->title = copystr(&c->title, t);
1306 titlechangeleave(void *a, void *b, Client *c) {
1307 c->linkhover = NULL;
1312 toggle(Client *c, const Arg *arg) {
1313 WebKitWebSettings *settings;
1314 char *name = (char *)arg->v;
1316 Arg a = { .b = FALSE };
1318 settings = webkit_web_view_get_settings(c->view);
1319 g_object_get(G_OBJECT(settings), name, &value, NULL);
1320 g_object_set(G_OBJECT(settings), name, !value, NULL);
1326 togglecookiepolicy(Client *c, const Arg *arg) {
1328 SoupCookieJarAcceptPolicy policy;
1330 jar = SOUP_COOKIE_JAR(
1331 soup_session_get_feature(
1332 webkit_get_default_session(),
1333 SOUP_TYPE_COOKIE_JAR));
1334 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1337 if(policysel >= strlen(cookiepolicies))
1340 g_object_set(G_OBJECT(jar), "accept-policy",
1341 cookiepolicy_get(), NULL);
1344 /* Do not reload. */
1348 togglegeolocation(Client *c, const Arg *arg) {
1349 Arg a = { .b = FALSE };
1351 allowgeolocation ^= 1;
1357 twitch(Client *c, const Arg *arg) {
1361 a = gtk_scrolled_window_get_vadjustment(
1362 GTK_SCROLLED_WINDOW(c->scroll));
1364 v = gtk_adjustment_get_value(a);
1369 v = MIN(v, gtk_adjustment_get_upper(a) -
1370 gtk_adjustment_get_page_size(a));
1371 gtk_adjustment_set_value(a, v);
1375 togglescrollbars(Client *c, const Arg *arg) {
1376 GtkPolicyType vspolicy;
1379 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1381 if(vspolicy == GTK_POLICY_AUTOMATIC) {
1382 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1383 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1385 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1386 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1395 togglestyle(Client *c, const Arg *arg) {
1396 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1399 enablestyles = !enablestyles;
1400 uri = enablestyles ? getstyle(geturi(c)) : g_strdup("");
1401 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1407 gettogglestat(Client *c){
1410 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1412 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1414 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1416 togglestat[p++] = value? 'C': 'c';
1418 togglestat[p++] = allowgeolocation? 'G': 'g';
1420 togglestat[p++] = enablediskcache? 'D': 'd';
1422 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1423 togglestat[p++] = value? 'I': 'i';
1425 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1426 togglestat[p++] = value? 'S': 's';
1428 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1429 togglestat[p++] = value? 'V': 'v';
1431 togglestat[p++] = enablestyles ? 'M': 'm';
1433 togglestat[p] = '\0';
1437 getpagestat(Client *c) {
1438 const char *uri = geturi(c);
1440 if(strstr(uri, "https://") == uri) {
1441 pagestat[0] = c->sslfailed ? 'U' : 'T';
1446 pagestat[1] = usingproxy ? 'P' : '-';
1452 updatetitle(Client *c) {
1455 if(showindicators) {
1460 t = g_strdup_printf("%s:%s | %s", togglestat,
1461 pagestat, c->linkhover);
1462 } else if(c->progress != 100) {
1463 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1464 togglestat, pagestat,
1465 (c->title == NULL)? "" : c->title);
1467 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1468 (c->title == NULL)? "" : c->title);
1471 gtk_window_set_title(GTK_WINDOW(c->win), t);
1474 gtk_window_set_title(GTK_WINDOW(c->win),
1475 (c->title == NULL)? "" : c->title);
1480 updatewinid(Client *c) {
1481 snprintf(winid, LENGTH(winid), "%u",
1482 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1487 die("usage: %s [-bBfFgGiIkKnNpPsSvx]"
1488 " [-a cookiepolicies ] "
1489 " [-c cookiefile] [-e xid] [-r scriptfile]"
1490 " [-t stylefile] [-u useragent] [-z zoomlevel]"
1491 " [uri]\n", basename(argv0));
1495 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1496 JSObjectRef win, Client *c) {
1501 zoom(Client *c, const Arg *arg) {
1505 webkit_web_view_zoom_out(c->view);
1506 } else if(arg->i > 0) {
1508 webkit_web_view_zoom_in(c->view);
1512 webkit_web_view_set_zoom_level(c->view, 1.0);
1517 main(int argc, char *argv[]) {
1521 memset(&arg, 0, sizeof(arg));
1523 /* command line args */
1526 cookiepolicies = EARGF(usage());
1529 enablescrollbars = 0;
1532 enablescrollbars = 1;
1535 cookiefile = EARGF(usage());
1538 enablediskcache = 0;
1541 enablediskcache = 1;
1544 embed = strtol(EARGF(usage()), NULL, 0);
1547 runinfullscreen = 1;
1550 runinfullscreen = 0;
1553 allowgeolocation = 0;
1556 allowgeolocation = 1;
1577 enableinspector = 0;
1580 enableinspector = 1;
1589 scriptfile = EARGF(usage());
1598 stylefile = EARGF(usage());
1601 useragent = EARGF(usage());
1604 die("surf-"VERSION", ©2009-2014 surf engineers, "
1605 "see LICENSE for details\n");
1610 zoomlevel = strtof(EARGF(usage()), NULL);
1621 loaduri(clients, &arg);
1629 return EXIT_SUCCESS;