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 const char *title, *targeturi;
67 gboolean zoomed, fullscreen, isinspecting, sslfailed;
73 void (*func)(Client *c, const Arg *arg);
81 void (*func)(Client *c, const Arg *arg);
92 static Atom atoms[AtomLast];
93 static Client *clients = NULL;
94 static Window embed = 0;
95 static gboolean showxid = FALSE;
96 static char winid[64];
97 static char togglestat[9];
98 static char pagestat[3];
99 static GTlsDatabase *tlsdb;
100 static int cookiepolicy;
101 static char *stylefile = NULL;
103 static void addaccelgroup(Client *c);
104 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
105 WebKitWebResource *r, WebKitNetworkRequest *req,
106 WebKitNetworkResponse *resp, Client *c);
107 static char *buildfile(const char *path);
108 static char *buildpath(const char *path);
109 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
110 static void cleanup(void);
111 static void clipboard(Client *c, const Arg *arg);
113 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
114 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
116 static char *copystr(char **str, const char *src);
117 static GtkWidget *createview(WebKitWebView *v, WebKitNavigationAction *a,
119 static gboolean decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
120 WebKitPolicyDecisionType dt, Client *c);
121 static void decidenavigation(WebKitPolicyDecision *d, Client *c);
122 static void decidenewwindow(WebKitPolicyDecision *d, Client *c);
123 static void decideresource(WebKitPolicyDecision *d, Client *c);
124 static gboolean deletion_interface(WebKitWebView *view,
125 WebKitDOMHTMLElement *arg1, Client *c);
126 static void destroyclient(Client *c);
127 static void destroywin(GtkWidget* w, Client *c);
128 static void die(const char *errstr, ...);
129 static void eval(Client *c, const Arg *arg);
130 static void find(Client *c, const Arg *arg);
131 static void fullscreen(Client *c, const Arg *arg);
132 static gboolean permissionrequested(WebKitWebView *v,
133 WebKitPermissionRequest *r, Client *c);
134 static const char *getatom(Client *c, int a);
135 static void gettogglestat(Client *c);
136 static void getpagestat(Client *c);
137 static char *geturi(Client *c);
138 static const gchar *getstyle(const char *uri);
139 static void setstyle(Client *c, const char *style);
141 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
143 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
145 static void inspector(Client *c, const Arg *arg);
146 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
148 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
149 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
150 static void inspector_finished(WebKitWebInspector *i, Client *c);
152 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
153 GdkModifierType mods, Client *c);
154 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
155 guint modifiers, Client *c);
156 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
158 static void loaduri(Client *c, const Arg *arg);
159 static void navigate(Client *c, const Arg *arg);
160 static Client *newclient(Client *c);
161 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
162 static void showview(WebKitWebView *v, Client *c);
163 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
164 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
165 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
166 WebKitHitTestResult *target, gboolean keyboard,
168 static void menuactivate(GtkMenuItem *item, Client *c);
169 static void print(Client *c, const Arg *arg);
170 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
172 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
173 static void linkopen(Client *c, const Arg *arg);
174 static void linkopenembed(Client *c, const Arg *arg);
175 static void reload(Client *c, const Arg *arg);
176 static void scroll_h(Client *c, const Arg *arg);
177 static void scroll_v(Client *c, const Arg *arg);
178 static void scroll(GtkAdjustment *a, const Arg *arg);
179 static void setatom(Client *c, int a, const char *v);
180 static void setup(void);
181 static void sigchld(int unused);
182 static void spawn(Client *c, const Arg *arg);
183 static void stop(Client *c, const Arg *arg);
184 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
185 static void titlechangeleave(void *a, void *b, Client *c);
186 static void toggle(Client *c, const Arg *arg);
187 static void togglecookiepolicy(Client *c, const Arg *arg);
188 static void togglegeolocation(Client *c, const Arg *arg);
189 static void togglescrollbars(Client *c, const Arg *arg);
190 static void togglestyle(Client *c, const Arg *arg);
191 static void updatetitle(Client *c);
192 static void updatewinid(Client *c);
193 static void usage(void);
194 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
195 JSContextRef js, JSObjectRef win, Client *c);
196 static void zoom(Client *c, const Arg *arg);
198 /* configuration, allows nested code to access above variables */
202 addaccelgroup(Client *c)
205 GtkAccelGroup *group = gtk_accel_group_new();
208 for (i = 0; i < LENGTH(keys); i++) {
209 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
210 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
213 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
217 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
218 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
221 const gchar *uri = webkit_network_request_get_uri(req);
224 if (g_str_has_suffix(uri, "/favicon.ico"))
225 webkit_network_request_set_uri(req, "about:blank");
227 if (!g_str_has_prefix(uri, "http://")
228 && !g_str_has_prefix(uri, "https://")
229 && !g_str_has_prefix(uri, "about:")
230 && !g_str_has_prefix(uri, "file://")
231 && !g_str_has_prefix(uri, "data:")
232 && !g_str_has_prefix(uri, "blob:")
233 && strlen(uri) > 0) {
234 for (i = 0; i < strlen(uri); i++) {
235 if (!g_ascii_isprint(uri[i])) {
241 handleplumb(c, w, uri);
246 buildfile(const char *path)
248 char *dname, *bname, *bpath, *fpath;
251 dname = g_path_get_dirname(path);
252 bname = g_path_get_basename(path);
254 bpath = buildpath(dname);
257 fpath = g_build_filename(bpath, bname, NULL);
261 if (!(f = fopen(fpath, "a")))
262 die("Could not open file: %s\n", fpath);
264 g_chmod(fpath, 0600); /* always */
271 buildpath(const char *path)
274 char *apath, *name, *p, *fpath;
276 if (path[0] == '~') {
277 if (path[1] == '/' || path[1] == '\0') {
278 p = (char *)&path[1];
279 pw = getpwuid(getuid());
281 if ((p = strchr(path, '/')))
282 name = g_strndup(&path[1], --p - path);
284 name = g_strdup(&path[1]);
286 if (!(pw = getpwnam(name))) {
287 die("Can't get user %s home directory: %s.\n",
292 apath = g_build_filename(pw->pw_dir, p, NULL);
294 apath = g_strdup(path);
297 /* creating directory */
298 if (g_mkdir_with_parents(apath, 0700) < 0)
299 die("Could not access directory: %s\n", apath);
301 fpath = realpath(apath, NULL);
308 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
310 WebKitHitTestResultContext context;
311 WebKitHitTestResult *result;
315 result = webkit_web_view_get_hit_test_result(web, e);
316 g_object_get(result, "context", &context, NULL);
317 g_object_get(result, "link-uri", &arg.v, NULL);
318 for (i = 0; i < LENGTH(buttons); i++) {
319 if (context & buttons[i].click
320 && e->button == buttons[i].button
321 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
322 && buttons[i].func) {
323 buttons[i].func(c, buttons[i].click == ClkLink
324 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
335 destroyclient(clients);
342 WebKitCookieAcceptPolicy
343 cookiepolicy_get(void)
345 switch (cookiepolicies[cookiepolicy]) {
347 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
349 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
355 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
359 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
362 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
364 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
366 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
375 evalscript(JSContextRef js, char *script, char* scriptname)
377 JSStringRef jsscript, jsscriptname;
378 JSValueRef exception = NULL;
380 jsscript = JSStringCreateWithUTF8CString(script);
381 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
382 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
383 jsscriptname, 0, &exception);
384 JSStringRelease(jsscript);
385 JSStringRelease(jsscriptname);
389 runscript(WebKitWebFrame *frame)
394 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
395 evalscript(webkit_web_frame_get_global_context(frame), script,
401 clipboard(Client *c, const Arg *arg)
403 gboolean paste = *(gboolean *)arg;
406 gtk_clipboard_request_text(gtk_clipboard_get(
407 GDK_SELECTION_PRIMARY),
410 gtk_clipboard_set_text(gtk_clipboard_get(
411 GDK_SELECTION_PRIMARY), c->linkhover
412 ? c->linkhover : geturi(c), -1);
417 copystr(char **str, const char *src)
430 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
434 switch (webkit_navigation_action_get_navigation_type(a)) {
435 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
437 * popup windows of type “other” are almost always triggered
438 * by user gesture, so inverse the logic here
440 /* instead of this, compare destination uri to mouse-over uri for validating window */
441 if (webkit_navigation_action_is_user_gesture(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:
457 return GTK_WIDGET(n->view);
461 decidepolicy(WebKitWebView *v, WebKitPolicyDecision *d,
462 WebKitPolicyDecisionType dt, Client *c)
465 case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
466 decidenavigation(d, c);
468 case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
469 decidenewwindow(d, c);
471 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
472 decideresource(d, c);
475 webkit_policy_decision_ignore(d);
482 decidenavigation(WebKitPolicyDecision *d, Client *c)
484 WebKitNavigationAction *a;
486 a = webkit_navigation_policy_decision_get_navigation_action(
487 WEBKIT_NAVIGATION_POLICY_DECISION(d));
489 switch (webkit_navigation_action_get_navigation_type(a)) {
490 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
491 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
492 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
493 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
494 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
495 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
497 /* Do not navigate to links with a "_blank" target (popup) */
498 if (webkit_navigation_policy_decision_get_frame_name(
499 WEBKIT_NAVIGATION_POLICY_DECISION(d))) {
500 webkit_policy_decision_ignore(d);
502 /* Filter out navigation to different domain ? */
503 /* get action→urirequest, copy and load in new window+view
505 webkit_policy_decision_use(d);
512 decidenewwindow(WebKitPolicyDecision *d, Client *c)
514 WebKitNavigationAction *a;
517 a = webkit_navigation_policy_decision_get_navigation_action(
518 WEBKIT_NAVIGATION_POLICY_DECISION(d));
520 switch (webkit_navigation_action_get_navigation_type(a)) {
521 case WEBKIT_NAVIGATION_TYPE_LINK_CLICKED: /* fallthrough */
522 case WEBKIT_NAVIGATION_TYPE_FORM_SUBMITTED: /* fallthrough */
523 case WEBKIT_NAVIGATION_TYPE_BACK_FORWARD: /* fallthrough */
524 case WEBKIT_NAVIGATION_TYPE_RELOAD: /* fallthrough */
525 case WEBKIT_NAVIGATION_TYPE_FORM_RESUBMITTED:
526 /* Filter domains here */
527 /* If the value of “mouse-button” is not 0, then the navigation was triggered by a mouse event.
528 * test for link clicked but no button ? */
529 arg.v = webkit_uri_request_get_uri(
530 webkit_navigation_action_get_request(a));
531 newwindow(c, &arg, 0);
533 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
538 webkit_policy_decision_ignore(d);
542 decideresource(WebKitPolicyDecision *d, Client *c)
544 WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
545 WebKitURIResponse *res;
547 if (webkit_response_policy_decision_is_mime_type_supported(r)) {
548 webkit_policy_decision_use(d);
550 res = webkit_response_policy_decision_get_response(r);
551 webkit_policy_decision_ignore(d);
557 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
563 destroyclient(Client *c)
567 webkit_web_view_stop_loading(c->view);
568 gtk_widget_destroy(GTK_WIDGET(c->view));
569 gtk_widget_destroy(c->scroll);
570 gtk_widget_destroy(c->vbox);
571 gtk_widget_destroy(c->win);
573 for (p = clients; p && p->next != c; p = p->next)
585 destroywin(GtkWidget* w, Client *c)
591 die(const char *errstr, ...)
595 va_start(ap, errstr);
596 vfprintf(stderr, errstr, ap);
602 find(Client *c, const Arg *arg)
606 s = getatom(c, AtomFind);
607 gboolean forward = *(gboolean *)arg;
608 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
612 fullscreen(Client *c, const Arg *arg)
615 gtk_window_unfullscreen(GTK_WINDOW(c->win));
617 gtk_window_fullscreen(GTK_WINDOW(c->win));
618 c->fullscreen = !c->fullscreen;
622 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
624 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
625 if (allowgeolocation)
626 webkit_permission_request_allow(r);
628 webkit_permission_request_deny(r);
636 getatom(Client *c, int a)
638 static char buf[BUFSIZ];
641 unsigned long ldummy;
642 unsigned char *p = NULL;
644 XGetWindowProperty(dpy, c->xid,
645 atoms[a], 0L, BUFSIZ, False, XA_STRING,
646 &adummy, &idummy, &ldummy, &ldummy, &p);
648 strncpy(buf, (char *)p, LENGTH(buf)-1);
661 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
667 getstyle(const char *uri)
671 if (stylefile != NULL)
674 for (i = 0; i < LENGTH(styles); i++) {
675 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
677 return styles[i].style;
684 setstyle(Client *c, const char *style)
686 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
688 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
692 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
696 webkit_web_view_stop_loading(w);
697 arg = (Arg)PLUMB((char *)uri);
702 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
707 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
713 inspector(Client *c, const Arg *arg)
715 if (enableinspector) {
717 webkit_web_inspector_close(c->inspector);
719 webkit_web_inspector_show(c->inspector);
724 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
726 return WEBKIT_WEB_VIEW(webkit_web_view_new());
730 inspector_show(WebKitWebInspector *i, Client *c)
737 w = webkit_web_inspector_get_web_view(i);
738 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
739 gtk_widget_show(GTK_WIDGET(w));
740 c->isinspecting = true;
746 inspector_close(WebKitWebInspector *i, Client *c)
750 if (!c->isinspecting)
753 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
755 gtk_widget_destroy(w);
756 c->isinspecting = false;
762 inspector_finished(WebKitWebInspector *i, Client *c)
764 g_free(c->inspector);
768 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
772 gboolean processed = FALSE;
774 mods = CLEANMASK(mods);
775 key = gdk_keyval_to_lower(key);
777 for (i = 0; i < LENGTH(keys); i++) {
778 if (key == keys[i].keyval
779 && mods == keys[i].mod
781 keys[i].func(c, &(keys[i].arg));
790 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
793 WebKitHitTestResultContext hc;
795 /* Keep the hit test to know where is the pointer on the next click */
798 hc = webkit_hit_test_result_get_context(h);
801 c->targeturi = webkit_hit_test_result_get_link_uri(h);
803 c->targeturi = webkit_hit_test_result_get_image_uri(h);
804 else if (hc & OnMedia)
805 c->targeturi = webkit_hit_test_result_get_media_uri(h);
812 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
814 WebKitWebFrame *frame;
815 WebKitWebDataSource *src;
816 WebKitNetworkRequest *request;
820 switch (webkit_web_view_get_load_status (c->view)) {
821 case WEBKIT_LOAD_COMMITTED:
823 if (strstr(uri, "https://") == uri) {
824 frame = webkit_web_view_get_main_frame(c->view);
825 src = webkit_web_frame_get_data_source(frame);
826 request = webkit_web_data_source_get_request(src);
827 msg = webkit_network_request_get_message(request);
828 c->sslfailed = !(soup_message_get_flags(msg)
829 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
831 setatom(c, AtomUri, uri);
834 setstyle(c, getstyle(uri));
836 case WEBKIT_LOAD_FINISHED:
846 loaduri(Client *c, const Arg *arg)
849 const char *uri = (char *)arg->v;
850 Arg a = { .b = FALSE };
853 if (strcmp(uri, "") == 0)
856 /* In case it's a file path. */
857 if (stat(uri, &st) == 0) {
858 rp = realpath(uri, NULL);
859 u = g_strdup_printf("file://%s", rp);
862 u = g_strrstr(uri, "://") ? g_strdup(uri)
863 : g_strdup_printf("http://%s", uri);
866 setatom(c, AtomUri, uri);
868 /* prevents endless loop */
869 if (strcmp(u, geturi(c)) == 0) {
872 webkit_web_view_load_uri(c->view, u);
874 c->title = copystr(&c->title, u);
881 navigate(Client *c, const Arg *arg)
883 int steps = *(int *)arg;
884 webkit_web_view_go_back_or_forward(c->view, steps);
888 newclient(Client *rc)
893 if (!(c = calloc(1, sizeof(Client))))
894 die("Cannot malloc!\n");
902 c->view = newview(c, rc ? rc->view : NULL);
908 newview(Client *c, WebKitWebView *rv)
911 WebKitSettings *settings;
912 WebKitUserContentManager *contentmanager;
913 WebKitWebContext *context;
919 webkit_web_view_new_with_related_view(rv));
921 settings = webkit_settings_new_with_settings(
922 "auto-load-images", loadimages,
923 "default-font-size", defaultfontsize,
924 "enable-caret-browsing", enablecaretbrowsing,
925 "enable-developer-extras", enableinspector,
926 "enable-dns-prefetching", enablednsprefetching,
927 "enable-frame-flattening", enableframeflattening,
928 "enable-html5-database", enablecache,
929 "enable-html5-local-storage", enablecache,
930 "enable-javascript", enablescripts,
931 "enable-plugins", enableplugins,
933 if (!(ua = getenv("SURF_USERAGENT")))
935 webkit_settings_set_user_agent(settings, ua);
936 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
937 * for more interesting settings */
939 contentmanager = webkit_user_content_manager_new();
941 context = webkit_web_context_new_with_website_data_manager(
942 webkit_website_data_manager_new(
943 "base-cache-directory", cachedir,
944 "base-data-directory", cachedir,
947 /* rendering process model, can be a shared unique one or one for each
949 webkit_web_context_set_process_model(context,
950 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
952 webkit_web_context_set_tls_errors_policy(context, strictssl ?
953 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
955 webkit_web_context_set_cache_model(context, enablecache ?
956 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
958 /* Currently only works with text file to be compatible with curl */
959 webkit_cookie_manager_set_persistent_storage(
960 webkit_web_context_get_cookie_manager(context), cookiefile,
961 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
963 webkit_cookie_manager_set_accept_policy(
964 webkit_web_context_get_cookie_manager(context),
967 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
968 "settings", settings,
969 "user-content-manager", contentmanager,
970 "web-context", context,
974 g_signal_connect(G_OBJECT(v),
976 G_CALLBACK(titlechanged), c);
977 g_signal_connect(G_OBJECT(v),
978 "mouse-target-changed",
979 G_CALLBACK(mousetargetchanged), c);
980 g_signal_connect(G_OBJECT(v),
981 "permission-request",
982 G_CALLBACK(permissionrequested), c);
983 g_signal_connect(G_OBJECT(v),
985 G_CALLBACK(createview), c);
986 g_signal_connect(G_OBJECT(v), "ready-to-show",
987 G_CALLBACK(showview), c);
988 g_signal_connect(G_OBJECT(v),
990 G_CALLBACK(decidepolicy), c);
991 g_signal_connect(G_OBJECT(v),
992 "window-object-cleared",
993 G_CALLBACK(windowobjectcleared), c);
994 g_signal_connect(G_OBJECT(v),
995 "notify::load-status",
996 G_CALLBACK(loadstatuschange), c);
997 g_signal_connect(G_OBJECT(v),
999 G_CALLBACK(progresschange), c);
1000 g_signal_connect(G_OBJECT(v),
1001 "download-requested",
1002 G_CALLBACK(initdownload), c);
1003 g_signal_connect(G_OBJECT(v),
1004 "button-release-event",
1005 G_CALLBACK(buttonrelease), c);
1006 g_signal_connect(G_OBJECT(v),
1008 G_CALLBACK(contextmenu), c);
1009 g_signal_connect(G_OBJECT(v),
1010 "resource-request-starting",
1011 G_CALLBACK(beforerequest), c);
1012 g_signal_connect(G_OBJECT(v),
1013 "should-show-delete-interface-for-element",
1014 G_CALLBACK(deletion_interface), c);
1020 showview(WebKitWebView *v, Client *c)
1022 GdkGeometry hints = { 1, 1 };
1023 GdkRGBA bgcolor = { 0 };
1028 c->win = gtk_plug_new(embed);
1030 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1032 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
1033 * is always correct, so we should still call this function.
1034 * But when doing so, we *must* differentiate between a
1035 * WM_CLASS and a resource on the window. By convention, the
1036 * window class (WM_CLASS) is capped, while the resource is in
1037 * lowercase. Both these values come as a pair.
1039 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
1041 /* TA: 20091214: And set the role here as well -- so that
1042 * sessions can pick this up.
1044 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
1046 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
1047 g_signal_connect(G_OBJECT(c->win),
1049 G_CALLBACK(destroywin), c);
1050 g_signal_connect(G_OBJECT(c->win),
1051 "leave_notify_event",
1052 G_CALLBACK(titlechangeleave), c);
1058 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
1061 gtk_widget_grab_focus(GTK_WIDGET(c->view));
1062 gtk_widget_show(GTK_WIDGET(c->view));
1063 gtk_widget_show(c->win);
1064 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1065 c->xid = gdk_x11_window_get_xid(gwin);
1066 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1068 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1069 gdk_window_add_filter(gwin, processx, c);
1073 /* This might conflict with _zoomto96dpi_. */
1074 if (zoomlevel != 1.0)
1075 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1077 if (runinfullscreen)
1078 fullscreen(c, NULL);
1080 setatom(c, AtomFind, "");
1081 setatom(c, AtomUri, "about:blank");
1083 webkit_web_view_set_background_color(c->view, &bgcolor);
1086 gdk_display_sync(gtk_widget_get_display(c->win));
1087 printf("%lu\n", c->xid);
1089 if (fclose(stdout) != 0) {
1090 die("Error closing stdout");
1096 newwindow(Client *c, const Arg *arg, gboolean noembed)
1099 const char *cmd[18], *uri;
1100 const Arg a = { .v = (void *)cmd };
1105 cmd[i++] = cookiepolicies;
1106 if (!enablescrollbars)
1108 if (embed && !noembed) {
1110 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1113 if (!allowgeolocation)
1125 if (enablediskcache)
1128 cmd[i++] = cookiefile;
1130 uri = arg->v ? (char *)arg->v : c->linkhover;
1138 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1139 gboolean keyboard, Client *c)
1141 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1143 for (GList *l = items; l; l = l->next)
1144 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1151 menuactivate(GtkMenuItem *item, Client *c)
1154 * context-menu-action-2000 open link
1155 * context-menu-action-1 open link in window
1156 * context-menu-action-2 download linked file
1157 * context-menu-action-3 copy link location
1158 * context-menu-action-7 copy image address
1159 * context-menu-action-13 reload
1160 * context-menu-action-10 back
1161 * context-menu-action-11 forward
1162 * context-menu-action-12 stop
1165 const gchar *name, *uri;
1166 GtkClipboard *prisel, *clpbrd;
1168 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1172 if (!g_strcmp0(name, "context-menu-action-3")) {
1173 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1174 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1175 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1176 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1177 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1178 uri = gtk_clipboard_wait_for_text(clpbrd);
1180 gtk_clipboard_set_text(prisel, uri, -1);
1185 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1187 Arg arg = {.v = text };
1189 loaduri((Client *) d, &arg);
1193 print(Client *c, const Arg *arg)
1195 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1199 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1201 Client *c = (Client *)d;
1205 if (((XEvent *)e)->type == PropertyNotify) {
1206 ev = &((XEvent *)e)->xproperty;
1207 if (ev->state == PropertyNewValue) {
1208 if (ev->atom == atoms[AtomFind]) {
1212 return GDK_FILTER_REMOVE;
1213 } else if (ev->atom == atoms[AtomGo]) {
1214 arg.v = getatom(c, AtomGo);
1217 return GDK_FILTER_REMOVE;
1221 return GDK_FILTER_CONTINUE;
1225 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1227 c->progress = webkit_web_view_get_progress(c->view) * 100;
1232 linkopen(Client *c, const Arg *arg)
1234 newwindow(NULL, arg, 1);
1238 linkopenembed(Client *c, const Arg *arg)
1240 newwindow(NULL, arg, 0);
1244 reload(Client *c, const Arg *arg)
1246 gboolean nocache = *(gboolean *)arg;
1248 webkit_web_view_reload_bypass_cache(c->view);
1250 webkit_web_view_reload(c->view);
1254 scroll_h(Client *c, const Arg *arg)
1256 scroll(gtk_scrolled_window_get_hadjustment(
1257 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1261 scroll_v(Client *c, const Arg *arg)
1263 scroll(gtk_scrolled_window_get_vadjustment(
1264 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1268 scroll(GtkAdjustment *a, const Arg *arg)
1272 v = gtk_adjustment_get_value(a);
1276 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1281 v += gtk_adjustment_get_step_increment(a) * arg->i;
1285 v = MIN(v, gtk_adjustment_get_upper(a) -
1286 gtk_adjustment_get_page_size(a));
1287 gtk_adjustment_set_value(a, v);
1291 setatom(Client *c, int a, const char *v)
1294 XChangeProperty(dpy, c->xid,
1295 atoms[a], XA_STRING, 8, PropModeReplace,
1296 (unsigned char *)v, strlen(v) + 1);
1303 char *styledirfile, *stylepath;
1304 WebKitWebContext *context;
1305 GError *error = NULL;
1307 /* clean up any zombies immediately */
1309 gtk_init(NULL, NULL);
1311 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1314 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1315 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1316 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1318 /* dirs and files */
1319 cookiefile = buildfile(cookiefile);
1320 scriptfile = buildfile(scriptfile);
1321 cachedir = buildpath(cachedir);
1322 if (stylefile == NULL) {
1323 styledir = buildpath(styledir);
1324 for (i = 0; i < LENGTH(styles); i++) {
1325 if (regcomp(&(styles[i].re), styles[i].regex,
1328 "Could not compile regex: %s\n",
1330 styles[i].regex = NULL;
1332 styledirfile = g_strconcat(styledir, "/",
1333 styles[i].style, NULL);
1334 stylepath = buildfile(styledirfile);
1335 styles[i].style = g_strconcat("file://", stylepath,
1337 g_free(styledirfile);
1342 stylepath = buildfile(stylefile);
1343 stylefile = g_strconcat("file://", stylepath, NULL);
1351 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1352 die("Can't install SIGCHLD handler");
1353 while (0 < waitpid(-1, NULL, WNOHANG));
1357 spawn(Client *c, const Arg *arg)
1361 close(ConnectionNumber(dpy));
1363 execvp(((char **)arg->v)[0], (char **)arg->v);
1364 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1371 eval(Client *c, const Arg *arg)
1373 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1374 evalscript(webkit_web_frame_get_global_context(frame),
1375 ((char **)arg->v)[0], "");
1379 stop(Client *c, const Arg *arg)
1381 webkit_web_view_stop_loading(c->view);
1385 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1387 c->title = webkit_web_view_get_title(c->view);
1392 titlechangeleave(void *a, void *b, Client *c)
1394 c->linkhover = NULL;
1399 toggle(Client *c, const Arg *arg)
1401 WebKitWebSettings *settings;
1402 char *name = (char *)arg->v;
1404 Arg a = { .b = FALSE };
1406 settings = webkit_web_view_get_settings(c->view);
1407 g_object_get(G_OBJECT(settings), name, &value, NULL);
1408 g_object_set(G_OBJECT(settings), name, !value, NULL);
1414 togglecookiepolicy(Client *c, const Arg *arg)
1417 cookiepolicy %= strlen(cookiepolicies);
1419 webkit_cookie_manager_set_accept_policy(
1420 webkit_web_context_get_cookie_manager(
1421 webkit_web_view_get_context(c->view)),
1422 cookiepolicy_get());
1425 /* Do not reload. */
1429 togglegeolocation(Client *c, const Arg *arg)
1431 Arg a = { .b = FALSE };
1433 allowgeolocation ^= 1;
1438 twitch(Client *c, const Arg *arg)
1443 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1446 v = gtk_adjustment_get_value(a);
1451 v = MIN(v, gtk_adjustment_get_upper(a) -
1452 gtk_adjustment_get_page_size(a));
1453 gtk_adjustment_set_value(a, v);
1457 togglescrollbars(Client *c, const Arg *arg)
1459 GtkPolicyType vspolicy;
1462 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1465 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1466 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1470 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1471 GTK_POLICY_AUTOMATIC,
1472 GTK_POLICY_AUTOMATIC);
1481 togglestyle(Client *c, const Arg *arg)
1483 enablestyle = !enablestyle;
1484 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1490 gettogglestat(Client *c)
1494 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1496 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1498 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1500 togglestat[p++] = value? 'C': 'c';
1502 togglestat[p++] = allowgeolocation? 'G': 'g';
1504 togglestat[p++] = enablediskcache? 'D': 'd';
1506 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1507 togglestat[p++] = value? 'I': 'i';
1509 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1510 togglestat[p++] = value? 'S': 's';
1512 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1513 togglestat[p++] = value? 'V': 'v';
1515 togglestat[p++] = enablestyle ? 'M': 'm';
1517 togglestat[p] = '\0';
1521 getpagestat(Client *c)
1523 const char *uri = geturi(c);
1525 if (strstr(uri, "https://") == uri)
1526 pagestat[0] = c->sslfailed ? 'U' : 'T';
1534 updatetitle(Client *c)
1538 if (showindicators) {
1543 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1545 } else if (c->progress != 100) {
1546 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1547 togglestat, pagestat,
1548 c->title == NULL ? "" : c->title);
1550 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1551 c->title == NULL ? "" : c->title);
1554 gtk_window_set_title(GTK_WINDOW(c->win), t);
1557 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1563 updatewinid(Client *c)
1565 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1571 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1572 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1573 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1577 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1578 JSObjectRef win, Client *c)
1584 zoom(Client *c, const Arg *arg)
1589 webkit_web_view_zoom_out(c->view);
1590 } else if (arg->i > 0) {
1592 webkit_web_view_zoom_in(c->view);
1596 webkit_web_view_set_zoom_level(c->view, 1.0);
1601 main(int argc, char *argv[])
1606 memset(&arg, 0, sizeof(arg));
1608 /* command line args */
1611 cookiepolicies = EARGF(usage());
1614 enablescrollbars = 0;
1617 enablescrollbars = 1;
1620 cookiefile = EARGF(usage());
1623 enablediskcache = 0;
1626 enablediskcache = 1;
1629 embed = strtol(EARGF(usage()), NULL, 0);
1632 runinfullscreen = 0;
1635 runinfullscreen = 1;
1638 allowgeolocation = 0;
1641 allowgeolocation = 1;
1662 enableinspector = 0;
1665 enableinspector = 1;
1674 scriptfile = EARGF(usage());
1683 stylefile = EARGF(usage());
1686 useragent = EARGF(usage());
1689 die("surf-"VERSION", ©2009-2015 surf engineers, "
1690 "see LICENSE for details\n");
1695 zoomlevel = strtof(EARGF(usage()), NULL);
1704 c = newclient(NULL);
1707 loaduri(clients, &arg);
1714 return EXIT_SUCCESS;