applied Nick's 96dpi patch, thanks
[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         /* While stupid, CSS specifies that a pixel represents 1/96 of an inch.
808          * This ensures websites are not unusably small with a high DPI screen.
809          * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting. */
810         if(zoomto96dpi) {
811                 screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
812                 dpi = gdk_screen_get_resolution(screen);
813                 if(dpi != -1) {
814                         g_object_set(G_OBJECT(settings), "enforce-96-dpi", true,
815                                         NULL);
816                         webkit_web_view_set_zoom_level(c->view, dpi/96);
817                 }
818         }
819
820         if(enableinspector) {
821                 c->inspector = WEBKIT_WEB_INSPECTOR(
822                                 webkit_web_view_get_inspector(c->view));
823                 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
824                                 G_CALLBACK(inspector_new), c);
825                 g_signal_connect(G_OBJECT(c->inspector), "show-window",
826                                 G_CALLBACK(inspector_show), c);
827                 g_signal_connect(G_OBJECT(c->inspector), "close-window",
828                                 G_CALLBACK(inspector_close), c);
829                 g_signal_connect(G_OBJECT(c->inspector), "finished",
830                                 G_CALLBACK(inspector_finished), c);
831                 c->isinspecting = false;
832         }
833
834         g_free(uri);
835
836         setatom(c, AtomFind, "");
837         setatom(c, AtomUri, "about:blank");
838         if(hidebackground)
839                 webkit_web_view_set_transparent(c->view, TRUE);
840
841         c->title = NULL;
842         c->next = clients;
843         clients = c;
844
845         if(showxid) {
846                 gdk_display_sync(gtk_widget_get_display(c->win));
847                 printf("%u\n",
848                         (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
849                 fflush(NULL);
850                 if (fclose(stdout) != 0) {
851                         die("Error closing stdout");
852                 }
853         }
854
855         return c;
856 }
857
858 static void
859 newwindow(Client *c, const Arg *arg, gboolean noembed) {
860         guint i = 0;
861         const char *cmd[14], *uri;
862         const Arg a = { .v = (void *)cmd };
863         char tmp[64];
864
865         cmd[i++] = argv0;
866         if(!enablescrollbars)
867                 cmd[i++] = "-b";
868         if(embed && !noembed) {
869                 cmd[i++] = "-e";
870                 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
871                 cmd[i++] = tmp;
872         }
873         if(!loadimages)
874                 cmd[i++] = "-i";
875         if(kioskmode)
876                 cmd[i++] = "-k";
877         if(!enableplugins)
878                 cmd[i++] = "-p";
879         if(!enablescripts)
880                 cmd[i++] = "-s";
881         if(showxid)
882                 cmd[i++] = "-x";
883         cmd[i++] = "-c";
884         cmd[i++] = cookiefile;
885         cmd[i++] = "--";
886         uri = arg->v ? (char *)arg->v : c->linkhover;
887         if(uri)
888                 cmd[i++] = uri;
889         cmd[i++] = NULL;
890         spawn(NULL, &a);
891 }
892
893 static void
894 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
895         GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
896
897         for(GList *l = items; l; l = l->next) {
898                 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
899         }
900
901         g_list_free(items);
902 }
903
904 static void
905 popupactivate(GtkMenuItem *menu, Client *c) {
906         /*
907          * context-menu-action-2000     open link
908          * context-menu-action-1        open link in window
909          * context-menu-action-2        download linked file
910          * context-menu-action-3        copy link location
911          * context-menu-action-13       reload
912          * context-menu-action-10       back
913          * context-menu-action-11       forward
914          * context-menu-action-12       stop
915          */
916
917         GtkAction *a = NULL;
918         const char *name;
919         GtkClipboard *prisel;
920
921         a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
922         if(a == NULL)
923                 return;
924
925         name = gtk_action_get_name(a);
926         if(!g_strcmp0(name, "context-menu-action-3")) {
927                 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
928                 gtk_clipboard_set_text(prisel, c->linkhover, -1);
929         }
930 }
931
932 static void
933 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
934         Arg arg = {.v = text };
935         if(text != NULL)
936                 loaduri((Client *) d, &arg);
937 }
938
939 static void
940 print(Client *c, const Arg *arg) {
941         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
942 }
943
944 static GdkFilterReturn
945 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
946         Client *c = (Client *)d;
947         XPropertyEvent *ev;
948         Arg arg;
949
950         if(((XEvent *)e)->type == PropertyNotify) {
951                 ev = &((XEvent *)e)->xproperty;
952                 if(ev->state == PropertyNewValue) {
953                         if(ev->atom == atoms[AtomFind]) {
954                                 arg.b = TRUE;
955                                 find(c, &arg);
956
957                                 return GDK_FILTER_REMOVE;
958                         } else if(ev->atom == atoms[AtomGo]) {
959                                 arg.v = getatom(c, AtomGo);
960                                 loaduri(c, &arg);
961
962                                 return GDK_FILTER_REMOVE;
963                         }
964                 }
965         }
966         return GDK_FILTER_CONTINUE;
967 }
968
969 static void
970 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
971         c->progress = webkit_web_view_get_progress(c->view) * 100;
972         updatetitle(c);
973 }
974
975 static void
976 reload(Client *c, const Arg *arg) {
977         gboolean nocache = *(gboolean *)arg;
978         if(nocache) {
979                  webkit_web_view_reload_bypass_cache(c->view);
980         } else {
981                  webkit_web_view_reload(c->view);
982         }
983 }
984
985 static void
986 scroll_h(Client *c, const Arg *arg) {
987         scroll(gtk_scrolled_window_get_hadjustment(
988                                 GTK_SCROLLED_WINDOW(c->scroll)), arg);
989 }
990
991 static void
992 scroll_v(Client *c, const Arg *arg) {
993         scroll(gtk_scrolled_window_get_vadjustment(
994                                 GTK_SCROLLED_WINDOW(c->scroll)), arg);
995 }
996
997 static void
998 scroll(GtkAdjustment *a, const Arg *arg) {
999         gdouble v;
1000
1001         v = gtk_adjustment_get_value(a);
1002         switch (arg->i){
1003         case +10000:
1004         case -10000:
1005                 v += gtk_adjustment_get_page_increment(a) *
1006                         (arg->i / 10000);
1007                 break;
1008         case +20000:
1009         case -20000:
1010         default:
1011                 v += gtk_adjustment_get_step_increment(a) * arg->i;
1012         }
1013
1014         v = MAX(v, 0.0);
1015         v = MIN(v, gtk_adjustment_get_upper(a) -
1016                         gtk_adjustment_get_page_size(a));
1017         gtk_adjustment_set_value(a, v);
1018 }
1019
1020 static void
1021 setatom(Client *c, int a, const char *v) {
1022         XSync(dpy, False);
1023         XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
1024                         atoms[a], XA_STRING, 8, PropModeReplace,
1025                         (unsigned char *)v, strlen(v) + 1);
1026 }
1027
1028 static void
1029 setup(void) {
1030         char *proxy;
1031         char *new_proxy;
1032         SoupURI *puri;
1033         SoupSession *s;
1034
1035         /* clean up any zombies immediately */
1036         sigchld(0);
1037         gtk_init(NULL, NULL);
1038
1039         dpy = GDK_DISPLAY();
1040
1041         /* atoms */
1042         atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1043         atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1044         atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1045
1046         /* dirs and files */
1047         cookiefile = buildpath(cookiefile);
1048         scriptfile = buildpath(scriptfile);
1049         stylefile = buildpath(stylefile);
1050
1051         /* request handler */
1052         s = webkit_get_default_session();
1053
1054         /* cookie jar */
1055         soup_session_add_feature(s,
1056                         SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1057                                         FALSE)));
1058
1059         /* ssl */
1060         g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
1061         g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1062
1063         /* proxy */
1064         if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1065                 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1066                         g_strdup_printf("http://%s", proxy);
1067                 puri = soup_uri_new(new_proxy);
1068                 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1069                 soup_uri_free(puri);
1070                 g_free(new_proxy);
1071                 usingproxy = 1;
1072         }
1073 }
1074
1075 static void
1076 sigchld(int unused) {
1077         if(signal(SIGCHLD, sigchld) == SIG_ERR)
1078                 die("Can't install SIGCHLD handler");
1079         while(0 < waitpid(-1, NULL, WNOHANG));
1080 }
1081
1082 static void
1083 source(Client *c, const Arg *arg) {
1084         Arg a = { .b = FALSE };
1085         gboolean s;
1086
1087         s = webkit_web_view_get_view_source_mode(c->view);
1088         webkit_web_view_set_view_source_mode(c->view, !s);
1089         reload(c, &a);
1090 }
1091
1092 static void
1093 spawn(Client *c, const Arg *arg) {
1094         if(fork() == 0) {
1095                 if(dpy)
1096                         close(ConnectionNumber(dpy));
1097                 setsid();
1098                 execvp(((char **)arg->v)[0], (char **)arg->v);
1099                 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1100                 perror(" failed");
1101                 exit(0);
1102         }
1103 }
1104
1105 static void
1106 eval(Client *c, const Arg *arg) {
1107         WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1108         evalscript(webkit_web_frame_get_global_context(frame),
1109                         ((char **)arg->v)[0], "");
1110 }
1111
1112 static void
1113 stop(Client *c, const Arg *arg) {
1114         webkit_web_view_stop_loading(c->view);
1115 }
1116
1117 static void
1118 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1119         c->title = copystr(&c->title, t);
1120         updatetitle(c);
1121 }
1122
1123 static void
1124 toggle(Client *c, const Arg *arg) {
1125         WebKitWebSettings *settings;
1126         char *name = (char *)arg->v;
1127         gboolean value;
1128         Arg a = { .b = FALSE };
1129
1130         settings = webkit_web_view_get_settings(c->view);
1131         g_object_get(G_OBJECT(settings), name, &value, NULL);
1132         g_object_set(G_OBJECT(settings), name, !value, NULL);
1133
1134         reload(c, &a);
1135 }
1136
1137 static void
1138 twitch(Client *c, const Arg *arg) {
1139         GtkAdjustment *a;
1140         gdouble v;
1141
1142         a = gtk_scrolled_window_get_vadjustment(
1143                         GTK_SCROLLED_WINDOW(c->scroll));
1144
1145         v = gtk_adjustment_get_value(a);
1146
1147         v += arg->i;
1148
1149         v = MAX(v, 0.0);
1150         v = MIN(v, gtk_adjustment_get_upper(a) -
1151                         gtk_adjustment_get_page_size(a));
1152         gtk_adjustment_set_value(a, v);
1153 }
1154
1155 static void
1156 togglegeolocation(Client *c, const Arg *arg) {
1157         Arg a = { .b = FALSE };
1158
1159         allowgeolocation ^= 1;
1160
1161         reload(c, &a);
1162 }
1163
1164 static void
1165 togglescrollbars(Client *c, const Arg *arg) {
1166         GtkPolicyType vspolicy;
1167         Arg a;
1168
1169         gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1170
1171         if(vspolicy == GTK_POLICY_AUTOMATIC) {
1172                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1173                                 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1174         } else {
1175                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1176                                 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1177                 a.i = +1;
1178                 twitch(c, &a);
1179                 a.i = -1;
1180                 twitch(c, &a);
1181         }
1182 }
1183
1184 static void
1185 togglestyle(Client *c, const Arg *arg) {
1186         WebKitWebSettings *settings;
1187         char *uri;
1188
1189         settings = webkit_web_view_get_settings(c->view);
1190         g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1191         uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, NULL);
1192         g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1193
1194         updatetitle(c);
1195 }
1196
1197 static void
1198 gettogglestat(Client *c){
1199         gboolean value;
1200         char *uri;
1201         WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1202
1203         g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1204                         &value, NULL);
1205         togglestat[0] = value? 'C': 'c';
1206
1207         togglestat[1] = allowgeolocation? 'G': 'g';
1208
1209         g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1210         togglestat[2] = value? 'I': 'i';
1211
1212         g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1213         togglestat[3] = value? 'S': 's';
1214
1215         g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1216         togglestat[4] = value? 'V': 'v';
1217
1218         g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1219         togglestat[5] = uri[0] ? 'M': 'm';
1220
1221         togglestat[6] = '\0';
1222 }
1223
1224 static void
1225 getpagestat(Client *c) {
1226         const char *uri = geturi(c);
1227
1228         if(strstr(uri, "https://") == uri) {
1229                 pagestat[0] = c->sslfailed ? 'U' : 'T';
1230         } else {
1231                 pagestat[0] = '-';
1232         }
1233
1234         pagestat[1] = usingproxy ? 'P' : '-';
1235         pagestat[2] = '\0';
1236
1237 }
1238
1239 static void
1240 updatetitle(Client *c) {
1241         char *t;
1242
1243         if(showindicators) {
1244                 gettogglestat(c);
1245                 getpagestat(c);
1246
1247                 if(c->linkhover) {
1248                         t = g_strdup_printf("%s:%s | %s", togglestat,
1249                                         pagestat, c->linkhover);
1250                 } else if(c->progress != 100) {
1251                         t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1252                                         togglestat, pagestat, c->title);
1253                 } else {
1254                         t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1255                                         c->title);
1256                 }
1257
1258                 gtk_window_set_title(GTK_WINDOW(c->win), t);
1259                 g_free(t);
1260         } else {
1261                 gtk_window_set_title(GTK_WINDOW(c->win), c->title);
1262         }
1263 }
1264
1265 static void
1266 updatewinid(Client *c) {
1267         snprintf(winid, LENGTH(winid), "%u",
1268                         (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1269 }
1270
1271 static void
1272 usage(void) {
1273         die("usage: %s [-biknpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1274                 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1275 }
1276
1277 static void
1278 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1279                 JSObjectRef win, Client *c) {
1280         runscript(frame);
1281 }
1282
1283 static void
1284 zoom(Client *c, const Arg *arg) {
1285         c->zoomed = TRUE;
1286         if(arg->i < 0) {
1287                 /* zoom out */
1288                 webkit_web_view_zoom_out(c->view);
1289         } else if(arg->i > 0) {
1290                 /* zoom in */
1291                 webkit_web_view_zoom_in(c->view);
1292         } else {
1293                 /* reset */
1294                 c->zoomed = FALSE;
1295                 webkit_web_view_set_zoom_level(c->view, 1.0);
1296         }
1297 }
1298
1299 int
1300 main(int argc, char *argv[]) {
1301         Arg arg;
1302
1303         memset(&arg, 0, sizeof(arg));
1304
1305         /* command line args */
1306         ARGBEGIN {
1307         case 'b':
1308                 enablescrollbars = 0;
1309                 break;
1310         case 'B':
1311                 enablescrollbars = 1;
1312                 break;
1313         case 'c':
1314                 cookiefile = EARGF(usage());
1315                 break;
1316         case 'e':
1317                 embed = strtol(EARGF(usage()), NULL, 0);
1318                 break;
1319         case 'g':
1320                 allowgeolocation = 0;
1321                 break;
1322         case 'G':
1323                 allowgeolocation = 1;
1324                 break;
1325         case 'i':
1326                 loadimages = 0;
1327                 break;
1328         case 'I':
1329                 loadimages = 1;
1330                 break;
1331         case 'k':
1332                 kioskmode = 0;
1333                 break;
1334         case 'K':
1335                 kioskmode = 1;
1336                 break;
1337         case 'n':
1338                 enableinspector = 0;
1339                 break;
1340         case 'N':
1341                 enableinspector = 1;
1342                 break;
1343         case 'p':
1344                 enableplugins = 0;
1345                 break;
1346         case 'P':
1347                 enableplugins = 1;
1348                 break;
1349         case 'r':
1350                 scriptfile = EARGF(usage());
1351                 break;
1352         case 's':
1353                 enablescripts = 0;
1354                 break;
1355         case 'S':
1356                 enablescripts = 1;
1357                 break;
1358         case 't':
1359                 stylefile = EARGF(usage());
1360                 break;
1361         case 'u':
1362                 useragent = EARGF(usage());
1363                 break;
1364         case 'v':
1365                 die("surf-"VERSION", ©2009-2012 surf engineers, "
1366                                 "see LICENSE for details\n");
1367         case 'x':
1368                 showxid = TRUE;
1369                 break;
1370         default:
1371                 usage();
1372         } ARGEND;
1373         if(argc > 0)
1374                 arg.v = argv[0];
1375
1376         setup();
1377         newclient();
1378         if(arg.v)
1379                 loaduri(clients, &arg);
1380
1381         gtk_main();
1382         cleanup();
1383
1384         return EXIT_SUCCESS;
1385 }
1386