Skip to content

[GTK4] Implement Clipboard.getAvailableTypes() #1990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ptziegler
Copy link
Contributor

@ptziegler ptziegler commented Apr 3, 2025

The list of available types is calculated using the GDK4 clipboard and
no longer falls back to the GTK3 bindings.

Note that UTF8_STRING, COMPOUND_TEXT and STRING_ID are not implemented
for GTK4, which is why those types must not be used in the TextTransfer,
regardless of whether X11 or Wayland is used.

Copy link
Contributor

github-actions bot commented Apr 3, 2025

Test Results

   408 files   -   137     408 suites   - 137   19m 4s ⏱️ - 11m 57s
 4 332 tests  -    37   4 322 ✅  -    35  10 💤  -  2  0 ❌ ±0 
12 521 runs   - 4 103  12 424 ✅  - 4 090  97 💤  - 13  0 ❌ ±0 

Results for commit de14b38. ± Comparison against base commit 0fe4a76.

This pull request removes 37 tests.
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicASCII_dollarSign
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicASCII_emptyString
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicASCII_letterA
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicASCII_letters
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicUTF16LE_null
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicUTF16_AsciiLetters
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicUTF16_Asciiletter
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicUTF16_LotsOfLetters
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicUTF16_letter
AllGTKTests org.eclipse.swt.tests.gtk.Test_GtkConverter ‑ test_HeuristicUTF16_letters
…

♻️ This comment has been updated with latest results.

@akurtakov
Copy link
Member

Doesn't this PR practically disable clipboard functionality almost entirely?

@ptziegler
Copy link
Contributor Author

Doesn't this PR practically disable clipboard functionality almost entirely?

Yes. But as mentioned, the clipboard is not implemented for GTK4. This method internally calls getAvailableClipboardTypes() and/or getAvailablePrimaryTypes(), which then calls gtk_clipboard_wait_for_contents().

The last method uses GTK3-only methods which will always cause an UnsatisfiedLinkError.

long gtk_clipboard_wait_for_contents(long clipboard, long target) {
long startTime = System.currentTimeMillis();
String key = "org.eclipse.swt.internal.gtk.dispatchEvent";
Display display = this.display;
display.setData(key, new int[]{GDK.GDK_PROPERTY_NOTIFY, GDK.GDK_SELECTION_CLEAR, GDK.GDK_SELECTION_REQUEST, GDK.GDK_SELECTION_NOTIFY});
long selection_data = GTK3.gtk_clipboard_wait_for_contents(clipboard, target);
display.setData(key, null);
long duration = System.currentTimeMillis() - startTime;
if (selection_data == 0 && duration > 5000) {
// Bug 241957: In case of timeout take clipboard ownership to unblock future calls
ClipboardProxy._getInstance(display).setData(this, new String[] {" "},
new Transfer[] { TextTransfer.getInstance() },
clipboard == GTKCLIPBOARD ? DND.CLIPBOARD : DND.SELECTION_CLIPBOARD);
}
return selection_data;
}

@laeubi
Copy link
Contributor

laeubi commented Apr 4, 2025

But as mentioned, the clipboard is not implemented for GTK4.

I'm not sure if it is relevant but GDK4 seems to have a clipboard class.

https://docs.gtk.org/gdk4/class.Clipboard.html

and this seems to be the method to use for getting the formats:

https://docs.gtk.org/gdk4/method.Clipboard.get_formats.html

so can we sue this or is there a subtile difference between GTK and GDK that makes it impossible?

@akurtakov
Copy link
Member

In Gtk 3.x gdk used to be its own library but in Gtk 4.x gdk library has been merged into main libgtk. @ptziegler Would you look into that? If not give me few days and I'll try to look into it myself.

@ptziegler
Copy link
Contributor Author

The GDK clipboard is already used in some places but the implementation is not yet complete.

static {
GTKCLIPBOARD = GTK.GTK4 ? GDK.gdk_display_get_clipboard(GDK.gdk_display_get_default()) : GTK3.gtk_clipboard_get (GDK.GDK_NONE);
byte[] buffer = Converter.wcsToMbcs("PRIMARY", true);
long primary = GTK.GTK4 ? 0 : GDK.gdk_atom_intern(buffer, false);
GTKPRIMARYCLIPBOARD = GTK.GTK4 ? GDK.gdk_display_get_primary_clipboard(GDK.gdk_display_get_default()) : GTK3.gtk_clipboard_get(primary);
buffer = Converter.wcsToMbcs("TARGETS", true);
TARGET = GTK.GTK4 ? 0 : GDK.gdk_atom_intern(buffer, false);
}

Would you look into that?

I can try to have a look what else is needed to complete this port, but I currently have to deal with allergy & stuff, so no promises...

@ptziegler ptziegler force-pushed the gtk4-disable-clipboard branch from 0a99081 to 7b1c4eb Compare April 4, 2025 18:01
@ptziegler ptziegler changed the title [GTK4] Disable Clipboard.getAvailableTypes() [GTK4] Implement Clipboard.getAvailableTypes() Apr 4, 2025
@ptziegler
Copy link
Contributor Author

ptziegler commented Apr 4, 2025

I've updated the PR to instead implement the GTK4 case, rather than disabling it.

Recording.webm

@ptziegler ptziegler force-pushed the gtk4-disable-clipboard branch from 7b1c4eb to a5c0906 Compare April 4, 2025 18:04
@ptziegler ptziegler added the gtk4 GTK4 issues label Apr 4, 2025
@ptziegler
Copy link
Contributor Author

As a side note: I've tested this together with #1984 and quite often the copy & paste actions in the popup menu don't work and the following error is logged.

(SWT:140252): Gtk-WARNING **: 20:10:05.026: Broken accounting of active state for widget 0x72becc95af10(GtkPopoverMenu)

This seems to be a GTK4 bug: https://gitlab.gnome.org/GNOME/gtk/-/issues/4563

@ptziegler ptziegler force-pushed the gtk4-disable-clipboard branch 4 times, most recently from 5327d80 to 8c637f5 Compare April 4, 2025 18:32
@akurtakov akurtakov force-pushed the gtk4-disable-clipboard branch from 8c637f5 to cede0b5 Compare April 7, 2025 07:14
The list of available types is calculated using the GDK4 clipboard and
no longer falls back to the GTK3 bindings.

Note that UTF8_STRING, COMPOUND_TEXT and STRING_ID are not implemented
for GTK4, which is why those types must not be used in the TextTransfer,
regardless of whether X11 or Wayland is used.
@ptziegler ptziegler force-pushed the gtk4-disable-clipboard branch from cede0b5 to de14b38 Compare April 7, 2025 15:34
@akurtakov
Copy link
Member

For some reason the Linux GHA seems to switched to on run GTK4. As it's not due to this change and it actually fixes a crash I am ignoring it and merging.

@akurtakov akurtakov merged commit 84b505e into eclipse-platform:master Apr 8, 2025
16 of 17 checks passed
@laeubi
Copy link
Contributor

laeubi commented Apr 8, 2025

For some reason the Linux GHA seems to switched to on run GTK4.

This is because the GTK4 label was added to this PR!

Jenkins will still compile/run on GTK3.

@ptziegler ptziegler deleted the gtk4-disable-clipboard branch April 8, 2025 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gtk4 GTK4 issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants