Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

297 lignes
9.1 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://www.wtfpl.net/ for more details.
  13. #
  14. """ Libcaca Python bindings """
  15. import ctypes
  16. from caca import _lib, _PYTHON3, _str_to_bytes
  17. from caca.canvas import _Canvas, 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 and _lib is not None:
  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. if _PYTHON3 and isinstance(driver, str):
  54. driver = _str_to_bytes(driver)
  55. self._dp = _lib.caca_create_display_with_driver(cv, driver)
  56. if self._dp == 0:
  57. raise DisplayError("Failed to create display")
  58. def get_driver(self):
  59. """ Return the caca graphical context's current output driver.
  60. """
  61. _lib.caca_get_display_driver.argtypes = [_Display]
  62. _lib.caca_get_display_driver.restype = ctypes.c_char_p
  63. return _lib.caca_get_display_driver(self)
  64. def set_driver(self, driver=None):
  65. """ Set the output driver.
  66. driver -- A string describing the desired output driver or NULL
  67. to choose the best driver automatically.
  68. """
  69. _lib.caca_set_display_driver.argtypes = [_Display, ctypes.c_char_p]
  70. _lib.caca_set_display_driver.restype = ctypes.c_int
  71. if not driver:
  72. driver = ctypes.c_char_p(0)
  73. else:
  74. if _PYTHON3 and isinstance(driver, str):
  75. driver = _str_to_bytes(driver)
  76. return _lib.caca_set_display_driver(self, driver)
  77. def get_canvas(self):
  78. """ Get the canvas attached to a caca graphical context.
  79. """
  80. _lib.caca_get_canvas.argtypes = [_Display]
  81. _lib.caca_get_canvas.restype = ctypes.POINTER(ctypes.c_char_p)
  82. return Canvas(pointer=_lib.caca_get_canvas(self))
  83. def refresh(self):
  84. """ Flush pending changes and redraw the screen.
  85. """
  86. _lib.caca_refresh_display.argtypes = [_Display]
  87. _lib.caca_refresh_display.restype = ctypes.c_int
  88. return _lib.caca_refresh_display(self)
  89. def set_time(self, usec):
  90. """ Set the refresh delay.
  91. usec -- the refresh delay in microseconds
  92. """
  93. _lib.caca_set_display_time.argtypes = [_Display, ctypes.c_int]
  94. _lib.caca_set_display_time.restype = ctypes.c_int
  95. return _lib.caca_set_display_time(self, usec)
  96. def get_time(self):
  97. """ Get the display's average rendering time.
  98. """
  99. _lib.caca_get_display_time.argtypes = [_Display]
  100. _lib.caca_get_display_time.restype = ctypes.c_int
  101. return _lib.caca_get_display_time(self)
  102. def set_title(self, title):
  103. """ Set the display title.
  104. title -- the desired display title
  105. """
  106. _lib.caca_set_display_title.argtypes = [_Display, ctypes.c_char_p]
  107. _lib.caca_set_display_title.restype = ctypes.c_int
  108. if _PYTHON3 and isinstance(title, str):
  109. title = _str_to_bytes(title)
  110. return _lib.caca_set_display_title(self, title)
  111. def set_mouse(self, flag):
  112. """ Show or hide the mouse pointer. This function works with the
  113. ncurses, S-Lang and X11 drivers.
  114. flag -- 0 hides the pointer, 1 shows the system's default pointer
  115. (usually an arrow)
  116. """
  117. _lib.caca_set_mouse.argtypes = [_Display, ctypes.c_int]
  118. _lib.caca_set_mouse.restype = ctypes.c_int
  119. return _lib.caca_set_mouse(self, flag)
  120. def set_cursor(self, flag):
  121. """ Show or hide the cursor, for devices that support such a feature.
  122. flag -- 0 hides the cursor, 1 shows the system's default cursor
  123. (usually a white rectangle).
  124. """
  125. _lib.caca_set_cursor.argtypes = [Display, ctypes.c_int]
  126. _lib.caca_set_cursor.restype = ctypes.c_int
  127. return _lib.caca_set_cursor(self, flag)
  128. def get_event(self, event_mask, event, timeout):
  129. """ Get the next mouse or keyboard input event.
  130. event_mask -- bitmask of requested events
  131. event -- a pointer to caca_event structure or NULL
  132. tiemout -- a timeout value in microseconds
  133. """
  134. _lib.caca_get_event.argtypes = [
  135. Display, ctypes.c_int, ctypes.POINTER(Event), ctypes.c_int
  136. ]
  137. return _lib.caca_get_event(self, event_mask, ctypes.byref(event),
  138. timeout)
  139. def get_mouse_x(self):
  140. """ Return the X mouse coordinate.
  141. """
  142. _lib.caca_get_mouse_x.argtypes = [Display]
  143. _lib.caca_get_mouse_x.restype = ctypes.c_int
  144. return _lib.caca_get_mouse_x(self)
  145. def get_mouse_y(self):
  146. """ Return the Y mouse coordinate.
  147. """
  148. _lib.caca_get_mouse_y.argtypes = [Display]
  149. _lib.caca_get_mouse_y.restype = ctypes.c_int
  150. return _lib.caca_get_mouse_y(self)
  151. class DisplayError(Exception):
  152. pass
  153. class Event(ctypes.Structure):
  154. """ Object to store libcaca event.
  155. """
  156. _fields_ = (
  157. ('opaque_structure', ctypes.c_char_p * 32),
  158. )
  159. def from_param(self):
  160. """ Required method to pass object as parameter of a C function.
  161. """
  162. return ctypes.byref(self)
  163. def get_type(self):
  164. """ Return an event's type.
  165. """
  166. _lib.caca_get_event_type.argtypes = [Event]
  167. _lib.caca_get_event_type.restype = ctypes.c_int
  168. return _lib.caca_get_event_type(self)
  169. def get_key_ch(self):
  170. """ Return a key press or key release event's 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. """ Not implemented.
  177. """
  178. raise DisplayError("Not implemented")
  179. def get_key_utf8(self):
  180. """ Return a key press or key release event's UTF-8 value
  181. as python string.
  182. """
  183. # set buffer for writing utf8 value
  184. buf = ctypes.c_buffer(7)
  185. _lib.caca_get_event_key_utf8.argtypes = [Event, ctypes.c_char_p]
  186. _lib.caca_get_event_key_utf8.restype = ctypes.c_int
  187. _lib.caca_get_event_key_utf8(self, buf)
  188. raw = []
  189. for item in list(buf.raw):
  190. if item == '\x00':
  191. break
  192. else:
  193. raw.append(item)
  194. return "".join(raw)
  195. def get_mouse_button(self):
  196. """ Return a mouse press or mouse release event's button.
  197. """
  198. _lib.caca_get_event_mouse_button.argtypes = [Event]
  199. _lib.caca_get_event_mouse_button.restype = ctypes.c_int
  200. return _lib.caca_get_event_mouse_button(self)
  201. def get_mouse_x(self):
  202. """ Return a mouse motion event's X coordinate.
  203. """
  204. _lib.caca_get_event_mouse_x.argtypes = [Event]
  205. _lib.caca_get_event_mouse_x.restype = ctypes.c_int
  206. return _lib.caca_get_event_mouse_x(self)
  207. def get_mouse_y(self):
  208. """ Return a mouse motion event's Y coordinate.
  209. """
  210. _lib.caca_get_event_mouse_y.argtypes = [Event]
  211. _lib.caca_get_event_mouse_y.restype = ctypes.c_int
  212. return _lib.caca_get_event_mouse_y(self)
  213. def get_resize_width(self):
  214. """ Return a resize event's display width value.
  215. """
  216. _lib.caca_get_event_resize_width.argtypes = [Event]
  217. _lib.caca_get_event_resize_width.restype = ctypes.c_int
  218. return _lib.caca_get_event_resize_width(self)
  219. def get_resize_height(self):
  220. """ Return a resize event's display height value.
  221. """
  222. _lib.caca_get_event_resize_height.argtypes = [Event]
  223. _lib.caca_get_event_resize_height.restype = ctypes.c_int
  224. return _lib.caca_get_event_resize_height(self)