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])
20 #define CLEANMASK(mask) (mask & ~(GDK_MOD2_MASK))
24 typedef union Arg Arg;
31 typedef struct Client {
32 GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar, *indicator;
34 WebKitDownload *download;
40 typedef struct Cookie {
58 void (*func)(Client *c, const Arg *arg);
63 SoupCookieJar *cookiejar;
65 Client *clients = NULL;
66 Cookie *cookies = NULL;
67 gboolean embed = FALSE;
68 gboolean showxid = FALSE;
69 gboolean ignore_once = FALSE;
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);
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);
112 static void update(Client *c, const gchar *title);
113 static void zoom(Client *c, const Arg *arg);
120 destroyclient(clients);
124 proccookies(SoupMessage *m, Client *c) {
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);
139 clipboard(Client *c, const Arg *arg) {
140 gboolean paste = *(gboolean *)arg;
142 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
144 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
148 destroyclient(Client *c) {
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);
168 destroywin(GtkWidget* w, Client *c) {
179 drawindicator(Client *c) {
188 width = c->progress * w->allocation.width / 100;
189 gc = gdk_gc_new(w->window);
190 gdk_color_parse(strstr(uri, "https://") == uri ?
191 progress_trust : progress, &fg);
192 gdk_gc_set_rgb_fg_color(gc, &fg);
193 gdk_draw_rectangle(w->window,
194 w->style->bg_gc[GTK_WIDGET_STATE(w)],
195 TRUE, 0, 0, w->allocation.width, w->allocation.height);
196 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
197 w->allocation.height);
198 g_object_unref(gc);/*g_free(gc);*/
202 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
208 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
209 WebKitDownloadStatus status;
211 status = webkit_download_get_status(c->download);
212 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
213 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
219 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
220 const gchar *home, *filename;
221 gchar *uri, *path, *html;
225 home = g_get_home_dir();
226 filename = webkit_download_get_suggested_filename(o);
227 path = g_build_filename(home, ".surf", "dl",
229 uri = g_strconcat("file://", path, NULL);
230 webkit_download_set_destination_uri(c->download, uri);
233 html = g_strdup_printf("Download <b>%s</b>...", filename);
234 webkit_web_view_load_html_string(c->view, html,
235 webkit_download_get_uri(c->download));
236 g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
237 g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
238 webkit_download_start(c->download);
248 if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
249 uri = g_strdup("about:blank");
254 hidesearch(Client *c, const Arg *arg) {
255 gtk_widget_hide(c->searchbar);
256 gtk_widget_grab_focus(GTK_WIDGET(c->view));
260 hideurl(Client *c, const Arg *arg) {
261 gtk_widget_hide(c->urlbar);
262 gtk_widget_grab_focus(GTK_WIDGET(c->view));
266 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
268 gboolean processed = FALSE;
270 if(ev->type != GDK_KEY_PRESS)
272 if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
274 else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
278 for(i = 0; i < LENGTH(keys); i++) {
279 if(focus & keys[i].focus && ev->keyval == keys[i].keyval &&
280 (CLEANMASK(ev->state) == keys[i].mod ||
281 CLEANMASK(ev->state) & keys[i].mod)
283 keys[i].func(c, &(keys[i].arg));
291 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c) {
293 gtk_window_set_title(GTK_WINDOW(c->win), l);
299 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
304 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
305 XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
310 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
316 loadfile(Client *c, const gchar *f) {
317 GIOChannel *chan = NULL;
323 if(strcmp(f, "-") == 0) {
324 chan = g_io_channel_unix_new(STDIN_FILENO);
326 code = g_string_new("");
327 while(g_io_channel_read_line(chan, &line, NULL, NULL,
328 &e) == G_IO_STATUS_NORMAL) {
329 g_string_append(code, line);
332 webkit_web_view_load_html_string(c->view, code->str,
334 g_io_channel_shutdown(chan, FALSE, NULL);
335 g_string_free(code, TRUE);
337 arg.v = uri = g_strdup("stdin");
340 arg.v = uri = g_strdup_printf("file://%s", f);
348 loaduri(Client *c, const Arg *arg) {
350 const gchar *uri = (gchar *)arg->v;
352 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
353 u = g_strrstr(uri, "://") ? g_strdup(uri)
354 : g_strdup_printf("http://%s", uri);
355 webkit_web_view_load_uri(c->view, u);
362 navigate(Client *c, const Arg *arg) {
363 gint steps = *(gint *)arg;
364 webkit_web_view_go_back_or_forward(c->view, steps);
370 if(!(c = calloc(1, sizeof(Client))))
371 die("Cannot malloc!\n");
374 c->win = gtk_plug_new(0);
377 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
378 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
380 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
381 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
382 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
385 c->vbox = gtk_vbox_new(FALSE, 0);
387 /* scrolled window */
388 c->scroll = gtk_scrolled_window_new(NULL, NULL);
389 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
390 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
393 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
394 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
395 g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
396 g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
397 g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
398 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
399 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(newwindow), c);
400 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
401 g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
404 c->urlbar = gtk_entry_new();
405 gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
408 c->searchbar = gtk_entry_new();
409 gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
412 c->indicator = gtk_drawing_area_new();
413 gtk_widget_set_size_request(c->indicator, 0, 2);
414 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
415 G_CALLBACK (exposeindicator), c);
420 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
421 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
422 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
423 gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
424 gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
425 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
428 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
429 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
430 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
431 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
432 gtk_widget_grab_focus(GTK_WIDGET(c->view));
433 gtk_widget_hide_all(c->searchbar);
434 gtk_widget_hide_all(c->urlbar);
435 gtk_widget_show(c->vbox);
436 gtk_widget_show(c->indicator);
437 gtk_widget_show(c->scroll);
438 gtk_widget_show(GTK_WIDGET(c->view));
439 gtk_widget_show(c->win);
440 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
441 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
442 webkit_web_view_set_full_content_zoom(c->view, TRUE);
448 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
453 newwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
454 Client *n = newclient();
460 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
461 Arg arg = {.v = text };
463 loaduri((Client *) d, &arg);
467 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
468 Client *c = (Client *)d;
472 unsigned long ldummy;
473 unsigned char *buf = NULL;
476 if(((XEvent *)e)->type == PropertyNotify) {
477 ev = &((XEvent *)e)->xproperty;
478 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
482 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
483 &adummy, &idummy, &ldummy, &ldummy, &buf);
488 return GDK_FILTER_REMOVE;
491 return GDK_FILTER_CONTINUE;
495 print(Client *c, const Arg *arg) {
496 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
500 progresschange(WebKitWebView* view, gint p, Client *c) {
506 request(SoupSession *s, SoupMessage *m, Client *c) {
507 soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
508 G_CALLBACK(proccookies), c);
512 reload(Client *c, const Arg *arg) {
513 gboolean nocache = *(gboolean *)arg;
515 webkit_web_view_reload_bypass_cache(c->view);
517 webkit_web_view_reload(c->view);
522 const gchar *filename, *home;
524 home = g_get_home_dir();
525 filename = g_build_filename(home, ".surf", "cookies", NULL);
529 setcookie(char *name, char *val, char *dom, char *path, long exp) {
536 session = webkit_get_default_session();
537 urlprop = XInternAtom(dpy, "_SURF_URL", False);
541 showsearch(Client *c, const Arg *arg) {
543 gtk_widget_show(c->searchbar);
544 gtk_widget_grab_focus(c->searchbar);
548 searchtext(Client *c, const Arg *arg) {
549 gboolean forward = *(gboolean *)arg;
550 webkit_web_view_search_text(c->view,
551 gtk_entry_get_text(GTK_ENTRY(c->searchbar)),
558 showurl(Client *c, const Arg *arg) {
563 gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
564 gtk_widget_show(c->urlbar);
565 gtk_widget_grab_focus(c->urlbar);
569 stop(Client *c, const Arg *arg) {
571 webkit_download_cancel(c->download);
573 webkit_web_view_stop_loading(c->view);
578 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
584 fputs("surf - simple browser\n", stderr);
585 die("usage: surf [-e] [-x] [uri]\n");
589 update(Client *c, const char *title) {
595 c->title = g_strdup(title);
597 if(c->progress == 100)
598 t = g_strdup(c->title);
600 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
602 gtk_window_set_title(GTK_WINDOW(c->win), t);
608 zoom(Client *c, const Arg *arg) {
609 if(arg->i < 0) /* zoom out */
610 webkit_web_view_zoom_out(c->view);
611 else if(arg->i > 0) /* zoom in */
612 webkit_web_view_zoom_in(c->view);
614 webkit_web_view_set_zoom_level(c->view, 1.0);
617 int main(int argc, char *argv[]) {
621 const gchar *home, *filename;
624 gtk_init(NULL, NULL);
625 if (!g_thread_supported())
628 while((o = getopt(argc, argv, "vhxeu:f:")) != -1)
638 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
643 if(optind + 1 == argc) {
645 arg.v = argv[optind];
646 if(strchr("./", argv[optind][0]) || strcmp("-", argv[optind]) == 0)
647 loadfile(c, argv[optind]);
652 else if(optind != argc)
658 home = g_get_home_dir();
659 filename = g_build_filename(home, ".surf", NULL);
660 g_mkdir_with_parents(filename, 0711);
661 filename = g_build_filename(home, ".surf", "dl", NULL);
662 g_mkdir_with_parents(filename, 0755);
664 /* cookie persistance */
665 s = webkit_get_default_session();
666 filename = g_build_filename(home, ".surf", "cookies.jar", NULL);
667 cookiejar = soup_cookie_jar_text_new(filename, FALSE);
668 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));