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>
26 #include <JavaScriptCore/JavaScript.h>
27 #include <webkit2/webkit2.h>
29 #include <X11/Xatom.h>
35 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
36 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
38 enum { AtomFind, AtomGo, AtomUri, AtomLast };
41 OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
42 OnLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
43 OnImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
44 OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
45 OnEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
46 OnBar = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
47 OnSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
48 OnAny = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
99 typedef struct Client {
102 WebKitWebInspector *inspector;
103 WebKitFindController *finder;
104 WebKitHitTestResult *mousepos;
105 GTlsCertificate *cert, *failedcert;
106 GTlsCertificateFlags tlserr;
108 unsigned long pageid;
109 int progress, fullscreen, https, insecure, errorpage;
110 const char *title, *overtitle, *targeturi;
118 void (*func)(Client *c, const Arg *a);
126 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
128 unsigned int stopevent;
139 Parameter config[ParameterLast];
150 static void usage(void);
151 static void setup(void);
152 static void sigchld(int unused);
153 static void sighup(int unused);
154 static char *buildfile(const char *path);
155 static char *buildpath(const char *path);
156 static char *untildepath(const char *path);
157 static const char *getuserhomedir(const char *user);
158 static const char *getcurrentuserhomedir(void);
159 static Client *newclient(Client *c);
160 static void loaduri(Client *c, const Arg *a);
161 static const char *geturi(Client *c);
162 static void setatom(Client *c, int a, const char *v);
163 static const char *getatom(Client *c, int a);
164 static void updatetitle(Client *c);
165 static void gettogglestats(Client *c);
166 static void getpagestats(Client *c);
167 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
168 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
169 static void seturiparameters(Client *c, const char *uri, ParamName *params);
170 static void setparameter(Client *c, int refresh, ParamName p, const Arg *a);
171 static const char *getcert(const char *uri);
172 static void setcert(Client *c, const char *file);
173 static const char *getstyle(const char *uri);
174 static void setstyle(Client *c, const char *file);
175 static void runscript(Client *c);
176 static void evalscript(Client *c, const char *jsstr, ...);
177 static void updatewinid(Client *c);
178 static void handleplumb(Client *c, const char *uri);
179 static void newwindow(Client *c, const Arg *a, int noembed);
180 static void spawn(Client *c, const Arg *a);
181 static void msgext(Client *c, char type, const Arg *a);
182 static void destroyclient(Client *c);
183 static void cleanup(void);
186 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
187 static void initwebextensions(WebKitWebContext *wc, Client *c);
188 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
190 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
191 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
193 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
194 static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
195 static void showview(WebKitWebView *v, Client *c);
196 static GtkWidget *createwindow(Client *c);
197 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
198 GTlsCertificate *cert,
199 GTlsCertificateFlags err, Client *c);
200 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
201 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
202 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
203 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
204 guint modifiers, Client *c);
205 static gboolean permissionrequested(WebKitWebView *v,
206 WebKitPermissionRequest *r, Client *c);
207 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
208 WebKitPolicyDecisionType dt, Client *c);
209 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
210 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
211 static void decideresource(WebKitPolicyDecision *d, Client *c);
212 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
214 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
216 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
217 static void download(Client *c, WebKitURIResponse *r);
218 static void webprocessterminated(WebKitWebView *v,
219 WebKitWebProcessTerminationReason r,
221 static void closeview(WebKitWebView *v, Client *c);
222 static void destroywin(GtkWidget* w, Client *c);
223 static gchar *parseuri(const gchar *uri);
226 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
227 static void reload(Client *c, const Arg *a);
228 static void print(Client *c, const Arg *a);
229 static void showcert(Client *c, const Arg *a);
230 static void clipboard(Client *c, const Arg *a);
231 static void zoom(Client *c, const Arg *a);
232 static void scrollv(Client *c, const Arg *a);
233 static void scrollh(Client *c, const Arg *a);
234 static void navigate(Client *c, const Arg *a);
235 static void stop(Client *c, const Arg *a);
236 static void toggle(Client *c, const Arg *a);
237 static void togglefullscreen(Client *c, const Arg *a);
238 static void togglecookiepolicy(Client *c, const Arg *a);
239 static void toggleinspector(Client *c, const Arg *a);
240 static void find(Client *c, const Arg *a);
243 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
244 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
245 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
247 static char winid[64];
248 static char togglestats[12];
249 static char pagestats[2];
250 static Atom atoms[AtomLast];
253 static int cookiepolicy;
255 static Client *clients;
256 static GdkDevice *gdkkb;
257 static char *stylefile;
258 static const char *useragent;
259 static Parameter *curconfig;
260 static int modparams[ParameterLast];
261 static int pipein[2], pipeout[2];
264 static ParamName loadtransient[] = {
278 static ParamName loadcommitted[] = {
304 static ParamName loadfinished[] = {
308 /* configuration, allows nested code to access above variables */
314 die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
315 "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
316 "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
326 /* clean up any zombies immediately */
328 if (signal(SIGHUP, sighup) == SIG_ERR)
329 die("Can't install SIGHUP handler");
331 if (!(dpy = XOpenDisplay(NULL)))
332 die("Can't open default display");
335 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
336 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
337 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
339 gtk_init(NULL, NULL);
341 gdpy = gdk_display_get_default();
343 curconfig = defconfig;
346 cookiefile = buildfile(cookiefile);
347 scriptfile = buildfile(scriptfile);
348 cachedir = buildpath(cachedir);
349 certdir = buildpath(certdir);
351 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
353 if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
354 fputs("Unable to create pipes\n", stderr);
356 gchanin = g_io_channel_unix_new(pipein[0]);
357 g_io_channel_set_encoding(gchanin, NULL, NULL);
358 g_io_channel_set_close_on_unref(gchanin, TRUE);
359 g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
363 for (i = 0; i < LENGTH(certs); ++i) {
364 if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
365 certs[i].file = g_strconcat(certdir, "/", certs[i].file,
368 fprintf(stderr, "Could not compile regex: %s\n",
370 certs[i].regex = NULL;
375 styledir = buildpath(styledir);
376 for (i = 0; i < LENGTH(styles); ++i) {
377 if (!regcomp(&(styles[i].re), styles[i].regex,
379 styles[i].file = g_strconcat(styledir, "/",
380 styles[i].file, NULL);
382 fprintf(stderr, "Could not compile regex: %s\n",
384 styles[i].regex = NULL;
389 stylefile = buildfile(stylefile);
392 for (i = 0; i < LENGTH(uriparams); ++i) {
393 if (regcomp(&(uriparams[i].re), uriparams[i].uri,
395 fprintf(stderr, "Could not compile regex: %s\n",
397 uriparams[i].uri = NULL;
401 /* copy default parameters with higher priority */
402 for (j = 0; j < ParameterLast; ++j) {
403 if (defconfig[j].prio >= uriparams[i].config[j].prio)
404 uriparams[i].config[j] = defconfig[j];
412 if (signal(SIGCHLD, sigchld) == SIG_ERR)
413 die("Can't install SIGCHLD handler");
414 while (waitpid(-1, NULL, WNOHANG) > 0)
424 for (c = clients; c; c = c->next)
429 buildfile(const char *path)
431 char *dname, *bname, *bpath, *fpath;
434 dname = g_path_get_dirname(path);
435 bname = g_path_get_basename(path);
437 bpath = buildpath(dname);
440 fpath = g_build_filename(bpath, bname, NULL);
444 if (!(f = fopen(fpath, "a")))
445 die("Could not open file: %s\n", fpath);
447 g_chmod(fpath, 0600); /* always */
454 getuserhomedir(const char *user)
456 struct passwd *pw = getpwnam(user);
459 die("Can't get user %s login information.\n", user);
465 getcurrentuserhomedir(void)
471 homedir = getenv("HOME");
475 user = getenv("USER");
477 return getuserhomedir(user);
479 pw = getpwuid(getuid());
481 die("Can't get current user home directory\n");
487 buildpath(const char *path)
492 apath = untildepath(path);
494 apath = g_strdup(path);
496 /* creating directory */
497 if (g_mkdir_with_parents(apath, 0700) < 0)
498 die("Could not access directory: %s\n", apath);
500 fpath = realpath(apath, NULL);
507 untildepath(const char *path)
509 char *apath, *name, *p;
512 if (path[1] == '/' || path[1] == '\0') {
513 p = (char *)&path[1];
514 homedir = getcurrentuserhomedir();
516 if ((p = strchr(path, '/')))
517 name = g_strndup(&path[1], p - (path + 1));
519 name = g_strdup(&path[1]);
521 homedir = getuserhomedir(name);
524 apath = g_build_filename(homedir, p, NULL);
529 newclient(Client *rc)
533 if (!(c = calloc(1, sizeof(Client))))
534 die("Cannot malloc!\n");
540 c->view = newview(c, rc ? rc->view : NULL);
546 loaduri(Client *c, const Arg *a)
549 char *url, *path, *apath;
550 const char *uri = a->v;
552 if (g_strcmp0(uri, "") == 0)
555 if (g_str_has_prefix(uri, "http://") ||
556 g_str_has_prefix(uri, "https://") ||
557 g_str_has_prefix(uri, "file://") ||
558 g_str_has_prefix(uri, "about:")) {
562 apath = untildepath(uri);
565 if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
566 url = g_strdup_printf("file://%s", path);
575 setatom(c, AtomUri, url);
577 if (strcmp(url, geturi(c)) == 0) {
580 webkit_web_view_load_uri(c->view, url);
592 if (!(uri = webkit_web_view_get_uri(c->view)))
598 setatom(Client *c, int a, const char *v)
600 XChangeProperty(dpy, c->xid,
601 atoms[a], XA_STRING, 8, PropModeReplace,
602 (unsigned char *)v, strlen(v) + 1);
607 getatom(Client *c, int a)
609 static char buf[BUFSIZ];
612 unsigned long ldummy;
613 unsigned char *p = NULL;
616 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
617 &adummy, &idummy, &ldummy, &ldummy, &p);
619 strncpy(buf, (char *)p, LENGTH(buf) - 1);
628 updatetitle(Client *c)
631 const char *name = c->overtitle ? c->overtitle :
632 c->title ? c->title : "";
634 if (curconfig[ShowIndicators].val.i) {
638 if (c->progress != 100)
639 title = g_strdup_printf("[%i%%] %s:%s | %s",
640 c->progress, togglestats, pagestats, name);
642 title = g_strdup_printf("%s:%s | %s",
643 togglestats, pagestats, name);
645 gtk_window_set_title(GTK_WINDOW(c->win), title);
648 gtk_window_set_title(GTK_WINDOW(c->win), name);
653 gettogglestats(Client *c)
655 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
656 togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
657 togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
658 togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
659 togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
660 togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
661 togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
662 togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
663 togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
664 togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
665 togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
666 togglestats[11] = '\0';
670 getpagestats(Client *c)
673 pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
679 WebKitCookieAcceptPolicy
680 cookiepolicy_get(void)
682 switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
684 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
686 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
687 default: /* fallthrough */
689 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
694 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
697 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
699 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
701 default: /* fallthrough */
702 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
708 seturiparameters(Client *c, const char *uri, ParamName *params)
710 Parameter *config, *uriconfig = NULL;
713 for (i = 0; i < LENGTH(uriparams); ++i) {
714 if (uriparams[i].uri &&
715 !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
716 uriconfig = uriparams[i].config;
721 curconfig = uriconfig ? uriconfig : defconfig;
723 for (i = 0; (p = params[i]) != ParameterLast; ++i) {
725 default: /* FALLTHROUGH */
726 if (!(defconfig[p].prio < curconfig[p].prio ||
727 defconfig[p].prio < modparams[p]))
732 setparameter(c, 0, p, &curconfig[p].val);
738 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
740 GdkRGBA bgcolor = { 0 };
741 WebKitSettings *s = webkit_web_view_get_settings(c->view);
743 modparams[p] = curconfig[p].prio;
746 case AcceleratedCanvas:
747 webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
749 case AccessMicrophone:
750 return; /* do nothing */
752 return; /* do nothing */
754 webkit_settings_set_enable_caret_browsing(s, a->i);
759 setcert(c, geturi(c));
760 return; /* do not update */
762 webkit_cookie_manager_set_accept_policy(
763 webkit_web_context_get_cookie_manager(
764 webkit_web_view_get_context(c->view)),
769 webkit_web_context_set_cache_model(
770 webkit_web_view_get_context(c->view), a->i ?
771 WEBKIT_CACHE_MODEL_WEB_BROWSER :
772 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
773 return; /* do not update */
775 webkit_settings_set_default_charset(s, a->v);
776 return; /* do not update */
778 webkit_settings_set_enable_dns_prefetching(s, a->i);
779 return; /* do not update */
780 case FileURLsCrossAccess:
781 webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
782 webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
783 return; /* do not update */
785 webkit_settings_set_default_font_size(s, a->i);
786 return; /* do not update */
787 case FrameFlattening:
788 webkit_settings_set_enable_frame_flattening(s, a->i);
795 webkit_web_view_set_background_color(c->view, &bgcolor);
796 return; /* do not update */
798 webkit_settings_set_enable_developer_extras(s, a->i);
799 return; /* do not update */
801 webkit_settings_set_enable_java(s, a->i);
802 return; /* do not update */
804 webkit_settings_set_enable_javascript(s, a->i);
807 return; /* do nothing */
809 webkit_settings_set_auto_load_images(s, a->i);
811 case MediaManualPlay:
812 webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
815 webkit_settings_set_enable_plugins(s, a->i);
817 case PreferredLanguages:
818 return; /* do nothing */
819 case RunInFullscreen:
820 return; /* do nothing */
822 /* Disabled until we write some WebKitWebExtension for
823 * manipulating the DOM directly.
824 enablescrollbars = !enablescrollbars;
825 evalscript(c, "document.documentElement.style.overflow = '%s'",
826 enablescrollbars ? "auto" : "hidden");
828 return; /* do not update */
831 case SmoothScrolling:
832 webkit_settings_set_enable_smooth_scrolling(s, a->i);
833 return; /* do not update */
835 webkit_settings_set_enable_site_specific_quirks(s, a->i);
838 webkit_web_context_set_spell_checking_enabled(
839 webkit_web_view_get_context(c->view), a->i);
840 return; /* do not update */
842 return; /* do nothing */
844 webkit_web_context_set_tls_errors_policy(
845 webkit_web_view_get_context(c->view), a->i ?
846 WEBKIT_TLS_ERRORS_POLICY_FAIL :
847 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
850 webkit_user_content_manager_remove_all_style_sheets(
851 webkit_web_view_get_user_content_manager(c->view));
853 setstyle(c, getstyle(geturi(c)));
857 webkit_settings_set_enable_webgl(s, a->i);
860 webkit_web_view_set_zoom_level(c->view, a->f);
861 return; /* do not update */
863 return; /* do nothing */
872 getcert(const char *uri)
876 for (i = 0; i < LENGTH(certs); ++i) {
877 if (certs[i].regex &&
878 !regexec(&(certs[i].re), uri, 0, NULL, 0))
879 return certs[i].file;
886 setcert(Client *c, const char *uri)
888 const char *file = getcert(uri);
890 GTlsCertificate *cert;
895 if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
896 fprintf(stderr, "Could not read certificate file: %s\n", file);
900 if ((uri = strstr(uri, "https://"))) {
901 uri += sizeof("https://") - 1;
902 host = g_strndup(uri, strchr(uri, '/') - uri);
903 webkit_web_context_allow_tls_certificate_for_host(
904 webkit_web_view_get_context(c->view), cert, host);
908 g_object_unref(cert);
913 getstyle(const char *uri)
920 for (i = 0; i < LENGTH(styles); ++i) {
921 if (styles[i].regex &&
922 !regexec(&(styles[i].re), uri, 0, NULL, 0))
923 return styles[i].file;
930 setstyle(Client *c, const char *file)
934 if (!g_file_get_contents(file, &style, NULL, NULL)) {
935 fprintf(stderr, "Could not read style file: %s\n", file);
939 webkit_user_content_manager_add_style_sheet(
940 webkit_web_view_get_user_content_manager(c->view),
941 webkit_user_style_sheet_new(style,
942 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
943 WEBKIT_USER_STYLE_LEVEL_USER,
955 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
956 evalscript(c, "%s", script);
961 evalscript(Client *c, const char *jsstr, ...)
967 script = g_strdup_vprintf(jsstr, ap);
970 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
975 updatewinid(Client *c)
977 snprintf(winid, LENGTH(winid), "%lu", c->xid);
981 handleplumb(Client *c, const char *uri)
983 Arg a = (Arg)PLUMB(uri);
988 newwindow(Client *c, const Arg *a, int noembed)
992 const char *cmd[29], *uri;
993 const Arg arg = { .v = cmd };
997 cmd[i++] = curconfig[CookiePolicies].val.v;
998 cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
999 if (cookiefile && g_strcmp0(cookiefile, "")) {
1001 cmd[i++] = cookiefile;
1003 if (stylefile && g_strcmp0(stylefile, "")) {
1005 cmd[i++] = stylefile;
1007 cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
1008 if (embed && !noembed) {
1010 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1013 cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
1014 cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
1015 cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
1016 cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
1017 cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
1018 cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
1019 cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
1020 if (scriptfile && g_strcmp0(scriptfile, "")) {
1022 cmd[i++] = scriptfile;
1024 cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
1025 cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
1026 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1028 cmd[i++] = fulluseragent;
1032 cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
1033 /* do not keep zoom level */
1043 spawn(Client *c, const Arg *a)
1047 close(ConnectionNumber(dpy));
1051 execvp(((char **)a->v)[0], (char **)a->v);
1052 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
1059 destroyclient(Client *c)
1063 webkit_web_view_stop_loading(c->view);
1064 /* Not needed, has already been called
1065 gtk_widget_destroy(c->win);
1068 for (p = clients; p && p->next != c; p = p->next)
1081 destroyclient(clients);
1093 newview(Client *c, WebKitWebView *rv)
1096 WebKitSettings *settings;
1097 WebKitWebContext *context;
1098 WebKitCookieManager *cookiemanager;
1099 WebKitUserContentManager *contentmanager;
1103 v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
1105 settings = webkit_settings_new_with_settings(
1106 "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1107 "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1108 "auto-load-images", curconfig[LoadImages].val.i,
1109 "default-charset", curconfig[DefaultCharset].val.v,
1110 "default-font-size", curconfig[FontSize].val.i,
1111 "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
1112 "enable-developer-extras", curconfig[Inspector].val.i,
1113 "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
1114 "enable-frame-flattening", curconfig[FrameFlattening].val.i,
1115 "enable-html5-database", curconfig[DiskCache].val.i,
1116 "enable-html5-local-storage", curconfig[DiskCache].val.i,
1117 "enable-java", curconfig[Java].val.i,
1118 "enable-javascript", curconfig[JavaScript].val.i,
1119 "enable-plugins", curconfig[Plugins].val.i,
1120 "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
1121 "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
1122 "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
1123 "enable-webgl", curconfig[WebGL].val.i,
1124 "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
1126 /* For more interesting settings, have a look at
1127 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
1129 if (strcmp(fulluseragent, "")) {
1130 webkit_settings_set_user_agent(settings, fulluseragent);
1131 } else if (surfuseragent) {
1132 webkit_settings_set_user_agent_with_application_details(
1133 settings, "Surf", VERSION);
1135 useragent = webkit_settings_get_user_agent(settings);
1137 contentmanager = webkit_user_content_manager_new();
1139 context = webkit_web_context_new_with_website_data_manager(
1140 webkit_website_data_manager_new(
1141 "base-cache-directory", cachedir,
1142 "base-data-directory", cachedir,
1145 cookiemanager = webkit_web_context_get_cookie_manager(context);
1147 /* rendering process model, can be a shared unique one
1148 * or one for each view */
1149 webkit_web_context_set_process_model(context,
1150 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1152 webkit_web_context_set_tls_errors_policy(context,
1153 curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
1154 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1156 webkit_web_context_set_cache_model(context,
1157 curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
1158 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1160 /* Currently only works with text file to be compatible with curl */
1161 webkit_cookie_manager_set_persistent_storage(cookiemanager,
1162 cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1164 webkit_cookie_manager_set_accept_policy(cookiemanager,
1165 cookiepolicy_get());
1167 webkit_web_context_set_preferred_languages(context,
1168 curconfig[PreferredLanguages].val.v);
1169 webkit_web_context_set_spell_checking_languages(context,
1170 curconfig[SpellLanguages].val.v);
1171 webkit_web_context_set_spell_checking_enabled(context,
1172 curconfig[SpellChecking].val.i);
1174 g_signal_connect(G_OBJECT(context), "download-started",
1175 G_CALLBACK(downloadstarted), c);
1176 g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
1177 G_CALLBACK(initwebextensions), c);
1179 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
1180 "settings", settings,
1181 "user-content-manager", contentmanager,
1182 "web-context", context,
1186 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
1187 G_CALLBACK(progresschanged), c);
1188 g_signal_connect(G_OBJECT(v), "notify::title",
1189 G_CALLBACK(titlechanged), c);
1190 g_signal_connect(G_OBJECT(v), "button-release-event",
1191 G_CALLBACK(buttonreleased), c);
1192 g_signal_connect(G_OBJECT(v), "close",
1193 G_CALLBACK(closeview), c);
1194 g_signal_connect(G_OBJECT(v), "create",
1195 G_CALLBACK(createview), c);
1196 g_signal_connect(G_OBJECT(v), "decide-policy",
1197 G_CALLBACK(decidepolicy), c);
1198 g_signal_connect(G_OBJECT(v), "insecure-content-detected",
1199 G_CALLBACK(insecurecontent), c);
1200 g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
1201 G_CALLBACK(loadfailedtls), c);
1202 g_signal_connect(G_OBJECT(v), "load-changed",
1203 G_CALLBACK(loadchanged), c);
1204 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
1205 G_CALLBACK(mousetargetchanged), c);
1206 g_signal_connect(G_OBJECT(v), "permission-request",
1207 G_CALLBACK(permissionrequested), c);
1208 g_signal_connect(G_OBJECT(v), "ready-to-show",
1209 G_CALLBACK(showview), c);
1210 g_signal_connect(G_OBJECT(v), "web-process-terminated",
1211 G_CALLBACK(webprocessterminated), c);
1217 readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
1219 static char msg[MSGBUFSZ], msgsz;
1220 GError *gerr = NULL;
1222 if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
1223 G_IO_STATUS_NORMAL) {
1224 fprintf(stderr, "surf: error reading pipe: %s\n",
1229 if ((msgsz = msg[0]) < 3) {
1230 fprintf(stderr, "surf: message too short: %d\n", msgsz);
1245 initwebextensions(WebKitWebContext *wc, Client *c)
1249 if (!pipeout[0] || !pipein[1])
1252 gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
1254 webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
1255 webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
1259 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
1263 switch (webkit_navigation_action_get_navigation_type(a)) {
1264 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1266 * popup windows of type “other” are almost always triggered
1267 * by user gesture, so inverse the logic here
1269 /* instead of this, compare destination uri to mouse-over uri for validating window */
1270 if (webkit_navigation_action_is_user_gesture(a))
1272 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1273 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1274 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1275 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1276 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1283 return GTK_WIDGET(n->view);
1287 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
1289 WebKitHitTestResultContext element;
1292 element = webkit_hit_test_result_get_context(c->mousepos);
1294 for (i = 0; i < LENGTH(buttons); ++i) {
1295 if (element & buttons[i].target &&
1296 e->button.button == buttons[i].button &&
1297 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
1299 buttons[i].func(c, &buttons[i].arg, c->mousepos);
1300 return buttons[i].stopevent;
1308 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1310 Client *c = (Client *)d;
1314 if (((XEvent *)e)->type == PropertyNotify) {
1315 ev = &((XEvent *)e)->xproperty;
1316 if (ev->state == PropertyNewValue) {
1317 if (ev->atom == atoms[AtomFind]) {
1320 return GDK_FILTER_REMOVE;
1321 } else if (ev->atom == atoms[AtomGo]) {
1322 a.v = getatom(c, AtomGo);
1325 return GDK_FILTER_REMOVE;
1329 return GDK_FILTER_CONTINUE;
1333 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1338 case GDK_ENTER_NOTIFY:
1339 c->overtitle = c->targeturi;
1343 if (!curconfig[KioskMode].val.i) {
1344 for (i = 0; i < LENGTH(keys); ++i) {
1345 if (gdk_keyval_to_lower(e->key.keyval) ==
1347 CLEANMASK(e->key.state) == keys[i].mod &&
1350 keys[i].func(c, &(keys[i].arg));
1355 case GDK_LEAVE_NOTIFY:
1356 c->overtitle = NULL;
1359 case GDK_WINDOW_STATE:
1360 if (e->window_state.changed_mask ==
1361 GDK_WINDOW_STATE_FULLSCREEN)
1362 c->fullscreen = e->window_state.new_window_state &
1363 GDK_WINDOW_STATE_FULLSCREEN;
1373 showview(WebKitWebView *v, Client *c)
1375 GdkRGBA bgcolor = { 0 };
1378 c->finder = webkit_web_view_get_find_controller(c->view);
1379 c->inspector = webkit_web_view_get_inspector(c->view);
1381 c->pageid = webkit_web_view_get_page_id(c->view);
1382 c->win = createwindow(c);
1384 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1385 gtk_widget_show_all(c->win);
1386 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1388 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1389 c->xid = gdk_x11_window_get_xid(gwin);
1392 gdk_display_sync(gtk_widget_get_display(c->win));
1397 if (curconfig[HideBackground].val.i)
1398 webkit_web_view_set_background_color(c->view, &bgcolor);
1400 if (!curconfig[KioskMode].val.i) {
1401 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1402 gdk_window_add_filter(gwin, processx, c);
1405 if (curconfig[RunInFullscreen].val.i)
1406 togglefullscreen(c, NULL);
1408 if (curconfig[ZoomLevel].val.f != 1.0)
1409 webkit_web_view_set_zoom_level(c->view,
1410 curconfig[ZoomLevel].val.f);
1412 setatom(c, AtomFind, "");
1413 setatom(c, AtomUri, "about:blank");
1417 createwindow(Client *c)
1423 w = gtk_plug_new(embed);
1425 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1427 wmstr = g_path_get_basename(argv0);
1428 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
1431 wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
1432 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1435 gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
1438 g_signal_connect(G_OBJECT(w), "destroy",
1439 G_CALLBACK(destroywin), c);
1440 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1441 G_CALLBACK(winevent), c);
1442 g_signal_connect(G_OBJECT(w), "key-press-event",
1443 G_CALLBACK(winevent), c);
1444 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1445 G_CALLBACK(winevent), c);
1446 g_signal_connect(G_OBJECT(w), "window-state-event",
1447 G_CALLBACK(winevent), c);
1453 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
1454 GTlsCertificateFlags err, Client *c)
1456 GString *errmsg = g_string_new(NULL);
1459 c->failedcert = g_object_ref(cert);
1463 if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
1464 g_string_append(errmsg,
1465 "The signing certificate authority is not known.<br>");
1466 if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
1467 g_string_append(errmsg,
1468 "The certificate does not match the expected identity "
1469 "of the site that it was retrieved from.<br>");
1470 if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
1471 g_string_append(errmsg,
1472 "The certificate's activation time "
1473 "is still in the future.<br>");
1474 if (err & G_TLS_CERTIFICATE_EXPIRED)
1475 g_string_append(errmsg, "The certificate has expired.<br>");
1476 if (err & G_TLS_CERTIFICATE_REVOKED)
1477 g_string_append(errmsg,
1478 "The certificate has been revoked according to "
1479 "the GTlsConnection's certificate revocation list.<br>");
1480 if (err & G_TLS_CERTIFICATE_INSECURE)
1481 g_string_append(errmsg,
1482 "The certificate's algorithm is considered insecure.<br>");
1483 if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
1484 g_string_append(errmsg,
1485 "Some error occurred validating the certificate.<br>");
1487 g_object_get(cert, "certificate-pem", &pem, NULL);
1488 html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
1489 "<p>You can inspect the following certificate "
1490 "with Ctrl-t (default keybinding).</p>"
1491 "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
1493 g_string_free(errmsg, TRUE);
1495 webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
1502 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1504 const char *uri = geturi(c);
1507 case WEBKIT_LOAD_STARTED:
1508 setatom(c, AtomUri, uri);
1510 c->https = c->insecure = 0;
1511 seturiparameters(c, uri, loadtransient);
1515 g_clear_object(&c->failedcert);
1517 case WEBKIT_LOAD_REDIRECTED:
1518 setatom(c, AtomUri, uri);
1520 seturiparameters(c, uri, loadtransient);
1522 case WEBKIT_LOAD_COMMITTED:
1523 seturiparameters(c, uri, loadcommitted);
1524 c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
1527 case WEBKIT_LOAD_FINISHED:
1528 seturiparameters(c, uri, loadfinished);
1529 /* Disabled until we write some WebKitWebExtension for
1530 * manipulating the DOM directly.
1531 evalscript(c, "document.documentElement.style.overflow = '%s'",
1532 enablescrollbars ? "auto" : "hidden");
1541 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1543 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1549 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1551 c->title = webkit_web_view_get_title(c->view);
1556 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1559 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1561 /* Keep the hit test to know where is the pointer on the next click */
1565 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1566 else if (hc & OnImg)
1567 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1568 else if (hc & OnMedia)
1569 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1571 c->targeturi = NULL;
1573 c->overtitle = c->targeturi;
1578 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1580 ParamName param = ParameterLast;
1582 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1583 param = Geolocation;
1584 } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
1585 if (webkit_user_media_permission_is_for_audio_device(
1586 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1587 param = AccessMicrophone;
1588 else if (webkit_user_media_permission_is_for_video_device(
1589 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1590 param = AccessWebcam;
1595 if (curconfig[param].val.i)
1596 webkit_permission_request_allow(r);
1598 webkit_permission_request_deny(r);
1604 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1605 WebKitPolicyDecisionType dt, Client *c)
1608 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1609 decidenavigation(d, c);
1611 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1612 decidenewwindow(d, c);
1614 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1615 decideresource(d, c);
1618 webkit_policy_decision_ignore(d);
1625 decidenavigation(WebKitPolicyDecision *d, Client *c)
1627 WebKitNavigationAction *a =
1628 webkit_navigation_policy_decision_get_navigation_action(
1629 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1631 switch (webkit_navigation_action_get_navigation_type(a)) {
1632 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1633 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1634 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1635 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1636 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1637 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1639 /* Do not navigate to links with a "_blank" target (popup) */
1640 if (webkit_navigation_policy_decision_get_frame_name(
1641 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1642 webkit_policy_decision_ignore(d);
1644 /* Filter out navigation to different domain ? */
1645 /* get action→urirequest, copy and load in new window+view
1646 * on Ctrl+Click ? */
1647 webkit_policy_decision_use(d);
1654 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1657 WebKitNavigationAction *a =
1658 webkit_navigation_policy_decision_get_navigation_action(
1659 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1662 switch (webkit_navigation_action_get_navigation_type(a)) {
1663 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1664 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1665 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1666 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1667 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1668 /* Filter domains here */
1669 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1670 * test for link clicked but no button ? */
1671 arg.v = webkit_uri_request_get_uri(
1672 webkit_navigation_action_get_request(a));
1673 newwindow(c, &arg, 0);
1675 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1680 webkit_policy_decision_ignore(d);
1684 decideresource(WebKitPolicyDecision *d, Client *c)
1687 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1688 WebKitURIResponse *res =
1689 webkit_response_policy_decision_get_response(r);
1690 const gchar *uri = webkit_uri_response_get_uri(res);
1692 if (g_str_has_suffix(uri, "/favicon.ico")) {
1693 webkit_policy_decision_ignore(d);
1697 if (!g_str_has_prefix(uri, "http://")
1698 && !g_str_has_prefix(uri, "https://")
1699 && !g_str_has_prefix(uri, "about:")
1700 && !g_str_has_prefix(uri, "file://")
1701 && !g_str_has_prefix(uri, "data:")
1702 && !g_str_has_prefix(uri, "blob:")
1703 && strlen(uri) > 0) {
1704 for (i = 0; i < strlen(uri); i++) {
1705 if (!g_ascii_isprint(uri[i])) {
1711 handleplumb(c, uri);
1712 webkit_policy_decision_ignore(d);
1717 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1718 webkit_policy_decision_use(d);
1720 webkit_policy_decision_ignore(d);
1726 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
1732 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1734 g_signal_connect(G_OBJECT(d), "notify::response",
1735 G_CALLBACK(responsereceived), c);
1739 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1741 download(c, webkit_download_get_response(d));
1742 webkit_download_cancel(d);
1746 download(Client *c, WebKitURIResponse *r)
1748 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1753 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
1756 fprintf(stderr, "web process terminated: %s\n",
1757 r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
1762 closeview(WebKitWebView *v, Client *c)
1764 gtk_widget_destroy(c->win);
1768 destroywin(GtkWidget* w, Client *c)
1776 parseuri(const gchar *uri) {
1779 for (i = 0; i < LENGTH(searchengines); i++) {
1780 if (searchengines[i].token == NULL || searchengines[i].uri == NULL ||
1781 *(uri + strlen(searchengines[i].token)) != ' ')
1783 if (g_str_has_prefix(uri, searchengines[i].token))
1784 return g_strdup_printf(searchengines[i].uri,
1785 uri + strlen(searchengines[i].token) + 1);
1788 return g_strdup_printf("http://%s", uri);
1792 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1794 Arg a = {.v = text };
1796 loaduri((Client *) d, &a);
1800 reload(Client *c, const Arg *a)
1803 webkit_web_view_reload_bypass_cache(c->view);
1805 webkit_web_view_reload(c->view);
1809 print(Client *c, const Arg *a)
1811 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1812 GTK_WINDOW(c->win));
1816 showcert(Client *c, const Arg *a)
1818 GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
1819 GcrCertificate *gcrt;
1822 GcrCertificateWidget *wcert;
1827 g_object_get(cert, "certificate", &crt, NULL);
1828 gcrt = gcr_simple_certificate_new(crt->data, crt->len);
1829 g_byte_array_unref(crt);
1831 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1832 wcert = gcr_certificate_widget_new(gcrt);
1833 g_object_unref(gcrt);
1835 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
1836 gtk_widget_show_all(win);
1840 clipboard(Client *c, const Arg *a)
1842 if (a->i) { /* load clipboard uri */
1843 gtk_clipboard_request_text(gtk_clipboard_get(
1844 GDK_SELECTION_PRIMARY),
1846 } else { /* copy uri */
1847 gtk_clipboard_set_text(gtk_clipboard_get(
1848 GDK_SELECTION_PRIMARY), c->targeturi
1849 ? c->targeturi : geturi(c), -1);
1854 zoom(Client *c, const Arg *a)
1857 webkit_web_view_set_zoom_level(c->view,
1858 curconfig[ZoomLevel].val.f + 0.1);
1860 webkit_web_view_set_zoom_level(c->view,
1861 curconfig[ZoomLevel].val.f - 0.1);
1863 webkit_web_view_set_zoom_level(c->view, 1.0);
1865 curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
1869 msgext(Client *c, char type, const Arg *a)
1871 static char msg[MSGBUFSZ];
1874 if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
1875 4, c->pageid, type, a->i))
1877 fprintf(stderr, "surf: message too long: %d\n", ret);
1881 if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
1882 fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
1886 scrollv(Client *c, const Arg *a)
1892 scrollh(Client *c, const Arg *a)
1898 navigate(Client *c, const Arg *a)
1901 webkit_web_view_go_back(c->view);
1903 webkit_web_view_go_forward(c->view);
1907 stop(Client *c, const Arg *a)
1909 webkit_web_view_stop_loading(c->view);
1913 toggle(Client *c, const Arg *a)
1915 curconfig[a->i].val.i ^= 1;
1916 setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
1920 togglefullscreen(Client *c, const Arg *a)
1922 /* toggling value is handled in winevent() */
1924 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1926 gtk_window_fullscreen(GTK_WINDOW(c->win));
1930 togglecookiepolicy(Client *c, const Arg *a)
1933 cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
1935 setparameter(c, 0, CookiePolicies, NULL);
1939 toggleinspector(Client *c, const Arg *a)
1941 if (webkit_web_inspector_is_attached(c->inspector))
1942 webkit_web_inspector_close(c->inspector);
1943 else if (curconfig[Inspector].val.i)
1944 webkit_web_inspector_show(c->inspector);
1948 find(Client *c, const Arg *a)
1954 webkit_find_controller_search_next(c->finder);
1956 webkit_find_controller_search_previous(c->finder);
1958 s = getatom(c, AtomFind);
1959 f = webkit_find_controller_get_search_text(c->finder);
1961 if (g_strcmp0(f, s) == 0) /* reset search */
1962 webkit_find_controller_search(c->finder, "", findopts,
1965 webkit_find_controller_search(c->finder, s, findopts,
1968 if (strcmp(s, "") == 0)
1969 webkit_find_controller_search_finish(c->finder);
1974 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1980 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1984 arg.v = webkit_hit_test_result_get_link_uri(h);
1985 newwindow(c, &arg, a->i);
1989 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1993 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1998 main(int argc, char *argv[])
2003 memset(&arg, 0, sizeof(arg));
2005 /* command line args */
2008 defconfig[CookiePolicies].val.v = EARGF(usage());
2009 defconfig[CookiePolicies].prio = 2;
2012 defconfig[ScrollBars].val.i = 0;
2013 defconfig[ScrollBars].prio = 2;
2016 defconfig[ScrollBars].val.i = 1;
2017 defconfig[ScrollBars].prio = 2;
2020 cookiefile = EARGF(usage());
2023 stylefile = EARGF(usage());
2026 defconfig[DiskCache].val.i = 0;
2027 defconfig[DiskCache].prio = 2;
2030 defconfig[DiskCache].val.i = 1;
2031 defconfig[DiskCache].prio = 2;
2034 embed = strtol(EARGF(usage()), NULL, 0);
2037 defconfig[RunInFullscreen].val.i = 0;
2038 defconfig[RunInFullscreen].prio = 2;
2041 defconfig[RunInFullscreen].val.i = 1;
2042 defconfig[RunInFullscreen].prio = 2;
2045 defconfig[Geolocation].val.i = 0;
2046 defconfig[Geolocation].prio = 2;
2049 defconfig[Geolocation].val.i = 1;
2050 defconfig[Geolocation].prio = 2;
2053 defconfig[LoadImages].val.i = 0;
2054 defconfig[LoadImages].prio = 2;
2057 defconfig[LoadImages].val.i = 1;
2058 defconfig[LoadImages].prio = 2;
2061 defconfig[KioskMode].val.i = 0;
2062 defconfig[KioskMode].prio = 2;
2065 defconfig[KioskMode].val.i = 1;
2066 defconfig[KioskMode].prio = 2;
2069 defconfig[Style].val.i = 0;
2070 defconfig[Style].prio = 2;
2073 defconfig[Style].val.i = 1;
2074 defconfig[Style].prio = 2;
2077 defconfig[Inspector].val.i = 0;
2078 defconfig[Inspector].prio = 2;
2081 defconfig[Inspector].val.i = 1;
2082 defconfig[Inspector].prio = 2;
2085 defconfig[Plugins].val.i = 0;
2086 defconfig[Plugins].prio = 2;
2089 defconfig[Plugins].val.i = 1;
2090 defconfig[Plugins].prio = 2;
2093 scriptfile = EARGF(usage());
2096 defconfig[JavaScript].val.i = 0;
2097 defconfig[JavaScript].prio = 2;
2100 defconfig[JavaScript].val.i = 1;
2101 defconfig[JavaScript].prio = 2;
2104 defconfig[StrictTLS].val.i = 0;
2105 defconfig[StrictTLS].prio = 2;
2108 defconfig[StrictTLS].val.i = 1;
2109 defconfig[StrictTLS].prio = 2;
2112 fulluseragent = EARGF(usage());
2115 die("surf-"VERSION", see LICENSE for © details\n");
2120 defconfig[Certificate].val.i = 0;
2121 defconfig[Certificate].prio = 2;
2124 defconfig[Certificate].val.i = 1;
2125 defconfig[Certificate].prio = 2;
2128 defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
2129 defconfig[ZoomLevel].prio = 2;
2140 arg.v = "about:blank";
2144 c = newclient(NULL);