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