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[6];
81 static char pagestat[3];
83 static void add_accels(Client *c);
84 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
85 WebKitWebResource *r, WebKitNetworkRequest *req,
86 WebKitNetworkResponse *resp, gpointer d);
87 static char *buildpath(const char *path);
88 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e,
90 static void cleanup(void);
91 static void clipboard(Client *c, const Arg *arg);
93 static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
94 SoupCookie *new_cookie);
95 static void cookiejar_finalize(GObject *self);
96 static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only);
97 static void cookiejar_set_property(GObject *self, guint prop_id,
98 const GValue *value, GParamSpec *pspec);
100 static char *copystr(char **str, const char *src);
101 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
103 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
104 WebKitNetworkRequest *r, gchar *m, WebKitWebPolicyDecision *p,
106 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
107 WebKitNetworkRequest *r, WebKitWebNavigationAction *n,
108 WebKitWebPolicyDecision *p, Client *c);
109 static void destroyclient(Client *c);
110 static void destroywin(GtkWidget* w, Client *c);
111 static void die(const char *errstr, ...);
112 static void eval(Client *c, const Arg *arg);
113 static void find(Client *c, const Arg *arg);
114 static void fullscreen(Client *c, const Arg *arg);
115 static const char *getatom(Client *c, int a);
116 static void gettogglestat(Client *c);
117 static void getpagestat(Client *c);
118 static char *geturi(Client *c);
119 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
121 static void inspector(Client *c, const Arg *arg);
122 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
124 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
125 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
126 static void inspector_finished(WebKitWebInspector *i, Client *c);
128 static gboolean keypress(GtkAccelGroup *group,
129 GObject *obj, guint key, GdkModifierType mods,
131 static void linkhover(WebKitWebView *v, const char* t, const char* l,
133 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
135 static void loaduri(Client *c, const Arg *arg);
136 static void navigate(Client *c, const Arg *arg);
137 static Client *newclient(void);
138 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
139 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
140 static void populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c);
141 static void popupactivate(GtkMenuItem *menu, Client *);
142 static void print(Client *c, const Arg *arg);
143 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
145 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
146 static void reload(Client *c, const Arg *arg);
147 static void scroll_h(Client *c, const Arg *arg);
148 static void scroll_v(Client *c, const Arg *arg);
149 static void scroll(GtkAdjustment *a, const Arg *arg);
150 static void setatom(Client *c, int a, const char *v);
151 static void setup(void);
152 static void sigchld(int unused);
153 static void source(Client *c, const Arg *arg);
154 static void spawn(Client *c, const Arg *arg);
155 static void stop(Client *c, const Arg *arg);
156 static void titlechange(WebKitWebView *v, WebKitWebFrame *frame,
157 const char *title, Client *c);
158 static void toggle(Client *c, const Arg *arg);
159 static void togglescrollbars(Client *c, const Arg *arg);
160 static void togglestyle(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 add_accels(Client *c) {
174 GtkAccelGroup *group = gtk_accel_group_new();
175 for(i = 0; i < LENGTH(keys); i++) {
176 GClosure *closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
177 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod,
180 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
184 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
185 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
187 const gchar *uri = webkit_network_request_get_uri(req);
189 if(g_str_has_suffix(uri, "/favicon.ico"))
190 webkit_network_request_set_uri(req, "about:blank");
194 buildpath(const char *path) {
198 /* creating directory */
200 apath = g_strdup(path);
201 } else if(path[0] == '~') {
203 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
205 apath = g_strconcat(g_get_home_dir(), "/",
209 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
212 if((p = strrchr(apath, '/'))) {
214 g_mkdir_with_parents(apath, 0700);
215 g_chmod(apath, 0700); /* in case it existed */
218 /* creating file (gives error when apath ends with "/") */
219 if((f = fopen(apath, "a"))) {
220 g_chmod(apath, 0600); /* always */
228 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
229 WebKitHitTestResultContext context;
230 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
234 g_object_get(result, "context", &context, NULL);
235 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
237 g_object_get(result, "link-uri", &arg.v, NULL);
238 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
248 destroyclient(clients);
255 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
256 SoupCookie *new_cookie) {
257 flock(COOKIEJAR(self)->lock, LOCK_EX);
258 if(new_cookie && !new_cookie->expires && sessiontime) {
259 soup_cookie_set_expires(new_cookie,
260 soup_date_new_from_now(sessiontime));
262 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
263 old_cookie, new_cookie);
264 flock(COOKIEJAR(self)->lock, LOCK_UN);
268 cookiejar_class_init(CookieJarClass *klass) {
269 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
270 G_OBJECT_CLASS(klass)->get_property =
271 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
272 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
273 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
274 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
278 cookiejar_finalize(GObject *self) {
279 close(COOKIEJAR(self)->lock);
280 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
284 cookiejar_init(CookieJar *self) {
285 self->lock = open(cookiefile, 0);
288 static SoupCookieJar *
289 cookiejar_new(const char *filename, gboolean read_only) {
290 return g_object_new(COOKIEJAR_TYPE,
291 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
292 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
296 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
298 flock(COOKIEJAR(self)->lock, LOCK_SH);
299 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
301 flock(COOKIEJAR(self)->lock, LOCK_UN);
305 evalscript(JSContextRef js, char *script, char* scriptname) {
306 JSStringRef jsscript, jsscriptname;
307 JSValueRef exception = NULL;
309 jsscript = JSStringCreateWithUTF8CString(script);
310 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
311 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
312 jsscriptname, 0, &exception);
313 JSStringRelease(jsscript);
314 JSStringRelease(jsscriptname);
318 runscript(WebKitWebFrame *frame) {
322 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
323 evalscript(webkit_web_frame_get_global_context(frame),
329 clipboard(Client *c, const Arg *arg) {
330 gboolean paste = *(gboolean *)arg;
333 gtk_clipboard_request_text(
334 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
337 gtk_clipboard_set_text(
338 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
339 c->linkhover ? c->linkhover : geturi(c), -1);
344 copystr(char **str, const char *src) {
355 static WebKitWebView *
356 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
357 Client *n = newclient();
362 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
363 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
364 if(!webkit_web_view_can_show_mime_type(v, m)) {
365 webkit_web_policy_decision_download(p);
372 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
373 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
377 if(webkit_web_navigation_action_get_reason(n) ==
378 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
379 webkit_web_policy_decision_ignore(p);
380 arg.v = (void *)webkit_network_request_get_uri(r);
381 newwindow(NULL, &arg, 0);
388 destroyclient(Client *c) {
391 webkit_web_view_stop_loading(c->view);
392 gtk_widget_destroy(GTK_WIDGET(c->view));
393 gtk_widget_destroy(c->scroll);
394 gtk_widget_destroy(c->vbox);
395 gtk_widget_destroy(c->win);
397 for(p = clients; p && p->next != c; p = p->next);
409 destroywin(GtkWidget* w, Client *c) {
414 die(const char *errstr, ...) {
417 va_start(ap, errstr);
418 vfprintf(stderr, errstr, ap);
424 find(Client *c, const Arg *arg) {
427 s = getatom(c, AtomFind);
428 gboolean forward = *(gboolean *)arg;
429 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
433 fullscreen(Client *c, const Arg *arg) {
435 gtk_window_unfullscreen(GTK_WINDOW(c->win));
437 gtk_window_fullscreen(GTK_WINDOW(c->win));
439 c->fullscreen = !c->fullscreen;
443 getatom(Client *c, int a) {
444 static char buf[BUFSIZ];
447 unsigned long ldummy;
448 unsigned char *p = NULL;
450 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
451 atoms[a], 0L, BUFSIZ, False, XA_STRING,
452 &adummy, &idummy, &ldummy, &ldummy, &p);
454 strncpy(buf, (char *)p, LENGTH(buf)-1);
467 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
473 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
477 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
483 inspector(Client *c, const Arg *arg) {
484 if(c->isinspecting) {
485 webkit_web_inspector_close(c->inspector);
487 webkit_web_inspector_show(c->inspector);
491 static WebKitWebView *
492 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
493 return WEBKIT_WEB_VIEW(webkit_web_view_new());
497 inspector_show(WebKitWebInspector *i, Client *c) {
503 w = webkit_web_inspector_get_web_view(i);
504 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
505 gtk_widget_show(GTK_WIDGET(w));
506 c->isinspecting = true;
512 inspector_close(WebKitWebInspector *i, Client *c) {
518 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
520 gtk_widget_destroy(w);
521 c->isinspecting = false;
527 inspector_finished(WebKitWebInspector *i, Client *c) {
528 g_free(c->inspector);
532 keypress(GtkAccelGroup *group, GObject *obj,
533 guint key, GdkModifierType mods, Client *c) {
535 gboolean processed = FALSE;
537 mods = CLEANMASK(mods);
538 key = gdk_keyval_to_lower(key);
540 for(i = 0; i < LENGTH(keys); i++) {
541 if(key == keys[i].keyval
542 && mods == keys[i].mod
544 keys[i].func(c, &(keys[i].arg));
553 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
555 c->linkhover = copystr(&c->linkhover, l);
556 } else if(c->linkhover) {
564 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
565 WebKitWebFrame *frame;
566 WebKitWebDataSource *src;
567 WebKitNetworkRequest *request;
571 switch(webkit_web_view_get_load_status (c->view)) {
572 case WEBKIT_LOAD_COMMITTED:
574 if(strstr(uri, "https://") == uri) {
575 frame = webkit_web_view_get_main_frame(c->view);
576 src = webkit_web_frame_get_data_source(frame);
577 request = webkit_web_data_source_get_request(src);
578 msg = webkit_network_request_get_message(request);
579 c->sslfailed = soup_message_get_flags(msg)
580 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
582 setatom(c, AtomUri, uri);
584 case WEBKIT_LOAD_FINISHED:
594 loaduri(Client *c, const Arg *arg) {
596 const char *uri = (char *)arg->v;
597 Arg a = { .b = FALSE };
600 if(strcmp(uri, "") == 0)
603 /* In case it's a file path. */
604 if(stat(uri, &st) == 0) {
605 rp = realpath(uri, NULL);
606 u = g_strdup_printf("file://%s", rp);
609 u = g_strrstr(uri, "://") ? g_strdup(uri)
610 : g_strdup_printf("http://%s", uri);
613 /* prevents endless loop */
614 if(c->uri && strcmp(u, c->uri) == 0) {
617 webkit_web_view_load_uri(c->view, u);
619 c->title = copystr(&c->title, u);
626 navigate(Client *c, const Arg *arg) {
627 int steps = *(int *)arg;
628 webkit_web_view_go_back_or_forward(c->view, steps);
634 WebKitWebSettings *settings;
635 WebKitWebFrame *frame;
636 GdkGeometry hints = { 1, 1 };
639 if(!(c = calloc(1, sizeof(Client))))
640 die("Cannot malloc!\n");
644 c->win = gtk_plug_new(embed);
646 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
648 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
649 * is always correct, so we should still call this function.
650 * But when doing so, we *must* differentiate between a
651 * WM_CLASS and a resource on the window. By convention, the
652 * window class (WM_CLASS) is capped, while the resource is in
653 * lowercase. Both these values come as a pair.
655 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
657 /* TA: 20091214: And set the role here as well -- so that
658 * sessions can pick this up.
660 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
662 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
663 g_signal_connect(G_OBJECT(c->win),
665 G_CALLBACK(destroywin), c);
671 c->pane = gtk_vpaned_new();
674 c->vbox = gtk_vbox_new(FALSE, 0);
675 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
678 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
680 g_signal_connect(G_OBJECT(c->view),
682 G_CALLBACK(titlechange), c);
683 g_signal_connect(G_OBJECT(c->view),
684 "hovering-over-link",
685 G_CALLBACK(linkhover), c);
686 g_signal_connect(G_OBJECT(c->view),
688 G_CALLBACK(createwindow), c);
689 g_signal_connect(G_OBJECT(c->view),
690 "new-window-policy-decision-requested",
691 G_CALLBACK(decidewindow), c);
692 g_signal_connect(G_OBJECT(c->view),
693 "mime-type-policy-decision-requested",
694 G_CALLBACK(decidedownload), c);
695 g_signal_connect(G_OBJECT(c->view),
696 "window-object-cleared",
697 G_CALLBACK(windowobjectcleared), c);
698 g_signal_connect(G_OBJECT(c->view),
699 "notify::load-status",
700 G_CALLBACK(loadstatuschange), c);
701 g_signal_connect(G_OBJECT(c->view),
703 G_CALLBACK(progresschange), c);
704 g_signal_connect(G_OBJECT(c->view),
705 "download-requested",
706 G_CALLBACK(initdownload), c);
707 g_signal_connect(G_OBJECT(c->view),
708 "button-release-event",
709 G_CALLBACK(buttonrelease), c);
710 g_signal_connect(G_OBJECT(c->view),
712 G_CALLBACK(populatepopup), c);
713 g_signal_connect(G_OBJECT(c->view),
714 "resource-request-starting",
715 G_CALLBACK(beforerequest), c);
717 /* Scrolled Window */
718 c->scroll = gtk_scrolled_window_new(NULL, NULL);
720 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
721 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
722 G_CALLBACK(gtk_true), NULL);
724 if(!enablescrollbars) {
725 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
726 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
728 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
729 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
733 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
734 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
735 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
738 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
739 TRUE, 0, GTK_PACK_START);
740 gtk_widget_grab_focus(GTK_WIDGET(c->view));
741 gtk_widget_show(c->pane);
742 gtk_widget_show(c->vbox);
743 gtk_widget_show(c->scroll);
744 gtk_widget_show(GTK_WIDGET(c->view));
745 gtk_widget_show(c->win);
746 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
748 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
749 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
750 webkit_web_view_set_full_content_zoom(c->view, TRUE);
754 settings = webkit_web_view_get_settings(c->view);
755 if(!(ua = getenv("SURF_USERAGENT")))
757 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
758 uri = g_strconcat("file://", stylefile, NULL);
759 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
760 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
762 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
764 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
766 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
767 enablespatialbrowsing, NULL);
768 g_object_set(G_OBJECT(settings), "enable-developer-extras",
769 enableinspector, NULL);
770 g_object_set(G_OBJECT(settings), "enable-default-context-menu",
771 kioskmode ^ 1, NULL);
772 g_object_set(G_OBJECT(settings), "default-font-size",
773 defaultfontsize, NULL);
775 if(enableinspector) {
776 c->inspector = WEBKIT_WEB_INSPECTOR(
777 webkit_web_view_get_inspector(c->view));
778 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
779 G_CALLBACK(inspector_new), c);
780 g_signal_connect(G_OBJECT(c->inspector), "show-window",
781 G_CALLBACK(inspector_show), c);
782 g_signal_connect(G_OBJECT(c->inspector), "close-window",
783 G_CALLBACK(inspector_close), c);
784 g_signal_connect(G_OBJECT(c->inspector), "finished",
785 G_CALLBACK(inspector_finished), c);
786 c->isinspecting = false;
791 setatom(c, AtomFind, "");
792 setatom(c, AtomUri, "about:blank");
794 webkit_web_view_set_transparent(c->view, TRUE);
801 gdk_display_sync(gtk_widget_get_display(c->win));
803 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
805 if (fclose(stdout) != 0) {
806 die("Error closing stdout");
814 newwindow(Client *c, const Arg *arg, gboolean noembed) {
816 const char *cmd[12], *uri;
817 const Arg a = { .v = (void *)cmd };
821 if(!enablescrollbars)
823 if(embed && !noembed) {
825 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
839 uri = arg->v ? (char *)arg->v : c->linkhover;
847 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
848 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
850 for(GList *l = items; l; l = l->next) {
851 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
858 popupactivate(GtkMenuItem *menu, Client *c) {
860 * context-menu-action-2000 open link
861 * context-menu-action-1 open link in window
862 * context-menu-action-2 download linked file
863 * context-menu-action-3 copy link location
864 * context-menu-action-13 reload
865 * context-menu-action-10 back
866 * context-menu-action-11 forward
867 * context-menu-action-12 stop
872 GtkClipboard *prisel;
874 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
878 name = gtk_action_get_name(a);
879 if(!g_strcmp0(name, "context-menu-action-3")) {
880 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
881 gtk_clipboard_set_text(prisel, c->linkhover, -1);
886 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
887 Arg arg = {.v = text };
889 loaduri((Client *) d, &arg);
893 print(Client *c, const Arg *arg) {
894 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
897 static GdkFilterReturn
898 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
899 Client *c = (Client *)d;
903 if(((XEvent *)e)->type == PropertyNotify) {
904 ev = &((XEvent *)e)->xproperty;
905 if(ev->state == PropertyNewValue) {
906 if(ev->atom == atoms[AtomFind]) {
910 return GDK_FILTER_REMOVE;
911 } else if(ev->atom == atoms[AtomGo]) {
912 arg.v = getatom(c, AtomGo);
915 return GDK_FILTER_REMOVE;
919 return GDK_FILTER_CONTINUE;
923 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
924 c->progress = webkit_web_view_get_progress(c->view) * 100;
929 reload(Client *c, const Arg *arg) {
930 gboolean nocache = *(gboolean *)arg;
932 webkit_web_view_reload_bypass_cache(c->view);
934 webkit_web_view_reload(c->view);
939 scroll_h(Client *c, const Arg *arg) {
940 scroll(gtk_scrolled_window_get_hadjustment(
941 GTK_SCROLLED_WINDOW(c->scroll)), arg);
945 scroll_v(Client *c, const Arg *arg) {
946 scroll(gtk_scrolled_window_get_vadjustment(
947 GTK_SCROLLED_WINDOW(c->scroll)), arg);
951 scroll(GtkAdjustment *a, const Arg *arg) {
954 v = gtk_adjustment_get_value(a);
958 v += gtk_adjustment_get_page_increment(a) *
964 v += gtk_adjustment_get_step_increment(a) * arg->i;
968 v = MIN(v, gtk_adjustment_get_upper(a) -
969 gtk_adjustment_get_page_size(a));
970 gtk_adjustment_set_value(a, v);
974 setatom(Client *c, int a, const char *v) {
976 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
977 atoms[a], XA_STRING, 8, PropModeReplace,
978 (unsigned char *)v, strlen(v) + 1);
988 /* clean up any zombies immediately */
990 gtk_init(NULL, NULL);
995 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
996 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
997 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1000 cookiefile = buildpath(cookiefile);
1001 scriptfile = buildpath(scriptfile);
1002 stylefile = buildpath(stylefile);
1004 /* request handler */
1005 s = webkit_get_default_session();
1008 soup_session_add_feature(s,
1009 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1013 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
1014 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1017 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1018 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1019 g_strdup_printf("http://%s", proxy);
1020 puri = soup_uri_new(new_proxy);
1021 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1022 soup_uri_free(puri);
1029 sigchld(int unused) {
1030 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1031 die("Can't install SIGCHLD handler");
1032 while(0 < waitpid(-1, NULL, WNOHANG));
1036 source(Client *c, const Arg *arg) {
1037 Arg a = { .b = FALSE };
1040 s = webkit_web_view_get_view_source_mode(c->view);
1041 webkit_web_view_set_view_source_mode(c->view, !s);
1046 spawn(Client *c, const Arg *arg) {
1049 close(ConnectionNumber(dpy));
1051 execvp(((char **)arg->v)[0], (char **)arg->v);
1052 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1059 eval(Client *c, const Arg *arg) {
1060 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1061 evalscript(webkit_web_frame_get_global_context(frame),
1062 ((char **)arg->v)[0], "");
1066 stop(Client *c, const Arg *arg) {
1067 webkit_web_view_stop_loading(c->view);
1071 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1072 c->title = copystr(&c->title, t);
1077 toggle(Client *c, const Arg *arg) {
1078 WebKitWebSettings *settings;
1079 char *name = (char *)arg->v;
1081 Arg a = { .b = FALSE };
1083 settings = webkit_web_view_get_settings(c->view);
1084 g_object_get(G_OBJECT(settings), name, &value, NULL);
1085 g_object_set(G_OBJECT(settings), name, !value, NULL);
1091 twitch(Client *c, const Arg *arg) {
1095 a = gtk_scrolled_window_get_vadjustment(
1096 GTK_SCROLLED_WINDOW(c->scroll));
1098 v = gtk_adjustment_get_value(a);
1103 v = MIN(v, gtk_adjustment_get_upper(a) -
1104 gtk_adjustment_get_page_size(a));
1105 gtk_adjustment_set_value(a, v);
1109 togglescrollbars(Client *c, const Arg *arg) {
1110 GtkPolicyType vspolicy;
1113 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1115 if(vspolicy == GTK_POLICY_AUTOMATIC) {
1116 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1117 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1119 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1120 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1129 togglestyle(Client *c, const Arg *arg) {
1130 WebKitWebSettings *settings;
1133 settings = webkit_web_view_get_settings(c->view);
1134 g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1135 uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, NULL);
1136 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1142 gettogglestat(Client *c){
1145 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1147 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1149 togglestat[0] = value? 'C': 'c';
1151 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1152 togglestat[1] = value? 'I': 'i';
1154 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1155 togglestat[2] = value? 'S': 's';
1157 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1158 togglestat[3] = value? 'V': 'v';
1160 g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1161 togglestat[4] = uri[0] ? 'M': 'm';
1163 togglestat[5] = '\0';
1167 getpagestat(Client *c) {
1168 const char *uri = geturi(c);
1170 if(strstr(uri, "https://") == uri) {
1171 pagestat[0] = c->sslfailed ? 'U' : 'T';
1176 pagestat[1] = usingproxy ? 'P' : '-';
1189 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->linkhover);
1190 } else if(c->progress != 100) {
1191 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress, togglestat,
1192 pagestat, c->title);
1194 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->title);
1197 gtk_window_set_title(GTK_WINDOW(c->win), t);
1202 updatewinid(Client *c) {
1203 snprintf(winid, LENGTH(winid), "%u",
1204 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1209 die("usage: %s [-biknpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1210 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1214 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1215 JSObjectRef win, Client *c) {
1220 zoom(Client *c, const Arg *arg) {
1224 webkit_web_view_zoom_out(c->view);
1225 } else if(arg->i > 0) {
1227 webkit_web_view_zoom_in(c->view);
1231 webkit_web_view_set_zoom_level(c->view, 1.0);
1236 main(int argc, char *argv[]) {
1239 memset(&arg, 0, sizeof(arg));
1241 /* command line args */
1244 enablescrollbars = 0;
1247 cookiefile = EARGF(usage());
1250 embed = strtol(EARGF(usage()), NULL, 0);
1259 enableinspector = 0;
1265 scriptfile = EARGF(usage());
1271 stylefile = EARGF(usage());
1274 useragent = EARGF(usage());
1277 die("surf-"VERSION", ©2009-2012 surf engineers, "
1278 "see LICENSE for details\n");
1291 loaduri(clients, &arg);
1296 return EXIT_SUCCESS;