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 SoupSession *session;
60 static Client *clients = NULL;
61 static GdkNativeWindow embed = 0;
62 static gboolean showxid = FALSE;
63 static int ignorexprop = 0;
64 static char winid[64];
65 static char *progname;
67 static char *buildpath(const char *path);
68 static void cleanup(void);
69 static void clipboard(Client *c, const Arg *arg);
70 static void context(WebKitWebView *v, GtkMenu *m, Client *c);
71 static char *copystr(char **str, const char *src);
72 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
73 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c);
74 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
75 static void destroyclient(Client *c);
76 static void destroywin(GtkWidget* w, Client *c);
77 static void die(char *str);
78 static void download(Client *c, const Arg *arg);
79 static void drawindicator(Client *c);
80 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
81 static void find(Client *c, const Arg *arg);
82 static const char *getatom(Client *c, Atom a);
83 static char *geturi(Client *c);
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 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
89 static void loaduri(Client *c, const Arg *arg);
90 static void navigate(Client *c, const Arg *arg);
91 static Client *newclient(void);
92 static void newwindow(Client *c, const Arg *arg);
93 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
94 static void print(Client *c, const Arg *arg);
95 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
96 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
97 static void reload(Client *c, const Arg *arg);
98 static void resize(GtkWidget *w, GtkAllocation *a, Client *c);
99 static void scroll(Client *c, const Arg *arg);
100 static void setatom(Client *c, Atom a, const char *v);
101 static void setup(void);
102 static void sigchld(int unused);
103 static void source(Client *c, const Arg *arg);
104 static void spawn(Client *c, const Arg *arg);
105 static void stop(Client *c, const Arg *arg);
106 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
107 static void update(Client *c);
108 static void updatedownload(WebKitDownload *o, GParamSpec *pspec, Client *c);
109 static void updatewinid(Client *c);
110 static void usage(void);
111 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
112 static void zoom(Client *c, const Arg *arg);
114 /* configuration, allows nested code to access above variables */
118 buildpath(const char *path) {
122 /* creating directory */
124 apath = g_strdup(path);
126 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
127 if((p = strrchr(apath, '/'))) {
129 g_mkdir_with_parents(apath, 0755);
132 /* creating file (gives error when apath ends with "/") */
133 if((f = g_fopen(apath, "a")))
141 destroyclient(clients);
149 clipboard(Client *c, const Arg *arg) {
150 gboolean paste = *(gboolean *)arg;
153 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
155 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
159 context(WebKitWebView *v, GtkMenu *m, Client *c) {
161 GtkContainer *parent;
163 gtk_widget_hide_all(GTK_WIDGET(m));
164 gtk_widget_show(GTK_WIDGET(m));
165 for(i = 0; i < LENGTH(items); i++) {
166 parent = GTK_CONTAINER(gtk_widget_get_parent(c->items[i]));
168 gtk_container_remove(parent, c->items[i]);
169 gtk_menu_shell_append(GTK_MENU_SHELL(m), c->items[i]);
170 gtk_widget_show(c->items[i]);
175 copystr(char **str, const char *src) {
187 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
188 Client *n = newclient();
193 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c) {
194 if(!webkit_web_view_can_show_mime_type(v, m)) {
195 webkit_web_policy_decision_download(p);
202 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 arg.v = (void *)webkit_network_request_get_uri(r);
208 newwindow(NULL, &arg);
215 destroyclient(Client *c) {
219 gtk_widget_destroy(c->indicator);
220 gtk_widget_destroy(GTK_WIDGET(c->view));
221 gtk_widget_destroy(c->scroll);
222 gtk_widget_destroy(c->vbox);
223 gtk_widget_destroy(c->win);
224 for(i = 0; i < LENGTH(items); i++)
225 gtk_widget_destroy(c->items[i]);
228 for(p = clients; p && p->next != c; p = p->next);
239 destroywin(GtkWidget* w, Client *c) {
250 download(Client *c, const Arg *arg) {
252 WebKitNetworkRequest *r;
256 uri = (char *)arg->v;
258 uri = c->linkhover ? c->linkhover : geturi(c);
259 r = webkit_network_request_new(uri);
260 dl = webkit_download_new(r);
261 initdownload(c->view, dl, c);
265 drawindicator(Client *c) {
272 uri = getatom(c, uriprop);
274 width = c->progress * w->allocation.width / 100;
275 gc = gdk_gc_new(w->window);
276 gdk_color_parse(strstr(uri, "https://") == uri ?
277 progress_trust : progress, &fg);
278 gdk_gc_set_rgb_fg_color(gc, &fg);
279 gdk_draw_rectangle(w->window,
280 w->style->bg_gc[GTK_WIDGET_STATE(w)],
281 TRUE, 0, 0, w->allocation.width, w->allocation.height);
282 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
283 w->allocation.height);
288 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
294 find(Client *c, const Arg *arg) {
297 s = getatom(c, findprop);
298 gboolean forward = *(gboolean *)arg;
299 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
303 getatom(Client *c, Atom a) {
304 static char buf[BUFSIZ];
307 unsigned long ldummy;
308 unsigned char *p = NULL;
310 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
311 a, 0L, BUFSIZ, False, XA_STRING,
312 &adummy, &idummy, &ldummy, &ldummy, &p);
314 strncpy(buf, (char *)p, LENGTH(buf)-1);
325 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
331 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
332 const char *filename;
337 filename = webkit_download_get_suggested_filename(o);
338 if(!strcmp("", filename))
339 filename = "index.html";
340 uri = g_strconcat("file://", dldir, "/", filename, NULL);
341 webkit_download_set_destination_uri(c->download, uri);
344 html = g_strdup_printf("Download <b>%s</b>...", filename);
345 webkit_web_view_load_html_string(c->view, html,
346 webkit_download_get_uri(c->download));
347 g_signal_connect(c->download, "notify::progress", G_CALLBACK(updatedownload), c);
348 g_signal_connect(c->download, "notify::status", G_CALLBACK(updatedownload), c);
349 webkit_download_start(c->download);
351 c->title = copystr(&c->title, filename);
358 itemclick(GtkMenuItem *mi, Client *c) {
362 label = gtk_menu_item_get_label(mi);
363 for(i = 0; i < LENGTH(items); i++)
364 if(!strcmp(items[i].label, label))
365 items[i].func(c, &(items[i].arg));
369 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
371 gboolean processed = FALSE;
374 for(i = 0; i < LENGTH(keys); i++) {
375 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
376 && CLEANMASK(ev->state) == keys[i].mod
378 keys[i].func(c, &(keys[i].arg));
386 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
388 c->linkhover = copystr(&c->linkhover, l);
389 else if(c->linkhover) {
397 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
398 switch(webkit_web_view_get_load_status (c->view)) {
399 case WEBKIT_LOAD_COMMITTED:
400 setatom(c, uriprop, geturi(c));
402 case WEBKIT_LOAD_FINISHED:
406 case WEBKIT_LOAD_PROVISIONAL:
407 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT:
413 loaduri(Client *c, const Arg *arg) {
415 const char *uri = (char *)arg->v;
416 Arg a = { .b = FALSE };
418 if(strcmp(uri, "") == 0)
420 u = g_strrstr(uri, "://") ? g_strdup(uri)
421 : g_strdup_printf("http://%s", uri);
422 /* prevents endless loop */
423 if(c->uri && strcmp(u, c->uri) == 0) {
427 webkit_web_view_load_uri(c->view, u);
429 c->title = copystr(&c->title, u);
436 navigate(Client *c, const Arg *arg) {
437 int steps = *(int *)arg;
438 webkit_web_view_go_back_or_forward(c->view, steps);
445 WebKitWebSettings *settings;
446 GdkGeometry hints = { 1, 1 };
449 if(!(c = calloc(1, sizeof(Client))))
450 die("Cannot malloc!\n");
453 c->win = gtk_plug_new(embed);
456 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
457 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
458 * is always correct, so we should still call this function.
459 * But when doing so, we *must* differentiate between a
460 * WM_CLASS and a resource on the window. By convention, the
461 * window class (WM_CLASS) is capped, while the resource is in
462 * lowercase. Both these values come as a pair.
464 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "surf");
466 /* TA: 20091214: And set the role here as well -- so that
467 * sessions can pick this up.
469 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
471 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
472 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
473 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
474 g_signal_connect(G_OBJECT(c->win), "size-allocate", G_CALLBACK(resize), c);
476 if(!(c->items = calloc(1, sizeof(GtkWidget *) * LENGTH(items))))
477 die("Cannot malloc!\n");
480 for(i = 0; i < LENGTH(items); i++) {
481 c->items[i] = gtk_menu_item_new_with_label(items[i].label);
482 g_signal_connect(G_OBJECT(c->items[i]), "activate",
483 G_CALLBACK(itemclick), c);
487 c->vbox = gtk_vbox_new(FALSE, 0);
489 /* Scrolled Window */
490 c->scroll = gtk_scrolled_window_new(NULL, NULL);
491 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
492 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
495 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
496 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
497 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
498 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
499 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
500 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
501 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
502 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
503 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(context), c);
504 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
505 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
508 c->indicator = gtk_drawing_area_new();
509 gtk_widget_set_size_request(c->indicator, 0, 2);
510 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
511 G_CALLBACK (exposeindicator), c);
514 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
515 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
516 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
517 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
520 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
521 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
522 gtk_widget_grab_focus(GTK_WIDGET(c->view));
523 gtk_widget_show(c->vbox);
524 gtk_widget_show(c->indicator);
525 gtk_widget_show(c->scroll);
526 gtk_widget_show(GTK_WIDGET(c->view));
527 gtk_widget_show(c->win);
528 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
529 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
530 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
531 webkit_web_view_set_full_content_zoom(c->view, TRUE);
532 settings = webkit_web_view_get_settings(c->view);
533 if(!(ua = getenv("SURF_USERAGENT")))
535 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
536 uri = g_strconcat("file://", stylefile, NULL);
537 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
539 setatom(c, findprop, "");
540 setatom(c, uriprop, "");
547 gdk_display_sync(gtk_widget_get_display(c->win));
548 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
555 newwindow(Client *c, const Arg *arg) {
557 const char *cmd[7], *uri;
558 const Arg a = { .v = (void *)cmd };
564 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
571 uri = arg->v ? (char *)arg->v : c->linkhover;
579 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
580 Arg arg = {.v = text };
582 loaduri((Client *) d, &arg);
586 print(Client *c, const Arg *arg) {
587 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
591 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
592 Client *c = (Client *)d;
596 if(((XEvent *)e)->type == PropertyNotify) {
597 ev = &((XEvent *)e)->xproperty;
600 else if(ev->state == PropertyNewValue) {
601 if(ev->atom == uriprop) {
602 arg.v = getatom(c, uriprop);
605 else if(ev->atom == findprop) {
609 return GDK_FILTER_REMOVE;
612 return GDK_FILTER_CONTINUE;
616 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
617 c->progress = webkit_web_view_get_progress(c->view);;
622 reload(Client *c, const Arg *arg) {
623 gboolean nocache = *(gboolean *)arg;
625 webkit_web_view_reload_bypass_cache(c->view);
627 webkit_web_view_reload(c->view);
631 resize(GtkWidget *w, GtkAllocation *a, Client *c) {
636 zoom = webkit_web_view_get_zoom_level(c->view);
637 if(a->width * a->height < 300 * 400 && zoom != 0.2)
638 webkit_web_view_set_zoom_level(c->view, 0.2);
640 webkit_web_view_set_zoom_level(c->view, 1.0);
644 scroll(Client *c, const Arg *arg) {
648 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(c->scroll));
649 v = gtk_adjustment_get_value(a);
650 v += gtk_adjustment_get_step_increment(a) * arg->i;
652 v = MIN(v, gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a));
653 gtk_adjustment_set_value(a, v);
657 setatom(Client *c, Atom a, const char *v) {
660 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), a,
661 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
672 /* clean up any zombies immediately */
674 gtk_init(NULL, NULL);
675 if (!g_thread_supported())
679 session = webkit_get_default_session();
680 uriprop = XInternAtom(dpy, "_SURF_URI", False);
681 findprop = XInternAtom(dpy, "_SURF_FIND", False);
683 /* create dirs and files */
684 cookiefile = buildpath(cookiefile);
685 dldir = buildpath(dldir);
686 scriptfile = buildpath(scriptfile);
687 stylefile = buildpath(stylefile);
690 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
691 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
692 g_strdup_printf("http://%s", proxy);
694 puri = soup_uri_new(new_proxy);
695 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
702 sigchld(int unused) {
703 if(signal(SIGCHLD, sigchld) == SIG_ERR)
704 die("Can't install SIGCHLD handler");
705 while(0 < waitpid(-1, NULL, WNOHANG));
709 source(Client *c, const Arg *arg) {
710 Arg a = { .b = FALSE };
713 s = webkit_web_view_get_view_source_mode(c->view);
714 webkit_web_view_set_view_source_mode(c->view, !s);
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 stop(Client *c, const Arg *arg) {
734 webkit_download_cancel(c->download);
736 webkit_web_view_stop_loading(c->view);
741 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
742 c->title = copystr(&c->title, t);
750 if(c->progress != 100)
751 t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
752 else if(c->linkhover)
753 t = g_strdup(c->linkhover);
755 t = g_strdup(c->title);
757 gtk_window_set_title(GTK_WINDOW(c->win), t);
762 updatedownload(WebKitDownload *o, GParamSpec *pspec, Client *c) {
763 WebKitDownloadStatus status;
765 status = webkit_download_get_status(c->download);
766 if(status == WEBKIT_DOWNLOAD_STATUS_STARTED || status == WEBKIT_DOWNLOAD_STATUS_CREATED) {
767 c->progress = (gint)(webkit_download_get_progress(c->download)*100);
773 updatewinid(Client *c) {
774 snprintf(winid, LENGTH(winid), "%u",
775 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
780 fputs("surf - simple browser\n", stderr);
781 die("usage: surf [-e Window] [-x] [uri]\n");
785 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
786 JSStringRef jsscript;
788 JSValueRef exception = NULL;
791 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
792 jsscript = JSStringCreateWithUTF8CString(script);
793 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), NULL, 0, &exception);
798 zoom(Client *c, const Arg *arg) {
800 if(arg->i < 0) /* zoom out */
801 webkit_web_view_zoom_out(c->view);
802 else if(arg->i > 0) /* zoom in */
803 webkit_web_view_zoom_in(c->view);
806 webkit_web_view_set_zoom_level(c->view, 1.0);
810 int main(int argc, char *argv[]) {
815 /* command line args */
816 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-'; i++) {
817 if(!strcmp(argv[i], "-x"))
819 else if(!strcmp(argv[i], "-e")) {
821 embed = atoi(argv[i]);
825 else if(!strcmp(argv[i], "--")) {
829 else if(!strcmp(argv[i], "-v"))
830 die("surf-"VERSION", © 2009 surf engineers, see LICENSE for details\n");
839 loaduri(clients, &arg);