Recent

Author Topic: How to access the Linux clipboard without unit Clipbrd?  (Read 1429 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
How to access the Linux clipboard without unit Clipbrd?
« on: August 06, 2019, 05:27:29 pm »
I want to access the Linux clipboard (only for plain text) from a small console program. I cannot use unit Clipbrd, because it does not work in a console program, because of using the LCL (on Windows this works perfectly). More see https://forum.lazarus.freepascal.org/index.php/topic,46259.0.html

So my idea is (inspired by reply #4 of the above link) the following program:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses gtk2,gdk2;
  4.  
  5. var c: PGtkClipboard;
  6.     s: string;
  7.  
  8. begin
  9. writeln('AAA');
  10.    c:=gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
  11. writeln('BBB');
  12.    s:='Hello world';
  13.    gtk_clipboard_set_text(c, PChar(s), Length(s));
  14. writeln('CCC');
  15.    gtk_clipboard_store(c);
  16. writeln('DDD');
  17. end.

It compiles but gives the following runtime errors:

Code: [Select]
AAA
(process:2761): Gtk-CRITICAL **: 17:09:15.766: IA__gtk_clipboard_get_for_display: assertion 'display != NULL' failed
BBB
(process:2761): Gtk-CRITICAL **: 17:09:15.767: IA__gtk_clipboard_set_text: assertion 'clipboard != NULL' failed
CCC
(process:2761): Gtk-CRITICAL **: 17:09:15.767: IA__gtk_clipboard_store: assertion 'GTK_IS_CLIPBOARD (clipboard)' failed
DDD

Probably some initialization is missing. I tried to look into the code but failed to understand it.

Does anyone know, how to modify this code that it works? I use Lazarus 1.8.4 with FPC 3.0.4 on Ubuntu 18.04 (64 bit). Thanks in advance.

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: How to access the Linux clipboard without unit Clipbrd?
« Reply #1 on: August 06, 2019, 06:56:07 pm »
Error disappeared when I added
Code: Pascal  [Select][+][-]
  1. gtk_init(@argc, @argv);

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to access the Linux clipboard without unit Clipbrd?
« Reply #2 on: August 06, 2019, 07:26:30 pm »
Hello bytebites, yes, the errors disappeared now. Thanks a lot. But I expected, after running my program, that the clipboard contains "Hello world". But it still contains that, what was in the clipboard, before I started my program. Is there some kind of "finalization" missing?

Do you or somebody else know, how my code must be modified to work? I want to write something into the clipboard.

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: How to access the Linux clipboard without unit Clipbrd?
« Reply #3 on: August 06, 2019, 07:42:20 pm »
Add readln at the end of the code and see what happens.
When the program terminates, pasted text disappears.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to access the Linux clipboard without unit Clipbrd?
« Reply #4 on: August 06, 2019, 08:04:06 pm »
I placed a readln() at the end of my program. While the program is waiting for the readln(), the clipboard is empty, does not contain "Hello world". After I press ENTER, the clipboard again contains that text, what was in the clipboard, before I started my program.

 

TinyPortal © 2005-2018