Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more input & input_scan shenanigans #1788

Merged
merged 6 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions input/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ static void computeNfds(void) {
}
}

// NOTE: Make sure the top member has the highest fd number, for clearTimer's sake when it recomputes nfds
static void reorderArray(void) {
if (fd_idx > 0) {
int prev_fd = inputfds[fd_idx - 1];
int opened_fd = inputfds[fd_idx];
if (opened_fd < prev_fd) {
inputfds[fd_idx - 1] = opened_fd;
inputfds[fd_idx] = prev_fd;
}
}
}

static int openInputDevice(lua_State* L)
{
const char* restrict inputdevice = luaL_checkstring(L, 1);
Expand Down Expand Up @@ -174,15 +186,8 @@ static int openInputDevice(lua_State* L)
}
}

// NOTE: Make sure the top member has the highest fd number, for clearTimer's sake when it recomputes nfds
if (fd_idx > 0) {
int prev_fd = inputfds[fd_idx - 1];
int opened_fd = inputfds[fd_idx];
if (opened_fd < prev_fd) {
inputfds[fd_idx - 1] = opened_fd;
inputfds[fd_idx] = prev_fd;
}
}
// Reorder the array to match clearTimer's expectations
reorderArray();

// We're done w/ inputdevice, pop it
lua_settop(L, 0);
Expand All @@ -195,6 +200,35 @@ static int openInputDevice(lua_State* L)
return 1; // fd
}

static int openInputFD(lua_State* L)
{
int fd = luaL_checkint(L, 1);
lua_settop(L, 0); // pop arg

if (fd < 0) {
return luaL_error(L, "Passed an invalid fd number to input.fdopen");
}
if (fd_idx >= ARRAY_SIZE(inputfds)) {
// Don't leak that fd on error, in case we're being called in protected mode
close(fd);
return luaL_error(L, "No free slot for new input fd <%d>", fd);
}

// Everything looks good, we can do our thing!
const char* restrict ko_dont_grab_input = getenv("KO_DONT_GRAB_INPUT");
if (ko_dont_grab_input == NULL) {
ioctl(fd, EVIOCGRAB, 1);
}

// Update our state for the new input slot...
inputfds[fd_idx] = fd;
reorderArray();
fd_idx++;
computeNfds();

return 0;
}

// Make sure our inputfds array is never sparse after closing one
static void repackFdArray(ssize_t fd_idx_to_close)
{
Expand Down Expand Up @@ -531,6 +565,7 @@ static int waitForInput(lua_State* L)

static const struct luaL_Reg input_func[] = {
{ "open", openInputDevice },
{ "fdopen", openInputFD },
{ "close", closeByFd },
{ "closeAll", closeAllInputDevices },
{ "waitForEvent", waitForInput },
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/fbink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ endif()
ko_write_gitclone_script(
GIT_CLONE_SCRIPT_FILENAME
https://github.com/NiLuJe/FBInk.git
db25821d8bcaba6edaea4d09fef7e5e395643f9c
012dd73784969a05095400137b358ef43522aed5
${SOURCE_DIR}
)

Expand Down
2 changes: 1 addition & 1 deletion thirdparty/kobo-usbms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ list(APPEND BUILD_CMD COMMAND ${KO_MAKE_RECURSIVE} "CFLAGS=${CFLAGS}" "LDFLAGS=$
ko_write_gitclone_script(
GIT_CLONE_SCRIPT_FILENAME
https://github.com/koreader/KoboUSBMS.git
d8d1a54ba01b498fc0ae254e643b82e96c44a9e6
24a8bf42243c78c6c5ed90bb6cbd7f3ff4b03616
${SOURCE_DIR}
)

Expand Down