Skip to content

Commit 347dbab

Browse files
committed
core:clipboard add missing files for windows and kde target
1 parent d7d0605 commit 347dbab

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Clipboard Dbus: an implementation of the Clipboard using dbus and klipper.
3+
'''
4+
5+
__all__ = ('ClipboardDbusKlipper', )
6+
7+
from kivy.utils import platform
8+
from kivy.core.clipboard import ClipboardBase
9+
10+
if platform != 'linux':
11+
raise SystemError('unsupported platform for dbus kde clipboard')
12+
13+
try:
14+
import dbus
15+
bus = dbus.SessionBus()
16+
proxy = bus.get_object("org.kde.klipper", "/klipper")
17+
except:
18+
raise
19+
20+
21+
class ClipboardDbusKlipper(ClipboardBase):
22+
23+
_is_init = False
24+
25+
def init(self):
26+
if ClipboardDbusKlipper._is_init:
27+
return
28+
self.iface = dbus.Interface(proxy, "org.kde.klipper.klipper")
29+
ClipboardDbusKlipper._is_init = True
30+
31+
def get(self, mimetype='text/plain'):
32+
self.init()
33+
return str(self.iface.getClipboardContents())
34+
35+
def put(self, data, mimetype='text/plain'):
36+
self.init()
37+
self.iface.setClipboardContents(data.replace('\x00', ''))
38+
39+
def get_types(self):
40+
self.init()
41+
return 'text/plain'
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
Clipboard windows: an implementation of the Clipboard using ctypes.
3+
'''
4+
5+
__all__ = ('ClipboardWindows', )
6+
7+
from kivy.utils import platform
8+
from kivy.core.clipboard import ClipboardBase
9+
10+
if platform != 'win':
11+
raise SystemError('unsupported platform for Windows clipboard')
12+
13+
14+
class ClipboardWindows(ClipboardBase):
15+
16+
def get(self, mimetype='text/plain'):
17+
ctypes.windll.user32.OpenClipboard(0)
18+
# 1 is CF_TEXT
19+
pcontents = ctypes.windll.user32.GetClipboardData(1)
20+
data = ctypes.c_char_p(pcontents).value
21+
#ctypes.windll.kernel32.GlobalUnlock(pcontents)
22+
ctypes.windll.user32.CloseClipboard()
23+
return data
24+
25+
def put(self, data, mimetype='text/plain'):
26+
GMEM_DDESHARE = 0x2000
27+
ctypes.windll.user32.OpenClipboard(0)
28+
ctypes.windll.user32.EmptyClipboard()
29+
try:
30+
# works on Python 2 (bytes() only takes one argument)
31+
hCd = ctypes.windll.kernel32.GlobalAlloc(
32+
GMEM_DDESHARE, len(bytes(text)) + 1)
33+
except TypeError:
34+
# works on Python 3 (bytes() requires an encoding)
35+
hCd = ctypes.windll.kernel32.GlobalAlloc(
36+
GMEM_DDESHARE, len(bytes(text, 'ascii')) + 1)
37+
pchData = ctypes.windll.kernel32.GlobalLock(hCd)
38+
try:
39+
# works on Python 2 (bytes() only takes one argument)
40+
ctypes.cdll.msvcrt.strcpy(
41+
ctypes.c_char_p(pchData), bytes(text))
42+
except TypeError:
43+
# works on Python 3 (bytes() requires an encoding)
44+
ctypes.cdll.msvcrt.strcpy(
45+
ctypes.c_char_p(pchData), bytes(text, 'ascii'))
46+
ctypes.windll.kernel32.GlobalUnlock(hCd)
47+
ctypes.windll.user32.SetClipboardData(1, hCd)
48+
ctypes.windll.user32.CloseClipboard()
49+
50+
def get_types(self):
51+
return list('text/plain',)
52+

0 commit comments

Comments
 (0)