tty: reading input from a tty is now handled by the line discipline
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <socks/libc/ctype.h>
|
||||
|
||||
static void default_ldisc_write(struct device *, const struct input_event *);
|
||||
static kern_status_t default_ldisc_read(struct device *tty, void *buf, size_t max, size_t *nr_read, socks_flags_t flags);
|
||||
|
||||
static char keycode_chars[] = {
|
||||
[KEY_UNKNOWN] = ' ',
|
||||
@@ -234,6 +235,7 @@ static char keycode_chars_shift[] = {
|
||||
|
||||
static struct tty_ldisc default_ldisc = {
|
||||
.name = "n_tty",
|
||||
.read = default_ldisc_read,
|
||||
.write = default_ldisc_write,
|
||||
};
|
||||
|
||||
@@ -254,6 +256,34 @@ static enum tty_modifier_key get_modifier_key(const struct input_event *ev)
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_arrow_key(const struct input_event *ev, char *ch)
|
||||
{
|
||||
bool ret = false;
|
||||
switch (ev->ev_key.key) {
|
||||
case KEY_UP:
|
||||
ret = true;
|
||||
*ch = 'A';
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
ret = true;
|
||||
*ch = 'B';
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
ret = true;
|
||||
*ch = 'C';
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
ret = true;
|
||||
*ch = 'D';
|
||||
break;
|
||||
default:
|
||||
ret = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void handle_modifier_keys(struct device *tty, enum tty_modifier_key modkey, const struct input_event *ev)
|
||||
{
|
||||
struct tty_device *ttydev = TTY_DEVICE(tty);
|
||||
@@ -273,13 +303,13 @@ static void convert_ev_to_chars(struct device *tty, const struct input_event *ev
|
||||
|
||||
struct tty_device *ttydev = TTY_DEVICE(tty);
|
||||
|
||||
char echo[2];
|
||||
char echo[4];
|
||||
int echo_len = 0;
|
||||
|
||||
char data[4];
|
||||
int data_len = 0;
|
||||
|
||||
char c = keycode_chars[ev->ev_key.key];
|
||||
if (c == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ttydev->tty_modstate & TTY_KEY_CTRL) {
|
||||
if (!isalpha(c)) {
|
||||
@@ -293,24 +323,67 @@ static void convert_ev_to_chars(struct device *tty, const struct input_event *ev
|
||||
c = tolower(c);
|
||||
c -= 97;
|
||||
c++;
|
||||
data[0] = c;
|
||||
data_len = 1;
|
||||
} else if (ttydev->tty_modstate & TTY_KEY_SHIFT) {
|
||||
c = keycode_chars_shift[ev->ev_key.key];
|
||||
echo[0] = c;
|
||||
echo_len = 1;
|
||||
} else {
|
||||
data[0] = c;
|
||||
data_len = 1;
|
||||
} else if (is_arrow_key(ev, &c)) {
|
||||
echo[0] = '^';
|
||||
echo[1] = '[';
|
||||
echo[2] = '[';
|
||||
echo[3] = c;
|
||||
echo_len = 4;
|
||||
|
||||
data[0] = 0x1b;
|
||||
data[1] = '[';
|
||||
data[2] = c;
|
||||
data_len = 3;
|
||||
} else if (c != 0) {
|
||||
echo[0] = c;
|
||||
echo_len = 1;
|
||||
data[0] = c;
|
||||
data_len = 1;
|
||||
}
|
||||
|
||||
if (c == 0) {
|
||||
if (data_len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ringbuffer_write(&ttydev->tty_input, sizeof c, &c, S_NOBLOCK);
|
||||
ringbuffer_write(&ttydev->tty_input, data_len, data, S_NOBLOCK);
|
||||
size_t nr_written;
|
||||
tty_write(tty, echo, echo_len, &nr_written, S_NOBLOCK);
|
||||
}
|
||||
|
||||
static kern_status_t canonical_read(struct device *tty, void *buf, size_t max, size_t *nr_read, socks_flags_t flags)
|
||||
{
|
||||
//struct tty_device *ttydev = TTY_DEVICE(tty);
|
||||
//struct termios backup = ttydev
|
||||
//char *linebuf = ttydev->tty_linebuf;
|
||||
//unsigned int pos = 0, len = 0;
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static kern_status_t non_canonical_read(struct device *tty, void *buf, size_t max, size_t *nr_read, socks_flags_t flags)
|
||||
{
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static kern_status_t default_ldisc_read(struct device *tty, void *buf, size_t max, size_t *nr_read, socks_flags_t flags)
|
||||
{
|
||||
struct tty_device *ttydev = TTY_DEVICE(tty);
|
||||
if (ttydev->tty_config.c_lflag & ICANON) {
|
||||
return canonical_read(tty, buf, max, nr_read, flags);
|
||||
} else {
|
||||
return non_canonical_read(tty, buf, max, nr_read, flags);
|
||||
}
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static void default_ldisc_write(struct device *tty, const struct input_event *ev)
|
||||
{
|
||||
enum tty_modifier_key modkeys = get_modifier_key(ev);
|
||||
|
||||
@@ -75,8 +75,14 @@ static void putchar(struct device *tty, int c)
|
||||
|
||||
kern_status_t tty_read(struct device *tty, void *buf, size_t max, size_t *nr_read, socks_flags_t flags)
|
||||
{
|
||||
printk("tty_read");
|
||||
return KERN_OK;
|
||||
kern_status_t status = KERN_UNSUPPORTED;
|
||||
struct tty_device *ttydev = TTY_DEVICE(tty);
|
||||
|
||||
if (ttydev->tty_ldisc || ttydev->tty_ldisc->read) {
|
||||
status = ttydev->tty_ldisc->read(tty, buf, max, nr_read, flags);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
kern_status_t tty_write(struct device *tty, const void *buf, size_t len, size_t *nr_written, socks_flags_t flags)
|
||||
|
||||
Reference in New Issue
Block a user