About Store Forum Documentation Contact



Post Reply 
Linux clipboard copy not working
Author Message
Fex Offline
Gold Supporter

Post: #1
Linux clipboard copy not working
On Ubuntu 22.04, copying text from the Editor to the clipboard doesn't seem to work for me reliably.

Changing this section in Misc.cpp

Code:
#elif LINUX
   // TODO: this text will disappear once the application gets closed
   if(XDisplay && App.window() && UTF8_STRING)
   {
      Atom FIND_ATOM(CLIPBOARD);
      Str8 utf=UTF8(text);
      int  ok=XChangeProperty(XDisplay, DefaultRootWindow(XDisplay), XA_CUT_BUFFER0, UTF8_STRING, 8, PropModeReplace, (const unsigned char*)utf(), utf.length());
      if(CLIPBOARD && App.window()!=XGetSelectionOwner(XDisplay, CLIPBOARD ))XSetSelectionOwner(XDisplay, CLIPBOARD , App.window(), CurrentTime);
      if(             App.window()!=XGetSelectionOwner(XDisplay, XA_PRIMARY))XSetSelectionOwner(XDisplay, XA_PRIMARY, App.window(), CurrentTime);
      return ok==1;
   }
   return false;

to

Code:
#elif LINUX
   Str8 command = S8 + "echo -n \"" + text + "\" | xclip -selection clipboard";
   int result = system(command);
   return (result == 0);

And installing xclip:

Code:
sudo apt-get install xclip

Seems to have made it work.

I don't know if doing this has any other side effects.
04-25-2024 03:01 PM
Find all posts by this user Quote this message in a reply
Fex Offline
Gold Supporter

Post: #2
RE: Linux clipboard copy not working
Sadly this seems to struggle with quotes, so I'll have to find something else
04-26-2024 02:19 AM
Find all posts by this user Quote this message in a reply
Fex Offline
Gold Supporter

Post: #3
RE: Linux clipboard copy not working
Adding this function above ClipSet
Code:
/******************************************************************************/
Str8 escape_for_shell(const Str8& input) {
    Str8 output;
    for (int i = 0; i < input.length(); i++) {
        Char8 c = input[i];
        switch (c) {
            case '"': output += "\\\""; break;  // Escape double quote
            case '\\': output += "\\\\"; break;  // Escape backslash
            default: output += c;
        }
    }
    return output;
}
/******************************************************************************/
Bool ClipSet(C Str &text)


and changing the block in ClipSet to:

Code:
#elif LINUX
    Str8 safeText = escape_for_shell(text);
    Str8 command = S + "echo -n \"" + safeText + "\" | xclip -selection clipboard";
    int result = system(command());
    return (result == 0);

Makes double quotes work.

Still fails to copy:

Code:
const Char hexDigits[17] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '\0' };

Instead it copies:

Code:
const Char hexDigits[17] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '�' };

But it is an improvement and might be good enough.
04-27-2024 07:32 PM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #4
RE: Linux clipboard copy not working
I'd rather not rely on a solution that requires installing some extra packages:
"sudo apt-get install xclip"
04-28-2024 04:44 AM
Find all posts by this user Quote this message in a reply
Post Reply