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 };
39 ClkDoc = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT,
40 ClkLink = WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK,
41 ClkImg = WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE,
42 ClkMedia = WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA,
43 ClkSel = WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION,
44 ClkEdit = WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE,
45 ClkAny = ClkDoc | ClkLink | ClkImg | ClkMedia | ClkSel | ClkEdit,
48 typedef union Arg Arg;
55 typedef struct Client {
59 WebKitWebInspector *inspector;
60 const char *title, *linkhover;
64 gboolean zoomed, fullscreen, isinspecting, sslfailed;
70 void (*func)(Client *c, const Arg *arg);
78 void (*func)(Client *c, const Arg *arg);
89 static Atom atoms[AtomLast];
90 static Client *clients = NULL;
91 static Window embed = 0;
92 static gboolean showxid = FALSE;
93 static char winid[64];
94 static char togglestat[9];
95 static char pagestat[3];
96 static GTlsDatabase *tlsdb;
97 static int cookiepolicy;
98 static char *stylefile = NULL;
100 static void addaccelgroup(Client *c);
101 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
102 WebKitWebResource *r, WebKitNetworkRequest *req,
103 WebKitNetworkResponse *resp, Client *c);
104 static char *buildfile(const char *path);
105 static char *buildpath(const char *path);
106 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
107 static void cleanup(void);
108 static void clipboard(Client *c, const Arg *arg);
110 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
111 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
113 static char *copystr(char **str, const char *src);
114 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
116 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
117 WebKitNetworkRequest *r, gchar *m,
118 WebKitWebPolicyDecision *p, Client *c);
119 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
120 WebKitNetworkRequest *r, WebKitWebNavigationAction
121 *n, WebKitWebPolicyDecision *p, Client *c);
122 static gboolean deletion_interface(WebKitWebView *view,
123 WebKitDOMHTMLElement *arg1, 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 eval(Client *c, const Arg *arg);
128 static void find(Client *c, const Arg *arg);
129 static void fullscreen(Client *c, const Arg *arg);
130 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
131 WebKitGeolocationPolicyDecision *d, Client *c);
132 static const char *getatom(Client *c, int a);
133 static void gettogglestat(Client *c);
134 static void getpagestat(Client *c);
135 static char *geturi(Client *c);
136 static const gchar *getstyle(const char *uri);
137 static void setstyle(Client *c, const char *style);
139 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
141 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
143 static void inspector(Client *c, const Arg *arg);
144 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
146 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
147 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
148 static void inspector_finished(WebKitWebInspector *i, Client *c);
150 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
151 GdkModifierType mods, Client *c);
152 static void linkhover(WebKitWebView *v, const char* t, const char* l,
154 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
156 static void loaduri(Client *c, const Arg *arg);
157 static void navigate(Client *c, const Arg *arg);
158 static Client *newclient(Client *c);
159 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
160 static void showview(WebKitWebView *v, Client *c);
161 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
162 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
163 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
164 WebKitHitTestResult *target, gboolean keyboard,
166 static void menuactivate(GtkMenuItem *item, Client *c);
167 static void print(Client *c, const Arg *arg);
168 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
170 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
171 static void linkopen(Client *c, const Arg *arg);
172 static void linkopenembed(Client *c, const Arg *arg);
173 static void reload(Client *c, const Arg *arg);
174 static void scroll_h(Client *c, const Arg *arg);
175 static void scroll_v(Client *c, const Arg *arg);
176 static void scroll(GtkAdjustment *a, const Arg *arg);
177 static void setatom(Client *c, int a, const char *v);
178 static void setup(void);
179 static void sigchld(int unused);
180 static void spawn(Client *c, const Arg *arg);
181 static void stop(Client *c, const Arg *arg);
182 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
183 static void titlechangeleave(void *a, void *b, Client *c);
184 static void toggle(Client *c, const Arg *arg);
185 static void togglecookiepolicy(Client *c, const Arg *arg);
186 static void togglegeolocation(Client *c, const Arg *arg);
187 static void togglescrollbars(Client *c, const Arg *arg);
188 static void togglestyle(Client *c, const Arg *arg);
189 static void updatetitle(Client *c);
190 static void updatewinid(Client *c);
191 static void usage(void);
192 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
193 JSContextRef js, JSObjectRef win, Client *c);
194 static void zoom(Client *c, const Arg *arg);
196 /* configuration, allows nested code to access above variables */
200 addaccelgroup(Client *c)
203 GtkAccelGroup *group = gtk_accel_group_new();
206 for (i = 0; i < LENGTH(keys); i++) {
207 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
208 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
211 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
215 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
216 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
219 const gchar *uri = webkit_network_request_get_uri(req);
222 if (g_str_has_suffix(uri, "/favicon.ico"))
223 webkit_network_request_set_uri(req, "about:blank");
225 if (!g_str_has_prefix(uri, "http://")
226 && !g_str_has_prefix(uri, "https://")
227 && !g_str_has_prefix(uri, "about:")
228 && !g_str_has_prefix(uri, "file://")
229 && !g_str_has_prefix(uri, "data:")
230 && !g_str_has_prefix(uri, "blob:")
231 && strlen(uri) > 0) {
232 for (i = 0; i < strlen(uri); i++) {
233 if (!g_ascii_isprint(uri[i])) {
239 handleplumb(c, w, uri);
244 buildfile(const char *path)
246 char *dname, *bname, *bpath, *fpath;
249 dname = g_path_get_dirname(path);
250 bname = g_path_get_basename(path);
252 bpath = buildpath(dname);
255 fpath = g_build_filename(bpath, bname, NULL);
259 if (!(f = fopen(fpath, "a")))
260 die("Could not open file: %s\n", fpath);
262 g_chmod(fpath, 0600); /* always */
269 buildpath(const char *path)
272 char *apath, *name, *p, *fpath;
274 if (path[0] == '~') {
275 if (path[1] == '/' || path[1] == '\0') {
276 p = (char *)&path[1];
277 pw = getpwuid(getuid());
279 if ((p = strchr(path, '/')))
280 name = g_strndup(&path[1], --p - path);
282 name = g_strdup(&path[1]);
284 if (!(pw = getpwnam(name))) {
285 die("Can't get user %s home directory: %s.\n",
290 apath = g_build_filename(pw->pw_dir, p, NULL);
292 apath = g_strdup(path);
295 /* creating directory */
296 if (g_mkdir_with_parents(apath, 0700) < 0)
297 die("Could not access directory: %s\n", apath);
299 fpath = realpath(apath, NULL);
306 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
308 WebKitHitTestResultContext context;
309 WebKitHitTestResult *result;
313 result = webkit_web_view_get_hit_test_result(web, e);
314 g_object_get(result, "context", &context, NULL);
315 g_object_get(result, "link-uri", &arg.v, NULL);
316 for (i = 0; i < LENGTH(buttons); i++) {
317 if (context & buttons[i].click
318 && e->button == buttons[i].button
319 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
320 && buttons[i].func) {
321 buttons[i].func(c, buttons[i].click == ClkLink
322 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
333 destroyclient(clients);
340 WebKitCookieAcceptPolicy
341 cookiepolicy_get(void)
343 switch (cookiepolicies[cookiepolicy]) {
345 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
347 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
353 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
357 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
360 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
362 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
364 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
373 evalscript(JSContextRef js, char *script, char* scriptname)
375 JSStringRef jsscript, jsscriptname;
376 JSValueRef exception = NULL;
378 jsscript = JSStringCreateWithUTF8CString(script);
379 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
380 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
381 jsscriptname, 0, &exception);
382 JSStringRelease(jsscript);
383 JSStringRelease(jsscriptname);
387 runscript(WebKitWebFrame *frame)
392 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
393 evalscript(webkit_web_frame_get_global_context(frame), script,
399 clipboard(Client *c, const Arg *arg)
401 gboolean paste = *(gboolean *)arg;
404 gtk_clipboard_request_text(gtk_clipboard_get(
405 GDK_SELECTION_PRIMARY),
408 gtk_clipboard_set_text(gtk_clipboard_get(
409 GDK_SELECTION_PRIMARY), c->linkhover
410 ? c->linkhover : geturi(c), -1);
415 copystr(char **str, const char *src)
428 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
430 Client *n = newclient();
435 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
436 gchar *m, WebKitWebPolicyDecision *p, Client *c)
438 if (!webkit_web_view_can_show_mime_type(v, m)) {
439 webkit_web_policy_decision_download(p);
446 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
447 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
452 if (webkit_web_navigation_action_get_reason(n)
453 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
454 webkit_web_policy_decision_ignore(p);
455 arg.v = (void *)webkit_network_request_get_uri(r);
456 newwindow(NULL, &arg, 0);
463 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
469 destroyclient(Client *c)
473 webkit_web_view_stop_loading(c->view);
474 gtk_widget_destroy(GTK_WIDGET(c->view));
475 gtk_widget_destroy(c->scroll);
476 gtk_widget_destroy(c->vbox);
477 gtk_widget_destroy(c->win);
479 for (p = clients; p && p->next != c; p = p->next)
491 destroywin(GtkWidget* w, Client *c)
497 die(const char *errstr, ...)
501 va_start(ap, errstr);
502 vfprintf(stderr, errstr, ap);
508 find(Client *c, const Arg *arg)
512 s = getatom(c, AtomFind);
513 gboolean forward = *(gboolean *)arg;
514 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
518 fullscreen(Client *c, const Arg *arg)
521 gtk_window_unfullscreen(GTK_WINDOW(c->win));
523 gtk_window_fullscreen(GTK_WINDOW(c->win));
524 c->fullscreen = !c->fullscreen;
528 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
529 WebKitGeolocationPolicyDecision *d, Client *c)
531 if (allowgeolocation)
532 webkit_geolocation_policy_allow(d);
534 webkit_geolocation_policy_deny(d);
538 getatom(Client *c, int a)
540 static char buf[BUFSIZ];
543 unsigned long ldummy;
544 unsigned char *p = NULL;
546 XGetWindowProperty(dpy, c->xid,
547 atoms[a], 0L, BUFSIZ, False, XA_STRING,
548 &adummy, &idummy, &ldummy, &ldummy, &p);
550 strncpy(buf, (char *)p, LENGTH(buf)-1);
563 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
569 getstyle(const char *uri)
573 if (stylefile != NULL)
576 for (i = 0; i < LENGTH(styles); i++) {
577 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
579 return styles[i].style;
586 setstyle(Client *c, const char *style)
588 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
590 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
594 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
598 webkit_web_view_stop_loading(w);
599 arg = (Arg)PLUMB((char *)uri);
604 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
609 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
615 inspector(Client *c, const Arg *arg)
617 if (enableinspector) {
619 webkit_web_inspector_close(c->inspector);
621 webkit_web_inspector_show(c->inspector);
626 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
628 return WEBKIT_WEB_VIEW(webkit_web_view_new());
632 inspector_show(WebKitWebInspector *i, Client *c)
639 w = webkit_web_inspector_get_web_view(i);
640 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
641 gtk_widget_show(GTK_WIDGET(w));
642 c->isinspecting = true;
648 inspector_close(WebKitWebInspector *i, Client *c)
652 if (!c->isinspecting)
655 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
657 gtk_widget_destroy(w);
658 c->isinspecting = false;
664 inspector_finished(WebKitWebInspector *i, Client *c)
666 g_free(c->inspector);
670 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
674 gboolean processed = FALSE;
676 mods = CLEANMASK(mods);
677 key = gdk_keyval_to_lower(key);
679 for (i = 0; i < LENGTH(keys); i++) {
680 if (key == keys[i].keyval
681 && mods == keys[i].mod
683 keys[i].func(c, &(keys[i].arg));
692 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
695 c->linkhover = copystr(&c->linkhover, l);
696 } else if (c->linkhover) {
704 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
706 WebKitWebFrame *frame;
707 WebKitWebDataSource *src;
708 WebKitNetworkRequest *request;
712 switch (webkit_web_view_get_load_status (c->view)) {
713 case WEBKIT_LOAD_COMMITTED:
715 if (strstr(uri, "https://") == uri) {
716 frame = webkit_web_view_get_main_frame(c->view);
717 src = webkit_web_frame_get_data_source(frame);
718 request = webkit_web_data_source_get_request(src);
719 msg = webkit_network_request_get_message(request);
720 c->sslfailed = !(soup_message_get_flags(msg)
721 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
723 setatom(c, AtomUri, uri);
726 setstyle(c, getstyle(uri));
728 case WEBKIT_LOAD_FINISHED:
738 loaduri(Client *c, const Arg *arg)
741 const char *uri = (char *)arg->v;
742 Arg a = { .b = FALSE };
745 if (strcmp(uri, "") == 0)
748 /* In case it's a file path. */
749 if (stat(uri, &st) == 0) {
750 rp = realpath(uri, NULL);
751 u = g_strdup_printf("file://%s", rp);
754 u = g_strrstr(uri, "://") ? g_strdup(uri)
755 : g_strdup_printf("http://%s", uri);
758 setatom(c, AtomUri, uri);
760 /* prevents endless loop */
761 if (strcmp(u, geturi(c)) == 0) {
764 webkit_web_view_load_uri(c->view, u);
766 c->title = copystr(&c->title, u);
773 navigate(Client *c, const Arg *arg)
775 int steps = *(int *)arg;
776 webkit_web_view_go_back_or_forward(c->view, steps);
780 newclient(Client *rc)
785 if (!(c = calloc(1, sizeof(Client))))
786 die("Cannot malloc!\n");
794 c->view = newview(c, rc ? rc->view : NULL);
800 newview(Client *c, WebKitWebView *rv)
803 WebKitSettings *settings;
804 WebKitUserContentManager *contentmanager;
805 WebKitWebContext *context;
811 webkit_web_view_new_with_related_view(rv));
813 settings = webkit_settings_new_with_settings(
814 "auto-load-images", loadimages,
815 "default-font-size", defaultfontsize,
816 "enable-caret-browsing", enablecaretbrowsing,
817 "enable-developer-extras", enableinspector,
818 "enable-dns-prefetching", enablednsprefetching,
819 "enable-frame-flattening", enableframeflattening,
820 "enable-html5-database", enablecache,
821 "enable-html5-local-storage", enablecache,
822 "enable-javascript", enablescripts,
823 "enable-plugins", enableplugins,
825 if (!(ua = getenv("SURF_USERAGENT")))
827 webkit_settings_set_user_agent(settings, ua);
828 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
829 * for more interesting settings */
831 contentmanager = webkit_user_content_manager_new();
833 context = webkit_web_context_new_with_website_data_manager(
834 webkit_website_data_manager_new(
835 "base-cache-directory", cachedir,
836 "base-data-directory", cachedir,
839 /* rendering process model, can be a shared unique one or one for each
841 webkit_web_context_set_process_model(context,
842 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
844 webkit_web_context_set_tls_errors_policy(context, strictssl ?
845 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
847 webkit_web_context_set_cache_model(context, enablecache ?
848 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
850 /* Currently only works with text file to be compatible with curl */
851 webkit_cookie_manager_set_persistent_storage(
852 webkit_web_context_get_cookie_manager(context), cookiefile,
853 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
855 webkit_cookie_manager_set_accept_policy(
856 webkit_web_context_get_cookie_manager(context),
859 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
860 "settings", settings,
861 "user-content-manager", contentmanager,
862 "web-context", context,
866 g_signal_connect(G_OBJECT(v),
868 G_CALLBACK(titlechanged), c);
869 g_signal_connect(G_OBJECT(v),
870 "hovering-over-link",
871 G_CALLBACK(linkhover), c);
872 g_signal_connect(G_OBJECT(v),
873 "geolocation-policy-decision-requested",
874 G_CALLBACK(geopolicyrequested), c);
875 g_signal_connect(G_OBJECT(v),
877 G_CALLBACK(createwindow), c);
878 g_signal_connect(G_OBJECT(v), "ready-to-show",
879 G_CALLBACK(showview), c);
880 g_signal_connect(G_OBJECT(v),
881 "new-window-policy-decision-requested",
882 G_CALLBACK(decidewindow), c);
883 g_signal_connect(G_OBJECT(v),
884 "mime-type-policy-decision-requested",
885 G_CALLBACK(decidedownload), c);
886 g_signal_connect(G_OBJECT(v),
887 "window-object-cleared",
888 G_CALLBACK(windowobjectcleared), c);
889 g_signal_connect(G_OBJECT(v),
890 "notify::load-status",
891 G_CALLBACK(loadstatuschange), c);
892 g_signal_connect(G_OBJECT(v),
894 G_CALLBACK(progresschange), c);
895 g_signal_connect(G_OBJECT(v),
896 "download-requested",
897 G_CALLBACK(initdownload), c);
898 g_signal_connect(G_OBJECT(v),
899 "button-release-event",
900 G_CALLBACK(buttonrelease), c);
901 g_signal_connect(G_OBJECT(v),
903 G_CALLBACK(contextmenu), c);
904 g_signal_connect(G_OBJECT(v),
905 "resource-request-starting",
906 G_CALLBACK(beforerequest), c);
907 g_signal_connect(G_OBJECT(v),
908 "should-show-delete-interface-for-element",
909 G_CALLBACK(deletion_interface), c);
915 showview(WebKitWebView *v, Client *c)
917 GdkGeometry hints = { 1, 1 };
918 GdkRGBA bgcolor = { 0 };
923 c->win = gtk_plug_new(embed);
925 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
927 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
928 * is always correct, so we should still call this function.
929 * But when doing so, we *must* differentiate between a
930 * WM_CLASS and a resource on the window. By convention, the
931 * window class (WM_CLASS) is capped, while the resource is in
932 * lowercase. Both these values come as a pair.
934 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
936 /* TA: 20091214: And set the role here as well -- so that
937 * sessions can pick this up.
939 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
941 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
942 g_signal_connect(G_OBJECT(c->win),
944 G_CALLBACK(destroywin), c);
945 g_signal_connect(G_OBJECT(c->win),
946 "leave_notify_event",
947 G_CALLBACK(titlechangeleave), c);
953 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
956 gtk_widget_grab_focus(GTK_WIDGET(c->view));
957 gtk_widget_show(GTK_WIDGET(c->view));
958 gtk_widget_show(c->win);
959 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
960 c->xid = gdk_x11_window_get_xid(gwin);
961 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
963 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
964 gdk_window_add_filter(gwin, processx, c);
968 /* This might conflict with _zoomto96dpi_. */
969 if (zoomlevel != 1.0)
970 webkit_web_view_set_zoom_level(c->view, zoomlevel);
975 setatom(c, AtomFind, "");
976 setatom(c, AtomUri, "about:blank");
978 webkit_web_view_set_background_color(c->view, &bgcolor);
981 gdk_display_sync(gtk_widget_get_display(c->win));
982 printf("%lu\n", c->xid);
984 if (fclose(stdout) != 0) {
985 die("Error closing stdout");
991 newwindow(Client *c, const Arg *arg, gboolean noembed)
994 const char *cmd[18], *uri;
995 const Arg a = { .v = (void *)cmd };
1000 cmd[i++] = cookiepolicies;
1001 if (!enablescrollbars)
1003 if (embed && !noembed) {
1005 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1008 if (!allowgeolocation)
1020 if (enablediskcache)
1023 cmd[i++] = cookiefile;
1025 uri = arg->v ? (char *)arg->v : c->linkhover;
1033 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1034 gboolean keyboard, Client *c)
1036 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1038 for (GList *l = items; l; l = l->next)
1039 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1046 menuactivate(GtkMenuItem *item, Client *c)
1049 * context-menu-action-2000 open link
1050 * context-menu-action-1 open link in window
1051 * context-menu-action-2 download linked file
1052 * context-menu-action-3 copy link location
1053 * context-menu-action-7 copy image address
1054 * context-menu-action-13 reload
1055 * context-menu-action-10 back
1056 * context-menu-action-11 forward
1057 * context-menu-action-12 stop
1060 const gchar *name, *uri;
1061 GtkClipboard *prisel, *clpbrd;
1063 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1067 if (!g_strcmp0(name, "context-menu-action-3")) {
1068 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1069 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1070 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1071 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1072 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1073 uri = gtk_clipboard_wait_for_text(clpbrd);
1075 gtk_clipboard_set_text(prisel, uri, -1);
1080 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1082 Arg arg = {.v = text };
1084 loaduri((Client *) d, &arg);
1088 print(Client *c, const Arg *arg)
1090 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1094 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1096 Client *c = (Client *)d;
1100 if (((XEvent *)e)->type == PropertyNotify) {
1101 ev = &((XEvent *)e)->xproperty;
1102 if (ev->state == PropertyNewValue) {
1103 if (ev->atom == atoms[AtomFind]) {
1107 return GDK_FILTER_REMOVE;
1108 } else if (ev->atom == atoms[AtomGo]) {
1109 arg.v = getatom(c, AtomGo);
1112 return GDK_FILTER_REMOVE;
1116 return GDK_FILTER_CONTINUE;
1120 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1122 c->progress = webkit_web_view_get_progress(c->view) * 100;
1127 linkopen(Client *c, const Arg *arg)
1129 newwindow(NULL, arg, 1);
1133 linkopenembed(Client *c, const Arg *arg)
1135 newwindow(NULL, arg, 0);
1139 reload(Client *c, const Arg *arg)
1141 gboolean nocache = *(gboolean *)arg;
1143 webkit_web_view_reload_bypass_cache(c->view);
1145 webkit_web_view_reload(c->view);
1149 scroll_h(Client *c, const Arg *arg)
1151 scroll(gtk_scrolled_window_get_hadjustment(
1152 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1156 scroll_v(Client *c, const Arg *arg)
1158 scroll(gtk_scrolled_window_get_vadjustment(
1159 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1163 scroll(GtkAdjustment *a, const Arg *arg)
1167 v = gtk_adjustment_get_value(a);
1171 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1176 v += gtk_adjustment_get_step_increment(a) * arg->i;
1180 v = MIN(v, gtk_adjustment_get_upper(a) -
1181 gtk_adjustment_get_page_size(a));
1182 gtk_adjustment_set_value(a, v);
1186 setatom(Client *c, int a, const char *v)
1189 XChangeProperty(dpy, c->xid,
1190 atoms[a], XA_STRING, 8, PropModeReplace,
1191 (unsigned char *)v, strlen(v) + 1);
1198 char *styledirfile, *stylepath;
1199 WebKitWebContext *context;
1200 GError *error = NULL;
1202 /* clean up any zombies immediately */
1204 gtk_init(NULL, NULL);
1206 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1209 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1210 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1211 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1213 /* dirs and files */
1214 cookiefile = buildfile(cookiefile);
1215 scriptfile = buildfile(scriptfile);
1216 cachedir = buildpath(cachedir);
1217 if (stylefile == NULL) {
1218 styledir = buildpath(styledir);
1219 for (i = 0; i < LENGTH(styles); i++) {
1220 if (regcomp(&(styles[i].re), styles[i].regex,
1223 "Could not compile regex: %s\n",
1225 styles[i].regex = NULL;
1227 styledirfile = g_strconcat(styledir, "/",
1228 styles[i].style, NULL);
1229 stylepath = buildfile(styledirfile);
1230 styles[i].style = g_strconcat("file://", stylepath,
1232 g_free(styledirfile);
1237 stylepath = buildfile(stylefile);
1238 stylefile = g_strconcat("file://", stylepath, NULL);
1246 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1247 die("Can't install SIGCHLD handler");
1248 while (0 < waitpid(-1, NULL, WNOHANG));
1252 spawn(Client *c, const Arg *arg)
1256 close(ConnectionNumber(dpy));
1258 execvp(((char **)arg->v)[0], (char **)arg->v);
1259 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1266 eval(Client *c, const Arg *arg)
1268 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1269 evalscript(webkit_web_frame_get_global_context(frame),
1270 ((char **)arg->v)[0], "");
1274 stop(Client *c, const Arg *arg)
1276 webkit_web_view_stop_loading(c->view);
1280 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1282 c->title = webkit_web_view_get_title(c->view);
1287 titlechangeleave(void *a, void *b, Client *c)
1289 c->linkhover = NULL;
1294 toggle(Client *c, const Arg *arg)
1296 WebKitWebSettings *settings;
1297 char *name = (char *)arg->v;
1299 Arg a = { .b = FALSE };
1301 settings = webkit_web_view_get_settings(c->view);
1302 g_object_get(G_OBJECT(settings), name, &value, NULL);
1303 g_object_set(G_OBJECT(settings), name, !value, NULL);
1309 togglecookiepolicy(Client *c, const Arg *arg)
1312 cookiepolicy %= strlen(cookiepolicies);
1314 webkit_cookie_manager_set_accept_policy(
1315 webkit_web_context_get_cookie_manager(
1316 webkit_web_view_get_context(c->view)),
1317 cookiepolicy_get());
1320 /* Do not reload. */
1324 togglegeolocation(Client *c, const Arg *arg)
1326 Arg a = { .b = FALSE };
1328 allowgeolocation ^= 1;
1333 twitch(Client *c, const Arg *arg)
1338 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1341 v = gtk_adjustment_get_value(a);
1346 v = MIN(v, gtk_adjustment_get_upper(a) -
1347 gtk_adjustment_get_page_size(a));
1348 gtk_adjustment_set_value(a, v);
1352 togglescrollbars(Client *c, const Arg *arg)
1354 GtkPolicyType vspolicy;
1357 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1360 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1361 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1365 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1366 GTK_POLICY_AUTOMATIC,
1367 GTK_POLICY_AUTOMATIC);
1376 togglestyle(Client *c, const Arg *arg)
1378 enablestyle = !enablestyle;
1379 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1385 gettogglestat(Client *c)
1389 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1391 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1393 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1395 togglestat[p++] = value? 'C': 'c';
1397 togglestat[p++] = allowgeolocation? 'G': 'g';
1399 togglestat[p++] = enablediskcache? 'D': 'd';
1401 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1402 togglestat[p++] = value? 'I': 'i';
1404 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1405 togglestat[p++] = value? 'S': 's';
1407 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1408 togglestat[p++] = value? 'V': 'v';
1410 togglestat[p++] = enablestyle ? 'M': 'm';
1412 togglestat[p] = '\0';
1416 getpagestat(Client *c)
1418 const char *uri = geturi(c);
1420 if (strstr(uri, "https://") == uri)
1421 pagestat[0] = c->sslfailed ? 'U' : 'T';
1429 updatetitle(Client *c)
1433 if (showindicators) {
1438 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1440 } else if (c->progress != 100) {
1441 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1442 togglestat, pagestat,
1443 c->title == NULL ? "" : c->title);
1445 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1446 c->title == NULL ? "" : c->title);
1449 gtk_window_set_title(GTK_WINDOW(c->win), t);
1452 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1458 updatewinid(Client *c)
1460 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1466 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1467 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1468 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1472 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1473 JSObjectRef win, Client *c)
1479 zoom(Client *c, const Arg *arg)
1484 webkit_web_view_zoom_out(c->view);
1485 } else if (arg->i > 0) {
1487 webkit_web_view_zoom_in(c->view);
1491 webkit_web_view_set_zoom_level(c->view, 1.0);
1496 main(int argc, char *argv[])
1501 memset(&arg, 0, sizeof(arg));
1503 /* command line args */
1506 cookiepolicies = EARGF(usage());
1509 enablescrollbars = 0;
1512 enablescrollbars = 1;
1515 cookiefile = EARGF(usage());
1518 enablediskcache = 0;
1521 enablediskcache = 1;
1524 embed = strtol(EARGF(usage()), NULL, 0);
1527 runinfullscreen = 0;
1530 runinfullscreen = 1;
1533 allowgeolocation = 0;
1536 allowgeolocation = 1;
1557 enableinspector = 0;
1560 enableinspector = 1;
1569 scriptfile = EARGF(usage());
1578 stylefile = EARGF(usage());
1581 useragent = EARGF(usage());
1584 die("surf-"VERSION", ©2009-2015 surf engineers, "
1585 "see LICENSE for details\n");
1590 zoomlevel = strtof(EARGF(usage()), NULL);
1599 c = newclient(NULL);
1602 loaduri(clients, &arg);
1609 return EXIT_SUCCESS;