1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
10 #include <gdk/gdkkeysyms.h>
16 #include <webkit/webkit.h>
17 #include <glib/gstdio.h>
19 #define LENGTH(x) (sizeof x / sizeof x[0])
23 typedef union Arg Arg;
27 const unsigned int ui;
32 typedef struct Client {
33 GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar;
35 WebKitDownload *download;
41 typedef struct Cookie {
52 void (*func)(Client *c, const Arg *arg);
54 gboolean stop; /* do not propagate keypress event/stop matching keys */
69 SoupCookieJar *cookiejar;
71 Client *clients = NULL;
72 Cookie *cookies = NULL;
73 gboolean embed = FALSE;
74 gboolean showxid = FALSE;
75 gboolean ignore_once = FALSE;
79 static void cleanup(void);
80 static void proccookies(SoupMessage *m, Client *c);
81 static void clipboard(Client *c, const Arg *arg);
82 static void destroyclient(Client *c);
83 static void destroywin(GtkWidget* w, Client *c);
84 static void die(char *str);
85 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
86 static gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c);
87 static gchar *geturi(Client *c);
88 static void hidesearch(Client *c, const Arg *arg);
89 static void hideurl(Client *c, const Arg *arg);
90 static gboolean keypress(GtkWidget* w, GdkEventKey *ev, Client *c);
91 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c);
92 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c);
93 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c);
94 static void loadfile(Client *c, const gchar *f);
95 static void loaduri(Client *c, const Arg *arg);
96 static void navigate(Client *c, const Arg *arg);
97 static Client *newclient();
98 static WebKitWebView *newwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
99 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
100 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
101 static void progresschange(WebKitWebView *view, gint p, Client *c);
102 static void request(SoupSession *s, SoupMessage *m, Client *c);
103 static void setcookie(char *name, char *val, char *dom, char *path, long exp);
104 static void reload(Client *c, const Arg *arg);
105 static void setup(void);
106 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame,
107 const gchar* title, Client *c);
108 static void searchtext(Client *c, const Arg *arg);
109 static void showsearch(Client *c, const Arg *arg);
110 static void showurl(Client *c, const Arg *arg);
111 static void stop(Client *c, const Arg *arg);
112 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
114 static void updatetitle(Client *c, const gchar *title);
115 static void zoompage(Client *c, const Arg *arg);
122 destroyclient(clients);
126 proccookies(SoupMessage *m, Client *c) {
131 for (l = soup_cookies_from_response(m); l; l = l->next){
132 co = (SoupCookie *)l->data;
133 t = co->expires ? soup_date_to_time_t(co->expires) : 0;
134 setcookie(co->name, co->value, co->domain, co->value, t);
140 clipboard(Client *c, const Arg *arg) {
141 gboolean paste = *(gboolean *)arg;
143 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
145 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
149 destroyclient(Client *c) {
152 gtk_widget_destroy(GTK_WIDGET(webkit_web_view_new()));
153 gtk_widget_destroy(c->scroll);
154 gtk_widget_destroy(c->urlbar);
155 gtk_widget_destroy(c->searchbar);
156 gtk_widget_destroy(c->vbox);
157 gtk_widget_destroy(c->win);
158 for(p = clients; p && p->next != c; p = p->next);
169 destroywin(GtkWidget* w, Client *c) {
180 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
181 WebKitDownloadStatus status;
183 status = webkit_download_get_status(c->download);
184 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
185 c->progress = (int)(webkit_download_get_progress(c->download)*100);
187 updatetitle(c, NULL);
191 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
192 const gchar *home, *filename;
193 gchar *uri, *path, *html;
197 home = g_get_home_dir();
198 filename = webkit_download_get_suggested_filename(o);
199 path = g_build_filename(home, ".surf", "dl",
201 uri = g_strconcat("file://", path, NULL);
202 webkit_download_set_destination_uri(c->download, uri);
205 html = g_strdup_printf("Download <b>%s</b>...", filename);
206 webkit_web_view_load_html_string(c->view, html,
207 webkit_download_get_uri(c->download));
208 g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
209 g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
210 webkit_download_start(c->download);
211 updatetitle(c, filename);
220 if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
221 uri = g_strdup("about:blank");
226 hidesearch(Client *c, const Arg *arg) {
227 gtk_widget_hide(c->searchbar);
228 gtk_widget_grab_focus(GTK_WIDGET(c->view));
232 hideurl(Client *c, const Arg *arg) {
233 gtk_widget_hide(c->urlbar);
234 gtk_widget_grab_focus(GTK_WIDGET(c->view));
238 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
241 if(ev->type != GDK_KEY_PRESS)
244 for(n = 0; n < LENGTH(keysets); n++)
245 switch(keysets[n].mode) {
247 if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
251 if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
257 fprintf(stderr, "keypress(): Unknown Keypressmode\n");
260 if(n < LENGTH(keysets)) {
262 for(m = 0; m < keysets[n].numkeys; m++) {
263 Key *keys = keysets[n].keys;
264 if(ev->keyval == keys[m].keyval
265 && (ev->state == keys[m].mod
266 || (ev->state & keys[m].mod))
268 keys[m].func(c, &(keys[m].arg));
278 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c) {
280 gtk_window_set_title(GTK_WINDOW(c->win), l);
282 updatetitle(c, NULL);
286 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
291 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
292 XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
297 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
299 updatetitle(c, NULL);
303 loadfile(Client *c, const gchar *f) {
304 GIOChannel *chan = NULL;
310 if(strcmp(f, "-") == 0) {
311 chan = g_io_channel_unix_new(STDIN_FILENO);
313 code = g_string_new("");
314 while(g_io_channel_read_line(chan, &line, NULL, NULL,
315 &e) == G_IO_STATUS_NORMAL) {
316 g_string_append(code, line);
319 webkit_web_view_load_html_string(c->view, code->str,
321 g_io_channel_shutdown(chan, FALSE, NULL);
322 g_string_free(code, TRUE);
324 arg.v = uri = g_strdup("stdin");
327 arg.v = uri = g_strdup_printf("file://%s", f);
335 loaduri(Client *c, const Arg *arg) {
337 const gchar *uri = (gchar *)arg->v;
339 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
340 u = g_strrstr(uri, "://") ? g_strdup(uri)
341 : g_strdup_printf("http://%s", uri);
342 webkit_web_view_load_uri(c->view, u);
349 navigate(Client *c, const Arg *arg) {
350 gboolean forward = *(gboolean *)arg;
352 webkit_web_view_go_forward(c->view);
354 webkit_web_view_go_back(c->view);
360 if(!(c = calloc(1, sizeof(Client))))
361 die("Cannot malloc!\n");
364 c->win = gtk_plug_new(0);
367 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
368 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
370 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
371 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
372 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
375 c->vbox = gtk_vbox_new(FALSE, 0);
377 /* scrolled window */
378 c->scroll = gtk_scrolled_window_new(NULL, NULL);
379 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
380 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
383 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
384 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
385 g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
386 g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
387 g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
388 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
389 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c);
390 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
391 g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
394 c->urlbar = gtk_entry_new();
395 gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
398 c->searchbar = gtk_entry_new();
399 gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
404 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
405 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
406 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
407 gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
408 gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
411 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
412 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
413 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
414 gtk_widget_grab_focus(GTK_WIDGET(c->view));
415 gtk_widget_hide_all(c->searchbar);
416 gtk_widget_hide_all(c->urlbar);
417 gtk_widget_show(c->vbox);
418 gtk_widget_show(c->scroll);
419 gtk_widget_show(GTK_WIDGET(c->view));
420 gtk_widget_show(c->win);
421 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
422 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
423 webkit_web_view_set_full_content_zoom(c->view, TRUE);
429 printf("%u\n", (unsigned int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
434 newwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
435 Client *n = newclient();
441 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
442 Arg arg = {.v = text };
444 loaduri((Client *) d, &arg);
448 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
449 Client *c = (Client *)d;
453 unsigned long ldummy;
454 unsigned char *buf = NULL;
457 if(((XEvent *)e)->type == PropertyNotify) {
458 ev = &((XEvent *)e)->xproperty;
459 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
463 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
464 &adummy, &idummy, &ldummy, &ldummy, &buf);
469 return GDK_FILTER_REMOVE;
472 return GDK_FILTER_CONTINUE;
476 progresschange(WebKitWebView* view, gint p, Client *c) {
478 updatetitle(c, NULL);
482 request(SoupSession *s, SoupMessage *m, Client *c) {
483 soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
484 G_CALLBACK(proccookies), c);
488 reload(Client *c, const Arg *arg) {
489 gboolean nocache = *(gboolean *)arg;
491 webkit_web_view_reload_bypass_cache(c->view);
493 webkit_web_view_reload(c->view);
497 setcookie(char *name, char *val, char *dom, char *path, long exp) {
498 printf("%s %s %s %s %li\n", name, val, dom, path, exp);
504 session = webkit_get_default_session();
505 urlprop = XInternAtom(dpy, "_SURF_URL", False);
509 showsearch(Client *c, const Arg *arg) {
511 gtk_widget_show(c->searchbar);
512 gtk_widget_grab_focus(c->searchbar);
516 searchtext(Client *c, const Arg *arg) {
517 gboolean forward = *(gboolean *)arg;
518 webkit_web_view_search_text(c->view,
519 gtk_entry_get_text(GTK_ENTRY(c->searchbar)),
526 showurl(Client *c, const Arg *arg) {
531 gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
532 gtk_widget_show(c->urlbar);
533 gtk_widget_grab_focus(c->urlbar);
537 stop(Client *c, const Arg *arg) {
539 webkit_download_cancel(c->download);
541 webkit_web_view_stop_loading(c->view);
546 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
552 fputs("surf - simple browser\n", stderr);
553 die("usage: surf [-e] [-x] [uri]\n");
557 updatetitle(Client *c, const char *title) {
563 c->title = g_strdup(title);
565 if(c->progress == 100)
566 t = g_strdup(c->title);
568 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
569 gtk_window_set_title(GTK_WINDOW(c->win), t);
575 zoompage(Client *c, const Arg *arg) {
576 if(*(float *)arg < 0) /* zoom out */
577 webkit_web_view_zoom_out(c->view);
578 else if(*(float *)arg == 0) /* zoom in */
579 webkit_web_view_zoom_in(c->view);
580 else /* absolute level */
581 webkit_web_view_set_zoom_level(c->view, *(float *)arg);
584 int main(int argc, char *argv[]) {
588 const gchar *home, *filename;
591 gtk_init(NULL, NULL);
592 if (!g_thread_supported())
595 while((o = getopt(argc, argv, "vhxeu:f:")) != -1)
605 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
610 if(optind + 1 == argc) {
612 arg.v = argv[optind];
613 if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0)
614 loadfile(c, argv[optind]);
619 else if(optind != argc)
625 home = g_get_home_dir();
626 filename = g_build_filename(home, ".surf", NULL);
627 g_mkdir_with_parents(filename, 0711);
628 filename = g_build_filename(home, ".surf", "dl", NULL);
629 g_mkdir_with_parents(filename, 0755);
631 /* cookie persistance */
632 s = webkit_get_default_session();
633 filename = g_build_filename(home, ".surf", "cookies", NULL);
634 cookiejar = soup_cookie_jar_text_new(filename, FALSE);
635 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));