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>
27 #define LENGTH(x) (sizeof x / sizeof x[0])
28 #define COOKIEJAR_TYPE (cookiejar_get_type ())
29 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
31 enum { AtomFind, AtomGo, AtomUri, AtomLast };
33 typedef union Arg Arg;
40 typedef struct Client {
41 GtkWidget *win, *scroll, *vbox, *indicator;
43 char *title, *linkhover;
44 const char *uri, *needle;
53 void (*func)(Client *c, const Arg *arg);
60 void (*func)(Client *c, const Arg *arg);
65 SoupCookieJarText parent_instance;
70 SoupCookieJarTextClass parent_class;
73 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
76 static Atom atoms[AtomLast];
77 static Client *clients = NULL;
78 static GdkNativeWindow embed = 0;
79 static gboolean showxid = FALSE;
80 static char winid[64];
81 static gboolean loadimage = 1, plugin = 1, script = 1, using_proxy = 0;
83 static char *buildpath(const char *path);
84 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl);
85 static void cleanup(void);
86 static void clipboard(Client *c, const Arg *arg);
87 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie);
88 static void cookiejar_finalize(GObject *self);
89 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
90 static void cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec);
91 static char *copystr(char **str, const char *src);
92 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
93 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c);
94 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
95 static void destroyclient(Client *c);
96 static void destroywin(GtkWidget* w, Client *c);
97 static void die(char *str);
98 static void drawindicator(Client *c);
99 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
100 static void find(Client *c, const Arg *arg);
101 static const char *getatom(Client *c, int a);
102 static char *geturi(Client *c);
103 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
104 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
105 static void linkhover(WebKitWebView *v, const char* t, const char* l, Client *c);
106 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
107 static void loaduri(Client *c, const Arg *arg);
108 static void navigate(Client *c, const Arg *arg);
109 static Client *newclient(void);
110 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
111 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
112 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
113 static void popupactivate(GtkMenuItem *menu, Client *);
114 static void print(Client *c, const Arg *arg);
115 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
116 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
117 static void reload(Client *c, const Arg *arg);
118 static void scroll_h(Client *c, const Arg *arg);
119 static void scroll_v(Client *c, const Arg *arg);
120 static void scroll(GtkAdjustment *a, const Arg *arg);
121 static void setatom(Client *c, int a, const char *v);
122 static void setup(void);
123 static void sigchld(int unused);
124 static void source(Client *c, const Arg *arg);
125 static void spawn(Client *c, const Arg *arg);
126 static void eval(Client *c, const Arg *arg);
127 static void stop(Client *c, const Arg *arg);
128 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
129 static void update(Client *c);
130 static void updatewinid(Client *c);
131 static void usage(void);
132 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
133 static void zoom(Client *c, const Arg *arg);
135 /* configuration, allows nested code to access above variables */
139 buildpath(const char *path) {
143 /* creating directory */
145 apath = g_strdup(path);
146 } else if(path[0] == '~') {
148 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
150 apath = g_strconcat(g_get_home_dir(), "/",
154 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
157 if((p = strrchr(apath, '/'))) {
159 g_mkdir_with_parents(apath, 0700);
160 g_chmod(apath, 0700); /* in case it existed */
163 /* creating file (gives error when apath ends with "/") */
164 if((f = fopen(apath, "a"))) {
165 g_chmod(apath, 0600); /* always */
173 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
174 WebKitHitTestResultContext context;
175 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
178 g_object_get(result, "context", &context, NULL);
179 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
181 g_object_get(result, "link-uri", &arg.v, NULL);
182 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
192 destroyclient(clients);
199 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) {
200 flock(COOKIEJAR(self)->lock, LOCK_EX);
201 if(new_cookie && !new_cookie->expires && sessiontime)
202 soup_cookie_set_expires(new_cookie, soup_date_new_from_now(sessiontime));
203 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
204 flock(COOKIEJAR(self)->lock, LOCK_UN);
208 cookiejar_class_init(CookieJarClass *klass) {
209 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
210 G_OBJECT_CLASS(klass)->get_property = G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
211 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
212 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
213 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
217 cookiejar_finalize(GObject *self) {
218 close(COOKIEJAR(self)->lock);
219 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
223 cookiejar_init(CookieJar *self) {
224 self->lock = open(cookiefile, 0);
227 static SoupCookieJar *
228 cookiejar_new(const char *filename, gboolean read_only) {
229 return g_object_new(COOKIEJAR_TYPE,
230 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
231 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
235 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) {
236 flock(COOKIEJAR(self)->lock, LOCK_SH);
237 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
238 flock(COOKIEJAR(self)->lock, LOCK_UN);
242 evalscript(JSContextRef js, char *script, char* scriptname) {
243 JSStringRef jsscript, jsscriptname;
244 JSValueRef exception = NULL;
246 jsscript = JSStringCreateWithUTF8CString(script);
247 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
248 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
249 JSStringRelease(jsscript);
250 JSStringRelease(jsscriptname);
254 runscript(WebKitWebFrame *frame) {
258 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
259 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
264 clipboard(Client *c, const Arg *arg) {
265 gboolean paste = *(gboolean *)arg;
268 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
270 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
274 copystr(char **str, const char *src) {
286 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
287 Client *n = newclient();
292 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c) {
293 if(!webkit_web_view_can_show_mime_type(v, m)) {
294 webkit_web_policy_decision_download(p);
301 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
304 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
305 webkit_web_policy_decision_ignore(p);
306 arg.v = (void *)webkit_network_request_get_uri(r);
307 newwindow(NULL, &arg, 0);
314 destroyclient(Client *c) {
317 webkit_web_view_stop_loading(c->view);
318 gtk_widget_destroy(c->indicator);
319 gtk_widget_destroy(GTK_WIDGET(c->view));
320 gtk_widget_destroy(c->scroll);
321 gtk_widget_destroy(c->vbox);
322 gtk_widget_destroy(c->win);
324 for(p = clients; p && p->next != c; p = p->next);
335 destroywin(GtkWidget* w, Client *c) {
346 drawindicator(Client *c) {
356 width = c->progress * w->allocation.width / 100;
357 gc = gdk_gc_new(w->window);
358 if(strstr(uri, "https://") == uri) {
360 colorname = c->sslfailed? progress_proxy_untrust : progress_proxy_trust;
362 colorname = c->sslfailed? progress_untrust : progress_trust;
366 colorname = progress_proxy;
368 colorname = progress;
372 gdk_color_parse(colorname, &fg);
373 gdk_gc_set_rgb_fg_color(gc, &fg);
374 gdk_draw_rectangle(w->window,
375 w->style->bg_gc[GTK_WIDGET_STATE(w)],
376 TRUE, 0, 0, w->allocation.width, w->allocation.height);
377 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
378 w->allocation.height);
383 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
389 find(Client *c, const Arg *arg) {
392 s = getatom(c, AtomFind);
393 gboolean forward = *(gboolean *)arg;
394 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
398 getatom(Client *c, int a) {
399 static char buf[BUFSIZ];
402 unsigned long ldummy;
403 unsigned char *p = NULL;
405 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
406 atoms[a], 0L, BUFSIZ, False, XA_STRING,
407 &adummy, &idummy, &ldummy, &ldummy, &p);
409 strncpy(buf, (char *)p, LENGTH(buf)-1);
420 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
426 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
430 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
436 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
438 gboolean processed = FALSE;
441 for(i = 0; i < LENGTH(keys); i++) {
442 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
443 && (ev->state & keys[i].mod) == keys[i].mod
445 keys[i].func(c, &(keys[i].arg));
453 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
455 c->linkhover = copystr(&c->linkhover, l);
457 else if(c->linkhover) {
465 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
466 WebKitWebFrame *frame;
467 WebKitWebDataSource *src;
468 WebKitNetworkRequest *request;
472 switch(webkit_web_view_get_load_status (c->view)) {
473 case WEBKIT_LOAD_COMMITTED:
475 if(strstr(uri, "https://") == uri) {
476 frame = webkit_web_view_get_main_frame(c->view);
477 src = webkit_web_frame_get_data_source(frame);
478 request = webkit_web_data_source_get_request(src);
479 msg = webkit_network_request_get_message(request);
480 c->sslfailed = soup_message_get_flags(msg)
481 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
483 setatom(c, AtomUri, uri);
485 case WEBKIT_LOAD_FINISHED:
495 loaduri(Client *c, const Arg *arg) {
497 const char *uri = (char *)arg->v;
498 Arg a = { .b = FALSE };
500 if(strcmp(uri, "") == 0)
502 u = g_strrstr(uri, "://") ? g_strdup(uri)
503 : g_strdup_printf("http://%s", uri);
504 /* prevents endless loop */
505 if(c->uri && strcmp(u, c->uri) == 0) {
509 webkit_web_view_load_uri(c->view, u);
511 c->title = copystr(&c->title, u);
518 navigate(Client *c, const Arg *arg) {
519 int steps = *(int *)arg;
520 webkit_web_view_go_back_or_forward(c->view, steps);
526 WebKitWebSettings *settings;
527 WebKitWebFrame *frame;
528 GdkGeometry hints = { 1, 1 };
531 if(!(c = calloc(1, sizeof(Client))))
532 die("Cannot malloc!\n");
535 c->win = gtk_plug_new(embed);
538 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
539 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
540 * is always correct, so we should still call this function.
541 * But when doing so, we *must* differentiate between a
542 * WM_CLASS and a resource on the window. By convention, the
543 * window class (WM_CLASS) is capped, while the resource is in
544 * lowercase. Both these values come as a pair.
546 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
548 /* TA: 20091214: And set the role here as well -- so that
549 * sessions can pick this up.
551 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
553 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
554 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
555 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
558 c->vbox = gtk_vbox_new(FALSE, 0);
560 /* Scrolled Window */
561 c->scroll = gtk_scrolled_window_new(NULL, NULL);
562 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
563 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
566 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
567 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
568 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
569 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
570 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
571 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
572 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
573 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
574 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
575 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
576 g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
577 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(populatepopup), c);
580 c->indicator = gtk_drawing_area_new();
581 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
582 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
583 G_CALLBACK (exposeindicator), c);
586 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
587 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
588 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
589 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
592 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
593 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
594 gtk_widget_grab_focus(GTK_WIDGET(c->view));
595 gtk_widget_show(c->vbox);
596 gtk_widget_show(c->scroll);
597 gtk_widget_show(GTK_WIDGET(c->view));
598 gtk_widget_show(c->win);
599 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
600 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
601 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
602 webkit_web_view_set_full_content_zoom(c->view, TRUE);
603 frame = webkit_web_view_get_main_frame(c->view);
605 settings = webkit_web_view_get_settings(c->view);
606 if(!(ua = getenv("SURF_USERAGENT")))
608 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
609 uri = g_strconcat("file://", stylefile, NULL);
610 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
611 g_object_set(G_OBJECT(settings), "auto-load-images", loadimage, NULL);
612 g_object_set(G_OBJECT(settings), "enable-plugins", plugin, NULL);
613 g_object_set(G_OBJECT(settings), "enable-scripts", script, NULL);
614 g_object_set(G_OBJECT(settings), "enable-spatial-navigation", SPATIAL_BROWSING, NULL);
618 setatom(c, AtomFind, "");
619 setatom(c, AtomUri, "about:blank");
621 webkit_web_view_set_transparent(c->view, TRUE);
627 gdk_display_sync(gtk_widget_get_display(c->win));
628 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
630 if (fclose(stdout) != 0) {
631 die("Error closing stdout");
638 newwindow(Client *c, const Arg *arg, gboolean noembed) {
640 const char *cmd[10], *uri;
641 const Arg a = { .v = (void *)cmd };
645 if(embed && !noembed) {
647 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
659 uri = arg->v ? (char *)arg->v : c->linkhover;
667 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
668 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
670 for(GList *l = items; l; l = l->next) {
671 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
678 popupactivate(GtkMenuItem *menu, Client *c) {
680 * context-menu-action-2000 open link
681 * context-menu-action-1 open link in window
682 * context-menu-action-2 download linked file
683 * context-menu-action-3 copy link location
684 * context-menu-action-13 reload
685 * context-menu-action-10 back
686 * context-menu-action-11 forward
687 * context-menu-action-12 stop
692 GtkClipboard *prisel;
694 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
698 name = gtk_action_get_name(a);
699 if(!g_strcmp0(name, "context-menu-action-3")) {
700 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
701 gtk_clipboard_set_text(prisel, c->linkhover, -1);
706 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
707 Arg arg = {.v = text };
709 loaduri((Client *) d, &arg);
713 print(Client *c, const Arg *arg) {
714 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
718 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
719 Client *c = (Client *)d;
723 if(((XEvent *)e)->type == PropertyNotify) {
724 ev = &((XEvent *)e)->xproperty;
725 if(ev->state == PropertyNewValue) {
726 if(ev->atom == atoms[AtomFind]) {
729 return GDK_FILTER_REMOVE;
731 else if(ev->atom == atoms[AtomGo]) {
732 arg.v = getatom(c, AtomGo);
734 return GDK_FILTER_REMOVE;
738 return GDK_FILTER_CONTINUE;
742 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
743 c->progress = webkit_web_view_get_progress(c->view) * 100;
748 reload(Client *c, const Arg *arg) {
749 gboolean nocache = *(gboolean *)arg;
751 webkit_web_view_reload_bypass_cache(c->view);
753 webkit_web_view_reload(c->view);
757 scroll_h(Client *c, const Arg *arg) {
758 scroll(gtk_scrolled_window_get_hadjustment(
759 GTK_SCROLLED_WINDOW(c->scroll)), arg);
763 scroll_v(Client *c, const Arg *arg) {
764 scroll(gtk_scrolled_window_get_vadjustment(
765 GTK_SCROLLED_WINDOW(c->scroll)), arg);
769 scroll(GtkAdjustment *a, const Arg *arg) {
772 v = gtk_adjustment_get_value(a);
776 v += gtk_adjustment_get_page_increment(a) *
782 v += gtk_adjustment_get_step_increment(a) * arg->i;
786 v = MIN(v, gtk_adjustment_get_upper(a) -
787 gtk_adjustment_get_page_size(a));
788 gtk_adjustment_set_value(a, v);
792 setatom(Client *c, int a, const char *v) {
794 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
795 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
806 /* clean up any zombies immediately */
808 gtk_init(NULL, NULL);
809 if (!g_thread_supported())
815 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
816 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
817 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
820 cookiefile = buildpath(cookiefile);
821 scriptfile = buildpath(scriptfile);
822 stylefile = buildpath(stylefile);
824 /* request handler */
825 s = webkit_get_default_session();
828 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
831 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
832 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
835 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
836 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
837 g_strdup_printf("http://%s", proxy);
838 puri = soup_uri_new(new_proxy);
839 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
847 sigchld(int unused) {
848 if(signal(SIGCHLD, sigchld) == SIG_ERR)
849 die("Can't install SIGCHLD handler");
850 while(0 < waitpid(-1, NULL, WNOHANG));
854 source(Client *c, const Arg *arg) {
855 Arg a = { .b = FALSE };
858 s = webkit_web_view_get_view_source_mode(c->view);
859 webkit_web_view_set_view_source_mode(c->view, !s);
864 spawn(Client *c, const Arg *arg) {
867 close(ConnectionNumber(dpy));
869 execvp(((char **)arg->v)[0], (char **)arg->v);
870 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
877 eval(Client *c, const Arg *arg) {
878 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
879 evalscript(webkit_web_frame_get_global_context(frame),
880 ((char **)arg->v)[0], "");
884 stop(Client *c, const Arg *arg) {
885 webkit_web_view_stop_loading(c->view);
889 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
890 c->title = copystr(&c->title, t);
899 t = g_strdup(c->linkhover);
900 } else if(c->progress != 100) {
902 gtk_widget_show(c->indicator);
903 t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
905 gtk_widget_hide_all(c->indicator);
906 t = g_strdup(c->title);
908 gtk_window_set_title(GTK_WINDOW(c->win), t);
913 updatewinid(Client *c) {
914 snprintf(winid, LENGTH(winid), "%u",
915 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
920 fputs("surf - simple browser\n", stderr);
921 die("usage: surf [-c cookiefile] [-e xid] [-i] [-p] [-r scriptfile]"
922 " [-s] [-t stylefile] [-u useragent] [-v] [-x] [uri]\n");
926 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
931 zoom(Client *c, const Arg *arg) {
933 if(arg->i < 0) /* zoom out */
934 webkit_web_view_zoom_out(c->view);
935 else if(arg->i > 0) /* zoom in */
936 webkit_web_view_zoom_in(c->view);
939 webkit_web_view_set_zoom_level(c->view, 1.0);
944 main(int argc, char *argv[]) {
947 /* command line args */
950 cookiefile = EARGF(usage());
953 embed = strtol(EARGF(usage()), NULL, 0);
962 scriptfile = EARGF(usage());
968 stylefile = EARGF(usage());
971 useragent = EARGF(usage());
977 die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
987 loaduri(clients, &arg);