6

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
tesche
  • 61
  • 4
  • 1
    The **Mouse Tracking** sections of `console_codes(4)` and xterm's `ctlseqs` (Linux distros usually have a copy at `/usr/share/doc/xterm/`) may help. – ninjalj Nov 21 '13 at 09:10
  • I found already half of that information via google, though it's nice to know I have it locally on my system too. So thanks for that. But I've meanwhile realized (or so I believe) that my problem lies not in the knowledge of the escape sequences or the terminal implementation, but in that the terminfo entry I'm using for that terminal does not yet properly advertise mouse support. So I've rewritten my question. – tesche Nov 21 '13 at 11:55
  • It start's getting weirder: I've written a test program in C using `has_mouse()`, `getch() == KEY_MOUSE`and `getmouse()` from ncurses. It works perfectly ok under both *xterm* and my terminal. Still, *mc* and *aptitude* only use the mouse correctly under *xterm*, but not my terminal. What am I missing? – tesche Nov 21 '13 at 18:24
  • I've changed to problem description again. – tesche Nov 26 '13 at 10:22
  • Note that xterm uses `CSI ?1000h` (`h`, not `m`) to enable mouse tracking. `mc` has the following hardcoded: `CSI?1001s` (save old highlight mouse tracking) `CSI?1000h`/`CSI?1002h` (enable mouse tracking) `CSI?1015h` (enable urxvt extended mouse coordinate reporting). (Where CSI is 7-bit CSI, i.e. `ESC [`) – ninjalj Nov 26 '13 at 13:04
  • Right - I've corrected 'm' -> 'h' so that other people reading this are not confused. But I had it correct in my terminal all along. – tesche Dec 03 '13 at 07:17
  • 1
    And I've also learned in the meantime that _mc_ has the mouse detection hardcoded too: It looks at the contents of the environment variable `TERM` and not the ncurses function `has_mouse()`. If not running in an _xterm_ it must be invoked with `mc -x`. My terminal was correct all along. – tesche Dec 03 '13 at 07:22
  • mc's a special case since it ignores the terminal description and ([see this](https://invisible-island.net/ncurses/man/ncurses.3x.html#h3-NCURSES_GPM_TERMS)) provides the mouse support that slang doesn't... – Thomas Dickey Dec 27 '18 at 01:57

1 Answers1

3

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105