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 {
56 GtkWidget *win, *scroll, *vbox, *pane;
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 WebKitWebFrame *frame;
782 GdkGeometry hints = { 1, 1 };
788 if (!(c = calloc(1, sizeof(Client))))
789 die("Cannot malloc!\n");
796 c->win = gtk_plug_new(embed);
798 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
800 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
801 * is always correct, so we should still call this function.
802 * But when doing so, we *must* differentiate between a
803 * WM_CLASS and a resource on the window. By convention, the
804 * window class (WM_CLASS) is capped, while the resource is in
805 * lowercase. Both these values come as a pair.
807 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
809 /* TA: 20091214: And set the role here as well -- so that
810 * sessions can pick this up.
812 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
814 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
815 g_signal_connect(G_OBJECT(c->win),
817 G_CALLBACK(destroywin), c);
818 g_signal_connect(G_OBJECT(c->win),
819 "leave_notify_event",
820 G_CALLBACK(titlechangeleave), c);
826 c->pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
829 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
830 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
833 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
835 g_signal_connect(G_OBJECT(c->view),
837 G_CALLBACK(titlechange), c);
838 g_signal_connect(G_OBJECT(c->view),
839 "hovering-over-link",
840 G_CALLBACK(linkhover), c);
841 g_signal_connect(G_OBJECT(c->view),
842 "geolocation-policy-decision-requested",
843 G_CALLBACK(geopolicyrequested), c);
844 g_signal_connect(G_OBJECT(c->view),
846 G_CALLBACK(createwindow), c);
847 g_signal_connect(G_OBJECT(c->view),
848 "new-window-policy-decision-requested",
849 G_CALLBACK(decidewindow), c);
850 g_signal_connect(G_OBJECT(c->view),
851 "mime-type-policy-decision-requested",
852 G_CALLBACK(decidedownload), c);
853 g_signal_connect(G_OBJECT(c->view),
854 "window-object-cleared",
855 G_CALLBACK(windowobjectcleared), c);
856 g_signal_connect(G_OBJECT(c->view),
857 "notify::load-status",
858 G_CALLBACK(loadstatuschange), c);
859 g_signal_connect(G_OBJECT(c->view),
861 G_CALLBACK(progresschange), c);
862 g_signal_connect(G_OBJECT(c->view),
863 "download-requested",
864 G_CALLBACK(initdownload), c);
865 g_signal_connect(G_OBJECT(c->view),
866 "button-release-event",
867 G_CALLBACK(buttonrelease), c);
868 g_signal_connect(G_OBJECT(c->view),
870 G_CALLBACK(contextmenu), c);
871 g_signal_connect(G_OBJECT(c->view),
872 "resource-request-starting",
873 G_CALLBACK(beforerequest), c);
874 g_signal_connect(G_OBJECT(c->view),
875 "should-show-delete-interface-for-element",
876 G_CALLBACK(deletion_interface), c);
878 /* Scrolled Window */
879 c->scroll = gtk_scrolled_window_new(NULL, NULL);
881 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
882 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
883 G_CALLBACK(gtk_true), NULL);
885 if (!enablescrollbars) {
886 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
890 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
891 GTK_POLICY_AUTOMATIC,
892 GTK_POLICY_AUTOMATIC);
896 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
897 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
898 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
901 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
903 gtk_widget_grab_focus(GTK_WIDGET(c->view));
904 gtk_widget_show(c->pane);
905 gtk_widget_show(c->vbox);
906 gtk_widget_show(c->scroll);
907 gtk_widget_show(GTK_WIDGET(c->view));
908 gtk_widget_show(c->win);
909 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
910 c->xid = gdk_x11_window_get_xid(gwin);
911 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
913 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
914 gdk_window_add_filter(gwin, processx, c);
915 webkit_web_view_set_full_content_zoom(c->view, TRUE);
919 settings = webkit_web_view_get_settings(c->view);
920 if (!(ua = getenv("SURF_USERAGENT")))
922 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
923 g_object_set(G_OBJECT(settings),
924 "auto-load-images", loadimages, NULL);
925 g_object_set(G_OBJECT(settings),
926 "enable-plugins", enableplugins, NULL);
927 g_object_set(G_OBJECT(settings),
928 "enable-scripts", enablescripts, NULL);
929 g_object_set(G_OBJECT(settings),
930 "enable-spatial-navigation", enablespatialbrowsing, NULL);
931 g_object_set(G_OBJECT(settings),
932 "enable-developer-extras", enableinspector, NULL);
933 g_object_set(G_OBJECT(settings),
934 "enable-default-context-menu", kioskmode ^ 1, NULL);
935 g_object_set(G_OBJECT(settings),
936 "default-font-size", defaultfontsize, NULL);
937 g_object_set(G_OBJECT(settings),
938 "resizable-text-areas", 1, NULL);
940 setstyle(c, getstyle("about:blank"));
943 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
944 * This ensures websites are not unusably small with a high DPI screen.
945 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
948 screen = gdk_window_get_screen(gwin);
949 dpi = gdk_screen_get_resolution(screen);
951 g_object_set(G_OBJECT(settings),
952 "enforce-96-dpi", true, NULL);
953 webkit_web_view_set_zoom_level(c->view, dpi/96);
956 /* This might conflict with _zoomto96dpi_. */
957 if (zoomlevel != 1.0)
958 webkit_web_view_set_zoom_level(c->view, zoomlevel);
960 if (enableinspector) {
961 c->inspector = webkit_web_view_get_inspector(c->view);
962 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
963 G_CALLBACK(inspector_new), c);
964 g_signal_connect(G_OBJECT(c->inspector), "show-window",
965 G_CALLBACK(inspector_show), c);
966 g_signal_connect(G_OBJECT(c->inspector), "close-window",
967 G_CALLBACK(inspector_close), c);
968 g_signal_connect(G_OBJECT(c->inspector), "finished",
969 G_CALLBACK(inspector_finished), c);
970 c->isinspecting = false;
976 setatom(c, AtomFind, "");
977 setatom(c, AtomUri, "about:blank");
979 webkit_web_view_set_transparent(c->view, TRUE);
985 gdk_display_sync(gtk_widget_get_display(c->win));
986 printf("%lu\n", c->xid);
988 if (fclose(stdout) != 0) {
989 die("Error closing stdout");
997 newwindow(Client *c, const Arg *arg, gboolean noembed)
1000 const char *cmd[18], *uri;
1001 const Arg a = { .v = (void *)cmd };
1006 cmd[i++] = cookiepolicies;
1007 if (!enablescrollbars)
1009 if (embed && !noembed) {
1011 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1014 if (!allowgeolocation)
1026 if (enablediskcache)
1029 cmd[i++] = cookiefile;
1031 uri = arg->v ? (char *)arg->v : c->linkhover;
1039 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1040 gboolean keyboard, Client *c)
1042 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1044 for (GList *l = items; l; l = l->next)
1045 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1052 menuactivate(GtkMenuItem *item, Client *c)
1055 * context-menu-action-2000 open link
1056 * context-menu-action-1 open link in window
1057 * context-menu-action-2 download linked file
1058 * context-menu-action-3 copy link location
1059 * context-menu-action-7 copy image address
1060 * context-menu-action-13 reload
1061 * context-menu-action-10 back
1062 * context-menu-action-11 forward
1063 * context-menu-action-12 stop
1066 const gchar *name, *uri;
1067 GtkClipboard *prisel, *clpbrd;
1069 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1073 if (!g_strcmp0(name, "context-menu-action-3")) {
1074 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1075 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1076 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1077 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1078 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1079 uri = gtk_clipboard_wait_for_text(clpbrd);
1081 gtk_clipboard_set_text(prisel, uri, -1);
1086 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1088 Arg arg = {.v = text };
1090 loaduri((Client *) d, &arg);
1094 print(Client *c, const Arg *arg)
1096 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1100 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1102 Client *c = (Client *)d;
1106 if (((XEvent *)e)->type == PropertyNotify) {
1107 ev = &((XEvent *)e)->xproperty;
1108 if (ev->state == PropertyNewValue) {
1109 if (ev->atom == atoms[AtomFind]) {
1113 return GDK_FILTER_REMOVE;
1114 } else if (ev->atom == atoms[AtomGo]) {
1115 arg.v = getatom(c, AtomGo);
1118 return GDK_FILTER_REMOVE;
1122 return GDK_FILTER_CONTINUE;
1126 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1128 c->progress = webkit_web_view_get_progress(c->view) * 100;
1133 linkopen(Client *c, const Arg *arg)
1135 newwindow(NULL, arg, 1);
1139 linkopenembed(Client *c, const Arg *arg)
1141 newwindow(NULL, arg, 0);
1145 reload(Client *c, const Arg *arg)
1147 gboolean nocache = *(gboolean *)arg;
1149 webkit_web_view_reload_bypass_cache(c->view);
1151 webkit_web_view_reload(c->view);
1155 scroll_h(Client *c, const Arg *arg)
1157 scroll(gtk_scrolled_window_get_hadjustment(
1158 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1162 scroll_v(Client *c, const Arg *arg)
1164 scroll(gtk_scrolled_window_get_vadjustment(
1165 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1169 scroll(GtkAdjustment *a, const Arg *arg)
1173 v = gtk_adjustment_get_value(a);
1177 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1182 v += gtk_adjustment_get_step_increment(a) * arg->i;
1186 v = MIN(v, gtk_adjustment_get_upper(a) -
1187 gtk_adjustment_get_page_size(a));
1188 gtk_adjustment_set_value(a, v);
1192 setatom(Client *c, int a, const char *v)
1195 XChangeProperty(dpy, c->xid,
1196 atoms[a], XA_STRING, 8, PropModeReplace,
1197 (unsigned char *)v, strlen(v) + 1);
1204 char *styledirfile, *stylepath;
1205 WebKitWebContext *context;
1206 GError *error = NULL;
1208 /* clean up any zombies immediately */
1210 gtk_init(NULL, NULL);
1212 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1215 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1216 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1217 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1219 /* dirs and files */
1220 cookiefile = buildfile(cookiefile);
1221 scriptfile = buildfile(scriptfile);
1222 cachefolder = buildpath(cachefolder);
1223 if (stylefile == NULL) {
1224 styledir = buildpath(styledir);
1225 for (i = 0; i < LENGTH(styles); i++) {
1226 if (regcomp(&(styles[i].re), styles[i].regex,
1229 "Could not compile regex: %s\n",
1231 styles[i].regex = NULL;
1233 styledirfile = g_strconcat(styledir, "/",
1234 styles[i].style, NULL);
1235 stylepath = buildfile(styledirfile);
1236 styles[i].style = g_strconcat("file://", stylepath,
1238 g_free(styledirfile);
1243 stylepath = buildfile(stylefile);
1244 stylefile = g_strconcat("file://", stylepath, NULL);
1249 webkit_cookie_manager_set_persistent_storage(
1250 webkit_web_context_get_cookie_manager(context), cookiefile,
1251 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1252 webkit_cookie_manager_set_accept_policy(
1253 webkit_web_context_get_cookie_manager(context),
1254 cookiepolicy_get());
1257 webkit_web_context_set_cache_model(context, enablecache ?
1258 WEBKIT_CACHE_MODEL_WEB_BROWSER :
1259 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1262 tlsdb = g_tls_file_database_new(cafile, &error);
1265 g_warning("Error loading SSL database %s: %s", cafile,
1267 g_error_free(error);
1269 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1270 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1276 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1277 die("Can't install SIGCHLD handler");
1278 while (0 < waitpid(-1, NULL, WNOHANG));
1282 spawn(Client *c, const Arg *arg)
1286 close(ConnectionNumber(dpy));
1288 execvp(((char **)arg->v)[0], (char **)arg->v);
1289 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1296 eval(Client *c, const Arg *arg)
1298 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1299 evalscript(webkit_web_frame_get_global_context(frame),
1300 ((char **)arg->v)[0], "");
1304 stop(Client *c, const Arg *arg)
1306 webkit_web_view_stop_loading(c->view);
1310 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1312 const gchar *t = webkit_web_view_get_title(view);
1314 c->title = copystr(&c->title, t);
1320 titlechangeleave(void *a, void *b, Client *c)
1322 c->linkhover = NULL;
1327 toggle(Client *c, const Arg *arg)
1329 WebKitWebSettings *settings;
1330 char *name = (char *)arg->v;
1332 Arg a = { .b = FALSE };
1334 settings = webkit_web_view_get_settings(c->view);
1335 g_object_get(G_OBJECT(settings), name, &value, NULL);
1336 g_object_set(G_OBJECT(settings), name, !value, NULL);
1342 togglecookiepolicy(Client *c, const Arg *arg)
1345 cookiepolicy %= strlen(cookiepolicies);
1347 webkit_cookie_manager_set_accept_policy(
1348 webkit_web_context_get_cookie_manager(
1349 webkit_web_view_get_context(c->view)),
1350 cookiepolicy_get());
1353 /* Do not reload. */
1357 togglegeolocation(Client *c, const Arg *arg)
1359 Arg a = { .b = FALSE };
1361 allowgeolocation ^= 1;
1366 twitch(Client *c, const Arg *arg)
1371 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1374 v = gtk_adjustment_get_value(a);
1379 v = MIN(v, gtk_adjustment_get_upper(a) -
1380 gtk_adjustment_get_page_size(a));
1381 gtk_adjustment_set_value(a, v);
1385 togglescrollbars(Client *c, const Arg *arg)
1387 GtkPolicyType vspolicy;
1390 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1393 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1394 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1398 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1399 GTK_POLICY_AUTOMATIC,
1400 GTK_POLICY_AUTOMATIC);
1409 togglestyle(Client *c, const Arg *arg)
1411 enablestyle = !enablestyle;
1412 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1418 gettogglestat(Client *c)
1422 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1424 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1426 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1428 togglestat[p++] = value? 'C': 'c';
1430 togglestat[p++] = allowgeolocation? 'G': 'g';
1432 togglestat[p++] = enablediskcache? 'D': 'd';
1434 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1435 togglestat[p++] = value? 'I': 'i';
1437 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1438 togglestat[p++] = value? 'S': 's';
1440 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1441 togglestat[p++] = value? 'V': 'v';
1443 togglestat[p++] = enablestyle ? 'M': 'm';
1445 togglestat[p] = '\0';
1449 getpagestat(Client *c)
1451 const char *uri = geturi(c);
1453 if (strstr(uri, "https://") == uri)
1454 pagestat[0] = c->sslfailed ? 'U' : 'T';
1462 updatetitle(Client *c)
1466 if (showindicators) {
1471 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1473 } else if (c->progress != 100) {
1474 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1475 togglestat, pagestat,
1476 c->title == NULL ? "" : c->title);
1478 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1479 c->title == NULL ? "" : c->title);
1482 gtk_window_set_title(GTK_WINDOW(c->win), t);
1485 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1491 updatewinid(Client *c)
1493 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1499 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1500 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1501 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1505 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1506 JSObjectRef win, Client *c)
1512 zoom(Client *c, const Arg *arg)
1517 webkit_web_view_zoom_out(c->view);
1518 } else if (arg->i > 0) {
1520 webkit_web_view_zoom_in(c->view);
1524 webkit_web_view_set_zoom_level(c->view, 1.0);
1529 main(int argc, char *argv[])
1534 memset(&arg, 0, sizeof(arg));
1536 /* command line args */
1539 cookiepolicies = EARGF(usage());
1542 enablescrollbars = 0;
1545 enablescrollbars = 1;
1548 cookiefile = EARGF(usage());
1551 enablediskcache = 0;
1554 enablediskcache = 1;
1557 embed = strtol(EARGF(usage()), NULL, 0);
1560 runinfullscreen = 0;
1563 runinfullscreen = 1;
1566 allowgeolocation = 0;
1569 allowgeolocation = 1;
1590 enableinspector = 0;
1593 enableinspector = 1;
1602 scriptfile = EARGF(usage());
1611 stylefile = EARGF(usage());
1614 useragent = EARGF(usage());
1617 die("surf-"VERSION", ©2009-2015 surf engineers, "
1618 "see LICENSE for details\n");
1623 zoomlevel = strtof(EARGF(usage()), NULL);
1634 loaduri(clients, &arg);
1641 return EXIT_SUCCESS;