2019-11-17 05:40:03 +03:00
|
|
|
module clipboard
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
struct WndClassEx {
|
|
|
|
cbSize u32
|
|
|
|
style u32
|
2019-11-17 05:40:03 +03:00
|
|
|
lpfnWndProc voidptr
|
2020-04-04 16:37:13 +03:00
|
|
|
cbClsExtra int
|
|
|
|
cbWndExtra int
|
|
|
|
hInstance C.HINSTANCE
|
|
|
|
hIcon C.HICON
|
|
|
|
hCursor C.HCURSOR
|
|
|
|
hbrBackground C.HBRUSH
|
|
|
|
lpszMenuName &u16 // LPCWSTR
|
2019-11-17 05:40:03 +03:00
|
|
|
lpszClassName &u16
|
2020-04-04 16:37:13 +03:00
|
|
|
hIconSm &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
|
|
|
|
fn C.GetClipboardOwner() &C.HWND
|
|
|
|
fn C.CreateWindowEx(dwExStyle i64, lpClassName &u16, lpWindowName &u16, dwStyle i64, x int, y int, nWidth int, nHeight int, hWndParent i64, hMenu voidptr, hInstance voidptr, lpParam voidptr) &C.HWND
|
2019-11-24 15:29:11 +03:00
|
|
|
//fn C.MultiByteToWideChar(CodePage u32, dwFlags u16, lpMultiByteStr byteptr, cbMultiByte int, lpWideCharStr u16, cchWideChar int) int
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.EmptyClipboard()
|
|
|
|
fn C.CloseClipboard()
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.GlobalAlloc(uFlag u32, size i64) C.HGLOBAL
|
|
|
|
fn C.GlobalFree(buf C.HGLOBAL)
|
|
|
|
fn C.GlobalLock(buf C.HGLOBAL)
|
|
|
|
fn C.GlobalUnlock(buf C.HGLOBAL)
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.SetClipboardData(uFormat u32, data voidptr) C.HANDLE
|
|
|
|
fn C.GetClipboardData(uFormat u32) C.HANDLE
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.DefWindowProc(hwnd C.HWND, msg u32, wParam C.WPARAM, lParam C.LPARAM) C.LRESULT
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.SetLastError(error i64)
|
2020-04-04 16:37:13 +03:00
|
|
|
fn C.OpenClipboard(hwnd C.HWND) int
|
|
|
|
fn C.DestroyWindow(hwnd C.HWND)
|
2019-11-17 05:40:03 +03:00
|
|
|
|
2020-04-04 16:37:13 +03:00
|
|
|
struct Clipboard {
|
2019-11-17 05:40:03 +03:00
|
|
|
max_retries int
|
|
|
|
retry_delay int
|
|
|
|
mut:
|
2020-04-04 16:37:13 +03:00
|
|
|
hwnd C.HWND
|
2019-12-07 15:51:00 +03:00
|
|
|
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-04-04 16:37:13 +03:00
|
|
|
last_error = C.GetLastError()
|
|
|
|
if C.OpenClipboard(cb.hwnd) > 0 {
|
2019-11-17 05:40:03 +03:00
|
|
|
return true
|
|
|
|
} else if last_error != u32(C.ERROR_ACCESS_DENIED) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
time.sleep(cb.retry_delay)
|
|
|
|
}
|
2020-04-04 16:37:13 +03:00
|
|
|
C.SetLastError(last_error)
|
2019-11-17 05:40:03 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_clipboard() &Clipboard {
|
|
|
|
mut cb := &Clipboard {
|
|
|
|
max_retries: 5
|
|
|
|
retry_delay: 5
|
|
|
|
}
|
2020-04-04 16:37:13 +03:00
|
|
|
class_name := "clipboard"
|
|
|
|
wndclass := WndClassEx {
|
|
|
|
cbSize: sizeof(WndClassEx)
|
|
|
|
lpfnWndProc: voidptr(&C.DefWindowProc)
|
|
|
|
lpszClassName: class_name.to_wide()
|
|
|
|
lpszMenuName: 0
|
|
|
|
hIconSm: 0
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2020-04-04 16:37:13 +03:00
|
|
|
if C.RegisterClassEx(&wndclass) == 0 && C.GetLastError() != u32(C.ERROR_CLASS_ALREADY_EXISTS) {
|
2019-11-17 05:40:03 +03:00
|
|
|
println("Failed registering class.")
|
|
|
|
}
|
2020-04-04 16:37:13 +03:00
|
|
|
hwnd := C.CreateWindowEx(0, wndclass.lpszClassName, wndclass.lpszClassName, 0, 0, 0, 0, 0, C.HWND_MESSAGE, C.NULL, C.NULL, C.NULL)
|
2019-11-17 05:40:03 +03:00
|
|
|
if hwnd == C.NULL {
|
|
|
|
println("Error creating window!")
|
|
|
|
}
|
|
|
|
cb.hwnd = hwnd
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) check_availability() bool {
|
2020-04-04 16:37:13 +03:00
|
|
|
return cb.hwnd != C.HWND(C.NULL)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) has_ownership() bool {
|
2020-04-04 16:37:13 +03:00
|
|
|
return C.GetClipboardOwner() == cb.hwnd
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2019-12-07 15:51:00 +03:00
|
|
|
fn (cb mut Clipboard) clear() {
|
2019-11-17 05:40:03 +03:00
|
|
|
if !cb.get_clipboard_lock() {return}
|
2020-04-04 16:37:13 +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
|
|
|
}
|
|
|
|
|
2019-12-07 15:51:00 +03:00
|
|
|
fn (cb mut Clipboard) free(){
|
2020-04-04 16:37:13 +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
|
2020-04-04 16:37:13 +03:00
|
|
|
fn to_wide(text string) &C.HGLOBAL {
|
|
|
|
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, sizeof(u16) * len_required)
|
|
|
|
if buf != C.HGLOBAL(C.NULL) {
|
|
|
|
mut locked := &u16(C.GlobalLock(buf))
|
|
|
|
C.MultiByteToWideChar(C.CP_UTF8, C.MB_ERR_INVALID_CHARS, text.str, text.len + 1, locked, len_required)
|
2019-11-17 05:40:03 +03:00
|
|
|
locked[len_required - 1] = u16(0)
|
2020-04-04 16:37:13 +03:00
|
|
|
C.GlobalUnlock(buf)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2019-12-07 15:51:00 +03:00
|
|
|
fn (cb mut Clipboard) set_text(text string) bool {
|
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
buf := to_wide(text)
|
|
|
|
if !cb.get_clipboard_lock() {
|
2020-04-04 16:37:13 +03:00
|
|
|
C.GlobalFree(buf)
|
2019-11-17 05:40:03 +03:00
|
|
|
return false
|
|
|
|
} else {
|
2019-11-24 13:22:57 +03:00
|
|
|
// EmptyClipboard must be called to properly update clipboard ownership
|
2020-04-04 16:37:13 +03:00
|
|
|
C.EmptyClipboard()
|
|
|
|
if C.SetClipboardData(C.CF_UNICODETEXT, buf) == C.HANDLE(C.NULL) {
|
2019-11-17 05:40:03 +03:00
|
|
|
println("SetClipboardData: Failed.")
|
2020-04-04 16:37:13 +03:00
|
|
|
C.CloseClipboard()
|
|
|
|
C.GlobalFree(buf)
|
2019-11-17 05:40:03 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 13:22:57 +03:00
|
|
|
// CloseClipboard appears to change the sequence number...
|
2020-04-04 16:37:13 +03:00
|
|
|
C.CloseClipboard()
|
2019-11-17 05:40:03 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-07 15:51:00 +03:00
|
|
|
fn (cb mut Clipboard) get_text() string {
|
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
if !cb.get_clipboard_lock() {
|
|
|
|
return ""
|
|
|
|
}
|
2020-04-04 16:37:13 +03:00
|
|
|
h_data := C.GetClipboardData(C.CF_UNICODETEXT)
|
|
|
|
if h_data == C.HANDLE(C.NULL) {
|
|
|
|
C.CloseClipboard()
|
2019-11-17 05:40:03 +03:00
|
|
|
return ""
|
|
|
|
}
|
2020-04-04 16:37:13 +03:00
|
|
|
str := string_from_wide(&u16(C.GlobalLock(h_data)))
|
|
|
|
C.GlobalUnlock(h_data)
|
2019-11-17 05:40:03 +03:00
|
|
|
return str
|
2019-11-18 13:10:31 +03:00
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
|
|
|
|
pub fn new_primary() &Clipboard {
|
|
|
|
panic('Primary clipboard is not supported on non-Linux systems.')
|
|
|
|
}
|