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 loadimages = 1, enableplugins = 1, enablescripts = 1,
83 static char togglestat[5];
85 static char *buildpath(const char *path);
86 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl);
87 static void cleanup(void);
88 static void clipboard(Client *c, const Arg *arg);
89 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie);
90 static void cookiejar_finalize(GObject *self);
91 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
92 static void cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec);
93 static char *copystr(char **str, const char *src);
94 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
95 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c);
96 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
97 static void destroyclient(Client *c);
98 static void destroywin(GtkWidget* w, Client *c);
99 static void die(char *str);
100 static void drawindicator(Client *c);
101 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
102 static void find(Client *c, const Arg *arg);
103 static const char *getatom(Client *c, int a);
104 static char *geturi(Client *c);
105 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
106 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
107 static void linkhover(WebKitWebView *v, const char* t, const char* l, Client *c);
108 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
109 static void loaduri(Client *c, const Arg *arg);
110 static void navigate(Client *c, const Arg *arg);
111 static Client *newclient(void);
112 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
113 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
114 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
115 static void popupactivate(GtkMenuItem *menu, Client *);
116 static void print(Client *c, const Arg *arg);
117 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
118 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
119 static void reload(Client *c, const Arg *arg);
120 static void scroll_h(Client *c, const Arg *arg);
121 static void scroll_v(Client *c, const Arg *arg);
122 static void scroll(GtkAdjustment *a, const Arg *arg);
123 static void setatom(Client *c, int a, const char *v);
124 static void setup(void);
125 static void sigchld(int unused);
126 static void source(Client *c, const Arg *arg);
127 static void spawn(Client *c, const Arg *arg);
128 static void eval(Client *c, const Arg *arg);
129 static void stop(Client *c, const Arg *arg);
130 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
131 static void toggle(Client *c, const Arg *arg);
132 static void gettogglestat(Client *c);
133 static void update(Client *c);
134 static void updatewinid(Client *c);
135 static void usage(void);
136 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
137 static void zoom(Client *c, const Arg *arg);
139 /* configuration, allows nested code to access above variables */
143 buildpath(const char *path) {
147 /* creating directory */
149 apath = g_strdup(path);
150 } else if(path[0] == '~') {
152 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
154 apath = g_strconcat(g_get_home_dir(), "/",
158 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
161 if((p = strrchr(apath, '/'))) {
163 g_mkdir_with_parents(apath, 0700);
164 g_chmod(apath, 0700); /* in case it existed */
167 /* creating file (gives error when apath ends with "/") */
168 if((f = fopen(apath, "a"))) {
169 g_chmod(apath, 0600); /* always */
177 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
178 WebKitHitTestResultContext context;
179 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
182 g_object_get(result, "context", &context, NULL);
183 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
185 g_object_get(result, "link-uri", &arg.v, NULL);
186 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
196 destroyclient(clients);
203 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) {
204 flock(COOKIEJAR(self)->lock, LOCK_EX);
205 if(new_cookie && !new_cookie->expires && sessiontime)
206 soup_cookie_set_expires(new_cookie, soup_date_new_from_now(sessiontime));
207 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
208 flock(COOKIEJAR(self)->lock, LOCK_UN);
212 cookiejar_class_init(CookieJarClass *klass) {
213 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
214 G_OBJECT_CLASS(klass)->get_property = G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
215 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
216 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
217 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
221 cookiejar_finalize(GObject *self) {
222 close(COOKIEJAR(self)->lock);
223 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
227 cookiejar_init(CookieJar *self) {
228 self->lock = open(cookiefile, 0);
231 static SoupCookieJar *
232 cookiejar_new(const char *filename, gboolean read_only) {
233 return g_object_new(COOKIEJAR_TYPE,
234 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
235 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
239 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) {
240 flock(COOKIEJAR(self)->lock, LOCK_SH);
241 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
242 flock(COOKIEJAR(self)->lock, LOCK_UN);
246 evalscript(JSContextRef js, char *script, char* scriptname) {
247 JSStringRef jsscript, jsscriptname;
248 JSValueRef exception = NULL;
250 jsscript = JSStringCreateWithUTF8CString(script);
251 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
252 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
253 JSStringRelease(jsscript);
254 JSStringRelease(jsscriptname);
258 runscript(WebKitWebFrame *frame) {
262 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
263 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
268 clipboard(Client *c, const Arg *arg) {
269 gboolean paste = *(gboolean *)arg;
272 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
274 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
278 copystr(char **str, const char *src) {
290 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
291 Client *n = newclient();
296 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c) {
297 if(!webkit_web_view_can_show_mime_type(v, m)) {
298 webkit_web_policy_decision_download(p);
305 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
308 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
309 webkit_web_policy_decision_ignore(p);
310 arg.v = (void *)webkit_network_request_get_uri(r);
311 newwindow(NULL, &arg, 0);
318 destroyclient(Client *c) {
321 webkit_web_view_stop_loading(c->view);
322 gtk_widget_destroy(c->indicator);
323 gtk_widget_destroy(GTK_WIDGET(c->view));
324 gtk_widget_destroy(c->scroll);
325 gtk_widget_destroy(c->vbox);
326 gtk_widget_destroy(c->win);
328 for(p = clients; p && p->next != c; p = p->next);
339 destroywin(GtkWidget* w, Client *c) {
350 drawindicator(Client *c) {
360 width = c->progress * w->allocation.width / 100;
361 gc = gdk_gc_new(w->window);
362 if(strstr(uri, "https://") == uri) {
364 colorname = c->sslfailed? progress_proxy_untrust : progress_proxy_trust;
366 colorname = c->sslfailed? progress_untrust : progress_trust;
370 colorname = progress_proxy;
372 colorname = progress;
376 gdk_color_parse(colorname, &fg);
377 gdk_gc_set_rgb_fg_color(gc, &fg);
378 gdk_draw_rectangle(w->window,
379 w->style->bg_gc[GTK_WIDGET_STATE(w)],
380 TRUE, 0, 0, w->allocation.width, w->allocation.height);
381 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
382 w->allocation.height);
387 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
393 find(Client *c, const Arg *arg) {
396 s = getatom(c, AtomFind);
397 gboolean forward = *(gboolean *)arg;
398 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
402 getatom(Client *c, int a) {
403 static char buf[BUFSIZ];
406 unsigned long ldummy;
407 unsigned char *p = NULL;
409 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
410 atoms[a], 0L, BUFSIZ, False, XA_STRING,
411 &adummy, &idummy, &ldummy, &ldummy, &p);
413 strncpy(buf, (char *)p, LENGTH(buf)-1);
424 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
430 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
434 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
440 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
442 gboolean processed = FALSE;
445 for(i = 0; i < LENGTH(keys); i++) {
446 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
447 && (ev->state & keys[i].mod) == keys[i].mod
449 keys[i].func(c, &(keys[i].arg));
458 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
460 c->linkhover = copystr(&c->linkhover, l);
461 } else if(c->linkhover) {
469 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
470 WebKitWebFrame *frame;
471 WebKitWebDataSource *src;
472 WebKitNetworkRequest *request;
476 switch(webkit_web_view_get_load_status (c->view)) {
477 case WEBKIT_LOAD_COMMITTED:
479 if(strstr(uri, "https://") == uri) {
480 frame = webkit_web_view_get_main_frame(c->view);
481 src = webkit_web_frame_get_data_source(frame);
482 request = webkit_web_data_source_get_request(src);
483 msg = webkit_network_request_get_message(request);
484 c->sslfailed = soup_message_get_flags(msg)
485 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
487 setatom(c, AtomUri, uri);
489 case WEBKIT_LOAD_FINISHED:
499 loaduri(Client *c, const Arg *arg) {
501 const char *uri = (char *)arg->v;
502 Arg a = { .b = FALSE };
504 if(strcmp(uri, "") == 0)
506 u = g_strrstr(uri, "://") ? g_strdup(uri)
507 : g_strdup_printf("http://%s", uri);
508 /* prevents endless loop */
509 if(c->uri && strcmp(u, c->uri) == 0) {
512 webkit_web_view_load_uri(c->view, u);
514 c->title = copystr(&c->title, u);
521 navigate(Client *c, const Arg *arg) {
522 int steps = *(int *)arg;
523 webkit_web_view_go_back_or_forward(c->view, steps);
529 WebKitWebSettings *settings;
530 WebKitWebFrame *frame;
531 GdkGeometry hints = { 1, 1 };
534 if(!(c = calloc(1, sizeof(Client))))
535 die("Cannot malloc!\n");
539 c->win = gtk_plug_new(embed);
541 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
543 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
544 * is always correct, so we should still call this function.
545 * But when doing so, we *must* differentiate between a
546 * WM_CLASS and a resource on the window. By convention, the
547 * window class (WM_CLASS) is capped, while the resource is in
548 * lowercase. Both these values come as a pair.
550 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
552 /* TA: 20091214: And set the role here as well -- so that
553 * sessions can pick this up.
555 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
557 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
558 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
559 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
562 c->vbox = gtk_vbox_new(FALSE, 0);
564 /* Scrolled Window */
565 c->scroll = gtk_scrolled_window_new(NULL, NULL);
566 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
567 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
570 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
571 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
572 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
573 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
574 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
575 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
576 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
577 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
578 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
579 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
580 g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
581 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(populatepopup), c);
584 c->indicator = gtk_drawing_area_new();
585 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
586 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
587 G_CALLBACK (exposeindicator), c);
590 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
591 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
592 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
593 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
596 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
597 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
598 gtk_widget_grab_focus(GTK_WIDGET(c->view));
599 gtk_widget_show(c->vbox);
600 gtk_widget_show(c->scroll);
601 gtk_widget_show(GTK_WIDGET(c->view));
602 gtk_widget_show(c->win);
603 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
604 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
605 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
606 webkit_web_view_set_full_content_zoom(c->view, TRUE);
608 frame = webkit_web_view_get_main_frame(c->view);
610 settings = webkit_web_view_get_settings(c->view);
611 if(!(ua = getenv("SURF_USERAGENT")))
613 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
614 uri = g_strconcat("file://", stylefile, NULL);
615 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
616 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages, NULL);
617 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins, NULL);
618 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts, NULL);
619 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
620 spatialbrowsing, NULL);
624 setatom(c, AtomFind, "");
625 setatom(c, AtomUri, "about:blank");
627 webkit_web_view_set_transparent(c->view, TRUE);
633 gdk_display_sync(gtk_widget_get_display(c->win));
634 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
636 if (fclose(stdout) != 0) {
637 die("Error closing stdout");
644 newwindow(Client *c, const Arg *arg, gboolean noembed) {
646 const char *cmd[10], *uri;
647 const Arg a = { .v = (void *)cmd };
651 if(embed && !noembed) {
653 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
665 uri = arg->v ? (char *)arg->v : c->linkhover;
673 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
674 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
676 for(GList *l = items; l; l = l->next) {
677 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
684 popupactivate(GtkMenuItem *menu, Client *c) {
686 * context-menu-action-2000 open link
687 * context-menu-action-1 open link in window
688 * context-menu-action-2 download linked file
689 * context-menu-action-3 copy link location
690 * context-menu-action-13 reload
691 * context-menu-action-10 back
692 * context-menu-action-11 forward
693 * context-menu-action-12 stop
698 GtkClipboard *prisel;
700 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
704 name = gtk_action_get_name(a);
705 if(!g_strcmp0(name, "context-menu-action-3")) {
706 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
707 gtk_clipboard_set_text(prisel, c->linkhover, -1);
712 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
713 Arg arg = {.v = text };
715 loaduri((Client *) d, &arg);
719 print(Client *c, const Arg *arg) {
720 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
724 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
725 Client *c = (Client *)d;
729 if(((XEvent *)e)->type == PropertyNotify) {
730 ev = &((XEvent *)e)->xproperty;
731 if(ev->state == PropertyNewValue) {
732 if(ev->atom == atoms[AtomFind]) {
735 return GDK_FILTER_REMOVE;
737 else if(ev->atom == atoms[AtomGo]) {
738 arg.v = getatom(c, AtomGo);
740 return GDK_FILTER_REMOVE;
744 return GDK_FILTER_CONTINUE;
748 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
749 c->progress = webkit_web_view_get_progress(c->view) * 100;
754 reload(Client *c, const Arg *arg) {
755 gboolean nocache = *(gboolean *)arg;
757 webkit_web_view_reload_bypass_cache(c->view);
759 webkit_web_view_reload(c->view);
763 scroll_h(Client *c, const Arg *arg) {
764 scroll(gtk_scrolled_window_get_hadjustment(
765 GTK_SCROLLED_WINDOW(c->scroll)), arg);
769 scroll_v(Client *c, const Arg *arg) {
770 scroll(gtk_scrolled_window_get_vadjustment(
771 GTK_SCROLLED_WINDOW(c->scroll)), arg);
775 scroll(GtkAdjustment *a, const Arg *arg) {
778 v = gtk_adjustment_get_value(a);
782 v += gtk_adjustment_get_page_increment(a) *
788 v += gtk_adjustment_get_step_increment(a) * arg->i;
792 v = MIN(v, gtk_adjustment_get_upper(a) -
793 gtk_adjustment_get_page_size(a));
794 gtk_adjustment_set_value(a, v);
798 setatom(Client *c, int a, const char *v) {
800 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
801 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
812 /* clean up any zombies immediately */
814 gtk_init(NULL, NULL);
815 if (!g_thread_supported())
821 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
822 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
823 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
826 cookiefile = buildpath(cookiefile);
827 scriptfile = buildpath(scriptfile);
828 stylefile = buildpath(stylefile);
830 /* request handler */
831 s = webkit_get_default_session();
834 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
837 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
838 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
841 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
842 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
843 g_strdup_printf("http://%s", proxy);
844 puri = soup_uri_new(new_proxy);
845 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
853 sigchld(int unused) {
854 if(signal(SIGCHLD, sigchld) == SIG_ERR)
855 die("Can't install SIGCHLD handler");
856 while(0 < waitpid(-1, NULL, WNOHANG));
860 source(Client *c, const Arg *arg) {
861 Arg a = { .b = FALSE };
864 s = webkit_web_view_get_view_source_mode(c->view);
865 webkit_web_view_set_view_source_mode(c->view, !s);
870 spawn(Client *c, const Arg *arg) {
873 close(ConnectionNumber(dpy));
875 execvp(((char **)arg->v)[0], (char **)arg->v);
876 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
883 eval(Client *c, const Arg *arg) {
884 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
885 evalscript(webkit_web_frame_get_global_context(frame),
886 ((char **)arg->v)[0], "");
890 stop(Client *c, const Arg *arg) {
891 webkit_web_view_stop_loading(c->view);
895 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
896 c->title = copystr(&c->title, t);
901 toggle(Client *c, const Arg *arg) {
902 WebKitWebSettings *settings;
903 char *name = (char *)arg->v;
905 Arg a = { .b = FALSE };
907 settings = webkit_web_view_get_settings(c->view);
908 g_object_get(G_OBJECT(settings), name, &value, NULL);
909 g_object_set(G_OBJECT(settings), name, !value, NULL);
915 gettogglestat(Client *c){
917 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
919 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
921 togglestat[0] = value? 'C': 'c';
923 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
924 togglestat[1] = value? 'I': 'i';
926 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
927 togglestat[2] = value? 'S': 's';
929 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
930 togglestat[3] = value? 'V': 'v';
932 togglestat[4] = '\0';
943 t = g_strdup_printf("%s| %s", togglestat, c->linkhover);
944 } else if(c->progress != 100) {
946 gtk_widget_show(c->indicator);
947 t = g_strdup_printf("[%i%%] %s| %s", c->progress, togglestat,
950 gtk_widget_hide_all(c->indicator);
951 t = g_strdup_printf("%s| %s", togglestat, c->title);
954 gtk_window_set_title(GTK_WINDOW(c->win), t);
959 updatewinid(Client *c) {
960 snprintf(winid, LENGTH(winid), "%u",
961 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
966 fputs("surf - simple browser\n", stderr);
967 die("usage: surf [-c cookiefile] [-e xid] [-i] [-p] [-r scriptfile]"
968 " [-s] [-t stylefile] [-u useragent] [-v] [-x] [uri]\n");
972 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
977 zoom(Client *c, const Arg *arg) {
979 if(arg->i < 0) /* zoom out */
980 webkit_web_view_zoom_out(c->view);
981 else if(arg->i > 0) /* zoom in */
982 webkit_web_view_zoom_in(c->view);
985 webkit_web_view_set_zoom_level(c->view, 1.0);
990 main(int argc, char *argv[]) {
993 memset(&arg, 0, sizeof(arg));
995 /* command line args */
998 cookiefile = EARGF(usage());
1001 embed = strtol(EARGF(usage()), NULL, 0);
1010 scriptfile = EARGF(usage());
1016 stylefile = EARGF(usage());
1019 useragent = EARGF(usage());
1025 die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
1035 loaduri(clients, &arg);
1040 return EXIT_SUCCESS;