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>
23 #define LENGTH(x) (sizeof x / sizeof x[0])
24 #define COOKIEJAR_TYPE (cookiejar_get_type ())
25 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
27 enum { AtomFind, AtomGo, AtomUri, AtomLast };
29 typedef union Arg Arg;
36 typedef struct Client {
37 GtkWidget *win, *scroll, *vbox, *indicator;
39 char *title, *linkhover;
40 const char *uri, *needle;
49 void (*func)(Client *c, const Arg *arg);
56 void (*func)(Client *c, const Arg *arg);
61 SoupCookieJarText parent_instance;
66 SoupCookieJarTextClass parent_class;
69 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
72 static Atom atoms[AtomLast];
73 static Client *clients = NULL;
74 static GdkNativeWindow embed = 0;
75 static gboolean showxid = FALSE;
76 static char winid[64];
77 static char *progname;
78 static gboolean loadimage = 1, plugin = 1, script = 1, using_proxy = 0;
80 static char *buildpath(const char *path);
81 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl);
82 static void cleanup(void);
83 static void clipboard(Client *c, const Arg *arg);
84 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie);
85 static void cookiejar_finalize(GObject *self);
86 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
87 static void cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec);
88 static char *copystr(char **str, const char *src);
89 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c);
90 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c);
91 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c);
92 static void destroyclient(Client *c);
93 static void destroywin(GtkWidget* w, Client *c);
94 static void die(char *str);
95 static void drawindicator(Client *c);
96 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
97 static void find(Client *c, const Arg *arg);
98 static const char *getatom(Client *c, int a);
99 static char *geturi(Client *c);
100 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
101 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
102 static void linkhover(WebKitWebView *v, const char* t, const char* l, Client *c);
103 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
104 static void loaduri(Client *c, const Arg *arg);
105 static void navigate(Client *c, const Arg *arg);
106 static Client *newclient(void);
107 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
108 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
109 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
110 static void popupactivate(GtkMenuItem *menu, Client *);
111 static void print(Client *c, const Arg *arg);
112 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event, gpointer d);
113 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
114 static void reload(Client *c, const Arg *arg);
115 static void scroll_h(Client *c, const Arg *arg);
116 static void scroll_v(Client *c, const Arg *arg);
117 static void scroll(GtkAdjustment *a, const Arg *arg);
118 static void setatom(Client *c, int a, const char *v);
119 static void setup(void);
120 static void sigchld(int unused);
121 static void source(Client *c, const Arg *arg);
122 static void spawn(Client *c, const Arg *arg);
123 static void eval(Client *c, const Arg *arg);
124 static void stop(Client *c, const Arg *arg);
125 static void titlechange(WebKitWebView *v, WebKitWebFrame* frame, const char* title, Client *c);
126 static void update(Client *c);
127 static void updatewinid(Client *c);
128 static void usage(void);
129 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c);
130 static void zoom(Client *c, const Arg *arg);
132 /* configuration, allows nested code to access above variables */
136 buildpath(const char *path) {
140 /* creating directory */
142 apath = g_strdup(path);
144 apath = g_strconcat(g_get_home_dir(), "/", path, NULL);
145 if((p = strrchr(apath, '/'))) {
147 g_mkdir_with_parents(apath, 0700);
148 g_chmod(apath, 0700); /* in case it existed */
151 /* creating file (gives error when apath ends with "/") */
152 if((f = fopen(apath, "a"))) {
153 g_chmod(apath, 0600); /* always */
160 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
161 WebKitHitTestResultContext context;
162 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web, e);
165 g_object_get(result, "context", &context, NULL);
166 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
168 g_object_get(result, "link-uri", &arg.v, NULL);
169 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
179 destroyclient(clients);
186 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) {
187 flock(COOKIEJAR(self)->lock, LOCK_EX);
188 if(new_cookie && !new_cookie->expires && sessiontime)
189 soup_cookie_set_expires(new_cookie, soup_date_new_from_now(sessiontime));
190 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
191 flock(COOKIEJAR(self)->lock, LOCK_UN);
195 cookiejar_class_init(CookieJarClass *klass) {
196 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
197 G_OBJECT_CLASS(klass)->get_property = G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
198 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
199 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
200 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
204 cookiejar_finalize(GObject *self) {
205 close(COOKIEJAR(self)->lock);
206 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
210 cookiejar_init(CookieJar *self) {
211 self->lock = open(cookiefile, 0);
214 static SoupCookieJar *
215 cookiejar_new(const char *filename, gboolean read_only) {
216 return g_object_new(COOKIEJAR_TYPE,
217 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
218 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
222 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) {
223 flock(COOKIEJAR(self)->lock, LOCK_SH);
224 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
225 flock(COOKIEJAR(self)->lock, LOCK_UN);
229 evalscript(JSContextRef js, char *script, char* scriptname) {
230 JSStringRef jsscript, jsscriptname;
231 JSValueRef exception = NULL;
233 jsscript = JSStringCreateWithUTF8CString(script);
234 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
235 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
236 JSStringRelease(jsscript);
237 JSStringRelease(jsscriptname);
241 runscript(WebKitWebFrame *frame) {
245 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
246 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
251 clipboard(Client *c, const Arg *arg) {
252 gboolean paste = *(gboolean *)arg;
255 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
257 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
261 copystr(char **str, const char *src) {
273 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
274 Client *n = newclient();
279 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p, Client *c) {
280 if(!webkit_web_view_can_show_mime_type(v, m)) {
281 webkit_web_policy_decision_download(p);
288 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r, WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p, Client *c) {
291 if(webkit_web_navigation_action_get_reason(n) == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
292 webkit_web_policy_decision_ignore(p);
293 arg.v = (void *)webkit_network_request_get_uri(r);
294 newwindow(NULL, &arg, 0);
301 destroyclient(Client *c) {
304 webkit_web_view_stop_loading(c->view);
305 gtk_widget_destroy(c->indicator);
306 gtk_widget_destroy(GTK_WIDGET(c->view));
307 gtk_widget_destroy(c->scroll);
308 gtk_widget_destroy(c->vbox);
309 gtk_widget_destroy(c->win);
311 for(p = clients; p && p->next != c; p = p->next);
322 destroywin(GtkWidget* w, Client *c) {
333 drawindicator(Client *c) {
343 width = c->progress * w->allocation.width / 100;
344 gc = gdk_gc_new(w->window);
345 if(strstr(uri, "https://") == uri) {
347 colorname = c->sslfailed? progress_proxy_untrust : progress_proxy_trust;
349 colorname = c->sslfailed? progress_untrust : progress_trust;
353 colorname = progress_proxy;
355 colorname = progress;
359 gdk_color_parse(colorname, &fg);
360 gdk_gc_set_rgb_fg_color(gc, &fg);
361 gdk_draw_rectangle(w->window,
362 w->style->bg_gc[GTK_WIDGET_STATE(w)],
363 TRUE, 0, 0, w->allocation.width, w->allocation.height);
364 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
365 w->allocation.height);
370 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
376 find(Client *c, const Arg *arg) {
379 s = getatom(c, AtomFind);
380 gboolean forward = *(gboolean *)arg;
381 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
385 getatom(Client *c, int a) {
386 static char buf[BUFSIZ];
389 unsigned long ldummy;
390 unsigned char *p = NULL;
392 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
393 atoms[a], 0L, BUFSIZ, False, XA_STRING,
394 &adummy, &idummy, &ldummy, &ldummy, &p);
396 strncpy(buf, (char *)p, LENGTH(buf)-1);
407 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
413 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
417 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
423 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
425 gboolean processed = FALSE;
428 for(i = 0; i < LENGTH(keys); i++) {
429 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
430 && (ev->state & keys[i].mod) == keys[i].mod
432 keys[i].func(c, &(keys[i].arg));
440 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
442 c->linkhover = copystr(&c->linkhover, l);
444 else if(c->linkhover) {
452 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
453 WebKitWebFrame *frame;
454 WebKitWebDataSource *src;
455 WebKitNetworkRequest *request;
459 switch(webkit_web_view_get_load_status (c->view)) {
460 case WEBKIT_LOAD_COMMITTED:
462 if(strstr(uri, "https://") == uri) {
463 frame = webkit_web_view_get_main_frame(c->view);
464 src = webkit_web_frame_get_data_source(frame);
465 request = webkit_web_data_source_get_request(src);
466 msg = webkit_network_request_get_message(request);
467 c->sslfailed = soup_message_get_flags(msg)
468 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
470 setatom(c, AtomUri, uri);
472 case WEBKIT_LOAD_FINISHED:
482 loaduri(Client *c, const Arg *arg) {
484 const char *uri = (char *)arg->v;
485 Arg a = { .b = FALSE };
487 if(strcmp(uri, "") == 0)
489 u = g_strrstr(uri, "://") ? g_strdup(uri)
490 : g_strdup_printf("http://%s", uri);
491 /* prevents endless loop */
492 if(c->uri && strcmp(u, c->uri) == 0) {
496 webkit_web_view_load_uri(c->view, u);
498 c->title = copystr(&c->title, u);
505 navigate(Client *c, const Arg *arg) {
506 int steps = *(int *)arg;
507 webkit_web_view_go_back_or_forward(c->view, steps);
513 WebKitWebSettings *settings;
514 WebKitWebFrame *frame;
515 GdkGeometry hints = { 1, 1 };
518 if(!(c = calloc(1, sizeof(Client))))
519 die("Cannot malloc!\n");
522 c->win = gtk_plug_new(embed);
525 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
526 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
527 * is always correct, so we should still call this function.
528 * But when doing so, we *must* differentiate between a
529 * WM_CLASS and a resource on the window. By convention, the
530 * window class (WM_CLASS) is capped, while the resource is in
531 * lowercase. Both these values come as a pair.
533 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
535 /* TA: 20091214: And set the role here as well -- so that
536 * sessions can pick this up.
538 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
540 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
541 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
542 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
545 c->vbox = gtk_vbox_new(FALSE, 0);
547 /* Scrolled Window */
548 c->scroll = gtk_scrolled_window_new(NULL, NULL);
549 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
550 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
553 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
554 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
555 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
556 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
557 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
558 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
559 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
560 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
561 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
562 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
563 g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
564 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(populatepopup), c);
567 c->indicator = gtk_drawing_area_new();
568 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
569 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
570 G_CALLBACK (exposeindicator), c);
573 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
574 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
575 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
576 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
579 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
580 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
581 gtk_widget_grab_focus(GTK_WIDGET(c->view));
582 gtk_widget_show(c->vbox);
583 gtk_widget_show(c->scroll);
584 gtk_widget_show(GTK_WIDGET(c->view));
585 gtk_widget_show(c->win);
586 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
587 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
588 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
589 webkit_web_view_set_full_content_zoom(c->view, TRUE);
590 frame = webkit_web_view_get_main_frame(c->view);
592 settings = webkit_web_view_get_settings(c->view);
593 if(!(ua = getenv("SURF_USERAGENT")))
595 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
596 uri = g_strconcat("file://", stylefile, NULL);
597 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
598 g_object_set(G_OBJECT(settings), "auto-load-images", loadimage, NULL);
599 g_object_set(G_OBJECT(settings), "enable-plugins", plugin, NULL);
600 g_object_set(G_OBJECT(settings), "enable-scripts", script, NULL);
601 g_object_set(G_OBJECT(settings), "enable-spatial-navigation", SPATIAL_BROWSING, NULL);
605 setatom(c, AtomFind, "");
606 setatom(c, AtomUri, "about:blank");
608 webkit_web_view_set_transparent(c->view, TRUE);
614 gdk_display_sync(gtk_widget_get_display(c->win));
615 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
617 if (fclose(stdout) != 0) {
618 die("Error closing stdout");
625 newwindow(Client *c, const Arg *arg, gboolean noembed) {
627 const char *cmd[10], *uri;
628 const Arg a = { .v = (void *)cmd };
632 if(embed && !noembed) {
634 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
646 uri = arg->v ? (char *)arg->v : c->linkhover;
654 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
655 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
657 for(GList *l = items; l; l = l->next) {
658 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
665 popupactivate(GtkMenuItem *menu, Client *c) {
667 * context-menu-action-2000 open link
668 * context-menu-action-1 open link in window
669 * context-menu-action-2 download linked file
670 * context-menu-action-3 copy link location
671 * context-menu-action-13 reload
672 * context-menu-action-10 back
673 * context-menu-action-11 forward
674 * context-menu-action-12 stop
679 GtkClipboard *prisel;
681 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
685 name = gtk_action_get_name(a);
686 if(!g_strcmp0(name, "context-menu-action-3")) {
687 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
688 gtk_clipboard_set_text(prisel, c->linkhover, -1);
693 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
694 Arg arg = {.v = text };
696 loaduri((Client *) d, &arg);
700 print(Client *c, const Arg *arg) {
701 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
705 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
706 Client *c = (Client *)d;
710 if(((XEvent *)e)->type == PropertyNotify) {
711 ev = &((XEvent *)e)->xproperty;
712 if(ev->state == PropertyNewValue) {
713 if(ev->atom == atoms[AtomFind]) {
716 return GDK_FILTER_REMOVE;
718 else if(ev->atom == atoms[AtomGo]) {
719 arg.v = getatom(c, AtomGo);
721 return GDK_FILTER_REMOVE;
725 return GDK_FILTER_CONTINUE;
729 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
730 c->progress = webkit_web_view_get_progress(c->view) * 100;
735 reload(Client *c, const Arg *arg) {
736 gboolean nocache = *(gboolean *)arg;
738 webkit_web_view_reload_bypass_cache(c->view);
740 webkit_web_view_reload(c->view);
744 scroll_h(Client *c, const Arg *arg) {
745 scroll(gtk_scrolled_window_get_hadjustment(
746 GTK_SCROLLED_WINDOW(c->scroll)), arg);
750 scroll_v(Client *c, const Arg *arg) {
751 scroll(gtk_scrolled_window_get_vadjustment(
752 GTK_SCROLLED_WINDOW(c->scroll)), arg);
756 scroll(GtkAdjustment *a, const Arg *arg) {
759 v = gtk_adjustment_get_value(a);
763 v += gtk_adjustment_get_page_increment(a) *
769 v += gtk_adjustment_get_step_increment(a) * arg->i;
773 v = MIN(v, gtk_adjustment_get_upper(a) -
774 gtk_adjustment_get_page_size(a));
775 gtk_adjustment_set_value(a, v);
779 setatom(Client *c, int a, const char *v) {
781 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
782 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
793 /* clean up any zombies immediately */
795 gtk_init(NULL, NULL);
796 if (!g_thread_supported())
802 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
803 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
804 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
807 cookiefile = buildpath(cookiefile);
808 scriptfile = buildpath(scriptfile);
809 stylefile = buildpath(stylefile);
811 /* request handler */
812 s = webkit_get_default_session();
815 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
818 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
819 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
822 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
823 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
824 g_strdup_printf("http://%s", proxy);
825 puri = soup_uri_new(new_proxy);
826 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
834 sigchld(int unused) {
835 if(signal(SIGCHLD, sigchld) == SIG_ERR)
836 die("Can't install SIGCHLD handler");
837 while(0 < waitpid(-1, NULL, WNOHANG));
841 source(Client *c, const Arg *arg) {
842 Arg a = { .b = FALSE };
845 s = webkit_web_view_get_view_source_mode(c->view);
846 webkit_web_view_set_view_source_mode(c->view, !s);
851 spawn(Client *c, const Arg *arg) {
854 close(ConnectionNumber(dpy));
856 execvp(((char **)arg->v)[0], (char **)arg->v);
857 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
864 eval(Client *c, const Arg *arg) {
865 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
866 evalscript(webkit_web_frame_get_global_context(frame),
867 ((char **)arg->v)[0], "");
871 stop(Client *c, const Arg *arg) {
872 webkit_web_view_stop_loading(c->view);
876 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
877 c->title = copystr(&c->title, t);
886 t = g_strdup(c->linkhover);
887 } else if(c->progress != 100) {
889 gtk_widget_show(c->indicator);
890 t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
892 gtk_widget_hide_all(c->indicator);
893 t = g_strdup(c->title);
895 gtk_window_set_title(GTK_WINDOW(c->win), t);
900 updatewinid(Client *c) {
901 snprintf(winid, LENGTH(winid), "%u",
902 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
907 fputs("surf - simple browser\n", stderr);
908 die("usage: surf [-e xid] [-i] [-p] [-s] [-v] [-x] [uri]\n");
912 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
917 zoom(Client *c, const Arg *arg) {
919 if(arg->i < 0) /* zoom out */
920 webkit_web_view_zoom_out(c->view);
921 else if(arg->i > 0) /* zoom in */
922 webkit_web_view_zoom_in(c->view);
925 webkit_web_view_set_zoom_level(c->view, 1.0);
930 main(int argc, char *argv[]) {
935 /* command line args */
936 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-' &&
937 argv[i][1] != '\0' && argv[i][2] == '\0'; i++) {
938 if(!strcmp(argv[i], "--")) {
945 embed = strtol(argv[i], NULL, 0);
962 die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
972 loaduri(clients, &arg);