Recent

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

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
I have the following console program:

Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses Interfaces, // needs package LCL
  4.      clipbrd;    // needs package LCLbase and unit Interfaces
  5.  
  6. begin
  7. writeln('"', Clipboard.AsText, '"');
  8. Clipboard.AsText:='Hello world';
  9. writeln('"', Clipboard.AsText, '"');
  10. end.  
On Windows 7 (32 bit) it works as expected:
 - it shows the text which was in the clipboard before the program was started
 - then it shows "Hello World"
 - after this the clipboard contains "Hello World"
 
On Linux (Ubuntu 18.04 64 bit) I have the following behaviour:
 - it shows "" instead of the text which was in the clipboard before the program was started
 - then it shows "Hello World"
 - after this the contents of the clipboard have not been changed
 
An example:
 - I select "some text" in the Lazarus text editor and copy it to the clipboard
 - after starting my program it shows:
   ""
   "Hello world"
 - after this I paste the clipboard into the Lazarus text editor and get again "some text"
 
I use Lazarus 1.8.4 with FPC 3.0.4. What am I doing wrong?
I attached my little project. Thanks in advance.
« Last Edit: August 07, 2019, 08:24:00 pm by Hartmut »

dbannon

  • Hero Member
  • *****
  • Posts: 677
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #1 on: August 01, 2019, 10:48:58 am »
Hmm, your problem sounds a bit like -

https://wiki.freepascal.org/Clipboard#How_to_fix_empty_GTK2_clipboard_on_exit

When you use lazarus to copy something into the clipboard, do you then exit Lazarus or just switch to another window ?

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

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #2 on: August 01, 2019, 11:08:27 am »
When you use lazarus to copy something into the clipboard, do you then exit Lazarus or just switch to another window ?
I did not exit Lazarus, just switched to a console window and back. But it makes no difference, if I exit Lazarus and start it again after running my program from a console window, the result is the same.

Your link sounds very interesting. I will work through it and report then. But as far as I saw, it would only solve 1 of my 2 problems: passing something from my program to the clipboard, not vice versa.

Thanks for your help.

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #3 on: August 01, 2019, 11:43:32 am »
Unfortunately unit 'fix_gtk_clipboard' did not help in my case. There is no difference in the behaviour of my program with this unit. Now it is:

Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses Interfaces, // needs package LCL
  4.      fix_gtk_clipboard,
  5.      clipbrd;    // needs package LCLbase and unit Interfaces
  6.  
  7. begin
  8. writeln('"', Clipboard.AsText, '"');
  9. Clipboard.AsText:='Hello world';
  10. writeln('"', Clipboard.AsText, '"');
  11. end.
       
In the link from dbannon there is a second code example dealing with procedure TMainForm.FormClose(). If I understand the describing text correctly, this does not matter in my case, because I have only a console program without a Form. Correct?

dbannon

  • Hero Member
  • *****
  • Posts: 677
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #4 on: August 02, 2019, 02:02:46 am »
Hmm, it does not matter where you call the extra code, in a GUI based app, convienant to call in formClose() but you can call it yourself.

Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2. uses gtk2, gdk2,
  3.   Interfaces, // needs package LCL
  4.      clipbrd;    // needs package LCLbase and unit Interfaces
  5.  
  6. procedure MungeClipboard;
  7. var
  8.   c: PGtkClipboard;
  9.   t: string;
  10. begin
  11.   c := gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
  12.   t := Clipboard.AsText;
  13.   gtk_clipboard_set_text(c, PChar(t), Length(t));
  14.   gtk_clipboard_store(c);
  15. end;
  16.  
  17. begin
  18. writeln('"', Clipboard.AsText, '"');
  19. Clipboard.AsText:='Hello world';
  20. writeln('"', Clipboard.AsText, '"');
  21. MungeClipboard;
  22. end.            

But that only solves half your problem, now, when you app exits, the clipboard does hold "Hello World" but I am afraid it does not correctly pick up what was in the clipboard before your app starts.

I don't have that issue in my GUI app, will have to look through code and see if there is something happening I don't remember. I am currently travelling through central Australia, internet access is very flucky, so am not a reliable help.....

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

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #5 on: August 02, 2019, 05:49:03 pm »
Thank you Davo for your new program. Enjoy your trip to Australia. Unfortunately procedure MungeClipboard() did not help in my case. There is no difference in the behaviour of my program with it.

Then I changed your program into:
Code: Pascal  [Select]
  1. begin
  2. Clipboard.AsText:='Hello world';
  3. readln;
  4. MungeClipboard;
  5. end.
When the program waits for the readln, the clipboard does not contain "Hello world", but contains that, what was in the clipboard, before my program started. So 'Clipboard.AsText' seems not to work in this console program...

I use Lazarus 1.8.4 with FPC 3.0.4 on Ubuntu 18.04.02 (64 bit). As a replacement I installed the KDE-Plasma desktop with "sudo apt install kubuntu-desktop". This is 11 month ago. I never had any problems with the clipboard in none of my Linux programs (gedit, firefox, thunderbird, lazarus, terminal console and many more). Can KDE-Plasma desktop be the reason for this problem?

Can somebody else verify this? Do others have the same clipboard problems in a console program (or not)? Thanks for your help.
« Last Edit: August 02, 2019, 05:51:23 pm by Hartmut »

Thaddy

  • Hero Member
  • *****
  • Posts: 8870
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #6 on: August 02, 2019, 05:58:05 pm »
Sorry never use KDE. But see if my previous posting helps, or wait for xiniman to reply: he is perfectly capable to interpret what I mean.
Most people that want to use threading should learn to patch their jeans first: use a needle.

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #7 on: August 02, 2019, 06:31:23 pm »
I created a small GUI program (attached) to test 'Clipboard.AsText' there:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.  Clipbrd;
  10.  
  11. type
  12.  TForm1 = class(TForm)
  13.   Button1: TButton;
  14.   procedure Button1Click(Sender: TObject);
  15.  private
  16.  public
  17.  end;
  18.  
  19. var Form1: TForm1;
  20.  
  21. implementation
  22.  
  23. {$R *.lfm}
  24.  
  25. procedure TForm1.Button1Click(Sender: TObject);
  26.    begin
  27.    Clipboard.AsText:='Hello world';
  28.    end;
  29.  
  30. end.  
It works perfectly on Windows and Ubuntu. As soon as you press the button, the clipboard contains "Hello world" and keeps it after the program has terminated. So the problem seems not to be my KDE-Plasma Desktop, but my small console program.

What must I change that it works? Thanks for any help.

Thaddy

  • Hero Member
  • *****
  • Posts: 8870
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #8 on: August 02, 2019, 06:37:23 pm »
It is not the matter that it works. It works. But eventually you run out of resources, because memory is leaking somewhere..
That is the bug!

Repeat your code 1000 times and look at your memory use.
Most people that want to use threading should learn to patch their jeans first: use a needle.

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #9 on: August 02, 2019, 07:32:16 pm »
Hello Thaddy, I modified my program to:
Code: Pascal  [Select]
  1. begin
  2. for n:=1 to 1000 do
  3.    Clipboard.AsText:='Hello world';
  4. readln;
  5. end.
When the program waits for readln, on Windows the memory usage is 1072 kb. On Linux I had to do some research (I'm a beginner on Linux) how to check the used memory. KSysGuard system monitor shows 2704 KB memory plus 20 MB common memory.

top -p <PID> shows:
Code: Text  [Select]
  1.   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     ZEIT+ BEFEHL                
  2.  2024 hg6       20   0  172588  23180  20476 S   0,0  0,3   0:00.02 test4      
   
For me 0.3% MEM does not look like a resources problem. What do you say?

wp

  • Hero Member
  • *****
  • Posts: 6225
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #10 on: August 02, 2019, 07:36:31 pm »
So the problem seems not to be my KDE-Plasma Desktop, but my small console program.
Could it be that the clipboard system is not correctly initialized for a console program on Linux? Unit clipbrd belongs to the LCL, and a console program normally does not require the LCL.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Thaddy

  • Hero Member
  • *****
  • Posts: 8870
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #11 on: August 02, 2019, 07:53:09 pm »
Did you make sure you did release trhe clipboard in a finally section?
pseudo code
Code: Pascal  [Select]
  1. var:Clipboard:TClipBoard;
  2. begin
  3.    ClipBoard := TClipBoard.create(nil);
  4.   try
  5.      // work with the clipboard
  6.   finally
  7.     Clipboard.free;
  8.   end;
  9. end.

In a console program you usually program like that. Dunno if it helps.
« Last Edit: August 02, 2019, 07:54:56 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #12 on: August 02, 2019, 07:54:18 pm »
Could it be that the clipboard system is not correctly initialized for a console program on Linux?
I can imagine that very well...

Unit clipbrd belongs to the LCL, and a console program normally does not require the LCL.
Does this mean, a console program on Linux cannot access the clipboard? That would be a pity. On Windows it works perfectly...

Thanks for your help.

Thaddy

  • Hero Member
  • *****
  • Posts: 8870
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #13 on: August 02, 2019, 07:58:43 pm »
Try my tip. Our posts crossed. If it does not work I have another possible solution.
Most people that want to use threading should learn to patch their jeans first: use a needle.

Hartmut

  • Sr. Member
  • ****
  • Posts: 277
Re: Clipboard (with plain text) does not work as expected on Linux
« Reply #14 on: August 02, 2019, 08:26:56 pm »
Hello Thaddy, your suggestion sounds a very good idea. But unfortunately I have no change in the behaviour. This is my program:
Code: Pascal  [Select]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses Interfaces, // needs package LCL
  4.      clipbrd,    // needs package LCLbase and unit Interfaces
  5.      LCLType;    // for 'ctPrimarySelection'
  6.  
  7. var Clipboard: TClipBoard;
  8.  
  9. begin
  10. ClipBoard:=TClipBoard.create(ctPrimarySelection);
  11. try
  12.    Clipboard.AsText:='Hello world';
  13. finally
  14.    Clipboard.free;
  15. end;
  16. readln;
  17. end.

I had to change your TClipBoard.create() command because it did not compile. After some research I tried ctPrimarySelection, ctSecondarySelection and ctClipboard. But the behaviour is always the same, the content of the clipboard is not changed to "Hello world". You said you have more ideas? Thanks for your help.