Skip to content

Commit

Permalink
Merge pull request #16 from alxf/issue-#15
Browse files Browse the repository at this point in the history
Issue #15: Fix returned pointer with caca types.
  • Loading branch information
alxf committed Jun 2, 2016
2 parents ae47c93 + f5a052e commit 0e23624
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
9 changes: 7 additions & 2 deletions python/caca/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
from caca import _PYTHON3, _str_to_bytes, _bytes_to_str
from caca.font import _Font


class _CanvasStruct(ctypes.Structure):
pass

class _Canvas(object):
""" Model for Canvas objects.
"""

def __init__(self):
self._cv = 0
self._cv = None

def from_param(self):
""" Required by ctypes module to call object as parameter of
Expand All @@ -38,7 +42,7 @@ def __str__(self):
return "<CacaCanvas %dx%d>" % (self.get_width(), self.get_height())

def __del__(self):
if self._cv > 0 and _lib is not None:
if self._cv and _lib is not None:
self._free()

def _free(self):
Expand All @@ -61,6 +65,7 @@ def __init__(self, width=0, height=0, pointer=None):
pointer -- pointer to libcaca canvas
"""
_lib.caca_create_canvas.argtypes = [ctypes.c_int, ctypes.c_int]
_lib.caca_create_canvas.restype = ctypes.POINTER(_CanvasStruct)

if pointer is None:
try:
Expand Down
13 changes: 12 additions & 1 deletion python/caca/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
from caca import _lib, _PYTHON3, _str_to_bytes
from caca.canvas import _Canvas, Canvas


class _DisplayStruct(ctypes.Structure):
pass


class _Display(object):
""" Model for Display objects.
"""
Expand All @@ -32,7 +37,7 @@ def __str__(self):
return "<CacaDisplay>"

def __del__(self):
if self._dp > 0 and _lib is not None:
if self._dp and _lib is not None:
self._free()

def _free(self):
Expand All @@ -57,11 +62,15 @@ def __init__(self, cv, driver=None):

if driver is None:
_lib.caca_create_display.argtypes = [_Canvas]
_lib.caca_create_display.restype = ctypes.POINTER(_DisplayStruct)
self._dp = _lib.caca_create_display(cv)
else:
_lib.caca_create_display_with_driver.argtypes = [
_Canvas, ctypes.c_char_p
]
_lib.caca_create_display_with_driver.restype = ctypes.POINTER(
_DisplayStruct
)
if _PYTHON3 and isinstance(driver, str):
driver = _str_to_bytes(driver)

Expand Down Expand Up @@ -197,9 +206,11 @@ def get_mouse_y(self):

return _lib.caca_get_mouse_y(self)


class DisplayError(Exception):
pass


class Event(ctypes.Structure):
""" Object to store libcaca event.
"""
Expand Down
9 changes: 7 additions & 2 deletions python/caca/dither.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
from caca import _lib
from caca.canvas import _Canvas


class _DitherStruct(ctypes.Structure):
pass

class _Dither(object):
""" Model for Dither object.
"""
def __init__(self):
self._dither = 0
self._dither = None

def from_param(self):
""" Required by ctypes module to call object as parameter of
Expand All @@ -32,7 +36,7 @@ def from_param(self):
return self._dither

def __del__(self):
if self._dither > 0:
if self._dither:
self._free()

def __str__(self):
Expand Down Expand Up @@ -66,6 +70,7 @@ def __init__(self, bpp, width, height, pitch, rmask, gmask, bmask, amask):
ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
ctypes.c_uint, ctypes.c_uint, ctypes.c_uint, ctypes.c_uint,
]
_lib.caca_create_dither.restype = ctypes.POINTER(_DitherStruct)

self._dither = _lib.caca_create_dither(bpp, width, height, pitch,
rmask, gmask, bmask, amask)
Expand Down
10 changes: 7 additions & 3 deletions python/caca/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

from caca import _lib, _PYTHON3, _str_to_bytes


class _FontStruct(ctypes.Structure):
pass

class _Font(object):
""" Model for Font object.
"""
def __init__(self):
self._font = 0
self._font = None

def from_param(self):
""" Required by ctypes module to call object as parameter of
Expand All @@ -33,7 +37,7 @@ def from_param(self):

def __del__(self):
if hasattr(self, "_font"):
if self._font > 0:
if self._font:
self._free()

def __str__(self):
Expand Down Expand Up @@ -62,7 +66,7 @@ def __init__(self, font, size=0):
else:
raise FontError("Unsupported method")

_lib.caca_load_font.restype = ctypes.c_int
_lib.caca_load_font.restype = ctypes.POINTER(_FontStruct)

if _PYTHON3:
font = _str_to_bytes(font)
Expand Down

0 comments on commit 0e23624

Please sign in to comment.