From b9e7fa0574568b5380d959498288ff7f4a1f94a1 Mon Sep 17 00:00:00 2001 From: Tin Date: Mon, 13 Nov 2023 19:01:41 +0100 Subject: [PATCH] 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 --- .vscode/extensions.json | 3 +- src/display-wayland.cc | 18 ++++++------ src/display-x11.cc | 12 ++++---- src/llua.cc | 13 +++++---- src/logging.h | 2 +- src/mouse-events.cc | 26 +++++++++++------ src/mouse-events.h | 63 ++++++++++++++++++++++------------------- src/x11.cc | 10 +++---- src/x11.h | 2 +- 9 files changed, 84 insertions(+), 65 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 69944203..d53b6911 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -2,6 +2,7 @@ "recommendations": [ "kamadorueda.alejandra", "ms-vscode.cmake-tools", - "mkhl.direnv" + "mkhl.direnv", + "xaver.clang-format" ] } diff --git a/src/display-wayland.cc b/src/display-wayland.cc index 8e546add..fbf4da1d 100644 --- a/src/display-wayland.cc +++ b/src/display-wayland.cc @@ -431,9 +431,9 @@ void window_layer_surface_set_size(struct window *window) { #ifdef BUILD_MOUSE_EVENTS static std::map> last_known_positions{}; -static void on_pointer_enter(void *data, wl_pointer *pointer, uint32_t serial, - wl_surface *surface, wl_fixed_t surface_x, - wl_fixed_t surface_y) { +static void on_pointer_enter(void *data, wl_pointer *pointer, + std::uint32_t serial, wl_surface *surface, + wl_fixed_t surface_x, wl_fixed_t surface_y) { auto w = reinterpret_cast(data); size_t x = static_cast(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, - uint32_t serial, struct wl_surface *surface) { + std::uint32_t serial, struct wl_surface *surface) { auto w = reinterpret_cast(data); std::array 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, - uint32_t _time, wl_fixed_t surface_x, + std::uint32_t _time, wl_fixed_t surface_x, wl_fixed_t surface_y) { auto w = reinterpret_cast(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, - uint32_t serial, uint32_t time, uint32_t button, - uint32_t state) { + std::uint32_t serial, std::uint32_t time, + std::uint32_t button, std::uint32_t state) { auto w = reinterpret_cast(data); std::array last = last_known_positions[pointer]; @@ -506,8 +506,8 @@ static void on_pointer_button(void *data, struct wl_pointer *pointer, llua_mouse_hook(event); } -void on_pointer_axis(void *data, struct wl_pointer *pointer, uint32_t time, - uint32_t axis, wl_fixed_t value) { +void on_pointer_axis(void *data, struct wl_pointer *pointer, std::uint32_t time, + std::uint32_t axis, wl_fixed_t value) { if (value == 0) return; auto w = reinterpret_cast(data); diff --git a/src/display-x11.cc b/src/display-x11.cc index 9c2091c0..94b3be24 100644 --- a/src/display-x11.cc +++ b/src/display-x11.cc @@ -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 // though valuators - move has first 2 set, other axis movements have // other. - bool is_cursor_move = data->valuators.mask_len * 8 > 2 && - XIMaskIsSet(data->valuators.mask, 0) && - XIMaskIsSet(data->valuators.mask, 1); - for (size_t i = 2; i < data->valuators.mask_len * 8; i++) { - if (XIMaskIsSet(data->valuators.mask, i)) { + bool is_cursor_move = + data->valuators.mask_len >= 1 && + (data->valuators.mask[0] & 3) == data->valuators.mask[0]; + for (std::size_t i = 1; i < data->valuators.mask_len; i++) { + if (data->valuators.mask[i] != 0) { is_cursor_move = false; break; } @@ -720,7 +720,7 @@ void display_output_x11::set_foreground_color(Colour c) { } int display_output_x11::calc_text_width(const char *s) { - size_t slen = strlen(s); + std::size_t slen = strlen(s); #ifdef BUILD_XFT if (use_xft.get(*state)) { XGlyphInfo gi; diff --git a/src/llua.cc b/src/llua.cc index 08b0d9c2..c49ffde2 100644 --- a/src/llua.cc +++ b/src/llua.cc @@ -563,11 +563,14 @@ bool llua_mouse_hook(const EventT &ev) { return result; } -template bool llua_mouse_hook(const mouse_scroll_event &ev); -template bool llua_mouse_hook(const mouse_button_event &ev); -template bool llua_mouse_hook(const mouse_move_event &ev); -template bool llua_mouse_hook( - const mouse_crossing_event &ev); +template bool llua_mouse_hook( + const conky::mouse_scroll_event &ev); +template bool llua_mouse_hook( + const conky::mouse_button_event &ev); +template bool llua_mouse_hook( + const conky::mouse_move_event &ev); +template bool llua_mouse_hook( + const conky::mouse_crossing_event &ev); #endif /* BUILD_MOUSE_EVENTS */ void llua_set_userdata(const char *key, const char *type, void *value) { diff --git a/src/logging.h b/src/logging.h index c4af5bdf..1c3de809 100644 --- a/src/logging.h +++ b/src/logging.h @@ -30,7 +30,7 @@ #ifndef _LOGGING_H #define _LOGGING_H -#include +#include // correct formatting for int types #include #include #include "config.h" diff --git a/src/mouse-events.cc b/src/mouse-events.cc index 6a9593bb..c7cfccc8 100644 --- a/src/mouse-events.cc +++ b/src/mouse-events.cc @@ -20,13 +20,19 @@ #include "mouse-events.h" -#include -#include #include +#include #include #include + #include "logging.h" +extern "C" { +#include +} + +namespace conky { + /* Lua helper functions */ void push_table_value(lua_State *L, std::string key, std::string value) { 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); } -template +template void push_bitset(lua_State *L, std::bitset it, std::array labels) { 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 mod_names = {{ @@ -79,11 +86,12 @@ void push_mods(lua_State *L, modifier_state_t mods) { } // Returns ms since Epoch. -inline size_t current_time_ms() { +inline std::size_t current_time_ms() { struct timespec spec; clock_gettime(CLOCK_REALTIME, &spec); - return static_cast(static_cast(spec.tv_sec) * 1'000 + - spec.tv_nsec / 1'000'000); + return static_cast(static_cast(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) { @@ -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 { mouse_positioned_event::push_lua_data(L); - push_table_value(L, "button_code", static_cast(this->button)); + push_table_value(L, "button_code", static_cast(this->button)); push_table_value(L, "button", this->button); push_mods(L, this->mods); } + +} // namespace conky \ No newline at end of file diff --git a/src/mouse-events.h b/src/mouse-events.h index e0cd5eb7..d00e5bca 100644 --- a/src/mouse-events.h +++ b/src/mouse-events.h @@ -28,23 +28,12 @@ #include "config.h" #include "logging.h" +extern "C" { #ifdef BUILD_X11 #include #endif /* BUILD_X11 */ -extern "C" { #include -} - -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 #include @@ -68,8 +57,21 @@ enum mouse_event_t { // Forward mouse button event code #define BTN_FORWARD 0x115 #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_RIGHT = BTN_RIGHT, BUTTON_MIDDLE = BTN_MIDDLE, @@ -106,7 +108,7 @@ inline mouse_button_t x11_mouse_button_code(unsigned int x11_mouse_button) { struct mouse_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); @@ -116,18 +118,18 @@ struct mouse_event { }; struct mouse_positioned_event : public mouse_event { - 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 = 0, y = 0; // position relative to window + 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, - size_t y_abs) + mouse_positioned_event(mouse_event_t type, std::size_t x, std::size_t y, + std::size_t x_abs, std::size_t y_abs) : mouse_event(type), x(x), y(y), x_abs(x_abs), y_abs(){}; void push_lua_data(lua_State *L) const; }; typedef std::bitset<6> modifier_state_t; -enum modifier_key : uint32_t { +enum modifier_key : std::uint32_t { MOD_SHIFT = 0, MOD_CONTROL = 1, 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 { 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, - modifier_state_t mods = 0) + mouse_move_event(std::size_t x, std::size_t y, std::size_t x_abs, + std::size_t y_abs, modifier_state_t mods = 0) : mouse_positioned_event{mouse_event_t::MOUSE_MOVE, x, y, x_abs, y_abs}, mods(mods){}; 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_UP, SCROLL_DOWN, @@ -195,8 +197,9 @@ struct mouse_scroll_event : public mouse_positioned_event { modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...) scroll_direction_t direction; - mouse_scroll_event(size_t x, size_t y, size_t x_abs, size_t y_abs, - scroll_direction_t direction, modifier_state_t mods = 0) + mouse_scroll_event(std::size_t x, std::size_t y, std::size_t x_abs, + 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}, direction(direction), mods(mods){}; @@ -208,9 +211,9 @@ struct mouse_button_event : public mouse_positioned_event { modifier_state_t mods; // held buttons and modifiers (ctrl, shift, ...) mouse_button_t button; - mouse_button_event(mouse_event_t type, size_t x, size_t y, size_t x_abs, - size_t y_abs, mouse_button_t button, - modifier_state_t mods = 0) + mouse_button_event(mouse_event_t type, std::size_t x, std::size_t y, + std::size_t x_abs, std::size_t y_abs, + mouse_button_t button, modifier_state_t mods = 0) : mouse_positioned_event{type, x, y, x_abs, y_abs}, button(button), mods(mods){}; @@ -219,9 +222,11 @@ struct mouse_button_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, - size_t y_abs) + mouse_crossing_event(mouse_event_t type, std::size_t x, std::size_t y, + std::size_t x_abs, std::size_t y_abs) : mouse_positioned_event{type, x, y, x_abs, y_abs} {}; }; +} // namespace conky + #endif /* MOUSE_EVENTS_H */ diff --git a/src/x11.cc b/src/x11.cc index 0e6ae02f..481bd770 100644 --- a/src/x11.cc +++ b/src/x11.cc @@ -245,7 +245,7 @@ static int x11_error_handler(Display *d, XErrorEvent *err) { const char *base_name = xcb_errors_get_name_for_error( xcb_errors_ctx, err->error_code, &extension); 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); snprintf(error_name, size, "%s (%s)", base_name, extension); 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( xcb_errors_ctx, err->request_code, err->minor_code); 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); snprintf(code_description, size, "%s - %s", major, minor); code_allocated = true; @@ -283,7 +283,7 @@ static int x11_error_handler(Display *d, XErrorEvent *err) { } } if (code_description == nullptr) { - const size_t size = 37; + const std::size_t size = 37; code_description = new char(size); snprintf(code_description, size, "error code: [major: %i, minor: %i]", err->request_code, err->minor_code); @@ -962,7 +962,7 @@ void x11_init_window(lua::state &l, bool own) { 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! */ XISetMask(mask_bytes, XI_Motion); @@ -1377,7 +1377,7 @@ void propagate_x11_event(XEvent &ev) { // returns the last leaf on the graph. inline Window last_descendant(Display *display, Window parent) { Window _ignored, *children; - uint32_t count; + std::uint32_t count; Window current = parent; diff --git a/src/x11.h b/src/x11.h index ed90f59c..3273a8bf 100644 --- a/src/x11.h +++ b/src/x11.h @@ -93,7 +93,7 @@ struct conky_x11_window { #endif /*BUILD_XFT*/ #ifdef BUILD_MOUSE_EVENTS // Don't feature gate with BUILD_XINPUT; controls fallback. - int32_t xi_opcode; + std::int32_t xi_opcode; #endif /* BUILD_MOUSE_EVENTS */ int width;