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;
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) {
342 width = c->progress * w->allocation.width / 100;
343 gc = gdk_gc_new(w->window);
344 if(strstr(uri, "https://") == uri)
345 gdk_color_parse(c->sslfailed ?
346 progress_untrust : progress_trust, &fg);
348 gdk_color_parse(progress, &fg);
349 gdk_gc_set_rgb_fg_color(gc, &fg);
350 gdk_draw_rectangle(w->window,
351 w->style->bg_gc[GTK_WIDGET_STATE(w)],
352 TRUE, 0, 0, w->allocation.width, w->allocation.height);
353 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
354 w->allocation.height);
359 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
365 find(Client *c, const Arg *arg) {
368 s = getatom(c, AtomFind);
369 gboolean forward = *(gboolean *)arg;
370 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
374 getatom(Client *c, int a) {
375 static char buf[BUFSIZ];
378 unsigned long ldummy;
379 unsigned char *p = NULL;
381 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
382 atoms[a], 0L, BUFSIZ, False, XA_STRING,
383 &adummy, &idummy, &ldummy, &ldummy, &p);
385 strncpy(buf, (char *)p, LENGTH(buf)-1);
396 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
402 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
406 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o));
412 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
414 gboolean processed = FALSE;
417 for(i = 0; i < LENGTH(keys); i++) {
418 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
419 && (ev->state & keys[i].mod) == keys[i].mod
421 keys[i].func(c, &(keys[i].arg));
429 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
431 c->linkhover = copystr(&c->linkhover, l);
433 else if(c->linkhover) {
441 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
442 WebKitWebFrame *frame;
443 WebKitWebDataSource *src;
444 WebKitNetworkRequest *request;
448 switch(webkit_web_view_get_load_status (c->view)) {
449 case WEBKIT_LOAD_COMMITTED:
451 if(strstr(uri, "https://") == uri) {
452 frame = webkit_web_view_get_main_frame(c->view);
453 src = webkit_web_frame_get_data_source(frame);
454 request = webkit_web_data_source_get_request(src);
455 msg = webkit_network_request_get_message(request);
456 c->sslfailed = soup_message_get_flags(msg)
457 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
459 setatom(c, AtomUri, uri);
461 case WEBKIT_LOAD_FINISHED:
471 loaduri(Client *c, const Arg *arg) {
473 const char *uri = (char *)arg->v;
474 Arg a = { .b = FALSE };
476 if(strcmp(uri, "") == 0)
478 u = g_strrstr(uri, "://") ? g_strdup(uri)
479 : g_strdup_printf("http://%s", uri);
480 /* prevents endless loop */
481 if(c->uri && strcmp(u, c->uri) == 0) {
485 webkit_web_view_load_uri(c->view, u);
487 c->title = copystr(&c->title, u);
494 navigate(Client *c, const Arg *arg) {
495 int steps = *(int *)arg;
496 webkit_web_view_go_back_or_forward(c->view, steps);
502 WebKitWebSettings *settings;
503 WebKitWebFrame *frame;
504 GdkGeometry hints = { 1, 1 };
507 if(!(c = calloc(1, sizeof(Client))))
508 die("Cannot malloc!\n");
511 c->win = gtk_plug_new(embed);
514 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
515 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
516 * is always correct, so we should still call this function.
517 * But when doing so, we *must* differentiate between a
518 * WM_CLASS and a resource on the window. By convention, the
519 * window class (WM_CLASS) is capped, while the resource is in
520 * lowercase. Both these values come as a pair.
522 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
524 /* TA: 20091214: And set the role here as well -- so that
525 * sessions can pick this up.
527 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
529 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
530 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(destroywin), c);
531 g_signal_connect(G_OBJECT(c->win), "key-press-event", G_CALLBACK(keypress), c);
534 c->vbox = gtk_vbox_new(FALSE, 0);
536 /* Scrolled Window */
537 c->scroll = gtk_scrolled_window_new(NULL, NULL);
538 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
539 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
542 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
543 g_signal_connect(G_OBJECT(c->view), "title-changed", G_CALLBACK(titlechange), c);
544 g_signal_connect(G_OBJECT(c->view), "hovering-over-link", G_CALLBACK(linkhover), c);
545 g_signal_connect(G_OBJECT(c->view), "create-web-view", G_CALLBACK(createwindow), c);
546 g_signal_connect(G_OBJECT(c->view), "new-window-policy-decision-requested", G_CALLBACK(decidewindow), c);
547 g_signal_connect(G_OBJECT(c->view), "mime-type-policy-decision-requested", G_CALLBACK(decidedownload), c);
548 g_signal_connect(G_OBJECT(c->view), "window-object-cleared", G_CALLBACK(windowobjectcleared), c);
549 g_signal_connect(G_OBJECT(c->view), "notify::load-status", G_CALLBACK(loadstatuschange), c);
550 g_signal_connect(G_OBJECT(c->view), "notify::progress", G_CALLBACK(progresschange), c);
551 g_signal_connect(G_OBJECT(c->view), "download-requested", G_CALLBACK(initdownload), c);
552 g_signal_connect(G_OBJECT(c->view), "button-release-event", G_CALLBACK(buttonrelease), c);
553 g_signal_connect(G_OBJECT(c->view), "populate-popup", G_CALLBACK(populatepopup), c);
556 c->indicator = gtk_drawing_area_new();
557 gtk_widget_set_size_request(c->indicator, 0, 2);
558 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
559 G_CALLBACK (exposeindicator), c);
562 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
563 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
564 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
565 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
568 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE, FALSE, 0, GTK_PACK_START);
569 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0, GTK_PACK_START);
570 gtk_widget_grab_focus(GTK_WIDGET(c->view));
571 gtk_widget_show(c->vbox);
572 gtk_widget_show(c->indicator);
573 gtk_widget_show(c->scroll);
574 gtk_widget_show(GTK_WIDGET(c->view));
575 gtk_widget_show(c->win);
576 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints, GDK_HINT_MIN_SIZE);
577 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
578 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
579 webkit_web_view_set_full_content_zoom(c->view, TRUE);
580 frame = webkit_web_view_get_main_frame(c->view);
582 settings = webkit_web_view_get_settings(c->view);
583 if(!(ua = getenv("SURF_USERAGENT")))
585 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
586 uri = g_strconcat("file://", stylefile, NULL);
587 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
588 g_object_set(G_OBJECT(settings), "auto-load-images", loadimage, NULL);
589 g_object_set(G_OBJECT(settings), "enable-plugins", plugin, NULL);
590 g_object_set(G_OBJECT(settings), "enable-scripts", script, NULL);
591 g_object_set(G_OBJECT(settings), "enable-spatial-navigation", SPATIAL_BROWSING, NULL);
595 setatom(c, AtomFind, "");
596 setatom(c, AtomUri, "about:blank");
598 webkit_web_view_set_transparent(c->view, TRUE);
604 gdk_display_sync(gtk_widget_get_display(c->win));
605 printf("%u\n", (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
607 if (fclose(stdout) != 0) {
608 die("Error closing stdout");
615 newwindow(Client *c, const Arg *arg, gboolean noembed) {
617 const char *cmd[10], *uri;
618 const Arg a = { .v = (void *)cmd };
622 if(embed && !noembed) {
624 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
636 uri = arg->v ? (char *)arg->v : c->linkhover;
644 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
645 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
647 for(GList *l = items; l; l = l->next) {
648 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
655 popupactivate(GtkMenuItem *menu, Client *c) {
657 * context-menu-action-2000 open link
658 * context-menu-action-1 open link in window
659 * context-menu-action-2 download linked file
660 * context-menu-action-3 copy link location
661 * context-menu-action-13 reload
662 * context-menu-action-10 back
663 * context-menu-action-11 forward
664 * context-menu-action-12 stop
669 GtkClipboard *prisel;
671 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
675 name = gtk_action_get_name(a);
676 if(!g_strcmp0(name, "context-menu-action-3")) {
677 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
678 gtk_clipboard_set_text(prisel, c->linkhover, -1);
683 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
684 Arg arg = {.v = text };
686 loaduri((Client *) d, &arg);
690 print(Client *c, const Arg *arg) {
691 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
695 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
696 Client *c = (Client *)d;
700 if(((XEvent *)e)->type == PropertyNotify) {
701 ev = &((XEvent *)e)->xproperty;
702 if(ev->state == PropertyNewValue) {
703 if(ev->atom == atoms[AtomFind]) {
706 return GDK_FILTER_REMOVE;
708 else if(ev->atom == atoms[AtomGo]) {
709 arg.v = getatom(c, AtomGo);
711 return GDK_FILTER_REMOVE;
715 return GDK_FILTER_CONTINUE;
719 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
720 c->progress = webkit_web_view_get_progress(c->view) * 100;
725 reload(Client *c, const Arg *arg) {
726 gboolean nocache = *(gboolean *)arg;
728 webkit_web_view_reload_bypass_cache(c->view);
730 webkit_web_view_reload(c->view);
734 scroll_h(Client *c, const Arg *arg) {
735 scroll(gtk_scrolled_window_get_hadjustment(
736 GTK_SCROLLED_WINDOW(c->scroll)), arg);
740 scroll_v(Client *c, const Arg *arg) {
741 scroll(gtk_scrolled_window_get_vadjustment(
742 GTK_SCROLLED_WINDOW(c->scroll)), arg);
746 scroll(GtkAdjustment *a, const Arg *arg) {
749 v = gtk_adjustment_get_value(a);
753 v += gtk_adjustment_get_page_increment(a) *
759 v += gtk_adjustment_get_step_increment(a) * arg->i;
763 v = MIN(v, gtk_adjustment_get_upper(a) -
764 gtk_adjustment_get_page_size(a));
765 gtk_adjustment_set_value(a, v);
769 setatom(Client *c, int a, const char *v) {
771 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window), atoms[a],
772 XA_STRING, 8, PropModeReplace, (unsigned char *)v,
783 /* clean up any zombies immediately */
785 gtk_init(NULL, NULL);
786 if (!g_thread_supported())
792 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
793 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
794 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
797 cookiefile = buildpath(cookiefile);
798 scriptfile = buildpath(scriptfile);
799 stylefile = buildpath(stylefile);
801 /* request handler */
802 s = webkit_get_default_session();
805 soup_session_add_feature(s, SOUP_SESSION_FEATURE(cookiejar_new(cookiefile, FALSE)));
808 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
809 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
812 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
813 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
814 g_strdup_printf("http://%s", proxy);
815 puri = soup_uri_new(new_proxy);
816 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
823 sigchld(int unused) {
824 if(signal(SIGCHLD, sigchld) == SIG_ERR)
825 die("Can't install SIGCHLD handler");
826 while(0 < waitpid(-1, NULL, WNOHANG));
830 source(Client *c, const Arg *arg) {
831 Arg a = { .b = FALSE };
834 s = webkit_web_view_get_view_source_mode(c->view);
835 webkit_web_view_set_view_source_mode(c->view, !s);
840 spawn(Client *c, const Arg *arg) {
843 close(ConnectionNumber(dpy));
845 execvp(((char **)arg->v)[0], (char **)arg->v);
846 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
853 eval(Client *c, const Arg *arg) {
854 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
855 evalscript(webkit_web_frame_get_global_context(frame),
856 ((char **)arg->v)[0], "");
860 stop(Client *c, const Arg *arg) {
861 webkit_web_view_stop_loading(c->view);
865 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
866 c->title = copystr(&c->title, t);
875 t = g_strdup(c->linkhover);
876 else if(c->progress != 100)
877 t = g_strdup_printf("[%i%%] %s", c->progress, c->title);
879 t = g_strdup(c->title);
881 gtk_window_set_title(GTK_WINDOW(c->win), t);
886 updatewinid(Client *c) {
887 snprintf(winid, LENGTH(winid), "%u",
888 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
893 fputs("surf - simple browser\n", stderr);
894 die("usage: surf [-e xid] [-i] [-p] [-s] [-v] [-x] [uri]\n");
898 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js, JSObjectRef win, Client *c) {
903 zoom(Client *c, const Arg *arg) {
905 if(arg->i < 0) /* zoom out */
906 webkit_web_view_zoom_out(c->view);
907 else if(arg->i > 0) /* zoom in */
908 webkit_web_view_zoom_in(c->view);
911 webkit_web_view_set_zoom_level(c->view, 1.0);
916 main(int argc, char *argv[]) {
921 /* command line args */
922 for(i = 1, arg.v = NULL; i < argc && argv[i][0] == '-' &&
923 argv[i][1] != '\0' && argv[i][2] == '\0'; i++) {
924 if(!strcmp(argv[i], "--")) {
931 embed = strtol(argv[i], NULL, 0);
948 die("surf-"VERSION", ©2009-2012 surf engineers, see LICENSE for details\n");
958 loaduri(clients, &arg);