package server import "sync" // terminalViewers is the set of client connections drawing one terminal (a // machine terminal or a chat's terminal command), most recently active // first. The first is the active client: the terminal's size is the size it // draws at, and only its emulator's automatic answers to terminal queries // (cursor position, device attributes) reach the process. Every client's // emulator answers each query it draws, so without this a query got one // answer per client, and the size was whichever client resized last, even // after it left. // // A client becomes active by typing, by a resize marked activate (the // client focused the terminal), or by being the first to attach or resize. // When the active client leaves, the next most recent one becomes active // and its last size is applied. The zero value is ready to use. type terminalViewers struct { mu sync.Mutex list []terminalViewer } type terminalViewer struct { client string cols, rows int } // terminalSize is a size to apply to the terminal, when ok. type terminalSize struct { cols, rows int ok bool } func (v *terminalViewers) indexLocked(client string) int { for i, viewer := range v.list { if viewer.client == client { return i } } return -1 } // frontLocked moves client to the front, adding it when absent, and // returns its entry. func (v *terminalViewers) frontLocked(client string) *terminalViewer { viewer := terminalViewer{client: client} if i := v.indexLocked(client); i >= 0 { viewer = v.list[i] v.list = append(v.list[:i], v.list[i+1:]...) } v.list = append([]terminalViewer{viewer}, v.list...) return &v.list[0] } // attach records client as drawing the terminal (it opened or read it): the // active one when no client is. func (v *terminalViewers) attach(client string) { if client == "" { return } v.mu.Lock() defer v.mu.Unlock() if v.indexLocked(client) < 0 { v.list = append(v.list, terminalViewer{client: client}) } } // resize records the size client draws the terminal at and reports the // size to apply: client's, when it is (or, with activate, becomes) the // active client, or when the active client has not reported a size yet. // An unknown client (empty) always applies its size, as before clients // were told apart. func (v *terminalViewers) resize(client string, cols, rows int, activate bool) terminalSize { if client == "" { return terminalSize{cols, rows, true} } v.mu.Lock() defer v.mu.Unlock() i := v.indexLocked(client) switch { case activate || len(v.list) == 0 || i == 0: viewer := v.frontLocked(client) viewer.cols, viewer.rows = cols, rows return terminalSize{cols, rows, true} case i < 0: v.list = append(v.list, terminalViewer{client: client, cols: cols, rows: rows}) default: v.list[i].cols, v.list[i].rows = cols, rows } front := v.list[0] return terminalSize{cols, rows, front.cols == 0 || front.rows == 0} } // typedAt is typed from a client drawing the terminal at cols x rows // (zero when it did not say): it becomes the active client at that size. func (v *terminalViewers) typedAt(client string, cols, rows int) terminalSize { if client != "" && cols > 0 && rows > 0 { return v.resize(client, cols, rows, true) } return v.typed(client) } // typed makes client, which typed into the terminal, the active client and // reports its last size when that changes who is active. func (v *terminalViewers) typed(client string) terminalSize { if client == "" { return terminalSize{} } v.mu.Lock() defer v.mu.Unlock() if len(v.list) > 0 && v.list[0].client == client { return terminalSize{} } viewer := v.frontLocked(client) return terminalSize{viewer.cols, viewer.rows, viewer.cols > 0 && viewer.rows > 0} } // answers reports whether client's emulator's automatic answers reach the // process: only the active client's do, or anyone's while no client is // known. func (v *terminalViewers) answers(client string) bool { if client == "" { return true } v.mu.Lock() defer v.mu.Unlock() return len(v.list) == 0 || v.list[0].client == client } // leave forgets client (its connection closed) and, when it was the active // client, reports the size of the next one, which becomes active. func (v *terminalViewers) leave(client string) terminalSize { v.mu.Lock() defer v.mu.Unlock() i := v.indexLocked(client) if i < 0 { return terminalSize{} } v.list = append(v.list[:i], v.list[i+1:]...) if i != 0 || len(v.list) == 0 { return terminalSize{} } next := v.list[0] return terminalSize{next.cols, next.rows, next.cols > 0 && next.rows > 0} } // active is the active client, or empty (tests). func (v *terminalViewers) active() string { v.mu.Lock() defer v.mu.Unlock() if len(v.list) == 0 { return "" } return v.list[0].client }