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, *indicator, *pane;
47 WebKitWebInspector *inspector;
48 char *title, *linkhover;
49 const char *uri, *needle;
52 gboolean zoomed, fullscreen, isinspecting, sslfailed;
57 void (*func)(Client *c, const Arg *arg);
64 void (*func)(Client *c, const Arg *arg);
69 SoupCookieJarText parent_instance;
74 SoupCookieJarTextClass parent_class;
77 G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
80 static Atom atoms[AtomLast];
81 static Client *clients = NULL;
82 static GdkNativeWindow embed = 0;
83 static gboolean showxid = FALSE;
84 static char winid[64];
85 static gboolean usingproxy = 0;
86 static char togglestat[5];
88 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
89 WebKitWebResource *r, WebKitNetworkRequest *req,
90 WebKitNetworkResponse *resp, gpointer d);
91 static char *buildpath(const char *path);
92 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
94 static void cleanup(void);
95 static void clipboard(Client *c, const Arg *arg);
97 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
98 SoupCookie *new_cookie);
99 static void cookiejar_finalize(GObject *self);
100 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
101 static void cookiejar_set_property(GObject *self, guint prop_id,
102 const GValue *value, GParamSpec *pspec);
104 static char *copystr(char **str, const char *src);
105 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
107 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
108 WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p,
110 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
111 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
112 WebKitWebPolicyDecision *p, Client *c);
113 static void destroyclient(Client *c);
114 static void destroywin(GtkWidget* w, Client *c);
115 static void die(const char *errstr, ...);
116 static void drawindicator(Client *c);
117 static void eval(Client *c, const Arg *arg);
118 static gboolean exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c);
119 static void find(Client *c, const Arg *arg);
120 static void fullscreen(Client *c, const Arg *arg);
121 static const char *getatom(Client *c, int a);
122 static void gettogglestat(Client *c);
123 static char *geturi(Client *c);
124 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
126 static void inspector(Client *c, const Arg *arg);
127 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
129 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
130 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
131 static void inspector_finished(WebKitWebInspector *i, Client *c);
133 static gboolean keypress(GtkWidget *w, GdkEventKey *ev, Client *c);
134 static void linkhover(WebKitWebView *v, const char* t, const char* l,
136 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
138 static void loaduri(Client *c, const Arg *arg);
139 static void navigate(Client *c, const Arg *arg);
140 static Client *newclient(void);
141 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
142 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
143 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
144 static void popupactivate(GtkMenuItem *menu, Client *);
145 static void print(Client *c, const Arg *arg);
146 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
148 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
149 static void reload(Client *c, const Arg *arg);
150 static void scroll_h(Client *c, const Arg *arg);
151 static void scroll_v(Client *c, const Arg *arg);
152 static void scroll(GtkAdjustment *a, const Arg *arg);
153 static void setatom(Client *c, int a, const char *v);
154 static void setup(void);
155 static void sigchld(int unused);
156 static void source(Client *c, const Arg *arg);
157 static void spawn(Client *c, const Arg *arg);
158 static void stop(Client *c, const Arg *arg);
159 static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
160 const char *title, Client *c);
161 static void toggle(Client *c, const Arg *arg);
162 static void update(Client *c);
163 static void updatewinid(Client *c);
164 static void usage(void);
165 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
166 JSContextRef js, JSObjectRef win, Client *c);
167 static void zoom(Client *c, const Arg *arg);
169 /* configuration, allows nested code to access above variables */
173 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
174 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
176 const gchar *uri = webkit_network_request_get_uri(req);
177 if(g_str_has_suffix(uri, "/favicon.ico"))
178 webkit_network_request_set_uri(req, "about:blank");
182 buildpath(const char *path) {
186 /* creating directory */
188 apath = g_strdup(path);
189 } else if(path[0] == '~') {
191 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
193 apath = g_strconcat(g_get_home_dir(), "/",
197 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
200 if((p = strrchr(apath, '/'))) {
202 g_mkdir_with_parents(apath, 0700);
203 g_chmod(apath, 0700); /* in case it existed */
206 /* creating file (gives error when apath ends with "/") */
207 if((f = fopen(apath, "a"))) {
208 g_chmod(apath, 0600); /* always */
216 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
217 WebKitHitTestResultContext context;
218 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
222 g_object_get(result, "context", &context, NULL);
223 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
225 g_object_get(result, "link-uri", &arg.v, NULL);
226 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
236 destroyclient(clients);
243 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
244 SoupCookie *new_cookie) {
245 flock(COOKIEJAR(self)->lock, LOCK_EX);
246 if(new_cookie && !new_cookie->expires && sessiontime) {
247 soup_cookie_set_expires(new_cookie,
248 soup_date_new_from_now(sessiontime));
250 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
251 old_cookie, new_cookie);
252 flock(COOKIEJAR(self)->lock, LOCK_UN);
256 cookiejar_class_init(CookieJarClass *klass) {
257 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
258 G_OBJECT_CLASS(klass)->get_property =
259 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
260 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
261 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
262 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
266 cookiejar_finalize(GObject *self) {
267 close(COOKIEJAR(self)->lock);
268 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
272 cookiejar_init(CookieJar *self) {
273 self->lock = open(cookiefile, 0);
276 static SoupCookieJar *
277 cookiejar_new(const char *filename, gboolean read_only) {
278 return g_object_new(COOKIEJAR_TYPE,
279 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
280 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
284 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
286 flock(COOKIEJAR(self)->lock, LOCK_SH);
287 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
289 flock(COOKIEJAR(self)->lock, LOCK_UN);
293 evalscript(JSContextRef js, char *script, char* scriptname) {
294 JSStringRef jsscript, jsscriptname;
295 JSValueRef exception = NULL;
297 jsscript = JSStringCreateWithUTF8CString(script);
298 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
299 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js), jsscriptname, 0, &exception);
300 JSStringRelease(jsscript);
301 JSStringRelease(jsscriptname);
305 runscript(WebKitWebFrame *frame) {
309 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
310 evalscript(webkit_web_frame_get_global_context(frame), script, scriptfile);
315 clipboard(Client *c, const Arg *arg) {
316 gboolean paste = *(gboolean *)arg;
319 gtk_clipboard_request_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), pasteuri, c);
321 gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), c->linkhover ? c->linkhover : geturi(c), -1);
325 copystr(char **str, const char *src) {
336 static WebKitWebView *
337 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
338 Client *n = newclient();
343 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
344 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
345 if(!webkit_web_view_can_show_mime_type(v, m)) {
346 webkit_web_policy_decision_download(p);
353 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
354 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
358 if(webkit_web_navigation_action_get_reason(n) ==
359 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
360 webkit_web_policy_decision_ignore(p);
361 arg.v = (void *)webkit_network_request_get_uri(r);
362 newwindow(NULL, &arg, 0);
369 destroyclient(Client *c) {
372 webkit_web_view_stop_loading(c->view);
373 gtk_widget_destroy(c->indicator);
374 gtk_widget_destroy(GTK_WIDGET(c->view));
375 gtk_widget_destroy(c->scroll);
376 gtk_widget_destroy(c->vbox);
377 gtk_widget_destroy(c->win);
379 for(p = clients; p && p->next != c; p = p->next);
391 destroywin(GtkWidget* w, Client *c) {
396 die(const char *errstr, ...) {
399 va_start(ap, errstr);
400 vfprintf(stderr, errstr, ap);
406 drawindicator(Client *c) {
416 width = c->progress * w->allocation.width / 100;
417 gc = gdk_gc_new(w->window);
418 if(strstr(uri, "https://") == uri) {
420 colorname = c->sslfailed? progress_proxy_untrust :
421 progress_proxy_trust;
423 colorname = c->sslfailed? progress_untrust :
428 colorname = progress_proxy;
430 colorname = progress;
434 gdk_color_parse(colorname, &fg);
435 gdk_gc_set_rgb_fg_color(gc, &fg);
436 gdk_draw_rectangle(w->window,
437 w->style->bg_gc[GTK_WIDGET_STATE(w)],
438 TRUE, 0, 0, w->allocation.width, w->allocation.height);
439 gdk_draw_rectangle(w->window, gc, TRUE, 0, 0, width,
440 w->allocation.height);
445 exposeindicator(GtkWidget *w, GdkEventExpose *e, Client *c) {
451 find(Client *c, const Arg *arg) {
454 s = getatom(c, AtomFind);
455 gboolean forward = *(gboolean *)arg;
456 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
460 fullscreen(Client *c, const Arg *arg) {
462 gtk_window_unfullscreen(GTK_WINDOW(c->win));
464 gtk_window_fullscreen(GTK_WINDOW(c->win));
466 c->fullscreen = !c->fullscreen;
470 getatom(Client *c, int a) {
471 static char buf[BUFSIZ];
474 unsigned long ldummy;
475 unsigned char *p = NULL;
477 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
478 atoms[a], 0L, BUFSIZ, False, XA_STRING,
479 &adummy, &idummy, &ldummy, &ldummy, &p);
481 strncpy(buf, (char *)p, LENGTH(buf)-1);
492 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
498 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
502 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
508 inspector(Client *c, const Arg *arg) {
509 if(c->isinspecting) {
510 webkit_web_inspector_close(c->inspector);
512 webkit_web_inspector_show(c->inspector);
516 static WebKitWebView *
517 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
518 return WEBKIT_WEB_VIEW(webkit_web_view_new());
522 inspector_show(WebKitWebInspector *i, Client *c) {
528 w = webkit_web_inspector_get_web_view(i);
529 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
530 gtk_widget_show(GTK_WIDGET(w));
531 c->isinspecting = true;
537 inspector_close(WebKitWebInspector *i, Client *c) {
543 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
545 gtk_widget_destroy(w);
546 c->isinspecting = false;
552 inspector_finished(WebKitWebInspector *i, Client *c) {
553 g_free(c->inspector);
557 keypress(GtkWidget* w, GdkEventKey *ev, Client *c) {
559 gboolean processed = FALSE;
562 for(i = 0; i < LENGTH(keys); i++) {
563 if(gdk_keyval_to_lower(ev->keyval) == keys[i].keyval
564 && CLEANMASK(ev->state) == keys[i].mod
566 keys[i].func(c, &(keys[i].arg));
575 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
577 c->linkhover = copystr(&c->linkhover, l);
578 } else if(c->linkhover) {
586 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
587 WebKitWebFrame *frame;
588 WebKitWebDataSource *src;
589 WebKitNetworkRequest *request;
593 switch(webkit_web_view_get_load_status (c->view)) {
594 case WEBKIT_LOAD_COMMITTED:
596 if(strstr(uri, "https://") == uri) {
597 frame = webkit_web_view_get_main_frame(c->view);
598 src = webkit_web_frame_get_data_source(frame);
599 request = webkit_web_data_source_get_request(src);
600 msg = webkit_network_request_get_message(request);
601 c->sslfailed = soup_message_get_flags(msg)
602 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
604 setatom(c, AtomUri, uri);
606 case WEBKIT_LOAD_FINISHED:
616 loaduri(Client *c, const Arg *arg) {
618 const char *uri = (char *)arg->v;
619 Arg a = { .b = FALSE };
621 if(strcmp(uri, "") == 0)
624 /* In case it's a file path. */
626 rp = realpath(uri, NULL);
627 u = g_strdup_printf("file://%s", rp);
630 u = g_strrstr(uri, "://") ? g_strdup(uri)
631 : g_strdup_printf("http://%s", uri);
634 /* prevents endless loop */
635 if(c->uri && strcmp(u, c->uri) == 0) {
638 webkit_web_view_load_uri(c->view, u);
640 c->title = copystr(&c->title, u);
647 navigate(Client *c, const Arg *arg) {
648 int steps = *(int *)arg;
649 webkit_web_view_go_back_or_forward(c->view, steps);
655 WebKitWebSettings *settings;
656 WebKitWebFrame *frame;
657 GdkGeometry hints = { 1, 1 };
660 if(!(c = calloc(1, sizeof(Client))))
661 die("Cannot malloc!\n");
665 c->win = gtk_plug_new(embed);
667 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
669 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
670 * is always correct, so we should still call this function.
671 * But when doing so, we *must* differentiate between a
672 * WM_CLASS and a resource on the window. By convention, the
673 * window class (WM_CLASS) is capped, while the resource is in
674 * lowercase. Both these values come as a pair.
676 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
678 /* TA: 20091214: And set the role here as well -- so that
679 * sessions can pick this up.
681 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
683 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
684 g_signal_connect(G_OBJECT(c->win),
686 G_CALLBACK(destroywin), c);
687 g_signal_connect(G_OBJECT(c->win),
689 G_CALLBACK(keypress), c);
692 c->pane = gtk_vpaned_new();
695 c->vbox = gtk_vbox_new(FALSE, 0);
696 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
698 /* Scrolled Window */
699 c->scroll = gtk_scrolled_window_new(NULL, NULL);
700 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
701 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
704 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
705 g_signal_connect(G_OBJECT(c->view),
707 G_CALLBACK(titlechange), c);
708 g_signal_connect(G_OBJECT(c->view),
709 "hovering-over-link",
710 G_CALLBACK(linkhover), c);
711 g_signal_connect(G_OBJECT(c->view),
713 G_CALLBACK(createwindow), c);
714 g_signal_connect(G_OBJECT(c->view),
715 "new-window-policy-decision-requested",
716 G_CALLBACK(decidewindow), c);
717 g_signal_connect(G_OBJECT(c->view),
718 "mime-type-policy-decision-requested",
719 G_CALLBACK(decidedownload), c);
720 g_signal_connect(G_OBJECT(c->view),
721 "window-object-cleared",
722 G_CALLBACK(windowobjectcleared), c);
723 g_signal_connect(G_OBJECT(c->view),
724 "notify::load-status",
725 G_CALLBACK(loadstatuschange), c);
726 g_signal_connect(G_OBJECT(c->view),
728 G_CALLBACK(progresschange), c);
729 g_signal_connect(G_OBJECT(c->view),
730 "download-requested",
731 G_CALLBACK(initdownload), c);
732 g_signal_connect(G_OBJECT(c->view),
733 "button-release-event",
734 G_CALLBACK(buttonrelease), c);
735 g_signal_connect(G_OBJECT(c->view),
737 G_CALLBACK(populatepopup), c);
738 g_signal_connect(G_OBJECT(c->view),
739 "resource-request-starting",
740 G_CALLBACK(beforerequest), c);
743 c->indicator = gtk_drawing_area_new();
744 gtk_widget_set_size_request(c->indicator, 0, indicator_thickness);
745 g_signal_connect (G_OBJECT (c->indicator), "expose_event",
746 G_CALLBACK (exposeindicator), c);
749 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
750 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
751 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
752 gtk_container_add(GTK_CONTAINER(c->vbox), c->indicator);
755 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->indicator, FALSE,
756 FALSE, 0, GTK_PACK_START);
757 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
758 TRUE, 0, GTK_PACK_START);
759 gtk_widget_grab_focus(GTK_WIDGET(c->view));
760 gtk_widget_show(c->pane);
761 gtk_widget_show(c->vbox);
762 gtk_widget_show(c->scroll);
763 gtk_widget_show(GTK_WIDGET(c->view));
764 gtk_widget_show(c->win);
765 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
767 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
768 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
769 webkit_web_view_set_full_content_zoom(c->view, TRUE);
771 frame = webkit_web_view_get_main_frame(c->view);
773 settings = webkit_web_view_get_settings(c->view);
774 if(!(ua = getenv("SURF_USERAGENT")))
776 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
777 uri = g_strconcat("file://", stylefile, NULL);
778 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
779 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
781 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
783 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
785 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
786 enablespatialbrowsing, NULL);
787 g_object_set(G_OBJECT(settings), "enable-developer-extras",
788 enableinspector, NULL);
790 if(enableinspector) {
791 c->inspector = WEBKIT_WEB_INSPECTOR(
792 webkit_web_view_get_inspector(c->view));
793 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
794 G_CALLBACK(inspector_new), c);
795 g_signal_connect(G_OBJECT(c->inspector), "show-window",
796 G_CALLBACK(inspector_show), c);
797 g_signal_connect(G_OBJECT(c->inspector), "close-window",
798 G_CALLBACK(inspector_close), c);
799 g_signal_connect(G_OBJECT(c->inspector), "finished",
800 G_CALLBACK(inspector_finished), c);
801 c->isinspecting = false;
806 setatom(c, AtomFind, "");
807 setatom(c, AtomUri, "about:blank");
809 webkit_web_view_set_transparent(c->view, TRUE);
816 gdk_display_sync(gtk_widget_get_display(c->win));
818 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
820 if (fclose(stdout) != 0) {
821 die("Error closing stdout");
829 newwindow(Client *c, const Arg *arg, gboolean noembed) {
831 const char *cmd[10], *uri;
832 const Arg a = { .v = (void *)cmd };
836 if(embed && !noembed) {
838 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
850 uri = arg->v ? (char *)arg->v : c->linkhover;
858 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
859 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
861 for(GList *l = items; l; l = l->next) {
862 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
869 popupactivate(GtkMenuItem *menu, Client *c) {
871 * context-menu-action-2000 open link
872 * context-menu-action-1 open link in window
873 * context-menu-action-2 download linked file
874 * context-menu-action-3 copy link location
875 * context-menu-action-13 reload
876 * context-menu-action-10 back
877 * context-menu-action-11 forward
878 * context-menu-action-12 stop
883 GtkClipboard *prisel;
885 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
889 name = gtk_action_get_name(a);
890 if(!g_strcmp0(name, "context-menu-action-3")) {
891 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
892 gtk_clipboard_set_text(prisel, c->linkhover, -1);
897 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
898 Arg arg = {.v = text };
900 loaduri((Client *) d, &arg);
904 print(Client *c, const Arg *arg) {
905 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
908 static GdkFilterReturn
909 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
910 Client *c = (Client *)d;
914 if(((XEvent *)e)->type == PropertyNotify) {
915 ev = &((XEvent *)e)->xproperty;
916 if(ev->state == PropertyNewValue) {
917 if(ev->atom == atoms[AtomFind]) {
920 return GDK_FILTER_REMOVE;
922 else if(ev->atom == atoms[AtomGo]) {
923 arg.v = getatom(c, AtomGo);
925 return GDK_FILTER_REMOVE;
929 return GDK_FILTER_CONTINUE;
933 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
934 c->progress = webkit_web_view_get_progress(c->view) * 100;
939 reload(Client *c, const Arg *arg) {
940 gboolean nocache = *(gboolean *)arg;
942 webkit_web_view_reload_bypass_cache(c->view);
944 webkit_web_view_reload(c->view);
948 scroll_h(Client *c, const Arg *arg) {
949 scroll(gtk_scrolled_window_get_hadjustment(
950 GTK_SCROLLED_WINDOW(c->scroll)), arg);
954 scroll_v(Client *c, const Arg *arg) {
955 scroll(gtk_scrolled_window_get_vadjustment(
956 GTK_SCROLLED_WINDOW(c->scroll)), arg);
960 scroll(GtkAdjustment *a, const Arg *arg) {
963 v = gtk_adjustment_get_value(a);
967 v += gtk_adjustment_get_page_increment(a) *
973 v += gtk_adjustment_get_step_increment(a) * arg->i;
977 v = MIN(v, gtk_adjustment_get_upper(a) -
978 gtk_adjustment_get_page_size(a));
979 gtk_adjustment_set_value(a, v);
983 setatom(Client *c, int a, const char *v) {
985 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
986 atoms[a], XA_STRING, 8, PropModeReplace,
987 (unsigned char *)v, strlen(v) + 1);
997 /* clean up any zombies immediately */
999 gtk_init(NULL, NULL);
1001 dpy = GDK_DISPLAY();
1004 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1005 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1006 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1008 /* dirs and files */
1009 cookiefile = buildpath(cookiefile);
1010 scriptfile = buildpath(scriptfile);
1011 stylefile = buildpath(stylefile);
1013 /* request handler */
1014 s = webkit_get_default_session();
1017 soup_session_add_feature(s,
1018 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1022 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
1023 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1026 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1027 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1028 g_strdup_printf("http://%s", proxy);
1029 puri = soup_uri_new(new_proxy);
1030 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1031 soup_uri_free(puri);
1038 sigchld(int unused) {
1039 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1040 die("Can't install SIGCHLD handler");
1041 while(0 < waitpid(-1, NULL, WNOHANG));
1045 source(Client *c, const Arg *arg) {
1046 Arg a = { .b = FALSE };
1049 s = webkit_web_view_get_view_source_mode(c->view);
1050 webkit_web_view_set_view_source_mode(c->view, !s);
1055 spawn(Client *c, const Arg *arg) {
1058 close(ConnectionNumber(dpy));
1060 execvp(((char **)arg->v)[0], (char **)arg->v);
1061 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1068 eval(Client *c, const Arg *arg) {
1069 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1070 evalscript(webkit_web_frame_get_global_context(frame),
1071 ((char **)arg->v)[0], "");
1075 stop(Client *c, const Arg *arg) {
1076 webkit_web_view_stop_loading(c->view);
1080 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1081 c->title = copystr(&c->title, t);
1086 toggle(Client *c, const Arg *arg) {
1087 WebKitWebSettings *settings;
1088 char *name = (char *)arg->v;
1090 Arg a = { .b = FALSE };
1092 settings = webkit_web_view_get_settings(c->view);
1093 g_object_get(G_OBJECT(settings), name, &value, NULL);
1094 g_object_set(G_OBJECT(settings), name, !value, NULL);
1100 gettogglestat(Client *c){
1102 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1104 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1106 togglestat[0] = value? 'C': 'c';
1108 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1109 togglestat[1] = value? 'I': 'i';
1111 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1112 togglestat[2] = value? 'S': 's';
1114 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1115 togglestat[3] = value? 'V': 'v';
1117 togglestat[4] = '\0';
1128 t = g_strdup_printf("%s| %s", togglestat, c->linkhover);
1129 } else if(c->progress != 100) {
1131 gtk_widget_show(c->indicator);
1132 t = g_strdup_printf("[%i%%] %s| %s", c->progress, togglestat,
1135 gtk_widget_hide_all(c->indicator);
1136 t = g_strdup_printf("%s| %s", togglestat, c->title);
1139 gtk_window_set_title(GTK_WINDOW(c->win), t);
1144 updatewinid(Client *c) {
1145 snprintf(winid, LENGTH(winid), "%u",
1146 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1151 die("usage: %s [-inpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1152 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1156 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1157 JSObjectRef win, Client *c) {
1162 zoom(Client *c, const Arg *arg) {
1166 webkit_web_view_zoom_out(c->view);
1167 } else if(arg->i > 0) {
1169 webkit_web_view_zoom_in(c->view);
1173 webkit_web_view_set_zoom_level(c->view, 1.0);
1178 main(int argc, char *argv[]) {
1181 memset(&arg, 0, sizeof(arg));
1183 /* command line args */
1186 cookiefile = EARGF(usage());
1189 embed = strtol(EARGF(usage()), NULL, 0);
1195 enableinspector = 0;
1201 scriptfile = EARGF(usage());
1207 stylefile = EARGF(usage());
1210 useragent = EARGF(usage());
1213 die("surf-"VERSION", ©2009-2012 surf engineers, "
1214 "see LICENSE for details\n");
1227 loaduri(clients, &arg);
1232 return EXIT_SUCCESS;