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 void geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
134 WebKitGeolocationPolicyDecision *d, 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 geopolicyrequested(WebKitWebView *v, WebKitWebFrame *f,
532 WebKitGeolocationPolicyDecision *d, Client *c)
534 if (allowgeolocation)
535 webkit_geolocation_policy_allow(d);
537 webkit_geolocation_policy_deny(d);
541 getatom(Client *c, int a)
543 static char buf[BUFSIZ];
546 unsigned long ldummy;
547 unsigned char *p = NULL;
549 XGetWindowProperty(dpy, c->xid,
550 atoms[a], 0L, BUFSIZ, False, XA_STRING,
551 &adummy, &idummy, &ldummy, &ldummy, &p);
553 strncpy(buf, (char *)p, LENGTH(buf)-1);
566 if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
572 getstyle(const char *uri)
576 if (stylefile != NULL)
579 for (i = 0; i < LENGTH(styles); i++) {
580 if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
582 return styles[i].style;
589 setstyle(Client *c, const char *style)
591 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
593 g_object_set(G_OBJECT(settings), "user-stylesheet-uri", style, NULL);
597 handleplumb(Client *c, WebKitWebView *w, const gchar *uri)
601 webkit_web_view_stop_loading(w);
602 arg = (Arg)PLUMB((char *)uri);
607 initdownload(WebKitWebView *view, WebKitDownload *o, Client *c)
612 arg = (Arg)DOWNLOAD((char *)webkit_download_get_uri(o), geturi(c));
618 inspector(Client *c, const Arg *arg)
620 if (enableinspector) {
622 webkit_web_inspector_close(c->inspector);
624 webkit_web_inspector_show(c->inspector);
629 inspector_new(WebKitWebInspector *i, WebKitWebView *v, Client *c)
631 return WEBKIT_WEB_VIEW(webkit_web_view_new());
635 inspector_show(WebKitWebInspector *i, Client *c)
642 w = webkit_web_inspector_get_web_view(i);
643 gtk_paned_pack2(GTK_PANED(c->pane), GTK_WIDGET(w), TRUE, TRUE);
644 gtk_widget_show(GTK_WIDGET(w));
645 c->isinspecting = true;
651 inspector_close(WebKitWebInspector *i, Client *c)
655 if (!c->isinspecting)
658 w = GTK_WIDGET(webkit_web_inspector_get_web_view(i));
660 gtk_widget_destroy(w);
661 c->isinspecting = false;
667 inspector_finished(WebKitWebInspector *i, Client *c)
669 g_free(c->inspector);
673 keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
677 gboolean processed = FALSE;
679 mods = CLEANMASK(mods);
680 key = gdk_keyval_to_lower(key);
682 for (i = 0; i < LENGTH(keys); i++) {
683 if (key == keys[i].keyval
684 && mods == keys[i].mod
686 keys[i].func(c, &(keys[i].arg));
695 mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
698 WebKitHitTestResultContext hc;
700 /* Keep the hit test to know where is the pointer on the next click */
703 hc = webkit_hit_test_result_get_context(h);
706 c->targeturi = webkit_hit_test_result_get_link_uri(h);
708 c->targeturi = webkit_hit_test_result_get_image_uri(h);
709 else if (hc & OnMedia)
710 c->targeturi = webkit_hit_test_result_get_media_uri(h);
717 loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
719 WebKitWebFrame *frame;
720 WebKitWebDataSource *src;
721 WebKitNetworkRequest *request;
725 switch (webkit_web_view_get_load_status (c->view)) {
726 case WEBKIT_LOAD_COMMITTED:
728 if (strstr(uri, "https://") == uri) {
729 frame = webkit_web_view_get_main_frame(c->view);
730 src = webkit_web_frame_get_data_source(frame);
731 request = webkit_web_data_source_get_request(src);
732 msg = webkit_network_request_get_message(request);
733 c->sslfailed = !(soup_message_get_flags(msg)
734 & SOUP_MESSAGE_CERTIFICATE_TRUSTED);
736 setatom(c, AtomUri, uri);
739 setstyle(c, getstyle(uri));
741 case WEBKIT_LOAD_FINISHED:
751 loaduri(Client *c, const Arg *arg)
754 const char *uri = (char *)arg->v;
755 Arg a = { .b = FALSE };
758 if (strcmp(uri, "") == 0)
761 /* In case it's a file path. */
762 if (stat(uri, &st) == 0) {
763 rp = realpath(uri, NULL);
764 u = g_strdup_printf("file://%s", rp);
767 u = g_strrstr(uri, "://") ? g_strdup(uri)
768 : g_strdup_printf("http://%s", uri);
771 setatom(c, AtomUri, uri);
773 /* prevents endless loop */
774 if (strcmp(u, geturi(c)) == 0) {
777 webkit_web_view_load_uri(c->view, u);
779 c->title = copystr(&c->title, u);
786 navigate(Client *c, const Arg *arg)
788 int steps = *(int *)arg;
789 webkit_web_view_go_back_or_forward(c->view, steps);
793 newclient(Client *rc)
798 if (!(c = calloc(1, sizeof(Client))))
799 die("Cannot malloc!\n");
807 c->view = newview(c, rc ? rc->view : NULL);
813 newview(Client *c, WebKitWebView *rv)
816 WebKitSettings *settings;
817 WebKitUserContentManager *contentmanager;
818 WebKitWebContext *context;
824 webkit_web_view_new_with_related_view(rv));
826 settings = webkit_settings_new_with_settings(
827 "auto-load-images", loadimages,
828 "default-font-size", defaultfontsize,
829 "enable-caret-browsing", enablecaretbrowsing,
830 "enable-developer-extras", enableinspector,
831 "enable-dns-prefetching", enablednsprefetching,
832 "enable-frame-flattening", enableframeflattening,
833 "enable-html5-database", enablecache,
834 "enable-html5-local-storage", enablecache,
835 "enable-javascript", enablescripts,
836 "enable-plugins", enableplugins,
838 if (!(ua = getenv("SURF_USERAGENT")))
840 webkit_settings_set_user_agent(settings, ua);
841 /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
842 * for more interesting settings */
844 contentmanager = webkit_user_content_manager_new();
846 context = webkit_web_context_new_with_website_data_manager(
847 webkit_website_data_manager_new(
848 "base-cache-directory", cachedir,
849 "base-data-directory", cachedir,
852 /* rendering process model, can be a shared unique one or one for each
854 webkit_web_context_set_process_model(context,
855 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
857 webkit_web_context_set_tls_errors_policy(context, strictssl ?
858 WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
860 webkit_web_context_set_cache_model(context, enablecache ?
861 WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
863 /* Currently only works with text file to be compatible with curl */
864 webkit_cookie_manager_set_persistent_storage(
865 webkit_web_context_get_cookie_manager(context), cookiefile,
866 WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
868 webkit_cookie_manager_set_accept_policy(
869 webkit_web_context_get_cookie_manager(context),
872 v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
873 "settings", settings,
874 "user-content-manager", contentmanager,
875 "web-context", context,
879 g_signal_connect(G_OBJECT(v),
881 G_CALLBACK(titlechanged), c);
882 g_signal_connect(G_OBJECT(v),
883 "mouse-target-changed",
884 G_CALLBACK(mousetargetchanged), c);
885 g_signal_connect(G_OBJECT(v),
886 "geolocation-policy-decision-requested",
887 G_CALLBACK(geopolicyrequested), c);
888 g_signal_connect(G_OBJECT(v),
890 G_CALLBACK(createwindow), c);
891 g_signal_connect(G_OBJECT(v), "ready-to-show",
892 G_CALLBACK(showview), c);
893 g_signal_connect(G_OBJECT(v),
894 "new-window-policy-decision-requested",
895 G_CALLBACK(decidewindow), c);
896 g_signal_connect(G_OBJECT(v),
897 "mime-type-policy-decision-requested",
898 G_CALLBACK(decidedownload), c);
899 g_signal_connect(G_OBJECT(v),
900 "window-object-cleared",
901 G_CALLBACK(windowobjectcleared), c);
902 g_signal_connect(G_OBJECT(v),
903 "notify::load-status",
904 G_CALLBACK(loadstatuschange), c);
905 g_signal_connect(G_OBJECT(v),
907 G_CALLBACK(progresschange), c);
908 g_signal_connect(G_OBJECT(v),
909 "download-requested",
910 G_CALLBACK(initdownload), c);
911 g_signal_connect(G_OBJECT(v),
912 "button-release-event",
913 G_CALLBACK(buttonrelease), c);
914 g_signal_connect(G_OBJECT(v),
916 G_CALLBACK(contextmenu), c);
917 g_signal_connect(G_OBJECT(v),
918 "resource-request-starting",
919 G_CALLBACK(beforerequest), c);
920 g_signal_connect(G_OBJECT(v),
921 "should-show-delete-interface-for-element",
922 G_CALLBACK(deletion_interface), c);
928 showview(WebKitWebView *v, Client *c)
930 GdkGeometry hints = { 1, 1 };
931 GdkRGBA bgcolor = { 0 };
936 c->win = gtk_plug_new(embed);
938 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
940 /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
941 * is always correct, so we should still call this function.
942 * But when doing so, we *must* differentiate between a
943 * WM_CLASS and a resource on the window. By convention, the
944 * window class (WM_CLASS) is capped, while the resource is in
945 * lowercase. Both these values come as a pair.
947 gtk_window_set_wmclass(GTK_WINDOW(c->win), "surf", "Surf");
949 /* TA: 20091214: And set the role here as well -- so that
950 * sessions can pick this up.
952 gtk_window_set_role(GTK_WINDOW(c->win), "Surf");
954 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
955 g_signal_connect(G_OBJECT(c->win),
957 G_CALLBACK(destroywin), c);
958 g_signal_connect(G_OBJECT(c->win),
959 "leave_notify_event",
960 G_CALLBACK(titlechangeleave), c);
966 gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
969 gtk_widget_grab_focus(GTK_WIDGET(c->view));
970 gtk_widget_show(GTK_WIDGET(c->view));
971 gtk_widget_show(c->win);
972 gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
973 c->xid = gdk_x11_window_get_xid(gwin);
974 gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
976 gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
977 gdk_window_add_filter(gwin, processx, c);
981 /* This might conflict with _zoomto96dpi_. */
982 if (zoomlevel != 1.0)
983 webkit_web_view_set_zoom_level(c->view, zoomlevel);
988 setatom(c, AtomFind, "");
989 setatom(c, AtomUri, "about:blank");
991 webkit_web_view_set_background_color(c->view, &bgcolor);
994 gdk_display_sync(gtk_widget_get_display(c->win));
995 printf("%lu\n", c->xid);
997 if (fclose(stdout) != 0) {
998 die("Error closing stdout");
1004 newwindow(Client *c, const Arg *arg, gboolean noembed)
1007 const char *cmd[18], *uri;
1008 const Arg a = { .v = (void *)cmd };
1013 cmd[i++] = cookiepolicies;
1014 if (!enablescrollbars)
1016 if (embed && !noembed) {
1018 snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
1021 if (!allowgeolocation)
1033 if (enablediskcache)
1036 cmd[i++] = cookiefile;
1038 uri = arg->v ? (char *)arg->v : c->linkhover;
1046 contextmenu(WebKitWebView *view, GtkWidget *menu, WebKitHitTestResult *target,
1047 gboolean keyboard, Client *c)
1049 GList *items = gtk_container_get_children(GTK_CONTAINER(GTK_MENU(menu)));
1051 for (GList *l = items; l; l = l->next)
1052 g_signal_connect(l->data, "activate", G_CALLBACK(menuactivate), c);
1059 menuactivate(GtkMenuItem *item, Client *c)
1062 * context-menu-action-2000 open link
1063 * context-menu-action-1 open link in window
1064 * context-menu-action-2 download linked file
1065 * context-menu-action-3 copy link location
1066 * context-menu-action-7 copy image address
1067 * context-menu-action-13 reload
1068 * context-menu-action-10 back
1069 * context-menu-action-11 forward
1070 * context-menu-action-12 stop
1073 const gchar *name, *uri;
1074 GtkClipboard *prisel, *clpbrd;
1076 name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
1080 if (!g_strcmp0(name, "context-menu-action-3")) {
1081 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1082 gtk_clipboard_set_text(prisel, c->linkhover, -1);
1083 } else if (!g_strcmp0(name, "context-menu-action-7")) {
1084 prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1085 clpbrd = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
1086 uri = gtk_clipboard_wait_for_text(clpbrd);
1088 gtk_clipboard_set_text(prisel, uri, -1);
1093 pasteuri(GtkClipboard *clipboard, const char *text, gpointer d)
1095 Arg arg = {.v = text };
1097 loaduri((Client *) d, &arg);
1101 print(Client *c, const Arg *arg)
1103 webkit_web_frame_print(webkit_web_view_get_main_frame(c->view));
1107 processx(GdkXEvent *e, GdkEvent *event, gpointer d)
1109 Client *c = (Client *)d;
1113 if (((XEvent *)e)->type == PropertyNotify) {
1114 ev = &((XEvent *)e)->xproperty;
1115 if (ev->state == PropertyNewValue) {
1116 if (ev->atom == atoms[AtomFind]) {
1120 return GDK_FILTER_REMOVE;
1121 } else if (ev->atom == atoms[AtomGo]) {
1122 arg.v = getatom(c, AtomGo);
1125 return GDK_FILTER_REMOVE;
1129 return GDK_FILTER_CONTINUE;
1133 progresschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
1135 c->progress = webkit_web_view_get_progress(c->view) * 100;
1140 linkopen(Client *c, const Arg *arg)
1142 newwindow(NULL, arg, 1);
1146 linkopenembed(Client *c, const Arg *arg)
1148 newwindow(NULL, arg, 0);
1152 reload(Client *c, const Arg *arg)
1154 gboolean nocache = *(gboolean *)arg;
1156 webkit_web_view_reload_bypass_cache(c->view);
1158 webkit_web_view_reload(c->view);
1162 scroll_h(Client *c, const Arg *arg)
1164 scroll(gtk_scrolled_window_get_hadjustment(
1165 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1169 scroll_v(Client *c, const Arg *arg)
1171 scroll(gtk_scrolled_window_get_vadjustment(
1172 GTK_SCROLLED_WINDOW(c->scroll)), arg);
1176 scroll(GtkAdjustment *a, const Arg *arg)
1180 v = gtk_adjustment_get_value(a);
1184 v += gtk_adjustment_get_page_increment(a) * (arg->i / 10000);
1189 v += gtk_adjustment_get_step_increment(a) * arg->i;
1193 v = MIN(v, gtk_adjustment_get_upper(a) -
1194 gtk_adjustment_get_page_size(a));
1195 gtk_adjustment_set_value(a, v);
1199 setatom(Client *c, int a, const char *v)
1202 XChangeProperty(dpy, c->xid,
1203 atoms[a], XA_STRING, 8, PropModeReplace,
1204 (unsigned char *)v, strlen(v) + 1);
1211 char *styledirfile, *stylepath;
1212 WebKitWebContext *context;
1213 GError *error = NULL;
1215 /* clean up any zombies immediately */
1217 gtk_init(NULL, NULL);
1219 dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
1222 atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
1223 atoms[AtomGo] = XInternAtom(dpy, "_SURF_GO", False);
1224 atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
1226 /* dirs and files */
1227 cookiefile = buildfile(cookiefile);
1228 scriptfile = buildfile(scriptfile);
1229 cachedir = buildpath(cachedir);
1230 if (stylefile == NULL) {
1231 styledir = buildpath(styledir);
1232 for (i = 0; i < LENGTH(styles); i++) {
1233 if (regcomp(&(styles[i].re), styles[i].regex,
1236 "Could not compile regex: %s\n",
1238 styles[i].regex = NULL;
1240 styledirfile = g_strconcat(styledir, "/",
1241 styles[i].style, NULL);
1242 stylepath = buildfile(styledirfile);
1243 styles[i].style = g_strconcat("file://", stylepath,
1245 g_free(styledirfile);
1250 stylepath = buildfile(stylefile);
1251 stylefile = g_strconcat("file://", stylepath, NULL);
1259 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1260 die("Can't install SIGCHLD handler");
1261 while (0 < waitpid(-1, NULL, WNOHANG));
1265 spawn(Client *c, const Arg *arg)
1269 close(ConnectionNumber(dpy));
1271 execvp(((char **)arg->v)[0], (char **)arg->v);
1272 fprintf(stderr, "surf: execvp %s", ((char **)arg->v)[0]);
1279 eval(Client *c, const Arg *arg)
1281 WebKitWebFrame *frame = webkit_web_view_get_main_frame(c->view);
1282 evalscript(webkit_web_frame_get_global_context(frame),
1283 ((char **)arg->v)[0], "");
1287 stop(Client *c, const Arg *arg)
1289 webkit_web_view_stop_loading(c->view);
1293 titlechanged(WebKitWebView *view, GParamSpec *ps, Client *c)
1295 c->title = webkit_web_view_get_title(c->view);
1300 titlechangeleave(void *a, void *b, Client *c)
1302 c->linkhover = NULL;
1307 toggle(Client *c, const Arg *arg)
1309 WebKitWebSettings *settings;
1310 char *name = (char *)arg->v;
1312 Arg a = { .b = FALSE };
1314 settings = webkit_web_view_get_settings(c->view);
1315 g_object_get(G_OBJECT(settings), name, &value, NULL);
1316 g_object_set(G_OBJECT(settings), name, !value, NULL);
1322 togglecookiepolicy(Client *c, const Arg *arg)
1325 cookiepolicy %= strlen(cookiepolicies);
1327 webkit_cookie_manager_set_accept_policy(
1328 webkit_web_context_get_cookie_manager(
1329 webkit_web_view_get_context(c->view)),
1330 cookiepolicy_get());
1333 /* Do not reload. */
1337 togglegeolocation(Client *c, const Arg *arg)
1339 Arg a = { .b = FALSE };
1341 allowgeolocation ^= 1;
1346 twitch(Client *c, const Arg *arg)
1351 a = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(
1354 v = gtk_adjustment_get_value(a);
1359 v = MIN(v, gtk_adjustment_get_upper(a) -
1360 gtk_adjustment_get_page_size(a));
1361 gtk_adjustment_set_value(a, v);
1365 togglescrollbars(Client *c, const Arg *arg)
1367 GtkPolicyType vspolicy;
1370 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(c->scroll), NULL,
1373 if (vspolicy == GTK_POLICY_AUTOMATIC) {
1374 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1378 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
1379 GTK_POLICY_AUTOMATIC,
1380 GTK_POLICY_AUTOMATIC);
1389 togglestyle(Client *c, const Arg *arg)
1391 enablestyle = !enablestyle;
1392 setstyle(c, enablestyle ? getstyle(geturi(c)) : "");
1398 gettogglestat(Client *c)
1402 WebKitWebSettings *settings = webkit_web_view_get_settings(c->view);
1404 togglestat[p++] = cookiepolicy_set(cookiepolicy_get());
1406 g_object_get(G_OBJECT(settings), "enable-caret-browsing", &value,
1408 togglestat[p++] = value? 'C': 'c';
1410 togglestat[p++] = allowgeolocation? 'G': 'g';
1412 togglestat[p++] = enablediskcache? 'D': 'd';
1414 g_object_get(G_OBJECT(settings), "auto-load-images", &value, NULL);
1415 togglestat[p++] = value? 'I': 'i';
1417 g_object_get(G_OBJECT(settings), "enable-scripts", &value, NULL);
1418 togglestat[p++] = value? 'S': 's';
1420 g_object_get(G_OBJECT(settings), "enable-plugins", &value, NULL);
1421 togglestat[p++] = value? 'V': 'v';
1423 togglestat[p++] = enablestyle ? 'M': 'm';
1425 togglestat[p] = '\0';
1429 getpagestat(Client *c)
1431 const char *uri = geturi(c);
1433 if (strstr(uri, "https://") == uri)
1434 pagestat[0] = c->sslfailed ? 'U' : 'T';
1442 updatetitle(Client *c)
1446 if (showindicators) {
1451 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1453 } else if (c->progress != 100) {
1454 t = g_strdup_printf("[%i%%] %s:%s | %s", c->progress,
1455 togglestat, pagestat,
1456 c->title == NULL ? "" : c->title);
1458 t = g_strdup_printf("%s:%s | %s", togglestat, pagestat,
1459 c->title == NULL ? "" : c->title);
1462 gtk_window_set_title(GTK_WINDOW(c->win), t);
1465 gtk_window_set_title(GTK_WINDOW(c->win), (c->title == NULL) ?
1471 updatewinid(Client *c)
1473 snprintf(winid, LENGTH(winid), "%lu", c->xid);
1479 die("usage: %s [-bBdDfFgGiIkKmMnNpPsSvx] [-a cookiepolicies ] "
1480 "[-c cookiefile] [-e xid] [-r scriptfile] [-t stylefile] "
1481 "[-u useragent] [-z zoomlevel] [uri]\n", basename(argv0));
1485 windowobjectcleared(GtkWidget *w, WebKitWebFrame *frame, JSContextRef js,
1486 JSObjectRef win, Client *c)
1492 zoom(Client *c, const Arg *arg)
1497 webkit_web_view_zoom_out(c->view);
1498 } else if (arg->i > 0) {
1500 webkit_web_view_zoom_in(c->view);
1504 webkit_web_view_set_zoom_level(c->view, 1.0);
1509 main(int argc, char *argv[])
1514 memset(&arg, 0, sizeof(arg));
1516 /* command line args */
1519 cookiepolicies = EARGF(usage());
1522 enablescrollbars = 0;
1525 enablescrollbars = 1;
1528 cookiefile = EARGF(usage());
1531 enablediskcache = 0;
1534 enablediskcache = 1;
1537 embed = strtol(EARGF(usage()), NULL, 0);
1540 runinfullscreen = 0;
1543 runinfullscreen = 1;
1546 allowgeolocation = 0;
1549 allowgeolocation = 1;
1570 enableinspector = 0;
1573 enableinspector = 1;
1582 scriptfile = EARGF(usage());
1591 stylefile = EARGF(usage());
1594 useragent = EARGF(usage());
1597 die("surf-"VERSION", ©2009-2015 surf engineers, "
1598 "see LICENSE for details\n");
1603 zoomlevel = strtof(EARGF(usage()), NULL);
1612 c = newclient(NULL);
1615 loaduri(clients, &arg);
1622 return EXIT_SUCCESS;