Debian Codegraph @

python/caca/display.py

raw
1 # -*- coding: utf-8 -*-
2 #
3 # libcaca Colour ASCII-Art library
4 # Python language bindings
5 # Copyright (c) 2010 Alex Foulon <[email protected]>
6 # All Rights Reserved
7 #
8 # This library is free software. It comes without any warranty, to
9 # the extent permitted by applicable law. You can redistribute it
10 # and/or modify it under the terms of the Do What the Fuck You Want
11 # to Public License, Version 2, as published by Sam Hocevar. See
12 # http://www.wtfpl.net/ for more details.
13 #
14
15 """ Libcaca Python bindings """
16
17 import ctypes
18
19 from caca import _lib, _PYTHON3, _str_to_bytes
20 from caca.canvas import _Canvas, Canvas
21
22
23 class _DisplayStruct(ctypes.Structure):
24 pass
25
26
27 class _Display(object):
28 """ Model for Display objects.
29 """
30 def from_param(self):
31 """ Required by ctypes module to call object as parameter of
32 a C function.
33 """
34 return self._dp
35
36 def __str__(self):
37 return "<CacaDisplay>"
38
39 def __del__(self):
40 if self._dp and _lib is not None:
41 self._free()
42
43 def _free(self):
44 """ Free a libcaca display.
45 """
46 _lib.caca_free_display.argtypes = [_Display]
47 _lib.caca_free_display.restype = ctypes.c_int
48
49 return _lib.caca_free_display(self)
50
51
52 class Display(_Display):
53 """ Display objects, methods are libcaca functions with display_t as first
54 parameter.
55 """
56 def __init__(self, cv, driver=None):
57 """ Display constructor.
58
59 cv -- canvas to attach.
60 driver -- caca driver to set with display
61 """
62
63 if driver is None:
64 _lib.caca_create_display.argtypes = [_Canvas]
65 _lib.caca_create_display.restype = ctypes.POINTER(_DisplayStruct)
66 self._dp = _lib.caca_create_display(cv)
67 else:
68 _lib.caca_create_display_with_driver.argtypes = [
69 _Canvas, ctypes.c_char_p
70 ]
71 _lib.caca_create_display_with_driver.restype = ctypes.POINTER(
72 _DisplayStruct
73 )
74 if _PYTHON3 and isinstance(driver, str):
75 driver = _str_to_bytes(driver)
76
77 self._dp = _lib.caca_create_display_with_driver(cv, driver)
78
79 if self._dp == 0:
80 raise DisplayError("Failed to create display")
81
82 def get_driver(self):
83 """ Return the caca graphical context's current output driver.
84 """
85 _lib.caca_get_display_driver.argtypes = [_Display]
86 _lib.caca_get_display_driver.restype = ctypes.c_char_p
87
88 return _lib.caca_get_display_driver(self)
89
90 def set_driver(self, driver=None):
91 """ Set the output driver.
92
93 driver -- A string describing the desired output driver or NULL
94 to choose the best driver automatically.
95 """
96 _lib.caca_set_display_driver.argtypes = [_Display, ctypes.c_char_p]
97 _lib.caca_set_display_driver.restype = ctypes.c_int
98
99 if not driver:
100 driver = ctypes.c_char_p(0)
101 else:
102 if _PYTHON3 and isinstance(driver, str):
103 driver = _str_to_bytes(driver)
104
105 return _lib.caca_set_display_driver(self, driver)
106
107 def get_canvas(self):
108 """ Get the canvas attached to a caca graphical context.
109 """
110 _lib.caca_get_canvas.argtypes = [_Display]
111 _lib.caca_get_canvas.restype = ctypes.POINTER(ctypes.c_char_p)
112
113 return Canvas(pointer=_lib.caca_get_canvas(self))
114
115 def refresh(self):
116 """ Flush pending changes and redraw the screen.
117 """
118 _lib.caca_refresh_display.argtypes = [_Display]
119 _lib.caca_refresh_display.restype = ctypes.c_int
120
121 return _lib.caca_refresh_display(self)
122
123 def set_time(self, usec):
124 """ Set the refresh delay.
125
126 usec -- the refresh delay in microseconds
127 """
128 _lib.caca_set_display_time.argtypes = [_Display, ctypes.c_int]
129 _lib.caca_set_display_time.restype = ctypes.c_int
130
131 return _lib.caca_set_display_time(self, usec)
132
133 def get_time(self):
134 """ Get the display's average rendering time.
135 """
136 _lib.caca_get_display_time.argtypes = [_Display]
137 _lib.caca_get_display_time.restype = ctypes.c_int
138
139 return _lib.caca_get_display_time(self)
140
141 def set_title(self, title):
142 """ Set the display title.
143
144 title -- the desired display title
145 """
146 _lib.caca_set_display_title.argtypes = [_Display, ctypes.c_char_p]
147 _lib.caca_set_display_title.restype = ctypes.c_int
148
149 if _PYTHON3 and isinstance(title, str):
150 title = _str_to_bytes(title)
151
152 return _lib.caca_set_display_title(self, title)
153
154 def set_mouse(self, flag):
155 """ Show or hide the mouse pointer. This function works with the
156 ncurses, S-Lang and X11 drivers.
157
158 flag -- 0 hides the pointer, 1 shows the system's default pointer
159 (usually an arrow)
160 """
161 _lib.caca_set_mouse.argtypes = [_Display, ctypes.c_int]
162 _lib.caca_set_mouse.restype = ctypes.c_int
163
164 return _lib.caca_set_mouse(self, flag)
165
166 def set_cursor(self, flag):
167 """ Show or hide the cursor, for devices that support such a feature.
168
169 flag -- 0 hides the cursor, 1 shows the system's default cursor
170 (usually a white rectangle).
171 """
172
173 _lib.caca_set_cursor.argtypes = [Display, ctypes.c_int]
174 _lib.caca_set_cursor.restype = ctypes.c_int
175
176 return _lib.caca_set_cursor(self, flag)
177
178 def get_event(self, event_mask, event, timeout):
179 """ Get the next mouse or keyboard input event.
180
181 event_mask -- bitmask of requested events
182 event -- a pointer to caca_event structure or NULL
183 tiemout -- a timeout value in microseconds
184 """
185
186 _lib.caca_get_event.argtypes = [
187 Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int
188 ]
189
190 return _lib.caca_get_event(self, event_mask, ctypes.byref(event),
191 timeout)
192
193 def get_mouse_x(self):
194 """ Return the X mouse coordinate.
195 """
196 _lib.caca_get_mouse_x.argtypes = [Display]
197 _lib.caca_get_mouse_x.restype = ctypes.c_int
198
199 return _lib.caca_get_mouse_x(self)
200
201 def get_mouse_y(self):
202 """ Return the Y mouse coordinate.
203 """
204 _lib.caca_get_mouse_y.argtypes = [Display]
205 _lib.caca_get_mouse_y.restype = ctypes.c_int
206
207 return _lib.caca_get_mouse_y(self)
208
209
210 class DisplayError(Exception):
211 pass
212
213
214 class Event(ctypes.Structure):
215 """ Object to store libcaca event.
216 """
217 _fields_ = (
218 ('opaque_structure', ctypes.c_char_p * 32),
219 )
220
221 def from_param(self):
222 """ Required method to pass object as parameter of a C function.
223 """
224 return ctypes.byref(self)
225
226 def get_type(self):
227 """ Return an event's type.
228 """
229 _lib.caca_get_event_type.argtypes = [Event]
230 _lib.caca_get_event_type.restype = ctypes.c_int
231
232 return _lib.caca_get_event_type(self)
233
234 def get_key_ch(self):
235 """ Return a key press or key release event's value.
236 """
237 _lib.caca_get_event_key_ch.argtypes = [Event]
238 _lib.caca_get_event_key_ch.restype = ctypes.c_int
239
240 return _lib.caca_get_event_key_ch(self)
241
242 def get_key_utf32(self):
243 """ Not implemented.
244 """
245 raise DisplayError("Not implemented")
246
247 def get_key_utf8(self):
248 """ Return a key press or key release event's UTF-8 value
249 as python string.
250 """
251 # set buffer for writing utf8 value
252 buf = ctypes.c_buffer(7)
253
254 _lib.caca_get_event_key_utf8.argtypes = [Event, ctypes.c_char_p]
255 _lib.caca_get_event_key_utf8.restype = ctypes.c_int
256
257 _lib.caca_get_event_key_utf8(self, buf)
258
259 raw = []
260 for item in list(buf.raw):
261 if item == '\x00':
262 break
263 else:
264 raw.append(item)
265
266 return "".join(raw)
267
268 def get_mouse_button(self):
269 """ Return a mouse press or mouse release event's button.
270 """
271 _lib.caca_get_event_mouse_button.argtypes = [Event]
272 _lib.caca_get_event_mouse_button.restype = ctypes.c_int
273
274 return _lib.caca_get_event_mouse_button(self)
275
276 def get_mouse_x(self):
277 """ Return a mouse motion event's X coordinate.
278 """
279 _lib.caca_get_event_mouse_x.argtypes = [Event]
280 _lib.caca_get_event_mouse_x.restype = ctypes.c_int
281
282 return _lib.caca_get_event_mouse_x(self)
283
284 def get_mouse_y(self):
285 """ Return a mouse motion event's Y coordinate.
286 """
287 _lib.caca_get_event_mouse_y.argtypes = [Event]
288 _lib.caca_get_event_mouse_y.restype = ctypes.c_int
289
290 return _lib.caca_get_event_mouse_y(self)
291
292 def get_resize_width(self):
293 """ Return a resize event's display width value.
294 """
295 _lib.caca_get_event_resize_width.argtypes = [Event]
296 _lib.caca_get_event_resize_width.restype = ctypes.c_int
297
298 return _lib.caca_get_event_resize_width(self)
299
300 def get_resize_height(self):
301 """ Return a resize event's display height value.
302 """
303 _lib.caca_get_event_resize_height.argtypes = [Event]
304 _lib.caca_get_event_resize_height.restype = ctypes.c_int
305
306 return _lib.caca_get_event_resize_height(self)
307