1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 04:17:33 +00:00

Namespace mouse-events.cc/.h

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>
This commit is contained in:
Tin 2023-11-13 19:01:41 +01:00 committed by Brenden Matthews
parent 1b45d95e26
commit b9e7fa0574
No known key found for this signature in database
GPG Key ID: 137B7AC2BDFD8DF0
9 changed files with 84 additions and 65 deletions

View File

@ -2,6 +2,7 @@
"recommendations": [ "recommendations": [
"kamadorueda.alejandra", "kamadorueda.alejandra",
"ms-vscode.cmake-tools", "ms-vscode.cmake-tools",
"mkhl.direnv" "mkhl.direnv",
"xaver.clang-format"
] ]
} }

View File

@ -431,9 +431,9 @@ void window_layer_surface_set_size(struct window *window) {
#ifdef BUILD_MOUSE_EVENTS #ifdef BUILD_MOUSE_EVENTS
static std::map<wl_pointer *, std::array<size_t, 2>> last_known_positions{}; static std::map<wl_pointer *, std::array<size_t, 2>> last_known_positions{};
static void on_pointer_enter(void *data, wl_pointer *pointer, uint32_t serial, static void on_pointer_enter(void *data, wl_pointer *pointer,
wl_surface *surface, wl_fixed_t surface_x, std::uint32_t serial, wl_surface *surface,
wl_fixed_t surface_y) { wl_fixed_t surface_x, wl_fixed_t surface_y) {
auto w = reinterpret_cast<struct window *>(data); auto w = reinterpret_cast<struct window *>(data);
size_t x = static_cast<size_t>(wl_fixed_to_double(surface_x)); size_t x = static_cast<size_t>(wl_fixed_to_double(surface_x));
@ -448,7 +448,7 @@ static void on_pointer_enter(void *data, wl_pointer *pointer, uint32_t serial,
} }
static void on_pointer_leave(void *data, struct wl_pointer *pointer, static void on_pointer_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface) { std::uint32_t serial, struct wl_surface *surface) {
auto w = reinterpret_cast<struct window *>(data); auto w = reinterpret_cast<struct window *>(data);
std::array<size_t, 2> last = last_known_positions[pointer]; std::array<size_t, 2> last = last_known_positions[pointer];
@ -462,7 +462,7 @@ static void on_pointer_leave(void *data, struct wl_pointer *pointer,
} }
static void on_pointer_motion(void *data, struct wl_pointer *pointer, static void on_pointer_motion(void *data, struct wl_pointer *pointer,
uint32_t _time, wl_fixed_t surface_x, std::uint32_t _time, wl_fixed_t surface_x,
wl_fixed_t surface_y) { wl_fixed_t surface_y) {
auto w = reinterpret_cast<struct window *>(data); auto w = reinterpret_cast<struct window *>(data);
@ -478,8 +478,8 @@ static void on_pointer_motion(void *data, struct wl_pointer *pointer,
} }
static void on_pointer_button(void *data, struct wl_pointer *pointer, static void on_pointer_button(void *data, struct wl_pointer *pointer,
uint32_t serial, uint32_t time, uint32_t button, std::uint32_t serial, std::uint32_t time,
uint32_t state) { std::uint32_t button, std::uint32_t state) {
auto w = reinterpret_cast<struct window *>(data); auto w = reinterpret_cast<struct window *>(data);
std::array<size_t, 2> last = last_known_positions[pointer]; std::array<size_t, 2> last = last_known_positions[pointer];
@ -506,8 +506,8 @@ static void on_pointer_button(void *data, struct wl_pointer *pointer,
llua_mouse_hook(event); llua_mouse_hook(event);
} }
void on_pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time, void on_pointer_axis(void *data, struct wl_pointer *pointer, std::uint32_t time,
uint32_t axis, wl_fixed_t value) { std::uint32_t axis, wl_fixed_t value) {
if (value == 0) return; if (value == 0) return;
auto w = reinterpret_cast<struct window *>(data); auto w = reinterpret_cast<struct window *>(data);

View File

@ -396,11 +396,11 @@ bool display_output_x11::main_loop_wait(double t) {
// the only way to differentiate between a scroll and move event is // the only way to differentiate between a scroll and move event is
// though valuators - move has first 2 set, other axis movements have // though valuators - move has first 2 set, other axis movements have
// other. // other.
bool is_cursor_move = data->valuators.mask_len * 8 > 2 && bool is_cursor_move =
XIMaskIsSet(data->valuators.mask, 0) && data->valuators.mask_len >= 1 &&
XIMaskIsSet(data->valuators.mask, 1); (data->valuators.mask[0] & 3) == data->valuators.mask[0];
for (size_t i = 2; i < data->valuators.mask_len * 8; i++) { for (std::size_t i = 1; i < data->valuators.mask_len; i++) {
if (XIMaskIsSet(data->valuators.mask, i)) { if (data->valuators.mask[i] != 0) {
is_cursor_move = false; is_cursor_move = false;
break; break;
} }
@ -720,7 +720,7 @@ void display_output_x11::set_foreground_color(Colour c) {
} }
int display_output_x11::calc_text_width(const char *s) { int display_output_x11::calc_text_width(const char *s) {
size_t slen = strlen(s); std::size_t slen = strlen(s);
#ifdef BUILD_XFT #ifdef BUILD_XFT
if (use_xft.get(*state)) { if (use_xft.get(*state)) {
XGlyphInfo gi; XGlyphInfo gi;

View File

@ -563,11 +563,14 @@ bool llua_mouse_hook(const EventT &ev) {
return result; return result;
} }
template bool llua_mouse_hook<mouse_scroll_event>(const mouse_scroll_event &ev); template bool llua_mouse_hook<conky::mouse_scroll_event>(
template bool llua_mouse_hook<mouse_button_event>(const mouse_button_event &ev); const conky::mouse_scroll_event &ev);
template bool llua_mouse_hook<mouse_move_event>(const mouse_move_event &ev); template bool llua_mouse_hook<conky::mouse_button_event>(
template bool llua_mouse_hook<mouse_crossing_event>( const conky::mouse_button_event &ev);
const mouse_crossing_event &ev); template bool llua_mouse_hook<conky::mouse_move_event>(
const conky::mouse_move_event &ev);
template bool llua_mouse_hook<conky::mouse_crossing_event>(
const conky::mouse_crossing_event &ev);
#endif /* BUILD_MOUSE_EVENTS */ #endif /* BUILD_MOUSE_EVENTS */
void llua_set_userdata(const char *key, const char *type, void *value) { void llua_set_userdata(const char *key, const char *type, void *value) {

View File

@ -30,7 +30,7 @@
#ifndef _LOGGING_H #ifndef _LOGGING_H
#define _LOGGING_H #define _LOGGING_H
#include <cinttypes> #include <cinttypes> // correct formatting for int types
#include <cstdio> #include <cstdio>
#include <stdexcept> #include <stdexcept>
#include "config.h" #include "config.h"

View File

@ -20,13 +20,19 @@
#include "mouse-events.h" #include "mouse-events.h"
#include <lua.h>
#include <time.h>
#include <array> #include <array>
#include <ctime>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include "logging.h" #include "logging.h"
extern "C" {
#include <lua.h>
}
namespace conky {
/* Lua helper functions */ /* Lua helper functions */
void push_table_value(lua_State *L, std::string key, std::string value) { void push_table_value(lua_State *L, std::string key, std::string value) {
lua_pushstring(L, key.c_str()); lua_pushstring(L, key.c_str());
@ -56,11 +62,12 @@ void push_table_value(lua_State *L, std::string key, bool value) {
lua_settable(L, -3); lua_settable(L, -3);
} }
template <size_t N> template <std::size_t N>
void push_bitset(lua_State *L, std::bitset<N> it, void push_bitset(lua_State *L, std::bitset<N> it,
std::array<std::string, N> labels) { std::array<std::string, N> labels) {
lua_newtable(L); lua_newtable(L);
for (size_t i = 0; i < N; i++) push_table_value(L, labels[i], it.test(i)); 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 = {{ const std::array<std::string, 6> mod_names = {{
@ -79,10 +86,11 @@ void push_mods(lua_State *L, modifier_state_t mods) {
} }
// Returns ms since Epoch. // Returns ms since Epoch.
inline size_t current_time_ms() { inline std::size_t current_time_ms() {
struct timespec spec; struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec); clock_gettime(CLOCK_REALTIME, &spec);
return static_cast<size_t>(static_cast<uint64_t>(spec.tv_sec) * 1'000 + return static_cast<std::size_t>(static_cast<std::uint64_t>(spec.tv_sec) *
1'000 +
spec.tv_nsec / 1'000'000); spec.tv_nsec / 1'000'000);
} }
@ -193,7 +201,9 @@ void mouse_scroll_event::push_lua_data(lua_State *L) const {
void mouse_button_event::push_lua_data(lua_State *L) const { void mouse_button_event::push_lua_data(lua_State *L) const {
mouse_positioned_event::push_lua_data(L); mouse_positioned_event::push_lua_data(L);
push_table_value(L, "button_code", static_cast<uint32_t>(this->button)); push_table_value(L, "button_code", static_cast<std::uint32_t>(this->button));
push_table_value(L, "button", this->button); push_table_value(L, "button", this->button);
push_mods(L, this->mods); push_mods(L, this->mods);
} }
} // namespace conky

View File

@ -28,23 +28,12 @@
#include "config.h" #include "config.h"
#include "logging.h" #include "logging.h"
extern "C" {
#ifdef BUILD_X11 #ifdef BUILD_X11
#include <X11/X.h> #include <X11/X.h>
#endif /* BUILD_X11 */ #endif /* BUILD_X11 */
extern "C" {
#include <lua.h> #include <lua.h>
}
enum mouse_event_t {
MOUSE_PRESS = 0,
MOUSE_RELEASE = 1,
MOUSE_SCROLL = 2,
MOUSE_MOVE = 3,
AREA_ENTER = 4,
AREA_LEAVE = 5,
MOUSE_EVENT_COUNT = 6,
};
#ifdef __linux #ifdef __linux
#include <linux/input-event-codes.h> #include <linux/input-event-codes.h>
@ -68,8 +57,21 @@ enum mouse_event_t {
// Forward mouse button event code // Forward mouse button event code
#define BTN_FORWARD 0x115 #define BTN_FORWARD 0x115
#endif #endif
}
enum mouse_button_t : uint32_t { namespace conky {
enum mouse_event_t {
MOUSE_PRESS = 0,
MOUSE_RELEASE = 1,
MOUSE_SCROLL = 2,
MOUSE_MOVE = 3,
AREA_ENTER = 4,
AREA_LEAVE = 5,
MOUSE_EVENT_COUNT = 6,
};
enum mouse_button_t : std::uint32_t {
BUTTON_LEFT = BTN_LEFT, BUTTON_LEFT = BTN_LEFT,
BUTTON_RIGHT = BTN_RIGHT, BUTTON_RIGHT = BTN_RIGHT,
BUTTON_MIDDLE = BTN_MIDDLE, BUTTON_MIDDLE = BTN_MIDDLE,
@ -106,7 +108,7 @@ inline mouse_button_t x11_mouse_button_code(unsigned int x11_mouse_button) {
struct mouse_event { struct mouse_event {
mouse_event_t type; // type of event mouse_event_t type; // type of event
size_t time; // ms since epoch when the event happened std::size_t time; // ms since epoch when the event happened
explicit mouse_event(mouse_event_t type); explicit mouse_event(mouse_event_t type);
@ -116,18 +118,18 @@ struct mouse_event {
}; };
struct mouse_positioned_event : public mouse_event { struct mouse_positioned_event : public mouse_event {
size_t x = 0, y = 0; // position relative to window std::size_t x = 0, y = 0; // position relative to window
size_t x_abs = 0, y_abs = 0; // position relative to root std::size_t x_abs = 0, y_abs = 0; // position relative to root
mouse_positioned_event(mouse_event_t type, size_t x, size_t y, size_t x_abs, mouse_positioned_event(mouse_event_t type, std::size_t x, std::size_t y,
size_t y_abs) std::size_t x_abs, std::size_t y_abs)
: mouse_event(type), x(x), y(y), x_abs(x_abs), y_abs(){}; : mouse_event(type), x(x), y(y), x_abs(x_abs), y_abs(){};
void push_lua_data(lua_State *L) const; void push_lua_data(lua_State *L) const;
}; };
typedef std::bitset<6> modifier_state_t; typedef std::bitset<6> modifier_state_t;
enum modifier_key : uint32_t { enum modifier_key : std::uint32_t {
MOD_SHIFT = 0, MOD_SHIFT = 0,
MOD_CONTROL = 1, MOD_CONTROL = 1,
MOD_ALT = 2, MOD_ALT = 2,
@ -154,15 +156,15 @@ inline modifier_state_t x11_modifier_state(unsigned int mods) {
struct mouse_move_event : public mouse_positioned_event { struct mouse_move_event : public mouse_positioned_event {
modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...) modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...)
mouse_move_event(size_t x, size_t y, size_t x_abs, size_t y_abs, mouse_move_event(std::size_t x, std::size_t y, std::size_t x_abs,
modifier_state_t mods = 0) std::size_t y_abs, modifier_state_t mods = 0)
: mouse_positioned_event{mouse_event_t::MOUSE_MOVE, x, y, x_abs, y_abs}, : mouse_positioned_event{mouse_event_t::MOUSE_MOVE, x, y, x_abs, y_abs},
mods(mods){}; mods(mods){};
void push_lua_data(lua_State *L) const; void push_lua_data(lua_State *L) const;
}; };
enum scroll_direction_t : uint8_t { enum scroll_direction_t : std::uint8_t {
SCROLL_UNKNOWN = 0, SCROLL_UNKNOWN = 0,
SCROLL_UP, SCROLL_UP,
SCROLL_DOWN, SCROLL_DOWN,
@ -195,8 +197,9 @@ struct mouse_scroll_event : public mouse_positioned_event {
modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...) modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...)
scroll_direction_t direction; scroll_direction_t direction;
mouse_scroll_event(size_t x, size_t y, size_t x_abs, size_t y_abs, mouse_scroll_event(std::size_t x, std::size_t y, std::size_t x_abs,
scroll_direction_t direction, modifier_state_t mods = 0) std::size_t y_abs, scroll_direction_t direction,
modifier_state_t mods = 0)
: mouse_positioned_event{mouse_event_t::MOUSE_SCROLL, x, y, x_abs, y_abs}, : mouse_positioned_event{mouse_event_t::MOUSE_SCROLL, x, y, x_abs, y_abs},
direction(direction), direction(direction),
mods(mods){}; mods(mods){};
@ -208,9 +211,9 @@ struct mouse_button_event : public mouse_positioned_event {
modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...) modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...)
mouse_button_t button; mouse_button_t button;
mouse_button_event(mouse_event_t type, size_t x, size_t y, size_t x_abs, mouse_button_event(mouse_event_t type, std::size_t x, std::size_t y,
size_t y_abs, mouse_button_t button, std::size_t x_abs, std::size_t y_abs,
modifier_state_t mods = 0) mouse_button_t button, modifier_state_t mods = 0)
: mouse_positioned_event{type, x, y, x_abs, y_abs}, : mouse_positioned_event{type, x, y, x_abs, y_abs},
button(button), button(button),
mods(mods){}; mods(mods){};
@ -219,9 +222,11 @@ struct mouse_button_event : public mouse_positioned_event {
}; };
struct mouse_crossing_event : public mouse_positioned_event { struct mouse_crossing_event : public mouse_positioned_event {
mouse_crossing_event(mouse_event_t type, size_t x, size_t y, size_t x_abs, mouse_crossing_event(mouse_event_t type, std::size_t x, std::size_t y,
size_t y_abs) std::size_t x_abs, std::size_t y_abs)
: mouse_positioned_event{type, x, y, x_abs, y_abs} {}; : mouse_positioned_event{type, x, y, x_abs, y_abs} {};
}; };
} // namespace conky
#endif /* MOUSE_EVENTS_H */ #endif /* MOUSE_EVENTS_H */

View File

@ -245,7 +245,7 @@ static int x11_error_handler(Display *d, XErrorEvent *err) {
const char *base_name = xcb_errors_get_name_for_error( const char *base_name = xcb_errors_get_name_for_error(
xcb_errors_ctx, err->error_code, &extension); xcb_errors_ctx, err->error_code, &extension);
if (extension != nullptr) { if (extension != nullptr) {
const size_t size = strlen(base_name) + strlen(extension) + 4; const std::size_t size = strlen(base_name) + strlen(extension) + 4;
error_name = new char(size); error_name = new char(size);
snprintf(error_name, size, "%s (%s)", base_name, extension); snprintf(error_name, size, "%s (%s)", base_name, extension);
name_allocated = true; name_allocated = true;
@ -258,7 +258,7 @@ static int x11_error_handler(Display *d, XErrorEvent *err) {
const char *minor = xcb_errors_get_name_for_minor_code( const char *minor = xcb_errors_get_name_for_minor_code(
xcb_errors_ctx, err->request_code, err->minor_code); xcb_errors_ctx, err->request_code, err->minor_code);
if (minor != nullptr) { if (minor != nullptr) {
const size_t size = strlen(base_name) + strlen(extension) + 4; const std::size_t size = strlen(base_name) + strlen(extension) + 4;
code_description = new char(size); code_description = new char(size);
snprintf(code_description, size, "%s - %s", major, minor); snprintf(code_description, size, "%s - %s", major, minor);
code_allocated = true; code_allocated = true;
@ -283,7 +283,7 @@ static int x11_error_handler(Display *d, XErrorEvent *err) {
} }
} }
if (code_description == nullptr) { if (code_description == nullptr) {
const size_t size = 37; const std::size_t size = 37;
code_description = new char(size); code_description = new char(size);
snprintf(code_description, size, "error code: [major: %i, minor: %i]", snprintf(code_description, size, "error code: [major: %i, minor: %i]",
err->request_code, err->minor_code); err->request_code, err->minor_code);
@ -962,7 +962,7 @@ void x11_init_window(lua::state &l, bool own) {
break; break;
} }
const size_t mask_size = (XI_LASTEVENT + 7) / 8; const std::size_t mask_size = (XI_LASTEVENT + 7) / 8;
unsigned char mask_bytes[mask_size] = {0}; /* must be zeroed! */ unsigned char mask_bytes[mask_size] = {0}; /* must be zeroed! */
XISetMask(mask_bytes, XI_Motion); XISetMask(mask_bytes, XI_Motion);
@ -1377,7 +1377,7 @@ void propagate_x11_event(XEvent &ev) {
// returns the last leaf on the graph. // returns the last leaf on the graph.
inline Window last_descendant(Display *display, Window parent) { inline Window last_descendant(Display *display, Window parent) {
Window _ignored, *children; Window _ignored, *children;
uint32_t count; std::uint32_t count;
Window current = parent; Window current = parent;

View File

@ -93,7 +93,7 @@ struct conky_x11_window {
#endif /*BUILD_XFT*/ #endif /*BUILD_XFT*/
#ifdef BUILD_MOUSE_EVENTS #ifdef BUILD_MOUSE_EVENTS
// Don't feature gate with BUILD_XINPUT; controls fallback. // Don't feature gate with BUILD_XINPUT; controls fallback.
int32_t xi_opcode; std::int32_t xi_opcode;
#endif /* BUILD_MOUSE_EVENTS */ #endif /* BUILD_MOUSE_EVENTS */
int width; int width;