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