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,
60 typedef union Arg Arg;
67 typedef struct Client {
71 WebKitWebInspector *inspector;
72 WebKitFindController *finder;
73 WebKitHitTestResult *mousepos;
74 GTlsCertificateFlags tlsflags;
75 const char *title, *targeturi;
85 void (*func)(Client *c, const Arg *arg);
93 void (*func)(Client *c, const Arg *a, WebKitHitTestResult *h);
95 unsigned int stopevent;
105 static Atom atoms[AtomLast];
106 static Client *clients = NULL;
107 static Window embed = 0;
108 static gboolean showxid = FALSE;
109 static char winid[64];
110 static char togglestats[10];
111 static char pagestats[2];
112 static GTlsDatabase *tlsdb;
113 static int cookiepolicy;
114 static char *stylefile = NULL;
115 static const char *useragent;
117 static void addaccelgroup(Client *c);
118 static char *buildfile(const char *path);
119 static char *buildpath(const char *path);
120 static gboolean buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c);
121 static void cleanup(void);
122 static void clipboard(Client *c, const Arg *a);
124 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
125 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
127 static char *copystr(char **str, const char *src);
128 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
130 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
131 WebKitPolicyDecisionType dt, Client *c);
132 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
133 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
134 static void decideresource(WebKitPolicyDecision *d, Client *c);
135 static void closeview(WebKitWebView *v, Client *c);
136 static void destroyclient(Client *c);
137 static void destroywin(GtkWidget* w, Client *c);
138 static void die(const char *errstr, ...);
139 static void evalscript(Client *c, const char *jsstr, ...);
140 static void runscript(Client *c);
141 static void find(Client *c, const Arg *a);
142 static void togglefullscreen(Client *c, const Arg *a);
143 static gboolean permissionrequested(WebKitWebView *v,
144 WebKitPermissionRequest *r, Client *c);
145 static const char *getatom(Client *c, int a);
146 static void gettogglestats(Client *c);
147 static void getpagestats(Client *c);
148 static char *geturi(Client *c);
149 static const gchar *getstyle(const char *uri);
150 static void setstyle(Client *c, const char *stylefile);
152 static void handleplumb(Client *c, const gchar *uri);
154 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
156 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
157 static void download(Client *c, WebKitURIResponse *r);
159 static void toggleinspector(Client *c, const Arg *a);
161 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
162 GdkModifierType mods, Client *c);
163 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
164 guint modifiers, Client *c);
165 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
166 static void loaduri(Client *c, const Arg *a);
167 static void navigate(Client *c, const Arg *a);
168 static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
169 static Client *newclient(Client *c);
170 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
171 static void showview(WebKitWebView *v, Client *c);
172 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
173 static GtkWidget *createwindow(Client *c);
174 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
175 static void print(Client *c, const Arg *a);
176 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
178 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
179 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
180 static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
181 static void reload(Client *c, const Arg *arg);
182 static void scroll_h(Client *c, const Arg *a);
183 static void scroll_v(Client *c, const Arg *a);
184 static void setatom(Client *c, int a, const char *v);
185 static void setup(void);
186 static void sigchld(int unused);
187 static void spawn(Client *c, const Arg *arg);
188 static void stop(Client *c, const Arg *arg);
189 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
190 static void toggle(Client *c, const Arg *a);
191 static void togglecookiepolicy(Client *c, const Arg *arg);
192 static void togglestyle(Client *c, const Arg *arg);
193 static void updatetitle(Client *c);
194 static void updatewinid(Client *c);
195 static void usage(void);
196 static void zoom(Client *c, const Arg *a);
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 buildfile(const char *path)
219 char *dname, *bname, *bpath, *fpath;
222 dname = g_path_get_dirname(path);
223 bname = g_path_get_basename(path);
225 bpath = buildpath(dname);
228 fpath = g_build_filename(bpath, bname, NULL);
232 if (!(f = fopen(fpath, "a")))
233 die("Could not open file: %s\n", fpath);
235 g_chmod(fpath, 0600); /* always */
242 buildpath(const char *path)
245 char *apath, *name, *p, *fpath;
247 if (path[0] == '~') {
248 if (path[1] == '/' || path[1] == '\0') {
249 p = (char *)&path[1];
250 pw = getpwuid(getuid());
252 if ((p = strchr(path, '/')))
253 name = g_strndup(&path[1], --p - path);
255 name = g_strdup(&path[1]);
257 if (!(pw = getpwnam(name))) {
258 die("Can't get user %s home directory: %s.\n",
263 apath = g_build_filename(pw->pw_dir, p, NULL);
265 apath = g_strdup(path);
268 /* creating directory */
269 if (g_mkdir_with_parents(apath, 0700) < 0)
270 die("Could not access directory: %s\n", apath);
272 fpath = realpath(apath, NULL);
279 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
281 WebKitHitTestResultContext element;
282 GdkEventButton *eb = (GdkEventButton*)e;
285 element = webkit_hit_test_result_get_context(c->mousepos);
287 for (i = 0; i < LENGTH(buttons); ++i) {
288 if (element & buttons[i].target &&
289 eb->button == buttons[i].button &&
290 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
292 buttons[i].func(c, &buttons[i].arg, c->mousepos);
293 return buttons[i].stopevent;
304 destroyclient(clients);
311 WebKitCookieAcceptPolicy
312 cookiepolicy_get(void)
314 switch (cookiepolicies[cookiepolicy]) {
316 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
318 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
324 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
328 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
331 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
333 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
335 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
344 evalscript(Client *c, const char *jsstr, ...)
350 script = g_strdup_vprintf(jsstr, ap);
353 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
363 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
364 evalscript(c, script);
369 clipboard(Client *c, const Arg *a)
371 if (a->b) { /* load clipboard uri */
372 gtk_clipboard_request_text(gtk_clipboard_get(
373 GDK_SELECTION_PRIMARY),
375 } else { /* copy uri */
376 gtk_clipboard_set_text(gtk_clipboard_get(
377 GDK_SELECTION_PRIMARY), c->targeturi
378 ? c->targeturi : geturi(c), -1);
383 copystr(char **str, const char *src)
396 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
400 switch (webkit_navigation_action_get_navigation_type(a)) {
401 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
403 * popup windows of type “other” are almost always triggered
404 * by user gesture, so inverse the logic here
406 /* instead of this, compare destination uri to mouse-over uri for validating window */
407 if (webkit_navigation_action_is_user_gesture(a)) {
411 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
412 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
413 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
414 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
415 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
423 return GTK_WIDGET(n->view);
427 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
428 WebKitPolicyDecisionType dt, Client *c)
431 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
432 decidenavigation(d, c);
434 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
435 decidenewwindow(d, c);
437 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
438 decideresource(d, c);
441 webkit_policy_decision_ignore(d);
448 decidenavigation(WebKitPolicyDecision *d, Client *c)
450 WebKitNavigationAction *a;
452 a = webkit_navigation_policy_decision_get_navigation_action(
453 WEBKIT_NAVIGATION_POLICY_DECISION(d));
455 switch (webkit_navigation_action_get_navigation_type(a)) {
456 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
457 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
458 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
459 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
460 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
461 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
463 /* Do not navigate to links with a "_blank" target (popup) */
464 if (webkit_navigation_policy_decision_get_frame_name(
465 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
466 webkit_policy_decision_ignore(d);
468 /* Filter out navigation to different domain ? */
469 /* get action→urirequest, copy and load in new window+view
471 webkit_policy_decision_use(d);
478 decidenewwindow(WebKitPolicyDecision *d, Client *c)
480 WebKitNavigationAction *a;
483 a = webkit_navigation_policy_decision_get_navigation_action(
484 WEBKIT_NAVIGATION_POLICY_DECISION(d));
486 switch (webkit_navigation_action_get_navigation_type(a)) {
487 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
488 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
489 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
490 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
491 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
492 /* Filter domains here */
493 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
494 * test for link clicked but no button ? */
495 arg.v = webkit_uri_request_get_uri(
496 webkit_navigation_action_get_request(a));
497 newwindow(c, &arg, 0);
499 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
504 webkit_policy_decision_ignore(d);
508 decideresource(WebKitPolicyDecision *d, Client *c)
512 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
513 WebKitURIResponse *res;
515 res = webkit_response_policy_decision_get_response(r);
516 uri = webkit_uri_response_get_uri(res);
518 if (g_str_has_suffix(uri, "/favicon.ico"))
519 webkit_uri_request_set_uri(
520 webkit_response_policy_decision_get_request(r),
523 if (!g_str_has_prefix(uri, "http://")
524 && !g_str_has_prefix(uri, "https://")
525 && !g_str_has_prefix(uri, "about:")
526 && !g_str_has_prefix(uri, "file://")
527 && !g_str_has_prefix(uri, "data:")
528 && !g_str_has_prefix(uri, "blob:")
529 && strlen(uri) > 0) {
530 for (i = 0; i < strlen(uri); i++) {
531 if (!g_ascii_isprint(uri[i])) {
538 webkit_policy_decision_ignore(d);
542 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
543 webkit_policy_decision_use(d);
545 webkit_policy_decision_ignore(d);
551 destroyclient(Client *c)
555 webkit_web_view_stop_loading(c->view);
556 /* Not needed, has already been called
557 gtk_widget_destroy(c->win);
560 for (p = clients; p && p->next != c; p = p->next)
570 closeview(WebKitWebView *v, Client *c)
572 gtk_widget_destroy(c->win);
576 destroywin(GtkWidget* w, Client *c)
584 die(const char *errstr, ...)
588 va_start(ap, errstr);
589 vfprintf(stderr, errstr, ap);
595 find(Client *c, const Arg *a)
601 webkit_find_controller_search_next(c->finder);
603 webkit_find_controller_search_previous(c->finder);
605 s = getatom(c, AtomFind);
606 f = webkit_find_controller_get_search_text(c->finder);
608 if (g_strcmp0(f, s) == 0) /* reset search */
609 webkit_find_controller_search(c->finder, "", findopts, G_MAXUINT);
611 webkit_find_controller_search(c->finder, s, findopts, G_MAXUINT);
613 if (strcmp(s, "") == 0)
614 webkit_find_controller_search_finish(c->finder);
619 togglefullscreen(Client *c, const Arg *a)
621 /* toggling value is handled in winevent() */
623 gtk_window_unfullscreen(GTK_WINDOW(c->win));
625 gtk_window_fullscreen(GTK_WINDOW(c->win));
629 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
631 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
632 if (allowgeolocation)
633 webkit_permission_request_allow(r);
635 webkit_permission_request_deny(r);
643 getatom(Client *c, int a)
645 static char buf[BUFSIZ];
648 unsigned long ldummy;
649 unsigned char *p = NULL;
651 XGetWindowProperty(dpy, c->xid,
652 atoms[a], 0L, BUFSIZ, False, XA_STRING,
653 &adummy, &idummy, &ldummy, &ldummy, &p);
655 strncpy(buf, (char *)p, LENGTH(buf)-1);
668 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
674 getstyle(const char *uri)
678 if (stylefile != NULL)
681 for (i = 0; i < LENGTH(styles); i++) {
682 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
684 return styles[i].style;
691 setstyle(Client *c, const char *stylefile)
695 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
696 fprintf(stderr, "Could not read style file: %s\n", stylefile);
700 webkit_user_content_manager_add_style_sheet(
701 webkit_web_view_get_user_content_manager(c->view),
702 webkit_user_style_sheet_new(style,
703 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
704 WEBKIT_USER_STYLE_LEVEL_USER,
711 handleplumb(Client *c, const gchar *uri)
715 arg = (Arg)PLUMB(uri);
720 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
722 g_signal_connect(G_OBJECT(d), "notify::response",
723 G_CALLBACK(responsereceived), c);
727 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
729 download(c, webkit_download_get_response(d));
730 webkit_download_cancel(d);
734 download(Client *c, WebKitURIResponse *r)
738 a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
743 toggleinspector(Client *c, const Arg *a)
745 if (enableinspector) {
746 if (webkit_web_inspector_is_attached(c->inspector))
747 webkit_web_inspector_close(c->inspector);
749 webkit_web_inspector_show(c->inspector);
754 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
758 gboolean processed = FALSE;
760 mods = CLEANMASK(mods);
761 key = gdk_keyval_to_lower(key);
763 for (i = 0; i < LENGTH(keys); i++) {
764 if (key == keys[i].keyval
765 && mods == keys[i].mod
767 keys[i].func(c, &(keys[i].arg));
776 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
779 WebKitHitTestResultContext hc;
781 /* Keep the hit test to know where is the pointer on the next click */
784 hc = webkit_hit_test_result_get_context(h);
787 c->targeturi = webkit_hit_test_result_get_link_uri(h);
789 c->targeturi = webkit_hit_test_result_get_image_uri(h);
790 else if (hc & OnMedia)
791 c->targeturi = webkit_hit_test_result_get_media_uri(h);
798 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
801 case WEBKIT_LOAD_STARTED:
802 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
804 case WEBKIT_LOAD_REDIRECTED:
805 setatom(c, AtomUri, geturi(c));
807 case WEBKIT_LOAD_COMMITTED:
808 if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
809 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
811 setatom(c, AtomUri, geturi(c));
814 setstyle(c, getstyle(geturi(c)));
816 case WEBKIT_LOAD_FINISHED:
817 /* Disabled until we write some WebKitWebExtension for
818 * manipulating the DOM directly.
819 evalscript(c, "document.documentElement.style.overflow = '%s'",
820 enablescrollbars ? "auto" : "hidden");
829 loaduri(Client *c, const Arg *a)
833 const char *uri = (char *)a->v;
835 if (g_strcmp0(uri, "") == 0)
838 if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
840 } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
841 url = g_strdup_printf("file://%s", path);
844 url = g_strdup_printf("http://%s", uri);
847 setatom(c, AtomUri, url);
849 if (strcmp(url, geturi(c)) == 0) {
852 webkit_web_view_load_uri(c->view, url);
853 c->title = geturi(c);
861 navigate(Client *c, const Arg *a)
864 webkit_web_view_go_back(c->view);
866 webkit_web_view_go_forward(c->view);
870 clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
876 newclient(Client *rc)
881 if (!(c = calloc(1, sizeof(Client))))
882 die("Cannot malloc!\n");
890 c->view = newview(c, rc ? rc->view : NULL);
891 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
897 newview(Client *c, WebKitWebView *rv)
900 WebKitSettings *settings;
901 WebKitUserContentManager *contentmanager;
902 WebKitWebContext *context;
907 webkit_web_view_new_with_related_view(rv));
909 settings = webkit_settings_new_with_settings(
910 "auto-load-images", loadimages,
911 "default-font-size", defaultfontsize,
912 "enable-caret-browsing", enablecaretbrowsing,
913 "enable-developer-extras", enableinspector,
914 "enable-dns-prefetching", enablednsprefetching,
915 "enable-frame-flattening", enableframeflattening,
916 "enable-html5-database", enablecache,
917 "enable-html5-local-storage", enablecache,
918 "enable-javascript", enablescripts,
919 "enable-plugins", enableplugins,
921 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
922 * for more interesting settings */
924 if (strcmp(fulluseragent, "")) {
925 webkit_settings_set_user_agent(settings, fulluseragent);
926 } else if (surfuseragent) {
927 webkit_settings_set_user_agent_with_application_details(
928 settings, "Surf", VERSION);
930 useragent = webkit_settings_get_user_agent(settings);
932 contentmanager = webkit_user_content_manager_new();
934 context = webkit_web_context_new_with_website_data_manager(
935 webkit_website_data_manager_new(
936 "base-cache-directory", cachedir,
937 "base-data-directory", cachedir,
940 /* rendering process model, can be a shared unique one or one for each
942 webkit_web_context_set_process_model(context,
943 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
945 webkit_web_context_set_tls_errors_policy(context, strictssl ?
946 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
948 webkit_web_context_set_cache_model(context, enablecache ?
949 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
951 /* Currently only works with text file to be compatible with curl */
952 webkit_cookie_manager_set_persistent_storage(
953 webkit_web_context_get_cookie_manager(context), cookiefile,
954 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
956 webkit_cookie_manager_set_accept_policy(
957 webkit_web_context_get_cookie_manager(context),
960 g_signal_connect(G_OBJECT(context), "download-started",
961 G_CALLBACK(downloadstarted), c);
963 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
964 "settings", settings,
965 "user-content-manager", contentmanager,
966 "web-context", context,
970 g_signal_connect(G_OBJECT(v),
972 G_CALLBACK(titlechanged), c);
973 g_signal_connect(G_OBJECT(v),
974 "mouse-target-changed",
975 G_CALLBACK(mousetargetchanged), c);
976 g_signal_connect(G_OBJECT(v),
977 "permission-request",
978 G_CALLBACK(permissionrequested), c);
979 g_signal_connect(G_OBJECT(v),
981 G_CALLBACK(createview), c);
982 g_signal_connect(G_OBJECT(v), "ready-to-show",
983 G_CALLBACK(showview), c);
984 g_signal_connect(G_OBJECT(v),
986 G_CALLBACK(decidepolicy), c);
987 g_signal_connect(G_OBJECT(v),
989 G_CALLBACK(loadchanged), c);
990 g_signal_connect(G_OBJECT(v),
991 "notify::estimated-load-progress",
992 G_CALLBACK(progresschanged), c);
993 g_signal_connect(G_OBJECT(v),
994 "button-release-event",
995 G_CALLBACK(buttonreleased), c);
996 g_signal_connect(G_OBJECT(v), "close",
997 G_CALLBACK(closeview), c);
1003 showview(WebKitWebView *v, Client *c)
1005 GdkGeometry hints = { 1, 1 };
1006 GdkRGBA bgcolor = { 0 };
1009 c->win = createwindow(c);
1011 if (enableinspector)
1012 c->inspector = webkit_web_view_get_inspector(c->view);
1014 c->finder = webkit_web_view_get_find_controller(c->view);
1020 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1023 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1024 gtk_widget_show(GTK_WIDGET(c->view));
1025 gtk_widget_show(c->win);
1026 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1027 c->xid = gdk_x11_window_get_xid(gwin);
1028 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1030 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1031 gdk_window_add_filter(gwin, processx, c);
1033 if (zoomlevel != 1.0)
1034 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1036 if (runinfullscreen)
1037 togglefullscreen(c, NULL);
1039 setatom(c, AtomFind, "");
1040 setatom(c, AtomUri, "about:blank");
1042 webkit_web_view_set_background_color(c->view, &bgcolor);
1045 gdk_display_sync(gtk_widget_get_display(c->win));
1046 printf("%lu\n", c->xid);
1048 if (fclose(stdout) != 0) {
1049 die("Error closing stdout");
1055 newwindow(Client *c, const Arg *a, int noembed)
1059 const char *cmd[26], *uri;
1060 const Arg arg = { .v = cmd };
1064 cmd[i++] = cookiepolicies;
1065 cmd[i++] = enablescrollbars ? "-B" : "-b";
1066 if (cookiefile && g_strcmp0(cookiefile, "")) {
1068 cmd[i++] = cookiefile;
1070 cmd[i++] = enablecache ? "-D" : "-d";
1071 if (embed && !noembed) {
1073 snprintf(tmp, LENGTH(tmp), "%lu", embed);
1076 cmd[i++] = runinfullscreen ? "-F" : "-f";
1077 cmd[i++] = allowgeolocation ? "-G" : "-g";
1078 cmd[i++] = loadimages ? "-I" : "-i";
1079 cmd[i++] = kioskmode ? "-K" : "-k";
1080 cmd[i++] = enablestyle ? "-M" : "-m";
1081 cmd[i++] = enableinspector ? "-N" : "-n";
1082 cmd[i++] = enableplugins ? "-P" : "-p";
1083 if (scriptfile && g_strcmp0(scriptfile, "")) {
1085 cmd[i++] = scriptfile;
1087 cmd[i++] = enablescripts ? "-S" : "-s";
1088 if (stylefile && g_strcmp0(stylefile, "")) {
1090 cmd[i++] = stylefile;
1092 if (fulluseragent && g_strcmp0(fulluseragent, "")) {
1094 cmd[i++] = fulluseragent;
1098 /* do not keep zoom level */
1108 createwindow(Client *c)
1113 w = gtk_plug_new(embed);
1115 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1117 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
1118 * is always correct, so we should still call this function.
1119 * But when doing so, we *must* differentiate between a
1120 * WM_CLASS and a resource on the window. By convention, the
1121 * window class (WM_CLASS) is capped, while the resource is in
1122 * lowercase. Both these values come as a pair.
1124 gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
1126 /* TA: 20091214: And set the role here as well -- so that
1127 * sessions can pick this up.
1129 gtk_window_set_role(GTK_WINDOW(w), "Surf");
1131 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
1134 g_signal_connect(G_OBJECT(w), "destroy",
1135 G_CALLBACK(destroywin), c);
1136 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1137 G_CALLBACK(winevent), c);
1138 g_signal_connect(G_OBJECT(w), "window-state-event",
1139 G_CALLBACK(winevent), c);
1145 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1147 Arg arg = {.v = text };
1149 loaduri((Client *) d, &arg);
1153 print(Client *c, const Arg *a)
1155 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1156 GTK_WINDOW(c->win));
1160 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1162 Client *c = (Client *)d;
1166 if (((XEvent *)e)->type == PropertyNotify) {
1167 ev = &((XEvent *)e)->xproperty;
1168 if (ev->state == PropertyNewValue) {
1169 if (ev->atom == atoms[AtomFind]) {
1172 return GDK_FILTER_REMOVE;
1173 } else if (ev->atom == atoms[AtomGo]) {
1174 arg.v = getatom(c, AtomGo);
1177 return GDK_FILTER_REMOVE;
1181 return GDK_FILTER_CONTINUE;
1185 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1187 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1193 clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h)
1197 arg.v = webkit_hit_test_result_get_link_uri(h);
1198 newwindow(c, &arg, a->b);
1202 reload(Client *c, const Arg *arg)
1204 gboolean nocache = *(gboolean *)arg;
1206 webkit_web_view_reload_bypass_cache(c->view);
1208 webkit_web_view_reload(c->view);
1212 scroll_h(Client *c, const Arg *a)
1214 evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1219 scroll_v(Client *c, const Arg *a)
1221 evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1226 setatom(Client *c, int a, const char *v)
1229 XChangeProperty(dpy, c->xid,
1230 atoms[a], XA_STRING, 8, PropModeReplace,
1231 (unsigned char *)v, strlen(v) + 1);
1238 WebKitWebContext *context;
1239 GError *error = NULL;
1241 /* clean up any zombies immediately */
1243 gtk_init(NULL, NULL);
1245 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1248 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1249 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1250 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1252 /* dirs and files */
1253 cookiefile = buildfile(cookiefile);
1254 scriptfile = buildfile(scriptfile);
1255 cachedir = buildpath(cachedir);
1257 if (stylefile == NULL) {
1258 styledir = buildpath(styledir);
1259 for (i = 0; i < LENGTH(styles); i++) {
1260 if (regcomp(&(styles[i].re), styles[i].regex,
1263 "Could not compile regex: %s\n",
1265 styles[i].regex = NULL;
1267 styles[i].style = g_strconcat(styledir, "/",
1268 styles[i].style, NULL);
1272 stylefile = buildfile(stylefile);
1279 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1280 die("Can't install SIGCHLD handler");
1281 while (0 < waitpid(-1, NULL, WNOHANG));
1285 spawn(Client *c, const Arg *arg)
1289 close(ConnectionNumber(dpy));
1291 execvp(((char **)arg->v)[0], (char **)arg->v);
1292 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1299 stop(Client *c, const Arg *arg)
1301 webkit_web_view_stop_loading(c->view);
1305 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1307 c->title = webkit_web_view_get_title(c->view);
1312 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1315 case GDK_LEAVE_NOTIFY:
1316 c->targeturi = NULL;
1319 case GDK_WINDOW_STATE: /* fallthrough */
1320 if (e->window_state.changed_mask ==
1321 GDK_WINDOW_STATE_FULLSCREEN) {
1322 c->fullscreen = e->window_state.new_window_state &
1323 GDK_WINDOW_STATE_FULLSCREEN;
1334 toggle(Client *c, const Arg *a)
1338 s = webkit_web_view_get_settings(c->view);
1340 switch ((unsigned int)a->i) {
1342 enablecaretbrowsing = !enablecaretbrowsing;
1343 webkit_settings_set_enable_caret_browsing(s,
1344 enablecaretbrowsing);
1346 return; /* do not reload */
1348 case FrameFlattening:
1349 enableframeflattening = !enableframeflattening;
1350 webkit_settings_set_enable_frame_flattening(s,
1351 enableframeflattening);
1354 allowgeolocation = !allowgeolocation;
1357 enablescripts = !enablescripts;
1358 webkit_settings_set_enable_javascript(s, enablescripts);
1361 loadimages = !loadimages;
1362 webkit_settings_set_auto_load_images(s, loadimages);
1365 enableplugins = !enableplugins;
1366 webkit_settings_set_enable_plugins(s, enableplugins);
1369 /* Disabled until we write some WebKitWebExtension for
1370 * manipulating the DOM directly.
1371 enablescrollbars = !enablescrollbars;
1372 evalscript(c, "document.documentElement.style.overflow = '%s'",
1373 enablescrollbars ? "auto" : "hidden");
1375 return; /* do not reload */
1384 togglecookiepolicy(Client *c, const Arg *arg)
1387 cookiepolicy %= strlen(cookiepolicies);
1389 webkit_cookie_manager_set_accept_policy(
1390 webkit_web_context_get_cookie_manager(
1391 webkit_web_view_get_context(c->view)),
1392 cookiepolicy_get());
1395 /* Do not reload. */
1399 togglestyle(Client *c, const Arg *arg)
1401 enablestyle = !enablestyle;
1402 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1408 gettogglestats(Client *c)
1410 togglestats[0] = cookiepolicy_set(cookiepolicy_get());
1411 togglestats[1] = enablecaretbrowsing ? 'C' : 'c';
1412 togglestats[2] = allowgeolocation ? 'G' : 'g';
1413 togglestats[3] = enablecache ? 'D' : 'd';
1414 togglestats[4] = loadimages ? 'I' : 'i';
1415 togglestats[5] = enablescripts ? 'S': 's';
1416 togglestats[6] = enableplugins ? 'V' : 'v';
1417 togglestats[7] = enablestyle ? 'M' : 'm';
1418 togglestats[8] = enableframeflattening ? 'F' : 'f';
1419 togglestats[9] = '\0';
1423 getpagestats(Client *c)
1425 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
1426 c->tlsflags > 0 ? 'U' : 'T';
1427 pagestats[1] = '\0';
1431 updatetitle(Client *c)
1435 if (showindicators) {
1439 if (c->progress != 100) {
1440 title = g_strdup_printf("[%i%%] %s:%s | %s",
1441 c->progress, togglestats, pagestats,
1442 c->targeturi ? c->targeturi : c->title);
1444 title = g_strdup_printf("%s:%s | %s",
1445 togglestats, pagestats,
1446 c->targeturi ? c->targeturi : c->title);
1449 gtk_window_set_title(GTK_WINDOW(c->win), title);
1452 gtk_window_set_title(GTK_WINDOW(c->win), c->title ?
1458 updatewinid(Client *c)
1460 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1466 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1467 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1468 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1472 zoom(Client *c, const Arg *a)
1475 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1477 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1479 webkit_web_view_set_zoom_level(c->view, 1.0);
1481 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1485 main(int argc, char *argv[])
1490 memset(&arg, 0, sizeof(arg));
1492 /* command line args */
1495 cookiepolicies = EARGF(usage());
1498 enablescrollbars = 0;
1501 enablescrollbars = 1;
1504 cookiefile = EARGF(usage());
1513 embed = strtol(EARGF(usage()), NULL, 0);
1516 runinfullscreen = 0;
1519 runinfullscreen = 1;
1522 allowgeolocation = 0;
1525 allowgeolocation = 1;
1546 enableinspector = 0;
1549 enableinspector = 1;
1558 scriptfile = EARGF(usage());
1567 stylefile = EARGF(usage());
1570 useragent = EARGF(usage());
1573 die("surf-"VERSION", ©2009-2015 surf engineers, "
1574 "see LICENSE for details\n");
1579 zoomlevel = strtof(EARGF(usage()), NULL);
1588 c = newclient(NULL);
1591 loaduri(clients, &arg);
1598 return EXIT_SUCCESS;