summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_hotkeys.cpp
blob: 4023904fe462d0940250cf04465ca5534248aebb (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "stdafx.h"

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

	lua_getfield(L, -1, "Flags");
	hk.dwFlags = lua_tointeger(L, -1);
	lua_pop(L, 1);

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

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

	lua_getfield(L, -1, "Description");
	hk.ptszDescription = mir_utf8decodeT(lua_tostring(L, -1));
	lua_pop(L, 1);

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

	lua_getfield(L, -1, "Hotkey");
	hk.DefHotKey = lua_tointeger(L, -1);
	lua_pop(L, 1);

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

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

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

	HOTKEYDESC hk;
	MakeHotkey(L, hk);

	int hScriptLangpack = CMLuaScript::GetScriptIdFromEnviroment(L);

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

	return 1;
}

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

	CallService(MS_HOTKEY_UNREGISTER, 0, (LPARAM)name);

	return 0;
}

static const char *mods[] = { "shift", "ctrl", "alt", "ext", NULL };

static int hotkeys_MakeHotkey(lua_State *L)
{
	int mod = 0;
	switch (lua_type(L, 1))
	{
	case LUA_TNUMBER:
		mod = luaL_checkinteger(L, 1);
		break;
	case LUA_TSTRING:
		mod = (1 << (luaL_checkoption(L, 1, NULL, mods) - 1));
		break;
	case LUA_TTABLE:
		lua_pushnil(L);
		while (lua_next(L, 1) != 0)
		{
			mod |= (1 << (luaL_checkoption(L, -1, NULL, mods) - 1));
			lua_pop(L, 1);
		}
		break;
	default:
		luaL_argerror(L, 1, luaL_typename(L, 1));
	}
	int vk = luaL_checknumber(L, 2);

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

	return 1;
}

static luaL_Reg hotkeysApi[] =
{
	{ "MakeHotkey", hotkeys_MakeHotkey },
	{ "Register", hotkeys_Register },
	{ "Unregister", hotkeys_Unregister },

	{ NULL, NULL }
};

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

	return 1;
}