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 *a);
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 closeview(WebKitWebView *v, Client *c);
124 static void destroyclient(Client *c);
125 static void destroywin(GtkWidget* w, Client *c);
126 static void die(const char *errstr, ...);
127 static void evalscript(Client *c, const char *jsstr, ...);
128 static void runscript(Client *c);
129 static void find(Client *c, const Arg *arg);
130 static void togglefullscreen(Client *c, const Arg *a);
131 static gboolean permissionrequested(WebKitWebView *v,
132 WebKitPermissionRequest *r, Client *c);
133 static const char *getatom(Client *c, int a);
134 static void gettogglestat(Client *c);
135 static void getpagestat(Client *c);
136 static char *geturi(Client *c);
137 static const gchar *getstyle(const char *uri);
138 static void setstyle(Client *c, const char *stylefile);
140 static void handleplumb(Client *c, const gchar *uri);
142 static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
144 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
145 static void download(Client *c, WebKitURIResponse *r);
147 static void inspector(Client *c, const Arg *arg);
148 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
150 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
151 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
152 static void inspector_finished(WebKitWebInspector *i, Client *c);
154 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
155 GdkModifierType mods, Client *c);
156 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
157 guint modifiers, Client *c);
158 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
159 static void loaduri(Client *c, const Arg *arg);
160 static void navigate(Client *c, const Arg *arg);
161 static Client *newclient(Client *c);
162 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
163 static void showview(WebKitWebView *v, Client *c);
164 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
165 static GtkWidget *createwindow(Client *c);
166 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
167 static void print(Client *c, const Arg *arg);
168 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
170 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
171 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
172 static void linkopen(Client *c, const Arg *arg);
173 static void linkopenembed(Client *c, const Arg *arg);
174 static void reload(Client *c, const Arg *arg);
175 static void scroll_h(Client *c, const Arg *arg);
176 static void scroll_v(Client *c, const Arg *arg);
177 static void scroll(GtkAdjustment *a, const Arg *arg);
178 static void setatom(Client *c, int a, const char *v);
179 static void setup(void);
180 static void sigchld(int unused);
181 static void spawn(Client *c, const Arg *arg);
182 static void stop(Client *c, const Arg *arg);
183 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
184 static void toggle(Client *c, const Arg *arg);
185 static void togglecookiepolicy(Client *c, const Arg *arg);
186 static void togglegeolocation(Client *c, const Arg *arg);
187 static void togglescrollbars(Client *c, const Arg *arg);
188 static void togglestyle(Client *c, const Arg *arg);
189 static void updatetitle(Client *c);
190 static void updatewinid(Client *c);
191 static void usage(void);
192 static void zoom(Client *c, const Arg *arg);
194 /* configuration, allows nested code to access above variables */
198 addaccelgroup(Client *c)
201 GtkAccelGroup *group = gtk_accel_group_new();
204 for (i = 0; i < LENGTH(keys); i++) {
205 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
206 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
209 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
213 buildfile(const char *path)
215 char *dname, *bname, *bpath, *fpath;
218 dname = g_path_get_dirname(path);
219 bname = g_path_get_basename(path);
221 bpath = buildpath(dname);
224 fpath = g_build_filename(bpath, bname, NULL);
228 if (!(f = fopen(fpath, "a")))
229 die("Could not open file: %s\n", fpath);
231 g_chmod(fpath, 0600); /* always */
238 buildpath(const char *path)
241 char *apath, *name, *p, *fpath;
243 if (path[0] == '~') {
244 if (path[1] == '/' || path[1] == '\0') {
245 p = (char *)&path[1];
246 pw = getpwuid(getuid());
248 if ((p = strchr(path, '/')))
249 name = g_strndup(&path[1], --p - path);
251 name = g_strdup(&path[1]);
253 if (!(pw = getpwnam(name))) {
254 die("Can't get user %s home directory: %s.\n",
259 apath = g_build_filename(pw->pw_dir, p, NULL);
261 apath = g_strdup(path);
264 /* creating directory */
265 if (g_mkdir_with_parents(apath, 0700) < 0)
266 die("Could not access directory: %s\n", apath);
268 fpath = realpath(apath, NULL);
275 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
277 WebKitHitTestResultContext element;
278 GdkEventButton *eb = (GdkEventButton*)e;
281 element = webkit_hit_test_result_get_context(c->mousepos);
283 for (i = 0; i < LENGTH(buttons); ++i) {
284 if (element & buttons[i].target &&
285 eb->button == buttons[i].button &&
286 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
288 buttons[i].func(c, &buttons[i].arg, c->mousepos);
289 return buttons[i].stopevent;
300 destroyclient(clients);
307 WebKitCookieAcceptPolicy
308 cookiepolicy_get(void)
310 switch (cookiepolicies[cookiepolicy]) {
312 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
314 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
320 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
324 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
327 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
329 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
331 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
340 evalscript(Client *c, const char *jsstr, ...)
346 script = g_strdup_vprintf(jsstr, ap);
349 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
359 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
360 evalscript(c, script);
365 clipboard(Client *c, const Arg *a)
367 if (a->b) { /* load clipboard uri */
368 gtk_clipboard_request_text(gtk_clipboard_get(
369 GDK_SELECTION_PRIMARY),
371 } else { /* copy uri */
372 gtk_clipboard_set_text(gtk_clipboard_get(
373 GDK_SELECTION_PRIMARY), c->targeturi
374 ? c->targeturi : geturi(c), -1);
379 copystr(char **str, const char *src)
392 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
396 switch (webkit_navigation_action_get_navigation_type(a)) {
397 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
399 * popup windows of type “other” are almost always triggered
400 * by user gesture, so inverse the logic here
402 /* instead of this, compare destination uri to mouse-over uri for validating window */
403 if (webkit_navigation_action_is_user_gesture(a)) {
407 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
408 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
409 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
410 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
411 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
419 return GTK_WIDGET(n->view);
423 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
424 WebKitPolicyDecisionType dt, Client *c)
427 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
428 decidenavigation(d, c);
430 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
431 decidenewwindow(d, c);
433 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
434 decideresource(d, c);
437 webkit_policy_decision_ignore(d);
444 decidenavigation(WebKitPolicyDecision *d, Client *c)
446 WebKitNavigationAction *a;
448 a = webkit_navigation_policy_decision_get_navigation_action(
449 WEBKIT_NAVIGATION_POLICY_DECISION(d));
451 switch (webkit_navigation_action_get_navigation_type(a)) {
452 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
453 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
454 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
455 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
456 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
457 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
459 /* Do not navigate to links with a "_blank" target (popup) */
460 if (webkit_navigation_policy_decision_get_frame_name(
461 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
462 webkit_policy_decision_ignore(d);
464 /* Filter out navigation to different domain ? */
465 /* get action→urirequest, copy and load in new window+view
467 webkit_policy_decision_use(d);
474 decidenewwindow(WebKitPolicyDecision *d, Client *c)
476 WebKitNavigationAction *a;
479 a = webkit_navigation_policy_decision_get_navigation_action(
480 WEBKIT_NAVIGATION_POLICY_DECISION(d));
482 switch (webkit_navigation_action_get_navigation_type(a)) {
483 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
484 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
485 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
486 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
487 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
488 /* Filter domains here */
489 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
490 * test for link clicked but no button ? */
491 arg.v = webkit_uri_request_get_uri(
492 webkit_navigation_action_get_request(a));
493 newwindow(c, &arg, 0);
495 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
500 webkit_policy_decision_ignore(d);
504 decideresource(WebKitPolicyDecision *d, Client *c)
508 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
509 WebKitURIResponse *res;
511 res = webkit_response_policy_decision_get_response(r);
512 uri = webkit_uri_response_get_uri(res);
514 if (g_str_has_suffix(uri, "/favicon.ico"))
515 webkit_uri_request_set_uri(
516 webkit_response_policy_decision_get_request(r),
519 if (!g_str_has_prefix(uri, "http://")
520 && !g_str_has_prefix(uri, "https://")
521 && !g_str_has_prefix(uri, "about:")
522 && !g_str_has_prefix(uri, "file://")
523 && !g_str_has_prefix(uri, "data:")
524 && !g_str_has_prefix(uri, "blob:")
525 && strlen(uri) > 0) {
526 for (i = 0; i < strlen(uri); i++) {
527 if (!g_ascii_isprint(uri[i])) {
534 webkit_policy_decision_ignore(d);
538 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
539 webkit_policy_decision_use(d);
541 webkit_policy_decision_ignore(d);
547 destroyclient(Client *c)
551 webkit_web_view_stop_loading(c->view);
552 /* Not needed, has already been called
553 gtk_widget_destroy(c->win);
556 for (p = clients; p && p->next != c; p = p->next)
566 closeview(WebKitWebView *v, Client *c)
568 gtk_widget_destroy(c->win);
572 destroywin(GtkWidget* w, Client *c)
580 die(const char *errstr, ...)
584 va_start(ap, errstr);
585 vfprintf(stderr, errstr, ap);
591 find(Client *c, const Arg *arg)
595 s = getatom(c, AtomFind);
596 gboolean forward = *(gboolean *)arg;
597 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
601 togglefullscreen(Client *c, const Arg *a)
603 /* toggling value is handled in winevent() */
605 gtk_window_unfullscreen(GTK_WINDOW(c->win));
607 gtk_window_fullscreen(GTK_WINDOW(c->win));
611 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
613 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
614 if (allowgeolocation)
615 webkit_permission_request_allow(r);
617 webkit_permission_request_deny(r);
625 getatom(Client *c, int a)
627 static char buf[BUFSIZ];
630 unsigned long ldummy;
631 unsigned char *p = NULL;
633 XGetWindowProperty(dpy, c->xid,
634 atoms[a], 0L, BUFSIZ, False, XA_STRING,
635 &adummy, &idummy, &ldummy, &ldummy, &p);
637 strncpy(buf, (char *)p, LENGTH(buf)-1);
650 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
656 getstyle(const char *uri)
660 if (stylefile != NULL)
663 for (i = 0; i < LENGTH(styles); i++) {
664 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
666 return styles[i].style;
673 setstyle(Client *c, const char *stylefile)
677 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
678 fprintf(stderr, "Could not read style file: %s\n", stylefile);
682 webkit_user_content_manager_add_style_sheet(
683 webkit_web_view_get_user_content_manager(c->view),
684 webkit_user_style_sheet_new(style,
685 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
686 WEBKIT_USER_STYLE_LEVEL_USER,
693 handleplumb(Client *c, const gchar *uri)
697 arg = (Arg)PLUMB(uri);
702 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
704 g_signal_connect(G_OBJECT(d), "notify::response",
705 G_CALLBACK(responsereceived), c);
709 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
711 download(c, webkit_download_get_response(d));
712 webkit_download_cancel(d);
716 download(Client *c, WebKitURIResponse *r)
720 a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
725 inspector(Client *c, const Arg *arg)
727 if (enableinspector) {
729 webkit_web_inspector_close(c->inspector);
731 webkit_web_inspector_show(c->inspector);
736 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
738 return WEBKIT_WEB_VIEW(webkit_web_view_new());
742 inspector_show(WebKitWebInspector *i, Client *c)
749 w = webkit_web_inspector_get_web_view(i);
750 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
751 gtk_widget_show(GTK_WIDGET(w));
752 c->isinspecting = true;
758 inspector_close(WebKitWebInspector *i, Client *c)
762 if (!c->isinspecting)
765 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
767 gtk_widget_destroy(w);
768 c->isinspecting = false;
774 inspector_finished(WebKitWebInspector *i, Client *c)
776 g_free(c->inspector);
780 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
784 gboolean processed = FALSE;
786 mods = CLEANMASK(mods);
787 key = gdk_keyval_to_lower(key);
789 for (i = 0; i < LENGTH(keys); i++) {
790 if (key == keys[i].keyval
791 && mods == keys[i].mod
793 keys[i].func(c, &(keys[i].arg));
802 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
805 WebKitHitTestResultContext hc;
807 /* Keep the hit test to know where is the pointer on the next click */
810 hc = webkit_hit_test_result_get_context(h);
813 c->targeturi = webkit_hit_test_result_get_link_uri(h);
815 c->targeturi = webkit_hit_test_result_get_image_uri(h);
816 else if (hc & OnMedia)
817 c->targeturi = webkit_hit_test_result_get_media_uri(h);
824 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
827 case WEBKIT_LOAD_STARTED:
828 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
830 case WEBKIT_LOAD_REDIRECTED:
831 setatom(c, AtomUri, geturi(c));
833 case WEBKIT_LOAD_COMMITTED:
834 if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
835 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
837 setatom(c, AtomUri, geturi(c));
840 setstyle(c, getstyle(geturi(c)));
842 case WEBKIT_LOAD_FINISHED:
843 /* Disabled until we write some WebKitWebExtension for
844 * manipulating the DOM directly.
845 evalscript(c, "document.documentElement.style.overflow = '%s'",
846 enablescrollbars ? "auto" : "hidden");
855 loaduri(Client *c, const Arg *arg)
858 const char *uri = (char *)arg->v;
859 Arg a = { .b = FALSE };
862 if (strcmp(uri, "") == 0)
865 /* In case it's a file path. */
866 if (stat(uri, &st) == 0) {
867 rp = realpath(uri, NULL);
868 u = g_strdup_printf("file://%s", rp);
871 u = g_strrstr(uri, "://") ? g_strdup(uri)
872 : g_strdup_printf("http://%s", uri);
875 setatom(c, AtomUri, uri);
877 /* prevents endless loop */
878 if (strcmp(u, geturi(c)) == 0) {
881 webkit_web_view_load_uri(c->view, u);
883 c->title = copystr(&c->title, u);
890 navigate(Client *c, const Arg *arg)
892 int steps = *(int *)arg;
893 webkit_web_view_go_back_or_forward(c->view, steps);
897 newclient(Client *rc)
902 if (!(c = calloc(1, sizeof(Client))))
903 die("Cannot malloc!\n");
911 c->view = newview(c, rc ? rc->view : NULL);
912 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
918 newview(Client *c, WebKitWebView *rv)
921 WebKitSettings *settings;
922 WebKitUserContentManager *contentmanager;
923 WebKitWebContext *context;
929 webkit_web_view_new_with_related_view(rv));
931 settings = webkit_settings_new_with_settings(
932 "auto-load-images", loadimages,
933 "default-font-size", defaultfontsize,
934 "enable-caret-browsing", enablecaretbrowsing,
935 "enable-developer-extras", enableinspector,
936 "enable-dns-prefetching", enablednsprefetching,
937 "enable-frame-flattening", enableframeflattening,
938 "enable-html5-database", enablecache,
939 "enable-html5-local-storage", enablecache,
940 "enable-javascript", enablescripts,
941 "enable-plugins", enableplugins,
943 if (!(ua = getenv("SURF_USERAGENT")))
945 webkit_settings_set_user_agent(settings, ua);
946 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
947 * for more interesting settings */
949 contentmanager = webkit_user_content_manager_new();
951 context = webkit_web_context_new_with_website_data_manager(
952 webkit_website_data_manager_new(
953 "base-cache-directory", cachedir,
954 "base-data-directory", cachedir,
957 /* rendering process model, can be a shared unique one or one for each
959 webkit_web_context_set_process_model(context,
960 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
962 webkit_web_context_set_tls_errors_policy(context, strictssl ?
963 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
965 webkit_web_context_set_cache_model(context, enablecache ?
966 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
968 /* Currently only works with text file to be compatible with curl */
969 webkit_cookie_manager_set_persistent_storage(
970 webkit_web_context_get_cookie_manager(context), cookiefile,
971 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
973 webkit_cookie_manager_set_accept_policy(
974 webkit_web_context_get_cookie_manager(context),
977 g_signal_connect(G_OBJECT(context), "download-started",
978 G_CALLBACK(downloadstarted), c);
980 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
981 "settings", settings,
982 "user-content-manager", contentmanager,
983 "web-context", context,
987 g_signal_connect(G_OBJECT(v),
989 G_CALLBACK(titlechanged), c);
990 g_signal_connect(G_OBJECT(v),
991 "mouse-target-changed",
992 G_CALLBACK(mousetargetchanged), c);
993 g_signal_connect(G_OBJECT(v),
994 "permission-request",
995 G_CALLBACK(permissionrequested), c);
996 g_signal_connect(G_OBJECT(v),
998 G_CALLBACK(createview), c);
999 g_signal_connect(G_OBJECT(v), "ready-to-show",
1000 G_CALLBACK(showview), c);
1001 g_signal_connect(G_OBJECT(v),
1003 G_CALLBACK(decidepolicy), c);
1004 g_signal_connect(G_OBJECT(v),
1006 G_CALLBACK(loadchanged), c);
1007 g_signal_connect(G_OBJECT(v),
1008 "notify::estimated-load-progress",
1009 G_CALLBACK(progresschanged), c);
1010 g_signal_connect(G_OBJECT(v),
1011 "button-release-event",
1012 G_CALLBACK(buttonreleased), c);
1013 g_signal_connect(G_OBJECT(v), "close",
1014 G_CALLBACK(closeview), c);
1020 showview(WebKitWebView *v, Client *c)
1022 GdkGeometry hints = { 1, 1 };
1023 GdkRGBA bgcolor = { 0 };
1026 c->win = createwindow(c);
1032 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1035 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1036 gtk_widget_show(GTK_WIDGET(c->view));
1037 gtk_widget_show(c->win);
1038 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1039 c->xid = gdk_x11_window_get_xid(gwin);
1040 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1042 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1043 gdk_window_add_filter(gwin, processx, c);
1045 /* This might conflict with _zoomto96dpi_. */
1046 if (zoomlevel != 1.0)
1047 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1049 if (runinfullscreen)
1050 togglefullscreen(c, NULL);
1052 setatom(c, AtomFind, "");
1053 setatom(c, AtomUri, "about:blank");
1055 webkit_web_view_set_background_color(c->view, &bgcolor);
1058 gdk_display_sync(gtk_widget_get_display(c->win));
1059 printf("%lu\n", c->xid);
1061 if (fclose(stdout) != 0) {
1062 die("Error closing stdout");
1068 newwindow(Client *c, const Arg *arg, gboolean noembed)
1071 const char *cmd[18], *uri;
1072 const Arg a = { .v = (void *)cmd };
1077 cmd[i++] = cookiepolicies;
1078 if (!enablescrollbars)
1080 if (embed && !noembed) {
1082 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1085 if (!allowgeolocation)
1097 if (enablediskcache)
1100 cmd[i++] = cookiefile;
1102 uri = arg->v ? (char *)arg->v : c->linkhover;
1110 createwindow(Client *c)
1115 w = gtk_plug_new(embed);
1117 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1119 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
1120 * is always correct, so we should still call this function.
1121 * But when doing so, we *must* differentiate between a
1122 * WM_CLASS and a resource on the window. By convention, the
1123 * window class (WM_CLASS) is capped, while the resource is in
1124 * lowercase. Both these values come as a pair.
1126 gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
1128 /* TA: 20091214: And set the role here as well -- so that
1129 * sessions can pick this up.
1131 gtk_window_set_role(GTK_WINDOW(w), "Surf");
1133 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
1136 g_signal_connect(G_OBJECT(w), "destroy",
1137 G_CALLBACK(destroywin), c);
1138 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1139 G_CALLBACK(winevent), c);
1140 g_signal_connect(G_OBJECT(w), "window-state-event",
1141 G_CALLBACK(winevent), c);
1147 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1149 Arg arg = {.v = text };
1151 loaduri((Client *) d, &arg);
1155 print(Client *c, const Arg *arg)
1157 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1161 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1163 Client *c = (Client *)d;
1167 if (((XEvent *)e)->type == PropertyNotify) {
1168 ev = &((XEvent *)e)->xproperty;
1169 if (ev->state == PropertyNewValue) {
1170 if (ev->atom == atoms[AtomFind]) {
1174 return GDK_FILTER_REMOVE;
1175 } else if (ev->atom == atoms[AtomGo]) {
1176 arg.v = getatom(c, AtomGo);
1179 return GDK_FILTER_REMOVE;
1183 return GDK_FILTER_CONTINUE;
1187 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1189 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1195 linkopen(Client *c, const Arg *arg)
1197 newwindow(NULL, arg, 1);
1201 linkopenembed(Client *c, const Arg *arg)
1203 newwindow(NULL, arg, 0);
1207 reload(Client *c, const Arg *arg)
1209 gboolean nocache = *(gboolean *)arg;
1211 webkit_web_view_reload_bypass_cache(c->view);
1213 webkit_web_view_reload(c->view);
1217 scroll_h(Client *c, const Arg *arg)
1219 scroll(gtk_scrolled_window_get_hadjustment(
1220 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1224 scroll_v(Client *c, const Arg *arg)
1226 scroll(gtk_scrolled_window_get_vadjustment(
1227 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1231 scroll(GtkAdjustment *a, const Arg *arg)
1235 v = gtk_adjustment_get_value(a);
1239 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1244 v += gtk_adjustment_get_step_increment(a) * arg->i;
1248 v = MIN(v, gtk_adjustment_get_upper(a) -
1249 gtk_adjustment_get_page_size(a));
1250 gtk_adjustment_set_value(a, v);
1254 setatom(Client *c, int a, const char *v)
1257 XChangeProperty(dpy, c->xid,
1258 atoms[a], XA_STRING, 8, PropModeReplace,
1259 (unsigned char *)v, strlen(v) + 1);
1266 WebKitWebContext *context;
1267 GError *error = NULL;
1269 /* clean up any zombies immediately */
1271 gtk_init(NULL, NULL);
1273 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1276 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1277 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1278 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1280 /* dirs and files */
1281 cookiefile = buildfile(cookiefile);
1282 scriptfile = buildfile(scriptfile);
1283 cachedir = buildpath(cachedir);
1285 if (stylefile == NULL) {
1286 styledir = buildpath(styledir);
1287 for (i = 0; i < LENGTH(styles); i++) {
1288 if (regcomp(&(styles[i].re), styles[i].regex,
1291 "Could not compile regex: %s\n",
1293 styles[i].regex = NULL;
1295 styles[i].style = g_strconcat(styledir, "/",
1296 styles[i].style, NULL);
1300 stylefile = buildfile(stylefile);
1307 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1308 die("Can't install SIGCHLD handler");
1309 while (0 < waitpid(-1, NULL, WNOHANG));
1313 spawn(Client *c, const Arg *arg)
1317 close(ConnectionNumber(dpy));
1319 execvp(((char **)arg->v)[0], (char **)arg->v);
1320 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1327 stop(Client *c, const Arg *arg)
1329 webkit_web_view_stop_loading(c->view);
1333 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1335 c->title = webkit_web_view_get_title(c->view);
1340 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1343 case GDK_LEAVE_NOTIFY:
1344 c->targeturi = NULL;
1347 case GDK_WINDOW_STATE: /* fallthrough */
1348 if (e->window_state.changed_mask ==
1349 GDK_WINDOW_STATE_FULLSCREEN) {
1350 c->fullscreen = e->window_state.new_window_state &
1351 GDK_WINDOW_STATE_FULLSCREEN;
1362 toggle(Client *c, const Arg *arg)
1364 WebKitWebSettings *settings;
1365 char *name = (char *)arg->v;
1367 Arg a = { .b = FALSE };
1369 settings = webkit_web_view_get_settings(c->view);
1370 g_object_get(G_OBJECT(settings), name, &value, NULL);
1371 g_object_set(G_OBJECT(settings), name, !value, NULL);
1377 togglecookiepolicy(Client *c, const Arg *arg)
1380 cookiepolicy %= strlen(cookiepolicies);
1382 webkit_cookie_manager_set_accept_policy(
1383 webkit_web_context_get_cookie_manager(
1384 webkit_web_view_get_context(c->view)),
1385 cookiepolicy_get());
1388 /* Do not reload. */
1392 togglegeolocation(Client *c, const Arg *arg)
1394 Arg a = { .b = FALSE };
1396 allowgeolocation ^= 1;
1401 twitch(Client *c, const Arg *arg)
1406 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1409 v = gtk_adjustment_get_value(a);
1414 v = MIN(v, gtk_adjustment_get_upper(a) -
1415 gtk_adjustment_get_page_size(a));
1416 gtk_adjustment_set_value(a, v);
1420 togglescrollbars(Client *c, const Arg *arg)
1422 GtkPolicyType vspolicy;
1425 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1428 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1429 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1433 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1434 GTK_POLICY_AUTOMATIC,
1435 GTK_POLICY_AUTOMATIC);
1444 togglestyle(Client *c, const Arg *arg)
1446 enablestyle = !enablestyle;
1447 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1453 gettogglestat(Client *c)
1457 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1459 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1461 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1463 togglestat[p++] = value? 'C': 'c';
1465 togglestat[p++] = allowgeolocation? 'G': 'g';
1467 togglestat[p++] = enablediskcache? 'D': 'd';
1469 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1470 togglestat[p++] = value? 'I': 'i';
1472 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1473 togglestat[p++] = value? 'S': 's';
1475 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1476 togglestat[p++] = value? 'V': 'v';
1478 togglestat[p++] = enablestyle ? 'M': 'm';
1480 togglestat[p] = '\0';
1484 getpagestat(Client *c)
1486 const char *uri = geturi(c);
1488 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
1489 c->tlsflags > 0 ? 'U' : 'T';
1494 updatetitle(Client *c)
1498 if (showindicators) {
1503 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1505 } else if (c->progress != 100) {
1506 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1507 togglestat, pagestat,
1508 c->title == NULL ? "" : c->title);
1510 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1511 c->title == NULL ? "" : c->title);
1514 gtk_window_set_title(GTK_WINDOW(c->win), t);
1517 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1523 updatewinid(Client *c)
1525 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1531 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1532 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1533 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1537 zoom(Client *c, const Arg *arg)
1542 webkit_web_view_zoom_out(c->view);
1543 } else if (arg->i > 0) {
1545 webkit_web_view_zoom_in(c->view);
1549 webkit_web_view_set_zoom_level(c->view, 1.0);
1554 main(int argc, char *argv[])
1559 memset(&arg, 0, sizeof(arg));
1561 /* command line args */
1564 cookiepolicies = EARGF(usage());
1567 enablescrollbars = 0;
1570 enablescrollbars = 1;
1573 cookiefile = EARGF(usage());
1576 enablediskcache = 0;
1579 enablediskcache = 1;
1582 embed = strtol(EARGF(usage()), NULL, 0);
1585 runinfullscreen = 0;
1588 runinfullscreen = 1;
1591 allowgeolocation = 0;
1594 allowgeolocation = 1;
1615 enableinspector = 0;
1618 enableinspector = 1;
1627 scriptfile = EARGF(usage());
1636 stylefile = EARGF(usage());
1639 useragent = EARGF(usage());
1642 die("surf-"VERSION", ©2009-2015 surf engineers, "
1643 "see LICENSE for details\n");
1648 zoomlevel = strtof(EARGF(usage()), NULL);
1657 c = newclient(NULL);
1660 loaduri(clients, &arg);
1667 return EXIT_SUCCESS;