Recent

Author Topic: Clipboard (with plain text) does not work as expected on Linux (SOLVED)  (Read 1542 times)

dbannon

  • Hero Member
  • *****
  • Posts: 759
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #15 on: August 03, 2019, 02:39:34 am »
Did you make sure you did release trhe clipboard in a finally section?

I don't have access to any coding right now but pretty sure including clipbrd both creates and frees the clipboard.  Not from memory, the Primary* one though.

Sounds to me, like WP suggested, that as clipBrd, sort of belongs in LCL its dependent on some initialization that happens in a real GUI app. Strange than including LCL is not enough ....

That sample I posted back up this thread did set the system clipboard to whatever the app set it to (ie, solved half your problem).   I'm using Lazarus Fixes, 2.0.3, I doubt thats an issue but maybe ?  Other wise, I don't see how you got a different answer to me. Your use of 1.8.4 is probably not a great idea....

Davo

Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Thaddy

  • Hero Member
  • *****
  • Posts: 9193
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #16 on: August 03, 2019, 08:05:18 am »
Just to be sure: It can be that the clipboard is already created in an initialization section and released in a finalization section.
You can also install a different clipboard manager and see if that helps (not the component, but the underlying X-server.
See https://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/
« Last Edit: August 03, 2019, 08:08:13 am by Thaddy »
also related to equus asinus.

Hartmut

  • Sr. Member
  • ****
  • Posts: 280
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #17 on: August 03, 2019, 03:49:40 pm »
Hello Davo and Thaddy, thanks for your replies. First I followed Thaddy's link in this way:
 - installed the different clipboard manager by "sudo apt-get install xclip"
 - run my program from reply #14
 - displayed the clipboard contents with "xclip -o"
 - unfortunately there was no difference then before: when my program was waiting for the readln and when my program had finished, both the clipboard did not contain "Hello world", but contains that, what was in the clipboard, before my program started :-(

Then I expanded my program by calling procedure MungeClipboard() from reply #4 after "Clipboard.AsText:='Hello world'", but no difference.

For me the problem seems to be, that the LCL, which is used by unit Clipbrd, needs more initialization, which is missing in a console program on Linux (on Windows it works perfectly). Is this a bug? What do you think?

dbannon

  • Hero Member
  • *****
  • Posts: 759
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #18 on: August 04, 2019, 01:37:29 am »
Yes Hartmut, sounds like a bug to me !

I just tried you trick of using xclip, after running my version of your app, xclip -o says clipboard is empty, pressing ,shift-ctrl-v gives me a "hello world".   How about that ?

That says to me its something to do with how the data in the clipboard is being described. The clipboard can hold data in a range of formats, an app asks the clipboard for a list of those formats and picks one, if it cannot find a format it likes, it spits out nothing.   This comment applies to the 'post app' phenomena, not why we see nothing in there as your app starts up.

At startup, we have to assume LCL is doing something to get the clipboard contents and make it available, your console version is not doing that and that is, apparently, a bug.  Might take a bit of work finding out what that something is :-(

I have some code somewhere I used to debug another aspect of clipboard on linux, when I get a chance I'll see if I still have it. Useful to describe the clipboard contents.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

lucamar

  • Hero Member
  • *****
  • Posts: 2086
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #19 on: August 04, 2019, 02:26:40 am »
I just tried you trick of using xclip, after running my version of your app, xclip -o says clipboard is empty, pressing ,shift-ctrl-v gives me a "hello world".   How about that ?

There is no "Linux clipboard"; in Linux he clipboard is a function of the Window Manager and is available only to GUI (X) applications.

When you press Shift+Ctrl+V you're invo¡king a, let's call it, method of the terminal emulation application which, in turn, sents the appropiate"keys" to the console app to "paste" the clipboard contents. Conversely, when you press Shift+Ctrl+C the terminal emulator copies from its "screen buffer" to the clipboard. Think of it like a Lazarus program handling TProcess's in/out pipes.

IIRC, it's possible to connect a terminal application running under an X session to the session's clipboard, though I don't remember how. What I do kind of remember is that the process was a rather convoluted one and not apt for the faint of heart. :)

A googling for "console clipboard Linux" should turn back some answers. Or look into the source of xclip to see how it works (incidentally, the fact that this utility exists should tell you something ;)).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

dbannon

  • Hero Member
  • *****
  • Posts: 759
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #20 on: August 04, 2019, 11:33:40 am »
... There is no "Linux clipboard"; in Linux he clipboard is a function of the Window Manager and is available only to GUI (X) applications.

OK lucamar thats technically true. But our friend, and most desktop linux users are using a terminal session under some Desktop and most expect std copy and paste to work. Most terminal apps have a menu item Edit->paste etc. Personally, I prefer the traditional primary selection and paste model but I am an traditionalist !   I agree that a unix box booted to only command line would not be expected to support copy and paste but it would not support xclip either.

Hmm.... my version of vim does not support the 'system' clipboard. Thats surprises me but as I said, I use primary selection. But I am pretty sure I remember having used a version of vi that would paste the system (ie GTK) clipboard. So a fpc app that does not 'import' (for lack of a better word) the Desktop clipboard has a bug IMHO.

Hartmut - its looking like yours is a hard problem !  Should you consider making it a gui app that does not actually show its main window ?  As Lucamar pointed out, thats probably not going to work in a pure terminal session, even if you ssh into a box for example. But it will work in any likely terminal emulator session.

Davo

Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Abelisto

  • Jr. Member
  • **
  • Posts: 82
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #21 on: August 04, 2019, 12:12:02 pm »
@Hartmut

You need to move the call of MungeClipboard just after clipboard assigning to initialize GTK clipboard by assigned value immediately:
Code: Pascal  [Select]
  1. ...
  2. Clipboard.AsText:='Hello world';
  3. MungeClipboard;
  4. ...
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Hartmut

  • Sr. Member
  • ****
  • Posts: 280
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #22 on: August 04, 2019, 06:33:57 pm »
I just tried you trick of using xclip, after running my version of your app, xclip -o says clipboard is empty, pressing ,shift-ctrl-v gives me a "hello world".   How about that ?
Sounds strange. I always tested with having some pure text in the clipboard, before I started my program, to see if it is changed or not.

That says to me its something to do with how the data in the clipboard is being described. The clipboard can hold data in a range of formats, an app asks the clipboard for a list of those formats and picks one, if it cannot find a format it likes, it spits out nothing.   
To avoid that problems, I always had some pure text in the clipboard, before I started my program.

There is no "Linux clipboard"; in Linux he clipboard is a function of the Window Manager and is available only to GUI (X) applications.
How does 'xclip' access the clipboard? For me xclip does not look like a GUI application, or am I wrong?

A googling for "console clipboard Linux" should turn back some answers.
I tried and got lots of "matches", but the only one I found, how to do it in Free Pascal, was:

Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses Interfaces, Forms, Clipbrd;    
  4.  
  5. begin
  6. Application.Initialize;
  7. Clipboard.AsText:='Hello world';
  8. end.  

But unfortunately it did not work (it made no difference).

Should you consider making it a gui app that does not actually show its main window? 
Hmm, sounds interesting, but GUI programs normally are big, and I wanted to use the clipboard in some small console applications...

@Abelisto: I tested your suggestion, unfortunately it did not work. Does it work on your installation? If yes, please write your OS-Version and Lazarus/FPC Version.

So my main questions are:
1) Can anybody else access the clipboard (pure text) in a console program on a Linux system? Which version of Linux / Lazarus / FPC do you use?
2) Is it a bug, that it does not work? (Davo tends to yes)
3) Is there a workaround to add the missing "initialization" manually?

Thanks to all for your help!
« Last Edit: August 04, 2019, 06:36:55 pm by Hartmut »

dbannon

  • Hero Member
  • *****
  • Posts: 759
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #23 on: August 05, 2019, 02:40:53 am »
[gui app] Hmm, sounds interesting, but GUI programs normally are big, and I wanted to use the clipboard in some small console applications...

Well, once striped of debugging, a basic gui app is about 4meg. And grows quite slowly as you add things to it.

While I do agree its wrong you 'have' to do this, its worth noting that the the clipbrd unit is an LCL unit not a fpc unit.  That might well be key to the argument ....

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Hartmut

  • Sr. Member
  • ****
  • Posts: 280
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #24 on: August 06, 2019, 05:22:16 pm »
Hello Davo, congratulations to your 600th posting. You are right, a minimal GUI application would be a solution, if I don't find a better one. I have an idea for that from your MungeClipboard() procedure. I opened a new topic for that in https://forum.lazarus.freepascal.org/index.php/topic,46308.0.html

Thanks a lot to all who have helped me!
« Last Edit: August 06, 2019, 05:29:51 pm by Hartmut »

Abelisto

  • Jr. Member
  • **
  • Posts: 82
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #25 on: August 06, 2019, 07:33:17 pm »
Note that there is no such thing as "clipboard" in Linux console (classic) applications. Some applications supports so-called "buffers" (vim, tmux etc) but it is application-wide, not system-wide feature. Windows-like clipboard was introduced in X Window System (as I remember) so it is the part of the GUI extension of the classic (console-based) *nix. And it is supported by terminal emulators, not by console applications itself.

So be ready for problems with "clipboard" if you will to use some remote access (SSH for example) ore even virtual terminals (Ctrl+Alt+Fn) on your local device.
« Last Edit: August 06, 2019, 07:34:54 pm by Abelisto »
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Hartmut

  • Sr. Member
  • ****
  • Posts: 280
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #26 on: August 06, 2019, 08:02:20 pm »
Hello Abelisto, thanks for your reply. In general I don't need remote access (SSH for example) or virtual terminals (Ctrl+Alt+Fn). But if I understand you correctly, then accessing the clipboard in a console program is not possible on Linux. I already opened another topic for this (see link in reply #24). If this brings no success, I will create a minimal GUI application, even though I wanted a smaller program.

Abelisto

  • Jr. Member
  • **
  • Posts: 82
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #27 on: August 07, 2019, 09:15:01 am »
Hope that it is helpful.

There is xsel utility allows to manipulate by x-server clipboards (in terminal type man xsel)

The simple example how to save some text in the x clipboard:

Code: Bash  [Select]
  1. echo -n "Hello World!" | xsel --clipboard

After that you able to paste "Hello World!" everywhere in your x-based OS.

Surely you can to explore the xsel source code to learn more about x window clipboards :)
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Thaddy

  • Hero Member
  • *****
  • Posts: 9193
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #28 on: August 07, 2019, 09:21:40 am »
... There is no "Linux clipboard"; in Linux he clipboard is a function of the Window Manager and is available only to GUI (X) applications.

OK lucamar thats technically true.

Well. No. It is actually very easy to have a text-based clipboard. A small Daemon and a file. Loads of example code.
also related to equus asinus.

Hartmut

  • Sr. Member
  • ****
  • Posts: 280
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #29 on: August 07, 2019, 08:23:18 pm »
Thanks to Abelisto and Thaddy for your replies.

@Thaddy: This sounds very interesting. I tried to find something but with the very small information you provided I was not successful.

@Abelisto: 'xsel' seems to be a good solution for my current program, because this must not run on foreign computers, where xsel is not installed.

Here are some informations about xsel:
 - it's not part of Ubuntu, you must install it via "sudo apt install xsel"
 - it can process German special characters like Ä Ö Ü ä ö ü ß
 - it can process LineFeeds via e.g. echo -n -e "Hello \n World" | xsel -b
 - if your text can contain characters like " $ \ (and probably some more) and you want them to pass without changes, you must escape them by "\"
 - to send the clipboard to STDOUT you can use command "xsel -b"
Probably tool 'xclip' (see link in Thaddy's reply #16) can do the same as 'xsel'.

Puhh, I have a solution :-)) Thanks a lot to all who helped me!