Debian Codegraph @

src/pygame_sdl2/scrap.pyx

raw
1 # Copyright 2014 Patrick Dawson <[email protected]>
2 #
3 # This software is provided 'as-is', without any express or implied
4 # warranty. In no event will the authors be held liable for any damages
5 # arising from the use of this software.
6 #
7 # Permission is granted to anyone to use this software for any purpose,
8 # including commercial applications, and to alter it and redistribute it
9 # freely, subject to the following restrictions:
10 #
11 # 1. The origin of this software must not be misrepresented; you must not
12 # claim that you wrote the original software. If you use this software
13 # in a product, an acknowledgment in the product documentation would be
14 # appreciated but is not required.
15 # 2. Altered source versions must be plainly marked as such, and must not be
16 # misrepresented as being the original software.
17 # 3. This notice may not be removed or altered from any source distribution.
18
19 from sdl2 cimport *
20
21 from pygame_sdl2.error import error
22 from pygame_sdl2.locals import SCRAP_TEXT
23
24 from pygame_sdl2.compat import bytes_
25
26 try:
27 import emscripten
28 except ImportError:
29 emscripten = None
30
31 def init():
32 pass
33
34 def get(type):
35 cdef char *text = NULL
36 if type == SCRAP_TEXT:
37 text = SDL_GetClipboardText()
38 if text == NULL:
39 raise error()
40 rv = bytes_(text)
41 SDL_free(text)
42 return rv
43 else:
44 raise error("Not implemented.")
45
46 def get_types():
47 return [SCRAP_TEXT]
48
49 def put(type, data):
50 if type != SCRAP_TEXT:
51 raise error("Not implemented.")
52
53 if emscripten is not None:
54 # SDL_SetClipboardText() is not implemented for Web
55 import re
56 text = data.decode('utf-8')
57 script = 'navigator.clipboard.writeText(`%s`)' % (re.sub(r'([\\`$])', r'\\\1', text),)
58 emscripten.run_script(script)
59 return
60
61 data = bytes_(data)
62
63 if SDL_SetClipboardText(data) != 0:
64 raise error()
65
66 def contains(type):
67 if type != SCRAP_TEXT:
68 return False
69
70 return SDL_HasClipboardText() == SDL_TRUE
71
72 def lost():
73 return False
74
75 def set_mode(mode):
76 pass
77
78 _types = """
79 SCRAP_TEXT : int
80 """