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;
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 toggleinspector(Client *c, const Arg *a);
149 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
150 GdkModifierType mods, Client *c);
151 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
152 guint modifiers, Client *c);
153 static void loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c);
154 static void loaduri(Client *c, const Arg *arg);
155 static void navigate(Client *c, const Arg *a);
156 static Client *newclient(Client *c);
157 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
158 static void showview(WebKitWebView *v, Client *c);
159 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
160 static GtkWidget *createwindow(Client *c);
161 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
162 static void print(Client *c, const Arg *a);
163 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
165 static gboolean winevent(GtkWidget *w, GdkEvent *e, Client *c);
166 static void progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c);
167 static void linkopen(Client *c, const Arg *arg);
168 static void linkopenembed(Client *c, const Arg *arg);
169 static void reload(Client *c, const Arg *arg);
170 static void scroll_h(Client *c, const Arg *a);
171 static void scroll_v(Client *c, const Arg *a);
172 static void setatom(Client *c, int a, const char *v);
173 static void setup(void);
174 static void sigchld(int unused);
175 static void spawn(Client *c, const Arg *arg);
176 static void stop(Client *c, const Arg *arg);
177 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
178 static void toggle(Client *c, const Arg *arg);
179 static void togglecookiepolicy(Client *c, const Arg *arg);
180 static void togglegeolocation(Client *c, const Arg *arg);
181 static void togglestyle(Client *c, const Arg *arg);
182 static void updatetitle(Client *c);
183 static void updatewinid(Client *c);
184 static void usage(void);
185 static void zoom(Client *c, const Arg *a);
187 /* configuration, allows nested code to access above variables */
191 addaccelgroup(Client *c)
194 GtkAccelGroup *group = gtk_accel_group_new();
197 for (i = 0; i < LENGTH(keys); i++) {
198 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
199 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
202 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
206 buildfile(const char *path)
208 char *dname, *bname, *bpath, *fpath;
211 dname = g_path_get_dirname(path);
212 bname = g_path_get_basename(path);
214 bpath = buildpath(dname);
217 fpath = g_build_filename(bpath, bname, NULL);
221 if (!(f = fopen(fpath, "a")))
222 die("Could not open file: %s\n", fpath);
224 g_chmod(fpath, 0600); /* always */
231 buildpath(const char *path)
234 char *apath, *name, *p, *fpath;
236 if (path[0] == '~') {
237 if (path[1] == '/' || path[1] == '\0') {
238 p = (char *)&path[1];
239 pw = getpwuid(getuid());
241 if ((p = strchr(path, '/')))
242 name = g_strndup(&path[1], --p - path);
244 name = g_strdup(&path[1]);
246 if (!(pw = getpwnam(name))) {
247 die("Can't get user %s home directory: %s.\n",
252 apath = g_build_filename(pw->pw_dir, p, NULL);
254 apath = g_strdup(path);
257 /* creating directory */
258 if (g_mkdir_with_parents(apath, 0700) < 0)
259 die("Could not access directory: %s\n", apath);
261 fpath = realpath(apath, NULL);
268 buttonreleased(GtkWidget *w, GdkEventKey *e, Client *c)
270 WebKitHitTestResultContext element;
271 GdkEventButton *eb = (GdkEventButton*)e;
274 element = webkit_hit_test_result_get_context(c->mousepos);
276 for (i = 0; i < LENGTH(buttons); ++i) {
277 if (element & buttons[i].target &&
278 eb->button == buttons[i].button &&
279 CLEANMASK(eb->state) == CLEANMASK(buttons[i].mask) &&
281 buttons[i].func(c, &buttons[i].arg, c->mousepos);
282 return buttons[i].stopevent;
293 destroyclient(clients);
300 WebKitCookieAcceptPolicy
301 cookiepolicy_get(void)
303 switch (cookiepolicies[cookiepolicy]) {
305 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
307 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
313 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
317 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
320 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
322 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
324 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
333 evalscript(Client *c, const char *jsstr, ...)
339 script = g_strdup_vprintf(jsstr, ap);
342 webkit_web_view_run_javascript(c->view, script, NULL, NULL, NULL);
352 if (g_file_get_contents(scriptfile, &script, &l, NULL) && l)
353 evalscript(c, script);
358 clipboard(Client *c, const Arg *a)
360 if (a->b) { /* load clipboard uri */
361 gtk_clipboard_request_text(gtk_clipboard_get(
362 GDK_SELECTION_PRIMARY),
364 } else { /* copy uri */
365 gtk_clipboard_set_text(gtk_clipboard_get(
366 GDK_SELECTION_PRIMARY), c->targeturi
367 ? c->targeturi : geturi(c), -1);
372 copystr(char **str, const char *src)
385 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
389 switch (webkit_navigation_action_get_navigation_type(a)) {
390 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
392 * popup windows of type “other” are almost always triggered
393 * by user gesture, so inverse the logic here
395 /* instead of this, compare destination uri to mouse-over uri for validating window */
396 if (webkit_navigation_action_is_user_gesture(a)) {
400 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
401 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
402 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
403 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
404 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
412 return GTK_WIDGET(n->view);
416 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
417 WebKitPolicyDecisionType dt, Client *c)
420 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
421 decidenavigation(d, c);
423 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
424 decidenewwindow(d, c);
426 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
427 decideresource(d, c);
430 webkit_policy_decision_ignore(d);
437 decidenavigation(WebKitPolicyDecision *d, Client *c)
439 WebKitNavigationAction *a;
441 a = webkit_navigation_policy_decision_get_navigation_action(
442 WEBKIT_NAVIGATION_POLICY_DECISION(d));
444 switch (webkit_navigation_action_get_navigation_type(a)) {
445 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
446 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
447 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
448 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
449 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
450 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
452 /* Do not navigate to links with a "_blank" target (popup) */
453 if (webkit_navigation_policy_decision_get_frame_name(
454 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
455 webkit_policy_decision_ignore(d);
457 /* Filter out navigation to different domain ? */
458 /* get action→urirequest, copy and load in new window+view
460 webkit_policy_decision_use(d);
467 decidenewwindow(WebKitPolicyDecision *d, Client *c)
469 WebKitNavigationAction *a;
472 a = webkit_navigation_policy_decision_get_navigation_action(
473 WEBKIT_NAVIGATION_POLICY_DECISION(d));
475 switch (webkit_navigation_action_get_navigation_type(a)) {
476 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
477 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
478 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
479 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
480 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
481 /* Filter domains here */
482 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
483 * test for link clicked but no button ? */
484 arg.v = webkit_uri_request_get_uri(
485 webkit_navigation_action_get_request(a));
486 newwindow(c, &arg, 0);
488 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
493 webkit_policy_decision_ignore(d);
497 decideresource(WebKitPolicyDecision *d, Client *c)
501 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
502 WebKitURIResponse *res;
504 res = webkit_response_policy_decision_get_response(r);
505 uri = webkit_uri_response_get_uri(res);
507 if (g_str_has_suffix(uri, "/favicon.ico"))
508 webkit_uri_request_set_uri(
509 webkit_response_policy_decision_get_request(r),
512 if (!g_str_has_prefix(uri, "http://")
513 && !g_str_has_prefix(uri, "https://")
514 && !g_str_has_prefix(uri, "about:")
515 && !g_str_has_prefix(uri, "file://")
516 && !g_str_has_prefix(uri, "data:")
517 && !g_str_has_prefix(uri, "blob:")
518 && strlen(uri) > 0) {
519 for (i = 0; i < strlen(uri); i++) {
520 if (!g_ascii_isprint(uri[i])) {
527 webkit_policy_decision_ignore(d);
531 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
532 webkit_policy_decision_use(d);
534 webkit_policy_decision_ignore(d);
540 destroyclient(Client *c)
544 webkit_web_view_stop_loading(c->view);
545 /* Not needed, has already been called
546 gtk_widget_destroy(c->win);
549 for (p = clients; p && p->next != c; p = p->next)
559 closeview(WebKitWebView *v, Client *c)
561 gtk_widget_destroy(c->win);
565 destroywin(GtkWidget* w, Client *c)
573 die(const char *errstr, ...)
577 va_start(ap, errstr);
578 vfprintf(stderr, errstr, ap);
584 find(Client *c, const Arg *arg)
588 s = getatom(c, AtomFind);
589 gboolean forward = *(gboolean *)arg;
590 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
594 togglefullscreen(Client *c, const Arg *a)
596 /* toggling value is handled in winevent() */
598 gtk_window_unfullscreen(GTK_WINDOW(c->win));
600 gtk_window_fullscreen(GTK_WINDOW(c->win));
604 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
606 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
607 if (allowgeolocation)
608 webkit_permission_request_allow(r);
610 webkit_permission_request_deny(r);
618 getatom(Client *c, int a)
620 static char buf[BUFSIZ];
623 unsigned long ldummy;
624 unsigned char *p = NULL;
626 XGetWindowProperty(dpy, c->xid,
627 atoms[a], 0L, BUFSIZ, False, XA_STRING,
628 &adummy, &idummy, &ldummy, &ldummy, &p);
630 strncpy(buf, (char *)p, LENGTH(buf)-1);
643 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
649 getstyle(const char *uri)
653 if (stylefile != NULL)
656 for (i = 0; i < LENGTH(styles); i++) {
657 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
659 return styles[i].style;
666 setstyle(Client *c, const char *stylefile)
670 if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
671 fprintf(stderr, "Could not read style file: %s\n", stylefile);
675 webkit_user_content_manager_add_style_sheet(
676 webkit_web_view_get_user_content_manager(c->view),
677 webkit_user_style_sheet_new(style,
678 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
679 WEBKIT_USER_STYLE_LEVEL_USER,
686 handleplumb(Client *c, const gchar *uri)
690 arg = (Arg)PLUMB(uri);
695 downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
697 g_signal_connect(G_OBJECT(d), "notify::response",
698 G_CALLBACK(responsereceived), c);
702 responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
704 download(c, webkit_download_get_response(d));
705 webkit_download_cancel(d);
709 download(Client *c, WebKitURIResponse *r)
713 a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
718 toggleinspector(Client *c, const Arg *a)
720 if (enableinspector) {
721 if (webkit_web_inspector_is_attached(c->inspector))
722 webkit_web_inspector_close(c->inspector);
724 webkit_web_inspector_show(c->inspector);
729 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
733 gboolean processed = FALSE;
735 mods = CLEANMASK(mods);
736 key = gdk_keyval_to_lower(key);
738 for (i = 0; i < LENGTH(keys); i++) {
739 if (key == keys[i].keyval
740 && mods == keys[i].mod
742 keys[i].func(c, &(keys[i].arg));
751 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
754 WebKitHitTestResultContext hc;
756 /* Keep the hit test to know where is the pointer on the next click */
759 hc = webkit_hit_test_result_get_context(h);
762 c->targeturi = webkit_hit_test_result_get_link_uri(h);
764 c->targeturi = webkit_hit_test_result_get_image_uri(h);
765 else if (hc & OnMedia)
766 c->targeturi = webkit_hit_test_result_get_media_uri(h);
773 loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
776 case WEBKIT_LOAD_STARTED:
777 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
779 case WEBKIT_LOAD_REDIRECTED:
780 setatom(c, AtomUri, geturi(c));
782 case WEBKIT_LOAD_COMMITTED:
783 if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
784 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
786 setatom(c, AtomUri, geturi(c));
789 setstyle(c, getstyle(geturi(c)));
791 case WEBKIT_LOAD_FINISHED:
792 /* Disabled until we write some WebKitWebExtension for
793 * manipulating the DOM directly.
794 evalscript(c, "document.documentElement.style.overflow = '%s'",
795 enablescrollbars ? "auto" : "hidden");
804 loaduri(Client *c, const Arg *arg)
807 const char *uri = (char *)arg->v;
808 Arg a = { .b = FALSE };
811 if (strcmp(uri, "") == 0)
814 /* In case it's a file path. */
815 if (stat(uri, &st) == 0) {
816 rp = realpath(uri, NULL);
817 u = g_strdup_printf("file://%s", rp);
820 u = g_strrstr(uri, "://") ? g_strdup(uri)
821 : g_strdup_printf("http://%s", uri);
824 setatom(c, AtomUri, uri);
826 /* prevents endless loop */
827 if (strcmp(u, geturi(c)) == 0) {
830 webkit_web_view_load_uri(c->view, u);
832 c->title = copystr(&c->title, u);
839 navigate(Client *c, const Arg *a)
842 webkit_web_view_go_back(c->view);
844 webkit_web_view_go_forward(c->view);
848 newclient(Client *rc)
853 if (!(c = calloc(1, sizeof(Client))))
854 die("Cannot malloc!\n");
862 c->view = newview(c, rc ? rc->view : NULL);
863 c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
869 newview(Client *c, WebKitWebView *rv)
872 WebKitSettings *settings;
873 WebKitUserContentManager *contentmanager;
874 WebKitWebContext *context;
880 webkit_web_view_new_with_related_view(rv));
882 settings = webkit_settings_new_with_settings(
883 "auto-load-images", loadimages,
884 "default-font-size", defaultfontsize,
885 "enable-caret-browsing", enablecaretbrowsing,
886 "enable-developer-extras", enableinspector,
887 "enable-dns-prefetching", enablednsprefetching,
888 "enable-frame-flattening", enableframeflattening,
889 "enable-html5-database", enablecache,
890 "enable-html5-local-storage", enablecache,
891 "enable-javascript", enablescripts,
892 "enable-plugins", enableplugins,
894 if (!(ua = getenv("SURF_USERAGENT")))
896 webkit_settings_set_user_agent(settings, ua);
897 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
898 * for more interesting settings */
900 contentmanager = webkit_user_content_manager_new();
902 context = webkit_web_context_new_with_website_data_manager(
903 webkit_website_data_manager_new(
904 "base-cache-directory", cachedir,
905 "base-data-directory", cachedir,
908 /* rendering process model, can be a shared unique one or one for each
910 webkit_web_context_set_process_model(context,
911 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
913 webkit_web_context_set_tls_errors_policy(context, strictssl ?
914 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
916 webkit_web_context_set_cache_model(context, enablecache ?
917 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
919 /* Currently only works with text file to be compatible with curl */
920 webkit_cookie_manager_set_persistent_storage(
921 webkit_web_context_get_cookie_manager(context), cookiefile,
922 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
924 webkit_cookie_manager_set_accept_policy(
925 webkit_web_context_get_cookie_manager(context),
928 g_signal_connect(G_OBJECT(context), "download-started",
929 G_CALLBACK(downloadstarted), c);
931 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
932 "settings", settings,
933 "user-content-manager", contentmanager,
934 "web-context", context,
938 g_signal_connect(G_OBJECT(v),
940 G_CALLBACK(titlechanged), c);
941 g_signal_connect(G_OBJECT(v),
942 "mouse-target-changed",
943 G_CALLBACK(mousetargetchanged), c);
944 g_signal_connect(G_OBJECT(v),
945 "permission-request",
946 G_CALLBACK(permissionrequested), c);
947 g_signal_connect(G_OBJECT(v),
949 G_CALLBACK(createview), c);
950 g_signal_connect(G_OBJECT(v), "ready-to-show",
951 G_CALLBACK(showview), c);
952 g_signal_connect(G_OBJECT(v),
954 G_CALLBACK(decidepolicy), c);
955 g_signal_connect(G_OBJECT(v),
957 G_CALLBACK(loadchanged), c);
958 g_signal_connect(G_OBJECT(v),
959 "notify::estimated-load-progress",
960 G_CALLBACK(progresschanged), c);
961 g_signal_connect(G_OBJECT(v),
962 "button-release-event",
963 G_CALLBACK(buttonreleased), c);
964 g_signal_connect(G_OBJECT(v), "close",
965 G_CALLBACK(closeview), c);
971 showview(WebKitWebView *v, Client *c)
973 GdkGeometry hints = { 1, 1 };
974 GdkRGBA bgcolor = { 0 };
977 c->win = createwindow(c);
980 c->inspector = webkit_web_view_get_inspector(c->view);
986 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
989 gtk_widget_grab_focus(GTK_WIDGET(c->view));
990 gtk_widget_show(GTK_WIDGET(c->view));
991 gtk_widget_show(c->win);
992 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
993 c->xid = gdk_x11_window_get_xid(gwin);
994 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
996 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
997 gdk_window_add_filter(gwin, processx, c);
999 if (zoomlevel != 1.0)
1000 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1002 if (runinfullscreen)
1003 togglefullscreen(c, NULL);
1005 setatom(c, AtomFind, "");
1006 setatom(c, AtomUri, "about:blank");
1008 webkit_web_view_set_background_color(c->view, &bgcolor);
1011 gdk_display_sync(gtk_widget_get_display(c->win));
1012 printf("%lu\n", c->xid);
1014 if (fclose(stdout) != 0) {
1015 die("Error closing stdout");
1021 newwindow(Client *c, const Arg *arg, gboolean noembed)
1024 const char *cmd[18], *uri;
1025 const Arg a = { .v = (void *)cmd };
1030 cmd[i++] = cookiepolicies;
1031 if (!enablescrollbars)
1033 if (embed && !noembed) {
1035 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1038 if (!allowgeolocation)
1050 if (enablediskcache)
1053 cmd[i++] = cookiefile;
1055 uri = arg->v ? (char *)arg->v : c->linkhover;
1063 createwindow(Client *c)
1068 w = gtk_plug_new(embed);
1070 w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1072 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
1073 * is always correct, so we should still call this function.
1074 * But when doing so, we *must* differentiate between a
1075 * WM_CLASS and a resource on the window. By convention, the
1076 * window class (WM_CLASS) is capped, while the resource is in
1077 * lowercase. Both these values come as a pair.
1079 gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
1081 /* TA: 20091214: And set the role here as well -- so that
1082 * sessions can pick this up.
1084 gtk_window_set_role(GTK_WINDOW(w), "Surf");
1086 gtk_window_set_default_size(GTK_WINDOW(w), 800, 600);
1089 g_signal_connect(G_OBJECT(w), "destroy",
1090 G_CALLBACK(destroywin), c);
1091 g_signal_connect(G_OBJECT(w), "leave-notify-event",
1092 G_CALLBACK(winevent), c);
1093 g_signal_connect(G_OBJECT(w), "window-state-event",
1094 G_CALLBACK(winevent), c);
1100 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1102 Arg arg = {.v = text };
1104 loaduri((Client *) d, &arg);
1108 print(Client *c, const Arg *a)
1110 webkit_print_operation_run_dialog(webkit_print_operation_new(c->view),
1111 GTK_WINDOW(c->win));
1115 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1117 Client *c = (Client *)d;
1121 if (((XEvent *)e)->type == PropertyNotify) {
1122 ev = &((XEvent *)e)->xproperty;
1123 if (ev->state == PropertyNewValue) {
1124 if (ev->atom == atoms[AtomFind]) {
1128 return GDK_FILTER_REMOVE;
1129 } else if (ev->atom == atoms[AtomGo]) {
1130 arg.v = getatom(c, AtomGo);
1133 return GDK_FILTER_REMOVE;
1137 return GDK_FILTER_CONTINUE;
1141 progresschanged(WebKitWebView *v, GParamSpec *ps, Client *c)
1143 c->progress = webkit_web_view_get_estimated_load_progress(c->view) *
1149 linkopen(Client *c, const Arg *arg)
1151 newwindow(NULL, arg, 1);
1155 linkopenembed(Client *c, const Arg *arg)
1157 newwindow(NULL, arg, 0);
1161 reload(Client *c, const Arg *arg)
1163 gboolean nocache = *(gboolean *)arg;
1165 webkit_web_view_reload_bypass_cache(c->view);
1167 webkit_web_view_reload(c->view);
1171 scroll_h(Client *c, const Arg *a)
1173 evalscript(c, "window.scrollBy(%d * (window.innerWidth / 100), 0)",
1178 scroll_v(Client *c, const Arg *a)
1180 evalscript(c, "window.scrollBy(0, %d * (window.innerHeight / 100))",
1185 setatom(Client *c, int a, const char *v)
1188 XChangeProperty(dpy, c->xid,
1189 atoms[a], XA_STRING, 8, PropModeReplace,
1190 (unsigned char *)v, strlen(v) + 1);
1197 WebKitWebContext *context;
1198 GError *error = NULL;
1200 /* clean up any zombies immediately */
1202 gtk_init(NULL, NULL);
1204 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1207 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1208 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1209 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1211 /* dirs and files */
1212 cookiefile = buildfile(cookiefile);
1213 scriptfile = buildfile(scriptfile);
1214 cachedir = buildpath(cachedir);
1216 if (stylefile == NULL) {
1217 styledir = buildpath(styledir);
1218 for (i = 0; i < LENGTH(styles); i++) {
1219 if (regcomp(&(styles[i].re), styles[i].regex,
1222 "Could not compile regex: %s\n",
1224 styles[i].regex = NULL;
1226 styles[i].style = g_strconcat(styledir, "/",
1227 styles[i].style, NULL);
1231 stylefile = buildfile(stylefile);
1238 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1239 die("Can't install SIGCHLD handler");
1240 while (0 < waitpid(-1, NULL, WNOHANG));
1244 spawn(Client *c, const Arg *arg)
1248 close(ConnectionNumber(dpy));
1250 execvp(((char **)arg->v)[0], (char **)arg->v);
1251 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1258 stop(Client *c, const Arg *arg)
1260 webkit_web_view_stop_loading(c->view);
1264 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1266 c->title = webkit_web_view_get_title(c->view);
1271 winevent(GtkWidget *w, GdkEvent *e, Client *c)
1274 case GDK_LEAVE_NOTIFY:
1275 c->targeturi = NULL;
1278 case GDK_WINDOW_STATE: /* fallthrough */
1279 if (e->window_state.changed_mask ==
1280 GDK_WINDOW_STATE_FULLSCREEN) {
1281 c->fullscreen = e->window_state.new_window_state &
1282 GDK_WINDOW_STATE_FULLSCREEN;
1293 toggle(Client *c, const Arg *arg)
1295 WebKitWebSettings *settings;
1296 char *name = (char *)arg->v;
1298 Arg a = { .b = FALSE };
1300 settings = webkit_web_view_get_settings(c->view);
1301 g_object_get(G_OBJECT(settings), name, &value, NULL);
1302 g_object_set(G_OBJECT(settings), name, !value, NULL);
1308 togglecookiepolicy(Client *c, const Arg *arg)
1311 cookiepolicy %= strlen(cookiepolicies);
1313 webkit_cookie_manager_set_accept_policy(
1314 webkit_web_context_get_cookie_manager(
1315 webkit_web_view_get_context(c->view)),
1316 cookiepolicy_get());
1319 /* Do not reload. */
1323 togglegeolocation(Client *c, const Arg *arg)
1325 Arg a = { .b = FALSE };
1327 allowgeolocation ^= 1;
1332 togglestyle(Client *c, const Arg *arg)
1334 enablestyle = !enablestyle;
1335 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1341 gettogglestat(Client *c)
1345 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1347 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1349 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1351 togglestat[p++] = value? 'C': 'c';
1353 togglestat[p++] = allowgeolocation? 'G': 'g';
1355 togglestat[p++] = enablediskcache? 'D': 'd';
1357 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1358 togglestat[p++] = value? 'I': 'i';
1360 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1361 togglestat[p++] = value? 'S': 's';
1363 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1364 togglestat[p++] = value? 'V': 'v';
1366 togglestat[p++] = enablestyle ? 'M': 'm';
1368 togglestat[p] = '\0';
1372 getpagestat(Client *c)
1374 const char *uri = geturi(c);
1376 pagestats[0] = c->tlsflags > G_TLS_CERTIFICATE_VALIDATE_ALL ? '-' :
1377 c->tlsflags > 0 ? 'U' : 'T';
1382 updatetitle(Client *c)
1386 if (showindicators) {
1391 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1393 } else if (c->progress != 100) {
1394 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1395 togglestat, pagestat,
1396 c->title == NULL ? "" : c->title);
1398 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1399 c->title == NULL ? "" : c->title);
1402 gtk_window_set_title(GTK_WINDOW(c->win), t);
1405 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1411 updatewinid(Client *c)
1413 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1419 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1420 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1421 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1425 zoom(Client *c, const Arg *a)
1428 webkit_web_view_set_zoom_level(c->view, zoomlevel + 0.1);
1430 webkit_web_view_set_zoom_level(c->view, zoomlevel - 0.1);
1432 webkit_web_view_set_zoom_level(c->view, 1.0);
1434 zoomlevel = webkit_web_view_get_zoom_level(c->view);
1438 main(int argc, char *argv[])
1443 memset(&arg, 0, sizeof(arg));
1445 /* command line args */
1448 cookiepolicies = EARGF(usage());
1451 enablescrollbars = 0;
1454 enablescrollbars = 1;
1457 cookiefile = EARGF(usage());
1460 enablediskcache = 0;
1463 enablediskcache = 1;
1466 embed = strtol(EARGF(usage()), NULL, 0);
1469 runinfullscreen = 0;
1472 runinfullscreen = 1;
1475 allowgeolocation = 0;
1478 allowgeolocation = 1;
1499 enableinspector = 0;
1502 enableinspector = 1;
1511 scriptfile = EARGF(usage());
1520 stylefile = EARGF(usage());
1523 useragent = EARGF(usage());
1526 die("surf-"VERSION", ©2009-2015 surf engineers, "
1527 "see LICENSE for details\n");
1532 zoomlevel = strtof(EARGF(usage()), NULL);
1541 c = newclient(NULL);
1544 loaduri(clients, &arg);
1551 return EXIT_SUCCESS;