Skip to content

Commit

Permalink
rMlib: Use unistdpp for FD / socket management
Browse files Browse the repository at this point in the history
  • Loading branch information
timower committed Oct 16, 2023
1 parent fa891d5 commit 0eaa91d
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 200 deletions.
7 changes: 5 additions & 2 deletions apps/rocket/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <Device.h>
#include <UI.h>

#include <unistdpp/file.h>

using namespace rmlib;

namespace {
Expand Down Expand Up @@ -336,9 +338,10 @@ class LauncherState : public StateBase<LauncherWidget> {

if (res == 0) {
// Get the reason
auto irq = rmlib::device::readFile("/sys/power/pm_wakeup_irq");
auto irq = unistdpp::readFile("/sys/power/pm_wakeup_irq");
if (!irq.has_value()) {
std::cout << "Error getting reason: " << irq.error().msg << std::endl;
std::cout << "Error getting reason: " << unistdpp::toString(irq.error())
<< std::endl;

// If there is no irq it must be the user which pressed the button:
return true;
Expand Down
5 changes: 1 addition & 4 deletions apps/tilem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ project(tilem)
add_subdirectory(emu)

add_executable(${PROJECT_NAME}
main.cpp
${WABBIT_SRC})
main.cpp)

target_compile_definitions(${PROJECT_NAME} PRIVATE _LINUX)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

target_include_directories(${PROJECT_NAME} PRIVATE ${WABBIT_INCLUDE})

target_link_libraries(${PROJECT_NAME}
PRIVATE
tiemu
Expand Down
3 changes: 2 additions & 1 deletion apps/yaft/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ fork_and_exec(int* master,
return true;
}

constexpr auto select_timeout = std::chrono::microseconds(SELECT_TIMEOUT);
constexpr auto select_timeout =
std::chrono::milliseconds(SELECT_TIMEOUT / 1000);

int
main(int argc, const char* argv[]) {
Expand Down
57 changes: 10 additions & 47 deletions libs/rMlib/Device.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Device.h"

#include <unistdpp/file.h>

#include <dirent.h>
#include <fcntl.h>
#include <fstream>
Expand Down Expand Up @@ -51,47 +53,14 @@ const InputPaths rm2_paths = {
};
} // namespace

ErrorOr<std::string>
readFile(std::string_view path) {
assert(path[path.length()] == '\0' && "path must be null terminated");
FILE* f = fopen(&path[0], "rb");
if (f == nullptr) {
return Error::errn();
}

fseek(f, 0, SEEK_END);
const unsigned long size = ftell(f);
fseek(f, 0, SEEK_SET);

std::string result(size + 1, '\0');
auto read = fread(result.data(), size, 1, f);
if (read != size && !(read == 0 && feof(f))) {
fclose(f);
return tl::unexpected(
Error{ "Only read: " + std::to_string(read) + " bytes?" });
}

fclose(f);
return result;
}

ErrorOr<DeviceType>
getDeviceType() {
#ifdef EMULATE
return DeviceType::reMarkable2;
#else
static const auto result = []() -> ErrorOr<DeviceType> {
constexpr auto path = "/sys/devices/soc0/machine";
std::ifstream ifs(path);
if (!ifs.is_open()) {
return Error::make("Couldn't open device path");
}

std::string name;
name.reserve(16);
name.assign(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>());

auto name = TRY(unistdpp::readFile(path));
if (name.find("2.0") == std::string::npos) {
return DeviceType::reMarkable1;
}
Expand Down Expand Up @@ -151,22 +120,16 @@ listDirectory(std::string_view path, bool onlyFiles) {

bool
IsPogoConnected() {
int fd = open(
#ifndef EMULATE
"/sys/pogo/status/pogo_connected"
constexpr auto path = "/sys/pogo/status/pogo_connected";
#else
"/tmp/pogo"
constexpr auto path = "/tmp/pogo";
#endif
,
O_RDWR);
if (fd == -1) {
return false;
}

char buf = '\0';
read(fd, &buf, 1);
close(fd);

return buf == '1';
return unistdpp::open(path, O_RDWR)
.and_then([](unistdpp::FD fd) {
return fd.readAll<char>().transform([&](char buf) { return buf == '1'; });
})
.value_or(false);
}
} // namespace rmlib::device
61 changes: 21 additions & 40 deletions libs/rMlib/EmulatedInput.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Input.h"

#include <unistdpp/pipe.h>

#include <atomic>
#include <iostream>
#include <thread>
Expand All @@ -21,17 +23,18 @@ InputDeviceBase::~InputDeviceBase() {}

namespace {
struct FakeInputDevice : public InputDeviceBase {
int pipes[2];
unistdpp::Pipe pipes;
unsigned int sdlUserEvent;

FakeInputDevice() : InputDeviceBase(0, nullptr, "test") {
FakeInputDevice() : InputDeviceBase(unistdpp::FD(), nullptr, "test") {
sdlUserEvent = SDL_RegisterEvents(1);

int res = pipe(pipes);
if (res != 0) {
perror("Error creating input notify pipes");
}
std::cout << "Got pipes: " << pipes[0] << ", " << pipes[1] << "\n";
pipes = unistdpp::pipe()
.or_else([](auto err) {
std::cerr << unistdpp::toString(err) << "\n";
std::exit(EXIT_FAILURE);
})
.value();
}

void flood() final {}
Expand Down Expand Up @@ -59,43 +62,22 @@ InputManager::openAll(bool monitor) {
}

ErrorOr<std::vector<Event>>
InputManager::waitForInput(fd_set& fdSet,
int maxFd,
std::optional<std::chrono::microseconds> timeout) {
InputManager::waitForInput(std::vector<pollfd>& extraFds,
std::optional<std::chrono::milliseconds> timeout) {
static bool down = false;

FakeInputDevice& dev = static_cast<FakeInputDevice&>(getBaseDevices()->key);

std::thread selectThread([maxFd,
readPipe = dev.pipes[0],
&fdSet,
std::thread selectThread([&readPipe = dev.pipes.readPipe,
&extraFds,
&timeout,
userEv = dev.sdlUserEvent]() {
if (maxFd == 0 && !FD_ISSET(0, &fdSet)) {
return;
}

// Also listen to the notify pipe
int maxFd2 = std::max(maxFd, readPipe);
FD_SET(readPipe, &fdSet);

auto tv = timeval{ 0, 0 };
if (timeout.has_value()) {
constexpr auto second_in_usec =
std::chrono::microseconds(std::chrono::seconds(1)).count();
tv.tv_sec =
std::chrono::duration_cast<std::chrono::seconds>(*timeout).count();
tv.tv_usec = timeout->count() - (tv.tv_sec * second_in_usec);
}

auto ret = select(maxFd2 + 1,
&fdSet,
nullptr,
nullptr,
timeout.has_value() ? &tv : nullptr);
extraFds.push_back(unistdpp::waitFor(readPipe, unistdpp::Wait::READ));

if (ret < 0) {
perror("Select on input failed");
auto ret = unistdpp::poll(extraFds, timeout);
if (!ret) {
std::cerr << "Poll failure: " << unistdpp::toString(ret.error()) << "\n";
return;
}

Expand All @@ -104,9 +86,8 @@ InputManager::waitForInput(fd_set& fdSet,
}

// Read the single notify byte.
if (FD_ISSET(readPipe, &fdSet)) {
char buf;
read(readPipe, &buf, 1);
if (unistdpp::canRead(extraFds.back())) {
(void)readPipe.readAll<char>();
return;
}

Expand All @@ -129,7 +110,7 @@ InputManager::waitForInput(fd_set& fdSet,
}

if (event.type != dev.sdlUserEvent) {
write(dev.pipes[1], "1", 1);
(void)dev.pipes.writePipe.writeAll('1');
}

selectThread.join();
Expand Down
Loading

0 comments on commit 0eaa91d

Please sign in to comment.