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 COOKIEJAR_TYPE (cookiejar_get_type ())
32 #define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
34 enum { AtomFind, AtomGo, AtomUri, AtomLast };
36 typedef union Arg Arg;
43 typedef struct Client {
44 GtkWidget *win, *scroll, *vbox, *indicator;
46 WebKitWebInspector *inspector;
47 char *title, *linkhover;
48 const char *uri, *needle;
51 gboolean zoomed, fullscreen, isinspector, sslfailed;
56 void (*func)(Client *c, const Arg *arg);
63 void (*func)(Client *c, const Arg *arg);
68 SoupCookieJarText parent_instance;
73 SoupCookieJarTextClass parent_class;
76 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
79 static Atom atoms[AtomLast];
80 static Client *clients = NULL;
81 static GdkNativeWindow embed = 0;
82 static gboolean showxid = FALSE;
83 static char winid[64];
84 static gboolean usingproxy = 0;
85 static char togglestat[5];
87 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
88 WebKitWebResource *r, WebKitNetworkRequest *req,
89 WebKitNetworkResponse *resp, gpointer d);
90 static char *buildpath(const char *path);
91 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
93 static void cleanup(void);
94 static void clipboard(Client *c, const Arg *arg);
96 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
97 SoupCookie *new_cookie);
98 static void cookiejar_finalize(GObject *self);
99 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
100 static void cookiejar_set_property(GObject *self, guint prop_id,
101 const GValue *value, GParamSpec *pspec);
103 static char *copystr(char **str, const char *src);
104 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
106 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
107 WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p,
109 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
110 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
111 WebKitWebPolicyDecision *p, Client *c);
112 static void destroyclient(Client *c);
113 static void destroywin(GtkWidget* w, Client *c);
114 static void die(const char *errstr, ...);
115 static void drawindicator(Client *c);
116 static void eval(Client *c, const Arg *arg);
117 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
118 static void find(Client *c, const Arg *arg);
119 static void fullscreen(Client *c, const Arg *arg);
120 static const char *getatom(Client *c, int a);
121 static void gettogglestat(Client *c);
122 static char *geturi(Client *c);
123 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
125 static void inspector(Client *c, const Arg *arg);
126 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
128 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
129 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
130 static void inspector_finished(WebKitWebInspector *i, Client *c);
132 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
133 static void linkhover(WebKitWebView *v, const char* t, const char* l,
135 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
137 static void loaduri(Client *c, const Arg *arg);
138 static void navigate(Client *c, const Arg *arg);
139 static Client *newclient(void);
140 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
141 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
142 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
143 static void popupactivate(GtkMenuItem *menu, Client *);
144 static void print(Client *c, const Arg *arg);
145 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
147 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
148 static void reload(Client *c, const Arg *arg);
149 static void scroll_h(Client *c, const Arg *arg);
150 static void scroll_v(Client *c, const Arg *arg);
151 static void scroll(GtkAdjustment *a, const Arg *arg);
152 static void setatom(Client *c, int a, const char *v);
153 static void setup(void);
154 static void sigchld(int unused);
155 static void source(Client *c, const Arg *arg);
156 static void spawn(Client *c, const Arg *arg);
157 static void stop(Client *c, const Arg *arg);
158 static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
159 const char *title, Client *c);
160 static void toggle(Client *c, const Arg *arg);
161 static void update(Client *c);
162 static void updatewinid(Client *c);
163 static void usage(void);
164 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
165 JSContextRef js, JSObjectRef win, Client *c);
166 static void zoom(Client *c, const Arg *arg);
168 /* configuration, allows nested code to access above variables */
172 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
173 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
175 const gchar *uri = webkit_network_request_get_uri(req);
176 if(g_str_has_suffix(uri, "/favicon.ico"))
177 webkit_network_request_set_uri(req, "about:blank");
181 buildpath(const char *path) {
185 /* creating directory */
187 apath = g_strdup(path);
188 } else if(path[0] == '~') {
190 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
192 apath = g_strconcat(g_get_home_dir(), "/",
196 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
199 if((p = strrchr(apath, '/'))) {
201 g_mkdir_with_parents(apath, 0700);
202 g_chmod(apath, 0700); /* in case it existed */
205 /* creating file (gives error when apath ends with "/") */
206 if((f = fopen(apath, "a"))) {
207 g_chmod(apath, 0600); /* always */
215 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
216 WebKitHitTestResultContext context;
217 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
221 g_object_get(result, "context", &context, NULL);
222 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
224 g_object_get(result, "link-uri", &arg.v, NULL);
225 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
235 destroyclient(clients);
242 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
243 SoupCookie *new_cookie) {
244 flock(COOKIEJAR(self)->lock, LOCK_EX);
245 if(new_cookie && !new_cookie->expires && sessiontime) {
246 soup_cookie_set_expires(new_cookie,
247 soup_date_new_from_now(sessiontime));
249 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
250 old_cookie, new_cookie);
251 flock(COOKIEJAR(self)->lock, LOCK_UN);
255 cookiejar_class_init(CookieJarClass *klass) {
256 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
257 G_OBJECT_CLASS(klass)->get_property =
258 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
259 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
260 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
261 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
265 cookiejar_finalize(GObject *self) {
266 close(COOKIEJAR(self)->lock);
267 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
271 cookiejar_init(CookieJar *self) {
272 self->lock = open(cookiefile, 0);
275 static SoupCookieJar *
276 cookiejar_new(const char *filename, gboolean read_only) {
277 return g_object_new(COOKIEJAR_TYPE,
278 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
279 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
283 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
285 flock(COOKIEJAR(self)->lock, LOCK_SH);
286 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
288 flock(COOKIEJAR(self)->lock, LOCK_UN);
292 evalscript(JSContextRef js, char *script, char* scriptname) {
293 JSStringRef jsscript, jsscriptname;
294 JSValueRef exception = NULL;
296 jsscript = JSStringCreateWithUTF8CString(script);
297 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
298 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
299 JSStringRelease(jsscript);
300 JSStringRelease(jsscriptname);
304 runscript(WebKitWebFrame *frame) {
308 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
309 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
314 clipboard(Client *c, const Arg *arg) {
315 gboolean paste = *(gboolean *)arg;
318 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
320 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
324 copystr(char **str, const char *src) {
335 static WebKitWebView *
336 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
337 Client *n = newclient();
342 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
343 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
344 if(!webkit_web_view_can_show_mime_type(v, m)) {
345 webkit_web_policy_decision_download(p);
352 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
353 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
357 if(webkit_web_navigation_action_get_reason(n) ==
358 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
359 webkit_web_policy_decision_ignore(p);
360 arg.v = (void *)webkit_network_request_get_uri(r);
361 newwindow(NULL, &arg, 0);
368 destroyclient(Client *c) {
371 webkit_web_view_stop_loading(c->view);
372 gtk_widget_destroy(c->indicator);
373 gtk_widget_destroy(GTK_WIDGET(c->view));
374 gtk_widget_destroy(c->scroll);
375 gtk_widget_destroy(c->vbox);
376 gtk_widget_destroy(c->win);
378 for(p = clients; p && p->next != c; p = p->next);
390 destroywin(GtkWidget* w, Client *c) {
395 die(const char *errstr, ...) {
398 va_start(ap, errstr);
399 vfprintf(stderr, errstr, ap);
405 drawindicator(Client *c) {
415 width = c->progress * w->allocation.width / 100;
416 gc = gdk_gc_new(w->window);
417 if(strstr(uri, "https://") == uri) {
419 colorname = c->sslfailed? progress_proxy_untrust :
420 progress_proxy_trust;
422 colorname = c->sslfailed? progress_untrust :
427 colorname = progress_proxy;
429 colorname = progress;
433 gdk_color_parse(colorname, &fg);
434 gdk_gc_set_rgb_fg_color(gc, &fg);
435 gdk_draw_rectangle(w->window,
436 w->style->bg_gc[GTK_WIDGET_STATE(w)],
437 TRUE, 0, 0, w->allocation.width, w->allocation.height);
438 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
439 w->allocation.height);
444 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
450 find(Client *c, const Arg *arg) {
453 s = getatom(c, AtomFind);
454 gboolean forward = *(gboolean *)arg;
455 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
459 fullscreen(Client *c, const Arg *arg) {
461 gtk_window_unfullscreen(GTK_WINDOW(c->win));
463 gtk_window_fullscreen(GTK_WINDOW(c->win));
465 c->fullscreen = !c->fullscreen;
469 getatom(Client *c, int a) {
470 static char buf[BUFSIZ];
473 unsigned long ldummy;
474 unsigned char *p = NULL;
476 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
477 atoms[a], 0L, BUFSIZ, False, XA_STRING,
478 &adummy, &idummy, &ldummy, &ldummy, &p);
480 strncpy(buf, (char *)p, LENGTH(buf)-1);
491 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
497 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
501 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
507 inspector(Client *c, const Arg *arg) {
510 webkit_web_inspector_show(c->inspector);
513 static WebKitWebView *
514 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
515 Client *n = newclient();
516 n->isinspector = true;
522 inspector_show(WebKitWebInspector *i, Client *c) {
523 gtk_widget_show(GTK_WIDGET(webkit_web_inspector_get_web_view(i)));
528 inspector_close(WebKitWebInspector *i, Client *c) {
529 gtk_widget_hide(GTK_WIDGET(webkit_web_inspector_get_web_view(i)));
534 inspector_finished(WebKitWebInspector *i, Client *c) {
535 g_free(c->inspector);
539 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
541 gboolean processed = FALSE;
544 for(i = 0; i < LENGTH(keys); i++) {
545 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
546 && (ev->state & keys[i].mod) == keys[i].mod
548 keys[i].func(c, &(keys[i].arg));
557 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
559 c->linkhover = copystr(&c->linkhover, l);
560 } else if(c->linkhover) {
568 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
569 WebKitWebFrame *frame;
570 WebKitWebDataSource *src;
571 WebKitNetworkRequest *request;
575 switch(webkit_web_view_get_load_status (c->view)) {
576 case WEBKIT_LOAD_COMMITTED:
578 if(strstr(uri, "https://") == uri) {
579 frame = webkit_web_view_get_main_frame(c->view);
580 src = webkit_web_frame_get_data_source(frame);
581 request = webkit_web_data_source_get_request(src);
582 msg = webkit_network_request_get_message(request);
583 c->sslfailed = soup_message_get_flags(msg)
584 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
586 setatom(c, AtomUri, uri);
588 case WEBKIT_LOAD_FINISHED:
598 loaduri(Client *c, const Arg *arg) {
600 const char *uri = (char *)arg->v;
601 Arg a = { .b = FALSE };
603 if(strcmp(uri, "") == 0)
606 /* In case it's a file path. */
608 rp = realpath(uri, NULL);
609 u = g_strdup_printf("file://%s", rp);
612 u = g_strrstr(uri, "://") ? g_strdup(uri)
613 : g_strdup_printf("http://%s", uri);
616 /* prevents endless loop */
617 if(c->uri && strcmp(u, c->uri) == 0) {
620 webkit_web_view_load_uri(c->view, u);
622 c->title = copystr(&c->title, u);
629 navigate(Client *c, const Arg *arg) {
630 int steps = *(int *)arg;
631 webkit_web_view_go_back_or_forward(c->view, steps);
637 WebKitWebSettings *settings;
638 WebKitWebFrame *frame;
639 GdkGeometry hints = { 1, 1 };
642 if(!(c = calloc(1, sizeof(Client))))
643 die("Cannot malloc!\n");
647 c->win = gtk_plug_new(embed);
649 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
651 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
652 * is always correct, so we should still call this function.
653 * But when doing so, we *must* differentiate between a
654 * WM_CLASS and a resource on the window. By convention, the
655 * window class (WM_CLASS) is capped, while the resource is in
656 * lowercase. Both these values come as a pair.
658 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
660 /* TA: 20091214: And set the role here as well -- so that
661 * sessions can pick this up.
663 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
665 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
666 g_signal_connect(G_OBJECT(c->win),
668 G_CALLBACK(destroywin), c);
669 g_signal_connect(G_OBJECT(c->win),
671 G_CALLBACK(keypress), c);
674 c->vbox = gtk_vbox_new(FALSE, 0);
676 /* Scrolled Window */
677 c->scroll = gtk_scrolled_window_new(NULL, NULL);
678 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
679 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
682 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
683 g_signal_connect(G_OBJECT(c->view),
685 G_CALLBACK(titlechange), c);
686 g_signal_connect(G_OBJECT(c->view),
687 "hovering-over-link",
688 G_CALLBACK(linkhover), c);
689 g_signal_connect(G_OBJECT(c->view),
691 G_CALLBACK(createwindow), c);
692 g_signal_connect(G_OBJECT(c->view),
693 "new-window-policy-decision-requested",
694 G_CALLBACK(decidewindow), c);
695 g_signal_connect(G_OBJECT(c->view),
696 "mime-type-policy-decision-requested",
697 G_CALLBACK(decidedownload), c);
698 g_signal_connect(G_OBJECT(c->view),
699 "window-object-cleared",
700 G_CALLBACK(windowobjectcleared), c);
701 g_signal_connect(G_OBJECT(c->view),
702 "notify::load-status",
703 G_CALLBACK(loadstatuschange), c);
704 g_signal_connect(G_OBJECT(c->view),
706 G_CALLBACK(progresschange), c);
707 g_signal_connect(G_OBJECT(c->view),
708 "download-requested",
709 G_CALLBACK(initdownload), c);
710 g_signal_connect(G_OBJECT(c->view),
711 "button-release-event",
712 G_CALLBACK(buttonrelease), c);
713 g_signal_connect(G_OBJECT(c->view),
715 G_CALLBACK(populatepopup), c);
716 g_signal_connect(G_OBJECT(c->view),
717 "resource-request-starting",
718 G_CALLBACK(beforerequest), c);
721 c->indicator = gtk_drawing_area_new();
722 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
723 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
724 G_CALLBACK (exposeindicator), c);
727 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
728 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
729 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
730 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
733 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE,
734 FALSE, 0, GTK_PACK_START);
735 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
736 TRUE, 0, GTK_PACK_START);
737 gtk_widget_grab_focus(GTK_WIDGET(c->view));
738 gtk_widget_show(c->vbox);
739 gtk_widget_show(c->scroll);
740 gtk_widget_show(GTK_WIDGET(c->view));
741 gtk_widget_show(c->win);
742 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
744 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
745 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
746 webkit_web_view_set_full_content_zoom(c->view, TRUE);
748 frame = webkit_web_view_get_main_frame(c->view);
750 settings = webkit_web_view_get_settings(c->view);
751 if(!(ua = getenv("SURF_USERAGENT")))
753 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
754 uri = g_strconcat("file://", stylefile, NULL);
755 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
756 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
758 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
760 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
762 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
763 enablespatialbrowsing, NULL);
764 g_object_set(G_OBJECT(settings), "enable-developer-extras",
765 enableinspector, NULL);
767 if(enableinspector) {
768 c->inspector = WEBKIT_WEB_INSPECTOR(
769 webkit_web_view_get_inspector(c->view));
770 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
771 G_CALLBACK(inspector_new), c);
772 g_signal_connect(G_OBJECT(c->inspector), "show-window",
773 G_CALLBACK(inspector_show), c);
774 g_signal_connect(G_OBJECT(c->inspector), "close-window",
775 G_CALLBACK(inspector_close), c);
776 g_signal_connect(G_OBJECT(c->inspector), "finished",
777 G_CALLBACK(inspector_finished), c);
778 c->isinspector = false;
783 setatom(c, AtomFind, "");
784 setatom(c, AtomUri, "about:blank");
786 webkit_web_view_set_transparent(c->view, TRUE);
792 gdk_display_sync(gtk_widget_get_display(c->win));
794 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
796 if (fclose(stdout) != 0) {
797 die("Error closing stdout");
804 newwindow(Client *c, const Arg *arg, gboolean noembed) {
806 const char *cmd[10], *uri;
807 const Arg a = { .v = (void *)cmd };
811 if(embed && !noembed) {
813 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
825 uri = arg->v ? (char *)arg->v : c->linkhover;
833 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
834 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
836 for(GList *l = items; l; l = l->next) {
837 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
844 popupactivate(GtkMenuItem *menu, Client *c) {
846 * context-menu-action-2000 open link
847 * context-menu-action-1 open link in window
848 * context-menu-action-2 download linked file
849 * context-menu-action-3 copy link location
850 * context-menu-action-13 reload
851 * context-menu-action-10 back
852 * context-menu-action-11 forward
853 * context-menu-action-12 stop
858 GtkClipboard *prisel;
860 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
864 name = gtk_action_get_name(a);
865 if(!g_strcmp0(name, "context-menu-action-3")) {
866 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
867 gtk_clipboard_set_text(prisel, c->linkhover, -1);
872 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
873 Arg arg = {.v = text };
875 loaduri((Client *) d, &arg);
879 print(Client *c, const Arg *arg) {
880 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
883 static GdkFilterReturn
884 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
885 Client *c = (Client *)d;
889 if(((XEvent *)e)->type == PropertyNotify) {
890 ev = &((XEvent *)e)->xproperty;
891 if(ev->state == PropertyNewValue) {
892 if(ev->atom == atoms[AtomFind]) {
895 return GDK_FILTER_REMOVE;
897 else if(ev->atom == atoms[AtomGo]) {
898 arg.v = getatom(c, AtomGo);
900 return GDK_FILTER_REMOVE;
904 return GDK_FILTER_CONTINUE;
908 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
909 c->progress = webkit_web_view_get_progress(c->view) * 100;
914 reload(Client *c, const Arg *arg) {
915 gboolean nocache = *(gboolean *)arg;
917 webkit_web_view_reload_bypass_cache(c->view);
919 webkit_web_view_reload(c->view);
923 scroll_h(Client *c, const Arg *arg) {
924 scroll(gtk_scrolled_window_get_hadjustment(
925 GTK_SCROLLED_WINDOW(c->scroll)), arg);
929 scroll_v(Client *c, const Arg *arg) {
930 scroll(gtk_scrolled_window_get_vadjustment(
931 GTK_SCROLLED_WINDOW(c->scroll)), arg);
935 scroll(GtkAdjustment *a, const Arg *arg) {
938 v = gtk_adjustment_get_value(a);
942 v += gtk_adjustment_get_page_increment(a) *
948 v += gtk_adjustment_get_step_increment(a) * arg->i;
952 v = MIN(v, gtk_adjustment_get_upper(a) -
953 gtk_adjustment_get_page_size(a));
954 gtk_adjustment_set_value(a, v);
958 setatom(Client *c, int a, const char *v) {
960 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
961 atoms[a], XA_STRING, 8, PropModeReplace,
962 (unsigned char *)v, strlen(v) + 1);
972 /* clean up any zombies immediately */
974 gtk_init(NULL, NULL);
975 if (!g_thread_supported())
981 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
982 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
983 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
986 cookiefile = buildpath(cookiefile);
987 scriptfile = buildpath(scriptfile);
988 stylefile = buildpath(stylefile);
990 /* request handler */
991 s = webkit_get_default_session();
994 soup_session_add_feature(s,
995 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
999 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
1000 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1003 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1004 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1005 g_strdup_printf("http://%s", proxy);
1006 puri = soup_uri_new(new_proxy);
1007 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1008 soup_uri_free(puri);
1015 sigchld(int unused) {
1016 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1017 die("Can't install SIGCHLD handler");
1018 while(0 < waitpid(-1, NULL, WNOHANG));
1022 source(Client *c, const Arg *arg) {
1023 Arg a = { .b = FALSE };
1026 s = webkit_web_view_get_view_source_mode(c->view);
1027 webkit_web_view_set_view_source_mode(c->view, !s);
1032 spawn(Client *c, const Arg *arg) {
1035 close(ConnectionNumber(dpy));
1037 execvp(((char **)arg->v)[0], (char **)arg->v);
1038 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1045 eval(Client *c, const Arg *arg) {
1046 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1047 evalscript(webkit_web_frame_get_global_context(frame),
1048 ((char **)arg->v)[0], "");
1052 stop(Client *c, const Arg *arg) {
1053 webkit_web_view_stop_loading(c->view);
1057 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1058 c->title = copystr(&c->title, t);
1063 toggle(Client *c, const Arg *arg) {
1064 WebKitWebSettings *settings;
1065 char *name = (char *)arg->v;
1067 Arg a = { .b = FALSE };
1069 settings = webkit_web_view_get_settings(c->view);
1070 g_object_get(G_OBJECT(settings), name, &value, NULL);
1071 g_object_set(G_OBJECT(settings), name, !value, NULL);
1077 gettogglestat(Client *c){
1079 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1081 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1083 togglestat[0] = value? 'C': 'c';
1085 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1086 togglestat[1] = value? 'I': 'i';
1088 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1089 togglestat[2] = value? 'S': 's';
1091 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1092 togglestat[3] = value? 'V': 'v';
1094 togglestat[4] = '\0';
1105 t = g_strdup_printf("%s| %s", togglestat, c->linkhover);
1106 } else if(c->progress != 100) {
1108 gtk_widget_show(c->indicator);
1109 t = g_strdup_printf("[%i%%] %s| %s", c->progress, togglestat,
1112 gtk_widget_hide_all(c->indicator);
1113 t = g_strdup_printf("%s| %s", togglestat, c->title);
1116 gtk_window_set_title(GTK_WINDOW(c->win), t);
1121 updatewinid(Client *c) {
1122 snprintf(winid, LENGTH(winid), "%u",
1123 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1128 die("usage: %s [-inpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1129 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1133 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1134 JSObjectRef win, Client *c) {
1139 zoom(Client *c, const Arg *arg) {
1143 webkit_web_view_zoom_out(c->view);
1144 } else if(arg->i > 0) {
1146 webkit_web_view_zoom_in(c->view);
1150 webkit_web_view_set_zoom_level(c->view, 1.0);
1155 main(int argc, char *argv[]) {
1158 memset(&arg, 0, sizeof(arg));
1160 /* command line args */
1163 cookiefile = EARGF(usage());
1166 embed = strtol(EARGF(usage()), NULL, 0);
1172 enableinspector = 0;
1178 scriptfile = EARGF(usage());
1184 stylefile = EARGF(usage());
1187 useragent = EARGF(usage());
1190 die("surf-"VERSION", ©2009-2012 surf engineers, "
1191 "see LICENSE for details\n");
1204 loaduri(clients, &arg);
1209 return EXIT_SUCCESS;