usercss works again.
[surf.git] / surf.c
1 /* See LICENSE file for copyright and license details.
2  *
3  * To understand surf, start reading main().
4  */
5 #include <X11/X.h>
6 #include <X11/Xatom.h>
7 #include <gtk/gtk.h>
8 #include <gdk/gdkx.h>
9 #include <gdk/gdk.h>
10 #include <gdk/gdkkeysyms.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <getopt.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <webkit/webkit.h>
17 #include <glib/gstdio.h>
18
19 #define LENGTH(x)               (sizeof x / sizeof x[0])
20 #define CLEANMASK(mask)         (mask & ~(GDK_MOD2_MASK))
21
22 Display *dpy;
23 Atom urlprop;
24 typedef union Arg Arg;
25 union Arg {
26         const gboolean b;
27         const gint i;
28         const void *v;
29 } ;
30
31 typedef struct Client {
32         GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar, *indicator;
33         WebKitWebView *view;
34         WebKitDownload *download;
35         gchar *title;
36         gint progress;
37         struct Client *next;
38 } Client;
39
40 typedef struct Cookie {
41         char *name;
42         char *value;
43         char *domain;
44         char *path;
45         struct Cookie *next;
46 } Cookie;
47
48 typedef enum {
49     BROWSER = 0x0001,
50     SEARCHBAR = 0x0010,
51     URLBAR = 0x0100,
52     ALWAYS = ~0,
53 } KeyFocus;
54
55 typedef struct {
56         guint mod;
57         guint keyval;
58         void (*func)(Client *c, const Arg *arg);
59         const Arg arg;
60         KeyFocus focus;
61 } Key;
62
63 static SoupCookieJar *cookiejar;
64 static SoupSession *session;
65 static Client *clients = NULL;
66 /*static Cookie *cookies = NULL;*/
67 static GdkNativeWindow embed = 0;
68 static gboolean showxid = FALSE;
69 static gboolean ignore_once = FALSE;
70 static gchar *workdir;
71 extern char *optarg;
72 extern gint optind;
73
74 static void cleanup(void);
75 static void clipboard(Client *c, const Arg *arg);
76 static gchar *copystr(gchar **str, const gchar *src);
77 static void destroyclient(Client *c);
78 static void destroywin(GtkWidget* w, Client *c);
79 static void die(char *str);
80 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
81 static void drawindicator(Client *c);
82 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
83 static gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c);
84 static gchar *geturi(Client *c);
85 static void hidesearch(Client *c, const Arg *arg);
86 static void hideurl(Client *c, const Arg *arg);
87 static gboolean keypress(GtkWidget* w, GdkEventKey *ev, Client *c);
88 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c);
89 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c);
90 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c);
91 static void loadfile(Client *c, const gchar *f);
92 static void loaduri(Client *c, const Arg *arg);
93 static void navigate(Client *c, const Arg *arg);
94 static Client *newclient(void);
95 static WebKitWebView *newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c);
96 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
97 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
98 static void print(Client *c, const Arg *arg);
99 static void proccookies(SoupMessage *m, Client *c);
100 static void progresschange(WebKitWebView *view, gint p, Client *c);
101 static void request(SoupSession *s, SoupMessage *m, Client *c);
102 static void reload(Client *c, const Arg *arg);
103 static void rereadcookies(void);
104 static void setcookie(char *name, char *val, char *dom, char *path, long exp);
105 static void setup(void);
106 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame,
107                 const gchar* title, Client *c);
108 static void scroll(Client *c, const Arg *arg);
109 static void searchtext(Client *c, const Arg *arg);
110 static void source(Client *c, const Arg *arg);
111 static void showsearch(Client *c, const Arg *arg);
112 static void showurl(Client *c, const Arg *arg);
113 static void stop(Client *c, const Arg *arg);
114 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
115 static gboolean unfocusbar(GtkWidget *w, GdkEventFocus *e, Client *c);
116 static void usage(void);
117 static void update(Client *c);
118 static void zoom(Client *c, const Arg *arg);
119
120 #include "config.h"
121
122 void
123 cleanup(void) {
124         while(clients)
125                 destroyclient(clients);
126         g_free(workdir);
127 }
128
129 void
130 clipboard(Client *c, const Arg *arg) {
131         gboolean paste = *(gboolean *)arg;
132
133         if(paste)
134                 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
135         else
136                 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
137 }
138
139 gchar *
140 copystr(gchar **str, const gchar *src) {
141         gchar *tmp;
142         tmp = g_strdup(src);
143
144         if(str && *str) {
145                 g_free(*str);
146                 *str = tmp;
147         }
148         return tmp;
149 }
150
151 void
152 destroyclient(Client *c) {
153         Client *p;
154
155         gtk_widget_destroy(GTK_WIDGET(webkit_web_view_new()));
156         gtk_widget_destroy(c->scroll);
157         gtk_widget_destroy(c->urlbar);
158         gtk_widget_destroy(c->searchbar);
159         gtk_widget_destroy(c->vbox);
160         gtk_widget_destroy(c->win);
161         for(p = clients; p && p->next != c; p = p->next);
162         if(p)
163                 p->next = c->next;
164         else
165                 clients = c->next;
166         free(c);
167         if(clients == NULL)
168                 gtk_main_quit();
169 }
170
171 void
172 destroywin(GtkWidget* w, Client *c) {
173         destroyclient(c);
174 }
175
176 void
177 die(char *str) {
178         fputs(str, stderr);
179         exit(EXIT_FAILURE);
180 }
181
182 void
183 drawindicator(Client *c) {
184         gint width;
185         gchar *uri;
186         GtkWidget *w;
187         GdkGC *gc;
188         GdkColor fg;
189
190         uri = geturi(c);
191         w = c->indicator;
192         width = c->progress * w->allocation.width / 100;
193         gc = gdk_gc_new(w->window);
194         gdk_color_parse(strstr(uri, "https://") == uri ?
195                         progress_trust : progress, &fg);
196         gdk_gc_set_rgb_fg_color(gc, &fg);
197         gdk_draw_rectangle(w->window,
198                         w->style->bg_gc[GTK_WIDGET_STATE(w)],
199                         TRUE, 0, 0, w->allocation.width, w->allocation.height);
200         gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
201                         w->allocation.height);
202         g_object_unref(gc);
203 }
204
205 gboolean
206 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
207         drawindicator(c);
208         return TRUE;
209 }
210
211 void
212 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
213         WebKitDownloadStatus status;
214
215         status = webkit_download_get_status(c->download);
216         if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
217                 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
218         }
219         update(c);
220 }
221
222 gboolean
223 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
224         const gchar *filename;
225         gchar *uri, *path, *html;
226
227         stop(c, NULL);
228         c->download = o;
229         filename = webkit_download_get_suggested_filename(o);
230         path = g_build_filename(workdir, "dl", 
231                         filename, NULL);
232         uri = g_strconcat("file://", path, NULL);
233         webkit_download_set_destination_uri(c->download, uri);
234         c->progress = 0;
235         g_free(uri);
236         html = g_strdup_printf("Download <b>%s</b>...", filename);
237         webkit_web_view_load_html_string(c->view, html,
238                         webkit_download_get_uri(c->download));
239         g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
240         g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
241         webkit_download_start(c->download);
242         
243         c->title = copystr(&c->title, filename);
244         update(c);
245         g_free(html);
246         return TRUE;
247 }
248
249 gchar *
250 geturi(Client *c) {
251         gchar *uri;
252
253         if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
254                 uri = copystr(NULL, "about:blank");
255         return uri;
256 }
257
258 void
259 hidesearch(Client *c, const Arg *arg) {
260         gtk_widget_hide(c->searchbar);
261         gtk_widget_grab_focus(GTK_WIDGET(c->view));
262 }
263
264 void
265 hideurl(Client *c, const Arg *arg) {
266         gtk_widget_hide(c->urlbar);
267         gtk_widget_grab_focus(GTK_WIDGET(c->view));
268 }
269
270 gboolean
271 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
272         guint i, focus;
273         gboolean processed = FALSE;
274
275         if(ev->type != GDK_KEY_PRESS)
276                 return FALSE;
277         if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
278                 focus = SEARCHBAR;
279         else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
280                 focus = URLBAR;
281         else
282                 focus = BROWSER;
283         for(i = 0; i < LENGTH(keys); i++) {
284                 if(focus & keys[i].focus
285                                 && gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
286                                 && CLEANMASK(ev->state) == keys[i].mod
287                                 && keys[i].func) {
288                         keys[i].func(c, &(keys[i].arg));
289                         processed = TRUE;
290                 }
291         }
292         return processed;
293 }
294
295 void
296 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c) {
297         if(l)
298                 gtk_window_set_title(GTK_WINDOW(c->win), l);
299         else
300                 update(c);
301 }
302
303 void
304 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
305         gchar *uri;
306
307         ignore_once = TRUE;
308         uri = geturi(c);
309         XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
310                         XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
311                         strlen(uri) + 1);
312 }
313
314 void
315 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
316         c->progress = 0;
317         update(c);
318 }
319
320 void
321 loadfile(Client *c, const gchar *f) {
322         GIOChannel *chan = NULL;
323         GError *e = NULL;
324         GString *code;
325         gchar *line, *uri;
326         Arg arg;
327
328         if(strcmp(f, "-") == 0) {
329                 chan = g_io_channel_unix_new(STDIN_FILENO);
330                 if (chan) {
331                         code = g_string_new("");
332                         while(g_io_channel_read_line(chan, &line, NULL, NULL,
333                                                 &e) == G_IO_STATUS_NORMAL) {
334                                 g_string_append(code, line);
335                                 g_free(line);
336                         }
337                         webkit_web_view_load_html_string(c->view, code->str,
338                                         "file://.");
339                         g_io_channel_shutdown(chan, FALSE, NULL);
340                         g_string_free(code, TRUE);
341                 }
342                 arg.v = uri = g_strdup("stdin");
343         }
344         else {
345                 arg.v = uri = g_strdup_printf("file://%s", f);
346                 loaduri(c, &arg);
347         }
348         c->title = copystr(&c->title, uri);
349         update(c);
350         g_free(uri);
351 }
352
353 void
354 loaduri(Client *c, const Arg *arg) {
355         gchar *u;
356         const gchar *uri = (gchar *)arg->v;
357         if(!uri)
358                 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
359         u = g_strrstr(uri, "://") ? g_strdup(uri)
360                 : g_strdup_printf("http://%s", uri);
361         webkit_web_view_load_uri(c->view, u);
362         c->progress = 0;
363         c->title = copystr(&c->title, u);
364         g_free(u);
365         update(c);
366 }
367
368 void
369 navigate(Client *c, const Arg *arg) {
370         gint steps = *(gint *)arg;
371         webkit_web_view_go_back_or_forward(c->view, steps);
372 }
373
374 Client *
375 newclient(void) {
376         Client *c;
377         WebKitWebSettings *settings;
378         gchar *filename;
379
380         if(!(c = calloc(1, sizeof(Client))))
381                 die("Cannot malloc!\n");
382         /* Window */
383         if(embed) {
384                 c->win = gtk_plug_new(embed);
385         }
386         else {
387                 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
388                 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
389         }
390         gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
391         g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
392         g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
393
394         /* VBox */
395         c->vbox = gtk_vbox_new(FALSE, 0);
396
397         /* scrolled window */
398         c->scroll = gtk_scrolled_window_new(NULL, NULL);
399         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
400                         GTK_POLICY_NEVER, GTK_POLICY_NEVER);
401
402         /* webview */
403         c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
404         g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
405         g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
406         g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
407         g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
408         g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
409         g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c);
410         g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
411         g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
412
413         /* urlbar */
414         c->urlbar = gtk_entry_new();
415         gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
416         g_signal_connect(G_OBJECT(c->urlbar), "focus-out-event", G_CALLBACK(unfocusbar), c);
417
418         /* searchbar */
419         c->searchbar = gtk_entry_new();
420         gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
421         g_signal_connect(G_OBJECT(c->searchbar), "focus-out-event", G_CALLBACK(unfocusbar), c);
422
423         /* indicator */
424         c->indicator = gtk_drawing_area_new();
425         gtk_widget_set_size_request(c->indicator, 0, 2);
426         g_signal_connect (G_OBJECT (c->indicator), "expose_event",
427                         G_CALLBACK (exposeindicator), c);
428
429         /* downloadbar */
430
431         /* Arranging */
432         gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
433         gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
434         gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
435         gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
436         gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
437         gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
438
439         /* Setup */
440         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
441         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
442         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
443         gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
444         gtk_widget_grab_focus(GTK_WIDGET(c->view));
445         gtk_widget_hide_all(c->searchbar);
446         gtk_widget_hide_all(c->urlbar);
447         gtk_widget_show(c->vbox);
448         gtk_widget_show(c->indicator);
449         gtk_widget_show(c->scroll);
450         gtk_widget_show(GTK_WIDGET(c->view));
451         gtk_widget_show(c->win);
452         gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
453         gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
454         webkit_web_view_set_full_content_zoom(c->view, TRUE);
455         settings = webkit_web_view_get_settings(c->view);
456         g_object_set (G_OBJECT(settings), "user-agent", "surf", NULL);
457         filename = g_build_filename(workdir, "style.css", NULL);
458         filename = g_strdup_printf("file://%s", filename);
459         g_object_set (G_OBJECT(settings), "user-stylesheet-uri", filename, NULL);
460
461         c->download = NULL;
462         c->title = NULL;
463         c->next = clients;
464         clients = c;
465         if(showxid) {
466                 gdk_display_sync(gtk_widget_get_display(c->win));
467                 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
468                 fflush(NULL);
469         }
470         return c;
471 }
472
473 WebKitWebView *
474 newwindow(WebKitWebView  *v, WebKitWebFrame *f, Client *c) {
475         Client *n = newclient();
476         return n->view;
477 }
478
479  
480 void
481 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
482         Arg arg = {.v = text };
483         if(text != NULL)
484                 loaduri((Client *) d, &arg);
485 }
486
487 GdkFilterReturn
488 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
489         Client *c = (Client *)d;
490         XPropertyEvent *ev;
491         Atom adummy;
492         gint idummy;
493         unsigned long ldummy;
494         unsigned char *buf = NULL;
495         Arg arg;
496
497         if(((XEvent *)e)->type == PropertyNotify) {
498                 ev = &((XEvent *)e)->xproperty;
499                 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
500                         if(ignore_once)
501                                ignore_once = FALSE;
502                         else {
503                                 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
504                                         &adummy, &idummy, &ldummy, &ldummy, &buf);
505                                 arg.v = buf;
506                                 loaduri(c, &arg);
507                                 XFree(buf);
508                         }
509                         return GDK_FILTER_REMOVE;
510                 }
511         }
512         return GDK_FILTER_CONTINUE;
513 }
514
515 void
516 print(Client *c, const Arg *arg) {
517         webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
518 }
519
520 void
521 proccookies(SoupMessage *m, Client *c) {
522         GSList *l;
523         SoupCookie *co;
524         long t;
525
526         rereadcookies();
527         for (l = soup_cookies_from_response(m); l; l = l->next){
528                 co = (SoupCookie *)l->data;
529                 t = co->expires ?  soup_date_to_time_t(co->expires) : 0;
530                 setcookie(co->name, co->value, co->domain, co->value, t);
531         }
532         g_slist_free(l);
533 }
534
535 void
536 progresschange(WebKitWebView* view, gint p, Client *c) {
537         c->progress = p;
538         update(c);
539 }
540
541 void
542 request(SoupSession *s, SoupMessage *m, Client *c) {
543         soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
544                         G_CALLBACK(proccookies), c);
545 }
546
547 void
548 reload(Client *c, const Arg *arg) {
549         gboolean nocache = *(gboolean *)arg;
550         if(nocache)
551                  webkit_web_view_reload_bypass_cache(c->view);
552         else
553                  webkit_web_view_reload(c->view);
554 }
555
556 void
557 rereadcookies(void) {
558         const gchar *filename;
559
560         filename = g_build_filename(workdir, "cookies", NULL);
561 }
562
563 void
564 scroll(Client *c, const Arg *arg) {
565         gdouble v;
566         GtkAdjustment *a;
567
568         a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
569         v = gtk_adjustment_get_value(a);
570         v += gtk_adjustment_get_step_increment(a) * arg->i;
571         v = MAX(v, 0.0);
572         v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
573         gtk_adjustment_set_value(a, v);
574 }
575
576 void
577 setcookie(char *name, char *val, char *dom, char *path, long exp) {
578
579 }
580
581 void
582 setup(void) {
583         const gchar *home, *name;
584         SoupSession *s;
585         FILE *tmp;
586
587         dpy = GDK_DISPLAY();
588         session = webkit_get_default_session();
589         urlprop = XInternAtom(dpy, "_SURF_URL", False);
590
591
592         /* create dirs and files */
593         home = g_get_home_dir();
594         workdir = g_strdup(g_build_filename(home, ".surf", NULL));
595         g_mkdir_with_parents(workdir, 0755);
596         name = g_build_filename(workdir, "dl", NULL);
597         g_mkdir(name, 0755);
598         name = g_build_filename(workdir, "style.css", NULL);
599         if((tmp = g_fopen(name, "a")));
600                 fclose(tmp);
601
602
603         /* cookie persistance */
604         s = webkit_get_default_session();
605         name = g_build_filename(workdir, "cookies.txt", NULL);
606         cookiejar = soup_cookie_jar_text_new(name, FALSE);
607         soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
608 }
609
610 void
611 showsearch(Client *c, const Arg *arg) {
612         hideurl(c, NULL);
613         gtk_widget_show(c->searchbar);
614         gtk_widget_grab_focus(c->searchbar);
615 }
616
617 void
618 source(Client *c, const Arg *arg) {
619         Arg a = { .b = FALSE };
620         /*gboolean s;
621
622         s = webkit_web_view_get_view_source_mode(c->view);
623         webkit_web_view_set_view_source_mode(c->view, c->source);*/
624         reload(c, &a);
625 }
626
627 void
628 searchtext(Client *c, const Arg *arg) {
629         gboolean forward = *(gboolean *)arg;
630         webkit_web_view_search_text(c->view,
631                         gtk_entry_get_text(GTK_ENTRY(c->searchbar)),
632                         FALSE,
633                         forward,
634                         TRUE);
635 }
636
637 void
638 showurl(Client *c, const Arg *arg) {
639         gchar *uri;
640
641         hidesearch(c, NULL);
642         uri = geturi(c);
643         gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
644         gtk_widget_show(c->urlbar);
645         gtk_widget_grab_focus(c->urlbar);
646 }
647
648 void
649 stop(Client *c, const Arg *arg) {
650         if(c->download)
651                 webkit_download_cancel(c->download);
652         else
653                 webkit_web_view_stop_loading(c->view);
654         c->download = NULL;
655 }
656
657 void
658 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
659         c->title = copystr(&c->title, t);
660         update(c);
661 }
662
663 gboolean
664 unfocusbar(GtkWidget *w, GdkEventFocus *e, Client *c) {
665         hidesearch(c, NULL);
666         hideurl(c, NULL);
667         return FALSE;
668 }
669
670 void
671 usage(void) {
672         fputs("surf - simple browser\n", stderr);
673         die("usage: surf [-e Window] [-x] [uri]\n");
674 }
675
676 void
677 update(Client *c) {
678         gchar *t;
679
680         if(c->progress == 100)
681                 t = g_strdup(c->title);
682         else
683                 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
684         drawindicator(c);
685         gtk_window_set_title(GTK_WINDOW(c->win), t);
686         g_free(t);
687
688 }
689
690 void
691 zoom(Client *c, const Arg *arg) {
692         if(arg->i < 0)          /* zoom out */
693                 webkit_web_view_zoom_out(c->view);
694         else if(arg->i > 0)     /* zoom in */
695                 webkit_web_view_zoom_in(c->view);
696         else                    /* reset */
697                 webkit_web_view_set_zoom_level(c->view, 1.0);
698 }
699
700 int main(int argc, char *argv[]) {
701         Client *c;
702         gint o, a;
703         Arg arg;
704
705         gtk_init(NULL, NULL);
706         if (!g_thread_supported())
707                 g_thread_init(NULL);
708         while((o = getopt(argc, argv, "vhxe:")) != -1)
709                 switch(o) {
710                 case 'x':
711                         showxid = TRUE;
712                         break;
713                 case 'e':
714                         if(!(a = atoi(optarg)))
715                                 usage();
716                         embed = a;
717                         break;
718                 case 'v':
719                         die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
720                         break;
721                 default:
722                         usage();
723                 }
724         setup();
725         if(optind + 1 == argc) {
726                 c = newclient();
727                 arg.v = argv[optind];
728                 if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0)
729                         loadfile(c, argv[optind]);
730                 else
731                         loaduri(c, &arg);
732
733         }
734         else if(optind != argc)
735                 usage();
736         if(!clients)
737                 newclient();
738
739
740         gtk_main();
741         cleanup();
742         return EXIT_SUCCESS;
743 }