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 void context(WebKitWebView *v, GtkMenu *m, Client *c);
86 static gchar *copystr(gchar **str, const gchar *src);
87 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
88 static void destroyclient(Client *c);
89 static void destroywin(GtkWidget* w, Client *c);
90 static void die(gchar *str);
91 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
92 static void drawindicator(Client *c);
93 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
94 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
95 static gchar *geturi(Client *c);
96 static void hidesearch(Client *c, const Arg *arg);
97 static void hideurl(Client *c, const Arg *arg);
98 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
99 static void linkhover(WebKitWebView *v, const gchar* t, const gchar* l, Client *c);
100 static void loadcommit(WebKitWebView *v, WebKitWebFrame *f, Client *c);
101 static void loadstart(WebKitWebView *v, WebKitWebFrame *f, Client *c);
102 static void loaduri(Client *c, const Arg *arg);
103 static void navigate(Client *c, const Arg *arg);
104 static Client *newclient(void);
105 static void newwindow(Client *c, const Arg *arg);
106 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
107 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
108 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
109 static void print(Client *c, const Arg *arg);
110 static void proccookies(SoupMessage *m, Client *c);
111 static void progresschange(WebKitWebView *v, gint p, Client *c);
112 static void request(SoupSession *s, SoupMessage *m, Client *c);
113 static void reload(Client *c, const Arg *arg);
114 static void rereadcookies(void);
115 static void sigchld(int unused);
116 static void setcookie(gchar *name, gchar *val, gchar *dom, gchar *path, long exp);
117 static void setup(void);
118 static void spawn(Client *c, const Arg *arg);
119 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const gchar* title, Client *c);
120 static void scroll(Client *c, const Arg *arg);
121 static void searchtext(Client *c, const Arg *arg);
122 static void source(Client *c, const Arg *arg);
123 static void showsearch(Client *c, const Arg *arg);
124 static void showurl(Client *c, const Arg *arg);
125 static void stop(Client *c, const Arg *arg);
126 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const gchar* title, Client *c);
127 static gboolean focusview(GtkWidget *w, GdkEventFocus *e, Client *c);
128 static void usage(void);
129 static void update(Client *c);
130 static void updatewinid(Client *c);
131 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
132 static void zoom(Client *c, const Arg *arg);
134 /* configuration, allows nested code to access above variables */
138 buildpath(const gchar *path) {
142 /* creating directory */
144 apath = g_strdup(path);
146 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
147 if((p = strrchr(apath, '/'))) {
149 g_mkdir_with_parents(apath, 0755);
152 /* creating file (gives error when apath ends with "/") */
153 if((f = g_fopen(apath, "a")))
161 destroyclient(clients);
169 clipboard(Client *c, const Arg *arg) {
170 gboolean paste = *(gboolean *)arg;
173 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
175 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
179 context(WebKitWebView *v, GtkMenu *m, Client *c) {
181 //gtk_menu_shell_append ((GtkMenuShell *)(menu),(child))
185 copystr(gchar **str, const gchar *src) {
197 destroyclient(Client *c) {
200 gtk_widget_destroy(GTK_WIDGET(c->view));
201 gtk_widget_destroy(c->scroll);
202 gtk_widget_destroy(c->urlbar);
203 gtk_widget_destroy(c->searchbar);
204 gtk_widget_destroy(c->vbox);
205 gtk_widget_destroy(c->win);
206 for(p = clients; p && p->next != c; p = p->next);
217 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
219 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
220 webkit_web_policy_decision_ignore(p);
221 arg.v = (void *)webkit_network_request_get_uri(r);
222 newwindow(NULL, &arg);
229 destroywin(GtkWidget* w, Client *c) {
240 drawindicator(Client *c) {
249 width = c->progress * w->allocation.width / 100;
250 gc = gdk_gc_new(w->window);
251 gdk_color_parse(strstr(uri, "https://") == uri ?
252 progress_trust : progress, &fg);
253 gdk_gc_set_rgb_fg_color(gc, &fg);
254 gdk_draw_rectangle(w->window,
255 w->style->bg_gc[GTK_WIDGET_STATE(w)],
256 TRUE, 0, 0, w->allocation.width, w->allocation.height);
257 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
258 w->allocation.height);
263 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
269 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
270 WebKitDownloadStatus status;
272 status = webkit_download_get_status(c->download);
273 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
274 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
280 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
281 const gchar *filename;
286 filename = webkit_download_get_suggested_filename(o);
287 uri = g_strconcat("file://", dldir, "/", filename, NULL);
288 webkit_download_set_destination_uri(c->download, uri);
291 html = g_strdup_printf("Download <b>%s</b>...", filename);
292 webkit_web_view_load_html_string(c->view, html,
293 webkit_download_get_uri(c->download));
294 g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
295 g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
296 webkit_download_start(c->download);
298 c->title = copystr(&c->title, filename);
308 if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
309 uri = copystr(NULL, "about:blank");
314 hidesearch(Client *c, const Arg *arg) {
315 gtk_widget_hide(c->searchbar);
316 gtk_widget_grab_focus(GTK_WIDGET(c->view));
320 hideurl(Client *c, const Arg *arg) {
321 gtk_widget_hide(c->urlbar);
322 gtk_widget_grab_focus(GTK_WIDGET(c->view));
326 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
328 gboolean processed = FALSE;
330 if(ev->type != GDK_KEY_PRESS)
332 if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
334 else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
339 for(i = 0; i < LENGTH(keys); i++) {
340 if(focus & keys[i].focus
341 && gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
342 && CLEANMASK(ev->state) == keys[i].mod
344 keys[i].func(c, &(keys[i].arg));
352 linkhover(WebKitWebView *v, const gchar* t, const gchar* l, Client *c) {
354 gtk_window_set_title(GTK_WINDOW(c->win), l);
360 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
365 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
366 XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
371 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
377 loaduri(Client *c, const Arg *arg) {
379 const gchar *uri = (gchar *)arg->v;
381 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
382 u = g_strrstr(uri, "://") ? g_strdup(uri)
383 : g_strdup_printf("http://%s", uri);
384 webkit_web_view_load_uri(c->view, u);
386 c->title = copystr(&c->title, u);
392 navigate(Client *c, const Arg *arg) {
393 gint steps = *(gint *)arg;
394 webkit_web_view_go_back_or_forward(c->view, steps);
400 WebKitWebSettings *settings;
403 if(!(c = calloc(1, sizeof(Client))))
404 die("Cannot malloc!\n");
407 c->win = gtk_plug_new(embed);
410 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
411 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
413 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
414 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
415 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
418 c->vbox = gtk_vbox_new(FALSE, 0);
420 /* scrolled window */
421 c->scroll = gtk_scrolled_window_new(NULL, NULL);
422 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
423 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
426 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
427 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
428 g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
429 g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
430 g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
431 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
432 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
433 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
434 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
435 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
436 g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
437 g_signal_connect(G_OBJECT(c->view), "focus-in-event", G_CALLBACK(focusview), c);
438 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(context), c);
441 c->urlbar = gtk_entry_new();
442 gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
445 c->searchbar = gtk_entry_new();
446 gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
449 c->indicator = gtk_drawing_area_new();
450 gtk_widget_set_size_request(c->indicator, 0, 2);
451 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
452 G_CALLBACK (exposeindicator), c);
455 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
456 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
457 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
458 gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
459 gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
460 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
463 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
464 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
465 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
466 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
467 gtk_widget_grab_focus(GTK_WIDGET(c->view));
468 gtk_widget_hide_all(c->searchbar);
469 gtk_widget_hide_all(c->urlbar);
470 gtk_widget_show(c->vbox);
471 gtk_widget_show(c->indicator);
472 gtk_widget_show(c->scroll);
473 gtk_widget_show(GTK_WIDGET(c->view));
474 gtk_widget_show(c->win);
475 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
476 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
477 webkit_web_view_set_full_content_zoom(c->view, TRUE);
478 settings = webkit_web_view_get_settings(c->view);
479 g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
480 uri = g_strconcat("file://", stylefile, NULL);
481 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
489 gdk_display_sync(gtk_widget_get_display(c->win));
490 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
497 newwindow(Client *c, const Arg *arg) {
498 guint i = 0, urlindex;
500 const Arg a = { .v = (void *)cmd };
506 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
515 cmd[i++] = (char *)arg->v;
521 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
522 Client *n = newclient();
527 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
528 Arg arg = {.v = text };
530 loaduri((Client *) d, &arg);
534 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
535 Client *c = (Client *)d;
539 unsigned long ldummy;
540 unsigned char *buf = NULL;
543 if(((XEvent *)e)->type == PropertyNotify) {
544 ev = &((XEvent *)e)->xproperty;
545 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
549 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
550 &adummy, &idummy, &ldummy, &ldummy, &buf);
555 return GDK_FILTER_REMOVE;
558 return GDK_FILTER_CONTINUE;
562 print(Client *c, const Arg *arg) {
563 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
567 proccookies(SoupMessage *m, Client *c) {
573 for (l = soup_cookies_from_response(m); l; l = l->next){
574 co = (SoupCookie *)l->data;
575 t = co->expires ? soup_date_to_time_t(co->expires) : 0;
576 setcookie(co->name, co->value, co->domain, co->value, t);
582 progresschange(WebKitWebView *v, gint p, Client *c) {
588 request(SoupSession *s, SoupMessage *m, Client *c) {
589 soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
590 G_CALLBACK(proccookies), c);
594 reload(Client *c, const Arg *arg) {
595 gboolean nocache = *(gboolean *)arg;
597 webkit_web_view_reload_bypass_cache(c->view);
599 webkit_web_view_reload(c->view);
603 rereadcookies(void) {
608 scroll(Client *c, const Arg *arg) {
612 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
613 v = gtk_adjustment_get_value(a);
614 v += gtk_adjustment_get_step_increment(a) * arg->i;
616 v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
617 gtk_adjustment_set_value(a, v);
621 sigchld(int unused) {
622 if(signal(SIGCHLD, sigchld) == SIG_ERR)
623 die("Can't install SIGCHLD handler");
624 while(0 < waitpid(-1, NULL, WNOHANG));
628 setcookie(gchar *name, gchar *val, gchar *dom, gchar *path, long exp) {
636 /* clean up any zombies immediately */
638 gtk_init(NULL, NULL);
639 if (!g_thread_supported())
643 session = webkit_get_default_session();
644 urlprop = XInternAtom(dpy, "_SURF_URL", False);
646 /* create dirs and files */
647 cookiefile = buildpath(cookiefile);
648 dldir = buildpath(dldir);
649 scriptfile = buildpath(scriptfile);
650 stylefile = buildpath(stylefile);
652 /* cookie persistance */
653 s = webkit_get_default_session();
654 cookiejar = soup_cookie_jar_text_new(cookiefile, FALSE);
655 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
659 showsearch(Client *c, const Arg *arg) {
661 gtk_widget_show(c->searchbar);
662 gtk_widget_grab_focus(c->searchbar);
666 source(Client *c, const Arg *arg) {
667 Arg a = { .b = FALSE };
670 s = webkit_web_view_get_view_source_mode(c->view);
671 webkit_web_view_set_view_source_mode(c->view, !s);
676 searchtext(Client *c, const Arg *arg) {
678 gboolean forward = *(gboolean *)arg;
679 text = gtk_entry_get_text(GTK_ENTRY(c->searchbar));
680 webkit_web_view_search_text(c->view, text, FALSE, forward, TRUE);
681 webkit_web_view_mark_text_matches(c->view, text, FALSE, 0);
685 showurl(Client *c, const Arg *arg) {
690 gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
691 gtk_widget_show(c->urlbar);
692 gtk_widget_grab_focus(c->urlbar);
696 stop(Client *c, const Arg *arg) {
698 webkit_download_cancel(c->download);
700 webkit_web_view_stop_loading(c->view);
705 spawn(Client *c, const Arg *arg) {
708 close(ConnectionNumber(dpy));
710 execvp(((gchar **)arg->v)[0], (gchar **)arg->v);
711 fprintf(stderr, "tabbed: execvp %s", ((gchar **)arg->v)[0]);
718 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
719 c->title = copystr(&c->title, t);
724 focusview(GtkWidget *w, GdkEventFocus *e, Client *c) {
732 fputs("surf - simple browser\n", stderr);
733 die("usage: surf [-e Window] [-x] [uri]\n");
740 if(c->progress == 100)
741 t = g_strdup(c->title);
743 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
745 gtk_window_set_title(GTK_WINDOW(c->win), t);
751 updatewinid(Client *c) {
752 snprintf(winid, LENGTH(winid), "%u",
753 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
757 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
758 JSStringRef jsscript;
760 JSValueRef exception = NULL;
763 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
764 jsscript = JSStringCreateWithUTF8CString(script);
765 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
770 zoom(Client *c, const Arg *arg) {
771 if(arg->i < 0) /* zoom out */
772 webkit_web_view_zoom_out(c->view);
773 else if(arg->i > 0) /* zoom in */
774 webkit_web_view_zoom_in(c->view);
776 webkit_web_view_set_zoom_level(c->view, 1.0);
779 int main(int argc, char *argv[]) {
784 /* command line args */
785 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-'; i++) {
786 if(!strcmp(argv[i], "-x"))
788 else if(!strcmp(argv[i], "-e")) {
790 embed = atoi(argv[i]);
794 else if(!strcmp(argv[i], "--")) {
798 else if(!strcmp(argv[i], "-v"))
799 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
808 loaduri(clients, &arg);