source: libcaca/trunk/python/caca/display.py @ 4411

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