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, *indicator;
36 WebKitDownload *download;
37 char *title, *linkhover;
38 const char *uri, *needle;
46 void (*func)(Client *c, const Arg *arg);
53 void (*func)(Client *c, const Arg *arg);
58 static Atom uriprop, findprop;
59 static SoupCookieJar *cookies;
60 static SoupSession *session;
61 static Client *clients = NULL;
62 static GdkNativeWindow embed = 0;
63 static gboolean showxid = FALSE;
64 static int ignorexprop = 0;
65 static char winid[64];
66 static char *progname;
67 static gboolean lockcookie = FALSE;
69 static char *buildpath(const char *path);
70 static void changecookie(SoupCookieJar *jar, SoupCookie *o, SoupCookie *n, gpointer p);
71 static void cleanup(void);
72 static void clipboard(Client *c, const Arg *arg);
73 static void context(WebKitWebView *v, GtkMenu *m, Client *c);
74 static char *copystr(char **str, const char *src);
75 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
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 char *geturi(Client *c);
83 static const char *getatom(Client *c, Atom a);
84 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
85 static void itemclick(GtkMenuItem *mi, Client *c);
86 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
87 static void linkhover(WebKitWebView *v, const char* t, const char* l, Client *c);
88 static void loadcommit(WebKitWebView *v, WebKitWebFrame *f, Client *c);
89 static void loadstart(WebKitWebView *v, WebKitWebFrame *f, Client *c);
90 static void loaduri(Client *c, const Arg *arg);
91 static void navigate(Client *c, const Arg *arg);
92 static Client *newclient(void);
93 static void newwindow(Client *c, const Arg *arg);
94 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
95 static void pasteuri(GtkClipboard *clipboard, const char *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 *v, gint p, Client *c);
99 static void reloadcookies();
100 static void reload(Client *c, const Arg *arg);
101 static void resize(GtkWidget *w, GtkAllocation *a, Client *c);
102 static void sigchld(int unused);
103 static void setatom(Client *c, Atom a, const char *v);
104 static void setup(void);
105 static void spawn(Client *c, const Arg *arg);
106 static void scroll(Client *c, const Arg *arg);
107 static void find(Client *c, const Arg *arg);
108 static void source(Client *c, const Arg *arg);
109 static void stop(Client *c, const Arg *arg);
110 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
111 static void usage(void);
112 static void update(Client *c);
113 static void updatewinid(Client *c);
114 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
115 static void zoom(Client *c, const Arg *arg);
117 /* configuration, allows nested code to access above variables */
121 buildpath(const char *path) {
125 /* creating directory */
127 apath = g_strdup(path);
129 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
130 if((p = strrchr(apath, '/'))) {
132 g_mkdir_with_parents(apath, 0755);
135 /* creating file (gives error when apath ends with "/") */
136 if((f = g_fopen(apath, "a")))
142 changecookie(SoupCookieJar *j, SoupCookie *oc, SoupCookie *c, gpointer p) {
148 if(c && c->expires == NULL) {
149 e = soup_date_new_from_time_t(time(NULL) + sessiontime);
150 c = soup_cookie_copy(c);
151 soup_cookie_set_expires(c, e);
154 jar = soup_cookie_jar_text_new(cookiefile, FALSE);
156 soup_cookie_jar_add_cookie(jar, soup_cookie_copy(c));
158 soup_cookie_jar_delete_cookie(jar, oc);
165 destroyclient(clients);
173 clipboard(Client *c, const Arg *arg) {
174 gboolean paste = *(gboolean *)arg;
177 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
179 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), geturi(c), -1);
183 context(WebKitWebView *v, GtkMenu *m, Client *c) {
185 GtkContainer *parent;
187 gtk_widget_hide_all(GTK_WIDGET(m));
188 gtk_widget_show(GTK_WIDGET(m));
189 for(i = 0; i < LENGTH(items); i++) {
190 parent = GTK_CONTAINER(gtk_widget_get_parent(c->items[i]));
192 gtk_container_remove(parent, c->items[i]);
193 gtk_menu_shell_append(GTK_MENU_SHELL(m), c->items[i]);
194 gtk_widget_show(c->items[i]);
199 copystr(char **str, const char *src) {
211 destroyclient(Client *c) {
215 gtk_widget_destroy(GTK_WIDGET(c->view));
216 gtk_widget_destroy(c->scroll);
217 gtk_widget_destroy(c->vbox);
218 gtk_widget_destroy(c->indicator);
219 gtk_widget_destroy(c->win);
220 for(i = 0; i < LENGTH(items); i++)
221 gtk_widget_destroy(c->items[i]);
224 for(p = clients; p && p->next != c; p = p->next);
235 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
238 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
239 webkit_web_policy_decision_ignore(p);
240 arg.v = (void *)webkit_network_request_get_uri(r);
241 newwindow(NULL, &arg);
248 destroywin(GtkWidget* w, Client *c) {
259 drawindicator(Client *c) {
266 uri = getatom(c, uriprop);
268 width = c->progress * w->allocation.width / 100;
269 gc = gdk_gc_new(w->window);
270 gdk_color_parse(strstr(uri, "https://") == uri ?
271 progress_trust : progress, &fg);
272 gdk_gc_set_rgb_fg_color(gc, &fg);
273 gdk_draw_rectangle(w->window,
274 w->style->bg_gc[GTK_WIDGET_STATE(w)],
275 TRUE, 0, 0, w->allocation.width, w->allocation.height);
276 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
277 w->allocation.height);
282 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
288 download(WebKitDownload *o, GParamSpec *pspec, Client *c) {
289 WebKitDownloadStatus status;
291 status = webkit_download_get_status(c->download);
292 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
293 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
299 getatom(Client *c, Atom a) {
300 static char buf[BUFSIZ];
303 unsigned long ldummy;
304 unsigned char *p = NULL;
306 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
307 a, 0L, BUFSIZ, False, XA_STRING,
308 &adummy, &idummy, &ldummy, &ldummy, &p);
310 strncpy(buf, (char *)p, LENGTH(buf)-1);
318 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
319 const char *filename;
324 filename = webkit_download_get_suggested_filename(o);
325 uri = g_strconcat("file://", dldir, "/", filename, NULL);
326 webkit_download_set_destination_uri(c->download, uri);
329 html = g_strdup_printf("Download <b>%s</b>...", filename);
330 webkit_web_view_load_html_string(c->view, html,
331 webkit_download_get_uri(c->download));
332 g_signal_connect(c->download, "notify::progress", G_CALLBACK(download), c);
333 g_signal_connect(c->download, "notify::status", G_CALLBACK(download), c);
334 webkit_download_start(c->download);
336 c->title = copystr(&c->title, filename);
346 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
352 itemclick(GtkMenuItem *mi, Client *c) {
356 label = gtk_menu_item_get_label(mi);
357 for(i = 0; i < LENGTH(items); i++)
358 if(!strcmp(items[i].label, label))
359 items[i].func(c, &(items[i].arg));
363 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
365 gboolean processed = FALSE;
368 for(i = 0; i < LENGTH(keys); i++) {
369 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
370 && CLEANMASK(ev->state) == keys[i].mod
372 keys[i].func(c, &(keys[i].arg));
380 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
382 c->linkhover = copystr(&c->linkhover, l);
383 else if(c->linkhover) {
391 loadcommit(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
392 setatom(c, uriprop, geturi(c));
397 loadstart(WebKitWebView *view, WebKitWebFrame *f, Client *c) {
403 loaduri(Client *c, const Arg *arg) {
405 const char *uri = (char *)arg->v;
407 u = g_strrstr(uri, "://") ? g_strdup(uri)
408 : g_strdup_printf("http://%s", uri);
409 webkit_web_view_load_uri(c->view, u);
411 c->title = copystr(&c->title, u);
417 navigate(Client *c, const Arg *arg) {
418 gint steps = *(gint *)arg;
419 webkit_web_view_go_back_or_forward(c->view, steps);
426 WebKitWebSettings *settings;
427 GdkGeometry hints = { 1, 1 };
430 if(!(c = calloc(1, sizeof(Client))))
431 die("Cannot malloc!\n");
434 c->win = gtk_plug_new(embed);
437 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
438 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
440 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
441 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
442 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
443 g_signal_connect(G_OBJECT(c->win), "size-allocate", G_CALLBACK(resize), c);
445 if(!(c->items = calloc(1, sizeof(GtkWidget *) * LENGTH(items))))
446 die("Cannot malloc!\n");
449 for(i = 0; i < LENGTH(items); i++) {
450 c->items[i] = gtk_menu_item_new_with_label(items[i].label);
451 g_signal_connect(G_OBJECT(c->items[i]), "activate",
452 G_CALLBACK(itemclick), c);
456 c->vbox = gtk_vbox_new(FALSE, 0);
458 /* scrolled window */
459 c->scroll = gtk_scrolled_window_new(NULL, NULL);
460 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
461 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
464 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
465 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
466 g_signal_connect(G_OBJECT(c->view), "load-progress-changed", G_CALLBACK(progresschange), c);
467 g_signal_connect(G_OBJECT(c->view), "load-committed", G_CALLBACK(loadcommit), c);
468 g_signal_connect(G_OBJECT(c->view), "load-started", G_CALLBACK(loadstart), c);
469 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
470 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
471 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
472 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
473 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
474 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(context), c);
477 c->indicator = gtk_drawing_area_new();
478 gtk_widget_set_size_request(c->indicator, 0, 2);
479 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
480 G_CALLBACK (exposeindicator), c);
483 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
484 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
485 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
486 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
489 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
490 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
491 gtk_widget_grab_focus(GTK_WIDGET(c->view));
492 gtk_widget_show(c->vbox);
493 gtk_widget_show(c->indicator);
494 gtk_widget_show(c->scroll);
495 gtk_widget_show(GTK_WIDGET(c->view));
496 gtk_widget_show(c->win);
497 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
498 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
499 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
500 webkit_web_view_set_full_content_zoom(c->view, TRUE);
501 settings = webkit_web_view_get_settings(c->view);
502 g_object_set(G_OBJECT(settings), "user-agent", useragent, NULL);
503 uri = g_strconcat("file://", stylefile, NULL);
504 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
506 setatom(c, findprop, "");
513 gdk_display_sync(gtk_widget_get_display(c->win));
514 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
521 newwindow(Client *c, const Arg *arg) {
523 const char *cmd[7], *uri;
524 const Arg a = { .v = (void *)cmd };
530 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
537 uri = arg->v ? (char *)arg->v : c->linkhover;
545 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
546 Client *n = newclient();
551 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
552 Arg arg = {.v = text };
554 loaduri((Client *) d, &arg);
558 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
559 Client *c = (Client *)d;
563 if(((XEvent *)e)->type == PropertyNotify) {
564 ev = &((XEvent *)e)->xproperty;
567 else if(ev->state == PropertyNewValue) {
568 if(ev->atom == uriprop) {
569 arg.v = getatom(c, uriprop);
572 else if(ev->atom == findprop) {
576 return GDK_FILTER_REMOVE;
579 return GDK_FILTER_CONTINUE;
583 print(Client *c, const Arg *arg) {
584 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
588 progresschange(WebKitWebView *v, gint p, Client *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);
608 for(l = e = soup_cookie_jar_all_cookies(cookies); e; e = e->next)
609 soup_cookie_jar_delete_cookie(cookies, (SoupCookie *)e->data);
610 soup_cookies_free(l);
611 jar = soup_cookie_jar_text_new(cookiefile, TRUE);
612 for(l = e = soup_cookie_jar_all_cookies(jar); e; e = e->next)
613 soup_cookie_jar_add_cookie(cookies, (SoupCookie *)e->data);
620 scroll(Client *c, const Arg *arg) {
624 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
625 v = gtk_adjustment_get_value(a);
626 v += gtk_adjustment_get_step_increment(a) * arg->i;
628 v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
629 gtk_adjustment_set_value(a, v);
633 resize(GtkWidget *w, GtkAllocation *a, Client *c) {
638 zoom = webkit_web_view_get_zoom_level(c->view);
639 if(a->width * a->height < 300 * 400 && zoom != 0.2)
640 webkit_web_view_set_zoom_level(c->view, 0.2);
642 webkit_web_view_set_zoom_level(c->view, 1.0);
646 sigchld(int unused) {
647 if(signal(SIGCHLD, sigchld) == SIG_ERR)
648 die("Can't install SIGCHLD handler");
649 while(0 < waitpid(-1, NULL, WNOHANG));
653 setatom(Client *c, Atom a, const char *v) {
656 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), a,
657 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
665 /* clean up any zombies immediately */
667 gtk_init(NULL, NULL);
668 if (!g_thread_supported())
672 session = webkit_get_default_session();
673 uriprop = XInternAtom(dpy, "_SURF_URI", False);
674 findprop = XInternAtom(dpy, "_SURF_FIND", False);
676 /* create dirs and files */
677 cookiefile = buildpath(cookiefile);
678 dldir = buildpath(dldir);
679 scriptfile = buildpath(scriptfile);
680 stylefile = buildpath(stylefile);
682 /* cookie persistance */
683 s = webkit_get_default_session();
684 cookies = soup_cookie_jar_new();
685 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookies));
686 g_signal_connect(cookies, "changed", G_CALLBACK(changecookie), NULL);
691 source(Client *c, const Arg *arg) {
692 Arg a = { .b = FALSE };
695 s = webkit_web_view_get_view_source_mode(c->view);
696 webkit_web_view_set_view_source_mode(c->view, !s);
701 find(Client *c, const Arg *arg) {
704 s = getatom(c, findprop);
705 gboolean forward = *(gboolean *)arg;
706 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
710 stop(Client *c, const Arg *arg) {
712 webkit_download_cancel(c->download);
714 webkit_web_view_stop_loading(c->view);
719 spawn(Client *c, const Arg *arg) {
722 close(ConnectionNumber(dpy));
724 execvp(((char **)arg->v)[0], (char **)arg->v);
725 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
732 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
733 c->title = copystr(&c->title, t);
739 fputs("surf - simple browser\n", stderr);
740 die("usage: surf [-e Window] [-x] [uri]\n");
747 if(c->progress != 100)
748 t = g_strdup_printf("%s [%i%%]", c->title, c->progress);
749 else if(c->linkhover)
750 t = g_strdup(c->linkhover);
752 t = g_strdup(c->title);
754 gtk_window_set_title(GTK_WINDOW(c->win), t);
760 updatewinid(Client *c) {
761 snprintf(winid, LENGTH(winid), "%u",
762 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
766 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
767 JSStringRef jsscript;
769 JSValueRef exception = NULL;
772 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
773 jsscript = JSStringCreateWithUTF8CString(script);
774 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
779 zoom(Client *c, const Arg *arg) {
781 if(arg->i < 0) /* zoom out */
782 webkit_web_view_zoom_out(c->view);
783 else if(arg->i > 0) /* zoom in */
784 webkit_web_view_zoom_in(c->view);
787 webkit_web_view_set_zoom_level(c->view, 1.0);
791 int main(int argc, char *argv[]) {
796 /* command line args */
797 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-'; i++) {
798 if(!strcmp(argv[i], "-x"))
800 else if(!strcmp(argv[i], "-e")) {
802 embed = atoi(argv[i]);
806 else if(!strcmp(argv[i], "--")) {
810 else if(!strcmp(argv[i], "-v"))
811 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
820 loaduri(clients, &arg);