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);
241 static void playexternal(Client *c, const Arg *a);
244 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
245 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
246 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
248 static char winid[64];
249 static char togglestats[12];
250 static char pagestats[2];
251 static Atom atoms[AtomLast];
254 static int cookiepolicy;
256 static Client *clients;
257 static GdkDevice *gdkkb;
258 static char *stylefile;
259 static const char *useragent;
260 static Parameter *curconfig;
261 static int modparams[ParameterLast];
262 static int pipein[2], pipeout[2];
265 static ParamName loadtransient[] = {
279 static ParamName loadcommitted[] = {
305 static ParamName loadfinished[] = {
309 /* configuration, allows nested code to access above variables */
315 die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
316 "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
317 "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
327 /* clean up any zombies immediately */
329 if (signal(SIGHUP, sighup) == SIG_ERR)
330 die("Can't install SIGHUP handler");
332 if (!(dpy = XOpenDisplay(NULL)))
333 die("Can't open default display");
336 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
337 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
338 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
340 gtk_init(NULL, NULL);
342 gdpy = gdk_display_get_default();
344 curconfig = defconfig;
347 cookiefile = buildfile(cookiefile);
348 scriptfile = buildfile(scriptfile);
349 cachedir = buildpath(cachedir);
350 certdir = buildpath(certdir);
352 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
354 if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
355 fputs("Unable to create pipes\n", stderr);
357 gchanin = g_io_channel_unix_new(pipein[0]);
358 g_io_channel_set_encoding(gchanin, NULL, NULL);
359 g_io_channel_set_close_on_unref(gchanin, TRUE);
360 g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
364 for (i = 0; i < LENGTH(certs); ++i) {
365 if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
366 certs[i].file = g_strconcat(certdir, "/", certs[i].file,
369 fprintf(stderr, "Could not compile regex: %s\n",
371 certs[i].regex = NULL;
376 styledir = buildpath(styledir);
377 for (i = 0; i < LENGTH(styles); ++i) {
378 if (!regcomp(&(styles[i].re), styles[i].regex,
380 styles[i].file = g_strconcat(styledir, "/",
381 styles[i].file, NULL);
383 fprintf(stderr, "Could not compile regex: %s\n",
385 styles[i].regex = NULL;
390 stylefile = buildfile(stylefile);
393 for (i = 0; i < LENGTH(uriparams); ++i) {
394 if (regcomp(&(uriparams[i].re), uriparams[i].uri,
396 fprintf(stderr, "Could not compile regex: %s\n",
398 uriparams[i].uri = NULL;
402 /* copy default parameters with higher priority */
403 for (j = 0; j < ParameterLast; ++j) {
404 if (defconfig[j].prio >= uriparams[i].config[j].prio)
405 uriparams[i].config[j] = defconfig[j];
413 if (signal(SIGCHLD, sigchld) == SIG_ERR)
414 die("Can't install SIGCHLD handler");
415 while (waitpid(-1, NULL, WNOHANG) > 0)
425 for (c = clients; c; c = c->next)
430 buildfile(const char *path)
432 char *dname, *bname, *bpath, *fpath;
435 dname = g_path_get_dirname(path);
436 bname = g_path_get_basename(path);
438 bpath = buildpath(dname);
441 fpath = g_build_filename(bpath, bname, NULL);
445 if (!(f = fopen(fpath, "a")))
446 die("Could not open file: %s\n", fpath);
448 g_chmod(fpath, 0600); /* always */
455 getuserhomedir(const char *user)
457 struct passwd *pw = getpwnam(user);
460 die("Can't get user %s login information.\n", user);
466 getcurrentuserhomedir(void)
472 homedir = getenv("HOME");
476 user = getenv("USER");
478 return getuserhomedir(user);
480 pw = getpwuid(getuid());
482 die("Can't get current user home directory\n");
488 buildpath(const char *path)
493 apath = untildepath(path);
495 apath = g_strdup(path);
497 /* creating directory */
498 if (g_mkdir_with_parents(apath, 0700) < 0)
499 die("Could not access directory: %s\n", apath);
501 fpath = realpath(apath, NULL);
508 untildepath(const char *path)
510 char *apath, *name, *p;
513 if (path[1] == '/' || path[1] == '\0') {
514 p = (char *)&path[1];
515 homedir = getcurrentuserhomedir();
517 if ((p = strchr(path, '/')))
518 name = g_strndup(&path[1], p - (path + 1));
520 name = g_strdup(&path[1]);
522 homedir = getuserhomedir(name);
525 apath = g_build_filename(homedir, p, NULL);
530 newclient(Client *rc)
534 if (!(c = calloc(1, sizeof(Client))))
535 die("Cannot malloc!\n");
541 c->view = newview(c, rc ? rc->view : NULL);
547 loaduri(Client *c, const Arg *a)
550 char *url, *path, *apath;
551 const char *uri = a->v;
553 if (g_strcmp0(uri, "") == 0)
556 if (g_str_has_prefix(uri, "http://") ||
557 g_str_has_prefix(uri, "https://") ||
558 g_str_has_prefix(uri, "file://") ||
559 g_str_has_prefix(uri, "about:")) {
563 apath = untildepath(uri);
566 if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
567 url = g_strdup_printf("file://%s", path);
576 setatom(c, AtomUri, url);
578 if (strcmp(url, geturi(c)) == 0) {
581 webkit_web_view_load_uri(c->view, url);
593 if (!(uri = webkit_web_view_get_uri(c->view)))
599 setatom(Client *c, int a, const char *v)
601 XChangeProperty(dpy, c->xid,
602 atoms[a], XA_STRING, 8, PropModeReplace,
603 (unsigned char *)v, strlen(v) + 1);
608 getatom(Client *c, int a)
610 static char buf[BUFSIZ];
613 unsigned long ldummy;
614 unsigned char *p = NULL;
617 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
618 &adummy, &idummy, &ldummy, &ldummy, &p);
620 strncpy(buf, (char *)p, LENGTH(buf) - 1);
629 updatetitle(Client *c)
632 const char *name = c->overtitle ? c->overtitle :
633 c->title ? c->title : "";
635 if (curconfig[ShowIndicators].val.i) {
639 if (c->progress != 100)
640 title = g_strdup_printf("[%i%%] %s:%s | %s",
641 c->progress, togglestats, pagestats, name);
643 title = g_strdup_printf("%s:%s | %s",
644 togglestats, pagestats, name);
646 gtk_window_set_title(GTK_WINDOW(c->win), title);
649 gtk_window_set_title(GTK_WINDOW(c->win), name);
654 gettogglestats(Client *c)
656 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
657 togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
658 togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
659 togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
660 togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
661 togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
662 togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
663 togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
664 togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
665 togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
666 togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
667 togglestats[11] = '\0';
671 getpagestats(Client *c)
674 pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
680 WebKitCookieAcceptPolicy
681 cookiepolicy_get(void)
683 switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
685 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
687 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
688 default: /* fallthrough */
690 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
695 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
698 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
700 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
702 default: /* fallthrough */
703 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
709 seturiparameters(Client *c, const char *uri, ParamName *params)
711 Parameter *config, *uriconfig = NULL;
714 for (i = 0; i < LENGTH(uriparams); ++i) {
715 if (uriparams[i].uri &&
716 !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
717 uriconfig = uriparams[i].config;
722 curconfig = uriconfig ? uriconfig : defconfig;
724 for (i = 0; (p = params[i]) != ParameterLast; ++i) {
726 default: /* FALLTHROUGH */
727 if (!(defconfig[p].prio < curconfig[p].prio ||
728 defconfig[p].prio < modparams[p]))
733 setparameter(c, 0, p, &curconfig[p].val);
739 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
741 GdkRGBA bgcolor = { 0 };
742 WebKitSettings *s = webkit_web_view_get_settings(c->view);
744 modparams[p] = curconfig[p].prio;
747 case AcceleratedCanvas:
748 webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
750 case AccessMicrophone:
751 return; /* do nothing */
753 return; /* do nothing */
755 webkit_settings_set_enable_caret_browsing(s, a->i);
760 setcert(c, geturi(c));
761 return; /* do not update */
763 webkit_cookie_manager_set_accept_policy(
764 webkit_web_context_get_cookie_manager(
765 webkit_web_view_get_context(c->view)),
770 webkit_web_context_set_cache_model(
771 webkit_web_view_get_context(c->view), a->i ?
772 WEBKIT_CACHE_MODEL_WEB_BROWSER :
773 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
774 return; /* do not update */
776 webkit_settings_set_default_charset(s, a->v);
777 return; /* do not update */
779 webkit_settings_set_enable_dns_prefetching(s, a->i);
780 return; /* do not update */
781 case FileURLsCrossAccess:
782 webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
783 webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
784 return; /* do not update */
786 webkit_settings_set_default_font_size(s, a->i);
787 return; /* do not update */
788 case FrameFlattening:
789 webkit_settings_set_enable_frame_flattening(s, a->i);
796 webkit_web_view_set_background_color(c->view, &bgcolor);
797 return; /* do not update */
799 webkit_settings_set_enable_developer_extras(s, a->i);
800 return; /* do not update */
802 webkit_settings_set_enable_java(s, a->i);
803 return; /* do not update */
805 webkit_settings_set_enable_javascript(s, a->i);
808 return; /* do nothing */
810 webkit_settings_set_auto_load_images(s, a->i);
812 case MediaManualPlay:
813 webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
816 webkit_settings_set_enable_plugins(s, a->i);
818 case PreferredLanguages:
819 return; /* do nothing */
820 case RunInFullscreen:
821 return; /* do nothing */
823 /* Disabled until we write some WebKitWebExtension for
824 * manipulating the DOM directly.
825 enablescrollbars = !enablescrollbars;
826 evalscript(c, "document.documentElement.style.overflow = '%s'",
827 enablescrollbars ? "auto" : "hidden");
829 return; /* do not update */
832 case SmoothScrolling:
833 webkit_settings_set_enable_smooth_scrolling(s, a->i);
834 return; /* do not update */
836 webkit_settings_set_enable_site_specific_quirks(s, a->i);
839 webkit_web_context_set_spell_checking_enabled(
840 webkit_web_view_get_context(c->view), a->i);
841 return; /* do not update */
843 return; /* do nothing */
845 webkit_web_context_set_tls_errors_policy(
846 webkit_web_view_get_context(c->view), a->i ?
847 WEBKIT_TLS_ERRORS_POLICY_FAIL :
848 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
851 webkit_user_content_manager_remove_all_style_sheets(
852 webkit_web_view_get_user_content_manager(c->view));
854 setstyle(c, getstyle(geturi(c)));
858 webkit_settings_set_enable_webgl(s, a->i);
861 webkit_web_view_set_zoom_level(c->view, a->f);
862 return; /* do not update */
864 return; /* do nothing */
873 getcert(const char *uri)
877 for (i = 0; i < LENGTH(certs); ++i) {
878 if (certs[i].regex &&
879 !regexec(&(certs[i].re), uri, 0, NULL, 0))
880 return certs[i].file;
887 setcert(Client *c, const char *uri)
889 const char *file = getcert(uri);
891 GTlsCertificate *cert;
896 if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
897 fprintf(stderr, "Could not read certificate file: %s\n", file);
901 if ((uri = strstr(uri, "https://"))) {
902 uri += sizeof("https://") - 1;
903 host = g_strndup(uri, strchr(uri, '/') - uri);
904 webkit_web_context_allow_tls_certificate_for_host(
905 webkit_web_view_get_context(c->view), cert, host);
909 g_object_unref(cert);
914 getstyle(const char *uri)
921 for (i = 0; i < LENGTH(styles); ++i) {
922 if (styles[i].regex &&
923 !regexec(&(styles[i].re), uri, 0, NULL, 0))
924 return styles[i].file;
931 setstyle(Client *c, const char *file)
935 if (!g_file_get_contents(file, &style, NULL, NULL)) {
936 fprintf(stderr, "Could not read style file: %s\n", file);
940 webkit_user_content_manager_add_style_sheet(
941 webkit_web_view_get_user_content_manager(c->view),
942 webkit_user_style_sheet_new(style,
943 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
944 WEBKIT_USER_STYLE_LEVEL_USER,
956 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
957 evalscript(c, "%s", script);
962 evalscript(Client *c, const char *jsstr, ...)
968 script = g_strdup_vprintf(jsstr, ap);
971 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
976 updatewinid(Client *c)
978 snprintf(winid, LENGTH(winid), "%lu", c->xid);
982 handleplumb(Client *c, const char *uri)
984 Arg a = (Arg)PLUMB(uri);
989 newwindow(Client *c, const Arg *a, int noembed)
993 const char *cmd[29], *uri;
994 const Arg arg = { .v = cmd };
998 cmd[i++] = curconfig[CookiePolicies].val.v;
999 cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
1000 if (cookiefile && g_strcmp0(cookiefile, "")) {
1002 cmd[i++] = cookiefile;
1004 if (stylefile && g_strcmp0(stylefile, "")) {
1006 cmd[i++] = stylefile;
1008 cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
1009 if (embed && !noembed) {
1011 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1014 cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
1015 cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
1016 cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
1017 cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
1018 cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
1019 cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
1020 cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
1021 if (scriptfile && g_strcmp0(scriptfile, "")) {
1023 cmd[i++] = scriptfile;
1025 cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
1026 cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
1027 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1029 cmd[i++] = fulluseragent;
1033 cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
1034 /* do not keep zoom level */
1044 spawn(Client *c, const Arg *a)
1048 close(ConnectionNumber(dpy));
1052 execvp(((char **)a->v)[0], (char **)a->v);
1053 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
1060 destroyclient(Client *c)
1064 webkit_web_view_stop_loading(c->view);
1065 /* Not needed, has already been called
1066 gtk_widget_destroy(c->win);
1069 for (p = clients; p && p->next != c; p = p->next)
1082 destroyclient(clients);
1094 newview(Client *c, WebKitWebView *rv)
1097 WebKitSettings *settings;
1098 WebKitWebContext *context;
1099 WebKitCookieManager *cookiemanager;
1100 WebKitUserContentManager *contentmanager;
1104 v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
1106 settings = webkit_settings_new_with_settings(
1107 "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1108 "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1109 "auto-load-images", curconfig[LoadImages].val.i,
1110 "default-charset", curconfig[DefaultCharset].val.v,
1111 "default-font-size", curconfig[FontSize].val.i,
1112 "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
1113 "enable-developer-extras", curconfig[Inspector].val.i,
1114 "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
1115 "enable-frame-flattening", curconfig[FrameFlattening].val.i,
1116 "enable-html5-database", curconfig[DiskCache].val.i,
1117 "enable-html5-local-storage", curconfig[DiskCache].val.i,
1118 "enable-java", curconfig[Java].val.i,
1119 "enable-javascript", curconfig[JavaScript].val.i,
1120 "enable-plugins", curconfig[Plugins].val.i,
1121 "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
1122 "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
1123 "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
1124 "enable-webgl", curconfig[WebGL].val.i,
1125 "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
1127 /* For more interesting settings, have a look at
1128 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
1130 if (strcmp(fulluseragent, "")) {
1131 webkit_settings_set_user_agent(settings, fulluseragent);
1132 } else if (surfuseragent) {
1133 webkit_settings_set_user_agent_with_application_details(
1134 settings, "Surf", VERSION);
1136 useragent = webkit_settings_get_user_agent(settings);
1138 contentmanager = webkit_user_content_manager_new();
1140 context = webkit_web_context_new_with_website_data_manager(
1141 webkit_website_data_manager_new(
1142 "base-cache-directory", cachedir,
1143 "base-data-directory", cachedir,
1146 cookiemanager = webkit_web_context_get_cookie_manager(context);
1148 /* rendering process model, can be a shared unique one
1149 * or one for each view */
1150 webkit_web_context_set_process_model(context,
1151 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1153 webkit_web_context_set_tls_errors_policy(context,
1154 curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
1155 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1157 webkit_web_context_set_cache_model(context,
1158 curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
1159 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1161 /* Currently only works with text file to be compatible with curl */
1162 webkit_cookie_manager_set_persistent_storage(cookiemanager,
1163 cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1165 webkit_cookie_manager_set_accept_policy(cookiemanager,
1166 cookiepolicy_get());
1168 webkit_web_context_set_preferred_languages(context,
1169 curconfig[PreferredLanguages].val.v);
1170 webkit_web_context_set_spell_checking_languages(context,
1171 curconfig[SpellLanguages].val.v);
1172 webkit_web_context_set_spell_checking_enabled(context,
1173 curconfig[SpellChecking].val.i);
1175 g_signal_connect(G_OBJECT(context), "download-started",
1176 G_CALLBACK(downloadstarted), c);
1177 g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
1178 G_CALLBACK(initwebextensions), c);
1180 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
1181 "settings", settings,
1182 "user-content-manager", contentmanager,
1183 "web-context", context,
1187 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
1188 G_CALLBACK(progresschanged), c);
1189 g_signal_connect(G_OBJECT(v), "notify::title",
1190 G_CALLBACK(titlechanged), c);
1191 g_signal_connect(G_OBJECT(v), "button-release-event",
1192 G_CALLBACK(buttonreleased), c);
1193 g_signal_connect(G_OBJECT(v), "close",
1194 G_CALLBACK(closeview), c);
1195 g_signal_connect(G_OBJECT(v), "create",
1196 G_CALLBACK(createview), c);
1197 g_signal_connect(G_OBJECT(v), "decide-policy",
1198 G_CALLBACK(decidepolicy), c);
1199 g_signal_connect(G_OBJECT(v), "insecure-content-detected",
1200 G_CALLBACK(insecurecontent), c);
1201 g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
1202 G_CALLBACK(loadfailedtls), c);
1203 g_signal_connect(G_OBJECT(v), "load-changed",
1204 G_CALLBACK(loadchanged), c);
1205 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
1206 G_CALLBACK(mousetargetchanged), c);
1207 g_signal_connect(G_OBJECT(v), "permission-request",
1208 G_CALLBACK(permissionrequested), c);
1209 g_signal_connect(G_OBJECT(v), "ready-to-show",
1210 G_CALLBACK(showview), c);
1211 g_signal_connect(G_OBJECT(v), "web-process-terminated",
1212 G_CALLBACK(webprocessterminated), c);
1218 readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
1220 static char msg[MSGBUFSZ], msgsz;
1221 GError *gerr = NULL;
1223 if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
1224 G_IO_STATUS_NORMAL) {
1225 fprintf(stderr, "surf: error reading pipe: %s\n",
1230 if ((msgsz = msg[0]) < 3) {
1231 fprintf(stderr, "surf: message too short: %d\n", msgsz);
1246 initwebextensions(WebKitWebContext *wc, Client *c)
1250 if (!pipeout[0] || !pipein[1])
1253 gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
1255 webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
1256 webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
1260 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
1264 switch (webkit_navigation_action_get_navigation_type(a)) {
1265 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1267 * popup windows of type “other” are almost always triggered
1268 * by user gesture, so inverse the logic here
1270 /* instead of this, compare destination uri to mouse-over uri for validating window */
1271 if (webkit_navigation_action_is_user_gesture(a))
1273 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1274 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1275 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1276 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1277 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1284 return GTK_WIDGET(n->view);
1288 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
1290 WebKitHitTestResultContext element;
1293 element = webkit_hit_test_result_get_context(c->mousepos);
1295 for (i = 0; i < LENGTH(buttons); ++i) {
1296 if (element & buttons[i].target &&
1297 e->button.button == buttons[i].button &&
1298 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
1300 buttons[i].func(c, &buttons[i].arg, c->mousepos);
1301 return buttons[i].stopevent;
1309 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1311 Client *c = (Client *)d;
1315 if (((XEvent *)e)->type == PropertyNotify) {
1316 ev = &((XEvent *)e)->xproperty;
1317 if (ev->state == PropertyNewValue) {
1318 if (ev->atom == atoms[AtomFind]) {
1321 return GDK_FILTER_REMOVE;
1322 } else if (ev->atom == atoms[AtomGo]) {
1323 a.v = getatom(c, AtomGo);
1326 return GDK_FILTER_REMOVE;
1330 return GDK_FILTER_CONTINUE;
1334 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1339 case GDK_ENTER_NOTIFY:
1340 c->overtitle = c->targeturi;
1344 if (!curconfig[KioskMode].val.i) {
1345 for (i = 0; i < LENGTH(keys); ++i) {
1346 if (gdk_keyval_to_lower(e->key.keyval) ==
1348 CLEANMASK(e->key.state) == keys[i].mod &&
1351 keys[i].func(c, &(keys[i].arg));
1356 case GDK_LEAVE_NOTIFY:
1357 c->overtitle = NULL;
1360 case GDK_WINDOW_STATE:
1361 if (e->window_state.changed_mask ==
1362 GDK_WINDOW_STATE_FULLSCREEN)
1363 c->fullscreen = e->window_state.new_window_state &
1364 GDK_WINDOW_STATE_FULLSCREEN;
1374 showview(WebKitWebView *v, Client *c)
1376 GdkRGBA bgcolor = { 0 };
1379 c->finder = webkit_web_view_get_find_controller(c->view);
1380 c->inspector = webkit_web_view_get_inspector(c->view);
1382 c->pageid = webkit_web_view_get_page_id(c->view);
1383 c->win = createwindow(c);
1385 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1386 gtk_widget_show_all(c->win);
1387 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1389 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1390 c->xid = gdk_x11_window_get_xid(gwin);
1393 gdk_display_sync(gtk_widget_get_display(c->win));
1398 if (curconfig[HideBackground].val.i)
1399 webkit_web_view_set_background_color(c->view, &bgcolor);
1401 if (!curconfig[KioskMode].val.i) {
1402 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1403 gdk_window_add_filter(gwin, processx, c);
1406 if (curconfig[RunInFullscreen].val.i)
1407 togglefullscreen(c, NULL);
1409 if (curconfig[ZoomLevel].val.f != 1.0)
1410 webkit_web_view_set_zoom_level(c->view,
1411 curconfig[ZoomLevel].val.f);
1413 setatom(c, AtomFind, "");
1414 setatom(c, AtomUri, "about:blank");
1418 createwindow(Client *c)
1424 w = gtk_plug_new(embed);
1426 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1428 wmstr = g_path_get_basename(argv0);
1429 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
1432 wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
1433 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1436 gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
1439 g_signal_connect(G_OBJECT(w), "destroy",
1440 G_CALLBACK(destroywin), c);
1441 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1442 G_CALLBACK(winevent), c);
1443 g_signal_connect(G_OBJECT(w), "key-press-event",
1444 G_CALLBACK(winevent), c);
1445 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1446 G_CALLBACK(winevent), c);
1447 g_signal_connect(G_OBJECT(w), "window-state-event",
1448 G_CALLBACK(winevent), c);
1454 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
1455 GTlsCertificateFlags err, Client *c)
1457 GString *errmsg = g_string_new(NULL);
1460 c->failedcert = g_object_ref(cert);
1464 if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
1465 g_string_append(errmsg,
1466 "The signing certificate authority is not known.<br>");
1467 if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
1468 g_string_append(errmsg,
1469 "The certificate does not match the expected identity "
1470 "of the site that it was retrieved from.<br>");
1471 if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
1472 g_string_append(errmsg,
1473 "The certificate's activation time "
1474 "is still in the future.<br>");
1475 if (err & G_TLS_CERTIFICATE_EXPIRED)
1476 g_string_append(errmsg, "The certificate has expired.<br>");
1477 if (err & G_TLS_CERTIFICATE_REVOKED)
1478 g_string_append(errmsg,
1479 "The certificate has been revoked according to "
1480 "the GTlsConnection's certificate revocation list.<br>");
1481 if (err & G_TLS_CERTIFICATE_INSECURE)
1482 g_string_append(errmsg,
1483 "The certificate's algorithm is considered insecure.<br>");
1484 if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
1485 g_string_append(errmsg,
1486 "Some error occurred validating the certificate.<br>");
1488 g_object_get(cert, "certificate-pem", &pem, NULL);
1489 html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
1490 "<p>You can inspect the following certificate "
1491 "with Ctrl-t (default keybinding).</p>"
1492 "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
1494 g_string_free(errmsg, TRUE);
1496 webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
1503 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1505 const char *uri = geturi(c);
1508 case WEBKIT_LOAD_STARTED:
1509 setatom(c, AtomUri, uri);
1511 c->https = c->insecure = 0;
1512 seturiparameters(c, uri, loadtransient);
1516 g_clear_object(&c->failedcert);
1518 case WEBKIT_LOAD_REDIRECTED:
1519 setatom(c, AtomUri, uri);
1521 seturiparameters(c, uri, loadtransient);
1523 case WEBKIT_LOAD_COMMITTED:
1524 seturiparameters(c, uri, loadcommitted);
1525 c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
1528 case WEBKIT_LOAD_FINISHED:
1529 seturiparameters(c, uri, loadfinished);
1530 /* Disabled until we write some WebKitWebExtension for
1531 * manipulating the DOM directly.
1532 evalscript(c, "document.documentElement.style.overflow = '%s'",
1533 enablescrollbars ? "auto" : "hidden");
1542 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1544 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1550 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1552 c->title = webkit_web_view_get_title(c->view);
1557 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1560 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1562 /* Keep the hit test to know where is the pointer on the next click */
1566 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1567 else if (hc & OnImg)
1568 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1569 else if (hc & OnMedia)
1570 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1572 c->targeturi = NULL;
1574 c->overtitle = c->targeturi;
1579 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1581 ParamName param = ParameterLast;
1583 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1584 param = Geolocation;
1585 } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
1586 if (webkit_user_media_permission_is_for_audio_device(
1587 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1588 param = AccessMicrophone;
1589 else if (webkit_user_media_permission_is_for_video_device(
1590 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1591 param = AccessWebcam;
1596 if (curconfig[param].val.i)
1597 webkit_permission_request_allow(r);
1599 webkit_permission_request_deny(r);
1605 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1606 WebKitPolicyDecisionType dt, Client *c)
1609 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1610 decidenavigation(d, c);
1612 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1613 decidenewwindow(d, c);
1615 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1616 decideresource(d, c);
1619 webkit_policy_decision_ignore(d);
1626 decidenavigation(WebKitPolicyDecision *d, Client *c)
1628 WebKitNavigationAction *a =
1629 webkit_navigation_policy_decision_get_navigation_action(
1630 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1632 switch (webkit_navigation_action_get_navigation_type(a)) {
1633 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1634 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1635 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1636 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1637 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1638 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1640 /* Do not navigate to links with a "_blank" target (popup) */
1641 if (webkit_navigation_policy_decision_get_frame_name(
1642 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1643 webkit_policy_decision_ignore(d);
1645 /* Filter out navigation to different domain ? */
1646 /* get action→urirequest, copy and load in new window+view
1647 * on Ctrl+Click ? */
1648 webkit_policy_decision_use(d);
1655 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1658 WebKitNavigationAction *a =
1659 webkit_navigation_policy_decision_get_navigation_action(
1660 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1663 switch (webkit_navigation_action_get_navigation_type(a)) {
1664 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1665 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1666 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1667 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1668 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1669 /* Filter domains here */
1670 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1671 * test for link clicked but no button ? */
1672 arg.v = webkit_uri_request_get_uri(
1673 webkit_navigation_action_get_request(a));
1674 newwindow(c, &arg, 0);
1676 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1681 webkit_policy_decision_ignore(d);
1685 decideresource(WebKitPolicyDecision *d, Client *c)
1688 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1689 WebKitURIResponse *res =
1690 webkit_response_policy_decision_get_response(r);
1691 const gchar *uri = webkit_uri_response_get_uri(res);
1693 if (g_str_has_suffix(uri, "/favicon.ico")) {
1694 webkit_policy_decision_ignore(d);
1698 if (!g_str_has_prefix(uri, "http://")
1699 && !g_str_has_prefix(uri, "https://")
1700 && !g_str_has_prefix(uri, "about:")
1701 && !g_str_has_prefix(uri, "file://")
1702 && !g_str_has_prefix(uri, "data:")
1703 && !g_str_has_prefix(uri, "blob:")
1704 && strlen(uri) > 0) {
1705 for (i = 0; i < strlen(uri); i++) {
1706 if (!g_ascii_isprint(uri[i])) {
1712 handleplumb(c, uri);
1713 webkit_policy_decision_ignore(d);
1718 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1719 webkit_policy_decision_use(d);
1721 webkit_policy_decision_ignore(d);
1727 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
1733 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1735 g_signal_connect(G_OBJECT(d), "notify::response",
1736 G_CALLBACK(responsereceived), c);
1740 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1742 download(c, webkit_download_get_response(d));
1743 webkit_download_cancel(d);
1747 download(Client *c, WebKitURIResponse *r)
1749 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1754 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
1757 fprintf(stderr, "web process terminated: %s\n",
1758 r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
1763 closeview(WebKitWebView *v, Client *c)
1765 gtk_widget_destroy(c->win);
1769 destroywin(GtkWidget* w, Client *c)
1777 parseuri(const gchar *uri) {
1780 for (i = 0; i < LENGTH(searchengines); i++) {
1781 if (searchengines[i].token == NULL || searchengines[i].uri == NULL ||
1782 *(uri + strlen(searchengines[i].token)) != ' ')
1784 if (g_str_has_prefix(uri, searchengines[i].token))
1785 return g_strdup_printf(searchengines[i].uri,
1786 uri + strlen(searchengines[i].token) + 1);
1789 return g_strdup_printf("http://%s", uri);
1793 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1795 Arg a = {.v = text };
1797 loaduri((Client *) d, &a);
1801 reload(Client *c, const Arg *a)
1804 webkit_web_view_reload_bypass_cache(c->view);
1806 webkit_web_view_reload(c->view);
1810 print(Client *c, const Arg *a)
1812 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1813 GTK_WINDOW(c->win));
1817 showcert(Client *c, const Arg *a)
1819 GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
1820 GcrCertificate *gcrt;
1823 GcrCertificateWidget *wcert;
1828 g_object_get(cert, "certificate", &crt, NULL);
1829 gcrt = gcr_simple_certificate_new(crt->data, crt->len);
1830 g_byte_array_unref(crt);
1832 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1833 wcert = gcr_certificate_widget_new(gcrt);
1834 g_object_unref(gcrt);
1836 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
1837 gtk_widget_show_all(win);
1841 clipboard(Client *c, const Arg *a)
1843 if (a->i) { /* load clipboard uri */
1844 gtk_clipboard_request_text(gtk_clipboard_get(
1845 GDK_SELECTION_PRIMARY),
1847 } else { /* copy uri */
1848 gtk_clipboard_set_text(gtk_clipboard_get(
1849 GDK_SELECTION_PRIMARY), c->targeturi
1850 ? c->targeturi : geturi(c), -1);
1855 zoom(Client *c, const Arg *a)
1858 webkit_web_view_set_zoom_level(c->view,
1859 curconfig[ZoomLevel].val.f + 0.1);
1861 webkit_web_view_set_zoom_level(c->view,
1862 curconfig[ZoomLevel].val.f - 0.1);
1864 webkit_web_view_set_zoom_level(c->view, 1.0);
1866 curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
1870 msgext(Client *c, char type, const Arg *a)
1872 static char msg[MSGBUFSZ];
1875 if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
1876 4, c->pageid, type, a->i))
1878 fprintf(stderr, "surf: message too long: %d\n", ret);
1882 if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
1883 fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
1887 scrollv(Client *c, const Arg *a)
1893 scrollh(Client *c, const Arg *a)
1899 navigate(Client *c, const Arg *a)
1902 webkit_web_view_go_back(c->view);
1904 webkit_web_view_go_forward(c->view);
1908 stop(Client *c, const Arg *a)
1910 webkit_web_view_stop_loading(c->view);
1914 toggle(Client *c, const Arg *a)
1916 curconfig[a->i].val.i ^= 1;
1917 setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
1921 togglefullscreen(Client *c, const Arg *a)
1923 /* toggling value is handled in winevent() */
1925 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1927 gtk_window_fullscreen(GTK_WINDOW(c->win));
1931 togglecookiepolicy(Client *c, const Arg *a)
1934 cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
1936 setparameter(c, 0, CookiePolicies, NULL);
1940 toggleinspector(Client *c, const Arg *a)
1942 if (webkit_web_inspector_is_attached(c->inspector))
1943 webkit_web_inspector_close(c->inspector);
1944 else if (curconfig[Inspector].val.i)
1945 webkit_web_inspector_show(c->inspector);
1949 find(Client *c, const Arg *a)
1955 webkit_find_controller_search_next(c->finder);
1957 webkit_find_controller_search_previous(c->finder);
1959 s = getatom(c, AtomFind);
1960 f = webkit_find_controller_get_search_text(c->finder);
1962 if (g_strcmp0(f, s) == 0) /* reset search */
1963 webkit_find_controller_search(c->finder, "", findopts,
1966 webkit_find_controller_search(c->finder, s, findopts,
1969 if (strcmp(s, "") == 0)
1970 webkit_find_controller_search_finish(c->finder);
1975 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1981 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1985 arg.v = webkit_hit_test_result_get_link_uri(h);
1986 newwindow(c, &arg, a->i);
1990 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1994 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1999 playexternal(Client *c, const Arg *a)
2003 arg = (Arg)VIDEOPLAY(geturi(c));
2008 main(int argc, char *argv[])
2013 memset(&arg, 0, sizeof(arg));
2015 /* command line args */
2018 defconfig[CookiePolicies].val.v = EARGF(usage());
2019 defconfig[CookiePolicies].prio = 2;
2022 defconfig[ScrollBars].val.i = 0;
2023 defconfig[ScrollBars].prio = 2;
2026 defconfig[ScrollBars].val.i = 1;
2027 defconfig[ScrollBars].prio = 2;
2030 cookiefile = EARGF(usage());
2033 stylefile = EARGF(usage());
2036 defconfig[DiskCache].val.i = 0;
2037 defconfig[DiskCache].prio = 2;
2040 defconfig[DiskCache].val.i = 1;
2041 defconfig[DiskCache].prio = 2;
2044 embed = strtol(EARGF(usage()), NULL, 0);
2047 defconfig[RunInFullscreen].val.i = 0;
2048 defconfig[RunInFullscreen].prio = 2;
2051 defconfig[RunInFullscreen].val.i = 1;
2052 defconfig[RunInFullscreen].prio = 2;
2055 defconfig[Geolocation].val.i = 0;
2056 defconfig[Geolocation].prio = 2;
2059 defconfig[Geolocation].val.i = 1;
2060 defconfig[Geolocation].prio = 2;
2063 defconfig[LoadImages].val.i = 0;
2064 defconfig[LoadImages].prio = 2;
2067 defconfig[LoadImages].val.i = 1;
2068 defconfig[LoadImages].prio = 2;
2071 defconfig[KioskMode].val.i = 0;
2072 defconfig[KioskMode].prio = 2;
2075 defconfig[KioskMode].val.i = 1;
2076 defconfig[KioskMode].prio = 2;
2079 defconfig[Style].val.i = 0;
2080 defconfig[Style].prio = 2;
2083 defconfig[Style].val.i = 1;
2084 defconfig[Style].prio = 2;
2087 defconfig[Inspector].val.i = 0;
2088 defconfig[Inspector].prio = 2;
2091 defconfig[Inspector].val.i = 1;
2092 defconfig[Inspector].prio = 2;
2095 defconfig[Plugins].val.i = 0;
2096 defconfig[Plugins].prio = 2;
2099 defconfig[Plugins].val.i = 1;
2100 defconfig[Plugins].prio = 2;
2103 scriptfile = EARGF(usage());
2106 defconfig[JavaScript].val.i = 0;
2107 defconfig[JavaScript].prio = 2;
2110 defconfig[JavaScript].val.i = 1;
2111 defconfig[JavaScript].prio = 2;
2114 defconfig[StrictTLS].val.i = 0;
2115 defconfig[StrictTLS].prio = 2;
2118 defconfig[StrictTLS].val.i = 1;
2119 defconfig[StrictTLS].prio = 2;
2122 fulluseragent = EARGF(usage());
2125 die("surf-"VERSION", see LICENSE for © details\n");
2130 defconfig[Certificate].val.i = 0;
2131 defconfig[Certificate].prio = 2;
2134 defconfig[Certificate].val.i = 1;
2135 defconfig[Certificate].prio = 2;
2138 defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
2139 defconfig[ZoomLevel].prio = 2;
2150 arg.v = "about:blank";
2154 c = newclient(NULL);