-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2602 from m32/master
python clipboard - wx winport version
- Loading branch information
Showing
4 changed files
with
245 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import gi | ||
|
||
gi.require_version("Gtk", "3.0") | ||
from gi.repository import Gtk, Gdk | ||
|
||
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD) | ||
for mime in ( | ||
"text/plain", | ||
"text/plain;charset=utf-8", | ||
"text/html", | ||
"text/uri-list", | ||
"image/png", | ||
'x-special/gnome-copied-files', | ||
'SAVE_TARGETS', | ||
'TIMESTAMP', | ||
'TARGETS', | ||
): | ||
text = clipboard.wait_for_contents(Gdk.Atom.intern(mime, True)) | ||
if text: | ||
print(mime, text.get_data()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
import sys | ||
import logging | ||
|
||
import ctypes as ct | ||
import far2lc | ||
from far2l.plugin import PluginBase | ||
from yfar import FarPlugin | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
class Plugin(FarPlugin): | ||
label = "Python Clip GET" | ||
openFrom = ["PLUGINSMENU", 'FILEPANEL'] | ||
|
||
def CopyFiles(self, data): | ||
pwd = self.get_panel().directory | ||
data = data.decode('utf8') | ||
log.debug(f'copyfiles: {data}') | ||
prefix = 'file://' | ||
for uri in data.split('\n'): | ||
if uri[:len(prefix)] != prefix: | ||
continue | ||
fqname = uri[len(prefix):] | ||
fname = fqname.split('/')[-1] | ||
dqname = os.path.join(pwd, fname) | ||
log.debug(f'CopyFile: {fqname} -> {dqname}') | ||
with open(dqname, 'wb') as fo: | ||
with open(fqname, 'rb') as fi: | ||
rec = fi.read(4096) | ||
if not rec: | ||
break | ||
fo.write(rec) | ||
|
||
def OpenPlugin(self, OpenFrom): | ||
winport = self.ffi.cast("struct WINPORTDECL *", far2lc.WINPORT()) | ||
clipurifmt = winport.RegisterClipboardFormat("text/uri-list") | ||
if not clipurifmt: | ||
log.error('uclipset.ClipboardRegisterFormat.1') | ||
return | ||
clipgnofmt = winport.RegisterClipboardFormat("x-special/gnome-copied-files") | ||
if not clipgnofmt: | ||
log.error('uclipset.ClipboardRegisterFormat.2') | ||
return | ||
if not winport.OpenClipboard(self.ffi.NULL): | ||
log.error('uclipset.OpenClipboard') | ||
return | ||
try: | ||
data = winport.GetClipboardData(clipurifmt) | ||
if data is not None: | ||
nb = winport.ClipboardSize(data) | ||
result = self.ffi.buffer(data, nb-1) | ||
self.CopyFiles(bytes(result)) | ||
else: | ||
data = winport.GetClipboardData(clipgnofmt) | ||
if data is not None: | ||
nb = winport.ClipboardSize(data) | ||
result = self.ffi.buffer(data, nb-1) | ||
self.CopyFiles(bytes(result)) | ||
except: | ||
log.exception('uclipset.GetClipboardData') | ||
finally: | ||
if not winport.CloseClipboard(): | ||
log.error('uclipset.CloseClipboard') | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import sys | ||
import logging | ||
|
||
import ctypes as ct | ||
import far2lc | ||
from far2l.plugin import PluginBase | ||
from yfar import FarPlugin | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
CAH_ALLOCATED_MAGIC = 0x0610ba10A110CED0 | ||
CAH_FREED_MAGIC = 0x0610ba10F4EED000 | ||
class ClipboardAllocHeader(ct.Structure): | ||
_fields_ = [ | ||
('size', ct.c_uint32), | ||
('padding', ct.c_uint32), | ||
('magic', ct.c_uint64), | ||
] | ||
|
||
class Plugin(FarPlugin): | ||
label = "Python Clip SET" | ||
openFrom = ["PLUGINSMENU", 'FILEPANEL'] | ||
|
||
def SetClipboard(self, winport, fmt, fqname): | ||
fqname = fqname.encode('utf8') | ||
#log.debug(f'clip.{fmt} set: {fqname} {len(fqname)}') | ||
ptr = winport.ClipboardAlloc(len(fqname)) | ||
if ptr is None: | ||
log.debug('clipboard alloc failed') | ||
else: | ||
if 0: | ||
m = int(self.ffi.cast('DWORD64', ptr)) | ||
log.debug(f'ptr={ptr} m={m:x}') | ||
m -= ct.sizeof(ClipboardAllocHeader) | ||
log.debug(f'm={m:x}') | ||
mp = ClipboardAllocHeader.from_address(m) | ||
log.debug(f'mp.size={mp.size} mp.padding={mp.padding} mp.magic={mp.magic:x} CAH_ALLOCATED_MAGIC={CAH_ALLOCATED_MAGIC:x}') | ||
self.ffi.memmove(ptr, fqname, len(fqname)) | ||
res = winport.SetClipboardData(fmt, ptr) | ||
|
||
def OpenPlugin(self, OpenFrom): | ||
winport = self.ffi.cast("struct WINPORTDECL *", far2lc.WINPORT()) | ||
clipurifmt = winport.RegisterClipboardFormat("text/uri-list") | ||
if not clipurifmt: | ||
log.error('uclipset.ClipboardRegisterFormat.1') | ||
return | ||
clipgnofmt = winport.RegisterClipboardFormat("x-special/gnome-copied-files") | ||
if not clipgnofmt: | ||
log.error('uclipset.ClipboardRegisterFormat.2') | ||
return | ||
if not winport.OpenClipboard(self.ffi.NULL): | ||
log.error('uclipset.OpenClipboard') | ||
return | ||
try: | ||
files = [] | ||
panel = self.get_panel() | ||
for f in panel.selected: | ||
fqname = f.full_file_name | ||
files.append("file://"+fqname) | ||
#self.SetClipboard(winport, 1, fqname) | ||
self.SetClipboard(winport, clipurifmt, "\n".join(files)) | ||
self.SetClipboard(winport, clipgnofmt, "copy\n"+"\n".join(files)) | ||
except: | ||
log.exception('uclipset.SetClipboard') | ||
finally: | ||
if not winport.CloseClipboard(): | ||
log.error('uclipset.CloseClipboard') | ||
return | ||
# typedef BOOL (*WINPORT_IsClipboardFormatAvailable) (UINT format); | ||
# typedef PVOID (*WINPORT_GetClipboardData) (UINT format); | ||
# typedef SIZE_T (*WINPORT_ClipboardSize) (PVOID mem); |