| 1 | // Copyright 2026 The TCell Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package vt |
| 16 | |
| 17 | // Backend describes the backend of a terminal. |
| 18 | // This can be used to create a real emulator, while allowing the processor |
| 19 | // front end to handle the common details of parsing escape sequences, the state |
| 20 | // machine, and so forth. Backends support a limited set of common functionality, |
| 21 | // including a cursor. They only need to support writing at the cursor. |
| 22 | type Backend interface { |
| 23 | |
| 24 | // GetPrivateMode returns the status of a given private mode. |
| 25 | GetPrivateMode(PrivateMode) ModeStatus |
| 26 | |
| 27 | // SetPrivateMode sets a private mode to the given status. |
| 28 | // If either value is invalid, this should simply ignore the operation. |
| 29 | SetPrivateMode(PrivateMode, ModeStatus) error |
| 30 | |
| 31 | // GetSize returns the size of the terminal in characters. |
| 32 | // The X and Y are counts, so the bottom right cell should be at coordinate (X-1, Y-1). |
| 33 | GetSize() Coord |
| 34 | |
| 35 | // Colors returns the number of colors this terminal can support. For direct color, |
| 36 | // return 1<<24. The XTerm palette is assumed. Monochrome terminals should return 0. |
| 37 | Colors() int |
| 38 | |
| 39 | // Put content at the given location. The string in the cell might be a grapheme cluster, and the width can be |
| 40 | // 0, 1, or 2. The backend should try to optimize by keeping style and last coordinates if needed. |
| 41 | // A graphical backend could use this and be completely stateless. |
| 42 | Put(Coord, Cell) |
| 43 | |
| 44 | // GetPosition returns the cursor position. |
| 45 | GetPosition() Coord |
| 46 | |
| 47 | // SetPosition sets the cursor position. If the position is out of bounds, |
| 48 | // it should be clipped to the window size. |
| 49 | SetPosition(Coord) |
| 50 | |
| 51 | // Reset resets the terminal to default state. |
| 52 | Reset() |
| 53 | |
| 54 | // RaiseResize is called by the emulation layer when it has completed its own internal resizing. |
| 55 | // The backend is responsible for sending a signal (if needed) to child processes as part of this |
| 56 | // function. (The emulation layer knows nothing of child processes.) |
| 57 | RaiseResize() |
| 58 | |
| 59 | // Buffering is called by the emulator to indicate that the backend should buffer contents because |
| 60 | // multiple updates are taking place. This should be treated in addition to mode 2026, if the backend |
| 61 | // supports it. (Mode 2026 should only be supported by the backend if it actually supports true |
| 62 | // double buffering.) |
| 63 | Buffering(bool) |
| 64 | |
| 65 | // SetCursor is used to set the current cursor style. If the backend does not support changing |
| 66 | // the cursor shape, it should implement at least hidden, steady, and blinking (typically as a block). |
| 67 | SetCursor(CursorStyle) |
| 68 | } |
| 69 | |
| 70 | // Beeper can be implemented by a backend to indicate it can ring the bell or beep. |
| 71 | // This is typically done in response to a 0x07 bell. |
| 72 | type Beeper interface { |
| 73 | Beep() |
| 74 | } |
| 75 | |
| 76 | // Resizer adds notifications when the window size changes. |
| 77 | type Resizer interface { |
| 78 | // NotifyResize registers a channel to be posted to if the window size changes. |
| 79 | NotifyResize(chan<- bool) |
| 80 | } |
| 81 | |
| 82 | // Titler adds support for setting the window title. (Typically this is OSC 2.) |
| 83 | // Note that for security reasons we only support setting this. |
| 84 | // We don't bother with icon titles, since few terminal emulators support it, and it |
| 85 | // would be hard for us to do this in any portable fashion. |
| 86 | type Titler interface { |
| 87 | // SetWindowTitle only changes the window title. |
| 88 | SetWindowTitle(string) |
| 89 | } |
| 90 | |
| 91 | // MouseReporting determines what mouse events the backend reports. |
| 92 | type MouseReporting int |
| 93 | |
| 94 | const ( |
| 95 | MouseDisabled = MouseReporting(iota) // No mouse reports at all. |
| 96 | MouseButtons // Report button events only. |
| 97 | MouseDrag // Report drag events. |
| 98 | MouseMotion // Report motion events (movement). |
| 99 | ) |
| 100 | |
| 101 | // Mouser adds support configuring mouse reporting. |
| 102 | // We also assume that a mouse reporter can report focus events. |
| 103 | type Mouser interface { |
| 104 | SetMouse(MouseReporting) |
| 105 | } |
| 106 | |
| 107 | // Blitter implements a cell-level blit, where a rectangular range of cells is copied from one |
| 108 | // location to another. The source and destination may overlap. The old locations will remain |
| 109 | // unchanged except of course or cells overwritten by the blit. The content will also be clipped |
| 110 | // to the visible dimensions. |
| 111 | type Blitter interface { |
| 112 | Blit(src, dst, dim Coord) |
| 113 | } |
| 114 | |
| 115 | // Clipboard implements a clipboard or copy buffer for copy/paste activity. |
| 116 | // The backend may prevent sending clipboard data by returning an empty string |
| 117 | // for the clipboard. Frequently this is done for security reasons. |
| 118 | type Clipboard interface { |
| 119 | |
| 120 | // SetClipboard sets the contents of the clipboard. |
| 121 | SetClipboard([]byte) |
| 122 | |
| 123 | // GetClipboard gets the contents of the clipboard. |
| 124 | // It will return nil if the operation is not supported. |
| 125 | // An empty clipboard will be []byte{} |
| 126 | GetClipboard() []byte |
| 127 | } |
| 128 | |
| 129 | // AdvancedKeyboard provides raw keyboard events, which gives |
| 130 | // access to key presses, and releases, physical keys, and so forth. |
| 131 | // These keyboards should provide a mapping facility to obtain associated |
| 132 | // Unicode text via a layout, as well. |
| 133 | type AdvancedKeyboard interface { |
| 134 | // IsAdvancedKeyboard returns true if the emulator supports full keyboard |
| 135 | // reporting. This must include key press and release events, and mapping |
| 136 | // of physical keys (thus permitting key disambiguation). |
| 137 | IsAdvancedKeyboard() bool |
| 138 | } |