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 const char *title, *linkhover;
66 gboolean zoomed, fullscreen, isinspecting, sslfailed;
72 void (*func)(Client *c, const Arg *arg);
80 void (*func)(Client *c, const Arg *arg);
91 static Atom atoms[AtomLast];
92 static Client *clients = NULL;
93 static Window embed = 0;
94 static gboolean showxid = FALSE;
95 static char winid[64];
96 static char togglestat[9];
97 static char pagestat[3];
98 static GTlsDatabase *tlsdb;
99 static int cookiepolicy;
100 static char *stylefile = NULL;
102 static void addaccelgroup(Client *c);
103 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
104 WebKitWebResource *r, WebKitNetworkRequest *req,
105 WebKitNetworkResponse *resp, Client *c);
106 static char *buildfile(const char *path);
107 static char *buildpath(const char *path);
108 static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
109 static void cleanup(void);
110 static void clipboard(Client *c, const Arg *arg);
112 static WebKitCookieAcceptPolicy cookiepolicy_get(void);
113 static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
115 static char *copystr(char **str, const char *src);
116 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
118 static gboolean decidedownload(WebKitWebView *v, WebKitWebFrame *f,
119 WebKitNetworkRequest *r, gchar *m,
120 WebKitWebPolicyDecision *p, Client *c);
121 static gboolean decidewindow(WebKitWebView *v, WebKitWebFrame *f,
122 WebKitNetworkRequest *r, WebKitWebNavigationAction
123 *n, WebKitWebPolicyDecision *p, Client *c);
124 static gboolean deletion_interface(WebKitWebView *view,
125 WebKitDOMHTMLElement *arg1, Client *c);
126 static void destroyclient(Client *c);
127 static void destroywin(GtkWidget* w, Client *c);
128 static void die(const char *errstr, ...);
129 static void eval(Client *c, const Arg *arg);
130 static void find(Client *c, const Arg *arg);
131 static void fullscreen(Client *c, const Arg *arg);
132 static void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
133 WebKitGeolocationPolicyDecision *d, Client *c);
134 static const char *getatom(Client *c, int a);
135 static void gettogglestat(Client *c);
136 static void getpagestat(Client *c);
137 static char *geturi(Client *c);
138 static const gchar *getstyle(const char *uri);
139 static void setstyle(Client *c, const char *style);
141 static void handleplumb(Client *c, WebKitWebView *w, const gchar *uri);
143 static gboolean initdownload(WebKitWebView *v, WebKitDownload *o, Client *c);
145 static void inspector(Client *c, const Arg *arg);
146 static WebKitWebView *inspector_new(WebKitWebInspector *i, WebKitWebView *v,
148 static gboolean inspector_show(WebKitWebInspector *i, Client *c);
149 static gboolean inspector_close(WebKitWebInspector *i, Client *c);
150 static void inspector_finished(WebKitWebInspector *i, Client *c);
152 static gboolean keypress(GtkAccelGroup *group, GObject *obj, guint key,
153 GdkModifierType mods, Client *c);
154 static void linkhover(WebKitWebView *v, const char* t, const char* l,
156 static void loadstatuschange(WebKitWebView *view, GParamSpec *pspec,
158 static void loaduri(Client *c, const Arg *arg);
159 static void navigate(Client *c, const Arg *arg);
160 static Client *newclient(Client *c);
161 static WebKitWebView *newview(Client *c, WebKitWebView *rv);
162 static void showview(WebKitWebView *v, Client *c);
163 static void newwindow(Client *c, const Arg *arg, gboolean noembed);
164 static void pasteuri(GtkClipboard *clipboard, const char *text, gpointer d);
165 static gboolean contextmenu(WebKitWebView *view, GtkWidget *menu,
166 WebKitHitTestResult *target, gboolean keyboard,
168 static void menuactivate(GtkMenuItem *item, Client *c);
169 static void print(Client *c, const Arg *arg);
170 static GdkFilterReturn processx(GdkXEvent *xevent, GdkEvent *event,
172 static void progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c);
173 static void linkopen(Client *c, const Arg *arg);
174 static void linkopenembed(Client *c, const Arg *arg);
175 static void reload(Client *c, const Arg *arg);
176 static void scroll_h(Client *c, const Arg *arg);
177 static void scroll_v(Client *c, const Arg *arg);
178 static void scroll(GtkAdjustment *a, const Arg *arg);
179 static void setatom(Client *c, int a, const char *v);
180 static void setup(void);
181 static void sigchld(int unused);
182 static void spawn(Client *c, const Arg *arg);
183 static void stop(Client *c, const Arg *arg);
184 static void titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c);
185 static void titlechangeleave(void *a, void *b, Client *c);
186 static void toggle(Client *c, const Arg *arg);
187 static void togglecookiepolicy(Client *c, const Arg *arg);
188 static void togglegeolocation(Client *c, const Arg *arg);
189 static void togglescrollbars(Client *c, const Arg *arg);
190 static void togglestyle(Client *c, const Arg *arg);
191 static void updatetitle(Client *c);
192 static void updatewinid(Client *c);
193 static void usage(void);
194 static void windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame,
195 JSContextRef js, JSObjectRef win, Client *c);
196 static void zoom(Client *c, const Arg *arg);
198 /* configuration, allows nested code to access above variables */
202 addaccelgroup(Client *c)
205 GtkAccelGroup *group = gtk_accel_group_new();
208 for (i = 0; i < LENGTH(keys); i++) {
209 closure = g_cclosure_new(G_CALLBACK(keypress), c, NULL);
210 gtk_accel_group_connect(group, keys[i].keyval, keys[i].mod, 0,
213 gtk_window_add_accel_group(GTK_WINDOW(c->win), group);
217 beforerequest(WebKitWebView *w, WebKitWebFrame *f, WebKitWebResource *r,
218 WebKitNetworkRequest *req, WebKitNetworkResponse *resp,
221 const gchar *uri = webkit_network_request_get_uri(req);
224 if (g_str_has_suffix(uri, "/favicon.ico"))
225 webkit_network_request_set_uri(req, "about:blank");
227 if (!g_str_has_prefix(uri, "http://")
228 && !g_str_has_prefix(uri, "https://")
229 && !g_str_has_prefix(uri, "about:")
230 && !g_str_has_prefix(uri, "file://")
231 && !g_str_has_prefix(uri, "data:")
232 && !g_str_has_prefix(uri, "blob:")
233 && strlen(uri) > 0) {
234 for (i = 0; i < strlen(uri); i++) {
235 if (!g_ascii_isprint(uri[i])) {
241 handleplumb(c, w, uri);
246 buildfile(const char *path)
248 char *dname, *bname, *bpath, *fpath;
251 dname = g_path_get_dirname(path);
252 bname = g_path_get_basename(path);
254 bpath = buildpath(dname);
257 fpath = g_build_filename(bpath, bname, NULL);
261 if (!(f = fopen(fpath, "a")))
262 die("Could not open file: %s\n", fpath);
264 g_chmod(fpath, 0600); /* always */
271 buildpath(const char *path)
274 char *apath, *name, *p, *fpath;
276 if (path[0] == '~') {
277 if (path[1] == '/' || path[1] == '\0') {
278 p = (char *)&path[1];
279 pw = getpwuid(getuid());
281 if ((p = strchr(path, '/')))
282 name = g_strndup(&path[1], --p - path);
284 name = g_strdup(&path[1]);
286 if (!(pw = getpwnam(name))) {
287 die("Can't get user %s home directory: %s.\n",
292 apath = g_build_filename(pw->pw_dir, p, NULL);
294 apath = g_strdup(path);
297 /* creating directory */
298 if (g_mkdir_with_parents(apath, 0700) < 0)
299 die("Could not access directory: %s\n", apath);
301 fpath = realpath(apath, NULL);
308 buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
310 WebKitHitTestResultContext context;
311 WebKitHitTestResult *result;
315 result = webkit_web_view_get_hit_test_result(web, e);
316 g_object_get(result, "context", &context, NULL);
317 g_object_get(result, "link-uri", &arg.v, NULL);
318 for (i = 0; i < LENGTH(buttons); i++) {
319 if (context & buttons[i].click
320 && e->button == buttons[i].button
321 && CLEANMASK(e->state) == CLEANMASK(buttons[i].mask)
322 && buttons[i].func) {
323 buttons[i].func(c, buttons[i].click == ClkLink
324 && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
335 destroyclient(clients);
342 WebKitCookieAcceptPolicy
343 cookiepolicy_get(void)
345 switch (cookiepolicies[cookiepolicy]) {
347 return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
349 return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
355 return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
359 cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
362 case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
364 case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
366 case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
375 evalscript(JSContextRef js, char *script, char* scriptname)
377 JSStringRef jsscript, jsscriptname;
378 JSValueRef exception = NULL;
380 jsscript = JSStringCreateWithUTF8CString(script);
381 jsscriptname = JSStringCreateWithUTF8CString(scriptname);
382 JSEvaluateScript(js, jsscript, JSContextGetGlobalObject(js),
383 jsscriptname, 0, &exception);
384 JSStringRelease(jsscript);
385 JSStringRelease(jsscriptname);
389 runscript(WebKitWebFrame *frame)
394 if (g_file_get_contents(scriptfile, &script, NULL, &error)) {
395 evalscript(webkit_web_frame_get_global_context(frame), script,
401 clipboard(Client *c, const Arg *arg)
403 gboolean paste = *(gboolean *)arg;
406 gtk_clipboard_request_text(gtk_clipboard_get(
407 GDK_SELECTION_PRIMARY),
410 gtk_clipboard_set_text(gtk_clipboard_get(
411 GDK_SELECTION_PRIMARY), c->linkhover
412 ? c->linkhover : geturi(c), -1);
417 copystr(char **str, const char *src)
430 createwindow(WebKitWebView *v, WebKitWebFrame *f, Client *c)
432 Client *n = newclient();
437 decidedownload(WebKitWebView *v, WebKitWebFrame *f, WebKitNetworkRequest *r,
438 gchar *m, WebKitWebPolicyDecision *p, Client *c)
440 if (!webkit_web_view_can_show_mime_type(v, m)) {
441 webkit_web_policy_decision_download(p);
448 decidewindow(WebKitWebView *view, WebKitWebFrame *f, WebKitNetworkRequest *r,
449 WebKitWebNavigationAction *n, WebKitWebPolicyDecision *p,
454 if (webkit_web_navigation_action_get_reason(n)
455 == WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) {
456 webkit_web_policy_decision_ignore(p);
457 arg.v = (void *)webkit_network_request_get_uri(r);
458 newwindow(NULL, &arg, 0);
465 deletion_interface(WebKitWebView *view, WebKitDOMHTMLElement *arg1, Client *c)
471 destroyclient(Client *c)
475 webkit_web_view_stop_loading(c->view);
476 gtk_widget_destroy(GTK_WIDGET(c->view));
477 gtk_widget_destroy(c->scroll);
478 gtk_widget_destroy(c->vbox);
479 gtk_widget_destroy(c->win);
481 for (p = clients; p && p->next != c; p = p->next)
493 destroywin(GtkWidget* w, Client *c)
499 die(const char *errstr, ...)
503 va_start(ap, errstr);
504 vfprintf(stderr, errstr, ap);
510 find(Client *c, const Arg *arg)
514 s = getatom(c, AtomFind);
515 gboolean forward = *(gboolean *)arg;
516 webkit_web_view_search_text(c->view, s, FALSE, forward, TRUE);
520 fullscreen(Client *c, const Arg *arg)
523 gtk_window_unfullscreen(GTK_WINDOW(c->win));
525 gtk_window_fullscreen(GTK_WINDOW(c->win));
526 c->fullscreen = !c->fullscreen;
530 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
531 WebKitGeolocationPolicyDecision *d, Client *c)
533 if (allowgeolocation)
534 webkit_geolocation_policy_allow(d);
536 webkit_geolocation_policy_deny(d);
540 getatom(Client *c, int a)
542 static char buf[BUFSIZ];
545 unsigned long ldummy;
546 unsigned char *p = NULL;
548 XGetWindowProperty(dpy, c->xid,
549 atoms[a], 0L, BUFSIZ, False, XA_STRING,
550 &adummy, &idummy, &ldummy, &ldummy, &p);
552 strncpy(buf, (char *)p, LENGTH(buf)-1);
565 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
571 getstyle(const char *uri)
575 if (stylefile != NULL)
578 for (i = 0; i < LENGTH(styles); i++) {
579 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
581 return styles[i].style;
588 setstyle(Client *c, const char *style)
590 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
592 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
596 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
600 webkit_web_view_stop_loading(w);
601 arg = (Arg)PLUMB((char *)uri);
606 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
611 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
617 inspector(Client *c, const Arg *arg)
619 if (enableinspector) {
621 webkit_web_inspector_close(c->inspector);
623 webkit_web_inspector_show(c->inspector);
628 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
630 return WEBKIT_WEB_VIEW(webkit_web_view_new());
634 inspector_show(WebKitWebInspector *i, Client *c)
641 w = webkit_web_inspector_get_web_view(i);
642 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
643 gtk_widget_show(GTK_WIDGET(w));
644 c->isinspecting = true;
650 inspector_close(WebKitWebInspector *i, Client *c)
654 if (!c->isinspecting)
657 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
659 gtk_widget_destroy(w);
660 c->isinspecting = false;
666 inspector_finished(WebKitWebInspector *i, Client *c)
668 g_free(c->inspector);
672 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
676 gboolean processed = FALSE;
678 mods = CLEANMASK(mods);
679 key = gdk_keyval_to_lower(key);
681 for (i = 0; i < LENGTH(keys); i++) {
682 if (key == keys[i].keyval
683 && mods == keys[i].mod
685 keys[i].func(c, &(keys[i].arg));
694 linkhover(WebKitWebView *v, const char* t, const char* l, Client *c)
697 c->linkhover = copystr(&c->linkhover, l);
698 } else if (c->linkhover) {
706 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
708 WebKitWebFrame *frame;
709 WebKitWebDataSource *src;
710 WebKitNetworkRequest *request;
714 switch (webkit_web_view_get_load_status (c->view)) {
715 case WEBKIT_LOAD_COMMITTED:
717 if (strstr(uri, "https://") == uri) {
718 frame = webkit_web_view_get_main_frame(c->view);
719 src = webkit_web_frame_get_data_source(frame);
720 request = webkit_web_data_source_get_request(src);
721 msg = webkit_network_request_get_message(request);
722 c->sslfailed = !(soup_message_get_flags(msg)
723 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
725 setatom(c, AtomUri, uri);
728 setstyle(c, getstyle(uri));
730 case WEBKIT_LOAD_FINISHED:
740 loaduri(Client *c, const Arg *arg)
743 const char *uri = (char *)arg->v;
744 Arg a = { .b = FALSE };
747 if (strcmp(uri, "") == 0)
750 /* In case it's a file path. */
751 if (stat(uri, &st) == 0) {
752 rp = realpath(uri, NULL);
753 u = g_strdup_printf("file://%s", rp);
756 u = g_strrstr(uri, "://") ? g_strdup(uri)
757 : g_strdup_printf("http://%s", uri);
760 setatom(c, AtomUri, uri);
762 /* prevents endless loop */
763 if (strcmp(u, geturi(c)) == 0) {
766 webkit_web_view_load_uri(c->view, u);
768 c->title = copystr(&c->title, u);
775 navigate(Client *c, const Arg *arg)
777 int steps = *(int *)arg;
778 webkit_web_view_go_back_or_forward(c->view, steps);
782 newclient(Client *rc)
787 if (!(c = calloc(1, sizeof(Client))))
788 die("Cannot malloc!\n");
796 c->view = newview(c, rc ? rc->view : NULL);
802 newview(Client *c, WebKitWebView *rv)
805 WebKitSettings *settings;
806 WebKitUserContentManager *contentmanager;
807 WebKitWebContext *context;
813 webkit_web_view_new_with_related_view(rv));
815 settings = webkit_settings_new_with_settings(
816 "auto-load-images", loadimages,
817 "default-font-size", defaultfontsize,
818 "enable-caret-browsing", enablecaretbrowsing,
819 "enable-developer-extras", enableinspector,
820 "enable-dns-prefetching", enablednsprefetching,
821 "enable-frame-flattening", enableframeflattening,
822 "enable-html5-database", enablecache,
823 "enable-html5-local-storage", enablecache,
824 "enable-javascript", enablescripts,
825 "enable-plugins", enableplugins,
827 if (!(ua = getenv("SURF_USERAGENT")))
829 webkit_settings_set_user_agent(settings, ua);
830 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
831 * for more interesting settings */
833 contentmanager = webkit_user_content_manager_new();
835 context = webkit_web_context_new_with_website_data_manager(
836 webkit_website_data_manager_new(
837 "base-cache-directory", cachedir,
838 "base-data-directory", cachedir,
841 /* rendering process model, can be a shared unique one or one for each
843 webkit_web_context_set_process_model(context,
844 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
846 webkit_web_context_set_tls_errors_policy(context, strictssl ?
847 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
849 webkit_web_context_set_cache_model(context, enablecache ?
850 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
852 /* Currently only works with text file to be compatible with curl */
853 webkit_cookie_manager_set_persistent_storage(
854 webkit_web_context_get_cookie_manager(context), cookiefile,
855 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
857 webkit_cookie_manager_set_accept_policy(
858 webkit_web_context_get_cookie_manager(context),
861 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
862 "settings", settings,
863 "user-content-manager", contentmanager,
864 "web-context", context,
868 g_signal_connect(G_OBJECT(v),
870 G_CALLBACK(titlechanged), c);
871 g_signal_connect(G_OBJECT(v),
872 "hovering-over-link",
873 G_CALLBACK(linkhover), c);
874 g_signal_connect(G_OBJECT(v),
875 "geolocation-policy-decision-requested",
876 G_CALLBACK(geopolicyrequested), c);
877 g_signal_connect(G_OBJECT(v),
879 G_CALLBACK(createwindow), c);
880 g_signal_connect(G_OBJECT(v), "ready-to-show",
881 G_CALLBACK(showview), c);
882 g_signal_connect(G_OBJECT(v),
883 "new-window-policy-decision-requested",
884 G_CALLBACK(decidewindow), c);
885 g_signal_connect(G_OBJECT(v),
886 "mime-type-policy-decision-requested",
887 G_CALLBACK(decidedownload), c);
888 g_signal_connect(G_OBJECT(v),
889 "window-object-cleared",
890 G_CALLBACK(windowobjectcleared), c);
891 g_signal_connect(G_OBJECT(v),
892 "notify::load-status",
893 G_CALLBACK(loadstatuschange), c);
894 g_signal_connect(G_OBJECT(v),
896 G_CALLBACK(progresschange), c);
897 g_signal_connect(G_OBJECT(v),
898 "download-requested",
899 G_CALLBACK(initdownload), c);
900 g_signal_connect(G_OBJECT(v),
901 "button-release-event",
902 G_CALLBACK(buttonrelease), c);
903 g_signal_connect(G_OBJECT(v),
905 G_CALLBACK(contextmenu), c);
906 g_signal_connect(G_OBJECT(v),
907 "resource-request-starting",
908 G_CALLBACK(beforerequest), c);
909 g_signal_connect(G_OBJECT(v),
910 "should-show-delete-interface-for-element",
911 G_CALLBACK(deletion_interface), c);
917 showview(WebKitWebView *v, Client *c)
919 GdkGeometry hints = { 1, 1 };
920 GdkRGBA bgcolor = { 0 };
925 c->win = gtk_plug_new(embed);
927 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
929 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
930 * is always correct, so we should still call this function.
931 * But when doing so, we *must* differentiate between a
932 * WM_CLASS and a resource on the window. By convention, the
933 * window class (WM_CLASS) is capped, while the resource is in
934 * lowercase. Both these values come as a pair.
936 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
938 /* TA: 20091214: And set the role here as well -- so that
939 * sessions can pick this up.
941 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
943 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
944 g_signal_connect(G_OBJECT(c->win),
946 G_CALLBACK(destroywin), c);
947 g_signal_connect(G_OBJECT(c->win),
948 "leave_notify_event",
949 G_CALLBACK(titlechangeleave), c);
955 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
958 gtk_widget_grab_focus(GTK_WIDGET(c->view));
959 gtk_widget_show(GTK_WIDGET(c->view));
960 gtk_widget_show(c->win);
961 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
962 c->xid = gdk_x11_window_get_xid(gwin);
963 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
965 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
966 gdk_window_add_filter(gwin, processx, c);
970 /* This might conflict with _zoomto96dpi_. */
971 if (zoomlevel != 1.0)
972 webkit_web_view_set_zoom_level(c->view, zoomlevel);
977 setatom(c, AtomFind, "");
978 setatom(c, AtomUri, "about:blank");
980 webkit_web_view_set_background_color(c->view, &bgcolor);
983 gdk_display_sync(gtk_widget_get_display(c->win));
984 printf("%lu\n", c->xid);
986 if (fclose(stdout) != 0) {
987 die("Error closing stdout");
993 newwindow(Client *c, const Arg *arg, gboolean noembed)
996 const char *cmd[18], *uri;
997 const Arg a = { .v = (void *)cmd };
1002 cmd[i++] = cookiepolicies;
1003 if (!enablescrollbars)
1005 if (embed && !noembed) {
1007 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1010 if (!allowgeolocation)
1022 if (enablediskcache)
1025 cmd[i++] = cookiefile;
1027 uri = arg->v ? (char *)arg->v : c->linkhover;
1035 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1036 gboolean keyboard, Client *c)
1038 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1040 for (GList *l = items; l; l = l->next)
1041 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1048 menuactivate(GtkMenuItem *item, Client *c)
1051 * context-menu-action-2000 open link
1052 * context-menu-action-1 open link in window
1053 * context-menu-action-2 download linked file
1054 * context-menu-action-3 copy link location
1055 * context-menu-action-7 copy image address
1056 * context-menu-action-13 reload
1057 * context-menu-action-10 back
1058 * context-menu-action-11 forward
1059 * context-menu-action-12 stop
1062 const gchar *name, *uri;
1063 GtkClipboard *prisel, *clpbrd;
1065 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1069 if (!g_strcmp0(name, "context-menu-action-3")) {
1070 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1071 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1072 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1073 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1074 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1075 uri = gtk_clipboard_wait_for_text(clpbrd);
1077 gtk_clipboard_set_text(prisel, uri, -1);
1082 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1084 Arg arg = {.v = text };
1086 loaduri((Client *) d, &arg);
1090 print(Client *c, const Arg *arg)
1092 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1096 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1098 Client *c = (Client *)d;
1102 if (((XEvent *)e)->type == PropertyNotify) {
1103 ev = &((XEvent *)e)->xproperty;
1104 if (ev->state == PropertyNewValue) {
1105 if (ev->atom == atoms[AtomFind]) {
1109 return GDK_FILTER_REMOVE;
1110 } else if (ev->atom == atoms[AtomGo]) {
1111 arg.v = getatom(c, AtomGo);
1114 return GDK_FILTER_REMOVE;
1118 return GDK_FILTER_CONTINUE;
1122 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1124 c->progress = webkit_web_view_get_progress(c->view) * 100;
1129 linkopen(Client *c, const Arg *arg)
1131 newwindow(NULL, arg, 1);
1135 linkopenembed(Client *c, const Arg *arg)
1137 newwindow(NULL, arg, 0);
1141 reload(Client *c, const Arg *arg)
1143 gboolean nocache = *(gboolean *)arg;
1145 webkit_web_view_reload_bypass_cache(c->view);
1147 webkit_web_view_reload(c->view);
1151 scroll_h(Client *c, const Arg *arg)
1153 scroll(gtk_scrolled_window_get_hadjustment(
1154 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1158 scroll_v(Client *c, const Arg *arg)
1160 scroll(gtk_scrolled_window_get_vadjustment(
1161 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1165 scroll(GtkAdjustment *a, const Arg *arg)
1169 v = gtk_adjustment_get_value(a);
1173 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1178 v += gtk_adjustment_get_step_increment(a) * arg->i;
1182 v = MIN(v, gtk_adjustment_get_upper(a) -
1183 gtk_adjustment_get_page_size(a));
1184 gtk_adjustment_set_value(a, v);
1188 setatom(Client *c, int a, const char *v)
1191 XChangeProperty(dpy, c->xid,
1192 atoms[a], XA_STRING, 8, PropModeReplace,
1193 (unsigned char *)v, strlen(v) + 1);
1200 char *styledirfile, *stylepath;
1201 WebKitWebContext *context;
1202 GError *error = NULL;
1204 /* clean up any zombies immediately */
1206 gtk_init(NULL, NULL);
1208 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1211 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1212 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1213 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1215 /* dirs and files */
1216 cookiefile = buildfile(cookiefile);
1217 scriptfile = buildfile(scriptfile);
1218 cachedir = buildpath(cachedir);
1219 if (stylefile == NULL) {
1220 styledir = buildpath(styledir);
1221 for (i = 0; i < LENGTH(styles); i++) {
1222 if (regcomp(&(styles[i].re), styles[i].regex,
1225 "Could not compile regex: %s\n",
1227 styles[i].regex = NULL;
1229 styledirfile = g_strconcat(styledir, "/",
1230 styles[i].style, NULL);
1231 stylepath = buildfile(styledirfile);
1232 styles[i].style = g_strconcat("file://", stylepath,
1234 g_free(styledirfile);
1239 stylepath = buildfile(stylefile);
1240 stylefile = g_strconcat("file://", stylepath, NULL);
1248 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1249 die("Can't install SIGCHLD handler");
1250 while (0 < waitpid(-1, NULL, WNOHANG));
1254 spawn(Client *c, const Arg *arg)
1258 close(ConnectionNumber(dpy));
1260 execvp(((char **)arg->v)[0], (char **)arg->v);
1261 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1268 eval(Client *c, const Arg *arg)
1270 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1271 evalscript(webkit_web_frame_get_global_context(frame),
1272 ((char **)arg->v)[0], "");
1276 stop(Client *c, const Arg *arg)
1278 webkit_web_view_stop_loading(c->view);
1282 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1284 c->title = webkit_web_view_get_title(c->view);
1289 titlechangeleave(void *a, void *b, Client *c)
1291 c->linkhover = NULL;
1296 toggle(Client *c, const Arg *arg)
1298 WebKitWebSettings *settings;
1299 char *name = (char *)arg->v;
1301 Arg a = { .b = FALSE };
1303 settings = webkit_web_view_get_settings(c->view);
1304 g_object_get(G_OBJECT(settings), name, &value, NULL);
1305 g_object_set(G_OBJECT(settings), name, !value, NULL);
1311 togglecookiepolicy(Client *c, const Arg *arg)
1314 cookiepolicy %= strlen(cookiepolicies);
1316 webkit_cookie_manager_set_accept_policy(
1317 webkit_web_context_get_cookie_manager(
1318 webkit_web_view_get_context(c->view)),
1319 cookiepolicy_get());
1322 /* Do not reload. */
1326 togglegeolocation(Client *c, const Arg *arg)
1328 Arg a = { .b = FALSE };
1330 allowgeolocation ^= 1;
1335 twitch(Client *c, const Arg *arg)
1340 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1343 v = gtk_adjustment_get_value(a);
1348 v = MIN(v, gtk_adjustment_get_upper(a) -
1349 gtk_adjustment_get_page_size(a));
1350 gtk_adjustment_set_value(a, v);
1354 togglescrollbars(Client *c, const Arg *arg)
1356 GtkPolicyType vspolicy;
1359 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1362 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1363 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1367 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1368 GTK_POLICY_AUTOMATIC,
1369 GTK_POLICY_AUTOMATIC);
1378 togglestyle(Client *c, const Arg *arg)
1380 enablestyle = !enablestyle;
1381 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1387 gettogglestat(Client *c)
1391 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1393 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1395 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1397 togglestat[p++] = value? 'C': 'c';
1399 togglestat[p++] = allowgeolocation? 'G': 'g';
1401 togglestat[p++] = enablediskcache? 'D': 'd';
1403 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1404 togglestat[p++] = value? 'I': 'i';
1406 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1407 togglestat[p++] = value? 'S': 's';
1409 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1410 togglestat[p++] = value? 'V': 'v';
1412 togglestat[p++] = enablestyle ? 'M': 'm';
1414 togglestat[p] = '\0';
1418 getpagestat(Client *c)
1420 const char *uri = geturi(c);
1422 if (strstr(uri, "https://") == uri)
1423 pagestat[0] = c->sslfailed ? 'U' : 'T';
1431 updatetitle(Client *c)
1435 if (showindicators) {
1440 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1442 } else if (c->progress != 100) {
1443 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1444 togglestat, pagestat,
1445 c->title == NULL ? "" : c->title);
1447 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1448 c->title == NULL ? "" : c->title);
1451 gtk_window_set_title(GTK_WINDOW(c->win), t);
1454 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1460 updatewinid(Client *c)
1462 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1468 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1469 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1470 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1474 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1475 JSObjectRef win, Client *c)
1481 zoom(Client *c, const Arg *arg)
1486 webkit_web_view_zoom_out(c->view);
1487 } else if (arg->i > 0) {
1489 webkit_web_view_zoom_in(c->view);
1493 webkit_web_view_set_zoom_level(c->view, 1.0);
1498 main(int argc, char *argv[])
1503 memset(&arg, 0, sizeof(arg));
1505 /* command line args */
1508 cookiepolicies = EARGF(usage());
1511 enablescrollbars = 0;
1514 enablescrollbars = 1;
1517 cookiefile = EARGF(usage());
1520 enablediskcache = 0;
1523 enablediskcache = 1;
1526 embed = strtol(EARGF(usage()), NULL, 0);
1529 runinfullscreen = 0;
1532 runinfullscreen = 1;
1535 allowgeolocation = 0;
1538 allowgeolocation = 1;
1559 enableinspector = 0;
1562 enableinspector = 1;
1571 scriptfile = EARGF(usage());
1580 stylefile = EARGF(usage());
1583 useragent = EARGF(usage());
1586 die("surf-"VERSION", ©2009-2015 surf engineers, "
1587 "see LICENSE for details\n");
1592 zoomlevel = strtof(EARGF(usage()), NULL);
1601 c = newclient(NULL);
1604 loaduri(clients, &arg);
1611 return EXIT_SUCCESS;