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 toggle(Client *c, const Arg *arg);
130 static void update(Client *c);
131 static void updatewinid(Client *c);
132 static void usage(void);
133 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
134 static void zoom(Client *c, const Arg *arg);
136 /* configuration, allows nested code to access above variables */
140 buildpath(const char *path) {
144 /* creating directory */
146 apath = g_strdup(path);
147 } else if(path[0] == '~') {
149 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
151 apath = g_strconcat(g_get_home_dir(), "/",
155 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
158 if((p = strrchr(apath, '/'))) {
160 g_mkdir_with_parents(apath, 0700);
161 g_chmod(apath, 0700); /* in case it existed */
164 /* creating file (gives error when apath ends with "/") */
165 if((f = fopen(apath, "a"))) {
166 g_chmod(apath, 0600); /* always */
174 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
175 WebKitHitTestResultContext context;
176 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
179 g_object_get(result, "context", &context, NULL);
180 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
182 g_object_get(result, "link-uri", &arg.v, NULL);
183 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
193 destroyclient(clients);
200 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) {
201 flock(COOKIEJAR(self)->lock, LOCK_EX);
202 if(new_cookie && !new_cookie->expires && sessiontime)
203 soup_cookie_set_expires(new_cookie, soup_date_new_from_now(sessiontime));
204 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
205 flock(COOKIEJAR(self)->lock, LOCK_UN);
209 cookiejar_class_init(CookieJarClass *klass) {
210 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
211 G_OBJECT_CLASS(klass)->get_property = G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
212 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
213 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
214 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
218 cookiejar_finalize(GObject *self) {
219 close(COOKIEJAR(self)->lock);
220 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
224 cookiejar_init(CookieJar *self) {
225 self->lock = open(cookiefile, 0);
228 static SoupCookieJar *
229 cookiejar_new(const char *filename, gboolean read_only) {
230 return g_object_new(COOKIEJAR_TYPE,
231 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
232 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
236 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) {
237 flock(COOKIEJAR(self)->lock, LOCK_SH);
238 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
239 flock(COOKIEJAR(self)->lock, LOCK_UN);
243 evalscript(JSContextRef js, char *script, char* scriptname) {
244 JSStringRef jsscript, jsscriptname;
245 JSValueRef exception = NULL;
247 jsscript = JSStringCreateWithUTF8CString(script);
248 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
249 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
250 JSStringRelease(jsscript);
251 JSStringRelease(jsscriptname);
255 runscript(WebKitWebFrame *frame) {
259 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
260 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
265 clipboard(Client *c, const Arg *arg) {
266 gboolean paste = *(gboolean *)arg;
269 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
271 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
275 copystr(char **str, const char *src) {
287 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
288 Client *n = newclient();
293 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c) {
294 if(!webkit_web_view_can_show_mime_type(v, m)) {
295 webkit_web_policy_decision_download(p);
302 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
305 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
306 webkit_web_policy_decision_ignore(p);
307 arg.v = (void *)webkit_network_request_get_uri(r);
308 newwindow(NULL, &arg, 0);
315 destroyclient(Client *c) {
318 webkit_web_view_stop_loading(c->view);
319 gtk_widget_destroy(c->indicator);
320 gtk_widget_destroy(GTK_WIDGET(c->view));
321 gtk_widget_destroy(c->scroll);
322 gtk_widget_destroy(c->vbox);
323 gtk_widget_destroy(c->win);
325 for(p = clients; p && p->next != c; p = p->next);
336 destroywin(GtkWidget* w, Client *c) {
347 drawindicator(Client *c) {
357 width = c->progress * w->allocation.width / 100;
358 gc = gdk_gc_new(w->window);
359 if(strstr(uri, "https://") == uri) {
361 colorname = c->sslfailed? progress_proxy_untrust : progress_proxy_trust;
363 colorname = c->sslfailed? progress_untrust : progress_trust;
367 colorname = progress_proxy;
369 colorname = progress;
373 gdk_color_parse(colorname, &fg);
374 gdk_gc_set_rgb_fg_color(gc, &fg);
375 gdk_draw_rectangle(w->window,
376 w->style->bg_gc[GTK_WIDGET_STATE(w)],
377 TRUE, 0, 0, w->allocation.width, w->allocation.height);
378 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
379 w->allocation.height);
384 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
390 find(Client *c, const Arg *arg) {
393 s = getatom(c, AtomFind);
394 gboolean forward = *(gboolean *)arg;
395 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
399 getatom(Client *c, int a) {
400 static char buf[BUFSIZ];
403 unsigned long ldummy;
404 unsigned char *p = NULL;
406 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
407 atoms[a], 0L, BUFSIZ, False, XA_STRING,
408 &adummy, &idummy, &ldummy, &ldummy, &p);
410 strncpy(buf, (char *)p, LENGTH(buf)-1);
421 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
427 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
431 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
437 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
439 gboolean processed = FALSE;
442 for(i = 0; i < LENGTH(keys); i++) {
443 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
444 && (ev->state & keys[i].mod) == keys[i].mod
446 keys[i].func(c, &(keys[i].arg));
454 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
456 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) {
508 webkit_web_view_load_uri(c->view, u);
510 c->title = copystr(&c->title, u);
517 navigate(Client *c, const Arg *arg) {
518 int steps = *(int *)arg;
519 webkit_web_view_go_back_or_forward(c->view, steps);
525 WebKitWebSettings *settings;
526 WebKitWebFrame *frame;
527 GdkGeometry hints = { 1, 1 };
530 if(!(c = calloc(1, sizeof(Client))))
531 die("Cannot malloc!\n");
534 c->win = gtk_plug_new(embed);
537 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
538 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
539 * is always correct, so we should still call this function.
540 * But when doing so, we *must* differentiate between a
541 * WM_CLASS and a resource on the window. By convention, the
542 * window class (WM_CLASS) is capped, while the resource is in
543 * lowercase. Both these values come as a pair.
545 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
547 /* TA: 20091214: And set the role here as well -- so that
548 * sessions can pick this up.
550 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
552 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
553 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
554 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
557 c->vbox = gtk_vbox_new(FALSE, 0);
559 /* Scrolled Window */
560 c->scroll = gtk_scrolled_window_new(NULL, NULL);
561 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
562 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
565 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
566 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
567 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
568 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
569 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
570 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
571 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
572 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
573 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
574 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
575 g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
576 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(populatepopup), c);
579 c->indicator = gtk_drawing_area_new();
580 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
581 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
582 G_CALLBACK (exposeindicator), c);
585 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
586 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
587 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
588 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
591 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
592 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
593 gtk_widget_grab_focus(GTK_WIDGET(c->view));
594 gtk_widget_show(c->vbox);
595 gtk_widget_show(c->scroll);
596 gtk_widget_show(GTK_WIDGET(c->view));
597 gtk_widget_show(c->win);
598 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
599 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
600 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
601 webkit_web_view_set_full_content_zoom(c->view, TRUE);
602 frame = webkit_web_view_get_main_frame(c->view);
604 settings = webkit_web_view_get_settings(c->view);
605 if(!(ua = getenv("SURF_USERAGENT")))
607 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
608 uri = g_strconcat("file://", stylefile, NULL);
609 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
610 g_object_set(G_OBJECT(settings), "auto-load-images", loadimage, NULL);
611 g_object_set(G_OBJECT(settings), "enable-plugins", plugin, NULL);
612 g_object_set(G_OBJECT(settings), "enable-scripts", script, NULL);
613 g_object_set(G_OBJECT(settings), "enable-spatial-navigation", SPATIAL_BROWSING, NULL);
617 setatom(c, AtomFind, "");
618 setatom(c, AtomUri, "about:blank");
620 webkit_web_view_set_transparent(c->view, TRUE);
626 gdk_display_sync(gtk_widget_get_display(c->win));
627 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
629 if (fclose(stdout) != 0) {
630 die("Error closing stdout");
637 newwindow(Client *c, const Arg *arg, gboolean noembed) {
639 const char *cmd[10], *uri;
640 const Arg a = { .v = (void *)cmd };
644 if(embed && !noembed) {
646 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
658 uri = arg->v ? (char *)arg->v : c->linkhover;
666 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
667 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
669 for(GList *l = items; l; l = l->next) {
670 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
677 popupactivate(GtkMenuItem *menu, Client *c) {
679 * context-menu-action-2000 open link
680 * context-menu-action-1 open link in window
681 * context-menu-action-2 download linked file
682 * context-menu-action-3 copy link location
683 * context-menu-action-13 reload
684 * context-menu-action-10 back
685 * context-menu-action-11 forward
686 * context-menu-action-12 stop
691 GtkClipboard *prisel;
693 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
697 name = gtk_action_get_name(a);
698 if(!g_strcmp0(name, "context-menu-action-3")) {
699 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
700 gtk_clipboard_set_text(prisel, c->linkhover, -1);
705 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
706 Arg arg = {.v = text };
708 loaduri((Client *) d, &arg);
712 print(Client *c, const Arg *arg) {
713 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
717 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
718 Client *c = (Client *)d;
722 if(((XEvent *)e)->type == PropertyNotify) {
723 ev = &((XEvent *)e)->xproperty;
724 if(ev->state == PropertyNewValue) {
725 if(ev->atom == atoms[AtomFind]) {
728 return GDK_FILTER_REMOVE;
730 else if(ev->atom == atoms[AtomGo]) {
731 arg.v = getatom(c, AtomGo);
733 return GDK_FILTER_REMOVE;
737 return GDK_FILTER_CONTINUE;
741 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
742 c->progress = webkit_web_view_get_progress(c->view) * 100;
747 reload(Client *c, const Arg *arg) {
748 gboolean nocache = *(gboolean *)arg;
750 webkit_web_view_reload_bypass_cache(c->view);
752 webkit_web_view_reload(c->view);
756 scroll_h(Client *c, const Arg *arg) {
757 scroll(gtk_scrolled_window_get_hadjustment(
758 GTK_SCROLLED_WINDOW(c->scroll)), arg);
762 scroll_v(Client *c, const Arg *arg) {
763 scroll(gtk_scrolled_window_get_vadjustment(
764 GTK_SCROLLED_WINDOW(c->scroll)), arg);
768 scroll(GtkAdjustment *a, const Arg *arg) {
771 v = gtk_adjustment_get_value(a);
775 v += gtk_adjustment_get_page_increment(a) *
781 v += gtk_adjustment_get_step_increment(a) * arg->i;
785 v = MIN(v, gtk_adjustment_get_upper(a) -
786 gtk_adjustment_get_page_size(a));
787 gtk_adjustment_set_value(a, v);
791 setatom(Client *c, int a, const char *v) {
793 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
794 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
805 /* clean up any zombies immediately */
807 gtk_init(NULL, NULL);
808 if (!g_thread_supported())
814 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
815 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
816 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
819 cookiefile = buildpath(cookiefile);
820 scriptfile = buildpath(scriptfile);
821 stylefile = buildpath(stylefile);
823 /* request handler */
824 s = webkit_get_default_session();
827 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
830 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
831 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
834 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
835 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
836 g_strdup_printf("http://%s", proxy);
837 puri = soup_uri_new(new_proxy);
838 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
846 sigchld(int unused) {
847 if(signal(SIGCHLD, sigchld) == SIG_ERR)
848 die("Can't install SIGCHLD handler");
849 while(0 < waitpid(-1, NULL, WNOHANG));
853 source(Client *c, const Arg *arg) {
854 Arg a = { .b = FALSE };
857 s = webkit_web_view_get_view_source_mode(c->view);
858 webkit_web_view_set_view_source_mode(c->view, !s);
863 spawn(Client *c, const Arg *arg) {
866 close(ConnectionNumber(dpy));
868 execvp(((char **)arg->v)[0], (char **)arg->v);
869 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
876 eval(Client *c, const Arg *arg) {
877 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
878 evalscript(webkit_web_frame_get_global_context(frame),
879 ((char **)arg->v)[0], "");
883 stop(Client *c, const Arg *arg) {
884 webkit_web_view_stop_loading(c->view);
888 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
889 c->title = copystr(&c->title, t);
894 toggle(Client *c, const Arg *arg) {
895 WebKitWebSettings *settings;
896 char *name = (char *)arg->v;
898 Arg a = { .b = FALSE };
900 settings = webkit_web_view_get_settings(c->view);
901 g_object_get(G_OBJECT(settings), name, &value, NULL);
902 g_object_set(G_OBJECT(settings), name, !value, NULL);
912 t = g_strdup(c->linkhover);
913 } else if(c->progress != 100) {
915 gtk_widget_show(c->indicator);
916 t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
918 gtk_widget_hide_all(c->indicator);
919 t = g_strdup(c->title);
922 gtk_window_set_title(GTK_WINDOW(c->win), t);
927 updatewinid(Client *c) {
928 snprintf(winid, LENGTH(winid), "%u",
929 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
934 fputs("surf - simple browser\n", stderr);
935 die("usage: surf [-c cookiefile] [-e xid] [-i] [-p] [-r scriptfile]"
936 " [-s] [-t stylefile] [-u useragent] [-v] [-x] [uri]\n");
940 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
945 zoom(Client *c, const Arg *arg) {
947 if(arg->i < 0) /* zoom out */
948 webkit_web_view_zoom_out(c->view);
949 else if(arg->i > 0) /* zoom in */
950 webkit_web_view_zoom_in(c->view);
953 webkit_web_view_set_zoom_level(c->view, 1.0);
958 main(int argc, char *argv[]) {
961 memset(&arg, 0, sizeof(arg));
963 /* command line args */
966 cookiefile = EARGF(usage());
969 embed = strtol(EARGF(usage()), NULL, 0);
978 scriptfile = EARGF(usage());
984 stylefile = EARGF(usage());
987 useragent = EARGF(usage());
993 die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
1003 loaduri(clients, &arg);
1008 return EXIT_SUCCESS;