Just some comment correction.
[surf.git] / surf.c
1 /* See LICENSE file for copyright and license details.
2  *
3  * To understand surf, start reading main().
4  */
5
6 #include <signal.h>
7 #include <X11/X.h>
8 #include <X11/Xatom.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 <webkit/webkit.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
27 #include "arg.h"
28
29 char *argv0;
30
31 #define LENGTH(x)               (sizeof x / sizeof x[0])
32 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
33 #define COOKIEJAR_TYPE          (cookiejar_get_type ())
34 #define COOKIEJAR(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
35
36 enum { AtomFind, AtomGo, AtomUri, AtomLast };
37
38 typedef union Arg Arg;
39 union Arg {
40         gboolean b;
41         gint i;
42         const void *v;
43 };
44
45 typedef struct Client {
46         GtkWidget *win, *scroll, *vbox, *pane;
47         WebKitWebView *view;
48         WebKitWebInspector *inspector;
49         char *title, *linkhover;
50         const char *uri, *needle;
51         gint progress;
52         struct Client *next;
53         gboolean zoomed, fullscreen, isinspecting, sslfailed;
54 } Client;
55
56 typedef struct {
57         guint mod;
58         guint keyval;
59         void (*func)(Client *c, const Arg *arg);
60         const Arg arg;
61 } Key;
62
63 typedef struct {
64         SoupCookieJarText parent_instance;
65         int lock;
66 } CookieJar;
67
68 typedef struct {
69         SoupCookieJarTextClass parent_class;
70 } CookieJarClass;
71
72 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
73
74 static Display *dpy;
75 static Atom atoms[AtomLast];
76 static Client *clients = NULL;
77 static GdkNativeWindow embed = 0;
78 static gboolean showxid = FALSE;
79 static char winid[64];
80 static gboolean usingproxy = 0;
81 static char togglestat[7];
82 static char pagestat[3];
83
84 static void addaccelgroup(Client *c);
85 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
86                 WebKitWebResource *r, WebKitNetworkRequest *req,
87                 WebKitNetworkResponse *resp, gpointer d);
88 static char *buildpath(const char *path);
89 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
90                 GList *gl);
91 static void cleanup(void);
92 static void clipboard(Client *c, const Arg *arg);
93
94 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
95                 SoupCookie *new_cookie);
96 static void cookiejar_finalize(GObject *self);
97 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
98 static void cookiejar_set_property(GObject *self, guint prop_id,
99                 const GValue *value, GParamSpec *pspec);
100
101 static char *copystr(char **str, const char *src);
102 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
103                 Client *c);
104 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
105                 WebKitNetworkRequest *r, gchar *m,  WebKitWebPolicyDecision *p,
106                 Client *c);
107 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
108                 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
109                 WebKitWebPolicyDecision *p, Client *c);
110 static gboolean deletion_interface(WebKitWebView *view,
111                 WebKitDOMHTMLElement *arg1, Client *c);
112 static void destroyclient(Client *c);
113 static void destroywin(GtkWidget* w, Client *c);
114 static void die(const char *errstr, ...);
115 static void eval(Client *c, const Arg *arg);
116 static void find(Client *c, const Arg *arg);
117 static void fullscreen(Client *c, const Arg *arg);
118 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
119                 WebKitGeolocationPolicyDecision *d, Client *c);
120 static const char *getatom(Client *c, int a);
121 static void gettogglestat(Client *c);
122 static void getpagestat(Client *c);
123 static char *geturi(Client *c);
124 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
125
126 static void inspector(Client *c, const Arg *arg);
127 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
128                 Client *c);
129 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
130 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
131 static void inspector_finished(WebKitWebInspector *i, Client *c);
132
133 static gboolean keypress(GtkAccelGroup *group,
134                 GObject *obj, guint key, GdkModifierType mods,
135                 Client *c);
136 static void linkhover(WebKitWebView *v, const char* t, const char* l,
137                 Client *c);
138 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
139                 Client *c);
140 static void loaduri(Client *c, const Arg *arg);
141 static void navigate(Client *c, const Arg *arg);
142 static Client *newclient(void);
143 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
144 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
145 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
146 static void popupactivate(GtkMenuItem *menu, Client *);
147 static void print(Client *c, const Arg *arg);
148 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
149                 gpointer d);
150 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
151 static void reload(Client *c, const Arg *arg);
152 static void scroll_h(Client *c, const Arg *arg);
153 static void scroll_v(Client *c, const Arg *arg);
154 static void scroll(GtkAdjustment *a, const Arg *arg);
155 static void setatom(Client *c, int a, const char *v);
156 static void setup(void);
157 static void sigchld(int unused);
158 static void source(Client *c, const Arg *arg);
159 static void spawn(Client *c, const Arg *arg);
160 static void stop(Client *c, const Arg *arg);
161 static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
162                 const char *title, Client *c);
163 static void toggle(Client *c, const Arg *arg);
164 static void togglegeolocation(Client *c, const Arg *arg);
165 static void togglescrollbars(Client *c, const Arg *arg);
166 static void togglestyle(Client *c, const Arg *arg);
167 static void updatetitle(Client *c);
168 static void updatewinid(Client *c);
169 static void usage(void);
170 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
171                 JSContextRef js, JSObjectRef win, Client *c);
172 static void zoom(Client *c, const Arg *arg);
173
174 /* configuration, allows nested code to access above variables */
175 #include "config.h"
176
177 static void
178 addaccelgroup(Client *c) {
179         int i;
180         GtkAccelGroup *group = gtk_accel_group_new();
181         GClosure *closure;
182
183         for(i = 0; i < LENGTH(keys); i++) {
184                 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
185                 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod,
186                                 0, closure);
187         }
188         gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
189 }
190
191 static void
192 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
193                 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
194                 gpointer d) {
195         const gchar *uri = webkit_network_request_get_uri(req);
196
197         if(g_str_has_suffix(uri, "/favicon.ico"))
198                 webkit_network_request_set_uri(req, "about:blank");
199 }
200
201 static char *
202 buildpath(const char *path) {
203         char *apath, *p;
204         FILE *f;
205
206         /* creating directory */
207         if(path[0] == '/') {
208                 apath = g_strdup(path);
209         } else if(path[0] == '~') {
210                 if(path[1] == '/') {
211                         apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
212                 } else {
213                         apath = g_strconcat(g_get_home_dir(), "/",
214                                         &path[1], NULL);
215                 }
216         } else {
217                 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
218         }
219
220         if((p = strrchr(apath, '/'))) {
221                 *p = '\0';
222                 g_mkdir_with_parents(apath, 0700);
223                 g_chmod(apath, 0700); /* in case it existed */
224                 *p = '/';
225         }
226         /* creating file (gives error when apath ends with "/") */
227         if((f = fopen(apath, "a"))) {
228                 g_chmod(apath, 0600); /* always */
229                 fclose(f);
230         }
231
232         return apath;
233 }
234
235 static gboolean
236 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
237         WebKitHitTestResultContext context;
238         WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
239                         e);
240         Arg arg;
241
242         g_object_get(result, "context", &context, NULL);
243         if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
244                 if(e->button == 2) {
245                         g_object_get(result, "link-uri", &arg.v, NULL);
246                         newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
247                         return true;
248                 }
249         }
250         return false;
251 }
252
253 static void
254 cleanup(void) {
255         while(clients)
256                 destroyclient(clients);
257         g_free(cookiefile);
258         g_free(scriptfile);
259         g_free(stylefile);
260 }
261
262 static void
263 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
264                 SoupCookie *new_cookie) {
265         flock(COOKIEJAR(self)->lock, LOCK_EX);
266         if(new_cookie && !new_cookie->expires && sessiontime) {
267                 soup_cookie_set_expires(new_cookie,
268                                 soup_date_new_from_now(sessiontime));
269         }
270         SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
271                         old_cookie, new_cookie);
272         flock(COOKIEJAR(self)->lock, LOCK_UN);
273 }
274
275 static void
276 cookiejar_class_init(CookieJarClass *klass) {
277         SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
278         G_OBJECT_CLASS(klass)->get_property =
279                 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
280         G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
281         G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
282         g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
283 }
284
285 static void
286 cookiejar_finalize(GObject *self) {
287         close(COOKIEJAR(self)->lock);
288         G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
289 }
290
291 static void
292 cookiejar_init(CookieJar *self) {
293         self->lock = open(cookiefile, 0);
294 }
295
296 static SoupCookieJar *
297 cookiejar_new(const char *filename, gboolean read_only) {
298         return g_object_new(COOKIEJAR_TYPE,
299                             SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
300                             SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
301 }
302
303 static void
304 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
305                 GParamSpec *pspec) {
306         flock(COOKIEJAR(self)->lock, LOCK_SH);
307         G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
308                         value, pspec);
309         flock(COOKIEJAR(self)->lock, LOCK_UN);
310 }
311
312 static void
313 evalscript(JSContextRef js, char *script, char* scriptname) {
314         JSStringRef jsscript, jsscriptname;
315         JSValueRef exception = NULL;
316
317         jsscript = JSStringCreateWithUTF8CString(script);
318         jsscriptname = JSStringCreateWithUTF8CString(scriptname);
319         JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
320                         jsscriptname, 0, &exception);
321         JSStringRelease(jsscript);
322         JSStringRelease(jsscriptname);
323 }
324
325 static void
326 runscript(WebKitWebFrame *frame) {
327         char *script;
328         GError *error;
329
330         if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
331                 evalscript(webkit_web_frame_get_global_context(frame),
332                                 script, scriptfile);
333         }
334 }
335
336 static void
337 clipboard(Client *c, const Arg *arg) {
338         gboolean paste = *(gboolean *)arg;
339
340         if(paste) {
341                 gtk_clipboard_request_text(
342                                 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
343                                 pasteuri, c);
344         } else {
345                 gtk_clipboard_set_text(
346                                 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
347                                 c->linkhover ? c->linkhover : geturi(c), -1);
348         }
349 }
350
351 static char *
352 copystr(char **str, const char *src) {
353         char *tmp;
354         tmp = g_strdup(src);
355
356         if(str && *str) {
357                 g_free(*str);
358                 *str = tmp;
359         }
360         return tmp;
361 }
362
363 static WebKitWebView *
364 createwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
365         Client *n = newclient();
366         return n->view;
367 }
368
369 static gboolean
370 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
371                 gchar *m,  WebKitWebPolicyDecision *p, Client *c) {
372         if(!webkit_web_view_can_show_mime_type(v, m)) {
373                 webkit_web_policy_decision_download(p);
374                 return TRUE;
375         }
376         return FALSE;
377 }
378
379 static gboolean
380 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
381                 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
382                 Client *c) {
383         Arg arg;
384
385         if(webkit_web_navigation_action_get_reason(n) ==
386                         WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
387                 webkit_web_policy_decision_ignore(p);
388                 arg.v = (void *)webkit_network_request_get_uri(r);
389                 newwindow(NULL, &arg, 0);
390                 return TRUE;
391         }
392         return FALSE;
393 }
394
395 static gboolean
396 deletion_interface(WebKitWebView *view,
397                 WebKitDOMHTMLElement *arg1, Client *c) {
398         return FALSE;
399 }
400
401 static void
402 destroyclient(Client *c) {
403         Client *p;
404
405         webkit_web_view_stop_loading(c->view);
406         gtk_widget_destroy(GTK_WIDGET(c->view));
407         gtk_widget_destroy(c->scroll);
408         gtk_widget_destroy(c->vbox);
409         gtk_widget_destroy(c->win);
410
411         for(p = clients; p && p->next != c; p = p->next);
412         if(p) {
413                 p->next = c->next;
414         } else {
415                 clients = c->next;
416         }
417         free(c);
418         if(clients == NULL)
419                 gtk_main_quit();
420 }
421
422 static void
423 destroywin(GtkWidget* w, Client *c) {
424         destroyclient(c);
425 }
426
427 static void
428 die(const char *errstr, ...) {
429         va_list ap;
430
431         va_start(ap, errstr);
432         vfprintf(stderr, errstr, ap);
433         va_end(ap);
434         exit(EXIT_FAILURE);
435 }
436
437 static void
438 find(Client *c, const Arg *arg) {
439         const char *s;
440
441         s = getatom(c, AtomFind);
442         gboolean forward = *(gboolean *)arg;
443         webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
444 }
445
446 static void
447 fullscreen(Client *c, const Arg *arg) {
448         if(c->fullscreen) {
449                 gtk_window_unfullscreen(GTK_WINDOW(c->win));
450         } else {
451                 gtk_window_fullscreen(GTK_WINDOW(c->win));
452         }
453         c->fullscreen = !c->fullscreen;
454 }
455
456 static void
457 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
458                 WebKitGeolocationPolicyDecision *d, Client *c) {
459         if(allowgeolocation) {
460                 webkit_geolocation_policy_allow(d);
461         } else {
462                 webkit_geolocation_policy_deny(d);
463         }
464 }
465
466 static const char *
467 getatom(Client *c, int a) {
468         static char buf[BUFSIZ];
469         Atom adummy;
470         int idummy;
471         unsigned long ldummy;
472         unsigned char *p = NULL;
473
474         XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
475                         atoms[a], 0L, BUFSIZ, False, XA_STRING,
476                         &adummy, &idummy, &ldummy, &ldummy, &p);
477         if(p) {
478                 strncpy(buf, (char *)p, LENGTH(buf)-1);
479         } else {
480                 buf[0] = '\0';
481         }
482         XFree(p);
483
484         return buf;
485 }
486
487 static char *
488 geturi(Client *c) {
489         char *uri;
490
491         if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
492                 uri = "about:blank";
493         return uri;
494 }
495
496 static gboolean
497 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
498         Arg arg;
499
500         updatewinid(c);
501         arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
502         spawn(c, &arg);
503         return FALSE;
504 }
505
506 static void
507 inspector(Client *c, const Arg *arg) {
508         if(c->isinspecting) {
509                 webkit_web_inspector_close(c->inspector);
510         } else {
511                 webkit_web_inspector_show(c->inspector);
512         }
513 }
514
515 static WebKitWebView *
516 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
517         return WEBKIT_WEB_VIEW(webkit_web_view_new());
518 }
519
520 static gboolean
521 inspector_show(WebKitWebInspector *i, Client *c) {
522         WebKitWebView *w;
523
524         if(c->isinspecting)
525                 return false;
526
527         w = webkit_web_inspector_get_web_view(i);
528         gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
529         gtk_widget_show(GTK_WIDGET(w));
530         c->isinspecting = true;
531
532         return true;
533 }
534
535 static gboolean
536 inspector_close(WebKitWebInspector *i, Client *c) {
537         GtkWidget *w;
538
539         if(!c->isinspecting)
540                 return false;
541
542         w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
543         gtk_widget_hide(w);
544         gtk_widget_destroy(w);
545         c->isinspecting = false;
546
547         return true;
548 }
549
550 static void
551 inspector_finished(WebKitWebInspector *i, Client *c) {
552         g_free(c->inspector);
553 }
554
555 static gboolean
556 keypress(GtkAccelGroup *group, GObject *obj,
557                 guint key, GdkModifierType mods, Client *c) {
558         guint i;
559         gboolean processed = FALSE;
560
561         mods = CLEANMASK(mods);
562         key = gdk_keyval_to_lower(key);
563         updatewinid(c);
564         for(i = 0; i < LENGTH(keys); i++) {
565                 if(key == keys[i].keyval
566                                 && mods == keys[i].mod
567                                 && keys[i].func) {
568                         keys[i].func(c, &(keys[i].arg));
569                         processed = TRUE;
570                 }
571         }
572
573         return processed;
574 }
575
576 static void
577 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
578         if(l) {
579                 c->linkhover = copystr(&c->linkhover, l);
580         } else if(c->linkhover) {
581                 free(c->linkhover);
582                 c->linkhover = NULL;
583         }
584         updatetitle(c);
585 }
586
587 static void
588 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
589         WebKitWebFrame *frame;
590         WebKitWebDataSource *src;
591         WebKitNetworkRequest *request;
592         SoupMessage *msg;
593         char *uri;
594
595         switch(webkit_web_view_get_load_status (c->view)) {
596         case WEBKIT_LOAD_COMMITTED:
597                 uri = geturi(c);
598                 if(strstr(uri, "https://") == uri) {
599                         frame = webkit_web_view_get_main_frame(c->view);
600                         src = webkit_web_frame_get_data_source(frame);
601                         request = webkit_web_data_source_get_request(src);
602                         msg = webkit_network_request_get_message(request);
603                         c->sslfailed = soup_message_get_flags(msg)
604                                        ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
605                 }
606                 setatom(c, AtomUri, uri);
607                 break;
608         case WEBKIT_LOAD_FINISHED:
609                 c->progress = 100;
610                 updatetitle(c);
611                 break;
612         default:
613                 break;
614         }
615 }
616
617 static void
618 loaduri(Client *c, const Arg *arg) {
619         char *u, *rp;
620         const char *uri = (char *)arg->v;
621         Arg a = { .b = FALSE };
622         struct stat st;
623
624         if(strcmp(uri, "") == 0)
625                 return;
626
627         /* In case it's a file path. */
628         if(stat(uri, &st) == 0) {
629                 rp = realpath(uri, NULL);
630                 u = g_strdup_printf("file://%s", rp);
631                 free(rp);
632         } else {
633                 u = g_strrstr(uri, "://") ? g_strdup(uri)
634                         : g_strdup_printf("http://%s", uri);
635         }
636
637         /* prevents endless loop */
638         if(c->uri && strcmp(u, c->uri) == 0) {
639                 reload(c, &a);
640         } else {
641                 webkit_web_view_load_uri(c->view, u);
642                 c->progress = 0;
643                 c->title = copystr(&c->title, u);
644                 g_free(u);
645                 updatetitle(c);
646         }
647 }
648
649 static void
650 navigate(Client *c, const Arg *arg) {
651         int steps = *(int *)arg;
652         webkit_web_view_go_back_or_forward(c->view, steps);
653 }
654
655 static Client *
656 newclient(void) {
657         Client *c;
658         WebKitWebSettings *settings;
659         WebKitWebFrame *frame;
660         GdkGeometry hints = { 1, 1 };
661         GdkScreen *screen;
662         gdouble dpi;
663         char *uri, *ua;
664
665         if(!(c = calloc(1, sizeof(Client))))
666                 die("Cannot malloc!\n");
667
668         /* Window */
669         if(embed) {
670                 c->win = gtk_plug_new(embed);
671         } else {
672                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
673
674                 /* TA:  20091214:  Despite what the GNOME docs say, the ICCCM
675                  * is always correct, so we should still call this function.
676                  * But when doing so, we *must* differentiate between a
677                  * WM_CLASS and a resource on the window.  By convention, the
678                  * window class (WM_CLASS) is capped, while the resource is in
679                  * lowercase.   Both these values come as a pair.
680                  */
681                 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
682
683                 /* TA:  20091214:  And set the role here as well -- so that
684                  * sessions can pick this up.
685                  */
686                 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
687         }
688         gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
689         g_signal_connect(G_OBJECT(c->win),
690                         "destroy",
691                         G_CALLBACK(destroywin), c);
692
693         if(!kioskmode)
694                 addaccelgroup(c);
695
696         /* Pane */
697         c->pane = gtk_vpaned_new();
698
699         /* VBox */
700         c->vbox = gtk_vbox_new(FALSE, 0);
701         gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
702
703         /* Webview */
704         c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
705
706         g_signal_connect(G_OBJECT(c->view),
707                         "title-changed",
708                         G_CALLBACK(titlechange), c);
709         g_signal_connect(G_OBJECT(c->view),
710                         "hovering-over-link",
711                         G_CALLBACK(linkhover), c);
712         g_signal_connect(G_OBJECT(c->view),
713                         "geolocation-policy-decision-requested",
714                         G_CALLBACK(geopolicyrequested), c);
715         g_signal_connect(G_OBJECT(c->view),
716                         "create-web-view",
717                         G_CALLBACK(createwindow), c);
718         g_signal_connect(G_OBJECT(c->view),
719                         "new-window-policy-decision-requested",
720                         G_CALLBACK(decidewindow), c);
721         g_signal_connect(G_OBJECT(c->view),
722                         "mime-type-policy-decision-requested",
723                         G_CALLBACK(decidedownload), c);
724         g_signal_connect(G_OBJECT(c->view),
725                         "window-object-cleared",
726                         G_CALLBACK(windowobjectcleared), c);
727         g_signal_connect(G_OBJECT(c->view),
728                         "notify::load-status",
729                         G_CALLBACK(loadstatuschange), c);
730         g_signal_connect(G_OBJECT(c->view),
731                         "notify::progress",
732                         G_CALLBACK(progresschange), c);
733         g_signal_connect(G_OBJECT(c->view),
734                         "download-requested",
735                         G_CALLBACK(initdownload), c);
736         g_signal_connect(G_OBJECT(c->view),
737                         "button-release-event",
738                         G_CALLBACK(buttonrelease), c);
739         g_signal_connect(G_OBJECT(c->view),
740                         "populate-popup",
741                         G_CALLBACK(populatepopup), c);
742         g_signal_connect(G_OBJECT(c->view),
743                         "resource-request-starting",
744                         G_CALLBACK(beforerequest), c);
745         g_signal_connect(G_OBJECT(c->view),
746                         "should-show-delete-interface-for-element",
747                         G_CALLBACK(deletion_interface), c);
748
749         /* Scrolled Window */
750         c->scroll = gtk_scrolled_window_new(NULL, NULL);
751
752         frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
753         g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
754                         G_CALLBACK(gtk_true), NULL);
755
756         if(!enablescrollbars) {
757                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
758                                 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
759         } else {
760                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
761                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
762         }
763
764         /* Arranging */
765         gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
766         gtk_container_add(GTK_CONTAINER(c->win), c->pane);
767         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
768
769         /* Setup */
770         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
771                         TRUE, 0, GTK_PACK_START);
772         gtk_widget_grab_focus(GTK_WIDGET(c->view));
773         gtk_widget_show(c->pane);
774         gtk_widget_show(c->vbox);
775         gtk_widget_show(c->scroll);
776         gtk_widget_show(GTK_WIDGET(c->view));
777         gtk_widget_show(c->win);
778         gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
779                         GDK_HINT_MIN_SIZE);
780         gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
781         gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
782         webkit_web_view_set_full_content_zoom(c->view, TRUE);
783
784         runscript(frame);
785
786         settings = webkit_web_view_get_settings(c->view);
787         if(!(ua = getenv("SURF_USERAGENT")))
788                 ua = useragent;
789         g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
790         uri = g_strconcat("file://", stylefile, NULL);
791         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
792         g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
793                         NULL);
794         g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
795                         NULL);
796         g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
797                         NULL);
798         g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
799                         enablespatialbrowsing, NULL);
800         g_object_set(G_OBJECT(settings), "enable-developer-extras",
801                         enableinspector, NULL);
802         g_object_set(G_OBJECT(settings), "enable-default-context-menu",
803                         kioskmode ^ 1, NULL);
804         g_object_set(G_OBJECT(settings), "default-font-size",
805                         defaultfontsize, NULL);
806
807         /*
808          * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
809          * This ensures websites are not unusably small with a high DPI screen.
810          * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
811          */
812         if(zoomto96dpi) {
813                 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
814                 dpi = gdk_screen_get_resolution(screen);
815                 if(dpi != -1) {
816                         g_object_set(G_OBJECT(settings), "enforce-96-dpi", true,
817                                         NULL);
818                         webkit_web_view_set_zoom_level(c->view, dpi/96);
819                 }
820         }
821
822         if(enableinspector) {
823                 c->inspector = WEBKIT_WEB_INSPECTOR(
824                                 webkit_web_view_get_inspector(c->view));
825                 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
826                                 G_CALLBACK(inspector_new), c);
827                 g_signal_connect(G_OBJECT(c->inspector), "show-window",
828                                 G_CALLBACK(inspector_show), c);
829                 g_signal_connect(G_OBJECT(c->inspector), "close-window",
830                                 G_CALLBACK(inspector_close), c);
831                 g_signal_connect(G_OBJECT(c->inspector), "finished",
832                                 G_CALLBACK(inspector_finished), c);
833                 c->isinspecting = false;
834         }
835
836         g_free(uri);
837
838         setatom(c, AtomFind, "");
839         setatom(c, AtomUri, "about:blank");
840         if(hidebackground)
841                 webkit_web_view_set_transparent(c->view, TRUE);
842
843         c->title = NULL;
844         c->next = clients;
845         clients = c;
846
847         if(showxid) {
848                 gdk_display_sync(gtk_widget_get_display(c->win));
849                 printf("%u\n",
850                         (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
851                 fflush(NULL);
852                 if (fclose(stdout) != 0) {
853                         die("Error closing stdout");
854                 }
855         }
856
857         return c;
858 }
859
860 static void
861 newwindow(Client *c, const Arg *arg, gboolean noembed) {
862         guint i = 0;
863         const char *cmd[14], *uri;
864         const Arg a = { .v = (void *)cmd };
865         char tmp[64];
866
867         cmd[i++] = argv0;
868         if(!enablescrollbars)
869                 cmd[i++] = "-b";
870         if(embed && !noembed) {
871                 cmd[i++] = "-e";
872                 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
873                 cmd[i++] = tmp;
874         }
875         if(!loadimages)
876                 cmd[i++] = "-i";
877         if(kioskmode)
878                 cmd[i++] = "-k";
879         if(!enableplugins)
880                 cmd[i++] = "-p";
881         if(!enablescripts)
882                 cmd[i++] = "-s";
883         if(showxid)
884                 cmd[i++] = "-x";
885         cmd[i++] = "-c";
886         cmd[i++] = cookiefile;
887         cmd[i++] = "--";
888         uri = arg->v ? (char *)arg->v : c->linkhover;
889         if(uri)
890                 cmd[i++] = uri;
891         cmd[i++] = NULL;
892         spawn(NULL, &a);
893 }
894
895 static void
896 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
897         GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
898
899         for(GList *l = items; l; l = l->next) {
900                 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
901         }
902
903         g_list_free(items);
904 }
905
906 static void
907 popupactivate(GtkMenuItem *menu, Client *c) {
908         /*
909          * context-menu-action-2000     open link
910          * context-menu-action-1        open link in window
911          * context-menu-action-2        download linked file
912          * context-menu-action-3        copy link location
913          * context-menu-action-13       reload
914          * context-menu-action-10       back
915          * context-menu-action-11       forward
916          * context-menu-action-12       stop
917          */
918
919         GtkAction *a = NULL;
920         const char *name;
921         GtkClipboard *prisel;
922
923         a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
924         if(a == NULL)
925                 return;
926
927         name = gtk_action_get_name(a);
928         if(!g_strcmp0(name, "context-menu-action-3")) {
929                 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
930                 gtk_clipboard_set_text(prisel, c->linkhover, -1);
931         }
932 }
933
934 static void
935 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
936         Arg arg = {.v = text };
937         if(text != NULL)
938                 loaduri((Client *) d, &arg);
939 }
940
941 static void
942 print(Client *c, const Arg *arg) {
943         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
944 }
945
946 static GdkFilterReturn
947 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
948         Client *c = (Client *)d;
949         XPropertyEvent *ev;
950         Arg arg;
951
952         if(((XEvent *)e)->type == PropertyNotify) {
953                 ev = &((XEvent *)e)->xproperty;
954                 if(ev->state == PropertyNewValue) {
955                         if(ev->atom == atoms[AtomFind]) {
956                                 arg.b = TRUE;
957                                 find(c, &arg);
958
959                                 return GDK_FILTER_REMOVE;
960                         } else if(ev->atom == atoms[AtomGo]) {
961                                 arg.v = getatom(c, AtomGo);
962                                 loaduri(c, &arg);
963
964                                 return GDK_FILTER_REMOVE;
965                         }
966                 }
967         }
968         return GDK_FILTER_CONTINUE;
969 }
970
971 static void
972 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
973         c->progress = webkit_web_view_get_progress(c->view) * 100;
974         updatetitle(c);
975 }
976
977 static void
978 reload(Client *c, const Arg *arg) {
979         gboolean nocache = *(gboolean *)arg;
980         if(nocache) {
981                  webkit_web_view_reload_bypass_cache(c->view);
982         } else {
983                  webkit_web_view_reload(c->view);
984         }
985 }
986
987 static void
988 scroll_h(Client *c, const Arg *arg) {
989         scroll(gtk_scrolled_window_get_hadjustment(
990                                 GTK_SCROLLED_WINDOW(c->scroll)), arg);
991 }
992
993 static void
994 scroll_v(Client *c, const Arg *arg) {
995         scroll(gtk_scrolled_window_get_vadjustment(
996                                 GTK_SCROLLED_WINDOW(c->scroll)), arg);
997 }
998
999 static void
1000 scroll(GtkAdjustment *a, const Arg *arg) {
1001         gdouble v;
1002
1003         v = gtk_adjustment_get_value(a);
1004         switch (arg->i){
1005         case +10000:
1006         case -10000:
1007                 v += gtk_adjustment_get_page_increment(a) *
1008                         (arg->i / 10000);
1009                 break;
1010         case +20000:
1011         case -20000:
1012         default:
1013                 v += gtk_adjustment_get_step_increment(a) * arg->i;
1014         }
1015
1016         v = MAX(v, 0.0);
1017         v = MIN(v, gtk_adjustment_get_upper(a) -
1018                         gtk_adjustment_get_page_size(a));
1019         gtk_adjustment_set_value(a, v);
1020 }
1021
1022 static void
1023 setatom(Client *c, int a, const char *v) {
1024         XSync(dpy, False);
1025         XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1026                         atoms[a], XA_STRING, 8, PropModeReplace,
1027                         (unsigned char *)v, strlen(v) + 1);
1028 }
1029
1030 static void
1031 setup(void) {
1032         char *proxy;
1033         char *new_proxy;
1034         SoupURI *puri;
1035         SoupSession *s;
1036
1037         /* clean up any zombies immediately */
1038         sigchld(0);
1039         gtk_init(NULL, NULL);
1040
1041         dpy = GDK_DISPLAY();
1042
1043         /* atoms */
1044         atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1045         atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1046         atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1047
1048         /* dirs and files */
1049         cookiefile = buildpath(cookiefile);
1050         scriptfile = buildpath(scriptfile);
1051         stylefile = buildpath(stylefile);
1052
1053         /* request handler */
1054         s = webkit_get_default_session();
1055
1056         /* cookie jar */
1057         soup_session_add_feature(s,
1058                         SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1059                                         FALSE)));
1060
1061         /* ssl */
1062         g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
1063         g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1064
1065         /* proxy */
1066         if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1067                 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1068                         g_strdup_printf("http://%s", proxy);
1069                 puri = soup_uri_new(new_proxy);
1070                 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1071                 soup_uri_free(puri);
1072                 g_free(new_proxy);
1073                 usingproxy = 1;
1074         }
1075 }
1076
1077 static void
1078 sigchld(int unused) {
1079         if(signal(SIGCHLD, sigchld) == SIG_ERR)
1080                 die("Can't install SIGCHLD handler");
1081         while(0 < waitpid(-1, NULL, WNOHANG));
1082 }
1083
1084 static void
1085 source(Client *c, const Arg *arg) {
1086         Arg a = { .b = FALSE };
1087         gboolean s;
1088
1089         s = webkit_web_view_get_view_source_mode(c->view);
1090         webkit_web_view_set_view_source_mode(c->view, !s);
1091         reload(c, &a);
1092 }
1093
1094 static void
1095 spawn(Client *c, const Arg *arg) {
1096         if(fork() == 0) {
1097                 if(dpy)
1098                         close(ConnectionNumber(dpy));
1099                 setsid();
1100                 execvp(((char **)arg->v)[0], (char **)arg->v);
1101                 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1102                 perror(" failed");
1103                 exit(0);
1104         }
1105 }
1106
1107 static void
1108 eval(Client *c, const Arg *arg) {
1109         WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1110         evalscript(webkit_web_frame_get_global_context(frame),
1111                         ((char **)arg->v)[0], "");
1112 }
1113
1114 static void
1115 stop(Client *c, const Arg *arg) {
1116         webkit_web_view_stop_loading(c->view);
1117 }
1118
1119 static void
1120 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1121         c->title = copystr(&c->title, t);
1122         updatetitle(c);
1123 }
1124
1125 static void
1126 toggle(Client *c, const Arg *arg) {
1127         WebKitWebSettings *settings;
1128         char *name = (char *)arg->v;
1129         gboolean value;
1130         Arg a = { .b = FALSE };
1131
1132         settings = webkit_web_view_get_settings(c->view);
1133         g_object_get(G_OBJECT(settings), name, &value, NULL);
1134         g_object_set(G_OBJECT(settings), name, !value, NULL);
1135
1136         reload(c, &a);
1137 }
1138
1139 static void
1140 twitch(Client *c, const Arg *arg) {
1141         GtkAdjustment *a;
1142         gdouble v;
1143
1144         a = gtk_scrolled_window_get_vadjustment(
1145                         GTK_SCROLLED_WINDOW(c->scroll));
1146
1147         v = gtk_adjustment_get_value(a);
1148
1149         v += arg->i;
1150
1151         v = MAX(v, 0.0);
1152         v = MIN(v, gtk_adjustment_get_upper(a) -
1153                         gtk_adjustment_get_page_size(a));
1154         gtk_adjustment_set_value(a, v);
1155 }
1156
1157 static void
1158 togglegeolocation(Client *c, const Arg *arg) {
1159         Arg a = { .b = FALSE };
1160
1161         allowgeolocation ^= 1;
1162
1163         reload(c, &a);
1164 }
1165
1166 static void
1167 togglescrollbars(Client *c, const Arg *arg) {
1168         GtkPolicyType vspolicy;
1169         Arg a;
1170
1171         gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1172
1173         if(vspolicy == GTK_POLICY_AUTOMATIC) {
1174                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1175                                 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1176         } else {
1177                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1178                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1179                 a.i = +1;
1180                 twitch(c, &a);
1181                 a.i = -1;
1182                 twitch(c, &a);
1183         }
1184 }
1185
1186 static void
1187 togglestyle(Client *c, const Arg *arg) {
1188         WebKitWebSettings *settings;
1189         char *uri;
1190
1191         settings = webkit_web_view_get_settings(c->view);
1192         g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1193         uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, NULL);
1194         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1195
1196         updatetitle(c);
1197 }
1198
1199 static void
1200 gettogglestat(Client *c){
1201         gboolean value;
1202         char *uri;
1203         WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1204
1205         g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1206                         &value, NULL);
1207         togglestat[0] = value? 'C': 'c';
1208
1209         togglestat[1] = allowgeolocation? 'G': 'g';
1210
1211         g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1212         togglestat[2] = value? 'I': 'i';
1213
1214         g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1215         togglestat[3] = value? 'S': 's';
1216
1217         g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1218         togglestat[4] = value? 'V': 'v';
1219
1220         g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1221         togglestat[5] = uri[0] ? 'M': 'm';
1222
1223         togglestat[6] = '\0';
1224 }
1225
1226 static void
1227 getpagestat(Client *c) {
1228         const char *uri = geturi(c);
1229
1230         if(strstr(uri, "https://") == uri) {
1231                 pagestat[0] = c->sslfailed ? 'U' : 'T';
1232         } else {
1233                 pagestat[0] = '-';
1234         }
1235
1236         pagestat[1] = usingproxy ? 'P' : '-';
1237         pagestat[2] = '\0';
1238
1239 }
1240
1241 static void
1242 updatetitle(Client *c) {
1243         char *t;
1244
1245         if(showindicators) {
1246                 gettogglestat(c);
1247                 getpagestat(c);
1248
1249                 if(c->linkhover) {
1250                         t = g_strdup_printf("%s:%s | %s", togglestat,
1251                                         pagestat, c->linkhover);
1252                 } else if(c->progress != 100) {
1253                         t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1254                                         togglestat, pagestat, c->title);
1255                 } else {
1256                         t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1257                                         c->title);
1258                 }
1259
1260                 gtk_window_set_title(GTK_WINDOW(c->win), t);
1261                 g_free(t);
1262         } else {
1263                 gtk_window_set_title(GTK_WINDOW(c->win), c->title);
1264         }
1265 }
1266
1267 static void
1268 updatewinid(Client *c) {
1269         snprintf(winid, LENGTH(winid), "%u",
1270                         (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1271 }
1272
1273 static void
1274 usage(void) {
1275         die("usage: %s [-biknpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1276                 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1277 }
1278
1279 static void
1280 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1281                 JSObjectRef win, Client *c) {
1282         runscript(frame);
1283 }
1284
1285 static void
1286 zoom(Client *c, const Arg *arg) {
1287         c->zoomed = TRUE;
1288         if(arg->i < 0) {
1289                 /* zoom out */
1290                 webkit_web_view_zoom_out(c->view);
1291         } else if(arg->i > 0) {
1292                 /* zoom in */
1293                 webkit_web_view_zoom_in(c->view);
1294         } else {
1295                 /* reset */
1296                 c->zoomed = FALSE;
1297                 webkit_web_view_set_zoom_level(c->view, 1.0);
1298         }
1299 }
1300
1301 int
1302 main(int argc, char *argv[]) {
1303         Arg arg;
1304
1305         memset(&arg, 0, sizeof(arg));
1306
1307         /* command line args */
1308         ARGBEGIN {
1309         case 'b':
1310                 enablescrollbars = 0;
1311                 break;
1312         case 'B':
1313                 enablescrollbars = 1;
1314                 break;
1315         case 'c':
1316                 cookiefile = EARGF(usage());
1317                 break;
1318         case 'e':
1319                 embed = strtol(EARGF(usage()), NULL, 0);
1320                 break;
1321         case 'g':
1322                 allowgeolocation = 0;
1323                 break;
1324         case 'G':
1325                 allowgeolocation = 1;
1326                 break;
1327         case 'i':
1328                 loadimages = 0;
1329                 break;
1330         case 'I':
1331                 loadimages = 1;
1332                 break;
1333         case 'k':
1334                 kioskmode = 0;
1335                 break;
1336         case 'K':
1337                 kioskmode = 1;
1338                 break;
1339         case 'n':
1340                 enableinspector = 0;
1341                 break;
1342         case 'N':
1343                 enableinspector = 1;
1344                 break;
1345         case 'p':
1346                 enableplugins = 0;
1347                 break;
1348         case 'P':
1349                 enableplugins = 1;
1350                 break;
1351         case 'r':
1352                 scriptfile = EARGF(usage());
1353                 break;
1354         case 's':
1355                 enablescripts = 0;
1356                 break;
1357         case 'S':
1358                 enablescripts = 1;
1359                 break;
1360         case 't':
1361                 stylefile = EARGF(usage());
1362                 break;
1363         case 'u':
1364                 useragent = EARGF(usage());
1365                 break;
1366         case 'v':
1367                 die("surf-"VERSION", ©2009-2012 surf engineers, "
1368                                 "see LICENSE for details\n");
1369         case 'x':
1370                 showxid = TRUE;
1371                 break;
1372         default:
1373                 usage();
1374         } ARGEND;
1375         if(argc > 0)
1376                 arg.v = argv[0];
1377
1378         setup();
1379         newclient();
1380         if(arg.v)
1381                 loaduri(clients, &arg);
1382
1383         gtk_main();
1384         cleanup();
1385
1386         return EXIT_SUCCESS;
1387 }
1388