Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

252 рядки
8.6 KiB

  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. """ Libcaca Python bindings """
  15. import ctypes
  16. from caca import _lib
  17. from caca.canvas import _Canvas
  18. class _Display(object):
  19. """ Model for Display objects.
  20. """
  21. def from_param(self):
  22. """ Required by ctypes module to call object as parameter of
  23. a C function.
  24. """
  25. return self._dp
  26. def __str__(self):
  27. return "<CacaDisplay>"
  28. def __del__(self):
  29. if self._dp > 0:
  30. self._free()
  31. def _free(self):
  32. """ Free a libcaca display.
  33. """
  34. _lib.caca_free_display.argtypes = [_Display]
  35. _lib.caca_free_display.restype = ctypes.c_int
  36. return _lib.caca_free_display(self)
  37. class Display(_Display):
  38. """ Display objects, methods are libcaca functions with display_t as first
  39. parameter.
  40. """
  41. def __init__(self, cv, driver=None):
  42. """ Display constructor.
  43. cv -- canvas to attach.
  44. driver -- caca driver to set with display
  45. """
  46. if driver is None:
  47. _lib.caca_create_display.argtypes = [_Canvas]
  48. self._dp = _lib.caca_create_display(cv)
  49. else:
  50. _lib.caca_create_display_with_driver.argtypes = [
  51. _Canvas, ctypes.c_char_p
  52. ]
  53. self._dp = _lib.caca_create_display_with_driver(cv, driver)
  54. def get_driver(self):
  55. """ Return the caca graphical context's current output driver.
  56. """
  57. _lib.caca_get_display_driver.argtypes = [_Display]
  58. _lib.caca_get_display_driver.restype = ctypes.c_char_p
  59. return _lib.caca_get_display_driver(self)
  60. def set_driver(self, driver=None):
  61. """ Set the output driver.
  62. driver -- A string describing the desired output driver or NULL
  63. to choose the best driver automatically.
  64. """
  65. _lib.caca_set_display_driver.argtypes = [_Display, ctypes.c_char_p]
  66. _lib.caca_set_display_driver.restype = ctypes.c_int
  67. if not driver:
  68. driver = ctypes.c_char_p(0)
  69. return _lib.caca_set_display_driver(self, driver)
  70. def get_canvas(self):
  71. """ Get the canvas attached to a caca graphical context.
  72. """
  73. _lib.caca_get_canvas.argtypes = [_Display]
  74. return _lib.caca_get_canvas(self)
  75. def refresh(self):
  76. """ Flush pending changes and redraw the screen.
  77. """
  78. _lib.caca_refresh_display.argtypes = [_Display]
  79. _lib.caca_refresh_display.restype = ctypes.c_int
  80. return _lib.caca_refresh_display(self)
  81. def set_time(self, usec):
  82. """ Set the refresh delay.
  83. usec -- the refresh delay in microseconds
  84. """
  85. _lib.caca_set_display_time.argtypes = [_Display, ctypes.c_int]
  86. _lib.caca_set_display_time.restype = ctypes.c_int
  87. return _lib.caca_set_display_time(self, usec)
  88. def get_time(self):
  89. """ Get the display's average rendering time.
  90. """
  91. _lib.caca_get_display_time.argtypes = [_Display]
  92. _lib.caca_get_display_time.restype = ctypes.c_int
  93. return _lib.caca_get_display_time(self)
  94. def set_title(self, title):
  95. """ Set the display title.
  96. title -- the desired display title
  97. """
  98. _lib.caca_set_display_title.argtypes = [_Display, ctypes.c_char_p]
  99. _lib.caca_set_display_title.restype = ctypes.c_int
  100. return _lib.caca_set_display_title(self, title)
  101. def set_mouse(self, flag):
  102. """ Show or hide the mouse pointer. This function works with the ncurses,
  103. S-Lang and X11 drivers.
  104. flag -- 0 hides the pointer, 1 shows the system's default pointer (usually an arrow).
  105. """
  106. _lib.caca_set_mouse.argtypes = [_Display, ctypes.c_int]
  107. _lib.caca_set_mouse.restype = ctypes.c_int
  108. return _lib.caca_set_mouse(self, flag)
  109. def set_cursor(self, flag):
  110. """ Show or hide the cursor, for devices that support such a feature.
  111. flag -- 0 hides the cursor, 1 shows the system's default cursor (usually a white rectangle).
  112. """
  113. _lib.caca_set_cursor.argtypes = [Display, ctypes.c_int]
  114. _lib.caca_set_cursor.restype = ctypes.c_int
  115. return _lib.caca_set_cursor(self, flag)
  116. def get_event(self, event_mask, event, timeout):
  117. """ Poll the event queue for mouse or keyboard events matching the event mask
  118. and return the first matching event. Non-matching events are discarded.
  119. If event_mask is zero, the function returns immediately.
  120. The timeout value tells how long this function needs to wait for an event.
  121. A value of zero returns immediately and the function returns zero if no more events
  122. are pending in the queue. A negative value causes the function to wait indefinitely
  123. until a matching event is received.
  124. If not null, ev will be filled with information about the event received. If null,
  125. the function will return but no information about the event will be sent.
  126. event_mask -- bitmask of requested events
  127. event -- a pointer to caca_event structure or NULL
  128. tiemout -- a timeout value in microseconds
  129. """
  130. _lib.caca_get_event.argtypes = [Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int]
  131. return _lib.caca_get_event(self, event_mask, ctypes.byref(event), timeout)
  132. def get_mouse_x(self):
  133. """ Return the X coordinate of the mouse position last time it was detected.
  134. This function is not reliable if the ncurses or S-Lang drivers are being used,
  135. because mouse position is only detected when the mouse is clicked.
  136. Other drivers such as X11 work well.
  137. """
  138. _lib.caca_get_mouse_x.argtypes = [Display]
  139. _lib.caca_get_mouse_x.restype = ctypes.c_int
  140. return _lib.caca_get_mouse_x(self)
  141. def get_mouse_y(self):
  142. """ Return the Y coordinate of the mouse position last time it was detected.
  143. This function is not reliable if the ncurses or S-Lang drivers are being used,
  144. because mouse position is only detected when the mouse is clicked.
  145. Other drivers such as X11 work well.
  146. """
  147. _lib.caca_get_mouse_y.argtypes = [Display]
  148. _lib.caca_get_mouse_y.restype = ctypes.c_int
  149. return _lib.caca_get_mouse_y(self)
  150. class Event(ctypes.Structure):
  151. """ Object to store libcaca event.
  152. """
  153. _fields_ = (
  154. ('opaque_structure', ctypes.c_char_p * 32),
  155. )
  156. def from_param(self):
  157. """ Required method to pass object as parameter of a C function.
  158. """
  159. return ctypes.byref(self)
  160. def get_type(self):
  161. """ Return the type of an event. This function may always be called
  162. on an event after caca_get_event() was called, and its return value
  163. indicates which other functions may be called.
  164. """
  165. _lib.caca_get_event_type.argtypes = [Event]
  166. _lib.caca_get_event_type.restype = ctypes.c_int
  167. return _lib.caca_get_event_type(self)
  168. def get_key_ch(self):
  169. """ Return either the ASCII value for an event's key, or if the key is not
  170. an ASCII character, an appropriate KEY_* value
  171. """
  172. _lib.caca_get_event_key_ch.argtypes = [Event]
  173. _lib.caca_get_event_key_ch.restype = ctypes.c_int
  174. return _lib.caca_get_event_key_ch(self)
  175. def get_key_utf32(self):
  176. """ Return the UTF-32/UCS-4 value for an event's key if it resolves
  177. to a printable character.
  178. """
  179. _lib.caca_get_event_key_utf32.argtypes = [Event]
  180. _lib.caca_get_event_key_utf32.restype = ctypes.c_uint32
  181. return _lib.caca_get_event_key_utf32(self)
  182. def get_key_utf8(self):
  183. """ Write the UTF-8 value for an event's key if it resolves to a
  184. printable character. Up to 6 UTF-8 bytes and a null termination
  185. are written.
  186. """
  187. # set buffer for writing utf8 value
  188. buf = ctypes.c_buffer(2)
  189. _lib.caca_get_event_key_utf8.argtypes = [Event, ctypes.c_char_p]
  190. _lib.caca_get_event_key_utf8.restype = ctypes.c_int
  191. _lib.caca_get_event_key_utf8(self, buf)
  192. return buf