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))
34 #define SETB(p, s) [p] = (Parameter){ { .b = s }, }
35 #define SETI(p, s) [p] = (Parameter){ { .i = s }, }
36 #define SETV(p, s) [p] = (Parameter){ { .v = s }, }
37 #define SETF(p, s) [p] = (Parameter){ { .f = s }, }
38 #define FSETB(p, s) [p] = (Parameter){ { .b = s }, 1 }
39 #define FSETI(p, s) [p] = (Parameter){ { .i = s }, 1 }
40 #define FSETV(p, s) [p] = (Parameter){ { .v = s }, 1 }
41 #define FSETF(p, s) [p] = (Parameter){ { .f = s }, 1 }
43 enum { AtomFind, AtomGo, AtomUri, AtomLast };
72 OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
73 OnLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
74 OnImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
75 OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
76 OnEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
77 OnBar = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
78 OnSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
79 OnAny = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
94 typedef struct Client {
97 WebKitWebInspector *inspector;
98 WebKitFindController *finder;
99 WebKitHitTestResult *mousepos;
100 GTlsCertificateFlags tlsflags;
102 int progress, fullscreen;
103 const char *title, *overtitle, *targeturi;
111 void (*func)(Client *c, const Arg *a);
119 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
121 unsigned int stopevent;
126 Parameter config[ParameterLast];
137 static void usage(void);
138 static void die(const char *errstr, ...);
139 static void setup(void);
140 static void sigchld(int unused);
141 static char *buildfile(const char *path);
142 static char *buildpath(const char *path);
143 static Client *newclient(Client *c);
144 static void loaduri(Client *c, const Arg *a);
145 static const char *geturi(Client *c);
146 static void setatom(Client *c, int a, const char *v);
147 static const char *getatom(Client *c, int a);
148 static void updatetitle(Client *c);
149 static void gettogglestats(Client *c);
150 static void getpagestats(Client *c);
151 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
152 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
153 static const char *getstyle(const char *uri);
154 static void setstyle(Client *c, const char *stylefile);
155 static void runscript(Client *c);
156 static void evalscript(Client *c, const char *jsstr, ...);
157 static void updatewinid(Client *c);
158 static void handleplumb(Client *c, const char *uri);
159 static void newwindow(Client *c, const Arg *a, int noembed);
160 static void spawn(Client *c, const Arg *a);
161 static void destroyclient(Client *c);
162 static void cleanup(void);
165 static GdkDevice *getkbdevice(void);
166 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
167 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
169 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
170 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
172 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
173 static void showview(WebKitWebView *v, Client *c);
174 static GtkWidget *createwindow(Client *c);
175 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
176 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
177 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
178 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
179 guint modifiers, Client *c);
180 static gboolean permissionrequested(WebKitWebView *v,
181 WebKitPermissionRequest *r, Client *c);
182 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
183 WebKitPolicyDecisionType dt, Client *c);
184 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
185 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
186 static void decideresource(WebKitPolicyDecision *d, Client *c);
187 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
189 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
190 static void download(Client *c, WebKitURIResponse *r);
191 static void closeview(WebKitWebView *v, Client *c);
192 static void destroywin(GtkWidget* w, Client *c);
195 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
196 static void reload(Client *c, const Arg *a);
197 static void print(Client *c, const Arg *a);
198 static void clipboard(Client *c, const Arg *a);
199 static void zoom(Client *c, const Arg *a);
200 static void scroll(Client *c, const Arg *a);
201 static void navigate(Client *c, const Arg *a);
202 static void stop(Client *c, const Arg *a);
203 static void toggle(Client *c, const Arg *a);
204 static void togglefullscreen(Client *c, const Arg *a);
205 static void togglecookiepolicy(Client *c, const Arg *a);
206 static void togglestyle(Client *c, const Arg *a);
207 static void toggleinspector(Client *c, const Arg *a);
208 static void find(Client *c, const Arg *a);
211 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
212 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
213 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
215 static char winid[64];
216 static char togglestats[10];
217 static char pagestats[2];
218 static Atom atoms[AtomLast];
221 static int cookiepolicy;
223 static Client *clients;
224 static GdkDevice *gdkkb;
225 static char *stylefile;
226 static const char *useragent;
229 /* configuration, allows nested code to access above variables */
235 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
236 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
237 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
241 die(const char *errstr, ...)
245 va_start(ap, errstr);
246 vfprintf(stderr, errstr, ap);
256 /* clean up any zombies immediately */
258 gtk_init(NULL, NULL);
260 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
263 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
264 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
265 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
268 cookiefile = buildfile(cookiefile);
269 scriptfile = buildfile(scriptfile);
270 cachedir = buildpath(cachedir);
272 gdkkb = getkbdevice();
275 styledir = buildpath(styledir);
276 for (i = 0; i < LENGTH(styles); ++i) {
277 if (regcomp(&(styles[i].re), styles[i].regex,
280 "Could not compile regex: %s\n",
282 styles[i].regex = NULL;
284 styles[i].style = g_strconcat(styledir, "/",
285 styles[i].style, NULL);
289 stylefile = buildfile(stylefile);
296 if (signal(SIGCHLD, sigchld) == SIG_ERR)
297 die("Can't install SIGCHLD handler");
298 while (waitpid(-1, NULL, WNOHANG) > 0)
303 buildfile(const char *path)
305 char *dname, *bname, *bpath, *fpath;
308 dname = g_path_get_dirname(path);
309 bname = g_path_get_basename(path);
311 bpath = buildpath(dname);
314 fpath = g_build_filename(bpath, bname, NULL);
318 if (!(f = fopen(fpath, "a")))
319 die("Could not open file: %s\n", fpath);
321 g_chmod(fpath, 0600); /* always */
328 buildpath(const char *path)
331 char *apath, *name, *p, *fpath;
333 if (path[0] == '~') {
334 if (path[1] == '/' || path[1] == '\0') {
335 p = (char *)&path[1];
336 pw = getpwuid(getuid());
338 if ((p = strchr(path, '/')))
339 name = g_strndup(&path[1], --p - path);
341 name = g_strdup(&path[1]);
343 if (!(pw = getpwnam(name))) {
344 die("Can't get user %s home directory: %s.\n",
349 apath = g_build_filename(pw->pw_dir, p, NULL);
351 apath = g_strdup(path);
354 /* creating directory */
355 if (g_mkdir_with_parents(apath, 0700) < 0)
356 die("Could not access directory: %s\n", apath);
358 fpath = realpath(apath, NULL);
365 newclient(Client *rc)
369 if (!(c = calloc(1, sizeof(Client))))
370 die("Cannot malloc!\n");
376 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
377 c->view = newview(c, rc ? rc->view : NULL);
383 loaduri(Client *c, const Arg *a)
387 const char *uri = a->v;
389 if (g_strcmp0(uri, "") == 0)
392 if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
394 } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
395 url = g_strdup_printf("file://%s", path);
398 url = g_strdup_printf("http://%s", uri);
401 setatom(c, AtomUri, url);
403 if (strcmp(url, geturi(c)) == 0) {
406 webkit_web_view_load_uri(c->view, url);
418 if (!(uri = webkit_web_view_get_uri(c->view)))
424 setatom(Client *c, int a, const char *v)
427 XChangeProperty(dpy, c->xid,
428 atoms[a], XA_STRING, 8, PropModeReplace,
429 (unsigned char *)v, strlen(v) + 1);
433 getatom(Client *c, int a)
435 static char buf[BUFSIZ];
438 unsigned long ldummy;
439 unsigned char *p = NULL;
441 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
442 &adummy, &idummy, &ldummy, &ldummy, &p);
444 strncpy(buf, (char *)p, LENGTH(buf) - 1);
453 updatetitle(Client *c)
456 const char *name = c->overtitle ? c->overtitle :
457 c->title ? c->title : "";
459 if (showindicators) {
463 if (c->progress != 100)
464 title = g_strdup_printf("[%i%%] %s:%s | %s",
465 c->progress, togglestats, pagestats, name);
467 title = g_strdup_printf("%s:%s | %s",
468 togglestats, pagestats, name);
470 gtk_window_set_title(GTK_WINDOW(c->win), title);
473 gtk_window_set_title(GTK_WINDOW(c->win), name);
478 gettogglestats(Client *c)
480 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
481 togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
482 togglestats[2] = allowgeolocation ? 'G' : 'g';
483 togglestats[3] = enablecache ? 'D' : 'd';
484 togglestats[4] = loadimages ? 'I' : 'i';
485 togglestats[5] = enablescripts ? 'S' : 's';
486 togglestats[6] = enableplugins ? 'V' : 'v';
487 togglestats[7] = enablestyle ? 'M' : 'm';
488 togglestats[8] = enableframeflattening ? 'F' : 'f';
489 togglestats[9] = '\0';
493 getpagestats(Client *c)
495 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
496 c->tlsflags > 0 ? 'U' : 'T';
500 WebKitCookieAcceptPolicy
501 cookiepolicy_get(void)
503 switch (cookiepolicies[cookiepolicy]) {
505 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
507 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
508 default: /* fallthrough */
510 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
516 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
519 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
521 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
523 default: /* fallthrough */
524 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
531 getstyle(const char *uri)
538 for (i = 0; i < LENGTH(styles); ++i) {
539 if (styles[i].regex &&
540 !regexec(&(styles[i].re), uri, 0, NULL, 0))
541 return styles[i].style;
548 setstyle(Client *c, const char *stylefile)
552 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
553 fprintf(stderr, "Could not read style file: %s\n", stylefile);
557 webkit_user_content_manager_add_style_sheet(
558 webkit_web_view_get_user_content_manager(c->view),
559 webkit_user_style_sheet_new(style,
560 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
561 WEBKIT_USER_STYLE_LEVEL_USER,
573 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
574 evalscript(c, script);
579 evalscript(Client *c, const char *jsstr, ...)
585 script = g_strdup_vprintf(jsstr, ap);
588 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
593 updatewinid(Client *c)
595 snprintf(winid, LENGTH(winid), "%lu", c->xid);
599 handleplumb(Client *c, const char *uri)
601 Arg a = (Arg)PLUMB(uri);
606 newwindow(Client *c, const Arg *a, int noembed)
610 const char *cmd[26], *uri;
611 const Arg arg = { .v = cmd };
615 cmd[i++] = cookiepolicies;
616 cmd[i++] = enablescrollbars ? "-B" : "-b";
617 if (cookiefile && g_strcmp0(cookiefile, "")) {
619 cmd[i++] = cookiefile;
621 cmd[i++] = enablecache ? "-D" : "-d";
622 if (embed && !noembed) {
624 snprintf(tmp, LENGTH(tmp), "%lu", embed);
627 cmd[i++] = runinfullscreen ? "-F" : "-f";
628 cmd[i++] = allowgeolocation ? "-G" : "-g";
629 cmd[i++] = loadimages ? "-I" : "-i";
630 cmd[i++] = kioskmode ? "-K" : "-k";
631 cmd[i++] = enablestyle ? "-M" : "-m";
632 cmd[i++] = enableinspector ? "-N" : "-n";
633 cmd[i++] = enableplugins ? "-P" : "-p";
634 if (scriptfile && g_strcmp0(scriptfile, "")) {
636 cmd[i++] = scriptfile;
638 cmd[i++] = enablescripts ? "-S" : "-s";
639 if (stylefile && g_strcmp0(stylefile, "")) {
641 cmd[i++] = stylefile;
643 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
645 cmd[i++] = fulluseragent;
649 /* do not keep zoom level */
659 spawn(Client *c, const Arg *a)
663 close(ConnectionNumber(dpy));
665 execvp(((char **)a->v)[0], (char **)a->v);
666 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
673 destroyclient(Client *c)
677 webkit_web_view_stop_loading(c->view);
678 /* Not needed, has already been called
679 gtk_widget_destroy(c->win);
682 for (p = clients; p && p->next != c; p = p->next)
695 destroyclient(clients);
705 GList *l, *gdl = gdk_device_manager_list_devices(
706 gdk_display_get_device_manager(gdk_display_get_default()),
707 GDK_DEVICE_TYPE_MASTER);
708 GdkDevice *gd = NULL;
710 for (l = gdl; l != NULL; l = l->next)
711 if (gdk_device_get_source(l->data) == GDK_SOURCE_KEYBOARD)
719 newview(Client *c, WebKitWebView *rv)
722 WebKitSettings *settings;
723 WebKitUserContentManager *contentmanager;
724 WebKitWebContext *context;
729 webkit_web_view_new_with_related_view(rv));
731 settings = webkit_settings_new_with_settings(
732 "auto-load-images", loadimages,
733 "default-font-size", defaultfontsize,
734 "enable-caret-browsing", enablecaretbrowsing,
735 "enable-developer-extras", enableinspector,
736 "enable-dns-prefetching", enablednsprefetching,
737 "enable-frame-flattening", enableframeflattening,
738 "enable-html5-database", enablecache,
739 "enable-html5-local-storage", enablecache,
740 "enable-javascript", enablescripts,
741 "enable-plugins", enableplugins,
743 /* For mor interesting settings, have a look at
744 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
746 if (strcmp(fulluseragent, "")) {
747 webkit_settings_set_user_agent(settings, fulluseragent);
748 } else if (surfuseragent) {
749 webkit_settings_set_user_agent_with_application_details(
750 settings, "Surf", VERSION);
752 useragent = webkit_settings_get_user_agent(settings);
754 contentmanager = webkit_user_content_manager_new();
756 context = webkit_web_context_new_with_website_data_manager(
757 webkit_website_data_manager_new(
758 "base-cache-directory", cachedir,
759 "base-data-directory", cachedir,
762 /* rendering process model, can be a shared unique one
763 * or one for each view */
764 webkit_web_context_set_process_model(context,
765 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
767 webkit_web_context_set_tls_errors_policy(context, strictssl ?
768 WEBKIT_TLS_ERRORS_POLICY_FAIL :
769 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
771 webkit_web_context_set_cache_model(context, enablecache ?
772 WEBKIT_CACHE_MODEL_WEB_BROWSER :
773 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
775 /* Currently only works with text file to be compatible with curl */
776 webkit_cookie_manager_set_persistent_storage(
777 webkit_web_context_get_cookie_manager(context), cookiefile,
778 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
780 webkit_cookie_manager_set_accept_policy(
781 webkit_web_context_get_cookie_manager(context),
784 webkit_web_context_set_preferred_languages(context,
786 webkit_web_context_set_spell_checking_languages(context,
788 webkit_web_context_set_spell_checking_enabled(context,
789 enablespellchecking);
791 g_signal_connect(G_OBJECT(context), "download-started",
792 G_CALLBACK(downloadstarted), c);
794 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
795 "settings", settings,
796 "user-content-manager", contentmanager,
797 "web-context", context,
801 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
802 G_CALLBACK(progresschanged), c);
803 g_signal_connect(G_OBJECT(v), "notify::title",
804 G_CALLBACK(titlechanged), c);
805 g_signal_connect(G_OBJECT(v), "button-release-event",
806 G_CALLBACK(buttonreleased), c);
807 g_signal_connect(G_OBJECT(v), "close",
808 G_CALLBACK(closeview), c);
809 g_signal_connect(G_OBJECT(v), "create",
810 G_CALLBACK(createview), c);
811 g_signal_connect(G_OBJECT(v), "decide-policy",
812 G_CALLBACK(decidepolicy), c);
813 g_signal_connect(G_OBJECT(v), "load-changed",
814 G_CALLBACK(loadchanged), c);
815 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
816 G_CALLBACK(mousetargetchanged), c);
817 g_signal_connect(G_OBJECT(v), "permission-request",
818 G_CALLBACK(permissionrequested), c);
819 g_signal_connect(G_OBJECT(v), "ready-to-show",
820 G_CALLBACK(showview), c);
826 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
830 switch (webkit_navigation_action_get_navigation_type(a)) {
831 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
833 * popup windows of type “other” are almost always triggered
834 * by user gesture, so inverse the logic here
836 /* instead of this, compare destination uri to mouse-over uri for validating window */
837 if (webkit_navigation_action_is_user_gesture(a))
839 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
840 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
841 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
842 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
843 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
850 return GTK_WIDGET(n->view);
854 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
856 WebKitHitTestResultContext element;
859 element = webkit_hit_test_result_get_context(c->mousepos);
861 for (i = 0; i < LENGTH(buttons); ++i) {
862 if (element & buttons[i].target &&
863 e->button.button == buttons[i].button &&
864 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
866 buttons[i].func(c, &buttons[i].arg, c->mousepos);
867 return buttons[i].stopevent;
875 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
877 Client *c = (Client *)d;
881 if (((XEvent *)e)->type == PropertyNotify) {
882 ev = &((XEvent *)e)->xproperty;
883 if (ev->state == PropertyNewValue) {
884 if (ev->atom == atoms[AtomFind]) {
887 return GDK_FILTER_REMOVE;
888 } else if (ev->atom == atoms[AtomGo]) {
889 a.v = getatom(c, AtomGo);
892 return GDK_FILTER_REMOVE;
896 return GDK_FILTER_CONTINUE;
900 winevent(GtkWidget *w, GdkEvent *e, Client *c)
905 case GDK_ENTER_NOTIFY:
906 c->overtitle = c->targeturi;
911 for (i = 0; i < LENGTH(keys); ++i) {
912 if (gdk_keyval_to_lower(e->key.keyval) ==
914 CLEANMASK(e->key.state) == keys[i].mod &&
917 keys[i].func(c, &(keys[i].arg));
922 case GDK_LEAVE_NOTIFY:
926 case GDK_WINDOW_STATE:
927 if (e->window_state.changed_mask ==
928 GDK_WINDOW_STATE_FULLSCREEN)
929 c->fullscreen = e->window_state.new_window_state &
930 GDK_WINDOW_STATE_FULLSCREEN;
940 showview(WebKitWebView *v, Client *c)
942 GdkRGBA bgcolor = { 0 };
945 c->finder = webkit_web_view_get_find_controller(c->view);
947 c->inspector = webkit_web_view_get_inspector(c->view);
949 c->win = createwindow(c);
951 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
952 gtk_widget_show_all(c->win);
953 gtk_widget_grab_focus(GTK_WIDGET(c->view));
955 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
956 c->xid = gdk_x11_window_get_xid(gwin);
959 gdk_display_sync(gtk_widget_get_display(c->win));
964 webkit_web_view_set_background_color(c->view, &bgcolor);
967 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
968 gdk_window_add_filter(gwin, processx, c);
972 togglefullscreen(c, NULL);
974 if (zoomlevel != 1.0)
975 webkit_web_view_set_zoom_level(c->view, zoomlevel);
977 setatom(c, AtomFind, "");
978 setatom(c, AtomUri, "about:blank");
982 createwindow(Client *c)
988 w = gtk_plug_new(embed);
990 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
992 wmstr = g_path_get_basename(argv0);
993 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
996 wmstr = g_strdup_printf("%s[%lu]", "Surf",
997 webkit_web_view_get_page_id(c->view));
998 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1001 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
1004 g_signal_connect(G_OBJECT(w), "destroy",
1005 G_CALLBACK(destroywin), c);
1006 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1007 G_CALLBACK(winevent), c);
1008 g_signal_connect(G_OBJECT(w), "key-press-event",
1009 G_CALLBACK(winevent), c);
1010 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1011 G_CALLBACK(winevent), c);
1012 g_signal_connect(G_OBJECT(w), "window-state-event",
1013 G_CALLBACK(winevent), c);
1019 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1021 const char *title = geturi(c);
1024 case WEBKIT_LOAD_STARTED:
1025 setatom(c, AtomUri, title);
1027 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
1029 case WEBKIT_LOAD_REDIRECTED:
1030 setatom(c, AtomUri, title);
1033 case WEBKIT_LOAD_COMMITTED:
1034 setatom(c, AtomUri, title);
1036 if (!webkit_web_view_get_tls_info(c->view, NULL,
1038 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
1041 setstyle(c, getstyle(geturi(c)));
1043 case WEBKIT_LOAD_FINISHED:
1044 /* Disabled until we write some WebKitWebExtension for
1045 * manipulating the DOM directly.
1046 evalscript(c, "document.documentElement.style.overflow = '%s'",
1047 enablescrollbars ? "auto" : "hidden");
1056 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1058 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1064 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1066 c->title = webkit_web_view_get_title(c->view);
1071 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1074 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1076 /* Keep the hit test to know where is the pointer on the next click */
1080 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1081 else if (hc & OnImg)
1082 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1083 else if (hc & OnMedia)
1084 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1086 c->targeturi = NULL;
1088 c->overtitle = c->targeturi;
1093 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1095 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1096 if (allowgeolocation)
1097 webkit_permission_request_allow(r);
1099 webkit_permission_request_deny(r);
1107 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1108 WebKitPolicyDecisionType dt, Client *c)
1111 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1112 decidenavigation(d, c);
1114 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1115 decidenewwindow(d, c);
1117 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1118 decideresource(d, c);
1121 webkit_policy_decision_ignore(d);
1128 decidenavigation(WebKitPolicyDecision *d, Client *c)
1130 WebKitNavigationAction *a =
1131 webkit_navigation_policy_decision_get_navigation_action(
1132 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1134 switch (webkit_navigation_action_get_navigation_type(a)) {
1135 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1136 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1137 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1138 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1139 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1140 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1142 /* Do not navigate to links with a "_blank" target (popup) */
1143 if (webkit_navigation_policy_decision_get_frame_name(
1144 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1145 webkit_policy_decision_ignore(d);
1147 /* Filter out navigation to different domain ? */
1148 /* get action→urirequest, copy and load in new window+view
1149 * on Ctrl+Click ? */
1150 webkit_policy_decision_use(d);
1157 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1160 WebKitNavigationAction *a =
1161 webkit_navigation_policy_decision_get_navigation_action(
1162 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1165 switch (webkit_navigation_action_get_navigation_type(a)) {
1166 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1167 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1168 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1169 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1170 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1171 /* Filter domains here */
1172 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1173 * test for link clicked but no button ? */
1174 arg.v = webkit_uri_request_get_uri(
1175 webkit_navigation_action_get_request(a));
1176 newwindow(c, &arg, 0);
1178 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1183 webkit_policy_decision_ignore(d);
1187 decideresource(WebKitPolicyDecision *d, Client *c)
1190 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1191 WebKitURIResponse *res =
1192 webkit_response_policy_decision_get_response(r);
1193 const gchar *uri = webkit_uri_response_get_uri(res);
1195 if (g_str_has_suffix(uri, "/favicon.ico")) {
1196 webkit_policy_decision_ignore(d);
1200 if (!g_str_has_prefix(uri, "http://")
1201 && !g_str_has_prefix(uri, "https://")
1202 && !g_str_has_prefix(uri, "about:")
1203 && !g_str_has_prefix(uri, "file://")
1204 && !g_str_has_prefix(uri, "data:")
1205 && !g_str_has_prefix(uri, "blob:")
1206 && strlen(uri) > 0) {
1207 for (i = 0; i < strlen(uri); i++) {
1208 if (!g_ascii_isprint(uri[i])) {
1214 handleplumb(c, uri);
1215 webkit_policy_decision_ignore(d);
1220 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1221 webkit_policy_decision_use(d);
1223 webkit_policy_decision_ignore(d);
1229 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1231 g_signal_connect(G_OBJECT(d), "notify::response",
1232 G_CALLBACK(responsereceived), c);
1236 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1238 download(c, webkit_download_get_response(d));
1239 webkit_download_cancel(d);
1243 download(Client *c, WebKitURIResponse *r)
1245 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1250 closeview(WebKitWebView *v, Client *c)
1252 gtk_widget_destroy(c->win);
1256 destroywin(GtkWidget* w, Client *c)
1264 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1266 Arg a = {.v = text };
1268 loaduri((Client *) d, &a);
1272 reload(Client *c, const Arg *a)
1275 webkit_web_view_reload_bypass_cache(c->view);
1277 webkit_web_view_reload(c->view);
1281 print(Client *c, const Arg *a)
1283 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1284 GTK_WINDOW(c->win));
1288 clipboard(Client *c, const Arg *a)
1290 if (a->b) { /* load clipboard uri */
1291 gtk_clipboard_request_text(gtk_clipboard_get(
1292 GDK_SELECTION_PRIMARY),
1294 } else { /* copy uri */
1295 gtk_clipboard_set_text(gtk_clipboard_get(
1296 GDK_SELECTION_PRIMARY), c->targeturi
1297 ? c->targeturi : geturi(c), -1);
1302 zoom(Client *c, const Arg *a)
1305 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1307 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1309 webkit_web_view_set_zoom_level(c->view, 1.0);
1311 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1315 scroll(Client *c, const Arg *a)
1317 GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
1319 gdk_event_set_device(ev, gdkkb);
1320 // gdk_event_set_screen(ev, gdk_screen_get_default());
1321 ev->key.window = gtk_widget_get_window(GTK_WIDGET(c->win));
1322 ev->key.state = GDK_CONTROL_MASK;
1323 ev->key.time = GDK_CURRENT_TIME;
1327 ev->key.keyval = GDK_KEY_Down;
1330 ev->key.keyval = GDK_KEY_Page_Down;
1333 ev->key.keyval = GDK_KEY_Left;
1336 ev->key.keyval = GDK_KEY_Right;
1339 ev->key.keyval = GDK_KEY_Page_Up;
1342 ev->key.keyval = GDK_KEY_Up;
1350 navigate(Client *c, const Arg *a)
1353 webkit_web_view_go_back(c->view);
1355 webkit_web_view_go_forward(c->view);
1359 stop(Client *c, const Arg *a)
1361 webkit_web_view_stop_loading(c->view);
1365 toggle(Client *c, const Arg *a)
1367 WebKitSettings *s = webkit_web_view_get_settings(c->view);
1369 switch ((unsigned int)a->i) {
1371 enablecaretbrowsing = !enablecaretbrowsing;
1372 webkit_settings_set_enable_caret_browsing(s,
1373 enablecaretbrowsing);
1375 return; /* do not reload */
1377 case FrameFlattening:
1378 enableframeflattening = !enableframeflattening;
1379 webkit_settings_set_enable_frame_flattening(s,
1380 enableframeflattening);
1383 allowgeolocation = !allowgeolocation;
1386 enablescripts = !enablescripts;
1387 webkit_settings_set_enable_javascript(s, enablescripts);
1390 loadimages = !loadimages;
1391 webkit_settings_set_auto_load_images(s, loadimages);
1394 enableplugins = !enableplugins;
1395 webkit_settings_set_enable_plugins(s, enableplugins);
1398 /* Disabled until we write some WebKitWebExtension for
1399 * manipulating the DOM directly.
1400 enablescrollbars = !enablescrollbars;
1401 evalscript(c, "document.documentElement.style.overflow = '%s'",
1402 enablescrollbars ? "auto" : "hidden");
1404 return; /* do not reload */
1413 togglefullscreen(Client *c, const Arg *a)
1415 /* toggling value is handled in winevent() */
1417 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1419 gtk_window_fullscreen(GTK_WINDOW(c->win));
1423 togglecookiepolicy(Client *c, const Arg *a)
1426 cookiepolicy %= strlen(cookiepolicies);
1428 webkit_cookie_manager_set_accept_policy(
1429 webkit_web_context_get_cookie_manager(
1430 webkit_web_view_get_context(c->view)),
1431 cookiepolicy_get());
1434 /* Do not reload. */
1438 togglestyle(Client *c, const Arg *a)
1440 enablestyle = !enablestyle;
1443 setstyle(c, getstyle(geturi(c)));
1445 webkit_user_content_manager_remove_all_style_sheets(
1446 webkit_web_view_get_user_content_manager(c->view));
1452 toggleinspector(Client *c, const Arg *a)
1454 if (enableinspector) {
1455 if (webkit_web_inspector_is_attached(c->inspector))
1456 webkit_web_inspector_close(c->inspector);
1458 webkit_web_inspector_show(c->inspector);
1463 find(Client *c, const Arg *a)
1469 webkit_find_controller_search_next(c->finder);
1471 webkit_find_controller_search_previous(c->finder);
1473 s = getatom(c, AtomFind);
1474 f = webkit_find_controller_get_search_text(c->finder);
1476 if (g_strcmp0(f, s) == 0) /* reset search */
1477 webkit_find_controller_search(c->finder, "", findopts,
1480 webkit_find_controller_search(c->finder, s, findopts,
1483 if (strcmp(s, "") == 0)
1484 webkit_find_controller_search_finish(c->finder);
1489 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1495 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1499 arg.v = webkit_hit_test_result_get_link_uri(h);
1500 newwindow(c, &arg, a->b);
1504 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1508 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1513 main(int argc, char *argv[])
1518 memset(&arg, 0, sizeof(arg));
1520 /* command line args */
1523 cookiepolicies = EARGF(usage());
1526 enablescrollbars = 0;
1529 enablescrollbars = 1;
1532 cookiefile = EARGF(usage());
1541 embed = strtol(EARGF(usage()), NULL, 0);
1544 runinfullscreen = 0;
1547 runinfullscreen = 1;
1550 allowgeolocation = 0;
1553 allowgeolocation = 1;
1574 enableinspector = 0;
1577 enableinspector = 1;
1586 scriptfile = EARGF(usage());
1595 stylefile = EARGF(usage());
1598 fulluseragent = EARGF(usage());
1601 die("surf-"VERSION", ©2009-2015 surf engineers, "
1602 "see LICENSE for details\n");
1607 zoomlevel = strtof(EARGF(usage()), NULL);
1615 arg.v = "about:blank";
1618 c = newclient(NULL);