Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement proper mouse support in a terminal / terminfo entry?

I've implemented a terminal emulator and a corresponding terminfo entry that allows me to run ncurses programs like emacs, mc (midnight commander) or tig (git browser). I want to add mouse support to the terminal, most notably to position the cursor in emacs by clicking into the window. After a lot of googling and some help on stackoverflow I learned about the required terminfo fields (most notably kmous) and control (e.g. \E[?1000h) and "key" (\E[M...) sequences and implemented mouse button events in my terminal. I've written a small ncurses program that goes something like this:

initscr ();
clear ();
noecho ();
cbreak ();

keypad (stdscr, TRUE);

mousemask (ALL_MOUSE_EVENT, NULL);

if (has_mouse ())
{
  while (1)
  {
    switch (getch ())
    {
    case KEY_MOUSE:
      if (getmouse (&event) == OK)
      {
        printf ("mouse event 0x%x at %i,%i\n", event.bstate, event.x, event.y);

This program works fine on xterm and my terminal, so both my terminal and its terminfo entry can't be completely wrong.

However, mc appears to not recognize mouse support in my terminal, does not even issue any \E[?1000h sequence to activate it and is therefore utterly confused by the mouse button events my terminal sends (even without \E[?1000hactivation).

What am I missing?

like image 283
tesche Avatar asked Nov 11 '22 18:11

tesche


1 Answers

Someone pointed out this problem recently (though the question was not mentioned):

20181124

    + modify the initialization checks for mouse so that the xterm+sm+1006
      block will work with terminal descriptions not mentioning xterm
      (report by Tomas Janousek).

The problem was that the code would use the kmous capability if TERM had "xterm", and otherwise would default to the original xterm mouse protocol (which didn't have "any event" capability). That probably was overlooked for quite a while due to inertia (people using the "xterm" terminal descriptions with other terminals).

The ncurses manual page does say what's intended:

Because there are no standard terminal responses that would serve to identify terminals which support the xterm mouse protocol, ncurses assumes that if your $TERM environment variable contains "xterm", or kmous is defined in the terminal description, then the terminal may send mouse events.

like image 172
Thomas Dickey Avatar answered Dec 24 '22 05:12

Thomas Dickey