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 decidedownload(WebKitWebView *v, WebKitWebFrame *f,
120 WebKitNetworkRequest *r, gchar *m,
121 WebKitWebPolicyDecision *p, Client *c);
122 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
123 WebKitNetworkRequest *r, WebKitWebNavigationAction
124 *n, WebKitWebPolicyDecision *p, Client *c);
125 static gboolean deletion_interface(WebKitWebView *view,
126 WebKitDOMHTMLElement *arg1, Client *c);
127 static void destroyclient(Client *c);
128 static void destroywin(GtkWidget* w, Client *c);
129 static void die(const char *errstr, ...);
130 static void eval(Client *c, const Arg *arg);
131 static void find(Client *c, const Arg *arg);
132 static void fullscreen(Client *c, const Arg *arg);
133 static gboolean permissionrequested(WebKitWebView *v,
134 WebKitPermissionRequest *r, Client *c);
135 static const char *getatom(Client *c, int a);
136 static void gettogglestat(Client *c);
137 static void getpagestat(Client *c);
138 static char *geturi(Client *c);
139 static const gchar *getstyle(const char *uri);
140 static void setstyle(Client *c, const char *style);
142 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
144 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
146 static void inspector(Client *c, const Arg *arg);
147 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
149 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
150 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
151 static void inspector_finished(WebKitWebInspector *i, Client *c);
153 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
154 GdkModifierType mods, Client *c);
155 static void mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h,
156 guint modifiers, Client *c);
157 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
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 void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
166 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
167 WebKitHitTestResult *target, gboolean keyboard,
169 static void menuactivate(GtkMenuItem *item, Client *c);
170 static void print(Client *c, const Arg *arg);
171 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
173 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
174 static void linkopen(Client *c, const Arg *arg);
175 static void linkopenembed(Client *c, const Arg *arg);
176 static void reload(Client *c, const Arg *arg);
177 static void scroll_h(Client *c, const Arg *arg);
178 static void scroll_v(Client *c, const Arg *arg);
179 static void scroll(GtkAdjustment *a, const Arg *arg);
180 static void setatom(Client *c, int a, const char *v);
181 static void setup(void);
182 static void sigchld(int unused);
183 static void spawn(Client *c, const Arg *arg);
184 static void stop(Client *c, const Arg *arg);
185 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
186 static void titlechangeleave(void *a, void *b, Client *c);
187 static void toggle(Client *c, const Arg *arg);
188 static void togglecookiepolicy(Client *c, const Arg *arg);
189 static void togglegeolocation(Client *c, const Arg *arg);
190 static void togglescrollbars(Client *c, const Arg *arg);
191 static void togglestyle(Client *c, const Arg *arg);
192 static void updatetitle(Client *c);
193 static void updatewinid(Client *c);
194 static void usage(void);
195 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
196 JSContextRef js, JSObjectRef win, Client *c);
197 static void zoom(Client *c, const Arg *arg);
199 /* configuration, allows nested code to access above variables */
203 addaccelgroup(Client *c)
206 GtkAccelGroup *group = gtk_accel_group_new();
209 for (i = 0; i < LENGTH(keys); i++) {
210 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
211 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
214 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
218 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
219 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
222 const gchar *uri = webkit_network_request_get_uri(req);
225 if (g_str_has_suffix(uri, "/favicon.ico"))
226 webkit_network_request_set_uri(req, "about:blank");
228 if (!g_str_has_prefix(uri, "http://")
229 && !g_str_has_prefix(uri, "https://")
230 && !g_str_has_prefix(uri, "about:")
231 && !g_str_has_prefix(uri, "file://")
232 && !g_str_has_prefix(uri, "data:")
233 && !g_str_has_prefix(uri, "blob:")
234 && strlen(uri) > 0) {
235 for (i = 0; i < strlen(uri); i++) {
236 if (!g_ascii_isprint(uri[i])) {
242 handleplumb(c, w, uri);
247 buildfile(const char *path)
249 char *dname, *bname, *bpath, *fpath;
252 dname = g_path_get_dirname(path);
253 bname = g_path_get_basename(path);
255 bpath = buildpath(dname);
258 fpath = g_build_filename(bpath, bname, NULL);
262 if (!(f = fopen(fpath, "a")))
263 die("Could not open file: %s\n", fpath);
265 g_chmod(fpath, 0600); /* always */
272 buildpath(const char *path)
275 char *apath, *name, *p, *fpath;
277 if (path[0] == '~') {
278 if (path[1] == '/' || path[1] == '\0') {
279 p = (char *)&path[1];
280 pw = getpwuid(getuid());
282 if ((p = strchr(path, '/')))
283 name = g_strndup(&path[1], --p - path);
285 name = g_strdup(&path[1]);
287 if (!(pw = getpwnam(name))) {
288 die("Can't get user %s home directory: %s.\n",
293 apath = g_build_filename(pw->pw_dir, p, NULL);
295 apath = g_strdup(path);
298 /* creating directory */
299 if (g_mkdir_with_parents(apath, 0700) < 0)
300 die("Could not access directory: %s\n", apath);
302 fpath = realpath(apath, NULL);
309 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
311 WebKitHitTestResultContext context;
312 WebKitHitTestResult *result;
316 result = webkit_web_view_get_hit_test_result(web, e);
317 g_object_get(result, "context", &context, NULL);
318 g_object_get(result, "link-uri", &arg.v, NULL);
319 for (i = 0; i < LENGTH(buttons); i++) {
320 if (context & buttons[i].click
321 && e->button == buttons[i].button
322 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
323 && buttons[i].func) {
324 buttons[i].func(c, buttons[i].click == ClkLink
325 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
336 destroyclient(clients);
343 WebKitCookieAcceptPolicy
344 cookiepolicy_get(void)
346 switch (cookiepolicies[cookiepolicy]) {
348 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
350 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
356 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
360 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
363 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
365 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
367 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
376 evalscript(JSContextRef js, char *script, char* scriptname)
378 JSStringRef jsscript, jsscriptname;
379 JSValueRef exception = NULL;
381 jsscript = JSStringCreateWithUTF8CString(script);
382 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
383 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
384 jsscriptname, 0, &exception);
385 JSStringRelease(jsscript);
386 JSStringRelease(jsscriptname);
390 runscript(WebKitWebFrame *frame)
395 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
396 evalscript(webkit_web_frame_get_global_context(frame), script,
402 clipboard(Client *c, const Arg *arg)
404 gboolean paste = *(gboolean *)arg;
407 gtk_clipboard_request_text(gtk_clipboard_get(
408 GDK_SELECTION_PRIMARY),
411 gtk_clipboard_set_text(gtk_clipboard_get(
412 GDK_SELECTION_PRIMARY), c->linkhover
413 ? c->linkhover : geturi(c), -1);
418 copystr(char **str, const char *src)
431 createview(WebKitWebView *v, WebKitNavigationAction *a, Client *c)
435 switch (webkit_navigation_action_get_navigation_type(a)) {
436 case WEBKIT_NAVIGATION_TYPE_OTHER: /* fallthrough */
438 * popup windows of type “other” are almost always triggered
439 * by user gesture, so inverse the logic here
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 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
462 gchar *m, WebKitWebPolicyDecision *p, Client *c)
464 if (!webkit_web_view_can_show_mime_type(v, m)) {
465 webkit_web_policy_decision_download(p);
472 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
473 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
478 if (webkit_web_navigation_action_get_reason(n)
479 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
480 webkit_web_policy_decision_ignore(p);
481 arg.v = (void *)webkit_network_request_get_uri(r);
482 newwindow(NULL, &arg, 0);
489 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
495 destroyclient(Client *c)
499 webkit_web_view_stop_loading(c->view);
500 gtk_widget_destroy(GTK_WIDGET(c->view));
501 gtk_widget_destroy(c->scroll);
502 gtk_widget_destroy(c->vbox);
503 gtk_widget_destroy(c->win);
505 for (p = clients; p && p->next != c; p = p->next)
517 destroywin(GtkWidget* w, Client *c)
523 die(const char *errstr, ...)
527 va_start(ap, errstr);
528 vfprintf(stderr, errstr, ap);
534 find(Client *c, const Arg *arg)
538 s = getatom(c, AtomFind);
539 gboolean forward = *(gboolean *)arg;
540 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
544 fullscreen(Client *c, const Arg *arg)
547 gtk_window_unfullscreen(GTK_WINDOW(c->win));
549 gtk_window_fullscreen(GTK_WINDOW(c->win));
550 c->fullscreen = !c->fullscreen;
554 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
556 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
557 if (allowgeolocation)
558 webkit_permission_request_allow(r);
560 webkit_permission_request_deny(r);
568 getatom(Client *c, int a)
570 static char buf[BUFSIZ];
573 unsigned long ldummy;
574 unsigned char *p = NULL;
576 XGetWindowProperty(dpy, c->xid,
577 atoms[a], 0L, BUFSIZ, False, XA_STRING,
578 &adummy, &idummy, &ldummy, &ldummy, &p);
580 strncpy(buf, (char *)p, LENGTH(buf)-1);
593 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
599 getstyle(const char *uri)
603 if (stylefile != NULL)
606 for (i = 0; i < LENGTH(styles); i++) {
607 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
609 return styles[i].style;
616 setstyle(Client *c, const char *style)
618 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
620 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
624 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
628 webkit_web_view_stop_loading(w);
629 arg = (Arg)PLUMB((char *)uri);
634 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
639 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
645 inspector(Client *c, const Arg *arg)
647 if (enableinspector) {
649 webkit_web_inspector_close(c->inspector);
651 webkit_web_inspector_show(c->inspector);
656 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
658 return WEBKIT_WEB_VIEW(webkit_web_view_new());
662 inspector_show(WebKitWebInspector *i, Client *c)
669 w = webkit_web_inspector_get_web_view(i);
670 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
671 gtk_widget_show(GTK_WIDGET(w));
672 c->isinspecting = true;
678 inspector_close(WebKitWebInspector *i, Client *c)
682 if (!c->isinspecting)
685 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
687 gtk_widget_destroy(w);
688 c->isinspecting = false;
694 inspector_finished(WebKitWebInspector *i, Client *c)
696 g_free(c->inspector);
700 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
704 gboolean processed = FALSE;
706 mods = CLEANMASK(mods);
707 key = gdk_keyval_to_lower(key);
709 for (i = 0; i < LENGTH(keys); i++) {
710 if (key == keys[i].keyval
711 && mods == keys[i].mod
713 keys[i].func(c, &(keys[i].arg));
722 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
725 WebKitHitTestResultContext hc;
727 /* Keep the hit test to know where is the pointer on the next click */
730 hc = webkit_hit_test_result_get_context(h);
733 c->targeturi = webkit_hit_test_result_get_link_uri(h);
735 c->targeturi = webkit_hit_test_result_get_image_uri(h);
736 else if (hc & OnMedia)
737 c->targeturi = webkit_hit_test_result_get_media_uri(h);
744 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
746 WebKitWebFrame *frame;
747 WebKitWebDataSource *src;
748 WebKitNetworkRequest *request;
752 switch (webkit_web_view_get_load_status (c->view)) {
753 case WEBKIT_LOAD_COMMITTED:
755 if (strstr(uri, "https://") == uri) {
756 frame = webkit_web_view_get_main_frame(c->view);
757 src = webkit_web_frame_get_data_source(frame);
758 request = webkit_web_data_source_get_request(src);
759 msg = webkit_network_request_get_message(request);
760 c->sslfailed = !(soup_message_get_flags(msg)
761 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
763 setatom(c, AtomUri, uri);
766 setstyle(c, getstyle(uri));
768 case WEBKIT_LOAD_FINISHED:
778 loaduri(Client *c, const Arg *arg)
781 const char *uri = (char *)arg->v;
782 Arg a = { .b = FALSE };
785 if (strcmp(uri, "") == 0)
788 /* In case it's a file path. */
789 if (stat(uri, &st) == 0) {
790 rp = realpath(uri, NULL);
791 u = g_strdup_printf("file://%s", rp);
794 u = g_strrstr(uri, "://") ? g_strdup(uri)
795 : g_strdup_printf("http://%s", uri);
798 setatom(c, AtomUri, uri);
800 /* prevents endless loop */
801 if (strcmp(u, geturi(c)) == 0) {
804 webkit_web_view_load_uri(c->view, u);
806 c->title = copystr(&c->title, u);
813 navigate(Client *c, const Arg *arg)
815 int steps = *(int *)arg;
816 webkit_web_view_go_back_or_forward(c->view, steps);
820 newclient(Client *rc)
825 if (!(c = calloc(1, sizeof(Client))))
826 die("Cannot malloc!\n");
834 c->view = newview(c, rc ? rc->view : NULL);
840 newview(Client *c, WebKitWebView *rv)
843 WebKitSettings *settings;
844 WebKitUserContentManager *contentmanager;
845 WebKitWebContext *context;
851 webkit_web_view_new_with_related_view(rv));
853 settings = webkit_settings_new_with_settings(
854 "auto-load-images", loadimages,
855 "default-font-size", defaultfontsize,
856 "enable-caret-browsing", enablecaretbrowsing,
857 "enable-developer-extras", enableinspector,
858 "enable-dns-prefetching", enablednsprefetching,
859 "enable-frame-flattening", enableframeflattening,
860 "enable-html5-database", enablecache,
861 "enable-html5-local-storage", enablecache,
862 "enable-javascript", enablescripts,
863 "enable-plugins", enableplugins,
865 if (!(ua = getenv("SURF_USERAGENT")))
867 webkit_settings_set_user_agent(settings, ua);
868 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
869 * for more interesting settings */
871 contentmanager = webkit_user_content_manager_new();
873 context = webkit_web_context_new_with_website_data_manager(
874 webkit_website_data_manager_new(
875 "base-cache-directory", cachedir,
876 "base-data-directory", cachedir,
879 /* rendering process model, can be a shared unique one or one for each
881 webkit_web_context_set_process_model(context,
882 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
884 webkit_web_context_set_tls_errors_policy(context, strictssl ?
885 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
887 webkit_web_context_set_cache_model(context, enablecache ?
888 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
890 /* Currently only works with text file to be compatible with curl */
891 webkit_cookie_manager_set_persistent_storage(
892 webkit_web_context_get_cookie_manager(context), cookiefile,
893 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
895 webkit_cookie_manager_set_accept_policy(
896 webkit_web_context_get_cookie_manager(context),
899 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
900 "settings", settings,
901 "user-content-manager", contentmanager,
902 "web-context", context,
906 g_signal_connect(G_OBJECT(v),
908 G_CALLBACK(titlechanged), c);
909 g_signal_connect(G_OBJECT(v),
910 "mouse-target-changed",
911 G_CALLBACK(mousetargetchanged), c);
912 g_signal_connect(G_OBJECT(v),
913 "permission-request",
914 G_CALLBACK(permissionrequested), c);
915 g_signal_connect(G_OBJECT(v),
917 G_CALLBACK(createview), c);
918 g_signal_connect(G_OBJECT(v), "ready-to-show",
919 G_CALLBACK(showview), c);
920 g_signal_connect(G_OBJECT(v),
921 "new-window-policy-decision-requested",
922 G_CALLBACK(decidewindow), c);
923 g_signal_connect(G_OBJECT(v),
924 "mime-type-policy-decision-requested",
925 G_CALLBACK(decidedownload), c);
926 g_signal_connect(G_OBJECT(v),
927 "window-object-cleared",
928 G_CALLBACK(windowobjectcleared), c);
929 g_signal_connect(G_OBJECT(v),
930 "notify::load-status",
931 G_CALLBACK(loadstatuschange), c);
932 g_signal_connect(G_OBJECT(v),
934 G_CALLBACK(progresschange), c);
935 g_signal_connect(G_OBJECT(v),
936 "download-requested",
937 G_CALLBACK(initdownload), c);
938 g_signal_connect(G_OBJECT(v),
939 "button-release-event",
940 G_CALLBACK(buttonrelease), c);
941 g_signal_connect(G_OBJECT(v),
943 G_CALLBACK(contextmenu), c);
944 g_signal_connect(G_OBJECT(v),
945 "resource-request-starting",
946 G_CALLBACK(beforerequest), c);
947 g_signal_connect(G_OBJECT(v),
948 "should-show-delete-interface-for-element",
949 G_CALLBACK(deletion_interface), c);
955 showview(WebKitWebView *v, Client *c)
957 GdkGeometry hints = { 1, 1 };
958 GdkRGBA bgcolor = { 0 };
963 c->win = gtk_plug_new(embed);
965 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
967 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
968 * is always correct, so we should still call this function.
969 * But when doing so, we *must* differentiate between a
970 * WM_CLASS and a resource on the window. By convention, the
971 * window class (WM_CLASS) is capped, while the resource is in
972 * lowercase. Both these values come as a pair.
974 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
976 /* TA: 20091214: And set the role here as well -- so that
977 * sessions can pick this up.
979 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
981 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
982 g_signal_connect(G_OBJECT(c->win),
984 G_CALLBACK(destroywin), c);
985 g_signal_connect(G_OBJECT(c->win),
986 "leave_notify_event",
987 G_CALLBACK(titlechangeleave), c);
993 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
996 gtk_widget_grab_focus(GTK_WIDGET(c->view));
997 gtk_widget_show(GTK_WIDGET(c->view));
998 gtk_widget_show(c->win);
999 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
1000 c->xid = gdk_x11_window_get_xid(gwin);
1001 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
1003 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
1004 gdk_window_add_filter(gwin, processx, c);
1008 /* This might conflict with _zoomto96dpi_. */
1009 if (zoomlevel != 1.0)
1010 webkit_web_view_set_zoom_level(c->view, zoomlevel);
1012 if (runinfullscreen)
1013 fullscreen(c, NULL);
1015 setatom(c, AtomFind, "");
1016 setatom(c, AtomUri, "about:blank");
1018 webkit_web_view_set_background_color(c->view, &bgcolor);
1021 gdk_display_sync(gtk_widget_get_display(c->win));
1022 printf("%lu\n", c->xid);
1024 if (fclose(stdout) != 0) {
1025 die("Error closing stdout");
1031 newwindow(Client *c, const Arg *arg, gboolean noembed)
1034 const char *cmd[18], *uri;
1035 const Arg a = { .v = (void *)cmd };
1040 cmd[i++] = cookiepolicies;
1041 if (!enablescrollbars)
1043 if (embed && !noembed) {
1045 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1048 if (!allowgeolocation)
1060 if (enablediskcache)
1063 cmd[i++] = cookiefile;
1065 uri = arg->v ? (char *)arg->v : c->linkhover;
1073 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1074 gboolean keyboard, Client *c)
1076 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1078 for (GList *l = items; l; l = l->next)
1079 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1086 menuactivate(GtkMenuItem *item, Client *c)
1089 * context-menu-action-2000 open link
1090 * context-menu-action-1 open link in window
1091 * context-menu-action-2 download linked file
1092 * context-menu-action-3 copy link location
1093 * context-menu-action-7 copy image address
1094 * context-menu-action-13 reload
1095 * context-menu-action-10 back
1096 * context-menu-action-11 forward
1097 * context-menu-action-12 stop
1100 const gchar *name, *uri;
1101 GtkClipboard *prisel, *clpbrd;
1103 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1107 if (!g_strcmp0(name, "context-menu-action-3")) {
1108 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1109 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1110 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1111 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1112 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1113 uri = gtk_clipboard_wait_for_text(clpbrd);
1115 gtk_clipboard_set_text(prisel, uri, -1);
1120 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1122 Arg arg = {.v = text };
1124 loaduri((Client *) d, &arg);
1128 print(Client *c, const Arg *arg)
1130 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1134 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1136 Client *c = (Client *)d;
1140 if (((XEvent *)e)->type == PropertyNotify) {
1141 ev = &((XEvent *)e)->xproperty;
1142 if (ev->state == PropertyNewValue) {
1143 if (ev->atom == atoms[AtomFind]) {
1147 return GDK_FILTER_REMOVE;
1148 } else if (ev->atom == atoms[AtomGo]) {
1149 arg.v = getatom(c, AtomGo);
1152 return GDK_FILTER_REMOVE;
1156 return GDK_FILTER_CONTINUE;
1160 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1162 c->progress = webkit_web_view_get_progress(c->view) * 100;
1167 linkopen(Client *c, const Arg *arg)
1169 newwindow(NULL, arg, 1);
1173 linkopenembed(Client *c, const Arg *arg)
1175 newwindow(NULL, arg, 0);
1179 reload(Client *c, const Arg *arg)
1181 gboolean nocache = *(gboolean *)arg;
1183 webkit_web_view_reload_bypass_cache(c->view);
1185 webkit_web_view_reload(c->view);
1189 scroll_h(Client *c, const Arg *arg)
1191 scroll(gtk_scrolled_window_get_hadjustment(
1192 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1196 scroll_v(Client *c, const Arg *arg)
1198 scroll(gtk_scrolled_window_get_vadjustment(
1199 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1203 scroll(GtkAdjustment *a, const Arg *arg)
1207 v = gtk_adjustment_get_value(a);
1211 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1216 v += gtk_adjustment_get_step_increment(a) * arg->i;
1220 v = MIN(v, gtk_adjustment_get_upper(a) -
1221 gtk_adjustment_get_page_size(a));
1222 gtk_adjustment_set_value(a, v);
1226 setatom(Client *c, int a, const char *v)
1229 XChangeProperty(dpy, c->xid,
1230 atoms[a], XA_STRING, 8, PropModeReplace,
1231 (unsigned char *)v, strlen(v) + 1);
1238 char *styledirfile, *stylepath;
1239 WebKitWebContext *context;
1240 GError *error = NULL;
1242 /* clean up any zombies immediately */
1244 gtk_init(NULL, NULL);
1246 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1249 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1250 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1251 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1253 /* dirs and files */
1254 cookiefile = buildfile(cookiefile);
1255 scriptfile = buildfile(scriptfile);
1256 cachedir = buildpath(cachedir);
1257 if (stylefile == NULL) {
1258 styledir = buildpath(styledir);
1259 for (i = 0; i < LENGTH(styles); i++) {
1260 if (regcomp(&(styles[i].re), styles[i].regex,
1263 "Could not compile regex: %s\n",
1265 styles[i].regex = NULL;
1267 styledirfile = g_strconcat(styledir, "/",
1268 styles[i].style, NULL);
1269 stylepath = buildfile(styledirfile);
1270 styles[i].style = g_strconcat("file://", stylepath,
1272 g_free(styledirfile);
1277 stylepath = buildfile(stylefile);
1278 stylefile = g_strconcat("file://", stylepath, NULL);
1286 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1287 die("Can't install SIGCHLD handler");
1288 while (0 < waitpid(-1, NULL, WNOHANG));
1292 spawn(Client *c, const Arg *arg)
1296 close(ConnectionNumber(dpy));
1298 execvp(((char **)arg->v)[0], (char **)arg->v);
1299 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1306 eval(Client *c, const Arg *arg)
1308 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1309 evalscript(webkit_web_frame_get_global_context(frame),
1310 ((char **)arg->v)[0], "");
1314 stop(Client *c, const Arg *arg)
1316 webkit_web_view_stop_loading(c->view);
1320 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1322 c->title = webkit_web_view_get_title(c->view);
1327 titlechangeleave(void *a, void *b, Client *c)
1329 c->linkhover = NULL;
1334 toggle(Client *c, const Arg *arg)
1336 WebKitWebSettings *settings;
1337 char *name = (char *)arg->v;
1339 Arg a = { .b = FALSE };
1341 settings = webkit_web_view_get_settings(c->view);
1342 g_object_get(G_OBJECT(settings), name, &value, NULL);
1343 g_object_set(G_OBJECT(settings), name, !value, NULL);
1349 togglecookiepolicy(Client *c, const Arg *arg)
1352 cookiepolicy %= strlen(cookiepolicies);
1354 webkit_cookie_manager_set_accept_policy(
1355 webkit_web_context_get_cookie_manager(
1356 webkit_web_view_get_context(c->view)),
1357 cookiepolicy_get());
1360 /* Do not reload. */
1364 togglegeolocation(Client *c, const Arg *arg)
1366 Arg a = { .b = FALSE };
1368 allowgeolocation ^= 1;
1373 twitch(Client *c, const Arg *arg)
1378 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1381 v = gtk_adjustment_get_value(a);
1386 v = MIN(v, gtk_adjustment_get_upper(a) -
1387 gtk_adjustment_get_page_size(a));
1388 gtk_adjustment_set_value(a, v);
1392 togglescrollbars(Client *c, const Arg *arg)
1394 GtkPolicyType vspolicy;
1397 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1400 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1401 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1405 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1406 GTK_POLICY_AUTOMATIC,
1407 GTK_POLICY_AUTOMATIC);
1416 togglestyle(Client *c, const Arg *arg)
1418 enablestyle = !enablestyle;
1419 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1425 gettogglestat(Client *c)
1429 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1431 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1433 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1435 togglestat[p++] = value? 'C': 'c';
1437 togglestat[p++] = allowgeolocation? 'G': 'g';
1439 togglestat[p++] = enablediskcache? 'D': 'd';
1441 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1442 togglestat[p++] = value? 'I': 'i';
1444 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1445 togglestat[p++] = value? 'S': 's';
1447 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1448 togglestat[p++] = value? 'V': 'v';
1450 togglestat[p++] = enablestyle ? 'M': 'm';
1452 togglestat[p] = '\0';
1456 getpagestat(Client *c)
1458 const char *uri = geturi(c);
1460 if (strstr(uri, "https://") == uri)
1461 pagestat[0] = c->sslfailed ? 'U' : 'T';
1469 updatetitle(Client *c)
1473 if (showindicators) {
1478 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1480 } else if (c->progress != 100) {
1481 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1482 togglestat, pagestat,
1483 c->title == NULL ? "" : c->title);
1485 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1486 c->title == NULL ? "" : c->title);
1489 gtk_window_set_title(GTK_WINDOW(c->win), t);
1492 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1498 updatewinid(Client *c)
1500 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1506 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1507 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1508 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1512 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1513 JSObjectRef win, Client *c)
1519 zoom(Client *c, const Arg *arg)
1524 webkit_web_view_zoom_out(c->view);
1525 } else if (arg->i > 0) {
1527 webkit_web_view_zoom_in(c->view);
1531 webkit_web_view_set_zoom_level(c->view, 1.0);
1536 main(int argc, char *argv[])
1541 memset(&arg, 0, sizeof(arg));
1543 /* command line args */
1546 cookiepolicies = EARGF(usage());
1549 enablescrollbars = 0;
1552 enablescrollbars = 1;
1555 cookiefile = EARGF(usage());
1558 enablediskcache = 0;
1561 enablediskcache = 1;
1564 embed = strtol(EARGF(usage()), NULL, 0);
1567 runinfullscreen = 0;
1570 runinfullscreen = 1;
1573 allowgeolocation = 0;
1576 allowgeolocation = 1;
1597 enableinspector = 0;
1600 enableinspector = 1;
1609 scriptfile = EARGF(usage());
1618 stylefile = EARGF(usage());
1621 useragent = EARGF(usage());
1624 die("surf-"VERSION", ©2009-2015 surf engineers, "
1625 "see LICENSE for details\n");
1630 zoomlevel = strtof(EARGF(usage()), NULL);
1639 c = newclient(NULL);
1642 loaduri(clients, &arg);
1649 return EXIT_SUCCESS;