mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-11-19 19:45:15 +00:00
b9e7fa0574
Cleanup number namespaces so they use cstdint types instead of stdint.h types. Latter could've caused some issues in the future. Simplify valuator mask checking expression. Signed-off-by: Tin <tin.svagelj@live.com>
209 lines
5.3 KiB
C++
209 lines
5.3 KiB
C++
/*
|
|
* mouse_events.cc: conky support for mouse events
|
|
*
|
|
* Copyright (C) 2020 Tin Svagelj tin.svagelj@live.com
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
#include "mouse-events.h"
|
|
|
|
#include <array>
|
|
#include <ctime>
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
#include "logging.h"
|
|
|
|
extern "C" {
|
|
#include <lua.h>
|
|
}
|
|
|
|
namespace conky {
|
|
|
|
/* Lua helper functions */
|
|
void push_table_value(lua_State *L, std::string key, std::string value) {
|
|
lua_pushstring(L, key.c_str());
|
|
lua_pushstring(L, value.c_str());
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
template <typename T>
|
|
typename std::enable_if<std::is_integral<T>::value>::type push_table_value(
|
|
lua_State *L, std::string key, T value) {
|
|
lua_pushstring(L, key.c_str());
|
|
lua_pushinteger(L, value);
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
template <typename T>
|
|
typename std::enable_if<std::is_floating_point<T>::value>::type
|
|
push_table_value(lua_State *L, std::string key, T value) {
|
|
lua_pushstring(L, key.c_str());
|
|
lua_pushnumber(L, value);
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
void push_table_value(lua_State *L, std::string key, bool value) {
|
|
lua_pushstring(L, key.c_str());
|
|
lua_pushboolean(L, value);
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
template <std::size_t N>
|
|
void push_bitset(lua_State *L, std::bitset<N> it,
|
|
std::array<std::string, N> labels) {
|
|
lua_newtable(L);
|
|
for (std::size_t i = 0; i < N; i++)
|
|
push_table_value(L, labels[i], it.test(i));
|
|
}
|
|
|
|
const std::array<std::string, 6> mod_names = {{
|
|
"shift",
|
|
"control",
|
|
"alt",
|
|
"super",
|
|
"caps_lock",
|
|
"num_lock",
|
|
}};
|
|
|
|
void push_mods(lua_State *L, modifier_state_t mods) {
|
|
lua_pushstring(L, "mods");
|
|
push_bitset(L, mods, mod_names);
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
// Returns ms since Epoch.
|
|
inline std::size_t current_time_ms() {
|
|
struct timespec spec;
|
|
clock_gettime(CLOCK_REALTIME, &spec);
|
|
return static_cast<std::size_t>(static_cast<std::uint64_t>(spec.tv_sec) *
|
|
1'000 +
|
|
spec.tv_nsec / 1'000'000);
|
|
}
|
|
|
|
void push_table_value(lua_State *L, std::string key, mouse_event_t type) {
|
|
lua_pushstring(L, key.c_str());
|
|
switch (type) {
|
|
case MOUSE_PRESS:
|
|
lua_pushstring(L, "button_down");
|
|
break;
|
|
case MOUSE_RELEASE:
|
|
lua_pushstring(L, "button_up");
|
|
break;
|
|
case MOUSE_SCROLL:
|
|
lua_pushstring(L, "mouse_scroll");
|
|
break;
|
|
case MOUSE_MOVE:
|
|
lua_pushstring(L, "mouse_move");
|
|
break;
|
|
case AREA_ENTER:
|
|
lua_pushstring(L, "mouse_enter");
|
|
break;
|
|
case AREA_LEAVE:
|
|
lua_pushstring(L, "mouse_leave");
|
|
break;
|
|
default:
|
|
lua_pushnil(L);
|
|
break;
|
|
}
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
void push_table_value(lua_State *L, std::string key,
|
|
scroll_direction_t direction) {
|
|
lua_pushstring(L, key.c_str());
|
|
switch (direction) {
|
|
case SCROLL_DOWN:
|
|
lua_pushstring(L, "down");
|
|
break;
|
|
case SCROLL_UP:
|
|
lua_pushstring(L, "up");
|
|
break;
|
|
case SCROLL_LEFT:
|
|
lua_pushstring(L, "left");
|
|
break;
|
|
case SCROLL_RIGHT:
|
|
lua_pushstring(L, "right");
|
|
break;
|
|
default:
|
|
lua_pushnil(L);
|
|
break;
|
|
}
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
void push_table_value(lua_State *L, std::string key, mouse_button_t button) {
|
|
lua_pushstring(L, key.c_str());
|
|
switch (button) {
|
|
case BUTTON_LEFT:
|
|
lua_pushstring(L, "left");
|
|
break;
|
|
case BUTTON_RIGHT:
|
|
lua_pushstring(L, "right");
|
|
break;
|
|
case BUTTON_MIDDLE:
|
|
lua_pushstring(L, "middle");
|
|
break;
|
|
case BUTTON_BACK:
|
|
lua_pushstring(L, "back");
|
|
break;
|
|
case BUTTON_FORWARD:
|
|
lua_pushstring(L, "forward");
|
|
break;
|
|
default:
|
|
lua_pushnil(L);
|
|
break;
|
|
}
|
|
lua_settable(L, -3);
|
|
}
|
|
|
|
/* Class methods */
|
|
mouse_event::mouse_event(mouse_event_t type)
|
|
: type(type), time(current_time_ms()){};
|
|
|
|
void mouse_event::push_lua_table(lua_State *L) const {
|
|
lua_newtable(L);
|
|
push_table_value(L, "type", this->type);
|
|
push_table_value(L, "time", this->time);
|
|
push_lua_data(L);
|
|
}
|
|
|
|
void mouse_positioned_event::push_lua_data(lua_State *L) const {
|
|
push_table_value(L, "x", this->x);
|
|
push_table_value(L, "y", this->y);
|
|
push_table_value(L, "x_abs", this->x_abs);
|
|
push_table_value(L, "y_abs", this->y_abs);
|
|
}
|
|
|
|
void mouse_move_event::push_lua_data(lua_State *L) const {
|
|
mouse_positioned_event::push_lua_data(L);
|
|
push_mods(L, this->mods);
|
|
}
|
|
|
|
void mouse_scroll_event::push_lua_data(lua_State *L) const {
|
|
mouse_positioned_event::push_lua_data(L);
|
|
push_table_value(L, "direction", this->direction);
|
|
push_mods(L, this->mods);
|
|
}
|
|
|
|
void mouse_button_event::push_lua_data(lua_State *L) const {
|
|
mouse_positioned_event::push_lua_data(L);
|
|
push_table_value(L, "button_code", static_cast<std::uint32_t>(this->button));
|
|
push_table_value(L, "button", this->button);
|
|
push_mods(L, this->mods);
|
|
}
|
|
|
|
} // namespace conky
|