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;
99 static SoupCache *diskcache = NULL;
101 static void addaccelgroup(Client *c);
102 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
103 WebKitWebResource *r, WebKitNetworkRequest *req,
104 WebKitNetworkResponse *resp, Client *c);
105 static char *buildfile(const char *path);
106 static char *buildpath(const char *path);
107 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
108 static void cleanup(void);
109 static void clipboard(Client *c, const Arg *arg);
111 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
112 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
114 static char *copystr(char **str, const char *src);
115 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
117 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
118 WebKitNetworkRequest *r, gchar *m,
119 WebKitWebPolicyDecision *p, Client *c);
120 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
121 WebKitNetworkRequest *r, WebKitWebNavigationAction
122 *n, WebKitWebPolicyDecision *p, Client *c);
123 static gboolean deletion_interface(WebKitWebView *view,
124 WebKitDOMHTMLElement *arg1, Client *c);
125 static void destroyclient(Client *c);
126 static void destroywin(GtkWidget* w, Client *c);
127 static void die(const char *errstr, ...);
128 static void eval(Client *c, const Arg *arg);
129 static void find(Client *c, const Arg *arg);
130 static void fullscreen(Client *c, const Arg *arg);
131 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
132 WebKitGeolocationPolicyDecision *d, Client *c);
133 static const char *getatom(Client *c, int a);
134 static void gettogglestat(Client *c);
135 static void getpagestat(Client *c);
136 static char *geturi(Client *c);
137 static const gchar *getstyle(const char *uri);
138 static void setstyle(Client *c, const char *style);
140 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
142 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
144 static void inspector(Client *c, const Arg *arg);
145 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
147 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
148 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
149 static void inspector_finished(WebKitWebInspector *i, Client *c);
151 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
152 GdkModifierType mods, Client *c);
153 static void linkhover(WebKitWebView *v, const char* t, const char* l,
155 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
157 static void loaduri(Client *c, const Arg *arg);
158 static void navigate(Client *c, const Arg *arg);
159 static Client *newclient(void);
160 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
161 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
162 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
163 WebKitHitTestResult *target, gboolean keyboard,
165 static void menuactivate(GtkMenuItem *item, Client *c);
166 static void print(Client *c, const Arg *arg);
167 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
169 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
170 static void linkopen(Client *c, const Arg *arg);
171 static void linkopenembed(Client *c, const Arg *arg);
172 static void reload(Client *c, const Arg *arg);
173 static void scroll_h(Client *c, const Arg *arg);
174 static void scroll_v(Client *c, const Arg *arg);
175 static void scroll(GtkAdjustment *a, const Arg *arg);
176 static void setatom(Client *c, int a, const char *v);
177 static void setup(void);
178 static void sigchld(int unused);
179 static void spawn(Client *c, const Arg *arg);
180 static void stop(Client *c, const Arg *arg);
181 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
182 static void titlechangeleave(void *a, void *b, Client *c);
183 static void toggle(Client *c, const Arg *arg);
184 static void togglecookiepolicy(Client *c, const Arg *arg);
185 static void togglegeolocation(Client *c, const Arg *arg);
186 static void togglescrollbars(Client *c, const Arg *arg);
187 static void togglestyle(Client *c, const Arg *arg);
188 static void updatetitle(Client *c);
189 static void updatewinid(Client *c);
190 static void usage(void);
191 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
192 JSContextRef js, JSObjectRef win, Client *c);
193 static void zoom(Client *c, const Arg *arg);
195 /* configuration, allows nested code to access above variables */
199 addaccelgroup(Client *c)
202 GtkAccelGroup *group = gtk_accel_group_new();
205 for (i = 0; i < LENGTH(keys); i++) {
206 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
207 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
210 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
214 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
215 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
218 const gchar *uri = webkit_network_request_get_uri(req);
221 if (g_str_has_suffix(uri, "/favicon.ico"))
222 webkit_network_request_set_uri(req, "about:blank");
224 if (!g_str_has_prefix(uri, "http://")
225 && !g_str_has_prefix(uri, "https://")
226 && !g_str_has_prefix(uri, "about:")
227 && !g_str_has_prefix(uri, "file://")
228 && !g_str_has_prefix(uri, "data:")
229 && !g_str_has_prefix(uri, "blob:")
230 && strlen(uri) > 0) {
231 for (i = 0; i < strlen(uri); i++) {
232 if (!g_ascii_isprint(uri[i])) {
238 handleplumb(c, w, uri);
243 buildfile(const char *path)
245 char *dname, *bname, *bpath, *fpath;
248 dname = g_path_get_dirname(path);
249 bname = g_path_get_basename(path);
251 bpath = buildpath(dname);
254 fpath = g_build_filename(bpath, bname, NULL);
258 if (!(f = fopen(fpath, "a")))
259 die("Could not open file: %s\n", fpath);
261 g_chmod(fpath, 0600); /* always */
268 buildpath(const char *path)
271 char *apath, *name, *p, *fpath;
273 if (path[0] == '~') {
274 if (path[1] == '/' || path[1] == '\0') {
275 p = (char *)&path[1];
276 pw = getpwuid(getuid());
278 if ((p = strchr(path, '/')))
279 name = g_strndup(&path[1], --p - path);
281 name = g_strdup(&path[1]);
283 if (!(pw = getpwnam(name))) {
284 die("Can't get user %s home directory: %s.\n",
289 apath = g_build_filename(pw->pw_dir, p, NULL);
291 apath = g_strdup(path);
294 /* creating directory */
295 if (g_mkdir_with_parents(apath, 0700) < 0)
296 die("Could not access directory: %s\n", apath);
298 fpath = realpath(apath, NULL);
305 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
307 WebKitHitTestResultContext context;
308 WebKitHitTestResult *result;
312 result = webkit_web_view_get_hit_test_result(web, e);
313 g_object_get(result, "context", &context, NULL);
314 g_object_get(result, "link-uri", &arg.v, NULL);
315 for (i = 0; i < LENGTH(buttons); i++) {
316 if (context & buttons[i].click
317 && e->button == buttons[i].button
318 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
319 && buttons[i].func) {
320 buttons[i].func(c, buttons[i].click == ClkLink
321 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
332 soup_cache_flush(diskcache);
333 soup_cache_dump(diskcache);
336 destroyclient(clients);
342 WebKitCookieAcceptPolicy
343 cookiepolicy_get(void)
345 switch (cookiepolicies[cookiepolicy]) {
347 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
349 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
355 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
359 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
362 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
364 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
366 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
375 evalscript(JSContextRef js, char *script, char* scriptname)
377 JSStringRef jsscript, jsscriptname;
378 JSValueRef exception = NULL;
380 jsscript = JSStringCreateWithUTF8CString(script);
381 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
382 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
383 jsscriptname, 0, &exception);
384 JSStringRelease(jsscript);
385 JSStringRelease(jsscriptname);
389 runscript(WebKitWebFrame *frame)
394 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
395 evalscript(webkit_web_frame_get_global_context(frame), script,
401 clipboard(Client *c, const Arg *arg)
403 gboolean paste = *(gboolean *)arg;
406 gtk_clipboard_request_text(gtk_clipboard_get(
407 GDK_SELECTION_PRIMARY),
410 gtk_clipboard_set_text(gtk_clipboard_get(
411 GDK_SELECTION_PRIMARY), c->linkhover
412 ? c->linkhover : geturi(c), -1);
417 copystr(char **str, const char *src)
430 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
432 Client *n = newclient();
437 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
438 gchar *m, WebKitWebPolicyDecision *p, Client *c)
440 if (!webkit_web_view_can_show_mime_type(v, m)) {
441 webkit_web_policy_decision_download(p);
448 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
449 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
454 if (webkit_web_navigation_action_get_reason(n)
455 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
456 webkit_web_policy_decision_ignore(p);
457 arg.v = (void *)webkit_network_request_get_uri(r);
458 newwindow(NULL, &arg, 0);
465 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
471 destroyclient(Client *c)
475 webkit_web_view_stop_loading(c->view);
476 gtk_widget_destroy(GTK_WIDGET(c->view));
477 gtk_widget_destroy(c->scroll);
478 gtk_widget_destroy(c->vbox);
479 gtk_widget_destroy(c->win);
481 for (p = clients; p && p->next != c; p = p->next)
493 destroywin(GtkWidget* w, Client *c)
499 die(const char *errstr, ...)
503 va_start(ap, errstr);
504 vfprintf(stderr, errstr, ap);
510 find(Client *c, const Arg *arg)
514 s = getatom(c, AtomFind);
515 gboolean forward = *(gboolean *)arg;
516 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
520 fullscreen(Client *c, const Arg *arg)
523 gtk_window_unfullscreen(GTK_WINDOW(c->win));
525 gtk_window_fullscreen(GTK_WINDOW(c->win));
526 c->fullscreen = !c->fullscreen;
530 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
531 WebKitGeolocationPolicyDecision *d, Client *c)
533 if (allowgeolocation)
534 webkit_geolocation_policy_allow(d);
536 webkit_geolocation_policy_deny(d);
540 getatom(Client *c, int a)
542 static char buf[BUFSIZ];
545 unsigned long ldummy;
546 unsigned char *p = NULL;
548 XGetWindowProperty(dpy, c->xid,
549 atoms[a], 0L, BUFSIZ, False, XA_STRING,
550 &adummy, &idummy, &ldummy, &ldummy, &p);
552 strncpy(buf, (char *)p, LENGTH(buf)-1);
565 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
571 getstyle(const char *uri)
575 if (stylefile != NULL)
578 for (i = 0; i < LENGTH(styles); i++) {
579 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
581 return styles[i].style;
588 setstyle(Client *c, const char *style)
590 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
592 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
596 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
600 webkit_web_view_stop_loading(w);
601 arg = (Arg)PLUMB((char *)uri);
606 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
611 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
617 inspector(Client *c, const Arg *arg)
619 if (enableinspector) {
621 webkit_web_inspector_close(c->inspector);
623 webkit_web_inspector_show(c->inspector);
628 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
630 return WEBKIT_WEB_VIEW(webkit_web_view_new());
634 inspector_show(WebKitWebInspector *i, Client *c)
641 w = webkit_web_inspector_get_web_view(i);
642 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
643 gtk_widget_show(GTK_WIDGET(w));
644 c->isinspecting = true;
650 inspector_close(WebKitWebInspector *i, Client *c)
654 if (!c->isinspecting)
657 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
659 gtk_widget_destroy(w);
660 c->isinspecting = false;
666 inspector_finished(WebKitWebInspector *i, Client *c)
668 g_free(c->inspector);
672 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
676 gboolean processed = FALSE;
678 mods = CLEANMASK(mods);
679 key = gdk_keyval_to_lower(key);
681 for (i = 0; i < LENGTH(keys); i++) {
682 if (key == keys[i].keyval
683 && mods == keys[i].mod
685 keys[i].func(c, &(keys[i].arg));
694 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
697 c->linkhover = copystr(&c->linkhover, l);
698 } else if (c->linkhover) {
706 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
708 WebKitWebFrame *frame;
709 WebKitWebDataSource *src;
710 WebKitNetworkRequest *request;
714 switch (webkit_web_view_get_load_status (c->view)) {
715 case WEBKIT_LOAD_COMMITTED:
717 if (strstr(uri, "https://") == uri) {
718 frame = webkit_web_view_get_main_frame(c->view);
719 src = webkit_web_frame_get_data_source(frame);
720 request = webkit_web_data_source_get_request(src);
721 msg = webkit_network_request_get_message(request);
722 c->sslfailed = !(soup_message_get_flags(msg)
723 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
725 setatom(c, AtomUri, uri);
728 setstyle(c, getstyle(uri));
730 case WEBKIT_LOAD_FINISHED:
734 soup_cache_flush(diskcache);
735 soup_cache_dump(diskcache);
744 loaduri(Client *c, const Arg *arg)
747 const char *uri = (char *)arg->v;
748 Arg a = { .b = FALSE };
751 if (strcmp(uri, "") == 0)
754 /* In case it's a file path. */
755 if (stat(uri, &st) == 0) {
756 rp = realpath(uri, NULL);
757 u = g_strdup_printf("file://%s", rp);
760 u = g_strrstr(uri, "://") ? g_strdup(uri)
761 : g_strdup_printf("http://%s", uri);
764 setatom(c, AtomUri, uri);
766 /* prevents endless loop */
767 if (strcmp(u, geturi(c)) == 0) {
770 webkit_web_view_load_uri(c->view, u);
772 c->title = copystr(&c->title, u);
779 navigate(Client *c, const Arg *arg)
781 int steps = *(int *)arg;
782 webkit_web_view_go_back_or_forward(c->view, steps);
789 WebKitWebSettings *settings;
790 WebKitWebFrame *frame;
791 GdkGeometry hints = { 1, 1 };
797 if (!(c = calloc(1, sizeof(Client))))
798 die("Cannot malloc!\n");
805 c->win = gtk_plug_new(embed);
807 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
809 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
810 * is always correct, so we should still call this function.
811 * But when doing so, we *must* differentiate between a
812 * WM_CLASS and a resource on the window. By convention, the
813 * window class (WM_CLASS) is capped, while the resource is in
814 * lowercase. Both these values come as a pair.
816 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
818 /* TA: 20091214: And set the role here as well -- so that
819 * sessions can pick this up.
821 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
823 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
824 g_signal_connect(G_OBJECT(c->win),
826 G_CALLBACK(destroywin), c);
827 g_signal_connect(G_OBJECT(c->win),
828 "leave_notify_event",
829 G_CALLBACK(titlechangeleave), c);
835 c->pane = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
838 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
839 gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
842 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
844 g_signal_connect(G_OBJECT(c->view),
846 G_CALLBACK(titlechange), c);
847 g_signal_connect(G_OBJECT(c->view),
848 "hovering-over-link",
849 G_CALLBACK(linkhover), c);
850 g_signal_connect(G_OBJECT(c->view),
851 "geolocation-policy-decision-requested",
852 G_CALLBACK(geopolicyrequested), c);
853 g_signal_connect(G_OBJECT(c->view),
855 G_CALLBACK(createwindow), c);
856 g_signal_connect(G_OBJECT(c->view),
857 "new-window-policy-decision-requested",
858 G_CALLBACK(decidewindow), c);
859 g_signal_connect(G_OBJECT(c->view),
860 "mime-type-policy-decision-requested",
861 G_CALLBACK(decidedownload), c);
862 g_signal_connect(G_OBJECT(c->view),
863 "window-object-cleared",
864 G_CALLBACK(windowobjectcleared), c);
865 g_signal_connect(G_OBJECT(c->view),
866 "notify::load-status",
867 G_CALLBACK(loadstatuschange), c);
868 g_signal_connect(G_OBJECT(c->view),
870 G_CALLBACK(progresschange), c);
871 g_signal_connect(G_OBJECT(c->view),
872 "download-requested",
873 G_CALLBACK(initdownload), c);
874 g_signal_connect(G_OBJECT(c->view),
875 "button-release-event",
876 G_CALLBACK(buttonrelease), c);
877 g_signal_connect(G_OBJECT(c->view),
879 G_CALLBACK(contextmenu), c);
880 g_signal_connect(G_OBJECT(c->view),
881 "resource-request-starting",
882 G_CALLBACK(beforerequest), c);
883 g_signal_connect(G_OBJECT(c->view),
884 "should-show-delete-interface-for-element",
885 G_CALLBACK(deletion_interface), c);
887 /* Scrolled Window */
888 c->scroll = gtk_scrolled_window_new(NULL, NULL);
890 frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
891 g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
892 G_CALLBACK(gtk_true), NULL);
894 if (!enablescrollbars) {
895 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
899 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
900 GTK_POLICY_AUTOMATIC,
901 GTK_POLICY_AUTOMATIC);
905 gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
906 gtk_container_add(GTK_CONTAINER(c->win), c->pane);
907 gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
910 gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
912 gtk_widget_grab_focus(GTK_WIDGET(c->view));
913 gtk_widget_show(c->pane);
914 gtk_widget_show(c->vbox);
915 gtk_widget_show(c->scroll);
916 gtk_widget_show(GTK_WIDGET(c->view));
917 gtk_widget_show(c->win);
918 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
919 c->xid = gdk_x11_window_get_xid(gwin);
920 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
922 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
923 gdk_window_add_filter(gwin, processx, c);
924 webkit_web_view_set_full_content_zoom(c->view, TRUE);
928 settings = webkit_web_view_get_settings(c->view);
929 if (!(ua = getenv("SURF_USERAGENT")))
931 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
932 g_object_set(G_OBJECT(settings),
933 "auto-load-images", loadimages, NULL);
934 g_object_set(G_OBJECT(settings),
935 "enable-plugins", enableplugins, NULL);
936 g_object_set(G_OBJECT(settings),
937 "enable-scripts", enablescripts, NULL);
938 g_object_set(G_OBJECT(settings),
939 "enable-spatial-navigation", enablespatialbrowsing, NULL);
940 g_object_set(G_OBJECT(settings),
941 "enable-developer-extras", enableinspector, NULL);
942 g_object_set(G_OBJECT(settings),
943 "enable-default-context-menu", kioskmode ^ 1, NULL);
944 g_object_set(G_OBJECT(settings),
945 "default-font-size", defaultfontsize, NULL);
946 g_object_set(G_OBJECT(settings),
947 "resizable-text-areas", 1, NULL);
949 setstyle(c, getstyle("about:blank"));
952 * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
953 * This ensures websites are not unusably small with a high DPI screen.
954 * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
957 screen = gdk_window_get_screen(gwin);
958 dpi = gdk_screen_get_resolution(screen);
960 g_object_set(G_OBJECT(settings),
961 "enforce-96-dpi", true, NULL);
962 webkit_web_view_set_zoom_level(c->view, dpi/96);
965 /* This might conflict with _zoomto96dpi_. */
966 if (zoomlevel != 1.0)
967 webkit_web_view_set_zoom_level(c->view, zoomlevel);
969 if (enableinspector) {
970 c->inspector = webkit_web_view_get_inspector(c->view);
971 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
972 G_CALLBACK(inspector_new), c);
973 g_signal_connect(G_OBJECT(c->inspector), "show-window",
974 G_CALLBACK(inspector_show), c);
975 g_signal_connect(G_OBJECT(c->inspector), "close-window",
976 G_CALLBACK(inspector_close), c);
977 g_signal_connect(G_OBJECT(c->inspector), "finished",
978 G_CALLBACK(inspector_finished), c);
979 c->isinspecting = false;
985 setatom(c, AtomFind, "");
986 setatom(c, AtomUri, "about:blank");
988 webkit_web_view_set_transparent(c->view, TRUE);
994 gdk_display_sync(gtk_widget_get_display(c->win));
995 printf("%lu\n", c->xid);
997 if (fclose(stdout) != 0) {
998 die("Error closing stdout");
1006 newwindow(Client *c, const Arg *arg, gboolean noembed)
1009 const char *cmd[18], *uri;
1010 const Arg a = { .v = (void *)cmd };
1015 cmd[i++] = cookiepolicies;
1016 if (!enablescrollbars)
1018 if (embed && !noembed) {
1020 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1023 if (!allowgeolocation)
1035 if (enablediskcache)
1038 cmd[i++] = cookiefile;
1040 uri = arg->v ? (char *)arg->v : c->linkhover;
1048 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1049 gboolean keyboard, Client *c)
1051 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1053 for (GList *l = items; l; l = l->next)
1054 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1061 menuactivate(GtkMenuItem *item, Client *c)
1064 * context-menu-action-2000 open link
1065 * context-menu-action-1 open link in window
1066 * context-menu-action-2 download linked file
1067 * context-menu-action-3 copy link location
1068 * context-menu-action-7 copy image address
1069 * context-menu-action-13 reload
1070 * context-menu-action-10 back
1071 * context-menu-action-11 forward
1072 * context-menu-action-12 stop
1075 const gchar *name, *uri;
1076 GtkClipboard *prisel, *clpbrd;
1078 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1082 if (!g_strcmp0(name, "context-menu-action-3")) {
1083 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1084 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1085 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1086 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1087 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1088 uri = gtk_clipboard_wait_for_text(clpbrd);
1090 gtk_clipboard_set_text(prisel, uri, -1);
1095 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1097 Arg arg = {.v = text };
1099 loaduri((Client *) d, &arg);
1103 print(Client *c, const Arg *arg)
1105 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1109 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1111 Client *c = (Client *)d;
1115 if (((XEvent *)e)->type == PropertyNotify) {
1116 ev = &((XEvent *)e)->xproperty;
1117 if (ev->state == PropertyNewValue) {
1118 if (ev->atom == atoms[AtomFind]) {
1122 return GDK_FILTER_REMOVE;
1123 } else if (ev->atom == atoms[AtomGo]) {
1124 arg.v = getatom(c, AtomGo);
1127 return GDK_FILTER_REMOVE;
1131 return GDK_FILTER_CONTINUE;
1135 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1137 c->progress = webkit_web_view_get_progress(c->view) * 100;
1142 linkopen(Client *c, const Arg *arg)
1144 newwindow(NULL, arg, 1);
1148 linkopenembed(Client *c, const Arg *arg)
1150 newwindow(NULL, arg, 0);
1154 reload(Client *c, const Arg *arg)
1156 gboolean nocache = *(gboolean *)arg;
1158 webkit_web_view_reload_bypass_cache(c->view);
1160 webkit_web_view_reload(c->view);
1164 scroll_h(Client *c, const Arg *arg)
1166 scroll(gtk_scrolled_window_get_hadjustment(
1167 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1171 scroll_v(Client *c, const Arg *arg)
1173 scroll(gtk_scrolled_window_get_vadjustment(
1174 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1178 scroll(GtkAdjustment *a, const Arg *arg)
1182 v = gtk_adjustment_get_value(a);
1186 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1191 v += gtk_adjustment_get_step_increment(a) * arg->i;
1195 v = MIN(v, gtk_adjustment_get_upper(a) -
1196 gtk_adjustment_get_page_size(a));
1197 gtk_adjustment_set_value(a, v);
1201 setatom(Client *c, int a, const char *v)
1204 XChangeProperty(dpy, c->xid,
1205 atoms[a], XA_STRING, 8, PropModeReplace,
1206 (unsigned char *)v, strlen(v) + 1);
1213 char *styledirfile, *stylepath;
1215 WebKitWebContext *context;
1216 GError *error = NULL;
1218 /* clean up any zombies immediately */
1220 gtk_init(NULL, NULL);
1222 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1225 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1226 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1227 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1229 /* dirs and files */
1230 cookiefile = buildfile(cookiefile);
1231 scriptfile = buildfile(scriptfile);
1232 cachefolder = buildpath(cachefolder);
1233 if (stylefile == NULL) {
1234 styledir = buildpath(styledir);
1235 for (i = 0; i < LENGTH(styles); i++) {
1236 if (regcomp(&(styles[i].re), styles[i].regex,
1239 "Could not compile regex: %s\n",
1241 styles[i].regex = NULL;
1243 styledirfile = g_strconcat(styledir, "/",
1244 styles[i].style, NULL);
1245 stylepath = buildfile(styledirfile);
1246 styles[i].style = g_strconcat("file://", stylepath,
1248 g_free(styledirfile);
1253 stylepath = buildfile(stylefile);
1254 stylefile = g_strconcat("file://", stylepath, NULL);
1258 /* request handler */
1259 s = webkit_get_default_session();
1262 webkit_cookie_manager_set_persistent_storage(
1263 webkit_web_context_get_cookie_manager(context), cookiefile,
1264 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1265 webkit_cookie_manager_set_accept_policy(
1266 webkit_web_context_get_cookie_manager(context),
1267 cookiepolicy_get());
1270 if (enablediskcache) {
1271 diskcache = soup_cache_new(cachefolder,
1272 SOUP_CACHE_SINGLE_USER);
1273 soup_cache_set_max_size(diskcache, diskcachebytes);
1274 soup_cache_load(diskcache);
1275 soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
1279 tlsdb = g_tls_file_database_new(cafile, &error);
1282 g_warning("Error loading SSL database %s: %s", cafile,
1284 g_error_free(error);
1286 g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
1287 g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
1293 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1294 die("Can't install SIGCHLD handler");
1295 while (0 < waitpid(-1, NULL, WNOHANG));
1299 spawn(Client *c, const Arg *arg)
1303 close(ConnectionNumber(dpy));
1305 execvp(((char **)arg->v)[0], (char **)arg->v);
1306 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1313 eval(Client *c, const Arg *arg)
1315 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1316 evalscript(webkit_web_frame_get_global_context(frame),
1317 ((char **)arg->v)[0], "");
1321 stop(Client *c, const Arg *arg)
1323 webkit_web_view_stop_loading(c->view);
1327 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1329 const gchar *t = webkit_web_view_get_title(view);
1331 c->title = copystr(&c->title, t);
1337 titlechangeleave(void *a, void *b, Client *c)
1339 c->linkhover = NULL;
1344 toggle(Client *c, const Arg *arg)
1346 WebKitWebSettings *settings;
1347 char *name = (char *)arg->v;
1349 Arg a = { .b = FALSE };
1351 settings = webkit_web_view_get_settings(c->view);
1352 g_object_get(G_OBJECT(settings), name, &value, NULL);
1353 g_object_set(G_OBJECT(settings), name, !value, NULL);
1359 togglecookiepolicy(Client *c, const Arg *arg)
1362 cookiepolicy %= strlen(cookiepolicies);
1364 webkit_cookie_manager_set_accept_policy(
1365 webkit_web_context_get_cookie_manager(
1366 webkit_web_view_get_context(c->view)),
1367 cookiepolicy_get());
1370 /* Do not reload. */
1374 togglegeolocation(Client *c, const Arg *arg)
1376 Arg a = { .b = FALSE };
1378 allowgeolocation ^= 1;
1383 twitch(Client *c, const Arg *arg)
1388 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1391 v = gtk_adjustment_get_value(a);
1396 v = MIN(v, gtk_adjustment_get_upper(a) -
1397 gtk_adjustment_get_page_size(a));
1398 gtk_adjustment_set_value(a, v);
1402 togglescrollbars(Client *c, const Arg *arg)
1404 GtkPolicyType vspolicy;
1407 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1410 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1411 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1415 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1416 GTK_POLICY_AUTOMATIC,
1417 GTK_POLICY_AUTOMATIC);
1426 togglestyle(Client *c, const Arg *arg)
1428 enablestyle = !enablestyle;
1429 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1435 gettogglestat(Client *c)
1439 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1441 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1443 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1445 togglestat[p++] = value? 'C': 'c';
1447 togglestat[p++] = allowgeolocation? 'G': 'g';
1449 togglestat[p++] = enablediskcache? 'D': 'd';
1451 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1452 togglestat[p++] = value? 'I': 'i';
1454 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1455 togglestat[p++] = value? 'S': 's';
1457 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1458 togglestat[p++] = value? 'V': 'v';
1460 togglestat[p++] = enablestyle ? 'M': 'm';
1462 togglestat[p] = '\0';
1466 getpagestat(Client *c)
1468 const char *uri = geturi(c);
1470 if (strstr(uri, "https://") == uri)
1471 pagestat[0] = c->sslfailed ? 'U' : 'T';
1479 updatetitle(Client *c)
1483 if (showindicators) {
1488 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1490 } else if (c->progress != 100) {
1491 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1492 togglestat, pagestat,
1493 c->title == NULL ? "" : c->title);
1495 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1496 c->title == NULL ? "" : c->title);
1499 gtk_window_set_title(GTK_WINDOW(c->win), t);
1502 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1508 updatewinid(Client *c)
1510 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1516 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1517 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1518 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1522 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1523 JSObjectRef win, Client *c)
1529 zoom(Client *c, const Arg *arg)
1534 webkit_web_view_zoom_out(c->view);
1535 } else if (arg->i > 0) {
1537 webkit_web_view_zoom_in(c->view);
1541 webkit_web_view_set_zoom_level(c->view, 1.0);
1546 main(int argc, char *argv[])
1551 memset(&arg, 0, sizeof(arg));
1553 /* command line args */
1556 cookiepolicies = EARGF(usage());
1559 enablescrollbars = 0;
1562 enablescrollbars = 1;
1565 cookiefile = EARGF(usage());
1568 enablediskcache = 0;
1571 enablediskcache = 1;
1574 embed = strtol(EARGF(usage()), NULL, 0);
1577 runinfullscreen = 0;
1580 runinfullscreen = 1;
1583 allowgeolocation = 0;
1586 allowgeolocation = 1;
1607 enableinspector = 0;
1610 enableinspector = 1;
1619 scriptfile = EARGF(usage());
1628 stylefile = EARGF(usage());
1631 useragent = EARGF(usage());
1634 die("surf-"VERSION", ©2009-2015 surf engineers, "
1635 "see LICENSE for details\n");
1640 zoomlevel = strtof(EARGF(usage()), NULL);
1651 loaduri(clients, &arg);
1658 return EXIT_SUCCESS;