diff --git a/src/core/lcurses/lcurses.c b/src/core/lcurses/lcurses.c index e077378..2b500ab 100644 --- a/src/core/lcurses/lcurses.c +++ b/src/core/lcurses/lcurses.c @@ -768,61 +768,61 @@ LC_NUMBER2(LINES, LINES) static int lc_ungetmouse(lua_State *L) { - MEVENT e; - e.bstate = luaL_checklong(L, 1); - e.x = luaL_checkint(L, 2); - e.y = luaL_checkint(L, 3); - e.z = luaL_checkint(L, 4); - e.id = luaL_checkint(L, 5); +MEVENT e; +e.bstate = luaL_checklong(L, 1); +e.x = luaL_checkint(L, 2); +e.y = luaL_checkint(L, 3); +e.z = luaL_checkint(L, 4); +e.id = luaL_checkint(L, 5); - lua_pushboolean(L, !(!ungetmouse(&e))); - return 1; +lua_pushboolean(L, !(!ungetmouse(&e))); +return 1; } static int lc_getmouse(lua_State *L) { - MEVENT e; - if(getmouse(&e) == OK) - { - lua_pushinteger(L, e.bstate); - lua_pushinteger(L, e.x); - lua_pushinteger(L, e.y); - lua_pushinteger(L, e.z); - lua_pushinteger(L, e.id); - return 5; - } +MEVENT e; +if (getmouse(&e) == OK) +{ + lua_pushinteger(L, e.bstate); + lua_pushinteger(L, e.x); + lua_pushinteger(L, e.y); + lua_pushinteger(L, e.z); + lua_pushinteger(L, e.id); + return 5; +} - lua_pushnil(L); - return 1; +lua_pushnil(L); +return 1; } static int lc_mousemask(lua_State *L) { - mmask_t m = luaL_checkint(L, 1); - mmask_t om; - m = mousemask(m, &om); - lua_pushinteger(L, m); - lua_pushinteger(L, om); - return 2; + mmask_t m = luaL_checkint(L, 1); + mmask_t om; + m = mousemask(m, &om); + lua_pushinteger(L, m); + lua_pushinteger(L, om); + return 2; } static int lc_mouseinterval(lua_State *L) { - if (!lua_gettop(L)) - lua_pushinteger(L, mouseinterval(-1)); - else - lua_pushinteger(L, mouseinterval(luaL_checkint(L, 1))); - return 1; + if (!lua_gettop(L)) + lua_pushinteger(L, mouseinterval(-1)); + else + lua_pushinteger(L, mouseinterval(luaL_checkint(L, 1))); + return 1; } static int lc_has_mouse(lua_State *L) { - lua_pushboolean(L, has_mouse()); - return 1; + lua_pushboolean(L, has_mouse()); + return 1; } #endif #endif diff --git a/src/ltui/event.lua b/src/ltui/event.lua index 30ffd4f..f98d43d 100644 --- a/src/ltui/event.lua +++ b/src/ltui/event.lua @@ -76,6 +76,12 @@ event:register("cm_max", "cm_quit", "cm_exit", "cm_enter") -- event.keyboard = object {_init = { "key_code", "key_name", "key_meta" }, type = event.ev_keyboard} +-- define mouse event +-- +-- btn_name = button number and event type +-- btn_code = mouse event code +-- x, y = coordinates +-- event.mouse = object {_init = { "btn_code", "x", "y", "btn_name" }, type = event.ev_mouse} -- define command event diff --git a/src/ltui/program.lua b/src/ltui/program.lua index dd0478e..497a876 100644 --- a/src/ltui/program.lua +++ b/src/ltui/program.lua @@ -141,15 +141,9 @@ function program:event() local key_code, key_name, key_meta = self:_input_key() if key_code then if curses.KEY_MOUSE and key_code == curses.KEY_MOUSE then - local s, x, y = curses.getmouse() - local name - for n, v in pairs(curses) do - if v == s and n:match('BUTTON') then - name = n or name - break - end - end - return event.mouse{s, x, y, name} + local code, x, y = curses.getmouse() + local name = self:_mouse_map()[code] + return event.mouse{code, x, y, name} end return event.keyboard{key_code, key_name, key_meta} end @@ -349,6 +343,20 @@ function program:_key_map() return self._KEYMAP end +-- get mouse map +function program:_mouse_map() + if not self._MOUSEMAP then + -- must be defined dynamically since it depends + -- on curses implementation + self._MOUSEMAP = {} + for n, v in pairs(curses) do + if n:match('MOUSE') and n ~= 'KEY_MOUSE' or n:match('BUTTON') then + self._MOUSEMAP[v] = n + end + end + end + return self._MOUSEMAP +end -- get input key function program:_input_key() @@ -415,6 +423,7 @@ function program:_input_key() end -- return key info + return ch, key_name, alt end diff --git a/tests/getch_test.lua b/tests/events.lua similarity index 81% rename from tests/getch_test.lua rename to tests/events.lua index 2d126e2..55b616f 100644 --- a/tests/getch_test.lua +++ b/tests/events.lua @@ -16,10 +16,10 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- --- Copyright (C) 2015-2020, TBOOX Open Source Group. +-- Copyright (C) 2020, TBOOX Open Source Group. -- --- @author ruki --- @file window.lua +-- @author Lael N. Santos +-- @file events.lua -- require("tests/load") @@ -74,17 +74,17 @@ end -- on event function demo:on_event(e) - if e.type < event.ev_max then - self:teste():text_set('tp: ' .. - tostring(e.type) .. - '; name: ' .. - tostring(e.key_name or e.btn_name) .. - '; code: ' .. - tostring(e.key_code or e.x) .. - '; meta: ' .. - tostring(e.key_code or e.y)) - end - application.on_event(self, e) + if e.type < event.ev_max then + self:teste():text_set('type: ' .. + tostring(e.type) .. + '; name: ' .. + tostring(e.key_name or e.btn_name) .. + '; code: ' .. + tostring(e.key_code or e.x) .. + '; meta: ' .. + tostring(e.key_code or e.y)) + end + application.on_event(self, e) end -- run demo