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>
19 #include <webkit/webkit.h>
20 #include <glib/gstdio.h>
21 #include <JavaScriptCore/JavaScript.h>
30 #define LENGTH(x) (sizeof x / sizeof x[0])
31 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
32 #define COOKIEJAR_TYPE (cookiejar_get_type ())
33 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
35 enum { AtomFind, AtomGo, AtomUri, AtomLast };
37 typedef union Arg Arg;
44 typedef struct Client {
45 GtkWidget *win, *scroll, *vbox, *pane;
47 WebKitWebInspector *inspector;
48 char *title, *linkhover;
49 const char *uri, *needle;
52 gboolean zoomed, fullscreen, isinspecting, sslfailed;
58 void (*func)(Client *c, const Arg *arg);
63 SoupCookieJarText parent_instance;
68 SoupCookieJarTextClass parent_class;
71 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
74 static Atom atoms[AtomLast];
75 static Client *clients = NULL;
76 static GdkNativeWindow embed = 0;
77 static gboolean showxid = FALSE;
78 static char winid[64];
79 static gboolean usingproxy = 0;
80 static char togglestat[5];
81 static char pagestat[3];
83 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
84 WebKitWebResource *r, WebKitNetworkRequest *req,
85 WebKitNetworkResponse *resp, gpointer d);
86 static char *buildpath(const char *path);
87 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
89 static void cleanup(void);
90 static void clipboard(Client *c, const Arg *arg);
92 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
93 SoupCookie *new_cookie);
94 static void cookiejar_finalize(GObject *self);
95 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
96 static void cookiejar_set_property(GObject *self, guint prop_id,
97 const GValue *value, GParamSpec *pspec);
99 static char *copystr(char **str, const char *src);
100 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
102 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
103 WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p,
105 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
106 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
107 WebKitWebPolicyDecision *p, Client *c);
108 static void destroyclient(Client *c);
109 static void destroywin(GtkWidget* w, Client *c);
110 static void die(const char *errstr, ...);
111 static void eval(Client *c, const Arg *arg);
112 static void find(Client *c, const Arg *arg);
113 static void fullscreen(Client *c, const Arg *arg);
114 static const char *getatom(Client *c, int a);
115 static void gettogglestat(Client *c);
116 static void getpagestat(Client *c);
117 static char *geturi(Client *c);
118 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
120 static void inspector(Client *c, const Arg *arg);
121 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
123 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
124 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
125 static void inspector_finished(WebKitWebInspector *i, Client *c);
127 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
128 static void linkhover(WebKitWebView *v, const char* t, const char* l,
130 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
132 static void loaduri(Client *c, const Arg *arg);
133 static void navigate(Client *c, const Arg *arg);
134 static Client *newclient(void);
135 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
136 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
137 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
138 static void popupactivate(GtkMenuItem *menu, Client *);
139 static void print(Client *c, const Arg *arg);
140 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
142 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
143 static void reload(Client *c, const Arg *arg);
144 static void scroll_h(Client *c, const Arg *arg);
145 static void scroll_v(Client *c, const Arg *arg);
146 static void scroll(GtkAdjustment *a, const Arg *arg);
147 static void setatom(Client *c, int a, const char *v);
148 static void setup(void);
149 static void sigchld(int unused);
150 static void source(Client *c, const Arg *arg);
151 static void spawn(Client *c, const Arg *arg);
152 static void stop(Client *c, const Arg *arg);
153 static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
154 const char *title, Client *c);
155 static void toggle(Client *c, const Arg *arg);
156 static void update(Client *c);
157 static void updatewinid(Client *c);
158 static void usage(void);
159 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
160 JSContextRef js, JSObjectRef win, Client *c);
161 static void zoom(Client *c, const Arg *arg);
163 /* configuration, allows nested code to access above variables */
167 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
168 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
170 const gchar *uri = webkit_network_request_get_uri(req);
171 if(g_str_has_suffix(uri, "/favicon.ico"))
172 webkit_network_request_set_uri(req, "about:blank");
176 buildpath(const char *path) {
180 /* creating directory */
182 apath = g_strdup(path);
183 } else if(path[0] == '~') {
185 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
187 apath = g_strconcat(g_get_home_dir(), "/",
191 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
194 if((p = strrchr(apath, '/'))) {
196 g_mkdir_with_parents(apath, 0700);
197 g_chmod(apath, 0700); /* in case it existed */
200 /* creating file (gives error when apath ends with "/") */
201 if((f = fopen(apath, "a"))) {
202 g_chmod(apath, 0600); /* always */
210 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
211 WebKitHitTestResultContext context;
212 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
216 g_object_get(result, "context", &context, NULL);
217 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
219 g_object_get(result, "link-uri", &arg.v, NULL);
220 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
230 destroyclient(clients);
237 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
238 SoupCookie *new_cookie) {
239 flock(COOKIEJAR(self)->lock, LOCK_EX);
240 if(new_cookie && !new_cookie->expires && sessiontime) {
241 soup_cookie_set_expires(new_cookie,
242 soup_date_new_from_now(sessiontime));
244 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
245 old_cookie, new_cookie);
246 flock(COOKIEJAR(self)->lock, LOCK_UN);
250 cookiejar_class_init(CookieJarClass *klass) {
251 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
252 G_OBJECT_CLASS(klass)->get_property =
253 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
254 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
255 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
256 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
260 cookiejar_finalize(GObject *self) {
261 close(COOKIEJAR(self)->lock);
262 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
266 cookiejar_init(CookieJar *self) {
267 self->lock = open(cookiefile, 0);
270 static SoupCookieJar *
271 cookiejar_new(const char *filename, gboolean read_only) {
272 return g_object_new(COOKIEJAR_TYPE,
273 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
274 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
278 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
280 flock(COOKIEJAR(self)->lock, LOCK_SH);
281 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
283 flock(COOKIEJAR(self)->lock, LOCK_UN);
287 evalscript(JSContextRef js, char *script, char* scriptname) {
288 JSStringRef jsscript, jsscriptname;
289 JSValueRef exception = NULL;
291 jsscript = JSStringCreateWithUTF8CString(script);
292 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
293 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
294 JSStringRelease(jsscript);
295 JSStringRelease(jsscriptname);
299 runscript(WebKitWebFrame *frame) {
303 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
304 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
309 clipboard(Client *c, const Arg *arg) {
310 gboolean paste = *(gboolean *)arg;
313 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
315 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
319 copystr(char **str, const char *src) {
330 static WebKitWebView *
331 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
332 Client *n = newclient();
337 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
338 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
339 if(!webkit_web_view_can_show_mime_type(v, m)) {
340 webkit_web_policy_decision_download(p);
347 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
348 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
352 if(webkit_web_navigation_action_get_reason(n) ==
353 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
354 webkit_web_policy_decision_ignore(p);
355 arg.v = (void *)webkit_network_request_get_uri(r);
356 newwindow(NULL, &arg, 0);
363 destroyclient(Client *c) {
366 webkit_web_view_stop_loading(c->view);
367 gtk_widget_destroy(GTK_WIDGET(c->view));
368 gtk_widget_destroy(c->scroll);
369 gtk_widget_destroy(c->vbox);
370 gtk_widget_destroy(c->win);
372 for(p = clients; p && p->next != c; p = p->next);
384 destroywin(GtkWidget* w, Client *c) {
389 die(const char *errstr, ...) {
392 va_start(ap, errstr);
393 vfprintf(stderr, errstr, ap);
399 find(Client *c, const Arg *arg) {
402 s = getatom(c, AtomFind);
403 gboolean forward = *(gboolean *)arg;
404 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
408 fullscreen(Client *c, const Arg *arg) {
410 gtk_window_unfullscreen(GTK_WINDOW(c->win));
412 gtk_window_fullscreen(GTK_WINDOW(c->win));
414 c->fullscreen = !c->fullscreen;
418 getatom(Client *c, int a) {
419 static char buf[BUFSIZ];
422 unsigned long ldummy;
423 unsigned char *p = NULL;
425 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
426 atoms[a], 0L, BUFSIZ, False, XA_STRING,
427 &adummy, &idummy, &ldummy, &ldummy, &p);
429 strncpy(buf, (char *)p, LENGTH(buf)-1);
440 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
446 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
450 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
456 inspector(Client *c, const Arg *arg) {
457 if(c->isinspecting) {
458 webkit_web_inspector_close(c->inspector);
460 webkit_web_inspector_show(c->inspector);
464 static WebKitWebView *
465 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
466 return WEBKIT_WEB_VIEW(webkit_web_view_new());
470 inspector_show(WebKitWebInspector *i, Client *c) {
476 w = webkit_web_inspector_get_web_view(i);
477 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
478 gtk_widget_show(GTK_WIDGET(w));
479 c->isinspecting = true;
485 inspector_close(WebKitWebInspector *i, Client *c) {
491 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
493 gtk_widget_destroy(w);
494 c->isinspecting = false;
500 inspector_finished(WebKitWebInspector *i, Client *c) {
501 g_free(c->inspector);
505 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
507 gboolean processed = FALSE;
510 for(i = 0; i < LENGTH(keys); i++) {
511 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
512 && CLEANMASK(ev->state) == keys[i].mod
514 keys[i].func(c, &(keys[i].arg));
523 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
525 c->linkhover = copystr(&c->linkhover, l);
526 } else if(c->linkhover) {
534 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
535 WebKitWebFrame *frame;
536 WebKitWebDataSource *src;
537 WebKitNetworkRequest *request;
541 switch(webkit_web_view_get_load_status (c->view)) {
542 case WEBKIT_LOAD_COMMITTED:
544 if(strstr(uri, "https://") == uri) {
545 frame = webkit_web_view_get_main_frame(c->view);
546 src = webkit_web_frame_get_data_source(frame);
547 request = webkit_web_data_source_get_request(src);
548 msg = webkit_network_request_get_message(request);
549 c->sslfailed = soup_message_get_flags(msg)
550 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
552 setatom(c, AtomUri, uri);
554 case WEBKIT_LOAD_FINISHED:
564 loaduri(Client *c, const Arg *arg) {
566 const char *uri = (char *)arg->v;
567 Arg a = { .b = FALSE };
569 if(strcmp(uri, "") == 0)
572 /* In case it's a file path. */
574 rp = realpath(uri, NULL);
575 u = g_strdup_printf("file://%s", rp);
578 u = g_strrstr(uri, "://") ? g_strdup(uri)
579 : g_strdup_printf("http://%s", uri);
582 /* prevents endless loop */
583 if(c->uri && strcmp(u, c->uri) == 0) {
586 webkit_web_view_load_uri(c->view, u);
588 c->title = copystr(&c->title, u);
595 navigate(Client *c, const Arg *arg) {
596 int steps = *(int *)arg;
597 webkit_web_view_go_back_or_forward(c->view, steps);
603 WebKitWebSettings *settings;
604 WebKitWebFrame *frame;
605 GdkGeometry hints = { 1, 1 };
608 if(!(c = calloc(1, sizeof(Client))))
609 die("Cannot malloc!\n");
613 c->win = gtk_plug_new(embed);
615 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
617 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
618 * is always correct, so we should still call this function.
619 * But when doing so, we *must* differentiate between a
620 * WM_CLASS and a resource on the window. By convention, the
621 * window class (WM_CLASS) is capped, while the resource is in
622 * lowercase. Both these values come as a pair.
624 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
626 /* TA: 20091214: And set the role here as well -- so that
627 * sessions can pick this up.
629 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
631 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
632 g_signal_connect(G_OBJECT(c->win),
634 G_CALLBACK(destroywin), c);
635 g_signal_connect(G_OBJECT(c->win),
637 G_CALLBACK(keypress), c);
640 c->pane = gtk_vpaned_new();
643 c->vbox = gtk_vbox_new(FALSE, 0);
644 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
646 /* Scrolled Window */
647 c->scroll = gtk_scrolled_window_new(NULL, NULL);
648 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
649 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
652 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
653 g_signal_connect(G_OBJECT(c->view),
655 G_CALLBACK(titlechange), c);
656 g_signal_connect(G_OBJECT(c->view),
657 "hovering-over-link",
658 G_CALLBACK(linkhover), c);
659 g_signal_connect(G_OBJECT(c->view),
661 G_CALLBACK(createwindow), c);
662 g_signal_connect(G_OBJECT(c->view),
663 "new-window-policy-decision-requested",
664 G_CALLBACK(decidewindow), c);
665 g_signal_connect(G_OBJECT(c->view),
666 "mime-type-policy-decision-requested",
667 G_CALLBACK(decidedownload), c);
668 g_signal_connect(G_OBJECT(c->view),
669 "window-object-cleared",
670 G_CALLBACK(windowobjectcleared), c);
671 g_signal_connect(G_OBJECT(c->view),
672 "notify::load-status",
673 G_CALLBACK(loadstatuschange), c);
674 g_signal_connect(G_OBJECT(c->view),
676 G_CALLBACK(progresschange), c);
677 g_signal_connect(G_OBJECT(c->view),
678 "download-requested",
679 G_CALLBACK(initdownload), c);
680 g_signal_connect(G_OBJECT(c->view),
681 "button-release-event",
682 G_CALLBACK(buttonrelease), c);
683 g_signal_connect(G_OBJECT(c->view),
685 G_CALLBACK(populatepopup), c);
686 g_signal_connect(G_OBJECT(c->view),
687 "resource-request-starting",
688 G_CALLBACK(beforerequest), c);
691 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
692 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
693 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
696 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
697 TRUE, 0, GTK_PACK_START);
698 gtk_widget_grab_focus(GTK_WIDGET(c->view));
699 gtk_widget_show(c->pane);
700 gtk_widget_show(c->vbox);
701 gtk_widget_show(c->scroll);
702 gtk_widget_show(GTK_WIDGET(c->view));
703 gtk_widget_show(c->win);
704 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
706 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
707 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
708 webkit_web_view_set_full_content_zoom(c->view, TRUE);
710 frame = webkit_web_view_get_main_frame(c->view);
712 settings = webkit_web_view_get_settings(c->view);
713 if(!(ua = getenv("SURF_USERAGENT")))
715 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
716 uri = g_strconcat("file://", stylefile, NULL);
717 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
718 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
720 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
722 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
724 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
725 enablespatialbrowsing, NULL);
726 g_object_set(G_OBJECT(settings), "enable-developer-extras",
727 enableinspector, NULL);
729 if(enableinspector) {
730 c->inspector = WEBKIT_WEB_INSPECTOR(
731 webkit_web_view_get_inspector(c->view));
732 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
733 G_CALLBACK(inspector_new), c);
734 g_signal_connect(G_OBJECT(c->inspector), "show-window",
735 G_CALLBACK(inspector_show), c);
736 g_signal_connect(G_OBJECT(c->inspector), "close-window",
737 G_CALLBACK(inspector_close), c);
738 g_signal_connect(G_OBJECT(c->inspector), "finished",
739 G_CALLBACK(inspector_finished), c);
740 c->isinspecting = false;
745 setatom(c, AtomFind, "");
746 setatom(c, AtomUri, "about:blank");
748 webkit_web_view_set_transparent(c->view, TRUE);
755 gdk_display_sync(gtk_widget_get_display(c->win));
757 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
759 if (fclose(stdout) != 0) {
760 die("Error closing stdout");
768 newwindow(Client *c, const Arg *arg, gboolean noembed) {
770 const char *cmd[10], *uri;
771 const Arg a = { .v = (void *)cmd };
775 if(embed && !noembed) {
777 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
789 uri = arg->v ? (char *)arg->v : c->linkhover;
797 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
798 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
800 for(GList *l = items; l; l = l->next) {
801 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
808 popupactivate(GtkMenuItem *menu, Client *c) {
810 * context-menu-action-2000 open link
811 * context-menu-action-1 open link in window
812 * context-menu-action-2 download linked file
813 * context-menu-action-3 copy link location
814 * context-menu-action-13 reload
815 * context-menu-action-10 back
816 * context-menu-action-11 forward
817 * context-menu-action-12 stop
822 GtkClipboard *prisel;
824 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
828 name = gtk_action_get_name(a);
829 if(!g_strcmp0(name, "context-menu-action-3")) {
830 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
831 gtk_clipboard_set_text(prisel, c->linkhover, -1);
836 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
837 Arg arg = {.v = text };
839 loaduri((Client *) d, &arg);
843 print(Client *c, const Arg *arg) {
844 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
847 static GdkFilterReturn
848 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
849 Client *c = (Client *)d;
853 if(((XEvent *)e)->type == PropertyNotify) {
854 ev = &((XEvent *)e)->xproperty;
855 if(ev->state == PropertyNewValue) {
856 if(ev->atom == atoms[AtomFind]) {
859 return GDK_FILTER_REMOVE;
861 else if(ev->atom == atoms[AtomGo]) {
862 arg.v = getatom(c, AtomGo);
864 return GDK_FILTER_REMOVE;
868 return GDK_FILTER_CONTINUE;
872 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
873 c->progress = webkit_web_view_get_progress(c->view) * 100;
878 reload(Client *c, const Arg *arg) {
879 gboolean nocache = *(gboolean *)arg;
881 webkit_web_view_reload_bypass_cache(c->view);
883 webkit_web_view_reload(c->view);
887 scroll_h(Client *c, const Arg *arg) {
888 scroll(gtk_scrolled_window_get_hadjustment(
889 GTK_SCROLLED_WINDOW(c->scroll)), arg);
893 scroll_v(Client *c, const Arg *arg) {
894 scroll(gtk_scrolled_window_get_vadjustment(
895 GTK_SCROLLED_WINDOW(c->scroll)), arg);
899 scroll(GtkAdjustment *a, const Arg *arg) {
902 v = gtk_adjustment_get_value(a);
906 v += gtk_adjustment_get_page_increment(a) *
912 v += gtk_adjustment_get_step_increment(a) * arg->i;
916 v = MIN(v, gtk_adjustment_get_upper(a) -
917 gtk_adjustment_get_page_size(a));
918 gtk_adjustment_set_value(a, v);
922 setatom(Client *c, int a, const char *v) {
924 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
925 atoms[a], XA_STRING, 8, PropModeReplace,
926 (unsigned char *)v, strlen(v) + 1);
936 /* clean up any zombies immediately */
938 gtk_init(NULL, NULL);
943 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
944 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
945 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
948 cookiefile = buildpath(cookiefile);
949 scriptfile = buildpath(scriptfile);
950 stylefile = buildpath(stylefile);
952 /* request handler */
953 s = webkit_get_default_session();
956 soup_session_add_feature(s,
957 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
961 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
962 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
965 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
966 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
967 g_strdup_printf("http://%s", proxy);
968 puri = soup_uri_new(new_proxy);
969 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
977 sigchld(int unused) {
978 if(signal(SIGCHLD, sigchld) == SIG_ERR)
979 die("Can't install SIGCHLD handler");
980 while(0 < waitpid(-1, NULL, WNOHANG));
984 source(Client *c, const Arg *arg) {
985 Arg a = { .b = FALSE };
988 s = webkit_web_view_get_view_source_mode(c->view);
989 webkit_web_view_set_view_source_mode(c->view, !s);
994 spawn(Client *c, const Arg *arg) {
997 close(ConnectionNumber(dpy));
999 execvp(((char **)arg->v)[0], (char **)arg->v);
1000 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1007 eval(Client *c, const Arg *arg) {
1008 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1009 evalscript(webkit_web_frame_get_global_context(frame),
1010 ((char **)arg->v)[0], "");
1014 stop(Client *c, const Arg *arg) {
1015 webkit_web_view_stop_loading(c->view);
1019 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1020 c->title = copystr(&c->title, t);
1025 toggle(Client *c, const Arg *arg) {
1026 WebKitWebSettings *settings;
1027 char *name = (char *)arg->v;
1029 Arg a = { .b = FALSE };
1031 settings = webkit_web_view_get_settings(c->view);
1032 g_object_get(G_OBJECT(settings), name, &value, NULL);
1033 g_object_set(G_OBJECT(settings), name, !value, NULL);
1039 gettogglestat(Client *c){
1041 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1043 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1045 togglestat[0] = value? 'C': 'c';
1047 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1048 togglestat[1] = value? 'I': 'i';
1050 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1051 togglestat[2] = value? 'S': 's';
1053 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1054 togglestat[3] = value? 'V': 'v';
1056 togglestat[4] = '\0';
1060 getpagestat(Client *c) {
1061 const char *uri = geturi(c);
1063 if(strstr(uri, "https://") == uri) {
1064 pagestat[0] = c->sslfailed ? 'U' : 'T';
1069 pagestat[1] = usingproxy ? 'P' : '-';
1082 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->linkhover);
1083 } else if(c->progress != 100) {
1084 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress, togglestat,
1085 pagestat, c->title);
1087 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->title);
1090 gtk_window_set_title(GTK_WINDOW(c->win), t);
1095 updatewinid(Client *c) {
1096 snprintf(winid, LENGTH(winid), "%u",
1097 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1102 die("usage: %s [-inpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1103 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1107 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1108 JSObjectRef win, Client *c) {
1113 zoom(Client *c, const Arg *arg) {
1117 webkit_web_view_zoom_out(c->view);
1118 } else if(arg->i > 0) {
1120 webkit_web_view_zoom_in(c->view);
1124 webkit_web_view_set_zoom_level(c->view, 1.0);
1129 main(int argc, char *argv[]) {
1132 memset(&arg, 0, sizeof(arg));
1134 /* command line args */
1137 cookiefile = EARGF(usage());
1140 embed = strtol(EARGF(usage()), NULL, 0);
1146 enableinspector = 0;
1152 scriptfile = EARGF(usage());
1158 stylefile = EARGF(usage());
1161 useragent = EARGF(usage());
1164 die("surf-"VERSION", ©2009-2012 surf engineers, "
1165 "see LICENSE for details\n");
1178 loaduri(clients, &arg);
1183 return EXIT_SUCCESS;