Skip to content

Commit

Permalink
Add reMarkable2 generateFakeEvents (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
snelg committed Mar 6, 2021
1 parent 58fc509 commit 30c52fe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -109,13 +109,13 @@ libs: \
$(OUTPUT_DIR)/libs/libinkview-compat.so: input/inkview-compat.c
$(CC) $(DYNLIB_CFLAGS) -linkview -o $@ $<

$(OUTPUT_DIR)/libs/libkoreader-input.so: input/*.c input/*.h $(if $(KINDLE),$(POPEN_NOSHELL_LIB),)
$(OUTPUT_DIR)/libs/libkoreader-input.so: input/*.c input/*.h $(if $(or $(KINDLE),$(REMARKABLE)),$(POPEN_NOSHELL_LIB),)
@echo "Building koreader input module..."
$(CC) $(DYNLIB_CFLAGS) $(SYMVIS_FLAGS) -I$(POPEN_NOSHELL_DIR) -I./input \
$(if $(CERVANTES),-DCERVANTES,) $(if $(KOBO),-DKOBO,) $(if $(KINDLE),-DKINDLE,) $(if $(POCKETBOOK),-DPOCKETBOOK,) $(if $(REMARKABLE),-DREMARKABLE,) $(if $(SONY_PRSTUX),-DSONY_PRSTUX,)\
-o $@ \
input/input.c \
$(if $(KINDLE),$(POPEN_NOSHELL_LIB),) \
$(if $(or $(KINDLE),$(REMARKABLE)),$(POPEN_NOSHELL_LIB),) \
$(if $(POCKETBOOK),-linkview,)

# Would need a bit of patching to be able to use -fvisibility=hidden...
Expand Down
70 changes: 65 additions & 5 deletions input/input-remarkable.h
Expand Up @@ -18,13 +18,16 @@
#ifndef _KO_INPUT_REMARKABLE_H
#define _KO_INPUT_REMARKABLE_H

#define MACHINE_PATH "/sys/devices/soc0/machine"
#define CHARGER_DEVPATH "/devices/soc0/soc/2100000.aips-bus/2184000.usb/power_supply/imx_usb_charger"
#define CHARGER_ONLINE_PATH "/sys" CHARGER_DEVPATH "/online"
#define BATTERY_DEVPATH "/devices/soc0/soc/2100000.aips-bus/21a0000.i2c/i2c-0/0-0055/power_supply/bq27441-0"
#define BATTERY_STATUS_PATH "/sys" BATTERY_DEVPATH "/status"

#include "libue.h"
#include "input.h"
#include "popen_noshell.h"
static struct popen_noshell_pass_to_pclose pclose_arg;

static size_t read_file(char const* path, char* buf, size_t buflen)
{
Expand All @@ -47,13 +50,59 @@ static void input_write_event(int fd, int code)
}
}

static void generateFakeEvent(int pipefd[2]) {
/*
* On wakeup, reMarkable 2 does not send Power Button events
* So we watch DBus and insert a "Power Button Released" event when the rM2 wakes up
*/
static void generateFakeEventRM2(int pipefd) {
FILE *fp;
char std_out[256];
int status;
struct input_event ev;

ev.type = EV_KEY;
ev.code = 116; // rM2 "Power button" key
ev.value = 0; // Key released

char *argv[] = { "dbus-monitor", "--system", "member='PrepareForSleep'", (char *)NULL };
fp = popen_noshell("dbus-monitor", (const char * const *)argv, "r", &pclose_arg, 0);
if (!fp) {
fprintf(stderr, "[remarkable-fake-event] Failed to popen_noshell dbus-monitor\n");
return;
}

fflush(fp);

while (fgets(std_out, sizeof(std_out)-1, fp)) {
if (!strncmp(std_out, " boolean false", 16)) {
gettimeofday(&ev.time, NULL);
if (write(pipefd, &ev, sizeof(struct input_event)) == -1) {
fprintf(stderr, "Failed to generate Power Button Released event.\n");
}
}
}

status = pclose_noshell(&pclose_arg);
if (status == -1) {
err(EXIT_FAILURE, "pclose_noshell()");
} else {
if (WIFEXITED(status)) {
printf("dbus-monitor exited normally with status: %d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("dbus-monitor was killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("dbus-monitor was stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("dbus-monitor continued\n");
}
}
}

static void generateFakeEventRM1(int pipefd) {
int re;
struct uevent_listener listener;
struct uevent uev;

close(pipefd[0]);

re = ue_init_listener(&listener);
if (re < 0) {
fprintf(stderr, "[remarkable-fake-event] Failed to initilize libue listener, err: %d\n", re);
Expand All @@ -70,7 +119,7 @@ static void generateFakeEvent(int pipefd[2]) {
if (read_file(CHARGER_ONLINE_PATH, fbuf, sizeof(fbuf))) {
int new_state = strcmp(fbuf, "1\n") == 0? 1 : 0;
if (new_state != charger_state) {
input_write_event(pipefd[1], new_state? CODE_FAKE_USB_PLUG_IN : CODE_FAKE_USB_PLUG_OUT);
input_write_event(pipefd, new_state? CODE_FAKE_USB_PLUG_IN : CODE_FAKE_USB_PLUG_OUT);
}
charger_state = new_state;
}
Expand All @@ -79,7 +128,7 @@ static void generateFakeEvent(int pipefd[2]) {
if (read_file(BATTERY_STATUS_PATH, fbuf, sizeof(fbuf))) {
int new_state = strcmp(fbuf, "Charging\n") == 0? 1 : 0;
if (new_state != battery_state) {
input_write_event(pipefd[1], new_state? CODE_FAKE_CHARGING : CODE_FAKE_NOT_CHARGING);
input_write_event(pipefd, new_state? CODE_FAKE_CHARGING : CODE_FAKE_NOT_CHARGING);
}
battery_state = new_state;
}
Expand All @@ -88,4 +137,15 @@ static void generateFakeEvent(int pipefd[2]) {
}
}

static void generateFakeEvent(int pipefd[2]) {
close(pipefd[0]);

char mbuf[32];
if (read_file(MACHINE_PATH, mbuf, sizeof(mbuf)) && !strncmp(mbuf, "reMarkable 2.0", 14)) {
generateFakeEventRM2(pipefd[1]);
} else {
generateFakeEventRM1(pipefd[1]);
}
}

#endif

0 comments on commit 30c52fe

Please sign in to comment.