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 showview(WebKitWebView *v, Client *c);
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 destroyclient(clients);
338 WebKitCookieAcceptPolicy
339 cookiepolicy_get(void)
341 switch (cookiepolicies[cookiepolicy]) {
343 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
345 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
351 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
355 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
358 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
360 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
362 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
371 evalscript(JSContextRef js, char *script, char* scriptname)
373 JSStringRef jsscript, jsscriptname;
374 JSValueRef exception = NULL;
376 jsscript = JSStringCreateWithUTF8CString(script);
377 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
378 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
379 jsscriptname, 0, &exception);
380 JSStringRelease(jsscript);
381 JSStringRelease(jsscriptname);
385 runscript(WebKitWebFrame *frame)
390 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
391 evalscript(webkit_web_frame_get_global_context(frame), script,
397 clipboard(Client *c, const Arg *arg)
399 gboolean paste = *(gboolean *)arg;
402 gtk_clipboard_request_text(gtk_clipboard_get(
403 GDK_SELECTION_PRIMARY),
406 gtk_clipboard_set_text(gtk_clipboard_get(
407 GDK_SELECTION_PRIMARY), c->linkhover
408 ? c->linkhover : geturi(c), -1);
413 copystr(char **str, const char *src)
426 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
428 Client *n = newclient();
433 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
434 gchar *m, WebKitWebPolicyDecision *p, Client *c)
436 if (!webkit_web_view_can_show_mime_type(v, m)) {
437 webkit_web_policy_decision_download(p);
444 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
445 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
450 if (webkit_web_navigation_action_get_reason(n)
451 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
452 webkit_web_policy_decision_ignore(p);
453 arg.v = (void *)webkit_network_request_get_uri(r);
454 newwindow(NULL, &arg, 0);
461 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
467 destroyclient(Client *c)
471 webkit_web_view_stop_loading(c->view);
472 gtk_widget_destroy(GTK_WIDGET(c->view));
473 gtk_widget_destroy(c->scroll);
474 gtk_widget_destroy(c->vbox);
475 gtk_widget_destroy(c->win);
477 for (p = clients; p && p->next != c; p = p->next)
489 destroywin(GtkWidget* w, Client *c)
495 die(const char *errstr, ...)
499 va_start(ap, errstr);
500 vfprintf(stderr, errstr, ap);
506 find(Client *c, const Arg *arg)
510 s = getatom(c, AtomFind);
511 gboolean forward = *(gboolean *)arg;
512 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
516 fullscreen(Client *c, const Arg *arg)
519 gtk_window_unfullscreen(GTK_WINDOW(c->win));
521 gtk_window_fullscreen(GTK_WINDOW(c->win));
522 c->fullscreen = !c->fullscreen;
526 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
527 WebKitGeolocationPolicyDecision *d, Client *c)
529 if (allowgeolocation)
530 webkit_geolocation_policy_allow(d);
532 webkit_geolocation_policy_deny(d);
536 getatom(Client *c, int a)
538 static char buf[BUFSIZ];
541 unsigned long ldummy;
542 unsigned char *p = NULL;
544 XGetWindowProperty(dpy, c->xid,
545 atoms[a], 0L, BUFSIZ, False, XA_STRING,
546 &adummy, &idummy, &ldummy, &ldummy, &p);
548 strncpy(buf, (char *)p, LENGTH(buf)-1);
561 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
567 getstyle(const char *uri)
571 if (stylefile != NULL)
574 for (i = 0; i < LENGTH(styles); i++) {
575 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
577 return styles[i].style;
584 setstyle(Client *c, const char *style)
586 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
588 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
592 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
596 webkit_web_view_stop_loading(w);
597 arg = (Arg)PLUMB((char *)uri);
602 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
607 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
613 inspector(Client *c, const Arg *arg)
615 if (enableinspector) {
617 webkit_web_inspector_close(c->inspector);
619 webkit_web_inspector_show(c->inspector);
624 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
626 return WEBKIT_WEB_VIEW(webkit_web_view_new());
630 inspector_show(WebKitWebInspector *i, Client *c)
637 w = webkit_web_inspector_get_web_view(i);
638 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
639 gtk_widget_show(GTK_WIDGET(w));
640 c->isinspecting = true;
646 inspector_close(WebKitWebInspector *i, Client *c)
650 if (!c->isinspecting)
653 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
655 gtk_widget_destroy(w);
656 c->isinspecting = false;
662 inspector_finished(WebKitWebInspector *i, Client *c)
664 g_free(c->inspector);
668 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
672 gboolean processed = FALSE;
674 mods = CLEANMASK(mods);
675 key = gdk_keyval_to_lower(key);
677 for (i = 0; i < LENGTH(keys); i++) {
678 if (key == keys[i].keyval
679 && mods == keys[i].mod
681 keys[i].func(c, &(keys[i].arg));
690 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
693 c->linkhover = copystr(&c->linkhover, l);
694 } else if (c->linkhover) {
702 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
704 WebKitWebFrame *frame;
705 WebKitWebDataSource *src;
706 WebKitNetworkRequest *request;
710 switch (webkit_web_view_get_load_status (c->view)) {
711 case WEBKIT_LOAD_COMMITTED:
713 if (strstr(uri, "https://") == uri) {
714 frame = webkit_web_view_get_main_frame(c->view);
715 src = webkit_web_frame_get_data_source(frame);
716 request = webkit_web_data_source_get_request(src);
717 msg = webkit_network_request_get_message(request);
718 c->sslfailed = !(soup_message_get_flags(msg)
719 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
721 setatom(c, AtomUri, uri);
724 setstyle(c, getstyle(uri));
726 case WEBKIT_LOAD_FINISHED:
736 loaduri(Client *c, const Arg *arg)
739 const char *uri = (char *)arg->v;
740 Arg a = { .b = FALSE };
743 if (strcmp(uri, "") == 0)
746 /* In case it's a file path. */
747 if (stat(uri, &st) == 0) {
748 rp = realpath(uri, NULL);
749 u = g_strdup_printf("file://%s", rp);
752 u = g_strrstr(uri, "://") ? g_strdup(uri)
753 : g_strdup_printf("http://%s", uri);
756 setatom(c, AtomUri, uri);
758 /* prevents endless loop */
759 if (strcmp(u, geturi(c)) == 0) {
762 webkit_web_view_load_uri(c->view, u);
764 c->title = copystr(&c->title, u);
771 navigate(Client *c, const Arg *arg)
773 int steps = *(int *)arg;
774 webkit_web_view_go_back_or_forward(c->view, steps);
781 WebKitWebSettings *settings;
785 if (!(c = calloc(1, sizeof(Client))))
786 die("Cannot malloc!\n");
792 c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
794 g_signal_connect(G_OBJECT(c->view),
796 G_CALLBACK(titlechange), c);
797 g_signal_connect(G_OBJECT(c->view),
798 "hovering-over-link",
799 G_CALLBACK(linkhover), c);
800 g_signal_connect(G_OBJECT(c->view),
801 "geolocation-policy-decision-requested",
802 G_CALLBACK(geopolicyrequested), c);
803 g_signal_connect(G_OBJECT(c->view),
805 G_CALLBACK(createwindow), c);
806 g_signal_connect(G_OBJECT(v), "ready-to-show",
807 G_CALLBACK(showview), c);
808 g_signal_connect(G_OBJECT(c->view),
809 "new-window-policy-decision-requested",
810 G_CALLBACK(decidewindow), c);
811 g_signal_connect(G_OBJECT(c->view),
812 "mime-type-policy-decision-requested",
813 G_CALLBACK(decidedownload), c);
814 g_signal_connect(G_OBJECT(c->view),
815 "window-object-cleared",
816 G_CALLBACK(windowobjectcleared), c);
817 g_signal_connect(G_OBJECT(c->view),
818 "notify::load-status",
819 G_CALLBACK(loadstatuschange), c);
820 g_signal_connect(G_OBJECT(c->view),
822 G_CALLBACK(progresschange), c);
823 g_signal_connect(G_OBJECT(c->view),
824 "download-requested",
825 G_CALLBACK(initdownload), c);
826 g_signal_connect(G_OBJECT(c->view),
827 "button-release-event",
828 G_CALLBACK(buttonrelease), c);
829 g_signal_connect(G_OBJECT(c->view),
831 G_CALLBACK(contextmenu), c);
832 g_signal_connect(G_OBJECT(c->view),
833 "resource-request-starting",
834 G_CALLBACK(beforerequest), c);
835 g_signal_connect(G_OBJECT(c->view),
836 "should-show-delete-interface-for-element",
837 G_CALLBACK(deletion_interface), c);
839 settings = webkit_web_view_get_settings(c->view);
840 if (!(ua = getenv("SURF_USERAGENT")))
842 g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
843 g_object_set(G_OBJECT(settings),
844 "auto-load-images", loadimages, NULL);
845 g_object_set(G_OBJECT(settings),
846 "enable-plugins", enableplugins, NULL);
847 g_object_set(G_OBJECT(settings),
848 "enable-scripts", enablescripts, NULL);
849 g_object_set(G_OBJECT(settings),
850 "enable-spatial-navigation", enablespatialbrowsing, NULL);
851 g_object_set(G_OBJECT(settings),
852 "enable-developer-extras", enableinspector, NULL);
853 g_object_set(G_OBJECT(settings),
854 "enable-default-context-menu", kioskmode ^ 1, NULL);
855 g_object_set(G_OBJECT(settings),
856 "default-font-size", defaultfontsize, NULL);
857 g_object_set(G_OBJECT(settings),
858 "resizable-text-areas", 1, NULL);
860 setstyle(c, getstyle("about:blank"));
862 if (enableinspector) {
863 c->inspector = webkit_web_view_get_inspector(c->view);
864 g_signal_connect(G_OBJECT(c->inspector), "inspect-web-view",
865 G_CALLBACK(inspector_new), c);
866 g_signal_connect(G_OBJECT(c->inspector), "show-window",
867 G_CALLBACK(inspector_show), c);
868 g_signal_connect(G_OBJECT(c->inspector), "close-window",
869 G_CALLBACK(inspector_close), c);
870 g_signal_connect(G_OBJECT(c->inspector), "finished",
871 G_CALLBACK(inspector_finished), c);
872 c->isinspecting = false;
882 showview(WebKitWebView *v, Client *c)
884 GdkGeometry hints = { 1, 1 };
885 GdkRGBA bgcolor = { 0 };
890 c->win = gtk_plug_new(embed);
892 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
894 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
895 * is always correct, so we should still call this function.
896 * But when doing so, we *must* differentiate between a
897 * WM_CLASS and a resource on the window. By convention, the
898 * window class (WM_CLASS) is capped, while the resource is in
899 * lowercase. Both these values come as a pair.
901 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
903 /* TA: 20091214: And set the role here as well -- so that
904 * sessions can pick this up.
906 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
908 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
909 g_signal_connect(G_OBJECT(c->win),
911 G_CALLBACK(destroywin), c);
912 g_signal_connect(G_OBJECT(c->win),
913 "leave_notify_event",
914 G_CALLBACK(titlechangeleave), c);
920 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
923 gtk_widget_grab_focus(GTK_WIDGET(c->view));
924 gtk_widget_show(GTK_WIDGET(c->view));
925 gtk_widget_show(c->win);
926 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
927 c->xid = gdk_x11_window_get_xid(gwin);
928 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
930 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
931 gdk_window_add_filter(gwin, processx, c);
935 /* This might conflict with _zoomto96dpi_. */
936 if (zoomlevel != 1.0)
937 webkit_web_view_set_zoom_level(c->view, zoomlevel);
942 setatom(c, AtomFind, "");
943 setatom(c, AtomUri, "about:blank");
945 webkit_web_view_set_background_color(c->view, &bgcolor);
948 gdk_display_sync(gtk_widget_get_display(c->win));
949 printf("%lu\n", c->xid);
951 if (fclose(stdout) != 0) {
952 die("Error closing stdout");
958 newwindow(Client *c, const Arg *arg, gboolean noembed)
961 const char *cmd[18], *uri;
962 const Arg a = { .v = (void *)cmd };
967 cmd[i++] = cookiepolicies;
968 if (!enablescrollbars)
970 if (embed && !noembed) {
972 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
975 if (!allowgeolocation)
990 cmd[i++] = cookiefile;
992 uri = arg->v ? (char *)arg->v : c->linkhover;
1000 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1001 gboolean keyboard, Client *c)
1003 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1005 for (GList *l = items; l; l = l->next)
1006 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1013 menuactivate(GtkMenuItem *item, Client *c)
1016 * context-menu-action-2000 open link
1017 * context-menu-action-1 open link in window
1018 * context-menu-action-2 download linked file
1019 * context-menu-action-3 copy link location
1020 * context-menu-action-7 copy image address
1021 * context-menu-action-13 reload
1022 * context-menu-action-10 back
1023 * context-menu-action-11 forward
1024 * context-menu-action-12 stop
1027 const gchar *name, *uri;
1028 GtkClipboard *prisel, *clpbrd;
1030 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1034 if (!g_strcmp0(name, "context-menu-action-3")) {
1035 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1036 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1037 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1038 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1039 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1040 uri = gtk_clipboard_wait_for_text(clpbrd);
1042 gtk_clipboard_set_text(prisel, uri, -1);
1047 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1049 Arg arg = {.v = text };
1051 loaduri((Client *) d, &arg);
1055 print(Client *c, const Arg *arg)
1057 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1061 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1063 Client *c = (Client *)d;
1067 if (((XEvent *)e)->type == PropertyNotify) {
1068 ev = &((XEvent *)e)->xproperty;
1069 if (ev->state == PropertyNewValue) {
1070 if (ev->atom == atoms[AtomFind]) {
1074 return GDK_FILTER_REMOVE;
1075 } else if (ev->atom == atoms[AtomGo]) {
1076 arg.v = getatom(c, AtomGo);
1079 return GDK_FILTER_REMOVE;
1083 return GDK_FILTER_CONTINUE;
1087 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1089 c->progress = webkit_web_view_get_progress(c->view) * 100;
1094 linkopen(Client *c, const Arg *arg)
1096 newwindow(NULL, arg, 1);
1100 linkopenembed(Client *c, const Arg *arg)
1102 newwindow(NULL, arg, 0);
1106 reload(Client *c, const Arg *arg)
1108 gboolean nocache = *(gboolean *)arg;
1110 webkit_web_view_reload_bypass_cache(c->view);
1112 webkit_web_view_reload(c->view);
1116 scroll_h(Client *c, const Arg *arg)
1118 scroll(gtk_scrolled_window_get_hadjustment(
1119 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1123 scroll_v(Client *c, const Arg *arg)
1125 scroll(gtk_scrolled_window_get_vadjustment(
1126 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1130 scroll(GtkAdjustment *a, const Arg *arg)
1134 v = gtk_adjustment_get_value(a);
1138 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1143 v += gtk_adjustment_get_step_increment(a) * arg->i;
1147 v = MIN(v, gtk_adjustment_get_upper(a) -
1148 gtk_adjustment_get_page_size(a));
1149 gtk_adjustment_set_value(a, v);
1153 setatom(Client *c, int a, const char *v)
1156 XChangeProperty(dpy, c->xid,
1157 atoms[a], XA_STRING, 8, PropModeReplace,
1158 (unsigned char *)v, strlen(v) + 1);
1165 char *styledirfile, *stylepath;
1166 WebKitWebContext *context;
1167 GError *error = NULL;
1169 /* clean up any zombies immediately */
1171 gtk_init(NULL, NULL);
1173 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1176 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1177 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1178 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1180 /* dirs and files */
1181 cookiefile = buildfile(cookiefile);
1182 scriptfile = buildfile(scriptfile);
1183 cachefolder = buildpath(cachefolder);
1184 if (stylefile == NULL) {
1185 styledir = buildpath(styledir);
1186 for (i = 0; i < LENGTH(styles); i++) {
1187 if (regcomp(&(styles[i].re), styles[i].regex,
1190 "Could not compile regex: %s\n",
1192 styles[i].regex = NULL;
1194 styledirfile = g_strconcat(styledir, "/",
1195 styles[i].style, NULL);
1196 stylepath = buildfile(styledirfile);
1197 styles[i].style = g_strconcat("file://", stylepath,
1199 g_free(styledirfile);
1204 stylepath = buildfile(stylefile);
1205 stylefile = g_strconcat("file://", stylepath, NULL);
1210 webkit_cookie_manager_set_persistent_storage(
1211 webkit_web_context_get_cookie_manager(context), cookiefile,
1212 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
1213 webkit_cookie_manager_set_accept_policy(
1214 webkit_web_context_get_cookie_manager(context),
1215 cookiepolicy_get());
1217 /* rendering process model, can be a shared unique one or one for each
1219 webkit_web_context_set_process_model(context,
1220 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1223 webkit_web_context_set_cache_model(context, enablecache ?
1224 WEBKIT_CACHE_MODEL_WEB_BROWSER :
1225 WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
1228 webkit_web_context_set_tls_errors_policy(context, strictssl ?
1229 WEBKIT_TLS_ERRORS_POLICY_FAIL :
1230 WEBKIT_TLS_ERRORS_POLICY_IGNORE);
1236 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1237 die("Can't install SIGCHLD handler");
1238 while (0 < waitpid(-1, NULL, WNOHANG));
1242 spawn(Client *c, const Arg *arg)
1246 close(ConnectionNumber(dpy));
1248 execvp(((char **)arg->v)[0], (char **)arg->v);
1249 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1256 eval(Client *c, const Arg *arg)
1258 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1259 evalscript(webkit_web_frame_get_global_context(frame),
1260 ((char **)arg->v)[0], "");
1264 stop(Client *c, const Arg *arg)
1266 webkit_web_view_stop_loading(c->view);
1270 titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1272 const gchar *t = webkit_web_view_get_title(view);
1274 c->title = copystr(&c->title, t);
1280 titlechangeleave(void *a, void *b, Client *c)
1282 c->linkhover = NULL;
1287 toggle(Client *c, const Arg *arg)
1289 WebKitWebSettings *settings;
1290 char *name = (char *)arg->v;
1292 Arg a = { .b = FALSE };
1294 settings = webkit_web_view_get_settings(c->view);
1295 g_object_get(G_OBJECT(settings), name, &value, NULL);
1296 g_object_set(G_OBJECT(settings), name, !value, NULL);
1302 togglecookiepolicy(Client *c, const Arg *arg)
1305 cookiepolicy %= strlen(cookiepolicies);
1307 webkit_cookie_manager_set_accept_policy(
1308 webkit_web_context_get_cookie_manager(
1309 webkit_web_view_get_context(c->view)),
1310 cookiepolicy_get());
1313 /* Do not reload. */
1317 togglegeolocation(Client *c, const Arg *arg)
1319 Arg a = { .b = FALSE };
1321 allowgeolocation ^= 1;
1326 twitch(Client *c, const Arg *arg)
1331 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1334 v = gtk_adjustment_get_value(a);
1339 v = MIN(v, gtk_adjustment_get_upper(a) -
1340 gtk_adjustment_get_page_size(a));
1341 gtk_adjustment_set_value(a, v);
1345 togglescrollbars(Client *c, const Arg *arg)
1347 GtkPolicyType vspolicy;
1350 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1353 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1354 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1358 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1359 GTK_POLICY_AUTOMATIC,
1360 GTK_POLICY_AUTOMATIC);
1369 togglestyle(Client *c, const Arg *arg)
1371 enablestyle = !enablestyle;
1372 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1378 gettogglestat(Client *c)
1382 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1384 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1386 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1388 togglestat[p++] = value? 'C': 'c';
1390 togglestat[p++] = allowgeolocation? 'G': 'g';
1392 togglestat[p++] = enablediskcache? 'D': 'd';
1394 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1395 togglestat[p++] = value? 'I': 'i';
1397 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1398 togglestat[p++] = value? 'S': 's';
1400 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1401 togglestat[p++] = value? 'V': 'v';
1403 togglestat[p++] = enablestyle ? 'M': 'm';
1405 togglestat[p] = '\0';
1409 getpagestat(Client *c)
1411 const char *uri = geturi(c);
1413 if (strstr(uri, "https://") == uri)
1414 pagestat[0] = c->sslfailed ? 'U' : 'T';
1422 updatetitle(Client *c)
1426 if (showindicators) {
1431 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1433 } else if (c->progress != 100) {
1434 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1435 togglestat, pagestat,
1436 c->title == NULL ? "" : c->title);
1438 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1439 c->title == NULL ? "" : c->title);
1442 gtk_window_set_title(GTK_WINDOW(c->win), t);
1445 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1451 updatewinid(Client *c)
1453 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1459 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1460 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1461 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1465 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1466 JSObjectRef win, Client *c)
1472 zoom(Client *c, const Arg *arg)
1477 webkit_web_view_zoom_out(c->view);
1478 } else if (arg->i > 0) {
1480 webkit_web_view_zoom_in(c->view);
1484 webkit_web_view_set_zoom_level(c->view, 1.0);
1489 main(int argc, char *argv[])
1494 memset(&arg, 0, sizeof(arg));
1496 /* command line args */
1499 cookiepolicies = EARGF(usage());
1502 enablescrollbars = 0;
1505 enablescrollbars = 1;
1508 cookiefile = EARGF(usage());
1511 enablediskcache = 0;
1514 enablediskcache = 1;
1517 embed = strtol(EARGF(usage()), NULL, 0);
1520 runinfullscreen = 0;
1523 runinfullscreen = 1;
1526 allowgeolocation = 0;
1529 allowgeolocation = 1;
1550 enableinspector = 0;
1553 enableinspector = 1;
1562 scriptfile = EARGF(usage());
1571 stylefile = EARGF(usage());
1574 useragent = EARGF(usage());
1577 die("surf-"VERSION", ©2009-2015 surf engineers, "
1578 "see LICENSE for details\n");
1583 zoomlevel = strtof(EARGF(usage()), NULL);
1594 loaduri(clients, &arg);
1601 return EXIT_SUCCESS;