diff --git a/RemarkableLamyEraser.pro b/RemarkableLamyEraser.pro new file mode 100644 index 0000000..dbc5c9d --- /dev/null +++ b/RemarkableLamyEraser.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +CONFIG += console +CONFIG -= app_bundle +CONFIG -= qt + +INCLUDEPATH += /opt/codex/rm11x/2.5.2/sysroots/x86_64-codexsdk-linux/usr/lib/arm-remarkable-linux-gnueabi/gcc/arm-remarkable-linux-gnueabi/7.3.0/include + +SOURCES += \ + main.c + +target.path = /home/root +INSTALLS += target + +DISTFILES += \ + RemarkableLamyEraser/LamyEraser.service diff --git a/RemarkableLamyEraser/LamyEraser.service b/RemarkableLamyEraser/LamyEraser.service new file mode 100644 index 0000000..97da97a --- /dev/null +++ b/RemarkableLamyEraser/LamyEraser.service @@ -0,0 +1,10 @@ +[Unit] +Description=Lamy Pen Button as Eraser +After=home.mount + +[Service] +ExecStart=/home/root/RemarkableLamyEraser/RemarkableLamyEraser +Restart=on-failure + +[Install] +WantedBy=multi-user.target diff --git a/main.c b/main.c new file mode 100644 index 0000000..5118ecb --- /dev/null +++ b/main.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include +#include + +#define PEN_DEVICE "/dev/input/event1" + +//const struct input_event tool_touch_off = { .type = EV_KEY, .code = BTN_TOUCH, .value = 0}; //these might be used in the future to improve press and hold mode +//const struct input_event tool_pen_on = { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 1}; //used when pen approaches the screen +//const struct input_event tool_pen_off = { .type = EV_KEY, .code = BTN_TOOL_PEN, .value = 0}; +const struct input_event tool_rubber_on = { .type = EV_KEY, .code = BTN_TOOL_RUBBER, .value = 1}; //used when rubber approaches the screen +const struct input_event tool_rubber_off = { .type = EV_KEY, .code = BTN_TOOL_RUBBER, .value = 0}; + + +void writeEvent(int fd, struct input_event event) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + event.time = tv; + //debug: printf("writing: seconds = %ld, usec= %ld, type = %d, code = %d, value = %d\n", event.time.tv_sec, event.time.tv_usec, event.type, event.code, event.value); + write(fd, &event, sizeof(struct input_event)); +} + +int main(int argc, char *argv[]) { + struct input_event ev_pen; + int fd_pen; + bool toggleMode = false; + bool toggle = 0; + char name[256] = "Unknown"; + + if (argc > 2) { + printf("Too many arguments supplied."); + return EXIT_FAILURE; + } + else if (argc == 2) { + if (!strncmp(argv[1], "--toggle", 8)) { + printf("MODE: TOGGLE\n"); + toggleMode = true; + } + } + else { + printf("MODE: PRESS AND HOLD\n"); + toggleMode = false; + } + + if ((getuid()) != 0) { + fprintf(stderr, "You are not root! This may not work...\n"); + return EXIT_SUCCESS; + } + + /* Open Device */ + fd_pen = open(PEN_DEVICE, O_RDWR); + if (fd_pen == -1) { + fprintf(stderr, "%s is not a vaild device\n", PEN_DEVICE); + return EXIT_FAILURE; + } + + + /* Print Device Name */ + ioctl(fd_pen, EVIOCGNAME(sizeof(name)), name); + printf("Reading from:\n"); + printf("device file = %s\n", PEN_DEVICE); + printf("device name = %s\n", name); + + + for (;;) { + const size_t ev_pen_size = sizeof(struct input_event); + read(fd_pen, &ev_pen, ev_pen_size); + + if (toggleMode) { //toggle mode + if (ev_pen.code == BTN_STYLUS && ev_pen.value == 1) { + toggle = !toggle; + printf("toggle: %d\n", toggle); + } + if (toggle) + if (ev_pen.code == BTN_TOOL_PEN) { + if (ev_pen.value == 1) { + printf("writing eraser on"); + writeEvent(fd_pen, tool_rubber_on); + } + else { + printf("writing eraser off"); + writeEvent(fd_pen, tool_rubber_off); + } + } + } + else //press and hold mode + { + if (ev_pen.code == BTN_STYLUS) { + ev_pen.code = BTN_TOOL_RUBBER; //value will follow the button, so we can reuse the message + writeEvent(fd_pen, ev_pen); + } + } + } + return EXIT_SUCCESS; +}