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 };
40 OnDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
41 OnLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
42 OnImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
43 OnMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
44 OnEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
45 OnBar = WEBKIT_HIT_TEST_RESULT_CONTEXT_SCROLLBAR,
46 OnSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
47 OnAny = OnDoc | OnLink | OnImg | OnMedia | OnEdit | OnBar | OnSel,
50 typedef union Arg Arg;
57 typedef struct Client {
61 WebKitWebInspector *inspector;
62 WebKitHitTestResult *mousepos;
63 GTlsCertificateFlags tlsflags;
64 const char *title, *targeturi;
68 gboolean zoomed, fullscreen, isinspecting;
74 void (*func)(Client *c, const Arg *arg);
82 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
84 unsigned int stopevent;
94 static Atom atoms[AtomLast];
95 static Client *clients = NULL;
96 static Window embed = 0;
97 static gboolean showxid = FALSE;
98 static char winid[64];
99 static char togglestat[9];
100 static char pagestat[3];
101 static GTlsDatabase *tlsdb;
102 static int cookiepolicy;
103 static char *stylefile = NULL;
105 static void addaccelgroup(Client *c);
106 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
107 WebKitWebResource *r, WebKitNetworkRequest *req,
108 WebKitNetworkResponse *resp, Client *c);
109 static char *buildfile(const char *path);
110 static char *buildpath(const char *path);
111 static gboolean buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c);
112 static void cleanup(void);
113 static void clipboard(Client *c, const Arg *arg);
115 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
116 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
118 static char *copystr(char **str, const char *src);
119 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
121 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
122 WebKitPolicyDecisionType dt, Client *c);
123 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
124 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
125 static void decideresource(WebKitPolicyDecision *d, Client *c);
126 static gboolean deletion_interface(WebKitWebView *view,
127 WebKitDOMHTMLElement *arg1, Client *c);
128 static void destroyclient(Client *c);
129 static void destroywin(GtkWidget* w, Client *c);
130 static void die(const char *errstr, ...);
131 static void eval(Client *c, const Arg *arg);
132 static void find(Client *c, const Arg *arg);
133 static void fullscreen(Client *c, const Arg *arg);
134 static gboolean permissionrequested(WebKitWebView *v,
135 WebKitPermissionRequest *r, Client *c);
136 static const char *getatom(Client *c, int a);
137 static void gettogglestat(Client *c);
138 static void getpagestat(Client *c);
139 static char *geturi(Client *c);
140 static const gchar *getstyle(const char *uri);
141 static void setstyle(Client *c, const char *style);
143 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
145 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
147 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
148 static void download(Client *c, WebKitURIResponse *r);
150 static void inspector(Client *c, const Arg *arg);
151 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
153 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
154 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
155 static void inspector_finished(WebKitWebInspector *i, Client *c);
157 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
158 GdkModifierType mods, Client *c);
159 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
160 guint modifiers, Client *c);
161 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
162 static void loaduri(Client *c, const Arg *arg);
163 static void navigate(Client *c, const Arg *arg);
164 static Client *newclient(Client *c);
165 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
166 static void showview(WebKitWebView *v, Client *c);
167 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
168 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
169 static void print(Client *c, const Arg *arg);
170 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
172 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
173 static void linkopen(Client *c, const Arg *arg);
174 static void linkopenembed(Client *c, const Arg *arg);
175 static void reload(Client *c, const Arg *arg);
176 static void scroll_h(Client *c, const Arg *arg);
177 static void scroll_v(Client *c, const Arg *arg);
178 static void scroll(GtkAdjustment *a, const Arg *arg);
179 static void setatom(Client *c, int a, const char *v);
180 static void setup(void);
181 static void sigchld(int unused);
182 static void spawn(Client *c, const Arg *arg);
183 static void stop(Client *c, const Arg *arg);
184 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
185 static void titlechangeleave(void *a, void *b, Client *c);
186 static void toggle(Client *c, const Arg *arg);
187 static void togglecookiepolicy(Client *c, const Arg *arg);
188 static void togglegeolocation(Client *c, const Arg *arg);
189 static void togglescrollbars(Client *c, const Arg *arg);
190 static void togglestyle(Client *c, const Arg *arg);
191 static void updatetitle(Client *c);
192 static void updatewinid(Client *c);
193 static void usage(void);
194 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
195 JSContextRef js, JSObjectRef win, Client *c);
196 static void zoom(Client *c, const Arg *arg);
198 /* configuration, allows nested code to access above variables */
202 addaccelgroup(Client *c)
205 GtkAccelGroup *group = gtk_accel_group_new();
208 for (i = 0; i < LENGTH(keys); i++) {
209 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
210 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
213 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
217 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
218 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
221 const gchar *uri = webkit_network_request_get_uri(req);
224 if (g_str_has_suffix(uri, "/favicon.ico"))
225 webkit_network_request_set_uri(req, "about:blank");
227 if (!g_str_has_prefix(uri, "http://")
228 && !g_str_has_prefix(uri, "https://")
229 && !g_str_has_prefix(uri, "about:")
230 && !g_str_has_prefix(uri, "file://")
231 && !g_str_has_prefix(uri, "data:")
232 && !g_str_has_prefix(uri, "blob:")
233 && strlen(uri) > 0) {
234 for (i = 0; i < strlen(uri); i++) {
235 if (!g_ascii_isprint(uri[i])) {
241 handleplumb(c, w, uri);
246 buildfile(const char *path)
248 char *dname, *bname, *bpath, *fpath;
251 dname = g_path_get_dirname(path);
252 bname = g_path_get_basename(path);
254 bpath = buildpath(dname);
257 fpath = g_build_filename(bpath, bname, NULL);
261 if (!(f = fopen(fpath, "a")))
262 die("Could not open file: %s\n", fpath);
264 g_chmod(fpath, 0600); /* always */
271 buildpath(const char *path)
274 char *apath, *name, *p, *fpath;
276 if (path[0] == '~') {
277 if (path[1] == '/' || path[1] == '\0') {
278 p = (char *)&path[1];
279 pw = getpwuid(getuid());
281 if ((p = strchr(path, '/')))
282 name = g_strndup(&path[1], --p - path);
284 name = g_strdup(&path[1]);
286 if (!(pw = getpwnam(name))) {
287 die("Can't get user %s home directory: %s.\n",
292 apath = g_build_filename(pw->pw_dir, p, NULL);
294 apath = g_strdup(path);
297 /* creating directory */
298 if (g_mkdir_with_parents(apath, 0700) < 0)
299 die("Could not access directory: %s\n", apath);
301 fpath = realpath(apath, NULL);
308 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
310 WebKitHitTestResultContext element;
311 GdkEventButton *eb = (GdkEventButton*)e;
314 element = webkit_hit_test_result_get_context(c->mousepos);
316 for (i = 0; i < LENGTH(buttons); ++i) {
317 if (element & buttons[i].target &&
318 eb->button == buttons[i].button &&
319 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
321 buttons[i].func(c, &buttons[i].arg, c->mousepos);
322 return buttons[i].stopevent;
333 destroyclient(clients);
340 WebKitCookieAcceptPolicy
341 cookiepolicy_get(void)
343 switch (cookiepolicies[cookiepolicy]) {
345 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
347 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
353 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
357 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
360 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
362 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
364 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
373 evalscript(JSContextRef js, char *script, char* scriptname)
375 JSStringRef jsscript, jsscriptname;
376 JSValueRef exception = NULL;
378 jsscript = JSStringCreateWithUTF8CString(script);
379 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
380 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
381 jsscriptname, 0, &exception);
382 JSStringRelease(jsscript);
383 JSStringRelease(jsscriptname);
387 runscript(WebKitWebFrame *frame)
392 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
393 evalscript(webkit_web_frame_get_global_context(frame), script,
399 clipboard(Client *c, const Arg *arg)
401 gboolean paste = *(gboolean *)arg;
404 gtk_clipboard_request_text(gtk_clipboard_get(
405 GDK_SELECTION_PRIMARY),
408 gtk_clipboard_set_text(gtk_clipboard_get(
409 GDK_SELECTION_PRIMARY), c->linkhover
410 ? c->linkhover : geturi(c), -1);
415 copystr(char **str, const char *src)
428 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
432 switch (webkit_navigation_action_get_navigation_type(a)) {
433 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
435 * popup windows of type “other” are almost always triggered
436 * by user gesture, so inverse the logic here
438 /* instead of this, compare destination uri to mouse-over uri for validating window */
439 if (webkit_navigation_action_is_user_gesture(a)) {
443 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
444 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
445 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
446 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
447 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
455 return GTK_WIDGET(n->view);
459 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
460 WebKitPolicyDecisionType dt, Client *c)
463 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
464 decidenavigation(d, c);
466 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
467 decidenewwindow(d, c);
469 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
470 decideresource(d, c);
473 webkit_policy_decision_ignore(d);
480 decidenavigation(WebKitPolicyDecision *d, Client *c)
482 WebKitNavigationAction *a;
484 a = webkit_navigation_policy_decision_get_navigation_action(
485 WEBKIT_NAVIGATION_POLICY_DECISION(d));
487 switch (webkit_navigation_action_get_navigation_type(a)) {
488 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
489 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
490 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
491 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
492 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
493 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
495 /* Do not navigate to links with a "_blank" target (popup) */
496 if (webkit_navigation_policy_decision_get_frame_name(
497 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
498 webkit_policy_decision_ignore(d);
500 /* Filter out navigation to different domain ? */
501 /* get action→urirequest, copy and load in new window+view
503 webkit_policy_decision_use(d);
510 decidenewwindow(WebKitPolicyDecision *d, Client *c)
512 WebKitNavigationAction *a;
515 a = webkit_navigation_policy_decision_get_navigation_action(
516 WEBKIT_NAVIGATION_POLICY_DECISION(d));
518 switch (webkit_navigation_action_get_navigation_type(a)) {
519 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
520 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
521 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
522 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
523 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
524 /* Filter domains here */
525 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
526 * test for link clicked but no button ? */
527 arg.v = webkit_uri_request_get_uri(
528 webkit_navigation_action_get_request(a));
529 newwindow(c, &arg, 0);
531 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
536 webkit_policy_decision_ignore(d);
540 decideresource(WebKitPolicyDecision *d, Client *c)
542 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
543 WebKitURIResponse *res;
545 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
546 webkit_policy_decision_use(d);
548 res = webkit_response_policy_decision_get_response(r);
549 webkit_policy_decision_ignore(d);
555 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
561 destroyclient(Client *c)
565 webkit_web_view_stop_loading(c->view);
566 gtk_widget_destroy(GTK_WIDGET(c->view));
567 gtk_widget_destroy(c->scroll);
568 gtk_widget_destroy(c->vbox);
569 gtk_widget_destroy(c->win);
571 for (p = clients; p && p->next != c; p = p->next)
583 destroywin(GtkWidget* w, Client *c)
589 die(const char *errstr, ...)
593 va_start(ap, errstr);
594 vfprintf(stderr, errstr, ap);
600 find(Client *c, const Arg *arg)
604 s = getatom(c, AtomFind);
605 gboolean forward = *(gboolean *)arg;
606 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
610 fullscreen(Client *c, const Arg *arg)
613 gtk_window_unfullscreen(GTK_WINDOW(c->win));
615 gtk_window_fullscreen(GTK_WINDOW(c->win));
616 c->fullscreen = !c->fullscreen;
620 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
622 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
623 if (allowgeolocation)
624 webkit_permission_request_allow(r);
626 webkit_permission_request_deny(r);
634 getatom(Client *c, int a)
636 static char buf[BUFSIZ];
639 unsigned long ldummy;
640 unsigned char *p = NULL;
642 XGetWindowProperty(dpy, c->xid,
643 atoms[a], 0L, BUFSIZ, False, XA_STRING,
644 &adummy, &idummy, &ldummy, &ldummy, &p);
646 strncpy(buf, (char *)p, LENGTH(buf)-1);
659 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
665 getstyle(const char *uri)
669 if (stylefile != NULL)
672 for (i = 0; i < LENGTH(styles); i++) {
673 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
675 return styles[i].style;
682 setstyle(Client *c, const char *style)
684 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
686 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
690 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
694 webkit_web_view_stop_loading(w);
695 arg = (Arg)PLUMB((char *)uri);
700 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
702 g_signal_connect(G_OBJECT(d), "notify::response",
703 G_CALLBACK(responsereceived), c);
707 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
709 download(c, webkit_download_get_response(d));
710 webkit_download_cancel(d);
714 download(Client *c, WebKitURIResponse *r)
718 a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
723 inspector(Client *c, const Arg *arg)
725 if (enableinspector) {
727 webkit_web_inspector_close(c->inspector);
729 webkit_web_inspector_show(c->inspector);
734 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
736 return WEBKIT_WEB_VIEW(webkit_web_view_new());
740 inspector_show(WebKitWebInspector *i, Client *c)
747 w = webkit_web_inspector_get_web_view(i);
748 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
749 gtk_widget_show(GTK_WIDGET(w));
750 c->isinspecting = true;
756 inspector_close(WebKitWebInspector *i, Client *c)
760 if (!c->isinspecting)
763 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
765 gtk_widget_destroy(w);
766 c->isinspecting = false;
772 inspector_finished(WebKitWebInspector *i, Client *c)
774 g_free(c->inspector);
778 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
782 gboolean processed = FALSE;
784 mods = CLEANMASK(mods);
785 key = gdk_keyval_to_lower(key);
787 for (i = 0; i < LENGTH(keys); i++) {
788 if (key == keys[i].keyval
789 && mods == keys[i].mod
791 keys[i].func(c, &(keys[i].arg));
800 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
803 WebKitHitTestResultContext hc;
805 /* Keep the hit test to know where is the pointer on the next click */
808 hc = webkit_hit_test_result_get_context(h);
811 c->targeturi = webkit_hit_test_result_get_link_uri(h);
813 c->targeturi = webkit_hit_test_result_get_image_uri(h);
814 else if (hc & OnMedia)
815 c->targeturi = webkit_hit_test_result_get_media_uri(h);
822 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
825 case WEBKIT_LOAD_STARTED:
826 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
828 case WEBKIT_LOAD_REDIRECTED:
829 setatom(c, AtomUri, geturi(c));
831 case WEBKIT_LOAD_COMMITTED:
832 if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
833 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
835 setatom(c, AtomUri, geturi(c));
838 setstyle(c, getstyle(geturi(c)));
840 case WEBKIT_LOAD_FINISHED:
841 /* Disabled until we write some WebKitWebExtension for
842 * manipulating the DOM directly.
843 evalscript(c, "document.documentElement.style.overflow = '%s'",
844 enablescrollbars ? "auto" : "hidden");
853 loaduri(Client *c, const Arg *arg)
856 const char *uri = (char *)arg->v;
857 Arg a = { .b = FALSE };
860 if (strcmp(uri, "") == 0)
863 /* In case it's a file path. */
864 if (stat(uri, &st) == 0) {
865 rp = realpath(uri, NULL);
866 u = g_strdup_printf("file://%s", rp);
869 u = g_strrstr(uri, "://") ? g_strdup(uri)
870 : g_strdup_printf("http://%s", uri);
873 setatom(c, AtomUri, uri);
875 /* prevents endless loop */
876 if (strcmp(u, geturi(c)) == 0) {
879 webkit_web_view_load_uri(c->view, u);
881 c->title = copystr(&c->title, u);
888 navigate(Client *c, const Arg *arg)
890 int steps = *(int *)arg;
891 webkit_web_view_go_back_or_forward(c->view, steps);
895 newclient(Client *rc)
900 if (!(c = calloc(1, sizeof(Client))))
901 die("Cannot malloc!\n");
909 c->view = newview(c, rc ? rc->view : NULL);
910 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
916 newview(Client *c, WebKitWebView *rv)
919 WebKitSettings *settings;
920 WebKitUserContentManager *contentmanager;
921 WebKitWebContext *context;
927 webkit_web_view_new_with_related_view(rv));
929 settings = webkit_settings_new_with_settings(
930 "auto-load-images", loadimages,
931 "default-font-size", defaultfontsize,
932 "enable-caret-browsing", enablecaretbrowsing,
933 "enable-developer-extras", enableinspector,
934 "enable-dns-prefetching", enablednsprefetching,
935 "enable-frame-flattening", enableframeflattening,
936 "enable-html5-database", enablecache,
937 "enable-html5-local-storage", enablecache,
938 "enable-javascript", enablescripts,
939 "enable-plugins", enableplugins,
941 if (!(ua = getenv("SURF_USERAGENT")))
943 webkit_settings_set_user_agent(settings, ua);
944 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
945 * for more interesting settings */
947 contentmanager = webkit_user_content_manager_new();
949 context = webkit_web_context_new_with_website_data_manager(
950 webkit_website_data_manager_new(
951 "base-cache-directory", cachedir,
952 "base-data-directory", cachedir,
955 /* rendering process model, can be a shared unique one or one for each
957 webkit_web_context_set_process_model(context,
958 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
960 webkit_web_context_set_tls_errors_policy(context, strictssl ?
961 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
963 webkit_web_context_set_cache_model(context, enablecache ?
964 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
966 /* Currently only works with text file to be compatible with curl */
967 webkit_cookie_manager_set_persistent_storage(
968 webkit_web_context_get_cookie_manager(context), cookiefile,
969 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
971 webkit_cookie_manager_set_accept_policy(
972 webkit_web_context_get_cookie_manager(context),
975 g_signal_connect(G_OBJECT(context), "download-started",
976 G_CALLBACK(downloadstarted), c);
978 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
979 "settings", settings,
980 "user-content-manager", contentmanager,
981 "web-context", context,
985 g_signal_connect(G_OBJECT(v),
987 G_CALLBACK(titlechanged), c);
988 g_signal_connect(G_OBJECT(v),
989 "mouse-target-changed",
990 G_CALLBACK(mousetargetchanged), c);
991 g_signal_connect(G_OBJECT(v),
992 "permission-request",
993 G_CALLBACK(permissionrequested), c);
994 g_signal_connect(G_OBJECT(v),
996 G_CALLBACK(createview), c);
997 g_signal_connect(G_OBJECT(v), "ready-to-show",
998 G_CALLBACK(showview), c);
999 g_signal_connect(G_OBJECT(v),
1001 G_CALLBACK(decidepolicy), c);
1002 g_signal_connect(G_OBJECT(v),
1003 "window-object-cleared",
1004 G_CALLBACK(windowobjectcleared), c);
1005 g_signal_connect(G_OBJECT(v),
1007 G_CALLBACK(loadchanged), c);
1008 g_signal_connect(G_OBJECT(v),
1009 "notify::estimated-load-progress",
1010 G_CALLBACK(progresschanged), c);
1011 g_signal_connect(G_OBJECT(v),
1012 "button-release-event",
1013 G_CALLBACK(buttonreleased), c);
1014 g_signal_connect(G_OBJECT(v),
1015 "resource-request-starting",
1016 G_CALLBACK(beforerequest), c);
1017 g_signal_connect(G_OBJECT(v),
1018 "should-show-delete-interface-for-element",
1019 G_CALLBACK(deletion_interface), c);
1025 showview(WebKitWebView *v, Client *c)
1027 GdkGeometry hints = { 1, 1 };
1028 GdkRGBA bgcolor = { 0 };
1033 c->win = gtk_plug_new(embed);
1035 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1037 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
1038 * is always correct, so we should still call this function.
1039 * But when doing so, we *must* differentiate between a
1040 * WM_CLASS and a resource on the window. By convention, the
1041 * window class (WM_CLASS) is capped, while the resource is in
1042 * lowercase. Both these values come as a pair.
1044 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
1046 /* TA: 20091214: And set the role here as well -- so that
1047 * sessions can pick this up.
1049 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
1051 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
1052 g_signal_connect(G_OBJECT(c->win),
1054 G_CALLBACK(destroywin), c);
1055 g_signal_connect(G_OBJECT(c->win),
1056 "leave_notify_event",
1057 G_CALLBACK(titlechangeleave), c);
1063 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1066 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1067 gtk_widget_show(GTK_WIDGET(c->view));
1068 gtk_widget_show(c->win);
1069 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1070 c->xid = gdk_x11_window_get_xid(gwin);
1071 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1073 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1074 gdk_window_add_filter(gwin, processx, c);
1078 /* This might conflict with _zoomto96dpi_. */
1079 if (zoomlevel != 1.0)
1080 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1082 if (runinfullscreen)
1083 fullscreen(c, NULL);
1085 setatom(c, AtomFind, "");
1086 setatom(c, AtomUri, "about:blank");
1088 webkit_web_view_set_background_color(c->view, &bgcolor);
1091 gdk_display_sync(gtk_widget_get_display(c->win));
1092 printf("%lu\n", c->xid);
1094 if (fclose(stdout) != 0) {
1095 die("Error closing stdout");
1101 newwindow(Client *c, const Arg *arg, gboolean noembed)
1104 const char *cmd[18], *uri;
1105 const Arg a = { .v = (void *)cmd };
1110 cmd[i++] = cookiepolicies;
1111 if (!enablescrollbars)
1113 if (embed && !noembed) {
1115 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1118 if (!allowgeolocation)
1130 if (enablediskcache)
1133 cmd[i++] = cookiefile;
1135 uri = arg->v ? (char *)arg->v : c->linkhover;
1143 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1145 Arg arg = {.v = text };
1147 loaduri((Client *) d, &arg);
1151 print(Client *c, const Arg *arg)
1153 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1157 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1159 Client *c = (Client *)d;
1163 if (((XEvent *)e)->type == PropertyNotify) {
1164 ev = &((XEvent *)e)->xproperty;
1165 if (ev->state == PropertyNewValue) {
1166 if (ev->atom == atoms[AtomFind]) {
1170 return GDK_FILTER_REMOVE;
1171 } else if (ev->atom == atoms[AtomGo]) {
1172 arg.v = getatom(c, AtomGo);
1175 return GDK_FILTER_REMOVE;
1179 return GDK_FILTER_CONTINUE;
1183 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1185 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1191 linkopen(Client *c, const Arg *arg)
1193 newwindow(NULL, arg, 1);
1197 linkopenembed(Client *c, const Arg *arg)
1199 newwindow(NULL, arg, 0);
1203 reload(Client *c, const Arg *arg)
1205 gboolean nocache = *(gboolean *)arg;
1207 webkit_web_view_reload_bypass_cache(c->view);
1209 webkit_web_view_reload(c->view);
1213 scroll_h(Client *c, const Arg *arg)
1215 scroll(gtk_scrolled_window_get_hadjustment(
1216 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1220 scroll_v(Client *c, const Arg *arg)
1222 scroll(gtk_scrolled_window_get_vadjustment(
1223 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1227 scroll(GtkAdjustment *a, const Arg *arg)
1231 v = gtk_adjustment_get_value(a);
1235 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1240 v += gtk_adjustment_get_step_increment(a) * arg->i;
1244 v = MIN(v, gtk_adjustment_get_upper(a) -
1245 gtk_adjustment_get_page_size(a));
1246 gtk_adjustment_set_value(a, v);
1250 setatom(Client *c, int a, const char *v)
1253 XChangeProperty(dpy, c->xid,
1254 atoms[a], XA_STRING, 8, PropModeReplace,
1255 (unsigned char *)v, strlen(v) + 1);
1262 char *styledirfile, *stylepath;
1263 WebKitWebContext *context;
1264 GError *error = NULL;
1266 /* clean up any zombies immediately */
1268 gtk_init(NULL, NULL);
1270 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1273 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1274 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1275 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1277 /* dirs and files */
1278 cookiefile = buildfile(cookiefile);
1279 scriptfile = buildfile(scriptfile);
1280 cachedir = buildpath(cachedir);
1281 if (stylefile == NULL) {
1282 styledir = buildpath(styledir);
1283 for (i = 0; i < LENGTH(styles); i++) {
1284 if (regcomp(&(styles[i].re), styles[i].regex,
1287 "Could not compile regex: %s\n",
1289 styles[i].regex = NULL;
1291 styledirfile = g_strconcat(styledir, "/",
1292 styles[i].style, NULL);
1293 stylepath = buildfile(styledirfile);
1294 styles[i].style = g_strconcat("file://", stylepath,
1296 g_free(styledirfile);
1301 stylepath = buildfile(stylefile);
1302 stylefile = g_strconcat("file://", stylepath, NULL);
1310 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1311 die("Can't install SIGCHLD handler");
1312 while (0 < waitpid(-1, NULL, WNOHANG));
1316 spawn(Client *c, const Arg *arg)
1320 close(ConnectionNumber(dpy));
1322 execvp(((char **)arg->v)[0], (char **)arg->v);
1323 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1330 eval(Client *c, const Arg *arg)
1332 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1333 evalscript(webkit_web_frame_get_global_context(frame),
1334 ((char **)arg->v)[0], "");
1338 stop(Client *c, const Arg *arg)
1340 webkit_web_view_stop_loading(c->view);
1344 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1346 c->title = webkit_web_view_get_title(c->view);
1351 titlechangeleave(void *a, void *b, Client *c)
1353 c->linkhover = NULL;
1358 toggle(Client *c, const Arg *arg)
1360 WebKitWebSettings *settings;
1361 char *name = (char *)arg->v;
1363 Arg a = { .b = FALSE };
1365 settings = webkit_web_view_get_settings(c->view);
1366 g_object_get(G_OBJECT(settings), name, &value, NULL);
1367 g_object_set(G_OBJECT(settings), name, !value, NULL);
1373 togglecookiepolicy(Client *c, const Arg *arg)
1376 cookiepolicy %= strlen(cookiepolicies);
1378 webkit_cookie_manager_set_accept_policy(
1379 webkit_web_context_get_cookie_manager(
1380 webkit_web_view_get_context(c->view)),
1381 cookiepolicy_get());
1384 /* Do not reload. */
1388 togglegeolocation(Client *c, const Arg *arg)
1390 Arg a = { .b = FALSE };
1392 allowgeolocation ^= 1;
1397 twitch(Client *c, const Arg *arg)
1402 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1405 v = gtk_adjustment_get_value(a);
1410 v = MIN(v, gtk_adjustment_get_upper(a) -
1411 gtk_adjustment_get_page_size(a));
1412 gtk_adjustment_set_value(a, v);
1416 togglescrollbars(Client *c, const Arg *arg)
1418 GtkPolicyType vspolicy;
1421 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1424 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1425 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1429 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1430 GTK_POLICY_AUTOMATIC,
1431 GTK_POLICY_AUTOMATIC);
1440 togglestyle(Client *c, const Arg *arg)
1442 enablestyle = !enablestyle;
1443 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1449 gettogglestat(Client *c)
1453 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1455 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1457 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1459 togglestat[p++] = value? 'C': 'c';
1461 togglestat[p++] = allowgeolocation? 'G': 'g';
1463 togglestat[p++] = enablediskcache? 'D': 'd';
1465 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1466 togglestat[p++] = value? 'I': 'i';
1468 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1469 togglestat[p++] = value? 'S': 's';
1471 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1472 togglestat[p++] = value? 'V': 'v';
1474 togglestat[p++] = enablestyle ? 'M': 'm';
1476 togglestat[p] = '\0';
1480 getpagestat(Client *c)
1482 const char *uri = geturi(c);
1484 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
1485 c->tlsflags > 0 ? 'U' : 'T';
1490 updatetitle(Client *c)
1494 if (showindicators) {
1499 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1501 } else if (c->progress != 100) {
1502 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1503 togglestat, pagestat,
1504 c->title == NULL ? "" : c->title);
1506 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1507 c->title == NULL ? "" : c->title);
1510 gtk_window_set_title(GTK_WINDOW(c->win), t);
1513 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1519 updatewinid(Client *c)
1521 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1527 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1528 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1529 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1533 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1534 JSObjectRef win, Client *c)
1540 zoom(Client *c, const Arg *arg)
1545 webkit_web_view_zoom_out(c->view);
1546 } else if (arg->i > 0) {
1548 webkit_web_view_zoom_in(c->view);
1552 webkit_web_view_set_zoom_level(c->view, 1.0);
1557 main(int argc, char *argv[])
1562 memset(&arg, 0, sizeof(arg));
1564 /* command line args */
1567 cookiepolicies = EARGF(usage());
1570 enablescrollbars = 0;
1573 enablescrollbars = 1;
1576 cookiefile = EARGF(usage());
1579 enablediskcache = 0;
1582 enablediskcache = 1;
1585 embed = strtol(EARGF(usage()), NULL, 0);
1588 runinfullscreen = 0;
1591 runinfullscreen = 1;
1594 allowgeolocation = 0;
1597 allowgeolocation = 1;
1618 enableinspector = 0;
1621 enableinspector = 1;
1630 scriptfile = EARGF(usage());
1639 stylefile = EARGF(usage());
1642 useragent = EARGF(usage());
1645 die("surf-"VERSION", ©2009-2015 surf engineers, "
1646 "see LICENSE for details\n");
1651 zoomlevel = strtof(EARGF(usage()), NULL);
1660 c = newclient(NULL);
1663 loaduri(clients, &arg);
1670 return EXIT_SUCCESS;