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>
34 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
35 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
36 #define COOKIEJAR_TYPE (cookiejar_get_type ())
37 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
39 enum { AtomFind, AtomGo, AtomUri, AtomLast };
41 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
42 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
43 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
44 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
45 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
46 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
47 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
50 typedef union Arg Arg;
57 typedef struct Client {
58 GtkWidget *win, *scroll, *vbox, *pane;
61 WebKitWebInspector *inspector;
62 char *title, *linkhover;
66 gboolean zoomed, fullscreen, isinspecting, sslfailed;
72 void (*func)(Client *c, const Arg *arg);
80 void (*func)(Client *c, const Arg *arg);
85 SoupCookieJarText parent_instance;
90 SoupCookieJarTextClass parent_class;
93 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
102 static Atom atoms[AtomLast];
103 static Client *clients = NULL;
104 static Window embed = 0;
105 static gboolean showxid = FALSE;
106 static char winid[64];
107 static gboolean usingproxy = 0;
108 static char togglestat[9];
109 static char pagestat[3];
110 static GTlsDatabase *tlsdb;
111 static int policysel = 0;
112 static char *stylefile = NULL;
113 static SoupCache *diskcache = NULL;
115 static void addaccelgroup(Client *c);
116 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
117 WebKitWebResource *r, WebKitNetworkRequest *req,
118 WebKitNetworkResponse *resp, Client *c);
119 static char *buildfile(const char *path);
120 static char *buildpath(const char *path);
121 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
122 static void cleanup(void);
123 static void clipboard(Client *c, const Arg *arg);
125 /* Cookiejar implementation */
126 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
127 SoupCookie *new_cookie);
128 static void cookiejar_finalize(GObject *self);
129 static SoupCookieJarAcceptPolicy cookiepolicy_get(void);
130 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only,
131 SoupCookieJarAcceptPolicy policy);
132 static void cookiejar_set_property(GObject *self, guint prop_id,
133 const GValue *value, GParamSpec *pspec);
134 static char cookiepolicy_set(const SoupCookieJarAcceptPolicy p);
136 static char *copystr(char **str, const char *src);
137 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
139 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
140 WebKitNetworkRequest *r, gchar *m,
141 WebKitWebPolicyDecision *p, Client *c);
142 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
143 WebKitNetworkRequest *r, WebKitWebNavigationAction
144 *n, WebKitWebPolicyDecision *p, Client *c);
145 static gboolean deletion_interface(WebKitWebView *view,
146 WebKitDOMHTMLElement *arg1, Client *c);
147 static void destroyclient(Client *c);
148 static void destroywin(GtkWidget* w, Client *c);
149 static void die(const char *errstr, ...);
150 static void eval(Client *c, const Arg *arg);
151 static void find(Client *c, const Arg *arg);
152 static void fullscreen(Client *c, const Arg *arg);
153 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
154 WebKitGeolocationPolicyDecision *d, Client *c);
155 static const char *getatom(Client *c, int a);
156 static void gettogglestat(Client *c);
157 static void getpagestat(Client *c);
158 static char *geturi(Client *c);
159 static const gchar *getstyle(const char *uri);
160 static void setstyle(Client *c, const char *style);
162 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
164 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
166 static void inspector(Client *c, const Arg *arg);
167 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
169 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
170 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
171 static void inspector_finished(WebKitWebInspector *i, Client *c);
173 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
174 GdkModifierType mods, Client *c);
175 static void linkhover(WebKitWebView *v, const char* t, const char* l,
177 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
179 static void loaduri(Client *c, const Arg *arg);
180 static void navigate(Client *c, const Arg *arg);
181 static Client *newclient(void);
182 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
183 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
184 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
185 WebKitHitTestResult *target, gboolean keyboard,
187 static void menuactivate(GtkMenuItem *item, Client *c);
188 static void print(Client *c, const Arg *arg);
189 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
191 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
192 static void linkopen(Client *c, const Arg *arg);
193 static void linkopenembed(Client *c, const Arg *arg);
194 static void reload(Client *c, const Arg *arg);
195 static void scroll_h(Client *c, const Arg *arg);
196 static void scroll_v(Client *c, const Arg *arg);
197 static void scroll(GtkAdjustment *a, const Arg *arg);
198 static void setatom(Client *c, int a, const char *v);
199 static void setup(void);
200 static void sigchld(int unused);
201 static void source(Client *c, const Arg *arg);
202 static void spawn(Client *c, const Arg *arg);
203 static void stop(Client *c, const Arg *arg);
204 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
205 static void titlechangeleave(void *a, void *b, Client *c);
206 static void toggle(Client *c, const Arg *arg);
207 static void togglecookiepolicy(Client *c, const Arg *arg);
208 static void togglegeolocation(Client *c, const Arg *arg);
209 static void togglescrollbars(Client *c, const Arg *arg);
210 static void togglestyle(Client *c, const Arg *arg);
211 static void updatetitle(Client *c);
212 static void updatewinid(Client *c);
213 static void usage(void);
214 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
215 JSContextRef js, JSObjectRef win, Client *c);
216 static void zoom(Client *c, const Arg *arg);
218 /* configuration, allows nested code to access above variables */
222 addaccelgroup(Client *c)
225 GtkAccelGroup *group = gtk_accel_group_new();
228 for (i = 0; i < LENGTH(keys); i++) {
229 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
230 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
233 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
237 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
238 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
241 const gchar *uri = webkit_network_request_get_uri(req);
244 if (g_str_has_suffix(uri, "/favicon.ico"))
245 webkit_network_request_set_uri(req, "about:blank");
247 if (!g_str_has_prefix(uri, "http://")
248 && !g_str_has_prefix(uri, "https://")
249 && !g_str_has_prefix(uri, "about:")
250 && !g_str_has_prefix(uri, "file://")
251 && !g_str_has_prefix(uri, "data:")
252 && !g_str_has_prefix(uri, "blob:")
253 && strlen(uri) > 0) {
254 for (i = 0; i < strlen(uri); i++) {
255 if (!g_ascii_isprint(uri[i])) {
261 handleplumb(c, w, uri);
266 buildfile(const char *path)
268 char *dname, *bname, *bpath, *fpath;
271 dname = g_path_get_dirname(path);
272 bname = g_path_get_basename(path);
274 bpath = buildpath(dname);
277 fpath = g_build_filename(bpath, bname, NULL);
281 if (!(f = fopen(fpath, "a")))
282 die("Could not open file: %s\n", fpath);
284 g_chmod(fpath, 0600); /* always */
291 buildpath(const char *path)
294 char *apath, *name, *p, *fpath;
296 if (path[0] == '~') {
297 if (path[1] == '/' || path[1] == '\0') {
298 p = (char *)&path[1];
299 pw = getpwuid(getuid());
301 if ((p = strchr(path, '/')))
302 name = g_strndup(&path[1], --p - path);
304 name = g_strdup(&path[1]);
306 if (!(pw = getpwnam(name))) {
307 die("Can't get user %s home directory: %s.\n",
312 apath = g_build_filename(pw->pw_dir, p, NULL);
314 apath = g_strdup(path);
317 /* creating directory */
318 if (g_mkdir_with_parents(apath, 0700) < 0)
319 die("Could not access directory: %s\n", apath);
321 fpath = realpath(apath, NULL);
328 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
330 WebKitHitTestResultContext context;
331 WebKitHitTestResult *result;
335 result = webkit_web_view_get_hit_test_result(web, e);
336 g_object_get(result, "context", &context, NULL);
337 g_object_get(result, "link-uri", &arg.v, NULL);
338 for (i = 0; i < LENGTH(buttons); i++) {
339 if (context & buttons[i].click
340 && e->button == buttons[i].button
341 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
342 && buttons[i].func) {
343 buttons[i].func(c, buttons[i].click == ClkLink
344 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
355 soup_cache_flush(diskcache);
356 soup_cache_dump(diskcache);
359 destroyclient(clients);
366 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
367 SoupCookie *new_cookie)
369 flock(COOKIEJAR(self)->lock, LOCK_EX);
370 if (new_cookie && !new_cookie->expires && sessiontime) {
371 soup_cookie_set_expires(new_cookie,
372 soup_date_new_from_now(sessiontime));
374 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
377 flock(COOKIEJAR(self)->lock, LOCK_UN);
381 cookiejar_class_init(CookieJarClass *klass)
383 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
384 G_OBJECT_CLASS(klass)->get_property =
385 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
386 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
387 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
388 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
392 cookiejar_finalize(GObject *self)
394 close(COOKIEJAR(self)->lock);
395 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
399 cookiejar_init(CookieJar *self)
401 self->lock = open(cookiefile, 0);
405 cookiejar_new(const char *filename, gboolean read_only,
406 SoupCookieJarAcceptPolicy policy)
408 return g_object_new(COOKIEJAR_TYPE,
409 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
410 SOUP_COOKIE_JAR_READ_ONLY, read_only,
411 SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
415 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
418 flock(COOKIEJAR(self)->lock, LOCK_SH);
419 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
421 flock(COOKIEJAR(self)->lock, LOCK_UN);
424 SoupCookieJarAcceptPolicy
425 cookiepolicy_get(void)
427 switch (cookiepolicies[policysel]) {
429 return SOUP_COOKIE_JAR_ACCEPT_NEVER;
431 return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
437 return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
441 cookiepolicy_set(const SoupCookieJarAcceptPolicy ep)
444 case SOUP_COOKIE_JAR_ACCEPT_NEVER:
446 case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
448 case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
457 evalscript(JSContextRef js, char *script, char* scriptname)
459 JSStringRef jsscript, jsscriptname;
460 JSValueRef exception = NULL;
462 jsscript = JSStringCreateWithUTF8CString(script);
463 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
464 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
465 jsscriptname, 0, &exception);
466 JSStringRelease(jsscript);
467 JSStringRelease(jsscriptname);
471 runscript(WebKitWebFrame *frame)
476 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
477 evalscript(webkit_web_frame_get_global_context(frame), script,
483 clipboard(Client *c, const Arg *arg)
485 gboolean paste = *(gboolean *)arg;
488 gtk_clipboard_request_text(gtk_clipboard_get(
489 GDK_SELECTION_PRIMARY),
492 gtk_clipboard_set_text(gtk_clipboard_get(
493 GDK_SELECTION_PRIMARY), c->linkhover
494 ? c->linkhover : geturi(c), -1);
499 copystr(char **str, const char *src)
512 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
514 Client *n = newclient();
519 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
520 gchar *m, WebKitWebPolicyDecision *p, Client *c)
522 if (!webkit_web_view_can_show_mime_type(v, m)) {
523 webkit_web_policy_decision_download(p);
530 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
531 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
536 if (webkit_web_navigation_action_get_reason(n)
537 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
538 webkit_web_policy_decision_ignore(p);
539 arg.v = (void *)webkit_network_request_get_uri(r);
540 newwindow(NULL, &arg, 0);
547 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
553 destroyclient(Client *c)
557 webkit_web_view_stop_loading(c->view);
558 gtk_widget_destroy(GTK_WIDGET(c->view));
559 gtk_widget_destroy(c->scroll);
560 gtk_widget_destroy(c->vbox);
561 gtk_widget_destroy(c->win);
563 for (p = clients; p && p->next != c; p = p->next)
575 destroywin(GtkWidget* w, Client *c)
581 die(const char *errstr, ...)
585 va_start(ap, errstr);
586 vfprintf(stderr, errstr, ap);
592 find(Client *c, const Arg *arg)
596 s = getatom(c, AtomFind);
597 gboolean forward = *(gboolean *)arg;
598 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
602 fullscreen(Client *c, const Arg *arg)
605 gtk_window_unfullscreen(GTK_WINDOW(c->win));
607 gtk_window_fullscreen(GTK_WINDOW(c->win));
608 c->fullscreen = !c->fullscreen;
612 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
613 WebKitGeolocationPolicyDecision *d, Client *c)
615 if (allowgeolocation)
616 webkit_geolocation_policy_allow(d);
618 webkit_geolocation_policy_deny(d);
622 getatom(Client *c, int a)
624 static char buf[BUFSIZ];
627 unsigned long ldummy;
628 unsigned char *p = NULL;
630 XGetWindowProperty(dpy, c->xid,
631 atoms[a], 0L, BUFSIZ, False, XA_STRING,
632 &adummy, &idummy, &ldummy, &ldummy, &p);
634 strncpy(buf, (char *)p, LENGTH(buf)-1);
647 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
653 getstyle(const char *uri)
657 if (stylefile != NULL)
660 for (i = 0; i < LENGTH(styles); i++) {
661 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
663 return styles[i].style;
670 setstyle(Client *c, const char *style)
672 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
674 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
678 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
682 webkit_web_view_stop_loading(w);
683 arg = (Arg)PLUMB((char *)uri);
688 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
693 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
699 inspector(Client *c, const Arg *arg)
701 if (enableinspector) {
703 webkit_web_inspector_close(c->inspector);
705 webkit_web_inspector_show(c->inspector);
710 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
712 return WEBKIT_WEB_VIEW(webkit_web_view_new());
716 inspector_show(WebKitWebInspector *i, Client *c)
723 w = webkit_web_inspector_get_web_view(i);
724 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
725 gtk_widget_show(GTK_WIDGET(w));
726 c->isinspecting = true;
732 inspector_close(WebKitWebInspector *i, Client *c)
736 if (!c->isinspecting)
739 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
741 gtk_widget_destroy(w);
742 c->isinspecting = false;
748 inspector_finished(WebKitWebInspector *i, Client *c)
750 g_free(c->inspector);
754 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
758 gboolean processed = FALSE;
760 mods = CLEANMASK(mods);
761 key = gdk_keyval_to_lower(key);
763 for (i = 0; i < LENGTH(keys); i++) {
764 if (key == keys[i].keyval
765 && mods == keys[i].mod
767 keys[i].func(c, &(keys[i].arg));
776 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
779 c->linkhover = copystr(&c->linkhover, l);
780 } else if (c->linkhover) {
788 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
790 WebKitWebFrame *frame;
791 WebKitWebDataSource *src;
792 WebKitNetworkRequest *request;
796 switch (webkit_web_view_get_load_status (c->view)) {
797 case WEBKIT_LOAD_COMMITTED:
799 if (strstr(uri, "https://") == uri) {
800 frame = webkit_web_view_get_main_frame(c->view);
801 src = webkit_web_frame_get_data_source(frame);
802 request = webkit_web_data_source_get_request(src);
803 msg = webkit_network_request_get_message(request);
804 c->sslfailed = !(soup_message_get_flags(msg)
805 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
807 setatom(c, AtomUri, uri);
810 setstyle(c, getstyle(uri));
812 case WEBKIT_LOAD_FINISHED:
816 soup_cache_flush(diskcache);
817 soup_cache_dump(diskcache);
826 loaduri(Client *c, const Arg *arg)
829 const char *uri = (char *)arg->v;
830 Arg a = { .b = FALSE };
833 if (strcmp(uri, "") == 0)
836 /* In case it's a file path. */
837 if (stat(uri, &st) == 0) {
838 rp = realpath(uri, NULL);
839 u = g_strdup_printf("file://%s", rp);
842 u = g_strrstr(uri, "://") ? g_strdup(uri)
843 : g_strdup_printf("http://%s", uri);
846 setatom(c, AtomUri, uri);
848 /* prevents endless loop */
849 if (strcmp(u, geturi(c)) == 0) {
852 webkit_web_view_load_uri(c->view, u);
854 c->title = copystr(&c->title, u);
861 navigate(Client *c, const Arg *arg)
863 int steps = *(int *)arg;
864 webkit_web_view_go_back_or_forward(c->view, steps);
871 WebKitWebSettings *settings;
872 WebKitWebFrame *frame;
873 GdkGeometry hints = { 1, 1 };
879 if (!(c = calloc(1, sizeof(Client))))
880 die("Cannot malloc!\n");
887 c->win = gtk_plug_new(embed);
889 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
891 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
892 * is always correct, so we should still call this function.
893 * But when doing so, we *must* differentiate between a
894 * WM_CLASS and a resource on the window. By convention, the
895 * window class (WM_CLASS) is capped, while the resource is in
896 * lowercase. Both these values come as a pair.
898 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
900 /* TA: 20091214: And set the role here as well -- so that
901 * sessions can pick this up.
903 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
905 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
906 g_signal_connect(G_OBJECT(c->win),
908 G_CALLBACK(destroywin), c);
909 g_signal_connect(G_OBJECT(c->win),
910 "leave_notify_event",
911 G_CALLBACK(titlechangeleave), c);
917 c->pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
920 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
921 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
924 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
926 g_signal_connect(G_OBJECT(c->view),
928 G_CALLBACK(titlechange), c);
929 g_signal_connect(G_OBJECT(c->view),
930 "hovering-over-link",
931 G_CALLBACK(linkhover), c);
932 g_signal_connect(G_OBJECT(c->view),
933 "geolocation-policy-decision-requested",
934 G_CALLBACK(geopolicyrequested), c);
935 g_signal_connect(G_OBJECT(c->view),
937 G_CALLBACK(createwindow), c);
938 g_signal_connect(G_OBJECT(c->view),
939 "new-window-policy-decision-requested",
940 G_CALLBACK(decidewindow), c);
941 g_signal_connect(G_OBJECT(c->view),
942 "mime-type-policy-decision-requested",
943 G_CALLBACK(decidedownload), c);
944 g_signal_connect(G_OBJECT(c->view),
945 "window-object-cleared",
946 G_CALLBACK(windowobjectcleared), c);
947 g_signal_connect(G_OBJECT(c->view),
948 "notify::load-status",
949 G_CALLBACK(loadstatuschange), c);
950 g_signal_connect(G_OBJECT(c->view),
952 G_CALLBACK(progresschange), c);
953 g_signal_connect(G_OBJECT(c->view),
954 "download-requested",
955 G_CALLBACK(initdownload), c);
956 g_signal_connect(G_OBJECT(c->view),
957 "button-release-event",
958 G_CALLBACK(buttonrelease), c);
959 g_signal_connect(G_OBJECT(c->view),
961 G_CALLBACK(contextmenu), c);
962 g_signal_connect(G_OBJECT(c->view),
963 "resource-request-starting",
964 G_CALLBACK(beforerequest), c);
965 g_signal_connect(G_OBJECT(c->view),
966 "should-show-delete-interface-for-element",
967 G_CALLBACK(deletion_interface), c);
969 /* Scrolled Window */
970 c->scroll = gtk_scrolled_window_new(NULL, NULL);
972 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
973 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
974 G_CALLBACK(gtk_true), NULL);
976 if (!enablescrollbars) {
977 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
981 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
982 GTK_POLICY_AUTOMATIC,
983 GTK_POLICY_AUTOMATIC);
987 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
988 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
989 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
992 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
994 gtk_widget_grab_focus(GTK_WIDGET(c->view));
995 gtk_widget_show(c->pane);
996 gtk_widget_show(c->vbox);
997 gtk_widget_show(c->scroll);
998 gtk_widget_show(GTK_WIDGET(c->view));
999 gtk_widget_show(c->win);
1000 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1001 c->xid = gdk_x11_window_get_xid(gwin);
1002 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1004 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1005 gdk_window_add_filter(gwin, processx, c);
1006 webkit_web_view_set_full_content_zoom(c->view, TRUE);
1010 settings = webkit_web_view_get_settings(c->view);
1011 if (!(ua = getenv("SURF_USERAGENT")))
1013 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
1014 g_object_set(G_OBJECT(settings),
1015 "auto-load-images", loadimages, NULL);
1016 g_object_set(G_OBJECT(settings),
1017 "enable-plugins", enableplugins, NULL);
1018 g_object_set(G_OBJECT(settings),
1019 "enable-scripts", enablescripts, NULL);
1020 g_object_set(G_OBJECT(settings),
1021 "enable-spatial-navigation", enablespatialbrowsing, NULL);
1022 g_object_set(G_OBJECT(settings),
1023 "enable-developer-extras", enableinspector, NULL);
1024 g_object_set(G_OBJECT(settings),
1025 "enable-default-context-menu", kioskmode ^ 1, NULL);
1026 g_object_set(G_OBJECT(settings),
1027 "default-font-size", defaultfontsize, NULL);
1028 g_object_set(G_OBJECT(settings),
1029 "resizable-text-areas", 1, NULL);
1031 setstyle(c, getstyle("about:blank"));
1034 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
1035 * This ensures websites are not unusably small with a high DPI screen.
1036 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
1039 screen = gdk_window_get_screen(gwin);
1040 dpi = gdk_screen_get_resolution(screen);
1042 g_object_set(G_OBJECT(settings),
1043 "enforce-96-dpi", true, NULL);
1044 webkit_web_view_set_zoom_level(c->view, dpi/96);
1047 /* This might conflict with _zoomto96dpi_. */
1048 if (zoomlevel != 1.0)
1049 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1051 if (enableinspector) {
1052 c->inspector = webkit_web_view_get_inspector(c->view);
1053 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
1054 G_CALLBACK(inspector_new), c);
1055 g_signal_connect(G_OBJECT(c->inspector), "show-window",
1056 G_CALLBACK(inspector_show), c);
1057 g_signal_connect(G_OBJECT(c->inspector), "close-window",
1058 G_CALLBACK(inspector_close), c);
1059 g_signal_connect(G_OBJECT(c->inspector), "finished",
1060 G_CALLBACK(inspector_finished), c);
1061 c->isinspecting = false;
1064 if (runinfullscreen)
1065 fullscreen(c, NULL);
1067 setatom(c, AtomFind, "");
1068 setatom(c, AtomUri, "about:blank");
1070 webkit_web_view_set_transparent(c->view, TRUE);
1076 gdk_display_sync(gtk_widget_get_display(c->win));
1077 printf("%lu\n", c->xid);
1079 if (fclose(stdout) != 0) {
1080 die("Error closing stdout");
1088 newwindow(Client *c, const Arg *arg, gboolean noembed)
1091 const char *cmd[18], *uri;
1092 const Arg a = { .v = (void *)cmd };
1097 cmd[i++] = cookiepolicies;
1098 if (!enablescrollbars)
1100 if (embed && !noembed) {
1102 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1105 if (!allowgeolocation)
1117 if (enablediskcache)
1120 cmd[i++] = cookiefile;
1122 uri = arg->v ? (char *)arg->v : c->linkhover;
1130 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1131 gboolean keyboard, Client *c)
1133 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1135 for (GList *l = items; l; l = l->next)
1136 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1143 menuactivate(GtkMenuItem *item, Client *c)
1146 * context-menu-action-2000 open link
1147 * context-menu-action-1 open link in window
1148 * context-menu-action-2 download linked file
1149 * context-menu-action-3 copy link location
1150 * context-menu-action-7 copy image address
1151 * context-menu-action-13 reload
1152 * context-menu-action-10 back
1153 * context-menu-action-11 forward
1154 * context-menu-action-12 stop
1157 const gchar *name, *uri;
1158 GtkClipboard *prisel, *clpbrd;
1160 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1164 if (!g_strcmp0(name, "context-menu-action-3")) {
1165 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1166 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1167 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1168 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1169 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1170 uri = gtk_clipboard_wait_for_text(clpbrd);
1172 gtk_clipboard_set_text(prisel, uri, -1);
1177 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1179 Arg arg = {.v = text };
1181 loaduri((Client *) d, &arg);
1185 print(Client *c, const Arg *arg)
1187 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1191 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1193 Client *c = (Client *)d;
1197 if (((XEvent *)e)->type == PropertyNotify) {
1198 ev = &((XEvent *)e)->xproperty;
1199 if (ev->state == PropertyNewValue) {
1200 if (ev->atom == atoms[AtomFind]) {
1204 return GDK_FILTER_REMOVE;
1205 } else if (ev->atom == atoms[AtomGo]) {
1206 arg.v = getatom(c, AtomGo);
1209 return GDK_FILTER_REMOVE;
1213 return GDK_FILTER_CONTINUE;
1217 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1219 c->progress = webkit_web_view_get_progress(c->view) * 100;
1224 linkopen(Client *c, const Arg *arg)
1226 newwindow(NULL, arg, 1);
1230 linkopenembed(Client *c, const Arg *arg)
1232 newwindow(NULL, arg, 0);
1236 reload(Client *c, const Arg *arg)
1238 gboolean nocache = *(gboolean *)arg;
1240 webkit_web_view_reload_bypass_cache(c->view);
1242 webkit_web_view_reload(c->view);
1246 scroll_h(Client *c, const Arg *arg)
1248 scroll(gtk_scrolled_window_get_hadjustment(
1249 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1253 scroll_v(Client *c, const Arg *arg)
1255 scroll(gtk_scrolled_window_get_vadjustment(
1256 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1260 scroll(GtkAdjustment *a, const Arg *arg)
1264 v = gtk_adjustment_get_value(a);
1268 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1273 v += gtk_adjustment_get_step_increment(a) * arg->i;
1277 v = MIN(v, gtk_adjustment_get_upper(a) -
1278 gtk_adjustment_get_page_size(a));
1279 gtk_adjustment_set_value(a, v);
1283 setatom(Client *c, int a, const char *v)
1286 XChangeProperty(dpy, c->xid,
1287 atoms[a], XA_STRING, 8, PropModeReplace,
1288 (unsigned char *)v, strlen(v) + 1);
1295 char *proxy, *new_proxy;
1296 char *styledirfile, *stylepath;
1299 GError *error = NULL;
1301 /* clean up any zombies immediately */
1303 gtk_init(NULL, NULL);
1305 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1308 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1309 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1310 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1312 /* dirs and files */
1313 cookiefile = buildfile(cookiefile);
1314 scriptfile = buildfile(scriptfile);
1315 cachefolder = buildpath(cachefolder);
1316 if (stylefile == NULL) {
1317 styledir = buildpath(styledir);
1318 for (i = 0; i < LENGTH(styles); i++) {
1319 if (regcomp(&(styles[i].re), styles[i].regex,
1322 "Could not compile regex: %s\n",
1324 styles[i].regex = NULL;
1326 styledirfile = g_strconcat(styledir, "/",
1327 styles[i].style, NULL);
1328 stylepath = buildfile(styledirfile);
1329 styles[i].style = g_strconcat("file://", stylepath,
1331 g_free(styledirfile);
1336 stylepath = buildfile(stylefile);
1337 stylefile = g_strconcat("file://", stylepath, NULL);
1341 /* request handler */
1342 s = webkit_get_default_session();
1345 soup_session_add_feature(s,
1346 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1347 FALSE, cookiepolicy_get())));
1350 if (enablediskcache) {
1351 diskcache = soup_cache_new(cachefolder,
1352 SOUP_CACHE_SINGLE_USER);
1353 soup_cache_set_max_size(diskcache, diskcachebytes);
1354 soup_cache_load(diskcache);
1355 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1359 tlsdb = g_tls_file_database_new(cafile, &error);
1362 g_warning("Error loading SSL database %s: %s", cafile,
1364 g_error_free(error);
1366 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1367 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1370 if ((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1371 new_proxy = g_strrstr(proxy, "http://")
1372 || g_strrstr(proxy, "socks://")
1373 || g_strrstr(proxy, "socks4://")
1374 || g_strrstr(proxy, "socks5://")
1376 : g_strdup_printf("http://%s", proxy);
1377 puri = soup_uri_new(new_proxy);
1378 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1379 soup_uri_free(puri);
1388 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1389 die("Can't install SIGCHLD handler");
1390 while (0 < waitpid(-1, NULL, WNOHANG));
1394 source(Client *c, const Arg *arg)
1396 Arg a = { .b = FALSE };
1399 s = webkit_web_view_get_view_source_mode(c->view);
1400 webkit_web_view_set_view_source_mode(c->view, !s);
1405 spawn(Client *c, const Arg *arg)
1409 close(ConnectionNumber(dpy));
1411 execvp(((char **)arg->v)[0], (char **)arg->v);
1412 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1419 eval(Client *c, const Arg *arg)
1421 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1422 evalscript(webkit_web_frame_get_global_context(frame),
1423 ((char **)arg->v)[0], "");
1427 stop(Client *c, const Arg *arg)
1429 webkit_web_view_stop_loading(c->view);
1433 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1435 const gchar *t = webkit_web_view_get_title(view);
1437 c->title = copystr(&c->title, t);
1443 titlechangeleave(void *a, void *b, Client *c)
1445 c->linkhover = NULL;
1450 toggle(Client *c, const Arg *arg)
1452 WebKitWebSettings *settings;
1453 char *name = (char *)arg->v;
1455 Arg a = { .b = FALSE };
1457 settings = webkit_web_view_get_settings(c->view);
1458 g_object_get(G_OBJECT(settings), name, &value, NULL);
1459 g_object_set(G_OBJECT(settings), name, !value, NULL);
1465 togglecookiepolicy(Client *c, const Arg *arg)
1468 SoupCookieJarAcceptPolicy policy;
1470 jar = SOUP_COOKIE_JAR(soup_session_get_feature(
1471 webkit_get_default_session(),
1472 SOUP_TYPE_COOKIE_JAR));
1473 g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
1476 if (policysel >= strlen(cookiepolicies))
1479 g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);
1482 /* Do not reload. */
1486 togglegeolocation(Client *c, const Arg *arg)
1488 Arg a = { .b = FALSE };
1490 allowgeolocation ^= 1;
1495 twitch(Client *c, const Arg *arg)
1500 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1503 v = gtk_adjustment_get_value(a);
1508 v = MIN(v, gtk_adjustment_get_upper(a) -
1509 gtk_adjustment_get_page_size(a));
1510 gtk_adjustment_set_value(a, v);
1514 togglescrollbars(Client *c, const Arg *arg)
1516 GtkPolicyType vspolicy;
1519 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1522 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1523 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1527 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1528 GTK_POLICY_AUTOMATIC,
1529 GTK_POLICY_AUTOMATIC);
1538 togglestyle(Client *c, const Arg *arg)
1540 enablestyle = !enablestyle;
1541 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1547 gettogglestat(Client *c)
1551 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1553 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1555 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1557 togglestat[p++] = value? 'C': 'c';
1559 togglestat[p++] = allowgeolocation? 'G': 'g';
1561 togglestat[p++] = enablediskcache? 'D': 'd';
1563 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1564 togglestat[p++] = value? 'I': 'i';
1566 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1567 togglestat[p++] = value? 'S': 's';
1569 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1570 togglestat[p++] = value? 'V': 'v';
1572 togglestat[p++] = enablestyle ? 'M': 'm';
1574 togglestat[p] = '\0';
1578 getpagestat(Client *c)
1580 const char *uri = geturi(c);
1582 if (strstr(uri, "https://") == uri)
1583 pagestat[0] = c->sslfailed ? 'U' : 'T';
1587 pagestat[1] = usingproxy ? 'P' : '-';
1592 updatetitle(Client *c)
1596 if (showindicators) {
1601 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1603 } else if (c->progress != 100) {
1604 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1605 togglestat, pagestat,
1606 c->title == NULL ? "" : c->title);
1608 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1609 c->title == NULL ? "" : c->title);
1612 gtk_window_set_title(GTK_WINDOW(c->win), t);
1615 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1621 updatewinid(Client *c)
1623 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1629 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1630 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1631 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1635 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1636 JSObjectRef win, Client *c)
1642 zoom(Client *c, const Arg *arg)
1647 webkit_web_view_zoom_out(c->view);
1648 } else if (arg->i > 0) {
1650 webkit_web_view_zoom_in(c->view);
1654 webkit_web_view_set_zoom_level(c->view, 1.0);
1659 main(int argc, char *argv[])
1664 memset(&arg, 0, sizeof(arg));
1666 /* command line args */
1669 cookiepolicies = EARGF(usage());
1672 enablescrollbars = 0;
1675 enablescrollbars = 1;
1678 cookiefile = EARGF(usage());
1681 enablediskcache = 0;
1684 enablediskcache = 1;
1687 embed = strtol(EARGF(usage()), NULL, 0);
1690 runinfullscreen = 0;
1693 runinfullscreen = 1;
1696 allowgeolocation = 0;
1699 allowgeolocation = 1;
1720 enableinspector = 0;
1723 enableinspector = 1;
1732 scriptfile = EARGF(usage());
1741 stylefile = EARGF(usage());
1744 useragent = EARGF(usage());
1747 die("surf-"VERSION", ©2009-2015 surf engineers, "
1748 "see LICENSE for details\n");
1753 zoomlevel = strtof(EARGF(usage()), NULL);
1764 loaduri(clients, &arg);
1771 return EXIT_SUCCESS;