surf

A surf web browser [kocotian build]
git clone git://git.kocotian.pl/surf.git
Log | Files | Refs | README | LICENSE

surf.c (57773B)


      1 /* See LICENSE file for copyright and license details.
      2  *
      3  * To understand surf, start reading main().
      4  */
      5 #include <sys/file.h>
      6 #include <sys/socket.h>
      7 #include <sys/types.h>
      8 #include <sys/wait.h>
      9 #include <glib.h>
     10 #include <inttypes.h>
     11 #include <libgen.h>
     12 #include <limits.h>
     13 #include <pwd.h>
     14 #include <regex.h>
     15 #include <signal.h>
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <unistd.h>
     20 
     21 #include <gdk/gdk.h>
     22 #include <gdk/gdkkeysyms.h>
     23 #include <gdk/gdkx.h>
     24 #include <glib/gstdio.h>
     25 #include <gtk/gtk.h>
     26 #include <gtk/gtkx.h>
     27 #include <gcr/gcr.h>
     28 #include <JavaScriptCore/JavaScript.h>
     29 #include <webkit2/webkit2.h>
     30 #include <X11/X.h>
     31 #include <X11/Xatom.h>
     32 #include <glib.h>
     33 
     34 #include "arg.h"
     35 #include "common.h"
     36 
     37 #define LENGTH(x)               (sizeof(x) / sizeof(x[0]))
     38 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
     39 
     40 enum { AtomFind, AtomGo, AtomUri, AtomLast };
     41 
     42 enum {
     43 	OnDoc   = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
     44 	OnLink  = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
     45 	OnImg   = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
     46 	OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
     47 	OnEdit  = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
     48 	OnBar   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
     49 	OnSel   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
     50 	OnAny   = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
     51 };
     52 
     53 typedef enum {
     54 	AcceleratedCanvas,
     55 	AccessMicrophone,
     56 	AccessWebcam,
     57 	CaretBrowsing,
     58 	Certificate,
     59 	CookiePolicies,
     60 	DiskCache,
     61 	DefaultCharset,
     62 	DNSPrefetch,
     63 	Ephemeral,
     64 	FileURLsCrossAccess,
     65 	FontSize,
     66 	FrameFlattening,
     67 	Geolocation,
     68 	HideBackground,
     69 	Inspector,
     70 	Java,
     71 	JavaScript,
     72 	KioskMode,
     73 	LoadImages,
     74 	MediaManualPlay,
     75 	Plugins,
     76 	PreferredLanguages,
     77 	RunInFullscreen,
     78 	ScrollBars,
     79 	ShowIndicators,
     80 	SiteQuirks,
     81 	SmoothScrolling,
     82 	SpellChecking,
     83 	SpellLanguages,
     84 	StrictTLS,
     85 	Style,
     86 	WebGL,
     87 	ZoomLevel,
     88 	ClipboardNotPrimary,
     89 	ParameterLast
     90 } ParamName;
     91 
     92 typedef union {
     93 	int i;
     94 	float f;
     95 	const void *v;
     96 } Arg;
     97 
     98 typedef struct {
     99 	Arg val;
    100 	int prio;
    101 } Parameter;
    102 
    103 typedef struct Client {
    104 	GtkWidget *win;
    105 	WebKitWebView *view;
    106 	WebKitWebInspector *inspector;
    107 	WebKitFindController *finder;
    108 	WebKitHitTestResult *mousepos;
    109 	GTlsCertificate *cert, *failedcert;
    110 	GTlsCertificateFlags tlserr;
    111 	Window xid;
    112 	guint64 pageid;
    113 	int progress, fullscreen, https, insecure, errorpage;
    114 	const char *title, *overtitle, *targeturi;
    115 	const char *needle;
    116 	struct Client *next;
    117 } Client;
    118 
    119 typedef struct {
    120 	guint mod;
    121 	guint keyval;
    122 	void (*func)(Client *c, const Arg *a);
    123 	const Arg arg;
    124 } Key;
    125 
    126 typedef struct {
    127 	unsigned int target;
    128 	unsigned int mask;
    129 	guint button;
    130 	void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
    131 	const Arg arg;
    132 	unsigned int stopevent;
    133 } Button;
    134 
    135 typedef struct {
    136 	char *token;
    137 	char *uri;
    138 } SearchEngine;
    139 
    140 typedef struct {
    141 	const char *uri;
    142 	Parameter config[ParameterLast];
    143 	regex_t re;
    144 } UriParameters;
    145 
    146 typedef struct {
    147 	char *regex;
    148 	char *file;
    149 	regex_t re;
    150 } SiteSpecific;
    151 
    152 /* Surf */
    153 static void die(const char *errstr, ...);
    154 static void usage(void);
    155 static void setup(void);
    156 static void sigchld(int unused);
    157 static void sighup(int unused);
    158 static void sigusr1(int unused);
    159 static char *buildfile(const char *path);
    160 static char *buildpath(const char *path);
    161 static char *untildepath(const char *path);
    162 static const char *getuserhomedir(const char *user);
    163 static const char *getcurrentuserhomedir(void);
    164 static Client *newclient(Client *c);
    165 static void loaduri(Client *c, const Arg *a);
    166 static const char *geturi(Client *c);
    167 static void setatom(Client *c, int a, const char *v);
    168 static const char *getatom(Client *c, int a);
    169 static void updatetitle(Client *c);
    170 static void gettogglestats(Client *c);
    171 static void getpagestats(Client *c);
    172 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
    173 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
    174 static void seturiparameters(Client *c, const char *uri, ParamName *params);
    175 static void setparameter(Client *c, int refresh, ParamName p, const Arg *a);
    176 static const char *getcert(const char *uri);
    177 static void setcert(Client *c, const char *file);
    178 static const char *getstyle(const char *uri);
    179 static void setstyle(Client *c, const char *file);
    180 static void runscript(Client *c);
    181 static void evalscript(Client *c, const char *jsstr, ...);
    182 static void updatewinid(Client *c);
    183 static void handleplumb(Client *c, const char *uri);
    184 static void newwindow(Client *c, const Arg *a, int noembed);
    185 static void spawn(Client *c, const Arg *a);
    186 static void msgext(Client *c, char type, const Arg *a);
    187 static void destroyclient(Client *c);
    188 static void cleanup(void);
    189 static void updatehistory(const char *u, const char *t);
    190 
    191 /* GTK/WebKit */
    192 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
    193 static void initwebextensions(WebKitWebContext *wc, Client *c);
    194 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
    195                              Client *c);
    196 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
    197 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
    198                                 gpointer d);
    199 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
    200 static gboolean readsock(GIOChannel *s, GIOCondition ioc, gpointer unused);
    201 static void showview(WebKitWebView *v, Client *c);
    202 static GtkWidget *createwindow(Client *c);
    203 static gboolean loadfailedtls(WebKitWebView *v, gchar *uri,
    204                               GTlsCertificate *cert,
    205                               GTlsCertificateFlags err, Client *c);
    206 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
    207 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
    208 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
    209 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
    210                                guint modifiers, Client *c);
    211 static gboolean permissionrequested(WebKitWebView *v,
    212                                     WebKitPermissionRequest *r, Client *c);
    213 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
    214                              WebKitPolicyDecisionType dt, Client *c);
    215 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
    216 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
    217 static void decideresource(WebKitPolicyDecision *d, Client *c);
    218 static void insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e,
    219                             Client *c);
    220 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
    221                             Client *c);
    222 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
    223 static void download(Client *c, WebKitURIResponse *r);
    224 static void webprocessterminated(WebKitWebView *v,
    225                                  WebKitWebProcessTerminationReason r,
    226                                  Client *c);
    227 static void closeview(WebKitWebView *v, Client *c);
    228 static void destroywin(GtkWidget* w, Client *c);
    229 static gchar *parseuri(const gchar *uri);
    230 
    231 /* Hotkeys */
    232 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
    233 static void reload(Client *c, const Arg *a);
    234 static void print(Client *c, const Arg *a);
    235 static void showcert(Client *c, const Arg *a);
    236 static void clipboard(Client *c, const Arg *a);
    237 static void zoom(Client *c, const Arg *a);
    238 static void scrollv(Client *c, const Arg *a);
    239 static void scrollh(Client *c, const Arg *a);
    240 static void navigate(Client *c, const Arg *a);
    241 static void stop(Client *c, const Arg *a);
    242 static void toggle(Client *c, const Arg *a);
    243 static void togglefullscreen(Client *c, const Arg *a);
    244 static void togglecookiepolicy(Client *c, const Arg *a);
    245 static void toggleinspector(Client *c, const Arg *a);
    246 static void find(Client *c, const Arg *a);
    247 static void externalpipe(Client *c, const Arg *a);
    248 
    249 /* Buttons */
    250 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
    251 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
    252 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
    253 
    254 static char winid[64];
    255 static char togglestats[12];
    256 static char pagestats[2];
    257 static Atom atoms[AtomLast];
    258 static Window embed;
    259 static int showxid;
    260 static int cookiepolicy;
    261 static Display *dpy;
    262 static Client *clients;
    263 static GdkDevice *gdkkb;
    264 static char *stylefile;
    265 static const char *useragent;
    266 static Parameter *curconfig;
    267 static int modparams[ParameterLast];
    268 static int spair[2];
    269 char *argv0;
    270 
    271 static ParamName loadtransient[] = {
    272 	Certificate,
    273 	CookiePolicies,
    274 	DiskCache,
    275 	DNSPrefetch,
    276 	FileURLsCrossAccess,
    277 	JavaScript,
    278 	LoadImages,
    279 	PreferredLanguages,
    280 	ShowIndicators,
    281 	StrictTLS,
    282 	ParameterLast
    283 };
    284 
    285 static ParamName loadcommitted[] = {
    286 	AcceleratedCanvas,
    287 //	AccessMicrophone,
    288 //	AccessWebcam,
    289 	CaretBrowsing,
    290 	DefaultCharset,
    291 	FontSize,
    292 	FrameFlattening,
    293 	Geolocation,
    294 	HideBackground,
    295 	Inspector,
    296 	Java,
    297 //	KioskMode,
    298 	MediaManualPlay,
    299 	Plugins,
    300 	RunInFullscreen,
    301 	ScrollBars,
    302 	SiteQuirks,
    303 	SmoothScrolling,
    304 	SpellChecking,
    305 	SpellLanguages,
    306 	Style,
    307 	ZoomLevel,
    308 	ClipboardNotPrimary,
    309 	ParameterLast
    310 };
    311 
    312 static ParamName loadfinished[] = {
    313 	ParameterLast
    314 };
    315 
    316 /* configuration, allows nested code to access above variables */
    317 #include "config.h"
    318 
    319 static void
    320 externalpipe_execute(char* buffer, Arg *arg) {
    321 	int to[2];
    322 	void (*oldsigpipe)(int);
    323 
    324 	if (pipe(to) == -1)
    325 		return;
    326 
    327 	switch (fork()) {
    328 	case -1:
    329 		close(to[0]);
    330 		close(to[1]);
    331 		return;
    332 	case 0:
    333 		dup2(to[0], STDIN_FILENO); close(to[0]); close(to[1]);
    334 		execvp(((char **)arg->v)[0], (char **)arg->v);
    335 		fprintf(stderr, "st: execvp %s\n", ((char **)arg->v)[0]);
    336 		perror("failed");
    337 		exit(0);
    338 	}
    339 
    340 	close(to[0]);
    341 	oldsigpipe = signal(SIGPIPE, SIG_IGN);
    342 	write(to[1], buffer, strlen(buffer));
    343 	close(to[1]);
    344 	signal(SIGPIPE, oldsigpipe);
    345 }
    346 
    347 static void
    348 externalpipe_resource_done(WebKitWebResource *r, GAsyncResult *s, Arg *arg)
    349 {
    350 	GError *gerr = NULL;
    351 	guchar *buffer = webkit_web_resource_get_data_finish(r, s, NULL, &gerr);
    352 	if (gerr == NULL) {
    353 		externalpipe_execute((char *) buffer, arg);
    354 	} else {
    355 		g_error_free(gerr);
    356 	}
    357 	g_free(buffer);
    358 }
    359 
    360 static void
    361 externalpipe_js_done(WebKitWebView *wv, GAsyncResult *s, Arg *arg)
    362 {
    363 	WebKitJavascriptResult *j = webkit_web_view_run_javascript_finish(
    364 		wv, s, NULL);
    365 	if (!j) {
    366 		return;
    367 	}
    368 	JSCValue *v = webkit_javascript_result_get_js_value(j);
    369 	if (jsc_value_is_string(v)) {
    370 		char *buffer = jsc_value_to_string(v);
    371 		externalpipe_execute(buffer, arg);
    372 		g_free(buffer);
    373 	}
    374 	webkit_javascript_result_unref(j);
    375 }
    376 
    377 void
    378 externalpipe(Client *c, const Arg *arg)
    379 {
    380 	if (curconfig[JavaScript].val.i) {
    381 		webkit_web_view_run_javascript(
    382 			c->view, "window.document.documentElement.outerHTML",
    383 			NULL, externalpipe_js_done, arg);
    384 	} else {
    385 		WebKitWebResource *resource = webkit_web_view_get_main_resource(c->view);
    386 		if (resource != NULL) {
    387 			webkit_web_resource_get_data(
    388 				resource, NULL, externalpipe_resource_done, arg);
    389 		}
    390 	}
    391 }
    392 
    393 void
    394 die(const char *errstr, ...)
    395 {
    396        va_list ap;
    397 
    398        va_start(ap, errstr);
    399        vfprintf(stderr, errstr, ap);
    400        va_end(ap);
    401        exit(1);
    402 }
    403 
    404 void
    405 usage(void)
    406 {
    407 	die("usage: surf [-bBdDfFgGiIkKmMnNpPsStTvwxX]\n"
    408 	    "[-a cookiepolicies ] [-c cookiefile] [-C stylefile] [-e xid]\n"
    409 	    "[-r scriptfile] [-u useragent] [-z zoomlevel] [uri]\n");
    410 }
    411 
    412 void
    413 setup(void)
    414 {
    415 	GIOChannel *gchanin;
    416 	GdkDisplay *gdpy;
    417 	int i, j;
    418 
    419 	/* clean up any zombies immediately */
    420 	sigchld(0);
    421 	if (signal(SIGHUP, sighup) == SIG_ERR)
    422 		die("Can't install SIGHUP handler");
    423 
    424 	if (!(dpy = XOpenDisplay(NULL)))
    425 		die("Can't open default display");
    426 
    427 	/* atoms */
    428 	atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
    429 	atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
    430 	atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
    431 
    432 	gtk_init(NULL, NULL);
    433 
    434 	gdpy = gdk_display_get_default();
    435 
    436 	curconfig = defconfig;
    437 
    438 	/* dirs and files */
    439 	cookiefile = buildfile(cookiefile);
    440 	historyfile = buildfile(historyfile);
    441 	scriptfile = buildfile(scriptfile);
    442 	certdir    = buildpath(certdir);
    443 	if (curconfig[Ephemeral].val.i)
    444 		cachedir = NULL;
    445 	else
    446 		cachedir   = buildpath(cachedir);
    447 
    448 	gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
    449 
    450 	if (socketpair(AF_UNIX, SOCK_DGRAM, 0, spair) < 0) {
    451 		fputs("Unable to create sockets\n", stderr);
    452 		spair[0] = spair[1] = -1;
    453 	} else {
    454 		gchanin = g_io_channel_unix_new(spair[0]);
    455 		g_io_channel_set_encoding(gchanin, NULL, NULL);
    456 		g_io_channel_set_flags(gchanin, g_io_channel_get_flags(gchanin)
    457 		                       | G_IO_FLAG_NONBLOCK, NULL);
    458 		g_io_channel_set_close_on_unref(gchanin, TRUE);
    459 		g_io_add_watch(gchanin, G_IO_IN, readsock, NULL);
    460 	}
    461 
    462 
    463 	for (i = 0; i < LENGTH(certs); ++i) {
    464 		if (!regcomp(&(certs[i].re), certs[i].regex, REG_EXTENDED)) {
    465 			certs[i].file = g_strconcat(certdir, "/", certs[i].file,
    466 			                            NULL);
    467 		} else {
    468 			fprintf(stderr, "Could not compile regex: %s\n",
    469 			        certs[i].regex);
    470 			certs[i].regex = NULL;
    471 		}
    472 	}
    473 
    474 	if (!stylefile) {
    475 		styledir = buildpath(styledir);
    476 		for (i = 0; i < LENGTH(styles); ++i) {
    477 			if (!regcomp(&(styles[i].re), styles[i].regex,
    478 			    REG_EXTENDED)) {
    479 				styles[i].file = g_strconcat(styledir, "/",
    480 				                    styles[i].file, NULL);
    481 			} else {
    482 				fprintf(stderr, "Could not compile regex: %s\n",
    483 				        styles[i].regex);
    484 				styles[i].regex = NULL;
    485 			}
    486 		}
    487 		g_free(styledir);
    488 	} else {
    489 		stylefile = buildfile(stylefile);
    490 	}
    491 
    492 	for (i = 0; i < LENGTH(uriparams); ++i) {
    493 		if (regcomp(&(uriparams[i].re), uriparams[i].uri,
    494 		    REG_EXTENDED)) {
    495 			fprintf(stderr, "Could not compile regex: %s\n",
    496 			        uriparams[i].uri);
    497 			uriparams[i].uri = NULL;
    498 			continue;
    499 		}
    500 
    501 		/* copy default parameters with higher priority */
    502 		for (j = 0; j < ParameterLast; ++j) {
    503 			if (defconfig[j].prio >= uriparams[i].config[j].prio)
    504 				uriparams[i].config[j] = defconfig[j];
    505 		}
    506 	}
    507 }
    508 
    509 void
    510 sigchld(int unused)
    511 {
    512 	if (signal(SIGCHLD, sigchld) == SIG_ERR)
    513 		die("Can't install SIGCHLD handler");
    514 	while (waitpid(-1, NULL, WNOHANG) > 0)
    515 		;
    516 }
    517 
    518 void
    519 sigusr1(int unused)
    520 {
    521   static Arg a = {.v = externalpipe_sigusr1};
    522 	Client *c;
    523 	for (c = clients; c; c = c->next)
    524 		externalpipe(c, &a);
    525 }
    526 
    527 void
    528 sighup(int unused)
    529 {
    530 	Arg a = { .i = 0 };
    531 	Client *c;
    532 
    533 	for (c = clients; c; c = c->next)
    534 		reload(c, &a);
    535 }
    536 
    537 char *
    538 buildfile(const char *path)
    539 {
    540 	char *dname, *bname, *bpath, *fpath;
    541 	FILE *f;
    542 
    543 	dname = g_path_get_dirname(path);
    544 	bname = g_path_get_basename(path);
    545 
    546 	bpath = buildpath(dname);
    547 	g_free(dname);
    548 
    549 	fpath = g_build_filename(bpath, bname, NULL);
    550 	g_free(bpath);
    551 	g_free(bname);
    552 
    553 	if (!(f = fopen(fpath, "a")))
    554 		die("Could not open file: %s\n", fpath);
    555 
    556 	g_chmod(fpath, 0600); /* always */
    557 	fclose(f);
    558 
    559 	return fpath;
    560 }
    561 
    562 static const char*
    563 getuserhomedir(const char *user)
    564 {
    565 	struct passwd *pw = getpwnam(user);
    566 
    567 	if (!pw)
    568 		die("Can't get user %s login information.\n", user);
    569 
    570 	return pw->pw_dir;
    571 }
    572 
    573 static const char*
    574 getcurrentuserhomedir(void)
    575 {
    576 	const char *homedir;
    577 	const char *user;
    578 	struct passwd *pw;
    579 
    580 	homedir = getenv("HOME");
    581 	if (homedir)
    582 		return homedir;
    583 
    584 	user = getenv("USER");
    585 	if (user)
    586 		return getuserhomedir(user);
    587 
    588 	pw = getpwuid(getuid());
    589 	if (!pw)
    590 		die("Can't get current user home directory\n");
    591 
    592 	return pw->pw_dir;
    593 }
    594 
    595 char *
    596 buildpath(const char *path)
    597 {
    598 	char *apath, *fpath;
    599 
    600 	if (path[0] == '~')
    601 		apath = untildepath(path);
    602 	else
    603 		apath = g_strdup(path);
    604 
    605 	/* creating directory */
    606 	if (g_mkdir_with_parents(apath, 0700) < 0)
    607 		die("Could not access directory: %s\n", apath);
    608 
    609 	fpath = realpath(apath, NULL);
    610 	g_free(apath);
    611 
    612 	return fpath;
    613 }
    614 
    615 char *
    616 untildepath(const char *path)
    617 {
    618        char *apath, *name, *p;
    619        const char *homedir;
    620 
    621        if (path[1] == '/' || path[1] == '\0') {
    622                p = (char *)&path[1];
    623                homedir = getcurrentuserhomedir();
    624        } else {
    625                if ((p = strchr(path, '/')))
    626                        name = g_strndup(&path[1], p - (path + 1));
    627                else
    628                        name = g_strdup(&path[1]);
    629 
    630                homedir = getuserhomedir(name);
    631                g_free(name);
    632        }
    633        apath = g_build_filename(homedir, p, NULL);
    634        return apath;
    635 }
    636 
    637 Client *
    638 newclient(Client *rc)
    639 {
    640 	Client *c;
    641 
    642 	if (!(c = calloc(1, sizeof(Client))))
    643 		die("Cannot malloc!\n");
    644 
    645 	c->next = clients;
    646 	clients = c;
    647 
    648 	c->progress = 100;
    649 	c->view = newview(c, rc ? rc->view : NULL);
    650 
    651 	return c;
    652 }
    653 
    654 void
    655 loaduri(Client *c, const Arg *a)
    656 {
    657 	struct stat st;
    658 	char *url, *path, *apath;
    659 	const char *uri = a->v;
    660 
    661 	if (g_strcmp0(uri, "") == 0)
    662 		return;
    663 
    664 	if (g_str_has_prefix(uri, "http://")  ||
    665 	    g_str_has_prefix(uri, "https://") ||
    666 	    g_str_has_prefix(uri, "file://")  ||
    667 	    g_str_has_prefix(uri, "about:")) {
    668 		url = g_strdup(uri);
    669 	} else {
    670 		if (uri[0] == '~')
    671 			apath = untildepath(uri);
    672 		else
    673 			apath = (char *)uri;
    674 		if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
    675 			url = g_strdup_printf("file://%s", path);
    676 			free(path);
    677 		} else {
    678 			url = parseuri(uri);
    679 		}
    680 		if (apath != uri)
    681 			free(apath);
    682 	}
    683 
    684 	setatom(c, AtomUri, url);
    685 
    686 	if (strcmp(url, geturi(c)) == 0) {
    687 		reload(c, a);
    688 	} else {
    689 		webkit_web_view_load_uri(c->view, url);
    690 		updatetitle(c);
    691 	}
    692 
    693 	g_free(url);
    694 }
    695 
    696 const char *
    697 geturi(Client *c)
    698 {
    699 	const char *uri;
    700 
    701 	if (!(uri = webkit_web_view_get_uri(c->view)))
    702 		uri = "about:blank";
    703 	return uri;
    704 }
    705 
    706 void
    707 setatom(Client *c, int a, const char *v)
    708 {
    709 	XChangeProperty(dpy, c->xid,
    710 	                atoms[a], XA_STRING, 8, PropModeReplace,
    711 	                (unsigned char *)v, strlen(v) + 1);
    712 	XSync(dpy, False);
    713 }
    714 
    715 const char *
    716 getatom(Client *c, int a)
    717 {
    718 	static char buf[BUFSIZ];
    719 	Atom adummy;
    720 	int idummy;
    721 	unsigned long ldummy;
    722 	unsigned char *p = NULL;
    723 
    724 	XSync(dpy, False);
    725 	XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
    726 	                   &adummy, &idummy, &ldummy, &ldummy, &p);
    727 	if (p)
    728 		strncpy(buf, (char *)p, LENGTH(buf) - 1);
    729 	else
    730 		buf[0] = '\0';
    731 	XFree(p);
    732 
    733 	return buf;
    734 }
    735 
    736 void
    737 updatetitle(Client *c)
    738 {
    739 	char *title;
    740 	const char *name = c->overtitle ? c->overtitle :
    741 	                   c->title ? c->title : "";
    742 
    743 	if (curconfig[ShowIndicators].val.i) {
    744 		gettogglestats(c);
    745 		getpagestats(c);
    746 
    747 		if (c->progress != 100)
    748 			title = g_strdup_printf("[%i%%] %s:%s | %s",
    749 			        c->progress, togglestats, pagestats, name);
    750 		else
    751 			title = g_strdup_printf("%s:%s | %s",
    752 			        togglestats, pagestats, name);
    753 
    754 		gtk_window_set_title(GTK_WINDOW(c->win), title);
    755 		g_free(title);
    756 	} else {
    757 		gtk_window_set_title(GTK_WINDOW(c->win), name);
    758 	}
    759 }
    760 
    761 void
    762 gettogglestats(Client *c)
    763 {
    764 	togglestats[0] = cookiepolicy_set(cookiepolicy_get());
    765 	togglestats[1] = curconfig[CaretBrowsing].val.i ?   'C' : 'c';
    766 	togglestats[2] = curconfig[Geolocation].val.i ?     'G' : 'g';
    767 	togglestats[3] = curconfig[DiskCache].val.i ?       'D' : 'd';
    768 	togglestats[4] = curconfig[LoadImages].val.i ?      'I' : 'i';
    769 	togglestats[5] = curconfig[JavaScript].val.i ?      'S' : 's';
    770 	togglestats[6] = curconfig[Plugins].val.i ?         'V' : 'v';
    771 	togglestats[7] = curconfig[Style].val.i ?           'M' : 'm';
    772 	togglestats[8] = curconfig[FrameFlattening].val.i ? 'F' : 'f';
    773 	togglestats[9] = curconfig[Certificate].val.i ?     'X' : 'x';
    774 	togglestats[10] = curconfig[StrictTLS].val.i ?      'T' : 't';
    775 	togglestats[11] = '\0';
    776 }
    777 
    778 void
    779 getpagestats(Client *c)
    780 {
    781 	if (c->https)
    782 		pagestats[0] = (c->tlserr || c->insecure) ?  'U' : 'T';
    783 	else
    784 		pagestats[0] = '-';
    785 	pagestats[1] = '\0';
    786 }
    787 
    788 WebKitCookieAcceptPolicy
    789 cookiepolicy_get(void)
    790 {
    791 	switch (((char *)curconfig[CookiePolicies].val.v)[cookiepolicy]) {
    792 	case 'a':
    793 		return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
    794 	case '@':
    795 		return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
    796 	default: /* fallthrough */
    797 	case 'A':
    798 		return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
    799 	}
    800 }
    801 
    802 char
    803 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
    804 {
    805 	switch (p) {
    806 	case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
    807 		return 'a';
    808 	case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
    809 		return '@';
    810 	default: /* fallthrough */
    811 	case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
    812 		return 'A';
    813 	}
    814 }
    815 
    816 void
    817 seturiparameters(Client *c, const char *uri, ParamName *params)
    818 {
    819 	Parameter *config, *uriconfig = NULL;
    820 	int i, p;
    821 
    822 	for (i = 0; i < LENGTH(uriparams); ++i) {
    823 		if (uriparams[i].uri &&
    824 		    !regexec(&(uriparams[i].re), uri, 0, NULL, 0)) {
    825 			uriconfig = uriparams[i].config;
    826 			break;
    827 		}
    828 	}
    829 
    830 	curconfig = uriconfig ? uriconfig : defconfig;
    831 
    832 	for (i = 0; (p = params[i]) != ParameterLast; ++i) {
    833 		switch(p) {
    834 		default: /* FALLTHROUGH */
    835 			if (!(defconfig[p].prio < curconfig[p].prio ||
    836 			    defconfig[p].prio < modparams[p]))
    837 				continue;
    838 		case Certificate:
    839 		case CookiePolicies:
    840 		case Style:
    841 			setparameter(c, 0, p, &curconfig[p].val);
    842 		}
    843 	}
    844 }
    845 
    846 void
    847 setparameter(Client *c, int refresh, ParamName p, const Arg *a)
    848 {
    849 	GdkRGBA bgcolor = { 0 };
    850 	WebKitSettings *s = webkit_web_view_get_settings(c->view);
    851 
    852 	modparams[p] = curconfig[p].prio;
    853 
    854 	switch (p) {
    855 	case AcceleratedCanvas:
    856 		webkit_settings_set_enable_accelerated_2d_canvas(s, a->i);
    857 		break;
    858 	case AccessMicrophone:
    859 		return; /* do nothing */
    860 	case AccessWebcam:
    861 		return; /* do nothing */
    862 	case CaretBrowsing:
    863 		webkit_settings_set_enable_caret_browsing(s, a->i);
    864 		refresh = 0;
    865 		break;
    866 	case Certificate:
    867 		if (a->i)
    868 			setcert(c, geturi(c));
    869 		return; /* do not update */
    870 	case CookiePolicies:
    871 		webkit_cookie_manager_set_accept_policy(
    872 		    webkit_web_context_get_cookie_manager(
    873 		    webkit_web_view_get_context(c->view)),
    874 		    cookiepolicy_get());
    875 		refresh = 0;
    876 		break;
    877 	case DiskCache:
    878 		webkit_web_context_set_cache_model(
    879 		    webkit_web_view_get_context(c->view), a->i ?
    880 		    WEBKIT_CACHE_MODEL_WEB_BROWSER :
    881 		    WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
    882 		return; /* do not update */
    883 	case DefaultCharset:
    884 		webkit_settings_set_default_charset(s, a->v);
    885 		return; /* do not update */
    886 	case DNSPrefetch:
    887 		webkit_settings_set_enable_dns_prefetching(s, a->i);
    888 		return; /* do not update */
    889 	case FileURLsCrossAccess:
    890 		webkit_settings_set_allow_file_access_from_file_urls(s, a->i);
    891 		webkit_settings_set_allow_universal_access_from_file_urls(s, a->i);
    892 		return; /* do not update */
    893 	case FontSize:
    894 		webkit_settings_set_default_font_size(s, a->i);
    895 		return; /* do not update */
    896 	case FrameFlattening:
    897 		webkit_settings_set_enable_frame_flattening(s, a->i);
    898 		break;
    899 	case Geolocation:
    900 		refresh = 0;
    901 		break;
    902 	case HideBackground:
    903 		if (a->i)
    904 			webkit_web_view_set_background_color(c->view, &bgcolor);
    905 		return; /* do not update */
    906 	case Inspector:
    907 		webkit_settings_set_enable_developer_extras(s, a->i);
    908 		return; /* do not update */
    909 	case Java:
    910 		webkit_settings_set_enable_java(s, a->i);
    911 		return; /* do not update */
    912 	case JavaScript:
    913 		webkit_settings_set_enable_javascript(s, a->i);
    914 		break;
    915 	case KioskMode:
    916 		return; /* do nothing */
    917 	case LoadImages:
    918 		webkit_settings_set_auto_load_images(s, a->i);
    919 		break;
    920 	case MediaManualPlay:
    921 		webkit_settings_set_media_playback_requires_user_gesture(s, a->i);
    922 		break;
    923 	case Plugins:
    924 		webkit_settings_set_enable_plugins(s, a->i);
    925 		break;
    926 	case PreferredLanguages:
    927 		return; /* do nothing */
    928 	case RunInFullscreen:
    929 		return; /* do nothing */
    930 	case ScrollBars:
    931 		/* Disabled until we write some WebKitWebExtension for
    932 		 * manipulating the DOM directly.
    933 		enablescrollbars = !enablescrollbars;
    934 		evalscript(c, "document.documentElement.style.overflow = '%s'",
    935 		    enablescrollbars ? "auto" : "hidden");
    936 		*/
    937 		return; /* do not update */
    938 	case ShowIndicators:
    939 		break;
    940 	case SmoothScrolling:
    941 		webkit_settings_set_enable_smooth_scrolling(s, a->i);
    942 		return; /* do not update */
    943 	case SiteQuirks:
    944 		webkit_settings_set_enable_site_specific_quirks(s, a->i);
    945 		break;
    946 	case SpellChecking:
    947 		webkit_web_context_set_spell_checking_enabled(
    948 		    webkit_web_view_get_context(c->view), a->i);
    949 		return; /* do not update */
    950 	case SpellLanguages:
    951 		return; /* do nothing */
    952 	case StrictTLS:
    953 		webkit_web_context_set_tls_errors_policy(
    954 		    webkit_web_view_get_context(c->view), a->i ?
    955 		    WEBKIT_TLS_ERRORS_POLICY_FAIL :
    956 		    WEBKIT_TLS_ERRORS_POLICY_IGNORE);
    957 		break;
    958 	case Style:
    959 		webkit_user_content_manager_remove_all_style_sheets(
    960 		    webkit_web_view_get_user_content_manager(c->view));
    961 		if (a->i)
    962 			setstyle(c, getstyle(geturi(c)));
    963 		refresh = 0;
    964 		break;
    965 	case WebGL:
    966 		webkit_settings_set_enable_webgl(s, a->i);
    967 		break;
    968 	case ZoomLevel:
    969 		webkit_web_view_set_zoom_level(c->view, a->f);
    970 		return; /* do not update */
    971 	default:
    972 		return; /* do nothing */
    973 	}
    974 
    975 	updatetitle(c);
    976 	if (refresh)
    977 		reload(c, a);
    978 }
    979 
    980 const char *
    981 getcert(const char *uri)
    982 {
    983 	int i;
    984 
    985 	for (i = 0; i < LENGTH(certs); ++i) {
    986 		if (certs[i].regex &&
    987 		    !regexec(&(certs[i].re), uri, 0, NULL, 0))
    988 			return certs[i].file;
    989 	}
    990 
    991 	return NULL;
    992 }
    993 
    994 void
    995 setcert(Client *c, const char *uri)
    996 {
    997 	const char *file = getcert(uri);
    998 	char *host;
    999 	GTlsCertificate *cert;
   1000 
   1001 	if (!file)
   1002 		return;
   1003 
   1004 	if (!(cert = g_tls_certificate_new_from_file(file, NULL))) {
   1005 		fprintf(stderr, "Could not read certificate file: %s\n", file);
   1006 		return;
   1007 	}
   1008 
   1009 	if ((uri = strstr(uri, "https://"))) {
   1010 		uri += sizeof("https://") - 1;
   1011 		host = g_strndup(uri, strchr(uri, '/') - uri);
   1012 		webkit_web_context_allow_tls_certificate_for_host(
   1013 		    webkit_web_view_get_context(c->view), cert, host);
   1014 		g_free(host);
   1015 	}
   1016 
   1017 	g_object_unref(cert);
   1018 
   1019 }
   1020 
   1021 const char *
   1022 getstyle(const char *uri)
   1023 {
   1024 	int i;
   1025 
   1026 	if (stylefile)
   1027 		return stylefile;
   1028 
   1029 	for (i = 0; i < LENGTH(styles); ++i) {
   1030 		if (styles[i].regex &&
   1031 		    !regexec(&(styles[i].re), uri, 0, NULL, 0))
   1032 			return styles[i].file;
   1033 	}
   1034 
   1035 	return "";
   1036 }
   1037 
   1038 void
   1039 setstyle(Client *c, const char *file)
   1040 {
   1041 	gchar *style;
   1042 
   1043 	if (!g_file_get_contents(file, &style, NULL, NULL)) {
   1044 		fprintf(stderr, "Could not read style file: %s\n", file);
   1045 		return;
   1046 	}
   1047 
   1048 	webkit_user_content_manager_add_style_sheet(
   1049 	    webkit_web_view_get_user_content_manager(c->view),
   1050 	    webkit_user_style_sheet_new(style,
   1051 	    WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
   1052 	    WEBKIT_USER_STYLE_LEVEL_USER,
   1053 	    NULL, NULL));
   1054 
   1055 	g_free(style);
   1056 }
   1057 
   1058 void
   1059 runscript(Client *c)
   1060 {
   1061 	gchar *script;
   1062 	gsize l;
   1063 
   1064 	if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
   1065 		evalscript(c, "%s", script);
   1066 	g_free(script);
   1067 }
   1068 
   1069 void
   1070 evalscript(Client *c, const char *jsstr, ...)
   1071 {
   1072 	va_list ap;
   1073 	gchar *script;
   1074 
   1075 	va_start(ap, jsstr);
   1076 	script = g_strdup_vprintf(jsstr, ap);
   1077 	va_end(ap);
   1078 
   1079 	webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
   1080 	g_free(script);
   1081 }
   1082 
   1083 void
   1084 updatewinid(Client *c)
   1085 {
   1086 	snprintf(winid, LENGTH(winid), "%lu", c->xid);
   1087 }
   1088 
   1089 void
   1090 handleplumb(Client *c, const char *uri)
   1091 {
   1092 	Arg a = (Arg)PLUMB(uri);
   1093 	spawn(c, &a);
   1094 }
   1095 
   1096 void
   1097 newwindow(Client *c, const Arg *a, int noembed)
   1098 {
   1099 	int i = 0;
   1100 	char tmp[64];
   1101 	const char *cmd[29], *uri;
   1102 	const Arg arg = { .v = cmd };
   1103 
   1104 	cmd[i++] = argv0;
   1105 	cmd[i++] = "-a";
   1106 	cmd[i++] = curconfig[CookiePolicies].val.v;
   1107 	cmd[i++] = curconfig[ScrollBars].val.i ? "-B" : "-b";
   1108 	if (cookiefile && g_strcmp0(cookiefile, "")) {
   1109 		cmd[i++] = "-c";
   1110 		cmd[i++] = cookiefile;
   1111 	}
   1112 	if (stylefile && g_strcmp0(stylefile, "")) {
   1113 		cmd[i++] = "-C";
   1114 		cmd[i++] = stylefile;
   1115 	}
   1116 	cmd[i++] = curconfig[DiskCache].val.i ? "-D" : "-d";
   1117 	if (embed && !noembed) {
   1118 		cmd[i++] = "-e";
   1119 		snprintf(tmp, LENGTH(tmp), "%lu", embed);
   1120 		cmd[i++] = tmp;
   1121 	}
   1122 	cmd[i++] = curconfig[RunInFullscreen].val.i ? "-F" : "-f" ;
   1123 	cmd[i++] = curconfig[Geolocation].val.i ?     "-G" : "-g" ;
   1124 	cmd[i++] = curconfig[LoadImages].val.i ?      "-I" : "-i" ;
   1125 	cmd[i++] = curconfig[KioskMode].val.i ?       "-K" : "-k" ;
   1126 	cmd[i++] = curconfig[Style].val.i ?           "-M" : "-m" ;
   1127 	cmd[i++] = curconfig[Inspector].val.i ?       "-N" : "-n" ;
   1128 	cmd[i++] = curconfig[Plugins].val.i ?         "-P" : "-p" ;
   1129 	if (scriptfile && g_strcmp0(scriptfile, "")) {
   1130 		cmd[i++] = "-r";
   1131 		cmd[i++] = scriptfile;
   1132 	}
   1133 	cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
   1134 	cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
   1135 	if (fulluseragent && g_strcmp0(fulluseragent, "")) {
   1136 		cmd[i++] = "-u";
   1137 		cmd[i++] = fulluseragent;
   1138 	}
   1139 	if (showxid)
   1140 		cmd[i++] = "-w";
   1141 	cmd[i++] = curconfig[Certificate].val.i ? "-X" : "-x" ;
   1142 	/* do not keep zoom level */
   1143 	cmd[i++] = "--";
   1144 	if ((uri = a->v))
   1145 		cmd[i++] = uri;
   1146 	cmd[i] = NULL;
   1147 
   1148 	spawn(c, &arg);
   1149 }
   1150 
   1151 void
   1152 spawn(Client *c, const Arg *a)
   1153 {
   1154 	if (fork() == 0) {
   1155 		if (dpy)
   1156 			close(ConnectionNumber(dpy));
   1157 		close(spair[0]);
   1158 		close(spair[1]);
   1159 		setsid();
   1160 		execvp(((char **)a->v)[0], (char **)a->v);
   1161 		fprintf(stderr, "%s: execvp %s", argv0, ((char **)a->v)[0]);
   1162 		perror(" failed");
   1163 		exit(1);
   1164 	}
   1165 }
   1166 
   1167 void
   1168 destroyclient(Client *c)
   1169 {
   1170 	Client *p;
   1171 
   1172 	webkit_web_view_stop_loading(c->view);
   1173 	/* Not needed, has already been called
   1174 	gtk_widget_destroy(c->win);
   1175 	 */
   1176 
   1177 	for (p = clients; p && p->next != c; p = p->next)
   1178 		;
   1179 	if (p)
   1180 		p->next = c->next;
   1181 	else
   1182 		clients = c->next;
   1183 	free(c);
   1184 }
   1185 
   1186 void
   1187 cleanup(void)
   1188 {
   1189 	while (clients)
   1190 		destroyclient(clients);
   1191 
   1192 	close(spair[0]);
   1193 	close(spair[1]);
   1194 	g_free(cookiefile);
   1195 	g_free(historyfile);
   1196 	g_free(scriptfile);
   1197 	g_free(stylefile);
   1198 	g_free(cachedir);
   1199 	XCloseDisplay(dpy);
   1200 }
   1201 
   1202 void
   1203 updatehistory(const char *u, const char *t)
   1204 {
   1205 	FILE *f;
   1206 	f = fopen(historyfile, "a+");
   1207 
   1208 	char b[20];
   1209 	time_t now = time (0);
   1210 	strftime (b, 20, "%Y-%m-%d %H:%M:%S", localtime (&now));
   1211 	fputs(b, f);
   1212 
   1213 	fprintf(f, " %s %s\n", u, t);
   1214 	fclose(f);
   1215 }
   1216 
   1217 WebKitWebView *
   1218 newview(Client *c, WebKitWebView *rv)
   1219 {
   1220 	WebKitWebView *v;
   1221 	WebKitSettings *settings;
   1222 	WebKitWebContext *context;
   1223 	WebKitCookieManager *cookiemanager;
   1224 	WebKitUserContentManager *contentmanager;
   1225 
   1226 	/* Webview */
   1227 	if (rv) {
   1228 		v = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(rv));
   1229 	} else {
   1230 		settings = webkit_settings_new_with_settings(
   1231 		   "allow-file-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
   1232 		   "allow-universal-access-from-file-urls", curconfig[FileURLsCrossAccess].val.i,
   1233 		   "auto-load-images", curconfig[LoadImages].val.i,
   1234 		   "default-charset", curconfig[DefaultCharset].val.v,
   1235 		   "default-font-size", curconfig[FontSize].val.i,
   1236 		   "enable-caret-browsing", curconfig[CaretBrowsing].val.i,
   1237 		   "enable-developer-extras", curconfig[Inspector].val.i,
   1238 		   "enable-dns-prefetching", curconfig[DNSPrefetch].val.i,
   1239 		   "enable-frame-flattening", curconfig[FrameFlattening].val.i,
   1240 		   "enable-html5-database", curconfig[DiskCache].val.i,
   1241 		   "enable-html5-local-storage", curconfig[DiskCache].val.i,
   1242 		   "enable-java", curconfig[Java].val.i,
   1243 		   "enable-javascript", curconfig[JavaScript].val.i,
   1244 		   "enable-plugins", curconfig[Plugins].val.i,
   1245 		   "enable-accelerated-2d-canvas", curconfig[AcceleratedCanvas].val.i,
   1246 		   "enable-site-specific-quirks", curconfig[SiteQuirks].val.i,
   1247 		   "enable-smooth-scrolling", curconfig[SmoothScrolling].val.i,
   1248 		   "enable-webgl", curconfig[WebGL].val.i,
   1249 		   "media-playback-requires-user-gesture", curconfig[MediaManualPlay].val.i,
   1250 		   NULL);
   1251 /* For more interesting settings, have a look at
   1252  * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
   1253 
   1254 		if (strcmp(fulluseragent, "")) {
   1255 			webkit_settings_set_user_agent(settings, fulluseragent);
   1256 		} else if (surfuseragent) {
   1257 			webkit_settings_set_user_agent_with_application_details(
   1258 			    settings, "Surf", VERSION);
   1259 		}
   1260 		useragent = webkit_settings_get_user_agent(settings);
   1261 
   1262 		contentmanager = webkit_user_content_manager_new();
   1263 
   1264 		if (curconfig[Ephemeral].val.i) {
   1265 			context = webkit_web_context_new_ephemeral();
   1266 		} else {
   1267 			context = webkit_web_context_new_with_website_data_manager(
   1268 			          webkit_website_data_manager_new(
   1269 			          "base-cache-directory", cachedir,
   1270 			          "base-data-directory", cachedir,
   1271 			          NULL));
   1272 		}
   1273 
   1274 
   1275 		cookiemanager = webkit_web_context_get_cookie_manager(context);
   1276 
   1277 		/* rendering process model, can be a shared unique one
   1278 		 * or one for each view */
   1279 		webkit_web_context_set_process_model(context,
   1280 		    WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
   1281 		/* TLS */
   1282 		webkit_web_context_set_tls_errors_policy(context,
   1283 		    curconfig[StrictTLS].val.i ? WEBKIT_TLS_ERRORS_POLICY_FAIL :
   1284 		    WEBKIT_TLS_ERRORS_POLICY_IGNORE);
   1285 		/* disk cache */
   1286 		webkit_web_context_set_cache_model(context,
   1287 		    curconfig[DiskCache].val.i ? WEBKIT_CACHE_MODEL_WEB_BROWSER :
   1288 		    WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
   1289 		/* plugins directories */
   1290 		for (; *plugindirs; ++plugindirs)
   1291 			webkit_web_context_set_additional_plugins_directory(
   1292 			    context, *plugindirs);
   1293 
   1294 		/* Currently only works with text file to be compatible with curl */
   1295 		if (!curconfig[Ephemeral].val.i)
   1296 			webkit_cookie_manager_set_persistent_storage(cookiemanager,
   1297 			    cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
   1298 		/* cookie policy */
   1299 		webkit_cookie_manager_set_accept_policy(cookiemanager,
   1300 		    cookiepolicy_get());
   1301 		/* languages */
   1302 		webkit_web_context_set_preferred_languages(context,
   1303 		    curconfig[PreferredLanguages].val.v);
   1304 		webkit_web_context_set_spell_checking_languages(context,
   1305 		    curconfig[SpellLanguages].val.v);
   1306 		webkit_web_context_set_spell_checking_enabled(context,
   1307 		    curconfig[SpellChecking].val.i);
   1308 
   1309 		g_signal_connect(G_OBJECT(context), "download-started",
   1310 		                 G_CALLBACK(downloadstarted), c);
   1311 		g_signal_connect(G_OBJECT(context), "initialize-web-extensions",
   1312 		                 G_CALLBACK(initwebextensions), c);
   1313 
   1314 		v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
   1315 		    "settings", settings,
   1316 		    "user-content-manager", contentmanager,
   1317 		    "web-context", context,
   1318 		    NULL);
   1319 	}
   1320 
   1321 	g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
   1322 			 G_CALLBACK(progresschanged), c);
   1323 	g_signal_connect(G_OBJECT(v), "notify::title",
   1324 			 G_CALLBACK(titlechanged), c);
   1325 	g_signal_connect(G_OBJECT(v), "button-release-event",
   1326 			 G_CALLBACK(buttonreleased), c);
   1327 	g_signal_connect(G_OBJECT(v), "close",
   1328 			G_CALLBACK(closeview), c);
   1329 	g_signal_connect(G_OBJECT(v), "create",
   1330 			 G_CALLBACK(createview), c);
   1331 	g_signal_connect(G_OBJECT(v), "decide-policy",
   1332 			 G_CALLBACK(decidepolicy), c);
   1333 	g_signal_connect(G_OBJECT(v), "insecure-content-detected",
   1334 			 G_CALLBACK(insecurecontent), c);
   1335 	g_signal_connect(G_OBJECT(v), "load-failed-with-tls-errors",
   1336 			 G_CALLBACK(loadfailedtls), c);
   1337 	g_signal_connect(G_OBJECT(v), "load-changed",
   1338 			 G_CALLBACK(loadchanged), c);
   1339 	g_signal_connect(G_OBJECT(v), "mouse-target-changed",
   1340 			 G_CALLBACK(mousetargetchanged), c);
   1341 	g_signal_connect(G_OBJECT(v), "permission-request",
   1342 			 G_CALLBACK(permissionrequested), c);
   1343 	g_signal_connect(G_OBJECT(v), "ready-to-show",
   1344 			 G_CALLBACK(showview), c);
   1345 	g_signal_connect(G_OBJECT(v), "web-process-terminated",
   1346 			 G_CALLBACK(webprocessterminated), c);
   1347 
   1348 	return v;
   1349 }
   1350 
   1351 static gboolean
   1352 readsock(GIOChannel *s, GIOCondition ioc, gpointer unused)
   1353 {
   1354 	static char msg[MSGBUFSZ];
   1355 	GError *gerr = NULL;
   1356 	gsize msgsz;
   1357 
   1358 	if (g_io_channel_read_chars(s, msg, sizeof(msg), &msgsz, &gerr) !=
   1359 	    G_IO_STATUS_NORMAL) {
   1360 		if (gerr) {
   1361 			fprintf(stderr, "surf: error reading socket: %s\n",
   1362 			        gerr->message);
   1363 			g_error_free(gerr);
   1364 		}
   1365 		return TRUE;
   1366 	}
   1367 	if (msgsz < 2) {
   1368 		fprintf(stderr, "surf: message too short: %d\n", msgsz);
   1369 		return TRUE;
   1370 	}
   1371 
   1372 	return TRUE;
   1373 }
   1374 
   1375 void
   1376 initwebextensions(WebKitWebContext *wc, Client *c)
   1377 {
   1378 	GVariant *gv;
   1379 
   1380 	if (spair[1] < 0)
   1381 		return;
   1382 
   1383 	gv = g_variant_new("i", spair[1]);
   1384 
   1385 	webkit_web_context_set_web_extensions_initialization_user_data(wc, gv);
   1386 	webkit_web_context_set_web_extensions_directory(wc, WEBEXTDIR);
   1387 }
   1388 
   1389 GtkWidget *
   1390 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
   1391 {
   1392 	Client *n;
   1393 
   1394 	switch (webkit_navigation_action_get_navigation_type(a)) {
   1395 	case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
   1396 		/*
   1397 		 * popup windows of type “other” are almost always triggered
   1398 		 * by user gesture, so inverse the logic here
   1399 		 */
   1400 /* instead of this, compare destination uri to mouse-over uri for validating window */
   1401 		if (webkit_navigation_action_is_user_gesture(a))
   1402 			return NULL;
   1403 	case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
   1404 	case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
   1405 	case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
   1406 	case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
   1407 	case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
   1408 		n = newclient(c);
   1409 		break;
   1410 	default:
   1411 		return NULL;
   1412 	}
   1413 
   1414 	return GTK_WIDGET(n->view);
   1415 }
   1416 
   1417 gboolean
   1418 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
   1419 {
   1420 	WebKitHitTestResultContext element;
   1421 	int i;
   1422 
   1423 	element = webkit_hit_test_result_get_context(c->mousepos);
   1424 
   1425 	for (i = 0; i < LENGTH(buttons); ++i) {
   1426 		if (element & buttons[i].target &&
   1427 		    e->button.button == buttons[i].button &&
   1428 		    CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
   1429 		    buttons[i].func) {
   1430 			buttons[i].func(c, &buttons[i].arg, c->mousepos);
   1431 			return buttons[i].stopevent;
   1432 		}
   1433 	}
   1434 
   1435 	return FALSE;
   1436 }
   1437 
   1438 GdkFilterReturn
   1439 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
   1440 {
   1441 	Client *c = (Client *)d;
   1442 	XPropertyEvent *ev;
   1443 	Arg a;
   1444 
   1445 	if (((XEvent *)e)->type == PropertyNotify) {
   1446 		ev = &((XEvent *)e)->xproperty;
   1447 		if (ev->state == PropertyNewValue) {
   1448 			if (ev->atom == atoms[AtomFind]) {
   1449 				find(c, NULL);
   1450 
   1451 				return GDK_FILTER_REMOVE;
   1452 			} else if (ev->atom == atoms[AtomGo]) {
   1453 				a.v = getatom(c, AtomGo);
   1454 				loaduri(c, &a);
   1455 
   1456 				return GDK_FILTER_REMOVE;
   1457 			}
   1458 		}
   1459 	}
   1460 	return GDK_FILTER_CONTINUE;
   1461 }
   1462 
   1463 gboolean
   1464 winevent(GtkWidget *w, GdkEvent *e, Client *c)
   1465 {
   1466 	int i;
   1467 
   1468 	switch (e->type) {
   1469 	case GDK_ENTER_NOTIFY:
   1470 		c->overtitle = c->targeturi;
   1471 		updatetitle(c);
   1472 		break;
   1473 	case GDK_KEY_PRESS:
   1474 		if (!curconfig[KioskMode].val.i) {
   1475 			for (i = 0; i < LENGTH(keys); ++i) {
   1476 				if (gdk_keyval_to_lower(e->key.keyval) ==
   1477 				    keys[i].keyval &&
   1478 				    CLEANMASK(e->key.state) == keys[i].mod &&
   1479 				    keys[i].func) {
   1480 					updatewinid(c);
   1481 					keys[i].func(c, &(keys[i].arg));
   1482 					return TRUE;
   1483 				}
   1484 			}
   1485 		}
   1486 	case GDK_LEAVE_NOTIFY:
   1487 		c->overtitle = NULL;
   1488 		updatetitle(c);
   1489 		break;
   1490 	case GDK_WINDOW_STATE:
   1491 		if (e->window_state.changed_mask ==
   1492 		    GDK_WINDOW_STATE_FULLSCREEN)
   1493 			c->fullscreen = e->window_state.new_window_state &
   1494 			                GDK_WINDOW_STATE_FULLSCREEN;
   1495 		break;
   1496 	default:
   1497 		break;
   1498 	}
   1499 
   1500 	return FALSE;
   1501 }
   1502 
   1503 void
   1504 showview(WebKitWebView *v, Client *c)
   1505 {
   1506 	GdkRGBA bgcolor = { 0 };
   1507 	GdkWindow *gwin;
   1508 
   1509 	c->finder = webkit_web_view_get_find_controller(c->view);
   1510 	c->inspector = webkit_web_view_get_inspector(c->view);
   1511 
   1512 	c->pageid = webkit_web_view_get_page_id(c->view);
   1513 	c->win = createwindow(c);
   1514 
   1515 	gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
   1516 	gtk_widget_show_all(c->win);
   1517 	gtk_widget_grab_focus(GTK_WIDGET(c->view));
   1518 
   1519 	gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
   1520 	c->xid = gdk_x11_window_get_xid(gwin);
   1521 	updatewinid(c);
   1522 	if (showxid) {
   1523 		gdk_display_sync(gtk_widget_get_display(c->win));
   1524 		puts(winid);
   1525 		fflush(stdout);
   1526 	}
   1527 
   1528 	if (curconfig[HideBackground].val.i)
   1529 		webkit_web_view_set_background_color(c->view, &bgcolor);
   1530 
   1531 	if (!curconfig[KioskMode].val.i) {
   1532 		gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
   1533 		gdk_window_add_filter(gwin, processx, c);
   1534 	}
   1535 
   1536 	if (curconfig[RunInFullscreen].val.i)
   1537 		togglefullscreen(c, NULL);
   1538 
   1539 	if (curconfig[ZoomLevel].val.f != 1.0)
   1540 		webkit_web_view_set_zoom_level(c->view,
   1541 		                               curconfig[ZoomLevel].val.f);
   1542 
   1543 	setatom(c, AtomFind, "");
   1544 	setatom(c, AtomUri, "about:blank");
   1545 }
   1546 
   1547 GtkWidget *
   1548 createwindow(Client *c)
   1549 {
   1550 	char *wmstr;
   1551 	GtkWidget *w;
   1552 
   1553 	if (embed) {
   1554 		w = gtk_plug_new(embed);
   1555 	} else {
   1556 		w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   1557 
   1558 		wmstr = g_path_get_basename(argv0);
   1559 		gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
   1560 		g_free(wmstr);
   1561 
   1562 		wmstr = g_strdup_printf("%s[%"PRIu64"]", "Surf", c->pageid);
   1563 		gtk_window_set_role(GTK_WINDOW(w), wmstr);
   1564 		g_free(wmstr);
   1565 
   1566 		gtk_window_set_default_size(GTK_WINDOW(w), winsize[0], winsize[1]);
   1567 	}
   1568 
   1569 	g_signal_connect(G_OBJECT(w), "destroy",
   1570 	                 G_CALLBACK(destroywin), c);
   1571 	g_signal_connect(G_OBJECT(w), "enter-notify-event",
   1572 	                 G_CALLBACK(winevent), c);
   1573 	g_signal_connect(G_OBJECT(w), "key-press-event",
   1574 	                 G_CALLBACK(winevent), c);
   1575 	g_signal_connect(G_OBJECT(w), "leave-notify-event",
   1576 	                 G_CALLBACK(winevent), c);
   1577 	g_signal_connect(G_OBJECT(w), "window-state-event",
   1578 	                 G_CALLBACK(winevent), c);
   1579 
   1580 	return w;
   1581 }
   1582 
   1583 gboolean
   1584 loadfailedtls(WebKitWebView *v, gchar *uri, GTlsCertificate *cert,
   1585               GTlsCertificateFlags err, Client *c)
   1586 {
   1587 	GString *errmsg = g_string_new(NULL);
   1588 	gchar *html, *pem;
   1589 
   1590 	c->failedcert = g_object_ref(cert);
   1591 	c->tlserr = err;
   1592 	c->errorpage = 1;
   1593 
   1594 	if (err & G_TLS_CERTIFICATE_UNKNOWN_CA)
   1595 		g_string_append(errmsg,
   1596 		    "The signing certificate authority is not known.<br>");
   1597 	if (err & G_TLS_CERTIFICATE_BAD_IDENTITY)
   1598 		g_string_append(errmsg,
   1599 		    "The certificate does not match the expected identity "
   1600 		    "of the site that it was retrieved from.<br>");
   1601 	if (err & G_TLS_CERTIFICATE_NOT_ACTIVATED)
   1602 		g_string_append(errmsg,
   1603 		    "The certificate's activation time "
   1604 		    "is still in the future.<br>");
   1605 	if (err & G_TLS_CERTIFICATE_EXPIRED)
   1606 		g_string_append(errmsg, "The certificate has expired.<br>");
   1607 	if (err & G_TLS_CERTIFICATE_REVOKED)
   1608 		g_string_append(errmsg,
   1609 		    "The certificate has been revoked according to "
   1610 		    "the GTlsConnection's certificate revocation list.<br>");
   1611 	if (err & G_TLS_CERTIFICATE_INSECURE)
   1612 		g_string_append(errmsg,
   1613 		    "The certificate's algorithm is considered insecure.<br>");
   1614 	if (err & G_TLS_CERTIFICATE_GENERIC_ERROR)
   1615 		g_string_append(errmsg,
   1616 		    "Some error occurred validating the certificate.<br>");
   1617 
   1618 	g_object_get(cert, "certificate-pem", &pem, NULL);
   1619 	html = g_strdup_printf("<p>Could not validate TLS for “%s”<br>%s</p>"
   1620 	                       "<p>You can inspect the following certificate "
   1621 	                       "with Ctrl-t (default keybinding).</p>"
   1622 	                       "<p><pre>%s</pre></p>", uri, errmsg->str, pem);
   1623 	g_free(pem);
   1624 	g_string_free(errmsg, TRUE);
   1625 
   1626 	webkit_web_view_load_alternate_html(c->view, html, uri, NULL);
   1627 	g_free(html);
   1628 
   1629 	return TRUE;
   1630 }
   1631 
   1632 
   1633 void
   1634 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
   1635 {
   1636 	const char *uri = geturi(c);
   1637 
   1638 	switch (e) {
   1639 	case WEBKIT_LOAD_STARTED:
   1640 		setatom(c, AtomUri, uri);
   1641 		c->title = uri;
   1642 		c->https = c->insecure = 0;
   1643 		seturiparameters(c, uri, loadtransient);
   1644 		if (c->errorpage)
   1645 			c->errorpage = 0;
   1646 		else
   1647 			g_clear_object(&c->failedcert);
   1648 		break;
   1649 	case WEBKIT_LOAD_REDIRECTED:
   1650 		setatom(c, AtomUri, uri);
   1651 		c->title = uri;
   1652 		seturiparameters(c, uri, loadtransient);
   1653 		break;
   1654 	case WEBKIT_LOAD_COMMITTED:
   1655 		setatom(c, AtomUri, uri);
   1656 		c->title = uri;
   1657 		seturiparameters(c, uri, loadcommitted);
   1658 		c->https = webkit_web_view_get_tls_info(c->view, &c->cert,
   1659 		                                        &c->tlserr);
   1660 		break;
   1661 	case WEBKIT_LOAD_FINISHED:
   1662 		seturiparameters(c, uri, loadfinished);
   1663 		updatehistory(uri, c->title);
   1664 		/* Disabled until we write some WebKitWebExtension for
   1665 		 * manipulating the DOM directly.
   1666 		evalscript(c, "document.documentElement.style.overflow = '%s'",
   1667 		    enablescrollbars ? "auto" : "hidden");
   1668 		*/
   1669 		runscript(c);
   1670 		break;
   1671 	}
   1672 	updatetitle(c);
   1673 }
   1674 
   1675 void
   1676 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
   1677 {
   1678 	c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
   1679 	              100;
   1680 	updatetitle(c);
   1681 }
   1682 
   1683 void
   1684 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
   1685 {
   1686 	c->title = webkit_web_view_get_title(c->view);
   1687 	updatetitle(c);
   1688 }
   1689 
   1690 void
   1691 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
   1692     Client *c)
   1693 {
   1694 	WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
   1695 
   1696 	/* Keep the hit test to know where is the pointer on the next click */
   1697 	c->mousepos = h;
   1698 
   1699 	if (hc & OnLink)
   1700 		c->targeturi = webkit_hit_test_result_get_link_uri(h);
   1701 	else if (hc & OnImg)
   1702 		c->targeturi = webkit_hit_test_result_get_image_uri(h);
   1703 	else if (hc & OnMedia)
   1704 		c->targeturi = webkit_hit_test_result_get_media_uri(h);
   1705 	else
   1706 		c->targeturi = NULL;
   1707 
   1708 	c->overtitle = c->targeturi;
   1709 	updatetitle(c);
   1710 }
   1711 
   1712 gboolean
   1713 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
   1714 {
   1715 	ParamName param = ParameterLast;
   1716 
   1717 	if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
   1718 		param = Geolocation;
   1719 	} else if (WEBKIT_IS_USER_MEDIA_PERMISSION_REQUEST(r)) {
   1720 		if (webkit_user_media_permission_is_for_audio_device(
   1721 		    WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
   1722 			param = AccessMicrophone;
   1723 		else if (webkit_user_media_permission_is_for_video_device(
   1724 		         WEBKIT_USER_MEDIA_PERMISSION_REQUEST(r)))
   1725 			param = AccessWebcam;
   1726 	} else {
   1727 		return FALSE;
   1728 	}
   1729 
   1730 	if (curconfig[param].val.i)
   1731 		webkit_permission_request_allow(r);
   1732 	else
   1733 		webkit_permission_request_deny(r);
   1734 
   1735 	return TRUE;
   1736 }
   1737 
   1738 gboolean
   1739 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
   1740     WebKitPolicyDecisionType dt, Client *c)
   1741 {
   1742 	switch (dt) {
   1743 	case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
   1744 		decidenavigation(d, c);
   1745 		break;
   1746 	case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
   1747 		decidenewwindow(d, c);
   1748 		break;
   1749 	case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
   1750 		decideresource(d, c);
   1751 		break;
   1752 	default:
   1753 		webkit_policy_decision_ignore(d);
   1754 		break;
   1755 	}
   1756 	return TRUE;
   1757 }
   1758 
   1759 void
   1760 decidenavigation(WebKitPolicyDecision *d, Client *c)
   1761 {
   1762 	WebKitNavigationAction *a =
   1763 	    webkit_navigation_policy_decision_get_navigation_action(
   1764 	    WEBKIT_NAVIGATION_POLICY_DECISION(d));
   1765 
   1766 	switch (webkit_navigation_action_get_navigation_type(a)) {
   1767 	case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
   1768 	case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
   1769 	case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
   1770 	case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
   1771 	case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
   1772 	case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
   1773 	default:
   1774 		/* Do not navigate to links with a "_blank" target (popup) */
   1775 		if (webkit_navigation_policy_decision_get_frame_name(
   1776 		    WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
   1777 			webkit_policy_decision_ignore(d);
   1778 		} else {
   1779 			/* Filter out navigation to different domain ? */
   1780 			/* get action→urirequest, copy and load in new window+view
   1781 			 * on Ctrl+Click ? */
   1782 			webkit_policy_decision_use(d);
   1783 		}
   1784 		break;
   1785 	}
   1786 }
   1787 
   1788 void
   1789 decidenewwindow(WebKitPolicyDecision *d, Client *c)
   1790 {
   1791 	Arg arg;
   1792 	WebKitNavigationAction *a =
   1793 	    webkit_navigation_policy_decision_get_navigation_action(
   1794 	    WEBKIT_NAVIGATION_POLICY_DECISION(d));
   1795 
   1796 
   1797 	switch (webkit_navigation_action_get_navigation_type(a)) {
   1798 	case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
   1799 	case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
   1800 	case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
   1801 	case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
   1802 	case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
   1803 		/* Filter domains here */
   1804 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
   1805  * test for link clicked but no button ? */
   1806 		arg.v = webkit_uri_request_get_uri(
   1807 		        webkit_navigation_action_get_request(a));
   1808 		newwindow(c, &arg, 0);
   1809 		break;
   1810 	case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
   1811 	default:
   1812 		break;
   1813 	}
   1814 
   1815 	webkit_policy_decision_ignore(d);
   1816 }
   1817 
   1818 void
   1819 decideresource(WebKitPolicyDecision *d, Client *c)
   1820 {
   1821 	int i, isascii = 1;
   1822 	WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
   1823 	WebKitURIResponse *res =
   1824 	    webkit_response_policy_decision_get_response(r);
   1825 	const gchar *uri = webkit_uri_response_get_uri(res);
   1826 
   1827 	if (g_str_has_suffix(uri, "/favicon.ico")) {
   1828 		webkit_policy_decision_ignore(d);
   1829 		return;
   1830 	}
   1831 
   1832 	if (!g_str_has_prefix(uri, "http://")
   1833 	    && !g_str_has_prefix(uri, "https://")
   1834 	    && !g_str_has_prefix(uri, "about:")
   1835 	    && !g_str_has_prefix(uri, "file://")
   1836 	    && !g_str_has_prefix(uri, "data:")
   1837 	    && !g_str_has_prefix(uri, "blob:")
   1838 	    && strlen(uri) > 0) {
   1839 		for (i = 0; i < strlen(uri); i++) {
   1840 			if (!g_ascii_isprint(uri[i])) {
   1841 				isascii = 0;
   1842 				break;
   1843 			}
   1844 		}
   1845 		if (isascii) {
   1846 			handleplumb(c, uri);
   1847 			webkit_policy_decision_ignore(d);
   1848 			return;
   1849 		}
   1850 	}
   1851 
   1852 	if (webkit_response_policy_decision_is_mime_type_supported(r)) {
   1853 		webkit_policy_decision_use(d);
   1854 	} else {
   1855 		webkit_policy_decision_ignore(d);
   1856 		download(c, res);
   1857 	}
   1858 }
   1859 
   1860 void
   1861 insecurecontent(WebKitWebView *v, WebKitInsecureContentEvent e, Client *c)
   1862 {
   1863 	c->insecure = 1;
   1864 }
   1865 
   1866 void
   1867 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
   1868 {
   1869 	g_signal_connect(G_OBJECT(d), "notify::response",
   1870 	                 G_CALLBACK(responsereceived), c);
   1871 }
   1872 
   1873 void
   1874 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
   1875 {
   1876 	download(c, webkit_download_get_response(d));
   1877 	webkit_download_cancel(d);
   1878 }
   1879 
   1880 void
   1881 download(Client *c, WebKitURIResponse *r)
   1882 {
   1883 	Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
   1884 	spawn(c, &a);
   1885 }
   1886 
   1887 void
   1888 webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
   1889                      Client *c)
   1890 {
   1891 	fprintf(stderr, "web process terminated: %s\n",
   1892 	        r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
   1893 	closeview(v, c);
   1894 }
   1895 
   1896 void
   1897 closeview(WebKitWebView *v, Client *c)
   1898 {
   1899 	gtk_widget_destroy(c->win);
   1900 }
   1901 
   1902 void
   1903 destroywin(GtkWidget* w, Client *c)
   1904 {
   1905 	destroyclient(c);
   1906 	if (!clients)
   1907 		gtk_main_quit();
   1908 }
   1909 
   1910 gchar *
   1911 parseuri(const gchar *uri) {
   1912 	guint i, havedot, havespace, reserved;
   1913 	havedot = havespace = reserved = 0;
   1914 
   1915 	for (i = 0; i < LENGTH(searchengines); i++) {
   1916 		if (searchengines[i].token == NULL || searchengines[i].uri == NULL ||
   1917 		    *(uri + strlen(searchengines[i].token)) != ' ')
   1918 			continue;
   1919 		if (g_str_has_prefix(uri, searchengines[i].token))
   1920 			return g_strdup_printf(searchengines[i].uri,
   1921 					       uri + strlen(searchengines[i].token) + 1);
   1922 	}
   1923 
   1924 	for (i = 0; i < strlen(uri); ++i)
   1925 		if (uri[i] == '.') havedot = 1;
   1926 		else if (uri[i] == ' ') havespace = 1;
   1927 
   1928 	for (i = 0; i < LENGTH(reservednames); ++i)
   1929 		if (!strcmp(reservednames[i], uri))
   1930 				reserved = 1;
   1931 
   1932 	if (!reserved && (!havedot || havespace))
   1933 		return g_strdup_printf(searchengines[0].uri,
   1934 					   uri);
   1935 
   1936 	return g_strdup_printf("http://%s", uri);
   1937 }
   1938 
   1939 void
   1940 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
   1941 {
   1942 	Arg a = {.v = text };
   1943 	if (text)
   1944 		loaduri((Client *) d, &a);
   1945 }
   1946 
   1947 void
   1948 reload(Client *c, const Arg *a)
   1949 {
   1950 	if (a->i)
   1951 		webkit_web_view_reload_bypass_cache(c->view);
   1952 	else
   1953 		webkit_web_view_reload(c->view);
   1954 }
   1955 
   1956 void
   1957 print(Client *c, const Arg *a)
   1958 {
   1959 	webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
   1960 	                                  GTK_WINDOW(c->win));
   1961 }
   1962 
   1963 void
   1964 showcert(Client *c, const Arg *a)
   1965 {
   1966 	GTlsCertificate *cert = c->failedcert ? c->failedcert : c->cert;
   1967 	GcrCertificate *gcrt;
   1968 	GByteArray *crt;
   1969 	GtkWidget *win;
   1970 	GcrCertificateWidget *wcert;
   1971 
   1972 	if (!cert)
   1973 		return;
   1974 
   1975 	g_object_get(cert, "certificate", &crt, NULL);
   1976 	gcrt = gcr_simple_certificate_new(crt->data, crt->len);
   1977 	g_byte_array_unref(crt);
   1978 
   1979 	win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   1980 	wcert = gcr_certificate_widget_new(gcrt);
   1981 	g_object_unref(gcrt);
   1982 
   1983 	gtk_container_add(GTK_CONTAINER(win), GTK_WIDGET(wcert));
   1984 	gtk_widget_show_all(win);
   1985 }
   1986 
   1987 void
   1988 clipboard(Client *c, const Arg *a)
   1989 {
   1990 	/* User defined choice of selection, see config.h */
   1991 	GdkAtom	selection = GDK_SELECTION_PRIMARY;
   1992 	if (curconfig[ClipboardNotPrimary].val.i > 0)
   1993 		selection = GDK_SELECTION_CLIPBOARD;
   1994 
   1995 	if (a->i) { /* load clipboard uri */
   1996 		gtk_clipboard_request_text(gtk_clipboard_get(
   1997                                           selection),
   1998 		                           pasteuri, c);
   1999 	} else { /* copy uri */
   2000 		gtk_clipboard_set_text(gtk_clipboard_get(
   2001 		                       selection), c->targeturi
   2002 		                       ? c->targeturi : geturi(c), -1);
   2003 	}
   2004 }
   2005 
   2006 void
   2007 zoom(Client *c, const Arg *a)
   2008 {
   2009 	if (a->i > 0)
   2010 		webkit_web_view_set_zoom_level(c->view,
   2011 		                               curconfig[ZoomLevel].val.f + 0.1);
   2012 	else if (a->i < 0)
   2013 		webkit_web_view_set_zoom_level(c->view,
   2014 		                               curconfig[ZoomLevel].val.f - 0.1);
   2015 	else
   2016 		webkit_web_view_set_zoom_level(c->view, 1.0);
   2017 
   2018 	curconfig[ZoomLevel].val.f = webkit_web_view_get_zoom_level(c->view);
   2019 }
   2020 
   2021 static void
   2022 msgext(Client *c, char type, const Arg *a)
   2023 {
   2024 	static char msg[MSGBUFSZ];
   2025 	int ret;
   2026 
   2027 	if (spair[0] < 0)
   2028 		return;
   2029 
   2030 	if ((ret = snprintf(msg, sizeof(msg), "%c%c%c", c->pageid, type, a->i))
   2031 	    >= sizeof(msg)) {
   2032 		fprintf(stderr, "surf: message too long: %d\n", ret);
   2033 		return;
   2034 	}
   2035 
   2036 	if (send(spair[0], msg, ret, 0) != ret)
   2037 		fprintf(stderr, "surf: error sending: %u%c%d (%d)\n",
   2038 		        c->pageid, type, a->i, ret);
   2039 }
   2040 
   2041 void
   2042 scrollv(Client *c, const Arg *a)
   2043 {
   2044 	msgext(c, 'v', a);
   2045 }
   2046 
   2047 void
   2048 scrollh(Client *c, const Arg *a)
   2049 {
   2050 	msgext(c, 'h', a);
   2051 }
   2052 
   2053 void
   2054 navigate(Client *c, const Arg *a)
   2055 {
   2056 	if (a->i < 0)
   2057 		webkit_web_view_go_back(c->view);
   2058 	else if (a->i > 0)
   2059 		webkit_web_view_go_forward(c->view);
   2060 }
   2061 
   2062 void
   2063 stop(Client *c, const Arg *a)
   2064 {
   2065 	webkit_web_view_stop_loading(c->view);
   2066 }
   2067 
   2068 void
   2069 toggle(Client *c, const Arg *a)
   2070 {
   2071 	curconfig[a->i].val.i ^= 1;
   2072 	setparameter(c, 1, (ParamName)a->i, &curconfig[a->i].val);
   2073 }
   2074 
   2075 void
   2076 togglefullscreen(Client *c, const Arg *a)
   2077 {
   2078 	/* toggling value is handled in winevent() */
   2079 	if (c->fullscreen)
   2080 		gtk_window_unfullscreen(GTK_WINDOW(c->win));
   2081 	else
   2082 		gtk_window_fullscreen(GTK_WINDOW(c->win));
   2083 }
   2084 
   2085 void
   2086 togglecookiepolicy(Client *c, const Arg *a)
   2087 {
   2088 	++cookiepolicy;
   2089 	cookiepolicy %= strlen(curconfig[CookiePolicies].val.v);
   2090 
   2091 	setparameter(c, 0, CookiePolicies, NULL);
   2092 }
   2093 
   2094 void
   2095 toggleinspector(Client *c, const Arg *a)
   2096 {
   2097 	if (webkit_web_inspector_is_attached(c->inspector))
   2098 		webkit_web_inspector_close(c->inspector);
   2099 	else if (curconfig[Inspector].val.i)
   2100 		webkit_web_inspector_show(c->inspector);
   2101 }
   2102 
   2103 void
   2104 find(Client *c, const Arg *a)
   2105 {
   2106 	const char *s, *f;
   2107 
   2108 	if (a && a->i) {
   2109 		if (a->i > 0)
   2110 			webkit_find_controller_search_next(c->finder);
   2111 		else
   2112 			webkit_find_controller_search_previous(c->finder);
   2113 	} else {
   2114 		s = getatom(c, AtomFind);
   2115 		f = webkit_find_controller_get_search_text(c->finder);
   2116 
   2117 		if (g_strcmp0(f, s) == 0) /* reset search */
   2118 			webkit_find_controller_search(c->finder, "", findopts,
   2119 			                              G_MAXUINT);
   2120 
   2121 		webkit_find_controller_search(c->finder, s, findopts,
   2122 		                              G_MAXUINT);
   2123 
   2124 		if (strcmp(s, "") == 0)
   2125 			webkit_find_controller_search_finish(c->finder);
   2126 	}
   2127 }
   2128 
   2129 void
   2130 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
   2131 {
   2132 	navigate(c, a);
   2133 }
   2134 
   2135 void
   2136 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
   2137 {
   2138 	Arg arg;
   2139 
   2140 	arg.v = webkit_hit_test_result_get_link_uri(h);
   2141 	newwindow(c, &arg, a->i);
   2142 }
   2143 
   2144 void
   2145 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
   2146 {
   2147 	Arg arg;
   2148 
   2149 	arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
   2150 	spawn(c, &arg);
   2151 }
   2152 
   2153 int
   2154 main(int argc, char *argv[])
   2155 {
   2156 	Arg arg;
   2157 	Client *c;
   2158 
   2159 	memset(&arg, 0, sizeof(arg));
   2160 
   2161 	/* command line args */
   2162 	ARGBEGIN {
   2163 	case 'a':
   2164 		defconfig[CookiePolicies].val.v = EARGF(usage());
   2165 		defconfig[CookiePolicies].prio = 2;
   2166 		break;
   2167 	case 'b':
   2168 		defconfig[ScrollBars].val.i = 0;
   2169 		defconfig[ScrollBars].prio = 2;
   2170 		break;
   2171 	case 'B':
   2172 		defconfig[ScrollBars].val.i = 1;
   2173 		defconfig[ScrollBars].prio = 2;
   2174 		break;
   2175 	case 'c':
   2176 		cookiefile = EARGF(usage());
   2177 		break;
   2178 	case 'C':
   2179 		stylefile = EARGF(usage());
   2180 		break;
   2181 	case 'd':
   2182 		defconfig[DiskCache].val.i = 0;
   2183 		defconfig[DiskCache].prio = 2;
   2184 		break;
   2185 	case 'D':
   2186 		defconfig[DiskCache].val.i = 1;
   2187 		defconfig[DiskCache].prio = 2;
   2188 		break;
   2189 	case 'e':
   2190 		embed = strtol(EARGF(usage()), NULL, 0);
   2191 		break;
   2192 	case 'f':
   2193 		defconfig[RunInFullscreen].val.i = 0;
   2194 		defconfig[RunInFullscreen].prio = 2;
   2195 		break;
   2196 	case 'F':
   2197 		defconfig[RunInFullscreen].val.i = 1;
   2198 		defconfig[RunInFullscreen].prio = 2;
   2199 		break;
   2200 	case 'g':
   2201 		defconfig[Geolocation].val.i = 0;
   2202 		defconfig[Geolocation].prio = 2;
   2203 		break;
   2204 	case 'G':
   2205 		defconfig[Geolocation].val.i = 1;
   2206 		defconfig[Geolocation].prio = 2;
   2207 		break;
   2208 	case 'h':
   2209 		startgo = 1;
   2210 		break;
   2211 	case 'i':
   2212 		defconfig[LoadImages].val.i = 0;
   2213 		defconfig[LoadImages].prio = 2;
   2214 		break;
   2215 	case 'I':
   2216 		defconfig[LoadImages].val.i = 1;
   2217 		defconfig[LoadImages].prio = 2;
   2218 		break;
   2219 	case 'k':
   2220 		defconfig[KioskMode].val.i = 0;
   2221 		defconfig[KioskMode].prio = 2;
   2222 		break;
   2223 	case 'K':
   2224 		defconfig[KioskMode].val.i = 1;
   2225 		defconfig[KioskMode].prio = 2;
   2226 		break;
   2227 	case 'm':
   2228 		defconfig[Style].val.i = 0;
   2229 		defconfig[Style].prio = 2;
   2230 		break;
   2231 	case 'M':
   2232 		defconfig[Style].val.i = 1;
   2233 		defconfig[Style].prio = 2;
   2234 		break;
   2235 	case 'n':
   2236 		defconfig[Inspector].val.i = 0;
   2237 		defconfig[Inspector].prio = 2;
   2238 		break;
   2239 	case 'N':
   2240 		defconfig[Inspector].val.i = 1;
   2241 		defconfig[Inspector].prio = 2;
   2242 		break;
   2243 	case 'p':
   2244 		defconfig[Plugins].val.i = 0;
   2245 		defconfig[Plugins].prio = 2;
   2246 		break;
   2247 	case 'P':
   2248 		defconfig[Plugins].val.i = 1;
   2249 		defconfig[Plugins].prio = 2;
   2250 		break;
   2251 	case 'r':
   2252 		scriptfile = EARGF(usage());
   2253 		break;
   2254 	case 's':
   2255 		defconfig[JavaScript].val.i = 0;
   2256 		defconfig[JavaScript].prio = 2;
   2257 		break;
   2258 	case 'S':
   2259 		defconfig[JavaScript].val.i = 1;
   2260 		defconfig[JavaScript].prio = 2;
   2261 		break;
   2262 	case 't':
   2263 		defconfig[StrictTLS].val.i = 0;
   2264 		defconfig[StrictTLS].prio = 2;
   2265 		break;
   2266 	case 'T':
   2267 		defconfig[StrictTLS].val.i = 1;
   2268 		defconfig[StrictTLS].prio = 2;
   2269 		break;
   2270 	case 'u':
   2271 		fulluseragent = EARGF(usage());
   2272 		break;
   2273 	case 'v':
   2274 		die("surf-"VERSION", see LICENSE for © details\n");
   2275 	case 'w':
   2276 		showxid = 1;
   2277 		break;
   2278 	case 'x':
   2279 		defconfig[Certificate].val.i = 0;
   2280 		defconfig[Certificate].prio = 2;
   2281 		break;
   2282 	case 'X':
   2283 		defconfig[Certificate].val.i = 1;
   2284 		defconfig[Certificate].prio = 2;
   2285 		break;
   2286 	case 'z':
   2287 		defconfig[ZoomLevel].val.f = strtof(EARGF(usage()), NULL);
   2288 		defconfig[ZoomLevel].prio = 2;
   2289 		break;
   2290 	default:
   2291 		usage();
   2292 	} ARGEND;
   2293 	if (argc > 0)
   2294 		arg.v = argv[0];
   2295 	else
   2296 #ifdef HOMEPAGE
   2297 		arg.v = HOMEPAGE;
   2298 #else
   2299 		arg.v = "about:blank";
   2300 #endif
   2301 
   2302 	setup();
   2303 	c = newclient(NULL);
   2304 	showview(NULL, c);
   2305 
   2306 	struct sigaction sa;
   2307 	sa.sa_handler = sigusr1;
   2308 	sigemptyset(&sa.sa_mask);
   2309 	sa.sa_flags = SA_RESTART;
   2310 	sigaction(SIGUSR1, &sa, NULL);
   2311 
   2312 	loaduri(c, &arg);
   2313 	updatetitle(c);
   2314 
   2315 	if (startgo) {
   2316 		/* start directly into GO prompt */
   2317 		Arg a = (Arg)GO("_SURF_URI", "_SURF_GO", PROMPT_GO);
   2318 		spawn(c, &a);
   2319 	}
   2320 
   2321 	gtk_main();
   2322 	cleanup();
   2323 
   2324 	return 0;
   2325 }