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