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, AtomSearch, 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;
133 Parameter config[ParameterLast];
144 static void usage(void);
145 static void setup(void);
146 static void sigchld(int unused);
147 static void sighup(int unused);
148 static char *buildfile(const char *path);
149 static char *buildpath(const char *path);
150 static char *untildepath(const char *path);
151 static const char *getuserhomedir(const char *user);
152 static const char *getcurrentuserhomedir(void);
153 static Client *newclient(Client *c);
154 static void loaduri(Client *c, const Arg *a);
155 static const char *geturi(Client *c);
156 static void setatom(Client *c, int a, const char *v);
157 static const char *getatom(Client *c, int a);
158 static void updatetitle(Client *c);
159 static void gettogglestats(Client *c);
160 static void getpagestats(Client *c);
161 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
162 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
163 static void seturiparameters(Client *c, const char *uri, ParamName *params);
164 static void setparameter(Client *c, int refresh, ParamName p, const Arg *a);
165 static const char *getcert(const char *uri);
166 static void setcert(Client *c, const char *file);
167 static const char *getstyle(const char *uri);
168 static void setstyle(Client *c, const char *file);
169 static void runscript(Client *c);
170 static void evalscript(Client *c, const char *jsstr, ...);
171 static void updatewinid(Client *c);
172 static void handleplumb(Client *c, const char *uri);
173 static void newwindow(Client *c, const Arg *a, int noembed);
174 static void spawn(Client *c, const Arg *a);
175 static void msgext(Client *c, char type, const Arg *a);
176 static void destroyclient(Client *c);
177 static void cleanup(void);
180 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
181 static void initwebextensions(WebKitWebContext *wc, Client *c);
182 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
184 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
185 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
187 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
188 static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
189 static void showview(WebKitWebView *v, Client *c);
190 static GtkWidget *createwindow(Client *c);
191 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
192 GTlsCertificate *cert,
193 GTlsCertificateFlags err, Client *c);
194 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
195 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
196 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
197 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
198 guint modifiers, Client *c);
199 static gboolean permissionrequested(WebKitWebView *v,
200 WebKitPermissionRequest *r, Client *c);
201 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
202 WebKitPolicyDecisionType dt, Client *c);
203 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
204 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
205 static void decideresource(WebKitPolicyDecision *d, Client *c);
206 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
208 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
210 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
211 static void download(Client *c, WebKitURIResponse *r);
212 static void webprocessterminated(WebKitWebView *v,
213 WebKitWebProcessTerminationReason r,
215 static void closeview(WebKitWebView *v, Client *c);
216 static void destroywin(GtkWidget* w, Client *c);
219 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
220 static void reload(Client *c, const Arg *a);
221 static void print(Client *c, const Arg *a);
222 static void showcert(Client *c, const Arg *a);
223 static void clipboard(Client *c, const Arg *a);
224 static void zoom(Client *c, const Arg *a);
225 static void scrollv(Client *c, const Arg *a);
226 static void scrollh(Client *c, const Arg *a);
227 static void navigate(Client *c, const Arg *a);
228 static void stop(Client *c, const Arg *a);
229 static void toggle(Client *c, const Arg *a);
230 static void togglefullscreen(Client *c, const Arg *a);
231 static void togglecookiepolicy(Client *c, const Arg *a);
232 static void toggleinspector(Client *c, const Arg *a);
233 static void find(Client *c, const Arg *a);
234 static void search(Client *c, const Arg *a);
237 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
238 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
239 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
241 static char winid[64];
242 static char togglestats[12];
243 static char pagestats[2];
244 static Atom atoms[AtomLast];
247 static int cookiepolicy;
249 static Client *clients;
250 static GdkDevice *gdkkb;
251 static char *stylefile;
252 static const char *useragent;
253 static Parameter *curconfig;
254 static int modparams[ParameterLast];
255 static int pipein[2], pipeout[2];
258 static ParamName loadtransient[] = {
272 static ParamName loadcommitted[] = {
298 static ParamName loadfinished[] = {
302 /* configuration, allows nested code to access above variables */
308 die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
309 "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
310 "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
320 /* clean up any zombies immediately */
322 if (signal(SIGHUP, sighup) == SIG_ERR)
323 die("Can't install SIGHUP handler");
325 if (!(dpy = XOpenDisplay(NULL)))
326 die("Can't open default display");
329 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
330 atoms[AtomSearch] = XInternAtom(dpy, "_SURF_SEARCH", False);
331 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
332 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
334 gtk_init(NULL, NULL);
336 gdpy = gdk_display_get_default();
338 curconfig = defconfig;
341 cookiefile = buildfile(cookiefile);
342 scriptfile = buildfile(scriptfile);
343 cachedir = buildpath(cachedir);
344 certdir = buildpath(certdir);
346 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
348 if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
349 fputs("Unable to create pipes\n", stderr);
351 gchanin = g_io_channel_unix_new(pipein[0]);
352 g_io_channel_set_encoding(gchanin, NULL, NULL);
353 g_io_channel_set_close_on_unref(gchanin, TRUE);
354 g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
358 for (i = 0; i < LENGTH(certs); ++i) {
359 if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
360 certs[i].file = g_strconcat(certdir, "/", certs[i].file,
363 fprintf(stderr, "Could not compile regex: %s\n",
365 certs[i].regex = NULL;
370 styledir = buildpath(styledir);
371 for (i = 0; i < LENGTH(styles); ++i) {
372 if (!regcomp(&(styles[i].re), styles[i].regex,
374 styles[i].file = g_strconcat(styledir, "/",
375 styles[i].file, NULL);
377 fprintf(stderr, "Could not compile regex: %s\n",
379 styles[i].regex = NULL;
384 stylefile = buildfile(stylefile);
387 for (i = 0; i < LENGTH(uriparams); ++i) {
388 if (regcomp(&(uriparams[i].re), uriparams[i].uri,
390 fprintf(stderr, "Could not compile regex: %s\n",
392 uriparams[i].uri = NULL;
396 /* copy default parameters with higher priority */
397 for (j = 0; j < ParameterLast; ++j) {
398 if (defconfig[j].prio >= uriparams[i].config[j].prio)
399 uriparams[i].config[j] = defconfig[j];
407 if (signal(SIGCHLD, sigchld) == SIG_ERR)
408 die("Can't install SIGCHLD handler");
409 while (waitpid(-1, NULL, WNOHANG) > 0)
419 for (c = clients; c; c = c->next)
424 buildfile(const char *path)
426 char *dname, *bname, *bpath, *fpath;
429 dname = g_path_get_dirname(path);
430 bname = g_path_get_basename(path);
432 bpath = buildpath(dname);
435 fpath = g_build_filename(bpath, bname, NULL);
439 if (!(f = fopen(fpath, "a")))
440 die("Could not open file: %s\n", fpath);
442 g_chmod(fpath, 0600); /* always */
449 getuserhomedir(const char *user)
451 struct passwd *pw = getpwnam(user);
454 die("Can't get user %s login information.\n", user);
460 getcurrentuserhomedir(void)
466 homedir = getenv("HOME");
470 user = getenv("USER");
472 return getuserhomedir(user);
474 pw = getpwuid(getuid());
476 die("Can't get current user home directory\n");
482 buildpath(const char *path)
487 apath = untildepath(path);
489 apath = g_strdup(path);
491 /* creating directory */
492 if (g_mkdir_with_parents(apath, 0700) < 0)
493 die("Could not access directory: %s\n", apath);
495 fpath = realpath(apath, NULL);
502 untildepath(const char *path)
504 char *apath, *name, *p;
507 if (path[1] == '/' || path[1] == '\0') {
508 p = (char *)&path[1];
509 homedir = getcurrentuserhomedir();
511 if ((p = strchr(path, '/')))
512 name = g_strndup(&path[1], p - (path + 1));
514 name = g_strdup(&path[1]);
516 homedir = getuserhomedir(name);
519 apath = g_build_filename(homedir, p, NULL);
524 newclient(Client *rc)
528 if (!(c = calloc(1, sizeof(Client))))
529 die("Cannot malloc!\n");
535 c->view = newview(c, rc ? rc->view : NULL);
541 loaduri(Client *c, const Arg *a)
544 char *url, *path, *apath;
545 const char *uri = a->v;
547 if (g_strcmp0(uri, "") == 0)
550 if (g_str_has_prefix(uri, "http://") ||
551 g_str_has_prefix(uri, "https://") ||
552 g_str_has_prefix(uri, "file://") ||
553 g_str_has_prefix(uri, "about:")) {
557 apath = untildepath(uri);
560 if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
561 url = g_strdup_printf("file://%s", path);
564 url = g_strdup_printf("http://%s", uri);
570 setatom(c, AtomUri, url);
572 if (strcmp(url, geturi(c)) == 0) {
575 webkit_web_view_load_uri(c->view, url);
583 search(Client *c, const Arg *a)
588 url = g_strdup_printf(searchurl, a->v);
600 if (!(uri = webkit_web_view_get_uri(c->view)))
606 setatom(Client *c, int a, const char *v)
608 XChangeProperty(dpy, c->xid,
609 atoms[a], XA_STRING, 8, PropModeReplace,
610 (unsigned char *)v, strlen(v) + 1);
615 getatom(Client *c, int a)
617 static char buf[BUFSIZ];
620 unsigned long ldummy;
621 unsigned char *p = NULL;
624 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
625 &adummy, &idummy, &ldummy, &ldummy, &p);
627 strncpy(buf, (char *)p, LENGTH(buf) - 1);
636 updatetitle(Client *c)
639 const char *name = c->overtitle ? c->overtitle :
640 c->title ? c->title : "";
642 if (curconfig[ShowIndicators].val.i) {
646 if (c->progress != 100)
647 title = g_strdup_printf("[%i%%] %s:%s | %s",
648 c->progress, togglestats, pagestats, name);
650 title = g_strdup_printf("%s:%s | %s",
651 togglestats, pagestats, name);
653 gtk_window_set_title(GTK_WINDOW(c->win), title);
656 gtk_window_set_title(GTK_WINDOW(c->win), name);
661 gettogglestats(Client *c)
663 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
664 togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
665 togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
666 togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
667 togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
668 togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
669 togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
670 togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
671 togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
672 togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
673 togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
674 togglestats[11] = '\0';
678 getpagestats(Client *c)
681 pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
687 WebKitCookieAcceptPolicy
688 cookiepolicy_get(void)
690 switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
692 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
694 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
695 default: /* fallthrough */
697 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
702 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
705 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
707 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
709 default: /* fallthrough */
710 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
716 seturiparameters(Client *c, const char *uri, ParamName *params)
718 Parameter *config, *uriconfig = NULL;
721 for (i = 0; i < LENGTH(uriparams); ++i) {
722 if (uriparams[i].uri &&
723 !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
724 uriconfig = uriparams[i].config;
729 curconfig = uriconfig ? uriconfig : defconfig;
731 for (i = 0; (p = params[i]) != ParameterLast; ++i) {
733 default: /* FALLTHROUGH */
734 if (!(defconfig[p].prio < curconfig[p].prio ||
735 defconfig[p].prio < modparams[p]))
740 setparameter(c, 0, p, &curconfig[p].val);
746 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
748 GdkRGBA bgcolor = { 0 };
749 WebKitSettings *s = webkit_web_view_get_settings(c->view);
751 modparams[p] = curconfig[p].prio;
754 case AcceleratedCanvas:
755 webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
757 case AccessMicrophone:
758 return; /* do nothing */
760 return; /* do nothing */
762 webkit_settings_set_enable_caret_browsing(s, a->i);
767 setcert(c, geturi(c));
768 return; /* do not update */
770 webkit_cookie_manager_set_accept_policy(
771 webkit_web_context_get_cookie_manager(
772 webkit_web_view_get_context(c->view)),
777 webkit_web_context_set_cache_model(
778 webkit_web_view_get_context(c->view), a->i ?
779 WEBKIT_CACHE_MODEL_WEB_BROWSER :
780 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
781 return; /* do not update */
783 webkit_settings_set_default_charset(s, a->v);
784 return; /* do not update */
786 webkit_settings_set_enable_dns_prefetching(s, a->i);
787 return; /* do not update */
788 case FileURLsCrossAccess:
789 webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
790 webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
791 return; /* do not update */
793 webkit_settings_set_default_font_size(s, a->i);
794 return; /* do not update */
795 case FrameFlattening:
796 webkit_settings_set_enable_frame_flattening(s, a->i);
803 webkit_web_view_set_background_color(c->view, &bgcolor);
804 return; /* do not update */
806 webkit_settings_set_enable_developer_extras(s, a->i);
807 return; /* do not update */
809 webkit_settings_set_enable_java(s, a->i);
810 return; /* do not update */
812 webkit_settings_set_enable_javascript(s, a->i);
815 return; /* do nothing */
817 webkit_settings_set_auto_load_images(s, a->i);
819 case MediaManualPlay:
820 webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
823 webkit_settings_set_enable_plugins(s, a->i);
825 case PreferredLanguages:
826 return; /* do nothing */
827 case RunInFullscreen:
828 return; /* do nothing */
830 /* Disabled until we write some WebKitWebExtension for
831 * manipulating the DOM directly.
832 enablescrollbars = !enablescrollbars;
833 evalscript(c, "document.documentElement.style.overflow = '%s'",
834 enablescrollbars ? "auto" : "hidden");
836 return; /* do not update */
839 case SmoothScrolling:
840 webkit_settings_set_enable_smooth_scrolling(s, a->i);
841 return; /* do not update */
843 webkit_settings_set_enable_site_specific_quirks(s, a->i);
846 webkit_web_context_set_spell_checking_enabled(
847 webkit_web_view_get_context(c->view), a->i);
848 return; /* do not update */
850 return; /* do nothing */
852 webkit_web_context_set_tls_errors_policy(
853 webkit_web_view_get_context(c->view), a->i ?
854 WEBKIT_TLS_ERRORS_POLICY_FAIL :
855 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
858 webkit_user_content_manager_remove_all_style_sheets(
859 webkit_web_view_get_user_content_manager(c->view));
861 setstyle(c, getstyle(geturi(c)));
865 webkit_settings_set_enable_webgl(s, a->i);
868 webkit_web_view_set_zoom_level(c->view, a->f);
869 return; /* do not update */
871 return; /* do nothing */
880 getcert(const char *uri)
884 for (i = 0; i < LENGTH(certs); ++i) {
885 if (certs[i].regex &&
886 !regexec(&(certs[i].re), uri, 0, NULL, 0))
887 return certs[i].file;
894 setcert(Client *c, const char *uri)
896 const char *file = getcert(uri);
898 GTlsCertificate *cert;
903 if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
904 fprintf(stderr, "Could not read certificate file: %s\n", file);
908 if ((uri = strstr(uri, "https://"))) {
909 uri += sizeof("https://") - 1;
910 host = g_strndup(uri, strchr(uri, '/') - uri);
911 webkit_web_context_allow_tls_certificate_for_host(
912 webkit_web_view_get_context(c->view), cert, host);
916 g_object_unref(cert);
921 getstyle(const char *uri)
928 for (i = 0; i < LENGTH(styles); ++i) {
929 if (styles[i].regex &&
930 !regexec(&(styles[i].re), uri, 0, NULL, 0))
931 return styles[i].file;
938 setstyle(Client *c, const char *file)
942 if (!g_file_get_contents(file, &style, NULL, NULL)) {
943 fprintf(stderr, "Could not read style file: %s\n", file);
947 webkit_user_content_manager_add_style_sheet(
948 webkit_web_view_get_user_content_manager(c->view),
949 webkit_user_style_sheet_new(style,
950 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
951 WEBKIT_USER_STYLE_LEVEL_USER,
963 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
964 evalscript(c, "%s", script);
969 evalscript(Client *c, const char *jsstr, ...)
975 script = g_strdup_vprintf(jsstr, ap);
978 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
983 updatewinid(Client *c)
985 snprintf(winid, LENGTH(winid), "%lu", c->xid);
989 handleplumb(Client *c, const char *uri)
991 Arg a = (Arg)PLUMB(uri);
996 newwindow(Client *c, const Arg *a, int noembed)
1000 const char *cmd[29], *uri;
1001 const Arg arg = { .v = cmd };
1005 cmd[i++] = curconfig[CookiePolicies].val.v;
1006 cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
1007 if (cookiefile && g_strcmp0(cookiefile, "")) {
1009 cmd[i++] = cookiefile;
1011 if (stylefile && g_strcmp0(stylefile, "")) {
1013 cmd[i++] = stylefile;
1015 cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
1016 if (embed && !noembed) {
1018 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1021 cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
1022 cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
1023 cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
1024 cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
1025 cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
1026 cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
1027 cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
1028 if (scriptfile && g_strcmp0(scriptfile, "")) {
1030 cmd[i++] = scriptfile;
1032 cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
1033 cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
1034 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1036 cmd[i++] = fulluseragent;
1040 cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
1041 /* do not keep zoom level */
1051 spawn(Client *c, const Arg *a)
1055 close(ConnectionNumber(dpy));
1059 execvp(((char **)a->v)[0], (char **)a->v);
1060 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
1067 destroyclient(Client *c)
1071 webkit_web_view_stop_loading(c->view);
1072 /* Not needed, has already been called
1073 gtk_widget_destroy(c->win);
1076 for (p = clients; p && p->next != c; p = p->next)
1089 destroyclient(clients);
1101 newview(Client *c, WebKitWebView *rv)
1104 WebKitSettings *settings;
1105 WebKitWebContext *context;
1106 WebKitCookieManager *cookiemanager;
1107 WebKitUserContentManager *contentmanager;
1111 v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
1113 settings = webkit_settings_new_with_settings(
1114 "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1115 "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1116 "auto-load-images", curconfig[LoadImages].val.i,
1117 "default-charset", curconfig[DefaultCharset].val.v,
1118 "default-font-size", curconfig[FontSize].val.i,
1119 "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
1120 "enable-developer-extras", curconfig[Inspector].val.i,
1121 "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
1122 "enable-frame-flattening", curconfig[FrameFlattening].val.i,
1123 "enable-html5-database", curconfig[DiskCache].val.i,
1124 "enable-html5-local-storage", curconfig[DiskCache].val.i,
1125 "enable-java", curconfig[Java].val.i,
1126 "enable-javascript", curconfig[JavaScript].val.i,
1127 "enable-plugins", curconfig[Plugins].val.i,
1128 "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
1129 "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
1130 "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
1131 "enable-webgl", curconfig[WebGL].val.i,
1132 "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
1134 /* For more interesting settings, have a look at
1135 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
1137 if (strcmp(fulluseragent, "")) {
1138 webkit_settings_set_user_agent(settings, fulluseragent);
1139 } else if (surfuseragent) {
1140 webkit_settings_set_user_agent_with_application_details(
1141 settings, "Surf", VERSION);
1143 useragent = webkit_settings_get_user_agent(settings);
1145 contentmanager = webkit_user_content_manager_new();
1147 context = webkit_web_context_new_with_website_data_manager(
1148 webkit_website_data_manager_new(
1149 "base-cache-directory", cachedir,
1150 "base-data-directory", cachedir,
1153 cookiemanager = webkit_web_context_get_cookie_manager(context);
1155 /* rendering process model, can be a shared unique one
1156 * or one for each view */
1157 webkit_web_context_set_process_model(context,
1158 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1160 webkit_web_context_set_tls_errors_policy(context,
1161 curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
1162 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1164 webkit_web_context_set_cache_model(context,
1165 curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
1166 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1168 /* Currently only works with text file to be compatible with curl */
1169 webkit_cookie_manager_set_persistent_storage(cookiemanager,
1170 cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1172 webkit_cookie_manager_set_accept_policy(cookiemanager,
1173 cookiepolicy_get());
1175 webkit_web_context_set_preferred_languages(context,
1176 curconfig[PreferredLanguages].val.v);
1177 webkit_web_context_set_spell_checking_languages(context,
1178 curconfig[SpellLanguages].val.v);
1179 webkit_web_context_set_spell_checking_enabled(context,
1180 curconfig[SpellChecking].val.i);
1182 g_signal_connect(G_OBJECT(context), "download-started",
1183 G_CALLBACK(downloadstarted), c);
1184 g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
1185 G_CALLBACK(initwebextensions), c);
1187 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
1188 "settings", settings,
1189 "user-content-manager", contentmanager,
1190 "web-context", context,
1194 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
1195 G_CALLBACK(progresschanged), c);
1196 g_signal_connect(G_OBJECT(v), "notify::title",
1197 G_CALLBACK(titlechanged), c);
1198 g_signal_connect(G_OBJECT(v), "button-release-event",
1199 G_CALLBACK(buttonreleased), c);
1200 g_signal_connect(G_OBJECT(v), "close",
1201 G_CALLBACK(closeview), c);
1202 g_signal_connect(G_OBJECT(v), "create",
1203 G_CALLBACK(createview), c);
1204 g_signal_connect(G_OBJECT(v), "decide-policy",
1205 G_CALLBACK(decidepolicy), c);
1206 g_signal_connect(G_OBJECT(v), "insecure-content-detected",
1207 G_CALLBACK(insecurecontent), c);
1208 g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
1209 G_CALLBACK(loadfailedtls), c);
1210 g_signal_connect(G_OBJECT(v), "load-changed",
1211 G_CALLBACK(loadchanged), c);
1212 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
1213 G_CALLBACK(mousetargetchanged), c);
1214 g_signal_connect(G_OBJECT(v), "permission-request",
1215 G_CALLBACK(permissionrequested), c);
1216 g_signal_connect(G_OBJECT(v), "ready-to-show",
1217 G_CALLBACK(showview), c);
1218 g_signal_connect(G_OBJECT(v), "web-process-terminated",
1219 G_CALLBACK(webprocessterminated), c);
1225 readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
1227 static char msg[MSGBUFSZ], msgsz;
1228 GError *gerr = NULL;
1230 if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
1231 G_IO_STATUS_NORMAL) {
1232 fprintf(stderr, "surf: error reading pipe: %s\n",
1237 if ((msgsz = msg[0]) < 3) {
1238 fprintf(stderr, "surf: message too short: %d\n", msgsz);
1253 initwebextensions(WebKitWebContext *wc, Client *c)
1257 if (!pipeout[0] || !pipein[1])
1260 gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
1262 webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
1263 webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
1267 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
1271 switch (webkit_navigation_action_get_navigation_type(a)) {
1272 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1274 * popup windows of type “other” are almost always triggered
1275 * by user gesture, so inverse the logic here
1277 /* instead of this, compare destination uri to mouse-over uri for validating window */
1278 if (webkit_navigation_action_is_user_gesture(a))
1280 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1281 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1282 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1283 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1284 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1291 return GTK_WIDGET(n->view);
1295 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
1297 WebKitHitTestResultContext element;
1300 element = webkit_hit_test_result_get_context(c->mousepos);
1302 for (i = 0; i < LENGTH(buttons); ++i) {
1303 if (element & buttons[i].target &&
1304 e->button.button == buttons[i].button &&
1305 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
1307 buttons[i].func(c, &buttons[i].arg, c->mousepos);
1308 return buttons[i].stopevent;
1316 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1318 Client *c = (Client *)d;
1322 if (((XEvent *)e)->type == PropertyNotify) {
1323 ev = &((XEvent *)e)->xproperty;
1324 if (ev->state == PropertyNewValue) {
1325 if (ev->atom == atoms[AtomFind]) {
1328 return GDK_FILTER_REMOVE;
1329 } else if (ev->atom == atoms[AtomSearch]) {
1330 a.v = getatom(c, AtomSearch);
1332 } else if (ev->atom == atoms[AtomGo]) {
1333 a.v = getatom(c, AtomGo);
1336 return GDK_FILTER_REMOVE;
1340 return GDK_FILTER_CONTINUE;
1344 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1349 case GDK_ENTER_NOTIFY:
1350 c->overtitle = c->targeturi;
1354 if (!curconfig[KioskMode].val.i) {
1355 for (i = 0; i < LENGTH(keys); ++i) {
1356 if (gdk_keyval_to_lower(e->key.keyval) ==
1358 CLEANMASK(e->key.state) == keys[i].mod &&
1361 keys[i].func(c, &(keys[i].arg));
1366 case GDK_LEAVE_NOTIFY:
1367 c->overtitle = NULL;
1370 case GDK_WINDOW_STATE:
1371 if (e->window_state.changed_mask ==
1372 GDK_WINDOW_STATE_FULLSCREEN)
1373 c->fullscreen = e->window_state.new_window_state &
1374 GDK_WINDOW_STATE_FULLSCREEN;
1384 showview(WebKitWebView *v, Client *c)
1386 GdkRGBA bgcolor = { 0 };
1389 c->finder = webkit_web_view_get_find_controller(c->view);
1390 c->inspector = webkit_web_view_get_inspector(c->view);
1392 c->pageid = webkit_web_view_get_page_id(c->view);
1393 c->win = createwindow(c);
1395 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1396 gtk_widget_show_all(c->win);
1397 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1399 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1400 c->xid = gdk_x11_window_get_xid(gwin);
1403 gdk_display_sync(gtk_widget_get_display(c->win));
1408 if (curconfig[HideBackground].val.i)
1409 webkit_web_view_set_background_color(c->view, &bgcolor);
1411 if (!curconfig[KioskMode].val.i) {
1412 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1413 gdk_window_add_filter(gwin, processx, c);
1416 if (curconfig[RunInFullscreen].val.i)
1417 togglefullscreen(c, NULL);
1419 if (curconfig[ZoomLevel].val.f != 1.0)
1420 webkit_web_view_set_zoom_level(c->view,
1421 curconfig[ZoomLevel].val.f);
1423 setatom(c, AtomFind, "");
1424 setatom(c, AtomUri, "about:blank");
1428 createwindow(Client *c)
1434 w = gtk_plug_new(embed);
1436 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1438 wmstr = g_path_get_basename(argv0);
1439 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
1442 wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
1443 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1446 gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
1449 g_signal_connect(G_OBJECT(w), "destroy",
1450 G_CALLBACK(destroywin), c);
1451 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1452 G_CALLBACK(winevent), c);
1453 g_signal_connect(G_OBJECT(w), "key-press-event",
1454 G_CALLBACK(winevent), c);
1455 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1456 G_CALLBACK(winevent), c);
1457 g_signal_connect(G_OBJECT(w), "window-state-event",
1458 G_CALLBACK(winevent), c);
1464 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
1465 GTlsCertificateFlags err, Client *c)
1467 GString *errmsg = g_string_new(NULL);
1470 c->failedcert = g_object_ref(cert);
1474 if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
1475 g_string_append(errmsg,
1476 "The signing certificate authority is not known.<br>");
1477 if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
1478 g_string_append(errmsg,
1479 "The certificate does not match the expected identity "
1480 "of the site that it was retrieved from.<br>");
1481 if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
1482 g_string_append(errmsg,
1483 "The certificate's activation time "
1484 "is still in the future.<br>");
1485 if (err & G_TLS_CERTIFICATE_EXPIRED)
1486 g_string_append(errmsg, "The certificate has expired.<br>");
1487 if (err & G_TLS_CERTIFICATE_REVOKED)
1488 g_string_append(errmsg,
1489 "The certificate has been revoked according to "
1490 "the GTlsConnection's certificate revocation list.<br>");
1491 if (err & G_TLS_CERTIFICATE_INSECURE)
1492 g_string_append(errmsg,
1493 "The certificate's algorithm is considered insecure.<br>");
1494 if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
1495 g_string_append(errmsg,
1496 "Some error occurred validating the certificate.<br>");
1498 g_object_get(cert, "certificate-pem", &pem, NULL);
1499 html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
1500 "<p>You can inspect the following certificate "
1501 "with Ctrl-t (default keybinding).</p>"
1502 "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
1504 g_string_free(errmsg, TRUE);
1506 webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
1513 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1515 const char *uri = geturi(c);
1518 case WEBKIT_LOAD_STARTED:
1519 setatom(c, AtomUri, uri);
1521 c->https = c->insecure = 0;
1522 seturiparameters(c, uri, loadtransient);
1526 g_clear_object(&c->failedcert);
1528 case WEBKIT_LOAD_REDIRECTED:
1529 setatom(c, AtomUri, uri);
1531 seturiparameters(c, uri, loadtransient);
1533 case WEBKIT_LOAD_COMMITTED:
1534 seturiparameters(c, uri, loadcommitted);
1535 c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
1538 case WEBKIT_LOAD_FINISHED:
1539 seturiparameters(c, uri, loadfinished);
1540 /* Disabled until we write some WebKitWebExtension for
1541 * manipulating the DOM directly.
1542 evalscript(c, "document.documentElement.style.overflow = '%s'",
1543 enablescrollbars ? "auto" : "hidden");
1552 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1554 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1560 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1562 c->title = webkit_web_view_get_title(c->view);
1567 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1570 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1572 /* Keep the hit test to know where is the pointer on the next click */
1576 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1577 else if (hc & OnImg)
1578 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1579 else if (hc & OnMedia)
1580 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1582 c->targeturi = NULL;
1584 c->overtitle = c->targeturi;
1589 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1591 ParamName param = ParameterLast;
1593 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1594 param = Geolocation;
1595 } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
1596 if (webkit_user_media_permission_is_for_audio_device(
1597 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1598 param = AccessMicrophone;
1599 else if (webkit_user_media_permission_is_for_video_device(
1600 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1601 param = AccessWebcam;
1606 if (curconfig[param].val.i)
1607 webkit_permission_request_allow(r);
1609 webkit_permission_request_deny(r);
1615 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1616 WebKitPolicyDecisionType dt, Client *c)
1619 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1620 decidenavigation(d, c);
1622 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1623 decidenewwindow(d, c);
1625 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1626 decideresource(d, c);
1629 webkit_policy_decision_ignore(d);
1636 decidenavigation(WebKitPolicyDecision *d, Client *c)
1638 WebKitNavigationAction *a =
1639 webkit_navigation_policy_decision_get_navigation_action(
1640 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1642 switch (webkit_navigation_action_get_navigation_type(a)) {
1643 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1644 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1645 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1646 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1647 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1648 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1650 /* Do not navigate to links with a "_blank" target (popup) */
1651 if (webkit_navigation_policy_decision_get_frame_name(
1652 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1653 webkit_policy_decision_ignore(d);
1655 /* Filter out navigation to different domain ? */
1656 /* get action→urirequest, copy and load in new window+view
1657 * on Ctrl+Click ? */
1658 webkit_policy_decision_use(d);
1665 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1668 WebKitNavigationAction *a =
1669 webkit_navigation_policy_decision_get_navigation_action(
1670 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1673 switch (webkit_navigation_action_get_navigation_type(a)) {
1674 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1675 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1676 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1677 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1678 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1679 /* Filter domains here */
1680 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1681 * test for link clicked but no button ? */
1682 arg.v = webkit_uri_request_get_uri(
1683 webkit_navigation_action_get_request(a));
1684 newwindow(c, &arg, 0);
1686 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1691 webkit_policy_decision_ignore(d);
1695 decideresource(WebKitPolicyDecision *d, Client *c)
1698 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1699 WebKitURIResponse *res =
1700 webkit_response_policy_decision_get_response(r);
1701 const gchar *uri = webkit_uri_response_get_uri(res);
1703 if (g_str_has_suffix(uri, "/favicon.ico")) {
1704 webkit_policy_decision_ignore(d);
1708 if (!g_str_has_prefix(uri, "http://")
1709 && !g_str_has_prefix(uri, "https://")
1710 && !g_str_has_prefix(uri, "about:")
1711 && !g_str_has_prefix(uri, "file://")
1712 && !g_str_has_prefix(uri, "data:")
1713 && !g_str_has_prefix(uri, "blob:")
1714 && strlen(uri) > 0) {
1715 for (i = 0; i < strlen(uri); i++) {
1716 if (!g_ascii_isprint(uri[i])) {
1722 handleplumb(c, uri);
1723 webkit_policy_decision_ignore(d);
1728 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1729 webkit_policy_decision_use(d);
1731 webkit_policy_decision_ignore(d);
1737 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
1743 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1745 g_signal_connect(G_OBJECT(d), "notify::response",
1746 G_CALLBACK(responsereceived), c);
1750 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1752 download(c, webkit_download_get_response(d));
1753 webkit_download_cancel(d);
1757 download(Client *c, WebKitURIResponse *r)
1759 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1764 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
1767 fprintf(stderr, "web process terminated: %s\n",
1768 r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
1773 closeview(WebKitWebView *v, Client *c)
1775 gtk_widget_destroy(c->win);
1779 destroywin(GtkWidget* w, Client *c)
1787 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1789 Arg a = {.v = text };
1791 loaduri((Client *) d, &a);
1795 reload(Client *c, const Arg *a)
1798 webkit_web_view_reload_bypass_cache(c->view);
1800 webkit_web_view_reload(c->view);
1804 print(Client *c, const Arg *a)
1806 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1807 GTK_WINDOW(c->win));
1811 showcert(Client *c, const Arg *a)
1813 GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
1814 GcrCertificate *gcrt;
1817 GcrCertificateWidget *wcert;
1822 g_object_get(cert, "certificate", &crt, NULL);
1823 gcrt = gcr_simple_certificate_new(crt->data, crt->len);
1824 g_byte_array_unref(crt);
1826 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1827 wcert = gcr_certificate_widget_new(gcrt);
1828 g_object_unref(gcrt);
1830 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
1831 gtk_widget_show_all(win);
1835 clipboard(Client *c, const Arg *a)
1837 if (a->i) { /* load clipboard uri */
1838 gtk_clipboard_request_text(gtk_clipboard_get(
1839 GDK_SELECTION_PRIMARY),
1841 } else { /* copy uri */
1842 gtk_clipboard_set_text(gtk_clipboard_get(
1843 GDK_SELECTION_PRIMARY), c->targeturi
1844 ? c->targeturi : geturi(c), -1);
1849 zoom(Client *c, const Arg *a)
1852 webkit_web_view_set_zoom_level(c->view,
1853 curconfig[ZoomLevel].val.f + 0.1);
1855 webkit_web_view_set_zoom_level(c->view,
1856 curconfig[ZoomLevel].val.f - 0.1);
1858 webkit_web_view_set_zoom_level(c->view, 1.0);
1860 curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
1864 msgext(Client *c, char type, const Arg *a)
1866 static char msg[MSGBUFSZ];
1869 if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
1870 4, c->pageid, type, a->i))
1872 fprintf(stderr, "surf: message too long: %d\n", ret);
1876 if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
1877 fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
1881 scrollv(Client *c, const Arg *a)
1887 scrollh(Client *c, const Arg *a)
1893 navigate(Client *c, const Arg *a)
1896 webkit_web_view_go_back(c->view);
1898 webkit_web_view_go_forward(c->view);
1902 stop(Client *c, const Arg *a)
1904 webkit_web_view_stop_loading(c->view);
1908 toggle(Client *c, const Arg *a)
1910 curconfig[a->i].val.i ^= 1;
1911 setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
1915 togglefullscreen(Client *c, const Arg *a)
1917 /* toggling value is handled in winevent() */
1919 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1921 gtk_window_fullscreen(GTK_WINDOW(c->win));
1925 togglecookiepolicy(Client *c, const Arg *a)
1928 cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
1930 setparameter(c, 0, CookiePolicies, NULL);
1934 toggleinspector(Client *c, const Arg *a)
1936 if (webkit_web_inspector_is_attached(c->inspector))
1937 webkit_web_inspector_close(c->inspector);
1938 else if (curconfig[Inspector].val.i)
1939 webkit_web_inspector_show(c->inspector);
1943 find(Client *c, const Arg *a)
1949 webkit_find_controller_search_next(c->finder);
1951 webkit_find_controller_search_previous(c->finder);
1953 s = getatom(c, AtomFind);
1954 f = webkit_find_controller_get_search_text(c->finder);
1956 if (g_strcmp0(f, s) == 0) /* reset search */
1957 webkit_find_controller_search(c->finder, "", findopts,
1960 webkit_find_controller_search(c->finder, s, findopts,
1963 if (strcmp(s, "") == 0)
1964 webkit_find_controller_search_finish(c->finder);
1969 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1975 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1979 arg.v = webkit_hit_test_result_get_link_uri(h);
1980 newwindow(c, &arg, a->i);
1984 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1988 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1993 main(int argc, char *argv[])
1998 memset(&arg, 0, sizeof(arg));
2000 /* command line args */
2003 defconfig[CookiePolicies].val.v = EARGF(usage());
2004 defconfig[CookiePolicies].prio = 2;
2007 defconfig[ScrollBars].val.i = 0;
2008 defconfig[ScrollBars].prio = 2;
2011 defconfig[ScrollBars].val.i = 1;
2012 defconfig[ScrollBars].prio = 2;
2015 cookiefile = EARGF(usage());
2018 stylefile = EARGF(usage());
2021 defconfig[DiskCache].val.i = 0;
2022 defconfig[DiskCache].prio = 2;
2025 defconfig[DiskCache].val.i = 1;
2026 defconfig[DiskCache].prio = 2;
2029 embed = strtol(EARGF(usage()), NULL, 0);
2032 defconfig[RunInFullscreen].val.i = 0;
2033 defconfig[RunInFullscreen].prio = 2;
2036 defconfig[RunInFullscreen].val.i = 1;
2037 defconfig[RunInFullscreen].prio = 2;
2040 defconfig[Geolocation].val.i = 0;
2041 defconfig[Geolocation].prio = 2;
2044 defconfig[Geolocation].val.i = 1;
2045 defconfig[Geolocation].prio = 2;
2048 defconfig[LoadImages].val.i = 0;
2049 defconfig[LoadImages].prio = 2;
2052 defconfig[LoadImages].val.i = 1;
2053 defconfig[LoadImages].prio = 2;
2056 defconfig[KioskMode].val.i = 0;
2057 defconfig[KioskMode].prio = 2;
2060 defconfig[KioskMode].val.i = 1;
2061 defconfig[KioskMode].prio = 2;
2064 defconfig[Style].val.i = 0;
2065 defconfig[Style].prio = 2;
2068 defconfig[Style].val.i = 1;
2069 defconfig[Style].prio = 2;
2072 defconfig[Inspector].val.i = 0;
2073 defconfig[Inspector].prio = 2;
2076 defconfig[Inspector].val.i = 1;
2077 defconfig[Inspector].prio = 2;
2080 defconfig[Plugins].val.i = 0;
2081 defconfig[Plugins].prio = 2;
2084 defconfig[Plugins].val.i = 1;
2085 defconfig[Plugins].prio = 2;
2088 scriptfile = EARGF(usage());
2091 defconfig[JavaScript].val.i = 0;
2092 defconfig[JavaScript].prio = 2;
2095 defconfig[JavaScript].val.i = 1;
2096 defconfig[JavaScript].prio = 2;
2099 defconfig[StrictTLS].val.i = 0;
2100 defconfig[StrictTLS].prio = 2;
2103 defconfig[StrictTLS].val.i = 1;
2104 defconfig[StrictTLS].prio = 2;
2107 fulluseragent = EARGF(usage());
2110 die("surf-"VERSION", see LICENSE for © details\n");
2115 defconfig[Certificate].val.i = 0;
2116 defconfig[Certificate].prio = 2;
2119 defconfig[Certificate].val.i = 1;
2120 defconfig[Certificate].prio = 2;
2123 defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
2124 defconfig[ZoomLevel].prio = 2;
2135 arg.v = "about:blank";
2139 c = newclient(NULL);