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>
19 #include <webkit/webkit.h>
20 #include <glib/gstdio.h>
21 #include <JavaScriptCore/JavaScript.h>
28 #define LENGTH(x) (sizeof x / sizeof x[0])
29 #define COOKIEJAR_TYPE (cookiejar_get_type ())
30 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
32 enum { AtomFind, AtomGo, AtomUri, AtomLast };
34 typedef union Arg Arg;
41 typedef struct Client {
42 GtkWidget *win, *scroll, *vbox, *indicator;
44 char *title, *linkhover;
45 const char *uri, *needle;
49 gboolean zoomed, fullscreen;
54 void (*func)(Client *c, const Arg *arg);
61 void (*func)(Client *c, const Arg *arg);
66 SoupCookieJarText parent_instance;
71 SoupCookieJarTextClass parent_class;
74 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
77 static Atom atoms[AtomLast];
78 static Client *clients = NULL;
79 static GdkNativeWindow embed = 0;
80 static gboolean showxid = FALSE;
81 static char winid[64];
82 static gboolean loadimages = 1, enableplugins = 1, enablescripts = 1,
84 static char togglestat[5];
86 static char *buildpath(const char *path);
87 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl);
88 static void cleanup(void);
89 static void clipboard(Client *c, const Arg *arg);
90 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie);
91 static void cookiejar_finalize(GObject *self);
92 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
93 static void cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec);
94 static char *copystr(char **str, const char *src);
95 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
96 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c);
97 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
98 static void destroyclient(Client *c);
99 static void destroywin(GtkWidget* w, Client *c);
100 static void die(char *str);
101 static void drawindicator(Client *c);
102 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
103 static void find(Client *c, const Arg *arg);
104 static void fullscreen(Client *c, const Arg *arg);
105 static const char *getatom(Client *c, int a);
106 static char *geturi(Client *c);
107 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
108 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
109 static void linkhover(WebKitWebView *v, const char* t, const char* l, Client *c);
110 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
111 static void loaduri(Client *c, const Arg *arg);
112 static void navigate(Client *c, const Arg *arg);
113 static Client *newclient(void);
114 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
115 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
116 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
117 static void popupactivate(GtkMenuItem *menu, Client *);
118 static void print(Client *c, const Arg *arg);
119 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
120 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
121 static void reload(Client *c, const Arg *arg);
122 static void scroll_h(Client *c, const Arg *arg);
123 static void scroll_v(Client *c, const Arg *arg);
124 static void scroll(GtkAdjustment *a, const Arg *arg);
125 static void setatom(Client *c, int a, const char *v);
126 static void setup(void);
127 static void sigchld(int unused);
128 static void source(Client *c, const Arg *arg);
129 static void spawn(Client *c, const Arg *arg);
130 static void eval(Client *c, const Arg *arg);
131 static void stop(Client *c, const Arg *arg);
132 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
133 static void toggle(Client *c, const Arg *arg);
134 static void gettogglestat(Client *c);
135 static void update(Client *c);
136 static void updatewinid(Client *c);
137 static void usage(void);
138 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
139 static void zoom(Client *c, const Arg *arg);
141 /* configuration, allows nested code to access above variables */
145 buildpath(const char *path) {
149 /* creating directory */
151 apath = g_strdup(path);
152 } else if(path[0] == '~') {
154 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
156 apath = g_strconcat(g_get_home_dir(), "/",
160 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
163 if((p = strrchr(apath, '/'))) {
165 g_mkdir_with_parents(apath, 0700);
166 g_chmod(apath, 0700); /* in case it existed */
169 /* creating file (gives error when apath ends with "/") */
170 if((f = fopen(apath, "a"))) {
171 g_chmod(apath, 0600); /* always */
179 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
180 WebKitHitTestResultContext context;
181 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
184 g_object_get(result, "context", &context, NULL);
185 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
187 g_object_get(result, "link-uri", &arg.v, NULL);
188 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
198 destroyclient(clients);
205 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) {
206 flock(COOKIEJAR(self)->lock, LOCK_EX);
207 if(new_cookie && !new_cookie->expires && sessiontime)
208 soup_cookie_set_expires(new_cookie, soup_date_new_from_now(sessiontime));
209 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
210 flock(COOKIEJAR(self)->lock, LOCK_UN);
214 cookiejar_class_init(CookieJarClass *klass) {
215 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
216 G_OBJECT_CLASS(klass)->get_property = G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
217 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
218 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
219 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
223 cookiejar_finalize(GObject *self) {
224 close(COOKIEJAR(self)->lock);
225 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
229 cookiejar_init(CookieJar *self) {
230 self->lock = open(cookiefile, 0);
233 static SoupCookieJar *
234 cookiejar_new(const char *filename, gboolean read_only) {
235 return g_object_new(COOKIEJAR_TYPE,
236 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
237 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
241 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) {
242 flock(COOKIEJAR(self)->lock, LOCK_SH);
243 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
244 flock(COOKIEJAR(self)->lock, LOCK_UN);
248 evalscript(JSContextRef js, char *script, char* scriptname) {
249 JSStringRef jsscript, jsscriptname;
250 JSValueRef exception = NULL;
252 jsscript = JSStringCreateWithUTF8CString(script);
253 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
254 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
255 JSStringRelease(jsscript);
256 JSStringRelease(jsscriptname);
260 runscript(WebKitWebFrame *frame) {
264 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
265 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
270 clipboard(Client *c, const Arg *arg) {
271 gboolean paste = *(gboolean *)arg;
274 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
276 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
280 copystr(char **str, const char *src) {
292 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
293 Client *n = newclient();
298 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c) {
299 if(!webkit_web_view_can_show_mime_type(v, m)) {
300 webkit_web_policy_decision_download(p);
307 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
310 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
311 webkit_web_policy_decision_ignore(p);
312 arg.v = (void *)webkit_network_request_get_uri(r);
313 newwindow(NULL, &arg, 0);
320 destroyclient(Client *c) {
323 webkit_web_view_stop_loading(c->view);
324 gtk_widget_destroy(c->indicator);
325 gtk_widget_destroy(GTK_WIDGET(c->view));
326 gtk_widget_destroy(c->scroll);
327 gtk_widget_destroy(c->vbox);
328 gtk_widget_destroy(c->win);
330 for(p = clients; p && p->next != c; p = p->next);
341 destroywin(GtkWidget* w, Client *c) {
352 drawindicator(Client *c) {
362 width = c->progress * w->allocation.width / 100;
363 gc = gdk_gc_new(w->window);
364 if(strstr(uri, "https://") == uri) {
366 colorname = c->sslfailed? progress_proxy_untrust : progress_proxy_trust;
368 colorname = c->sslfailed? progress_untrust : progress_trust;
372 colorname = progress_proxy;
374 colorname = progress;
378 gdk_color_parse(colorname, &fg);
379 gdk_gc_set_rgb_fg_color(gc, &fg);
380 gdk_draw_rectangle(w->window,
381 w->style->bg_gc[GTK_WIDGET_STATE(w)],
382 TRUE, 0, 0, w->allocation.width, w->allocation.height);
383 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
384 w->allocation.height);
389 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
395 find(Client *c, const Arg *arg) {
398 s = getatom(c, AtomFind);
399 gboolean forward = *(gboolean *)arg;
400 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
404 fullscreen(Client *c, const Arg *arg) {
406 gtk_window_unfullscreen(GTK_WINDOW(c->win));
408 gtk_window_fullscreen(GTK_WINDOW(c->win));
410 c->fullscreen = !c->fullscreen;
414 getatom(Client *c, int a) {
415 static char buf[BUFSIZ];
418 unsigned long ldummy;
419 unsigned char *p = NULL;
421 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
422 atoms[a], 0L, BUFSIZ, False, XA_STRING,
423 &adummy, &idummy, &ldummy, &ldummy, &p);
425 strncpy(buf, (char *)p, LENGTH(buf)-1);
436 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
442 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
446 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
452 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
454 gboolean processed = FALSE;
457 for(i = 0; i < LENGTH(keys); i++) {
458 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
459 && (ev->state & keys[i].mod) == keys[i].mod
461 keys[i].func(c, &(keys[i].arg));
470 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
472 c->linkhover = copystr(&c->linkhover, l);
473 } else if(c->linkhover) {
481 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
482 WebKitWebFrame *frame;
483 WebKitWebDataSource *src;
484 WebKitNetworkRequest *request;
488 switch(webkit_web_view_get_load_status (c->view)) {
489 case WEBKIT_LOAD_COMMITTED:
491 if(strstr(uri, "https://") == uri) {
492 frame = webkit_web_view_get_main_frame(c->view);
493 src = webkit_web_frame_get_data_source(frame);
494 request = webkit_web_data_source_get_request(src);
495 msg = webkit_network_request_get_message(request);
496 c->sslfailed = soup_message_get_flags(msg)
497 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
499 setatom(c, AtomUri, uri);
501 case WEBKIT_LOAD_FINISHED:
511 loaduri(Client *c, const Arg *arg) {
513 const char *uri = (char *)arg->v;
514 Arg a = { .b = FALSE };
516 if(strcmp(uri, "") == 0)
519 /* In case it's a file path. */
521 rp = realpath(uri, NULL);
522 u = g_strdup_printf("file://%s", rp);
525 u = g_strrstr(uri, "://") ? g_strdup(uri)
526 : g_strdup_printf("http://%s", uri);
529 /* prevents endless loop */
530 if(c->uri && strcmp(u, c->uri) == 0) {
533 webkit_web_view_load_uri(c->view, u);
535 c->title = copystr(&c->title, u);
542 navigate(Client *c, const Arg *arg) {
543 int steps = *(int *)arg;
544 webkit_web_view_go_back_or_forward(c->view, steps);
550 WebKitWebSettings *settings;
551 WebKitWebFrame *frame;
552 GdkGeometry hints = { 1, 1 };
555 if(!(c = calloc(1, sizeof(Client))))
556 die("Cannot malloc!\n");
560 c->win = gtk_plug_new(embed);
562 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
564 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
565 * is always correct, so we should still call this function.
566 * But when doing so, we *must* differentiate between a
567 * WM_CLASS and a resource on the window. By convention, the
568 * window class (WM_CLASS) is capped, while the resource is in
569 * lowercase. Both these values come as a pair.
571 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
573 /* TA: 20091214: And set the role here as well -- so that
574 * sessions can pick this up.
576 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
578 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
579 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
580 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
583 c->vbox = gtk_vbox_new(FALSE, 0);
585 /* Scrolled Window */
586 c->scroll = gtk_scrolled_window_new(NULL, NULL);
587 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
588 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
591 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
592 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
593 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
594 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
595 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
596 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
597 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
598 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
599 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
600 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
601 g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
602 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(populatepopup), c);
605 c->indicator = gtk_drawing_area_new();
606 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
607 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
608 G_CALLBACK (exposeindicator), c);
611 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
612 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
613 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
614 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
617 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
618 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
619 gtk_widget_grab_focus(GTK_WIDGET(c->view));
620 gtk_widget_show(c->vbox);
621 gtk_widget_show(c->scroll);
622 gtk_widget_show(GTK_WIDGET(c->view));
623 gtk_widget_show(c->win);
624 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
625 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
626 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
627 webkit_web_view_set_full_content_zoom(c->view, TRUE);
629 frame = webkit_web_view_get_main_frame(c->view);
631 settings = webkit_web_view_get_settings(c->view);
632 if(!(ua = getenv("SURF_USERAGENT")))
634 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
635 uri = g_strconcat("file://", stylefile, NULL);
636 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
637 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages, NULL);
638 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins, NULL);
639 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts, NULL);
640 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
641 spatialbrowsing, NULL);
645 setatom(c, AtomFind, "");
646 setatom(c, AtomUri, "about:blank");
648 webkit_web_view_set_transparent(c->view, TRUE);
654 gdk_display_sync(gtk_widget_get_display(c->win));
655 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
657 if (fclose(stdout) != 0) {
658 die("Error closing stdout");
665 newwindow(Client *c, const Arg *arg, gboolean noembed) {
667 const char *cmd[10], *uri;
668 const Arg a = { .v = (void *)cmd };
672 if(embed && !noembed) {
674 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
686 uri = arg->v ? (char *)arg->v : c->linkhover;
694 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
695 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
697 for(GList *l = items; l; l = l->next) {
698 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
705 popupactivate(GtkMenuItem *menu, Client *c) {
707 * context-menu-action-2000 open link
708 * context-menu-action-1 open link in window
709 * context-menu-action-2 download linked file
710 * context-menu-action-3 copy link location
711 * context-menu-action-13 reload
712 * context-menu-action-10 back
713 * context-menu-action-11 forward
714 * context-menu-action-12 stop
719 GtkClipboard *prisel;
721 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
725 name = gtk_action_get_name(a);
726 if(!g_strcmp0(name, "context-menu-action-3")) {
727 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
728 gtk_clipboard_set_text(prisel, c->linkhover, -1);
733 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
734 Arg arg = {.v = text };
736 loaduri((Client *) d, &arg);
740 print(Client *c, const Arg *arg) {
741 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
745 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
746 Client *c = (Client *)d;
750 if(((XEvent *)e)->type == PropertyNotify) {
751 ev = &((XEvent *)e)->xproperty;
752 if(ev->state == PropertyNewValue) {
753 if(ev->atom == atoms[AtomFind]) {
756 return GDK_FILTER_REMOVE;
758 else if(ev->atom == atoms[AtomGo]) {
759 arg.v = getatom(c, AtomGo);
761 return GDK_FILTER_REMOVE;
765 return GDK_FILTER_CONTINUE;
769 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
770 c->progress = webkit_web_view_get_progress(c->view) * 100;
775 reload(Client *c, const Arg *arg) {
776 gboolean nocache = *(gboolean *)arg;
778 webkit_web_view_reload_bypass_cache(c->view);
780 webkit_web_view_reload(c->view);
784 scroll_h(Client *c, const Arg *arg) {
785 scroll(gtk_scrolled_window_get_hadjustment(
786 GTK_SCROLLED_WINDOW(c->scroll)), arg);
790 scroll_v(Client *c, const Arg *arg) {
791 scroll(gtk_scrolled_window_get_vadjustment(
792 GTK_SCROLLED_WINDOW(c->scroll)), arg);
796 scroll(GtkAdjustment *a, const Arg *arg) {
799 v = gtk_adjustment_get_value(a);
803 v += gtk_adjustment_get_page_increment(a) *
809 v += gtk_adjustment_get_step_increment(a) * arg->i;
813 v = MIN(v, gtk_adjustment_get_upper(a) -
814 gtk_adjustment_get_page_size(a));
815 gtk_adjustment_set_value(a, v);
819 setatom(Client *c, int a, const char *v) {
821 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
822 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
833 /* clean up any zombies immediately */
835 gtk_init(NULL, NULL);
836 if (!g_thread_supported())
842 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
843 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
844 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
847 cookiefile = buildpath(cookiefile);
848 scriptfile = buildpath(scriptfile);
849 stylefile = buildpath(stylefile);
851 /* request handler */
852 s = webkit_get_default_session();
855 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
858 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
859 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
862 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
863 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
864 g_strdup_printf("http://%s", proxy);
865 puri = soup_uri_new(new_proxy);
866 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
874 sigchld(int unused) {
875 if(signal(SIGCHLD, sigchld) == SIG_ERR)
876 die("Can't install SIGCHLD handler");
877 while(0 < waitpid(-1, NULL, WNOHANG));
881 source(Client *c, const Arg *arg) {
882 Arg a = { .b = FALSE };
885 s = webkit_web_view_get_view_source_mode(c->view);
886 webkit_web_view_set_view_source_mode(c->view, !s);
891 spawn(Client *c, const Arg *arg) {
894 close(ConnectionNumber(dpy));
896 execvp(((char **)arg->v)[0], (char **)arg->v);
897 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
904 eval(Client *c, const Arg *arg) {
905 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
906 evalscript(webkit_web_frame_get_global_context(frame),
907 ((char **)arg->v)[0], "");
911 stop(Client *c, const Arg *arg) {
912 webkit_web_view_stop_loading(c->view);
916 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
917 c->title = copystr(&c->title, t);
922 toggle(Client *c, const Arg *arg) {
923 WebKitWebSettings *settings;
924 char *name = (char *)arg->v;
926 Arg a = { .b = FALSE };
928 settings = webkit_web_view_get_settings(c->view);
929 g_object_get(G_OBJECT(settings), name, &value, NULL);
930 g_object_set(G_OBJECT(settings), name, !value, NULL);
936 gettogglestat(Client *c){
938 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
940 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
942 togglestat[0] = value? 'C': 'c';
944 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
945 togglestat[1] = value? 'I': 'i';
947 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
948 togglestat[2] = value? 'S': 's';
950 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
951 togglestat[3] = value? 'V': 'v';
953 togglestat[4] = '\0';
964 t = g_strdup_printf("%s| %s", togglestat, c->linkhover);
965 } else if(c->progress != 100) {
967 gtk_widget_show(c->indicator);
968 t = g_strdup_printf("[%i%%] %s| %s", c->progress, togglestat,
971 gtk_widget_hide_all(c->indicator);
972 t = g_strdup_printf("%s| %s", togglestat, c->title);
975 gtk_window_set_title(GTK_WINDOW(c->win), t);
980 updatewinid(Client *c) {
981 snprintf(winid, LENGTH(winid), "%u",
982 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
987 fputs("surf - simple browser\n", stderr);
988 die("usage: surf [-c cookiefile] [-e xid] [-i] [-p] [-r scriptfile]"
989 " [-s] [-t stylefile] [-u useragent] [-v] [-x] [uri]\n");
993 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
998 zoom(Client *c, const Arg *arg) {
1002 webkit_web_view_zoom_out(c->view);
1003 } else if(arg->i > 0) {
1005 webkit_web_view_zoom_in(c->view);
1009 webkit_web_view_set_zoom_level(c->view, 1.0);
1014 main(int argc, char *argv[]) {
1017 memset(&arg, 0, sizeof(arg));
1019 /* command line args */
1022 cookiefile = EARGF(usage());
1025 embed = strtol(EARGF(usage()), NULL, 0);
1034 scriptfile = EARGF(usage());
1040 stylefile = EARGF(usage());
1043 useragent = EARGF(usage());
1049 die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
1059 loaduri(clients, &arg);
1064 return EXIT_SUCCESS;