1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
11 #include <gdk/gdkkeysyms.h>
13 #include <sys/types.h>
18 #include <webkit/webkit.h>
19 #include <glib/gstdio.h>
20 #include <JavaScriptCore/JavaScript.h>
22 #define LENGTH(x) (sizeof x / sizeof x[0])
23 #define CLEANMASK(mask) (mask & ~(GDK_MOD2_MASK))
25 typedef union Arg Arg;
32 typedef struct Client {
33 GtkWidget *win, *scroll, *vbox, *urlbar, *searchbar, *indicator;
35 WebKitDownload *download;
43 void (*func)(Client *c, const Arg *arg);
47 typedef struct Cookie {
65 void (*func)(Client *c, const Arg *arg);
72 static SoupCookieJar *cookiejar;
73 static SoupSession *session;
74 static Client *clients = NULL;
75 /*static Cookie *cookies = NULL;*/
76 static GdkNativeWindow embed = 0;
77 static gboolean showxid = FALSE;
78 static gboolean ignore_once = FALSE;
79 static gchar winid[64];
80 static gchar *progname;
82 static gchar *buildpath(const gchar *path);
83 static void cleanup(void);
84 static void clipboard(Client *c, const Arg *arg);
85 static gchar *copystr(gchar **str, const gchar *src);
86 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
87 static void destroyclient(Client *c);
88 static void destroywin(GtkWidget* w, Client *c);
89 static void die(gchar *str);
90 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
91 static void drawindicator(Client *c);
92 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
93 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
94 static gchar *geturi(Client *c);
95 static void hidesearch(Client *c, const Arg *arg);
96 static void hideurl(Client *c, const Arg *arg);
97 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
98 static void linkhover(WebKitWebView *v, const gchar* t, const gchar* l, Client *c);
99 static void loadcommit(WebKitWebView *v, WebKitWebFrame *f, Client *c);
100 static void loadstart(WebKitWebView *v, WebKitWebFrame *f, Client *c);
101 static void loaduri(Client *c, const Arg *arg);
102 static void navigate(Client *c, const Arg *arg);
103 static Client *newclient(void);
104 static void newproc(const gchar *url);
105 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
106 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
107 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
108 static void print(Client *c, const Arg *arg);
109 static void proccookies(SoupMessage *m, Client *c);
110 static void progresschange(WebKitWebView *v, gint p, Client *c);
111 static void request(SoupSession *s, SoupMessage *m, Client *c);
112 static void reload(Client *c, const Arg *arg);
113 static void rereadcookies(void);
114 static void sigchld(int unused);
115 static void setcookie(gchar *name, gchar *val, gchar *dom, gchar *path, long exp);
116 static void setup(void);
117 static void spawn(Client *c, const Arg *arg);
118 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const gchar* title, Client *c);
119 static void scroll(Client *c, const Arg *arg);
120 static void searchtext(Client *c, const Arg *arg);
121 static void source(Client *c, const Arg *arg);
122 static void showsearch(Client *c, const Arg *arg);
123 static void showurl(Client *c, const Arg *arg);
124 static void stop(Client *c, const Arg *arg);
125 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const gchar* title, Client *c);
126 static gboolean focusview(GtkWidget *w, GdkEventFocus *e, Client *c);
127 static void usage(void);
128 static void update(Client *c);
129 static void updatewinid(Client *c);
130 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
131 static void zoom(Client *c, const Arg *arg);
133 /* configuration, allows nested code to access above variables */
137 buildpath(const gchar *path) {
141 /* creating directory */
143 apath = g_strdup(path);
145 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
146 if((p = strrchr(apath, '/'))) {
148 g_mkdir_with_parents(apath, 0755);
151 /* creating file (gives error when apath ends with "/") */
152 if((f = g_fopen(apath, "a")))
160 destroyclient(clients);
168 clipboard(Client *c, const Arg *arg) {
169 gboolean paste = *(gboolean *)arg;
172 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
174 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
178 copystr(gchar **str, const gchar *src) {
190 destroyclient(Client *c) {
193 gtk_widget_destroy(GTK_WIDGET(c->view));
194 gtk_widget_destroy(c->scroll);
195 gtk_widget_destroy(c->urlbar);
196 gtk_widget_destroy(c->searchbar);
197 gtk_widget_destroy(c->vbox);
198 gtk_widget_destroy(c->win);
199 for(p = clients; p && p->next != c; p = p->next);
210 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
211 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
212 webkit_web_policy_decision_ignore(p);
213 newproc(webkit_network_request_get_uri(r));
220 destroywin(GtkWidget* w, Client *c) {
231 drawindicator(Client *c) {
240 width = c->progress * w->allocation.width / 100;
241 gc = gdk_gc_new(w->window);
242 gdk_color_parse(strstr(uri, "https://") == uri ?
243 progress_trust : progress, &fg);
244 gdk_gc_set_rgb_fg_color(gc, &fg);
245 gdk_draw_rectangle(w->window,
246 w->style->bg_gc[GTK_WIDGET_STATE(w)],
247 TRUE, 0, 0, w->allocation.width, w->allocation.height);
248 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
249 w->allocation.height);
254 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
260 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
261 WebKitDownloadStatus status;
263 status = webkit_download_get_status(c->download);
264 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
265 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
271 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
272 const gchar *filename;
277 filename = webkit_download_get_suggested_filename(o);
278 uri = g_strconcat("file://", dldir, "/", filename, NULL);
279 webkit_download_set_destination_uri(c->download, uri);
282 html = g_strdup_printf("Download <b>%s</b>...", filename);
283 webkit_web_view_load_html_string(c->view, html,
284 webkit_download_get_uri(c->download));
285 g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
286 g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
287 webkit_download_start(c->download);
289 c->title = copystr(&c->title, filename);
299 if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
300 uri = copystr(NULL, "about:blank");
305 hidesearch(Client *c, const Arg *arg) {
306 gtk_widget_hide(c->searchbar);
307 gtk_widget_grab_focus(GTK_WIDGET(c->view));
311 hideurl(Client *c, const Arg *arg) {
312 gtk_widget_hide(c->urlbar);
313 gtk_widget_grab_focus(GTK_WIDGET(c->view));
317 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
319 gboolean processed = FALSE;
321 if(ev->type != GDK_KEY_PRESS)
323 if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
325 else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
330 for(i = 0; i < LENGTH(keys); i++) {
331 if(focus & keys[i].focus
332 && gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
333 && CLEANMASK(ev->state) == keys[i].mod
335 keys[i].func(c, &(keys[i].arg));
343 linkhover(WebKitWebView *v, const gchar* t, const gchar* l, Client *c) {
345 gtk_window_set_title(GTK_WINDOW(c->win), l);
351 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
356 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
357 XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
362 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
368 loaduri(Client *c, const Arg *arg) {
370 const gchar *uri = (gchar *)arg->v;
372 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
373 u = g_strrstr(uri, "://") ? g_strdup(uri)
374 : g_strdup_printf("http://%s", uri);
375 webkit_web_view_load_uri(c->view, u);
377 c->title = copystr(&c->title, u);
383 navigate(Client *c, const Arg *arg) {
384 gint steps = *(gint *)arg;
385 webkit_web_view_go_back_or_forward(c->view, steps);
391 WebKitWebSettings *settings;
394 if(!(c = calloc(1, sizeof(Client))))
395 die("Cannot malloc!\n");
398 c->win = gtk_plug_new(embed);
401 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
402 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
404 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
405 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
406 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
409 c->vbox = gtk_vbox_new(FALSE, 0);
411 /* scrolled window */
412 c->scroll = gtk_scrolled_window_new(NULL, NULL);
413 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
414 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
417 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
418 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
419 g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
420 g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
421 g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
422 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
423 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
424 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
425 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
426 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
427 g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
428 g_signal_connect(G_OBJECT(c->view), "focus-in-event", G_CALLBACK(focusview), c);
431 c->urlbar = gtk_entry_new();
432 gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
435 c->searchbar = gtk_entry_new();
436 gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
439 c->indicator = gtk_drawing_area_new();
440 gtk_widget_set_size_request(c->indicator, 0, 2);
441 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
442 G_CALLBACK (exposeindicator), c);
445 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
446 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
447 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
448 gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
449 gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
450 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
453 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
454 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
455 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
456 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
457 gtk_widget_grab_focus(GTK_WIDGET(c->view));
458 gtk_widget_hide_all(c->searchbar);
459 gtk_widget_hide_all(c->urlbar);
460 gtk_widget_show(c->vbox);
461 gtk_widget_show(c->indicator);
462 gtk_widget_show(c->scroll);
463 gtk_widget_show(GTK_WIDGET(c->view));
464 gtk_widget_show(c->win);
465 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
466 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
467 webkit_web_view_set_full_content_zoom(c->view, TRUE);
468 settings = webkit_web_view_get_settings(c->view);
469 g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
470 uri = g_strconcat("file://", stylefile, NULL);
471 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
479 gdk_display_sync(gtk_widget_get_display(c->win));
480 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
487 newproc(const gchar *url) {
488 guint i = 0, urlindex;
490 const Arg arg = { .v = (void *)cmd };
496 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
511 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
512 Client *n = newclient();
517 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
518 Arg arg = {.v = text };
520 loaduri((Client *) d, &arg);
524 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
525 Client *c = (Client *)d;
529 unsigned long ldummy;
530 unsigned char *buf = NULL;
533 if(((XEvent *)e)->type == PropertyNotify) {
534 ev = &((XEvent *)e)->xproperty;
535 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
539 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
540 &adummy, &idummy, &ldummy, &ldummy, &buf);
545 return GDK_FILTER_REMOVE;
548 return GDK_FILTER_CONTINUE;
552 print(Client *c, const Arg *arg) {
553 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
557 proccookies(SoupMessage *m, Client *c) {
563 for (l = soup_cookies_from_response(m); l; l = l->next){
564 co = (SoupCookie *)l->data;
565 t = co->expires ? soup_date_to_time_t(co->expires) : 0;
566 setcookie(co->name, co->value, co->domain, co->value, t);
572 progresschange(WebKitWebView *v, gint p, Client *c) {
578 request(SoupSession *s, SoupMessage *m, Client *c) {
579 soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
580 G_CALLBACK(proccookies), c);
584 reload(Client *c, const Arg *arg) {
585 gboolean nocache = *(gboolean *)arg;
587 webkit_web_view_reload_bypass_cache(c->view);
589 webkit_web_view_reload(c->view);
593 rereadcookies(void) {
598 scroll(Client *c, const Arg *arg) {
602 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
603 v = gtk_adjustment_get_value(a);
604 v += gtk_adjustment_get_step_increment(a) * arg->i;
606 v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
607 gtk_adjustment_set_value(a, v);
611 sigchld(int unused) {
612 if(signal(SIGCHLD, sigchld) == SIG_ERR)
613 die("Can't install SIGCHLD handler");
614 while(0 < waitpid(-1, NULL, WNOHANG));
618 setcookie(gchar *name, gchar *val, gchar *dom, gchar *path, long exp) {
626 /* clean up any zombies immediately */
628 gtk_init(NULL, NULL);
629 if (!g_thread_supported())
633 session = webkit_get_default_session();
634 urlprop = XInternAtom(dpy, "_SURF_URL", False);
636 /* create dirs and files */
637 cookiefile = buildpath(cookiefile);
638 dldir = buildpath(dldir);
639 scriptfile = buildpath(scriptfile);
640 stylefile = buildpath(stylefile);
642 /* cookie persistance */
643 s = webkit_get_default_session();
644 cookiejar = soup_cookie_jar_text_new(cookiefile, FALSE);
645 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
649 showsearch(Client *c, const Arg *arg) {
651 gtk_widget_show(c->searchbar);
652 gtk_widget_grab_focus(c->searchbar);
656 source(Client *c, const Arg *arg) {
657 Arg a = { .b = FALSE };
660 s = webkit_web_view_get_view_source_mode(c->view);
661 webkit_web_view_set_view_source_mode(c->view, !s);
666 searchtext(Client *c, const Arg *arg) {
668 gboolean forward = *(gboolean *)arg;
669 text = gtk_entry_get_text(GTK_ENTRY(c->searchbar));
670 webkit_web_view_search_text(c->view, text, FALSE, forward, TRUE);
671 webkit_web_view_mark_text_matches(c->view, text, FALSE, 0);
675 showurl(Client *c, const Arg *arg) {
680 gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
681 gtk_widget_show(c->urlbar);
682 gtk_widget_grab_focus(c->urlbar);
686 stop(Client *c, const Arg *arg) {
688 webkit_download_cancel(c->download);
690 webkit_web_view_stop_loading(c->view);
695 spawn(Client *c, const Arg *arg) {
698 close(ConnectionNumber(dpy));
700 execvp(((gchar **)arg->v)[0], (gchar **)arg->v);
701 fprintf(stderr, "tabbed: execvp %s", ((gchar **)arg->v)[0]);
708 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
709 c->title = copystr(&c->title, t);
714 focusview(GtkWidget *w, GdkEventFocus *e, Client *c) {
722 fputs("surf - simple browser\n", stderr);
723 die("usage: surf [-e Window] [-x] [uri]\n");
730 if(c->progress == 100)
731 t = g_strdup(c->title);
733 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
735 gtk_window_set_title(GTK_WINDOW(c->win), t);
741 updatewinid(Client *c) {
742 snprintf(winid, LENGTH(winid), "%u",
743 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
747 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
748 JSStringRef jsscript;
750 JSValueRef exception = NULL;
753 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
754 jsscript = JSStringCreateWithUTF8CString(script);
755 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
760 zoom(Client *c, const Arg *arg) {
761 if(arg->i < 0) /* zoom out */
762 webkit_web_view_zoom_out(c->view);
763 else if(arg->i > 0) /* zoom in */
764 webkit_web_view_zoom_in(c->view);
766 webkit_web_view_set_zoom_level(c->view, 1.0);
769 int main(int argc, char *argv[]) {
774 /* command line args */
775 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-'; i++) {
776 if(!strcmp(argv[i], "-x"))
778 else if(!strcmp(argv[i], "-e")) {
780 embed = atoi(argv[i]);
784 else if(!strcmp(argv[i], "--")) {
788 else if(!strcmp(argv[i], "-v"))
789 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
798 loaduri(clients, &arg);