1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
21 #include <gdk/gdkkeysyms.h>
23 #include <glib/gstdio.h>
27 #include <JavaScriptCore/JavaScript.h>
28 #include <webkit2/webkit2.h>
30 #include <X11/Xatom.h>
34 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
35 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
37 enum { AtomFind, AtomGo, AtomUri, AtomLast };
40 OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
41 OnLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
42 OnImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
43 OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
44 OnEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
45 OnBar = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
46 OnSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
47 OnAny = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
98 typedef struct Client {
101 WebKitWebInspector *inspector;
102 WebKitFindController *finder;
103 WebKitHitTestResult *mousepos;
104 GTlsCertificate *cert, *failedcert;
105 GTlsCertificateFlags tlserr;
107 int progress, fullscreen, https, insecure, errorpage;
108 const char *title, *overtitle, *targeturi;
116 void (*func)(Client *c, const Arg *a);
124 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
126 unsigned int stopevent;
131 Parameter config[ParameterLast];
142 static void usage(void);
143 static void die(const char *errstr, ...);
144 static void setup(void);
145 static void sigchld(int unused);
146 static void sighup(int unused);
147 static char *buildfile(const char *path);
148 static char *buildpath(const char *path);
149 static char *untildepath(const char *path);
150 static const char *getuserhomedir(const char *user);
151 static const char *getcurrentuserhomedir(void);
152 static Client *newclient(Client *c);
153 static void loaduri(Client *c, const Arg *a);
154 static const char *geturi(Client *c);
155 static void setatom(Client *c, int a, const char *v);
156 static const char *getatom(Client *c, int a);
157 static void updatetitle(Client *c);
158 static void gettogglestats(Client *c);
159 static void getpagestats(Client *c);
160 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
161 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
162 static void seturiparameters(Client *c, const char *uri, ParamName *params);
163 static void setparameter(Client *c, int refresh, ParamName p, const Arg *a);
164 static const char *getcert(const char *uri);
165 static void setcert(Client *c, const char *file);
166 static const char *getstyle(const char *uri);
167 static void setstyle(Client *c, const char *file);
168 static void runscript(Client *c);
169 static void evalscript(Client *c, const char *jsstr, ...);
170 static void updatewinid(Client *c);
171 static void handleplumb(Client *c, const char *uri);
172 static void newwindow(Client *c, const Arg *a, int noembed);
173 static void spawn(Client *c, const Arg *a);
174 static void destroyclient(Client *c);
175 static void cleanup(void);
178 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
179 static void initwebextensions(WebKitWebContext *wc, Client *c);
180 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
182 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
183 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
185 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
186 static void showview(WebKitWebView *v, Client *c);
187 static GtkWidget *createwindow(Client *c);
188 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
189 GTlsCertificate *cert,
190 GTlsCertificateFlags err, Client *c);
191 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
192 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
193 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
194 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
195 guint modifiers, Client *c);
196 static gboolean permissionrequested(WebKitWebView *v,
197 WebKitPermissionRequest *r, Client *c);
198 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
199 WebKitPolicyDecisionType dt, Client *c);
200 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
201 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
202 static void decideresource(WebKitPolicyDecision *d, Client *c);
203 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
205 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
207 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
208 static void download(Client *c, WebKitURIResponse *r);
209 static void webprocessterminated(WebKitWebView *v,
210 WebKitWebProcessTerminationReason r,
212 static void closeview(WebKitWebView *v, Client *c);
213 static void destroywin(GtkWidget* w, Client *c);
216 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
217 static void reload(Client *c, const Arg *a);
218 static void print(Client *c, const Arg *a);
219 static void showcert(Client *c, const Arg *a);
220 static void clipboard(Client *c, const Arg *a);
221 static void zoom(Client *c, const Arg *a);
222 static void scroll(Client *c, const Arg *a);
223 static void navigate(Client *c, const Arg *a);
224 static void stop(Client *c, const Arg *a);
225 static void toggle(Client *c, const Arg *a);
226 static void togglefullscreen(Client *c, const Arg *a);
227 static void togglecookiepolicy(Client *c, const Arg *a);
228 static void toggleinspector(Client *c, const Arg *a);
229 static void find(Client *c, const Arg *a);
232 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
233 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
234 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
236 static char winid[64];
237 static char togglestats[12];
238 static char pagestats[2];
239 static Atom atoms[AtomLast];
242 static int cookiepolicy;
244 static Client *clients;
245 static GdkDevice *gdkkb;
246 static char *stylefile;
247 static const char *useragent;
248 static Parameter *curconfig;
249 static int modparams[ParameterLast];
252 static ParamName loadtransient[] = {
266 static ParamName loadcommitted[] = {
292 static ParamName loadfinished[] = {
296 /* configuration, allows nested code to access above variables */
302 die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
303 "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
304 "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
308 die(const char *errstr, ...)
312 va_start(ap, errstr);
313 vfprintf(stderr, errstr, ap);
324 /* clean up any zombies immediately */
326 if (signal(SIGHUP, sighup) == SIG_ERR)
327 die("Can't install SIGHUP handler");
329 if (!(dpy = XOpenDisplay(NULL)))
330 die("Can't open default display");
333 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
334 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
335 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
337 gtk_init(NULL, NULL);
339 gdpy = gdk_display_get_default();
341 curconfig = defconfig;
344 cookiefile = buildfile(cookiefile);
345 scriptfile = buildfile(scriptfile);
346 cachedir = buildpath(cachedir);
347 certdir = buildpath(certdir);
349 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
351 for (i = 0; i < LENGTH(certs); ++i) {
352 if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
353 certs[i].file = g_strconcat(certdir, "/", certs[i].file,
356 fprintf(stderr, "Could not compile regex: %s\n",
358 certs[i].regex = NULL;
363 styledir = buildpath(styledir);
364 for (i = 0; i < LENGTH(styles); ++i) {
365 if (!regcomp(&(styles[i].re), styles[i].regex,
367 styles[i].file = g_strconcat(styledir, "/",
368 styles[i].file, NULL);
370 fprintf(stderr, "Could not compile regex: %s\n",
372 styles[i].regex = NULL;
377 stylefile = buildfile(stylefile);
380 for (i = 0; i < LENGTH(uriparams); ++i) {
381 if (regcomp(&(uriparams[i].re), uriparams[i].uri,
383 fprintf(stderr, "Could not compile regex: %s\n",
385 uriparams[i].uri = NULL;
389 /* copy default parameters with higher priority */
390 for (j = 0; j < ParameterLast; ++j) {
391 if (defconfig[j].prio >= uriparams[i].config[j].prio)
392 uriparams[i].config[j] = defconfig[j];
400 if (signal(SIGCHLD, sigchld) == SIG_ERR)
401 die("Can't install SIGCHLD handler");
402 while (waitpid(-1, NULL, WNOHANG) > 0)
412 for (c = clients; c; c = c->next)
417 buildfile(const char *path)
419 char *dname, *bname, *bpath, *fpath;
422 dname = g_path_get_dirname(path);
423 bname = g_path_get_basename(path);
425 bpath = buildpath(dname);
428 fpath = g_build_filename(bpath, bname, NULL);
432 if (!(f = fopen(fpath, "a")))
433 die("Could not open file: %s\n", fpath);
435 g_chmod(fpath, 0600); /* always */
442 getuserhomedir(const char *user)
444 struct passwd *pw = getpwnam(user);
447 die("Can't get user %s login information.\n", user);
453 getcurrentuserhomedir(void)
459 homedir = getenv("HOME");
463 user = getenv("USER");
465 return getuserhomedir(user);
467 pw = getpwuid(getuid());
469 die("Can't get current user home directory\n");
475 buildpath(const char *path)
480 apath = untildepath(path);
482 apath = g_strdup(path);
484 /* creating directory */
485 if (g_mkdir_with_parents(apath, 0700) < 0)
486 die("Could not access directory: %s\n", apath);
488 fpath = realpath(apath, NULL);
495 untildepath(const char *path)
497 char *apath, *name, *p;
500 if (path[1] == '/' || path[1] == '\0') {
501 p = (char *)&path[1];
502 homedir = getcurrentuserhomedir();
504 if ((p = strchr(path, '/')))
505 name = g_strndup(&path[1], p - (path + 1));
507 name = g_strdup(&path[1]);
509 homedir = getuserhomedir(name);
512 apath = g_build_filename(homedir, p, NULL);
517 newclient(Client *rc)
521 if (!(c = calloc(1, sizeof(Client))))
522 die("Cannot malloc!\n");
528 c->view = newview(c, rc ? rc->view : NULL);
534 loaduri(Client *c, const Arg *a)
537 char *url, *path, *apath;
538 const char *uri = a->v;
540 if (g_strcmp0(uri, "") == 0)
543 if (g_str_has_prefix(uri, "http://") ||
544 g_str_has_prefix(uri, "https://") ||
545 g_str_has_prefix(uri, "file://") ||
546 g_str_has_prefix(uri, "about:")) {
550 apath = untildepath(uri);
553 if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
554 url = g_strdup_printf("file://%s", path);
557 url = g_strdup_printf("http://%s", uri);
563 setatom(c, AtomUri, url);
565 if (strcmp(url, geturi(c)) == 0) {
568 webkit_web_view_load_uri(c->view, url);
580 if (!(uri = webkit_web_view_get_uri(c->view)))
586 setatom(Client *c, int a, const char *v)
588 XChangeProperty(dpy, c->xid,
589 atoms[a], XA_STRING, 8, PropModeReplace,
590 (unsigned char *)v, strlen(v) + 1);
595 getatom(Client *c, int a)
597 static char buf[BUFSIZ];
600 unsigned long ldummy;
601 unsigned char *p = NULL;
604 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
605 &adummy, &idummy, &ldummy, &ldummy, &p);
607 strncpy(buf, (char *)p, LENGTH(buf) - 1);
616 updatetitle(Client *c)
619 const char *name = c->overtitle ? c->overtitle :
620 c->title ? c->title : "";
622 if (curconfig[ShowIndicators].val.i) {
626 if (c->progress != 100)
627 title = g_strdup_printf("[%i%%] %s:%s | %s",
628 c->progress, togglestats, pagestats, name);
630 title = g_strdup_printf("%s:%s | %s",
631 togglestats, pagestats, name);
633 gtk_window_set_title(GTK_WINDOW(c->win), title);
636 gtk_window_set_title(GTK_WINDOW(c->win), name);
641 gettogglestats(Client *c)
643 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
644 togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
645 togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
646 togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
647 togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
648 togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
649 togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
650 togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
651 togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
652 togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
653 togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
654 togglestats[11] = '\0';
658 getpagestats(Client *c)
661 pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
667 WebKitCookieAcceptPolicy
668 cookiepolicy_get(void)
670 switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
672 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
674 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
675 default: /* fallthrough */
677 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
682 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
685 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
687 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
689 default: /* fallthrough */
690 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
696 seturiparameters(Client *c, const char *uri, ParamName *params)
698 Parameter *config, *uriconfig = NULL;
701 for (i = 0; i < LENGTH(uriparams); ++i) {
702 if (uriparams[i].uri &&
703 !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
704 uriconfig = uriparams[i].config;
709 curconfig = uriconfig ? uriconfig : defconfig;
711 for (i = 0; (p = params[i]) != ParameterLast; ++i) {
713 default: /* FALLTHROUGH */
714 if (!(defconfig[p].prio < curconfig[p].prio ||
715 defconfig[p].prio < modparams[p]))
720 setparameter(c, 0, p, &curconfig[p].val);
726 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
728 GdkRGBA bgcolor = { 0 };
729 WebKitSettings *s = webkit_web_view_get_settings(c->view);
731 modparams[p] = curconfig[p].prio;
734 case AcceleratedCanvas:
735 webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
737 case AccessMicrophone:
738 return; /* do nothing */
740 return; /* do nothing */
742 webkit_settings_set_enable_caret_browsing(s, a->i);
747 setcert(c, geturi(c));
748 return; /* do not update */
750 webkit_cookie_manager_set_accept_policy(
751 webkit_web_context_get_cookie_manager(
752 webkit_web_view_get_context(c->view)),
757 webkit_web_context_set_cache_model(
758 webkit_web_view_get_context(c->view), a->i ?
759 WEBKIT_CACHE_MODEL_WEB_BROWSER :
760 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
761 return; /* do not update */
763 webkit_settings_set_default_charset(s, a->v);
764 return; /* do not update */
766 webkit_settings_set_enable_dns_prefetching(s, a->i);
767 return; /* do not update */
768 case FileURLsCrossAccess:
769 webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
770 webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
771 return; /* do not update */
773 webkit_settings_set_default_font_size(s, a->i);
774 return; /* do not update */
775 case FrameFlattening:
776 webkit_settings_set_enable_frame_flattening(s, a->i);
783 webkit_web_view_set_background_color(c->view, &bgcolor);
784 return; /* do not update */
786 webkit_settings_set_enable_developer_extras(s, a->i);
787 return; /* do not update */
789 webkit_settings_set_enable_java(s, a->i);
790 return; /* do not update */
792 webkit_settings_set_enable_javascript(s, a->i);
795 return; /* do nothing */
797 webkit_settings_set_auto_load_images(s, a->i);
799 case MediaManualPlay:
800 webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
803 webkit_settings_set_enable_plugins(s, a->i);
805 case PreferredLanguages:
806 return; /* do nothing */
807 case RunInFullscreen:
808 return; /* do nothing */
810 /* Disabled until we write some WebKitWebExtension for
811 * manipulating the DOM directly.
812 enablescrollbars = !enablescrollbars;
813 evalscript(c, "document.documentElement.style.overflow = '%s'",
814 enablescrollbars ? "auto" : "hidden");
816 return; /* do not update */
819 case SmoothScrolling:
820 webkit_settings_set_enable_smooth_scrolling(s, a->i);
821 return; /* do not update */
823 webkit_settings_set_enable_site_specific_quirks(s, a->i);
826 webkit_web_context_set_spell_checking_enabled(
827 webkit_web_view_get_context(c->view), a->i);
828 return; /* do not update */
830 return; /* do nothing */
832 webkit_web_context_set_tls_errors_policy(
833 webkit_web_view_get_context(c->view), a->i ?
834 WEBKIT_TLS_ERRORS_POLICY_FAIL :
835 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
838 webkit_user_content_manager_remove_all_style_sheets(
839 webkit_web_view_get_user_content_manager(c->view));
841 setstyle(c, getstyle(geturi(c)));
845 webkit_settings_set_enable_webgl(s, a->i);
848 webkit_web_view_set_zoom_level(c->view, a->f);
849 return; /* do not update */
851 return; /* do nothing */
860 getcert(const char *uri)
864 for (i = 0; i < LENGTH(certs); ++i) {
865 if (certs[i].regex &&
866 !regexec(&(certs[i].re), uri, 0, NULL, 0))
867 return certs[i].file;
874 setcert(Client *c, const char *uri)
876 const char *file = getcert(uri);
878 GTlsCertificate *cert;
883 if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
884 fprintf(stderr, "Could not read certificate file: %s\n", file);
888 if ((uri = strstr(uri, "https://"))) {
889 uri += sizeof("https://") - 1;
890 host = g_strndup(uri, strchr(uri, '/') - uri);
891 webkit_web_context_allow_tls_certificate_for_host(
892 webkit_web_view_get_context(c->view), cert, host);
896 g_object_unref(cert);
901 getstyle(const char *uri)
908 for (i = 0; i < LENGTH(styles); ++i) {
909 if (styles[i].regex &&
910 !regexec(&(styles[i].re), uri, 0, NULL, 0))
911 return styles[i].file;
918 setstyle(Client *c, const char *file)
922 if (!g_file_get_contents(file, &style, NULL, NULL)) {
923 fprintf(stderr, "Could not read style file: %s\n", file);
927 webkit_user_content_manager_add_style_sheet(
928 webkit_web_view_get_user_content_manager(c->view),
929 webkit_user_style_sheet_new(style,
930 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
931 WEBKIT_USER_STYLE_LEVEL_USER,
943 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
944 evalscript(c, "%s", script);
949 evalscript(Client *c, const char *jsstr, ...)
955 script = g_strdup_vprintf(jsstr, ap);
958 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
963 updatewinid(Client *c)
965 snprintf(winid, LENGTH(winid), "%lu", c->xid);
969 handleplumb(Client *c, const char *uri)
971 Arg a = (Arg)PLUMB(uri);
976 newwindow(Client *c, const Arg *a, int noembed)
980 const char *cmd[29], *uri;
981 const Arg arg = { .v = cmd };
985 cmd[i++] = curconfig[CookiePolicies].val.v;
986 cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
987 if (cookiefile && g_strcmp0(cookiefile, "")) {
989 cmd[i++] = cookiefile;
991 if (stylefile && g_strcmp0(stylefile, "")) {
993 cmd[i++] = stylefile;
995 cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
996 if (embed && !noembed) {
998 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1001 cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
1002 cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
1003 cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
1004 cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
1005 cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
1006 cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
1007 cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
1008 if (scriptfile && g_strcmp0(scriptfile, "")) {
1010 cmd[i++] = scriptfile;
1012 cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
1013 cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
1014 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1016 cmd[i++] = fulluseragent;
1020 cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
1021 /* do not keep zoom level */
1031 spawn(Client *c, const Arg *a)
1035 close(ConnectionNumber(dpy));
1037 execvp(((char **)a->v)[0], (char **)a->v);
1038 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
1045 destroyclient(Client *c)
1049 webkit_web_view_stop_loading(c->view);
1050 /* Not needed, has already been called
1051 gtk_widget_destroy(c->win);
1054 for (p = clients; p && p->next != c; p = p->next)
1067 destroyclient(clients);
1076 newview(Client *c, WebKitWebView *rv)
1079 WebKitSettings *settings;
1080 WebKitUserContentManager *contentmanager;
1081 WebKitWebContext *context;
1082 WebKitCookieManager *cookiemanager;
1086 v = WEBKIT_WEB_VIEW(
1087 webkit_web_view_new_with_related_view(rv));
1089 settings = webkit_settings_new_with_settings(
1090 "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1091 "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1092 "auto-load-images", curconfig[LoadImages].val.i,
1093 "default-charset", curconfig[DefaultCharset].val.v,
1094 "default-font-size", curconfig[FontSize].val.i,
1095 "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
1096 "enable-developer-extras", curconfig[Inspector].val.i,
1097 "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
1098 "enable-frame-flattening", curconfig[FrameFlattening].val.i,
1099 "enable-html5-database", curconfig[DiskCache].val.i,
1100 "enable-html5-local-storage", curconfig[DiskCache].val.i,
1101 "enable-java", curconfig[Java].val.i,
1102 "enable-javascript", curconfig[JavaScript].val.i,
1103 "enable-plugins", curconfig[Plugins].val.i,
1104 "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
1105 "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
1106 "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
1107 "enable-webgl", curconfig[WebGL].val.i,
1108 "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
1110 /* For more interesting settings, have a look at
1111 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
1113 if (strcmp(fulluseragent, "")) {
1114 webkit_settings_set_user_agent(settings, fulluseragent);
1115 } else if (surfuseragent) {
1116 webkit_settings_set_user_agent_with_application_details(
1117 settings, "Surf", VERSION);
1119 useragent = webkit_settings_get_user_agent(settings);
1121 contentmanager = webkit_user_content_manager_new();
1123 context = webkit_web_context_new_with_website_data_manager(
1124 webkit_website_data_manager_new(
1125 "base-cache-directory", cachedir,
1126 "base-data-directory", cachedir,
1129 cookiemanager = webkit_web_context_get_cookie_manager(context);
1131 /* rendering process model, can be a shared unique one
1132 * or one for each view */
1133 webkit_web_context_set_process_model(context,
1134 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1136 webkit_web_context_set_tls_errors_policy(context,
1137 curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
1138 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1140 webkit_web_context_set_cache_model(context,
1141 curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
1142 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1144 /* Currently only works with text file to be compatible with curl */
1145 webkit_cookie_manager_set_persistent_storage(cookiemanager,
1146 cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1148 webkit_cookie_manager_set_accept_policy(cookiemanager,
1149 cookiepolicy_get());
1151 webkit_web_context_set_preferred_languages(context,
1152 curconfig[PreferredLanguages].val.v);
1153 webkit_web_context_set_spell_checking_languages(context,
1154 curconfig[SpellLanguages].val.v);
1155 webkit_web_context_set_spell_checking_enabled(context,
1156 curconfig[SpellChecking].val.i);
1158 g_signal_connect(G_OBJECT(context), "download-started",
1159 G_CALLBACK(downloadstarted), c);
1160 g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
1161 G_CALLBACK(initwebextensions), c);
1163 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
1164 "settings", settings,
1165 "user-content-manager", contentmanager,
1166 "web-context", context,
1170 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
1171 G_CALLBACK(progresschanged), c);
1172 g_signal_connect(G_OBJECT(v), "notify::title",
1173 G_CALLBACK(titlechanged), c);
1174 g_signal_connect(G_OBJECT(v), "button-release-event",
1175 G_CALLBACK(buttonreleased), c);
1176 g_signal_connect(G_OBJECT(v), "close",
1177 G_CALLBACK(closeview), c);
1178 g_signal_connect(G_OBJECT(v), "create",
1179 G_CALLBACK(createview), c);
1180 g_signal_connect(G_OBJECT(v), "decide-policy",
1181 G_CALLBACK(decidepolicy), c);
1182 g_signal_connect(G_OBJECT(v), "insecure-content-detected",
1183 G_CALLBACK(insecurecontent), c);
1184 g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
1185 G_CALLBACK(loadfailedtls), c);
1186 g_signal_connect(G_OBJECT(v), "load-changed",
1187 G_CALLBACK(loadchanged), c);
1188 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
1189 G_CALLBACK(mousetargetchanged), c);
1190 g_signal_connect(G_OBJECT(v), "permission-request",
1191 G_CALLBACK(permissionrequested), c);
1192 g_signal_connect(G_OBJECT(v), "ready-to-show",
1193 G_CALLBACK(showview), c);
1194 g_signal_connect(G_OBJECT(v), "web-process-terminated",
1195 G_CALLBACK(webprocessterminated), c);
1201 initwebextensions(WebKitWebContext *wc, Client *c)
1203 webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
1207 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
1211 switch (webkit_navigation_action_get_navigation_type(a)) {
1212 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1214 * popup windows of type “other” are almost always triggered
1215 * by user gesture, so inverse the logic here
1217 /* instead of this, compare destination uri to mouse-over uri for validating window */
1218 if (webkit_navigation_action_is_user_gesture(a))
1220 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1221 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1222 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1223 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1224 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1231 return GTK_WIDGET(n->view);
1235 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
1237 WebKitHitTestResultContext element;
1240 element = webkit_hit_test_result_get_context(c->mousepos);
1242 for (i = 0; i < LENGTH(buttons); ++i) {
1243 if (element & buttons[i].target &&
1244 e->button.button == buttons[i].button &&
1245 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
1247 buttons[i].func(c, &buttons[i].arg, c->mousepos);
1248 return buttons[i].stopevent;
1256 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1258 Client *c = (Client *)d;
1262 if (((XEvent *)e)->type == PropertyNotify) {
1263 ev = &((XEvent *)e)->xproperty;
1264 if (ev->state == PropertyNewValue) {
1265 if (ev->atom == atoms[AtomFind]) {
1268 return GDK_FILTER_REMOVE;
1269 } else if (ev->atom == atoms[AtomGo]) {
1270 a.v = getatom(c, AtomGo);
1273 return GDK_FILTER_REMOVE;
1277 return GDK_FILTER_CONTINUE;
1281 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1286 case GDK_ENTER_NOTIFY:
1287 c->overtitle = c->targeturi;
1291 if (!curconfig[KioskMode].val.i) {
1292 for (i = 0; i < LENGTH(keys); ++i) {
1293 if (gdk_keyval_to_lower(e->key.keyval) ==
1295 CLEANMASK(e->key.state) == keys[i].mod &&
1298 keys[i].func(c, &(keys[i].arg));
1303 case GDK_LEAVE_NOTIFY:
1304 c->overtitle = NULL;
1307 case GDK_WINDOW_STATE:
1308 if (e->window_state.changed_mask ==
1309 GDK_WINDOW_STATE_FULLSCREEN)
1310 c->fullscreen = e->window_state.new_window_state &
1311 GDK_WINDOW_STATE_FULLSCREEN;
1321 showview(WebKitWebView *v, Client *c)
1323 GdkRGBA bgcolor = { 0 };
1326 c->finder = webkit_web_view_get_find_controller(c->view);
1327 c->inspector = webkit_web_view_get_inspector(c->view);
1329 c->win = createwindow(c);
1331 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1332 gtk_widget_show_all(c->win);
1333 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1335 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1336 c->xid = gdk_x11_window_get_xid(gwin);
1339 gdk_display_sync(gtk_widget_get_display(c->win));
1344 if (curconfig[HideBackground].val.i)
1345 webkit_web_view_set_background_color(c->view, &bgcolor);
1347 if (!curconfig[KioskMode].val.i) {
1348 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1349 gdk_window_add_filter(gwin, processx, c);
1352 if (curconfig[RunInFullscreen].val.i)
1353 togglefullscreen(c, NULL);
1355 if (curconfig[ZoomLevel].val.f != 1.0)
1356 webkit_web_view_set_zoom_level(c->view,
1357 curconfig[ZoomLevel].val.f);
1359 setatom(c, AtomFind, "");
1360 setatom(c, AtomUri, "about:blank");
1364 createwindow(Client *c)
1370 w = gtk_plug_new(embed);
1372 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1374 wmstr = g_path_get_basename(argv0);
1375 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
1378 wmstr = g_strdup_printf("%s[%lu]", "Surf",
1379 webkit_web_view_get_page_id(c->view));
1380 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1383 gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
1386 g_signal_connect(G_OBJECT(w), "destroy",
1387 G_CALLBACK(destroywin), c);
1388 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1389 G_CALLBACK(winevent), c);
1390 g_signal_connect(G_OBJECT(w), "key-press-event",
1391 G_CALLBACK(winevent), c);
1392 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1393 G_CALLBACK(winevent), c);
1394 g_signal_connect(G_OBJECT(w), "window-state-event",
1395 G_CALLBACK(winevent), c);
1401 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
1402 GTlsCertificateFlags err, Client *c)
1404 GString *errmsg = g_string_new(NULL);
1407 c->failedcert = g_object_ref(cert);
1411 if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
1412 g_string_append(errmsg,
1413 "The signing certificate authority is not known.<br>");
1414 if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
1415 g_string_append(errmsg,
1416 "The certificate does not match the expected identity "
1417 "of the site that it was retrieved from.<br>");
1418 if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
1419 g_string_append(errmsg,
1420 "The certificate's activation time "
1421 "is still in the future.<br>");
1422 if (err & G_TLS_CERTIFICATE_EXPIRED)
1423 g_string_append(errmsg, "The certificate has expired.<br>");
1424 if (err & G_TLS_CERTIFICATE_REVOKED)
1425 g_string_append(errmsg,
1426 "The certificate has been revoked according to "
1427 "the GTlsConnection's certificate revocation list.<br>");
1428 if (err & G_TLS_CERTIFICATE_INSECURE)
1429 g_string_append(errmsg,
1430 "The certificate's algorithm is considered insecure.<br>");
1431 if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
1432 g_string_append(errmsg,
1433 "Some error occurred validating the certificate.<br>");
1435 g_object_get(cert, "certificate-pem", &pem, NULL);
1436 html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
1437 "<p>You can inspect the following certificate "
1438 "with Ctrl-t (default keybinding).</p>"
1439 "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
1441 g_string_free(errmsg, TRUE);
1443 webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
1450 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1452 const char *uri = geturi(c);
1455 case WEBKIT_LOAD_STARTED:
1456 setatom(c, AtomUri, uri);
1458 c->https = c->insecure = 0;
1459 seturiparameters(c, uri, loadtransient);
1463 g_clear_object(&c->failedcert);
1465 case WEBKIT_LOAD_REDIRECTED:
1466 setatom(c, AtomUri, uri);
1468 seturiparameters(c, uri, loadtransient);
1470 case WEBKIT_LOAD_COMMITTED:
1471 seturiparameters(c, uri, loadcommitted);
1472 c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
1475 case WEBKIT_LOAD_FINISHED:
1476 seturiparameters(c, uri, loadfinished);
1477 /* Disabled until we write some WebKitWebExtension for
1478 * manipulating the DOM directly.
1479 evalscript(c, "document.documentElement.style.overflow = '%s'",
1480 enablescrollbars ? "auto" : "hidden");
1489 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1491 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1497 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1499 c->title = webkit_web_view_get_title(c->view);
1504 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1507 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1509 /* Keep the hit test to know where is the pointer on the next click */
1513 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1514 else if (hc & OnImg)
1515 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1516 else if (hc & OnMedia)
1517 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1519 c->targeturi = NULL;
1521 c->overtitle = c->targeturi;
1526 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1528 ParamName param = ParameterLast;
1530 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1531 param = Geolocation;
1532 } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
1533 if (webkit_user_media_permission_is_for_audio_device(
1534 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1535 param = AccessMicrophone;
1536 else if (webkit_user_media_permission_is_for_video_device(
1537 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1538 param = AccessWebcam;
1543 if (curconfig[param].val.i)
1544 webkit_permission_request_allow(r);
1546 webkit_permission_request_deny(r);
1552 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1553 WebKitPolicyDecisionType dt, Client *c)
1556 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1557 decidenavigation(d, c);
1559 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1560 decidenewwindow(d, c);
1562 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1563 decideresource(d, c);
1566 webkit_policy_decision_ignore(d);
1573 decidenavigation(WebKitPolicyDecision *d, Client *c)
1575 WebKitNavigationAction *a =
1576 webkit_navigation_policy_decision_get_navigation_action(
1577 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1579 switch (webkit_navigation_action_get_navigation_type(a)) {
1580 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1581 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1582 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1583 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1584 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1585 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1587 /* Do not navigate to links with a "_blank" target (popup) */
1588 if (webkit_navigation_policy_decision_get_frame_name(
1589 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1590 webkit_policy_decision_ignore(d);
1592 /* Filter out navigation to different domain ? */
1593 /* get action→urirequest, copy and load in new window+view
1594 * on Ctrl+Click ? */
1595 webkit_policy_decision_use(d);
1602 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1605 WebKitNavigationAction *a =
1606 webkit_navigation_policy_decision_get_navigation_action(
1607 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1610 switch (webkit_navigation_action_get_navigation_type(a)) {
1611 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1612 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1613 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1614 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1615 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1616 /* Filter domains here */
1617 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1618 * test for link clicked but no button ? */
1619 arg.v = webkit_uri_request_get_uri(
1620 webkit_navigation_action_get_request(a));
1621 newwindow(c, &arg, 0);
1623 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1628 webkit_policy_decision_ignore(d);
1632 decideresource(WebKitPolicyDecision *d, Client *c)
1635 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1636 WebKitURIResponse *res =
1637 webkit_response_policy_decision_get_response(r);
1638 const gchar *uri = webkit_uri_response_get_uri(res);
1640 if (g_str_has_suffix(uri, "/favicon.ico")) {
1641 webkit_policy_decision_ignore(d);
1645 if (!g_str_has_prefix(uri, "http://")
1646 && !g_str_has_prefix(uri, "https://")
1647 && !g_str_has_prefix(uri, "about:")
1648 && !g_str_has_prefix(uri, "file://")
1649 && !g_str_has_prefix(uri, "data:")
1650 && !g_str_has_prefix(uri, "blob:")
1651 && strlen(uri) > 0) {
1652 for (i = 0; i < strlen(uri); i++) {
1653 if (!g_ascii_isprint(uri[i])) {
1659 handleplumb(c, uri);
1660 webkit_policy_decision_ignore(d);
1665 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1666 webkit_policy_decision_use(d);
1668 webkit_policy_decision_ignore(d);
1674 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
1680 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1682 g_signal_connect(G_OBJECT(d), "notify::response",
1683 G_CALLBACK(responsereceived), c);
1687 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1689 download(c, webkit_download_get_response(d));
1690 webkit_download_cancel(d);
1694 download(Client *c, WebKitURIResponse *r)
1696 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1701 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
1704 fprintf(stderr, "web process terminated: %s\n",
1705 r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
1710 closeview(WebKitWebView *v, Client *c)
1712 gtk_widget_destroy(c->win);
1716 destroywin(GtkWidget* w, Client *c)
1724 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1726 Arg a = {.v = text };
1728 loaduri((Client *) d, &a);
1732 reload(Client *c, const Arg *a)
1735 webkit_web_view_reload_bypass_cache(c->view);
1737 webkit_web_view_reload(c->view);
1741 print(Client *c, const Arg *a)
1743 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1744 GTK_WINDOW(c->win));
1748 showcert(Client *c, const Arg *a)
1750 GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
1751 GcrCertificate *gcrt;
1754 GcrCertificateWidget *wcert;
1759 g_object_get(cert, "certificate", &crt, NULL);
1760 gcrt = gcr_simple_certificate_new(crt->data, crt->len);
1761 g_byte_array_unref(crt);
1763 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1764 wcert = gcr_certificate_widget_new(gcrt);
1765 g_object_unref(gcrt);
1767 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
1768 gtk_widget_show_all(win);
1772 clipboard(Client *c, const Arg *a)
1774 if (a->i) { /* load clipboard uri */
1775 gtk_clipboard_request_text(gtk_clipboard_get(
1776 GDK_SELECTION_PRIMARY),
1778 } else { /* copy uri */
1779 gtk_clipboard_set_text(gtk_clipboard_get(
1780 GDK_SELECTION_PRIMARY), c->targeturi
1781 ? c->targeturi : geturi(c), -1);
1786 zoom(Client *c, const Arg *a)
1789 webkit_web_view_set_zoom_level(c->view,
1790 curconfig[ZoomLevel].val.f + 0.1);
1792 webkit_web_view_set_zoom_level(c->view,
1793 curconfig[ZoomLevel].val.f - 0.1);
1795 webkit_web_view_set_zoom_level(c->view, 1.0);
1797 curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
1801 scroll(Client *c, const Arg *a)
1803 GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
1805 gdk_event_set_device(ev, gdkkb);
1806 ev->key.window = gtk_widget_get_window(GTK_WIDGET(c->win));
1807 ev->key.state = GDK_CONTROL_MASK;
1808 ev->key.time = GDK_CURRENT_TIME;
1812 ev->key.keyval = GDK_KEY_Down;
1815 ev->key.keyval = GDK_KEY_Page_Down;
1818 ev->key.keyval = GDK_KEY_Left;
1821 ev->key.keyval = GDK_KEY_Right;
1824 ev->key.keyval = GDK_KEY_Page_Up;
1827 ev->key.keyval = GDK_KEY_Up;
1835 navigate(Client *c, const Arg *a)
1838 webkit_web_view_go_back(c->view);
1840 webkit_web_view_go_forward(c->view);
1844 stop(Client *c, const Arg *a)
1846 webkit_web_view_stop_loading(c->view);
1850 toggle(Client *c, const Arg *a)
1852 curconfig[a->i].val.i ^= 1;
1853 setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
1857 togglefullscreen(Client *c, const Arg *a)
1859 /* toggling value is handled in winevent() */
1861 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1863 gtk_window_fullscreen(GTK_WINDOW(c->win));
1867 togglecookiepolicy(Client *c, const Arg *a)
1870 cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
1872 setparameter(c, 0, CookiePolicies, NULL);
1876 toggleinspector(Client *c, const Arg *a)
1878 if (webkit_web_inspector_is_attached(c->inspector))
1879 webkit_web_inspector_close(c->inspector);
1880 else if (curconfig[Inspector].val.i)
1881 webkit_web_inspector_show(c->inspector);
1885 find(Client *c, const Arg *a)
1891 webkit_find_controller_search_next(c->finder);
1893 webkit_find_controller_search_previous(c->finder);
1895 s = getatom(c, AtomFind);
1896 f = webkit_find_controller_get_search_text(c->finder);
1898 if (g_strcmp0(f, s) == 0) /* reset search */
1899 webkit_find_controller_search(c->finder, "", findopts,
1902 webkit_find_controller_search(c->finder, s, findopts,
1905 if (strcmp(s, "") == 0)
1906 webkit_find_controller_search_finish(c->finder);
1911 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1917 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1921 arg.v = webkit_hit_test_result_get_link_uri(h);
1922 newwindow(c, &arg, a->i);
1926 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1930 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1935 main(int argc, char *argv[])
1940 memset(&arg, 0, sizeof(arg));
1942 /* command line args */
1945 defconfig[CookiePolicies].val.v = EARGF(usage());
1946 defconfig[CookiePolicies].prio = 2;
1949 defconfig[ScrollBars].val.i = 0;
1950 defconfig[ScrollBars].prio = 2;
1953 defconfig[ScrollBars].val.i = 1;
1954 defconfig[ScrollBars].prio = 2;
1957 cookiefile = EARGF(usage());
1960 stylefile = EARGF(usage());
1963 defconfig[DiskCache].val.i = 0;
1964 defconfig[DiskCache].prio = 2;
1967 defconfig[DiskCache].val.i = 1;
1968 defconfig[DiskCache].prio = 2;
1971 embed = strtol(EARGF(usage()), NULL, 0);
1974 defconfig[RunInFullscreen].val.i = 0;
1975 defconfig[RunInFullscreen].prio = 2;
1978 defconfig[RunInFullscreen].val.i = 1;
1979 defconfig[RunInFullscreen].prio = 2;
1982 defconfig[Geolocation].val.i = 0;
1983 defconfig[Geolocation].prio = 2;
1986 defconfig[Geolocation].val.i = 1;
1987 defconfig[Geolocation].prio = 2;
1990 defconfig[LoadImages].val.i = 0;
1991 defconfig[LoadImages].prio = 2;
1994 defconfig[LoadImages].val.i = 1;
1995 defconfig[LoadImages].prio = 2;
1998 defconfig[KioskMode].val.i = 0;
1999 defconfig[KioskMode].prio = 2;
2002 defconfig[KioskMode].val.i = 1;
2003 defconfig[KioskMode].prio = 2;
2006 defconfig[Style].val.i = 0;
2007 defconfig[Style].prio = 2;
2010 defconfig[Style].val.i = 1;
2011 defconfig[Style].prio = 2;
2014 defconfig[Inspector].val.i = 0;
2015 defconfig[Inspector].prio = 2;
2018 defconfig[Inspector].val.i = 1;
2019 defconfig[Inspector].prio = 2;
2022 defconfig[Plugins].val.i = 0;
2023 defconfig[Plugins].prio = 2;
2026 defconfig[Plugins].val.i = 1;
2027 defconfig[Plugins].prio = 2;
2030 scriptfile = EARGF(usage());
2033 defconfig[JavaScript].val.i = 0;
2034 defconfig[JavaScript].prio = 2;
2037 defconfig[JavaScript].val.i = 1;
2038 defconfig[JavaScript].prio = 2;
2041 defconfig[StrictTLS].val.i = 0;
2042 defconfig[StrictTLS].prio = 2;
2045 defconfig[StrictTLS].val.i = 1;
2046 defconfig[StrictTLS].prio = 2;
2049 fulluseragent = EARGF(usage());
2052 die("surf-"VERSION", see LICENSE for © details\n");
2057 defconfig[Certificate].val.i = 0;
2058 defconfig[Certificate].prio = 2;
2061 defconfig[Certificate].val.i = 1;
2062 defconfig[Certificate].prio = 2;
2065 defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
2066 defconfig[ZoomLevel].prio = 2;
2074 arg.v = "about:blank";
2077 c = newclient(NULL);