2

When I research mouse interfacing with ncurses, I see many options, but I do not see any way to detect when the mouse has left the program window. The window is the window of the terminal emulator, not an ncurses window.

AlgoRythm
  • 1,196
  • 10
  • 31
  • The entire universe of a terminal program or application, like curses, is inhabited by little characters, running all over the place. The terminal program doesn't know anything about a strange creature called a "mouse". In order for the terminal program to have any clue what that is, there has to be a character sequence that indicates that a mouse pointer entered the terminal window, or left it. Reviewing the terminfo man page, I am unable to find anything of that kind, so I do not believe that there's any such thing. – Sam Varshavchik Feb 01 '19 at 03:33
  • @SamVarshavchik I realize how escape sequences work. I was also not able to find anything, which is why I asked here. – AlgoRythm Feb 01 '19 at 03:35
  • Support for the mouse pointer in terminal applications seems to be quite lacking in its documentation. In terminfo's man page, the closest I could find is a mention of some sequence called `req_mouse_pos`, but what it is, and what it means, seems to be somewhat lacking in description, and a Google search comes up nothing but references to the same man page. – Sam Varshavchik Feb 01 '19 at 03:49

1 Answers1

2

That's not in the repertoire of ncurses' mouse interface, but for some terminals you could set them up to send xterm's leave- and enter-window control sequences, which your program could read either byte-by-byte using getch, or by using define_key to associate the responses as a "function key".

XTerm Control Sequences lists in the section on FocusIn/FocusOut:

FocusIn/FocusOut can be combined with any of the mouse events since it uses a different protocol. When set, it causes xterm to send CSI I when the terminal gains focus, and CSI O when it loses focus.

That is enabled with

CSI ? Pm h
          DEC Private Mode Set (DECSET).
...
            Ps = 1 0 0 4  -> Send FocusIn/FocusOut events, xterm.

for instance,

printf("\033[?1004h");
fflush(stdout);

(a few other terminals implement this, but since they do not document their behavior, you'll have to experiment to find out whether this applies to the terminal you happen to be using).

In ncurses, you could associate the responses with define_key, e.g.,

#define KEY_FOCUS_IN     1001
#define KEY_FOCUS_OUT    1002

define_key("\033[I", KEY_FOCUS_IN);
define_key("\033[O", KEY_FOCUS_OUT);

and (if keypad is enabled), detect those values in your program as the return value from getch.

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