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 char *buildfile(const char *path);
107 static char *buildpath(const char *path);
108 static gboolean buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c);
109 static void cleanup(void);
110 static void clipboard(Client *c, const Arg *arg);
112 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
113 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
115 static char *copystr(char **str, const char *src);
116 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
118 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
119 WebKitPolicyDecisionType dt, Client *c);
120 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
121 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
122 static void decideresource(WebKitPolicyDecision *d, Client *c);
123 static void destroyclient(Client *c);
124 static void destroywin(GtkWidget* w, Client *c);
125 static void die(const char *errstr, ...);
126 static void eval(Client *c, const Arg *arg);
127 static void find(Client *c, const Arg *arg);
128 static void fullscreen(Client *c, const Arg *arg);
129 static gboolean permissionrequested(WebKitWebView *v,
130 WebKitPermissionRequest *r, Client *c);
131 static const char *getatom(Client *c, int a);
132 static void gettogglestat(Client *c);
133 static void getpagestat(Client *c);
134 static char *geturi(Client *c);
135 static const gchar *getstyle(const char *uri);
136 static void setstyle(Client *c, const char *style);
138 static void handleplumb(Client *c, const gchar *uri);
140 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
142 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
143 static void download(Client *c, WebKitURIResponse *r);
145 static void inspector(Client *c, const Arg *arg);
146 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
148 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
149 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
150 static void inspector_finished(WebKitWebInspector *i, Client *c);
152 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
153 GdkModifierType mods, Client *c);
154 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
155 guint modifiers, Client *c);
156 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
157 static void loaduri(Client *c, const Arg *arg);
158 static void navigate(Client *c, const Arg *arg);
159 static Client *newclient(Client *c);
160 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
161 static void showview(WebKitWebView *v, Client *c);
162 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
163 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
164 static void print(Client *c, const Arg *arg);
165 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
167 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
168 static void linkopen(Client *c, const Arg *arg);
169 static void linkopenembed(Client *c, const Arg *arg);
170 static void reload(Client *c, const Arg *arg);
171 static void scroll_h(Client *c, const Arg *arg);
172 static void scroll_v(Client *c, const Arg *arg);
173 static void scroll(GtkAdjustment *a, const Arg *arg);
174 static void setatom(Client *c, int a, const char *v);
175 static void setup(void);
176 static void sigchld(int unused);
177 static void spawn(Client *c, const Arg *arg);
178 static void stop(Client *c, const Arg *arg);
179 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
180 static void titlechangeleave(void *a, void *b, Client *c);
181 static void toggle(Client *c, const Arg *arg);
182 static void togglecookiepolicy(Client *c, const Arg *arg);
183 static void togglegeolocation(Client *c, const Arg *arg);
184 static void togglescrollbars(Client *c, const Arg *arg);
185 static void togglestyle(Client *c, const Arg *arg);
186 static void updatetitle(Client *c);
187 static void updatewinid(Client *c);
188 static void usage(void);
189 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
190 JSContextRef js, JSObjectRef win, Client *c);
191 static void zoom(Client *c, const Arg *arg);
193 /* configuration, allows nested code to access above variables */
197 addaccelgroup(Client *c)
200 GtkAccelGroup *group = gtk_accel_group_new();
203 for (i = 0; i < LENGTH(keys); i++) {
204 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
205 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
208 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
212 buildfile(const char *path)
214 char *dname, *bname, *bpath, *fpath;
217 dname = g_path_get_dirname(path);
218 bname = g_path_get_basename(path);
220 bpath = buildpath(dname);
223 fpath = g_build_filename(bpath, bname, NULL);
227 if (!(f = fopen(fpath, "a")))
228 die("Could not open file: %s\n", fpath);
230 g_chmod(fpath, 0600); /* always */
237 buildpath(const char *path)
240 char *apath, *name, *p, *fpath;
242 if (path[0] == '~') {
243 if (path[1] == '/' || path[1] == '\0') {
244 p = (char *)&path[1];
245 pw = getpwuid(getuid());
247 if ((p = strchr(path, '/')))
248 name = g_strndup(&path[1], --p - path);
250 name = g_strdup(&path[1]);
252 if (!(pw = getpwnam(name))) {
253 die("Can't get user %s home directory: %s.\n",
258 apath = g_build_filename(pw->pw_dir, p, NULL);
260 apath = g_strdup(path);
263 /* creating directory */
264 if (g_mkdir_with_parents(apath, 0700) < 0)
265 die("Could not access directory: %s\n", apath);
267 fpath = realpath(apath, NULL);
274 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
276 WebKitHitTestResultContext element;
277 GdkEventButton *eb = (GdkEventButton*)e;
280 element = webkit_hit_test_result_get_context(c->mousepos);
282 for (i = 0; i < LENGTH(buttons); ++i) {
283 if (element & buttons[i].target &&
284 eb->button == buttons[i].button &&
285 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
287 buttons[i].func(c, &buttons[i].arg, c->mousepos);
288 return buttons[i].stopevent;
299 destroyclient(clients);
306 WebKitCookieAcceptPolicy
307 cookiepolicy_get(void)
309 switch (cookiepolicies[cookiepolicy]) {
311 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
313 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
319 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
323 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
326 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
328 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
330 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
339 evalscript(JSContextRef js, char *script, char* scriptname)
341 JSStringRef jsscript, jsscriptname;
342 JSValueRef exception = NULL;
344 jsscript = JSStringCreateWithUTF8CString(script);
345 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
346 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
347 jsscriptname, 0, &exception);
348 JSStringRelease(jsscript);
349 JSStringRelease(jsscriptname);
353 runscript(WebKitWebFrame *frame)
358 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
359 evalscript(webkit_web_frame_get_global_context(frame), script,
365 clipboard(Client *c, const Arg *arg)
367 gboolean paste = *(gboolean *)arg;
370 gtk_clipboard_request_text(gtk_clipboard_get(
371 GDK_SELECTION_PRIMARY),
374 gtk_clipboard_set_text(gtk_clipboard_get(
375 GDK_SELECTION_PRIMARY), c->linkhover
376 ? c->linkhover : geturi(c), -1);
381 copystr(char **str, const char *src)
394 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
398 switch (webkit_navigation_action_get_navigation_type(a)) {
399 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
401 * popup windows of type “other” are almost always triggered
402 * by user gesture, so inverse the logic here
404 /* instead of this, compare destination uri to mouse-over uri for validating window */
405 if (webkit_navigation_action_is_user_gesture(a)) {
409 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
410 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
411 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
412 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
413 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
421 return GTK_WIDGET(n->view);
425 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
426 WebKitPolicyDecisionType dt, Client *c)
429 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
430 decidenavigation(d, c);
432 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
433 decidenewwindow(d, c);
435 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
436 decideresource(d, c);
439 webkit_policy_decision_ignore(d);
446 decidenavigation(WebKitPolicyDecision *d, Client *c)
448 WebKitNavigationAction *a;
450 a = webkit_navigation_policy_decision_get_navigation_action(
451 WEBKIT_NAVIGATION_POLICY_DECISION(d));
453 switch (webkit_navigation_action_get_navigation_type(a)) {
454 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
455 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
456 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
457 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
458 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
459 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
461 /* Do not navigate to links with a "_blank" target (popup) */
462 if (webkit_navigation_policy_decision_get_frame_name(
463 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
464 webkit_policy_decision_ignore(d);
466 /* Filter out navigation to different domain ? */
467 /* get action→urirequest, copy and load in new window+view
469 webkit_policy_decision_use(d);
476 decidenewwindow(WebKitPolicyDecision *d, Client *c)
478 WebKitNavigationAction *a;
481 a = webkit_navigation_policy_decision_get_navigation_action(
482 WEBKIT_NAVIGATION_POLICY_DECISION(d));
484 switch (webkit_navigation_action_get_navigation_type(a)) {
485 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
486 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
487 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
488 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
489 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
490 /* Filter domains here */
491 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
492 * test for link clicked but no button ? */
493 arg.v = webkit_uri_request_get_uri(
494 webkit_navigation_action_get_request(a));
495 newwindow(c, &arg, 0);
497 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
502 webkit_policy_decision_ignore(d);
506 decideresource(WebKitPolicyDecision *d, Client *c)
510 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
511 WebKitURIResponse *res;
513 res = webkit_response_policy_decision_get_response(r);
514 uri = webkit_uri_response_get_uri(res);
516 if (g_str_has_suffix(uri, "/favicon.ico"))
517 webkit_uri_request_set_uri(
518 webkit_response_policy_decision_get_request(r),
521 if (!g_str_has_prefix(uri, "http://")
522 && !g_str_has_prefix(uri, "https://")
523 && !g_str_has_prefix(uri, "about:")
524 && !g_str_has_prefix(uri, "file://")
525 && !g_str_has_prefix(uri, "data:")
526 && !g_str_has_prefix(uri, "blob:")
527 && strlen(uri) > 0) {
528 for (i = 0; i < strlen(uri); i++) {
529 if (!g_ascii_isprint(uri[i])) {
536 webkit_policy_decision_ignore(d);
540 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
541 webkit_policy_decision_use(d);
543 webkit_policy_decision_ignore(d);
549 destroyclient(Client *c)
553 webkit_web_view_stop_loading(c->view);
554 gtk_widget_destroy(GTK_WIDGET(c->view));
555 gtk_widget_destroy(c->scroll);
556 gtk_widget_destroy(c->vbox);
557 gtk_widget_destroy(c->win);
559 for (p = clients; p && p->next != c; p = p->next)
571 destroywin(GtkWidget* w, Client *c)
577 die(const char *errstr, ...)
581 va_start(ap, errstr);
582 vfprintf(stderr, errstr, ap);
588 find(Client *c, const Arg *arg)
592 s = getatom(c, AtomFind);
593 gboolean forward = *(gboolean *)arg;
594 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
598 fullscreen(Client *c, const Arg *arg)
601 gtk_window_unfullscreen(GTK_WINDOW(c->win));
603 gtk_window_fullscreen(GTK_WINDOW(c->win));
604 c->fullscreen = !c->fullscreen;
608 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
610 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
611 if (allowgeolocation)
612 webkit_permission_request_allow(r);
614 webkit_permission_request_deny(r);
622 getatom(Client *c, int a)
624 static char buf[BUFSIZ];
627 unsigned long ldummy;
628 unsigned char *p = NULL;
630 XGetWindowProperty(dpy, c->xid,
631 atoms[a], 0L, BUFSIZ, False, XA_STRING,
632 &adummy, &idummy, &ldummy, &ldummy, &p);
634 strncpy(buf, (char *)p, LENGTH(buf)-1);
647 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
653 getstyle(const char *uri)
657 if (stylefile != NULL)
660 for (i = 0; i < LENGTH(styles); i++) {
661 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
663 return styles[i].style;
670 setstyle(Client *c, const char *style)
672 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
674 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
678 handleplumb(Client *c, const gchar *uri)
682 arg = (Arg)PLUMB(uri);
687 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
689 g_signal_connect(G_OBJECT(d), "notify::response",
690 G_CALLBACK(responsereceived), c);
694 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
696 download(c, webkit_download_get_response(d));
697 webkit_download_cancel(d);
701 download(Client *c, WebKitURIResponse *r)
705 a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
710 inspector(Client *c, const Arg *arg)
712 if (enableinspector) {
714 webkit_web_inspector_close(c->inspector);
716 webkit_web_inspector_show(c->inspector);
721 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
723 return WEBKIT_WEB_VIEW(webkit_web_view_new());
727 inspector_show(WebKitWebInspector *i, Client *c)
734 w = webkit_web_inspector_get_web_view(i);
735 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
736 gtk_widget_show(GTK_WIDGET(w));
737 c->isinspecting = true;
743 inspector_close(WebKitWebInspector *i, Client *c)
747 if (!c->isinspecting)
750 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
752 gtk_widget_destroy(w);
753 c->isinspecting = false;
759 inspector_finished(WebKitWebInspector *i, Client *c)
761 g_free(c->inspector);
765 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
769 gboolean processed = FALSE;
771 mods = CLEANMASK(mods);
772 key = gdk_keyval_to_lower(key);
774 for (i = 0; i < LENGTH(keys); i++) {
775 if (key == keys[i].keyval
776 && mods == keys[i].mod
778 keys[i].func(c, &(keys[i].arg));
787 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
790 WebKitHitTestResultContext hc;
792 /* Keep the hit test to know where is the pointer on the next click */
795 hc = webkit_hit_test_result_get_context(h);
798 c->targeturi = webkit_hit_test_result_get_link_uri(h);
800 c->targeturi = webkit_hit_test_result_get_image_uri(h);
801 else if (hc & OnMedia)
802 c->targeturi = webkit_hit_test_result_get_media_uri(h);
809 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
812 case WEBKIT_LOAD_STARTED:
813 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
815 case WEBKIT_LOAD_REDIRECTED:
816 setatom(c, AtomUri, geturi(c));
818 case WEBKIT_LOAD_COMMITTED:
819 if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
820 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
822 setatom(c, AtomUri, geturi(c));
825 setstyle(c, getstyle(geturi(c)));
827 case WEBKIT_LOAD_FINISHED:
828 /* Disabled until we write some WebKitWebExtension for
829 * manipulating the DOM directly.
830 evalscript(c, "document.documentElement.style.overflow = '%s'",
831 enablescrollbars ? "auto" : "hidden");
840 loaduri(Client *c, const Arg *arg)
843 const char *uri = (char *)arg->v;
844 Arg a = { .b = FALSE };
847 if (strcmp(uri, "") == 0)
850 /* In case it's a file path. */
851 if (stat(uri, &st) == 0) {
852 rp = realpath(uri, NULL);
853 u = g_strdup_printf("file://%s", rp);
856 u = g_strrstr(uri, "://") ? g_strdup(uri)
857 : g_strdup_printf("http://%s", uri);
860 setatom(c, AtomUri, uri);
862 /* prevents endless loop */
863 if (strcmp(u, geturi(c)) == 0) {
866 webkit_web_view_load_uri(c->view, u);
868 c->title = copystr(&c->title, u);
875 navigate(Client *c, const Arg *arg)
877 int steps = *(int *)arg;
878 webkit_web_view_go_back_or_forward(c->view, steps);
882 newclient(Client *rc)
887 if (!(c = calloc(1, sizeof(Client))))
888 die("Cannot malloc!\n");
896 c->view = newview(c, rc ? rc->view : NULL);
897 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
903 newview(Client *c, WebKitWebView *rv)
906 WebKitSettings *settings;
907 WebKitUserContentManager *contentmanager;
908 WebKitWebContext *context;
914 webkit_web_view_new_with_related_view(rv));
916 settings = webkit_settings_new_with_settings(
917 "auto-load-images", loadimages,
918 "default-font-size", defaultfontsize,
919 "enable-caret-browsing", enablecaretbrowsing,
920 "enable-developer-extras", enableinspector,
921 "enable-dns-prefetching", enablednsprefetching,
922 "enable-frame-flattening", enableframeflattening,
923 "enable-html5-database", enablecache,
924 "enable-html5-local-storage", enablecache,
925 "enable-javascript", enablescripts,
926 "enable-plugins", enableplugins,
928 if (!(ua = getenv("SURF_USERAGENT")))
930 webkit_settings_set_user_agent(settings, ua);
931 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
932 * for more interesting settings */
934 contentmanager = webkit_user_content_manager_new();
936 context = webkit_web_context_new_with_website_data_manager(
937 webkit_website_data_manager_new(
938 "base-cache-directory", cachedir,
939 "base-data-directory", cachedir,
942 /* rendering process model, can be a shared unique one or one for each
944 webkit_web_context_set_process_model(context,
945 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
947 webkit_web_context_set_tls_errors_policy(context, strictssl ?
948 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
950 webkit_web_context_set_cache_model(context, enablecache ?
951 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
953 /* Currently only works with text file to be compatible with curl */
954 webkit_cookie_manager_set_persistent_storage(
955 webkit_web_context_get_cookie_manager(context), cookiefile,
956 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
958 webkit_cookie_manager_set_accept_policy(
959 webkit_web_context_get_cookie_manager(context),
962 g_signal_connect(G_OBJECT(context), "download-started",
963 G_CALLBACK(downloadstarted), c);
965 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
966 "settings", settings,
967 "user-content-manager", contentmanager,
968 "web-context", context,
972 g_signal_connect(G_OBJECT(v),
974 G_CALLBACK(titlechanged), c);
975 g_signal_connect(G_OBJECT(v),
976 "mouse-target-changed",
977 G_CALLBACK(mousetargetchanged), c);
978 g_signal_connect(G_OBJECT(v),
979 "permission-request",
980 G_CALLBACK(permissionrequested), c);
981 g_signal_connect(G_OBJECT(v),
983 G_CALLBACK(createview), c);
984 g_signal_connect(G_OBJECT(v), "ready-to-show",
985 G_CALLBACK(showview), c);
986 g_signal_connect(G_OBJECT(v),
988 G_CALLBACK(decidepolicy), c);
989 g_signal_connect(G_OBJECT(v),
990 "window-object-cleared",
991 G_CALLBACK(windowobjectcleared), c);
992 g_signal_connect(G_OBJECT(v),
994 G_CALLBACK(loadchanged), c);
995 g_signal_connect(G_OBJECT(v),
996 "notify::estimated-load-progress",
997 G_CALLBACK(progresschanged), c);
998 g_signal_connect(G_OBJECT(v),
999 "button-release-event",
1000 G_CALLBACK(buttonreleased), c);
1006 showview(WebKitWebView *v, Client *c)
1008 GdkGeometry hints = { 1, 1 };
1009 GdkRGBA bgcolor = { 0 };
1014 c->win = gtk_plug_new(embed);
1016 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1018 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
1019 * is always correct, so we should still call this function.
1020 * But when doing so, we *must* differentiate between a
1021 * WM_CLASS and a resource on the window. By convention, the
1022 * window class (WM_CLASS) is capped, while the resource is in
1023 * lowercase. Both these values come as a pair.
1025 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
1027 /* TA: 20091214: And set the role here as well -- so that
1028 * sessions can pick this up.
1030 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
1032 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
1033 g_signal_connect(G_OBJECT(c->win),
1035 G_CALLBACK(destroywin), c);
1036 g_signal_connect(G_OBJECT(c->win),
1037 "leave_notify_event",
1038 G_CALLBACK(titlechangeleave), c);
1044 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1047 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1048 gtk_widget_show(GTK_WIDGET(c->view));
1049 gtk_widget_show(c->win);
1050 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1051 c->xid = gdk_x11_window_get_xid(gwin);
1052 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1054 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1055 gdk_window_add_filter(gwin, processx, c);
1059 /* This might conflict with _zoomto96dpi_. */
1060 if (zoomlevel != 1.0)
1061 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1063 if (runinfullscreen)
1064 fullscreen(c, NULL);
1066 setatom(c, AtomFind, "");
1067 setatom(c, AtomUri, "about:blank");
1069 webkit_web_view_set_background_color(c->view, &bgcolor);
1072 gdk_display_sync(gtk_widget_get_display(c->win));
1073 printf("%lu\n", c->xid);
1075 if (fclose(stdout) != 0) {
1076 die("Error closing stdout");
1082 newwindow(Client *c, const Arg *arg, gboolean noembed)
1085 const char *cmd[18], *uri;
1086 const Arg a = { .v = (void *)cmd };
1091 cmd[i++] = cookiepolicies;
1092 if (!enablescrollbars)
1094 if (embed && !noembed) {
1096 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1099 if (!allowgeolocation)
1111 if (enablediskcache)
1114 cmd[i++] = cookiefile;
1116 uri = arg->v ? (char *)arg->v : c->linkhover;
1124 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1126 Arg arg = {.v = text };
1128 loaduri((Client *) d, &arg);
1132 print(Client *c, const Arg *arg)
1134 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1138 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1140 Client *c = (Client *)d;
1144 if (((XEvent *)e)->type == PropertyNotify) {
1145 ev = &((XEvent *)e)->xproperty;
1146 if (ev->state == PropertyNewValue) {
1147 if (ev->atom == atoms[AtomFind]) {
1151 return GDK_FILTER_REMOVE;
1152 } else if (ev->atom == atoms[AtomGo]) {
1153 arg.v = getatom(c, AtomGo);
1156 return GDK_FILTER_REMOVE;
1160 return GDK_FILTER_CONTINUE;
1164 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1166 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1172 linkopen(Client *c, const Arg *arg)
1174 newwindow(NULL, arg, 1);
1178 linkopenembed(Client *c, const Arg *arg)
1180 newwindow(NULL, arg, 0);
1184 reload(Client *c, const Arg *arg)
1186 gboolean nocache = *(gboolean *)arg;
1188 webkit_web_view_reload_bypass_cache(c->view);
1190 webkit_web_view_reload(c->view);
1194 scroll_h(Client *c, const Arg *arg)
1196 scroll(gtk_scrolled_window_get_hadjustment(
1197 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1201 scroll_v(Client *c, const Arg *arg)
1203 scroll(gtk_scrolled_window_get_vadjustment(
1204 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1208 scroll(GtkAdjustment *a, const Arg *arg)
1212 v = gtk_adjustment_get_value(a);
1216 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1221 v += gtk_adjustment_get_step_increment(a) * arg->i;
1225 v = MIN(v, gtk_adjustment_get_upper(a) -
1226 gtk_adjustment_get_page_size(a));
1227 gtk_adjustment_set_value(a, v);
1231 setatom(Client *c, int a, const char *v)
1234 XChangeProperty(dpy, c->xid,
1235 atoms[a], XA_STRING, 8, PropModeReplace,
1236 (unsigned char *)v, strlen(v) + 1);
1243 char *styledirfile, *stylepath;
1244 WebKitWebContext *context;
1245 GError *error = NULL;
1247 /* clean up any zombies immediately */
1249 gtk_init(NULL, NULL);
1251 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1254 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1255 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1256 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1258 /* dirs and files */
1259 cookiefile = buildfile(cookiefile);
1260 scriptfile = buildfile(scriptfile);
1261 cachedir = buildpath(cachedir);
1262 if (stylefile == NULL) {
1263 styledir = buildpath(styledir);
1264 for (i = 0; i < LENGTH(styles); i++) {
1265 if (regcomp(&(styles[i].re), styles[i].regex,
1268 "Could not compile regex: %s\n",
1270 styles[i].regex = NULL;
1272 styledirfile = g_strconcat(styledir, "/",
1273 styles[i].style, NULL);
1274 stylepath = buildfile(styledirfile);
1275 styles[i].style = g_strconcat("file://", stylepath,
1277 g_free(styledirfile);
1282 stylepath = buildfile(stylefile);
1283 stylefile = g_strconcat("file://", stylepath, NULL);
1291 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1292 die("Can't install SIGCHLD handler");
1293 while (0 < waitpid(-1, NULL, WNOHANG));
1297 spawn(Client *c, const Arg *arg)
1301 close(ConnectionNumber(dpy));
1303 execvp(((char **)arg->v)[0], (char **)arg->v);
1304 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1311 eval(Client *c, const Arg *arg)
1313 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1314 evalscript(webkit_web_frame_get_global_context(frame),
1315 ((char **)arg->v)[0], "");
1319 stop(Client *c, const Arg *arg)
1321 webkit_web_view_stop_loading(c->view);
1325 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1327 c->title = webkit_web_view_get_title(c->view);
1332 titlechangeleave(void *a, void *b, Client *c)
1334 c->linkhover = NULL;
1339 toggle(Client *c, const Arg *arg)
1341 WebKitWebSettings *settings;
1342 char *name = (char *)arg->v;
1344 Arg a = { .b = FALSE };
1346 settings = webkit_web_view_get_settings(c->view);
1347 g_object_get(G_OBJECT(settings), name, &value, NULL);
1348 g_object_set(G_OBJECT(settings), name, !value, NULL);
1354 togglecookiepolicy(Client *c, const Arg *arg)
1357 cookiepolicy %= strlen(cookiepolicies);
1359 webkit_cookie_manager_set_accept_policy(
1360 webkit_web_context_get_cookie_manager(
1361 webkit_web_view_get_context(c->view)),
1362 cookiepolicy_get());
1365 /* Do not reload. */
1369 togglegeolocation(Client *c, const Arg *arg)
1371 Arg a = { .b = FALSE };
1373 allowgeolocation ^= 1;
1378 twitch(Client *c, const Arg *arg)
1383 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1386 v = gtk_adjustment_get_value(a);
1391 v = MIN(v, gtk_adjustment_get_upper(a) -
1392 gtk_adjustment_get_page_size(a));
1393 gtk_adjustment_set_value(a, v);
1397 togglescrollbars(Client *c, const Arg *arg)
1399 GtkPolicyType vspolicy;
1402 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1405 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1406 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1410 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1411 GTK_POLICY_AUTOMATIC,
1412 GTK_POLICY_AUTOMATIC);
1421 togglestyle(Client *c, const Arg *arg)
1423 enablestyle = !enablestyle;
1424 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1430 gettogglestat(Client *c)
1434 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1436 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1438 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1440 togglestat[p++] = value? 'C': 'c';
1442 togglestat[p++] = allowgeolocation? 'G': 'g';
1444 togglestat[p++] = enablediskcache? 'D': 'd';
1446 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1447 togglestat[p++] = value? 'I': 'i';
1449 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1450 togglestat[p++] = value? 'S': 's';
1452 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1453 togglestat[p++] = value? 'V': 'v';
1455 togglestat[p++] = enablestyle ? 'M': 'm';
1457 togglestat[p] = '\0';
1461 getpagestat(Client *c)
1463 const char *uri = geturi(c);
1465 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
1466 c->tlsflags > 0 ? 'U' : 'T';
1471 updatetitle(Client *c)
1475 if (showindicators) {
1480 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1482 } else if (c->progress != 100) {
1483 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1484 togglestat, pagestat,
1485 c->title == NULL ? "" : c->title);
1487 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1488 c->title == NULL ? "" : c->title);
1491 gtk_window_set_title(GTK_WINDOW(c->win), t);
1494 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1500 updatewinid(Client *c)
1502 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1508 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1509 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1510 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1514 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1515 JSObjectRef win, Client *c)
1521 zoom(Client *c, const Arg *arg)
1526 webkit_web_view_zoom_out(c->view);
1527 } else if (arg->i > 0) {
1529 webkit_web_view_zoom_in(c->view);
1533 webkit_web_view_set_zoom_level(c->view, 1.0);
1538 main(int argc, char *argv[])
1543 memset(&arg, 0, sizeof(arg));
1545 /* command line args */
1548 cookiepolicies = EARGF(usage());
1551 enablescrollbars = 0;
1554 enablescrollbars = 1;
1557 cookiefile = EARGF(usage());
1560 enablediskcache = 0;
1563 enablediskcache = 1;
1566 embed = strtol(EARGF(usage()), NULL, 0);
1569 runinfullscreen = 0;
1572 runinfullscreen = 1;
1575 allowgeolocation = 0;
1578 allowgeolocation = 1;
1599 enableinspector = 0;
1602 enableinspector = 1;
1611 scriptfile = EARGF(usage());
1620 stylefile = EARGF(usage());
1623 useragent = EARGF(usage());
1626 die("surf-"VERSION", ©2009-2015 surf engineers, "
1627 "see LICENSE for details\n");
1632 zoomlevel = strtof(EARGF(usage()), NULL);
1641 c = newclient(NULL);
1644 loaduri(clients, &arg);
1651 return EXIT_SUCCESS;