Serenity Operating System
at hosted 102 lines 3.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/CircularDeque.h> 30#include <Kernel/Devices/CharacterDevice.h> 31#include <Kernel/DoubleBuffer.h> 32#include <Kernel/UnixTypes.h> 33 34namespace Kernel { 35 36class Process; 37 38class TTY : public CharacterDevice { 39public: 40 virtual ~TTY() override; 41 42 virtual ssize_t read(FileDescription&, u8*, ssize_t) override; 43 virtual ssize_t write(FileDescription&, const u8*, ssize_t) override; 44 virtual bool can_read(const FileDescription&) const override; 45 virtual bool can_write(const FileDescription&) const override; 46 virtual int ioctl(FileDescription&, unsigned request, unsigned arg) override final; 47 virtual String absolute_path(const FileDescription&) const override { return tty_name(); } 48 49 virtual StringView tty_name() const = 0; 50 51 unsigned short rows() const { return m_rows; } 52 unsigned short columns() const { return m_columns; } 53 54 void set_pgid(pid_t pgid) { m_pgid = pgid; } 55 pid_t pgid() const { return m_pgid; } 56 57 void set_termios(const termios&); 58 bool should_generate_signals() const { return m_termios.c_lflag & ISIG; } 59 bool should_flush_on_signal() const { return !(m_termios.c_lflag & NOFLSH); } 60 bool should_echo_input() const { return m_termios.c_lflag & ECHO; } 61 bool in_canonical_mode() const { return m_termios.c_lflag & ICANON; } 62 63 void set_default_termios(); 64 void hang_up(); 65 66protected: 67 virtual ssize_t on_tty_write(const u8*, ssize_t) = 0; 68 void set_size(unsigned short columns, unsigned short rows); 69 70 TTY(unsigned major, unsigned minor); 71 void emit(u8); 72 virtual void echo(u8) = 0; 73 74 bool can_do_backspace() const; 75 void do_backspace(); 76 void erase_word(); 77 void erase_character(); 78 void kill_line(); 79 void flush_input(); 80 81 bool is_eol(u8) const; 82 bool is_eof(u8) const; 83 bool is_kill(u8) const; 84 bool is_erase(u8) const; 85 bool is_werase(u8) const; 86 87 void generate_signal(int signal); 88 89 int m_available_lines { 0 }; 90 91private: 92 // ^CharacterDevice 93 virtual bool is_tty() const final override { return true; } 94 95 CircularDeque<u8, 1024> m_input_buffer; 96 pid_t m_pgid { 0 }; 97 termios m_termios; 98 unsigned short m_rows { 0 }; 99 unsigned short m_columns { 0 }; 100}; 101 102}