deadpixi / mtm

Perhaps the smallest useful terminal multiplexer in the world.
1.1k stars 51 forks source link

Arrow Keys not working under Termion #46

Closed xvxx closed 4 years ago

xvxx commented 4 years ago

Hi there, I don't think this is a bug in mtm, but I could use some guidance if possible!

When using mtm to run a program created using the termion library for Rust, arrow keypresses are not being detected.

I've done some digging and it looks like this is because (by default) mtm sends "application mode" cursor commands like \EOA instead of "normal mode" cursor commands such as \E[A. Is this right or am I misunderstanding?

This line in particular checks for pnm on a node and sends the O or [ prefix accordingly:

https://github.com/deadpixi/mtm/blob/36910ba0/mtm.c#L1037

static void
sendarrow(const NODE *n, const char *k)
{
    char buf[100] = {0};
    snprintf(buf, sizeof(buf) - 1, "\033%s%s", n->pnm? "O" : "[", k);
    SEND(n, buf);
}

However, it seems that Termion only looks for \E[A and friends when detecting arrow keys: https://gitlab.redox-os.org/redox-os/termion/-/blob/a448f510/src/event.rs#L154-157

You can verify this by cloning Termion and running the keys example both within and without mtm:

git clone https://github.com/redox-os/termion
cd termion
cargo run --example keys

In my terminal, without mtm, I can run the example and see arrow keypresses registered. Under mtm, most keys work but the arrow keys don't do anything.

All that said, here's my question:

As an application developer, should I be sending an escape code to say I want "normal mode"? aka should I be setting pnm = false somehow? Or should the Termion library be looking for \EOA and parsing it the same as \E[A?

If so I am happy to send a patch. I guess I'm trying to determine if I should fix this behavior in my app or in the Termion library.

Thank you!

deadpixi commented 4 years ago

Hi Chris, mtm tries to pass all the relevant tests performed by vttest (“relevant” in this case meaning “the tests that exercise the features we want to emulate”). Moreover, mtm attempts to emulate GNU screen as well.

Most applications that use terminfo will send the appropriate escape sequences to get the terminal in a state they want, and also read from terminfo what the expected sequences are for function/cursor keys.

Anyway, it looks like screen starts with application mode reset for keypresses, so that’s what mtm should do too…so please check the latest master and see if that works for you.

Thanks, Rob

On Jun 20, 2020, at 15:46, chris west notifications@github.com wrote:

Hi there, I don't think this is a bug in mtm, but I could use some guidance if possible!

When using mtm to run a program created using the termion https://github.com/redox-os/termion library for Rust, arrow keypresses are not being detected.

I've done some digging and it looks like this is because (by default) mtm sends "application mode" cursor commands like \EOA instead of "normal mode" cursor commands such as \E[A. Is this right or am I misunderstanding?

This line in particular checks for pnm on a node and sends the O or [ prefix accordingly:

https://github.com/deadpixi/mtm/blob/36910ba0/mtm.c#L1037 https://github.com/deadpixi/mtm/blob/36910ba0/mtm.c#L1037 static void sendarrow(const NODE n, const char k) { char buf[100] = {0}; snprintf(buf, sizeof(buf) - 1, "\033%s%s", n->pnm? "O" : "[", k); SEND(n, buf); } However, it seems that Termion only looks for \E[A and friends when detecting arrow keys.

You can verify this by cloning Termion and running the keys example both within and without mtm:

git clone https://github.com/redox-os/termion cd termion cargo run --example keys In my terminal, without mtm, I can run the example and see arrow keypresses registered. Under mtm, most keys work but the arrow keys don't do anything.

All that said, here's my question:

As an application developer, should I be sending an escape code to say I want "normal mode"? aka should I be setting pnm = false somehow? Or should the Termion library be looking for \EOA and parsing it the same as \E[A?

If so I am happy to send a patch. I guess I'm trying to determine if I should fix this behavior in my app or in the Termion library.

Thank you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/deadpixi/mtm/issues/46, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEILV5XKMMSBXW7SWX2233RXUNZ5ANCNFSM4ODRFD3Q.

xvxx commented 4 years ago

Works great! Thanks so much!