user3595342
user3595342

Reputation:

Access Clipboard Data programmatically with C on Linux platform

I have tried looking for ways to access Linux clipboard (Access and modify clipboard) but there is no clear solution to the problem. I have seen these post, 1, 2and tried to look for solution, all I could find are either Windows solution or OSX solution for this problem. Is there a formal way to solve this problem? Thank you very much.

Upvotes: 3

Views: 1107

Answers (2)

MichaelDL
MichaelDL

Reputation: 65

void runproc(char *output, char *proc) {
    FILE *fp;
    fp = popen(proc, "r");
    if (fp == NULL) {
        ERRMSG(-1, true, "runproc failure");
    }
    fgets(output, 4096, fp);
    pclose(fp);
}

int cbcopy(char *text) {
    /* copies text to the system clipboard
    usint xclip command */
    char cmd[10240]; // 10K
    int rc = 0;
    sprintf(cmd, "echo \"%s\" | xclip -selection clipboard", text);
    rc = system(cmd);
    return rc;
}

char *cbpaste(char *text) {
    /* pastes clipboard into text */
    char cmd[64]; // 10K
    strncpy(cmd, "xclip -o", 10);
    runproc(text, cmd);
    chomp(text);
    return text;
}

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

The clipboard in Linux does not work similar to how it works under Windows and OS X. There is no separate storage for it, but rather it is an X selection that one application "owns" and will transfer data for when requested. If you wanted to modify the contents then you would need to request the current selection contents, modify that, and then make it available in your application as the new clipboard.

Upvotes: 3

Related Questions