1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
12 #include <gdk/gdkkeysyms.h>
14 #include <sys/types.h>
20 #include <webkit2/webkit2.h>
21 #include <glib/gstdio.h>
22 #include <JavaScriptCore/JavaScript.h>
34 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
35 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
37 enum { AtomFind, AtomGo, AtomUri, AtomLast };
39 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
40 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
41 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
42 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
43 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
44 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
45 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
48 typedef union Arg Arg;
55 typedef struct Client {
59 WebKitWebInspector *inspector;
60 char *title, *linkhover;
64 gboolean zoomed, fullscreen, isinspecting, sslfailed;
70 void (*func)(Client *c, const Arg *arg);
78 void (*func)(Client *c, const Arg *arg);
89 static Atom atoms[AtomLast];
90 static Client *clients = NULL;
91 static Window embed = 0;
92 static gboolean showxid = FALSE;
93 static char winid[64];
94 static char togglestat[9];
95 static char pagestat[3];
96 static GTlsDatabase *tlsdb;
97 static int cookiepolicy;
98 static char *stylefile = NULL;
100 static void addaccelgroup(Client *c);
101 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
102 WebKitWebResource *r, WebKitNetworkRequest *req,
103 WebKitNetworkResponse *resp, Client *c);
104 static char *buildfile(const char *path);
105 static char *buildpath(const char *path);
106 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
107 static void cleanup(void);
108 static void clipboard(Client *c, const Arg *arg);
110 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
111 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
113 static char *copystr(char **str, const char *src);
114 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
116 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
117 WebKitNetworkRequest *r, gchar *m,
118 WebKitWebPolicyDecision *p, Client *c);
119 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
120 WebKitNetworkRequest *r, WebKitWebNavigationAction
121 *n, WebKitWebPolicyDecision *p, Client *c);
122 static gboolean deletion_interface(WebKitWebView *view,
123 WebKitDOMHTMLElement *arg1, Client *c);
124 static void destroyclient(Client *c);
125 static void destroywin(GtkWidget* w, Client *c);
126 static void die(const char *errstr, ...);
127 static void eval(Client *c, const Arg *arg);
128 static void find(Client *c, const Arg *arg);
129 static void fullscreen(Client *c, const Arg *arg);
130 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
131 WebKitGeolocationPolicyDecision *d, Client *c);
132 static const char *getatom(Client *c, int a);
133 static void gettogglestat(Client *c);
134 static void getpagestat(Client *c);
135 static char *geturi(Client *c);
136 static const gchar *getstyle(const char *uri);
137 static void setstyle(Client *c, const char *style);
139 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
141 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
143 static void inspector(Client *c, const Arg *arg);
144 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
146 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
147 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
148 static void inspector_finished(WebKitWebInspector *i, Client *c);
150 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
151 GdkModifierType mods, Client *c);
152 static void linkhover(WebKitWebView *v, const char* t, const char* l,
154 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
156 static void loaduri(Client *c, const Arg *arg);
157 static void navigate(Client *c, const Arg *arg);
158 static Client *newclient(void);
159 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
160 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
161 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
162 WebKitHitTestResult *target, gboolean keyboard,
164 static void menuactivate(GtkMenuItem *item, Client *c);
165 static void print(Client *c, const Arg *arg);
166 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
168 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
169 static void linkopen(Client *c, const Arg *arg);
170 static void linkopenembed(Client *c, const Arg *arg);
171 static void reload(Client *c, const Arg *arg);
172 static void scroll_h(Client *c, const Arg *arg);
173 static void scroll_v(Client *c, const Arg *arg);
174 static void scroll(GtkAdjustment *a, const Arg *arg);
175 static void setatom(Client *c, int a, const char *v);
176 static void setup(void);
177 static void sigchld(int unused);
178 static void spawn(Client *c, const Arg *arg);
179 static void stop(Client *c, const Arg *arg);
180 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
181 static void titlechangeleave(void *a, void *b, Client *c);
182 static void toggle(Client *c, const Arg *arg);
183 static void togglecookiepolicy(Client *c, const Arg *arg);
184 static void togglegeolocation(Client *c, const Arg *arg);
185 static void togglescrollbars(Client *c, const Arg *arg);
186 static void togglestyle(Client *c, const Arg *arg);
187 static void updatetitle(Client *c);
188 static void updatewinid(Client *c);
189 static void usage(void);
190 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
191 JSContextRef js, JSObjectRef win, Client *c);
192 static void zoom(Client *c, const Arg *arg);
194 /* configuration, allows nested code to access above variables */
198 addaccelgroup(Client *c)
201 GtkAccelGroup *group = gtk_accel_group_new();
204 for (i = 0; i < LENGTH(keys); i++) {
205 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
206 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
209 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
213 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
214 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
217 const gchar *uri = webkit_network_request_get_uri(req);
220 if (g_str_has_suffix(uri, "/favicon.ico"))
221 webkit_network_request_set_uri(req, "about:blank");
223 if (!g_str_has_prefix(uri, "http://")
224 && !g_str_has_prefix(uri, "https://")
225 && !g_str_has_prefix(uri, "about:")
226 && !g_str_has_prefix(uri, "file://")
227 && !g_str_has_prefix(uri, "data:")
228 && !g_str_has_prefix(uri, "blob:")
229 && strlen(uri) > 0) {
230 for (i = 0; i < strlen(uri); i++) {
231 if (!g_ascii_isprint(uri[i])) {
237 handleplumb(c, w, uri);
242 buildfile(const char *path)
244 char *dname, *bname, *bpath, *fpath;
247 dname = g_path_get_dirname(path);
248 bname = g_path_get_basename(path);
250 bpath = buildpath(dname);
253 fpath = g_build_filename(bpath, bname, NULL);
257 if (!(f = fopen(fpath, "a")))
258 die("Could not open file: %s\n", fpath);
260 g_chmod(fpath, 0600); /* always */
267 buildpath(const char *path)
270 char *apath, *name, *p, *fpath;
272 if (path[0] == '~') {
273 if (path[1] == '/' || path[1] == '\0') {
274 p = (char *)&path[1];
275 pw = getpwuid(getuid());
277 if ((p = strchr(path, '/')))
278 name = g_strndup(&path[1], --p - path);
280 name = g_strdup(&path[1]);
282 if (!(pw = getpwnam(name))) {
283 die("Can't get user %s home directory: %s.\n",
288 apath = g_build_filename(pw->pw_dir, p, NULL);
290 apath = g_strdup(path);
293 /* creating directory */
294 if (g_mkdir_with_parents(apath, 0700) < 0)
295 die("Could not access directory: %s\n", apath);
297 fpath = realpath(apath, NULL);
304 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
306 WebKitHitTestResultContext context;
307 WebKitHitTestResult *result;
311 result = webkit_web_view_get_hit_test_result(web, e);
312 g_object_get(result, "context", &context, NULL);
313 g_object_get(result, "link-uri", &arg.v, NULL);
314 for (i = 0; i < LENGTH(buttons); i++) {
315 if (context & buttons[i].click
316 && e->button == buttons[i].button
317 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
318 && buttons[i].func) {
319 buttons[i].func(c, buttons[i].click == ClkLink
320 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
331 destroyclient(clients);
337 WebKitCookieAcceptPolicy
338 cookiepolicy_get(void)
340 switch (cookiepolicies[cookiepolicy]) {
342 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
344 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
350 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
354 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
357 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
359 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
361 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
370 evalscript(JSContextRef js, char *script, char* scriptname)
372 JSStringRef jsscript, jsscriptname;
373 JSValueRef exception = NULL;
375 jsscript = JSStringCreateWithUTF8CString(script);
376 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
377 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
378 jsscriptname, 0, &exception);
379 JSStringRelease(jsscript);
380 JSStringRelease(jsscriptname);
384 runscript(WebKitWebFrame *frame)
389 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
390 evalscript(webkit_web_frame_get_global_context(frame), script,
396 clipboard(Client *c, const Arg *arg)
398 gboolean paste = *(gboolean *)arg;
401 gtk_clipboard_request_text(gtk_clipboard_get(
402 GDK_SELECTION_PRIMARY),
405 gtk_clipboard_set_text(gtk_clipboard_get(
406 GDK_SELECTION_PRIMARY), c->linkhover
407 ? c->linkhover : geturi(c), -1);
412 copystr(char **str, const char *src)
425 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
427 Client *n = newclient();
432 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
433 gchar *m, WebKitWebPolicyDecision *p, Client *c)
435 if (!webkit_web_view_can_show_mime_type(v, m)) {
436 webkit_web_policy_decision_download(p);
443 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
444 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
449 if (webkit_web_navigation_action_get_reason(n)
450 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
451 webkit_web_policy_decision_ignore(p);
452 arg.v = (void *)webkit_network_request_get_uri(r);
453 newwindow(NULL, &arg, 0);
460 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
466 destroyclient(Client *c)
470 webkit_web_view_stop_loading(c->view);
471 gtk_widget_destroy(GTK_WIDGET(c->view));
472 gtk_widget_destroy(c->scroll);
473 gtk_widget_destroy(c->vbox);
474 gtk_widget_destroy(c->win);
476 for (p = clients; p && p->next != c; p = p->next)
488 destroywin(GtkWidget* w, Client *c)
494 die(const char *errstr, ...)
498 va_start(ap, errstr);
499 vfprintf(stderr, errstr, ap);
505 find(Client *c, const Arg *arg)
509 s = getatom(c, AtomFind);
510 gboolean forward = *(gboolean *)arg;
511 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
515 fullscreen(Client *c, const Arg *arg)
518 gtk_window_unfullscreen(GTK_WINDOW(c->win));
520 gtk_window_fullscreen(GTK_WINDOW(c->win));
521 c->fullscreen = !c->fullscreen;
525 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
526 WebKitGeolocationPolicyDecision *d, Client *c)
528 if (allowgeolocation)
529 webkit_geolocation_policy_allow(d);
531 webkit_geolocation_policy_deny(d);
535 getatom(Client *c, int a)
537 static char buf[BUFSIZ];
540 unsigned long ldummy;
541 unsigned char *p = NULL;
543 XGetWindowProperty(dpy, c->xid,
544 atoms[a], 0L, BUFSIZ, False, XA_STRING,
545 &adummy, &idummy, &ldummy, &ldummy, &p);
547 strncpy(buf, (char *)p, LENGTH(buf)-1);
560 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
566 getstyle(const char *uri)
570 if (stylefile != NULL)
573 for (i = 0; i < LENGTH(styles); i++) {
574 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
576 return styles[i].style;
583 setstyle(Client *c, const char *style)
585 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
587 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
591 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
595 webkit_web_view_stop_loading(w);
596 arg = (Arg)PLUMB((char *)uri);
601 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
606 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
612 inspector(Client *c, const Arg *arg)
614 if (enableinspector) {
616 webkit_web_inspector_close(c->inspector);
618 webkit_web_inspector_show(c->inspector);
623 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
625 return WEBKIT_WEB_VIEW(webkit_web_view_new());
629 inspector_show(WebKitWebInspector *i, Client *c)
636 w = webkit_web_inspector_get_web_view(i);
637 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
638 gtk_widget_show(GTK_WIDGET(w));
639 c->isinspecting = true;
645 inspector_close(WebKitWebInspector *i, Client *c)
649 if (!c->isinspecting)
652 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
654 gtk_widget_destroy(w);
655 c->isinspecting = false;
661 inspector_finished(WebKitWebInspector *i, Client *c)
663 g_free(c->inspector);
667 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
671 gboolean processed = FALSE;
673 mods = CLEANMASK(mods);
674 key = gdk_keyval_to_lower(key);
676 for (i = 0; i < LENGTH(keys); i++) {
677 if (key == keys[i].keyval
678 && mods == keys[i].mod
680 keys[i].func(c, &(keys[i].arg));
689 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
692 c->linkhover = copystr(&c->linkhover, l);
693 } else if (c->linkhover) {
701 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
703 WebKitWebFrame *frame;
704 WebKitWebDataSource *src;
705 WebKitNetworkRequest *request;
709 switch (webkit_web_view_get_load_status (c->view)) {
710 case WEBKIT_LOAD_COMMITTED:
712 if (strstr(uri, "https://") == uri) {
713 frame = webkit_web_view_get_main_frame(c->view);
714 src = webkit_web_frame_get_data_source(frame);
715 request = webkit_web_data_source_get_request(src);
716 msg = webkit_network_request_get_message(request);
717 c->sslfailed = !(soup_message_get_flags(msg)
718 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
720 setatom(c, AtomUri, uri);
723 setstyle(c, getstyle(uri));
725 case WEBKIT_LOAD_FINISHED:
735 loaduri(Client *c, const Arg *arg)
738 const char *uri = (char *)arg->v;
739 Arg a = { .b = FALSE };
742 if (strcmp(uri, "") == 0)
745 /* In case it's a file path. */
746 if (stat(uri, &st) == 0) {
747 rp = realpath(uri, NULL);
748 u = g_strdup_printf("file://%s", rp);
751 u = g_strrstr(uri, "://") ? g_strdup(uri)
752 : g_strdup_printf("http://%s", uri);
755 setatom(c, AtomUri, uri);
757 /* prevents endless loop */
758 if (strcmp(u, geturi(c)) == 0) {
761 webkit_web_view_load_uri(c->view, u);
763 c->title = copystr(&c->title, u);
770 navigate(Client *c, const Arg *arg)
772 int steps = *(int *)arg;
773 webkit_web_view_go_back_or_forward(c->view, steps);
780 WebKitWebSettings *settings;
781 GdkGeometry hints = { 1, 1 };
787 if (!(c = calloc(1, sizeof(Client))))
788 die("Cannot malloc!\n");
795 c->win = gtk_plug_new(embed);
797 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
799 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
800 * is always correct, so we should still call this function.
801 * But when doing so, we *must* differentiate between a
802 * WM_CLASS and a resource on the window. By convention, the
803 * window class (WM_CLASS) is capped, while the resource is in
804 * lowercase. Both these values come as a pair.
806 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
808 /* TA: 20091214: And set the role here as well -- so that
809 * sessions can pick this up.
811 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
813 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
814 g_signal_connect(G_OBJECT(c->win),
816 G_CALLBACK(destroywin), c);
817 g_signal_connect(G_OBJECT(c->win),
818 "leave_notify_event",
819 G_CALLBACK(titlechangeleave), c);
825 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
827 g_signal_connect(G_OBJECT(c->view),
829 G_CALLBACK(titlechange), c);
830 g_signal_connect(G_OBJECT(c->view),
831 "hovering-over-link",
832 G_CALLBACK(linkhover), c);
833 g_signal_connect(G_OBJECT(c->view),
834 "geolocation-policy-decision-requested",
835 G_CALLBACK(geopolicyrequested), c);
836 g_signal_connect(G_OBJECT(c->view),
838 G_CALLBACK(createwindow), c);
839 g_signal_connect(G_OBJECT(c->view),
840 "new-window-policy-decision-requested",
841 G_CALLBACK(decidewindow), c);
842 g_signal_connect(G_OBJECT(c->view),
843 "mime-type-policy-decision-requested",
844 G_CALLBACK(decidedownload), c);
845 g_signal_connect(G_OBJECT(c->view),
846 "window-object-cleared",
847 G_CALLBACK(windowobjectcleared), c);
848 g_signal_connect(G_OBJECT(c->view),
849 "notify::load-status",
850 G_CALLBACK(loadstatuschange), c);
851 g_signal_connect(G_OBJECT(c->view),
853 G_CALLBACK(progresschange), c);
854 g_signal_connect(G_OBJECT(c->view),
855 "download-requested",
856 G_CALLBACK(initdownload), c);
857 g_signal_connect(G_OBJECT(c->view),
858 "button-release-event",
859 G_CALLBACK(buttonrelease), c);
860 g_signal_connect(G_OBJECT(c->view),
862 G_CALLBACK(contextmenu), c);
863 g_signal_connect(G_OBJECT(c->view),
864 "resource-request-starting",
865 G_CALLBACK(beforerequest), c);
866 g_signal_connect(G_OBJECT(c->view),
867 "should-show-delete-interface-for-element",
868 G_CALLBACK(deletion_interface), c);
871 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
874 gtk_widget_grab_focus(GTK_WIDGET(c->view));
875 gtk_widget_show(GTK_WIDGET(c->view));
876 gtk_widget_show(c->win);
877 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
878 c->xid = gdk_x11_window_get_xid(gwin);
879 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
881 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
882 gdk_window_add_filter(gwin, processx, c);
883 webkit_web_view_set_full_content_zoom(c->view, TRUE);
887 settings = webkit_web_view_get_settings(c->view);
888 if (!(ua = getenv("SURF_USERAGENT")))
890 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
891 g_object_set(G_OBJECT(settings),
892 "auto-load-images", loadimages, NULL);
893 g_object_set(G_OBJECT(settings),
894 "enable-plugins", enableplugins, NULL);
895 g_object_set(G_OBJECT(settings),
896 "enable-scripts", enablescripts, NULL);
897 g_object_set(G_OBJECT(settings),
898 "enable-spatial-navigation", enablespatialbrowsing, NULL);
899 g_object_set(G_OBJECT(settings),
900 "enable-developer-extras", enableinspector, NULL);
901 g_object_set(G_OBJECT(settings),
902 "enable-default-context-menu", kioskmode ^ 1, NULL);
903 g_object_set(G_OBJECT(settings),
904 "default-font-size", defaultfontsize, NULL);
905 g_object_set(G_OBJECT(settings),
906 "resizable-text-areas", 1, NULL);
908 setstyle(c, getstyle("about:blank"));
911 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
912 * This ensures websites are not unusably small with a high DPI screen.
913 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
916 screen = gdk_window_get_screen(gwin);
917 dpi = gdk_screen_get_resolution(screen);
919 g_object_set(G_OBJECT(settings),
920 "enforce-96-dpi", true, NULL);
921 webkit_web_view_set_zoom_level(c->view, dpi/96);
924 /* This might conflict with _zoomto96dpi_. */
925 if (zoomlevel != 1.0)
926 webkit_web_view_set_zoom_level(c->view, zoomlevel);
928 if (enableinspector) {
929 c->inspector = webkit_web_view_get_inspector(c->view);
930 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
931 G_CALLBACK(inspector_new), c);
932 g_signal_connect(G_OBJECT(c->inspector), "show-window",
933 G_CALLBACK(inspector_show), c);
934 g_signal_connect(G_OBJECT(c->inspector), "close-window",
935 G_CALLBACK(inspector_close), c);
936 g_signal_connect(G_OBJECT(c->inspector), "finished",
937 G_CALLBACK(inspector_finished), c);
938 c->isinspecting = false;
944 setatom(c, AtomFind, "");
945 setatom(c, AtomUri, "about:blank");
947 webkit_web_view_set_transparent(c->view, TRUE);
953 gdk_display_sync(gtk_widget_get_display(c->win));
954 printf("%lu\n", c->xid);
956 if (fclose(stdout) != 0) {
957 die("Error closing stdout");
965 newwindow(Client *c, const Arg *arg, gboolean noembed)
968 const char *cmd[18], *uri;
969 const Arg a = { .v = (void *)cmd };
974 cmd[i++] = cookiepolicies;
975 if (!enablescrollbars)
977 if (embed && !noembed) {
979 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
982 if (!allowgeolocation)
997 cmd[i++] = cookiefile;
999 uri = arg->v ? (char *)arg->v : c->linkhover;
1007 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1008 gboolean keyboard, Client *c)
1010 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1012 for (GList *l = items; l; l = l->next)
1013 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1020 menuactivate(GtkMenuItem *item, Client *c)
1023 * context-menu-action-2000 open link
1024 * context-menu-action-1 open link in window
1025 * context-menu-action-2 download linked file
1026 * context-menu-action-3 copy link location
1027 * context-menu-action-7 copy image address
1028 * context-menu-action-13 reload
1029 * context-menu-action-10 back
1030 * context-menu-action-11 forward
1031 * context-menu-action-12 stop
1034 const gchar *name, *uri;
1035 GtkClipboard *prisel, *clpbrd;
1037 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1041 if (!g_strcmp0(name, "context-menu-action-3")) {
1042 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1043 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1044 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1045 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1046 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1047 uri = gtk_clipboard_wait_for_text(clpbrd);
1049 gtk_clipboard_set_text(prisel, uri, -1);
1054 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1056 Arg arg = {.v = text };
1058 loaduri((Client *) d, &arg);
1062 print(Client *c, const Arg *arg)
1064 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1068 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1070 Client *c = (Client *)d;
1074 if (((XEvent *)e)->type == PropertyNotify) {
1075 ev = &((XEvent *)e)->xproperty;
1076 if (ev->state == PropertyNewValue) {
1077 if (ev->atom == atoms[AtomFind]) {
1081 return GDK_FILTER_REMOVE;
1082 } else if (ev->atom == atoms[AtomGo]) {
1083 arg.v = getatom(c, AtomGo);
1086 return GDK_FILTER_REMOVE;
1090 return GDK_FILTER_CONTINUE;
1094 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1096 c->progress = webkit_web_view_get_progress(c->view) * 100;
1101 linkopen(Client *c, const Arg *arg)
1103 newwindow(NULL, arg, 1);
1107 linkopenembed(Client *c, const Arg *arg)
1109 newwindow(NULL, arg, 0);
1113 reload(Client *c, const Arg *arg)
1115 gboolean nocache = *(gboolean *)arg;
1117 webkit_web_view_reload_bypass_cache(c->view);
1119 webkit_web_view_reload(c->view);
1123 scroll_h(Client *c, const Arg *arg)
1125 scroll(gtk_scrolled_window_get_hadjustment(
1126 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1130 scroll_v(Client *c, const Arg *arg)
1132 scroll(gtk_scrolled_window_get_vadjustment(
1133 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1137 scroll(GtkAdjustment *a, const Arg *arg)
1141 v = gtk_adjustment_get_value(a);
1145 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1150 v += gtk_adjustment_get_step_increment(a) * arg->i;
1154 v = MIN(v, gtk_adjustment_get_upper(a) -
1155 gtk_adjustment_get_page_size(a));
1156 gtk_adjustment_set_value(a, v);
1160 setatom(Client *c, int a, const char *v)
1163 XChangeProperty(dpy, c->xid,
1164 atoms[a], XA_STRING, 8, PropModeReplace,
1165 (unsigned char *)v, strlen(v) + 1);
1172 char *styledirfile, *stylepath;
1173 WebKitWebContext *context;
1174 GError *error = NULL;
1176 /* clean up any zombies immediately */
1178 gtk_init(NULL, NULL);
1180 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1183 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1184 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1185 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1187 /* dirs and files */
1188 cookiefile = buildfile(cookiefile);
1189 scriptfile = buildfile(scriptfile);
1190 cachefolder = buildpath(cachefolder);
1191 if (stylefile == NULL) {
1192 styledir = buildpath(styledir);
1193 for (i = 0; i < LENGTH(styles); i++) {
1194 if (regcomp(&(styles[i].re), styles[i].regex,
1197 "Could not compile regex: %s\n",
1199 styles[i].regex = NULL;
1201 styledirfile = g_strconcat(styledir, "/",
1202 styles[i].style, NULL);
1203 stylepath = buildfile(styledirfile);
1204 styles[i].style = g_strconcat("file://", stylepath,
1206 g_free(styledirfile);
1211 stylepath = buildfile(stylefile);
1212 stylefile = g_strconcat("file://", stylepath, NULL);
1217 webkit_cookie_manager_set_persistent_storage(
1218 webkit_web_context_get_cookie_manager(context), cookiefile,
1219 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1220 webkit_cookie_manager_set_accept_policy(
1221 webkit_web_context_get_cookie_manager(context),
1222 cookiepolicy_get());
1225 webkit_web_context_set_cache_model(context, enablecache ?
1226 WEBKIT_CACHE_MODEL_WEB_BROWSER :
1227 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1230 webkit_web_context_set_tls_errors_policy(context, strictssl ?
1231 WEBKIT_TLS_ERRORS_POLICY_FAIL :
1232 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1238 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1239 die("Can't install SIGCHLD handler");
1240 while (0 < waitpid(-1, NULL, WNOHANG));
1244 spawn(Client *c, const Arg *arg)
1248 close(ConnectionNumber(dpy));
1250 execvp(((char **)arg->v)[0], (char **)arg->v);
1251 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1258 eval(Client *c, const Arg *arg)
1260 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1261 evalscript(webkit_web_frame_get_global_context(frame),
1262 ((char **)arg->v)[0], "");
1266 stop(Client *c, const Arg *arg)
1268 webkit_web_view_stop_loading(c->view);
1272 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1274 const gchar *t = webkit_web_view_get_title(view);
1276 c->title = copystr(&c->title, t);
1282 titlechangeleave(void *a, void *b, Client *c)
1284 c->linkhover = NULL;
1289 toggle(Client *c, const Arg *arg)
1291 WebKitWebSettings *settings;
1292 char *name = (char *)arg->v;
1294 Arg a = { .b = FALSE };
1296 settings = webkit_web_view_get_settings(c->view);
1297 g_object_get(G_OBJECT(settings), name, &value, NULL);
1298 g_object_set(G_OBJECT(settings), name, !value, NULL);
1304 togglecookiepolicy(Client *c, const Arg *arg)
1307 cookiepolicy %= strlen(cookiepolicies);
1309 webkit_cookie_manager_set_accept_policy(
1310 webkit_web_context_get_cookie_manager(
1311 webkit_web_view_get_context(c->view)),
1312 cookiepolicy_get());
1315 /* Do not reload. */
1319 togglegeolocation(Client *c, const Arg *arg)
1321 Arg a = { .b = FALSE };
1323 allowgeolocation ^= 1;
1328 twitch(Client *c, const Arg *arg)
1333 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1336 v = gtk_adjustment_get_value(a);
1341 v = MIN(v, gtk_adjustment_get_upper(a) -
1342 gtk_adjustment_get_page_size(a));
1343 gtk_adjustment_set_value(a, v);
1347 togglescrollbars(Client *c, const Arg *arg)
1349 GtkPolicyType vspolicy;
1352 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1355 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1356 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1360 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1361 GTK_POLICY_AUTOMATIC,
1362 GTK_POLICY_AUTOMATIC);
1371 togglestyle(Client *c, const Arg *arg)
1373 enablestyle = !enablestyle;
1374 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1380 gettogglestat(Client *c)
1384 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1386 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1388 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1390 togglestat[p++] = value? 'C': 'c';
1392 togglestat[p++] = allowgeolocation? 'G': 'g';
1394 togglestat[p++] = enablediskcache? 'D': 'd';
1396 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1397 togglestat[p++] = value? 'I': 'i';
1399 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1400 togglestat[p++] = value? 'S': 's';
1402 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1403 togglestat[p++] = value? 'V': 'v';
1405 togglestat[p++] = enablestyle ? 'M': 'm';
1407 togglestat[p] = '\0';
1411 getpagestat(Client *c)
1413 const char *uri = geturi(c);
1415 if (strstr(uri, "https://") == uri)
1416 pagestat[0] = c->sslfailed ? 'U' : 'T';
1424 updatetitle(Client *c)
1428 if (showindicators) {
1433 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1435 } else if (c->progress != 100) {
1436 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1437 togglestat, pagestat,
1438 c->title == NULL ? "" : c->title);
1440 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1441 c->title == NULL ? "" : c->title);
1444 gtk_window_set_title(GTK_WINDOW(c->win), t);
1447 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1453 updatewinid(Client *c)
1455 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1461 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1462 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1463 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1467 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1468 JSObjectRef win, Client *c)
1474 zoom(Client *c, const Arg *arg)
1479 webkit_web_view_zoom_out(c->view);
1480 } else if (arg->i > 0) {
1482 webkit_web_view_zoom_in(c->view);
1486 webkit_web_view_set_zoom_level(c->view, 1.0);
1491 main(int argc, char *argv[])
1496 memset(&arg, 0, sizeof(arg));
1498 /* command line args */
1501 cookiepolicies = EARGF(usage());
1504 enablescrollbars = 0;
1507 enablescrollbars = 1;
1510 cookiefile = EARGF(usage());
1513 enablediskcache = 0;
1516 enablediskcache = 1;
1519 embed = strtol(EARGF(usage()), NULL, 0);
1522 runinfullscreen = 0;
1525 runinfullscreen = 1;
1528 allowgeolocation = 0;
1531 allowgeolocation = 1;
1552 enableinspector = 0;
1555 enableinspector = 1;
1564 scriptfile = EARGF(usage());
1573 stylefile = EARGF(usage());
1576 useragent = EARGF(usage());
1579 die("surf-"VERSION", ©2009-2015 surf engineers, "
1580 "see LICENSE for details\n");
1585 zoomlevel = strtof(EARGF(usage()), NULL);
1596 loaduri(clients, &arg);
1603 return EXIT_SUCCESS;