MacOS – How does Terminal.app detect a change to the terminal parameters

command linemacosterminalterminal.app

In an answer to another question, Chris Page said that Terminal can detect when the terminal parameters are in a state that is likely to be for password entry. For example, the terminal may be in cooked mode with character echo turned off.

How does Terminal detect this? Is there an API where you can subscribe to changes to the terminal parameters? Does Terminal poll? Is a special character sent to the terminal when the terminal parameters change?

Best Answer

Terminal polls the tty device state using tcgetattr(3) x-man-page://3/tcgetattr and checks that the c_lflag ICANON flag is set and the ECHO flag is not.

It polls after receiving output from the remote program, or when the user enters text using an inline input method that may need to be blocked from displaying user input as “marked” text.

Related Question