Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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