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);
184 static void updatehistory(const char *u, const char *t);
187 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
188 static void initwebextensions(WebKitWebContext *wc, Client *c);
189 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
191 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
192 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
194 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
195 static gboolean readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused);
196 static void showview(WebKitWebView *v, Client *c);
197 static GtkWidget *createwindow(Client *c);
198 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
199 GTlsCertificate *cert,
200 GTlsCertificateFlags err, Client *c);
201 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
202 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
203 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
204 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
205 guint modifiers, Client *c);
206 static gboolean permissionrequested(WebKitWebView *v,
207 WebKitPermissionRequest *r, Client *c);
208 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
209 WebKitPolicyDecisionType dt, Client *c);
210 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
211 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
212 static void decideresource(WebKitPolicyDecision *d, Client *c);
213 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
215 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
217 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
218 static void download(Client *c, WebKitURIResponse *r);
219 static void webprocessterminated(WebKitWebView *v,
220 WebKitWebProcessTerminationReason r,
222 static void closeview(WebKitWebView *v, Client *c);
223 static void destroywin(GtkWidget* w, Client *c);
224 static gchar *parseuri(const gchar *uri);
227 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
228 static void reload(Client *c, const Arg *a);
229 static void print(Client *c, const Arg *a);
230 static void showcert(Client *c, const Arg *a);
231 static void clipboard(Client *c, const Arg *a);
232 static void zoom(Client *c, const Arg *a);
233 static void scrollv(Client *c, const Arg *a);
234 static void scrollh(Client *c, const Arg *a);
235 static void navigate(Client *c, const Arg *a);
236 static void stop(Client *c, const Arg *a);
237 static void toggle(Client *c, const Arg *a);
238 static void togglefullscreen(Client *c, const Arg *a);
239 static void togglecookiepolicy(Client *c, const Arg *a);
240 static void toggleinspector(Client *c, const Arg *a);
241 static void find(Client *c, const Arg *a);
242 static void playexternal(Client *c, const Arg *a);
245 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
246 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
247 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
249 static char winid[64];
250 static char togglestats[12];
251 static char pagestats[2];
252 static Atom atoms[AtomLast];
255 static int cookiepolicy;
257 static Client *clients;
258 static GdkDevice *gdkkb;
259 static char *stylefile;
260 static const char *useragent;
261 static Parameter *curconfig;
262 static int modparams[ParameterLast];
263 static int pipein[2], pipeout[2];
266 static ParamName loadtransient[] = {
280 static ParamName loadcommitted[] = {
306 static ParamName loadfinished[] = {
310 /* configuration, allows nested code to access above variables */
316 die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
317 "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
318 "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
328 /* clean up any zombies immediately */
330 if (signal(SIGHUP, sighup) == SIG_ERR)
331 die("Can't install SIGHUP handler");
333 if (!(dpy = XOpenDisplay(NULL)))
334 die("Can't open default display");
337 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
338 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
339 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
341 gtk_init(NULL, NULL);
343 gdpy = gdk_display_get_default();
345 curconfig = defconfig;
348 cookiefile = buildfile(cookiefile);
349 historyfile = buildfile(historyfile);
350 scriptfile = buildfile(scriptfile);
351 cachedir = buildpath(cachedir);
352 certdir = buildpath(certdir);
354 gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
356 if (pipe(pipeout) < 0 || pipe(pipein) < 0) {
357 fputs("Unable to create pipes\n", stderr);
359 gchanin = g_io_channel_unix_new(pipein[0]);
360 g_io_channel_set_encoding(gchanin, NULL, NULL);
361 g_io_channel_set_close_on_unref(gchanin, TRUE);
362 g_io_add_watch(gchanin, G_IO_IN, readpipe, NULL);
366 for (i = 0; i < LENGTH(certs); ++i) {
367 if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
368 certs[i].file = g_strconcat(certdir, "/", certs[i].file,
371 fprintf(stderr, "Could not compile regex: %s\n",
373 certs[i].regex = NULL;
378 styledir = buildpath(styledir);
379 for (i = 0; i < LENGTH(styles); ++i) {
380 if (!regcomp(&(styles[i].re), styles[i].regex,
382 styles[i].file = g_strconcat(styledir, "/",
383 styles[i].file, NULL);
385 fprintf(stderr, "Could not compile regex: %s\n",
387 styles[i].regex = NULL;
392 stylefile = buildfile(stylefile);
395 for (i = 0; i < LENGTH(uriparams); ++i) {
396 if (regcomp(&(uriparams[i].re), uriparams[i].uri,
398 fprintf(stderr, "Could not compile regex: %s\n",
400 uriparams[i].uri = NULL;
404 /* copy default parameters with higher priority */
405 for (j = 0; j < ParameterLast; ++j) {
406 if (defconfig[j].prio >= uriparams[i].config[j].prio)
407 uriparams[i].config[j] = defconfig[j];
415 if (signal(SIGCHLD, sigchld) == SIG_ERR)
416 die("Can't install SIGCHLD handler");
417 while (waitpid(-1, NULL, WNOHANG) > 0)
427 for (c = clients; c; c = c->next)
432 buildfile(const char *path)
434 char *dname, *bname, *bpath, *fpath;
437 dname = g_path_get_dirname(path);
438 bname = g_path_get_basename(path);
440 bpath = buildpath(dname);
443 fpath = g_build_filename(bpath, bname, NULL);
447 if (!(f = fopen(fpath, "a")))
448 die("Could not open file: %s\n", fpath);
450 g_chmod(fpath, 0600); /* always */
457 getuserhomedir(const char *user)
459 struct passwd *pw = getpwnam(user);
462 die("Can't get user %s login information.\n", user);
468 getcurrentuserhomedir(void)
474 homedir = getenv("HOME");
478 user = getenv("USER");
480 return getuserhomedir(user);
482 pw = getpwuid(getuid());
484 die("Can't get current user home directory\n");
490 buildpath(const char *path)
495 apath = untildepath(path);
497 apath = g_strdup(path);
499 /* creating directory */
500 if (g_mkdir_with_parents(apath, 0700) < 0)
501 die("Could not access directory: %s\n", apath);
503 fpath = realpath(apath, NULL);
510 untildepath(const char *path)
512 char *apath, *name, *p;
515 if (path[1] == '/' || path[1] == '\0') {
516 p = (char *)&path[1];
517 homedir = getcurrentuserhomedir();
519 if ((p = strchr(path, '/')))
520 name = g_strndup(&path[1], p - (path + 1));
522 name = g_strdup(&path[1]);
524 homedir = getuserhomedir(name);
527 apath = g_build_filename(homedir, p, NULL);
532 newclient(Client *rc)
536 if (!(c = calloc(1, sizeof(Client))))
537 die("Cannot malloc!\n");
543 c->view = newview(c, rc ? rc->view : NULL);
549 loaduri(Client *c, const Arg *a)
552 char *url, *path, *apath;
553 const char *uri = a->v;
555 if (g_strcmp0(uri, "") == 0)
558 if (g_str_has_prefix(uri, "http://") ||
559 g_str_has_prefix(uri, "https://") ||
560 g_str_has_prefix(uri, "file://") ||
561 g_str_has_prefix(uri, "about:")) {
565 apath = untildepath(uri);
568 if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
569 url = g_strdup_printf("file://%s", path);
578 setatom(c, AtomUri, url);
580 if (strcmp(url, geturi(c)) == 0) {
583 webkit_web_view_load_uri(c->view, url);
595 if (!(uri = webkit_web_view_get_uri(c->view)))
601 setatom(Client *c, int a, const char *v)
603 XChangeProperty(dpy, c->xid,
604 atoms[a], XA_STRING, 8, PropModeReplace,
605 (unsigned char *)v, strlen(v) + 1);
610 getatom(Client *c, int a)
612 static char buf[BUFSIZ];
615 unsigned long ldummy;
616 unsigned char *p = NULL;
619 XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
620 &adummy, &idummy, &ldummy, &ldummy, &p);
622 strncpy(buf, (char *)p, LENGTH(buf) - 1);
631 updatetitle(Client *c)
634 const char *name = c->overtitle ? c->overtitle :
635 c->title ? c->title : "";
637 if (curconfig[ShowIndicators].val.i) {
641 if (c->progress != 100)
642 title = g_strdup_printf("[%i%%] %s:%s | %s",
643 c->progress, togglestats, pagestats, name);
645 title = g_strdup_printf("%s:%s | %s",
646 togglestats, pagestats, name);
648 gtk_window_set_title(GTK_WINDOW(c->win), title);
651 gtk_window_set_title(GTK_WINDOW(c->win), name);
656 gettogglestats(Client *c)
658 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
659 togglestats[1] = curconfig[CaretBrowsing].val.i ? 'C' : 'c';
660 togglestats[2] = curconfig[Geolocation].val.i ? 'G' : 'g';
661 togglestats[3] = curconfig[DiskCache].val.i ? 'D' : 'd';
662 togglestats[4] = curconfig[LoadImages].val.i ? 'I' : 'i';
663 togglestats[5] = curconfig[JavaScript].val.i ? 'S' : 's';
664 togglestats[6] = curconfig[Plugins].val.i ? 'V' : 'v';
665 togglestats[7] = curconfig[Style].val.i ? 'M' : 'm';
666 togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
667 togglestats[9] = curconfig[Certificate].val.i ? 'X' : 'x';
668 togglestats[10] = curconfig[StrictTLS].val.i ? 'T' : 't';
669 togglestats[11] = '\0';
673 getpagestats(Client *c)
676 pagestats[0] = (c->tlserr || c->insecure) ? 'U' : 'T';
682 WebKitCookieAcceptPolicy
683 cookiepolicy_get(void)
685 switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
687 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
689 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
690 default: /* fallthrough */
692 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
697 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
700 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
702 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
704 default: /* fallthrough */
705 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
711 seturiparameters(Client *c, const char *uri, ParamName *params)
713 Parameter *config, *uriconfig = NULL;
716 for (i = 0; i < LENGTH(uriparams); ++i) {
717 if (uriparams[i].uri &&
718 !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
719 uriconfig = uriparams[i].config;
724 curconfig = uriconfig ? uriconfig : defconfig;
726 for (i = 0; (p = params[i]) != ParameterLast; ++i) {
728 default: /* FALLTHROUGH */
729 if (!(defconfig[p].prio < curconfig[p].prio ||
730 defconfig[p].prio < modparams[p]))
735 setparameter(c, 0, p, &curconfig[p].val);
741 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
743 GdkRGBA bgcolor = { 0 };
744 WebKitSettings *s = webkit_web_view_get_settings(c->view);
746 modparams[p] = curconfig[p].prio;
749 case AcceleratedCanvas:
750 webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
752 case AccessMicrophone:
753 return; /* do nothing */
755 return; /* do nothing */
757 webkit_settings_set_enable_caret_browsing(s, a->i);
762 setcert(c, geturi(c));
763 return; /* do not update */
765 webkit_cookie_manager_set_accept_policy(
766 webkit_web_context_get_cookie_manager(
767 webkit_web_view_get_context(c->view)),
772 webkit_web_context_set_cache_model(
773 webkit_web_view_get_context(c->view), a->i ?
774 WEBKIT_CACHE_MODEL_WEB_BROWSER :
775 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
776 return; /* do not update */
778 webkit_settings_set_default_charset(s, a->v);
779 return; /* do not update */
781 webkit_settings_set_enable_dns_prefetching(s, a->i);
782 return; /* do not update */
783 case FileURLsCrossAccess:
784 webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
785 webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
786 return; /* do not update */
788 webkit_settings_set_default_font_size(s, a->i);
789 return; /* do not update */
790 case FrameFlattening:
791 webkit_settings_set_enable_frame_flattening(s, a->i);
798 webkit_web_view_set_background_color(c->view, &bgcolor);
799 return; /* do not update */
801 webkit_settings_set_enable_developer_extras(s, a->i);
802 return; /* do not update */
804 webkit_settings_set_enable_java(s, a->i);
805 return; /* do not update */
807 webkit_settings_set_enable_javascript(s, a->i);
810 return; /* do nothing */
812 webkit_settings_set_auto_load_images(s, a->i);
814 case MediaManualPlay:
815 webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
818 webkit_settings_set_enable_plugins(s, a->i);
820 case PreferredLanguages:
821 return; /* do nothing */
822 case RunInFullscreen:
823 return; /* do nothing */
825 /* Disabled until we write some WebKitWebExtension for
826 * manipulating the DOM directly.
827 enablescrollbars = !enablescrollbars;
828 evalscript(c, "document.documentElement.style.overflow = '%s'",
829 enablescrollbars ? "auto" : "hidden");
831 return; /* do not update */
834 case SmoothScrolling:
835 webkit_settings_set_enable_smooth_scrolling(s, a->i);
836 return; /* do not update */
838 webkit_settings_set_enable_site_specific_quirks(s, a->i);
841 webkit_web_context_set_spell_checking_enabled(
842 webkit_web_view_get_context(c->view), a->i);
843 return; /* do not update */
845 return; /* do nothing */
847 webkit_web_context_set_tls_errors_policy(
848 webkit_web_view_get_context(c->view), a->i ?
849 WEBKIT_TLS_ERRORS_POLICY_FAIL :
850 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
853 webkit_user_content_manager_remove_all_style_sheets(
854 webkit_web_view_get_user_content_manager(c->view));
856 setstyle(c, getstyle(geturi(c)));
860 webkit_settings_set_enable_webgl(s, a->i);
863 webkit_web_view_set_zoom_level(c->view, a->f);
864 return; /* do not update */
866 return; /* do nothing */
875 getcert(const char *uri)
879 for (i = 0; i < LENGTH(certs); ++i) {
880 if (certs[i].regex &&
881 !regexec(&(certs[i].re), uri, 0, NULL, 0))
882 return certs[i].file;
889 setcert(Client *c, const char *uri)
891 const char *file = getcert(uri);
893 GTlsCertificate *cert;
898 if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
899 fprintf(stderr, "Could not read certificate file: %s\n", file);
903 if ((uri = strstr(uri, "https://"))) {
904 uri += sizeof("https://") - 1;
905 host = g_strndup(uri, strchr(uri, '/') - uri);
906 webkit_web_context_allow_tls_certificate_for_host(
907 webkit_web_view_get_context(c->view), cert, host);
911 g_object_unref(cert);
916 getstyle(const char *uri)
923 for (i = 0; i < LENGTH(styles); ++i) {
924 if (styles[i].regex &&
925 !regexec(&(styles[i].re), uri, 0, NULL, 0))
926 return styles[i].file;
933 setstyle(Client *c, const char *file)
937 if (!g_file_get_contents(file, &style, NULL, NULL)) {
938 fprintf(stderr, "Could not read style file: %s\n", file);
942 webkit_user_content_manager_add_style_sheet(
943 webkit_web_view_get_user_content_manager(c->view),
944 webkit_user_style_sheet_new(style,
945 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
946 WEBKIT_USER_STYLE_LEVEL_USER,
958 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
959 evalscript(c, "%s", script);
964 evalscript(Client *c, const char *jsstr, ...)
970 script = g_strdup_vprintf(jsstr, ap);
973 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
978 updatewinid(Client *c)
980 snprintf(winid, LENGTH(winid), "%lu", c->xid);
984 handleplumb(Client *c, const char *uri)
986 Arg a = (Arg)PLUMB(uri);
991 newwindow(Client *c, const Arg *a, int noembed)
995 const char *cmd[29], *uri;
996 const Arg arg = { .v = cmd };
1000 cmd[i++] = curconfig[CookiePolicies].val.v;
1001 cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
1002 if (cookiefile && g_strcmp0(cookiefile, "")) {
1004 cmd[i++] = cookiefile;
1006 if (stylefile && g_strcmp0(stylefile, "")) {
1008 cmd[i++] = stylefile;
1010 cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
1011 if (embed && !noembed) {
1013 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1016 cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
1017 cmd[i++] = curconfig[Geolocation].val.i ? "-G" : "-g" ;
1018 cmd[i++] = curconfig[LoadImages].val.i ? "-I" : "-i" ;
1019 cmd[i++] = curconfig[KioskMode].val.i ? "-K" : "-k" ;
1020 cmd[i++] = curconfig[Style].val.i ? "-M" : "-m" ;
1021 cmd[i++] = curconfig[Inspector].val.i ? "-N" : "-n" ;
1022 cmd[i++] = curconfig[Plugins].val.i ? "-P" : "-p" ;
1023 if (scriptfile && g_strcmp0(scriptfile, "")) {
1025 cmd[i++] = scriptfile;
1027 cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
1028 cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
1029 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1031 cmd[i++] = fulluseragent;
1035 cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
1036 /* do not keep zoom level */
1046 spawn(Client *c, const Arg *a)
1050 close(ConnectionNumber(dpy));
1054 execvp(((char **)a->v)[0], (char **)a->v);
1055 fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
1062 destroyclient(Client *c)
1066 webkit_web_view_stop_loading(c->view);
1067 /* Not needed, has already been called
1068 gtk_widget_destroy(c->win);
1071 for (p = clients; p && p->next != c; p = p->next)
1084 destroyclient(clients);
1089 g_free(historyfile);
1097 updatehistory(const char *u, const char *t)
1100 f = fopen(historyfile, "a+");
1103 time_t now = time (0);
1104 strftime (b, 20, "%Y-%m-%d %H:%M:%S", localtime (&now));
1107 fprintf(f, " %s %s\n", u, t);
1112 newview(Client *c, WebKitWebView *rv)
1115 WebKitSettings *settings;
1116 WebKitWebContext *context;
1117 WebKitCookieManager *cookiemanager;
1118 WebKitUserContentManager *contentmanager;
1122 v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
1124 settings = webkit_settings_new_with_settings(
1125 "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1126 "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
1127 "auto-load-images", curconfig[LoadImages].val.i,
1128 "default-charset", curconfig[DefaultCharset].val.v,
1129 "default-font-size", curconfig[FontSize].val.i,
1130 "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
1131 "enable-developer-extras", curconfig[Inspector].val.i,
1132 "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
1133 "enable-frame-flattening", curconfig[FrameFlattening].val.i,
1134 "enable-html5-database", curconfig[DiskCache].val.i,
1135 "enable-html5-local-storage", curconfig[DiskCache].val.i,
1136 "enable-java", curconfig[Java].val.i,
1137 "enable-javascript", curconfig[JavaScript].val.i,
1138 "enable-plugins", curconfig[Plugins].val.i,
1139 "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
1140 "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
1141 "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
1142 "enable-webgl", curconfig[WebGL].val.i,
1143 "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
1145 /* For more interesting settings, have a look at
1146 * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
1148 if (strcmp(fulluseragent, "")) {
1149 webkit_settings_set_user_agent(settings, fulluseragent);
1150 } else if (surfuseragent) {
1151 webkit_settings_set_user_agent_with_application_details(
1152 settings, "Surf", VERSION);
1154 useragent = webkit_settings_get_user_agent(settings);
1156 contentmanager = webkit_user_content_manager_new();
1158 context = webkit_web_context_new_with_website_data_manager(
1159 webkit_website_data_manager_new(
1160 "base-cache-directory", cachedir,
1161 "base-data-directory", cachedir,
1164 cookiemanager = webkit_web_context_get_cookie_manager(context);
1166 /* rendering process model, can be a shared unique one
1167 * or one for each view */
1168 webkit_web_context_set_process_model(context,
1169 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1171 webkit_web_context_set_tls_errors_policy(context,
1172 curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
1173 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1175 webkit_web_context_set_cache_model(context,
1176 curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
1177 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1179 /* Currently only works with text file to be compatible with curl */
1180 webkit_cookie_manager_set_persistent_storage(cookiemanager,
1181 cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1183 webkit_cookie_manager_set_accept_policy(cookiemanager,
1184 cookiepolicy_get());
1186 webkit_web_context_set_preferred_languages(context,
1187 curconfig[PreferredLanguages].val.v);
1188 webkit_web_context_set_spell_checking_languages(context,
1189 curconfig[SpellLanguages].val.v);
1190 webkit_web_context_set_spell_checking_enabled(context,
1191 curconfig[SpellChecking].val.i);
1193 g_signal_connect(G_OBJECT(context), "download-started",
1194 G_CALLBACK(downloadstarted), c);
1195 g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
1196 G_CALLBACK(initwebextensions), c);
1198 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
1199 "settings", settings,
1200 "user-content-manager", contentmanager,
1201 "web-context", context,
1205 g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
1206 G_CALLBACK(progresschanged), c);
1207 g_signal_connect(G_OBJECT(v), "notify::title",
1208 G_CALLBACK(titlechanged), c);
1209 g_signal_connect(G_OBJECT(v), "button-release-event",
1210 G_CALLBACK(buttonreleased), c);
1211 g_signal_connect(G_OBJECT(v), "close",
1212 G_CALLBACK(closeview), c);
1213 g_signal_connect(G_OBJECT(v), "create",
1214 G_CALLBACK(createview), c);
1215 g_signal_connect(G_OBJECT(v), "decide-policy",
1216 G_CALLBACK(decidepolicy), c);
1217 g_signal_connect(G_OBJECT(v), "insecure-content-detected",
1218 G_CALLBACK(insecurecontent), c);
1219 g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
1220 G_CALLBACK(loadfailedtls), c);
1221 g_signal_connect(G_OBJECT(v), "load-changed",
1222 G_CALLBACK(loadchanged), c);
1223 g_signal_connect(G_OBJECT(v), "mouse-target-changed",
1224 G_CALLBACK(mousetargetchanged), c);
1225 g_signal_connect(G_OBJECT(v), "permission-request",
1226 G_CALLBACK(permissionrequested), c);
1227 g_signal_connect(G_OBJECT(v), "ready-to-show",
1228 G_CALLBACK(showview), c);
1229 g_signal_connect(G_OBJECT(v), "web-process-terminated",
1230 G_CALLBACK(webprocessterminated), c);
1236 readpipe(GIOChannel *s, GIOCondition ioc, gpointer unused)
1238 static char msg[MSGBUFSZ], msgsz;
1239 GError *gerr = NULL;
1241 if (g_io_channel_read_chars(s, msg, sizeof(msg), NULL, &gerr) !=
1242 G_IO_STATUS_NORMAL) {
1243 fprintf(stderr, "surf: error reading pipe: %s\n",
1248 if ((msgsz = msg[0]) < 3) {
1249 fprintf(stderr, "surf: message too short: %d\n", msgsz);
1264 initwebextensions(WebKitWebContext *wc, Client *c)
1268 if (!pipeout[0] || !pipein[1])
1271 gv = g_variant_new("(ii)", pipeout[0], pipein[1]);
1273 webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
1274 webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
1278 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
1282 switch (webkit_navigation_action_get_navigation_type(a)) {
1283 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1285 * popup windows of type “other” are almost always triggered
1286 * by user gesture, so inverse the logic here
1288 /* instead of this, compare destination uri to mouse-over uri for validating window */
1289 if (webkit_navigation_action_is_user_gesture(a))
1291 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1292 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1293 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1294 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1295 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1302 return GTK_WIDGET(n->view);
1306 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
1308 WebKitHitTestResultContext element;
1311 element = webkit_hit_test_result_get_context(c->mousepos);
1313 for (i = 0; i < LENGTH(buttons); ++i) {
1314 if (element & buttons[i].target &&
1315 e->button.button == buttons[i].button &&
1316 CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
1318 buttons[i].func(c, &buttons[i].arg, c->mousepos);
1319 return buttons[i].stopevent;
1327 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1329 Client *c = (Client *)d;
1333 if (((XEvent *)e)->type == PropertyNotify) {
1334 ev = &((XEvent *)e)->xproperty;
1335 if (ev->state == PropertyNewValue) {
1336 if (ev->atom == atoms[AtomFind]) {
1339 return GDK_FILTER_REMOVE;
1340 } else if (ev->atom == atoms[AtomGo]) {
1341 a.v = getatom(c, AtomGo);
1344 return GDK_FILTER_REMOVE;
1348 return GDK_FILTER_CONTINUE;
1352 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1357 case GDK_ENTER_NOTIFY:
1358 c->overtitle = c->targeturi;
1362 if (!curconfig[KioskMode].val.i) {
1363 for (i = 0; i < LENGTH(keys); ++i) {
1364 if (gdk_keyval_to_lower(e->key.keyval) ==
1366 CLEANMASK(e->key.state) == keys[i].mod &&
1369 keys[i].func(c, &(keys[i].arg));
1374 case GDK_LEAVE_NOTIFY:
1375 c->overtitle = NULL;
1378 case GDK_WINDOW_STATE:
1379 if (e->window_state.changed_mask ==
1380 GDK_WINDOW_STATE_FULLSCREEN)
1381 c->fullscreen = e->window_state.new_window_state &
1382 GDK_WINDOW_STATE_FULLSCREEN;
1392 showview(WebKitWebView *v, Client *c)
1394 GdkRGBA bgcolor = { 0 };
1397 c->finder = webkit_web_view_get_find_controller(c->view);
1398 c->inspector = webkit_web_view_get_inspector(c->view);
1400 c->pageid = webkit_web_view_get_page_id(c->view);
1401 c->win = createwindow(c);
1403 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1404 gtk_widget_show_all(c->win);
1405 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1407 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1408 c->xid = gdk_x11_window_get_xid(gwin);
1411 gdk_display_sync(gtk_widget_get_display(c->win));
1416 if (curconfig[HideBackground].val.i)
1417 webkit_web_view_set_background_color(c->view, &bgcolor);
1419 if (!curconfig[KioskMode].val.i) {
1420 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1421 gdk_window_add_filter(gwin, processx, c);
1424 if (curconfig[RunInFullscreen].val.i)
1425 togglefullscreen(c, NULL);
1427 if (curconfig[ZoomLevel].val.f != 1.0)
1428 webkit_web_view_set_zoom_level(c->view,
1429 curconfig[ZoomLevel].val.f);
1431 setatom(c, AtomFind, "");
1432 setatom(c, AtomUri, "about:blank");
1436 createwindow(Client *c)
1442 w = gtk_plug_new(embed);
1444 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1446 wmstr = g_path_get_basename(argv0);
1447 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
1450 wmstr = g_strdup_printf("%s[%lu]", "Surf", c->pageid);
1451 gtk_window_set_role(GTK_WINDOW(w), wmstr);
1454 gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
1457 g_signal_connect(G_OBJECT(w), "destroy",
1458 G_CALLBACK(destroywin), c);
1459 g_signal_connect(G_OBJECT(w), "enter-notify-event",
1460 G_CALLBACK(winevent), c);
1461 g_signal_connect(G_OBJECT(w), "key-press-event",
1462 G_CALLBACK(winevent), c);
1463 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1464 G_CALLBACK(winevent), c);
1465 g_signal_connect(G_OBJECT(w), "window-state-event",
1466 G_CALLBACK(winevent), c);
1472 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
1473 GTlsCertificateFlags err, Client *c)
1475 GString *errmsg = g_string_new(NULL);
1478 c->failedcert = g_object_ref(cert);
1482 if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
1483 g_string_append(errmsg,
1484 "The signing certificate authority is not known.<br>");
1485 if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
1486 g_string_append(errmsg,
1487 "The certificate does not match the expected identity "
1488 "of the site that it was retrieved from.<br>");
1489 if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
1490 g_string_append(errmsg,
1491 "The certificate's activation time "
1492 "is still in the future.<br>");
1493 if (err & G_TLS_CERTIFICATE_EXPIRED)
1494 g_string_append(errmsg, "The certificate has expired.<br>");
1495 if (err & G_TLS_CERTIFICATE_REVOKED)
1496 g_string_append(errmsg,
1497 "The certificate has been revoked according to "
1498 "the GTlsConnection's certificate revocation list.<br>");
1499 if (err & G_TLS_CERTIFICATE_INSECURE)
1500 g_string_append(errmsg,
1501 "The certificate's algorithm is considered insecure.<br>");
1502 if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
1503 g_string_append(errmsg,
1504 "Some error occurred validating the certificate.<br>");
1506 g_object_get(cert, "certificate-pem", &pem, NULL);
1507 html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
1508 "<p>You can inspect the following certificate "
1509 "with Ctrl-t (default keybinding).</p>"
1510 "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
1512 g_string_free(errmsg, TRUE);
1514 webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
1522 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
1524 const char *uri = geturi(c);
1527 case WEBKIT_LOAD_STARTED:
1528 setatom(c, AtomUri, uri);
1530 c->https = c->insecure = 0;
1531 seturiparameters(c, uri, loadtransient);
1535 g_clear_object(&c->failedcert);
1537 case WEBKIT_LOAD_REDIRECTED:
1538 setatom(c, AtomUri, uri);
1540 seturiparameters(c, uri, loadtransient);
1542 case WEBKIT_LOAD_COMMITTED:
1543 seturiparameters(c, uri, loadcommitted);
1544 c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
1547 case WEBKIT_LOAD_FINISHED:
1548 seturiparameters(c, uri, loadfinished);
1549 updatehistory(uri, c->title);
1550 /* Disabled until we write some WebKitWebExtension for
1551 * manipulating the DOM directly.
1552 evalscript(c, "document.documentElement.style.overflow = '%s'",
1553 enablescrollbars ? "auto" : "hidden");
1562 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1564 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1570 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1572 c->title = webkit_web_view_get_title(c->view);
1577 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1580 WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1582 /* Keep the hit test to know where is the pointer on the next click */
1586 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1587 else if (hc & OnImg)
1588 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1589 else if (hc & OnMedia)
1590 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1592 c->targeturi = NULL;
1594 c->overtitle = c->targeturi;
1599 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1601 ParamName param = ParameterLast;
1603 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1604 param = Geolocation;
1605 } else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
1606 if (webkit_user_media_permission_is_for_audio_device(
1607 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1608 param = AccessMicrophone;
1609 else if (webkit_user_media_permission_is_for_video_device(
1610 WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
1611 param = AccessWebcam;
1616 if (curconfig[param].val.i)
1617 webkit_permission_request_allow(r);
1619 webkit_permission_request_deny(r);
1625 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1626 WebKitPolicyDecisionType dt, Client *c)
1629 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1630 decidenavigation(d, c);
1632 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1633 decidenewwindow(d, c);
1635 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1636 decideresource(d, c);
1639 webkit_policy_decision_ignore(d);
1646 decidenavigation(WebKitPolicyDecision *d, Client *c)
1648 WebKitNavigationAction *a =
1649 webkit_navigation_policy_decision_get_navigation_action(
1650 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1652 switch (webkit_navigation_action_get_navigation_type(a)) {
1653 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1654 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1655 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1656 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1657 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1658 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1660 /* Do not navigate to links with a "_blank" target (popup) */
1661 if (webkit_navigation_policy_decision_get_frame_name(
1662 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1663 webkit_policy_decision_ignore(d);
1665 /* Filter out navigation to different domain ? */
1666 /* get action→urirequest, copy and load in new window+view
1667 * on Ctrl+Click ? */
1668 webkit_policy_decision_use(d);
1675 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1678 WebKitNavigationAction *a =
1679 webkit_navigation_policy_decision_get_navigation_action(
1680 WEBKIT_NAVIGATION_POLICY_DECISION(d));
1683 switch (webkit_navigation_action_get_navigation_type(a)) {
1684 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1685 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1686 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1687 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1688 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1689 /* Filter domains here */
1690 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1691 * test for link clicked but no button ? */
1692 arg.v = webkit_uri_request_get_uri(
1693 webkit_navigation_action_get_request(a));
1694 newwindow(c, &arg, 0);
1696 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1701 webkit_policy_decision_ignore(d);
1705 decideresource(WebKitPolicyDecision *d, Client *c)
1708 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1709 WebKitURIResponse *res =
1710 webkit_response_policy_decision_get_response(r);
1711 const gchar *uri = webkit_uri_response_get_uri(res);
1713 if (g_str_has_suffix(uri, "/favicon.ico")) {
1714 webkit_policy_decision_ignore(d);
1718 if (!g_str_has_prefix(uri, "http://")
1719 && !g_str_has_prefix(uri, "https://")
1720 && !g_str_has_prefix(uri, "about:")
1721 && !g_str_has_prefix(uri, "file://")
1722 && !g_str_has_prefix(uri, "data:")
1723 && !g_str_has_prefix(uri, "blob:")
1724 && strlen(uri) > 0) {
1725 for (i = 0; i < strlen(uri); i++) {
1726 if (!g_ascii_isprint(uri[i])) {
1732 handleplumb(c, uri);
1733 webkit_policy_decision_ignore(d);
1738 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1739 webkit_policy_decision_use(d);
1741 webkit_policy_decision_ignore(d);
1747 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
1753 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1755 g_signal_connect(G_OBJECT(d), "notify::response",
1756 G_CALLBACK(responsereceived), c);
1760 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1762 download(c, webkit_download_get_response(d));
1763 webkit_download_cancel(d);
1767 download(Client *c, WebKitURIResponse *r)
1769 Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1774 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
1777 fprintf(stderr, "web process terminated: %s\n",
1778 r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
1783 closeview(WebKitWebView *v, Client *c)
1785 gtk_widget_destroy(c->win);
1789 destroywin(GtkWidget* w, Client *c)
1797 parseuri(const gchar *uri) {
1800 for (i = 0; i < LENGTH(searchengines); i++) {
1801 if (searchengines[i].token == NULL || searchengines[i].uri == NULL ||
1802 *(uri + strlen(searchengines[i].token)) != ' ')
1804 if (g_str_has_prefix(uri, searchengines[i].token))
1805 return g_strdup_printf(searchengines[i].uri,
1806 uri + strlen(searchengines[i].token) + 1);
1809 return g_strdup_printf("http://%s", uri);
1813 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1815 Arg a = {.v = text };
1817 loaduri((Client *) d, &a);
1821 reload(Client *c, const Arg *a)
1824 webkit_web_view_reload_bypass_cache(c->view);
1826 webkit_web_view_reload(c->view);
1830 print(Client *c, const Arg *a)
1832 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1833 GTK_WINDOW(c->win));
1837 showcert(Client *c, const Arg *a)
1839 GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
1840 GcrCertificate *gcrt;
1843 GcrCertificateWidget *wcert;
1848 g_object_get(cert, "certificate", &crt, NULL);
1849 gcrt = gcr_simple_certificate_new(crt->data, crt->len);
1850 g_byte_array_unref(crt);
1852 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1853 wcert = gcr_certificate_widget_new(gcrt);
1854 g_object_unref(gcrt);
1856 gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
1857 gtk_widget_show_all(win);
1861 clipboard(Client *c, const Arg *a)
1863 if (a->i) { /* load clipboard uri */
1864 gtk_clipboard_request_text(gtk_clipboard_get(
1865 GDK_SELECTION_PRIMARY),
1867 } else { /* copy uri */
1868 gtk_clipboard_set_text(gtk_clipboard_get(
1869 GDK_SELECTION_PRIMARY), c->targeturi
1870 ? c->targeturi : geturi(c), -1);
1875 zoom(Client *c, const Arg *a)
1878 webkit_web_view_set_zoom_level(c->view,
1879 curconfig[ZoomLevel].val.f + 0.1);
1881 webkit_web_view_set_zoom_level(c->view,
1882 curconfig[ZoomLevel].val.f - 0.1);
1884 webkit_web_view_set_zoom_level(c->view, 1.0);
1886 curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
1890 msgext(Client *c, char type, const Arg *a)
1892 static char msg[MSGBUFSZ];
1895 if ((ret = snprintf(msg, sizeof(msg), "%c%c%c%c",
1896 4, c->pageid, type, a->i))
1898 fprintf(stderr, "surf: message too long: %d\n", ret);
1902 if (pipeout[1] && write(pipeout[1], msg, sizeof(msg)) < 0)
1903 fprintf(stderr, "surf: error sending: %.*s\n", ret-2, msg+2);
1907 scrollv(Client *c, const Arg *a)
1913 scrollh(Client *c, const Arg *a)
1919 navigate(Client *c, const Arg *a)
1922 webkit_web_view_go_back(c->view);
1924 webkit_web_view_go_forward(c->view);
1928 stop(Client *c, const Arg *a)
1930 webkit_web_view_stop_loading(c->view);
1934 toggle(Client *c, const Arg *a)
1936 curconfig[a->i].val.i ^= 1;
1937 setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
1941 togglefullscreen(Client *c, const Arg *a)
1943 /* toggling value is handled in winevent() */
1945 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1947 gtk_window_fullscreen(GTK_WINDOW(c->win));
1951 togglecookiepolicy(Client *c, const Arg *a)
1954 cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
1956 setparameter(c, 0, CookiePolicies, NULL);
1960 toggleinspector(Client *c, const Arg *a)
1962 if (webkit_web_inspector_is_attached(c->inspector))
1963 webkit_web_inspector_close(c->inspector);
1964 else if (curconfig[Inspector].val.i)
1965 webkit_web_inspector_show(c->inspector);
1969 find(Client *c, const Arg *a)
1975 webkit_find_controller_search_next(c->finder);
1977 webkit_find_controller_search_previous(c->finder);
1979 s = getatom(c, AtomFind);
1980 f = webkit_find_controller_get_search_text(c->finder);
1982 if (g_strcmp0(f, s) == 0) /* reset search */
1983 webkit_find_controller_search(c->finder, "", findopts,
1986 webkit_find_controller_search(c->finder, s, findopts,
1989 if (strcmp(s, "") == 0)
1990 webkit_find_controller_search_finish(c->finder);
1995 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
2001 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
2005 arg.v = webkit_hit_test_result_get_link_uri(h);
2006 newwindow(c, &arg, a->i);
2010 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
2014 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
2019 playexternal(Client *c, const Arg *a)
2023 arg = (Arg)VIDEOPLAY(geturi(c));
2028 main(int argc, char *argv[])
2033 memset(&arg, 0, sizeof(arg));
2035 /* command line args */
2038 defconfig[CookiePolicies].val.v = EARGF(usage());
2039 defconfig[CookiePolicies].prio = 2;
2042 defconfig[ScrollBars].val.i = 0;
2043 defconfig[ScrollBars].prio = 2;
2046 defconfig[ScrollBars].val.i = 1;
2047 defconfig[ScrollBars].prio = 2;
2050 cookiefile = EARGF(usage());
2053 stylefile = EARGF(usage());
2056 defconfig[DiskCache].val.i = 0;
2057 defconfig[DiskCache].prio = 2;
2060 defconfig[DiskCache].val.i = 1;
2061 defconfig[DiskCache].prio = 2;
2064 embed = strtol(EARGF(usage()), NULL, 0);
2067 defconfig[RunInFullscreen].val.i = 0;
2068 defconfig[RunInFullscreen].prio = 2;
2071 defconfig[RunInFullscreen].val.i = 1;
2072 defconfig[RunInFullscreen].prio = 2;
2075 defconfig[Geolocation].val.i = 0;
2076 defconfig[Geolocation].prio = 2;
2079 defconfig[Geolocation].val.i = 1;
2080 defconfig[Geolocation].prio = 2;
2083 defconfig[LoadImages].val.i = 0;
2084 defconfig[LoadImages].prio = 2;
2087 defconfig[LoadImages].val.i = 1;
2088 defconfig[LoadImages].prio = 2;
2091 defconfig[KioskMode].val.i = 0;
2092 defconfig[KioskMode].prio = 2;
2095 defconfig[KioskMode].val.i = 1;
2096 defconfig[KioskMode].prio = 2;
2099 defconfig[Style].val.i = 0;
2100 defconfig[Style].prio = 2;
2103 defconfig[Style].val.i = 1;
2104 defconfig[Style].prio = 2;
2107 defconfig[Inspector].val.i = 0;
2108 defconfig[Inspector].prio = 2;
2111 defconfig[Inspector].val.i = 1;
2112 defconfig[Inspector].prio = 2;
2115 defconfig[Plugins].val.i = 0;
2116 defconfig[Plugins].prio = 2;
2119 defconfig[Plugins].val.i = 1;
2120 defconfig[Plugins].prio = 2;
2123 scriptfile = EARGF(usage());
2126 defconfig[JavaScript].val.i = 0;
2127 defconfig[JavaScript].prio = 2;
2130 defconfig[JavaScript].val.i = 1;
2131 defconfig[JavaScript].prio = 2;
2134 defconfig[StrictTLS].val.i = 0;
2135 defconfig[StrictTLS].prio = 2;
2138 defconfig[StrictTLS].val.i = 1;
2139 defconfig[StrictTLS].prio = 2;
2142 fulluseragent = EARGF(usage());
2145 die("surf-"VERSION", see LICENSE for © details\n");
2150 defconfig[Certificate].val.i = 0;
2151 defconfig[Certificate].prio = 2;
2154 defconfig[Certificate].val.i = 1;
2155 defconfig[Certificate].prio = 2;
2158 defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
2159 defconfig[ZoomLevel].prio = 2;
2170 arg.v = "about:blank";
2174 c = newclient(NULL);