- 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 *a)
-{
- struct stat st;
- char *url, *path;
- const char *uri = (char *)a->v;
-
- if (g_strcmp0(uri, "") == 0)
- return;
-
- if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) {
- url = g_strdup(uri);
- } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
- url = g_strdup_printf("file://%s", path);
- free(path);
- } else {
- url = g_strdup_printf("http://%s", uri);
- }
-
- setatom(c, AtomUri, url);
-
- if (strcmp(url, geturi(c)) == 0) {
- reload(c, a);
- } else {
- webkit_web_view_load_uri(c->view, url);
- c->title = geturi(c);
- updatetitle(c);
- }
-
- g_free(url);
-}
-
-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);
-}
-
-void
-clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h)
-{
- navigate(c, a);
-}
-
-Client *
-newclient(Client *rc)
-{
- Client *c;
-
- 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;