Skip to content

Commit 2ce620a

Browse files
Sonar fixes
1 parent 3ff3317 commit 2ce620a

10 files changed

Lines changed: 218 additions & 263 deletions

File tree

.github/workflows/ci-linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ jobs:
178178
run: |
179179
uv sync --locked \
180180
--python "${PYTHON_VERSION}" \
181-
--no-python-downloads --no-install-project
181+
--no-python-downloads --no-install-project --no-build
182182
183183
- name: Run tests
184184
id: test

src/confighttp.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <filesystem>
1313
#include <format>
1414
#include <fstream>
15+
#include <new>
1516
#include <optional>
1617
#include <string_view>
1718
#include <vector>
@@ -20,6 +21,7 @@
2021
#include <boost/algorithm/string.hpp>
2122
#include <boost/asio/ssl/context.hpp>
2223
#include <boost/filesystem.hpp>
24+
#include <lizardbyte/common/env.h>
2325
#include <nlohmann/json.hpp>
2426
#include <Simple-Web-Server/crypto.hpp>
2527
#include <Simple-Web-Server/server_https.hpp>
@@ -148,7 +150,7 @@ namespace confighttp {
148150
const auto dot = version.find('.', start);
149151
const auto length = dot == std::string_view::npos ? std::string_view::npos : dot - start;
150152
const auto part = parse_driver_version_part(version.substr(start, length));
151-
if (!part) {
153+
if (!part.has_value()) {
152154
return std::nullopt;
153155
}
154156

@@ -305,9 +307,9 @@ namespace confighttp {
305307
}
306308

307309
for (DWORD index = 0;; ++index) {
308-
wchar_t subkey_name[256] = {};
309-
DWORD subkey_name_size = _countof(subkey_name);
310-
const auto enum_status = RegEnumKeyExW(root_key.get(), index, subkey_name, &subkey_name_size, nullptr, nullptr, nullptr, nullptr);
310+
std::wstring subkey_name(256, L'\0');
311+
DWORD subkey_name_size = static_cast<DWORD>(subkey_name.size());
312+
const auto enum_status = RegEnumKeyExW(root_key.get(), index, subkey_name.data(), &subkey_name_size, nullptr, nullptr, nullptr, nullptr);
311313
if (enum_status == ERROR_NO_MORE_ITEMS) {
312314
break;
313315
}
@@ -316,7 +318,7 @@ namespace confighttp {
316318
}
317319

318320
std::wstring device_key_path = L"SYSTEM\\CurrentControlSet\\Enum\\ROOT\\LIBVIRTUALHID\\";
319-
device_key_path.append(subkey_name, subkey_name_size);
321+
device_key_path.append(subkey_name, 0, subkey_name_size);
320322

321323
registry_key_t device_key;
322324
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, device_key_path.c_str(), 0, KEY_READ, device_key.put()) != ERROR_SUCCESS) {
@@ -1685,7 +1687,7 @@ namespace confighttp {
16851687
auto output_tree = build_driver_status(false, version_str, LIBVIRTUALHID_MINIMUM_VERSION);
16861688
bool requires_installed_driver = true;
16871689
std::string backend_name;
1688-
std::string error;
1690+
std::string runtime_error_message;
16891691

16901692
try {
16911693
const auto runtime = platf::virtualhid::create_runtime();
@@ -1695,14 +1697,14 @@ namespace confighttp {
16951697
requires_installed_driver = capabilities.requires_installed_driver;
16961698
output_tree = build_driver_status(capabilities.supports_gamepad, version_str, LIBVIRTUALHID_MINIMUM_VERSION);
16971699
}
1698-
} catch (const std::exception &e) {
1699-
error = e.what();
1700+
} catch (const std::bad_alloc &exception) {
1701+
runtime_error_message = exception.what();
17001702
}
17011703

17021704
output_tree["backend_name"] = backend_name;
17031705
output_tree["requires_installed_driver"] = requires_installed_driver;
1704-
if (!error.empty()) {
1705-
output_tree["error"] = error;
1706+
if (!runtime_error_message.empty()) {
1707+
output_tree["error"] = runtime_error_message;
17061708
}
17071709
#else
17081710
auto output_tree = build_driver_status(false, "", LIBVIRTUALHID_MINIMUM_VERSION);
@@ -1724,7 +1726,11 @@ namespace confighttp {
17241726
std::string version_str;
17251727

17261728
// Check if ViGEmBus driver exists
1727-
const std::filesystem::path driver_path = std::filesystem::path(std::getenv("SystemRoot") ? std::getenv("SystemRoot") : "C:\\Windows") / "System32" / "drivers" / "ViGEmBus.sys";
1729+
std::string system_root;
1730+
if (!lizardbyte::common::get_env("SystemRoot", system_root)) {
1731+
system_root = "C:\\Windows";
1732+
}
1733+
const std::filesystem::path driver_path = std::filesystem::path(system_root) / "System32" / "drivers" / "ViGEmBus.sys";
17281734
const auto installed = std::filesystem::exists(driver_path);
17291735
if (installed) {
17301736
platf::getFileVersionInfo(driver_path, version_str);

src/input.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ namespace input {
188188
~gamepad_t() {
189189
if (id >= 0) {
190190
task_pool.push([id = this->id]() {
191-
free_gamepad(platf_input, id);
191+
::input::free_gamepad(platf_input, id);
192192
});
193193
}
194194
}
@@ -1347,7 +1347,7 @@ namespace input {
13471347
gamepad.id = id;
13481348
} else if (!(packet->activeGamepadMask & (1 << packet->controllerNumber)) && gamepad.id >= 0) {
13491349
// If this is the final event for a gamepad being removed, free the gamepad and return.
1350-
free_gamepad(platf_input, gamepad.id);
1350+
::input::free_gamepad(platf_input, gamepad.id);
13511351
gamepad.id = -1;
13521352
return;
13531353
}

src/platform/common.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -838,17 +838,22 @@ namespace platf {
838838
virtual ~audio_control_t() = default;
839839
};
840840

841+
/**
842+
* @brief Platform-specific input backend context.
843+
*/
844+
struct input_raw_t;
845+
841846
/**
842847
* @brief Release a platform input backend created by input().
843848
*
844-
* @param p Pointer passed to the deleter or conversion helper.
849+
* @param input Platform input backend to release.
845850
*/
846-
void freeInput(void *);
851+
void freeInput(input_raw_t *input);
847852

848853
/**
849854
* @brief Owning pointer for a platform input backend.
850855
*/
851-
using input_t = util::safe_ptr<void, freeInput>;
856+
using input_t = util::safe_ptr<input_raw_t, freeInput>;
852857

853858
std::filesystem::path appdata();
854859

@@ -1174,7 +1179,7 @@ namespace platf {
11741179
* @param utf8 UTF-8 text submitted by the client.
11751180
* @param size Number of bytes or elements requested.
11761181
*/
1177-
void unicode(input_t &input, char *utf8, int size);
1182+
void unicode(input_t &input, const char *utf8, int size);
11781183

11791184
/**
11801185
* @brief Per-client input context allocated by a platform backend.

src/platform/linux/input/virtualhid.cpp

Lines changed: 16 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
// standard includes
7+
#include <memory>
78
#include <utility>
89

910
// platform includes
@@ -17,14 +18,14 @@
1718
using namespace std::literals;
1819

1920
namespace platf {
20-
namespace {
21+
/**
22+
* @brief Global libvirtualhid devices shared by clients.
23+
*/
24+
struct input_raw_t {
25+
virtualhid::input_context_t virtualhid; ///< libvirtualhid input context.
26+
};
2127

22-
/**
23-
* @brief Global libvirtualhid devices shared by clients.
24-
*/
25-
struct input_raw_t {
26-
virtualhid::input_context_t virtualhid; ///< libvirtualhid input context.
27-
};
28+
namespace {
2829

2930
/**
3031
* @brief Per-client libvirtualhid devices.
@@ -36,7 +37,7 @@ namespace platf {
3637
* @param input Platform input backend that receives the event.
3738
*/
3839
explicit client_input_raw_t(input_t &input):
39-
virtualhid {((input_raw_t *) input.get())->virtualhid} {}
40+
virtualhid {input->virtualhid} {}
4041

4142
virtualhid::client_context_t virtualhid; ///< libvirtualhid client context.
4243
};
@@ -51,77 +52,16 @@ namespace platf {
5152
return std::make_unique<client_input_raw_t>(input);
5253
}
5354

54-
void freeInput(void *p) {
55-
auto *input = (input_raw_t *) p;
56-
delete input;
57-
}
58-
59-
void move_mouse(input_t &input, int deltaX, int deltaY) {
60-
virtualhid::move_mouse(((input_raw_t *) input.get())->virtualhid, deltaX, deltaY);
61-
}
62-
63-
void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y) {
64-
virtualhid::abs_mouse(((input_raw_t *) input.get())->virtualhid, touch_port, x, y);
65-
}
66-
67-
void button_mouse(input_t &input, int button, bool release) {
68-
virtualhid::button_mouse(((input_raw_t *) input.get())->virtualhid, button, release);
69-
}
70-
71-
void scroll(input_t &input, int high_res_distance) {
72-
virtualhid::scroll(((input_raw_t *) input.get())->virtualhid, high_res_distance);
73-
}
74-
75-
void hscroll(input_t &input, int high_res_distance) {
76-
virtualhid::hscroll(((input_raw_t *) input.get())->virtualhid, high_res_distance);
77-
}
78-
79-
/**
80-
* @brief Press or release a virtual keyboard key.
81-
*
82-
* @param input Platform input backend that receives the event.
83-
* @param modcode Modifier key code to update.
84-
* @param release Whether the key or button event is a release.
85-
* @param flags Bit flags that modify the requested operation; ignored by this backend.
86-
*/
87-
void keyboard_update(input_t &input, std::uint16_t modcode, bool release, std::uint8_t flags) {
88-
virtualhid::keyboard_update(((input_raw_t *) input.get())->virtualhid, modcode, release, flags);
89-
}
90-
91-
void unicode(input_t &input, char *utf8, int size) {
92-
virtualhid::unicode(((input_raw_t *) input.get())->virtualhid, utf8, size);
93-
}
94-
95-
void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch) {
96-
virtualhid::touch_update(((client_input_raw_t *) input)->virtualhid, touch_port, touch);
97-
}
98-
99-
void pen_update(client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen) {
100-
virtualhid::pen_update(((client_input_raw_t *) input)->virtualhid, touch_port, pen);
101-
}
102-
103-
int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue) {
104-
return virtualhid::alloc_gamepad(((input_raw_t *) input.get())->virtualhid, id, metadata, std::move(feedback_queue));
105-
}
106-
107-
void free_gamepad(input_t &input, int nr) {
108-
virtualhid::free_gamepad(((input_raw_t *) input.get())->virtualhid, nr);
109-
}
110-
111-
void gamepad_update(input_t &input, int nr, const gamepad_state_t &gamepad_state) {
112-
virtualhid::gamepad_update(((input_raw_t *) input.get())->virtualhid, nr, gamepad_state);
113-
}
114-
115-
void gamepad_touch(input_t &input, const gamepad_touch_t &touch) {
116-
virtualhid::gamepad_touch(((input_raw_t *) input.get())->virtualhid, touch);
55+
void freeInput(input_raw_t *input) {
56+
std::default_delete<input_raw_t> {}(input);
11757
}
11858

119-
void gamepad_motion(input_t &input, const gamepad_motion_t &motion) {
120-
virtualhid::gamepad_motion(((input_raw_t *) input.get())->virtualhid, motion);
59+
virtualhid::input_context_t &virtualhid::get_input_context(input_t &input) {
60+
return input->virtualhid;
12161
}
12262

123-
void gamepad_battery(input_t &input, const gamepad_battery_t &battery) {
124-
virtualhid::gamepad_battery(((input_raw_t *) input.get())->virtualhid, battery);
63+
virtualhid::client_context_t &virtualhid::get_client_context(client_input_t *input) {
64+
return static_cast<client_input_raw_t *>(input)->virtualhid;
12565
}
12666

12767
platform_caps::caps_t get_capabilities() {
@@ -131,8 +71,7 @@ namespace platf {
13171
return caps;
13272
}
13373

134-
const auto &capabilities = runtime->capabilities();
135-
if (config::input.native_pen_touch && (capabilities.supports_touchscreen || capabilities.supports_pen_tablet)) {
74+
if (const auto &capabilities = runtime->capabilities(); config::input.native_pen_touch && (capabilities.supports_touchscreen || capabilities.supports_pen_tablet)) {
13675
caps |= platform_caps::pen_touch;
13776
}
13877
if (virtualhid::configured_gamepad_supports_touchpad()) {

src/platform/macos/input.cpp

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace platf {
3535
* @param input Platform input backend that receives the event.
3636
*/
3737
explicit client_input_raw_t(input_t &input):
38-
virtualhid {((input_raw_t *) input.get())->virtualhid} {}
38+
virtualhid {input->virtualhid} {}
3939

4040
virtualhid::client_context_t virtualhid; ///< libvirtualhid client context.
4141
};
@@ -48,69 +48,16 @@ namespace platf {
4848
return std::make_unique<client_input_raw_t>(input);
4949
}
5050

51-
void freeInput(void *p) {
52-
auto *input = (input_raw_t *) p;
53-
delete input;
51+
void freeInput(input_raw_t *input) {
52+
std::default_delete<input_raw_t> {}(input);
5453
}
5554

56-
void move_mouse(input_t &input, int deltaX, int deltaY) {
57-
virtualhid::move_mouse(((input_raw_t *) input.get())->virtualhid, deltaX, deltaY);
55+
virtualhid::input_context_t &virtualhid::get_input_context(input_t &input) {
56+
return input->virtualhid;
5857
}
5958

60-
void abs_mouse(input_t &input, const touch_port_t &touch_port, float x, float y) {
61-
virtualhid::abs_mouse(((input_raw_t *) input.get())->virtualhid, touch_port, x, y);
62-
}
63-
64-
void button_mouse(input_t &input, int button, bool release) {
65-
virtualhid::button_mouse(((input_raw_t *) input.get())->virtualhid, button, release);
66-
}
67-
68-
void scroll(input_t &input, int high_res_distance) {
69-
virtualhid::scroll(((input_raw_t *) input.get())->virtualhid, high_res_distance);
70-
}
71-
72-
void hscroll(input_t &input, int high_res_distance) {
73-
virtualhid::hscroll(((input_raw_t *) input.get())->virtualhid, high_res_distance);
74-
}
75-
76-
void keyboard_update(input_t &input, std::uint16_t modcode, bool release, std::uint8_t flags) {
77-
virtualhid::keyboard_update(((input_raw_t *) input.get())->virtualhid, modcode, release, flags);
78-
}
79-
80-
void unicode(input_t &input, char *utf8, int size) {
81-
virtualhid::unicode(((input_raw_t *) input.get())->virtualhid, utf8, size);
82-
}
83-
84-
void touch_update(client_input_t *input, const touch_port_t &touch_port, const touch_input_t &touch) {
85-
virtualhid::touch_update(((client_input_raw_t *) input)->virtualhid, touch_port, touch);
86-
}
87-
88-
void pen_update(client_input_t *input, const touch_port_t &touch_port, const pen_input_t &pen) {
89-
virtualhid::pen_update(((client_input_raw_t *) input)->virtualhid, touch_port, pen);
90-
}
91-
92-
int alloc_gamepad(input_t &input, const gamepad_id_t &id, const gamepad_arrival_t &metadata, feedback_queue_t feedback_queue) {
93-
return virtualhid::alloc_gamepad(((input_raw_t *) input.get())->virtualhid, id, metadata, std::move(feedback_queue));
94-
}
95-
96-
void free_gamepad(input_t &input, int nr) {
97-
virtualhid::free_gamepad(((input_raw_t *) input.get())->virtualhid, nr);
98-
}
99-
100-
void gamepad_update(input_t &input, int nr, const gamepad_state_t &gamepad_state) {
101-
virtualhid::gamepad_update(((input_raw_t *) input.get())->virtualhid, nr, gamepad_state);
102-
}
103-
104-
void gamepad_touch(input_t &input, const gamepad_touch_t &touch) {
105-
virtualhid::gamepad_touch(((input_raw_t *) input.get())->virtualhid, touch);
106-
}
107-
108-
void gamepad_motion(input_t &input, const gamepad_motion_t &motion) {
109-
virtualhid::gamepad_motion(((input_raw_t *) input.get())->virtualhid, motion);
110-
}
111-
112-
void gamepad_battery(input_t &input, const gamepad_battery_t &battery) {
113-
virtualhid::gamepad_battery(((input_raw_t *) input.get())->virtualhid, battery);
59+
virtualhid::client_context_t &virtualhid::get_client_context(client_input_t *input) {
60+
return static_cast<client_input_raw_t *>(input)->virtualhid;
11461
}
11562

11663
std::optional<util::point_t> get_mouse_loc(input_t & /*input*/) {

0 commit comments

Comments
 (0)