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, *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 addaccelgroup(Client *c);
109 static void loaduri(Client *c, const Arg *a);
110 static const char *geturi(Client *c);
111 static void setatom(Client *c, int a, const char *v);
112 static const char *getatom(Client *c, int a);
113 static void updatetitle(Client *c);
114 static void gettogglestats(Client *c);
115 static void getpagestats(Client *c);
116 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
117 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
118 static const char *getstyle(const char *uri);
119 static void setstyle(Client *c, const char *stylefile);
120 static void runscript(Client *c);
121 static void evalscript(Client *c, const char *jsstr, ...);
122 static void updatewinid(Client *c);
123 static void handleplumb(Client *c, const char *uri);
124 static void newwindow(Client *c, const Arg *a, int noembed);
125 static void spawn(Client *c, const Arg *a);
126 static void destroyclient(Client *c);
127 static void cleanup(void);
130 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
131 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
133 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
134 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
135 GdkModifierType mods, Client *c);
136 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
138 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
139 static void showview(WebKitWebView *v, Client *c);
140 static GtkWidget *createwindow(Client *c);
141 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
142 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
143 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
144 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
145 guint modifiers, Client *c);
146 static gboolean permissionrequested(WebKitWebView *v,
147 WebKitPermissionRequest *r, Client *c);
148 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
149 WebKitPolicyDecisionType dt, Client *c);
150 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
151 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
152 static void decideresource(WebKitPolicyDecision *d, Client *c);
153 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
155 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
156 static void download(Client *c, WebKitURIResponse *r);
157 static void closeview(WebKitWebView *v, Client *c);
158 static void destroywin(GtkWidget* w, Client *c);
161 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
162 static void reload(Client *c, const Arg *a);
163 static void print(Client *c, const Arg *a);
164 static void clipboard(Client *c, const Arg *a);
165 static void zoom(Client *c, const Arg *a);
166 static void scroll_v(Client *c, const Arg *a);
167 static void scroll_h(Client *c, const Arg *a);
168 static void navigate(Client *c, const Arg *a);
169 static void stop(Client *c, const Arg *a);
170 static void toggle(Client *c, const Arg *a);
171 static void togglefullscreen(Client *c, const Arg *a);
172 static void togglecookiepolicy(Client *c, const Arg *a);
173 static void togglestyle(Client *c, const Arg *a);
174 static void toggleinspector(Client *c, const Arg *a);
175 static void find(Client *c, const Arg *a);
178 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
179 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
180 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
182 static char winid[64];
183 static char togglestats[10];
184 static char pagestats[2];
185 static Atom atoms[AtomLast];
188 static int cookiepolicy;
190 static Client *clients;
191 static char *stylefile;
192 static const char *useragent;
195 /* configuration, allows nested code to access above variables */
201 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
202 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
203 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
207 die(const char *errstr, ...)
211 va_start(ap, errstr);
212 vfprintf(stderr, errstr, ap);
222 /* clean up any zombies immediately */
224 gtk_init(NULL, NULL);
226 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
229 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
230 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
231 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
234 cookiefile = buildfile(cookiefile);
235 scriptfile = buildfile(scriptfile);
236 cachedir = buildpath(cachedir);
239 styledir = buildpath(styledir);
240 for (i = 0; i < LENGTH(styles); ++i) {
241 if (regcomp(&(styles[i].re), styles[i].regex,
244 "Could not compile regex: %s\n",
246 styles[i].regex = NULL;
248 styles[i].style = g_strconcat(styledir, "/",
249 styles[i].style, NULL);
253 stylefile = buildfile(stylefile);
260 if (signal(SIGCHLD, sigchld) == SIG_ERR)
261 die("Can't install SIGCHLD handler");
262 while (waitpid(-1, NULL, WNOHANG) > 0)
267 buildfile(const char *path)
269 char *dname, *bname, *bpath, *fpath;
272 dname = g_path_get_dirname(path);
273 bname = g_path_get_basename(path);
275 bpath = buildpath(dname);
278 fpath = g_build_filename(bpath, bname, NULL);
282 if (!(f = fopen(fpath, "a")))
283 die("Could not open file: %s\n", fpath);
285 g_chmod(fpath, 0600); /* always */
292 buildpath(const char *path)
295 char *apath, *name, *p, *fpath;
297 if (path[0] == '~') {
298 if (path[1] == '/' || path[1] == '\0') {
299 p = (char *)&path[1];
300 pw = getpwuid(getuid());
302 if ((p = strchr(path, '/')))
303 name = g_strndup(&path[1], --p - path);
305 name = g_strdup(&path[1]);
307 if (!(pw = getpwnam(name))) {
308 die("Can't get user %s home directory: %s.\n",
313 apath = g_build_filename(pw->pw_dir, p, NULL);
315 apath = g_strdup(path);
318 /* creating directory */
319 if (g_mkdir_with_parents(apath, 0700) < 0)
320 die("Could not access directory: %s\n", apath);
322 fpath = realpath(apath, NULL);
329 newclient(Client *rc)
333 if (!(c = calloc(1, sizeof(Client))))
334 die("Cannot malloc!\n");
340 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
341 c->view = newview(c, rc ? rc->view : NULL);
347 addaccelgroup(Client *c)
350 GtkAccelGroup *group = gtk_accel_group_new();
353 for (i = 0; i < LENGTH(keys); i++) {
354 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
355 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
358 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
362 loaduri(Client *c, const Arg *a)
366 const char *uri = a->v;
368 if (g_strcmp0(uri, "") == 0)
371 if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
373 } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
374 url = g_strdup_printf("file://%s", path);
377 url = g_strdup_printf("http://%s", uri);
380 setatom(c, AtomUri, url);
382 if (strcmp(url, geturi(c)) == 0) {
385 webkit_web_view_load_uri(c->view, url);
386 c->title = geturi(c);
398 if (!(uri = webkit_web_view_get_uri(c->view)))
404 setatom(Client *c, int a, const char *v)
407 XChangeProperty(dpy, c->xid,
408 atoms[a], XA_STRING, 8, PropModeReplace,
409 (unsigned char *)v, strlen(v) + 1);
413 getatom(Client *c, int a)
415 static char buf[BUFSIZ];
418 unsigned long ldummy;
419 unsigned char *p = NULL;
421 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
422 &adummy, &idummy, &ldummy, &ldummy, &p);
424 strncpy(buf, (char *)p, LENGTH(buf) - 1);
433 updatetitle(Client *c)
436 const char *name = c->targeturi ? c->targeturi :
437 c->title ? c->title : "";
439 if (showindicators) {
443 if (c->progress != 100)
444 title = g_strdup_printf("[%i%%] %s:%s | %s",
445 c->progress, togglestats, pagestats, name);
447 title = g_strdup_printf("%s:%s | %s",
448 togglestats, pagestats, name);
450 gtk_window_set_title(GTK_WINDOW(c->win), title);
453 gtk_window_set_title(GTK_WINDOW(c->win), name);
458 gettogglestats(Client *c)
460 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
461 togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
462 togglestats[2] = allowgeolocation ? 'G' : 'g';
463 togglestats[3] = enablecache ? 'D' : 'd';
464 togglestats[4] = loadimages ? 'I' : 'i';
465 togglestats[5] = enablescripts ? 'S' : 's';
466 togglestats[6] = enableplugins ? 'V' : 'v';
467 togglestats[7] = enablestyle ? 'M' : 'm';
468 togglestats[8] = enableframeflattening ? 'F' : 'f';
469 togglestats[9] = '\0';
473 getpagestats(Client *c)
475 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
476 c->tlsflags > 0 ? 'U' : 'T';
480 WebKitCookieAcceptPolicy
481 cookiepolicy_get(void)
483 switch (cookiepolicies[cookiepolicy]) {
485 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
487 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
488 default: /* fallthrough */
490 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
496 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
499 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
501 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
503 default: /* fallthrough */
504 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
511 getstyle(const char *uri)
518 for (i = 0; i < LENGTH(styles); ++i) {
519 if (styles[i].regex &&
520 !regexec(&(styles[i].re), uri, 0, NULL, 0))
521 return styles[i].style;
528 setstyle(Client *c, const char *stylefile)
532 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
533 fprintf(stderr, "Could not read style file: %s\n", stylefile);
537 webkit_user_content_manager_add_style_sheet(
538 webkit_web_view_get_user_content_manager(c->view),
539 webkit_user_style_sheet_new(style,
540 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
541 WEBKIT_USER_STYLE_LEVEL_USER,
553 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
554 evalscript(c, script);
559 evalscript(Client *c, const char *jsstr, ...)
565 script = g_strdup_vprintf(jsstr, ap);
568 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
573 updatewinid(Client *c)
575 snprintf(winid, LENGTH(winid), "%lu", c->xid);
579 handleplumb(Client *c, const char *uri)
581 Arg a = (Arg)PLUMB(uri);
586 newwindow(Client *c, const Arg *a, int noembed)
590 const char *cmd[26], *uri;
591 const Arg arg = { .v = cmd };
595 cmd[i++] = cookiepolicies;
596 cmd[i++] = enablescrollbars ? "-B" : "-b";
597 if (cookiefile && g_strcmp0(cookiefile, "")) {
599 cmd[i++] = cookiefile;
601 cmd[i++] = enablecache ? "-D" : "-d";
602 if (embed && !noembed) {
604 snprintf(tmp, LENGTH(tmp), "%lu", embed);
607 cmd[i++] = runinfullscreen ? "-F" : "-f";
608 cmd[i++] = allowgeolocation ? "-G" : "-g";
609 cmd[i++] = loadimages ? "-I" : "-i";
610 cmd[i++] = kioskmode ? "-K" : "-k";
611 cmd[i++] = enablestyle ? "-M" : "-m";
612 cmd[i++] = enableinspector ? "-N" : "-n";
613 cmd[i++] = enableplugins ? "-P" : "-p";
614 if (scriptfile && g_strcmp0(scriptfile, "")) {
616 cmd[i++] = scriptfile;
618 cmd[i++] = enablescripts ? "-S" : "-s";
619 if (stylefile && g_strcmp0(stylefile, "")) {
621 cmd[i++] = stylefile;
623 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
625 cmd[i++] = fulluseragent;
629 /* do not keep zoom level */
639 spawn(Client *c, const Arg *a)
643 close(ConnectionNumber(dpy));
645 execvp(((char **)a->v)[0], (char **)a->v);
646 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
653 destroyclient(Client *c)
657 webkit_web_view_stop_loading(c->view);
658 /* Not needed, has already been called
659 gtk_widget_destroy(c->win);
662 for (p = clients; p && p->next != c; p = p->next)
675 destroyclient(clients);
683 newview(Client *c, WebKitWebView *rv)
686 WebKitSettings *settings;
687 WebKitUserContentManager *contentmanager;
688 WebKitWebContext *context;
693 webkit_web_view_new_with_related_view(rv));
695 settings = webkit_settings_new_with_settings(
696 "auto-load-images", loadimages,
697 "default-font-size", defaultfontsize,
698 "enable-caret-browsing", enablecaretbrowsing,
699 "enable-developer-extras", enableinspector,
700 "enable-dns-prefetching", enablednsprefetching,
701 "enable-frame-flattening", enableframeflattening,
702 "enable-html5-database", enablecache,
703 "enable-html5-local-storage", enablecache,
704 "enable-javascript", enablescripts,
705 "enable-plugins", enableplugins,
707 /* For mor interesting settings, have a look at
708 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
710 if (strcmp(fulluseragent, "")) {
711 webkit_settings_set_user_agent(settings, fulluseragent);
712 } else if (surfuseragent) {
713 webkit_settings_set_user_agent_with_application_details(
714 settings, "Surf", VERSION);
716 useragent = webkit_settings_get_user_agent(settings);
718 contentmanager = webkit_user_content_manager_new();
720 context = webkit_web_context_new_with_website_data_manager(
721 webkit_website_data_manager_new(
722 "base-cache-directory", cachedir,
723 "base-data-directory", cachedir,
726 /* rendering process model, can be a shared unique one
727 * or one for each view */
728 webkit_web_context_set_process_model(context,
729 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
731 webkit_web_context_set_tls_errors_policy(context, strictssl ?
732 WEBKIT_TLS_ERRORS_POLICY_FAIL :
733 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
735 webkit_web_context_set_cache_model(context, enablecache ?
736 WEBKIT_CACHE_MODEL_WEB_BROWSER :
737 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
739 /* Currently only works with text file to be compatible with curl */
740 webkit_cookie_manager_set_persistent_storage(
741 webkit_web_context_get_cookie_manager(context), cookiefile,
742 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
744 webkit_cookie_manager_set_accept_policy(
745 webkit_web_context_get_cookie_manager(context),
748 g_signal_connect(G_OBJECT(context), "download-started",
749 G_CALLBACK(downloadstarted), c);
751 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
752 "settings", settings,
753 "user-content-manager", contentmanager,
754 "web-context", context,
758 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
759 G_CALLBACK(progresschanged), c);
760 g_signal_connect(G_OBJECT(v), "notify::title",
761 G_CALLBACK(titlechanged), c);
762 g_signal_connect(G_OBJECT(v), "button-release-event",
763 G_CALLBACK(buttonreleased), c);
764 g_signal_connect(G_OBJECT(v), "close",
765 G_CALLBACK(closeview), c);
766 g_signal_connect(G_OBJECT(v), "create",
767 G_CALLBACK(createview), c);
768 g_signal_connect(G_OBJECT(v), "decide-policy",
769 G_CALLBACK(decidepolicy), c);
770 g_signal_connect(G_OBJECT(v), "load-changed",
771 G_CALLBACK(loadchanged), c);
772 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
773 G_CALLBACK(mousetargetchanged), c);
774 g_signal_connect(G_OBJECT(v), "permission-request",
775 G_CALLBACK(permissionrequested), c);
776 g_signal_connect(G_OBJECT(v), "ready-to-show",
777 G_CALLBACK(showview), c);
783 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
787 switch (webkit_navigation_action_get_navigation_type(a)) {
788 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
790 * popup windows of type “other” are almost always triggered
791 * by user gesture, so inverse the logic here
793 /* instead of this, compare destination uri to mouse-over uri for validating window */
794 if (webkit_navigation_action_is_user_gesture(a))
796 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
797 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
798 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
799 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
800 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
807 return GTK_WIDGET(n->view);
811 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
813 WebKitHitTestResultContext element;
816 element = webkit_hit_test_result_get_context(c->mousepos);
818 for (i = 0; i < LENGTH(buttons); ++i) {
819 if (element & buttons[i].target &&
820 e->button.button == buttons[i].button &&
821 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
823 buttons[i].func(c, &buttons[i].arg, c->mousepos);
824 return buttons[i].stopevent;
832 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
836 gboolean processed = FALSE;
838 mods = CLEANMASK(mods);
839 key = gdk_keyval_to_lower(key);
841 for (i = 0; i < LENGTH(keys); i++) {
842 if (key == keys[i].keyval
843 && mods == keys[i].mod
845 keys[i].func(c, &(keys[i].arg));
854 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
856 Client *c = (Client *)d;
860 if (((XEvent *)e)->type == PropertyNotify) {
861 ev = &((XEvent *)e)->xproperty;
862 if (ev->state == PropertyNewValue) {
863 if (ev->atom == atoms[AtomFind]) {
866 return GDK_FILTER_REMOVE;
867 } else if (ev->atom == atoms[AtomGo]) {
868 a.v = getatom(c, AtomGo);
871 return GDK_FILTER_REMOVE;
875 return GDK_FILTER_CONTINUE;
879 winevent(GtkWidget *w, GdkEvent *e, Client *c)
882 case GDK_LEAVE_NOTIFY:
886 case GDK_WINDOW_STATE: /* fallthrough */
887 if (e->window_state.changed_mask ==
888 GDK_WINDOW_STATE_FULLSCREEN) {
889 c->fullscreen = e->window_state.new_window_state &
890 GDK_WINDOW_STATE_FULLSCREEN;
901 showview(WebKitWebView *v, Client *c)
903 GdkRGBA bgcolor = { 0 };
906 c->finder = webkit_web_view_get_find_controller(c->view);
908 c->inspector = webkit_web_view_get_inspector(c->view);
910 c->win = createwindow(c);
912 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
913 gtk_widget_show_all(c->win);
914 gtk_widget_grab_focus(GTK_WIDGET(c->view));
916 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
917 c->xid = gdk_x11_window_get_xid(gwin);
920 gdk_display_sync(gtk_widget_get_display(c->win));
925 webkit_web_view_set_background_color(c->view, &bgcolor);
929 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
930 gdk_window_add_filter(gwin, processx, c);
934 togglefullscreen(c, NULL);
936 if (zoomlevel != 1.0)
937 webkit_web_view_set_zoom_level(c->view, zoomlevel);
939 setatom(c, AtomFind, "");
940 setatom(c, AtomUri, "about:blank");
944 createwindow(Client *c)
950 w = gtk_plug_new(embed);
952 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
954 wmstr = g_path_get_basename(argv0);
955 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
958 wmstr = g_strdup_printf("%s[%lu]", "Surf",
959 webkit_web_view_get_page_id(c->view));
960 gtk_window_set_role(GTK_WINDOW(w), wmstr);
963 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
966 g_signal_connect(G_OBJECT(w), "destroy",
967 G_CALLBACK(destroywin), c);
968 g_signal_connect(G_OBJECT(w), "leave-notify-event",
969 G_CALLBACK(winevent), c);
970 g_signal_connect(G_OBJECT(w), "window-state-event",
971 G_CALLBACK(winevent), c);
977 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
980 case WEBKIT_LOAD_STARTED:
981 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
983 case WEBKIT_LOAD_REDIRECTED:
984 setatom(c, AtomUri, geturi(c));
986 case WEBKIT_LOAD_COMMITTED:
987 if (!webkit_web_view_get_tls_info(c->view, NULL,
989 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
991 setatom(c, AtomUri, geturi(c));
994 setstyle(c, getstyle(geturi(c)));
996 case WEBKIT_LOAD_FINISHED:
997 /* Disabled until we write some WebKitWebExtension for
998 * manipulating the DOM directly.
999 evalscript(c, "document.documentElement.style.overflow = '%s'",
1000 enablescrollbars ? "auto" : "hidden");
1009 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1011 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1017 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1019 c->title = webkit_web_view_get_title(c->view);
1024 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1027 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1029 /* Keep the hit test to know where is the pointer on the next click */
1033 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1034 else if (hc & OnImg)
1035 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1036 else if (hc & OnMedia)
1037 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1039 c->targeturi = NULL;
1044 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1046 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1047 if (allowgeolocation)
1048 webkit_permission_request_allow(r);
1050 webkit_permission_request_deny(r);
1058 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1059 WebKitPolicyDecisionType dt, Client *c)
1062 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1063 decidenavigation(d, c);
1065 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1066 decidenewwindow(d, c);
1068 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1069 decideresource(d, c);
1072 webkit_policy_decision_ignore(d);
1079 decidenavigation(WebKitPolicyDecision *d, Client *c)
1081 WebKitNavigationAction *a =
1082 webkit_navigation_policy_decision_get_navigation_action(
1083 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1085 switch (webkit_navigation_action_get_navigation_type(a)) {
1086 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1087 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1088 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1089 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1090 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1091 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1093 /* Do not navigate to links with a "_blank" target (popup) */
1094 if (webkit_navigation_policy_decision_get_frame_name(
1095 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1096 webkit_policy_decision_ignore(d);
1098 /* Filter out navigation to different domain ? */
1099 /* get action→urirequest, copy and load in new window+view
1100 * on Ctrl+Click ? */
1101 webkit_policy_decision_use(d);
1108 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1111 WebKitNavigationAction *a =
1112 webkit_navigation_policy_decision_get_navigation_action(
1113 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1116 switch (webkit_navigation_action_get_navigation_type(a)) {
1117 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1118 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1119 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1120 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1121 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1122 /* Filter domains here */
1123 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1124 * test for link clicked but no button ? */
1125 arg.v = webkit_uri_request_get_uri(
1126 webkit_navigation_action_get_request(a));
1127 newwindow(c, &arg, 0);
1129 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1134 webkit_policy_decision_ignore(d);
1138 decideresource(WebKitPolicyDecision *d, Client *c)
1141 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1142 WebKitURIResponse *res =
1143 webkit_response_policy_decision_get_response(r);
1144 const gchar *uri = webkit_uri_response_get_uri(res);
1146 if (g_str_has_suffix(uri, "/favicon.ico")) {
1147 webkit_policy_decision_ignore(d);
1151 if (!g_str_has_prefix(uri, "http://")
1152 && !g_str_has_prefix(uri, "https://")
1153 && !g_str_has_prefix(uri, "about:")
1154 && !g_str_has_prefix(uri, "file://")
1155 && !g_str_has_prefix(uri, "data:")
1156 && !g_str_has_prefix(uri, "blob:")
1157 && strlen(uri) > 0) {
1158 for (i = 0; i < strlen(uri); i++) {
1159 if (!g_ascii_isprint(uri[i])) {
1165 handleplumb(c, uri);
1166 webkit_policy_decision_ignore(d);
1171 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1172 webkit_policy_decision_use(d);
1174 webkit_policy_decision_ignore(d);
1180 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1182 g_signal_connect(G_OBJECT(d), "notify::response",
1183 G_CALLBACK(responsereceived), c);
1187 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1189 download(c, webkit_download_get_response(d));
1190 webkit_download_cancel(d);
1194 download(Client *c, WebKitURIResponse *r)
1196 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1201 closeview(WebKitWebView *v, Client *c)
1203 gtk_widget_destroy(c->win);
1207 destroywin(GtkWidget* w, Client *c)
1215 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1217 Arg a = {.v = text };
1219 loaduri((Client *) d, &a);
1223 reload(Client *c, const Arg *a)
1226 webkit_web_view_reload_bypass_cache(c->view);
1228 webkit_web_view_reload(c->view);
1232 print(Client *c, const Arg *a)
1234 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1235 GTK_WINDOW(c->win));
1239 clipboard(Client *c, const Arg *a)
1241 if (a->b) { /* load clipboard uri */
1242 gtk_clipboard_request_text(gtk_clipboard_get(
1243 GDK_SELECTION_PRIMARY),
1245 } else { /* copy uri */
1246 gtk_clipboard_set_text(gtk_clipboard_get(
1247 GDK_SELECTION_PRIMARY), c->targeturi
1248 ? c->targeturi : geturi(c), -1);
1253 zoom(Client *c, const Arg *a)
1256 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1258 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1260 webkit_web_view_set_zoom_level(c->view, 1.0);
1262 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1266 scroll_v(Client *c, const Arg *a)
1268 evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1273 scroll_h(Client *c, const Arg *a)
1275 evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1280 navigate(Client *c, const Arg *a)
1283 webkit_web_view_go_back(c->view);
1285 webkit_web_view_go_forward(c->view);
1289 stop(Client *c, const Arg *a)
1291 webkit_web_view_stop_loading(c->view);
1295 toggle(Client *c, const Arg *a)
1297 WebKitSettings *s = webkit_web_view_get_settings(c->view);
1299 switch ((unsigned int)a->i) {
1301 enablecaretbrowsing = !enablecaretbrowsing;
1302 webkit_settings_set_enable_caret_browsing(s,
1303 enablecaretbrowsing);
1305 return; /* do not reload */
1307 case FrameFlattening:
1308 enableframeflattening = !enableframeflattening;
1309 webkit_settings_set_enable_frame_flattening(s,
1310 enableframeflattening);
1313 allowgeolocation = !allowgeolocation;
1316 enablescripts = !enablescripts;
1317 webkit_settings_set_enable_javascript(s, enablescripts);
1320 loadimages = !loadimages;
1321 webkit_settings_set_auto_load_images(s, loadimages);
1324 enableplugins = !enableplugins;
1325 webkit_settings_set_enable_plugins(s, enableplugins);
1328 /* Disabled until we write some WebKitWebExtension for
1329 * manipulating the DOM directly.
1330 enablescrollbars = !enablescrollbars;
1331 evalscript(c, "document.documentElement.style.overflow = '%s'",
1332 enablescrollbars ? "auto" : "hidden");
1334 return; /* do not reload */
1343 togglefullscreen(Client *c, const Arg *a)
1345 /* toggling value is handled in winevent() */
1347 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1349 gtk_window_fullscreen(GTK_WINDOW(c->win));
1353 togglecookiepolicy(Client *c, const Arg *a)
1356 cookiepolicy %= strlen(cookiepolicies);
1358 webkit_cookie_manager_set_accept_policy(
1359 webkit_web_context_get_cookie_manager(
1360 webkit_web_view_get_context(c->view)),
1361 cookiepolicy_get());
1364 /* Do not reload. */
1368 togglestyle(Client *c, const Arg *a)
1370 enablestyle = !enablestyle;
1373 setstyle(c, getstyle(geturi(c)));
1375 webkit_user_content_manager_remove_all_style_sheets(
1376 webkit_web_view_get_user_content_manager(c->view));
1382 toggleinspector(Client *c, const Arg *a)
1384 if (enableinspector) {
1385 if (webkit_web_inspector_is_attached(c->inspector))
1386 webkit_web_inspector_close(c->inspector);
1388 webkit_web_inspector_show(c->inspector);
1393 find(Client *c, const Arg *a)
1399 webkit_find_controller_search_next(c->finder);
1401 webkit_find_controller_search_previous(c->finder);
1403 s = getatom(c, AtomFind);
1404 f = webkit_find_controller_get_search_text(c->finder);
1406 if (g_strcmp0(f, s) == 0) /* reset search */
1407 webkit_find_controller_search(c->finder, "", findopts,
1410 webkit_find_controller_search(c->finder, s, findopts,
1413 if (strcmp(s, "") == 0)
1414 webkit_find_controller_search_finish(c->finder);
1419 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1425 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1429 arg.v = webkit_hit_test_result_get_link_uri(h);
1430 newwindow(c, &arg, a->b);
1434 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1438 if (webkit_hit_test_result_get_context(h) & OnMedia) {
1439 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1445 main(int argc, char *argv[])
1450 memset(&arg, 0, sizeof(arg));
1452 /* command line args */
1455 cookiepolicies = EARGF(usage());
1458 enablescrollbars = 0;
1461 enablescrollbars = 1;
1464 cookiefile = EARGF(usage());
1473 embed = strtol(EARGF(usage()), NULL, 0);
1476 runinfullscreen = 0;
1479 runinfullscreen = 1;
1482 allowgeolocation = 0;
1485 allowgeolocation = 1;
1506 enableinspector = 0;
1509 enableinspector = 1;
1518 scriptfile = EARGF(usage());
1527 stylefile = EARGF(usage());
1530 useragent = EARGF(usage());
1533 die("surf-"VERSION", ©2009-2015 surf engineers, "
1534 "see LICENSE for details\n");
1539 zoomlevel = strtof(EARGF(usage()), NULL);
1547 arg.v = "about:blank";
1550 c = newclient(NULL);