From 99cfb357153a3b2a069c560f98f472c34fb318df Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 12:45:32 +0300 Subject: [PATCH] clipboard: clipboard not working on windows and doesn't use global on linux (fixes #11338) --- vlib/clipboard/clipboard_windows.c.v | 9 ++++++--- vlib/clipboard/x11/clipboard.c.v | 12 ++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/vlib/clipboard/clipboard_windows.c.v b/vlib/clipboard/clipboard_windows.c.v index 8450d47fd..43eaab736 100644 --- a/vlib/clipboard/clipboard_windows.c.v +++ b/vlib/clipboard/clipboard_windows.c.v @@ -72,10 +72,11 @@ fn (cb &Clipboard) get_clipboard_lock() bool { if retries < 0 { break } - last_error = C.GetLastError() if C.OpenClipboard(cb.hwnd) > 0 { return true - } else if last_error != u32(C.ERROR_ACCESS_DENIED) { + } + last_error = C.GetLastError() + if last_error != u32(C.ERROR_ACCESS_DENIED) { return false } time.sleep(cb.retry_delay * time.second) @@ -186,9 +187,11 @@ pub fn (mut cb Clipboard) get_text() string { if !cb.get_clipboard_lock() { return '' } + defer { + C.CloseClipboard() + } h_data := C.GetClipboardData(C.CF_UNICODETEXT) if h_data == unsafe { nil } { - C.CloseClipboard() return '' } str := unsafe { string_from_wide(&u16(C.GlobalLock(C.HGLOBAL(h_data)))) } diff --git a/vlib/clipboard/x11/clipboard.c.v b/vlib/clipboard/x11/clipboard.c.v index b60c8be0f..ac7f5333a 100644 --- a/vlib/clipboard/x11/clipboard.c.v +++ b/vlib/clipboard/x11/clipboard.c.v @@ -142,6 +142,8 @@ pub mut: const atom_names = ['TARGETS', 'CLIPBOARD', 'PRIMARY', 'SECONDARY', 'TEXT', 'UTF8_STRING', 'text/plain', 'text/html'] +const atom_types = [AtomType.targets, .clipboard, .primary, .secondary, .text, .utf8_string, + .text_plain, .text_html] // UNSUPPORTED TYPES: MULTIPLE, INCR, TIMESTAMP, image/bmp, image/jpeg, image/tiff, image/png // all the atom types we need @@ -413,11 +415,13 @@ fn (mut cb Clipboard) intern_atoms() { cb.atoms << Atom(4) // XA_ATOM cb.atoms << Atom(31) // XA_STRING for i, name in atom_names { - only_if_exists := if i == int(AtomType.utf8_string) { 1 } else { 0 } - cb.atoms << C.XInternAtom(cb.display, &char(name.str), only_if_exists) - if i == int(AtomType.utf8_string) && cb.atoms[i] == Atom(0) { - cb.atoms[i] = cb.get_atom(.xa_string) + atom_type := atom_types[i] + only_if_exists := if atom_type == .utf8_string { 1 } else { 0 } + mut atom := C.XInternAtom(cb.display, &char(name.str), only_if_exists) + if atom_type == .utf8_string && atom == Atom(0) { + atom = cb.get_atom(.xa_string) } + cb.atoms << atom } } -- 2.39.5