Skip to content

Commit 28e4f7c

Browse files
committed
make input device selectable
1 parent 34c42d8 commit 28e4f7c

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

include/mouse.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
#pragma once
22

33
#include <glm/glm.hpp>
4+
#include <string>
5+
#include <utility>
6+
#include <vector>
47

8+
extern std::vector<std::pair<std::string, std::string>> input_devices;
9+
extern std::pair<std::string, std::string> active_device;
10+
11+
void ChangeMouseDevice(const std::pair<std::string, std::string> &device);
512
void MouseInit();
613
void MouseQuit();
714
void MouseMove(const glm::ivec2 &coords);

src/gui.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,22 @@ void Gui() {
704704
ImGui::SameLine();
705705
ColorButton("Purple", Colors::PURPLE);
706706

707+
Spacer();
708+
Title("Input Device");
709+
710+
if (ImGui::BeginCombo("Input Device", active_device.second.c_str())) {
711+
for (const auto &[path, name] : input_devices) {
712+
const bool is_selected = path == active_device.first;
713+
if (ImGui::Selectable(name.c_str(), is_selected)) {
714+
ChangeMouseDevice({path, name});
715+
}
716+
if (is_selected) {
717+
ImGui::SetItemDefaultFocus();
718+
}
719+
}
720+
ImGui::EndCombo();
721+
}
722+
707723
ImGui::EndChild();
708724
ImGui::SameLine(0, sizes.spacing);
709725

src/mouse.cpp

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
i32 mouse = -1;
1919
bool kernel = KernelModuleActive();
20+
std::vector<std::pair<std::string, std::string>> input_devices;
21+
std::pair<std::string, std::string> active_device;
2022

2123
std::vector<bool> HexToReversedBinary(const char hex_char) {
2224
int value = 0;
@@ -82,6 +84,24 @@ std::vector<bool> DecodeCapabilities(const std::string &filename) {
8284
return binary_out;
8385
}
8486

87+
void ChangeMouseDevice(const std::pair<std::string, std::string> &device) {
88+
mouse = open(device.first.c_str(), O_WRONLY);
89+
if (mouse < 0) {
90+
logging::Error("could not open mouse");
91+
const auto error = errno;
92+
if (error == EACCES) {
93+
const std::string username = getlogin();
94+
logging::Error(
95+
"user is not in input group. please execute sudo usermod -aG input {}", username);
96+
} else {
97+
logging::Error("error code: " + std::to_string(error));
98+
}
99+
std::exit(1);
100+
}
101+
logging::Info("input device: {} ({})", device.second, device.first);
102+
active_device = device;
103+
}
104+
85105
void MouseInit() {
86106
if (KernelModuleActive()) {
87107
mouse = open("/dev/stealthmem", O_RDWR);
@@ -92,6 +112,7 @@ void MouseInit() {
92112
return;
93113
}
94114

115+
input_devices.clear();
95116
for (const auto &entry : std::filesystem::directory_iterator("/dev/input")) {
96117
if (!entry.is_character_file()) {
97118
continue;
@@ -124,28 +145,15 @@ void MouseInit() {
124145
std::getline(name_file, device_name);
125146
name_file.close();
126147

127-
mouse = open(entry.path().c_str(), O_WRONLY);
128-
if (mouse < 0) {
129-
logging::Error("could not open mouse");
130-
const auto error = errno;
131-
if (error == EACCES) {
132-
const std::string username = getlogin();
133-
logging::Error(
134-
"user is not in input group. please execute sudo usermod -aG input {}",
135-
username);
136-
} else {
137-
logging::Error("error code: " + std::to_string(error));
138-
}
139-
std::exit(1);
140-
}
141-
142-
logging::Info("found mouse: {} ({})", device_name, event_name);
143-
return;
148+
input_devices.push_back({entry.path(), device_name});
144149
}
145150

146-
mouse = open("/dev/null", O_WRONLY);
147-
logging::Warning("no mouse device was found");
148-
std::exit(1);
151+
if (input_devices.empty()) {
152+
mouse = open("/dev/null", O_WRONLY);
153+
logging::Warning("no mouse device was found");
154+
std::exit(1);
155+
}
156+
ChangeMouseDevice(input_devices[0]);
149157
}
150158

151159
void MouseQuit() {

0 commit comments

Comments
 (0)