summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-12-15 23:24:55 +0800
committerruki <[email protected]>2025-12-15 23:24:55 +0800
commitb9116ea123e77faec8a4a0cfbfcbb38f2ebc2e66 (patch)
treebc8976c77263b6941f181fef460dad43dc2f5ee6
parentfb06feeb88ecd2512a4a737d7e543b88892ad783 (diff)
add tty.session_id
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/tty/session_id.c65
-rw-r--r--xmake/core/base/tty.lua17
3 files changed, 84 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 530071a68..ea9dc2af1 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -330,6 +330,7 @@ tb_int_t xm_libc_setbyte(lua_State *lua);
// the tty functions
tb_int_t xm_tty_term_mode(lua_State *lua);
+tb_int_t xm_tty_session_id(lua_State *lua);
// the package functions
tb_int_t xm_package_loadxmi(lua_State *lua);
@@ -649,6 +650,7 @@ static luaL_Reg const g_libc_functions[] = {
// the tty functions
static luaL_Reg const g_tty_functions[] = {
{ "term_mode", xm_tty_term_mode },
+ { "session_id", xm_tty_session_id },
{ tb_null, tb_null },
};
diff --git a/core/src/xmake/tty/session_id.c b/core/src/xmake/tty/session_id.c
new file mode 100644
index 000000000..d075a624a
--- /dev/null
+++ b/core/src/xmake/tty/session_id.c
@@ -0,0 +1,65 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, Xmake Open Source Community.
+ *
+ * @author ruki
+ * @file session_id.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "session_id"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+#ifdef TB_CONFIG_OS_WINDOWS
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// tty.session_id()
+tb_int_t xm_tty_session_id(lua_State *lua) {
+ tb_assert_and_check_return_val(lua, 0);
+
+#ifdef TB_CONFIG_OS_WINDOWS
+ HWND hwnd = GetConsoleWindow();
+ if (hwnd) {
+ // use hex string of hwnd as session id
+ lua_pushfstring(lua, "%p", hwnd);
+ return 1;
+ }
+#else
+ // we use the tty name as session id
+ tb_char_t const* name = ttyname(STDIN_FILENO);
+ if (name) {
+ lua_pushstring(lua, name);
+ return 1;
+ }
+#endif
+
+ // failed
+ lua_pushnil(lua);
+ return 1;
+}
diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua
index d35705770..742b819ab 100644
--- a/xmake/core/base/tty.lua
+++ b/xmake/core/base/tty.lua
@@ -26,6 +26,7 @@ local io = require("base/io")
-- save metatable and builtin functions
tty._term_mode = tty._term_mode or tty.term_mode
+tty._session_id = tty._session_id or tty.session_id
-- @see https://www2.ccs.neu.edu/research/gpc/VonaUtils/vona/terminal/vtansi.htm
-- http://www.termsys.demon.co.uk/vtansi.htm
@@ -538,6 +539,22 @@ function tty.term_mode(stdtype, newmode)
return oldmode
end
+-- get session id
+function tty.session_id()
+ local session_id = tty._SESSION_ID
+ if session_id == nil then
+ if tty._session_id then
+ local sid = tty._session_id()
+ if sid then
+ local hash = require("base/hash")
+ session_id = hash.strhash32(sid)
+ end
+ end
+ tty._SESSION_ID = session_id or 0
+ end
+ return session_id
+end
+
-- return module
return tty