diff --git a/src/platform/python/mgba/__init__.py b/src/platform/python/mgba/__init__.py index e69de29bb..7be537a5b 100644 --- a/src/platform/python/mgba/__init__.py +++ b/src/platform/python/mgba/__init__.py @@ -0,0 +1,15 @@ +# Copyright (c) 2013-2017 Jeffrey Pfau +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +from ._pylib import ffi, lib + +def createCallback(structName, cbName, funcName=None): + funcName = funcName or "_py{}{}".format(structName, cbName.capitalize()) + fullStruct = "struct {}*".format(structName) + def cb(handle, *args): + h = ffi.cast(fullStruct, handle) + getattr(ffi.from_handle(h.pyobj), cbName)(*args) + + return ffi.def_extern(name=funcName)(cb) diff --git a/src/platform/python/mgba/log.py b/src/platform/python/mgba/log.py index a3412b186..4514e67a9 100644 --- a/src/platform/python/mgba/log.py +++ b/src/platform/python/mgba/log.py @@ -4,11 +4,9 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. from ._pylib import ffi, lib +from . import createCallback -@ffi.def_extern() -def _pyLog(logger, category, level, message): - l = ffi.cast("struct mLoggerPy*", logger) - ffi.from_handle(l.pyobj).log(category, level, ffi.string(message).decode('UTF-8')) +createCallback("mLoggerPy", "log", "_pyLog") def installDefault(logger): lib.mLogSetDefaultLogger(logger._native)