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>
|
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>
|
2022-08-19 20:53:40 +02:00
|
|
|
#include <Kernel/Library/LockWeakPtr.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-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override;
|
|
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const override;
|
|
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const override;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override final;
|
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
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<void> set_termios(termios const&);
|
2021-11-06 15:06:08 -06:00
|
|
|
bool should_generate_signals() const { return (m_termios.c_lflag & ISIG) == ISIG; }
|
|
|
|
|
bool should_flush_on_signal() const { return (m_termios.c_lflag & NOFLSH) != NOFLSH; }
|
|
|
|
|
bool should_echo_input() const { return (m_termios.c_lflag & ECHO) == ECHO; }
|
|
|
|
|
bool in_canonical_mode() const { return (m_termios.c_lflag & ICANON) == 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
|
|
|
|
2022-03-26 09:06:30 +03:00
|
|
|
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_name() const = 0;
|
|
|
|
|
|
2022-04-28 01:17:32 -06:00
|
|
|
virtual bool is_graphical() const { return false; }
|
|
|
|
|
virtual void set_graphical(bool) { }
|
|
|
|
|
|
2018-10-30 13:59:29 +01:00
|
|
|
protected:
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> on_tty_write(UserOrKernelBuffer const&, 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
|
|
|
|
2021-12-23 20:08:18 +02:00
|
|
|
TTY(MajorNumber major, MinorNumber 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];
|
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
LockWeakPtr<Process> m_original_process_parent;
|
|
|
|
|
LockWeakPtr<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
|
|
|
|
|
|
|
|
}
|