Difference between the terminal file and the terminal screen

terminaltty

I recently posted a question about terminals and this seemed like it deserved its own question. If the tty which would be synonymous with terminal, is just the device driver/device file itself, what provides the actual interface to the terminal, and what is it called? I don't mean the gui in any way, I mean the actual area (either in a virtual terminal or a possibly a psuedo-terminal) where text is being printed and read. Is this considered a part of the terminal? Are these in some way controlled by the kernel? Basically if someone wanted to write a console program, like gnome-terminal (or even with virtual terminals), is there some sort of kernel based widget/interface that takes input and output? Basically is the tty (itself i.e. /dev/tty) just a file or does it include the code needed to display output (in various colors and weights), and read from the keyboard.

Is this correct: I found here What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'? that is says tty is synonymous with terminal. This is what is confusing me. I would think that to emulate a terminal or to provide a terminal, would require the terminal driver (tty) and the terminal display and interactive part (something like xterm). So by itself the tty is simply a driver for input and output, and what displays and reads the text from and to the tty is something like xterm? Would be calling the "/dev/tty a terminal driver" also be correct? Also when I say xterm I mean the value of the $TERM var.

Another thought (let me know about this one): The tty file is synonymous with the connection between the terminal or these day terminal emulator, and the shell. Now they are all in the same box, but the original terminals would be the terminal emulators of today (clearly), but the tty or tty files would sort of act like the serial connection between the terminal and the shell/operating system. These days it seems like to me the terminal emulator is an environment/window for interacting with the /dev/tty… files and the shell sits at the other side? So writing to the /dev/tty file would be like writing to or reading form the serial port? I'm assuming this is not exactly how it works, any help would be greatly appreciated :-(.

Best Answer

Originally, "tty" had two definitions: the hardware (now the emulator) and the driver (interfaced through /dev/pty* or /dev/tty*).

The hardware/emulator was/is responsible for:

  • Taking a stream of data and presenting it; this included interpreting control sequences like "move cursor left", "blinking cursor", "clear-screen" although these control sequences were often different among manufacturers.
  • Sending keycodes for keys typed by the user; most of these were standard ASCII characters, but some terminals sent propriety keycodes for even standard keys.

The 'tty' driver was responsible for:

  • Managing the buffering, in raw or canonical mode; for example, buffering a line of characters until Enter is pressed.
  • Managing the control flow; e.g. being able to stop/continue with Cntl-s/Cntl-q.
  • Translating propriety keycodes to standard ASCII, where applicable.
  • Intercepting certain control characters (like Cntl-c and Backspace) and processing them appropriately (send SIGINT on a Cntl-c or signal an EOF on Cntl-d.
  • Canonical display of characters, for example, if echo is turned off, then do not send feedback (character typed) back to the terminal.

The terminfo and termcap databases managed what terminal control characters should be sent for an operation (like 'clear-screen'). These control sequences were/are not interpreted by the driver, but by the hardware/emulator.