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;
41 typedef struct Cookie {
59 void (*func)(Client *c, const Arg *arg);
66 static SoupCookieJar *cookiejar;
67 static SoupSession *session;
68 static Client *clients = NULL;
69 /*static Cookie *cookies = NULL;*/
70 static GdkNativeWindow embed = 0;
71 static gboolean showxid = FALSE;
72 static gboolean ignore_once = FALSE;
73 static gchar winid[64];
74 static gchar *progname;
76 static gchar *buildpath(const gchar *path);
77 static void cleanup(void);
78 static void clipboard(Client *c, const Arg *arg);
79 static gchar *copystr(gchar **str, const gchar *src);
80 static gboolean decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
81 static void destroyclient(Client *c);
82 static void destroywin(GtkWidget* w, Client *c);
83 static void die(char *str);
84 static void download(WebKitDownload *o, GParamSpec *pspec, Client *c);
85 static void drawindicator(Client *c);
86 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
87 static gboolean initdownload(WebKitWebView *view, WebKitDownload *o, Client *c);
88 static gchar *geturi(Client *c);
89 static void hidesearch(Client *c, const Arg *arg);
90 static void hideurl(Client *c, const Arg *arg);
91 static gboolean keypress(GtkWidget* w, GdkEventKey *ev, Client *c);
92 static void linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c);
93 static void loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c);
94 static void loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c);
95 static void loaduri(Client *c, const Arg *arg);
96 static void navigate(Client *c, const Arg *arg);
97 static Client *newclient(void);
98 static void newproc(const gchar *url);
99 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
100 static void pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d);
101 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
102 static void print(Client *c, const Arg *arg);
103 static void proccookies(SoupMessage *m, Client *c);
104 static void progresschange(WebKitWebView *view, gint p, Client *c);
105 static void request(SoupSession *s, SoupMessage *m, Client *c);
106 static void reload(Client *c, const Arg *arg);
107 static void rereadcookies(void);
108 static void sigchld(int unused);
109 static void setcookie(char *name, char *val, char *dom, char *path, long exp);
110 static void setup(void);
111 static void spawn(Client *c, const Arg *arg);
112 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
113 static void scroll(Client *c, const Arg *arg);
114 static void searchtext(Client *c, const Arg *arg);
115 static void source(Client *c, const Arg *arg);
116 static void showsearch(Client *c, const Arg *arg);
117 static void showurl(Client *c, const Arg *arg);
118 static void stop(Client *c, const Arg *arg);
119 static void titlechange(WebKitWebView* view, WebKitWebFrame* frame, const gchar* title, Client *c);
120 static gboolean focusview(GtkWidget *w, GdkEventFocus *e, Client *c);
121 static void usage(void);
122 static void update(Client *c);
123 static void updatewinid(Client *c);
124 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
125 static void zoom(Client *c, const Arg *arg);
127 /* configuration, allows nested code to access above variables */
131 buildpath(const gchar *path) {
135 /* creating directory */
137 apath = g_strdup(path);
139 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
140 if((p = strrchr(apath, '/'))) {
142 g_mkdir_with_parents(apath, 0755);
145 /* creating file (gives error when apath ends with "/") */
146 if((f = g_fopen(apath, "a")))
154 destroyclient(clients);
162 clipboard(Client *c, const Arg *arg) {
163 gboolean paste = *(gboolean *)arg;
166 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteurl, c);
168 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), webkit_web_view_get_uri(c->view), -1);
172 copystr(gchar **str, const gchar *src) {
184 destroyclient(Client *c) {
187 gtk_widget_destroy(GTK_WIDGET(c->view));
188 gtk_widget_destroy(c->scroll);
189 gtk_widget_destroy(c->urlbar);
190 gtk_widget_destroy(c->searchbar);
191 gtk_widget_destroy(c->vbox);
192 gtk_widget_destroy(c->win);
193 for(p = clients; p && p->next != c; p = p->next);
204 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
205 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
206 webkit_web_policy_decision_ignore(p);
207 newproc(webkit_network_request_get_uri(r));
214 destroywin(GtkWidget* w, Client *c) {
225 drawindicator(Client *c) {
234 width = c->progress * w->allocation.width / 100;
235 gc = gdk_gc_new(w->window);
236 gdk_color_parse(strstr(uri, "https://") == uri ?
237 progress_trust : progress, &fg);
238 gdk_gc_set_rgb_fg_color(gc, &fg);
239 gdk_draw_rectangle(w->window,
240 w->style->bg_gc[GTK_WIDGET_STATE(w)],
241 TRUE, 0, 0, w->allocation.width, w->allocation.height);
242 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
243 w->allocation.height);
248 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
254 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
255 WebKitDownloadStatus status;
257 status = webkit_download_get_status(c->download);
258 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
259 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
265 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
266 const gchar *filename;
271 filename = webkit_download_get_suggested_filename(o);
272 uri = g_strconcat("file://", dldir, "/", filename, NULL);
273 webkit_download_set_destination_uri(c->download, uri);
276 html = g_strdup_printf("Download <b>%s</b>...", filename);
277 webkit_web_view_load_html_string(c->view, html,
278 webkit_download_get_uri(c->download));
279 g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
280 g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
281 webkit_download_start(c->download);
283 c->title = copystr(&c->title, filename);
293 if(!(uri = (gchar *)webkit_web_view_get_uri(c->view)))
294 uri = copystr(NULL, "about:blank");
299 hidesearch(Client *c, const Arg *arg) {
300 gtk_widget_hide(c->searchbar);
301 gtk_widget_grab_focus(GTK_WIDGET(c->view));
305 hideurl(Client *c, const Arg *arg) {
306 gtk_widget_hide(c->urlbar);
307 gtk_widget_grab_focus(GTK_WIDGET(c->view));
311 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
313 gboolean processed = FALSE;
315 if(ev->type != GDK_KEY_PRESS)
317 if(GTK_WIDGET_HAS_FOCUS(c->searchbar))
319 else if(GTK_WIDGET_HAS_FOCUS(c->urlbar))
324 for(i = 0; i < LENGTH(keys); i++) {
325 if(focus & keys[i].focus
326 && gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
327 && CLEANMASK(ev->state) == keys[i].mod
329 keys[i].func(c, &(keys[i].arg));
337 linkhover(WebKitWebView* page, const gchar* t, const gchar* l, Client *c) {
339 gtk_window_set_title(GTK_WINDOW(c->win), l);
345 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
350 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), urlprop,
351 XA_STRING, 8, PropModeReplace, (unsigned char *)uri,
356 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
362 loaduri(Client *c, const Arg *arg) {
364 const gchar *uri = (gchar *)arg->v;
366 uri = gtk_entry_get_text(GTK_ENTRY(c->urlbar));
367 u = g_strrstr(uri, "://") ? g_strdup(uri)
368 : g_strdup_printf("http://%s", uri);
369 webkit_web_view_load_uri(c->view, u);
371 c->title = copystr(&c->title, u);
377 navigate(Client *c, const Arg *arg) {
378 gint steps = *(gint *)arg;
379 webkit_web_view_go_back_or_forward(c->view, steps);
385 WebKitWebSettings *settings;
388 if(!(c = calloc(1, sizeof(Client))))
389 die("Cannot malloc!\n");
392 c->win = gtk_plug_new(embed);
395 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
396 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
398 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
399 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
400 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
403 c->vbox = gtk_vbox_new(FALSE, 0);
405 /* scrolled window */
406 c->scroll = gtk_scrolled_window_new(NULL, NULL);
407 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
408 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
411 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
412 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
413 g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
414 g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
415 g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
416 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
417 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
418 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
419 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
420 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
421 g_signal_connect_after(session, "request-started", G_CALLBACK(request), c);
422 g_signal_connect(G_OBJECT(c->view), "focus-in-event", G_CALLBACK(focusview), c);
425 c->urlbar = gtk_entry_new();
426 gtk_entry_set_has_frame(GTK_ENTRY(c->urlbar), FALSE);
429 c->searchbar = gtk_entry_new();
430 gtk_entry_set_has_frame(GTK_ENTRY(c->searchbar), FALSE);
433 c->indicator = gtk_drawing_area_new();
434 gtk_widget_set_size_request(c->indicator, 0, 2);
435 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
436 G_CALLBACK (exposeindicator), c);
439 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
440 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
441 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
442 gtk_container_add(GTK_CONTAINER(c->vbox), c->searchbar);
443 gtk_container_add(GTK_CONTAINER(c->vbox), c->urlbar);
444 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
447 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->urlbar, FALSE, FALSE, 0, GTK_PACK_START);
448 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->searchbar, FALSE, FALSE, 0, GTK_PACK_START);
449 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
450 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
451 gtk_widget_grab_focus(GTK_WIDGET(c->view));
452 gtk_widget_hide_all(c->searchbar);
453 gtk_widget_hide_all(c->urlbar);
454 gtk_widget_show(c->vbox);
455 gtk_widget_show(c->indicator);
456 gtk_widget_show(c->scroll);
457 gtk_widget_show(GTK_WIDGET(c->view));
458 gtk_widget_show(c->win);
459 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
460 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
461 webkit_web_view_set_full_content_zoom(c->view, TRUE);
462 settings = webkit_web_view_get_settings(c->view);
463 g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
464 uri = g_strconcat("file://", stylefile, NULL);
465 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
473 gdk_display_sync(gtk_widget_get_display(c->win));
474 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
481 newproc(const gchar *url) {
482 guint i = 0, urlindex;
484 const Arg arg = { .v = (void *)cmd };
490 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
505 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
506 Client *n = newclient();
511 pasteurl(GtkClipboard *clipboard, const gchar *text, gpointer d) {
512 Arg arg = {.v = text };
514 loaduri((Client *) d, &arg);
518 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
519 Client *c = (Client *)d;
523 unsigned long ldummy;
524 unsigned char *buf = NULL;
527 if(((XEvent *)e)->type == PropertyNotify) {
528 ev = &((XEvent *)e)->xproperty;
529 if(ev->atom == urlprop && ev->state == PropertyNewValue) {
533 XGetWindowProperty(dpy, ev->window, urlprop, 0L, BUFSIZ, False, XA_STRING,
534 &adummy, &idummy, &ldummy, &ldummy, &buf);
539 return GDK_FILTER_REMOVE;
542 return GDK_FILTER_CONTINUE;
546 print(Client *c, const Arg *arg) {
547 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
551 proccookies(SoupMessage *m, Client *c) {
557 for (l = soup_cookies_from_response(m); l; l = l->next){
558 co = (SoupCookie *)l->data;
559 t = co->expires ? soup_date_to_time_t(co->expires) : 0;
560 setcookie(co->name, co->value, co->domain, co->value, t);
566 progresschange(WebKitWebView* view, gint p, Client *c) {
572 request(SoupSession *s, SoupMessage *m, Client *c) {
573 soup_message_add_header_handler(m, "got-headers", "Set-Cookie",
574 G_CALLBACK(proccookies), c);
578 reload(Client *c, const Arg *arg) {
579 gboolean nocache = *(gboolean *)arg;
581 webkit_web_view_reload_bypass_cache(c->view);
583 webkit_web_view_reload(c->view);
587 rereadcookies(void) {
592 scroll(Client *c, const Arg *arg) {
596 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
597 v = gtk_adjustment_get_value(a);
598 v += gtk_adjustment_get_step_increment(a) * arg->i;
600 v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
601 gtk_adjustment_set_value(a, v);
605 sigchld(int unused) {
606 if(signal(SIGCHLD, sigchld) == SIG_ERR)
607 die("Can't install SIGCHLD handler");
608 while(0 < waitpid(-1, NULL, WNOHANG));
612 setcookie(char *name, char *val, char *dom, char *path, long exp) {
620 /* clean up any zombies immediately */
622 gtk_init(NULL, NULL);
623 if (!g_thread_supported())
627 session = webkit_get_default_session();
628 urlprop = XInternAtom(dpy, "_SURF_URL", False);
630 /* create dirs and files */
631 cookiefile = buildpath(cookiefile);
632 dldir = buildpath(dldir);
633 scriptfile = buildpath(scriptfile);
634 stylefile = buildpath(stylefile);
636 /* cookie persistance */
637 s = webkit_get_default_session();
638 cookiejar = soup_cookie_jar_text_new(cookiefile, FALSE);
639 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar));
643 showsearch(Client *c, const Arg *arg) {
645 gtk_widget_show(c->searchbar);
646 gtk_widget_grab_focus(c->searchbar);
650 source(Client *c, const Arg *arg) {
651 Arg a = { .b = FALSE };
654 s = webkit_web_view_get_view_source_mode(c->view);
655 webkit_web_view_set_view_source_mode(c->view, !s);
660 searchtext(Client *c, const Arg *arg) {
662 gboolean forward = *(gboolean *)arg;
663 text = gtk_entry_get_text(GTK_ENTRY(c->searchbar));
664 webkit_web_view_search_text(c->view, text, FALSE, forward, TRUE);
665 webkit_web_view_mark_text_matches(c->view, text, FALSE, 0);
669 showurl(Client *c, const Arg *arg) {
674 gtk_entry_set_text(GTK_ENTRY(c->urlbar), uri);
675 gtk_widget_show(c->urlbar);
676 gtk_widget_grab_focus(c->urlbar);
680 stop(Client *c, const Arg *arg) {
682 webkit_download_cancel(c->download);
684 webkit_web_view_stop_loading(c->view);
689 spawn(Client *c, const Arg *arg) {
692 close(ConnectionNumber(dpy));
694 execvp(((char **)arg->v)[0], (char **)arg->v);
695 fprintf(stderr, "tabbed: execvp %s", ((char **)arg->v)[0]);
702 titlechange(WebKitWebView *v, WebKitWebFrame *f, const gchar *t, Client *c) {
703 c->title = copystr(&c->title, t);
708 focusview(GtkWidget *w, GdkEventFocus *e, Client *c) {
716 fputs("surf - simple browser\n", stderr);
717 die("usage: surf [-e Window] [-x] [uri]\n");
724 if(c->progress == 100)
725 t = g_strdup(c->title);
727 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
729 gtk_window_set_title(GTK_WINDOW(c->win), t);
735 updatewinid(Client *c) {
736 snprintf(winid, LENGTH(winid), "%u",
737 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
741 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
742 JSStringRef jsscript;
744 JSValueRef exception = NULL;
747 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
748 jsscript = JSStringCreateWithUTF8CString(script);
749 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
754 zoom(Client *c, const Arg *arg) {
755 if(arg->i < 0) /* zoom out */
756 webkit_web_view_zoom_out(c->view);
757 else if(arg->i > 0) /* zoom in */
758 webkit_web_view_zoom_in(c->view);
760 webkit_web_view_set_zoom_level(c->view, 1.0);
763 int main(int argc, char *argv[]) {
768 /* command line args */
769 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-'; i++) {
770 if(!strcmp(argv[i], "-x"))
772 else if(!strcmp(argv[i], "-e")) {
774 embed = atoi(argv[i]);
778 else if(!strcmp(argv[i], "--")) {
782 else if(!strcmp(argv[i], "-v"))
783 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
792 loaduri(clients, &arg);