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;
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);
236 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
237 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
238 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
240 static char winid[64];
241 static char togglestats[12];
242 static char pagestats[2];
243 static Atom atoms[AtomLast];
246 static int cookiepolicy;
248 static Client *clients;
249 static GdkDevice *gdkkb;
250 static char *stylefile;
251 static const char *useragent;
252 static Parameter *curconfig;
253 static int modparams[ParameterLast];
254 static int pipein[2], pipeout[2];
257 static ParamName loadtransient[] = {
271 static ParamName loadcommitted[] = {
297 static ParamName loadfinished[] = {
301 /* configuration, allows nested code to access above variables */
307 die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
308 "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
309 "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
319 /* clean up any zombies immediately */
321 if (signal(SIGHUP, sighup) == SIG_ERR)
322 die("Can't install SIGHUP handler");
324 if (!(dpy = XOpenDisplay(NULL)))
325 die("Can't open default display");
328 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
329 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
330 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
332 gtk_init(NULL, NULL);
334 gdpy = gdk_display_get_default();
336 curconfig = defconfig;
339 cookiefile = buildfile(cookiefile);
340 scriptfile = buildfile(scriptfile);
341 cachedir = buildpath(cachedir);
342 certdir = buildpath(certdir);
344 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
346 if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
347 fputs("Unable to create pipes\n", stderr);
349 gchanin = g_io_channel_unix_new(pipein[0]);
350 g_io_channel_set_encoding(gchanin, NULL, NULL);
351 g_io_channel_set_close_on_unref(gchanin, TRUE);
352 g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
356 for (i = 0; i < LENGTH(certs); ++i) {
357 if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
358 certs[i].file = g_strconcat(certdir, "/", certs[i].file,
361 fprintf(stderr, "Could not compile regex: %s\n",
363 certs[i].regex = NULL;
368 styledir = buildpath(styledir);
369 for (i = 0; i < LENGTH(styles); ++i) {
370 if (!regcomp(&(styles[i].re), styles[i].regex,
372 styles[i].file = g_strconcat(styledir, "/",
373 styles[i].file, NULL);
375 fprintf(stderr, "Could not compile regex: %s\n",
377 styles[i].regex = NULL;
382 stylefile = buildfile(stylefile);
385 for (i = 0; i < LENGTH(uriparams); ++i) {
386 if (regcomp(&(uriparams[i].re), uriparams[i].uri,
388 fprintf(stderr, "Could not compile regex: %s\n",
390 uriparams[i].uri = NULL;
394 /* copy default parameters with higher priority */
395 for (j = 0; j < ParameterLast; ++j) {
396 if (defconfig[j].prio >= uriparams[i].config[j].prio)
397 uriparams[i].config[j] = defconfig[j];
405 if (signal(SIGCHLD, sigchld) == SIG_ERR)
406 die("Can't install SIGCHLD handler");
407 while (waitpid(-1, NULL, WNOHANG) > 0)
417 for (c = clients; c; c = c->next)
422 buildfile(const char *path)
424 char *dname, *bname, *bpath, *fpath;
427 dname = g_path_get_dirname(path);
428 bname = g_path_get_basename(path);
430 bpath = buildpath(dname);
433 fpath = g_build_filename(bpath, bname, NULL);
437 if (!(f = fopen(fpath, "a")))
438 die("Could not open file: %s\n", fpath);
440 g_chmod(fpath, 0600); /* always */
447 getuserhomedir(const char *user)
449 struct passwd *pw = getpwnam(user);
452 die("Can't get user %s login information.\n", user);
458 getcurrentuserhomedir(void)
464 homedir = getenv("HOME");
468 user = getenv("USER");
470 return getuserhomedir(user);
472 pw = getpwuid(getuid());
474 die("Can't get current user home directory\n");
480 buildpath(const char *path)
485 apath = untildepath(path);
487 apath = g_strdup(path);
489 /* creating directory */
490 if (g_mkdir_with_parents(apath, 0700) < 0)
491 die("Could not access directory: %s\n", apath);
493 fpath = realpath(apath, NULL);
500 untildepath(const char *path)
502 char *apath, *name, *p;
505 if (path[1] == '/' || path[1] == '\0') {
506 p = (char *)&path[1];
507 homedir = getcurrentuserhomedir();
509 if ((p = strchr(path, '/')))
510 name = g_strndup(&path[1], p - (path + 1));
512 name = g_strdup(&path[1]);
514 homedir = getuserhomedir(name);
517 apath = g_build_filename(homedir, p, NULL);
522 newclient(Client *rc)
526 if (!(c = calloc(1, sizeof(Client))))
527 die("Cannot malloc!\n");
533 c->view = newview(c, rc ? rc->view : NULL);
539 loaduri(Client *c, const Arg *a)
542 char *url, *path, *apath;
543 const char *uri = a->v;
545 if (g_strcmp0(uri, "") == 0)
548 if (g_str_has_prefix(uri, "http://") ||
549 g_str_has_prefix(uri, "https://") ||
550 g_str_has_prefix(uri, "file://") ||
551 g_str_has_prefix(uri, "about:")) {
555 apath = untildepath(uri);
558 if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
559 url = g_strdup_printf("file://%s", path);
562 url = g_strdup_printf("http://%s", uri);
568 setatom(c, AtomUri, url);
570 if (strcmp(url, geturi(c)) == 0) {
573 webkit_web_view_load_uri(c->view, url);
585 if (!(uri = webkit_web_view_get_uri(c->view)))
591 setatom(Client *c, int a, const char *v)
593 XChangeProperty(dpy, c->xid,
594 atoms[a], XA_STRING, 8, PropModeReplace,
595 (unsigned char *)v, strlen(v) + 1);
600 getatom(Client *c, int a)
602 static char buf[BUFSIZ];
605 unsigned long ldummy;
606 unsigned char *p = NULL;
609 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
610 &adummy, &idummy, &ldummy, &ldummy, &p);
612 strncpy(buf, (char *)p, LENGTH(buf) - 1);
621 updatetitle(Client *c)
624 const char *name = c->overtitle ? c->overtitle :
625 c->title ? c->title : "";
627 if (curconfig[ShowIndicators].val.i) {
631 if (c->progress != 100)
632 title = g_strdup_printf("[%i%%] %s:%s | %s",
633 c->progress, togglestats, pagestats, name);
635 title = g_strdup_printf("%s:%s | %s",
636 togglestats, pagestats, name);
638 gtk_window_set_title(GTK_WINDOW(c->win), title);
641 gtk_window_set_title(GTK_WINDOW(c->win), name);
646 gettogglestats(Client *c)
648 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
649 togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
650 togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
651 togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
652 togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
653 togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
654 togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
655 togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
656 togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
657 togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
658 togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
659 togglestats[11] = '\0';
663 getpagestats(Client *c)
666 pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
672 WebKitCookieAcceptPolicy
673 cookiepolicy_get(void)
675 switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
677 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
679 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
680 default: /* fallthrough */
682 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
687 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
690 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
692 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
694 default: /* fallthrough */
695 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
701 seturiparameters(Client *c, const char *uri, ParamName *params)
703 Parameter *config, *uriconfig = NULL;
706 for (i = 0; i < LENGTH(uriparams); ++i) {
707 if (uriparams[i].uri &&
708 !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
709 uriconfig = uriparams[i].config;
714 curconfig = uriconfig ? uriconfig : defconfig;
716 for (i = 0; (p = params[i]) != ParameterLast; ++i) {
718 default: /* FALLTHROUGH */
719 if (!(defconfig[p].prio < curconfig[p].prio ||
720 defconfig[p].prio < modparams[p]))
725 setparameter(c, 0, p, &curconfig[p].val);
731 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
733 GdkRGBA bgcolor = { 0 };
734 WebKitSettings *s = webkit_web_view_get_settings(c->view);
736 modparams[p] = curconfig[p].prio;
739 case AcceleratedCanvas:
740 webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
742 case AccessMicrophone:
743 return; /* do nothing */
745 return; /* do nothing */
747 webkit_settings_set_enable_caret_browsing(s, a->i);
752 setcert(c, geturi(c));
753 return; /* do not update */
755 webkit_cookie_manager_set_accept_policy(
756 webkit_web_context_get_cookie_manager(
757 webkit_web_view_get_context(c->view)),
762 webkit_web_context_set_cache_model(
763 webkit_web_view_get_context(c->view), a->i ?
764 WEBKIT_CACHE_MODEL_WEB_BROWSER :
765 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
766 return; /* do not update */
768 webkit_settings_set_default_charset(s, a->v);
769 return; /* do not update */
771 webkit_settings_set_enable_dns_prefetching(s, a->i);
772 return; /* do not update */
773 case FileURLsCrossAccess:
774 webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
775 webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
776 return; /* do not update */
778 webkit_settings_set_default_font_size(s, a->i);
779 return; /* do not update */
780 case FrameFlattening:
781 webkit_settings_set_enable_frame_flattening(s, a->i);
788 webkit_web_view_set_background_color(c->view, &bgcolor);
789 return; /* do not update */
791 webkit_settings_set_enable_developer_extras(s, a->i);
792 return; /* do not update */
794 webkit_settings_set_enable_java(s, a->i);
795 return; /* do not update */
797 webkit_settings_set_enable_javascript(s, a->i);
800 return; /* do nothing */
802 webkit_settings_set_auto_load_images(s, a->i);
804 case MediaManualPlay:
805 webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
808 webkit_settings_set_enable_plugins(s, a->i);
810 case PreferredLanguages:
811 return; /* do nothing */
812 case RunInFullscreen:
813 return; /* do nothing */
815 /* Disabled until we write some WebKitWebExtension for
816 * manipulating the DOM directly.
817 enablescrollbars = !enablescrollbars;
818 evalscript(c, "document.documentElement.style.overflow = '%s'",
819 enablescrollbars ? "auto" : "hidden");
821 return; /* do not update */
824 case SmoothScrolling:
825 webkit_settings_set_enable_smooth_scrolling(s, a->i);
826 return; /* do not update */
828 webkit_settings_set_enable_site_specific_quirks(s, a->i);
831 webkit_web_context_set_spell_checking_enabled(
832 webkit_web_view_get_context(c->view), a->i);
833 return; /* do not update */
835 return; /* do nothing */
837 webkit_web_context_set_tls_errors_policy(
838 webkit_web_view_get_context(c->view), a->i ?
839 WEBKIT_TLS_ERRORS_POLICY_FAIL :
840 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
843 webkit_user_content_manager_remove_all_style_sheets(
844 webkit_web_view_get_user_content_manager(c->view));
846 setstyle(c, getstyle(geturi(c)));
850 webkit_settings_set_enable_webgl(s, a->i);
853 webkit_web_view_set_zoom_level(c->view, a->f);
854 return; /* do not update */
856 return; /* do nothing */
865 getcert(const char *uri)
869 for (i = 0; i < LENGTH(certs); ++i) {
870 if (certs[i].regex &&
871 !regexec(&(certs[i].re), uri, 0, NULL, 0))
872 return certs[i].file;
879 setcert(Client *c, const char *uri)
881 const char *file = getcert(uri);
883 GTlsCertificate *cert;
888 if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
889 fprintf(stderr, "Could not read certificate file: %s\n", file);
893 if ((uri = strstr(uri, "https://"))) {
894 uri += sizeof("https://") - 1;
895 host = g_strndup(uri, strchr(uri, '/') - uri);
896 webkit_web_context_allow_tls_certificate_for_host(
897 webkit_web_view_get_context(c->view), cert, host);
901 g_object_unref(cert);
906 getstyle(const char *uri)
913 for (i = 0; i < LENGTH(styles); ++i) {
914 if (styles[i].regex &&
915 !regexec(&(styles[i].re), uri, 0, NULL, 0))
916 return styles[i].file;
923 setstyle(Client *c, const char *file)
927 if (!g_file_get_contents(file, &style, NULL, NULL)) {
928 fprintf(stderr, "Could not read style file: %s\n", file);
932 webkit_user_content_manager_add_style_sheet(
933 webkit_web_view_get_user_content_manager(c->view),
934 webkit_user_style_sheet_new(style,
935 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
936 WEBKIT_USER_STYLE_LEVEL_USER,
948 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
949 evalscript(c, "%s", script);
954 evalscript(Client *c, const char *jsstr, ...)
960 script = g_strdup_vprintf(jsstr, ap);
963 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
968 updatewinid(Client *c)
970 snprintf(winid, LENGTH(winid), "%lu", c->xid);
974 handleplumb(Client *c, const char *uri)
976 Arg a = (Arg)PLUMB(uri);
981 newwindow(Client *c, const Arg *a, int noembed)
985 const char *cmd[29], *uri;
986 const Arg arg = { .v = cmd };
990 cmd[i++] = curconfig[CookiePolicies].val.v;
991 cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
992 if (cookiefile && g_strcmp0(cookiefile, "")) {
994 cmd[i++] = cookiefile;
996 if (stylefile && g_strcmp0(stylefile, "")) {
998 cmd[i++] = stylefile;
1000 cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
1001 if (embed && !noembed) {
1003 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1006 cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
1007 cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
1008 cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
1009 cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
1010 cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
1011 cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
1012 cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
1013 if (scriptfile && g_strcmp0(scriptfile, "")) {
1015 cmd[i++] = scriptfile;
1017 cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
1018 cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
1019 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1021 cmd[i++] = fulluseragent;
1025 cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
1026 /* do not keep zoom level */
1036 spawn(Client *c, const Arg *a)
1040 close(ConnectionNumber(dpy));
1044 execvp(((char **)a->v)[0], (char **)a->v);
1045 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
1052 destroyclient(Client *c)
1056 webkit_web_view_stop_loading(c->view);
1057 /* Not needed, has already been called
1058 gtk_widget_destroy(c->win);
1061 for (p = clients; p && p->next != c; p = p->next)
1074 destroyclient(clients);
1086 newview(Client *c, WebKitWebView *rv)
1089 WebKitSettings *settings;
1090 WebKitWebContext *context;
1091 WebKitCookieManager *cookiemanager;
1092 WebKitUserContentManager *contentmanager;
1096 v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
1098 settings = webkit_settings_new_with_settings(
1099 "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1100 "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1101 "auto-load-images", curconfig[LoadImages].val.i,
1102 "default-charset", curconfig[DefaultCharset].val.v,
1103 "default-font-size", curconfig[FontSize].val.i,
1104 "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
1105 "enable-developer-extras", curconfig[Inspector].val.i,
1106 "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
1107 "enable-frame-flattening", curconfig[FrameFlattening].val.i,
1108 "enable-html5-database", curconfig[DiskCache].val.i,
1109 "enable-html5-local-storage", curconfig[DiskCache].val.i,
1110 "enable-java", curconfig[Java].val.i,
1111 "enable-javascript", curconfig[JavaScript].val.i,
1112 "enable-plugins", curconfig[Plugins].val.i,
1113 "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
1114 "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
1115 "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
1116 "enable-webgl", curconfig[WebGL].val.i,
1117 "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
1119 /* For more interesting settings, have a look at
1120 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
1122 if (strcmp(fulluseragent, "")) {
1123 webkit_settings_set_user_agent(settings, fulluseragent);
1124 } else if (surfuseragent) {
1125 webkit_settings_set_user_agent_with_application_details(
1126 settings, "Surf", VERSION);
1128 useragent = webkit_settings_get_user_agent(settings);
1130 contentmanager = webkit_user_content_manager_new();
1132 context = webkit_web_context_new_with_website_data_manager(
1133 webkit_website_data_manager_new(
1134 "base-cache-directory", cachedir,
1135 "base-data-directory", cachedir,
1138 cookiemanager = webkit_web_context_get_cookie_manager(context);
1140 /* rendering process model, can be a shared unique one
1141 * or one for each view */
1142 webkit_web_context_set_process_model(context,
1143 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1145 webkit_web_context_set_tls_errors_policy(context,
1146 curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
1147 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1149 webkit_web_context_set_cache_model(context,
1150 curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
1151 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1153 /* Currently only works with text file to be compatible with curl */
1154 webkit_cookie_manager_set_persistent_storage(cookiemanager,
1155 cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1157 webkit_cookie_manager_set_accept_policy(cookiemanager,
1158 cookiepolicy_get());
1160 webkit_web_context_set_preferred_languages(context,
1161 curconfig[PreferredLanguages].val.v);
1162 webkit_web_context_set_spell_checking_languages(context,
1163 curconfig[SpellLanguages].val.v);
1164 webkit_web_context_set_spell_checking_enabled(context,
1165 curconfig[SpellChecking].val.i);
1167 g_signal_connect(G_OBJECT(context), "download-started",
1168 G_CALLBACK(downloadstarted), c);
1169 g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
1170 G_CALLBACK(initwebextensions), c);
1172 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
1173 "settings", settings,
1174 "user-content-manager", contentmanager,
1175 "web-context", context,
1179 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
1180 G_CALLBACK(progresschanged), c);
1181 g_signal_connect(G_OBJECT(v), "notify::title",
1182 G_CALLBACK(titlechanged), c);
1183 g_signal_connect(G_OBJECT(v), "button-release-event",
1184 G_CALLBACK(buttonreleased), c);
1185 g_signal_connect(G_OBJECT(v), "close",
1186 G_CALLBACK(closeview), c);
1187 g_signal_connect(G_OBJECT(v), "create",
1188 G_CALLBACK(createview), c);
1189 g_signal_connect(G_OBJECT(v), "decide-policy",
1190 G_CALLBACK(decidepolicy), c);
1191 g_signal_connect(G_OBJECT(v), "insecure-content-detected",
1192 G_CALLBACK(insecurecontent), c);
1193 g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
1194 G_CALLBACK(loadfailedtls), c);
1195 g_signal_connect(G_OBJECT(v), "load-changed",
1196 G_CALLBACK(loadchanged), c);
1197 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
1198 G_CALLBACK(mousetargetchanged), c);
1199 g_signal_connect(G_OBJECT(v), "permission-request",
1200 G_CALLBACK(permissionrequested), c);
1201 g_signal_connect(G_OBJECT(v), "ready-to-show",
1202 G_CALLBACK(showview), c);
1203 g_signal_connect(G_OBJECT(v), "web-process-terminated",
1204 G_CALLBACK(webprocessterminated), c);
1210 readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
1212 static char msg[MSGBUFSZ], msgsz;
1213 GError *gerr = NULL;
1215 if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
1216 G_IO_STATUS_NORMAL) {
1217 fprintf(stderr, "surf: error reading pipe: %s\n",
1222 if ((msgsz = msg[0]) < 3) {
1223 fprintf(stderr, "surf: message too short: %d\n", msgsz);
1238 initwebextensions(WebKitWebContext *wc, Client *c)
1242 if (!pipeout[0] || !pipein[1])
1245 gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
1247 webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
1248 webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
1252 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
1256 switch (webkit_navigation_action_get_navigation_type(a)) {
1257 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1259 * popup windows of type “other” are almost always triggered
1260 * by user gesture, so inverse the logic here
1262 /* instead of this, compare destination uri to mouse-over uri for validating window */
1263 if (webkit_navigation_action_is_user_gesture(a))
1265 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1266 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1267 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1268 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1269 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1276 return GTK_WIDGET(n->view);
1280 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
1282 WebKitHitTestResultContext element;
1285 element = webkit_hit_test_result_get_context(c->mousepos);
1287 for (i = 0; i < LENGTH(buttons); ++i) {
1288 if (element & buttons[i].target &&
1289 e->button.button == buttons[i].button &&
1290 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
1292 buttons[i].func(c, &buttons[i].arg, c->mousepos);
1293 return buttons[i].stopevent;
1301 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1303 Client *c = (Client *)d;
1307 if (((XEvent *)e)->type == PropertyNotify) {
1308 ev = &((XEvent *)e)->xproperty;
1309 if (ev->state == PropertyNewValue) {
1310 if (ev->atom == atoms[AtomFind]) {
1313 return GDK_FILTER_REMOVE;
1314 } else if (ev->atom == atoms[AtomGo]) {
1315 a.v = getatom(c, AtomGo);
1318 return GDK_FILTER_REMOVE;
1322 return GDK_FILTER_CONTINUE;
1326 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1331 case GDK_ENTER_NOTIFY:
1332 c->overtitle = c->targeturi;
1336 if (!curconfig[KioskMode].val.i) {
1337 for (i = 0; i < LENGTH(keys); ++i) {
1338 if (gdk_keyval_to_lower(e->key.keyval) ==
1340 CLEANMASK(e->key.state) == keys[i].mod &&
1343 keys[i].func(c, &(keys[i].arg));
1348 case GDK_LEAVE_NOTIFY:
1349 c->overtitle = NULL;
1352 case GDK_WINDOW_STATE:
1353 if (e->window_state.changed_mask ==
1354 GDK_WINDOW_STATE_FULLSCREEN)
1355 c->fullscreen = e->window_state.new_window_state &
1356 GDK_WINDOW_STATE_FULLSCREEN;
1366 showview(WebKitWebView *v, Client *c)
1368 GdkRGBA bgcolor = { 0 };
1371 c->finder = webkit_web_view_get_find_controller(c->view);
1372 c->inspector = webkit_web_view_get_inspector(c->view);
1374 c->pageid = webkit_web_view_get_page_id(c->view);
1375 c->win = createwindow(c);
1377 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1378 gtk_widget_show_all(c->win);
1379 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1381 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1382 c->xid = gdk_x11_window_get_xid(gwin);
1385 gdk_display_sync(gtk_widget_get_display(c->win));
1390 if (curconfig[HideBackground].val.i)
1391 webkit_web_view_set_background_color(c->view, &bgcolor);
1393 if (!curconfig[KioskMode].val.i) {
1394 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1395 gdk_window_add_filter(gwin, processx, c);
1398 if (curconfig[RunInFullscreen].val.i)
1399 togglefullscreen(c, NULL);
1401 if (curconfig[ZoomLevel].val.f != 1.0)
1402 webkit_web_view_set_zoom_level(c->view,
1403 curconfig[ZoomLevel].val.f);
1405 setatom(c, AtomFind, "");
1406 setatom(c, AtomUri, "about:blank");
1410 createwindow(Client *c)
1416 w = gtk_plug_new(embed);
1418 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1420 wmstr = g_path_get_basename(argv0);
1421 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
1424 wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
1425 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1428 gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
1431 g_signal_connect(G_OBJECT(w), "destroy",
1432 G_CALLBACK(destroywin), c);
1433 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1434 G_CALLBACK(winevent), c);
1435 g_signal_connect(G_OBJECT(w), "key-press-event",
1436 G_CALLBACK(winevent), c);
1437 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1438 G_CALLBACK(winevent), c);
1439 g_signal_connect(G_OBJECT(w), "window-state-event",
1440 G_CALLBACK(winevent), c);
1446 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
1447 GTlsCertificateFlags err, Client *c)
1449 GString *errmsg = g_string_new(NULL);
1452 c->failedcert = g_object_ref(cert);
1456 if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
1457 g_string_append(errmsg,
1458 "The signing certificate authority is not known.<br>");
1459 if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
1460 g_string_append(errmsg,
1461 "The certificate does not match the expected identity "
1462 "of the site that it was retrieved from.<br>");
1463 if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
1464 g_string_append(errmsg,
1465 "The certificate's activation time "
1466 "is still in the future.<br>");
1467 if (err & G_TLS_CERTIFICATE_EXPIRED)
1468 g_string_append(errmsg, "The certificate has expired.<br>");
1469 if (err & G_TLS_CERTIFICATE_REVOKED)
1470 g_string_append(errmsg,
1471 "The certificate has been revoked according to "
1472 "the GTlsConnection's certificate revocation list.<br>");
1473 if (err & G_TLS_CERTIFICATE_INSECURE)
1474 g_string_append(errmsg,
1475 "The certificate's algorithm is considered insecure.<br>");
1476 if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
1477 g_string_append(errmsg,
1478 "Some error occurred validating the certificate.<br>");
1480 g_object_get(cert, "certificate-pem", &pem, NULL);
1481 html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
1482 "<p>You can inspect the following certificate "
1483 "with Ctrl-t (default keybinding).</p>"
1484 "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
1486 g_string_free(errmsg, TRUE);
1488 webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
1495 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1497 const char *uri = geturi(c);
1500 case WEBKIT_LOAD_STARTED:
1501 setatom(c, AtomUri, uri);
1503 c->https = c->insecure = 0;
1504 seturiparameters(c, uri, loadtransient);
1508 g_clear_object(&c->failedcert);
1510 case WEBKIT_LOAD_REDIRECTED:
1511 setatom(c, AtomUri, uri);
1513 seturiparameters(c, uri, loadtransient);
1515 case WEBKIT_LOAD_COMMITTED:
1516 seturiparameters(c, uri, loadcommitted);
1517 c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
1520 case WEBKIT_LOAD_FINISHED:
1521 seturiparameters(c, uri, loadfinished);
1522 /* Disabled until we write some WebKitWebExtension for
1523 * manipulating the DOM directly.
1524 evalscript(c, "document.documentElement.style.overflow = '%s'",
1525 enablescrollbars ? "auto" : "hidden");
1534 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1536 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1542 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1544 c->title = webkit_web_view_get_title(c->view);
1549 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1552 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1554 /* Keep the hit test to know where is the pointer on the next click */
1558 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1559 else if (hc & OnImg)
1560 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1561 else if (hc & OnMedia)
1562 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1564 c->targeturi = NULL;
1566 c->overtitle = c->targeturi;
1571 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1573 ParamName param = ParameterLast;
1575 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1576 param = Geolocation;
1577 } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
1578 if (webkit_user_media_permission_is_for_audio_device(
1579 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1580 param = AccessMicrophone;
1581 else if (webkit_user_media_permission_is_for_video_device(
1582 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1583 param = AccessWebcam;
1588 if (curconfig[param].val.i)
1589 webkit_permission_request_allow(r);
1591 webkit_permission_request_deny(r);
1597 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1598 WebKitPolicyDecisionType dt, Client *c)
1601 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1602 decidenavigation(d, c);
1604 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1605 decidenewwindow(d, c);
1607 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1608 decideresource(d, c);
1611 webkit_policy_decision_ignore(d);
1618 decidenavigation(WebKitPolicyDecision *d, Client *c)
1620 WebKitNavigationAction *a =
1621 webkit_navigation_policy_decision_get_navigation_action(
1622 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1624 switch (webkit_navigation_action_get_navigation_type(a)) {
1625 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1626 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1627 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1628 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1629 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1630 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1632 /* Do not navigate to links with a "_blank" target (popup) */
1633 if (webkit_navigation_policy_decision_get_frame_name(
1634 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1635 webkit_policy_decision_ignore(d);
1637 /* Filter out navigation to different domain ? */
1638 /* get action→urirequest, copy and load in new window+view
1639 * on Ctrl+Click ? */
1640 webkit_policy_decision_use(d);
1647 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1650 WebKitNavigationAction *a =
1651 webkit_navigation_policy_decision_get_navigation_action(
1652 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1655 switch (webkit_navigation_action_get_navigation_type(a)) {
1656 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1657 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1658 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1659 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1660 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1661 /* Filter domains here */
1662 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1663 * test for link clicked but no button ? */
1664 arg.v = webkit_uri_request_get_uri(
1665 webkit_navigation_action_get_request(a));
1666 newwindow(c, &arg, 0);
1668 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1673 webkit_policy_decision_ignore(d);
1677 decideresource(WebKitPolicyDecision *d, Client *c)
1680 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1681 WebKitURIResponse *res =
1682 webkit_response_policy_decision_get_response(r);
1683 const gchar *uri = webkit_uri_response_get_uri(res);
1685 if (g_str_has_suffix(uri, "/favicon.ico")) {
1686 webkit_policy_decision_ignore(d);
1690 if (!g_str_has_prefix(uri, "http://")
1691 && !g_str_has_prefix(uri, "https://")
1692 && !g_str_has_prefix(uri, "about:")
1693 && !g_str_has_prefix(uri, "file://")
1694 && !g_str_has_prefix(uri, "data:")
1695 && !g_str_has_prefix(uri, "blob:")
1696 && strlen(uri) > 0) {
1697 for (i = 0; i < strlen(uri); i++) {
1698 if (!g_ascii_isprint(uri[i])) {
1704 handleplumb(c, uri);
1705 webkit_policy_decision_ignore(d);
1710 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1711 webkit_policy_decision_use(d);
1713 webkit_policy_decision_ignore(d);
1719 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
1725 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1727 g_signal_connect(G_OBJECT(d), "notify::response",
1728 G_CALLBACK(responsereceived), c);
1732 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1734 download(c, webkit_download_get_response(d));
1735 webkit_download_cancel(d);
1739 download(Client *c, WebKitURIResponse *r)
1741 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1746 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
1749 fprintf(stderr, "web process terminated: %s\n",
1750 r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
1755 closeview(WebKitWebView *v, Client *c)
1757 gtk_widget_destroy(c->win);
1761 destroywin(GtkWidget* w, Client *c)
1769 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1771 Arg a = {.v = text };
1773 loaduri((Client *) d, &a);
1777 reload(Client *c, const Arg *a)
1780 webkit_web_view_reload_bypass_cache(c->view);
1782 webkit_web_view_reload(c->view);
1786 print(Client *c, const Arg *a)
1788 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1789 GTK_WINDOW(c->win));
1793 showcert(Client *c, const Arg *a)
1795 GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
1796 GcrCertificate *gcrt;
1799 GcrCertificateWidget *wcert;
1804 g_object_get(cert, "certificate", &crt, NULL);
1805 gcrt = gcr_simple_certificate_new(crt->data, crt->len);
1806 g_byte_array_unref(crt);
1808 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1809 wcert = gcr_certificate_widget_new(gcrt);
1810 g_object_unref(gcrt);
1812 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
1813 gtk_widget_show_all(win);
1817 clipboard(Client *c, const Arg *a)
1819 if (a->i) { /* load clipboard uri */
1820 gtk_clipboard_request_text(gtk_clipboard_get(
1821 GDK_SELECTION_PRIMARY),
1823 } else { /* copy uri */
1824 gtk_clipboard_set_text(gtk_clipboard_get(
1825 GDK_SELECTION_PRIMARY), c->targeturi
1826 ? c->targeturi : geturi(c), -1);
1831 zoom(Client *c, const Arg *a)
1834 webkit_web_view_set_zoom_level(c->view,
1835 curconfig[ZoomLevel].val.f + 0.1);
1837 webkit_web_view_set_zoom_level(c->view,
1838 curconfig[ZoomLevel].val.f - 0.1);
1840 webkit_web_view_set_zoom_level(c->view, 1.0);
1842 curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
1846 msgext(Client *c, char type, const Arg *a)
1848 static char msg[MSGBUFSZ];
1851 if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
1852 4, c->pageid, type, a->i))
1854 fprintf(stderr, "surf: message too long: %d\n", ret);
1858 if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
1859 fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
1863 scrollv(Client *c, const Arg *a)
1869 scrollh(Client *c, const Arg *a)
1875 navigate(Client *c, const Arg *a)
1878 webkit_web_view_go_back(c->view);
1880 webkit_web_view_go_forward(c->view);
1884 stop(Client *c, const Arg *a)
1886 webkit_web_view_stop_loading(c->view);
1890 toggle(Client *c, const Arg *a)
1892 curconfig[a->i].val.i ^= 1;
1893 setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
1897 togglefullscreen(Client *c, const Arg *a)
1899 /* toggling value is handled in winevent() */
1901 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1903 gtk_window_fullscreen(GTK_WINDOW(c->win));
1907 togglecookiepolicy(Client *c, const Arg *a)
1910 cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
1912 setparameter(c, 0, CookiePolicies, NULL);
1916 toggleinspector(Client *c, const Arg *a)
1918 if (webkit_web_inspector_is_attached(c->inspector))
1919 webkit_web_inspector_close(c->inspector);
1920 else if (curconfig[Inspector].val.i)
1921 webkit_web_inspector_show(c->inspector);
1925 find(Client *c, const Arg *a)
1931 webkit_find_controller_search_next(c->finder);
1933 webkit_find_controller_search_previous(c->finder);
1935 s = getatom(c, AtomFind);
1936 f = webkit_find_controller_get_search_text(c->finder);
1938 if (g_strcmp0(f, s) == 0) /* reset search */
1939 webkit_find_controller_search(c->finder, "", findopts,
1942 webkit_find_controller_search(c->finder, s, findopts,
1945 if (strcmp(s, "") == 0)
1946 webkit_find_controller_search_finish(c->finder);
1951 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1957 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1961 arg.v = webkit_hit_test_result_get_link_uri(h);
1962 newwindow(c, &arg, a->i);
1966 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1970 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1975 main(int argc, char *argv[])
1980 memset(&arg, 0, sizeof(arg));
1982 /* command line args */
1985 defconfig[CookiePolicies].val.v = EARGF(usage());
1986 defconfig[CookiePolicies].prio = 2;
1989 defconfig[ScrollBars].val.i = 0;
1990 defconfig[ScrollBars].prio = 2;
1993 defconfig[ScrollBars].val.i = 1;
1994 defconfig[ScrollBars].prio = 2;
1997 cookiefile = EARGF(usage());
2000 stylefile = EARGF(usage());
2003 defconfig[DiskCache].val.i = 0;
2004 defconfig[DiskCache].prio = 2;
2007 defconfig[DiskCache].val.i = 1;
2008 defconfig[DiskCache].prio = 2;
2011 embed = strtol(EARGF(usage()), NULL, 0);
2014 defconfig[RunInFullscreen].val.i = 0;
2015 defconfig[RunInFullscreen].prio = 2;
2018 defconfig[RunInFullscreen].val.i = 1;
2019 defconfig[RunInFullscreen].prio = 2;
2022 defconfig[Geolocation].val.i = 0;
2023 defconfig[Geolocation].prio = 2;
2026 defconfig[Geolocation].val.i = 1;
2027 defconfig[Geolocation].prio = 2;
2030 defconfig[LoadImages].val.i = 0;
2031 defconfig[LoadImages].prio = 2;
2034 defconfig[LoadImages].val.i = 1;
2035 defconfig[LoadImages].prio = 2;
2038 defconfig[KioskMode].val.i = 0;
2039 defconfig[KioskMode].prio = 2;
2042 defconfig[KioskMode].val.i = 1;
2043 defconfig[KioskMode].prio = 2;
2046 defconfig[Style].val.i = 0;
2047 defconfig[Style].prio = 2;
2050 defconfig[Style].val.i = 1;
2051 defconfig[Style].prio = 2;
2054 defconfig[Inspector].val.i = 0;
2055 defconfig[Inspector].prio = 2;
2058 defconfig[Inspector].val.i = 1;
2059 defconfig[Inspector].prio = 2;
2062 defconfig[Plugins].val.i = 0;
2063 defconfig[Plugins].prio = 2;
2066 defconfig[Plugins].val.i = 1;
2067 defconfig[Plugins].prio = 2;
2070 scriptfile = EARGF(usage());
2073 defconfig[JavaScript].val.i = 0;
2074 defconfig[JavaScript].prio = 2;
2077 defconfig[JavaScript].val.i = 1;
2078 defconfig[JavaScript].prio = 2;
2081 defconfig[StrictTLS].val.i = 0;
2082 defconfig[StrictTLS].prio = 2;
2085 defconfig[StrictTLS].val.i = 1;
2086 defconfig[StrictTLS].prio = 2;
2089 fulluseragent = EARGF(usage());
2092 die("surf-"VERSION", see LICENSE for © details\n");
2097 defconfig[Certificate].val.i = 0;
2098 defconfig[Certificate].prio = 2;
2101 defconfig[Certificate].val.i = 1;
2102 defconfig[Certificate].prio = 2;
2105 defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
2106 defconfig[ZoomLevel].prio = 2;
2117 arg.v = "about:blank";
2121 c = newclient(NULL);