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