-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaSL.cpp
163 lines (116 loc) · 3.79 KB
/
LuaSL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "LuaSL.hpp"
#include "psapi.h"
using LuaCFunction = int (*)(lua_State* L);
static int isOverlapping(lua_State* L) {
// Check that we have exactly 8 arguments
if (lua_gettop(L) != 8) {
lua_pushstring(L, "Function requires 8 parameters.");
lua_error(L);
}
// Read the parameters from the Lua stack
double x1 = lua_tonumber(L, 1);
double y1 = lua_tonumber(L, 2);
double w1 = lua_tonumber(L, 3);
double h1 = lua_tonumber(L, 4);
double x2 = lua_tonumber(L, 5);
double y2 = lua_tonumber(L, 6);
double w2 = lua_tonumber(L, 7);
double h2 = lua_tonumber(L, 8);
// Calculate the right and bottom edges of the boxes
double right1 = x1 + w1;
double bottom1 = y1 + h1;
double right2 = x2 + w2;
double bottom2 = y2 + h2;
// Check for overlap
bool overlap = !(right1 <= x2 || x1 >= right2 || bottom1 <= y2 || y1 >= bottom2);
// Push the result onto the stack
lua_pushboolean(L, overlap);
// Return number of results
return 1;
}
static int clamp(lua_State* L) {
double v = lua_tonumber(L, 1);
double i = lua_tonumber(L, 2);
double a = lua_tonumber(L, 3);
lua_pushnumber(L, std::max(i, std::min(v, a)));
return 1;
}
static int getfocusedWindowName(lua_State* L) {
HWND hwnd = GetForegroundWindow();
int length = GetWindowTextLength(hwnd);
wchar_t* title = new wchar_t[length + 1];
GetWindowText(hwnd, title, length + 1);
lua_pushstring(L, convert_str(std::wstring(title)).c_str());
return 1;
}
static int getfocusedWindowFileName(lua_State* L) {
HWND hwnd = GetForegroundWindow();
DWORD procid;
GetWindowThreadProcessId(hwnd, &procid);
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, procid);
if (!processHandle) {
lua_pushnil(L);
return 1;
}
char exePath[MAX_PATH];
if (GetModuleFileNameExA(processHandle, NULL, exePath, MAX_PATH) == 0) {
CloseHandle(processHandle);
lua_pushnil(L);
return 1;
}
lua_pushstring(L, std::string(exePath).c_str());
return 1;
}
static int getScreenWidth(lua_State* L) {
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
lua_pushnumber(L, desktop.right);
return 1;
}
static int getScreenHeight(lua_State* L) {
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
lua_pushnumber(L, desktop.bottom);
return 1;
}
static int getClipboard(lua_State* L) {
HANDLE h;
if (!OpenClipboard(NULL))
return 0;
h = GetClipboardData(CF_TEXT);
lua_pushstring(L, (char*)h);
CloseClipboard();
return 1;
}
static int setClipboard(lua_State* L) {
std::string clipcontent = getStringFromLuaState(L, 1);
if (!OpenClipboard(NULL))
return 0;
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, clipcontent.size() + 1);
if (!hg) {
CloseClipboard();
return 0;
}
memcpy(GlobalLock(hg), clipcontent.c_str(), clipcontent.size() + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
return 0;
}
void IncludeLuaSL(lua_State* L) {
std::unordered_map<std::string, LuaCFunction> functionMap;
functionMap["isOverlapping"] = isOverlapping;
functionMap["getfocusedWindowName"] = getfocusedWindowName;
functionMap["getfocusedWindowFileName"] = getfocusedWindowFileName;
functionMap["clamp"] = clamp;
functionMap["getScreenWidth"] = getScreenWidth;
functionMap["getScreenHeight"] = getScreenHeight;
functionMap["getClipboard"] = getClipboard;
functionMap["setClipboard"] = setClipboard;
for (const auto& pair : functionMap) {
lua_register(L, pair.first.c_str(), pair.second);
}
}