2019-11-17 05:40:03 +03:00
|
|
|
module clipboard
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2021-01-03 23:30:35 +03:00
|
|
|
#include <windows.h>
|
2020-06-19 13:54:56 +03:00
|
|
|
#flag -lUser32
|
2021-02-15 21:09:02 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
struct WndClassEx {
|
2020-05-16 17:12:23 +03:00
|
|
|
cb_size u32
|
|
|
|
style u32
|
|
|
|
lpfn_wnd_proc voidptr
|
|
|
|
cb_cls_extra int
|
|
|
|
cb_wnd_extra int
|
|
|
|
h_instance C.HINSTANCE
|
|
|
|
h_icon C.HICON
|
|
|
|
h_cursor C.HCURSOR
|
|
|
|
hbr_background C.HBRUSH
|
|
|
|
lpsz_menu_name &u16 // LPCWSTR
|
2020-11-03 01:00:29 +03:00
|
|
|
lpsz_class_name &u16
|
2020-05-16 17:12:23 +03:00
|
|
|
h_icon_sm &u16
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2019-11-24 13:22:57 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.RegisterClassEx(class WndClassEx) int
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.GetClipboardOwner() &C.HWND
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-11-03 01:00:29 +03:00
|
|
|
fn C.CreateWindowEx(dwExStyle i64, lpClassName &u16, lpWindowName &u16, dwStyle i64, x int, y int, nWidth int, nHeight int, hWndParent i64, hMenu voidptr, h_instance voidptr, lpParam voidptr) &C.HWND
|
2020-05-16 17:12:23 +03:00
|
|
|
|
|
|
|
// fn C.MultiByteToWideChar(CodePage u32, dw_flags u16, lpMultiByteStr byteptr, cbMultiByte int, lpWideCharStr u16, cchWideChar int) int
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.EmptyClipboard()
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.CloseClipboard()
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.GlobalAlloc(uFlag u32, size i64) C.HGLOBAL
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.GlobalFree(buf C.HGLOBAL)
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.GlobalLock(buf C.HGLOBAL)
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.GlobalUnlock(buf C.HGLOBAL)
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.SetClipboardData(uFormat u32, data voidptr) C.HANDLE
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.GetClipboardData(uFormat u32) C.HANDLE
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.DefWindowProc(hwnd C.HWND, msg u32, wParam C.WPARAM, lParam C.LPARAM) C.LRESULT
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.SetLastError(error i64)
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.OpenClipboard(hwnd C.HWND) int
|
2020-05-16 17:12:23 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.DestroyWindow(hwnd C.HWND)
|
2019-11-17 05:40:03 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
struct Clipboard {
|
2020-11-03 01:00:29 +03:00
|
|
|
max_retries int
|
|
|
|
retry_delay int
|
|
|
|
mut:
|
2021-01-21 14:45:59 +03:00
|
|
|
hwnd C.HWND
|
|
|
|
foo int // TODO remove
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) get_clipboard_lock() bool {
|
|
|
|
mut retries := cb.max_retries
|
|
|
|
mut last_error := u32(0)
|
|
|
|
for {
|
|
|
|
retries--
|
|
|
|
if retries < 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-11-03 01:00:29 +03:00
|
|
|
last_error = C.GetLastError()
|
|
|
|
if C.OpenClipboard(cb.hwnd) > 0 {
|
|
|
|
return true
|
|
|
|
} else if last_error != u32(C.ERROR_ACCESS_DENIED) {
|
|
|
|
return false
|
|
|
|
}
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(cb.retry_delay * time.second)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2020-11-03 01:00:29 +03:00
|
|
|
C.SetLastError(last_error)
|
|
|
|
return false
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_clipboard() &Clipboard {
|
2020-05-16 17:12:23 +03:00
|
|
|
mut cb := &Clipboard{
|
2020-11-03 01:00:29 +03:00
|
|
|
max_retries: 5
|
|
|
|
retry_delay: 5
|
|
|
|
}
|
2020-05-16 17:12:23 +03:00
|
|
|
class_name := 'clipboard'
|
|
|
|
wndclass := WndClassEx{
|
2020-11-03 01:00:29 +03:00
|
|
|
cb_size: sizeof(WndClassEx)
|
|
|
|
lpfn_wnd_proc: voidptr(&C.DefWindowProc)
|
|
|
|
lpsz_class_name: class_name.to_wide()
|
2020-05-16 17:12:23 +03:00
|
|
|
lpsz_menu_name: 0
|
|
|
|
h_icon_sm: 0
|
2020-11-03 01:00:29 +03:00
|
|
|
}
|
|
|
|
if C.RegisterClassEx(&wndclass) == 0 && C.GetLastError() != u32(C.ERROR_CLASS_ALREADY_EXISTS) {
|
2020-05-16 17:12:23 +03:00
|
|
|
println('Failed registering class.')
|
2020-11-03 01:00:29 +03:00
|
|
|
}
|
|
|
|
hwnd := C.CreateWindowEx(0, wndclass.lpsz_class_name, wndclass.lpsz_class_name, 0,
|
|
|
|
0, 0, 0, 0, C.HWND_MESSAGE, C.NULL, C.NULL, C.NULL)
|
|
|
|
if hwnd == C.NULL {
|
2020-05-16 17:12:23 +03:00
|
|
|
println('Error creating window!')
|
2020-11-03 01:00:29 +03:00
|
|
|
}
|
|
|
|
cb.hwnd = hwnd
|
|
|
|
return cb
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (cb &Clipboard) check_availability() bool {
|
2021-01-04 10:57:31 +03:00
|
|
|
return cb.hwnd != C.HWND(C.NULL)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (cb &Clipboard) has_ownership() bool {
|
2021-01-04 10:57:31 +03:00
|
|
|
return C.GetClipboardOwner() == cb.hwnd
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) clear() {
|
2020-05-16 17:12:23 +03:00
|
|
|
if !cb.get_clipboard_lock() {
|
|
|
|
return
|
|
|
|
}
|
2020-11-03 01:00:29 +03:00
|
|
|
C.EmptyClipboard()
|
|
|
|
C.CloseClipboard()
|
2019-12-07 15:51:00 +03:00
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) free() {
|
2020-11-03 01:00:29 +03:00
|
|
|
C.DestroyWindow(cb.hwnd)
|
2019-12-07 15:51:00 +03:00
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// the string.to_wide doesn't work with SetClipboardData, don't know why
|
2021-01-01 12:07:58 +03:00
|
|
|
fn to_wide(text string) C.HGLOBAL {
|
2020-11-03 01:00:29 +03:00
|
|
|
len_required := C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str,
|
|
|
|
text.len + 1, C.NULL, 0)
|
|
|
|
buf := C.GlobalAlloc(C.GMEM_MOVEABLE, i64(sizeof(u16)) * len_required)
|
2021-01-04 10:57:31 +03:00
|
|
|
if buf != C.HGLOBAL(C.NULL) {
|
2020-11-03 01:00:29 +03:00
|
|
|
mut locked := &u16(C.GlobalLock(buf))
|
|
|
|
C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str, text.len + 1,
|
|
|
|
locked, len_required)
|
|
|
|
unsafe {
|
|
|
|
locked[len_required - 1] = u16(0)
|
|
|
|
}
|
|
|
|
C.GlobalUnlock(buf)
|
|
|
|
}
|
|
|
|
return buf
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) set_text(text string) bool {
|
2019-12-07 15:51:00 +03:00
|
|
|
cb.foo = 0
|
2020-11-03 01:00:29 +03:00
|
|
|
buf := to_wide(text)
|
|
|
|
if !cb.get_clipboard_lock() {
|
|
|
|
C.GlobalFree(buf)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
// EmptyClipboard must be called to properly update clipboard ownership
|
|
|
|
C.EmptyClipboard()
|
2021-01-04 10:57:31 +03:00
|
|
|
if C.SetClipboardData(C.CF_UNICODETEXT, buf) == C.HANDLE(C.NULL) {
|
2020-05-16 17:12:23 +03:00
|
|
|
println('SetClipboardData: Failed.')
|
2020-11-03 01:00:29 +03:00
|
|
|
C.CloseClipboard()
|
|
|
|
C.GlobalFree(buf)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// CloseClipboard appears to change the sequence number...
|
|
|
|
C.CloseClipboard()
|
|
|
|
return true
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) get_text() string {
|
2019-12-07 15:51:00 +03:00
|
|
|
cb.foo = 0
|
2020-11-03 01:00:29 +03:00
|
|
|
if !cb.get_clipboard_lock() {
|
2020-05-16 17:12:23 +03:00
|
|
|
return ''
|
2020-11-03 01:00:29 +03:00
|
|
|
}
|
|
|
|
h_data := C.GetClipboardData(C.CF_UNICODETEXT)
|
2021-01-04 10:57:31 +03:00
|
|
|
if h_data == C.HANDLE(C.NULL) {
|
2020-11-03 01:00:29 +03:00
|
|
|
C.CloseClipboard()
|
2020-05-16 17:12:23 +03:00
|
|
|
return ''
|
2020-11-03 01:00:29 +03:00
|
|
|
}
|
2021-02-15 21:09:02 +03:00
|
|
|
str := unsafe { string_from_wide(&u16(C.GlobalLock(h_data))) }
|
2020-11-03 01:00:29 +03:00
|
|
|
C.GlobalUnlock(h_data)
|
|
|
|
return str
|
2019-11-18 13:10:31 +03:00
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
// new_primary returns a new X11 `PRIMARY` type `Clipboard` instance allocated on the heap.
|
|
|
|
// Please note: new_primary only works on X11 based systems.
|
2019-12-27 19:59:04 +03:00
|
|
|
pub fn new_primary() &Clipboard {
|
|
|
|
panic('Primary clipboard is not supported on non-Linux systems.')
|
|
|
|
}
|