1 /* See LICENSE file for copyright and license details.
3 * To understand surf, start reading main().
12 #include <gdk/gdkkeysyms.h>
14 #include <sys/types.h>
20 #include <webkit2/webkit2.h>
21 #include <glib/gstdio.h>
22 #include <JavaScriptCore/JavaScript.h>
34 #define LENGTH(x) (sizeof(x) / sizeof(x[0]))
35 #define CLEANMASK(mask) (mask & (MODKEY|GDK_SHIFT_MASK))
37 enum { AtomFind, AtomGo, AtomUri, AtomLast };
39 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
40 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
41 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
42 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
43 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
44 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
45 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
48 typedef union Arg Arg;
55 typedef struct Client {
59 WebKitWebInspector *inspector;
60 char *title, *linkhover;
64 gboolean zoomed, fullscreen, isinspecting, sslfailed;
70 void (*func)(Client *c, const Arg *arg);
78 void (*func)(Client *c, const Arg *arg);
89 static Atom atoms[AtomLast];
90 static Client *clients = NULL;
91 static Window embed = 0;
92 static gboolean showxid = FALSE;
93 static char winid[64];
94 static char togglestat[9];
95 static char pagestat[3];
96 static GTlsDatabase *tlsdb;
97 static int cookiepolicy;
98 static char *stylefile = NULL;
100 static void addaccelgroup(Client *c);
101 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
102 WebKitWebResource *r, WebKitNetworkRequest *req,
103 WebKitNetworkResponse *resp, Client *c);
104 static char *buildfile(const char *path);
105 static char *buildpath(const char *path);
106 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
107 static void cleanup(void);
108 static void clipboard(Client *c, const Arg *arg);
110 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
111 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
113 static char *copystr(char **str, const char *src);
114 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
116 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
117 WebKitNetworkRequest *r, gchar *m,
118 WebKitWebPolicyDecision *p, Client *c);
119 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
120 WebKitNetworkRequest *r, WebKitWebNavigationAction
121 *n, WebKitWebPolicyDecision *p, Client *c);
122 static gboolean deletion_interface(WebKitWebView *view,
123 WebKitDOMHTMLElement *arg1, Client *c);
124 static void destroyclient(Client *c);
125 static void destroywin(GtkWidget* w, Client *c);
126 static void die(const char *errstr, ...);
127 static void eval(Client *c, const Arg *arg);
128 static void find(Client *c, const Arg *arg);
129 static void fullscreen(Client *c, const Arg *arg);
130 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
131 WebKitGeolocationPolicyDecision *d, Client *c);
132 static const char *getatom(Client *c, int a);
133 static void gettogglestat(Client *c);
134 static void getpagestat(Client *c);
135 static char *geturi(Client *c);
136 static const gchar *getstyle(const char *uri);
137 static void setstyle(Client *c, const char *style);
139 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
141 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
143 static void inspector(Client *c, const Arg *arg);
144 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
146 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
147 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
148 static void inspector_finished(WebKitWebInspector *i, Client *c);
150 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
151 GdkModifierType mods, Client *c);
152 static void linkhover(WebKitWebView *v, const char* t, const char* l,
154 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
156 static void loaduri(Client *c, const Arg *arg);
157 static void navigate(Client *c, const Arg *arg);
158 static Client *newclient(void);
159 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
160 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
161 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
162 WebKitHitTestResult *target, gboolean keyboard,
164 static void menuactivate(GtkMenuItem *item, Client *c);
165 static void print(Client *c, const Arg *arg);
166 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
168 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
169 static void linkopen(Client *c, const Arg *arg);
170 static void linkopenembed(Client *c, const Arg *arg);
171 static void reload(Client *c, const Arg *arg);
172 static void scroll_h(Client *c, const Arg *arg);
173 static void scroll_v(Client *c, const Arg *arg);
174 static void scroll(GtkAdjustment *a, const Arg *arg);
175 static void setatom(Client *c, int a, const char *v);
176 static void setup(void);
177 static void sigchld(int unused);
178 static void spawn(Client *c, const Arg *arg);
179 static void stop(Client *c, const Arg *arg);
180 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
181 static void titlechangeleave(void *a, void *b, Client *c);
182 static void toggle(Client *c, const Arg *arg);
183 static void togglecookiepolicy(Client *c, const Arg *arg);
184 static void togglegeolocation(Client *c, const Arg *arg);
185 static void togglescrollbars(Client *c, const Arg *arg);
186 static void togglestyle(Client *c, const Arg *arg);
187 static void updatetitle(Client *c);
188 static void updatewinid(Client *c);
189 static void usage(void);
190 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
191 JSContextRef js, JSObjectRef win, Client *c);
192 static void zoom(Client *c, const Arg *arg);
194 /* configuration, allows nested code to access above variables */
198 addaccelgroup(Client *c)
201 GtkAccelGroup *group = gtk_accel_group_new();
204 for (i = 0; i < LENGTH(keys); i++) {
205 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
206 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
209 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
213 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
214 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
217 const gchar *uri = webkit_network_request_get_uri(req);
220 if (g_str_has_suffix(uri, "/favicon.ico"))
221 webkit_network_request_set_uri(req, "about:blank");
223 if (!g_str_has_prefix(uri, "http://")
224 && !g_str_has_prefix(uri, "https://")
225 && !g_str_has_prefix(uri, "about:")
226 && !g_str_has_prefix(uri, "file://")
227 && !g_str_has_prefix(uri, "data:")
228 && !g_str_has_prefix(uri, "blob:")
229 && strlen(uri) > 0) {
230 for (i = 0; i < strlen(uri); i++) {
231 if (!g_ascii_isprint(uri[i])) {
237 handleplumb(c, w, uri);
242 buildfile(const char *path)
244 char *dname, *bname, *bpath, *fpath;
247 dname = g_path_get_dirname(path);
248 bname = g_path_get_basename(path);
250 bpath = buildpath(dname);
253 fpath = g_build_filename(bpath, bname, NULL);
257 if (!(f = fopen(fpath, "a")))
258 die("Could not open file: %s\n", fpath);
260 g_chmod(fpath, 0600); /* always */
267 buildpath(const char *path)
270 char *apath, *name, *p, *fpath;
272 if (path[0] == '~') {
273 if (path[1] == '/' || path[1] == '\0') {
274 p = (char *)&path[1];
275 pw = getpwuid(getuid());
277 if ((p = strchr(path, '/')))
278 name = g_strndup(&path[1], --p - path);
280 name = g_strdup(&path[1]);
282 if (!(pw = getpwnam(name))) {
283 die("Can't get user %s home directory: %s.\n",
288 apath = g_build_filename(pw->pw_dir, p, NULL);
290 apath = g_strdup(path);
293 /* creating directory */
294 if (g_mkdir_with_parents(apath, 0700) < 0)
295 die("Could not access directory: %s\n", apath);
297 fpath = realpath(apath, NULL);
304 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
306 WebKitHitTestResultContext context;
307 WebKitHitTestResult *result;
311 result = webkit_web_view_get_hit_test_result(web, e);
312 g_object_get(result, "context", &context, NULL);
313 g_object_get(result, "link-uri", &arg.v, NULL);
314 for (i = 0; i < LENGTH(buttons); i++) {
315 if (context & buttons[i].click
316 && e->button == buttons[i].button
317 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
318 && buttons[i].func) {
319 buttons[i].func(c, buttons[i].click == ClkLink
320 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
331 destroyclient(clients);
337 WebKitCookieAcceptPolicy
338 cookiepolicy_get(void)
340 switch (cookiepolicies[cookiepolicy]) {
342 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
344 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
350 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
354 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
357 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
359 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
361 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
370 evalscript(JSContextRef js, char *script, char* scriptname)
372 JSStringRef jsscript, jsscriptname;
373 JSValueRef exception = NULL;
375 jsscript = JSStringCreateWithUTF8CString(script);
376 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
377 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
378 jsscriptname, 0, &exception);
379 JSStringRelease(jsscript);
380 JSStringRelease(jsscriptname);
384 runscript(WebKitWebFrame *frame)
389 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
390 evalscript(webkit_web_frame_get_global_context(frame), script,
396 clipboard(Client *c, const Arg *arg)
398 gboolean paste = *(gboolean *)arg;
401 gtk_clipboard_request_text(gtk_clipboard_get(
402 GDK_SELECTION_PRIMARY),
405 gtk_clipboard_set_text(gtk_clipboard_get(
406 GDK_SELECTION_PRIMARY), c->linkhover
407 ? c->linkhover : geturi(c), -1);
412 copystr(char **str, const char *src)
425 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
427 Client *n = newclient();
432 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
433 gchar *m, WebKitWebPolicyDecision *p, Client *c)
435 if (!webkit_web_view_can_show_mime_type(v, m)) {
436 webkit_web_policy_decision_download(p);
443 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
444 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
449 if (webkit_web_navigation_action_get_reason(n)
450 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
451 webkit_web_policy_decision_ignore(p);
452 arg.v = (void *)webkit_network_request_get_uri(r);
453 newwindow(NULL, &arg, 0);
460 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
466 destroyclient(Client *c)
470 webkit_web_view_stop_loading(c->view);
471 gtk_widget_destroy(GTK_WIDGET(c->view));
472 gtk_widget_destroy(c->scroll);
473 gtk_widget_destroy(c->vbox);
474 gtk_widget_destroy(c->win);
476 for (p = clients; p && p->next != c; p = p->next)
488 destroywin(GtkWidget* w, Client *c)
494 die(const char *errstr, ...)
498 va_start(ap, errstr);
499 vfprintf(stderr, errstr, ap);
505 find(Client *c, const Arg *arg)
509 s = getatom(c, AtomFind);
510 gboolean forward = *(gboolean *)arg;
511 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
515 fullscreen(Client *c, const Arg *arg)
518 gtk_window_unfullscreen(GTK_WINDOW(c->win));
520 gtk_window_fullscreen(GTK_WINDOW(c->win));
521 c->fullscreen = !c->fullscreen;
525 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
526 WebKitGeolocationPolicyDecision *d, Client *c)
528 if (allowgeolocation)
529 webkit_geolocation_policy_allow(d);
531 webkit_geolocation_policy_deny(d);
535 getatom(Client *c, int a)
537 static char buf[BUFSIZ];
540 unsigned long ldummy;
541 unsigned char *p = NULL;
543 XGetWindowProperty(dpy, c->xid,
544 atoms[a], 0L, BUFSIZ, False, XA_STRING,
545 &adummy, &idummy, &ldummy, &ldummy, &p);
547 strncpy(buf, (char *)p, LENGTH(buf)-1);
560 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
566 getstyle(const char *uri)
570 if (stylefile != NULL)
573 for (i = 0; i < LENGTH(styles); i++) {
574 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
576 return styles[i].style;
583 setstyle(Client *c, const char *style)
585 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
587 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
591 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
595 webkit_web_view_stop_loading(w);
596 arg = (Arg)PLUMB((char *)uri);
601 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
606 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
612 inspector(Client *c, const Arg *arg)
614 if (enableinspector) {
616 webkit_web_inspector_close(c->inspector);
618 webkit_web_inspector_show(c->inspector);
623 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
625 return WEBKIT_WEB_VIEW(webkit_web_view_new());
629 inspector_show(WebKitWebInspector *i, Client *c)
636 w = webkit_web_inspector_get_web_view(i);
637 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
638 gtk_widget_show(GTK_WIDGET(w));
639 c->isinspecting = true;
645 inspector_close(WebKitWebInspector *i, Client *c)
649 if (!c->isinspecting)
652 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
654 gtk_widget_destroy(w);
655 c->isinspecting = false;
661 inspector_finished(WebKitWebInspector *i, Client *c)
663 g_free(c->inspector);
667 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
671 gboolean processed = FALSE;
673 mods = CLEANMASK(mods);
674 key = gdk_keyval_to_lower(key);
676 for (i = 0; i < LENGTH(keys); i++) {
677 if (key == keys[i].keyval
678 && mods == keys[i].mod
680 keys[i].func(c, &(keys[i].arg));
689 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
692 c->linkhover = copystr(&c->linkhover, l);
693 } else if (c->linkhover) {
701 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
703 WebKitWebFrame *frame;
704 WebKitWebDataSource *src;
705 WebKitNetworkRequest *request;
709 switch (webkit_web_view_get_load_status (c->view)) {
710 case WEBKIT_LOAD_COMMITTED:
712 if (strstr(uri, "https://") == uri) {
713 frame = webkit_web_view_get_main_frame(c->view);
714 src = webkit_web_frame_get_data_source(frame);
715 request = webkit_web_data_source_get_request(src);
716 msg = webkit_network_request_get_message(request);
717 c->sslfailed = !(soup_message_get_flags(msg)
718 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
720 setatom(c, AtomUri, uri);
723 setstyle(c, getstyle(uri));
725 case WEBKIT_LOAD_FINISHED:
735 loaduri(Client *c, const Arg *arg)
738 const char *uri = (char *)arg->v;
739 Arg a = { .b = FALSE };
742 if (strcmp(uri, "") == 0)
745 /* In case it's a file path. */
746 if (stat(uri, &st) == 0) {
747 rp = realpath(uri, NULL);
748 u = g_strdup_printf("file://%s", rp);
751 u = g_strrstr(uri, "://") ? g_strdup(uri)
752 : g_strdup_printf("http://%s", uri);
755 setatom(c, AtomUri, uri);
757 /* prevents endless loop */
758 if (strcmp(u, geturi(c)) == 0) {
761 webkit_web_view_load_uri(c->view, u);
763 c->title = copystr(&c->title, u);
770 navigate(Client *c, const Arg *arg)
772 int steps = *(int *)arg;
773 webkit_web_view_go_back_or_forward(c->view, steps);
780 WebKitWebSettings *settings;
781 GdkGeometry hints = { 1, 1 };
787 if (!(c = calloc(1, sizeof(Client))))
788 die("Cannot malloc!\n");
795 c->win = gtk_plug_new(embed);
797 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
799 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
800 * is always correct, so we should still call this function.
801 * But when doing so, we *must* differentiate between a
802 * WM_CLASS and a resource on the window. By convention, the
803 * window class (WM_CLASS) is capped, while the resource is in
804 * lowercase. Both these values come as a pair.
806 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
808 /* TA: 20091214: And set the role here as well -- so that
809 * sessions can pick this up.
811 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
813 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
814 g_signal_connect(G_OBJECT(c->win),
816 G_CALLBACK(destroywin), c);
817 g_signal_connect(G_OBJECT(c->win),
818 "leave_notify_event",
819 G_CALLBACK(titlechangeleave), c);
825 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
827 g_signal_connect(G_OBJECT(c->view),
829 G_CALLBACK(titlechange), c);
830 g_signal_connect(G_OBJECT(c->view),
831 "hovering-over-link",
832 G_CALLBACK(linkhover), c);
833 g_signal_connect(G_OBJECT(c->view),
834 "geolocation-policy-decision-requested",
835 G_CALLBACK(geopolicyrequested), c);
836 g_signal_connect(G_OBJECT(c->view),
838 G_CALLBACK(createwindow), c);
839 g_signal_connect(G_OBJECT(c->view),
840 "new-window-policy-decision-requested",
841 G_CALLBACK(decidewindow), c);
842 g_signal_connect(G_OBJECT(c->view),
843 "mime-type-policy-decision-requested",
844 G_CALLBACK(decidedownload), c);
845 g_signal_connect(G_OBJECT(c->view),
846 "window-object-cleared",
847 G_CALLBACK(windowobjectcleared), c);
848 g_signal_connect(G_OBJECT(c->view),
849 "notify::load-status",
850 G_CALLBACK(loadstatuschange), c);
851 g_signal_connect(G_OBJECT(c->view),
853 G_CALLBACK(progresschange), c);
854 g_signal_connect(G_OBJECT(c->view),
855 "download-requested",
856 G_CALLBACK(initdownload), c);
857 g_signal_connect(G_OBJECT(c->view),
858 "button-release-event",
859 G_CALLBACK(buttonrelease), c);
860 g_signal_connect(G_OBJECT(c->view),
862 G_CALLBACK(contextmenu), c);
863 g_signal_connect(G_OBJECT(c->view),
864 "resource-request-starting",
865 G_CALLBACK(beforerequest), c);
866 g_signal_connect(G_OBJECT(c->view),
867 "should-show-delete-interface-for-element",
868 G_CALLBACK(deletion_interface), c);
871 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
874 gtk_widget_grab_focus(GTK_WIDGET(c->view));
875 gtk_widget_show(GTK_WIDGET(c->view));
876 gtk_widget_show(c->win);
877 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
878 c->xid = gdk_x11_window_get_xid(gwin);
879 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
881 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
882 gdk_window_add_filter(gwin, processx, c);
883 webkit_web_view_set_full_content_zoom(c->view, TRUE);
887 settings = webkit_web_view_get_settings(c->view);
888 if (!(ua = getenv("SURF_USERAGENT")))
890 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
891 g_object_set(G_OBJECT(settings),
892 "auto-load-images", loadimages, NULL);
893 g_object_set(G_OBJECT(settings),
894 "enable-plugins", enableplugins, NULL);
895 g_object_set(G_OBJECT(settings),
896 "enable-scripts", enablescripts, NULL);
897 g_object_set(G_OBJECT(settings),
898 "enable-spatial-navigation", enablespatialbrowsing, NULL);
899 g_object_set(G_OBJECT(settings),
900 "enable-developer-extras", enableinspector, NULL);
901 g_object_set(G_OBJECT(settings),
902 "enable-default-context-menu", kioskmode ^ 1, NULL);
903 g_object_set(G_OBJECT(settings),
904 "default-font-size", defaultfontsize, NULL);
905 g_object_set(G_OBJECT(settings),
906 "resizable-text-areas", 1, NULL);
908 setstyle(c, getstyle("about:blank"));
910 /* This might conflict with _zoomto96dpi_. */
911 if (zoomlevel != 1.0)
912 webkit_web_view_set_zoom_level(c->view, zoomlevel);
914 if (enableinspector) {
915 c->inspector = webkit_web_view_get_inspector(c->view);
916 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
917 G_CALLBACK(inspector_new), c);
918 g_signal_connect(G_OBJECT(c->inspector), "show-window",
919 G_CALLBACK(inspector_show), c);
920 g_signal_connect(G_OBJECT(c->inspector), "close-window",
921 G_CALLBACK(inspector_close), c);
922 g_signal_connect(G_OBJECT(c->inspector), "finished",
923 G_CALLBACK(inspector_finished), c);
924 c->isinspecting = false;
930 setatom(c, AtomFind, "");
931 setatom(c, AtomUri, "about:blank");
933 webkit_web_view_set_transparent(c->view, TRUE);
939 gdk_display_sync(gtk_widget_get_display(c->win));
940 printf("%lu\n", c->xid);
942 if (fclose(stdout) != 0) {
943 die("Error closing stdout");
951 newwindow(Client *c, const Arg *arg, gboolean noembed)
954 const char *cmd[18], *uri;
955 const Arg a = { .v = (void *)cmd };
960 cmd[i++] = cookiepolicies;
961 if (!enablescrollbars)
963 if (embed && !noembed) {
965 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
968 if (!allowgeolocation)
983 cmd[i++] = cookiefile;
985 uri = arg->v ? (char *)arg->v : c->linkhover;
993 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
994 gboolean keyboard, Client *c)
996 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
998 for (GList *l = items; l; l = l->next)
999 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1006 menuactivate(GtkMenuItem *item, Client *c)
1009 * context-menu-action-2000 open link
1010 * context-menu-action-1 open link in window
1011 * context-menu-action-2 download linked file
1012 * context-menu-action-3 copy link location
1013 * context-menu-action-7 copy image address
1014 * context-menu-action-13 reload
1015 * context-menu-action-10 back
1016 * context-menu-action-11 forward
1017 * context-menu-action-12 stop
1020 const gchar *name, *uri;
1021 GtkClipboard *prisel, *clpbrd;
1023 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1027 if (!g_strcmp0(name, "context-menu-action-3")) {
1028 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1029 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1030 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1031 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1032 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1033 uri = gtk_clipboard_wait_for_text(clpbrd);
1035 gtk_clipboard_set_text(prisel, uri, -1);
1040 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1042 Arg arg = {.v = text };
1044 loaduri((Client *) d, &arg);
1048 print(Client *c, const Arg *arg)
1050 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1054 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1056 Client *c = (Client *)d;
1060 if (((XEvent *)e)->type == PropertyNotify) {
1061 ev = &((XEvent *)e)->xproperty;
1062 if (ev->state == PropertyNewValue) {
1063 if (ev->atom == atoms[AtomFind]) {
1067 return GDK_FILTER_REMOVE;
1068 } else if (ev->atom == atoms[AtomGo]) {
1069 arg.v = getatom(c, AtomGo);
1072 return GDK_FILTER_REMOVE;
1076 return GDK_FILTER_CONTINUE;
1080 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1082 c->progress = webkit_web_view_get_progress(c->view) * 100;
1087 linkopen(Client *c, const Arg *arg)
1089 newwindow(NULL, arg, 1);
1093 linkopenembed(Client *c, const Arg *arg)
1095 newwindow(NULL, arg, 0);
1099 reload(Client *c, const Arg *arg)
1101 gboolean nocache = *(gboolean *)arg;
1103 webkit_web_view_reload_bypass_cache(c->view);
1105 webkit_web_view_reload(c->view);
1109 scroll_h(Client *c, const Arg *arg)
1111 scroll(gtk_scrolled_window_get_hadjustment(
1112 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1116 scroll_v(Client *c, const Arg *arg)
1118 scroll(gtk_scrolled_window_get_vadjustment(
1119 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1123 scroll(GtkAdjustment *a, const Arg *arg)
1127 v = gtk_adjustment_get_value(a);
1131 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1136 v += gtk_adjustment_get_step_increment(a) * arg->i;
1140 v = MIN(v, gtk_adjustment_get_upper(a) -
1141 gtk_adjustment_get_page_size(a));
1142 gtk_adjustment_set_value(a, v);
1146 setatom(Client *c, int a, const char *v)
1149 XChangeProperty(dpy, c->xid,
1150 atoms[a], XA_STRING, 8, PropModeReplace,
1151 (unsigned char *)v, strlen(v) + 1);
1158 char *styledirfile, *stylepath;
1159 WebKitWebContext *context;
1160 GError *error = NULL;
1162 /* clean up any zombies immediately */
1164 gtk_init(NULL, NULL);
1166 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1169 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1170 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1171 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1173 /* dirs and files */
1174 cookiefile = buildfile(cookiefile);
1175 scriptfile = buildfile(scriptfile);
1176 cachefolder = buildpath(cachefolder);
1177 if (stylefile == NULL) {
1178 styledir = buildpath(styledir);
1179 for (i = 0; i < LENGTH(styles); i++) {
1180 if (regcomp(&(styles[i].re), styles[i].regex,
1183 "Could not compile regex: %s\n",
1185 styles[i].regex = NULL;
1187 styledirfile = g_strconcat(styledir, "/",
1188 styles[i].style, NULL);
1189 stylepath = buildfile(styledirfile);
1190 styles[i].style = g_strconcat("file://", stylepath,
1192 g_free(styledirfile);
1197 stylepath = buildfile(stylefile);
1198 stylefile = g_strconcat("file://", stylepath, NULL);
1203 webkit_cookie_manager_set_persistent_storage(
1204 webkit_web_context_get_cookie_manager(context), cookiefile,
1205 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1206 webkit_cookie_manager_set_accept_policy(
1207 webkit_web_context_get_cookie_manager(context),
1208 cookiepolicy_get());
1210 /* rendering process model, can be a shared unique one or one for each
1212 webkit_web_context_set_process_model(context,
1213 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1216 webkit_web_context_set_cache_model(context, enablecache ?
1217 WEBKIT_CACHE_MODEL_WEB_BROWSER :
1218 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1221 webkit_web_context_set_tls_errors_policy(context, strictssl ?
1222 WEBKIT_TLS_ERRORS_POLICY_FAIL :
1223 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1229 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1230 die("Can't install SIGCHLD handler");
1231 while (0 < waitpid(-1, NULL, WNOHANG));
1235 spawn(Client *c, const Arg *arg)
1239 close(ConnectionNumber(dpy));
1241 execvp(((char **)arg->v)[0], (char **)arg->v);
1242 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1249 eval(Client *c, const Arg *arg)
1251 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1252 evalscript(webkit_web_frame_get_global_context(frame),
1253 ((char **)arg->v)[0], "");
1257 stop(Client *c, const Arg *arg)
1259 webkit_web_view_stop_loading(c->view);
1263 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1265 const gchar *t = webkit_web_view_get_title(view);
1267 c->title = copystr(&c->title, t);
1273 titlechangeleave(void *a, void *b, Client *c)
1275 c->linkhover = NULL;
1280 toggle(Client *c, const Arg *arg)
1282 WebKitWebSettings *settings;
1283 char *name = (char *)arg->v;
1285 Arg a = { .b = FALSE };
1287 settings = webkit_web_view_get_settings(c->view);
1288 g_object_get(G_OBJECT(settings), name, &value, NULL);
1289 g_object_set(G_OBJECT(settings), name, !value, NULL);
1295 togglecookiepolicy(Client *c, const Arg *arg)
1298 cookiepolicy %= strlen(cookiepolicies);
1300 webkit_cookie_manager_set_accept_policy(
1301 webkit_web_context_get_cookie_manager(
1302 webkit_web_view_get_context(c->view)),
1303 cookiepolicy_get());
1306 /* Do not reload. */
1310 togglegeolocation(Client *c, const Arg *arg)
1312 Arg a = { .b = FALSE };
1314 allowgeolocation ^= 1;
1319 twitch(Client *c, const Arg *arg)
1324 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1327 v = gtk_adjustment_get_value(a);
1332 v = MIN(v, gtk_adjustment_get_upper(a) -
1333 gtk_adjustment_get_page_size(a));
1334 gtk_adjustment_set_value(a, v);
1338 togglescrollbars(Client *c, const Arg *arg)
1340 GtkPolicyType vspolicy;
1343 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1346 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1347 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1351 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1352 GTK_POLICY_AUTOMATIC,
1353 GTK_POLICY_AUTOMATIC);
1362 togglestyle(Client *c, const Arg *arg)
1364 enablestyle = !enablestyle;
1365 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1371 gettogglestat(Client *c)
1375 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1377 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1379 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1381 togglestat[p++] = value? 'C': 'c';
1383 togglestat[p++] = allowgeolocation? 'G': 'g';
1385 togglestat[p++] = enablediskcache? 'D': 'd';
1387 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1388 togglestat[p++] = value? 'I': 'i';
1390 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1391 togglestat[p++] = value? 'S': 's';
1393 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1394 togglestat[p++] = value? 'V': 'v';
1396 togglestat[p++] = enablestyle ? 'M': 'm';
1398 togglestat[p] = '\0';
1402 getpagestat(Client *c)
1404 const char *uri = geturi(c);
1406 if (strstr(uri, "https://") == uri)
1407 pagestat[0] = c->sslfailed ? 'U' : 'T';
1415 updatetitle(Client *c)
1419 if (showindicators) {
1424 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1426 } else if (c->progress != 100) {
1427 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1428 togglestat, pagestat,
1429 c->title == NULL ? "" : c->title);
1431 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1432 c->title == NULL ? "" : c->title);
1435 gtk_window_set_title(GTK_WINDOW(c->win), t);
1438 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1444 updatewinid(Client *c)
1446 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1452 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1453 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1454 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1458 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1459 JSObjectRef win, Client *c)
1465 zoom(Client *c, const Arg *arg)
1470 webkit_web_view_zoom_out(c->view);
1471 } else if (arg->i > 0) {
1473 webkit_web_view_zoom_in(c->view);
1477 webkit_web_view_set_zoom_level(c->view, 1.0);
1482 main(int argc, char *argv[])
1487 memset(&arg, 0, sizeof(arg));
1489 /* command line args */
1492 cookiepolicies = EARGF(usage());
1495 enablescrollbars = 0;
1498 enablescrollbars = 1;
1501 cookiefile = EARGF(usage());
1504 enablediskcache = 0;
1507 enablediskcache = 1;
1510 embed = strtol(EARGF(usage()), NULL, 0);
1513 runinfullscreen = 0;
1516 runinfullscreen = 1;
1519 allowgeolocation = 0;
1522 allowgeolocation = 1;
1543 enableinspector = 0;
1546 enableinspector = 1;
1555 scriptfile = EARGF(usage());
1564 stylefile = EARGF(usage());
1567 useragent = EARGF(usage());
1570 die("surf-"VERSION", ©2009-2015 surf engineers, "
1571 "see LICENSE for details\n");
1576 zoomlevel = strtof(EARGF(usage()), NULL);
1587 loaduri(clients, &arg);
1594 return EXIT_SUCCESS;