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 WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
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 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
433 Client *n = newclient();
438 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
439 gchar *m, WebKitWebPolicyDecision *p, Client *c)
441 if (!webkit_web_view_can_show_mime_type(v, m)) {
442 webkit_web_policy_decision_download(p);
449 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
450 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
455 if (webkit_web_navigation_action_get_reason(n)
456 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
457 webkit_web_policy_decision_ignore(p);
458 arg.v = (void *)webkit_network_request_get_uri(r);
459 newwindow(NULL, &arg, 0);
466 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
472 destroyclient(Client *c)
476 webkit_web_view_stop_loading(c->view);
477 gtk_widget_destroy(GTK_WIDGET(c->view));
478 gtk_widget_destroy(c->scroll);
479 gtk_widget_destroy(c->vbox);
480 gtk_widget_destroy(c->win);
482 for (p = clients; p && p->next != c; p = p->next)
494 destroywin(GtkWidget* w, Client *c)
500 die(const char *errstr, ...)
504 va_start(ap, errstr);
505 vfprintf(stderr, errstr, ap);
511 find(Client *c, const Arg *arg)
515 s = getatom(c, AtomFind);
516 gboolean forward = *(gboolean *)arg;
517 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
521 fullscreen(Client *c, const Arg *arg)
524 gtk_window_unfullscreen(GTK_WINDOW(c->win));
526 gtk_window_fullscreen(GTK_WINDOW(c->win));
527 c->fullscreen = !c->fullscreen;
531 permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
533 if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
534 if (allowgeolocation)
535 webkit_permission_request_allow(r);
537 webkit_permission_request_deny(r);
545 getatom(Client *c, int a)
547 static char buf[BUFSIZ];
550 unsigned long ldummy;
551 unsigned char *p = NULL;
553 XGetWindowProperty(dpy, c->xid,
554 atoms[a], 0L, BUFSIZ, False, XA_STRING,
555 &adummy, &idummy, &ldummy, &ldummy, &p);
557 strncpy(buf, (char *)p, LENGTH(buf)-1);
570 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
576 getstyle(const char *uri)
580 if (stylefile != NULL)
583 for (i = 0; i < LENGTH(styles); i++) {
584 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
586 return styles[i].style;
593 setstyle(Client *c, const char *style)
595 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
597 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
601 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
605 webkit_web_view_stop_loading(w);
606 arg = (Arg)PLUMB((char *)uri);
611 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
616 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
622 inspector(Client *c, const Arg *arg)
624 if (enableinspector) {
626 webkit_web_inspector_close(c->inspector);
628 webkit_web_inspector_show(c->inspector);
633 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
635 return WEBKIT_WEB_VIEW(webkit_web_view_new());
639 inspector_show(WebKitWebInspector *i, Client *c)
646 w = webkit_web_inspector_get_web_view(i);
647 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
648 gtk_widget_show(GTK_WIDGET(w));
649 c->isinspecting = true;
655 inspector_close(WebKitWebInspector *i, Client *c)
659 if (!c->isinspecting)
662 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
664 gtk_widget_destroy(w);
665 c->isinspecting = false;
671 inspector_finished(WebKitWebInspector *i, Client *c)
673 g_free(c->inspector);
677 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
681 gboolean processed = FALSE;
683 mods = CLEANMASK(mods);
684 key = gdk_keyval_to_lower(key);
686 for (i = 0; i < LENGTH(keys); i++) {
687 if (key == keys[i].keyval
688 && mods == keys[i].mod
690 keys[i].func(c, &(keys[i].arg));
699 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
702 WebKitHitTestResultContext hc;
704 /* Keep the hit test to know where is the pointer on the next click */
707 hc = webkit_hit_test_result_get_context(h);
710 c->targeturi = webkit_hit_test_result_get_link_uri(h);
712 c->targeturi = webkit_hit_test_result_get_image_uri(h);
713 else if (hc & OnMedia)
714 c->targeturi = webkit_hit_test_result_get_media_uri(h);
721 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
723 WebKitWebFrame *frame;
724 WebKitWebDataSource *src;
725 WebKitNetworkRequest *request;
729 switch (webkit_web_view_get_load_status (c->view)) {
730 case WEBKIT_LOAD_COMMITTED:
732 if (strstr(uri, "https://") == uri) {
733 frame = webkit_web_view_get_main_frame(c->view);
734 src = webkit_web_frame_get_data_source(frame);
735 request = webkit_web_data_source_get_request(src);
736 msg = webkit_network_request_get_message(request);
737 c->sslfailed = !(soup_message_get_flags(msg)
738 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
740 setatom(c, AtomUri, uri);
743 setstyle(c, getstyle(uri));
745 case WEBKIT_LOAD_FINISHED:
755 loaduri(Client *c, const Arg *arg)
758 const char *uri = (char *)arg->v;
759 Arg a = { .b = FALSE };
762 if (strcmp(uri, "") == 0)
765 /* In case it's a file path. */
766 if (stat(uri, &st) == 0) {
767 rp = realpath(uri, NULL);
768 u = g_strdup_printf("file://%s", rp);
771 u = g_strrstr(uri, "://") ? g_strdup(uri)
772 : g_strdup_printf("http://%s", uri);
775 setatom(c, AtomUri, uri);
777 /* prevents endless loop */
778 if (strcmp(u, geturi(c)) == 0) {
781 webkit_web_view_load_uri(c->view, u);
783 c->title = copystr(&c->title, u);
790 navigate(Client *c, const Arg *arg)
792 int steps = *(int *)arg;
793 webkit_web_view_go_back_or_forward(c->view, steps);
797 newclient(Client *rc)
802 if (!(c = calloc(1, sizeof(Client))))
803 die("Cannot malloc!\n");
811 c->view = newview(c, rc ? rc->view : NULL);
817 newview(Client *c, WebKitWebView *rv)
820 WebKitSettings *settings;
821 WebKitUserContentManager *contentmanager;
822 WebKitWebContext *context;
828 webkit_web_view_new_with_related_view(rv));
830 settings = webkit_settings_new_with_settings(
831 "auto-load-images", loadimages,
832 "default-font-size", defaultfontsize,
833 "enable-caret-browsing", enablecaretbrowsing,
834 "enable-developer-extras", enableinspector,
835 "enable-dns-prefetching", enablednsprefetching,
836 "enable-frame-flattening", enableframeflattening,
837 "enable-html5-database", enablecache,
838 "enable-html5-local-storage", enablecache,
839 "enable-javascript", enablescripts,
840 "enable-plugins", enableplugins,
842 if (!(ua = getenv("SURF_USERAGENT")))
844 webkit_settings_set_user_agent(settings, ua);
845 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
846 * for more interesting settings */
848 contentmanager = webkit_user_content_manager_new();
850 context = webkit_web_context_new_with_website_data_manager(
851 webkit_website_data_manager_new(
852 "base-cache-directory", cachedir,
853 "base-data-directory", cachedir,
856 /* rendering process model, can be a shared unique one or one for each
858 webkit_web_context_set_process_model(context,
859 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
861 webkit_web_context_set_tls_errors_policy(context, strictssl ?
862 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
864 webkit_web_context_set_cache_model(context, enablecache ?
865 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
867 /* Currently only works with text file to be compatible with curl */
868 webkit_cookie_manager_set_persistent_storage(
869 webkit_web_context_get_cookie_manager(context), cookiefile,
870 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
872 webkit_cookie_manager_set_accept_policy(
873 webkit_web_context_get_cookie_manager(context),
876 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
877 "settings", settings,
878 "user-content-manager", contentmanager,
879 "web-context", context,
883 g_signal_connect(G_OBJECT(v),
885 G_CALLBACK(titlechanged), c);
886 g_signal_connect(G_OBJECT(v),
887 "mouse-target-changed",
888 G_CALLBACK(mousetargetchanged), c);
889 g_signal_connect(G_OBJECT(v),
890 "permission-request",
891 G_CALLBACK(permissionrequested), c);
892 g_signal_connect(G_OBJECT(v),
894 G_CALLBACK(createwindow), c);
895 g_signal_connect(G_OBJECT(v), "ready-to-show",
896 G_CALLBACK(showview), c);
897 g_signal_connect(G_OBJECT(v),
898 "new-window-policy-decision-requested",
899 G_CALLBACK(decidewindow), c);
900 g_signal_connect(G_OBJECT(v),
901 "mime-type-policy-decision-requested",
902 G_CALLBACK(decidedownload), c);
903 g_signal_connect(G_OBJECT(v),
904 "window-object-cleared",
905 G_CALLBACK(windowobjectcleared), c);
906 g_signal_connect(G_OBJECT(v),
907 "notify::load-status",
908 G_CALLBACK(loadstatuschange), c);
909 g_signal_connect(G_OBJECT(v),
911 G_CALLBACK(progresschange), c);
912 g_signal_connect(G_OBJECT(v),
913 "download-requested",
914 G_CALLBACK(initdownload), c);
915 g_signal_connect(G_OBJECT(v),
916 "button-release-event",
917 G_CALLBACK(buttonrelease), c);
918 g_signal_connect(G_OBJECT(v),
920 G_CALLBACK(contextmenu), c);
921 g_signal_connect(G_OBJECT(v),
922 "resource-request-starting",
923 G_CALLBACK(beforerequest), c);
924 g_signal_connect(G_OBJECT(v),
925 "should-show-delete-interface-for-element",
926 G_CALLBACK(deletion_interface), c);
932 showview(WebKitWebView *v, Client *c)
934 GdkGeometry hints = { 1, 1 };
935 GdkRGBA bgcolor = { 0 };
940 c->win = gtk_plug_new(embed);
942 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
944 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
945 * is always correct, so we should still call this function.
946 * But when doing so, we *must* differentiate between a
947 * WM_CLASS and a resource on the window. By convention, the
948 * window class (WM_CLASS) is capped, while the resource is in
949 * lowercase. Both these values come as a pair.
951 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
953 /* TA: 20091214: And set the role here as well -- so that
954 * sessions can pick this up.
956 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
958 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
959 g_signal_connect(G_OBJECT(c->win),
961 G_CALLBACK(destroywin), c);
962 g_signal_connect(G_OBJECT(c->win),
963 "leave_notify_event",
964 G_CALLBACK(titlechangeleave), c);
970 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
973 gtk_widget_grab_focus(GTK_WIDGET(c->view));
974 gtk_widget_show(GTK_WIDGET(c->view));
975 gtk_widget_show(c->win);
976 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
977 c->xid = gdk_x11_window_get_xid(gwin);
978 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
980 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
981 gdk_window_add_filter(gwin, processx, c);
985 /* This might conflict with _zoomto96dpi_. */
986 if (zoomlevel != 1.0)
987 webkit_web_view_set_zoom_level(c->view, zoomlevel);
992 setatom(c, AtomFind, "");
993 setatom(c, AtomUri, "about:blank");
995 webkit_web_view_set_background_color(c->view, &bgcolor);
998 gdk_display_sync(gtk_widget_get_display(c->win));
999 printf("%lu\n", c->xid);
1001 if (fclose(stdout) != 0) {
1002 die("Error closing stdout");
1008 newwindow(Client *c, const Arg *arg, gboolean noembed)
1011 const char *cmd[18], *uri;
1012 const Arg a = { .v = (void *)cmd };
1017 cmd[i++] = cookiepolicies;
1018 if (!enablescrollbars)
1020 if (embed && !noembed) {
1022 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1025 if (!allowgeolocation)
1037 if (enablediskcache)
1040 cmd[i++] = cookiefile;
1042 uri = arg->v ? (char *)arg->v : c->linkhover;
1050 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1051 gboolean keyboard, Client *c)
1053 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1055 for (GList *l = items; l; l = l->next)
1056 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1063 menuactivate(GtkMenuItem *item, Client *c)
1066 * context-menu-action-2000 open link
1067 * context-menu-action-1 open link in window
1068 * context-menu-action-2 download linked file
1069 * context-menu-action-3 copy link location
1070 * context-menu-action-7 copy image address
1071 * context-menu-action-13 reload
1072 * context-menu-action-10 back
1073 * context-menu-action-11 forward
1074 * context-menu-action-12 stop
1077 const gchar *name, *uri;
1078 GtkClipboard *prisel, *clpbrd;
1080 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1084 if (!g_strcmp0(name, "context-menu-action-3")) {
1085 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1086 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1087 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1088 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1089 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1090 uri = gtk_clipboard_wait_for_text(clpbrd);
1092 gtk_clipboard_set_text(prisel, uri, -1);
1097 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1099 Arg arg = {.v = text };
1101 loaduri((Client *) d, &arg);
1105 print(Client *c, const Arg *arg)
1107 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1111 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1113 Client *c = (Client *)d;
1117 if (((XEvent *)e)->type == PropertyNotify) {
1118 ev = &((XEvent *)e)->xproperty;
1119 if (ev->state == PropertyNewValue) {
1120 if (ev->atom == atoms[AtomFind]) {
1124 return GDK_FILTER_REMOVE;
1125 } else if (ev->atom == atoms[AtomGo]) {
1126 arg.v = getatom(c, AtomGo);
1129 return GDK_FILTER_REMOVE;
1133 return GDK_FILTER_CONTINUE;
1137 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1139 c->progress = webkit_web_view_get_progress(c->view) * 100;
1144 linkopen(Client *c, const Arg *arg)
1146 newwindow(NULL, arg, 1);
1150 linkopenembed(Client *c, const Arg *arg)
1152 newwindow(NULL, arg, 0);
1156 reload(Client *c, const Arg *arg)
1158 gboolean nocache = *(gboolean *)arg;
1160 webkit_web_view_reload_bypass_cache(c->view);
1162 webkit_web_view_reload(c->view);
1166 scroll_h(Client *c, const Arg *arg)
1168 scroll(gtk_scrolled_window_get_hadjustment(
1169 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1173 scroll_v(Client *c, const Arg *arg)
1175 scroll(gtk_scrolled_window_get_vadjustment(
1176 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1180 scroll(GtkAdjustment *a, const Arg *arg)
1184 v = gtk_adjustment_get_value(a);
1188 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1193 v += gtk_adjustment_get_step_increment(a) * arg->i;
1197 v = MIN(v, gtk_adjustment_get_upper(a) -
1198 gtk_adjustment_get_page_size(a));
1199 gtk_adjustment_set_value(a, v);
1203 setatom(Client *c, int a, const char *v)
1206 XChangeProperty(dpy, c->xid,
1207 atoms[a], XA_STRING, 8, PropModeReplace,
1208 (unsigned char *)v, strlen(v) + 1);
1215 char *styledirfile, *stylepath;
1216 WebKitWebContext *context;
1217 GError *error = NULL;
1219 /* clean up any zombies immediately */
1221 gtk_init(NULL, NULL);
1223 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1226 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1227 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1228 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1230 /* dirs and files */
1231 cookiefile = buildfile(cookiefile);
1232 scriptfile = buildfile(scriptfile);
1233 cachedir = buildpath(cachedir);
1234 if (stylefile == NULL) {
1235 styledir = buildpath(styledir);
1236 for (i = 0; i < LENGTH(styles); i++) {
1237 if (regcomp(&(styles[i].re), styles[i].regex,
1240 "Could not compile regex: %s\n",
1242 styles[i].regex = NULL;
1244 styledirfile = g_strconcat(styledir, "/",
1245 styles[i].style, NULL);
1246 stylepath = buildfile(styledirfile);
1247 styles[i].style = g_strconcat("file://", stylepath,
1249 g_free(styledirfile);
1254 stylepath = buildfile(stylefile);
1255 stylefile = g_strconcat("file://", stylepath, NULL);
1263 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1264 die("Can't install SIGCHLD handler");
1265 while (0 < waitpid(-1, NULL, WNOHANG));
1269 spawn(Client *c, const Arg *arg)
1273 close(ConnectionNumber(dpy));
1275 execvp(((char **)arg->v)[0], (char **)arg->v);
1276 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1283 eval(Client *c, const Arg *arg)
1285 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1286 evalscript(webkit_web_frame_get_global_context(frame),
1287 ((char **)arg->v)[0], "");
1291 stop(Client *c, const Arg *arg)
1293 webkit_web_view_stop_loading(c->view);
1297 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1299 c->title = webkit_web_view_get_title(c->view);
1304 titlechangeleave(void *a, void *b, Client *c)
1306 c->linkhover = NULL;
1311 toggle(Client *c, const Arg *arg)
1313 WebKitWebSettings *settings;
1314 char *name = (char *)arg->v;
1316 Arg a = { .b = FALSE };
1318 settings = webkit_web_view_get_settings(c->view);
1319 g_object_get(G_OBJECT(settings), name, &value, NULL);
1320 g_object_set(G_OBJECT(settings), name, !value, NULL);
1326 togglecookiepolicy(Client *c, const Arg *arg)
1329 cookiepolicy %= strlen(cookiepolicies);
1331 webkit_cookie_manager_set_accept_policy(
1332 webkit_web_context_get_cookie_manager(
1333 webkit_web_view_get_context(c->view)),
1334 cookiepolicy_get());
1337 /* Do not reload. */
1341 togglegeolocation(Client *c, const Arg *arg)
1343 Arg a = { .b = FALSE };
1345 allowgeolocation ^= 1;
1350 twitch(Client *c, const Arg *arg)
1355 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1358 v = gtk_adjustment_get_value(a);
1363 v = MIN(v, gtk_adjustment_get_upper(a) -
1364 gtk_adjustment_get_page_size(a));
1365 gtk_adjustment_set_value(a, v);
1369 togglescrollbars(Client *c, const Arg *arg)
1371 GtkPolicyType vspolicy;
1374 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1377 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1378 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1382 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1383 GTK_POLICY_AUTOMATIC,
1384 GTK_POLICY_AUTOMATIC);
1393 togglestyle(Client *c, const Arg *arg)
1395 enablestyle = !enablestyle;
1396 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1402 gettogglestat(Client *c)
1406 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1408 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1410 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1412 togglestat[p++] = value? 'C': 'c';
1414 togglestat[p++] = allowgeolocation? 'G': 'g';
1416 togglestat[p++] = enablediskcache? 'D': 'd';
1418 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1419 togglestat[p++] = value? 'I': 'i';
1421 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1422 togglestat[p++] = value? 'S': 's';
1424 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1425 togglestat[p++] = value? 'V': 'v';
1427 togglestat[p++] = enablestyle ? 'M': 'm';
1429 togglestat[p] = '\0';
1433 getpagestat(Client *c)
1435 const char *uri = geturi(c);
1437 if (strstr(uri, "https://") == uri)
1438 pagestat[0] = c->sslfailed ? 'U' : 'T';
1446 updatetitle(Client *c)
1450 if (showindicators) {
1455 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1457 } else if (c->progress != 100) {
1458 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1459 togglestat, pagestat,
1460 c->title == NULL ? "" : c->title);
1462 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1463 c->title == NULL ? "" : c->title);
1466 gtk_window_set_title(GTK_WINDOW(c->win), t);
1469 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1475 updatewinid(Client *c)
1477 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1483 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1484 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1485 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1489 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1490 JSObjectRef win, Client *c)
1496 zoom(Client *c, const Arg *arg)
1501 webkit_web_view_zoom_out(c->view);
1502 } else if (arg->i > 0) {
1504 webkit_web_view_zoom_in(c->view);
1508 webkit_web_view_set_zoom_level(c->view, 1.0);
1513 main(int argc, char *argv[])
1518 memset(&arg, 0, sizeof(arg));
1520 /* command line args */
1523 cookiepolicies = EARGF(usage());
1526 enablescrollbars = 0;
1529 enablescrollbars = 1;
1532 cookiefile = EARGF(usage());
1535 enablediskcache = 0;
1538 enablediskcache = 1;
1541 embed = strtol(EARGF(usage()), NULL, 0);
1544 runinfullscreen = 0;
1547 runinfullscreen = 1;
1550 allowgeolocation = 0;
1553 allowgeolocation = 1;
1574 enableinspector = 0;
1577 enableinspector = 1;
1586 scriptfile = EARGF(usage());
1595 stylefile = EARGF(usage());
1598 useragent = EARGF(usage());
1601 die("surf-"VERSION", ©2009-2015 surf engineers, "
1602 "see LICENSE for details\n");
1607 zoomlevel = strtof(EARGF(usage()), NULL);
1616 c = newclient(NULL);
1619 loaduri(clients, &arg);
1626 return EXIT_SUCCESS;