dynamically load macrobutton capi functions

log any exceptions raised in call()
This commit is contained in:
onyx-and-iris 2023-06-23 01:15:27 +01:00
parent 9f27968c5c
commit c6b203a1df

View File

@ -1,10 +1,13 @@
import ctypes as ct
import logging
from abc import ABCMeta
from ctypes.wintypes import CHAR, FLOAT, LONG, WCHAR
from .error import CAPIError
from .inst import libc
logger = logging.getLogger(__name__)
class CBindings(metaclass=ABCMeta):
"""
@ -13,6 +16,8 @@ class CBindings(metaclass=ABCMeta):
Maps expected ctype argument and res types for each binding.
"""
logger_cbindings = logger.getChild("Cbindings")
vm_login = libc.VBVMR_Login
vm_login.restype = LONG
vm_login.argtypes = None
@ -33,14 +38,17 @@ class CBindings(metaclass=ABCMeta):
vm_get_version.restype = LONG
vm_get_version.argtypes = [ct.POINTER(LONG)]
if hasattr(libc, "VBVMR_MacroButton_IsDirty"):
vm_mdirty = libc.VBVMR_MacroButton_IsDirty
vm_mdirty.restype = LONG
vm_mdirty.argtypes = None
if hasattr(libc, "VBVMR_MacroButton_GetStatus"):
vm_get_buttonstatus = libc.VBVMR_MacroButton_GetStatus
vm_get_buttonstatus.restype = LONG
vm_get_buttonstatus.argtypes = [LONG, ct.POINTER(FLOAT), LONG]
if hasattr(libc, "VBVMR_MacroButton_SetStatus"):
vm_set_buttonstatus = libc.VBVMR_MacroButton_SetStatus
vm_set_buttonstatus.restype = LONG
vm_set_buttonstatus.argtypes = [LONG, FLOAT, LONG]
@ -103,7 +111,15 @@ class CBindings(metaclass=ABCMeta):
vm_get_midi_message.restype = LONG
vm_get_midi_message.argtypes = [ct.POINTER(CHAR * 1024), LONG]
def call(self, func):
res = func()
if res != 0:
raise CAPIError(f"Function {func.func.__name__} returned {res}")
def call(self, func, *args, ok=(0,), ok_exp=None):
try:
res = func(*args)
if ok_exp is None:
if res not in ok:
raise CAPIError(f"{func.__name__} returned {res}")
elif not ok_exp(res):
raise CAPIError(f"{func.__name__} returned {res}")
return res
except CAPIError as e:
self.logger_cbindings.exception(f"{type(e).__name__}: {e}")
raise