Skip to content
pi-c Documentation
Login

Documentation

/* The system clipboard's own commands (src/tui/clipboard.c), with stand-ins on PATH. On Linux:
 * a copy goes to wl-copy under Wayland, else xclip, else xsel under X, and nothing without a
 * display; a paste reads wl-paste, else xclip, else xsel, an image before text. macOS runs
 * /usr/bin/pbcopy and osascript by absolute path or on the real pasteboard, which no test may
 * touch, so there only the refusals are checked. */
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE 1
#endif
#include "tui/clipboard.h"

#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

static char dir[PATH_MAX];

#ifndef __APPLE__
static void tool(const char *name, const char *body) {
    char p[PATH_MAX + 64];
    FILE *f;
    (void)snprintf(p, sizeof p, "%s/bin/%s", dir, name);
    f = fopen(p, "w");
    assert(f);
    fprintf(f, "#!/bin/sh\n%s\n", body);
    fclose(f);
    assert(chmod(p, 0755) == 0);
}

/* What the stand-in NAME was given on stdin ("" when it never ran), and forget it. */
static const char *got(const char *name) {
    static char buf[256];
    char p[PATH_MAX + 64];
    FILE *f;
    size_t n = 0;
    (void)snprintf(p, sizeof p, "%s/%s.in", dir, name);
    buf[0] = '\0';
    if ((f = fopen(p, "r")) != NULL) {
        n = fread(buf, 1, sizeof buf - 1, f);
        fclose(f);
        (void)unlink(p);
    }
    buf[n] = '\0';
    return buf;
}

static void env(const char *wayland, const char *display) {
    if (wayland)
        setenv("WAYLAND_DISPLAY", wayland, 1);
    else
        unsetenv("WAYLAND_DISPLAY");
    if (display)
        setenv("DISPLAY", display, 1);
    else
        unsetenv("DISPLAY");
}
#endif

int main(void) {
    char path[PATH_MAX + 8], tmpl[] = "/tmp/pic-clip.XXXXXX";
    const char *tmp = getenv("TMPDIR");
    tui_clip c;
    if (tmp && *tmp && strlen(tmp) + 16 < sizeof dir) {
        (void)snprintf(dir, sizeof dir, "%s/pic-clip.XXXXXX", tmp);
    } else {
        (void)snprintf(dir, sizeof dir, "%s", tmpl);
    }
    assert(mkdtemp(dir));
    (void)snprintf(path, sizeof path, "%s/bin", dir);
    assert(mkdir(path, 0700) == 0);
    unsetenv("PIC_TEST_CLIPBOARD_CMD");
    unsetenv("PIC_CLIPBOARD_FILES_FILE");
    unsetenv("PIC_CLIPBOARD_IMAGE_FILE");
    unsetenv("PIC_CLIPBOARD_TEXT_FILE");
    unsetenv("XDG_SESSION_TYPE");

    /* PIC_NO_NATIVE_CLIPBOARD: no command runs, on any OS. */
    setenv("PIC_NO_NATIVE_CLIPBOARD", "1", 1);
    assert(tui_clipboard_write("x", 1) == -1);
    assert(tui_clipboard_read(&c) == TUI_CLIP_NONE && !c.text && !c.image);
    unsetenv("PIC_NO_NATIVE_CLIPBOARD");
    puts("ok clipboard: PIC_NO_NATIVE_CLIPBOARD runs no command");

#ifdef __APPLE__
    puts("skip clipboard: pbcopy and osascript are the real pasteboard on macOS; wl-copy, xclip and xsel are checked on Linux");
#else
    {
        char line[PATH_MAX + 64], *search = malloc(strlen(path) + strlen(getenv("PATH") ? getenv("PATH") : "") + 2);
        assert(search);
        (void)sprintf(search, "%s:%s", path, getenv("PATH") ? getenv("PATH") : "/usr/bin:/bin"); /* the stand-ins first */
        setenv("PATH", search, 1);
        free(search);
        /* each copy stand-in keeps its stdin; xclip's own argv is checked too */
        (void)snprintf(line, sizeof line, "cat > %s/wl-copy.in", dir);
        tool("wl-copy", line);
        (void)snprintf(line, sizeof line, "[ \"$*\" = '-selection clipboard -in' ] || exit 3; cat > %s/xclip.in", dir);
        tool("xclip", line);
        (void)snprintf(line, sizeof line, "[ \"$*\" = '--clipboard --input' ] || exit 3; cat > %s/xsel.in", dir);
        tool("xsel", line);

        env("wayland-0", ":0");
        assert(tui_clipboard_write("hello", 5) == 0);
        assert(strcmp(got("wl-copy"), "hello") == 0 && !*got("xclip") && !*got("xsel"));
        puts("ok clipboard: under Wayland a copy goes to wl-copy");

        env(NULL, ":0");
        assert(tui_clipboard_write("x11", 3) == 0);
        assert(strcmp(got("xclip"), "x11") == 0 && !*got("wl-copy") && !*got("xsel"));
        puts("ok clipboard: under X a copy goes to xclip -selection clipboard");

        tool("xclip", "exit 1");
        assert(tui_clipboard_write("sel", 3) == 0);
        assert(strcmp(got("xsel"), "sel") == 0);
        puts("ok clipboard: when xclip fails, xsel --clipboard takes the copy");

        tool("xsel", "exit 1");
        assert(tui_clipboard_write("none", 4) == -1);
        env(NULL, NULL);
        assert(tui_clipboard_write("none", 4) == -1);
        assert(!*got("wl-copy") && !*got("xclip") && !*got("xsel"));
        puts("ok clipboard: no display, or every tool failing, is -1 (the terminal's OSC 52 then)");

        /* paste: wl-paste lists a PNG and gives its bytes; text when no image is listed */
        tool("wl-paste", "case \"$*\" in '--list-types') printf 'text/plain\\nimage/png\\n' ;;"
                         " '--type image/png --no-newline') printf 'PNGBYTES' ;;"
                         " '--no-newline --type text') printf 'wtext' ;; *) exit 3 ;; esac");
        env("wayland-0", NULL);
        assert(tui_clipboard_read(&c) == TUI_CLIP_IMAGE && c.image_len == 8 && memcmp(c.image, "PNGBYTES", 8) == 0);
        tui_clip_free(&c);
        tool("wl-paste", "case \"$*\" in '--list-types') printf 'text/plain\\n' ;;"
                         " '--no-newline --type text') printf 'wtext' ;; *) exit 3 ;; esac");
        assert(tui_clipboard_read(&c) == TUI_CLIP_TEXT && strcmp(c.text, "wtext") == 0);
        tui_clip_free(&c);
        puts("ok clipboard: under Wayland a paste reads wl-paste, an image before text");

        tool("xclip", "case \"$*\" in '-selection clipboard -out') printf 'xtext' ;; *) exit 1 ;; esac");
        env(NULL, ":0");
        assert(tui_clipboard_read(&c) == TUI_CLIP_TEXT && strcmp(c.text, "xtext") == 0);
        tui_clip_free(&c);
        tool("xclip", "exit 1");
        tool("xsel", "[ \"$*\" = '--clipboard --output' ] && printf 'stext'");
        assert(tui_clipboard_read(&c) == TUI_CLIP_TEXT && strcmp(c.text, "stext") == 0);
        tui_clip_free(&c);
        env(NULL, NULL);
        assert(tui_clipboard_read(&c) == TUI_CLIP_NONE);
        puts("ok clipboard: under X a paste reads xclip, else xsel; no display reads nothing");
    }
#endif
    { /* the stand-ins and their folders: every file this test wrote */
        static const char *const made[] = {"bin/wl-copy", "bin/wl-paste", "bin/xclip", "bin/xsel", "wl-copy.in",
                                           "xclip.in", "xsel.in", "bin"};
        char p[PATH_MAX + 64];
        for (size_t i = 0; i < sizeof made / sizeof *made; i++) {
            (void)snprintf(p, sizeof p, "%s/%s", dir, made[i]);
            (void)remove(p);
        }
        assert(rmdir(dir) == 0);
    }
    return 0;
}