1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
20 #include <gdk/gdkkeysyms.h>
22 #include <glib/gstdio.h>
25 #include <JavaScriptCore/JavaScript.h>
26 #include <webkit2/webkit2.h>
28 #include <X11/Xatom.h>
32 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
33 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
35 enum { AtomFind, AtomGo, AtomUri, AtomLast };
48 OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
49 OnLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
50 OnImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
51 OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
52 OnEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
53 OnBar = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
54 OnSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
55 OnAny = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
64 typedef struct Client {
67 WebKitWebInspector *inspector;
68 WebKitFindController *finder;
69 WebKitHitTestResult *mousepos;
70 GTlsCertificateFlags tlsflags;
72 int progress, fullscreen;
73 const char *title, *overtitle, *targeturi;
81 void (*func)(Client *c, const Arg *a);
89 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
91 unsigned int stopevent;
101 static void usage(void);
102 static void die(const char *errstr, ...);
103 static void setup(void);
104 static void sigchld(int unused);
105 static char *buildfile(const char *path);
106 static char *buildpath(const char *path);
107 static Client *newclient(Client *c);
108 static void loaduri(Client *c, const Arg *a);
109 static const char *geturi(Client *c);
110 static void setatom(Client *c, int a, const char *v);
111 static const char *getatom(Client *c, int a);
112 static void updatetitle(Client *c);
113 static void gettogglestats(Client *c);
114 static void getpagestats(Client *c);
115 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
116 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
117 static const char *getstyle(const char *uri);
118 static void setstyle(Client *c, const char *stylefile);
119 static void runscript(Client *c);
120 static void evalscript(Client *c, const char *jsstr, ...);
121 static void updatewinid(Client *c);
122 static void handleplumb(Client *c, const char *uri);
123 static void newwindow(Client *c, const Arg *a, int noembed);
124 static void spawn(Client *c, const Arg *a);
125 static void destroyclient(Client *c);
126 static void cleanup(void);
129 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
130 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
132 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
133 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
135 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
136 static void showview(WebKitWebView *v, Client *c);
137 static GtkWidget *createwindow(Client *c);
138 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
139 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
140 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
141 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
142 guint modifiers, Client *c);
143 static gboolean permissionrequested(WebKitWebView *v,
144 WebKitPermissionRequest *r, Client *c);
145 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
146 WebKitPolicyDecisionType dt, Client *c);
147 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
148 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
149 static void decideresource(WebKitPolicyDecision *d, Client *c);
150 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
152 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
153 static void download(Client *c, WebKitURIResponse *r);
154 static void closeview(WebKitWebView *v, Client *c);
155 static void destroywin(GtkWidget* w, Client *c);
158 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
159 static void reload(Client *c, const Arg *a);
160 static void print(Client *c, const Arg *a);
161 static void clipboard(Client *c, const Arg *a);
162 static void zoom(Client *c, const Arg *a);
163 static void scroll_v(Client *c, const Arg *a);
164 static void scroll_h(Client *c, const Arg *a);
165 static void navigate(Client *c, const Arg *a);
166 static void stop(Client *c, const Arg *a);
167 static void toggle(Client *c, const Arg *a);
168 static void togglefullscreen(Client *c, const Arg *a);
169 static void togglecookiepolicy(Client *c, const Arg *a);
170 static void togglestyle(Client *c, const Arg *a);
171 static void toggleinspector(Client *c, const Arg *a);
172 static void find(Client *c, const Arg *a);
175 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
176 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
177 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
179 static char winid[64];
180 static char togglestats[10];
181 static char pagestats[2];
182 static Atom atoms[AtomLast];
185 static int cookiepolicy;
187 static Client *clients;
188 static char *stylefile;
189 static const char *useragent;
192 /* configuration, allows nested code to access above variables */
198 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
199 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
200 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
204 die(const char *errstr, ...)
208 va_start(ap, errstr);
209 vfprintf(stderr, errstr, ap);
219 /* clean up any zombies immediately */
221 gtk_init(NULL, NULL);
223 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
226 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
227 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
228 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
231 cookiefile = buildfile(cookiefile);
232 scriptfile = buildfile(scriptfile);
233 cachedir = buildpath(cachedir);
236 styledir = buildpath(styledir);
237 for (i = 0; i < LENGTH(styles); ++i) {
238 if (regcomp(&(styles[i].re), styles[i].regex,
241 "Could not compile regex: %s\n",
243 styles[i].regex = NULL;
245 styles[i].style = g_strconcat(styledir, "/",
246 styles[i].style, NULL);
250 stylefile = buildfile(stylefile);
257 if (signal(SIGCHLD, sigchld) == SIG_ERR)
258 die("Can't install SIGCHLD handler");
259 while (waitpid(-1, NULL, WNOHANG) > 0)
264 buildfile(const char *path)
266 char *dname, *bname, *bpath, *fpath;
269 dname = g_path_get_dirname(path);
270 bname = g_path_get_basename(path);
272 bpath = buildpath(dname);
275 fpath = g_build_filename(bpath, bname, NULL);
279 if (!(f = fopen(fpath, "a")))
280 die("Could not open file: %s\n", fpath);
282 g_chmod(fpath, 0600); /* always */
289 buildpath(const char *path)
292 char *apath, *name, *p, *fpath;
294 if (path[0] == '~') {
295 if (path[1] == '/' || path[1] == '\0') {
296 p = (char *)&path[1];
297 pw = getpwuid(getuid());
299 if ((p = strchr(path, '/')))
300 name = g_strndup(&path[1], --p - path);
302 name = g_strdup(&path[1]);
304 if (!(pw = getpwnam(name))) {
305 die("Can't get user %s home directory: %s.\n",
310 apath = g_build_filename(pw->pw_dir, p, NULL);
312 apath = g_strdup(path);
315 /* creating directory */
316 if (g_mkdir_with_parents(apath, 0700) < 0)
317 die("Could not access directory: %s\n", apath);
319 fpath = realpath(apath, NULL);
326 newclient(Client *rc)
330 if (!(c = calloc(1, sizeof(Client))))
331 die("Cannot malloc!\n");
337 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
338 c->view = newview(c, rc ? rc->view : NULL);
344 loaduri(Client *c, const Arg *a)
348 const char *uri = a->v;
350 if (g_strcmp0(uri, "") == 0)
353 if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
355 } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
356 url = g_strdup_printf("file://%s", path);
359 url = g_strdup_printf("http://%s", uri);
362 setatom(c, AtomUri, url);
364 if (strcmp(url, geturi(c)) == 0) {
367 webkit_web_view_load_uri(c->view, url);
368 c->title = geturi(c);
380 if (!(uri = webkit_web_view_get_uri(c->view)))
386 setatom(Client *c, int a, const char *v)
389 XChangeProperty(dpy, c->xid,
390 atoms[a], XA_STRING, 8, PropModeReplace,
391 (unsigned char *)v, strlen(v) + 1);
395 getatom(Client *c, int a)
397 static char buf[BUFSIZ];
400 unsigned long ldummy;
401 unsigned char *p = NULL;
403 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
404 &adummy, &idummy, &ldummy, &ldummy, &p);
406 strncpy(buf, (char *)p, LENGTH(buf) - 1);
415 updatetitle(Client *c)
418 const char *name = c->overtitle ? c->overtitle :
419 c->title ? c->title : "";
421 if (showindicators) {
425 if (c->progress != 100)
426 title = g_strdup_printf("[%i%%] %s:%s | %s",
427 c->progress, togglestats, pagestats, name);
429 title = g_strdup_printf("%s:%s | %s",
430 togglestats, pagestats, name);
432 gtk_window_set_title(GTK_WINDOW(c->win), title);
435 gtk_window_set_title(GTK_WINDOW(c->win), name);
440 gettogglestats(Client *c)
442 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
443 togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
444 togglestats[2] = allowgeolocation ? 'G' : 'g';
445 togglestats[3] = enablecache ? 'D' : 'd';
446 togglestats[4] = loadimages ? 'I' : 'i';
447 togglestats[5] = enablescripts ? 'S' : 's';
448 togglestats[6] = enableplugins ? 'V' : 'v';
449 togglestats[7] = enablestyle ? 'M' : 'm';
450 togglestats[8] = enableframeflattening ? 'F' : 'f';
451 togglestats[9] = '\0';
455 getpagestats(Client *c)
457 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
458 c->tlsflags > 0 ? 'U' : 'T';
462 WebKitCookieAcceptPolicy
463 cookiepolicy_get(void)
465 switch (cookiepolicies[cookiepolicy]) {
467 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
469 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
470 default: /* fallthrough */
472 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
478 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
481 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
483 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
485 default: /* fallthrough */
486 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
493 getstyle(const char *uri)
500 for (i = 0; i < LENGTH(styles); ++i) {
501 if (styles[i].regex &&
502 !regexec(&(styles[i].re), uri, 0, NULL, 0))
503 return styles[i].style;
510 setstyle(Client *c, const char *stylefile)
514 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
515 fprintf(stderr, "Could not read style file: %s\n", stylefile);
519 webkit_user_content_manager_add_style_sheet(
520 webkit_web_view_get_user_content_manager(c->view),
521 webkit_user_style_sheet_new(style,
522 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
523 WEBKIT_USER_STYLE_LEVEL_USER,
535 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
536 evalscript(c, script);
541 evalscript(Client *c, const char *jsstr, ...)
547 script = g_strdup_vprintf(jsstr, ap);
550 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
555 updatewinid(Client *c)
557 snprintf(winid, LENGTH(winid), "%lu", c->xid);
561 handleplumb(Client *c, const char *uri)
563 Arg a = (Arg)PLUMB(uri);
568 newwindow(Client *c, const Arg *a, int noembed)
572 const char *cmd[26], *uri;
573 const Arg arg = { .v = cmd };
577 cmd[i++] = cookiepolicies;
578 cmd[i++] = enablescrollbars ? "-B" : "-b";
579 if (cookiefile && g_strcmp0(cookiefile, "")) {
581 cmd[i++] = cookiefile;
583 cmd[i++] = enablecache ? "-D" : "-d";
584 if (embed && !noembed) {
586 snprintf(tmp, LENGTH(tmp), "%lu", embed);
589 cmd[i++] = runinfullscreen ? "-F" : "-f";
590 cmd[i++] = allowgeolocation ? "-G" : "-g";
591 cmd[i++] = loadimages ? "-I" : "-i";
592 cmd[i++] = kioskmode ? "-K" : "-k";
593 cmd[i++] = enablestyle ? "-M" : "-m";
594 cmd[i++] = enableinspector ? "-N" : "-n";
595 cmd[i++] = enableplugins ? "-P" : "-p";
596 if (scriptfile && g_strcmp0(scriptfile, "")) {
598 cmd[i++] = scriptfile;
600 cmd[i++] = enablescripts ? "-S" : "-s";
601 if (stylefile && g_strcmp0(stylefile, "")) {
603 cmd[i++] = stylefile;
605 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
607 cmd[i++] = fulluseragent;
611 /* do not keep zoom level */
621 spawn(Client *c, const Arg *a)
625 close(ConnectionNumber(dpy));
627 execvp(((char **)a->v)[0], (char **)a->v);
628 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
635 destroyclient(Client *c)
639 webkit_web_view_stop_loading(c->view);
640 /* Not needed, has already been called
641 gtk_widget_destroy(c->win);
644 for (p = clients; p && p->next != c; p = p->next)
657 destroyclient(clients);
665 newview(Client *c, WebKitWebView *rv)
668 WebKitSettings *settings;
669 WebKitUserContentManager *contentmanager;
670 WebKitWebContext *context;
675 webkit_web_view_new_with_related_view(rv));
677 settings = webkit_settings_new_with_settings(
678 "auto-load-images", loadimages,
679 "default-font-size", defaultfontsize,
680 "enable-caret-browsing", enablecaretbrowsing,
681 "enable-developer-extras", enableinspector,
682 "enable-dns-prefetching", enablednsprefetching,
683 "enable-frame-flattening", enableframeflattening,
684 "enable-html5-database", enablecache,
685 "enable-html5-local-storage", enablecache,
686 "enable-javascript", enablescripts,
687 "enable-plugins", enableplugins,
689 /* For mor interesting settings, have a look at
690 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
692 if (strcmp(fulluseragent, "")) {
693 webkit_settings_set_user_agent(settings, fulluseragent);
694 } else if (surfuseragent) {
695 webkit_settings_set_user_agent_with_application_details(
696 settings, "Surf", VERSION);
698 useragent = webkit_settings_get_user_agent(settings);
700 contentmanager = webkit_user_content_manager_new();
702 context = webkit_web_context_new_with_website_data_manager(
703 webkit_website_data_manager_new(
704 "base-cache-directory", cachedir,
705 "base-data-directory", cachedir,
708 /* rendering process model, can be a shared unique one
709 * or one for each view */
710 webkit_web_context_set_process_model(context,
711 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
713 webkit_web_context_set_tls_errors_policy(context, strictssl ?
714 WEBKIT_TLS_ERRORS_POLICY_FAIL :
715 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
717 webkit_web_context_set_cache_model(context, enablecache ?
718 WEBKIT_CACHE_MODEL_WEB_BROWSER :
719 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
721 /* Currently only works with text file to be compatible with curl */
722 webkit_cookie_manager_set_persistent_storage(
723 webkit_web_context_get_cookie_manager(context), cookiefile,
724 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
726 webkit_cookie_manager_set_accept_policy(
727 webkit_web_context_get_cookie_manager(context),
730 g_signal_connect(G_OBJECT(context), "download-started",
731 G_CALLBACK(downloadstarted), c);
733 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
734 "settings", settings,
735 "user-content-manager", contentmanager,
736 "web-context", context,
740 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
741 G_CALLBACK(progresschanged), c);
742 g_signal_connect(G_OBJECT(v), "notify::title",
743 G_CALLBACK(titlechanged), c);
744 g_signal_connect(G_OBJECT(v), "button-release-event",
745 G_CALLBACK(buttonreleased), c);
746 g_signal_connect(G_OBJECT(v), "close",
747 G_CALLBACK(closeview), c);
748 g_signal_connect(G_OBJECT(v), "create",
749 G_CALLBACK(createview), c);
750 g_signal_connect(G_OBJECT(v), "decide-policy",
751 G_CALLBACK(decidepolicy), c);
752 g_signal_connect(G_OBJECT(v), "load-changed",
753 G_CALLBACK(loadchanged), c);
754 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
755 G_CALLBACK(mousetargetchanged), c);
756 g_signal_connect(G_OBJECT(v), "permission-request",
757 G_CALLBACK(permissionrequested), c);
758 g_signal_connect(G_OBJECT(v), "ready-to-show",
759 G_CALLBACK(showview), c);
765 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
769 switch (webkit_navigation_action_get_navigation_type(a)) {
770 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
772 * popup windows of type “other” are almost always triggered
773 * by user gesture, so inverse the logic here
775 /* instead of this, compare destination uri to mouse-over uri for validating window */
776 if (webkit_navigation_action_is_user_gesture(a))
778 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
779 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
780 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
781 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
782 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
789 return GTK_WIDGET(n->view);
793 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
795 WebKitHitTestResultContext element;
798 element = webkit_hit_test_result_get_context(c->mousepos);
800 for (i = 0; i < LENGTH(buttons); ++i) {
801 if (element & buttons[i].target &&
802 e->button.button == buttons[i].button &&
803 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
805 buttons[i].func(c, &buttons[i].arg, c->mousepos);
806 return buttons[i].stopevent;
814 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
816 Client *c = (Client *)d;
820 if (((XEvent *)e)->type == PropertyNotify) {
821 ev = &((XEvent *)e)->xproperty;
822 if (ev->state == PropertyNewValue) {
823 if (ev->atom == atoms[AtomFind]) {
826 return GDK_FILTER_REMOVE;
827 } else if (ev->atom == atoms[AtomGo]) {
828 a.v = getatom(c, AtomGo);
831 return GDK_FILTER_REMOVE;
835 return GDK_FILTER_CONTINUE;
839 winevent(GtkWidget *w, GdkEvent *e, Client *c)
844 case GDK_ENTER_NOTIFY:
845 c->overtitle = c->targeturi;
850 for (i = 0; i < LENGTH(keys); ++i) {
851 if (gdk_keyval_to_lower(e->key.keyval) ==
853 CLEANMASK(e->key.state) == keys[i].mod &&
856 keys[i].func(c, &(keys[i].arg));
861 case GDK_LEAVE_NOTIFY:
865 case GDK_WINDOW_STATE:
866 if (e->window_state.changed_mask ==
867 GDK_WINDOW_STATE_FULLSCREEN)
868 c->fullscreen = e->window_state.new_window_state &
869 GDK_WINDOW_STATE_FULLSCREEN;
879 showview(WebKitWebView *v, Client *c)
881 GdkRGBA bgcolor = { 0 };
884 c->finder = webkit_web_view_get_find_controller(c->view);
886 c->inspector = webkit_web_view_get_inspector(c->view);
888 c->win = createwindow(c);
890 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
891 gtk_widget_show_all(c->win);
892 gtk_widget_grab_focus(GTK_WIDGET(c->view));
894 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
895 c->xid = gdk_x11_window_get_xid(gwin);
898 gdk_display_sync(gtk_widget_get_display(c->win));
903 webkit_web_view_set_background_color(c->view, &bgcolor);
906 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
907 gdk_window_add_filter(gwin, processx, c);
911 togglefullscreen(c, NULL);
913 if (zoomlevel != 1.0)
914 webkit_web_view_set_zoom_level(c->view, zoomlevel);
916 setatom(c, AtomFind, "");
917 setatom(c, AtomUri, "about:blank");
921 createwindow(Client *c)
927 w = gtk_plug_new(embed);
929 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
931 wmstr = g_path_get_basename(argv0);
932 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
935 wmstr = g_strdup_printf("%s[%lu]", "Surf",
936 webkit_web_view_get_page_id(c->view));
937 gtk_window_set_role(GTK_WINDOW(w), wmstr);
940 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
943 g_signal_connect(G_OBJECT(w), "destroy",
944 G_CALLBACK(destroywin), c);
945 g_signal_connect(G_OBJECT(w), "enter-notify-event",
946 G_CALLBACK(winevent), c);
947 g_signal_connect(G_OBJECT(w), "key-press-event",
948 G_CALLBACK(winevent), c);
949 g_signal_connect(G_OBJECT(w), "leave-notify-event",
950 G_CALLBACK(winevent), c);
951 g_signal_connect(G_OBJECT(w), "window-state-event",
952 G_CALLBACK(winevent), c);
958 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
961 case WEBKIT_LOAD_STARTED:
962 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
964 case WEBKIT_LOAD_REDIRECTED:
965 setatom(c, AtomUri, geturi(c));
967 case WEBKIT_LOAD_COMMITTED:
968 if (!webkit_web_view_get_tls_info(c->view, NULL,
970 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
972 setatom(c, AtomUri, geturi(c));
975 setstyle(c, getstyle(geturi(c)));
977 case WEBKIT_LOAD_FINISHED:
978 /* Disabled until we write some WebKitWebExtension for
979 * manipulating the DOM directly.
980 evalscript(c, "document.documentElement.style.overflow = '%s'",
981 enablescrollbars ? "auto" : "hidden");
990 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
992 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
998 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1000 c->title = webkit_web_view_get_title(c->view);
1005 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1008 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1010 /* Keep the hit test to know where is the pointer on the next click */
1014 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1015 else if (hc & OnImg)
1016 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1017 else if (hc & OnMedia)
1018 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1020 c->targeturi = NULL;
1022 c->overtitle = c->targeturi;
1027 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1029 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1030 if (allowgeolocation)
1031 webkit_permission_request_allow(r);
1033 webkit_permission_request_deny(r);
1041 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1042 WebKitPolicyDecisionType dt, Client *c)
1045 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1046 decidenavigation(d, c);
1048 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1049 decidenewwindow(d, c);
1051 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1052 decideresource(d, c);
1055 webkit_policy_decision_ignore(d);
1062 decidenavigation(WebKitPolicyDecision *d, Client *c)
1064 WebKitNavigationAction *a =
1065 webkit_navigation_policy_decision_get_navigation_action(
1066 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1068 switch (webkit_navigation_action_get_navigation_type(a)) {
1069 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1070 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1071 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1072 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1073 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1074 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1076 /* Do not navigate to links with a "_blank" target (popup) */
1077 if (webkit_navigation_policy_decision_get_frame_name(
1078 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1079 webkit_policy_decision_ignore(d);
1081 /* Filter out navigation to different domain ? */
1082 /* get action→urirequest, copy and load in new window+view
1083 * on Ctrl+Click ? */
1084 webkit_policy_decision_use(d);
1091 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1094 WebKitNavigationAction *a =
1095 webkit_navigation_policy_decision_get_navigation_action(
1096 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1099 switch (webkit_navigation_action_get_navigation_type(a)) {
1100 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1101 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1102 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1103 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1104 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1105 /* Filter domains here */
1106 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1107 * test for link clicked but no button ? */
1108 arg.v = webkit_uri_request_get_uri(
1109 webkit_navigation_action_get_request(a));
1110 newwindow(c, &arg, 0);
1112 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1117 webkit_policy_decision_ignore(d);
1121 decideresource(WebKitPolicyDecision *d, Client *c)
1124 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1125 WebKitURIResponse *res =
1126 webkit_response_policy_decision_get_response(r);
1127 const gchar *uri = webkit_uri_response_get_uri(res);
1129 if (g_str_has_suffix(uri, "/favicon.ico")) {
1130 webkit_policy_decision_ignore(d);
1134 if (!g_str_has_prefix(uri, "http://")
1135 && !g_str_has_prefix(uri, "https://")
1136 && !g_str_has_prefix(uri, "about:")
1137 && !g_str_has_prefix(uri, "file://")
1138 && !g_str_has_prefix(uri, "data:")
1139 && !g_str_has_prefix(uri, "blob:")
1140 && strlen(uri) > 0) {
1141 for (i = 0; i < strlen(uri); i++) {
1142 if (!g_ascii_isprint(uri[i])) {
1148 handleplumb(c, uri);
1149 webkit_policy_decision_ignore(d);
1154 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1155 webkit_policy_decision_use(d);
1157 webkit_policy_decision_ignore(d);
1163 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1165 g_signal_connect(G_OBJECT(d), "notify::response",
1166 G_CALLBACK(responsereceived), c);
1170 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1172 download(c, webkit_download_get_response(d));
1173 webkit_download_cancel(d);
1177 download(Client *c, WebKitURIResponse *r)
1179 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1184 closeview(WebKitWebView *v, Client *c)
1186 gtk_widget_destroy(c->win);
1190 destroywin(GtkWidget* w, Client *c)
1198 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1200 Arg a = {.v = text };
1202 loaduri((Client *) d, &a);
1206 reload(Client *c, const Arg *a)
1209 webkit_web_view_reload_bypass_cache(c->view);
1211 webkit_web_view_reload(c->view);
1215 print(Client *c, const Arg *a)
1217 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1218 GTK_WINDOW(c->win));
1222 clipboard(Client *c, const Arg *a)
1224 if (a->b) { /* load clipboard uri */
1225 gtk_clipboard_request_text(gtk_clipboard_get(
1226 GDK_SELECTION_PRIMARY),
1228 } else { /* copy uri */
1229 gtk_clipboard_set_text(gtk_clipboard_get(
1230 GDK_SELECTION_PRIMARY), c->targeturi
1231 ? c->targeturi : geturi(c), -1);
1236 zoom(Client *c, const Arg *a)
1239 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1241 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1243 webkit_web_view_set_zoom_level(c->view, 1.0);
1245 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1249 scroll_v(Client *c, const Arg *a)
1251 evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1256 scroll_h(Client *c, const Arg *a)
1258 evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1263 navigate(Client *c, const Arg *a)
1266 webkit_web_view_go_back(c->view);
1268 webkit_web_view_go_forward(c->view);
1272 stop(Client *c, const Arg *a)
1274 webkit_web_view_stop_loading(c->view);
1278 toggle(Client *c, const Arg *a)
1280 WebKitSettings *s = webkit_web_view_get_settings(c->view);
1282 switch ((unsigned int)a->i) {
1284 enablecaretbrowsing = !enablecaretbrowsing;
1285 webkit_settings_set_enable_caret_browsing(s,
1286 enablecaretbrowsing);
1288 return; /* do not reload */
1290 case FrameFlattening:
1291 enableframeflattening = !enableframeflattening;
1292 webkit_settings_set_enable_frame_flattening(s,
1293 enableframeflattening);
1296 allowgeolocation = !allowgeolocation;
1299 enablescripts = !enablescripts;
1300 webkit_settings_set_enable_javascript(s, enablescripts);
1303 loadimages = !loadimages;
1304 webkit_settings_set_auto_load_images(s, loadimages);
1307 enableplugins = !enableplugins;
1308 webkit_settings_set_enable_plugins(s, enableplugins);
1311 /* Disabled until we write some WebKitWebExtension for
1312 * manipulating the DOM directly.
1313 enablescrollbars = !enablescrollbars;
1314 evalscript(c, "document.documentElement.style.overflow = '%s'",
1315 enablescrollbars ? "auto" : "hidden");
1317 return; /* do not reload */
1326 togglefullscreen(Client *c, const Arg *a)
1328 /* toggling value is handled in winevent() */
1330 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1332 gtk_window_fullscreen(GTK_WINDOW(c->win));
1336 togglecookiepolicy(Client *c, const Arg *a)
1339 cookiepolicy %= strlen(cookiepolicies);
1341 webkit_cookie_manager_set_accept_policy(
1342 webkit_web_context_get_cookie_manager(
1343 webkit_web_view_get_context(c->view)),
1344 cookiepolicy_get());
1347 /* Do not reload. */
1351 togglestyle(Client *c, const Arg *a)
1353 enablestyle = !enablestyle;
1356 setstyle(c, getstyle(geturi(c)));
1358 webkit_user_content_manager_remove_all_style_sheets(
1359 webkit_web_view_get_user_content_manager(c->view));
1365 toggleinspector(Client *c, const Arg *a)
1367 if (enableinspector) {
1368 if (webkit_web_inspector_is_attached(c->inspector))
1369 webkit_web_inspector_close(c->inspector);
1371 webkit_web_inspector_show(c->inspector);
1376 find(Client *c, const Arg *a)
1382 webkit_find_controller_search_next(c->finder);
1384 webkit_find_controller_search_previous(c->finder);
1386 s = getatom(c, AtomFind);
1387 f = webkit_find_controller_get_search_text(c->finder);
1389 if (g_strcmp0(f, s) == 0) /* reset search */
1390 webkit_find_controller_search(c->finder, "", findopts,
1393 webkit_find_controller_search(c->finder, s, findopts,
1396 if (strcmp(s, "") == 0)
1397 webkit_find_controller_search_finish(c->finder);
1402 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1408 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1412 arg.v = webkit_hit_test_result_get_link_uri(h);
1413 newwindow(c, &arg, a->b);
1417 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1421 if (webkit_hit_test_result_get_context(h) & OnMedia) {
1422 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1428 main(int argc, char *argv[])
1433 memset(&arg, 0, sizeof(arg));
1435 /* command line args */
1438 cookiepolicies = EARGF(usage());
1441 enablescrollbars = 0;
1444 enablescrollbars = 1;
1447 cookiefile = EARGF(usage());
1456 embed = strtol(EARGF(usage()), NULL, 0);
1459 runinfullscreen = 0;
1462 runinfullscreen = 1;
1465 allowgeolocation = 0;
1468 allowgeolocation = 1;
1489 enableinspector = 0;
1492 enableinspector = 1;
1501 scriptfile = EARGF(usage());
1510 stylefile = EARGF(usage());
1513 useragent = EARGF(usage());
1516 die("surf-"VERSION", ©2009-2015 surf engineers, "
1517 "see LICENSE for details\n");
1522 zoomlevel = strtof(EARGF(usage()), NULL);
1530 arg.v = "about:blank";
1533 c = newclient(NULL);