2019-11-17 05:40:03 +03:00
|
|
|
// Currently there is only X11 Selections support and no way to handle Wayland
|
|
|
|
// but since Wayland isn't extremely adopted, we are covering almost all Linux distros.
|
2020-12-25 20:21:42 +03:00
|
|
|
module x11
|
2019-11-17 05:40:03 +03:00
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import time
|
|
|
|
import sync
|
|
|
|
import math
|
2019-11-17 05:40:03 +03:00
|
|
|
|
|
|
|
#flag -lX11
|
2020-12-31 12:34:46 +03:00
|
|
|
#flag freebsd -I/usr/local/include
|
|
|
|
#flag freebsd -L/usr/local/lib -lX11
|
2021-01-19 21:47:02 +03:00
|
|
|
#include <X11/Xlib.h> # Please install a package with the X11 development headers, for example: `apt-get install libx11-dev`
|
2019-11-17 05:40:03 +03:00
|
|
|
// X11
|
2020-05-18 22:38:06 +03:00
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
struct C.Display {
|
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
struct C.Atom {
|
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
struct C.Window {
|
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
2019-11-17 05:40:03 +03:00
|
|
|
fn C.XInitThreads() int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XCloseDisplay(d &C.Display)
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XFlush(d &C.Display)
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XDestroyWindow(d &C.Display, w C.Window)
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XNextEvent(d &C.Display, e &C.XEvent)
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XSetSelectionOwner(d &C.Display, a C.Atom, w C.Window, time int)
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XGetSelectionOwner(d &C.Display, a C.Atom) C.Window
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XChangeProperty(d &C.Display, requestor C.Window, property C.Atom, typ C.Atom, format int, mode int, data voidptr, nelements int) int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XSendEvent(d &C.Display, requestor C.Window, propogate int, mask i64, event &C.XEvent)
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XInternAtom(d &C.Display, typ byteptr, only_if_exists int) C.Atom
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XCreateSimpleWindow(d &C.Display, root C.Window, x int, y int, width u32, height u32, border_width u32, border u64, background u64) C.Window
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2020-05-03 17:39:26 +03:00
|
|
|
fn C.XOpenDisplay(name byteptr) &C.Display
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XConvertSelection(d &C.Display, selection C.Atom, target C.Atom, property C.Atom, requestor C.Window, time int) int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XSync(d &C.Display, discard int) int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XGetWindowProperty(d &C.Display, w C.Window, property C.Atom, offset i64, length i64, delete int, req_type C.Atom, actual_type_return &C.Atom, actual_format_return &int, nitems &u64, bytes_after_return &u64, prop_return &byteptr) int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XDeleteProperty(d &C.Display, w C.Window, property C.Atom) int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.DefaultScreen(display &C.Display) int
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.RootWindow(display &C.Display, screen_number int) C.Window
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.BlackPixel(display &C.Display, screen_number int) u32
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.WhitePixel(display &C.Display, screen_number int) u32
|
2021-01-19 21:47:02 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.XFree(data voidptr)
|
2019-11-24 13:16:02 +03:00
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
fn todo_del() {}
|
2020-05-06 15:00:50 +03:00
|
|
|
|
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
struct C.XSelectionRequestEvent {
|
|
|
|
mut:
|
|
|
|
display &C.Display // Display the event was read from
|
|
|
|
owner C.Window
|
2020-05-03 17:39:26 +03:00
|
|
|
requestor C.Window
|
2020-05-18 22:38:06 +03:00
|
|
|
selection C.Atom
|
2021-01-19 21:47:02 +03:00
|
|
|
target C.Atom
|
|
|
|
property C.Atom
|
|
|
|
time int
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
struct C.XSelectionEvent {
|
|
|
|
mut:
|
|
|
|
@type int
|
|
|
|
display &C.Display // Display the event was read from
|
2020-05-03 17:39:26 +03:00
|
|
|
requestor C.Window
|
2020-05-18 22:38:06 +03:00
|
|
|
selection C.Atom
|
2021-01-19 21:47:02 +03:00
|
|
|
target C.Atom
|
|
|
|
property C.Atom
|
|
|
|
time int
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
struct C.XSelectionClearEvent {
|
|
|
|
mut:
|
|
|
|
window C.Window
|
2020-04-21 10:24:23 +03:00
|
|
|
selection C.Atom
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
2020-05-06 15:00:50 +03:00
|
|
|
[typedef]
|
2019-11-17 05:40:03 +03:00
|
|
|
struct C.XDestroyWindowEvent {
|
2021-01-19 21:47:02 +03:00
|
|
|
mut:
|
2020-05-03 17:39:26 +03:00
|
|
|
window C.Window
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2020-05-06 14:44:08 +03:00
|
|
|
|
|
|
|
[typedef]
|
2021-01-19 21:47:02 +03:00
|
|
|
union C.XEvent {
|
|
|
|
mut:
|
|
|
|
@type int
|
|
|
|
xdestroywindow C.XDestroyWindowEvent
|
|
|
|
xselectionclear C.XSelectionClearEvent
|
2020-04-21 10:24:23 +03:00
|
|
|
xselectionrequest C.XSelectionRequestEvent
|
2021-01-19 21:47:02 +03:00
|
|
|
xselection C.XSelectionEvent
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-01-19 21:47:02 +03:00
|
|
|
atom_names = ['TARGETS', 'CLIPBOARD', 'PRIMARY', 'SECONDARY', 'TEXT', 'UTF8_STRING', 'text/plain',
|
|
|
|
'text/html',
|
|
|
|
]
|
2019-11-17 05:40:03 +03:00
|
|
|
)
|
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
// UNSUPPORTED TYPES: MULTIPLE, INCR, TIMESTAMP, image/bmp, image/jpeg, image/tiff, image/png
|
2019-11-17 05:40:03 +03:00
|
|
|
// all the atom types we need
|
|
|
|
// currently we only support text
|
|
|
|
// in the future, maybe we can extend this
|
|
|
|
// to support other mime types
|
2020-04-21 10:24:23 +03:00
|
|
|
enum AtomType {
|
2021-01-19 21:47:02 +03:00
|
|
|
xa_atom = 0 // value 4
|
|
|
|
xa_string = 1 // value 31
|
2020-04-23 02:16:16 +03:00
|
|
|
targets = 2
|
|
|
|
clipboard = 3
|
|
|
|
primary = 4
|
|
|
|
secondary = 5
|
|
|
|
text = 6
|
2019-11-17 05:40:03 +03:00
|
|
|
utf8_string = 7
|
2020-04-23 02:16:16 +03:00
|
|
|
text_plain = 8
|
2019-11-17 05:40:03 +03:00
|
|
|
text_html = 9
|
|
|
|
}
|
|
|
|
|
2020-01-09 14:00:39 +03:00
|
|
|
pub struct Clipboard {
|
2020-05-03 17:39:26 +03:00
|
|
|
display &C.Display
|
2021-01-19 21:47:02 +03:00
|
|
|
mut:
|
|
|
|
selection C.Atom // the selection atom
|
|
|
|
window C.Window
|
|
|
|
atoms []C.Atom
|
|
|
|
mutex &sync.Mutex
|
|
|
|
text string // text data sent or received
|
|
|
|
got_text bool // used to confirm that we have got the text
|
|
|
|
is_owner bool // to save selection owner state
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
struct Property {
|
|
|
|
actual_type C.Atom
|
2019-11-17 05:40:03 +03:00
|
|
|
actual_format int
|
2021-01-19 21:47:02 +03:00
|
|
|
nitems u64
|
|
|
|
data byteptr
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
// new_clipboard returns a new `Clipboard` instance allocated on the heap.
|
|
|
|
// The `Clipboard` resources can be released with `free()`
|
2020-12-25 20:21:42 +03:00
|
|
|
pub fn new_clipboard() &Clipboard {
|
2019-11-17 05:40:03 +03:00
|
|
|
return new_x11_clipboard(.clipboard)
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
// new_x11_clipboard initializes a new clipboard of the given selection type.
|
|
|
|
// Multiple clipboard instance types can be initialized and used separately.
|
2020-04-21 10:24:23 +03:00
|
|
|
fn new_x11_clipboard(selection AtomType) &Clipboard {
|
2020-04-26 07:39:23 +03:00
|
|
|
if selection !in [.clipboard, .primary, .secondary] {
|
2021-01-19 21:47:02 +03:00
|
|
|
panic('Wrong AtomType. Must be one of .primary, .secondary or .clipboard.')
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2021-01-19 21:47:02 +03:00
|
|
|
// init x11 thread support
|
2020-04-21 10:24:23 +03:00
|
|
|
status := C.XInitThreads()
|
2019-11-17 05:40:03 +03:00
|
|
|
if status == 0 {
|
2021-01-19 21:47:02 +03:00
|
|
|
println('WARN: this system does not support threads; clipboard will cause the program to lock.')
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
display := new_display()
|
|
|
|
|
|
|
|
if display == C.NULL {
|
2021-01-19 21:47:02 +03:00
|
|
|
println('ERROR: No X Server running. Clipboard cannot be used.')
|
|
|
|
return &Clipboard{
|
|
|
|
display: 0
|
|
|
|
mutex: sync.new_mutex()
|
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mut cb := &Clipboard{
|
|
|
|
display: display
|
|
|
|
window: create_xwindow(display)
|
|
|
|
mutex: sync.new_mutex()
|
|
|
|
}
|
|
|
|
cb.intern_atoms()
|
|
|
|
cb.selection = cb.get_atom(selection)
|
|
|
|
// start the listener on another thread or
|
|
|
|
// we will be locked and will have to hard exit
|
|
|
|
go cb.start_listener()
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (cb &Clipboard) check_availability() bool {
|
2019-11-17 05:40:03 +03:00
|
|
|
return cb.display != C.NULL
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) free() {
|
2020-04-21 10:24:23 +03:00
|
|
|
C.XDestroyWindow(cb.display, cb.window)
|
2020-05-03 17:39:26 +03:00
|
|
|
cb.window = C.Window(C.None)
|
2021-01-19 21:47:02 +03:00
|
|
|
// FIX ME: program hangs when closing display
|
|
|
|
// XCloseDisplay(cb.display)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) clear() {
|
2021-01-30 17:23:55 +03:00
|
|
|
cb.mutex.@lock()
|
2020-05-03 17:39:26 +03:00
|
|
|
C.XSetSelectionOwner(cb.display, cb.selection, C.Window(C.None), C.CurrentTime)
|
2020-04-21 10:24:23 +03:00
|
|
|
C.XFlush(cb.display)
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.is_owner = false
|
2021-01-19 21:47:02 +03:00
|
|
|
cb.text = ''
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.mutex.unlock()
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (cb &Clipboard) has_ownership() bool {
|
2019-11-17 05:40:03 +03:00
|
|
|
return cb.is_owner
|
|
|
|
}
|
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
fn (cb &Clipboard) take_ownership() {
|
2020-04-21 10:24:23 +03:00
|
|
|
C.XSetSelectionOwner(cb.display, cb.selection, cb.window, C.CurrentTime)
|
|
|
|
C.XFlush(cb.display)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
// set_text stores `text` in the system clipboard.
|
2020-12-25 20:21:42 +03:00
|
|
|
pub fn (mut cb Clipboard) set_text(text string) bool {
|
2021-01-19 21:47:02 +03:00
|
|
|
if cb.window == C.Window(C.None) {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-30 17:23:55 +03:00
|
|
|
cb.mutex.@lock()
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.text = text
|
|
|
|
cb.is_owner = true
|
|
|
|
cb.take_ownership()
|
2020-04-21 10:24:23 +03:00
|
|
|
C.XFlush(cb.display)
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.mutex.unlock()
|
|
|
|
// sleep a little bit
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(1 * time.millisecond)
|
2019-11-17 05:40:03 +03:00
|
|
|
return cb.is_owner
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:49:40 +03:00
|
|
|
pub fn (mut cb Clipboard) get_text() string {
|
2021-01-19 21:47:02 +03:00
|
|
|
if cb.window == C.Window(C.None) {
|
|
|
|
return ''
|
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
if cb.is_owner {
|
|
|
|
return cb.text
|
|
|
|
}
|
|
|
|
cb.got_text = false
|
2019-12-07 15:13:23 +03:00
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
// Request a list of possible conversions, if we're pasting.
|
|
|
|
C.XConvertSelection(cb.display, cb.selection, cb.get_atom(.targets), cb.selection,
|
|
|
|
cb.window, C.CurrentTime)
|
2019-12-07 15:13:23 +03:00
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
// wait for the text to arrive
|
2019-11-17 05:40:03 +03:00
|
|
|
mut retries := 5
|
|
|
|
for {
|
2021-01-19 21:47:02 +03:00
|
|
|
if cb.got_text || retries == 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(50 * time.millisecond)
|
2019-11-17 05:40:03 +03:00
|
|
|
retries--
|
|
|
|
}
|
|
|
|
return cb.text
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
// transmit_selection is crucial to handling all the different data types.
|
|
|
|
// If we ever support other mimetypes they should be handled here.
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut cb Clipboard) transmit_selection(xse &C.XSelectionEvent) bool {
|
2019-11-17 05:40:03 +03:00
|
|
|
if xse.target == cb.get_atom(.targets) {
|
|
|
|
targets := cb.get_supported_targets()
|
2021-01-19 21:47:02 +03:00
|
|
|
C.XChangeProperty(xse.display, xse.requestor, xse.property, cb.get_atom(.xa_atom),
|
|
|
|
32, C.PropModeReplace, targets.data, targets.len)
|
|
|
|
} else if cb.is_supported_target(xse.target) && cb.is_owner && cb.text != '' {
|
2021-01-30 17:23:55 +03:00
|
|
|
cb.mutex.@lock()
|
2021-01-19 21:47:02 +03:00
|
|
|
C.XChangeProperty(xse.display, xse.requestor, xse.property, xse.target, 8, C.PropModeReplace,
|
|
|
|
cb.text.str, cb.text.len)
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.mutex.unlock()
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
fn (mut cb Clipboard) start_listener() {
|
2020-04-21 10:24:23 +03:00
|
|
|
event := C.XEvent{}
|
2019-11-17 05:40:03 +03:00
|
|
|
mut sent_request := false
|
2020-04-21 10:24:23 +03:00
|
|
|
mut to_be_requested := C.Atom(0)
|
2019-11-17 05:40:03 +03:00
|
|
|
for {
|
2020-04-21 10:24:23 +03:00
|
|
|
C.XNextEvent(cb.display, &event)
|
2021-01-19 21:47:02 +03:00
|
|
|
if unsafe { event.@type == 0 } {
|
|
|
|
println('error')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
match unsafe { event.@type } {
|
2019-11-17 05:40:03 +03:00
|
|
|
C.DestroyNotify {
|
2021-01-09 12:37:40 +03:00
|
|
|
if unsafe { event.xdestroywindow.window == cb.window } {
|
2020-10-14 22:51:16 +03:00
|
|
|
// we are done
|
2021-01-19 21:47:02 +03:00
|
|
|
return
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
C.SelectionClear {
|
2021-01-21 14:45:59 +03:00
|
|
|
if unsafe { event.xselectionclear.window == cb.window } && unsafe {
|
|
|
|
event.xselectionclear.selection == cb.selection
|
2021-02-21 18:05:03 +03:00
|
|
|
} {
|
2021-01-30 17:23:55 +03:00
|
|
|
cb.mutex.@lock()
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.is_owner = false
|
2021-01-19 21:47:02 +03:00
|
|
|
cb.text = ''
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.mutex.unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.SelectionRequest {
|
2021-01-19 21:47:02 +03:00
|
|
|
if unsafe { event.xselectionrequest.selection == cb.selection } {
|
|
|
|
mut xsre := &C.XSelectionRequestEvent{
|
|
|
|
display: 0
|
|
|
|
}
|
2021-01-09 12:37:40 +03:00
|
|
|
xsre = unsafe { &event.xselectionrequest }
|
2019-12-07 15:13:23 +03:00
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
mut xse := C.XSelectionEvent{
|
2019-11-17 05:40:03 +03:00
|
|
|
@type: C.SelectionNotify // 31
|
|
|
|
display: xsre.display
|
|
|
|
requestor: xsre.requestor
|
|
|
|
selection: xsre.selection
|
|
|
|
time: xsre.time
|
|
|
|
target: xsre.target
|
|
|
|
property: xsre.property
|
|
|
|
}
|
|
|
|
if !cb.transmit_selection(&xse) {
|
|
|
|
xse.property = new_atom(C.None)
|
|
|
|
}
|
2020-05-18 22:38:06 +03:00
|
|
|
C.XSendEvent(cb.display, xse.requestor, 0, C.PropertyChangeMask, voidptr(&xse))
|
2020-04-21 10:24:23 +03:00
|
|
|
C.XFlush(cb.display)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
C.SelectionNotify {
|
2021-01-21 14:45:59 +03:00
|
|
|
if unsafe {
|
2021-02-21 18:05:03 +03:00
|
|
|
event.xselection.selection == cb.selection
|
|
|
|
&& event.xselection.property != C.Atom(C.None)
|
|
|
|
} {
|
2021-01-09 12:37:40 +03:00
|
|
|
if unsafe { event.xselection.target == cb.get_atom(.targets) && !sent_request } {
|
2019-11-17 05:40:03 +03:00
|
|
|
sent_request = true
|
|
|
|
prop := read_property(cb.display, cb.window, cb.selection)
|
|
|
|
to_be_requested = cb.pick_target(prop)
|
2020-04-21 10:24:23 +03:00
|
|
|
if to_be_requested != C.Atom(0) {
|
2021-01-19 21:47:02 +03:00
|
|
|
C.XConvertSelection(cb.display, cb.selection, to_be_requested,
|
|
|
|
cb.selection, cb.window, C.CurrentTime)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
2021-01-09 12:37:40 +03:00
|
|
|
} else if unsafe { event.xselection.target == to_be_requested } {
|
2019-11-17 05:40:03 +03:00
|
|
|
sent_request = false
|
2020-04-21 10:24:23 +03:00
|
|
|
to_be_requested = C.Atom(0)
|
2021-01-30 17:23:55 +03:00
|
|
|
cb.mutex.@lock()
|
2021-01-21 14:45:59 +03:00
|
|
|
prop := unsafe {
|
|
|
|
read_property(event.xselection.display, event.xselection.requestor,
|
|
|
|
event.xselection.property)
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.XDeleteProperty(event.xselection.display, event.xselection.requestor,
|
|
|
|
event.xselection.property)
|
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
if cb.is_supported_target(prop.actual_type) {
|
|
|
|
cb.got_text = true
|
2020-08-19 17:06:23 +03:00
|
|
|
unsafe {
|
2021-01-19 21:47:02 +03:00
|
|
|
cb.text = byteptr(prop.data).vstring() // TODO: return byteptr to support other mimetypes
|
2020-08-19 17:06:23 +03:00
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
cb.mutex.unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 13:16:02 +03:00
|
|
|
C.PropertyNotify {}
|
2019-12-07 18:16:19 +03:00
|
|
|
else {}
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
/*
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
// intern_atoms initializes all the atoms we need.
|
2021-01-19 21:47:02 +03:00
|
|
|
fn (mut cb Clipboard) intern_atoms() {
|
|
|
|
cb.atoms << C.Atom(4) // XA_ATOM
|
|
|
|
cb.atoms << C.Atom(31) // XA_STRING
|
2021-02-21 18:05:03 +03:00
|
|
|
for i, name in x11.atom_names {
|
2021-01-19 21:47:02 +03:00
|
|
|
only_if_exists := if i == int(AtomType.utf8_string) { 1 } else { 0 }
|
2020-04-21 10:24:23 +03:00
|
|
|
cb.atoms << C.XInternAtom(cb.display, name.str, only_if_exists)
|
|
|
|
if i == int(AtomType.utf8_string) && cb.atoms[i] == C.Atom(C.None) {
|
2019-11-17 05:40:03 +03:00
|
|
|
cb.atoms[i] = cb.get_atom(.xa_string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:39:26 +03:00
|
|
|
fn read_property(d &C.Display, w C.Window, p C.Atom) Property {
|
2020-04-21 10:24:23 +03:00
|
|
|
actual_type := C.Atom(0)
|
2019-11-17 05:40:03 +03:00
|
|
|
actual_format := 0
|
2020-05-18 22:38:06 +03:00
|
|
|
nitems := u64(0)
|
|
|
|
bytes_after := u64(0)
|
2019-11-17 05:40:03 +03:00
|
|
|
ret := byteptr(0)
|
|
|
|
mut read_bytes := 1024
|
|
|
|
for {
|
2019-11-24 13:53:59 +03:00
|
|
|
if ret != 0 {
|
2019-11-17 05:40:03 +03:00
|
|
|
C.XFree(ret)
|
|
|
|
}
|
2021-03-05 17:41:11 +03:00
|
|
|
C.XGetWindowProperty(d, w, p, 0, read_bytes, 0, C.Atom(C.AnyPropertyType), &actual_type,
|
2021-01-19 21:47:02 +03:00
|
|
|
&actual_format, &nitems, &bytes_after, &ret)
|
2019-11-17 05:40:03 +03:00
|
|
|
read_bytes *= 2
|
2021-01-19 21:47:02 +03:00
|
|
|
if bytes_after == 0 {
|
|
|
|
break
|
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
return Property{actual_type, actual_format, nitems, ret}
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:45:59 +03:00
|
|
|
// pick_target finds the best target given a local copy of a property.
|
2020-04-21 10:24:23 +03:00
|
|
|
fn (cb &Clipboard) pick_target(prop Property) C.Atom {
|
2021-01-19 21:47:02 +03:00
|
|
|
// The list of targets is a list of atoms, so it should have type XA_ATOM
|
|
|
|
// but it may have the type TARGETS instead.
|
2021-02-21 18:05:03 +03:00
|
|
|
if (prop.actual_type != cb.get_atom(.xa_atom) && prop.actual_type != cb.get_atom(.targets))
|
|
|
|
|| prop.actual_format != 32 {
|
2021-01-19 21:47:02 +03:00
|
|
|
// This would be really broken. Targets have to be an atom list
|
|
|
|
// and applications should support this. Nevertheless, some
|
|
|
|
// seem broken (MATLAB 7, for instance), so ask for STRING
|
|
|
|
// next instead as the lowest common denominator
|
2019-11-17 05:40:03 +03:00
|
|
|
return cb.get_atom(.xa_string)
|
2021-01-19 21:47:02 +03:00
|
|
|
} else {
|
2020-04-21 10:24:23 +03:00
|
|
|
atom_list := &C.Atom(prop.data)
|
2019-12-07 15:13:23 +03:00
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
mut to_be_requested := C.Atom(0)
|
2019-11-17 05:40:03 +03:00
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
// This is higher than the maximum priority.
|
2019-11-17 05:40:03 +03:00
|
|
|
mut priority := math.max_i32
|
2019-12-07 15:13:23 +03:00
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
for i in 0 .. prop.nitems {
|
|
|
|
// See if this data type is allowed and of higher priority (closer to zero)
|
|
|
|
// than the present one.
|
2019-12-07 15:13:23 +03:00
|
|
|
|
2021-01-19 21:47:02 +03:00
|
|
|
target := unsafe { atom_list[i] }
|
2020-07-22 21:42:51 +03:00
|
|
|
if cb.is_supported_target(target) {
|
|
|
|
index := cb.get_target_index(target)
|
|
|
|
if priority > index && index >= 0 {
|
2019-11-17 05:40:03 +03:00
|
|
|
priority = index
|
2020-07-22 21:42:51 +03:00
|
|
|
to_be_requested = target
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return to_be_requested
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
fn (cb &Clipboard) get_atoms(types ...AtomType) []C.Atom {
|
2020-04-26 17:25:54 +03:00
|
|
|
mut atoms := []C.Atom{}
|
2019-11-17 05:40:03 +03:00
|
|
|
for typ in types {
|
2019-12-07 15:13:23 +03:00
|
|
|
atoms << cb.atoms[typ]
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
return atoms
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
fn (cb &Clipboard) get_atom(typ AtomType) C.Atom {
|
2019-11-17 05:40:03 +03:00
|
|
|
return cb.atoms[typ]
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
fn (cb &Clipboard) is_supported_target(target C.Atom) bool {
|
2019-11-17 05:40:03 +03:00
|
|
|
return cb.get_target_index(target) >= 0
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
fn (cb &Clipboard) get_target_index(target C.Atom) int {
|
2019-11-17 05:40:03 +03:00
|
|
|
for i, atom in cb.get_supported_targets() {
|
2021-01-19 21:47:02 +03:00
|
|
|
if atom == target {
|
|
|
|
return i
|
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
return -1
|
2019-11-24 13:16:02 +03:00
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
fn (cb &Clipboard) get_supported_targets() []C.Atom {
|
|
|
|
return cb.get_atoms(AtomType.utf8_string, .xa_string, .text, .text_plain, .text_html)
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2020-04-21 10:24:23 +03:00
|
|
|
fn new_atom(value int) &C.Atom {
|
2021-01-19 21:47:02 +03:00
|
|
|
return unsafe { &C.Atom(value) }
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:39:26 +03:00
|
|
|
fn create_xwindow(display &C.Display) C.Window {
|
2020-04-21 10:24:23 +03:00
|
|
|
n := C.DefaultScreen(display)
|
2021-01-19 21:47:02 +03:00
|
|
|
return C.XCreateSimpleWindow(display, C.RootWindow(display, n), 0, 0, 1, 1, 0, C.BlackPixel(display,
|
|
|
|
n), C.WhitePixel(display, n))
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2020-05-03 17:39:26 +03:00
|
|
|
fn new_display() &C.Display {
|
2020-04-21 10:24:23 +03:00
|
|
|
return C.XOpenDisplay(C.NULL)
|
2019-11-17 05:40:03 +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 {
|
|
|
|
return new_x11_clipboard(.primary)
|
|
|
|
}
|