diff --git a/CMakeLists.txt b/CMakeLists.txt index eb63418c..3259a9ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,11 @@ if(PK_ENABLE_OS) add_definitions(-DPK_ENABLE_OS=1) endif() +option(PK_MODULE_WIN32 "" OFF) +if(PK_MODULE_WIN32) + add_definitions(-DPK_MODULE_WIN32=1) +endif() + # PK_IS_MAIN determines whether the project is being used from root # or if it is added as a dependency (through add_subdirectory for example). if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") @@ -73,3 +78,7 @@ if(UNIX) target_link_libraries(${PROJECT_NAME} m) target_link_libraries(${PROJECT_NAME} dl) endif() + +if(PK_MODULE_WIN32) + target_link_libraries(${PROJECT_NAME} winmm.lib) +endif() diff --git a/include/pocketpy/interpreter/modules.h b/include/pocketpy/interpreter/modules.h index 659da832..0aee221b 100644 --- a/include/pocketpy/interpreter/modules.h +++ b/include/pocketpy/interpreter/modules.h @@ -1,7 +1,5 @@ #pragma once -void pk__add_module_pkpy(); -void pk__add_module_conio(); void pk__add_module_os(); void pk__add_module_sys(); void pk__add_module_math(); @@ -14,4 +12,6 @@ void pk__add_module_easing(); void pk__add_module_traceback(); void pk__add_module_enum(); void pk__add_module_linalg(); -void pk__add_module_array2d(); \ No newline at end of file +void pk__add_module_array2d(); + +void pk__add_module_win32(); \ No newline at end of file diff --git a/include/typings/conio.pyi b/include/typings/conio.pyi deleted file mode 100644 index c9c7b873..00000000 --- a/include/typings/conio.pyi +++ /dev/null @@ -1,2 +0,0 @@ -def _kbhit() -> int: ... -def _getch() -> int: ... diff --git a/include/typings/pkpy.pyi b/include/typings/pkpy.pyi deleted file mode 100644 index 70a7b6dd..00000000 --- a/include/typings/pkpy.pyi +++ /dev/null @@ -1 +0,0 @@ -from typing import Any diff --git a/include/typings/win32.pyi b/include/typings/win32.pyi new file mode 100644 index 00000000..d9dc5a16 --- /dev/null +++ b/include/typings/win32.pyi @@ -0,0 +1,4 @@ +def _kbhit() -> int: ... +def _getch() -> int: ... + +def PlaySoundA(pszSound: str, hmod: int, fdwSound: int) -> bool: ... diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index da9abd15..26442a5b 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -204,9 +204,6 @@ void VM__ctor(VM* self) { pk__add_module_array2d(); // add modules - pk__add_module_pkpy(); - pk__add_module_conio(); - pk__add_module_os(); pk__add_module_sys(); pk__add_module_math(); @@ -219,6 +216,9 @@ void VM__ctor(VM* self) { pk__add_module_traceback(); pk__add_module_enum(); + // add win32 module + pk__add_module_win32(); + // add python builtins do { bool ok; diff --git a/src/modules/pkpy.c b/src/modules/pkpy.c deleted file mode 100644 index 517204bb..00000000 --- a/src/modules/pkpy.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "pocketpy/pocketpy.h" - -void pk__add_module_pkpy() { py_newmodule("pkpy"); } - -#ifdef _WIN32 - -#include - -static bool conio__kbhit(int argc, py_Ref argv) { - PY_CHECK_ARGC(0); - int ret = _kbhit(); - py_newint(py_retval(), ret); - return true; -} - -static bool conio__getch(int argc, py_Ref argv) { - PY_CHECK_ARGC(0); - int ret = _getch(); - py_newint(py_retval(), ret); - return true; -} - -#endif - -void pk__add_module_conio() { - py_Ref mod = py_newmodule("conio"); - -#ifdef _WIN32 - py_bindfunc(mod, "_kbhit", conio__kbhit); - py_bindfunc(mod, "_getch", conio__getch); -#endif -} \ No newline at end of file diff --git a/src/modules/win32.c b/src/modules/win32.c new file mode 100644 index 00000000..42156b66 --- /dev/null +++ b/src/modules/win32.c @@ -0,0 +1,47 @@ +#include "pocketpy/pocketpy.h" + +#if defined(_WIN32) && defined(PK_MODULE_WIN32) + +#include +#include + +static bool win32__kbhit(int argc, py_Ref argv) { + PY_CHECK_ARGC(0); + int ret = _kbhit(); + py_newint(py_retval(), ret); + return true; +} + +static bool win32__getch(int argc, py_Ref argv) { + PY_CHECK_ARGC(0); + int ret = _getch(); + py_newint(py_retval(), ret); + return true; +} + +static bool win32_PlaySoundA(int argc, py_Ref argv) { + PY_CHECK_ARGC(3); + PY_CHECK_ARG_TYPE(0, tp_str); + PY_CHECK_ARG_TYPE(1, tp_int); + PY_CHECK_ARG_TYPE(2, tp_int); + const char* pszSound = py_tostr(argv); + py_i64 hmod = py_toint(py_arg(1)); + py_i64 fdwSound = py_toint(py_arg(2)); + int ret = PlaySoundA(pszSound, (HMODULE)hmod, fdwSound); + py_newbool(py_retval(), ret); + return true; +} + +#endif + + +void pk__add_module_win32() { +#if defined(_WIN32) && defined(PK_MODULE_WIN32) + py_Ref mod = py_newmodule("win32"); + + py_bindfunc(mod, "_kbhit", win32__kbhit); + py_bindfunc(mod, "_getch", win32__getch); + + py_bindfunc(mod, "PlaySoundA", win32_PlaySoundA); +#endif +}