From 907322a1224e782ffabcc36e2b71ee847e19c8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Thu, 17 Apr 2025 06:51:38 +0200 Subject: [PATCH 01/11] create ipc.c --- src/ipc.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/ipc.c diff --git a/src/ipc.c b/src/ipc.c new file mode 100644 index 0000000..9315f27 --- /dev/null +++ b/src/ipc.c @@ -0,0 +1,51 @@ +# include + +enum SendIPCErrors +{ + Ok = 0, + BufferNotDefined = 1, + BufferLocked = 2, + BufferOverflow = 3, + PidNotExist = 4 +} + +inline static void define_ipc(ksys_ipc_buffer *buffer, size_t bufLen) +{ + asm_inline( + "int $0x40" + ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) + ); +} + +inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg *msg, size_t msgLen) +{ + enum SendIPCErrors ret; + + asm_inline( + "int $0x40" + : "=a"(ret), + : "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(msgLen) + ) + + return ret; +} + +int syscalls_DefineIPCBuffer(lua_State *L) +{ + define_ipc(); + + return 0; +} + +int syscalls_SendIPCMessage(lua_State *L) +{ + lua_pushinteger( + L, + send_ipc( + lua_checkinteger(L, 1);, + + ) + ); + + return 1; +} -- 2.49.1 From f81b5c1b23292a3b6098f40f12b0912eb7534ca2 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Fri, 18 Apr 2025 15:13:25 +0500 Subject: [PATCH 02/11] create IPC folder && create IPC_msg class --- src/IPC/IPC_msg.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++ src/IPC/IPC_msg.h | 19 +++++ src/IPC/ipc.c | 48 ++++++++++++ src/IPC/ipc.h | 18 +++++ src/ipc.c | 51 ------------- 5 files changed, 272 insertions(+), 51 deletions(-) create mode 100644 src/IPC/IPC_msg.c create mode 100644 src/IPC/IPC_msg.h create mode 100644 src/IPC/ipc.c create mode 100644 src/IPC/ipc.h delete mode 100644 src/ipc.c diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c new file mode 100644 index 0000000..dff2676 --- /dev/null +++ b/src/IPC/IPC_msg.c @@ -0,0 +1,187 @@ +#include "IPC_msg.h" +#include +#include "../debug.h" +#include +#include + + + +static int syscalls_indexIPC_msg(lua_State* L) +{ + ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + const char* index = luaL_checkstring(L, 2); + + if (strcmp(index, "size") == 0) + { + lua_pushinteger(L, r->datalen); + } + else if (strcmp(index, "pid") == 0) + { + lua_pushboolean(L, r->pid); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +static int syscalls_newindexIPC_msg(lua_State* L) +{ + ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + lua_Integer index_i = lua_isnumber(L, 2); + uint32_t val = luaL_checkinteger(L, 3); + + if (!index_i) + { + const char* index = luaL_checkstring(L, 2); + if (strcmp(index, "size") == 0) + { + r->datalen = val; + } + else if (strcmp(index, "pid") == 0) + { + r->pid = val; + } + else + { + luaL_error(L, "wrong index: %s", index); + } + } + else + { + if (index_i < r->data) + { + ((lua_Integer*)r + (2 * sizeof(unsigned)))[index_i] = val; + } + else + { + luaL_error(L, "out of range. size: %d index: %d", r->datalen, index_i); + } + } + + lua_pushboolean(L, true); + + return 1; +} + +static bool compare_ipc_msg(ksys_ipc_msg* a, ksys_ipc_msg* b) +{ + if (a->datalen == b->datalen) + { + return memcmp( + a + (2 * sizeof(unsigned)), + b + (2 * sizeof(unsigned)), + a->datalen + ); + } + else + { + return false; + } +} + +static int syscalls_eqIPC_msg(lua_State* L) +{ + lua_pushboolean( + L, + compare_ipc_msg( + luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name), + luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name) + ) + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadByte(lua_State* L) +{ + lua_pushinteger( + L, + ((uint8_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadWord(lua_State* L) +{ + lua_pushinteger( + L, + ((uint16_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadDword(lua_State* L) +{ + lua_pushinteger( + L, + ((uint32_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static int syscalls_IPC_msg_ReadInteger(lua_State* L) +{ + lua_pushinteger( + L, + ((lua_Integer*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)] + ); + + return 1; +} + +static const luaL_Reg syscalls_IPC_msg_m[] = { + {"__index", syscalls_indexIPC_msg}, + {"__newindex", syscalls_newindexIPC_msg}, + {"__eq", syscalls_eqIPC_msg}, + {"ReadByte", syscalls_IPC_msg_ReadByte}, + {"ReadWord", syscalls_IPC_msg_ReadWord}, + {"ReadDword", syscalls_IPC_msg_ReadDword}, + {"ReadInteger", syscalls_IPC_msg_ReadInteger}, + {NULL, NULL} +}; + +struct registers* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) +{ + DEBUG_LINE("push IPC_msg entry"); + + ksys_ipc_msg* entry = lua_newuserdata(L, sizeof(ksys_ipc_msg) + dataLen); + + luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); + + return entry; +} + +static int syscalls_newIPC_msg(lua_State* L) +{ + size_t dataLen = luaL_checkinteger(L, 1); + ksys_ipc_msg* r = syscalls_pushIPC_msg(L, dataLen); + r->datalen = dataLen; + + return 1; +} + +static const luaL_Reg syscalls_IPC_msg_lib[] = { + {"new", syscalls_newIPC_msg}, + {NULL, NULL} +}; + +void syscalls_register_IPC_msg(lua_State* L) +{ + DEBUG_LINE("register IPC_msg entry"); + + luaL_newlib(L, syscalls_IPC_msg_lib); + + lua_setfield(L, -2, syscalls_IPC_msg_name); + + + luaL_newmetatable(L, syscalls_IPC_msg_metatable_name); + luaL_setfuncs(L, syscalls_IPC_msg_m, 0); + + lua_pop(L, 1); +} diff --git a/src/IPC/IPC_msg.h b/src/IPC/IPC_msg.h new file mode 100644 index 0000000..0b0d954 --- /dev/null +++ b/src/IPC/IPC_msg.h @@ -0,0 +1,19 @@ +#ifndef _SYSCALLS_IPC_MSG_H_ +#define _SYSCALLS_IPC_MSG_H_ + +#include "../syscalls.h" +#include + +#define syscalls_IPC_msg_metatable_name "syscalls IPC_msg metatable" +#define syscalls_IPC_msg_name "IPC_msg" + +int syscalls_newSystemColors(lua_State* L); + +ksys_colors_table_t* syscalls_pushSystemColors(lua_State* L); + +/** + * Register SystemColors lib + */ +void syscalls_register_SystemColors(lua_State* L); + +#endif // _SYSCALLS_IPC_MSG_H_ diff --git a/src/IPC/ipc.c b/src/IPC/ipc.c new file mode 100644 index 0000000..0bec3f8 --- /dev/null +++ b/src/IPC/ipc.c @@ -0,0 +1,48 @@ +#include "ipc.h" +#include "IPC_msg.h" +#include + +inline static void define_ipc(ksys_ipc_buffer* buffer, size_t bufLen) +{ + asm_inline( + "int $0x40" + ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) + ); +} + +inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) +{ + enum SendIPCErrors ret; + + asm_inline( + "int $0x40" + : "=a"(ret) + : "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(sizeof(ksys_ipc_msg) + msg->datalen) + ); + + return ret; +} + +int syscalls_DefineIPCBuffer(lua_State* L) +{ + uint32_t len = luaL_checkinteger(L, 1); + ksys_ipc_buffer* buffer; + + + define_ipc(buffer, len); + + return 1; +} + +int syscalls_SendIPCMessage(lua_State* L) +{ + lua_pushinteger( + L, + send_ipc( + lua_checkinteger(L, 1), + luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + ) + ); + + return 1; +} diff --git a/src/IPC/ipc.h b/src/IPC/ipc.h new file mode 100644 index 0000000..1c9e14b --- /dev/null +++ b/src/IPC/ipc.h @@ -0,0 +1,18 @@ +#ifndef __IPC_H__ +#define __IPC_H__ + +#include "../syscalls.h" + +enum SendIPCErrors +{ + Ok = 0, + BufferNotDefined = 1, + BufferLocked = 2, + BufferOverflow = 3, + PidNotExist = 4 +}; + +int syscalls_DefineIPCBuffer(lua_State* L); +int syscalls_SendIPCMessage(lua_State* L); + +#endif // __IPC_H__ \ No newline at end of file diff --git a/src/ipc.c b/src/ipc.c deleted file mode 100644 index 9315f27..0000000 --- a/src/ipc.c +++ /dev/null @@ -1,51 +0,0 @@ -# include - -enum SendIPCErrors -{ - Ok = 0, - BufferNotDefined = 1, - BufferLocked = 2, - BufferOverflow = 3, - PidNotExist = 4 -} - -inline static void define_ipc(ksys_ipc_buffer *buffer, size_t bufLen) -{ - asm_inline( - "int $0x40" - ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) - ); -} - -inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg *msg, size_t msgLen) -{ - enum SendIPCErrors ret; - - asm_inline( - "int $0x40" - : "=a"(ret), - : "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(msgLen) - ) - - return ret; -} - -int syscalls_DefineIPCBuffer(lua_State *L) -{ - define_ipc(); - - return 0; -} - -int syscalls_SendIPCMessage(lua_State *L) -{ - lua_pushinteger( - L, - send_ipc( - lua_checkinteger(L, 1);, - - ) - ); - - return 1; -} -- 2.49.1 From 180d3f795b215983ad668b5d908cbfe1dad83a17 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Fri, 18 Apr 2025 15:31:41 +0500 Subject: [PATCH 03/11] add IPC to build && fix errors --- Makefile | 7 ++++++- src/IPC/IPC_msg.c | 3 ++- src/IPC/IPC_msg.h | 6 +++--- src/IPC/ipc.c | 7 +++---- src/syscalls.c | 8 ++++++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 81b8f3e..01ae0fb 100644 --- a/Makefile +++ b/Makefile @@ -31,9 +31,10 @@ MYOBJS = Socket_O = src/sockets/socket.o src/sockets/socket_lua.o src/sockets/sockaddr.o +IPC_O = src/IPC/ipc.o src/IPC/IPC_msg.o Debug_O = src/debug/debug.o src/debug/registers.o -ALL_O = src/syscalls.o src/ARP_entry.o src/systemColors.o src/graphic.o $(Socket_O) $(Debug_O) +ALL_O = src/syscalls.o src/ARP_entry.o src/systemColors.o src/graphic.o $(Socket_O) $(Debug_O) $(IPC_O) syscalls.dll: $(ALL_O) $(CC) -shared -T dll.lds --entry _DllStartup -o $@ $(ALL_O) $(LIBS) @@ -54,6 +55,8 @@ src/sockets/sockaddr.o: src/sockets/sockaddr.c src/sockets/sockaddr.h src/graphic.o: src/graphic.c src/graphic.h src/debug/debug.o: src/debug/debug.c src/debug/debug.h src/debug/registers.h src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h +src/IPC/ipc.o: src/IPC/ipc.c src/IPC/ipc.h src/IPC/IPC_msg.h +src/IPC/IPC_msg.o: src/IPC/IPC_msg.c src/IPC/IPC_msg.h ## headers @@ -62,3 +65,5 @@ src/sockets/socket_lua.h: src/syscalls.h src/sockets/socket.h src/sockets/sockaddr.h: src/sockets/socket.h src/syscalls.h src/debug/debug.h: src/syscalls.h src/debug/registers.h: src/syscalls.h +src/IPC/IPC_msg.h: src/syscalls.h +src/IPC/ipc.h: src/syscalls.h diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c index dff2676..50e1aec 100644 --- a/src/IPC/IPC_msg.c +++ b/src/IPC/IPC_msg.c @@ -36,6 +36,7 @@ static int syscalls_newindexIPC_msg(lua_State* L) if (!index_i) { const char* index = luaL_checkstring(L, 2); + if (strcmp(index, "size") == 0) { r->datalen = val; @@ -76,7 +77,7 @@ static bool compare_ipc_msg(ksys_ipc_msg* a, ksys_ipc_msg* b) a->datalen ); } - else + else // длина сообщений не совпадает, занчитт они зразу не могут быть равны { return false; } diff --git a/src/IPC/IPC_msg.h b/src/IPC/IPC_msg.h index 0b0d954..0f911c4 100644 --- a/src/IPC/IPC_msg.h +++ b/src/IPC/IPC_msg.h @@ -7,13 +7,13 @@ #define syscalls_IPC_msg_metatable_name "syscalls IPC_msg metatable" #define syscalls_IPC_msg_name "IPC_msg" -int syscalls_newSystemColors(lua_State* L); +int syscalls_newIPC_msg(lua_State* L); -ksys_colors_table_t* syscalls_pushSystemColors(lua_State* L); +ksys_colors_table_t* syscalls_pushIPC_msg(lua_State* L); /** * Register SystemColors lib */ -void syscalls_register_SystemColors(lua_State* L); +void syscalls_register_IPC_msg(lua_State* L); #endif // _SYSCALLS_IPC_MSG_H_ diff --git a/src/IPC/ipc.c b/src/IPC/ipc.c index 0bec3f8..668d0d8 100644 --- a/src/IPC/ipc.c +++ b/src/IPC/ipc.c @@ -17,7 +17,7 @@ inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) asm_inline( "int $0x40" : "=a"(ret) - : "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(sizeof(ksys_ipc_msg) + msg->datalen) + : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"(sizeof(ksys_ipc_msg) + msg->datalen) ); return ret; @@ -28,10 +28,9 @@ int syscalls_DefineIPCBuffer(lua_State* L) uint32_t len = luaL_checkinteger(L, 1); ksys_ipc_buffer* buffer; - define_ipc(buffer, len); - return 1; + return 0; } int syscalls_SendIPCMessage(lua_State* L) @@ -39,7 +38,7 @@ int syscalls_SendIPCMessage(lua_State* L) lua_pushinteger( L, send_ipc( - lua_checkinteger(L, 1), + luaL_checkinteger(L, 1), luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) ) ); diff --git a/src/syscalls.c b/src/syscalls.c index 3855795..523e14a 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -9,13 +9,14 @@ #include "syscalls.h" #include "scancodes.h" -#include "ARP_entry.h" +#include "ARP_entry.h" #include "systemColors.h" #include "sockets/socket_lua.h" #include "debug/debug.h" #include "graphic.h" - +#include "IPC/ipc.h" +#include "IPC/IPC_msg.h" /* Режим ввода с клавиатуры @@ -1636,6 +1637,8 @@ static const luaL_Reg syscallsLib[] = { { "Done", syscalls_Done }, { "DefineBreakpoint", syscalls_DefineBreakpoint }, { "UndefBreakpoint", syscalls_UndefBreakpoint }, + { "DefineIPCBuffer", syscalls_DefineIPCBuffer }, + { "SendIPCMessage", syscalls_SendIPCMessage }, { NULL, NULL } }; @@ -1835,6 +1838,7 @@ LUALIB_API int luaopen_syscalls(lua_State* L) syscalls_push_buttons(L); syscalls_push_connectionStatus(L); syscalls_push_graphic(L); + syscalls_register_IPC_msg(L); syscalls_register_ARPEntry(L); syscalls_register_SystemColors(L); -- 2.49.1 From e75ef3eb9e860f0b05f873368e62dd6f1d69a964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Wed, 16 Apr 2025 08:41:11 +0200 Subject: [PATCH 04/11] Update manual Add more funcs --- doc/manual.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/doc/manual.md b/doc/manual.md index 3ee2a47..97df9fb 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -8,6 +8,17 @@ local syscalls = require("syscalls") ## Events +### `WaitEvent()` + +Endless wait event + +### `WaitEventTimeout(timeout)` + +Wait timeout 1/100 sec + +return event or nil + +### Events list ```lua syscalls.Event. ``` @@ -73,6 +84,55 @@ textSize, textLen, backgroundColor, encoding are optional. return color +## Buttons + +### `GetButton()` + +return pressed button or nil + +### buttons + +button's ids that defined default by window with skin + +```lua +syscalls.buttons. +``` + ++ `close` ++ `minimize` + +## SystemColors + +### SystemColors type + + + +### `GetSystemColors()` + +return SystemColors + +### `SetSytemColors(SystemColors)` + +## Threads + +### `ThreadInfo(pid)` + +return table: +```lua +{ + name: string, + pid: number, + cpu_usage: number, + memused: number, + winXPos: number, + winYPos: number, + winXSize: number, + winYPos: number, + slotState: number, + windowState: number +} +``` + ## Sockets ### `OpenSocket(domain, type, protocol)` -- 2.49.1 From ac932b3a438d99ecfd8c18c756e2a67b170ab40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80?= Date: Wed, 16 Apr 2025 08:59:49 +0200 Subject: [PATCH 05/11] update manual --- doc/manual.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/doc/manual.md b/doc/manual.md index 97df9fb..ed49911 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -78,7 +78,7 @@ textSize, textLen, backgroundColor, encoding are optional. ### `DrawLine(x1, y1, x2, y2)` -### `DrawRectangle(x, y, w, h color)` +### `DrawRectangle(x, y, widht, height, color)` ### `ReadPoint(x, y)` @@ -86,10 +86,17 @@ return color ## Buttons +### `DefineButton(x, y, widht, height, id, color)` + +### `DeleteButton(id)` + ### `GetButton()` return pressed button or nil +### `SetButtonStyle(style)` + + ### buttons button's ids that defined default by window with skin @@ -101,11 +108,48 @@ syscalls.buttons. + `close` + `minimize` +### ButtonStyles + +```lua +syscalls.buttonStyle. +``` + ## SystemColors ### SystemColors type +userdata +#### Fields + ++ frameArea ++ grabBar ++ grabBarButton ++ grabButtonText ++ grabText ++ workArea ++ workButton ++ workButton ++ workButtonText + workGraph ++ workText + +#### Constructor + +```lua +syscalls.SystemColors.new( + frameArea, + grabBar, + grabBarButton, + grabButtonText, + grabText, + workArea, + workButton, + workButton, + workButtonText, + workGraph, + workText +) +``` ### `GetSystemColors()` -- 2.49.1 From 8c155bbe09985a4424bacde39f24473cd5d40abe Mon Sep 17 00:00:00 2001 From: Egor00f Date: Wed, 16 Apr 2025 15:49:54 +0500 Subject: [PATCH 06/11] update manual.md --- doc/manual.md | 277 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 260 insertions(+), 17 deletions(-) diff --git a/doc/manual.md b/doc/manual.md index ed49911..008b973 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -1,13 +1,22 @@ # Manual +This is KolibriOS lua syscalls library. Usually you shouldn't use this library + +Better if you read before [KolibriOS syscalls wiki](http://wiki.kolibrios.org/wiki/) + +include: ```lua local syscalls = require("syscalls") ``` - - ## Events +### `SetEventMask(newMask)` + +### `CheckEvent()` + +check event + ### `WaitEvent()` Endless wait event @@ -16,9 +25,10 @@ Endless wait event Wait timeout 1/100 sec -return event or nil +return event or `nil` ### Events list + ```lua syscalls.Event. ``` @@ -32,6 +42,62 @@ syscalls.Event. + `IPC` + `Debug` +## Window + +### `CreateWindow(x, y, width, height, workColor, style, borderColor, titleColor)` + +Define window + +`borderColor` and `borderColor` only for `FixSizes` and `CanChangeSizes` (without skin styles) style. + +### `StartRedraw()` + +Start window redraw. Just call it before `CreateWindow` + +### `EndRedraw()` + +End window redraw. Just call it after you done redraw window. + +### `ChangeWindow(newX, newY, newWidth, newHeight)` + +### `FocusWindow(slot)` + +### `UnfocusWindow(slot)` + +### `SetWindowTitle(newTitle)` + +### `GetSkinHeight()` + +return skin height. + +### `SetSkin(path)` + +return error code + +### `GetSkinTitleArea()` + +return table: +```lua +{ + Left: number, + Right: number, + Top: number, + Bottom: number +} +``` + +### Style + +```lua +syscalls.windowStyle. +``` + ++ `FixSizes` ++ `NoDraw` (you must draw window manually) ++ `CanChangeSizes` ++ `WithSkin` (usually use it) ++ `WithSkinFixSizes` (with skin, but window size fixed) + ## Graphic ### Text encoding @@ -92,7 +158,7 @@ return color ### `GetButton()` -return pressed button or nil +return pressed button or `nil` ### `SetButtonStyle(style)` @@ -106,7 +172,7 @@ syscalls.buttons. ``` + `close` -+ `minimize` ++ `minimization` ### ButtonStyles @@ -114,6 +180,94 @@ syscalls.buttons. syscalls.buttonStyle. ``` +## Keyboard + +### `GetKey()` + +return: ++ nil if buffer empty ++ if hotkey return second number ++ if key pressed and key input mode is ascii return string(1 char), else return scancode + +example: + +```lua +local key, hotkey = syscalls.GetKey() + +if key then + print("key pressed") +end + +if hotkey then + print(hotkey pressed) +end +``` + +### `SetKeyInputMode(mode)` + +by default is `ASCII` + +### `GetKeyInputMode()` + +return key input mode. + +isn't syscall + +### Scancodes + +```lua +syscalls.scancode. +``` + ++ `A` ++ `B` ++ `C` ++ `D` ++ `E` ++ `F` ++ `G` ++ `H` ++ `J` ++ `K` ++ `L` ++ `M` ++ `O` ++ `P` ++ `Q` ++ `S` ++ `T` ++ `U` ++ `W` ++ `X` ++ `Y` ++ `Z` ++ `1` ++ `2` ++ `3` ++ `4` ++ `5` ++ `6` ++ `7` ++ `8` ++ `9` ++ `F1` ++ `F2` ++ `F3` ++ `F4` ++ `F5` ++ `F6` ++ `F7` ++ `F8` ++ `F9` ++ `F10` ++ `F11` ++ `F12` ++ `LeftShift` ++ `RightShift` ++ `LeftAlt` ++ `RightAlt` ++ `Tab` + ## SystemColors ### SystemColors type @@ -122,16 +276,21 @@ userdata #### Fields -+ frameArea -+ grabBar -+ grabBarButton -+ grabButtonText -+ grabText -+ workArea -+ workButton -+ workButton -+ workButtonText + workGraph -+ workText +```lua +{ + frameArea: number, + grabBar: number, + grabBarButton: number, + grabButtonText: number + grabText: number, + workArea: number, + workButton: number, + workButton: number, + workButtonText: number, + workGraph: number, + workText: number +} +``` #### Constructor @@ -155,7 +314,7 @@ syscalls.SystemColors.new( return SystemColors -### `SetSytemColors(SystemColors)` +### `SetSystemColors(SystemColors)` ## Threads @@ -173,10 +332,29 @@ return table: winXSize: number, winYPos: number, slotState: number, - windowState: number + windowState: number, + slotNumWindowStack: number, + posInWindowStack: number, + keyInputMode: number } ``` +### `KillBySlot(slot)` + +### Slot states + +```lua +syscalls.slotState. +``` + ++ `Running` ++ `Suspended` ++ `SuspendedWaitEvent` ++ `NormalTerm` ++ `ExceptTerm` ++ `WaitEvent` ++ `Free` + ## Sockets ### `OpenSocket(domain, type, protocol)` @@ -274,4 +452,69 @@ syscalls.SO. + `BINDTODEVICE` + `NONBLOCK` +## Debug + +### Registers type + +```lua +{ + eax: number + ebx: number, + esp: number, + esi: number, + edi: number, + eip: number, + eflags: number +} +``` + +### `DebugPutc(char)` + +Put char to debug board + +### `DebugPuts(text)` + +Put string to debug board + +### `GetRegisters(pid)` + +The process must be loaded for debugging (as stated in the general description). + +return registers table + +### `SetRegisters(pid, registers)` + +The process must be loaded for debugging (as stated in the general description). + +### `Disconnect(pid)` + +The process must be loaded for debugging (as stated in the general description). + +If the process was suspended, it resumes execution. + +### `Stop(pid)` + +The process must be loaded for debugging (as stated in the general description). + +### `Continue(pid)` + +The process must be loaded for debugging (as stated in the general description). + +### `ReadFromMem(pid, bytes, pointer, buffer)` + +The process must be loaded for debugging (as stated in the general description). + +return or `nil` + +### `WriteToMem(pid, bytes, pointer, buffer)` + +The process must be loaded for debugging (as stated in the general description). + +return or `nil` + +### `Done(pid)` + +### `DefineBreakpoint(pid, index, condition, len)` + +### `UndefBreakpoint(pid, index, condition, len)` -- 2.49.1 From 563259355dd41fd9640e6683729ffcf5b23c677a Mon Sep 17 00:00:00 2001 From: Egor00f Date: Wed, 16 Apr 2025 20:23:27 +0500 Subject: [PATCH 07/11] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=B8=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86,=20=D0=B2=D0=BE=D0=B7?= =?UTF-8?q?=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D0=B5=D0=BC=D1=8B=D1=85=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=BC=D0=B8=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=D0=BC=D0=B8(=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B0=D0=B5=D0=BC=D1=8B=D1=85=20?= =?UTF-8?q?=D0=B2=20=D0=B0=D1=80=D0=B3=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=D1=85)=20&&=20create=20and=20use=20macro=20LUA=5FPUSH=5FINTEGE?= =?UTF-8?q?R=5FFILED=20for=20push=20enums?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/debug/debug.c | 13 +-- src/debug/registers.c | 2 +- src/graphic.h | 62 ++++------ src/scancodes.h | 265 ++++++++++++++---------------------------- src/syscalls.c | 237 +++++++++++++++---------------------- src/syscalls.h | 4 + 6 files changed, 214 insertions(+), 369 deletions(-) diff --git a/src/debug/debug.c b/src/debug/debug.c index 28d46f5..bdb246b 100644 --- a/src/debug/debug.c +++ b/src/debug/debug.c @@ -37,9 +37,8 @@ int syscalls_SetMessageArea(lua_State* L) int syscalls_GetRegisters(lua_State* L) { - struct registers* r = syscalls_pushRegisters(L); - uint32_t pid = luaL_checkinteger(L, 1); + struct registers* r = syscalls_pushRegisters(L); asm_inline( "int $0x40" @@ -59,7 +58,7 @@ int syscalls_SetRegisters(lua_State* L) :: "a"(69), "b"(2), "c"(pid), "d"(sizeof(struct registers)), "S"(r) ); - return 1; + return 0; } int syscalls_Disconnect(lua_State* L) @@ -71,7 +70,7 @@ int syscalls_Disconnect(lua_State* L) :: "a"(69), "b"(3), "c"(pid) ); - return 1; + return 0; } int syscalls_Stop(lua_State* L) @@ -83,7 +82,7 @@ int syscalls_Stop(lua_State* L) :: "a"(69), "b"(4), "c"(pid) ); - return 1; + return 0; } int syscalls_Continue(lua_State* L) @@ -95,7 +94,7 @@ int syscalls_Continue(lua_State* L) :: "a"(69), "b"(5), "c"(pid) ); - return 1; + return 0; } int syscalls_ReadFromMem(lua_State* L) @@ -151,7 +150,7 @@ int syscalls_Done(lua_State* L) :: "a"(69), "b"(8), "c"(pid) ); - return 1; + return 0; } int syscalls_DefineBreakpoint(lua_State* L) diff --git a/src/debug/registers.c b/src/debug/registers.c index f153e05..bb52f4a 100644 --- a/src/debug/registers.c +++ b/src/debug/registers.c @@ -83,7 +83,7 @@ static int syscalls_newindexRegisters(lua_State* L) luaL_error(L, "wrong index: %s", index); } - return 1; + return 0; } static int syscalls_eqRegisters(lua_State* L) diff --git a/src/graphic.h b/src/graphic.h index 84574a2..df8dace 100644 --- a/src/graphic.h +++ b/src/graphic.h @@ -45,53 +45,37 @@ inline void syscalls_push_textSizes(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, TextScale_SIZE_6x9); - lua_setfield(L, -2, "6x9"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_6x9, "6x9"); - lua_pushinteger(L, TextScale_SIZE_8x16); - lua_setfield(L, -2, "8x16"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_8x16, "8x16"); - lua_pushinteger(L, TextScale_SIZE_12x18); - lua_setfield(L, -2, "12x18"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_12x18, "12x18"); - lua_pushinteger(L, TextScale_SIZE_16x32); - lua_setfield(L, -2, "16x32"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_16x32, "16x32"); - lua_pushinteger(L, TextScale_SIZE_18x27); - lua_setfield(L, -2, "18x27"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_18x27, "18x27"); - lua_pushinteger(L, TextScale_SIZE_24x36); - lua_setfield(L, -2, "24x36"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_24x36, "24x36"); - lua_pushinteger(L, TextScale_SIZE_24x48); - lua_setfield(L, -2, "24x48"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_24x48, "24x48"); - lua_pushinteger(L, TextScale_SIZE_30x45); - lua_setfield(L, -2, "30x45"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_30x45, "30x45"); - lua_pushinteger(L, TextScale_SIZE_32x64); - lua_setfield(L, -2, "32x64"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_32x64, "32x64"); - lua_pushinteger(L, TextScale_SIZE_36x54); - lua_setfield(L, -2, "36x54"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_36x54, "36x54"); - lua_pushinteger(L, TextScale_SIZE_40x80); - lua_setfield(L, -2, "40x80"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_40x80, "40x80"); - lua_pushinteger(L, TextScale_SIZE_42x63); - lua_setfield(L, -2, "42x63"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_42x63, "42x63"); - lua_pushinteger(L, TextScale_SIZE_48x72); - lua_setfield(L, -2, "48x72"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_48x72, "48x72"); - lua_pushinteger(L, TextScale_SIZE_48x96); - lua_setfield(L, -2, "48x96"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_48x96, "48x96"); - lua_pushinteger(L, TextScale_SIZE_56x112); - lua_setfield(L, -2, "56x112"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_56x112, "56x112"); - lua_pushinteger(L, TextScale_SIZE_64x128); - lua_setfield(L, -2, "64x128"); + LUA_PUSH_INTEGER_FIELD(L, TextScale_SIZE_64x128, "64x128"); lua_setfield(L, -2, "textSize"); } @@ -100,21 +84,17 @@ inline void syscalls_push_Encoding(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, cp866); - lua_setfield(L, -2, "cp866"); + LUA_PUSH_INTEGER_FIELD(L, cp866, "cp866"); - lua_pushinteger(L, cp866_8x16); - lua_setfield(L, -2, "cp866_8x16"); + LUA_PUSH_INTEGER_FIELD(L, cp866_8x16, "cp866_8x16"); - lua_pushinteger(L, utf8); - lua_setfield(L, -2, "utf8"); + LUA_PUSH_INTEGER_FIELD(L, utf8, "utf8"); - lua_pushinteger(L, utf16); - lua_setfield(L, -2, "utf16"); + LUA_PUSH_INTEGER_FIELD(L, utf16, "utf16"); lua_setfield(L, -2, "Encoding"); } #define syscalls_push_graphic(L) syscalls_push_textSizes(L); syscalls_push_Encoding(L); -#endif // __GRAPHIC_H__ +#endif // __GRAPHIC_H_ \ No newline at end of file diff --git a/src/scancodes.h b/src/scancodes.h index a582ab4..9d6f83e 100644 --- a/src/scancodes.h +++ b/src/scancodes.h @@ -8,270 +8,183 @@ static inline void syscalls_register_scancodes(lua_State* L) { - lua_newtable(L); + lua_newtable(L); - lua_pushinteger(L, KSYS_SCANCODE_0); - lua_setfield(L, -2, "0"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_0, "0"); - lua_pushinteger(L, KSYS_SCANCODE_1); - lua_setfield(L, -2, "1"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_1, "1"); - lua_pushinteger(L, KSYS_SCANCODE_2); - lua_setfield(L, -2, "2"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_2, "2"); - lua_pushinteger(L, KSYS_SCANCODE_3); - lua_setfield(L, -2, "3"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_3, "3"); - lua_pushinteger(L, KSYS_SCANCODE_4); - lua_setfield(L, -2, "4"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_4, "5"); - lua_pushinteger(L, KSYS_SCANCODE_5); - lua_setfield(L, -2, "6"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_5, "6"); - lua_pushinteger(L, KSYS_SCANCODE_7); - lua_setfield(L, -2, "7"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_7, "7"); - lua_pushinteger(L, KSYS_SCANCODE_8); - lua_setfield(L, -2, "8"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_8, "8"); - lua_pushinteger(L, KSYS_SCANCODE_9); - lua_setfield(L, -2, "9"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_9, "9"); - lua_pushinteger(L, KSYS_SCANCODE_A); - lua_setfield(L, -2, "A"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_A, "A"); - lua_pushinteger(L, KSYS_SCANCODE_B); - lua_setfield(L, -2, "B"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_B, "B"); - lua_pushinteger(L, KSYS_SCANCODE_C); - lua_setfield(L, -2, "C"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_C, "C"); - lua_pushinteger(L, KSYS_SCANCODE_D); - lua_setfield(L, -2, "D"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_D, "D"); - lua_pushinteger(L, KSYS_SCANCODE_E); - lua_setfield(L, -2, "E"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_E, "E"); - lua_pushinteger(L, KSYS_SCANCODE_F); - lua_setfield(L, -2, "F"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F, "F"); - lua_pushinteger(L, KSYS_SCANCODE_G); - lua_setfield(L, -2, "G"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_G, "G"); - lua_pushinteger(L, KSYS_SCANCODE_H); - lua_setfield(L, -2, "H"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_H, "H"); - lua_pushinteger(L, KSYS_SCANCODE_J); - lua_setfield(L, -2, "J"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_J, "J"); - lua_pushinteger(L, KSYS_SCANCODE_K); - lua_setfield(L, -2, "K"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_K, "K"); - lua_pushinteger(L, KSYS_SCANCODE_L); - lua_setfield(L, -2, "L"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_L, "L"); - lua_pushinteger(L, KSYS_SCANCODE_M); - lua_setfield(L, -2, "M"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_M, "M"); - lua_pushinteger(L, KSYS_SCANCODE_N); - lua_setfield(L, -2, "N"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_N, "N"); - lua_pushinteger(L, KSYS_SCANCODE_O); - lua_setfield(L, -2, "O"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_O, "O"); - lua_pushinteger(L, KSYS_SCANCODE_P); - lua_setfield(L, -2, "P"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_P, "P"); - lua_pushinteger(L, KSYS_SCANCODE_Q); - lua_setfield(L, -2, "Q"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_Q, "Q"); - lua_pushinteger(L, KSYS_SCANCODE_R); - lua_setfield(L, -2, "R"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_R, "R"); - lua_pushinteger(L, KSYS_SCANCODE_S); - lua_setfield(L, -2, "S"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_S, "S"); - lua_pushinteger(L, KSYS_SCANCODE_T); - lua_setfield(L, -2, "T"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_T, "T"); - lua_pushinteger(L, KSYS_SCANCODE_U); - lua_setfield(L, -2, "U"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_U, "U"); - lua_pushinteger(L, KSYS_SCANCODE_V); - lua_setfield(L, -2, "V"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_V, "V"); - lua_pushinteger(L, KSYS_SCANCODE_W); - lua_setfield(L, -2, "W"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_W, "W"); - lua_pushinteger(L, KSYS_SCANCODE_X); - lua_setfield(L, -2, "X"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_X, "X"); - lua_pushinteger(L, KSYS_SCANCODE_Y); - lua_setfield(L, -2, "Y"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_Y, "Y"); - lua_pushinteger(L, KSYS_SCANCODE_Z); - lua_setfield(L, -2, "Z"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_Z, "Z"); - lua_pushinteger(L, KSYS_SCANCODE_F1); - lua_setfield(L, -2, "F1"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F1, "F1"); - lua_pushinteger(L, KSYS_SCANCODE_F2); - lua_setfield(L, -2, "F2"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F2, "F2"); - lua_pushinteger(L, KSYS_SCANCODE_F3); - lua_setfield(L, -2, "F3"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F3, "F3"); - lua_pushinteger(L, KSYS_SCANCODE_F4); - lua_setfield(L, -2, "F4"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F4, "F4"); - lua_pushinteger(L, KSYS_SCANCODE_F5); - lua_setfield(L, -2, "F5"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F5, "F5"); - lua_pushinteger(L, KSYS_SCANCODE_F6); - lua_setfield(L, -2, "F6"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F6, "F6"); - lua_pushinteger(L, KSYS_SCANCODE_F7); - lua_setfield(L, -2, "F7"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F7, "F7"); - lua_pushinteger(L, KSYS_SCANCODE_F8); - lua_setfield(L, -2, "F8"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F8, "F8"); - lua_pushinteger(L, KSYS_SCANCODE_F9); - lua_setfield(L, -2, "F9"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F9, "F9"); - lua_pushinteger(L, KSYS_SCANCODE_F10); - lua_setfield(L, -2, "F10"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F10, "F10"); - lua_pushinteger(L, KSYS_SCANCODE_F11); - lua_setfield(L, -2, "F11"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F11, "F11"); - lua_pushinteger(L, KSYS_SCANCODE_F12); - lua_setfield(L, -2, "F12"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_F12, "F12"); - lua_pushinteger(L, KSYS_SCANCODE_LSHIFT); - lua_setfield(L, -2, "LeftShift"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_LSHIFT, "LeftShift"); - lua_pushinteger(L, KSYS_SCANCODE_RSHIFT); - lua_setfield(L, -2, "RightShift"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_RSHIFT, "RightShift"); - lua_pushinteger(L, KSYS_SCANCODE_BACKSLASH); - lua_setfield(L, -2, "Backslash"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_BACKSLASH, "Backslash"); - lua_pushinteger(L, KSYS_SCANCODE_COMMA); - lua_setfield(L, -2, "Comma"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_COMMA, "Comma"); - lua_pushinteger(L, KSYS_SCANCODE_SLASH); - lua_setfield(L, -2, "Slash"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_SLASH, "Slash"); - lua_pushinteger(L, KSYS_SCANCODE_LALT); - lua_setfield(L, -2, "LeftAlt"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_LALT, "LeftAlt"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_RALT); - lua_setfield(L, -2, "RightAlt"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_RALT, "RightAlt"); - lua_pushinteger(L, KSYS_SCANCODE_LCTRL); - lua_setfield(L, -2, "LeftCtrl"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_LCTRL, "LeftCtrl"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_RCTRL); - lua_setfield(L, -2, "RightCtrl"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_RCTRL, "RightCtrl"); - lua_pushinteger(L, KSYS_SCANCODE_CAPSLOCK); - lua_setfield(L, -2, "CapsLock"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_CAPSLOCK, "CapsLock"); - lua_pushinteger(L, KSYS_SCANCODE_NUMLOCK); - lua_setfield(L, -2, "NumLock"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMLOCK, "NumLock"); - lua_pushinteger(L, KSYS_SCANCODE_POINT); - lua_setfield(L, -2, "Point"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_POINT, "Point"); - lua_pushinteger(L, KSYS_SCANCODE_ENTER); - lua_setfield(L, -2, "Enter"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_ENTER, "Enter"); - lua_pushinteger(L, KSYS_SCANCODE_ESC); - lua_setfield(L, -2, "Esc"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_ESC, "Esc"); - lua_pushinteger(L, KSYS_SCANCODE_TAB); - lua_setfield(L, -2, "Tab"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_TAB, "Tab"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_HOME); - lua_setfield(L, -2, "Home"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_HOME, "Home"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_PGUP); - lua_setfield(L, -2, "PageUp"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_PGUP, "PageUp"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_PGDOWN); - lua_setfield(L, -2, "PageDown"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_PGDOWN, "PageDown"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_END); - lua_setfield(L, -2, "End"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_END, "End"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_UP); - lua_setfield(L, -2, "Up"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_UP, "Up"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_DOWN); - lua_setfield(L, -2, "Down"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_DOWN, "Down"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_LEFT); - lua_setfield(L, -2, "Left"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_LEFT, "Left"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_RIGHT); - lua_setfield(L, -2, "Right"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_RIGHT, "Right"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_DELETE); - lua_setfield(L, -2, "Delete"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_DELETE, "Delete"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_INSERT); - lua_setfield(L, -2, "Insert"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_INSERT, "Insert"); - lua_pushinteger(L, KSYS_SCANCODE_MINUS); - lua_setfield(L, -2, "Minus"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_MINUS, "Minus"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_NUMPAD_ENTER); - lua_setfield(L, -2, "NumpadEnter"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_NUMPAD_ENTER, "NumpadEnter"); - lua_pushinteger(L, KSYS_SCANCODE_EXT_NUMPAD_DIV); - lua_setfield(L, -2, "NumpadDiv"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_EXT_NUMPAD_DIV, "NumpadDiv"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_MULT); - lua_setfield(L, -2, "NumpadMult"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_MULT, "NumpadMult"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_MINUS); - lua_setfield(L, -2, "NumpadMinus"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_MINUS, "NumpadMinus"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_PLUS); - lua_setfield(L, -2, "NumpadPlus"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_PLUS, "NumpadPlus"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_0); - lua_setfield(L, -2, "Numpad_0"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_0, "Numpad_0"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_1); - lua_setfield(L, -2, "Numpad_1"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_1, "Numpad_1"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_2); - lua_setfield(L, -2, "Numpad_2"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_2, "Numpad_2"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_3); - lua_setfield(L, -2, "Numpad_3"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_3, "Numpad_3"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_4); - lua_setfield(L, -2, "Numpad_4"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_4, "Numpad_4"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_5); - lua_setfield(L, -2, "Numpad_5"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_5, "Numpad_5"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_6); - lua_setfield(L, -2, "Numpad_6"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_6, "Numpad_6"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_7); - lua_setfield(L, -2, "Numpad_7"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_7, "Numpad_7"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_8); - lua_setfield(L, -2, "Numpad_8"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_8, "Numpad_8"); - lua_pushinteger(L, KSYS_SCANCODE_NUMPAD_9); - lua_setfield(L, -2, "Numpad_9"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SCANCODE_NUMPAD_9, "Numpad_9"); - lua_setfield(L, -2, "scancode"); + lua_setfield(L, -2, "scancode"); } #endif // _SYSCALLS_ENUM_SCANCODES_ diff --git a/src/syscalls.c b/src/syscalls.c index 523e14a..640534a 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -96,6 +96,7 @@ static int syscalls_endRedraw(lua_State* L) static int syscalls_SetSkin(lua_State* L) { unsigned ret; + asm_inline( "int $0x40" : "=a"(ret) @@ -140,34 +141,39 @@ static int syscalls_SetWorkArea(lua_State* L) uint32_t bottom = luaL_checkinteger(L, 4); asm_inline( - "int $0x40" ::"a"(48), "b"(6), "c"(left * 65536 + right), "d"(top * 65536 + bottom)); + "int $0x40" :: + "a"(48), + "b"(6), + "c"((left << 16) | right), + "d"((top << 16) | bottom) + ); return 0; } static int syscalls_GetWorkArea(lua_State* L) { - ksys_pos_t leftlright; - ksys_pos_t toplbottom; + ksys_pos_t leftAndRight; + ksys_pos_t topAndBottom; asm_inline( "int $0x40" - :"=a"(leftlright), "=b"(toplbottom) + :"=a"(leftAndRight), "=b"(topAndBottom) : "a"(48), "b"(5) ); lua_createtable(L, 0, 4); - lua_pushinteger(L, leftlright.x); + lua_pushinteger(L, leftAndRight.x); lua_setfield(L, -2, "Left"); - lua_pushinteger(L, leftlright.y); + lua_pushinteger(L, leftAndRight.y); lua_setfield(L, -2, "Right"); - lua_pushinteger(L, toplbottom.x); + lua_pushinteger(L, topAndBottom.x); lua_setfield(L, -2, "Top"); - lua_pushinteger(L, toplbottom.y); + lua_pushinteger(L, topAndBottom.y); lua_setfield(L, -2, "Bottom"); return 1; @@ -196,6 +202,7 @@ static int syscalls_deleteButton(lua_State* L) static int syscalls_SetButtonsStyle(lua_State* L) { uint32_t style = luaL_checkinteger(L, 1); + asm_inline( "int $0x40" ::"a"(48), "b"(1), "c"(style)); @@ -229,7 +236,7 @@ static int syscalls_WindowMsg(lua_State* L) asm_inline( "int $0x40" - :"=a"(ret) + : "=a"(ret) : "a"(72), "b"(1), "c"(event), "d"(code) ); @@ -247,50 +254,49 @@ static int syscalls_threadInfo(lua_State* L) lua_createtable(L, 0, 13); lua_pushstring(L, t.name); - lua_setfield(L, -2, "name"); + lua_setfield(L, -2, "Name"); lua_pushinteger(L, t.cpu_usage); - lua_setfield(L, -2, "cpu_usage"); + lua_setfield(L, -2, "CpuUsage"); lua_pushinteger(L, t.memused); - lua_setfield(L, -2, "memused"); + lua_setfield(L, -2, "MemUsed"); lua_pushinteger(L, t.pid); - lua_setfield(L, -2, "pid"); + lua_setfield(L, -2, "PID"); lua_pushinteger(L, t.key_input_mode); - lua_setfield(L, -2, "keyInputMode"); + lua_setfield(L, -2, "KeyInputMode"); lua_pushinteger(L, t.pos_in_window_stack); - lua_setfield(L, -2, "posInWindowStack"); + lua_setfield(L, -2, "PosInWindowStack"); lua_pushinteger(L, t.slot_num_window_stack); - lua_setfield(L, -2, "slotNumWindowStack"); + lua_setfield(L, -2, "SlotNumWindowStack"); lua_pushinteger(L, t.slot_state); - lua_setfield(L, -2, "slotState"); + lua_setfield(L, -2, "SlotState"); lua_pushinteger(L, t.window_state); - lua_setfield(L, -2, "windowState"); + lua_setfield(L, -2, "WindowState"); lua_pushinteger(L, t.winx_size); - lua_setfield(L, -2, "winXSize"); + lua_setfield(L, -2, "WinXSize"); lua_pushinteger(L, t.winy_size); - lua_setfield(L, -2, "winYSize"); + lua_setfield(L, -2, "WinYSize"); lua_pushinteger(L, t.winx_start); - lua_setfield(L, -2, "winXPos"); + lua_setfield(L, -2, "WinXPos"); lua_pushinteger(L, t.winy_start); - lua_setfield(L, -2, "winYPos"); + lua_setfield(L, -2, "WinYPos"); return 1; } static int syscalls_KillBySlot(lua_State* L) { - _ksys_kill_by_slot( luaL_checkinteger(L, 1)); @@ -320,7 +326,10 @@ static int syscalls_checkEvent(lua_State* L) static int syscalls_waitEventTimeout(lua_State* L) { - lua_pushinteger(L, _ksys_wait_event_timeout(luaL_checkinteger(L, 1))); + lua_pushinteger( + L, + _ksys_wait_event_timeout(luaL_checkinteger(L, 1)) + ); return 1; } @@ -689,7 +698,7 @@ static int syscalls_getMouseEvents(lua_State* L) createMouseState(state, L); lua_pushboolean(L, state & (1 << 8)); - lua_setfield(L, -2, "getMouseEvents"); + lua_setfield(L, -2, "LeftButtonPressed"); lua_pushboolean(L, state & (1 << 9)); lua_setfield(L, -2, "RightButtonPressed"); @@ -757,13 +766,13 @@ static int syscalls_GetMouseSettings(lua_State* L) lua_createtable(L, 0, 3); lua_pushinteger(L, getMouseSettings(KSYS_MOUSE_GET_SPEED)); - lua_setfield(L, -2, "speed"); + lua_setfield(L, -2, "Speed"); lua_pushinteger(L, getMouseSettings(KSYS_MOUSE_GET_SENS)); - lua_setfield(L, -2, "sensitivity"); + lua_setfield(L, -2, "Sensitivity"); lua_pushinteger(L, getMouseSettings(KSYS_MOUSE_GET_DOUBLE_CLICK_DELAY)); - lua_setfield(L, -2, "doubleClickDelay"); + lua_setfield(L, -2, "DoubleClickDelay"); return 1; } @@ -826,7 +835,7 @@ static int syscalls_SetMouseSettings(lua_State* L) luaL_checktype(L, 1, LUA_TTABLE); lua_getfield(L, 1, "Speed"); - lua_getfield(L, 1, "Sens"); + lua_getfield(L, 1, "Sensitivity"); lua_getfield(L, 1, "DoubleClickDelay"); _ksys_set_mouse_settings(KSYS_MOUSE_GET_SPEED, luaL_checkinteger(L, -3)); @@ -986,10 +995,7 @@ static int syscalls_GetTXByteCount(lua_State* L) : "=a"(num), "=b"(NUM) : "a"(74), "b"(8 | device << 8)); - if (num == -1) - lua_pushnil(L); - else - lua_pushinteger(L, (uint64_t)(num | NUM << 31)); + syscalls_ReturnIntegerValueOrNil(num, num | NUM << 31, L); return 1; } @@ -1005,10 +1011,7 @@ static int syscalls_GetRXByteCount(lua_State* L) : "=a"(num), "=b"(NUM) : "a"(74), "b"(9 | device << 8)); - if (num == -1) - lua_pushnil(L); - else - lua_pushinteger(L, (uint64_t)(num | NUM << 31)); + syscalls_ReturnIntegerValueOrNil(num, num | NUM << 31, L); return 1; } @@ -1024,10 +1027,7 @@ static int syscalls_GetTXErrorPacketCount(lua_State* L) : "=a"(num), "=b"(NUM) : "a"(74), "b"(11 | device << 8)); - if (num == -1) - lua_pushnil(L); - else - lua_pushinteger(L, (uint64_t)(num | NUM << 31)); + syscalls_ReturnIntegerValueOrNil(num, num | NUM << 31, L); return 1; } @@ -1043,10 +1043,7 @@ static int syscalls_GetTXDropPacketCount(lua_State* L) : "=a"(num), "=b"(NUM) : "a"(74), "b"(12 | device << 8)); - if (num == -1) - lua_pushnil(L); - else - lua_pushinteger(L, (uint64_t)(num | NUM << 31)); + syscalls_ReturnIntegerValueOrNil(num, num | NUM << 31, L); return 1; } @@ -1062,10 +1059,7 @@ static int syscalls_GetTXMissPacketCount(lua_State* L) : "=a"(num), "=b"(NUM) : "a"(74), "b"(13 | device << 8)); - if (num == -1) - lua_pushnil(L); - else - lua_pushinteger(L, (uint64_t)(num | NUM << 31)); + syscalls_ReturnIntegerValueOrNil(num, num | NUM << 31, L); return 1; } @@ -1081,10 +1075,7 @@ static int syscalls_GetRXErrorPacketCount(lua_State* L) : "=a"(num), "=b"(NUM) : "a"(74), "b"(14 | device << 8)); - if (num == -1) - lua_pushnil(L); - else - lua_pushinteger(L, (uint64_t)(num | NUM << 31)); + syscalls_ReturnIntegerValueOrNil(num, num | NUM << 31, L); return 1; } @@ -1125,10 +1116,10 @@ enum ConnectionStatus { NoConnect = 0, Unknown = 1, - Mb10 = 4, // 10Mb - Mb100 = 5, // 100Mb - Gb = 6, // 1Gb - FullDuplex = 0b10 // + Mb10 = 4, // 10Mb + Mb100 = 5, // 100Mb + Gb = 6, // 1Gb + FullDuplex = 0b10 // }; static int syscalls_GetConnectionStatus(lua_State* L) @@ -1145,6 +1136,7 @@ static int syscalls_GetConnectionStatus(lua_State* L) lua_pushnil(L); else lua_pushinteger(L, num & 0x101); + lua_pushinteger(L, (num & FullDuplex) != 0); return 2; @@ -1421,7 +1413,7 @@ static int syscalls_ReadARPEntry(lua_State* L) if (eax == -1) lua_pushnil(L); else - memcpy(syscalls_pushARPEntry(L), &buffer, sizeof(struct ARP_entry)); + memcpy(syscalls_pushARPEntry(L), &buffer, sizeof(struct ARP_entry)); // не самый лучший вариант реализации, но если если запушить в луа это раньше то как потом запушить nil? хз может потому будут идеи более адекватной реализации return 1; } @@ -1540,8 +1532,8 @@ static const luaL_Reg syscallsLib[] = { /* keyboard funcs */ {"SetKeyInputMode", syscalls_setKeyInputMode}, {"GetKeyInputMode", syscalls_getKeyInputMode}, - {"getKey", syscalls_getKey}, - {"getControlKeyState", syscalls_getControlKeyState}, + {"GetKey", syscalls_getKey}, + {"GetControlKeyState", syscalls_getControlKeyState}, {"SetHotkey", syscalls_SetHotkey}, {"DeleteHotkey", syscalls_DeleteHotkey}, {"LockNormalInput", syscalls_LockNormalInput}, @@ -1646,35 +1638,25 @@ static inline void syscalls_push_events(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, KSYS_EVENT_NONE); - lua_setfield(L, -2, "None"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_NONE, "None"); - lua_pushinteger(L, KSYS_EVENT_REDRAW); - lua_setfield(L, -2, "Redraw"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_REDRAW, "Redraw"); - lua_pushinteger(L, KSYS_EVENT_KEY); - lua_setfield(L, -2, "Key"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_KEY, "Key"); - lua_pushinteger(L, KSYS_EVENT_BUTTON); - lua_setfield(L, -2, "Button"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_BUTTON, "Button"); - lua_pushinteger(L, KSYS_EVENT_DESKTOP); - lua_setfield(L, -2, "Desktop"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_DESKTOP, "Desktop"); - lua_pushinteger(L, KSYS_EVENT_MOUSE); - lua_setfield(L, -2, "Mouse"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_MOUSE, "Mouse"); - lua_pushinteger(L, KSYS_EVENT_IPC); - lua_setfield(L, -2, "IPC"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_IPC, "IPC"); - lua_pushinteger(L, KSYS_EVENT_NETWORK); - lua_setfield(L, -2, "Network"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_NETWORK, "Network"); - lua_pushinteger(L, KSYS_EVENT_DEBUG); - lua_setfield(L, -2, "Debug"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_DEBUG, "Debug"); - lua_pushinteger(L, KSYS_EVENT_IRQBEGIN); - lua_setfield(L, -2, "IRQBegin"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_EVENT_IRQBEGIN, "IRQBegin"); lua_setfield(L, -2, "Event"); } @@ -1683,20 +1665,15 @@ static inline void syscalls_push_buttonCodes(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, KSYS_MOUSE_LBUTTON_PRESSED); - lua_setfield(L, -2, "LeftButton"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_MOUSE_LBUTTON_PRESSED, "LeftButton"); - lua_pushinteger(L, KSYS_MOUSE_RBUTTON_PRESSED); - lua_setfield(L, -2, "RightButton"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_MOUSE_RBUTTON_PRESSED, "RightButton"); - lua_pushinteger(L, KSYS_MOUSE_MBUTTON_PRESSED); - lua_setfield(L, -2, "MiddleButton"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_MOUSE_MBUTTON_PRESSED, "MiddleButton"); - lua_pushinteger(L, KSYS_MOUSE_4BUTTON_PRESSED); - lua_setfield(L, -2, "Button4"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_MOUSE_4BUTTON_PRESSED, "Button4"); - lua_pushinteger(L, KSYS_MOUSE_5BUTTON_PRESSED); - lua_setfield(L, -2, "Button5"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_MOUSE_5BUTTON_PRESSED, "Button5"); lua_setfield(L, -2, "mouseButtons"); } @@ -1705,26 +1682,19 @@ static inline void syscalls_push_slotStates(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, KSYS_SLOT_STATE_RUNNING); - lua_setfield(L, -2, "Running"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_RUNNING, "Running"); - lua_pushinteger(L, KSYS_SLOT_STATE_SUSPENDED); - lua_setfield(L, -2, "Suspended"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_SUSPENDED, "Suspended"); - lua_pushinteger(L, KSYS_SLOT_STATE_SUSPENDED_WAIT_EVENT); - lua_setfield(L, -2, "SuspendedWaitEvent"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_SUSPENDED_WAIT_EVENT, "SuspendedWaitEvent"); - lua_pushinteger(L, KSYS_SLOT_STATE_NORMAL_TERM); - lua_setfield(L, -2, "NormalTerm"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_NORMAL_TERM, "NormalTerm"); - lua_pushinteger(L, KSYS_SLOT_STATE_EXCEPT_TERM); - lua_setfield(L, -2, "ExceptTerm"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_EXCEPT_TERM, "ExceptTerm"); - lua_pushinteger(L, KSYS_SLOT_STATE_EXCEPT_TERM); - lua_setfield(L, -2, "WaitEvent"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_EXCEPT_TERM, "WaitEvent"); - lua_pushinteger(L, KSYS_SLOT_STATE_WAIT_EVENT); - lua_setfield(L, -2, "Free"); + LUA_PUSH_INTEGER_FIELD(L, KSYS_SLOT_STATE_WAIT_EVENT, "Free"); lua_setfield(L, -2, "slotState"); } @@ -1733,21 +1703,15 @@ static inline void syscalls_push_hotkey_states(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, 0); - lua_setfield(L, -2, "hotkeyNoOne"); + LUA_PUSH_INTEGER_FIELD(L, 0, "NoOne"); - lua_pushinteger(L, 1); - lua_setfield(L, -2, "hotkeyOnlyOne"); + LUA_PUSH_INTEGER_FIELD(L, 1, "OnlyOne"); - lua_pushinteger(L, 2); - lua_setfield(L, -2, "hotkeyBoth"); + LUA_PUSH_INTEGER_FIELD(L, 2, "Both"); - lua_pushinteger(L, 4); - lua_setfield(L, -2, "hotkeyLeftOnly"); - - lua_pushinteger(L, 5); - lua_setfield(L, -2, "hotkeyRightOnly"); + LUA_PUSH_INTEGER_FIELD(L, 4, "LeftOnly"); + LUA_PUSH_INTEGER_FIELD(L, 5, "RightOnly"); lua_setfield(L, -2, "hotkeyStates"); } @@ -1756,12 +1720,9 @@ static inline void syscalls_push_buttonsStyle(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, 0); - lua_setfield(L, -2, "ButtonStyleFlat"); - - lua_pushinteger(L, 1); - lua_setfield(L, -2, "ButtonStyleVolume"); + LUA_PUSH_INTEGER_FIELD(L, 0, "Flat"); + LUA_PUSH_INTEGER_FIELD(L, 1, "Volume"); lua_setfield(L, -2, "buttonStyle"); } @@ -1770,20 +1731,15 @@ static inline void syscalls_push_windowStyles(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, 0); - lua_setfield(L, -2, "FixSizes"); + LUA_PUSH_INTEGER_FIELD(L, 0, "FixSizes"); - lua_pushinteger(L, 1); - lua_setfield(L, -2, "NoDraw"); + LUA_PUSH_INTEGER_FIELD(L, 1, "NoDraw"); - lua_pushinteger(L, 2); - lua_setfield(L, -2, "CanChangeSizes"); + LUA_PUSH_INTEGER_FIELD(L, 2, "CanChangeSizes"); - lua_pushinteger(L, 3); - lua_setfield(L, -2, "WithSkin"); + LUA_PUSH_INTEGER_FIELD(L, 3, "WithSkin"); - lua_pushinteger(L, 4); - lua_setfield(L, -2, "WithSkinFixSizes"); + LUA_PUSH_INTEGER_FIELD(L, 4, "WithSkinFixSizes"); lua_setfield(L, -2, "windowStyle"); } @@ -1792,11 +1748,9 @@ static inline void syscalls_push_buttons(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, 0xffff); - lua_setfield(L, -2, "minimization"); + LUA_PUSH_INTEGER_FIELD(L, 0xffff, "minimization"); - lua_pushinteger(L, 1); - lua_setfield(L, -2, "close"); + LUA_PUSH_INTEGER_FIELD(L, 1, "close"); lua_setfield(L, -2, "buttons"); } @@ -1805,20 +1759,15 @@ static inline void syscalls_push_connectionStatus(lua_State* L) { lua_newtable(L); - lua_pushinteger(L, NoConnect); - lua_setfield(L, -2, "no"); + LUA_PUSH_INTEGER_FIELD(L, NoConnect, "No"); - lua_pushinteger(L, Unknown); - lua_setfield(L, -2, "unknown"); + LUA_PUSH_INTEGER_FIELD(L, Unknown, "Unknown"); - lua_pushinteger(L, Mb10); - lua_setfield(L, -2, "10Mb"); + LUA_PUSH_INTEGER_FIELD(L, Mb10, "10Mb"); - lua_pushinteger(L, Mb100); - lua_setfield(L, -2, "100Mb"); + LUA_PUSH_INTEGER_FIELD(L, Mb100, "100Mb"); - lua_pushinteger(L, Gb); - lua_setfield(L, -2, "1Gb"); + LUA_PUSH_INTEGER_FIELD(L, Gb, "1GB"); lua_setfield(L, -2, "connectionStatus"); } diff --git a/src/syscalls.h b/src/syscalls.h index 0888c58..16b2755 100644 --- a/src/syscalls.h +++ b/src/syscalls.h @@ -53,4 +53,8 @@ inline void syscalls_ReturnStringOrNil(LUA_INTEGER cond, const char* value, lua_ } } +#define LUA_PUSH_INTEGER_FIELD(L, val, name) lua_pushinteger(L, val); lua_setfield(L, -2, name); +#define LUA_PUSH_STRING_FIELD(L, val, name) lua_pushstring(L, val); lua_setfield(L, -2, name); +#define LUA_PUSH_NUMBER_FIELD(L, val, name) lua_pushnumber(L, val); lua_setfield(L, -2, name); + #endif // __SYSCALLS_H__ -- 2.49.1 From 9b6f4a440491e912463a17104d123494dc41ff87 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Fri, 18 Apr 2025 16:11:11 +0500 Subject: [PATCH 08/11] add IPC errors && fix build errors --- src/IPC/IPC_msg.c | 2 +- src/IPC/IPC_msg.h | 4 +--- src/IPC/ipc.h | 16 +++++++++++++++- src/syscalls.c | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c index 50e1aec..9a08a97 100644 --- a/src/IPC/IPC_msg.c +++ b/src/IPC/IPC_msg.c @@ -147,7 +147,7 @@ static const luaL_Reg syscalls_IPC_msg_m[] = { {NULL, NULL} }; -struct registers* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) +ksys_ipc_msg* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) { DEBUG_LINE("push IPC_msg entry"); diff --git a/src/IPC/IPC_msg.h b/src/IPC/IPC_msg.h index 0f911c4..baa5fc7 100644 --- a/src/IPC/IPC_msg.h +++ b/src/IPC/IPC_msg.h @@ -7,9 +7,7 @@ #define syscalls_IPC_msg_metatable_name "syscalls IPC_msg metatable" #define syscalls_IPC_msg_name "IPC_msg" -int syscalls_newIPC_msg(lua_State* L); - -ksys_colors_table_t* syscalls_pushIPC_msg(lua_State* L); +ksys_ipc_msg* syscalls_pushIPC_msg(lua_State* L, size_t dataLen); /** * Register SystemColors lib diff --git a/src/IPC/ipc.h b/src/IPC/ipc.h index 1c9e14b..5bcfd5a 100644 --- a/src/IPC/ipc.h +++ b/src/IPC/ipc.h @@ -9,10 +9,24 @@ enum SendIPCErrors BufferNotDefined = 1, BufferLocked = 2, BufferOverflow = 3, - PidNotExist = 4 + PIDNotExist = 4 }; int syscalls_DefineIPCBuffer(lua_State* L); int syscalls_SendIPCMessage(lua_State* L); + +inline void syscalls_push_IPC_errors(lua_State* L) +{ + lua_createtable(L, 0, 5); + + LUA_PUSH_INTEGER_FIELD(L, Ok, "Ok"); + LUA_PUSH_INTEGER_FIELD(L, BufferLocked, "BufferLocked"); + LUA_PUSH_INTEGER_FIELD(L, BufferNotDefined, "BufferNotDefined"); + LUA_PUSH_INTEGER_FIELD(L, BufferOverflow, "BufferOverflow"); + LUA_PUSH_INTEGER_FIELD(L, PIDNotExist, "PIDNotExist"); + + lua_setfield(L, -2, "IPCError"); +} + #endif // __IPC_H__ \ No newline at end of file diff --git a/src/syscalls.c b/src/syscalls.c index 640534a..1f26835 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -1629,6 +1629,7 @@ static const luaL_Reg syscallsLib[] = { { "Done", syscalls_Done }, { "DefineBreakpoint", syscalls_DefineBreakpoint }, { "UndefBreakpoint", syscalls_UndefBreakpoint }, + /* IPC */ { "DefineIPCBuffer", syscalls_DefineIPCBuffer }, { "SendIPCMessage", syscalls_SendIPCMessage }, { NULL, NULL } @@ -1788,6 +1789,7 @@ LUALIB_API int luaopen_syscalls(lua_State* L) syscalls_push_connectionStatus(L); syscalls_push_graphic(L); syscalls_register_IPC_msg(L); + syscalls_push_IPC_errors(L); syscalls_register_ARPEntry(L); syscalls_register_SystemColors(L); -- 2.49.1 From 521d00fdb869bd9b91fb42490d85c14d3925ea37 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Thu, 8 May 2025 23:48:52 +0500 Subject: [PATCH 09/11] create buffer class && some warnings fixes && update manual && create vscode tasks --- .vscode/settings.json | 5 +- .vscode/tasks.json | 30 ++++++ Makefile | 10 +- doc/manual.md | 196 +++++++++++++++++++++++------------ lua | 1 + src/ARP_entry.c | 4 +- src/ARP_entry.h | 2 +- src/IPC/IPC_buffer.c | 232 ++++++++++++++++++++++++++++++++++++++++++ src/IPC/IPC_buffer.h | 22 ++++ src/IPC/IPC_msg.c | 38 +++++-- src/IPC/ipc.c | 49 ++++----- src/IPC/ipc.h | 2 +- src/debug/registers.h | 2 +- src/syscalls.c | 15 ++- tests/ipc_get.lua | 26 +++++ tests/ipc_send.lua | 20 ++++ 16 files changed, 547 insertions(+), 107 deletions(-) create mode 100644 .vscode/tasks.json create mode 160000 lua create mode 100644 src/IPC/IPC_buffer.c create mode 100644 src/IPC/IPC_buffer.h create mode 100644 tests/ipc_get.lua create mode 100644 tests/ipc_send.lua diff --git a/.vscode/settings.json b/.vscode/settings.json index 844e796..faf551b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,9 @@ "syscalls.h": "c", "registers.h": "c", "version_type.h": "c", - "library_version.h": "c" + "library_version.h": "c", + "scancodes.h": "c", + "ipc.h": "c" }, "cSpell.words": [ "syscalls", @@ -30,6 +32,7 @@ "luaL_checkudata", "luaL_optinteger", "luaL_pushfail", + "lua_pushlightuserdata", "lua_pushboolean", "lua_pushinteger", "lua_pushnumber", diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..63afc54 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build", + "type": "process", + "command": "make", + "args": [ + "syscalls.dll" + ], + "group": { + "isDefault": true, + "kind": "build" + }, + "problemMatcher": "$gcc" + }, + { + "label": "Clean", + "type": "process", + "command": "make", + "args": [ + "clean" + ], + "group": { + "isDefault": false, + "kind": "build" + } + } + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile index e0d1b27..7a7e0f2 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ MYOBJS = Socket_O = src/sockets/socket.o src/sockets/socket_lua.o src/sockets/sockaddr.o -IPC_O = src/IPC/ipc.o src/IPC/IPC_msg.o +IPC_O = src/IPC/ipc.o src/IPC/IPC_msg.o src/IPC/IPC_buffer.o Debug_O = src/debug/debug.o src/debug/registers.o Version_O = src/version/coreversion.o src/version/version_type.o @@ -61,10 +61,11 @@ src/graphic.o: src/graphic.c src/graphic.h src/debug/debug.o: src/debug/debug.c src/debug/debug.h src/debug/registers.h src/IPC/ipc.o: src/IPC/ipc.c src/IPC/ipc.h src/IPC/IPC_msg.h src/IPC/IPC_msg.o: src/IPC/IPC_msg.c src/IPC/IPC_msg.h +src/IPC/IPC_buffer.o: src/IPC/IPC_buffer.c src/IPC/IPC_buffer.h src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h src/debug.h src/version/coreversion.o: src/version/coreversion.c src/version/coreversion.h src/version/version_type.o: src/version/version_type.c src/version/version_type.h src/debug.h -src/background/background.o: src/background/background.c src/background/background.h +src/background/background.o: src/background/background.c src/background/background.h ## headers @@ -73,8 +74,9 @@ src/sockets/socket_lua.h: src/syscalls.h src/sockets/socket.h src/sockets/sockaddr.h: src/sockets/socket.h src/syscalls.h src/debug/debug.h: src/syscalls.h src/debug/registers.h: src/syscalls.h -src/IPC/IPC_msg.h: src/syscalls.h +src/IPC/IPC_msg.h: src/syscalls.h +src/IPC/IPC_buffer.h: src/syscalls.h src/IPC/ipc.h: src/syscalls.h src/version/coreversion.h: src/version/version_type.h src/version/version_type.h: src/syscalls.h -src/background/background.h: src/syscalls.h +src/background/background.h: src/syscalls.h diff --git a/doc/manual.md b/doc/manual.md index 008b973..84d88b1 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -4,24 +4,24 @@ This is KolibriOS lua syscalls library. Usually you shouldn't use this library Better if you read before [KolibriOS syscalls wiki](http://wiki.kolibrios.org/wiki/) -include: +include this library: ```lua local syscalls = require("syscalls") ``` ## Events -### `SetEventMask(newMask)` +### SetEventMask(newMask) -### `CheckEvent()` +### CheckEvent() -check event +check [event](#events-list) -### `WaitEvent()` +### WaitEvent() -Endless wait event +Endless wait [event](#events-list) -### `WaitEventTimeout(timeout)` +### WaitEventTimeout(timeout) Wait timeout 1/100 sec @@ -44,37 +44,37 @@ syscalls.Event. ## Window -### `CreateWindow(x, y, width, height, workColor, style, borderColor, titleColor)` +### CreateWindow(x, y, width, height, workColor, [style](#window-style), borderColor, titleColor) Define window -`borderColor` and `borderColor` only for `FixSizes` and `CanChangeSizes` (without skin styles) style. +`borderColor` and borderColor` only for FixSizes` and CanChangeSizes` (without skin styles) style. -### `StartRedraw()` +### StartRedraw() -Start window redraw. Just call it before `CreateWindow` +Start window redraw. Just call it before CreateWindow` -### `EndRedraw()` +### EndRedraw() End window redraw. Just call it after you done redraw window. -### `ChangeWindow(newX, newY, newWidth, newHeight)` +### ChangeWindow(newX, newY, newWidth, newHeight) -### `FocusWindow(slot)` +### FocusWindow(slot) -### `UnfocusWindow(slot)` +### UnfocusWindow(slot) -### `SetWindowTitle(newTitle)` +### SetWindowTitle(newTitle) -### `GetSkinHeight()` +### GetSkinHeight() return skin height. -### `SetSkin(path)` +### SetSkin(path) return error code -### `GetSkinTitleArea()` +### GetSkinTitleArea() return table: ```lua @@ -86,7 +86,7 @@ return table: } ``` -### Style +### Window Style ```lua syscalls.windowStyle. @@ -134,34 +134,35 @@ syscalls.textSize. + `56x112` + `64x128` -### `DrawText(text, xPos, yPos, textColor, textScale, textLen, backgroundColor, encoding)` +### DrawText(text, xPos, yPos, textColor, [textScale](#text-sizes), textLen, backgroundColor, [encoding](#text-encoding)) -### `DrawTextFixSize(text, xPos, yPos, textColor, textSize, textLen, backgroundColor, encoding)` +### DrawTextFixSize(text, xPos, yPos, textColor, [textScale](#text-sizes), textLen, backgroundColor, [encoding](#text-encoding)) Draw text. textSize, textLen, backgroundColor, encoding are optional. -### `DrawLine(x1, y1, x2, y2)` +### DrawLine(x1, y1, x2, y2) -### `DrawRectangle(x, y, widht, height, color)` +### DrawRectangle(x, y, width, height, color) -### `ReadPoint(x, y)` +### ReadPoint(x, y) return color ## Buttons -### `DefineButton(x, y, widht, height, id, color)` +### DefineButton(x, y, width, height, id, color) -### `DeleteButton(id)` +### DeleteButton(id) -### `GetButton()` +### GetButton() return pressed button or `nil` -### `SetButtonStyle(style)` +### SetButtonStyle([style](#button-styles)) +Set buttons style ### buttons @@ -174,20 +175,23 @@ syscalls.buttons. + `close` + `minimization` -### ButtonStyles +### Button styles ```lua syscalls.buttonStyle. ``` ++ `Flat` ++ `Volume` + ## Keyboard -### `GetKey()` +### GetKey() return: -+ nil if buffer empty ++ `nil` if buffer empty + if hotkey return second number -+ if key pressed and key input mode is ascii return string(1 char), else return scancode ++ if key pressed and key input mode is ascii return string(1 char), else return [scancode](#scancodes) example: @@ -199,15 +203,15 @@ if key then end if hotkey then - print(hotkey pressed) + print("hotkey pressed") end ``` -### `SetKeyInputMode(mode)` +### SetKeyInputMode(mode) by default is `ASCII` -### `GetKeyInputMode()` +### GetKeyInputMode() return key input mode. @@ -310,17 +314,18 @@ syscalls.SystemColors.new( ) ``` -### `GetSystemColors()` +### GetSystemColors() -return SystemColors +return [SystemColors](#systemcolors-type) -### `SetSystemColors(SystemColors)` +### SetSystemColors([SystemColors](#systemcolors-type)) ## Threads -### `ThreadInfo(pid)` +### ThreadInfo(pid) return table: + ```lua { name: string, @@ -339,7 +344,7 @@ return table: } ``` -### `KillBySlot(slot)` +### KillBySlot(slot) ### Slot states @@ -357,7 +362,9 @@ syscalls.slotState. ## Sockets -### `OpenSocket(domain, type, protocol)` +### OpenSocket([domain](#socket-types), [type](#address-families), [protocol](#ip-protocols)) + +return socket ```lua local socket, err = syscalls.OpenSocket( @@ -373,9 +380,9 @@ else end ``` -### `CloseSocket(socket)` +### CloseSocket(socket) -### `PairSocket()` +### PairSocket() ```lua local first, second = PairSocket() @@ -387,19 +394,19 @@ else end ``` -### `Bind(socket, address)` +### Bind(socket, address) -### `Listen(socket, backlog)` +### Listen(socket, backlog) -### `Connect(socket, address)` +### Connect(socket, address) -### `Accept(socket, , flags)` +### Accept(socket, , flags) -### `Receive(socket, , flags)` +### Receive(socket, , flags) -### `SetSocketOption(socket, opt)` +### SetSocketOption(socket, [opt](#socket-options)) -### `GetSocketOption(socket, opt)` +### GetSocketOption(socket, [opt](#socket-options)) ### Socket types @@ -468,53 +475,116 @@ syscalls.SO. } ``` -### `DebugPutc(char)` +### DebugPutc(char) Put char to debug board -### `DebugPuts(text)` +### DebugPuts(text) Put string to debug board -### `GetRegisters(pid)` +### GetRegisters(pid) The process must be loaded for debugging (as stated in the general description). return registers table -### `SetRegisters(pid, registers)` +### SetRegisters(pid, [registers](#registers-type)) The process must be loaded for debugging (as stated in the general description). -### `Disconnect(pid)` +### Disconnect(pid) The process must be loaded for debugging (as stated in the general description). If the process was suspended, it resumes execution. -### `Stop(pid)` +### Stop(pid) The process must be loaded for debugging (as stated in the general description). -### `Continue(pid)` +### Continue(pid) The process must be loaded for debugging (as stated in the general description). -### `ReadFromMem(pid, bytes, pointer, buffer)` +### ReadFromMem(pid, bytes, pointer, buffer) The process must be loaded for debugging (as stated in the general description). return or `nil` -### `WriteToMem(pid, bytes, pointer, buffer)` +### WriteToMem(pid, bytes, pointer, buffer) The process must be loaded for debugging (as stated in the general description). return or `nil` -### `Done(pid)` +### Done(pid) -### `DefineBreakpoint(pid, index, condition, len)` +### DefineBreakpoint(pid, index, condition, len) -### `UndefBreakpoint(pid, index, condition, len)` +### UndefBreakpoint(pid, index, condition, len) +## IPC + +### DefineIPCBuffer(size) + +Define buffer for IPC receive + +Return [buffer](#ipc_buffer) type + +### SendIPCMessage(pid, [message](#ipc_msg)) + +Send message to process by pid + +return [Error code](#ipc-error-codes) + +### IPC Error codes + +```lua +syscalls.IPCError. +``` + ++ `Ok` ++ `BufferLocked` ++ `BufferNotDefined` ++ `BufferOverflow` ++ `PIDNotExist` + +### IPC_buffer + +```lua +{ + used: number, + lock: boolean, + Lock: (self: IPC_buffer) -> nil + Unlock: (self: IPC_buffer) -> nil + GetMessage: (self: IPC_buffer, i: number) -> IPC_msg + GetLastMessage: (self: IPC_buffer) -> IPC_msg +} +``` + +#### Lock(self) + +Lock buffer + +#### Unlock(self) + +Unlock buffer + +#### GetMessage(self, i) + +return [message](#ipc_msg) that were send by number i + +#### GetLastMessage(self) + +return last sended [message](#ipc_msg) + +### IPC_msg + +```lua +{ + pid: number, + size: number +} +``` \ No newline at end of file diff --git a/lua b/lua new file mode 160000 index 0000000..eae2ea0 --- /dev/null +++ b/lua @@ -0,0 +1 @@ +Subproject commit eae2ea0aaa2c4d0ad9e9c5c6594f9b2378971ced diff --git a/src/ARP_entry.c b/src/ARP_entry.c index f2443b0..258c437 100644 --- a/src/ARP_entry.c +++ b/src/ARP_entry.c @@ -33,7 +33,7 @@ int syscalls_indexARPEntry(lua_State* L) else if (strcmp(index, "MAC") == 0) { char str[7]; - memset(str, entry->MAC, 6); + memcpy(str, &entry->MAC, 6); str[6] = '\n'; lua_pushstring(L, str); } @@ -79,7 +79,7 @@ int syscalls_newindexARPEntry(lua_State* L) } else { - _ksys_debug_puts("err\n"); + luaL_error(L, "wrong index: %s", index); } return 0; diff --git a/src/ARP_entry.h b/src/ARP_entry.h index 090ccd9..97b6979 100644 --- a/src/ARP_entry.h +++ b/src/ARP_entry.h @@ -10,7 +10,7 @@ struct ARP_entry { uint32_t IP; - char MAC[6]; + uint8_t MAC[6]; uint16_t Status; uint16_t TTL; }; diff --git a/src/IPC/IPC_buffer.c b/src/IPC/IPC_buffer.c new file mode 100644 index 0000000..92203e0 --- /dev/null +++ b/src/IPC/IPC_buffer.c @@ -0,0 +1,232 @@ +#include "IPC_buffer.h" +#include +#include "../debug.h" +#include + + + +static int syscalls_indexIPC_buffer(lua_State* L) +{ + struct IPC_buffer* r = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + const char* index = luaL_checkstring(L, 2); + + if (strcmp(index, "used") == 0) + { + lua_pushinteger(L, r->used); + } + else if (strcmp(index, "lock") == 0) + { + lua_pushboolean(L, r->lock); + } + else if (strcmp(index, "size") == 0) + { + lua_pushnumber(L, r->size); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +static int syscalls_newindexIPC_buffer(lua_State* L) +{ + struct IPC_buffer* r = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + const char* index = luaL_checkstring(L, 2); + int val = luaL_checkinteger(L, 3); + + if (strcmp(index, "used") == 0) + { + r->used = val; + } + else if (strcmp(index, "lock") == 0) + { + r->lock = val; + } + else + { + luaL_error(L, "wrong index: %s", index); + } + + return 0; +} + +static bool compare_ipc_buffer(ksys_ipc_buffer* a, ksys_ipc_buffer* b) +{ // блять че за высер... + if (a == b) + return true; + + if (a->used == b->used) + { + return memcmp( + a + (2 * sizeof(unsigned)), + b + (2 * sizeof(unsigned)), + a->used + ); + } + else // длина сообщений не совпадает, занчитт они зразу не могут быть равны + { + return false; + } +} + +static int syscalls_eqIPC_buffer(lua_State* L) +{ + lua_pushboolean( + L, + compare_ipc_buffer( + luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name), + luaL_checkudata(L, 2, syscalls_IPC_buffer_metatable_name) + ) + ); + + return 1; +} + +static int syscalls_IPC_buffer_lock(lua_State* L) +{ + ksys_ipc_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + + buffer->lock = 1; + + return 0; +} + +static int syscalls_IPC_buffer_unlock(lua_State* L) +{ + ksys_ipc_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + + buffer->lock = 0; + + return 0; +} + +/** + * @brief Получить сообщение под номером + * @param buffer + * @param i + * @return указатель на сообщение, если такого сообщения ещё нет, то возвращает + */ +static ksys_ipc_msg* IPC_buffer_get_message(struct IPC_buffer* buffer, int i) +{ + ksys_ipc_msg* j = (ksys_ipc_msg*)(buffer + 8); + unsigned diff = 0; + + for (int k = 0; k < i; k++) + { + diff += j->datalen + 8; // прибавление длинны сообщения и заголовка + j += diff; + + if (diff >= buffer->used) + return 0; + } + + return j; +} + +static ksys_ipc_msg* IPC_buffer_get_last_message(struct IPC_buffer* buffer) +{ + ksys_ipc_msg* j = (ksys_ipc_msg*)(buffer + 8); + unsigned diff = 0; + + while (diff < buffer->used) + { + j += diff; + diff += j->datalen + 8; // прибавление длинны сообщения и заголовка + } + + return j; +} + +static int syscalls_IPC_buffer_get_message(lua_State* L) +{ + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + unsigned i = luaL_checkinteger(L, 1); + + ksys_ipc_msg* msg = IPC_buffer_get_message(buffer, i); + + if (msg) + { + lua_pushlightuserdata(L, msg); + } + else + { + lua_pushnil(L); + } + + return 1; +} + +static int syscalls_IPC_buffer_get_last_message(lua_State* L) +{ + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + + lua_pushlightuserdata(L, IPC_buffer_get_last_message(buffer)); + + return 1; +} + +static int syscalls_IPC_buffer_reset(lua_State* L) +{ + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + + buffer->used = 0; + memset(&buffer->data, 0, buffer->size); + + return 0; +} + +static const luaL_Reg syscalls_IPC_buffer_m[] = { + {"__index", syscalls_indexIPC_buffer}, + {"__newindex", syscalls_newindexIPC_buffer}, + {"__eq", syscalls_eqIPC_buffer}, + {"Lock", syscalls_IPC_buffer_lock}, + {"Unlock", syscalls_IPC_buffer_unlock}, + {"GetMessage", syscalls_IPC_buffer_get_message}, + {"GetLastMessage", syscalls_IPC_buffer_get_last_message}, + {"Reset", syscalls_IPC_buffer_reset}, + {NULL, NULL} +}; + +struct IPC_buffer* syscalls_pushIPC_buffer(lua_State* L, size_t size) +{ + DEBUG_LINE("push " syscalls_IPC_buffer_name " entry"); + + struct IPC_buffer* buffer = lua_newuserdata(L, sizeof(struct IPC_buffer) + size); + + luaL_setmetatable(L, syscalls_IPC_buffer_metatable_name); + + buffer->used = 0; + buffer->size = size; + + return buffer; +} + +static int syscalls_newIPC_buffer(lua_State* L) +{ + size_t size = luaL_checkinteger(L, 1); + struct IPC_buffer* buffer = syscalls_pushIPC_buffer(L, size); + + return 1; +} + +static const luaL_Reg syscalls_IPC_buffer_lib[] = { + {"new", syscalls_newIPC_buffer}, + {NULL, NULL} +}; + +void syscalls_register_IPC_buffer(lua_State* L) +{ + DEBUG_LINE("register " syscalls_IPC_buffer_name " entry"); + + luaL_newlib(L, syscalls_IPC_buffer_lib); + + lua_setfield(L, -2, syscalls_IPC_buffer_name); + + + luaL_newmetatable(L, syscalls_IPC_buffer_metatable_name); + luaL_setfuncs(L, syscalls_IPC_buffer_m, 0); + + lua_pop(L, 1); +} diff --git a/src/IPC/IPC_buffer.h b/src/IPC/IPC_buffer.h new file mode 100644 index 0000000..1686011 --- /dev/null +++ b/src/IPC/IPC_buffer.h @@ -0,0 +1,22 @@ +#ifndef __IPC_BUFFER_H__ +#define __IPC_BUFFER_H__ + +#include "../syscalls.h" + +struct IPC_buffer +{ + size_t size; + ksys_ipc_buffer; +}; + +#define syscalls_IPC_buffer_metatable_name "syscalls IPCbuffer metatable" +#define syscalls_IPC_buffer_name "IPCbuffer" + +struct IPC_buffer* syscalls_pushIPC_buffer(lua_State* L, size_t dataLen); + +/** + * Register SystemColors lib + */ +void syscalls_register_IPC_buffer(lua_State* L); + +#endif // __IPC_BUFFER_H__ diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c index 9a08a97..3772326 100644 --- a/src/IPC/IPC_msg.c +++ b/src/IPC/IPC_msg.c @@ -52,7 +52,7 @@ static int syscalls_newindexIPC_msg(lua_State* L) } else { - if (index_i < r->data) + if (index_i < (unsigned)r->data) { ((lua_Integer*)r + (2 * sizeof(unsigned)))[index_i] = val; } @@ -62,9 +62,7 @@ static int syscalls_newindexIPC_msg(lua_State* L) } } - lua_pushboolean(L, true); - - return 1; + return 0; } static bool compare_ipc_msg(ksys_ipc_msg* a, ksys_ipc_msg* b) @@ -136,10 +134,19 @@ static int syscalls_IPC_msg_ReadInteger(lua_State* L) return 1; } +static int syscalls_IPC_msg_tostring(lua_State* L) +{ + ksys_ipc_msg* msg = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + lua_pushstring(L, &msg->data); + + return 1; +} + static const luaL_Reg syscalls_IPC_msg_m[] = { {"__index", syscalls_indexIPC_msg}, {"__newindex", syscalls_newindexIPC_msg}, {"__eq", syscalls_eqIPC_msg}, + {"__tostring", syscalls_IPC_msg_tostring}, {"ReadByte", syscalls_IPC_msg_ReadByte}, {"ReadWord", syscalls_IPC_msg_ReadWord}, {"ReadDword", syscalls_IPC_msg_ReadDword}, @@ -151,24 +158,39 @@ ksys_ipc_msg* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) { DEBUG_LINE("push IPC_msg entry"); - ksys_ipc_msg* entry = lua_newuserdata(L, sizeof(ksys_ipc_msg) + dataLen); + ksys_ipc_msg* msg = lua_newuserdata(L, sizeof(ksys_ipc_msg) + dataLen); luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); - return entry; + msg->datalen = dataLen; + + return msg; } static int syscalls_newIPC_msg(lua_State* L) { size_t dataLen = luaL_checkinteger(L, 1); - ksys_ipc_msg* r = syscalls_pushIPC_msg(L, dataLen); - r->datalen = dataLen; + ksys_ipc_msg* msg = syscalls_pushIPC_msg(L, dataLen); return 1; } +static int syscalls_fromStringIPC_msg(lua_State* L) +{ + const char* text = luaL_checkstring(L, 1); + size_t len = strlen(text); + + ksys_ipc_msg* r = syscalls_pushIPC_msg(L, len); + + memcpy(&r->data, text, len); + + return 1; +} + + static const luaL_Reg syscalls_IPC_msg_lib[] = { {"new", syscalls_newIPC_msg}, + {"fromString", syscalls_fromStringIPC_msg}, {NULL, NULL} }; diff --git a/src/IPC/ipc.c b/src/IPC/ipc.c index 668d0d8..a3a2728 100644 --- a/src/IPC/ipc.c +++ b/src/IPC/ipc.c @@ -1,47 +1,48 @@ #include "ipc.h" #include "IPC_msg.h" +#include "IPC_buffer.h" #include -inline static void define_ipc(ksys_ipc_buffer* buffer, size_t bufLen) +inline static void define_ipc(struct IPC_buffer* buffer, size_t bufLen) { - asm_inline( - "int $0x40" - ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) - ); + asm_inline( + "int $0x40" + ::"a"(60), "b"(1), "c"(buffer + (sizeof(struct IPC_buffer) - sizeof(ksys_ipc_msg))), "d"(bufLen) + ); } inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) { - enum SendIPCErrors ret; + enum SendIPCErrors ret; - asm_inline( - "int $0x40" - : "=a"(ret) - : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"(sizeof(ksys_ipc_msg) + msg->datalen) - ); + asm_inline( + "int $0x40" + : "=a"(ret) + : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"((sizeof(ksys_ipc_msg)) + msg->datalen) + ); - return ret; + return ret; } int syscalls_DefineIPCBuffer(lua_State* L) { - uint32_t len = luaL_checkinteger(L, 1); - ksys_ipc_buffer* buffer; + uint32_t len = luaL_checkinteger(L, 1); + ksys_ipc_buffer* buffer = syscalls_pushIPC_buffer(L, len); - define_ipc(buffer, len); + define_ipc(buffer, len); - return 0; + return 1; } int syscalls_SendIPCMessage(lua_State* L) { - lua_pushinteger( - L, - send_ipc( - luaL_checkinteger(L, 1), - luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) - ) - ); + lua_pushinteger( + L, + send_ipc( + luaL_checkinteger(L, 1), + luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name) + ) + ); - return 1; + return 1; } diff --git a/src/IPC/ipc.h b/src/IPC/ipc.h index 5bcfd5a..0f0aa42 100644 --- a/src/IPC/ipc.h +++ b/src/IPC/ipc.h @@ -29,4 +29,4 @@ inline void syscalls_push_IPC_errors(lua_State* L) lua_setfield(L, -2, "IPCError"); } -#endif // __IPC_H__ \ No newline at end of file +#endif // __IPC_H__ diff --git a/src/debug/registers.h b/src/debug/registers.h index e5dbd16..9bc5339 100644 --- a/src/debug/registers.h +++ b/src/debug/registers.h @@ -20,6 +20,6 @@ struct registers struct registers* syscalls_pushRegisters(lua_State* L); -inline void syscalls_register_registers(lua_State* L); +void syscalls_register_registers(lua_State* L); #endif // __REGISTERS_H__ diff --git a/src/syscalls.c b/src/syscalls.c index 9f9f077..f495896 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -17,6 +17,7 @@ #include "graphic.h" #include "IPC/ipc.h" #include "IPC/IPC_msg.h" +#include "IPC/IPC_buffer.h" #include "version/coreversion.h" #include "version/library_version.h" #include "background/background.h" @@ -252,7 +253,7 @@ static int syscalls_threadInfo(lua_State* L) { ksys_thread_t t; - _ksys_thread_info(&t, luaL_checkinteger(L, 1)); + _ksys_thread_info(&t, luaL_optinteger(L, 1, -1)); lua_createtable(L, 0, 13); @@ -1760,18 +1761,28 @@ LUALIB_API int luaopen_syscalls(lua_State* L) syscalls_push_events(L); syscalls_push_slotStates(L); + syscalls_register_scancodes(L); syscalls_push_hotkey_states(L); + syscalls_push_buttonsStyle(L); syscalls_push_windowStyles(L); + syscalls_push_buttons(L); + + // net syscalls_push_connectionStatus(L); + syscalls_register_ARPEntry(L); + syscalls_push_graphic(L); + + // Register IPC syscalls_register_IPC_msg(L); + syscalls_register_IPC_buffer(L); syscalls_push_IPC_errors(L); - syscalls_register_ARPEntry(L); syscalls_register_SystemColors(L); + syscalls_register_Version(L); _ksys_set_event_mask(7); // set default event mask diff --git a/tests/ipc_get.lua b/tests/ipc_get.lua new file mode 100644 index 0000000..82e8521 --- /dev/null +++ b/tests/ipc_get.lua @@ -0,0 +1,26 @@ +--[[ + Этот скрипт принимает данные от ipc_send и выводит в консоль +]] + +local syscalls = require("syscalls") + +syscalls.SetEventMask(1 << (syscalls.Event.IPC - 1)) + +local pid = syscalls.ThreadInfo().PID + +local f = io.open("/tmp0/1/lua_test_ipc_pid", "w") + +if f then + f:write(pid) + + f:close() +end + +local buffer = syscalls.DefineIPCBuffer(4096) + + +while true do + if syscalls.WaitEvent() == syscalls.Event.IPC then + print(buffer:GetLastMessage()) + end +end diff --git a/tests/ipc_send.lua b/tests/ipc_send.lua new file mode 100644 index 0000000..c6fd08e --- /dev/null +++ b/tests/ipc_send.lua @@ -0,0 +1,20 @@ +--[[ + скрипт который отправляет данные по IPC +]] + +local syscalls = require("syscalls") + +local f = io.open("/tmp0/1/lua_test_ipc_pid", "r") + +local pid + +if f then + pid = tonumber(f:read("l")) + f:close() +end + +if pid then + while true do + print(syscalls.SendIPCMessage(pid, syscalls.IPC_msg.fromString("Test aboba"))) + end +end -- 2.49.1 From 5e441a984f43dc69b6fe4dda3abd8de9d48bd2c3 Mon Sep 17 00:00:00 2001 From: Egor00f Date: Sun, 18 May 2025 15:05:26 +0500 Subject: [PATCH 10/11] =?UTF-8?q?=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D1=80?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=84=D0=B8=D0=BA=D1=81=D1=8B=20ipc=20(=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=81=D0=B5=D1=80=D0=B0=D0=B2=D0=BD=D0=BE=D0=B5?= =?UTF-8?q?=20=D0=B2=D1=81=D1=80=D0=B0=D1=82=D0=BE=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=D0=96=20=D0=BD=D0=B5=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 6 ++++-- src/IPC/IPC_buffer.c | 44 ++++++++++++++++++++++++++++---------------- src/IPC/IPC_buffer.h | 9 +++++++-- src/IPC/IPC_msg.c | 31 +++++++++++++++++++++++-------- src/IPC/ipc.c | 19 +++++++++++-------- src/syscalls.c | 4 ++-- tests/ipc_get.lua | 13 +++++++++++-- tests/ipc_send.lua | 3 ++- 8 files changed, 88 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 7a7e0f2..247497d 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,9 @@ clean: ## Sources -src/syscalls.o: src/syscalls.c src/syscalls.h src/systemColors.h src/ARP_entry.h src/scancodes.h src/sockets/socket_lua.h src/graphic.h src/version/library_version.h +IPC_H = src/IPC/IPC_msg.h src/IPC/IPC_buffer.h src/IPC/ipc.h + +src/syscalls.o: src/syscalls.c src/syscalls.h src/systemColors.h src/ARP_entry.h src/scancodes.h src/sockets/socket_lua.h src/graphic.h src/version/library_version.h $(IPC_H) src/ARP_entry.o: src/ARP_entry.c src/ARP_entry.h src/debug.h src/systemColors.o: src/systemColors.c src/systemColors.h src/debug.h src/sockets/socket.o: src/sockets/socket.c src/sockets/socket.h @@ -61,7 +63,7 @@ src/graphic.o: src/graphic.c src/graphic.h src/debug/debug.o: src/debug/debug.c src/debug/debug.h src/debug/registers.h src/IPC/ipc.o: src/IPC/ipc.c src/IPC/ipc.h src/IPC/IPC_msg.h src/IPC/IPC_msg.o: src/IPC/IPC_msg.c src/IPC/IPC_msg.h -src/IPC/IPC_buffer.o: src/IPC/IPC_buffer.c src/IPC/IPC_buffer.h +src/IPC/IPC_buffer.o: src/IPC/IPC_buffer.c src/IPC/IPC_buffer.h src/IPC/IPC_msg.h src/debug/registers.o: src/debug/registers.c src/debug/registers.h src/syscalls.h src/debug.h src/version/coreversion.o: src/version/coreversion.c src/version/coreversion.h src/version/version_type.o: src/version/version_type.c src/version/version_type.h src/debug.h diff --git a/src/IPC/IPC_buffer.c b/src/IPC/IPC_buffer.c index 92203e0..164ebff 100644 --- a/src/IPC/IPC_buffer.c +++ b/src/IPC/IPC_buffer.c @@ -1,4 +1,5 @@ #include "IPC_buffer.h" +#include "IPC_msg.h" #include #include "../debug.h" #include @@ -20,7 +21,7 @@ static int syscalls_indexIPC_buffer(lua_State* L) } else if (strcmp(index, "size") == 0) { - lua_pushnumber(L, r->size); + lua_pushinteger(L, r->size); } else { @@ -86,7 +87,7 @@ static int syscalls_eqIPC_buffer(lua_State* L) static int syscalls_IPC_buffer_lock(lua_State* L) { - ksys_ipc_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); buffer->lock = 1; @@ -95,7 +96,7 @@ static int syscalls_IPC_buffer_lock(lua_State* L) static int syscalls_IPC_buffer_unlock(lua_State* L) { - ksys_ipc_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); + struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); buffer->lock = 0; @@ -106,9 +107,9 @@ static int syscalls_IPC_buffer_unlock(lua_State* L) * @brief Получить сообщение под номером * @param buffer * @param i - * @return указатель на сообщение, если такого сообщения ещё нет, то возвращает + * @return указатель на сообщение, если такого сообщения ещё нет, то возвращает 0 */ -static ksys_ipc_msg* IPC_buffer_get_message(struct IPC_buffer* buffer, int i) +static ksys_ipc_msg* IPC_buffer_get_message(ksys_ipc_buffer* buffer, int i) { ksys_ipc_msg* j = (ksys_ipc_msg*)(buffer + 8); unsigned diff = 0; @@ -125,7 +126,7 @@ static ksys_ipc_msg* IPC_buffer_get_message(struct IPC_buffer* buffer, int i) return j; } -static ksys_ipc_msg* IPC_buffer_get_last_message(struct IPC_buffer* buffer) +static ksys_ipc_msg* IPC_buffer_get_last_message(ksys_ipc_buffer* buffer) { ksys_ipc_msg* j = (ksys_ipc_msg*)(buffer + 8); unsigned diff = 0; @@ -142,13 +143,14 @@ static ksys_ipc_msg* IPC_buffer_get_last_message(struct IPC_buffer* buffer) static int syscalls_IPC_buffer_get_message(lua_State* L) { struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); - unsigned i = luaL_checkinteger(L, 1); + unsigned i = luaL_checkinteger(L, 2); - ksys_ipc_msg* msg = IPC_buffer_get_message(buffer, i); + ksys_ipc_msg* msg = IPC_buffer_get_message(IPC_buffer_to_ksys_ipc_buffer(buffer), i); - if (msg) + if (msg != 0) { lua_pushlightuserdata(L, msg); + luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); } else { @@ -162,7 +164,17 @@ static int syscalls_IPC_buffer_get_last_message(lua_State* L) { struct IPC_buffer* buffer = luaL_checkudata(L, 1, syscalls_IPC_buffer_metatable_name); - lua_pushlightuserdata(L, IPC_buffer_get_last_message(buffer)); + ksys_ipc_msg* msg = IPC_buffer_get_last_message(IPC_buffer_to_ksys_ipc_buffer(buffer)); + + if ((unsigned)msg != (unsigned)buffer) + { + lua_pushlightuserdata(L, msg); + luaL_setmetatable(L, syscalls_IPC_msg_metatable_name); + } + else + { + lua_pushnil(L); + } return 1; } @@ -178,14 +190,9 @@ static int syscalls_IPC_buffer_reset(lua_State* L) } static const luaL_Reg syscalls_IPC_buffer_m[] = { - {"__index", syscalls_indexIPC_buffer}, {"__newindex", syscalls_newindexIPC_buffer}, {"__eq", syscalls_eqIPC_buffer}, - {"Lock", syscalls_IPC_buffer_lock}, - {"Unlock", syscalls_IPC_buffer_unlock}, - {"GetMessage", syscalls_IPC_buffer_get_message}, - {"GetLastMessage", syscalls_IPC_buffer_get_last_message}, - {"Reset", syscalls_IPC_buffer_reset}, + {"__index", syscalls_indexIPC_buffer}, {NULL, NULL} }; @@ -213,6 +220,11 @@ static int syscalls_newIPC_buffer(lua_State* L) static const luaL_Reg syscalls_IPC_buffer_lib[] = { {"new", syscalls_newIPC_buffer}, + {"Lock", syscalls_IPC_buffer_lock}, + {"Unlock", syscalls_IPC_buffer_unlock}, + {"GetMessage", syscalls_IPC_buffer_get_message}, + {"GetLastMessage", syscalls_IPC_buffer_get_last_message}, + {"Reset", syscalls_IPC_buffer_reset}, {NULL, NULL} }; diff --git a/src/IPC/IPC_buffer.h b/src/IPC/IPC_buffer.h index 1686011..6693603 100644 --- a/src/IPC/IPC_buffer.h +++ b/src/IPC/IPC_buffer.h @@ -9,8 +9,13 @@ struct IPC_buffer ksys_ipc_buffer; }; -#define syscalls_IPC_buffer_metatable_name "syscalls IPCbuffer metatable" -#define syscalls_IPC_buffer_name "IPCbuffer" +static inline ksys_ipc_buffer* IPC_buffer_to_ksys_ipc_buffer(struct IPC_buffer* buffer) +{ + return (ksys_ipc_buffer*)(buffer + (sizeof(struct IPC_buffer) - sizeof(ksys_ipc_buffer))); +} + +#define syscalls_IPC_buffer_metatable_name "syscalls IPC_buffer metatable" +#define syscalls_IPC_buffer_name "IPC_buffer" struct IPC_buffer* syscalls_pushIPC_buffer(lua_State* L, size_t dataLen); diff --git a/src/IPC/IPC_msg.c b/src/IPC/IPC_msg.c index 3772326..22e003f 100644 --- a/src/IPC/IPC_msg.c +++ b/src/IPC/IPC_msg.c @@ -2,8 +2,7 @@ #include #include "../debug.h" #include -#include - +#include static int syscalls_indexIPC_msg(lua_State* L) @@ -137,20 +136,27 @@ static int syscalls_IPC_msg_ReadInteger(lua_State* L) static int syscalls_IPC_msg_tostring(lua_State* L) { ksys_ipc_msg* msg = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); - lua_pushstring(L, &msg->data); + + char* buff = malloc(msg->datalen + 24 + 1); + + sprintf(buff, "pid: %d, len: %d, text: ", msg->pid, msg->datalen); + + memcpy(buff + 24, &msg->data, msg->datalen); + + buff[msg->datalen + 24] = '\0'; + + lua_pushstring(L, buff); + + free(buff); return 1; } static const luaL_Reg syscalls_IPC_msg_m[] = { - {"__index", syscalls_indexIPC_msg}, {"__newindex", syscalls_newindexIPC_msg}, {"__eq", syscalls_eqIPC_msg}, {"__tostring", syscalls_IPC_msg_tostring}, - {"ReadByte", syscalls_IPC_msg_ReadByte}, - {"ReadWord", syscalls_IPC_msg_ReadWord}, - {"ReadDword", syscalls_IPC_msg_ReadDword}, - {"ReadInteger", syscalls_IPC_msg_ReadInteger}, + {"__index", syscalls_indexIPC_msg}, {NULL, NULL} }; @@ -170,17 +176,22 @@ ksys_ipc_msg* syscalls_pushIPC_msg(lua_State* L, size_t dataLen) static int syscalls_newIPC_msg(lua_State* L) { size_t dataLen = luaL_checkinteger(L, 1); + unsigned pid = luaL_checkinteger(L, 2); ksys_ipc_msg* msg = syscalls_pushIPC_msg(L, dataLen); + msg->pid = pid; + return 1; } static int syscalls_fromStringIPC_msg(lua_State* L) { const char* text = luaL_checkstring(L, 1); + unsigned pid = luaL_checkinteger(L, 2); size_t len = strlen(text); ksys_ipc_msg* r = syscalls_pushIPC_msg(L, len); + r->pid = pid; memcpy(&r->data, text, len); @@ -191,6 +202,10 @@ static int syscalls_fromStringIPC_msg(lua_State* L) static const luaL_Reg syscalls_IPC_msg_lib[] = { {"new", syscalls_newIPC_msg}, {"fromString", syscalls_fromStringIPC_msg}, + {"ReadByte", syscalls_IPC_msg_ReadByte}, + {"ReadWord", syscalls_IPC_msg_ReadWord}, + {"ReadDword", syscalls_IPC_msg_ReadDword}, + {"ReadInteger", syscalls_IPC_msg_ReadInteger}, {NULL, NULL} }; diff --git a/src/IPC/ipc.c b/src/IPC/ipc.c index a3a2728..11c76fe 100644 --- a/src/IPC/ipc.c +++ b/src/IPC/ipc.c @@ -3,22 +3,22 @@ #include "IPC_buffer.h" #include -inline static void define_ipc(struct IPC_buffer* buffer, size_t bufLen) +inline static void define_ipc(ksys_ipc_buffer* buffer, size_t bufLen) { asm_inline( "int $0x40" - ::"a"(60), "b"(1), "c"(buffer + (sizeof(struct IPC_buffer) - sizeof(ksys_ipc_msg))), "d"(bufLen) + ::"a"(60), "b"(1), "c"(buffer), "d"(bufLen) ); } -inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) +inline static enum SendIPCErrors send_ipc(int pid, void* msg, size_t len) { enum SendIPCErrors ret; asm_inline( "int $0x40" : "=a"(ret) - : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"((sizeof(ksys_ipc_msg)) + msg->datalen) + : "a"(60), "b"(2), "c"(pid), "d"(msg), "S"(len) ); return ret; @@ -27,20 +27,23 @@ inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg) int syscalls_DefineIPCBuffer(lua_State* L) { uint32_t len = luaL_checkinteger(L, 1); - ksys_ipc_buffer* buffer = syscalls_pushIPC_buffer(L, len); + struct IPC_buffer* buffer = syscalls_pushIPC_buffer(L, len); - define_ipc(buffer, len); + define_ipc(IPC_buffer_to_ksys_ipc_buffer(buffer), len); return 1; } int syscalls_SendIPCMessage(lua_State* L) { + ksys_ipc_msg* msg = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name); + lua_pushinteger( L, send_ipc( - luaL_checkinteger(L, 1), - luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name) + msg->pid, + msg + 8, + msg->datalen ) ); diff --git a/src/syscalls.c b/src/syscalls.c index f495896..720261b 100644 --- a/src/syscalls.c +++ b/src/syscalls.c @@ -1139,7 +1139,7 @@ typedef enum SYSCALLS_PROTOCOLS ARP = 5 } SYSCALLS_PROTOCOLS; -inline int syscalls_ReadPacketSend(lua_State* L, SYSCALLS_PROTOCOLS protocol) +static inline int syscalls_ReadPacketSend(lua_State* L, SYSCALLS_PROTOCOLS protocol) { uint32_t eax; uint8_t device = luaL_checkinteger(L, 1); @@ -1154,7 +1154,7 @@ inline int syscalls_ReadPacketSend(lua_State* L, SYSCALLS_PROTOCOLS protocol) return 1; } -inline int syscalls_ReadPacketReceive(lua_State* L, SYSCALLS_PROTOCOLS protocol) +static inline int syscalls_ReadPacketReceive(lua_State* L, SYSCALLS_PROTOCOLS protocol) { uint32_t eax; uint8_t device = luaL_checkinteger(L, 1); diff --git a/tests/ipc_get.lua b/tests/ipc_get.lua index 82e8521..ba3cb74 100644 --- a/tests/ipc_get.lua +++ b/tests/ipc_get.lua @@ -4,23 +4,32 @@ local syscalls = require("syscalls") +-- Enable IPC event only syscalls.SetEventMask(1 << (syscalls.Event.IPC - 1)) +-- write pid to /tmp0/1/lua_test_ipc_pid + local pid = syscalls.ThreadInfo().PID local f = io.open("/tmp0/1/lua_test_ipc_pid", "w") if f then - f:write(pid) + f:write(pid); f:close() end +------------ + local buffer = syscalls.DefineIPCBuffer(4096) +syscalls.IPC_buffer.Unlock(buffer) while true do + print("buffer:", buffer.used .. '/' .. buffer.size, "Locked:" .. tostring(buffer.lock)) if syscalls.WaitEvent() == syscalls.Event.IPC then - print(buffer:GetLastMessage()) + syscalls.IPC_buffer.Lock(buffer) + print("message:", syscalls.IPC_buffer.GetLastMessage(buffer)) + syscalls.IPC_buffer.Unlock(buffer) end end diff --git a/tests/ipc_send.lua b/tests/ipc_send.lua index c6fd08e..2bd7187 100644 --- a/tests/ipc_send.lua +++ b/tests/ipc_send.lua @@ -15,6 +15,7 @@ end if pid then while true do - print(syscalls.SendIPCMessage(pid, syscalls.IPC_msg.fromString("Test aboba"))) + local msg = syscalls.IPC_msg.fromString("Test aboba", pid) + print("Send:", msg, "State:", syscalls.SendIPCMessage(msg)) end end -- 2.49.1 From 6ff2ed0386e0ffeaead7e139babf73708385972d Mon Sep 17 00:00:00 2001 From: Egor00f Date: Thu, 29 May 2025 13:46:26 +0500 Subject: [PATCH 11/11] fix build && add auto run for tests --- Makefile | 2 +- tests/run_tests.sh | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/run_tests.sh diff --git a/Makefile b/Makefile index 247497d..cd14c8d 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ KOLIBRIOS_REPO = $(abspath ../kolibrios) SDK_DIR = $(KOLIBRIOS_REPO)/contrib/sdk NewLib_DIR = $(SDK_DIR)/sources/newlib -SYSCFLAGS = -fno-ident -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -I$(NewLib_DIR)/libc/include -I$(TOOLCHAIN_PATH)/include/lua$(LUA_V) +SYSCFLAGS = -fno-ident -fomit-frame-pointer -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32 -I$(NewLib_DIR)/libc/include -I$(TOOLCHAIN_PATH)/include -I$(TOOLCHAIN_PATH)/include/lua$(LUA_V) SYSLIBS = -L $(SDK_DIR)/lib -lgcc -lc.dll -ldll MYCFLAGS = MYLDFLAGS = diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100644 index 0000000..21cd15d --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,11 @@ +#SHS + +# Graphic +../lua helloWorld.lua + +../lua SystemColors.lua + +# IPC tests +../lua ipc_get.lua +../lua ipc_send.lua + -- 2.49.1