Skip to content

Commit

Permalink
Add input command
Browse files Browse the repository at this point in the history
  • Loading branch information
rvaiya committed Sep 2, 2022
1 parent 5fc6cde commit 9599960
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 51 deletions.
9 changes: 5 additions & 4 deletions data/keyd.compose
Original file line number Diff line number Diff line change
Expand Up @@ -34486,7 +34486,8 @@
<Cancel> <q> <l> <x> : "󠇭"
<Cancel> <q> <l> <y> : "󠇮"
<Cancel> <q> <l> <z> : "󠇯"
<Cancel> <q> <m> <0> : "󰀀"
<Cancel> <q> <m> <1> : "󿿽"
<Cancel> <q> <m> <2> : "􀀀"
<Cancel> <q> <m> <3> : "􏿽"
<Cancel> <q> <m> <0> : "私"
<Cancel> <q> <m> <1> : "󰀀"
<Cancel> <q> <m> <2> : "󿿽"
<Cancel> <q> <m> <3> : "􀀀"
<Cancel> <q> <m> <4> : "􏿽"
1 change: 1 addition & 0 deletions data/unicode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34620,6 +34620,7 @@ E01EC;VARIATION SELECTOR-253;Mn;0;NSM;;;;;N;;;;;
E01ED;VARIATION SELECTOR-254;Mn;0;NSM;;;;;N;;;;;
E01EE;VARIATION SELECTOR-255;Mn;0;NSM;;;;;N;;;;;
E01EF;VARIATION SELECTOR-256;Mn;0;NSM;;;;;N;;;;;
079C1;VARIATION SELECTOR-256;Mn;0;NSM;;;;;N;;;;;
F0000;<Plane 15 Private Use, First>;Co;0;L;;;;;N;;;;;
FFFFD;<Plane 15 Private Use, Last>;Co;0;L;;;;;N;;;;;
100000;<Plane 16 Private Use, First>;Co;0;L;;;;;N;;;;;
Expand Down
21 changes: 20 additions & 1 deletion scripts/generate_xcompose
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ open('src/unicode.c', 'w').write(f'''
#include <stdint.h>
#include <stdlib.h>
#include "keys.h"
uint32_t unicode_table[] = {{ {','.join(map(str, codes))} }};
int lookup_xcompose_index(uint32_t codepoint) {{
int unicode_lookup_index(uint32_t codepoint)
{{
size_t i = 0;
for(i = 0; i < sizeof(unicode_table)/sizeof(unicode_table[0]); i++) {{
Expand All @@ -62,6 +64,23 @@ open('src/unicode.c', 'w').write(f'''
return -1;
}}
void unicode_get_sequence(int idx, uint8_t codes[4])
{{
uint8_t chars[] = {{
KEYD_0, KEYD_1, KEYD_2, KEYD_3, KEYD_4, KEYD_5, KEYD_6, KEYD_7,
KEYD_8, KEYD_9, KEYD_A, KEYD_B, KEYD_C, KEYD_D, KEYD_E, KEYD_F,
KEYD_G, KEYD_H, KEYD_I, KEYD_J, KEYD_K, KEYD_L, KEYD_M, KEYD_N,
KEYD_O, KEYD_P, KEYD_Q, KEYD_R, KEYD_S, KEYD_T, KEYD_U, KEYD_V,
KEYD_W, KEYD_X, KEYD_Y, KEYD_Z
}};
codes[0] = KEYD_CANCEL;
codes[1] = chars[idx / (36 * 36) % 36];
codes[2] = chars[idx / 36 % 36];
codes[3] = chars[idx % 36];
}}
'''
.replace('\n\t', '\n')
.lstrip()
Expand Down
2 changes: 1 addition & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ int parse_macro_expression(const char *s, struct macro *macro)
break;
}
}
} else if ((xcode = lookup_xcompose_index(codepoint)) > 0)
} else if ((xcode = unicode_lookup_index(codepoint)) > 0)
ADD_ENTRY(MACRO_UNICODE, xcode);

tok += chrsz;
Expand Down
69 changes: 69 additions & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,69 @@ static void send_fail(int con, const char *fmt, ...)
va_end(args);
}

static int input(char *buf, size_t sz)
{
size_t i;
uint32_t codepoint;
uint8_t code;
uint8_t codes[4];

int csz;

while ((csz = utf8_read_char(buf, &codepoint))) {
int found = 0;
char s[2];

if (csz == 1) {
uint8_t code, mods;
s[0] = (char)codepoint;
s[1] = 0;

found = 1;
if (!parse_key_sequence(s, &code, &mods)) {
if (mods & MOD_SHIFT) {
vkbd_send_key(vkbd, KEYD_LEFTSHIFT, 1);
vkbd_send_key(vkbd, code, 1);
vkbd_send_key(vkbd, code, 0);
vkbd_send_key(vkbd, KEYD_LEFTSHIFT, 0);
} else {
vkbd_send_key(vkbd, code, 1);
vkbd_send_key(vkbd, code, 0);
}
} else if ((char)codepoint == ' ') {
vkbd_send_key(vkbd, KEYD_SPACE, 1);
vkbd_send_key(vkbd, KEYD_SPACE, 0);
} else if ((char)codepoint == '\n') {
vkbd_send_key(vkbd, KEYD_ENTER, 1);
vkbd_send_key(vkbd, KEYD_ENTER, 0);
} else if ((char)codepoint == '\t') {
vkbd_send_key(vkbd, KEYD_TAB, 1);
vkbd_send_key(vkbd, KEYD_TAB, 0);
} else {
found = 0;
}
}

if (!found) {
int idx = unicode_lookup_index(codepoint);
if (idx < 0) {
err("ERROR: could not find code for \"%.*s\"", csz, buf);
return -1;
}

unicode_get_sequence(idx, codes);

for (i = 0; i < 4; i++) {
vkbd_send_key(vkbd, codes[i], 1);
vkbd_send_key(vkbd, codes[i], 0);
}
}
buf+=csz;
}

return 0;
}

static void handle_client(int con)
{
struct ipc_message msg;
Expand All @@ -252,6 +315,12 @@ static void handle_client(int con)
struct config_ent *ent;
int success;

case IPC_INPUT:
if (input(msg.data, msg.sz))
send_fail(con, "%s", errstr);
else
send_success(con);
break;
case IPC_RELOAD:
reload();
send_success(con);
Expand Down
23 changes: 3 additions & 20 deletions src/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,6 @@ static void update_mods(struct keyboard *kbd, int excluded_layer_idx, uint8_t mo
set_mods(kbd, mods);
}

static void generate_unicode_sequence(int idx, uint8_t codes[3])
{
uint8_t chars[] = {
KEYD_0, KEYD_1, KEYD_2, KEYD_3, KEYD_4, KEYD_5, KEYD_6, KEYD_7,
KEYD_8, KEYD_9, KEYD_A, KEYD_B, KEYD_C, KEYD_D, KEYD_E, KEYD_F,
KEYD_G, KEYD_H, KEYD_I, KEYD_J, KEYD_K, KEYD_L, KEYD_M, KEYD_N,
KEYD_O, KEYD_P, KEYD_Q, KEYD_R, KEYD_S, KEYD_T, KEYD_U, KEYD_V,
KEYD_W, KEYD_X, KEYD_Y, KEYD_Z
};

codes[0] = chars[idx / (36 * 36) % 36];
codes[1] = chars[idx / 36 % 36];
codes[2] = chars[idx % 36];
}

static void execute_macro(struct keyboard *kbd, int dl, const struct macro *macro)
{
size_t i;
Expand All @@ -182,7 +167,7 @@ static void execute_macro(struct keyboard *kbd, int dl, const struct macro *macr
switch (ent->type) {
size_t j;
uint16_t idx;
uint8_t codes[3];
uint8_t codes[4];
uint8_t code, mods;

case MACRO_HOLD:
Expand Down Expand Up @@ -210,12 +195,10 @@ static void execute_macro(struct keyboard *kbd, int dl, const struct macro *macr
idx = ent->data;

set_mods(kbd, 0);
send_key(kbd, KEYD_CANCEL, 1);
send_key(kbd, KEYD_CANCEL, 0);

generate_unicode_sequence(idx, codes);
unicode_get_sequence(idx, codes);

for (i = 0; i < 3; i++) {
for (i = 0; i < 4; i++) {
send_key(kbd, codes[i], 1);
send_key(kbd, codes[i], 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define KEYBOARD_H

#include "keyd.h"
#include "unicode.h"
#include "config.h"
#include "device.h"

Expand Down
20 changes: 20 additions & 0 deletions src/keyd.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,25 @@ static int add_bindings(int argc, char *argv[])
return ret;
}

static int input(int argc, char *argv[])
{
size_t sz = 0;
char buf[MAX_IPC_MESSAGE_SIZE];

while (1) {
size_t n;

if ((n = read(0, buf+sz, sizeof(buf)-sz)) <= 0)
break;
sz += n;

if (sizeof(buf) == sz)
die("maxiumum input length exceeded");
}

return ipc_exec(IPC_INPUT, buf, sz);
}

static int layer_listen(int argc, char *argv[])
{
struct ipc_message msg;
Expand Down Expand Up @@ -141,6 +160,7 @@ struct {
/* Keep -e and -m for backward compatibility. TODO: remove these at some point. */
{"monitor", "-m", "--monitor", monitor},
{"bind", "-e", "--expression", add_bindings},
{"input", "", "", input},

{"listen", "", "", layer_listen},

Expand Down
2 changes: 2 additions & 0 deletions src/keyd.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "keyboard.h"
#include "keys.h"
#include "vkbd.h"
#include "string.h"

#define MAX_IPC_MESSAGE_SIZE 4096

Expand Down Expand Up @@ -70,6 +71,7 @@ struct ipc_message {
IPC_FAIL,

IPC_BIND,
IPC_INPUT,
IPC_RELOAD,
IPC_LAYER_LISTEN,
} type;
Expand Down
23 changes: 21 additions & 2 deletions src/unicode.c

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*/


int lookup_xcompose_index(uint32_t codepoint);
int unicode_lookup_index(uint32_t codepoint);
void unicode_get_sequence(int idx, uint8_t codes[4]);

#endif
18 changes: 6 additions & 12 deletions t/macro-unicode-2.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ control up
1 up
cancel down
cancel up
3 down
3 up
2 down
2 up
3 down
3 up
3 down
3 up
4 down
4 up
2 down
2 up
o down
o up
y down
y up
6 down
6 up
control down
control up
16 changes: 6 additions & 10 deletions t/macro-unicode.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

cancel down
cancel up
3 down
3 up
2 down
2 up
3 down
3 up
3 down
3 up
4 down
4 up
o down
o up
y down
y up
6 down
6 up

0 comments on commit 9599960

Please sign in to comment.