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