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(Client *c);
159 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
160 static void showview(WebKitWebView *v, Client *c);
161 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
162 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
163 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
164 WebKitHitTestResult *target, gboolean keyboard,
166 static void menuactivate(GtkMenuItem *item, Client *c);
167 static void print(Client *c, const Arg *arg);
168 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
170 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
171 static void linkopen(Client *c, const Arg *arg);
172 static void linkopenembed(Client *c, const Arg *arg);
173 static void reload(Client *c, const Arg *arg);
174 static void scroll_h(Client *c, const Arg *arg);
175 static void scroll_v(Client *c, const Arg *arg);
176 static void scroll(GtkAdjustment *a, const Arg *arg);
177 static void setatom(Client *c, int a, const char *v);
178 static void setup(void);
179 static void sigchld(int unused);
180 static void spawn(Client *c, const Arg *arg);
181 static void stop(Client *c, const Arg *arg);
182 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
183 static void titlechangeleave(void *a, void *b, Client *c);
184 static void toggle(Client *c, const Arg *arg);
185 static void togglecookiepolicy(Client *c, const Arg *arg);
186 static void togglegeolocation(Client *c, const Arg *arg);
187 static void togglescrollbars(Client *c, const Arg *arg);
188 static void togglestyle(Client *c, const Arg *arg);
189 static void updatetitle(Client *c);
190 static void updatewinid(Client *c);
191 static void usage(void);
192 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
193 JSContextRef js, JSObjectRef win, Client *c);
194 static void zoom(Client *c, const Arg *arg);
196 /* configuration, allows nested code to access above variables */
200 addaccelgroup(Client *c)
203 GtkAccelGroup *group = gtk_accel_group_new();
206 for (i = 0; i < LENGTH(keys); i++) {
207 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
208 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
211 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
215 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
216 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
219 const gchar *uri = webkit_network_request_get_uri(req);
222 if (g_str_has_suffix(uri, "/favicon.ico"))
223 webkit_network_request_set_uri(req, "about:blank");
225 if (!g_str_has_prefix(uri, "http://")
226 && !g_str_has_prefix(uri, "https://")
227 && !g_str_has_prefix(uri, "about:")
228 && !g_str_has_prefix(uri, "file://")
229 && !g_str_has_prefix(uri, "data:")
230 && !g_str_has_prefix(uri, "blob:")
231 && strlen(uri) > 0) {
232 for (i = 0; i < strlen(uri); i++) {
233 if (!g_ascii_isprint(uri[i])) {
239 handleplumb(c, w, uri);
244 buildfile(const char *path)
246 char *dname, *bname, *bpath, *fpath;
249 dname = g_path_get_dirname(path);
250 bname = g_path_get_basename(path);
252 bpath = buildpath(dname);
255 fpath = g_build_filename(bpath, bname, NULL);
259 if (!(f = fopen(fpath, "a")))
260 die("Could not open file: %s\n", fpath);
262 g_chmod(fpath, 0600); /* always */
269 buildpath(const char *path)
272 char *apath, *name, *p, *fpath;
274 if (path[0] == '~') {
275 if (path[1] == '/' || path[1] == '\0') {
276 p = (char *)&path[1];
277 pw = getpwuid(getuid());
279 if ((p = strchr(path, '/')))
280 name = g_strndup(&path[1], --p - path);
282 name = g_strdup(&path[1]);
284 if (!(pw = getpwnam(name))) {
285 die("Can't get user %s home directory: %s.\n",
290 apath = g_build_filename(pw->pw_dir, p, NULL);
292 apath = g_strdup(path);
295 /* creating directory */
296 if (g_mkdir_with_parents(apath, 0700) < 0)
297 die("Could not access directory: %s\n", apath);
299 fpath = realpath(apath, NULL);
306 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
308 WebKitHitTestResultContext context;
309 WebKitHitTestResult *result;
313 result = webkit_web_view_get_hit_test_result(web, e);
314 g_object_get(result, "context", &context, NULL);
315 g_object_get(result, "link-uri", &arg.v, NULL);
316 for (i = 0; i < LENGTH(buttons); i++) {
317 if (context & buttons[i].click
318 && e->button == buttons[i].button
319 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
320 && buttons[i].func) {
321 buttons[i].func(c, buttons[i].click == ClkLink
322 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
333 destroyclient(clients);
339 WebKitCookieAcceptPolicy
340 cookiepolicy_get(void)
342 switch (cookiepolicies[cookiepolicy]) {
344 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
346 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
352 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
356 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
359 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
361 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
363 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
372 evalscript(JSContextRef js, char *script, char* scriptname)
374 JSStringRef jsscript, jsscriptname;
375 JSValueRef exception = NULL;
377 jsscript = JSStringCreateWithUTF8CString(script);
378 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
379 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
380 jsscriptname, 0, &exception);
381 JSStringRelease(jsscript);
382 JSStringRelease(jsscriptname);
386 runscript(WebKitWebFrame *frame)
391 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
392 evalscript(webkit_web_frame_get_global_context(frame), script,
398 clipboard(Client *c, const Arg *arg)
400 gboolean paste = *(gboolean *)arg;
403 gtk_clipboard_request_text(gtk_clipboard_get(
404 GDK_SELECTION_PRIMARY),
407 gtk_clipboard_set_text(gtk_clipboard_get(
408 GDK_SELECTION_PRIMARY), c->linkhover
409 ? c->linkhover : geturi(c), -1);
414 copystr(char **str, const char *src)
427 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
429 Client *n = newclient();
434 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
435 gchar *m, WebKitWebPolicyDecision *p, Client *c)
437 if (!webkit_web_view_can_show_mime_type(v, m)) {
438 webkit_web_policy_decision_download(p);
445 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
446 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
451 if (webkit_web_navigation_action_get_reason(n)
452 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
453 webkit_web_policy_decision_ignore(p);
454 arg.v = (void *)webkit_network_request_get_uri(r);
455 newwindow(NULL, &arg, 0);
462 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
468 destroyclient(Client *c)
472 webkit_web_view_stop_loading(c->view);
473 gtk_widget_destroy(GTK_WIDGET(c->view));
474 gtk_widget_destroy(c->scroll);
475 gtk_widget_destroy(c->vbox);
476 gtk_widget_destroy(c->win);
478 for (p = clients; p && p->next != c; p = p->next)
490 destroywin(GtkWidget* w, Client *c)
496 die(const char *errstr, ...)
500 va_start(ap, errstr);
501 vfprintf(stderr, errstr, ap);
507 find(Client *c, const Arg *arg)
511 s = getatom(c, AtomFind);
512 gboolean forward = *(gboolean *)arg;
513 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
517 fullscreen(Client *c, const Arg *arg)
520 gtk_window_unfullscreen(GTK_WINDOW(c->win));
522 gtk_window_fullscreen(GTK_WINDOW(c->win));
523 c->fullscreen = !c->fullscreen;
527 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
528 WebKitGeolocationPolicyDecision *d, Client *c)
530 if (allowgeolocation)
531 webkit_geolocation_policy_allow(d);
533 webkit_geolocation_policy_deny(d);
537 getatom(Client *c, int a)
539 static char buf[BUFSIZ];
542 unsigned long ldummy;
543 unsigned char *p = NULL;
545 XGetWindowProperty(dpy, c->xid,
546 atoms[a], 0L, BUFSIZ, False, XA_STRING,
547 &adummy, &idummy, &ldummy, &ldummy, &p);
549 strncpy(buf, (char *)p, LENGTH(buf)-1);
562 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
568 getstyle(const char *uri)
572 if (stylefile != NULL)
575 for (i = 0; i < LENGTH(styles); i++) {
576 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
578 return styles[i].style;
585 setstyle(Client *c, const char *style)
587 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
589 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
593 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
597 webkit_web_view_stop_loading(w);
598 arg = (Arg)PLUMB((char *)uri);
603 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
608 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
614 inspector(Client *c, const Arg *arg)
616 if (enableinspector) {
618 webkit_web_inspector_close(c->inspector);
620 webkit_web_inspector_show(c->inspector);
625 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
627 return WEBKIT_WEB_VIEW(webkit_web_view_new());
631 inspector_show(WebKitWebInspector *i, Client *c)
638 w = webkit_web_inspector_get_web_view(i);
639 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
640 gtk_widget_show(GTK_WIDGET(w));
641 c->isinspecting = true;
647 inspector_close(WebKitWebInspector *i, Client *c)
651 if (!c->isinspecting)
654 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
656 gtk_widget_destroy(w);
657 c->isinspecting = false;
663 inspector_finished(WebKitWebInspector *i, Client *c)
665 g_free(c->inspector);
669 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
673 gboolean processed = FALSE;
675 mods = CLEANMASK(mods);
676 key = gdk_keyval_to_lower(key);
678 for (i = 0; i < LENGTH(keys); i++) {
679 if (key == keys[i].keyval
680 && mods == keys[i].mod
682 keys[i].func(c, &(keys[i].arg));
691 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
694 c->linkhover = copystr(&c->linkhover, l);
695 } else if (c->linkhover) {
703 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
705 WebKitWebFrame *frame;
706 WebKitWebDataSource *src;
707 WebKitNetworkRequest *request;
711 switch (webkit_web_view_get_load_status (c->view)) {
712 case WEBKIT_LOAD_COMMITTED:
714 if (strstr(uri, "https://") == uri) {
715 frame = webkit_web_view_get_main_frame(c->view);
716 src = webkit_web_frame_get_data_source(frame);
717 request = webkit_web_data_source_get_request(src);
718 msg = webkit_network_request_get_message(request);
719 c->sslfailed = !(soup_message_get_flags(msg)
720 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
722 setatom(c, AtomUri, uri);
725 setstyle(c, getstyle(uri));
727 case WEBKIT_LOAD_FINISHED:
737 loaduri(Client *c, const Arg *arg)
740 const char *uri = (char *)arg->v;
741 Arg a = { .b = FALSE };
744 if (strcmp(uri, "") == 0)
747 /* In case it's a file path. */
748 if (stat(uri, &st) == 0) {
749 rp = realpath(uri, NULL);
750 u = g_strdup_printf("file://%s", rp);
753 u = g_strrstr(uri, "://") ? g_strdup(uri)
754 : g_strdup_printf("http://%s", uri);
757 setatom(c, AtomUri, uri);
759 /* prevents endless loop */
760 if (strcmp(u, geturi(c)) == 0) {
763 webkit_web_view_load_uri(c->view, u);
765 c->title = copystr(&c->title, u);
772 navigate(Client *c, const Arg *arg)
774 int steps = *(int *)arg;
775 webkit_web_view_go_back_or_forward(c->view, steps);
779 newclient(Client *rc)
784 if (!(c = calloc(1, sizeof(Client))))
785 die("Cannot malloc!\n");
793 c->view = newview(c, rc ? rc->view : NULL);
799 newview(Client *c, WebKitWebView *rv)
802 WebKitSettings *settings;
808 webkit_web_view_new_with_related_view(rv));
810 v = WEBKIT_WEB_VIEW(webkit_web_view_new());
812 settings = webkit_web_view_get_settings(v);
813 if (!(ua = getenv("SURF_USERAGENT")))
815 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
816 g_object_set(G_OBJECT(settings),
817 "auto-load-images", loadimages, NULL);
818 g_object_set(G_OBJECT(settings),
819 "enable-plugins", enableplugins, NULL);
820 g_object_set(G_OBJECT(settings),
821 "enable-scripts", enablescripts, NULL);
822 g_object_set(G_OBJECT(settings),
823 "enable-spatial-navigation", enablespatialbrowsing, NULL);
824 g_object_set(G_OBJECT(settings),
825 "enable-developer-extras", enableinspector, NULL);
826 g_object_set(G_OBJECT(settings),
827 "enable-default-context-menu", kioskmode ^ 1, NULL);
828 g_object_set(G_OBJECT(settings),
829 "default-font-size", defaultfontsize, NULL);
830 g_object_set(G_OBJECT(settings),
831 "resizable-text-areas", 1, NULL);
833 setstyle(c, getstyle("about:blank"));
835 if (enableinspector) {
836 c->inspector = webkit_web_view_get_inspector(v);
837 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
838 G_CALLBACK(inspector_new), c);
839 g_signal_connect(G_OBJECT(c->inspector), "show-window",
840 G_CALLBACK(inspector_show), c);
841 g_signal_connect(G_OBJECT(c->inspector), "close-window",
842 G_CALLBACK(inspector_close), c);
843 g_signal_connect(G_OBJECT(c->inspector), "finished",
844 G_CALLBACK(inspector_finished), c);
845 c->isinspecting = false;
849 g_signal_connect(G_OBJECT(v),
851 G_CALLBACK(titlechange), c);
852 g_signal_connect(G_OBJECT(v),
853 "hovering-over-link",
854 G_CALLBACK(linkhover), c);
855 g_signal_connect(G_OBJECT(v),
856 "geolocation-policy-decision-requested",
857 G_CALLBACK(geopolicyrequested), c);
858 g_signal_connect(G_OBJECT(v),
860 G_CALLBACK(createwindow), c);
861 g_signal_connect(G_OBJECT(v), "ready-to-show",
862 G_CALLBACK(showview), c);
863 g_signal_connect(G_OBJECT(v),
864 "new-window-policy-decision-requested",
865 G_CALLBACK(decidewindow), c);
866 g_signal_connect(G_OBJECT(v),
867 "mime-type-policy-decision-requested",
868 G_CALLBACK(decidedownload), c);
869 g_signal_connect(G_OBJECT(v),
870 "window-object-cleared",
871 G_CALLBACK(windowobjectcleared), c);
872 g_signal_connect(G_OBJECT(v),
873 "notify::load-status",
874 G_CALLBACK(loadstatuschange), c);
875 g_signal_connect(G_OBJECT(v),
877 G_CALLBACK(progresschange), c);
878 g_signal_connect(G_OBJECT(v),
879 "download-requested",
880 G_CALLBACK(initdownload), c);
881 g_signal_connect(G_OBJECT(v),
882 "button-release-event",
883 G_CALLBACK(buttonrelease), c);
884 g_signal_connect(G_OBJECT(v),
886 G_CALLBACK(contextmenu), c);
887 g_signal_connect(G_OBJECT(v),
888 "resource-request-starting",
889 G_CALLBACK(beforerequest), c);
890 g_signal_connect(G_OBJECT(v),
891 "should-show-delete-interface-for-element",
892 G_CALLBACK(deletion_interface), c);
898 showview(WebKitWebView *v, Client *c)
900 GdkGeometry hints = { 1, 1 };
901 GdkRGBA bgcolor = { 0 };
906 c->win = gtk_plug_new(embed);
908 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
910 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
911 * is always correct, so we should still call this function.
912 * But when doing so, we *must* differentiate between a
913 * WM_CLASS and a resource on the window. By convention, the
914 * window class (WM_CLASS) is capped, while the resource is in
915 * lowercase. Both these values come as a pair.
917 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
919 /* TA: 20091214: And set the role here as well -- so that
920 * sessions can pick this up.
922 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
924 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
925 g_signal_connect(G_OBJECT(c->win),
927 G_CALLBACK(destroywin), c);
928 g_signal_connect(G_OBJECT(c->win),
929 "leave_notify_event",
930 G_CALLBACK(titlechangeleave), c);
936 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
939 gtk_widget_grab_focus(GTK_WIDGET(c->view));
940 gtk_widget_show(GTK_WIDGET(c->view));
941 gtk_widget_show(c->win);
942 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
943 c->xid = gdk_x11_window_get_xid(gwin);
944 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
946 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
947 gdk_window_add_filter(gwin, processx, c);
951 /* This might conflict with _zoomto96dpi_. */
952 if (zoomlevel != 1.0)
953 webkit_web_view_set_zoom_level(c->view, zoomlevel);
958 setatom(c, AtomFind, "");
959 setatom(c, AtomUri, "about:blank");
961 webkit_web_view_set_background_color(c->view, &bgcolor);
964 gdk_display_sync(gtk_widget_get_display(c->win));
965 printf("%lu\n", c->xid);
967 if (fclose(stdout) != 0) {
968 die("Error closing stdout");
974 newwindow(Client *c, const Arg *arg, gboolean noembed)
977 const char *cmd[18], *uri;
978 const Arg a = { .v = (void *)cmd };
983 cmd[i++] = cookiepolicies;
984 if (!enablescrollbars)
986 if (embed && !noembed) {
988 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
991 if (!allowgeolocation)
1003 if (enablediskcache)
1006 cmd[i++] = cookiefile;
1008 uri = arg->v ? (char *)arg->v : c->linkhover;
1016 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1017 gboolean keyboard, Client *c)
1019 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1021 for (GList *l = items; l; l = l->next)
1022 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1029 menuactivate(GtkMenuItem *item, Client *c)
1032 * context-menu-action-2000 open link
1033 * context-menu-action-1 open link in window
1034 * context-menu-action-2 download linked file
1035 * context-menu-action-3 copy link location
1036 * context-menu-action-7 copy image address
1037 * context-menu-action-13 reload
1038 * context-menu-action-10 back
1039 * context-menu-action-11 forward
1040 * context-menu-action-12 stop
1043 const gchar *name, *uri;
1044 GtkClipboard *prisel, *clpbrd;
1046 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1050 if (!g_strcmp0(name, "context-menu-action-3")) {
1051 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1052 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1053 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1054 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1055 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1056 uri = gtk_clipboard_wait_for_text(clpbrd);
1058 gtk_clipboard_set_text(prisel, uri, -1);
1063 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1065 Arg arg = {.v = text };
1067 loaduri((Client *) d, &arg);
1071 print(Client *c, const Arg *arg)
1073 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1077 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1079 Client *c = (Client *)d;
1083 if (((XEvent *)e)->type == PropertyNotify) {
1084 ev = &((XEvent *)e)->xproperty;
1085 if (ev->state == PropertyNewValue) {
1086 if (ev->atom == atoms[AtomFind]) {
1090 return GDK_FILTER_REMOVE;
1091 } else if (ev->atom == atoms[AtomGo]) {
1092 arg.v = getatom(c, AtomGo);
1095 return GDK_FILTER_REMOVE;
1099 return GDK_FILTER_CONTINUE;
1103 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1105 c->progress = webkit_web_view_get_progress(c->view) * 100;
1110 linkopen(Client *c, const Arg *arg)
1112 newwindow(NULL, arg, 1);
1116 linkopenembed(Client *c, const Arg *arg)
1118 newwindow(NULL, arg, 0);
1122 reload(Client *c, const Arg *arg)
1124 gboolean nocache = *(gboolean *)arg;
1126 webkit_web_view_reload_bypass_cache(c->view);
1128 webkit_web_view_reload(c->view);
1132 scroll_h(Client *c, const Arg *arg)
1134 scroll(gtk_scrolled_window_get_hadjustment(
1135 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1139 scroll_v(Client *c, const Arg *arg)
1141 scroll(gtk_scrolled_window_get_vadjustment(
1142 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1146 scroll(GtkAdjustment *a, const Arg *arg)
1150 v = gtk_adjustment_get_value(a);
1154 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1159 v += gtk_adjustment_get_step_increment(a) * arg->i;
1163 v = MIN(v, gtk_adjustment_get_upper(a) -
1164 gtk_adjustment_get_page_size(a));
1165 gtk_adjustment_set_value(a, v);
1169 setatom(Client *c, int a, const char *v)
1172 XChangeProperty(dpy, c->xid,
1173 atoms[a], XA_STRING, 8, PropModeReplace,
1174 (unsigned char *)v, strlen(v) + 1);
1181 char *styledirfile, *stylepath;
1182 WebKitWebContext *context;
1183 GError *error = NULL;
1185 /* clean up any zombies immediately */
1187 gtk_init(NULL, NULL);
1189 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1192 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1193 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1194 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1196 /* dirs and files */
1197 cookiefile = buildfile(cookiefile);
1198 scriptfile = buildfile(scriptfile);
1199 cachefolder = buildpath(cachefolder);
1200 if (stylefile == NULL) {
1201 styledir = buildpath(styledir);
1202 for (i = 0; i < LENGTH(styles); i++) {
1203 if (regcomp(&(styles[i].re), styles[i].regex,
1206 "Could not compile regex: %s\n",
1208 styles[i].regex = NULL;
1210 styledirfile = g_strconcat(styledir, "/",
1211 styles[i].style, NULL);
1212 stylepath = buildfile(styledirfile);
1213 styles[i].style = g_strconcat("file://", stylepath,
1215 g_free(styledirfile);
1220 stylepath = buildfile(stylefile);
1221 stylefile = g_strconcat("file://", stylepath, NULL);
1226 webkit_cookie_manager_set_persistent_storage(
1227 webkit_web_context_get_cookie_manager(context), cookiefile,
1228 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1229 webkit_cookie_manager_set_accept_policy(
1230 webkit_web_context_get_cookie_manager(context),
1231 cookiepolicy_get());
1233 /* rendering process model, can be a shared unique one or one for each
1235 webkit_web_context_set_process_model(context,
1236 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1239 webkit_web_context_set_cache_model(context, enablecache ?
1240 WEBKIT_CACHE_MODEL_WEB_BROWSER :
1241 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1244 webkit_web_context_set_tls_errors_policy(context, strictssl ?
1245 WEBKIT_TLS_ERRORS_POLICY_FAIL :
1246 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1252 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1253 die("Can't install SIGCHLD handler");
1254 while (0 < waitpid(-1, NULL, WNOHANG));
1258 spawn(Client *c, const Arg *arg)
1262 close(ConnectionNumber(dpy));
1264 execvp(((char **)arg->v)[0], (char **)arg->v);
1265 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1272 eval(Client *c, const Arg *arg)
1274 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1275 evalscript(webkit_web_frame_get_global_context(frame),
1276 ((char **)arg->v)[0], "");
1280 stop(Client *c, const Arg *arg)
1282 webkit_web_view_stop_loading(c->view);
1286 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1288 const gchar *t = webkit_web_view_get_title(view);
1290 c->title = copystr(&c->title, t);
1296 titlechangeleave(void *a, void *b, Client *c)
1298 c->linkhover = NULL;
1303 toggle(Client *c, const Arg *arg)
1305 WebKitWebSettings *settings;
1306 char *name = (char *)arg->v;
1308 Arg a = { .b = FALSE };
1310 settings = webkit_web_view_get_settings(c->view);
1311 g_object_get(G_OBJECT(settings), name, &value, NULL);
1312 g_object_set(G_OBJECT(settings), name, !value, NULL);
1318 togglecookiepolicy(Client *c, const Arg *arg)
1321 cookiepolicy %= strlen(cookiepolicies);
1323 webkit_cookie_manager_set_accept_policy(
1324 webkit_web_context_get_cookie_manager(
1325 webkit_web_view_get_context(c->view)),
1326 cookiepolicy_get());
1329 /* Do not reload. */
1333 togglegeolocation(Client *c, const Arg *arg)
1335 Arg a = { .b = FALSE };
1337 allowgeolocation ^= 1;
1342 twitch(Client *c, const Arg *arg)
1347 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1350 v = gtk_adjustment_get_value(a);
1355 v = MIN(v, gtk_adjustment_get_upper(a) -
1356 gtk_adjustment_get_page_size(a));
1357 gtk_adjustment_set_value(a, v);
1361 togglescrollbars(Client *c, const Arg *arg)
1363 GtkPolicyType vspolicy;
1366 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1369 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1370 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1374 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1375 GTK_POLICY_AUTOMATIC,
1376 GTK_POLICY_AUTOMATIC);
1385 togglestyle(Client *c, const Arg *arg)
1387 enablestyle = !enablestyle;
1388 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1394 gettogglestat(Client *c)
1398 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1400 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1402 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1404 togglestat[p++] = value? 'C': 'c';
1406 togglestat[p++] = allowgeolocation? 'G': 'g';
1408 togglestat[p++] = enablediskcache? 'D': 'd';
1410 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1411 togglestat[p++] = value? 'I': 'i';
1413 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1414 togglestat[p++] = value? 'S': 's';
1416 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1417 togglestat[p++] = value? 'V': 'v';
1419 togglestat[p++] = enablestyle ? 'M': 'm';
1421 togglestat[p] = '\0';
1425 getpagestat(Client *c)
1427 const char *uri = geturi(c);
1429 if (strstr(uri, "https://") == uri)
1430 pagestat[0] = c->sslfailed ? 'U' : 'T';
1438 updatetitle(Client *c)
1442 if (showindicators) {
1447 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1449 } else if (c->progress != 100) {
1450 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1451 togglestat, pagestat,
1452 c->title == NULL ? "" : c->title);
1454 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1455 c->title == NULL ? "" : c->title);
1458 gtk_window_set_title(GTK_WINDOW(c->win), t);
1461 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1467 updatewinid(Client *c)
1469 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1475 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1476 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1477 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1481 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1482 JSObjectRef win, Client *c)
1488 zoom(Client *c, const Arg *arg)
1493 webkit_web_view_zoom_out(c->view);
1494 } else if (arg->i > 0) {
1496 webkit_web_view_zoom_in(c->view);
1500 webkit_web_view_set_zoom_level(c->view, 1.0);
1505 main(int argc, char *argv[])
1510 memset(&arg, 0, sizeof(arg));
1512 /* command line args */
1515 cookiepolicies = EARGF(usage());
1518 enablescrollbars = 0;
1521 enablescrollbars = 1;
1524 cookiefile = EARGF(usage());
1527 enablediskcache = 0;
1530 enablediskcache = 1;
1533 embed = strtol(EARGF(usage()), NULL, 0);
1536 runinfullscreen = 0;
1539 runinfullscreen = 1;
1542 allowgeolocation = 0;
1545 allowgeolocation = 1;
1566 enableinspector = 0;
1569 enableinspector = 1;
1578 scriptfile = EARGF(usage());
1587 stylefile = EARGF(usage());
1590 useragent = EARGF(usage());
1593 die("surf-"VERSION", ©2009-2015 surf engineers, "
1594 "see LICENSE for details\n");
1599 zoomlevel = strtof(EARGF(usage()), NULL);
1608 c = newclient(NULL);
1611 loaduri(clients, &arg);
1618 return EXIT_SUCCESS;