Simplify handling of c->title
[surf.git] / surf.c
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/types.h>
7 #include <sys/wait.h>
8 #include <libgen.h>
9 #include <limits.h>
10 #include <pwd.h>
11 #include <regex.h>
12 #include <signal.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include <gdk/gdk.h>
20 #include <gdk/gdkkeysyms.h>
21 #include <gdk/gdkx.h>
22 #include <glib/gstdio.h>
23 #include <gtk/gtk.h>
24 #include <gtk/gtkx.h>
25 #include <JavaScriptCore/JavaScript.h>
26 #include <webkit2/webkit2.h>
27 #include <X11/X.h>
28 #include <X11/Xatom.h>
29
30 #include "arg.h"
31
32 #define LENGTH(x)               (sizeof(x) / sizeof(x[0]))
33 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
34
35 enum { AtomFind, AtomGo, AtomUri, AtomLast };
36
37 enum {
38         CaretBrowsing,
39         FrameFlattening,
40         Geolocation,
41         JavaScript,
42         LoadImages,
43         Plugins,
44         ScrollBars,
45 };
46
47 enum {
48         OnDoc   = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
49         OnLink  = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
50         OnImg   = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
51         OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
52         OnEdit  = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
53         OnBar   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
54         OnSel   = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
55         OnAny   = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
56 };
57
58 typedef union {
59         int b;
60         int i;
61         const void *v;
62 } Arg;
63
64 typedef struct Client {
65         GtkWidget *win;
66         WebKitWebView *view;
67         WebKitWebInspector *inspector;
68         WebKitFindController *finder;
69         WebKitHitTestResult *mousepos;
70         GTlsCertificateFlags tlsflags;
71         Window xid;
72         int progress, fullscreen;
73         const char *title, *targeturi;
74         const char *needle;
75         struct Client *next;
76 } Client;
77
78 typedef struct {
79         guint mod;
80         guint keyval;
81         void (*func)(Client *c, const Arg *a);
82         const Arg arg;
83 } Key;
84
85 typedef struct {
86         unsigned int target;
87         unsigned int mask;
88         guint button;
89         void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
90         const Arg arg;
91         unsigned int stopevent;
92 } Button;
93
94 typedef struct {
95         char *regex;
96         char *style;
97         regex_t re;
98 } SiteStyle;
99
100 /* Surf */
101 static void usage(void);
102 static void die(const char *errstr, ...);
103 static void setup(void);
104 static void sigchld(int unused);
105 static char *buildfile(const char *path);
106 static char *buildpath(const char *path);
107 static Client *newclient(Client *c);
108 static void addaccelgroup(Client *c);
109 static void loaduri(Client *c, const Arg *a);
110 static const char *geturi(Client *c);
111 static void setatom(Client *c, int a, const char *v);
112 static const char *getatom(Client *c, int a);
113 static void updatetitle(Client *c);
114 static void gettogglestats(Client *c);
115 static void getpagestats(Client *c);
116 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
117 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
118 static const char *getstyle(const char *uri);
119 static void setstyle(Client *c, const char *stylefile);
120 static void runscript(Client *c);
121 static void evalscript(Client *c, const char *jsstr, ...);
122 static void updatewinid(Client *c);
123 static void handleplumb(Client *c, const char *uri);
124 static void newwindow(Client *c, const Arg *a, int noembed);
125 static void spawn(Client *c, const Arg *a);
126 static void destroyclient(Client *c);
127 static void cleanup(void);
128
129 /* GTK/WebKit */
130 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
131 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
132                              Client *c);
133 static gboolean buttonreleased(GtkWidget *w, GdkEvent *e, Client *c);
134 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
135                          GdkModifierType mods, Client *c);
136 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
137                                 gpointer d);
138 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
139 static void showview(WebKitWebView *v, Client *c);
140 static GtkWidget *createwindow(Client *c);
141 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
142 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
143 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
144 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
145                                guint modifiers, Client *c);
146 static gboolean permissionrequested(WebKitWebView *v,
147                                     WebKitPermissionRequest *r, Client *c);
148 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
149                              WebKitPolicyDecisionType dt, Client *c);
150 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
151 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
152 static void decideresource(WebKitPolicyDecision *d, Client *c);
153 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
154                             Client *c);
155 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
156 static void download(Client *c, WebKitURIResponse *r);
157 static void closeview(WebKitWebView *v, Client *c);
158 static void destroywin(GtkWidget* w, Client *c);
159
160 /* Hotkeys */
161 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
162 static void reload(Client *c, const Arg *a);
163 static void print(Client *c, const Arg *a);
164 static void clipboard(Client *c, const Arg *a);
165 static void zoom(Client *c, const Arg *a);
166 static void scroll_v(Client *c, const Arg *a);
167 static void scroll_h(Client *c, const Arg *a);
168 static void navigate(Client *c, const Arg *a);
169 static void stop(Client *c, const Arg *a);
170 static void toggle(Client *c, const Arg *a);
171 static void togglefullscreen(Client *c, const Arg *a);
172 static void togglecookiepolicy(Client *c, const Arg *a);
173 static void togglestyle(Client *c, const Arg *a);
174 static void toggleinspector(Client *c, const Arg *a);
175 static void find(Client *c, const Arg *a);
176
177 /* Buttons */
178 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
179 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
180 static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
181
182 static char winid[64];
183 static char togglestats[10];
184 static char pagestats[2];
185 static Atom atoms[AtomLast];
186 static Window embed;
187 static int showxid;
188 static int cookiepolicy;
189 static Display *dpy;
190 static Client *clients;
191 static char *stylefile;
192 static const char *useragent;
193 char *argv0;
194
195 /* configuration, allows nested code to access above variables */
196 #include "config.h"
197
198 void
199 usage(void)
200 {
201         die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
202             "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
203             "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
204 }
205
206 void
207 die(const char *errstr, ...)
208 {
209         va_list ap;
210
211         va_start(ap, errstr);
212         vfprintf(stderr, errstr, ap);
213         va_end(ap);
214         exit(1);
215 }
216
217 void
218 setup(void)
219 {
220         int i;
221
222         /* clean up any zombies immediately */
223         sigchld(0);
224         gtk_init(NULL, NULL);
225
226         dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
227
228         /* atoms */
229         atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
230         atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
231         atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
232
233         /* dirs and files */
234         cookiefile = buildfile(cookiefile);
235         scriptfile = buildfile(scriptfile);
236         cachedir   = buildpath(cachedir);
237
238         if (!stylefile) {
239                 styledir = buildpath(styledir);
240                 for (i = 0; i < LENGTH(styles); ++i) {
241                         if (regcomp(&(styles[i].re), styles[i].regex,
242                             REG_EXTENDED)) {
243                                 fprintf(stderr,
244                                         "Could not compile regex: %s\n",
245                                         styles[i].regex);
246                                 styles[i].regex = NULL;
247                         }
248                         styles[i].style = g_strconcat(styledir, "/",
249                                                       styles[i].style, NULL);
250                 }
251                 g_free(styledir);
252         } else {
253                 stylefile = buildfile(stylefile);
254         }
255 }
256
257 void
258 sigchld(int unused)
259 {
260         if (signal(SIGCHLD, sigchld) == SIG_ERR)
261                 die("Can't install SIGCHLD handler");
262         while (waitpid(-1, NULL, WNOHANG) > 0)
263                 ;
264 }
265
266 char *
267 buildfile(const char *path)
268 {
269         char *dname, *bname, *bpath, *fpath;
270         FILE *f;
271
272         dname = g_path_get_dirname(path);
273         bname = g_path_get_basename(path);
274
275         bpath = buildpath(dname);
276         g_free(dname);
277
278         fpath = g_build_filename(bpath, bname, NULL);
279         g_free(bpath);
280         g_free(bname);
281
282         if (!(f = fopen(fpath, "a")))
283                 die("Could not open file: %s\n", fpath);
284
285         g_chmod(fpath, 0600); /* always */
286         fclose(f);
287
288         return fpath;
289 }
290
291 char *
292 buildpath(const char *path)
293 {
294         struct passwd *pw;
295         char *apath, *name, *p, *fpath;
296
297         if (path[0] == '~') {
298                 if (path[1] == '/' || path[1] == '\0') {
299                         p = (char *)&path[1];
300                         pw = getpwuid(getuid());
301                 } else {
302                         if ((p = strchr(path, '/')))
303                                 name = g_strndup(&path[1], --p - path);
304                         else
305                                 name = g_strdup(&path[1]);
306
307                         if (!(pw = getpwnam(name))) {
308                                 die("Can't get user %s home directory: %s.\n",
309                                     name, path);
310                         }
311                         g_free(name);
312                 }
313                 apath = g_build_filename(pw->pw_dir, p, NULL);
314         } else {
315                 apath = g_strdup(path);
316         }
317
318         /* creating directory */
319         if (g_mkdir_with_parents(apath, 0700) < 0)
320                 die("Could not access directory: %s\n", apath);
321
322         fpath = realpath(apath, NULL);
323         g_free(apath);
324
325         return fpath;
326 }
327
328 Client *
329 newclient(Client *rc)
330 {
331         Client *c;
332
333         if (!(c = calloc(1, sizeof(Client))))
334                 die("Cannot malloc!\n");
335
336         c->next = clients;
337         clients = c;
338
339         c->progress = 100;
340         c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
341         c->view = newview(c, rc ? rc->view : NULL);
342
343         return c;
344 }
345
346 void
347 addaccelgroup(Client *c)
348 {
349         int i;
350         GtkAccelGroup *group = gtk_accel_group_new();
351         GClosure *closure;
352
353         for (i = 0; i < LENGTH(keys); i++) {
354                 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
355                 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
356                                         closure);
357         }
358         gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
359 }
360
361 void
362 loaduri(Client *c, const Arg *a)
363 {
364         struct stat st;
365         char *url, *path;
366         const char *uri = a->v;
367
368         if (g_strcmp0(uri, "") == 0)
369                 return;
370
371         if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
372                 url = g_strdup(uri);
373         } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
374                 url = g_strdup_printf("file://%s", path);
375                 free(path);
376         } else {
377                 url = g_strdup_printf("http://%s", uri);
378         }
379
380         setatom(c, AtomUri, url);
381
382         if (strcmp(url, geturi(c)) == 0) {
383                 reload(c, a);
384         } else {
385                 webkit_web_view_load_uri(c->view, url);
386                 c->title = geturi(c);
387                 updatetitle(c);
388         }
389
390         g_free(url);
391 }
392
393 const char *
394 geturi(Client *c)
395 {
396         const char *uri;
397
398         if (!(uri = webkit_web_view_get_uri(c->view)))
399                 uri = "about:blank";
400         return uri;
401 }
402
403 void
404 setatom(Client *c, int a, const char *v)
405 {
406         XSync(dpy, False);
407         XChangeProperty(dpy, c->xid,
408                         atoms[a], XA_STRING, 8, PropModeReplace,
409                         (unsigned char *)v, strlen(v) + 1);
410 }
411
412 const char *
413 getatom(Client *c, int a)
414 {
415         static char buf[BUFSIZ];
416         Atom adummy;
417         int idummy;
418         unsigned long ldummy;
419         unsigned char *p = NULL;
420
421         XGetWindowProperty(dpy, c->xid, atoms[a], 0L, BUFSIZ, False, XA_STRING,
422                            &adummy, &idummy, &ldummy, &ldummy, &p);
423         if (p)
424                 strncpy(buf, (char *)p, LENGTH(buf) - 1);
425         else
426                 buf[0] = '\0';
427         XFree(p);
428
429         return buf;
430 }
431
432 void
433 updatetitle(Client *c)
434 {
435         char *title;
436         const char *name = c->targeturi ? c->targeturi :
437                            c->title ? c->title : "";
438
439         if (showindicators) {
440                 gettogglestats(c);
441                 getpagestats(c);
442
443                 if (c->progress != 100)
444                         title = g_strdup_printf("[%i%%] %s:%s | %s",
445                                 c->progress, togglestats, pagestats, name);
446                 else
447                         title = g_strdup_printf("%s:%s | %s",
448                                 togglestats, pagestats, name);
449
450                 gtk_window_set_title(GTK_WINDOW(c->win), title);
451                 g_free(title);
452         } else {
453                 gtk_window_set_title(GTK_WINDOW(c->win), name);
454         }
455 }
456
457 void
458 gettogglestats(Client *c)
459 {
460         togglestats[0] = cookiepolicy_set(cookiepolicy_get());
461         togglestats[1] = enablecaretbrowsing ?   'C' : 'c';
462         togglestats[2] = allowgeolocation ?      'G' : 'g';
463         togglestats[3] = enablecache ?           'D' : 'd';
464         togglestats[4] = loadimages ?            'I' : 'i';
465         togglestats[5] = enablescripts ?         'S' : 's';
466         togglestats[6] = enableplugins ?         'V' : 'v';
467         togglestats[7] = enablestyle ?           'M' : 'm';
468         togglestats[8] = enableframeflattening ? 'F' : 'f';
469         togglestats[9] = '\0';
470 }
471
472 void
473 getpagestats(Client *c)
474 {
475         pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
476                        c->tlsflags > 0 ? 'U' : 'T';
477         pagestats[1] = '\0';
478 }
479
480 WebKitCookieAcceptPolicy
481 cookiepolicy_get(void)
482 {
483         switch (cookiepolicies[cookiepolicy]) {
484         case 'a':
485                 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
486         case '@':
487                 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
488         case 'A':
489         default:
490                 break;
491         }
492
493         return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
494 }
495
496 char
497 cookiepolicy_set(const WebKitCookieAcceptPolicy p)
498 {
499         switch (p) {
500         case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
501                 return 'a';
502         case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
503                 return '@';
504         case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
505         default:
506                 break;
507         }
508
509         return 'A';
510 }
511
512 const char *
513 getstyle(const char *uri)
514 {
515         int i;
516
517         if (stylefile)
518                 return stylefile;
519
520         for (i = 0; i < LENGTH(styles); ++i) {
521                 if (styles[i].regex &&
522                     !regexec(&(styles[i].re), uri, 0, NULL, 0))
523                         return styles[i].style;
524         }
525
526         return "";
527 }
528
529 void
530 setstyle(Client *c, const char *stylefile)
531 {
532         gchar *style;
533
534         if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
535                 fprintf(stderr, "Could not read style file: %s\n", stylefile);
536                 return;
537         }
538
539         webkit_user_content_manager_add_style_sheet(
540             webkit_web_view_get_user_content_manager(c->view),
541             webkit_user_style_sheet_new(style,
542             WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
543             WEBKIT_USER_STYLE_LEVEL_USER,
544             NULL, NULL));
545
546         g_free(style);
547 }
548
549 void
550 runscript(Client *c)
551 {
552         gchar *script;
553         gsize l;
554
555         if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
556                 evalscript(c, script);
557         g_free(script);
558 }
559
560 void
561 evalscript(Client *c, const char *jsstr, ...)
562 {
563         va_list ap;
564         gchar *script;
565
566         va_start(ap, jsstr);
567         script = g_strdup_vprintf(jsstr, ap);
568         va_end(ap);
569
570         webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
571         g_free(script);
572 }
573
574 void
575 updatewinid(Client *c)
576 {
577         snprintf(winid, LENGTH(winid), "%lu", c->xid);
578 }
579
580 void
581 handleplumb(Client *c, const char *uri)
582 {
583         Arg a = (Arg)PLUMB(uri);
584         spawn(c, &a);
585 }
586
587 void
588 newwindow(Client *c, const Arg *a, int noembed)
589 {
590         int i = 0;
591         char tmp[64];
592         const char *cmd[26], *uri;
593         const Arg arg = { .v = cmd };
594
595         cmd[i++] = argv0;
596         cmd[i++] = "-a";
597         cmd[i++] = cookiepolicies;
598         cmd[i++] = enablescrollbars ? "-B" : "-b";
599         if (cookiefile && g_strcmp0(cookiefile, "")) {
600                 cmd[i++] = "-c";
601                 cmd[i++] = cookiefile;
602         }
603         cmd[i++] = enablecache ? "-D" : "-d";
604         if (embed && !noembed) {
605                 cmd[i++] = "-e";
606                 snprintf(tmp, LENGTH(tmp), "%lu", embed);
607                 cmd[i++] = tmp;
608         }
609         cmd[i++] = runinfullscreen ? "-F" : "-f";
610         cmd[i++] = allowgeolocation ? "-G" : "-g";
611         cmd[i++] = loadimages ? "-I" : "-i";
612         cmd[i++] = kioskmode ? "-K" : "-k";
613         cmd[i++] = enablestyle ? "-M" : "-m";
614         cmd[i++] = enableinspector ? "-N" : "-n";
615         cmd[i++] = enableplugins ? "-P" : "-p";
616         if (scriptfile && g_strcmp0(scriptfile, "")) {
617                 cmd[i++] = "-r";
618                 cmd[i++] = scriptfile;
619         }
620         cmd[i++] = enablescripts ? "-S" : "-s";
621         if (stylefile && g_strcmp0(stylefile, "")) {
622                 cmd[i++] = "-t";
623                 cmd[i++] = stylefile;
624         }
625         if (fulluseragent && g_strcmp0(fulluseragent, "")) {
626                 cmd[i++] = "-u";
627                 cmd[i++] = fulluseragent;
628         }
629         if (showxid)
630                 cmd[i++] = "-x";
631         /* do not keep zoom level */
632         cmd[i++] = "--";
633         if ((uri = a->v))
634                 cmd[i++] = uri;
635         cmd[i] = NULL;
636
637         spawn(c, &arg);
638 }
639
640 void
641 spawn(Client *c, const Arg *a)
642 {
643         if (fork() == 0) {
644                 if (dpy)
645                         close(ConnectionNumber(dpy));
646                 setsid();
647                 execvp(((char **)a->v)[0], (char **)a->v);
648                 fprintf(stderr, "surf: execvp %s", ((char **)a->v)[0]);
649                 perror(" failed");
650                 exit(0);
651         }
652 }
653
654 void
655 destroyclient(Client *c)
656 {
657         Client *p;
658
659         webkit_web_view_stop_loading(c->view);
660         /* Not needed, has already been called
661         gtk_widget_destroy(c->win);
662          */
663
664         for (p = clients; p && p->next != c; p = p->next)
665                 ;
666         if (p)
667                 p->next = c->next;
668         else
669                 clients = c->next;
670         free(c);
671 }
672
673 void
674 cleanup(void)
675 {
676         while (clients)
677                 destroyclient(clients);
678         g_free(cookiefile);
679         g_free(scriptfile);
680         g_free(stylefile);
681         g_free(cachedir);
682 }
683
684 WebKitWebView *
685 newview(Client *c, WebKitWebView *rv)
686 {
687         WebKitWebView *v;
688         WebKitSettings *settings;
689         WebKitUserContentManager *contentmanager;
690         WebKitWebContext *context;
691
692         /* Webview */
693         if (rv) {
694                 v = WEBKIT_WEB_VIEW(
695                     webkit_web_view_new_with_related_view(rv));
696         } else {
697                 settings = webkit_settings_new_with_settings(
698                            "auto-load-images", loadimages,
699                            "default-font-size", defaultfontsize,
700                            "enable-caret-browsing", enablecaretbrowsing,
701                            "enable-developer-extras", enableinspector,
702                            "enable-dns-prefetching", enablednsprefetching,
703                            "enable-frame-flattening", enableframeflattening,
704                            "enable-html5-database", enablecache,
705                            "enable-html5-local-storage", enablecache,
706                            "enable-javascript", enablescripts,
707                            "enable-plugins", enableplugins,
708                            NULL);
709 /* For mor interesting settings, have a look at
710  * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
711
712                 if (strcmp(fulluseragent, "")) {
713                         webkit_settings_set_user_agent(settings, fulluseragent);
714                 } else if (surfuseragent) {
715                         webkit_settings_set_user_agent_with_application_details(
716                             settings, "Surf", VERSION);
717                 }
718                 useragent = webkit_settings_get_user_agent(settings);
719
720                 contentmanager = webkit_user_content_manager_new();
721
722                 context = webkit_web_context_new_with_website_data_manager(
723                           webkit_website_data_manager_new(
724                           "base-cache-directory", cachedir,
725                           "base-data-directory", cachedir,
726                           NULL));
727
728                 /* rendering process model, can be a shared unique one
729                  * or one for each view */
730                 webkit_web_context_set_process_model(context,
731                     WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
732                 /* ssl */
733                 webkit_web_context_set_tls_errors_policy(context, strictssl ?
734                     WEBKIT_TLS_ERRORS_POLICY_FAIL :
735                     WEBKIT_TLS_ERRORS_POLICY_IGNORE);
736                 /* disk cache */
737                 webkit_web_context_set_cache_model(context, enablecache ?
738                     WEBKIT_CACHE_MODEL_WEB_BROWSER :
739                     WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
740
741                 /* Currently only works with text file to be compatible with curl */
742                 webkit_cookie_manager_set_persistent_storage(
743                     webkit_web_context_get_cookie_manager(context), cookiefile,
744                     WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
745                 /* cookie policy */
746                 webkit_cookie_manager_set_accept_policy(
747                     webkit_web_context_get_cookie_manager(context),
748                     cookiepolicy_get());
749
750                 g_signal_connect(G_OBJECT(context), "download-started",
751                                  G_CALLBACK(downloadstarted), c);
752
753                 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
754                     "settings", settings,
755                     "user-content-manager", contentmanager,
756                     "web-context", context,
757                     NULL);
758         }
759
760         g_signal_connect(G_OBJECT(v), "notify::title",
761                          G_CALLBACK(titlechanged), c);
762         g_signal_connect(G_OBJECT(v), "mouse-target-changed",
763                          G_CALLBACK(mousetargetchanged), c);
764         g_signal_connect(G_OBJECT(v), "permission-request",
765                          G_CALLBACK(permissionrequested), c);
766         g_signal_connect(G_OBJECT(v), "create",
767                          G_CALLBACK(createview), c);
768         g_signal_connect(G_OBJECT(v), "ready-to-show",
769                          G_CALLBACK(showview), c);
770         g_signal_connect(G_OBJECT(v), "decide-policy",
771                          G_CALLBACK(decidepolicy), c);
772         g_signal_connect(G_OBJECT(v), "load-changed",
773                          G_CALLBACK(loadchanged), c);
774         g_signal_connect(G_OBJECT(v), "notify::estimated-load-progress",
775                          G_CALLBACK(progresschanged), c);
776         g_signal_connect(G_OBJECT(v), "button-release-event",
777                          G_CALLBACK(buttonreleased), c);
778         g_signal_connect(G_OBJECT(v), "close",
779                         G_CALLBACK(closeview), c);
780
781         return v;
782 }
783
784 GtkWidget *
785 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
786 {
787         Client *n;
788
789         switch (webkit_navigation_action_get_navigation_type(a)) {
790         case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
791                 /*
792                  * popup windows of type “other” are almost always triggered
793                  * by user gesture, so inverse the logic here
794                  */
795 /* instead of this, compare destination uri to mouse-over uri for validating window */
796                 if (webkit_navigation_action_is_user_gesture(a))
797                         return NULL;
798         case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
799         case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
800         case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
801         case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
802         case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
803                 n = newclient(c);
804                 break;
805         default:
806                 return NULL;
807         }
808
809         return GTK_WIDGET(n->view);
810 }
811
812 gboolean
813 buttonreleased(GtkWidget *w, GdkEvent *e, Client *c)
814 {
815         WebKitHitTestResultContext element;
816         int i;
817
818         element = webkit_hit_test_result_get_context(c->mousepos);
819
820         for (i = 0; i < LENGTH(buttons); ++i) {
821                 if (element & buttons[i].target &&
822                     e->button.button == buttons[i].button &&
823                     CLEANMASK(e->button.state) == CLEANMASK(buttons[i].mask) &&
824                     buttons[i].func) {
825                         buttons[i].func(c, &buttons[i].arg, c->mousepos);
826                         return buttons[i].stopevent;
827                 }
828         }
829
830         return FALSE;
831 }
832
833 gboolean
834 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
835          Client *c)
836 {
837         guint i;
838         gboolean processed = FALSE;
839
840         mods = CLEANMASK(mods);
841         key = gdk_keyval_to_lower(key);
842         updatewinid(c);
843         for (i = 0; i < LENGTH(keys); i++) {
844                 if (key == keys[i].keyval
845                     && mods == keys[i].mod
846                     && keys[i].func) {
847                         keys[i].func(c, &(keys[i].arg));
848                         processed = TRUE;
849                 }
850         }
851
852         return processed;
853 }
854
855 GdkFilterReturn
856 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
857 {
858         Client *c = (Client *)d;
859         XPropertyEvent *ev;
860         Arg a;
861
862         if (((XEvent *)e)->type == PropertyNotify) {
863                 ev = &((XEvent *)e)->xproperty;
864                 if (ev->state == PropertyNewValue) {
865                         if (ev->atom == atoms[AtomFind]) {
866                                 find(c, NULL);
867
868                                 return GDK_FILTER_REMOVE;
869                         } else if (ev->atom == atoms[AtomGo]) {
870                                 a.v = getatom(c, AtomGo);
871                                 loaduri(c, &a);
872
873                                 return GDK_FILTER_REMOVE;
874                         }
875                 }
876         }
877         return GDK_FILTER_CONTINUE;
878 }
879
880 gboolean
881 winevent(GtkWidget *w, GdkEvent *e, Client *c)
882 {
883         switch (e->type) {
884         case GDK_LEAVE_NOTIFY:
885                 c->targeturi = NULL;
886                 updatetitle(c);
887                 break;
888         case GDK_WINDOW_STATE: /* fallthrough */
889                 if (e->window_state.changed_mask ==
890                     GDK_WINDOW_STATE_FULLSCREEN) {
891                         c->fullscreen = e->window_state.new_window_state &
892                                         GDK_WINDOW_STATE_FULLSCREEN;
893                         break;
894                 }
895         default:
896                 return FALSE;
897         }
898
899         return TRUE;
900 }
901
902 void
903 showview(WebKitWebView *v, Client *c)
904 {
905         GdkRGBA bgcolor = { 0 };
906         GdkWindow *gwin;
907
908         c->finder = webkit_web_view_get_find_controller(c->view);
909         if (enableinspector)
910                 c->inspector = webkit_web_view_get_inspector(c->view);
911
912         c->win = createwindow(c);
913
914         gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
915         gtk_widget_show_all(c->win);
916         gtk_widget_grab_focus(GTK_WIDGET(c->view));
917
918         gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
919         c->xid = gdk_x11_window_get_xid(gwin);
920         updatewinid(c);
921         if (showxid) {
922                 gdk_display_sync(gtk_widget_get_display(c->win));
923                 puts(winid);
924         }
925
926         if (hidebackground)
927                 webkit_web_view_set_background_color(c->view, &bgcolor);
928
929         if (!kioskmode) {
930                 addaccelgroup(c);
931                 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
932                 gdk_window_add_filter(gwin, processx, c);
933         }
934
935         if (runinfullscreen)
936                 togglefullscreen(c, NULL);
937
938         if (zoomlevel != 1.0)
939                 webkit_web_view_set_zoom_level(c->view, zoomlevel);
940
941         setatom(c, AtomFind, "");
942         setatom(c, AtomUri, "about:blank");
943 }
944
945 GtkWidget *
946 createwindow(Client *c)
947 {
948         char *wmstr;
949         GtkWidget *w;
950
951         if (embed) {
952                 w = gtk_plug_new(embed);
953         } else {
954                 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
955
956                 wmstr = g_path_get_basename(argv0);
957                 gtk_window_set_wmclass(GTK_WINDOW(w), wmstr, "Surf");
958                 g_free(wmstr);
959
960                 wmstr = g_strdup_printf("%s[%lu]", "Surf",
961                         webkit_web_view_get_page_id(c->view));
962                 gtk_window_set_role(GTK_WINDOW(w), wmstr);
963                 g_free(wmstr);
964
965                 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
966         }
967
968         g_signal_connect(G_OBJECT(w), "destroy",
969                          G_CALLBACK(destroywin), c);
970         g_signal_connect(G_OBJECT(w), "leave-notify-event",
971                          G_CALLBACK(winevent), c);
972         g_signal_connect(G_OBJECT(w), "window-state-event",
973                          G_CALLBACK(winevent), c);
974
975         return w;
976 }
977
978 void
979 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
980 {
981         switch (e) {
982         case WEBKIT_LOAD_STARTED:
983                 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
984                 break;
985         case WEBKIT_LOAD_REDIRECTED:
986                 setatom(c, AtomUri, geturi(c));
987                 break;
988         case WEBKIT_LOAD_COMMITTED:
989                 if (!webkit_web_view_get_tls_info(c->view, NULL,
990                     &(c->tlsflags)))
991                         c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
992
993                 setatom(c, AtomUri, geturi(c));
994
995                 if (enablestyle)
996                         setstyle(c, getstyle(geturi(c)));
997                 break;
998         case WEBKIT_LOAD_FINISHED:
999                 /* Disabled until we write some WebKitWebExtension for
1000                  * manipulating the DOM directly.
1001                 evalscript(c, "document.documentElement.style.overflow = '%s'",
1002                     enablescrollbars ? "auto" : "hidden");
1003                 */
1004                 runscript(c);
1005                 break;
1006         }
1007         updatetitle(c);
1008 }
1009
1010 void
1011 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1012 {
1013         c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1014                       100;
1015         updatetitle(c);
1016 }
1017
1018 void
1019 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1020 {
1021         c->title = webkit_web_view_get_title(c->view);
1022         updatetitle(c);
1023 }
1024
1025 void
1026 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
1027     Client *c)
1028 {
1029         WebKitHitTestResultContext hc = webkit_hit_test_result_get_context(h);
1030
1031         /* Keep the hit test to know where is the pointer on the next click */
1032         c->mousepos = h;
1033
1034         if (hc & OnLink)
1035                 c->targeturi = webkit_hit_test_result_get_link_uri(h);
1036         else if (hc & OnImg)
1037                 c->targeturi = webkit_hit_test_result_get_image_uri(h);
1038         else if (hc & OnMedia)
1039                 c->targeturi = webkit_hit_test_result_get_media_uri(h);
1040         else
1041                 c->targeturi = NULL;
1042         updatetitle(c);
1043 }
1044
1045 gboolean
1046 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
1047 {
1048         if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
1049                 if (allowgeolocation)
1050                         webkit_permission_request_allow(r);
1051                 else
1052                         webkit_permission_request_deny(r);
1053                 return TRUE;
1054         }
1055
1056         return FALSE;
1057 }
1058
1059 gboolean
1060 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
1061     WebKitPolicyDecisionType dt, Client *c)
1062 {
1063         switch (dt) {
1064         case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
1065                 decidenavigation(d, c);
1066                 break;
1067         case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
1068                 decidenewwindow(d, c);
1069                 break;
1070         case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
1071                 decideresource(d, c);
1072                 break;
1073         default:
1074                 webkit_policy_decision_ignore(d);
1075                 break;
1076         }
1077         return TRUE;
1078 }
1079
1080 void
1081 decidenavigation(WebKitPolicyDecision *d, Client *c)
1082 {
1083         WebKitNavigationAction *a =
1084             webkit_navigation_policy_decision_get_navigation_action(
1085             WEBKIT_NAVIGATION_POLICY_DECISION(d));
1086
1087         switch (webkit_navigation_action_get_navigation_type(a)) {
1088         case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1089         case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1090         case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1091         case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1092         case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED: /* fallthrough */
1093         case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1094         default:
1095                 /* Do not navigate to links with a "_blank" target (popup) */
1096                 if (webkit_navigation_policy_decision_get_frame_name(
1097                     WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
1098                         webkit_policy_decision_ignore(d);
1099                 } else {
1100                         /* Filter out navigation to different domain ? */
1101                         /* get action→urirequest, copy and load in new window+view
1102                          * on Ctrl+Click ? */
1103                         webkit_policy_decision_use(d);
1104                 }
1105                 break;
1106         }
1107 }
1108
1109 void
1110 decidenewwindow(WebKitPolicyDecision *d, Client *c)
1111 {
1112         Arg arg;
1113         WebKitNavigationAction *a =
1114             webkit_navigation_policy_decision_get_navigation_action(
1115             WEBKIT_NAVIGATION_POLICY_DECISION(d));
1116
1117
1118         switch (webkit_navigation_action_get_navigation_type(a)) {
1119         case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
1120         case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
1121         case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
1122         case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
1123         case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
1124                 /* Filter domains here */
1125 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
1126  * test for link clicked but no button ? */
1127                 arg.v = webkit_uri_request_get_uri(
1128                         webkit_navigation_action_get_request(a));
1129                 newwindow(c, &arg, 0);
1130                 break;
1131         case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
1132         default:
1133                 break;
1134         }
1135
1136         webkit_policy_decision_ignore(d);
1137 }
1138
1139 void
1140 decideresource(WebKitPolicyDecision *d, Client *c)
1141 {
1142         int i, isascii = 1;
1143         WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
1144         WebKitURIResponse *res =
1145             webkit_response_policy_decision_get_response(r);
1146         const gchar *uri = webkit_uri_response_get_uri(res);
1147
1148         if (g_str_has_suffix(uri, "/favicon.ico"))
1149                 webkit_uri_request_set_uri(
1150                     webkit_response_policy_decision_get_request(r),
1151                     "about:blank");
1152
1153         if (!g_str_has_prefix(uri, "http://")
1154             && !g_str_has_prefix(uri, "https://")
1155             && !g_str_has_prefix(uri, "about:")
1156             && !g_str_has_prefix(uri, "file://")
1157             && !g_str_has_prefix(uri, "data:")
1158             && !g_str_has_prefix(uri, "blob:")
1159             && strlen(uri) > 0) {
1160                 for (i = 0; i < strlen(uri); i++) {
1161                         if (!g_ascii_isprint(uri[i])) {
1162                                 isascii = 0;
1163                                 break;
1164                         }
1165                 }
1166                 if (isascii) {
1167                         handleplumb(c, uri);
1168                         webkit_policy_decision_ignore(d);
1169                 }
1170         }
1171
1172         if (webkit_response_policy_decision_is_mime_type_supported(r)) {
1173                 webkit_policy_decision_use(d);
1174         } else {
1175                 webkit_policy_decision_ignore(d);
1176                 download(c, res);
1177         }
1178 }
1179
1180 void
1181 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
1182 {
1183         g_signal_connect(G_OBJECT(d), "notify::response",
1184                          G_CALLBACK(responsereceived), c);
1185 }
1186
1187 void
1188 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
1189 {
1190         download(c, webkit_download_get_response(d));
1191         webkit_download_cancel(d);
1192 }
1193
1194 void
1195 download(Client *c, WebKitURIResponse *r)
1196 {
1197         Arg a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
1198         spawn(c, &a);
1199 }
1200
1201 void
1202 closeview(WebKitWebView *v, Client *c)
1203 {
1204         gtk_widget_destroy(c->win);
1205 }
1206
1207 void
1208 destroywin(GtkWidget* w, Client *c)
1209 {
1210         destroyclient(c);
1211         if (!clients)
1212                 gtk_main_quit();
1213 }
1214
1215 void
1216 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1217 {
1218         Arg a = {.v = text };
1219         if (text)
1220                 loaduri((Client *) d, &a);
1221 }
1222
1223 void
1224 reload(Client *c, const Arg *a)
1225 {
1226         if (a->b)
1227                 webkit_web_view_reload_bypass_cache(c->view);
1228         else
1229                 webkit_web_view_reload(c->view);
1230 }
1231
1232 void
1233 print(Client *c, const Arg *a)
1234 {
1235         webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1236                                           GTK_WINDOW(c->win));
1237 }
1238
1239 void
1240 clipboard(Client *c, const Arg *a)
1241 {
1242         if (a->b) { /* load clipboard uri */
1243                 gtk_clipboard_request_text(gtk_clipboard_get(
1244                                            GDK_SELECTION_PRIMARY),
1245                                            pasteuri, c);
1246         } else { /* copy uri */
1247                 gtk_clipboard_set_text(gtk_clipboard_get(
1248                                        GDK_SELECTION_PRIMARY), c->targeturi
1249                                        ? c->targeturi : geturi(c), -1);
1250         }
1251 }
1252
1253 void
1254 zoom(Client *c, const Arg *a)
1255 {
1256         if (a->i > 0)
1257                 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1258         else if (a->i < 0)
1259                 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1260         else
1261                 webkit_web_view_set_zoom_level(c->view, 1.0);
1262
1263         zoomlevel = webkit_web_view_get_zoom_level(c->view);
1264 }
1265
1266 void
1267 scroll_v(Client *c, const Arg *a)
1268 {
1269         evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1270                    a->i);
1271 }
1272
1273 void
1274 scroll_h(Client *c, const Arg *a)
1275 {
1276         evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1277                    a->i);
1278 }
1279
1280 void
1281 navigate(Client *c, const Arg *a)
1282 {
1283         if (a->i < 0)
1284                 webkit_web_view_go_back(c->view);
1285         else if (a->i > 0)
1286                 webkit_web_view_go_forward(c->view);
1287 }
1288
1289 void
1290 stop(Client *c, const Arg *a)
1291 {
1292         webkit_web_view_stop_loading(c->view);
1293 }
1294
1295 void
1296 toggle(Client *c, const Arg *a)
1297 {
1298         WebKitSettings *s = webkit_web_view_get_settings(c->view);
1299
1300         switch ((unsigned int)a->i) {
1301         case CaretBrowsing:
1302                 enablecaretbrowsing = !enablecaretbrowsing;
1303                 webkit_settings_set_enable_caret_browsing(s,
1304                     enablecaretbrowsing);
1305                 updatetitle(c);
1306                 return; /* do not reload */
1307                 break;
1308         case FrameFlattening:
1309                 enableframeflattening = !enableframeflattening;
1310                 webkit_settings_set_enable_frame_flattening(s,
1311                     enableframeflattening);
1312                 break;
1313         case Geolocation:
1314                 allowgeolocation = !allowgeolocation;
1315                 break;
1316         case JavaScript:
1317                 enablescripts = !enablescripts;
1318                 webkit_settings_set_enable_javascript(s, enablescripts);
1319                 break;
1320         case LoadImages:
1321                 loadimages = !loadimages;
1322                 webkit_settings_set_auto_load_images(s, loadimages);
1323                 break;
1324         case Plugins:
1325                 enableplugins = !enableplugins;
1326                 webkit_settings_set_enable_plugins(s, enableplugins);
1327                 break;
1328         case ScrollBars:
1329                 /* Disabled until we write some WebKitWebExtension for
1330                  * manipulating the DOM directly.
1331                 enablescrollbars = !enablescrollbars;
1332                 evalscript(c, "document.documentElement.style.overflow = '%s'",
1333                     enablescrollbars ? "auto" : "hidden");
1334                 */
1335                 return; /* do not reload */
1336                 break;
1337         default:
1338                 break;
1339         }
1340         reload(c, a);
1341 }
1342
1343 void
1344 togglefullscreen(Client *c, const Arg *a)
1345 {
1346         /* toggling value is handled in winevent() */
1347         if (c->fullscreen)
1348                 gtk_window_unfullscreen(GTK_WINDOW(c->win));
1349         else
1350                 gtk_window_fullscreen(GTK_WINDOW(c->win));
1351 }
1352
1353 void
1354 togglecookiepolicy(Client *c, const Arg *a)
1355 {
1356         ++cookiepolicy;
1357         cookiepolicy %= strlen(cookiepolicies);
1358
1359         webkit_cookie_manager_set_accept_policy(
1360             webkit_web_context_get_cookie_manager(
1361             webkit_web_view_get_context(c->view)),
1362             cookiepolicy_get());
1363
1364         updatetitle(c);
1365         /* Do not reload. */
1366 }
1367
1368 void
1369 togglestyle(Client *c, const Arg *a)
1370 {
1371         enablestyle = !enablestyle;
1372         setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1373
1374         updatetitle(c);
1375 }
1376
1377 void
1378 toggleinspector(Client *c, const Arg *a)
1379 {
1380         if (enableinspector) {
1381                 if (webkit_web_inspector_is_attached(c->inspector))
1382                         webkit_web_inspector_close(c->inspector);
1383                 else
1384                         webkit_web_inspector_show(c->inspector);
1385         }
1386 }
1387
1388 void
1389 find(Client *c, const Arg *a)
1390 {
1391         const char *s, *f;
1392
1393         if (a && a->i) {
1394                 if (a->i > 0)
1395                         webkit_find_controller_search_next(c->finder);
1396                 else
1397                         webkit_find_controller_search_previous(c->finder);
1398         } else {
1399                 s = getatom(c, AtomFind);
1400                 f = webkit_find_controller_get_search_text(c->finder);
1401
1402                 if (g_strcmp0(f, s) == 0) /* reset search */
1403                         webkit_find_controller_search(c->finder, "", findopts,
1404                                                       G_MAXUINT);
1405
1406                 webkit_find_controller_search(c->finder, s, findopts,
1407                                               G_MAXUINT);
1408
1409                 if (strcmp(s, "") == 0)
1410                         webkit_find_controller_search_finish(c->finder);
1411         }
1412 }
1413
1414 void
1415 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
1416 {
1417         navigate(c, a);
1418 }
1419
1420 void
1421 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1422 {
1423         Arg arg;
1424
1425         arg.v = webkit_hit_test_result_get_link_uri(h);
1426         newwindow(c, &arg, a->b);
1427 }
1428
1429 void
1430 clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h)
1431 {
1432         Arg arg;
1433
1434         if (webkit_hit_test_result_get_context(h) & OnMedia) {
1435                 arg = (Arg)VIDEOPLAY(webkit_hit_test_result_get_media_uri(h));
1436                 spawn(c, &arg);
1437         }
1438 }
1439
1440 int
1441 main(int argc, char *argv[])
1442 {
1443         Arg arg;
1444         Client *c;
1445
1446         memset(&arg, 0, sizeof(arg));
1447
1448         /* command line args */
1449         ARGBEGIN {
1450         case 'a':
1451                 cookiepolicies = EARGF(usage());
1452                 break;
1453         case 'b':
1454                 enablescrollbars = 0;
1455                 break;
1456         case 'B':
1457                 enablescrollbars = 1;
1458                 break;
1459         case 'c':
1460                 cookiefile = EARGF(usage());
1461                 break;
1462         case 'd':
1463                 enablecache = 0;
1464                 break;
1465         case 'D':
1466                 enablecache = 1;
1467                 break;
1468         case 'e':
1469                 embed = strtol(EARGF(usage()), NULL, 0);
1470                 break;
1471         case 'f':
1472                 runinfullscreen = 0;
1473                 break;
1474         case 'F':
1475                 runinfullscreen = 1;
1476                 break;
1477         case 'g':
1478                 allowgeolocation = 0;
1479                 break;
1480         case 'G':
1481                 allowgeolocation = 1;
1482                 break;
1483         case 'i':
1484                 loadimages = 0;
1485                 break;
1486         case 'I':
1487                 loadimages = 1;
1488                 break;
1489         case 'k':
1490                 kioskmode = 0;
1491                 break;
1492         case 'K':
1493                 kioskmode = 1;
1494                 break;
1495         case 'm':
1496                 enablestyle = 0;
1497                 break;
1498         case 'M':
1499                 enablestyle = 1;
1500                 break;
1501         case 'n':
1502                 enableinspector = 0;
1503                 break;
1504         case 'N':
1505                 enableinspector = 1;
1506                 break;
1507         case 'p':
1508                 enableplugins = 0;
1509                 break;
1510         case 'P':
1511                 enableplugins = 1;
1512                 break;
1513         case 'r':
1514                 scriptfile = EARGF(usage());
1515                 break;
1516         case 's':
1517                 enablescripts = 0;
1518                 break;
1519         case 'S':
1520                 enablescripts = 1;
1521                 break;
1522         case 't':
1523                 stylefile = EARGF(usage());
1524                 break;
1525         case 'u':
1526                 useragent = EARGF(usage());
1527                 break;
1528         case 'v':
1529                 die("surf-"VERSION", ©2009-2015 surf engineers, "
1530                     "see LICENSE for details\n");
1531         case 'x':
1532                 showxid = 1;
1533                 break;
1534         case 'z':
1535                 zoomlevel = strtof(EARGF(usage()), NULL);
1536                 break;
1537         default:
1538                 usage();
1539         } ARGEND;
1540         if (argc > 0)
1541                 arg.v = argv[0];
1542         else
1543                 arg.v = "about:blank";
1544
1545         setup();
1546         c = newclient(NULL);
1547         showview(NULL, c);
1548
1549         loaduri(c, &arg);
1550         updatetitle(c);
1551
1552         gtk_main();
1553         cleanup();
1554
1555         return 0;
1556 }