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;
74 const char *title, *targeturi;
82 void (*func)(Client *c, const Arg *a);
90 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
92 unsigned int stopevent;
102 static void usage(void);
103 static void die(const char *errstr, ...);
104 static void setup(void);
105 static void sigchld(int unused);
106 static char *buildfile(const char *path);
107 static char *buildpath(const char *path);
108 static Client *newclient(Client *c);
109 static void addaccelgroup(Client *c);
110 static void loaduri(Client *c, const Arg *a);
111 static char *geturi(Client *c);
112 static void setatom(Client *c, int a, const char *v);
113 static const char *getatom(Client *c, int a);
114 static void updatetitle(Client *c);
115 static void gettogglestats(Client *c);
116 static void getpagestats(Client *c);
117 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
118 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
119 static const gchar *getstyle(const char *uri);
120 static void setstyle(Client *c, const char *stylefile);
121 static void runscript(Client *c);
122 static void evalscript(Client *c, const char *jsstr, ...);
123 static void updatewinid(Client *c);
124 static void handleplumb(Client *c, const gchar *uri);
125 static void newwindow(Client *c, const Arg *a, gboolean noembed);
126 static void spawn(Client *c, const Arg *a);
127 static void destroyclient(Client *c);
128 static void cleanup(void);
131 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
132 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
134 static gboolean buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c);
135 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
136 GdkModifierType mods, Client *c);
137 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
139 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
140 static void showview(WebKitWebView *v, Client *c);
141 static GtkWidget *createwindow(Client *c);
142 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
143 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
144 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
145 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
146 guint modifiers, Client *c);
147 static gboolean permissionrequested(WebKitWebView *v,
148 WebKitPermissionRequest *r, Client *c);
149 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
150 WebKitPolicyDecisionType dt, Client *c);
151 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
152 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
153 static void decideresource(WebKitPolicyDecision *d, Client *c);
154 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
156 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
157 static void download(Client *c, WebKitURIResponse *r);
158 static void closeview(WebKitWebView *v, Client *c);
159 static void destroywin(GtkWidget* w, Client *c);
162 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
163 static void reload(Client *c, const Arg *a);
164 static void print(Client *c, const Arg *a);
165 static void clipboard(Client *c, const Arg *a);
166 static void zoom(Client *c, const Arg *a);
167 static void scroll_v(Client *c, const Arg *a);
168 static void scroll_h(Client *c, const Arg *a);
169 static void navigate(Client *c, const Arg *a);
170 static void stop(Client *c, const Arg *a);
171 static void toggle(Client *c, const Arg *a);
172 static void togglefullscreen(Client *c, const Arg *a);
173 static void togglecookiepolicy(Client *c, const Arg *a);
174 static void togglestyle(Client *c, const Arg *a);
175 static void toggleinspector(Client *c, const Arg *a);
176 static void find(Client *c, const Arg *a);
179 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
180 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
181 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
183 static char winid[64];
184 static char togglestats[10];
185 static char pagestats[2];
186 static Atom atoms[AtomLast];
188 static gboolean showxid = FALSE;
189 static int cookiepolicy;
191 static Client *clients;
192 static char *stylefile;
193 static const char *useragent;
196 /* configuration, allows nested code to access above variables */
202 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
203 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
204 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
208 die(const char *errstr, ...)
212 va_start(ap, errstr);
213 vfprintf(stderr, errstr, ap);
223 /* clean up any zombies immediately */
225 gtk_init(NULL, NULL);
227 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
230 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
231 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
232 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
235 cookiefile = buildfile(cookiefile);
236 scriptfile = buildfile(scriptfile);
237 cachedir = buildpath(cachedir);
240 styledir = buildpath(styledir);
241 for (i = 0; i < LENGTH(styles); ++i) {
242 if (regcomp(&(styles[i].re), styles[i].regex,
245 "Could not compile regex: %s\n",
247 styles[i].regex = NULL;
249 styles[i].style = g_strconcat(styledir, "/",
250 styles[i].style, NULL);
254 stylefile = buildfile(stylefile);
261 if (signal(SIGCHLD, sigchld) == SIG_ERR)
262 die("Can't install SIGCHLD handler");
263 while (0 < waitpid(-1, NULL, WNOHANG));
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");
342 c->view = newview(c, rc ? rc->view : NULL);
343 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
349 addaccelgroup(Client *c)
352 GtkAccelGroup *group = gtk_accel_group_new();
355 for (i = 0; i < LENGTH(keys); i++) {
356 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
357 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
360 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
364 loaduri(Client *c, const Arg *a)
368 const char *uri = a->v;
370 if (g_strcmp0(uri, "") == 0)
373 if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
375 } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
376 url = g_strdup_printf("file://%s", path);
379 url = g_strdup_printf("http://%s", uri);
382 setatom(c, AtomUri, url);
384 if (strcmp(url, geturi(c)) == 0) {
387 webkit_web_view_load_uri(c->view, url);
388 c->title = geturi(c);
400 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
406 setatom(Client *c, int a, const char *v)
409 XChangeProperty(dpy, c->xid,
410 atoms[a], XA_STRING, 8, PropModeReplace,
411 (unsigned char *)v, strlen(v) + 1);
415 getatom(Client *c, int a)
417 static char buf[BUFSIZ];
420 unsigned long ldummy;
421 unsigned char *p = NULL;
423 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
424 &adummy, &idummy, &ldummy, &ldummy, &p);
426 strncpy(buf, (char *)p, LENGTH(buf) - 1);
435 updatetitle(Client *c)
439 if (showindicators) {
443 if (c->progress != 100) {
444 title = g_strdup_printf("[%i%%] %s:%s | %s",
445 c->progress, togglestats, pagestats,
446 c->targeturi ? c->targeturi : c->title);
448 title = g_strdup_printf("%s:%s | %s",
449 togglestats, pagestats,
450 c->targeturi ? c->targeturi : c->title);
453 gtk_window_set_title(GTK_WINDOW(c->win), title);
456 gtk_window_set_title(GTK_WINDOW(c->win), c->title ?
462 gettogglestats(Client *c)
464 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
465 togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
466 togglestats[2] = allowgeolocation ? 'G' : 'g';
467 togglestats[3] = enablecache ? 'D' : 'd';
468 togglestats[4] = loadimages ? 'I' : 'i';
469 togglestats[5] = enablescripts ? 'S' : 's';
470 togglestats[6] = enableplugins ? 'V' : 'v';
471 togglestats[7] = enablestyle ? 'M' : 'm';
472 togglestats[8] = enableframeflattening ? 'F' : 'f';
473 togglestats[9] = '\0';
477 getpagestats(Client *c)
479 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
480 c->tlsflags > 0 ? 'U' : 'T';
484 WebKitCookieAcceptPolicy
485 cookiepolicy_get(void)
487 switch (cookiepolicies[cookiepolicy]) {
489 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
491 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
497 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
501 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
504 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
506 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
508 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
517 getstyle(const char *uri)
524 for (i = 0; i < LENGTH(styles); ++i) {
525 if (styles[i].regex &&
526 !regexec(&(styles[i].re), uri, 0, NULL, 0))
527 return styles[i].style;
534 setstyle(Client *c, const char *stylefile)
538 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
539 fprintf(stderr, "Could not read style file: %s\n", stylefile);
543 webkit_user_content_manager_add_style_sheet(
544 webkit_web_view_get_user_content_manager(c->view),
545 webkit_user_style_sheet_new(style,
546 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
547 WEBKIT_USER_STYLE_LEVEL_USER,
559 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
560 evalscript(c, script);
565 evalscript(Client *c, const char *jsstr, ...)
571 script = g_strdup_vprintf(jsstr, ap);
574 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
579 updatewinid(Client *c)
581 snprintf(winid, LENGTH(winid), "%lu", c->xid);
585 handleplumb(Client *c, const gchar *uri)
587 Arg a = (Arg)PLUMB(uri);
592 newwindow(Client *c, const Arg *a, int noembed)
596 const char *cmd[26], *uri;
597 const Arg arg = { .v = cmd };
601 cmd[i++] = cookiepolicies;
602 cmd[i++] = enablescrollbars ? "-B" : "-b";
603 if (cookiefile && g_strcmp0(cookiefile, "")) {
605 cmd[i++] = cookiefile;
607 cmd[i++] = enablecache ? "-D" : "-d";
608 if (embed && !noembed) {
610 snprintf(tmp, LENGTH(tmp), "%lu", embed);
613 cmd[i++] = runinfullscreen ? "-F" : "-f";
614 cmd[i++] = allowgeolocation ? "-G" : "-g";
615 cmd[i++] = loadimages ? "-I" : "-i";
616 cmd[i++] = kioskmode ? "-K" : "-k";
617 cmd[i++] = enablestyle ? "-M" : "-m";
618 cmd[i++] = enableinspector ? "-N" : "-n";
619 cmd[i++] = enableplugins ? "-P" : "-p";
620 if (scriptfile && g_strcmp0(scriptfile, "")) {
622 cmd[i++] = scriptfile;
624 cmd[i++] = enablescripts ? "-S" : "-s";
625 if (stylefile && g_strcmp0(stylefile, "")) {
627 cmd[i++] = stylefile;
629 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
631 cmd[i++] = fulluseragent;
635 /* do not keep zoom level */
645 spawn(Client *c, const Arg *a)
649 close(ConnectionNumber(dpy));
651 execvp(((char **)a->v)[0], (char **)a->v);
652 fprintf(stderr, "surf: execvp %s", ((char **)a->v)[0]);
659 destroyclient(Client *c)
663 webkit_web_view_stop_loading(c->view);
664 /* Not needed, has already been called
665 gtk_widget_destroy(c->win);
668 for (p = clients; p && p->next != c; p = p->next)
681 destroyclient(clients);
689 newview(Client *c, WebKitWebView *rv)
692 WebKitSettings *settings;
693 WebKitUserContentManager *contentmanager;
694 WebKitWebContext *context;
699 webkit_web_view_new_with_related_view(rv));
701 settings = webkit_settings_new_with_settings(
702 "auto-load-images", loadimages,
703 "default-font-size", defaultfontsize,
704 "enable-caret-browsing", enablecaretbrowsing,
705 "enable-developer-extras", enableinspector,
706 "enable-dns-prefetching", enablednsprefetching,
707 "enable-frame-flattening", enableframeflattening,
708 "enable-html5-database", enablecache,
709 "enable-html5-local-storage", enablecache,
710 "enable-javascript", enablescripts,
711 "enable-plugins", enableplugins,
713 /* For mor interesting settings, have a look at
714 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
716 if (strcmp(fulluseragent, "")) {
717 webkit_settings_set_user_agent(settings, fulluseragent);
718 } else if (surfuseragent) {
719 webkit_settings_set_user_agent_with_application_details(
720 settings, "Surf", VERSION);
722 useragent = webkit_settings_get_user_agent(settings);
724 contentmanager = webkit_user_content_manager_new();
726 context = webkit_web_context_new_with_website_data_manager(
727 webkit_website_data_manager_new(
728 "base-cache-directory", cachedir,
729 "base-data-directory", cachedir,
732 /* rendering process model, can be a shared unique one
733 * or one for each view */
734 webkit_web_context_set_process_model(context,
735 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
737 webkit_web_context_set_tls_errors_policy(context, strictssl ?
738 WEBKIT_TLS_ERRORS_POLICY_FAIL :
739 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
741 webkit_web_context_set_cache_model(context, enablecache ?
742 WEBKIT_CACHE_MODEL_WEB_BROWSER :
743 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
745 /* Currently only works with text file to be compatible with curl */
746 webkit_cookie_manager_set_persistent_storage(
747 webkit_web_context_get_cookie_manager(context), cookiefile,
748 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
750 webkit_cookie_manager_set_accept_policy(
751 webkit_web_context_get_cookie_manager(context),
754 g_signal_connect(G_OBJECT(context), "download-started",
755 G_CALLBACK(downloadstarted), c);
757 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
758 "settings", settings,
759 "user-content-manager", contentmanager,
760 "web-context", context,
764 g_signal_connect(G_OBJECT(v), "notify::title",
765 G_CALLBACK(titlechanged), c);
766 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
767 G_CALLBACK(mousetargetchanged), c);
768 g_signal_connect(G_OBJECT(v), "permission-request",
769 G_CALLBACK(permissionrequested), c);
770 g_signal_connect(G_OBJECT(v), "create",
771 G_CALLBACK(createview), c);
772 g_signal_connect(G_OBJECT(v), "ready-to-show",
773 G_CALLBACK(showview), c);
774 g_signal_connect(G_OBJECT(v), "decide-policy",
775 G_CALLBACK(decidepolicy), c);
776 g_signal_connect(G_OBJECT(v), "load-changed",
777 G_CALLBACK(loadchanged), c);
778 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
779 G_CALLBACK(progresschanged), c);
780 g_signal_connect(G_OBJECT(v), "button-release-event",
781 G_CALLBACK(buttonreleased), c);
782 g_signal_connect(G_OBJECT(v), "close",
783 G_CALLBACK(closeview), c);
789 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
793 switch (webkit_navigation_action_get_navigation_type(a)) {
794 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
796 * popup windows of type “other” are almost always triggered
797 * by user gesture, so inverse the logic here
799 /* instead of this, compare destination uri to mouse-over uri for validating window */
800 if (webkit_navigation_action_is_user_gesture(a))
802 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
803 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
804 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
805 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
806 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
813 return GTK_WIDGET(n->view);
817 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
819 WebKitHitTestResultContext element;
820 GdkEventButton *eb = (GdkEventButton*)e;
823 element = webkit_hit_test_result_get_context(c->mousepos);
825 for (i = 0; i < LENGTH(buttons); ++i) {
826 if (element & buttons[i].target &&
827 eb->button == buttons[i].button &&
828 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
830 buttons[i].func(c, &buttons[i].arg, c->mousepos);
831 return buttons[i].stopevent;
839 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
843 gboolean processed = FALSE;
845 mods = CLEANMASK(mods);
846 key = gdk_keyval_to_lower(key);
848 for (i = 0; i < LENGTH(keys); i++) {
849 if (key == keys[i].keyval
850 && mods == keys[i].mod
852 keys[i].func(c, &(keys[i].arg));
861 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
863 Client *c = (Client *)d;
867 if (((XEvent *)e)->type == PropertyNotify) {
868 ev = &((XEvent *)e)->xproperty;
869 if (ev->state == PropertyNewValue) {
870 if (ev->atom == atoms[AtomFind]) {
873 return GDK_FILTER_REMOVE;
874 } else if (ev->atom == atoms[AtomGo]) {
875 a.v = getatom(c, AtomGo);
878 return GDK_FILTER_REMOVE;
882 return GDK_FILTER_CONTINUE;
886 winevent(GtkWidget *w, GdkEvent *e, Client *c)
889 case GDK_LEAVE_NOTIFY:
893 case GDK_WINDOW_STATE: /* fallthrough */
894 if (e->window_state.changed_mask ==
895 GDK_WINDOW_STATE_FULLSCREEN) {
896 c->fullscreen = e->window_state.new_window_state &
897 GDK_WINDOW_STATE_FULLSCREEN;
908 showview(WebKitWebView *v, Client *c)
910 GdkGeometry hints = { 1, 1 };
911 GdkRGBA bgcolor = { 0 };
914 c->finder = webkit_web_view_get_find_controller(c->view);
916 c->inspector = webkit_web_view_get_inspector(c->view);
918 c->win = createwindow(c);
920 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
921 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
923 gtk_widget_show_all(c->win);
924 gtk_widget_grab_focus(GTK_WIDGET(c->view));
926 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
927 c->xid = gdk_x11_window_get_xid(gwin);
930 gdk_display_sync(gtk_widget_get_display(c->win));
935 webkit_web_view_set_background_color(c->view, &bgcolor);
939 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
940 gdk_window_add_filter(gwin, processx, c);
944 togglefullscreen(c, NULL);
946 if (zoomlevel != 1.0)
947 webkit_web_view_set_zoom_level(c->view, zoomlevel);
949 setatom(c, AtomFind, "");
950 setatom(c, AtomUri, "about:blank");
954 createwindow(Client *c)
959 w = gtk_plug_new(embed);
961 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
963 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
964 * is always correct, so we should still call this function.
965 * But when doing so, we *must* differentiate between a
966 * WM_CLASS and a resource on the window. By convention, the
967 * window class (WM_CLASS) is capped, while the resource is in
968 * lowercase. Both these values come as a pair.
970 gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
972 /* TA: 20091214: And set the role here as well -- so that
973 * sessions can pick this up.
975 gtk_window_set_role(GTK_WINDOW(w), "Surf");
977 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
980 g_signal_connect(G_OBJECT(w), "destroy",
981 G_CALLBACK(destroywin), c);
982 g_signal_connect(G_OBJECT(w), "leave-notify-event",
983 G_CALLBACK(winevent), c);
984 g_signal_connect(G_OBJECT(w), "window-state-event",
985 G_CALLBACK(winevent), c);
991 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
994 case WEBKIT_LOAD_STARTED:
995 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
997 case WEBKIT_LOAD_REDIRECTED:
998 setatom(c, AtomUri, geturi(c));
1000 case WEBKIT_LOAD_COMMITTED:
1001 if (!webkit_web_view_get_tls_info(c->view, NULL,
1003 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
1005 setatom(c, AtomUri, geturi(c));
1008 setstyle(c, getstyle(geturi(c)));
1010 case WEBKIT_LOAD_FINISHED:
1011 /* Disabled until we write some WebKitWebExtension for
1012 * manipulating the DOM directly.
1013 evalscript(c, "document.documentElement.style.overflow = '%s'",
1014 enablescrollbars ? "auto" : "hidden");
1023 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1025 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1031 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1033 c->title = webkit_web_view_get_title(c->view);
1038 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1041 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1043 /* Keep the hit test to know where is the pointer on the next click */
1047 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1048 else if (hc & OnImg)
1049 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1050 else if (hc & OnMedia)
1051 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1053 c->targeturi = NULL;
1058 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1060 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1061 if (allowgeolocation)
1062 webkit_permission_request_allow(r);
1064 webkit_permission_request_deny(r);
1072 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1073 WebKitPolicyDecisionType dt, Client *c)
1076 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1077 decidenavigation(d, c);
1079 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1080 decidenewwindow(d, c);
1082 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1083 decideresource(d, c);
1086 webkit_policy_decision_ignore(d);
1093 decidenavigation(WebKitPolicyDecision *d, Client *c)
1095 WebKitNavigationAction *a =
1096 webkit_navigation_policy_decision_get_navigation_action(
1097 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: /* fallthrough */
1105 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1107 /* Do not navigate to links with a "_blank" target (popup) */
1108 if (webkit_navigation_policy_decision_get_frame_name(
1109 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1110 webkit_policy_decision_ignore(d);
1112 /* Filter out navigation to different domain ? */
1113 /* get action→urirequest, copy and load in new window+view
1114 * on Ctrl+Click ? */
1115 webkit_policy_decision_use(d);
1122 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1125 WebKitNavigationAction *a =
1126 webkit_navigation_policy_decision_get_navigation_action(
1127 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1130 switch (webkit_navigation_action_get_navigation_type(a)) {
1131 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1132 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1133 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1134 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1135 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1136 /* Filter domains here */
1137 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1138 * test for link clicked but no button ? */
1139 arg.v = webkit_uri_request_get_uri(
1140 webkit_navigation_action_get_request(a));
1141 newwindow(c, &arg, 0);
1143 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1148 webkit_policy_decision_ignore(d);
1152 decideresource(WebKitPolicyDecision *d, Client *c)
1155 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1156 WebKitURIResponse *res =
1157 webkit_response_policy_decision_get_response(r);
1158 const gchar *uri = webkit_uri_response_get_uri(res);
1160 if (g_str_has_suffix(uri, "/favicon.ico"))
1161 webkit_uri_request_set_uri(
1162 webkit_response_policy_decision_get_request(r),
1165 if (!g_str_has_prefix(uri, "http://")
1166 && !g_str_has_prefix(uri, "https://")
1167 && !g_str_has_prefix(uri, "about:")
1168 && !g_str_has_prefix(uri, "file://")
1169 && !g_str_has_prefix(uri, "data:")
1170 && !g_str_has_prefix(uri, "blob:")
1171 && strlen(uri) > 0) {
1172 for (i = 0; i < strlen(uri); i++) {
1173 if (!g_ascii_isprint(uri[i])) {
1179 handleplumb(c, uri);
1180 webkit_policy_decision_ignore(d);
1184 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1185 webkit_policy_decision_use(d);
1187 webkit_policy_decision_ignore(d);
1193 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1195 g_signal_connect(G_OBJECT(d), "notify::response",
1196 G_CALLBACK(responsereceived), c);
1200 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1202 download(c, webkit_download_get_response(d));
1203 webkit_download_cancel(d);
1207 download(Client *c, WebKitURIResponse *r)
1209 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1214 closeview(WebKitWebView *v, Client *c)
1216 gtk_widget_destroy(c->win);
1220 destroywin(GtkWidget* w, Client *c)
1228 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1230 Arg a = {.v = text };
1232 loaduri((Client *) d, &a);
1236 reload(Client *c, const Arg *a)
1238 gboolean nocache = *(gboolean *)a;
1240 webkit_web_view_reload_bypass_cache(c->view);
1242 webkit_web_view_reload(c->view);
1246 print(Client *c, const Arg *a)
1248 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1249 GTK_WINDOW(c->win));
1253 clipboard(Client *c, const Arg *a)
1255 if (a->b) { /* load clipboard uri */
1256 gtk_clipboard_request_text(gtk_clipboard_get(
1257 GDK_SELECTION_PRIMARY),
1259 } else { /* copy uri */
1260 gtk_clipboard_set_text(gtk_clipboard_get(
1261 GDK_SELECTION_PRIMARY), c->targeturi
1262 ? c->targeturi : geturi(c), -1);
1267 zoom(Client *c, const Arg *a)
1270 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1272 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1274 webkit_web_view_set_zoom_level(c->view, 1.0);
1276 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1280 scroll_v(Client *c, const Arg *a)
1282 evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1287 scroll_h(Client *c, const Arg *a)
1289 evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1294 navigate(Client *c, const Arg *a)
1297 webkit_web_view_go_back(c->view);
1299 webkit_web_view_go_forward(c->view);
1303 stop(Client *c, const Arg *a)
1305 webkit_web_view_stop_loading(c->view);
1309 toggle(Client *c, const Arg *a)
1311 WebKitSettings *s = webkit_web_view_get_settings(c->view);
1313 switch ((unsigned int)a->i) {
1315 enablecaretbrowsing = !enablecaretbrowsing;
1316 webkit_settings_set_enable_caret_browsing(s,
1317 enablecaretbrowsing);
1319 return; /* do not reload */
1321 case FrameFlattening:
1322 enableframeflattening = !enableframeflattening;
1323 webkit_settings_set_enable_frame_flattening(s,
1324 enableframeflattening);
1327 allowgeolocation = !allowgeolocation;
1330 enablescripts = !enablescripts;
1331 webkit_settings_set_enable_javascript(s, enablescripts);
1334 loadimages = !loadimages;
1335 webkit_settings_set_auto_load_images(s, loadimages);
1338 enableplugins = !enableplugins;
1339 webkit_settings_set_enable_plugins(s, enableplugins);
1342 /* Disabled until we write some WebKitWebExtension for
1343 * manipulating the DOM directly.
1344 enablescrollbars = !enablescrollbars;
1345 evalscript(c, "document.documentElement.style.overflow = '%s'",
1346 enablescrollbars ? "auto" : "hidden");
1348 return; /* do not reload */
1357 togglefullscreen(Client *c, const Arg *a)
1359 /* toggling value is handled in winevent() */
1361 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1363 gtk_window_fullscreen(GTK_WINDOW(c->win));
1367 togglecookiepolicy(Client *c, const Arg *a)
1370 cookiepolicy %= strlen(cookiepolicies);
1372 webkit_cookie_manager_set_accept_policy(
1373 webkit_web_context_get_cookie_manager(
1374 webkit_web_view_get_context(c->view)),
1375 cookiepolicy_get());
1378 /* Do not reload. */
1382 togglestyle(Client *c, const Arg *a)
1384 enablestyle = !enablestyle;
1385 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1391 toggleinspector(Client *c, const Arg *a)
1393 if (enableinspector) {
1394 if (webkit_web_inspector_is_attached(c->inspector))
1395 webkit_web_inspector_close(c->inspector);
1397 webkit_web_inspector_show(c->inspector);
1402 find(Client *c, const Arg *a)
1408 webkit_find_controller_search_next(c->finder);
1410 webkit_find_controller_search_previous(c->finder);
1412 s = getatom(c, AtomFind);
1413 f = webkit_find_controller_get_search_text(c->finder);
1415 if (g_strcmp0(f, s) == 0) /* reset search */
1416 webkit_find_controller_search(c->finder, "", findopts,
1419 webkit_find_controller_search(c->finder, s, findopts,
1422 if (strcmp(s, "") == 0)
1423 webkit_find_controller_search_finish(c->finder);
1428 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1434 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1438 arg.v = webkit_hit_test_result_get_link_uri(h);
1439 newwindow(c, &arg, a->b);
1443 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1447 if (webkit_hit_test_result_get_context(h) & OnMedia) {
1448 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1454 main(int argc, char *argv[])
1459 memset(&arg, 0, sizeof(arg));
1461 /* command line args */
1464 cookiepolicies = EARGF(usage());
1467 enablescrollbars = 0;
1470 enablescrollbars = 1;
1473 cookiefile = EARGF(usage());
1482 embed = strtol(EARGF(usage()), NULL, 0);
1485 runinfullscreen = 0;
1488 runinfullscreen = 1;
1491 allowgeolocation = 0;
1494 allowgeolocation = 1;
1515 enableinspector = 0;
1518 enableinspector = 1;
1527 scriptfile = EARGF(usage());
1536 stylefile = EARGF(usage());
1539 useragent = EARGF(usage());
1542 die("surf-"VERSION", ©2009-2015 surf engineers, "
1543 "see LICENSE for details\n");
1548 zoomlevel = strtof(EARGF(usage()), NULL);
1557 c = newclient(NULL);
1561 loaduri(clients, &arg);
1568 return EXIT_SUCCESS;