summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_hotkeys.cpp
blob: e998b04a9af181d232642b5c92c03723fd86d832 (plain)
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
#include "stdafx.h"

void MakeHotkey(lua_State *L, HOTKEYDESC &hk)
{
	hk.cbSize = sizeof(HOTKEYDESC);

	lua_pushliteral(L, "Flags");
	lua_gettable(L, -2);
	hk.dwFlags = lua_tointeger(L, -1);
	lua_pop(L, 1);

	if (!(hk.dwFlags & HKD_TCHAR))
		hk.dwFlags |= HKD_TCHAR;

	lua_pushliteral(L, "Name");
	lua_gettable(L, -2);
	hk.pszName = mir_utf8decodeA(luaL_checkstring(L, -1));
	lua_pop(L, 1);

	lua_pushliteral(L, "Description");
	lua_gettable(L, -2);
	hk.ptszDescription = mir_utf8decodeT((char*)lua_tostring(L, -1));
	lua_pop(L, 1);

	lua_pushliteral(L, "Section");
	lua_gettable(L, -2);
	hk.ptszSection = mir_utf8decodeT(luaL_optstring(L, -1, MODULE));
	lua_pop(L, 1);

	lua_pushliteral(L, "Hotkey");
	lua_gettable(L, -2);
	hk.DefHotKey = lua_tointeger(L, -1);
	lua_pop(L, 1);

	lua_pushliteral(L, "Service");
	lua_gettable(L, -2);
	hk.pszService = mir_utf8decodeA(luaL_checkstring(L, -1));
	lua_pop(L, 1);

	lua_pushliteral(L, "lParam");
	lua_gettable(L, -2);
	hk.lParam = (LPARAM)lua_touserdata(L, -1);
	lua_pop(L, 1);
}

static int lua_Register(lua_State *L)
{
	if (lua_type(L, 1) != LUA_TTABLE)
	{
		lua_pushlightuserdata(L, 0);
		return 1;
	}

	HOTKEYDESC hk;
	MakeHotkey(L, hk);

	INT_PTR res = ::CallService("CoreHotkeys/Register", (WPARAM)hScriptsLangpack, (LPARAM)&hk);
	lua_pushinteger(L, res);

	return 1;
}

static int lua_Unregister(lua_State *L)
{
	const char* name = luaL_checkstring(L, 1);

	INT_PTR res = ::CallService("CoreHotkeys/Unregister", 0, (LPARAM)name);
	lua_pushinteger(L, res);

	return 1;
}

static int lua_MakeHotkey(lua_State *L)
{
	int mod = luaL_checkinteger(L, 1);
	int vk = luaL_checkinteger(L, 2);

	WORD res = HOTKEYCODE(mod, vk);
	lua_pushinteger(L, res);

	return 1;
}

static luaL_Reg hotkeysApi[] =
{
	{ "Register", lua_Register },
	{ "Unregister", lua_Unregister },

	{ "MakeHotkey", lua_MakeHotkey },

	{ NULL, NULL }
};

LUAMOD_API int luaopen_m_hotkeys(lua_State *L)
{
	luaL_newlib(L, hotkeysApi);

	return 1;
}