From 9ee3410975ebe26cc448e6c379860b68afaad25e Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Mon, 13 Apr 2020 23:29:21 +0200 Subject: [PATCH] first working prototype --- .gitignore | 1 + .macrod.c.swp | Bin 0 -> 12288 bytes README.md | 0 macrod.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 .gitignore create mode 100644 .macrod.c.swp create mode 100644 README.md create mode 100644 macrod.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f47cb20 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.out diff --git a/.macrod.c.swp b/.macrod.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..84a3fb69f299e64fbafc10ee82b851b6b560e512 GIT binary patch literal 12288 zcmeI2-)|IE6vrVicBK4>)YvQ5kEcDuVp6A1+rQZPshga(u#uCp_Dx&ym2lRGno zrK!ZknCP=lM#ZS{=Y#P@-;5#t_yaTs9zl%{;uC}idGUMh%JVdQQ&8_sKdvpz=1pfY_A(4sCVVSy z2`>sOf%K0F3tw8MC4D=6l&ybZNl(!VXa$y~KpZ)vn+Dk98^@}&ZZN-|uUmU?Stj&k zt$m zKr5gX&@LQ3+>OoS|Npb!|1aIo*m>|h_yW8QUIi_17(5T2 z0i$3DtOZxrF!mR?1bznJfX~3k;3MFI0x-bu1C0F&&Vet%yI>A9feRX73akMG;Oc#h zodq9)_rPnw1y6$~z{BA3YR3Km7r_Pa9XJDC0X{em8ej%Yff4X1cnA!D)nFC)aTWXq zr@>w@2{r)^{>(A<19%^t0q=mFRvJSm*oveCO+eCArK_bsRQwL49;vJ$Cq3 zURUjY%WlmOSwI{a zQ%pkllqm2y8mJ>UsU;%~SBcZO?-;Qcx-%YZhYepw1zzkfR-0Wo&WlNqQ!7W@qcEK{ zvbP>p3$xbfC^czs**sdbl^+*vwLQm|+<@2Ig{kT|9$PwFKxjnLA?M^vMk1C zJ>TP}g!D-o%M1ng9VJ#Q1vl^tOo)_b_**=VHz8A@92c!1Y8T49SYeeSPlOD)Nt?2K zTX0AEEi{$b&?a34CtU0^>=;b85^^@j$b%)Z;fSP+t#m}#wbW2CsF(O6Wl0alx=D%C z#j?Y7BolLFgapS_Xqw82HpK)BJ7*fcH2%;iTBCPB(;)sy0vWLBYcdqe392G zpOW3H2vf+A9Nkpujdw10^JIYxi0Gv22~+YA|5y!<-A4^UvQhBNCeDK0PJ@c*A_;o) zlds>(_a2;_RNgAb7iL|KA@2^TBn*4M{LM&sL&YhTrnq@lX>AqCmFvbd-d8NYlAW77!8yZV0#trRNyY{4L z3e+aD7@lyU3pbKY%$m(OE7BMxU6X;R9SEiCq1v9Ehm&*XP}&;9YC@-cr}f44pm$9T z6-s=p#1R)raH4^01D66y9^EzQOrq4kDg|`1=P30{ygENlYV-=79vCFd(w8QpW1dg_ z2#i_ZwF|U=DO#dcN5P*Fksm}xf#cwrapYs3g{u{@HYME}LFD>gh+nprQO1jlYdAKv zX#Mq1Xurj59v*cHB$-m_-_xfqX zqbyHgjurW`%`M-Hq~E04+clwn7D}g1N2X_+(&j34!-W&!y6ep>dRJt-{*tbyTc^H% E04$HWcK`qY literal 0 HcmV?d00001 diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/macrod.c b/macrod.c new file mode 100644 index 0000000..f88853f --- /dev/null +++ b/macrod.c @@ -0,0 +1,111 @@ +#include +#include +#include +/* Standard errors */ +#include +/* Directory control */ +#include +#include + +#ifdef __linux__ + #include +#endif +#ifdef __FreeBSD__ + #include +#endif + +struct pressed_buffer { + unsigned short *buf; + unsigned int size; +}; + +int pressBufferAdd (struct pressed_buffer*, unsigned short); +int pressBufferRemove (struct pressed_buffer*, unsigned short); + +int main (void) // remember getopt() to automaically parse options +{ + FILE *fp; + fp = fopen("/dev/input/event0", "r"); + if (fp == NULL) { + fputs(strerror(errno), stderr); + exit(errno); + } + + struct input_event event; + struct pressed_buffer pb = {NULL, 0}; // Pressed keys buffer + + while (!ferror(fp)) { + fread(&event, sizeof(struct input_event), 1, fp); + if (event.type == EV_KEY) { + + switch (event.value) { + case (0): // Key release + pressBufferRemove(&pb, event.code); + break; + case (1): // Key press + pressBufferAdd(&pb, event.code); + break; + } + + printf("Pressed keys: "); + for (int i = 0; i < pb.size; i++) + printf("%d ", pb.buf[i]); + putchar('\n'); + } + } + + if (fclose(fp) == EOF) { + fputs(strerror(errno), stderr); + exit(errno); + } + return 0; +} + +int pressBufferAdd (struct pressed_buffer *pb, unsigned short key) +{ +/* Adds a keycode to the pressed buffer if it is not already present + * Returns non zero if the key was not added. */ + + if (!pb) return 1; + if (pb->buf != NULL) { + /* Linear search if the key is already buffered */ + for (int i = 0; i < pb->size; i++) + if (key == pb->buf[i]) return 1; + } + + unsigned short *b; + b = realloc(pb->buf, sizeof(unsigned short) * pb->size + 1); + if (!b) { + fprintf(stderr, "realloc failed in pressBufferAdd: %s", strerror(errno)); + exit(errno); + } + pb->buf = b; + pb->buf[pb->size++] = key; + + return 0; +} + +int pressBufferRemove (struct pressed_buffer *pb, unsigned short key) +{ +/* Removes a keycode from a pressed buffer if it is present returns + * non zero in case of failure (key not present or buffer empty). */ + if (!pb) return 1; + + for (int i = 0; i < pb->size; i++) { + if (pb->buf[i] == key) { + pb->size--; + pb->buf[i] = pb->buf[pb->size]; + unsigned short *b; + b = realloc(pb->buf, sizeof(unsigned short) * pb->size); + if (!b && pb->size) { + fprintf(stderr, "realloc failed in pressBufferRemove: %s", + strerror(errno)); + exit(errno); + } + pb->buf = b; + return 0; + } + } + + return 1; +}