2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-10-20 19:12:00 +13:00
|
|
|
#include <AK/CircularDeque.h>
|
2020-08-06 11:17:53 +02:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-04-03 12:36:40 +02:00
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2020-01-28 20:42:27 +01:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2020-08-15 23:43:19 +04:30
|
|
|
#include <Kernel/ProcessGroup.h>
|
2019-01-23 05:13:17 +01:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-10-30 13:59:29 +01:00
|
|
|
|
2021-05-23 23:16:00 +02:00
|
|
|
#define TTY_BUFFER_SIZE 1024
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
class TTY : public CharacterDevice {
|
|
|
|
public:
|
|
|
|
virtual ~TTY() override;
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
|
|
|
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
|
2020-04-10 19:44:42 +10:00
|
|
|
virtual bool can_read(const FileDescription&, size_t) const override;
|
|
|
|
virtual bool can_write(const FileDescription&, size_t) const override;
|
2021-07-26 03:47:25 -07:00
|
|
|
virtual KResult ioctl(FileDescription&, unsigned request, Userspace<void*> arg) override final;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual String absolute_path(const FileDescription&) const override { return tty_name(); }
|
2018-10-30 13:59:29 +01:00
|
|
|
|
2021-05-12 22:47:06 +02:00
|
|
|
virtual String const& tty_name() const = 0;
|
2018-10-30 15:33:37 +01:00
|
|
|
|
2018-11-29 03:45:23 +01:00
|
|
|
unsigned short rows() const { return m_rows; }
|
|
|
|
unsigned short columns() const { return m_columns; }
|
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
ProcessGroupID pgid() const
|
|
|
|
{
|
|
|
|
if (auto pg = m_pg.strong_ref())
|
|
|
|
return pg->pgid();
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-02 13:14:25 +01:00
|
|
|
|
2021-07-26 03:47:25 -07:00
|
|
|
KResult set_termios(const termios&);
|
2018-11-11 15:36:40 +01:00
|
|
|
bool should_generate_signals() const { return m_termios.c_lflag & ISIG; }
|
2019-11-02 03:24:14 +13:00
|
|
|
bool should_flush_on_signal() const { return !(m_termios.c_lflag & NOFLSH); }
|
2018-11-11 15:36:40 +01:00
|
|
|
bool should_echo_input() const { return m_termios.c_lflag & ECHO; }
|
2018-11-16 18:12:22 +01:00
|
|
|
bool in_canonical_mode() const { return m_termios.c_lflag & ICANON; }
|
2018-11-11 15:36:40 +01:00
|
|
|
|
2018-12-07 01:19:02 +01:00
|
|
|
void set_default_termios();
|
2019-02-05 12:55:19 +01:00
|
|
|
void hang_up();
|
2018-12-07 01:19:02 +01:00
|
|
|
|
2020-12-25 19:01:19 +02:00
|
|
|
// ^Device
|
|
|
|
virtual mode_t required_mode() const override { return 0620; }
|
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
protected:
|
2021-06-16 15:20:35 +02:00
|
|
|
virtual KResultOr<size_t> on_tty_write(const UserOrKernelBuffer&, size_t) = 0;
|
2018-11-29 03:45:23 +01:00
|
|
|
void set_size(unsigned short columns, unsigned short rows);
|
2018-10-30 22:03:02 +01:00
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
TTY(unsigned major, unsigned minor);
|
2020-11-29 16:05:27 -07:00
|
|
|
void emit(u8, bool do_evaluate_block_conditions = false);
|
2021-06-02 15:36:31 +02:00
|
|
|
void echo_with_processing(u8);
|
2019-10-20 19:12:00 +13:00
|
|
|
|
|
|
|
bool can_do_backspace() const;
|
|
|
|
void do_backspace();
|
|
|
|
void erase_word();
|
2020-03-26 08:15:29 +01:00
|
|
|
void erase_character();
|
2019-10-20 19:12:00 +13:00
|
|
|
void kill_line();
|
2019-11-02 03:24:14 +13:00
|
|
|
void flush_input();
|
2019-10-20 19:12:00 +13:00
|
|
|
|
|
|
|
bool is_eol(u8) const;
|
|
|
|
bool is_eof(u8) const;
|
|
|
|
bool is_kill(u8) const;
|
|
|
|
bool is_erase(u8) const;
|
|
|
|
bool is_werase(u8) const;
|
2018-10-30 15:33:37 +01:00
|
|
|
|
2019-02-05 12:55:19 +01:00
|
|
|
void generate_signal(int signal);
|
|
|
|
|
2019-10-20 19:12:00 +13:00
|
|
|
int m_available_lines { 0 };
|
|
|
|
|
2018-11-16 20:18:58 +01:00
|
|
|
private:
|
|
|
|
// ^CharacterDevice
|
2018-12-03 00:39:25 +01:00
|
|
|
virtual bool is_tty() const final override { return true; }
|
2021-06-02 15:36:31 +02:00
|
|
|
|
|
|
|
virtual void echo(u8) = 0;
|
2021-06-05 12:39:02 +02:00
|
|
|
|
|
|
|
template<typename Functor>
|
|
|
|
void process_output(u8, Functor put_char);
|
2018-10-30 15:33:37 +01:00
|
|
|
|
2021-05-23 23:16:00 +02:00
|
|
|
CircularDeque<u8, TTY_BUFFER_SIZE> m_input_buffer;
|
|
|
|
// FIXME: use something like AK::Bitmap but which takes a size template parameter
|
|
|
|
u8 m_special_character_bitmask[TTY_BUFFER_SIZE / 8];
|
|
|
|
|
2020-08-15 23:43:19 +04:30
|
|
|
WeakPtr<Process> m_original_process_parent;
|
|
|
|
WeakPtr<ProcessGroup> m_pg;
|
2019-01-23 06:53:01 +01:00
|
|
|
termios m_termios;
|
2018-11-29 03:45:23 +01:00
|
|
|
unsigned short m_rows { 0 };
|
|
|
|
unsigned short m_columns { 0 };
|
2018-10-30 13:59:29 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|