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, GdkEventKey *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 (0 < waitpid(-1, NULL, WNOHANG));
266 buildfile(const char *path)
268 char *dname, *bname, *bpath, *fpath;
271 dname = g_path_get_dirname(path);
272 bname = g_path_get_basename(path);
274 bpath = buildpath(dname);
277 fpath = g_build_filename(bpath, bname, NULL);
281 if (!(f = fopen(fpath, "a")))
282 die("Could not open file: %s\n", fpath);
284 g_chmod(fpath, 0600); /* always */
291 buildpath(const char *path)
294 char *apath, *name, *p, *fpath;
296 if (path[0] == '~') {
297 if (path[1] == '/' || path[1] == '\0') {
298 p = (char *)&path[1];
299 pw = getpwuid(getuid());
301 if ((p = strchr(path, '/')))
302 name = g_strndup(&path[1], --p - path);
304 name = g_strdup(&path[1]);
306 if (!(pw = getpwnam(name))) {
307 die("Can't get user %s home directory: %s.\n",
312 apath = g_build_filename(pw->pw_dir, p, NULL);
314 apath = g_strdup(path);
317 /* creating directory */
318 if (g_mkdir_with_parents(apath, 0700) < 0)
319 die("Could not access directory: %s\n", apath);
321 fpath = realpath(apath, NULL);
328 newclient(Client *rc)
332 if (!(c = calloc(1, sizeof(Client))))
333 die("Cannot malloc!\n");
341 c->view = newview(c, rc ? rc->view : NULL);
342 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
348 addaccelgroup(Client *c)
351 GtkAccelGroup *group = gtk_accel_group_new();
354 for (i = 0; i < LENGTH(keys); i++) {
355 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
356 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
359 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
363 loaduri(Client *c, const Arg *a)
367 const char *uri = a->v;
369 if (g_strcmp0(uri, "") == 0)
372 if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
374 } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
375 url = g_strdup_printf("file://%s", path);
378 url = g_strdup_printf("http://%s", uri);
381 setatom(c, AtomUri, url);
383 if (strcmp(url, geturi(c)) == 0) {
386 webkit_web_view_load_uri(c->view, url);
387 c->title = geturi(c);
399 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
405 setatom(Client *c, int a, const char *v)
408 XChangeProperty(dpy, c->xid,
409 atoms[a], XA_STRING, 8, PropModeReplace,
410 (unsigned char *)v, strlen(v) + 1);
414 getatom(Client *c, int a)
416 static char buf[BUFSIZ];
419 unsigned long ldummy;
420 unsigned char *p = NULL;
422 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
423 &adummy, &idummy, &ldummy, &ldummy, &p);
425 strncpy(buf, (char *)p, LENGTH(buf) - 1);
434 updatetitle(Client *c)
438 if (showindicators) {
442 if (c->progress != 100) {
443 title = g_strdup_printf("[%i%%] %s:%s | %s",
444 c->progress, togglestats, pagestats,
445 c->targeturi ? c->targeturi : c->title);
447 title = g_strdup_printf("%s:%s | %s",
448 togglestats, pagestats,
449 c->targeturi ? c->targeturi : c->title);
452 gtk_window_set_title(GTK_WINDOW(c->win), title);
455 gtk_window_set_title(GTK_WINDOW(c->win), c->title ?
461 gettogglestats(Client *c)
463 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
464 togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
465 togglestats[2] = allowgeolocation ? 'G' : 'g';
466 togglestats[3] = enablecache ? 'D' : 'd';
467 togglestats[4] = loadimages ? 'I' : 'i';
468 togglestats[5] = enablescripts ? 'S' : 's';
469 togglestats[6] = enableplugins ? 'V' : 'v';
470 togglestats[7] = enablestyle ? 'M' : 'm';
471 togglestats[8] = enableframeflattening ? 'F' : 'f';
472 togglestats[9] = '\0';
476 getpagestats(Client *c)
478 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
479 c->tlsflags > 0 ? 'U' : 'T';
483 WebKitCookieAcceptPolicy
484 cookiepolicy_get(void)
486 switch (cookiepolicies[cookiepolicy]) {
488 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
490 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
496 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
500 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
503 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
505 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
507 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
516 getstyle(const char *uri)
523 for (i = 0; i < LENGTH(styles); ++i) {
524 if (styles[i].regex &&
525 !regexec(&(styles[i].re), uri, 0, NULL, 0))
526 return styles[i].style;
533 setstyle(Client *c, const char *stylefile)
537 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
538 fprintf(stderr, "Could not read style file: %s\n", stylefile);
542 webkit_user_content_manager_add_style_sheet(
543 webkit_web_view_get_user_content_manager(c->view),
544 webkit_user_style_sheet_new(style,
545 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
546 WEBKIT_USER_STYLE_LEVEL_USER,
558 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
559 evalscript(c, script);
564 evalscript(Client *c, const char *jsstr, ...)
570 script = g_strdup_vprintf(jsstr, ap);
573 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
578 updatewinid(Client *c)
580 snprintf(winid, LENGTH(winid), "%lu", c->xid);
584 handleplumb(Client *c, const char *uri)
586 Arg a = (Arg)PLUMB(uri);
591 newwindow(Client *c, const Arg *a, int noembed)
595 const char *cmd[26], *uri;
596 const Arg arg = { .v = cmd };
600 cmd[i++] = cookiepolicies;
601 cmd[i++] = enablescrollbars ? "-B" : "-b";
602 if (cookiefile && g_strcmp0(cookiefile, "")) {
604 cmd[i++] = cookiefile;
606 cmd[i++] = enablecache ? "-D" : "-d";
607 if (embed && !noembed) {
609 snprintf(tmp, LENGTH(tmp), "%lu", embed);
612 cmd[i++] = runinfullscreen ? "-F" : "-f";
613 cmd[i++] = allowgeolocation ? "-G" : "-g";
614 cmd[i++] = loadimages ? "-I" : "-i";
615 cmd[i++] = kioskmode ? "-K" : "-k";
616 cmd[i++] = enablestyle ? "-M" : "-m";
617 cmd[i++] = enableinspector ? "-N" : "-n";
618 cmd[i++] = enableplugins ? "-P" : "-p";
619 if (scriptfile && g_strcmp0(scriptfile, "")) {
621 cmd[i++] = scriptfile;
623 cmd[i++] = enablescripts ? "-S" : "-s";
624 if (stylefile && g_strcmp0(stylefile, "")) {
626 cmd[i++] = stylefile;
628 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
630 cmd[i++] = fulluseragent;
634 /* do not keep zoom level */
644 spawn(Client *c, const Arg *a)
648 close(ConnectionNumber(dpy));
650 execvp(((char **)a->v)[0], (char **)a->v);
651 fprintf(stderr, "surf: execvp %s", ((char **)a->v)[0]);
658 destroyclient(Client *c)
662 webkit_web_view_stop_loading(c->view);
663 /* Not needed, has already been called
664 gtk_widget_destroy(c->win);
667 for (p = clients; p && p->next != c; p = p->next)
680 destroyclient(clients);
688 newview(Client *c, WebKitWebView *rv)
691 WebKitSettings *settings;
692 WebKitUserContentManager *contentmanager;
693 WebKitWebContext *context;
698 webkit_web_view_new_with_related_view(rv));
700 settings = webkit_settings_new_with_settings(
701 "auto-load-images", loadimages,
702 "default-font-size", defaultfontsize,
703 "enable-caret-browsing", enablecaretbrowsing,
704 "enable-developer-extras", enableinspector,
705 "enable-dns-prefetching", enablednsprefetching,
706 "enable-frame-flattening", enableframeflattening,
707 "enable-html5-database", enablecache,
708 "enable-html5-local-storage", enablecache,
709 "enable-javascript", enablescripts,
710 "enable-plugins", enableplugins,
712 /* For mor interesting settings, have a look at
713 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
715 if (strcmp(fulluseragent, "")) {
716 webkit_settings_set_user_agent(settings, fulluseragent);
717 } else if (surfuseragent) {
718 webkit_settings_set_user_agent_with_application_details(
719 settings, "Surf", VERSION);
721 useragent = webkit_settings_get_user_agent(settings);
723 contentmanager = webkit_user_content_manager_new();
725 context = webkit_web_context_new_with_website_data_manager(
726 webkit_website_data_manager_new(
727 "base-cache-directory", cachedir,
728 "base-data-directory", cachedir,
731 /* rendering process model, can be a shared unique one
732 * or one for each view */
733 webkit_web_context_set_process_model(context,
734 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
736 webkit_web_context_set_tls_errors_policy(context, strictssl ?
737 WEBKIT_TLS_ERRORS_POLICY_FAIL :
738 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
740 webkit_web_context_set_cache_model(context, enablecache ?
741 WEBKIT_CACHE_MODEL_WEB_BROWSER :
742 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
744 /* Currently only works with text file to be compatible with curl */
745 webkit_cookie_manager_set_persistent_storage(
746 webkit_web_context_get_cookie_manager(context), cookiefile,
747 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
749 webkit_cookie_manager_set_accept_policy(
750 webkit_web_context_get_cookie_manager(context),
753 g_signal_connect(G_OBJECT(context), "download-started",
754 G_CALLBACK(downloadstarted), c);
756 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
757 "settings", settings,
758 "user-content-manager", contentmanager,
759 "web-context", context,
763 g_signal_connect(G_OBJECT(v), "notify::title",
764 G_CALLBACK(titlechanged), c);
765 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
766 G_CALLBACK(mousetargetchanged), c);
767 g_signal_connect(G_OBJECT(v), "permission-request",
768 G_CALLBACK(permissionrequested), c);
769 g_signal_connect(G_OBJECT(v), "create",
770 G_CALLBACK(createview), c);
771 g_signal_connect(G_OBJECT(v), "ready-to-show",
772 G_CALLBACK(showview), c);
773 g_signal_connect(G_OBJECT(v), "decide-policy",
774 G_CALLBACK(decidepolicy), c);
775 g_signal_connect(G_OBJECT(v), "load-changed",
776 G_CALLBACK(loadchanged), c);
777 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
778 G_CALLBACK(progresschanged), c);
779 g_signal_connect(G_OBJECT(v), "button-release-event",
780 G_CALLBACK(buttonreleased), c);
781 g_signal_connect(G_OBJECT(v), "close",
782 G_CALLBACK(closeview), c);
788 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
792 switch (webkit_navigation_action_get_navigation_type(a)) {
793 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
795 * popup windows of type “other” are almost always triggered
796 * by user gesture, so inverse the logic here
798 /* instead of this, compare destination uri to mouse-over uri for validating window */
799 if (webkit_navigation_action_is_user_gesture(a))
801 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
802 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
803 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
804 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
805 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
812 return GTK_WIDGET(n->view);
816 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
818 WebKitHitTestResultContext element;
819 GdkEventButton *eb = (GdkEventButton*)e;
822 element = webkit_hit_test_result_get_context(c->mousepos);
824 for (i = 0; i < LENGTH(buttons); ++i) {
825 if (element & buttons[i].target &&
826 eb->button == buttons[i].button &&
827 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
829 buttons[i].func(c, &buttons[i].arg, c->mousepos);
830 return buttons[i].stopevent;
838 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
842 gboolean processed = FALSE;
844 mods = CLEANMASK(mods);
845 key = gdk_keyval_to_lower(key);
847 for (i = 0; i < LENGTH(keys); i++) {
848 if (key == keys[i].keyval
849 && mods == keys[i].mod
851 keys[i].func(c, &(keys[i].arg));
860 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
862 Client *c = (Client *)d;
866 if (((XEvent *)e)->type == PropertyNotify) {
867 ev = &((XEvent *)e)->xproperty;
868 if (ev->state == PropertyNewValue) {
869 if (ev->atom == atoms[AtomFind]) {
872 return GDK_FILTER_REMOVE;
873 } else if (ev->atom == atoms[AtomGo]) {
874 a.v = getatom(c, AtomGo);
877 return GDK_FILTER_REMOVE;
881 return GDK_FILTER_CONTINUE;
885 winevent(GtkWidget *w, GdkEvent *e, Client *c)
888 case GDK_LEAVE_NOTIFY:
892 case GDK_WINDOW_STATE: /* fallthrough */
893 if (e->window_state.changed_mask ==
894 GDK_WINDOW_STATE_FULLSCREEN) {
895 c->fullscreen = e->window_state.new_window_state &
896 GDK_WINDOW_STATE_FULLSCREEN;
907 showview(WebKitWebView *v, Client *c)
909 GdkGeometry hints = { 1, 1 };
910 GdkRGBA bgcolor = { 0 };
913 c->finder = webkit_web_view_get_find_controller(c->view);
915 c->inspector = webkit_web_view_get_inspector(c->view);
917 c->win = createwindow(c);
919 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
920 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
922 gtk_widget_show_all(c->win);
923 gtk_widget_grab_focus(GTK_WIDGET(c->view));
925 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
926 c->xid = gdk_x11_window_get_xid(gwin);
929 gdk_display_sync(gtk_widget_get_display(c->win));
934 webkit_web_view_set_background_color(c->view, &bgcolor);
938 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
939 gdk_window_add_filter(gwin, processx, c);
943 togglefullscreen(c, NULL);
945 if (zoomlevel != 1.0)
946 webkit_web_view_set_zoom_level(c->view, zoomlevel);
948 setatom(c, AtomFind, "");
949 setatom(c, AtomUri, "about:blank");
953 createwindow(Client *c)
958 w = gtk_plug_new(embed);
960 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
962 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
963 * is always correct, so we should still call this function.
964 * But when doing so, we *must* differentiate between a
965 * WM_CLASS and a resource on the window. By convention, the
966 * window class (WM_CLASS) is capped, while the resource is in
967 * lowercase. Both these values come as a pair.
969 gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
971 /* TA: 20091214: And set the role here as well -- so that
972 * sessions can pick this up.
974 gtk_window_set_role(GTK_WINDOW(w), "Surf");
976 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
979 g_signal_connect(G_OBJECT(w), "destroy",
980 G_CALLBACK(destroywin), c);
981 g_signal_connect(G_OBJECT(w), "leave-notify-event",
982 G_CALLBACK(winevent), c);
983 g_signal_connect(G_OBJECT(w), "window-state-event",
984 G_CALLBACK(winevent), c);
990 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
993 case WEBKIT_LOAD_STARTED:
994 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
996 case WEBKIT_LOAD_REDIRECTED:
997 setatom(c, AtomUri, geturi(c));
999 case WEBKIT_LOAD_COMMITTED:
1000 if (!webkit_web_view_get_tls_info(c->view, NULL,
1002 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
1004 setatom(c, AtomUri, geturi(c));
1007 setstyle(c, getstyle(geturi(c)));
1009 case WEBKIT_LOAD_FINISHED:
1010 /* Disabled until we write some WebKitWebExtension for
1011 * manipulating the DOM directly.
1012 evalscript(c, "document.documentElement.style.overflow = '%s'",
1013 enablescrollbars ? "auto" : "hidden");
1022 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1024 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1030 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1032 c->title = webkit_web_view_get_title(c->view);
1037 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1040 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1042 /* Keep the hit test to know where is the pointer on the next click */
1046 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1047 else if (hc & OnImg)
1048 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1049 else if (hc & OnMedia)
1050 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1052 c->targeturi = NULL;
1057 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1059 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1060 if (allowgeolocation)
1061 webkit_permission_request_allow(r);
1063 webkit_permission_request_deny(r);
1071 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1072 WebKitPolicyDecisionType dt, Client *c)
1075 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1076 decidenavigation(d, c);
1078 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1079 decidenewwindow(d, c);
1081 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1082 decideresource(d, c);
1085 webkit_policy_decision_ignore(d);
1092 decidenavigation(WebKitPolicyDecision *d, Client *c)
1094 WebKitNavigationAction *a =
1095 webkit_navigation_policy_decision_get_navigation_action(
1096 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1098 switch (webkit_navigation_action_get_navigation_type(a)) {
1099 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1100 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1101 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1102 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1103 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1104 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1106 /* Do not navigate to links with a "_blank" target (popup) */
1107 if (webkit_navigation_policy_decision_get_frame_name(
1108 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1109 webkit_policy_decision_ignore(d);
1111 /* Filter out navigation to different domain ? */
1112 /* get action→urirequest, copy and load in new window+view
1113 * on Ctrl+Click ? */
1114 webkit_policy_decision_use(d);
1121 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1124 WebKitNavigationAction *a =
1125 webkit_navigation_policy_decision_get_navigation_action(
1126 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1129 switch (webkit_navigation_action_get_navigation_type(a)) {
1130 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1131 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1132 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1133 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1134 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1135 /* Filter domains here */
1136 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1137 * test for link clicked but no button ? */
1138 arg.v = webkit_uri_request_get_uri(
1139 webkit_navigation_action_get_request(a));
1140 newwindow(c, &arg, 0);
1142 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1147 webkit_policy_decision_ignore(d);
1151 decideresource(WebKitPolicyDecision *d, Client *c)
1154 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1155 WebKitURIResponse *res =
1156 webkit_response_policy_decision_get_response(r);
1157 const gchar *uri = webkit_uri_response_get_uri(res);
1159 if (g_str_has_suffix(uri, "/favicon.ico"))
1160 webkit_uri_request_set_uri(
1161 webkit_response_policy_decision_get_request(r),
1164 if (!g_str_has_prefix(uri, "http://")
1165 && !g_str_has_prefix(uri, "https://")
1166 && !g_str_has_prefix(uri, "about:")
1167 && !g_str_has_prefix(uri, "file://")
1168 && !g_str_has_prefix(uri, "data:")
1169 && !g_str_has_prefix(uri, "blob:")
1170 && strlen(uri) > 0) {
1171 for (i = 0; i < strlen(uri); i++) {
1172 if (!g_ascii_isprint(uri[i])) {
1178 handleplumb(c, uri);
1179 webkit_policy_decision_ignore(d);
1183 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1184 webkit_policy_decision_use(d);
1186 webkit_policy_decision_ignore(d);
1192 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1194 g_signal_connect(G_OBJECT(d), "notify::response",
1195 G_CALLBACK(responsereceived), c);
1199 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1201 download(c, webkit_download_get_response(d));
1202 webkit_download_cancel(d);
1206 download(Client *c, WebKitURIResponse *r)
1208 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1213 closeview(WebKitWebView *v, Client *c)
1215 gtk_widget_destroy(c->win);
1219 destroywin(GtkWidget* w, Client *c)
1227 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1229 Arg a = {.v = text };
1231 loaduri((Client *) d, &a);
1235 reload(Client *c, const Arg *a)
1238 webkit_web_view_reload_bypass_cache(c->view);
1240 webkit_web_view_reload(c->view);
1244 print(Client *c, const Arg *a)
1246 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1247 GTK_WINDOW(c->win));
1251 clipboard(Client *c, const Arg *a)
1253 if (a->b) { /* load clipboard uri */
1254 gtk_clipboard_request_text(gtk_clipboard_get(
1255 GDK_SELECTION_PRIMARY),
1257 } else { /* copy uri */
1258 gtk_clipboard_set_text(gtk_clipboard_get(
1259 GDK_SELECTION_PRIMARY), c->targeturi
1260 ? c->targeturi : geturi(c), -1);
1265 zoom(Client *c, const Arg *a)
1268 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1270 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1272 webkit_web_view_set_zoom_level(c->view, 1.0);
1274 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1278 scroll_v(Client *c, const Arg *a)
1280 evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1285 scroll_h(Client *c, const Arg *a)
1287 evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1292 navigate(Client *c, const Arg *a)
1295 webkit_web_view_go_back(c->view);
1297 webkit_web_view_go_forward(c->view);
1301 stop(Client *c, const Arg *a)
1303 webkit_web_view_stop_loading(c->view);
1307 toggle(Client *c, const Arg *a)
1309 WebKitSettings *s = webkit_web_view_get_settings(c->view);
1311 switch ((unsigned int)a->i) {
1313 enablecaretbrowsing = !enablecaretbrowsing;
1314 webkit_settings_set_enable_caret_browsing(s,
1315 enablecaretbrowsing);
1317 return; /* do not reload */
1319 case FrameFlattening:
1320 enableframeflattening = !enableframeflattening;
1321 webkit_settings_set_enable_frame_flattening(s,
1322 enableframeflattening);
1325 allowgeolocation = !allowgeolocation;
1328 enablescripts = !enablescripts;
1329 webkit_settings_set_enable_javascript(s, enablescripts);
1332 loadimages = !loadimages;
1333 webkit_settings_set_auto_load_images(s, loadimages);
1336 enableplugins = !enableplugins;
1337 webkit_settings_set_enable_plugins(s, enableplugins);
1340 /* Disabled until we write some WebKitWebExtension for
1341 * manipulating the DOM directly.
1342 enablescrollbars = !enablescrollbars;
1343 evalscript(c, "document.documentElement.style.overflow = '%s'",
1344 enablescrollbars ? "auto" : "hidden");
1346 return; /* do not reload */
1355 togglefullscreen(Client *c, const Arg *a)
1357 /* toggling value is handled in winevent() */
1359 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1361 gtk_window_fullscreen(GTK_WINDOW(c->win));
1365 togglecookiepolicy(Client *c, const Arg *a)
1368 cookiepolicy %= strlen(cookiepolicies);
1370 webkit_cookie_manager_set_accept_policy(
1371 webkit_web_context_get_cookie_manager(
1372 webkit_web_view_get_context(c->view)),
1373 cookiepolicy_get());
1376 /* Do not reload. */
1380 togglestyle(Client *c, const Arg *a)
1382 enablestyle = !enablestyle;
1383 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1389 toggleinspector(Client *c, const Arg *a)
1391 if (enableinspector) {
1392 if (webkit_web_inspector_is_attached(c->inspector))
1393 webkit_web_inspector_close(c->inspector);
1395 webkit_web_inspector_show(c->inspector);
1400 find(Client *c, const Arg *a)
1406 webkit_find_controller_search_next(c->finder);
1408 webkit_find_controller_search_previous(c->finder);
1410 s = getatom(c, AtomFind);
1411 f = webkit_find_controller_get_search_text(c->finder);
1413 if (g_strcmp0(f, s) == 0) /* reset search */
1414 webkit_find_controller_search(c->finder, "", findopts,
1417 webkit_find_controller_search(c->finder, s, findopts,
1420 if (strcmp(s, "") == 0)
1421 webkit_find_controller_search_finish(c->finder);
1426 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1432 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1436 arg.v = webkit_hit_test_result_get_link_uri(h);
1437 newwindow(c, &arg, a->b);
1441 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1445 if (webkit_hit_test_result_get_context(h) & OnMedia) {
1446 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1452 main(int argc, char *argv[])
1457 memset(&arg, 0, sizeof(arg));
1459 /* command line args */
1462 cookiepolicies = EARGF(usage());
1465 enablescrollbars = 0;
1468 enablescrollbars = 1;
1471 cookiefile = EARGF(usage());
1480 embed = strtol(EARGF(usage()), NULL, 0);
1483 runinfullscreen = 0;
1486 runinfullscreen = 1;
1489 allowgeolocation = 0;
1492 allowgeolocation = 1;
1513 enableinspector = 0;
1516 enableinspector = 1;
1525 scriptfile = EARGF(usage());
1534 stylefile = EARGF(usage());
1537 useragent = EARGF(usage());
1540 die("surf-"VERSION", ©2009-2015 surf engineers, "
1541 "see LICENSE for details\n");
1546 zoomlevel = strtof(EARGF(usage()), NULL);
1555 c = newclient(NULL);
1559 loaduri(clients, &arg);
1566 return EXIT_SUCCESS;