查看“Terminals”的源代码
←
Terminals
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
This page is about the very essentials of how to implement video terminal functionality. Terminals are connected to the OS over [[Serial_Ports|serial port]], but could use network too with the [[telnet]] and [[ssh]] protocols. ==History== [[File:VT220_Irssi.jpg|200px|thumb|A real terminal]] [[File:PuTTY_Ubuntu.png|200px|thumb|PuTTY with serial port option]] [[File:Minicom_cs_xterm.png|200px|thumb|Minicom running in xterm]] Originally terminals were line printers. The user typed in a command, pressed enter, and the computer executed that command and printed the results line by line. This is where [[Command_Line|command line interface]] originates. It understood the basic [https://en.wikipedia.org/wiki/ASCII#Control_characters ASCII control characters], like ASCII 8 (backspace), ASCII 10 (line feed), ASCII 13 (carriage return) etc. but nothing else. Although this input and output method works for simple programs, it is very complicated to implement a text editor for example. Many editors kept the line terminal heritage, like Vi, but to provide a more comfortable interface, the printer was replaced with a screen. With it come the need to position the cursor on the screen, hence video terminal and video terminal sequences was born. There were many more or less successful terminals (most notably the VT52), but finally Digital Equipment Corporation's [[VT100]] got widespread and its codes got standardized. The VT100 codes (also called ANSI escape codes) are the most widely emulated sequences, ever. ==Communication== When your OS is communicating over the serial like, it is very likely that on the other end there's a terminal. Either a real one, or a terminal emulator, like PuTTY or minicom. It is the terminal's duty to handle the [[Keyboard|keyboard]], the key modifier keys (like Shift, Control, Alt), and send terminal key sequences. It is also the terminal's duty to interpret the received terminal codes (sometimes also called escape sequences) and visualize them properly. These sequences always start with the ASCII 27 (escape) character, "\033" in C literals, and terminated by a latin uppercase or lowercase letter or tilde ("~"). In between there might be numeric parameters (0-9) separated by comma ("," and ";") but such arguments are always prefixed by "[". This two bytes start sequence "\033[" also called Control Sequence Introducer, or CSI. ===Receiving Keys=== This section is about the bytes your OS might receive from a terminal. It is important to know that terminals are different, some are configurable, so they might send different codes. For a comprehensive list of all possible key codes, consult the [https://raw.githubusercontent.com/marcv81/termcap/master/termcap.src termcap database]. {| {{wikitable}} ! Code ! Name ! Meaning |- | ASCII 3 || Break (^C) || the Break signal |- | ASCII 7 || Bell (^G) || makes a beep |- | ASCII 8 || BS (^H) || Backspace, moves the cursor left and delete, some terminals send ASCII 127 instead |- | ASCII 9 || Tab (^I) || Tabulator, moves to cursor to the next multiple of 8 coloumn on the right |- | ASCII 10 || Line Feed (^J) || move the cursor down (on modern machines line feed and carriage return is combined and called newline) |- | ASCII 13 || Carriage Return (^M) || Return or Enter key, move the cursor to the left most coloumn |- | ASCII 27 || Escape (^[) || start the command sequences |- | ASCII 32 - 126 || keys || "normal" keys |- | ASCII 127 or \033[3~ || Delete || delete without moving the cursor, some terminals send ASCII 127 instead of ASCII 8, but \033[3~ is standard |- | \033[A || Up || up arrow key |- | \033[B || Down || down arrow key |- | \033[C || Right || right arrow key |- | \033[D || Left || left arrow key |- | \033[5~ or \033[S || PgUp || the Page Up key |- | \033[6~ or \033[T || PgDn || the Page Down key |- | \033[7~ or \033[H || Home || the Home key |- | \033[8~ || End || the End key |- | \033[11~ to \033[21~ || F1 - F10 || function keys |- | \033[''row'';''col''R || Status report || answer to the \033[6n code |} Note that cursor keys are standard, but there might be differences with the Home, End, PgUp, PgDn and Fn keys depending on the terminal. Key modifiers (like Shift, Caps Lock, Control, Alt, Compose etc.) are always interpreted by the terminal, and have no codes. ===Sending Sequences=== These are the bytes that your OS might send to a terminal. Terminals and terminal emulators are different, they usually understand different codes. However there's a subset of codes that all guaranteed to understand, and those are the VT100 codes. Modern terminal emulators usually support more, the [https://vt100.net/docs/vt220-rm/ VT220] subset is the smallest common denominator. Everything above that requires exact terminal type detection and differentiated codes. {| {{wikitable}} ! Code ! Name ! Meaning |- | \033[''n''A || CUU || move the cursor up ''n'' rows |- | \033[''n''B || CUD || move the cursor down ''n'' rows |- | \033[''n''C || CUF || move the cursor right ''n'' coloumns |- | \033[''n''D || CUB || move the cursor left ''n'' coloumns |- | \033[''n''E || CNL || move the cursor to the beginning of line ''n'' down |- | \033[''n''F || CPL || move the cursor to the beginning of line ''n'' up |- | \033[''n''G || CHA || move the cursor to coloumn ''n'' |- | \033[H || CUP || move the cursor to the top left corner of screen |- | \033[''row'';''col''H || CUP || move the cursor |- | \033[''row'';''col''f || HVP || move the cursor |- | \033[J or \033[0J || ED || clear the screen from cursor to the end |- | \033[1J || ED || clear the screen from beginning to the cursor |- | \033[2J || ED || clear the entire screen |- | \033[K or \033[0K || EL || clear the line from cursor to the end |- | \033[1K || EL || clear the line from beginning to the cursor |- | \033[2K || EL || clear the entire line |- | \033[S || SU || scroll up one page |- | \033[T || SD || scroll down one page |- | \033[m || SGR || reset color attributes |- | \033[1m || SGR || select bold or intensive color |- | \033[''30-37''m || SGR || select foreground color |- | \033[''40-47''m || SGR || select background color |} The Select Graphic Rendition (or SGR) codes are often concatenated into one command, for example to select bright white on blue background \033[1;37;0;44m. Some terminals understands color codes 90 - 97 (same as 1;30 - 1;37) and 100 - 107 (same as 1;40 - 1;47). For the foreground and background color codes, these are as follows: {| class="wikitable" |- ! FG ! BG ! Name ! RGB (VGA colors) |- | 30 || 40 || Black || style="background: #000; color: white" | 0,0,0 |- | 31 || 41 || Red || style="background: #AA0000; color: white" | 170,0,0 |- | 32 || 42 || Green || style="background: #00AA00; color: white" | 0,170,0 |- | 33 || 43 || Yellow || style="background: #AA5500; color: white" | 170,85,0 |- | 34 || 44 || Blue || style="background: #0000AA; color: white" | 0,0,170 |- | 35 || 45 || Magenta || style="background: #AA00AA; color: white" | 170,0,170 |- | 36 || 46 || Cyan || style="background: #00AAAA; color: white" | 0,170,170 |- | 37 || 47 || White || style="background: #AAAAAA; color: black" | 170,170,170 |- | 1;30 || 1;40 || Bright Black (Gray) || style="background: #555555; color: white" | 85,85,85 |- | 1;31 || 1;41 || Bright Red || style="background: #FF5555; color: white" | 255,85,85 |- | 1;32 || 1;42 || Bright Green || style="background: #55FF55; color: black" | 85,255,85 |- | 1;33 || 1;43 || Bright Yellow || style="background: #FFFF55; color: black" | 255,255,85 |- | 1;34 || 1;44 || Bright Blue || style="background: #5555FF; color: white" | 85,85,255 |- | 1;35 || 1;45 || Bright Magenta || style="background: #FF55FF; color: black" | 255,85,255 |- | 1;36 || 1;46 || Bright Cyan || style="background: #55FFFF; color: black" | 85,255,255 |- | 1;37 || 1;47 || Bright White || style="background: #FFFFFF; color: black" | 255,255,255 |} ===Detecting Terminal=== First and most, you should detect the size of the screen. This is done by moving the cursor over the screen, and then query the position. \033[999;999H\033[6n\033[H = your OS to the terminal \033[''row'';''col''R = the terminal's answer, screen size To detect the exact terminal type to determine if you can use advanced codes (like \033[38;2;''r'';''g'';''b''m RGB foreground color), well, not possible, as there's no commonly supported identify command. Each terminal has a special command with a specific answer that you can use. Or simply leave it to the user, many OS use the TERM environment variable to select a code set from the termcap database. ==Implementing A Terminal== Once your OS is sufficiently advanced, you'll probably want to implement a terminal emulator on your OS too so that you can receive connections. For that, you must interpret these sequences, display them on screen accordingly, and convert the keyboard scancodes into ASCII sequences. Under DOS, this exactly what ANSI.SYS does. For Linux, take a look at minicom's source code. ==See Also== *[[Keyboard]] *[[VGA Fonts]] *[[Serial ports]] ===External Links=== *[https://raw.githubusercontent.com/marcv81/termcap/master/termcap.src termcap database] *[https://vt100.net/docs/vt220-rm/ VT220 Programmer's Manual] *[https://vt100.net https://vt100.net] many useful docs and specs, no 1. source for video terminals *[https://www.chiark.greenend.org.uk/~sgtatham/putty/ PuTTY] *[https://en.wikipedia.org/wiki/Minicom Minicom] [[Category:Human Interface Device]] [[Category:Network Hardware]] [[Category:Common Devices]]
本页使用的模板:
模板:Wikitable
(
查看源代码
)
返回至“
Terminals
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
变体
已展开
已折叠
查看
阅读
查看源代码
查看历史
更多
已展开
已折叠
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息