-decideresource(WebKitPolicyDecision *d, Client *c)
-{
- const gchar *uri;
- int i, isascii = 1;
- WebKitResponsePolicyDecision *r = WEBKIT_RESPONSE_POLICY_DECISION(d);
- WebKitURIResponse *res;
-
- res = webkit_response_policy_decision_get_response(r);
- uri = webkit_uri_response_get_uri(res);
-
- if (g_str_has_suffix(uri, "/favicon.ico"))
- webkit_uri_request_set_uri(
- webkit_response_policy_decision_get_request(r),
- "about:blank");
-
- if (!g_str_has_prefix(uri, "http://")
- && !g_str_has_prefix(uri, "https://")
- && !g_str_has_prefix(uri, "about:")
- && !g_str_has_prefix(uri, "file://")
- && !g_str_has_prefix(uri, "data:")
- && !g_str_has_prefix(uri, "blob:")
- && strlen(uri) > 0) {
- for (i = 0; i < strlen(uri); i++) {
- if (!g_ascii_isprint(uri[i])) {
- isascii = 0;
- break;
- }
- }
- if (isascii) {
- handleplumb(c, uri);
- webkit_policy_decision_ignore(d);
- }
- }
-
- if (webkit_response_policy_decision_is_mime_type_supported(r)) {
- webkit_policy_decision_use(d);
- } else {
- webkit_policy_decision_ignore(d);
- download(c, res);
- }
-}
-
-void
-destroyclient(Client *c)
-{
- Client *p;
-
- webkit_web_view_stop_loading(c->view);
- /* Not needed, has already been called
- gtk_widget_destroy(c->win);
- */
-
- for (p = clients; p && p->next != c; p = p->next)
- ;
- if (p)
- p->next = c->next;
- else
- clients = c->next;
- free(c);
-}
-
-void
-closeview(WebKitWebView *v, Client *c)
-{
- gtk_widget_destroy(c->win);
-}
-
-void
-destroywin(GtkWidget* w, Client *c)
-{
- destroyclient(c);
- if (clients == NULL)
- gtk_main_quit();
-}
-
-void
-die(const char *errstr, ...)
-{
- va_list ap;
-
- va_start(ap, errstr);
- vfprintf(stderr, errstr, ap);
- va_end(ap);
- exit(EXIT_FAILURE);
-}
-
-void
-find(Client *c, const Arg *a)
-{
- const char *s, *f;
-
- if (a && a->i) {
- if (a->i > 0)
- webkit_find_controller_search_next(c->finder);
- else
- webkit_find_controller_search_previous(c->finder);
- } else {
- s = getatom(c, AtomFind);
- f = webkit_find_controller_get_search_text(c->finder);
-
- if (g_strcmp0(f, s) == 0) /* reset search */
- webkit_find_controller_search(c->finder, "", findopts, G_MAXUINT);
-
- webkit_find_controller_search(c->finder, s, findopts, G_MAXUINT);
-
- if (strcmp(s, "") == 0)
- webkit_find_controller_search_finish(c->finder);
- }
-}
-
-void
-togglefullscreen(Client *c, const Arg *a)
-{
- /* toggling value is handled in winevent() */
- if (c->fullscreen)
- gtk_window_unfullscreen(GTK_WINDOW(c->win));
- else
- gtk_window_fullscreen(GTK_WINDOW(c->win));
-}
-
-gboolean
-permissionrequested(WebKitWebView *v, WebKitPermissionRequest *r, Client *c)
-{
- if (WEBKIT_IS_GEOLOCATION_PERMISSION_REQUEST(r)) {
- if (allowgeolocation)
- webkit_permission_request_allow(r);
- else
- webkit_permission_request_deny(r);
- return TRUE;
- }
-
- return FALSE;
-}
-
-const char *
-getatom(Client *c, int a)
-{
- static char buf[BUFSIZ];
- Atom adummy;
- int idummy;
- unsigned long ldummy;
- unsigned char *p = NULL;
-
- XGetWindowProperty(dpy, c->xid,
- atoms[a], 0L, BUFSIZ, False, XA_STRING,
- &adummy, &idummy, &ldummy, &ldummy, &p);
- if (p)
- strncpy(buf, (char *)p, LENGTH(buf)-1);
- else
- buf[0] = '\0';
- XFree(p);
-
- return buf;
-}
-
-char *
-geturi(Client *c)
-{
- char *uri;
-
- if (!(uri = (char *)webkit_web_view_get_uri(c->view)))
- uri = "about:blank";
- return uri;
-}
-
-const gchar *
-getstyle(const char *uri)
-{
- int i;
-
- if (stylefile != NULL)
- return stylefile;
-
- for (i = 0; i < LENGTH(styles); i++) {
- if (styles[i].regex && !regexec(&(styles[i].re), uri, 0,
- NULL, 0))
- return styles[i].style;
- }
-
- return "";
-}
-
-void
-setstyle(Client *c, const char *stylefile)
-{
- gchar *style;
-
- if (!g_file_get_contents(stylefile, &style, NULL, NULL)) {
- fprintf(stderr, "Could not read style file: %s\n", stylefile);
- return;
- }
-
- webkit_user_content_manager_add_style_sheet(
- webkit_web_view_get_user_content_manager(c->view),
- webkit_user_style_sheet_new(style,
- WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
- WEBKIT_USER_STYLE_LEVEL_USER,
- NULL, NULL));
-
- g_free(style);
-}
-
-void
-handleplumb(Client *c, const gchar *uri)
-{
- Arg arg;
-
- arg = (Arg)PLUMB(uri);
- spawn(c, &arg);
-}
-
-void
-downloadstarted(WebKitWebContext *wc, WebKitDownload *d, Client *c)
-{
- g_signal_connect(G_OBJECT(d), "notify::response",
- G_CALLBACK(responsereceived), c);
-}
-
-void
-responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c)
-{
- download(c, webkit_download_get_response(d));
- webkit_download_cancel(d);
-}
-
-void
-download(Client *c, WebKitURIResponse *r)
-{
- Arg a;
-
- a = (Arg)DOWNLOAD(webkit_uri_response_get_uri(r), geturi(c));
- spawn(c, &a);
-}
-
-void
-toggleinspector(Client *c, const Arg *a)
-{
- if (enableinspector) {
- if (webkit_web_inspector_is_attached(c->inspector))
- webkit_web_inspector_close(c->inspector);
- else
- webkit_web_inspector_show(c->inspector);
- }
-}
-
-gboolean
-keypress(GtkAccelGroup *group, GObject *obj, guint key, GdkModifierType mods,
- Client *c)
-{
- guint i;
- gboolean processed = FALSE;
-
- mods = CLEANMASK(mods);
- key = gdk_keyval_to_lower(key);
- updatewinid(c);
- for (i = 0; i < LENGTH(keys); i++) {
- if (key == keys[i].keyval
- && mods == keys[i].mod
- && keys[i].func) {
- keys[i].func(c, &(keys[i].arg));
- processed = TRUE;
- }
- }
-
- return processed;
-}
-
-void
-mousetargetchanged(WebKitWebView *v, WebKitHitTestResult *h, guint modifiers,
- Client *c)
-{
- WebKitHitTestResultContext hc;
-
- /* Keep the hit test to know where is the pointer on the next click */
- c->mousepos = h;
-
- hc = webkit_hit_test_result_get_context(h);
-
- if (hc & OnLink)
- c->targeturi = webkit_hit_test_result_get_link_uri(h);
- else if (hc & OnImg)
- c->targeturi = webkit_hit_test_result_get_image_uri(h);
- else if (hc & OnMedia)
- c->targeturi = webkit_hit_test_result_get_media_uri(h);
- else
- c->targeturi = NULL;
- updatetitle(c);
-}
-
-void
-loadchanged(WebKitWebView *v, WebKitLoadEvent e, Client *c)
-{
- switch (e) {
- case WEBKIT_LOAD_STARTED:
- c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
- break;
- case WEBKIT_LOAD_REDIRECTED:
- setatom(c, AtomUri, geturi(c));
- break;
- case WEBKIT_LOAD_COMMITTED:
- if (!webkit_web_view_get_tls_info(c->view, NULL, &(c->tlsflags)))
- c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
-
- setatom(c, AtomUri, geturi(c));
-
- if (enablestyle)
- setstyle(c, getstyle(geturi(c)));
- break;
- case WEBKIT_LOAD_FINISHED:
- /* Disabled until we write some WebKitWebExtension for
- * manipulating the DOM directly.
- evalscript(c, "document.documentElement.style.overflow = '%s'",
- enablescrollbars ? "auto" : "hidden");
- */
- runscript(c);
- break;
- }
- updatetitle(c);
-}
-
-void
-loaduri(Client *c, const Arg *arg)
-{
- char *u = NULL, *rp;
- const char *uri = (char *)arg->v;
- Arg a = { .b = FALSE };
- struct stat st;
-
- if (strcmp(uri, "") == 0)
- return;
-
- /* In case it's a file path. */
- if (stat(uri, &st) == 0) {
- rp = realpath(uri, NULL);
- u = g_strdup_printf("file://%s", rp);
- free(rp);
- } else {
- u = g_strrstr(uri, "://") ? g_strdup(uri)
- : g_strdup_printf("http://%s", uri);
- }
-
- setatom(c, AtomUri, uri);
-
- /* prevents endless loop */
- if (strcmp(u, geturi(c)) == 0) {
- reload(c, &a);
- } else {
- webkit_web_view_load_uri(c->view, u);
- c->progress = 0;
- c->title = copystr(&c->title, u);
- updatetitle(c);
- }
- g_free(u);
-}
-
-void
-navigate(Client *c, const Arg *a)
-{
- if (a->i < 0)
- webkit_web_view_go_back(c->view);
- else if (a->i > 0)
- webkit_web_view_go_forward(c->view);
-}
-
-Client *
-newclient(Client *rc)
-{
- Client *c;
- gdouble dpi;
-
- if (!(c = calloc(1, sizeof(Client))))
- die("Cannot malloc!\n");
-
- c->title = NULL;
- c->progress = 100;
-
- c->next = clients;
- clients = c;
-
- c->view = newview(c, rc ? rc->view : NULL);
- c->tlsflags = G_TLS_CERTIFICATE_VALIDATE_ALL + 1;
-
- return c;
-}
-
-WebKitWebView *
-newview(Client *c, WebKitWebView *rv)
-{
- WebKitWebView *v;
- WebKitSettings *settings;
- WebKitUserContentManager *contentmanager;
- WebKitWebContext *context;
- char *ua;
-
- /* Webview */
- if (rv) {
- v = WEBKIT_WEB_VIEW(
- webkit_web_view_new_with_related_view(rv));
- } else {
- settings = webkit_settings_new_with_settings(
- "auto-load-images", loadimages,
- "default-font-size", defaultfontsize,
- "enable-caret-browsing", enablecaretbrowsing,
- "enable-developer-extras", enableinspector,
- "enable-dns-prefetching", enablednsprefetching,
- "enable-frame-flattening", enableframeflattening,
- "enable-html5-database", enablecache,
- "enable-html5-local-storage", enablecache,
- "enable-javascript", enablescripts,
- "enable-plugins", enableplugins,
- NULL);
- if (!(ua = getenv("SURF_USERAGENT")))
- ua = useragent;
- webkit_settings_set_user_agent(settings, ua);
- /* Have a look at http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html
- * for more interesting settings */
-
- contentmanager = webkit_user_content_manager_new();
-
- context = webkit_web_context_new_with_website_data_manager(
- webkit_website_data_manager_new(
- "base-cache-directory", cachedir,
- "base-data-directory", cachedir,
- NULL));
-
- /* rendering process model, can be a shared unique one or one for each
- * view */
- webkit_web_context_set_process_model(context,
- WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
- /* ssl */
- webkit_web_context_set_tls_errors_policy(context, strictssl ?
- WEBKIT_TLS_ERRORS_POLICY_FAIL : WEBKIT_TLS_ERRORS_POLICY_IGNORE);
- /* disk cache */
- webkit_web_context_set_cache_model(context, enablecache ?
- WEBKIT_CACHE_MODEL_WEB_BROWSER : WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
-
- /* Currently only works with text file to be compatible with curl */
- webkit_cookie_manager_set_persistent_storage(
- webkit_web_context_get_cookie_manager(context), cookiefile,
- WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
- /* cookie policy */
- webkit_cookie_manager_set_accept_policy(
- webkit_web_context_get_cookie_manager(context),
- cookiepolicy_get());
-
- g_signal_connect(G_OBJECT(context), "download-started",
- G_CALLBACK(downloadstarted), c);
-
- v = g_object_new(WEBKIT_TYPE_WEB_VIEW,
- "settings", settings,
- "user-content-manager", contentmanager,
- "web-context", context,
- NULL);
- }
-
- g_signal_connect(G_OBJECT(v),
- "notify::title",
- G_CALLBACK(titlechanged), c);
- g_signal_connect(G_OBJECT(v),
- "mouse-target-changed",
- G_CALLBACK(mousetargetchanged), c);
- g_signal_connect(G_OBJECT(v),
- "permission-request",
- G_CALLBACK(permissionrequested), c);
- g_signal_connect(G_OBJECT(v),
- "create",
- G_CALLBACK(createview), c);
- g_signal_connect(G_OBJECT(v), "ready-to-show",
- G_CALLBACK(showview), c);
- g_signal_connect(G_OBJECT(v),
- "decide-policy",
- G_CALLBACK(decidepolicy), c);
- g_signal_connect(G_OBJECT(v),
- "load-changed",
- G_CALLBACK(loadchanged), c);
- g_signal_connect(G_OBJECT(v),
- "notify::estimated-load-progress",
- G_CALLBACK(progresschanged), c);
- g_signal_connect(G_OBJECT(v),
- "button-release-event",
- G_CALLBACK(buttonreleased), c);
- g_signal_connect(G_OBJECT(v), "close",
- G_CALLBACK(closeview), c);
-
- return v;
-}
-
-void
-showview(WebKitWebView *v, Client *c)
-{
- GdkGeometry hints = { 1, 1 };
- GdkRGBA bgcolor = { 0 };
- GdkWindow *gwin;
-
- c->win = createwindow(c);
-
- if (enableinspector)
- c->inspector = webkit_web_view_get_inspector(c->view);
-
- c->finder = webkit_web_view_get_find_controller(c->view);
-
- if (!kioskmode)
- addaccelgroup(c);
-
- /* Arranging */
- gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
-
- /* Setup */
- gtk_widget_grab_focus(GTK_WIDGET(c->view));
- gtk_widget_show(GTK_WIDGET(c->view));
- gtk_widget_show(c->win);
- gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
- c->xid = gdk_x11_window_get_xid(gwin);
- gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
- GDK_HINT_MIN_SIZE);
- gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
- gdk_window_add_filter(gwin, processx, c);
-
- if (zoomlevel != 1.0)
- webkit_web_view_set_zoom_level(c->view, zoomlevel);
-
- if (runinfullscreen)
- togglefullscreen(c, NULL);
-
- setatom(c, AtomFind, "");
- setatom(c, AtomUri, "about:blank");
- if (hidebackground)
- webkit_web_view_set_background_color(c->view, &bgcolor);
-
- if (showxid) {
- gdk_display_sync(gtk_widget_get_display(c->win));
- printf("%lu\n", c->xid);
- fflush(NULL);
- if (fclose(stdout) != 0) {
- die("Error closing stdout");
- }
- }
-}
-
-void
-newwindow(Client *c, const Arg *arg, gboolean noembed)
-{
- guint i = 0;
- const char *cmd[18], *uri;
- const Arg a = { .v = (void *)cmd };
- char tmp[64];
-
- cmd[i++] = argv0;
- cmd[i++] = "-a";
- cmd[i++] = cookiepolicies;
- if (!enablescrollbars)
- cmd[i++] = "-b";
- if (embed && !noembed) {
- cmd[i++] = "-e";
- snprintf(tmp, LENGTH(tmp), "%u", (int)embed);
- cmd[i++] = tmp;
- }
- if (!allowgeolocation)
- cmd[i++] = "-g";
- if (!loadimages)
- cmd[i++] = "-i";
- if (kioskmode)
- cmd[i++] = "-k";
- if (!enableplugins)
- cmd[i++] = "-p";
- if (!enablescripts)
- cmd[i++] = "-s";
- if (showxid)
- cmd[i++] = "-x";
- if (enablecache)
- cmd[i++] = "-D";
- cmd[i++] = "-c";
- cmd[i++] = cookiefile;
- cmd[i++] = "--";
- uri = arg->v ? (char *)arg->v : c->linkhover;
- if (uri)
- cmd[i++] = uri;
- cmd[i++] = NULL;
- spawn(NULL, &a);
-}
-
-GtkWidget *
-createwindow(Client *c)
-{
- GtkWidget *w;
-
- if (embed) {
- w = gtk_plug_new(embed);
- } else {
- w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-
- /* TA: 20091214: Despite what the GNOME docs say, the ICCCM
- * is always correct, so we should still call this function.
- * But when doing so, we *must* differentiate between a
- * WM_CLASS and a resource on the window. By convention, the
- * window class (WM_CLASS) is capped, while the resource is in
- * lowercase. Both these values come as a pair.
- */
- gtk_window_set_wmclass(GTK_WINDOW(w), "surf", "Surf");
-
- /* TA: 20091214: And set the role here as well -- so that
- * sessions can pick this up.
- */
- gtk_window_set_role(GTK_WINDOW(w), "Surf");