ソースを参照

add `win32` module

blueloveTH 1 年間 前
コミット
2c7f0cdd07

+ 9 - 0
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()

+ 3 - 3
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();
+void pk__add_module_array2d();
+
+void pk__add_module_win32();

+ 0 - 2
include/typings/conio.pyi

@@ -1,2 +0,0 @@
-def _kbhit() -> int: ...
-def _getch() -> int: ...

+ 0 - 1
include/typings/pkpy.pyi

@@ -1 +0,0 @@
-from typing import Any

+ 4 - 0
include/typings/win32.pyi

@@ -0,0 +1,4 @@
+def _kbhit() -> int: ...
+def _getch() -> int: ...
+
+def PlaySoundA(pszSound: str, hmod: int, fdwSound: int) -> bool: ...

+ 3 - 3
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;

+ 0 - 32
src/modules/pkpy.c

@@ -1,32 +0,0 @@
-#include "pocketpy/pocketpy.h"
-
-void pk__add_module_pkpy() { py_newmodule("pkpy"); }
-
-#ifdef _WIN32
-
-#include <conio.h>
-
-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
-}

+ 47 - 0
src/modules/win32.c

@@ -0,0 +1,47 @@
+#include "pocketpy/pocketpy.h"
+
+#if defined(_WIN32) && defined(PK_MODULE_WIN32)
+
+#include <windows.h>
+#include <conio.h>
+
+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
+}