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 addaccelgroup(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 addaccelgroup(Client *c) {
174 GtkAccelGroup *group = gtk_accel_group_new();
177 for(i = 0; i < LENGTH(keys); i++) {
178 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
179 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod,
182 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
186 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
187 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
189 const gchar *uri = webkit_network_request_get_uri(req);
191 if(g_str_has_suffix(uri, "/favicon.ico"))
192 webkit_network_request_set_uri(req, "about:blank");
196 buildpath(const char *path) {
200 /* creating directory */
202 apath = g_strdup(path);
203 } else if(path[0] == '~') {
205 apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
207 apath = g_strconcat(g_get_home_dir(), "/",
211 apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
214 if((p = strrchr(apath, '/'))) {
216 g_mkdir_with_parents(apath, 0700);
217 g_chmod(apath, 0700); /* in case it existed */
220 /* creating file (gives error when apath ends with "/") */
221 if((f = fopen(apath, "a"))) {
222 g_chmod(apath, 0600); /* always */
230 buttonrelease(WebKitWebView *web, GdkEventButton *e, GList *gl) {
231 WebKitHitTestResultContext context;
232 WebKitHitTestResult *result = webkit_web_view_get_hit_test_result(web,
236 g_object_get(result, "context", &context, NULL);
237 if(context & WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK) {
239 g_object_get(result, "link-uri", &arg.v, NULL);
240 newwindow(NULL, &arg, e->state & GDK_CONTROL_MASK);
250 destroyclient(clients);
257 cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
258 SoupCookie *new_cookie) {
259 flock(COOKIEJAR(self)->lock, LOCK_EX);
260 if(new_cookie && !new_cookie->expires && sessiontime) {
261 soup_cookie_set_expires(new_cookie,
262 soup_date_new_from_now(sessiontime));
264 SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
265 old_cookie, new_cookie);
266 flock(COOKIEJAR(self)->lock, LOCK_UN);
270 cookiejar_class_init(CookieJarClass *klass) {
271 SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
272 G_OBJECT_CLASS(klass)->get_property =
273 G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
274 G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
275 G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
276 g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
280 cookiejar_finalize(GObject *self) {
281 close(COOKIEJAR(self)->lock);
282 G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
286 cookiejar_init(CookieJar *self) {
287 self->lock = open(cookiefile, 0);
290 static SoupCookieJar *
291 cookiejar_new(const char *filename, gboolean read_only) {
292 return g_object_new(COOKIEJAR_TYPE,
293 SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
294 SOUP_COOKIE_JAR_READ_ONLY, read_only, NULL);
298 cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
300 flock(COOKIEJAR(self)->lock, LOCK_SH);
301 G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
303 flock(COOKIEJAR(self)->lock, LOCK_UN);
307 evalscript(JSContextRef js, char *script, char* scriptname) {
308 JSStringRef jsscript, jsscriptname;
309 JSValueRef exception = NULL;
311 jsscript = JSStringCreateWithUTF8CString(script);
312 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
313 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
314 jsscriptname, 0, &exception);
315 JSStringRelease(jsscript);
316 JSStringRelease(jsscriptname);
320 runscript(WebKitWebFrame *frame) {
324 if(g_file_get_contents(scriptfile, &script, NULL, &error)) {
325 evalscript(webkit_web_frame_get_global_context(frame),
331 clipboard(Client *c, const Arg *arg) {
332 gboolean paste = *(gboolean *)arg;
335 gtk_clipboard_request_text(
336 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
339 gtk_clipboard_set_text(
340 gtk_clipboard_get(GDK_SELECTION_PRIMARY),
341 c->linkhover ? c->linkhover : geturi(c), -1);
346 copystr(char **str, const char *src) {
357 static WebKitWebView *
358 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c) {
359 Client *n = newclient();
364 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
365 gchar *m, WebKitWebPolicyDecision *p, Client *c) {
366 if(!webkit_web_view_can_show_mime_type(v, m)) {
367 webkit_web_policy_decision_download(p);
374 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
375 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
379 if(webkit_web_navigation_action_get_reason(n) ==
380 WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
381 webkit_web_policy_decision_ignore(p);
382 arg.v = (void *)webkit_network_request_get_uri(r);
383 newwindow(NULL, &arg, 0);
390 destroyclient(Client *c) {
393 webkit_web_view_stop_loading(c->view);
394 gtk_widget_destroy(GTK_WIDGET(c->view));
395 gtk_widget_destroy(c->scroll);
396 gtk_widget_destroy(c->vbox);
397 gtk_widget_destroy(c->win);
399 for(p = clients; p && p->next != c; p = p->next);
411 destroywin(GtkWidget* w, Client *c) {
416 die(const char *errstr, ...) {
419 va_start(ap, errstr);
420 vfprintf(stderr, errstr, ap);
426 find(Client *c, const Arg *arg) {
429 s = getatom(c, AtomFind);
430 gboolean forward = *(gboolean *)arg;
431 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
435 fullscreen(Client *c, const Arg *arg) {
437 gtk_window_unfullscreen(GTK_WINDOW(c->win));
439 gtk_window_fullscreen(GTK_WINDOW(c->win));
441 c->fullscreen = !c->fullscreen;
445 getatom(Client *c, int a) {
446 static char buf[BUFSIZ];
449 unsigned long ldummy;
450 unsigned char *p = NULL;
452 XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
453 atoms[a], 0L, BUFSIZ, False, XA_STRING,
454 &adummy, &idummy, &ldummy, &ldummy, &p);
456 strncpy(buf, (char *)p, LENGTH(buf)-1);
469 if(!(uri = (char *)webkit_web_view_get_uri(c->view)))
475 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c) {
479 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
485 inspector(Client *c, const Arg *arg) {
486 if(c->isinspecting) {
487 webkit_web_inspector_close(c->inspector);
489 webkit_web_inspector_show(c->inspector);
493 static WebKitWebView *
494 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c) {
495 return WEBKIT_WEB_VIEW(webkit_web_view_new());
499 inspector_show(WebKitWebInspector *i, Client *c) {
505 w = webkit_web_inspector_get_web_view(i);
506 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
507 gtk_widget_show(GTK_WIDGET(w));
508 c->isinspecting = true;
514 inspector_close(WebKitWebInspector *i, Client *c) {
520 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
522 gtk_widget_destroy(w);
523 c->isinspecting = false;
529 inspector_finished(WebKitWebInspector *i, Client *c) {
530 g_free(c->inspector);
534 keypress(GtkAccelGroup *group, GObject *obj,
535 guint key, GdkModifierType mods, Client *c) {
537 gboolean processed = FALSE;
539 mods = CLEANMASK(mods);
540 key = gdk_keyval_to_lower(key);
542 for(i = 0; i < LENGTH(keys); i++) {
543 if(key == keys[i].keyval
544 && mods == keys[i].mod
546 keys[i].func(c, &(keys[i].arg));
555 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c) {
557 c->linkhover = copystr(&c->linkhover, l);
558 } else if(c->linkhover) {
566 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
567 WebKitWebFrame *frame;
568 WebKitWebDataSource *src;
569 WebKitNetworkRequest *request;
573 switch(webkit_web_view_get_load_status (c->view)) {
574 case WEBKIT_LOAD_COMMITTED:
576 if(strstr(uri, "https://") == uri) {
577 frame = webkit_web_view_get_main_frame(c->view);
578 src = webkit_web_frame_get_data_source(frame);
579 request = webkit_web_data_source_get_request(src);
580 msg = webkit_network_request_get_message(request);
581 c->sslfailed = soup_message_get_flags(msg)
582 ^ SOUP_MESSAGE_CERTIFICATE_TRUSTED;
584 setatom(c, AtomUri, uri);
586 case WEBKIT_LOAD_FINISHED:
596 loaduri(Client *c, const Arg *arg) {
598 const char *uri = (char *)arg->v;
599 Arg a = { .b = FALSE };
602 if(strcmp(uri, "") == 0)
605 /* In case it's a file path. */
606 if(stat(uri, &st) == 0) {
607 rp = realpath(uri, NULL);
608 u = g_strdup_printf("file://%s", rp);
611 u = g_strrstr(uri, "://") ? g_strdup(uri)
612 : g_strdup_printf("http://%s", uri);
615 /* prevents endless loop */
616 if(c->uri && strcmp(u, c->uri) == 0) {
619 webkit_web_view_load_uri(c->view, u);
621 c->title = copystr(&c->title, u);
628 navigate(Client *c, const Arg *arg) {
629 int steps = *(int *)arg;
630 webkit_web_view_go_back_or_forward(c->view, steps);
636 WebKitWebSettings *settings;
637 WebKitWebFrame *frame;
638 GdkGeometry hints = { 1, 1 };
641 if(!(c = calloc(1, sizeof(Client))))
642 die("Cannot malloc!\n");
646 c->win = gtk_plug_new(embed);
648 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
650 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
651 * is always correct, so we should still call this function.
652 * But when doing so, we *must* differentiate between a
653 * WM_CLASS and a resource on the window. By convention, the
654 * window class (WM_CLASS) is capped, while the resource is in
655 * lowercase. Both these values come as a pair.
657 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
659 /* TA: 20091214: And set the role here as well -- so that
660 * sessions can pick this up.
662 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
664 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
665 g_signal_connect(G_OBJECT(c->win),
667 G_CALLBACK(destroywin), c);
673 c->pane = gtk_vpaned_new();
676 c->vbox = gtk_vbox_new(FALSE, 0);
677 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
680 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
682 g_signal_connect(G_OBJECT(c->view),
684 G_CALLBACK(titlechange), c);
685 g_signal_connect(G_OBJECT(c->view),
686 "hovering-over-link",
687 G_CALLBACK(linkhover), c);
688 g_signal_connect(G_OBJECT(c->view),
690 G_CALLBACK(createwindow), c);
691 g_signal_connect(G_OBJECT(c->view),
692 "new-window-policy-decision-requested",
693 G_CALLBACK(decidewindow), c);
694 g_signal_connect(G_OBJECT(c->view),
695 "mime-type-policy-decision-requested",
696 G_CALLBACK(decidedownload), c);
697 g_signal_connect(G_OBJECT(c->view),
698 "window-object-cleared",
699 G_CALLBACK(windowobjectcleared), c);
700 g_signal_connect(G_OBJECT(c->view),
701 "notify::load-status",
702 G_CALLBACK(loadstatuschange), c);
703 g_signal_connect(G_OBJECT(c->view),
705 G_CALLBACK(progresschange), c);
706 g_signal_connect(G_OBJECT(c->view),
707 "download-requested",
708 G_CALLBACK(initdownload), c);
709 g_signal_connect(G_OBJECT(c->view),
710 "button-release-event",
711 G_CALLBACK(buttonrelease), c);
712 g_signal_connect(G_OBJECT(c->view),
714 G_CALLBACK(populatepopup), c);
715 g_signal_connect(G_OBJECT(c->view),
716 "resource-request-starting",
717 G_CALLBACK(beforerequest), c);
719 /* Scrolled Window */
720 c->scroll = gtk_scrolled_window_new(NULL, NULL);
722 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
723 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
724 G_CALLBACK(gtk_true), NULL);
726 if(!enablescrollbars) {
727 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
728 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
730 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
731 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
735 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
736 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
737 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
740 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE,
741 TRUE, 0, GTK_PACK_START);
742 gtk_widget_grab_focus(GTK_WIDGET(c->view));
743 gtk_widget_show(c->pane);
744 gtk_widget_show(c->vbox);
745 gtk_widget_show(c->scroll);
746 gtk_widget_show(GTK_WIDGET(c->view));
747 gtk_widget_show(c->win);
748 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
750 gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
751 gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
752 webkit_web_view_set_full_content_zoom(c->view, TRUE);
756 settings = webkit_web_view_get_settings(c->view);
757 if(!(ua = getenv("SURF_USERAGENT")))
759 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
760 uri = g_strconcat("file://", stylefile, NULL);
761 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
762 g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
764 g_object_set(G_OBJECT(settings), "enable-plugins", enableplugins,
766 g_object_set(G_OBJECT(settings), "enable-scripts", enablescripts,
768 g_object_set(G_OBJECT(settings), "enable-spatial-navigation",
769 enablespatialbrowsing, NULL);
770 g_object_set(G_OBJECT(settings), "enable-developer-extras",
771 enableinspector, NULL);
772 g_object_set(G_OBJECT(settings), "enable-default-context-menu",
773 kioskmode ^ 1, NULL);
774 g_object_set(G_OBJECT(settings), "default-font-size",
775 defaultfontsize, NULL);
777 if(enableinspector) {
778 c->inspector = WEBKIT_WEB_INSPECTOR(
779 webkit_web_view_get_inspector(c->view));
780 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
781 G_CALLBACK(inspector_new), c);
782 g_signal_connect(G_OBJECT(c->inspector), "show-window",
783 G_CALLBACK(inspector_show), c);
784 g_signal_connect(G_OBJECT(c->inspector), "close-window",
785 G_CALLBACK(inspector_close), c);
786 g_signal_connect(G_OBJECT(c->inspector), "finished",
787 G_CALLBACK(inspector_finished), c);
788 c->isinspecting = false;
793 setatom(c, AtomFind, "");
794 setatom(c, AtomUri, "about:blank");
796 webkit_web_view_set_transparent(c->view, TRUE);
803 gdk_display_sync(gtk_widget_get_display(c->win));
805 (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
807 if (fclose(stdout) != 0) {
808 die("Error closing stdout");
816 newwindow(Client *c, const Arg *arg, gboolean noembed) {
818 const char *cmd[12], *uri;
819 const Arg a = { .v = (void *)cmd };
823 if(!enablescrollbars)
825 if(embed && !noembed) {
827 snprintf(tmp, LENGTH(tmp), "%u\n", (int)embed);
841 uri = arg->v ? (char *)arg->v : c->linkhover;
849 populatepopup(WebKitWebView *web, GtkMenu *menu, Client *c) {
850 GList *items = gtk_container_get_children(GTK_CONTAINER(menu));
852 for(GList *l = items; l; l = l->next) {
853 g_signal_connect(l->data, "activate", G_CALLBACK(popupactivate), c);
860 popupactivate(GtkMenuItem *menu, Client *c) {
862 * context-menu-action-2000 open link
863 * context-menu-action-1 open link in window
864 * context-menu-action-2 download linked file
865 * context-menu-action-3 copy link location
866 * context-menu-action-13 reload
867 * context-menu-action-10 back
868 * context-menu-action-11 forward
869 * context-menu-action-12 stop
874 GtkClipboard *prisel;
876 a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(menu));
880 name = gtk_action_get_name(a);
881 if(!g_strcmp0(name, "context-menu-action-3")) {
882 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
883 gtk_clipboard_set_text(prisel, c->linkhover, -1);
888 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d) {
889 Arg arg = {.v = text };
891 loaduri((Client *) d, &arg);
895 print(Client *c, const Arg *arg) {
896 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
899 static GdkFilterReturn
900 processx(GdkXEvent *e, GdkEvent *event, gpointer d) {
901 Client *c = (Client *)d;
905 if(((XEvent *)e)->type == PropertyNotify) {
906 ev = &((XEvent *)e)->xproperty;
907 if(ev->state == PropertyNewValue) {
908 if(ev->atom == atoms[AtomFind]) {
912 return GDK_FILTER_REMOVE;
913 } else if(ev->atom == atoms[AtomGo]) {
914 arg.v = getatom(c, AtomGo);
917 return GDK_FILTER_REMOVE;
921 return GDK_FILTER_CONTINUE;
925 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c) {
926 c->progress = webkit_web_view_get_progress(c->view) * 100;
931 reload(Client *c, const Arg *arg) {
932 gboolean nocache = *(gboolean *)arg;
934 webkit_web_view_reload_bypass_cache(c->view);
936 webkit_web_view_reload(c->view);
941 scroll_h(Client *c, const Arg *arg) {
942 scroll(gtk_scrolled_window_get_hadjustment(
943 GTK_SCROLLED_WINDOW(c->scroll)), arg);
947 scroll_v(Client *c, const Arg *arg) {
948 scroll(gtk_scrolled_window_get_vadjustment(
949 GTK_SCROLLED_WINDOW(c->scroll)), arg);
953 scroll(GtkAdjustment *a, const Arg *arg) {
956 v = gtk_adjustment_get_value(a);
960 v += gtk_adjustment_get_page_increment(a) *
966 v += gtk_adjustment_get_step_increment(a) * arg->i;
970 v = MIN(v, gtk_adjustment_get_upper(a) -
971 gtk_adjustment_get_page_size(a));
972 gtk_adjustment_set_value(a, v);
976 setatom(Client *c, int a, const char *v) {
978 XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
979 atoms[a], XA_STRING, 8, PropModeReplace,
980 (unsigned char *)v, strlen(v) + 1);
990 /* clean up any zombies immediately */
992 gtk_init(NULL, NULL);
997 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
998 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
999 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1001 /* dirs and files */
1002 cookiefile = buildpath(cookiefile);
1003 scriptfile = buildpath(scriptfile);
1004 stylefile = buildpath(stylefile);
1006 /* request handler */
1007 s = webkit_get_default_session();
1010 soup_session_add_feature(s,
1011 SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
1015 g_object_set(G_OBJECT(s), "ssl-ca-file", cafile, NULL);
1016 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1019 if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
1020 new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
1021 g_strdup_printf("http://%s", proxy);
1022 puri = soup_uri_new(new_proxy);
1023 g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
1024 soup_uri_free(puri);
1031 sigchld(int unused) {
1032 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1033 die("Can't install SIGCHLD handler");
1034 while(0 < waitpid(-1, NULL, WNOHANG));
1038 source(Client *c, const Arg *arg) {
1039 Arg a = { .b = FALSE };
1042 s = webkit_web_view_get_view_source_mode(c->view);
1043 webkit_web_view_set_view_source_mode(c->view, !s);
1048 spawn(Client *c, const Arg *arg) {
1051 close(ConnectionNumber(dpy));
1053 execvp(((char **)arg->v)[0], (char **)arg->v);
1054 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1061 eval(Client *c, const Arg *arg) {
1062 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1063 evalscript(webkit_web_frame_get_global_context(frame),
1064 ((char **)arg->v)[0], "");
1068 stop(Client *c, const Arg *arg) {
1069 webkit_web_view_stop_loading(c->view);
1073 titlechange(WebKitWebView *v, WebKitWebFrame *f, const char *t, Client *c) {
1074 c->title = copystr(&c->title, t);
1079 toggle(Client *c, const Arg *arg) {
1080 WebKitWebSettings *settings;
1081 char *name = (char *)arg->v;
1083 Arg a = { .b = FALSE };
1085 settings = webkit_web_view_get_settings(c->view);
1086 g_object_get(G_OBJECT(settings), name, &value, NULL);
1087 g_object_set(G_OBJECT(settings), name, !value, NULL);
1093 twitch(Client *c, const Arg *arg) {
1097 a = gtk_scrolled_window_get_vadjustment(
1098 GTK_SCROLLED_WINDOW(c->scroll));
1100 v = gtk_adjustment_get_value(a);
1105 v = MIN(v, gtk_adjustment_get_upper(a) -
1106 gtk_adjustment_get_page_size(a));
1107 gtk_adjustment_set_value(a, v);
1111 togglescrollbars(Client *c, const Arg *arg) {
1112 GtkPolicyType vspolicy;
1115 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL, &vspolicy);
1117 if(vspolicy == GTK_POLICY_AUTOMATIC) {
1118 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1119 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
1121 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1122 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1131 togglestyle(Client *c, const Arg *arg) {
1132 WebKitWebSettings *settings;
1135 settings = webkit_web_view_get_settings(c->view);
1136 g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1137 uri = uri[0] ? g_strdup("") : g_strconcat("file://", stylefile, NULL);
1138 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", uri, NULL);
1144 gettogglestat(Client *c){
1147 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1149 g_object_get(G_OBJECT(settings), "enable-caret-browsing",
1151 togglestat[0] = value? 'C': 'c';
1153 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1154 togglestat[1] = value? 'I': 'i';
1156 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1157 togglestat[2] = value? 'S': 's';
1159 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1160 togglestat[3] = value? 'V': 'v';
1162 g_object_get(G_OBJECT(settings), "user-stylesheet-uri", &uri, NULL);
1163 togglestat[4] = uri[0] ? 'M': 'm';
1165 togglestat[5] = '\0';
1169 getpagestat(Client *c) {
1170 const char *uri = geturi(c);
1172 if(strstr(uri, "https://") == uri) {
1173 pagestat[0] = c->sslfailed ? 'U' : 'T';
1178 pagestat[1] = usingproxy ? 'P' : '-';
1191 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->linkhover);
1192 } else if(c->progress != 100) {
1193 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress, togglestat,
1194 pagestat, c->title);
1196 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat, c->title);
1199 gtk_window_set_title(GTK_WINDOW(c->win), t);
1204 updatewinid(Client *c) {
1205 snprintf(winid, LENGTH(winid), "%u",
1206 (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
1211 die("usage: %s [-biknpsvx] [-c cookiefile] [-e xid] [-r scriptfile]"
1212 " [-t stylefile] [-u useragent] [uri]\n", basename(argv0));
1216 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1217 JSObjectRef win, Client *c) {
1222 zoom(Client *c, const Arg *arg) {
1226 webkit_web_view_zoom_out(c->view);
1227 } else if(arg->i > 0) {
1229 webkit_web_view_zoom_in(c->view);
1233 webkit_web_view_set_zoom_level(c->view, 1.0);
1238 main(int argc, char *argv[]) {
1241 memset(&arg, 0, sizeof(arg));
1243 /* command line args */
1246 enablescrollbars = 0;
1249 cookiefile = EARGF(usage());
1252 embed = strtol(EARGF(usage()), NULL, 0);
1261 enableinspector = 0;
1267 scriptfile = EARGF(usage());
1273 stylefile = EARGF(usage());
1276 useragent = EARGF(usage());
1279 die("surf-"VERSION", ©2009-2012 surf engineers, "
1280 "see LICENSE for details\n");
1293 loaduri(clients, &arg);
1298 return EXIT_SUCCESS;