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": [
"kamadorueda.alejandra",
"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
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,
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<struct window *>(data);
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,
uint32_t serial, struct wl_surface *surface) {
std::uint32_t serial, struct wl_surface *surface) {
auto w = reinterpret_cast<struct window *>(data);
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,
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<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,
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<struct window *>(data);
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);
}
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<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
// 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;

View File

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

View File

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

View File

@ -20,13 +20,19 @@
#include "mouse-events.h"
#include <lua.h>
#include <time.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());
@ -56,11 +62,12 @@ void push_table_value(lua_State *L, std::string key, bool value) {
lua_settable(L, -3);
}
template <size_t N>
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 (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 = {{
@ -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<size_t>(static_cast<uint64_t>(spec.tv_sec) * 1'000 +
spec.tv_nsec / 1'000'000);
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) {
@ -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<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_mods(L, this->mods);
}
} // namespace conky

View File

@ -28,23 +28,12 @@
#include "config.h"
#include "logging.h"
extern "C" {
#ifdef BUILD_X11
#include <X11/X.h>
#endif /* BUILD_X11 */
extern "C" {
#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
#include <linux/input-event-codes.h>
@ -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 */

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(
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;

View File

@ -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;