62 0x000000, 0xaa0000, 0x00aa00, 0xaa5500, 0x0000aa, 0xaa00aa, 0x00aaaa, 0xaaaaaa,
63 0x555555, 0xff5555, 0x55ff55, 0xffff55, 0x5555ff, 0xff55ff, 0x55ffff, 0xffffff,
80 return v << 16 | v << 8 | v;
83 static const uint8_t levels[] = {0, 95, 135, 175, 215, 255};
86 return levels[color / 36] << 16 | levels[color / 6 % 6] << 8 | levels[color % 6];
96 int r = rgb >> 16, g = (rgb >> 8) & 0xff, b = rgb & 0xff;
97 int best = 0, best_dist = 0x7fffffff;
99 for (
int i = 0; i < 16; ++i) {
103 int dist = dr * dr + dg * dg + db * db;
105 if (dist < best_dist) {
125 static const uint8_t vga[8] = {0, 4, 2, 6, 1, 5, 3, 7};
156 for (
int row = 0; row <
vt->
rows; ++row) {
157 for (
int col = 0; col <
vt->
cols; ++col) {
158 int i = row *
vt->
cols + col;
190 return ringbuf_write(
vt->
in, size, buf);
199 while (ringbuf_read(
vt->
in, 1, &c))
290 vt->
mode = (
struct vt_mode) {.mode = VT_AUTO};
300 if (
proc && signal > 0 && signal < NSIG)
328 if (
vt->
mode.mode == VT_PROCESS)
349 if (
fg->
mode.mode == VT_PROCESS) {
403 for (
size_t i = 0; i < len; ++i) {
451 return ringbuf_read_iobuf(
vt->
in, io->
resid, io);
499 return sizeof(
struct vt_mode);
501 return sizeof(
struct vt_stat);
503 return sizeof(
struct kbentry);
518 *(
char *) argp = KB_101;
526 if (arg != KD_TEXT && arg != KD_GRAPHICS)
534 if (arg == KD_GRAPHICS) {
550 struct kbentry *kbe = argp;
551 kbe->kb_value =
vt_keymap(kbe->kb_table, kbe->kb_index);
562 if (arg < K_RAW || arg > K_OFF)
570 if (arg != K_XLATE && arg != K_UNICODE)
579 for (
int i = 1; i <
VT_COUNT &&
id < 0; ++i)
589 *(
struct vt_mode *) argp =
vt->
mode;
593 struct vt_mode mode = *(
struct vt_mode *) argp;
595 if (mode.mode != VT_AUTO && mode.mode != VT_PROCESS)
603 if (mode.mode == VT_AUTO &&
vt ==
fg &&
pending >= 0)
610 struct vt_stat *stat = argp;
612 stat->v_active =
fg ?
fg->
id : 0;
618 stat->v_state |= 1 << (i + 1);
627 if (arg == VT_ACKACQ)
651 while (arg &&
fg != &
vts[arg - 1]) {
674 if ((request >> 8) == 0x4B || (request >> 8) == 0x56) {
689 if (copyin(&arg, argp, size))
693 return err ? err : copyout(argp, &arg, size);
714 file->vnode->read_queue = &
vt->read_queue;
727 file->vnode->read_queue = &
vt->read_queue;
789 .can_write = __vfs_can_always,
803 int attrs = dd->
minor & 128,
id = dd->
minor & 127;
815 size_t size = io->
resid, done = 0;
821 for (
size_t i = offset; i < len && done < size; ) {
824 for (; i < len && done + n < size && n <
sizeof(buf); ++i, ++n) {
825 if (attrs && i < 4) {
839 return done ? (
ssize_t) done : r;
859 .can_read = __vfs_can_always,
860 .can_write = __vfs_can_never,
861 .eof = __vfs_eof_always,
871 size_t len = ringbuf_available(
kmsg);
873 for (
size_t off = 0; off < len; ) {
874 size_t n = ringbuf_read_noconsume(
kmsg, off,
MIN(
sizeof(
buf), len - off),
buf);
902 for (
int i = 0; i <
VT_COUNT; ++i) {
int earlycon_on_screen(void)
Says whether the early console writes on the screen, rather than on a serial port.
int vt_fb_probe(void)
Sets up the virtual terminals on the framebuffer, if there is one the terminals can draw on,...
struct vt_display vt_fb_display
int vt_vga_probe(void)
Says whether VGA text mode can be used.
struct vt_display vt_vga_display
static uint64_t cells(const void *p, int cells)
Reads cells big-endian 32-bit cells at p as one number.
ssize_t tty_slave_write(struct tty *tty, size_t size, void *_buf)
Takes what programs write to the terminal, and sends it to the device through output processing (OPOS...
int tty_ioctl(struct tty *tty, int request, void *argp)
Handles a terminal's ioctl()s: its modes (TCGETS, TCSETS, ...), flushing, its session and foreground ...
ssize_t tty_write_iobuf(struct tty *tty, struct iobuf *io, ttyio write)
Writes to a terminal from an iobuf (user memory), to either side: a piece at a time through a kernel ...
int tty_new(struct proc *proc, size_t buf_size, ttyio master, ttyio slave, void *p, struct tty **ref)
Makes a new terminal, with the default modes (canonical, echo, signals, ICRNL, IXON,...
ssize_t tty_master_write(struct tty *tty, size_t size, void *buf)
Takes input from the terminal's device (the keyboard, a UART, or a pseudo-terminal's master),...
int tty_can_read(struct tty *tty, size_t buffered)
Says whether the terminal is readable, for select() and poll(): there is input, or an end-of-file.
int tty_eof(struct tty *tty)
Says whether a read that found nothing to return should return 0 at once: at an end-of-file (VEOF at ...
int tty_background(struct tty *tty, int sig)
Stops a background process that reads the terminal, writes it or changes it: if the current process i...
uint64_t tty_read_timeout(struct tty *tty)
Says how long a read waits for input: VTIME tenths of a second with VMIN 0 in non-canonical mode,...
void tty_open(struct tty *tty, struct file *file)
Handles a terminal device being opened: it becomes the controlling terminal of a session leader that ...
ssize_t iobuf_out(struct iobuf *io, const void *src, size_t n)
Copies bytes from the kernel into the iobuf's buffer, and moves past them.
void kdev_chrdev_register(devid_t major, struct dev *dev)
Registers a driver as the character device of a major number.
int vt_keymap(int table, int keycode)
Says what a key is, for KDGKBENT: programs that read key codes (K_MEDIUMRAW) use it to know what each...
void vt_keyboard_reset(void)
Forgets a prefix or a sequence that a change of mode cut in the middle.
#define MALLOC_DEFINE(type, _name, _desc)
void * kmalloc(size_t size, struct malloc_type *type, int flags)
Allocates memory for one of the kernel's structures.
int memcmp(const void *a, const void *b, size_t n)
Compares n bytes.
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
off_t posix_file_lseek(struct file *file, off_t offset, int whence)
Moves an open file's offset: to offset (SEEK_SET), by offset from where it is (SEEK_CUR),...
#define MODULE_INIT(name, i, f)
int posix_file_open(struct file *file)
Finishes opening a file: with O_TRUNC, a regular file is truncated to 0 bytes.
int posix_file_close(struct file *file)
Closes an open file by dropping its reference to its vnode.
int posix_file_ioctl(struct file *file, int request, void *argp)
Passes an ioctl() request on to the file's vnode (a device's, or its filesystem's).
ssize_t posix_file_read(struct file *file, struct iobuf *io)
Reads from an open file at its offset, and moves the offset past what was read.
ssize_t posix_file_write(struct file *file, struct iobuf *io)
Writes to an open file at its offset, and moves the offset past what was written.
void earlycon_disable()
Stops sending the kernel's messages to the early console, once a real console has taken over.
void printk_console(void(*write)(const char *s, size_t len))
Registers the console that the kernel's messages are written to, besides the message buffer.
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
struct proc * proc_pid_find(pid_t pid)
Finds a process by its ID.
int signal_proc_send(struct proc *proc, int signal)
Sends a signal to a process.
unsigned long long int uint64_t
A device driver: its operations, any of which may be NULL.
Which device: its kind (S_IFCHR or S_IFBLK) and numbers.
An open file (POSIX's "open file description", fs/file.c).
pid_t pid
the process's ID
int running
whether it's running
A terminal, and its line discipline's state.
struct session * session
the session it is the controlling terminal of, or NULL
void * p
the device's own data
struct vnode * vnode
the device's vnode, which /dev/tty opens in its place
void(*) input_flush(struct tty *tty)
discards the input that readers haven't read yet
A display that virtual terminals are shown on.
void(*) cursor(int row, int col, int visible)
moves a hardware cursor; NULL if the cursor is drawn (VT_CURSOR)
void(*) draw(int row, int col, struct vt_cell cell)
draws a cell
A virtual terminal: its screen, its emulator's state, and its tty.
struct waitq read_queue write_queue
void vt_term_write(struct vt *vt, const char *buf, size_t size)
Takes what programs write to a terminal, and changes its screen.
int vt_term_init(struct vt *vt, int rows, int cols)
Sets a terminal's screens up, the main one and the alternate one, both cleared, and resets it.
static ssize_t vt_write(struct devid *dd, off_t offset, struct iobuf *io)
Writes to a terminal's screen.
static struct vt * vt_file(struct file *file)
Returns the terminal of an open file.
static struct vt vts[VT_COUNT]
static void vt_input_flush(struct tty *tty)
Drops the input waiting to be read (tcflush()).
static struct waitq active_queue
static size_t vt_ioctl_argsize(int request)
Returns the size of the user memory that a KD* or VT_* request reads or writes, or 0 if its argument ...
static void refresh(struct vt *vt, int all)
Draws the cells of the foreground terminal that changed, or all of them with all, and moves the curso...
static ssize_t vt_tty_input(struct tty *tty, size_t size, void *buf)
Takes the terminal's input, after line editing, for its readers.
static void vt_signal(struct vt *vt, int signal)
Sends a signal to the process that put a terminal in VT_PROCESS mode.
static ssize_t vt_tty_output(struct tty *tty, size_t size, void *buf)
Takes the terminal's output: it goes through the emulator to the screen.
static int proc_alive(pid_t pid)
Returns whether a process is still running.
static uint8_t color16(uint8_t color)
Returns the closest of the 16 colours to one of the 256.
static int vt_ioctl(struct devid *dd, int request, void *argp)
Handles a terminal's ioctl()s: the console's own (KD*, VT_*), through a kernel copy of their argument...
static const uint32_t ansi_colors[16]
static struct vt * vt_get(struct devid *dd)
Returns the terminal of a device number: /dev/console is the first one, /dev/tty0 the one in the fore...
static void vt_input_to(struct vt *vt, const char *buf, size_t size)
Gives input to a terminal: key presses, or replies to requests.
static int vt_kd_ioctl(struct vt *vt, int request, void *argp)
Handles the Linux console's KD* and VT_* requests.
static struct vt_display * display
void vt_input(const char *buf, size_t size)
Gives what keys sent to the foreground terminal, as input.
static void vt_reply(struct vt *vt)
Gives the replies to requests that programs wrote (DSR, DA) to the terminal, as input.
static ssize_t vt_read(struct devid *dd, off_t offset, struct iobuf *io)
Reads a terminal's input.
static int vt_setup(struct vt *vt)
Sets a terminal up the first time it is used: its screen, its input ring and its terminal.
uint8_t vt_vga_attr(struct vt_cell cell)
Returns the VGA text mode attribute of a cell (its colours, in VGA's order), for the VGA display and ...
static int vt_file_close(struct file *file)
Closes a terminal.
static uint64_t vt_read_timeout(struct file *file)
Returns how long a read may wait, from the terminal's mode (VTIME), or 0 for no limit.
static int vt_probe(void)
Sets the virtual terminals up at boot, on the framebuffer or in VGA text mode, and shows the first on...
static int vt_can_read(struct file *file, size_t size)
Returns whether a read wouldn't have to wait.
static int vt_file_open(struct file *file)
Opens a terminal, setting it up the first time.
static void vt_kmsg_replay(void)
Shows the kernel messages printed so far, before the terminals were set up.
uint32_t vt_palette(uint8_t color)
Returns one of the 256 colours of xterm's palette: the 16 colours, a 6 x 6 x 6 cube,...
static int vt_file_eof(struct file *file)
Returns whether a read that found no data is at the end of the file.
static void vt_switch_now(int id)
Switches to terminal id (an index) at once: it is set up if needed, drawn, and its VT_PROCESS process...
static struct vt_cell * shown
static void vt_kmsg(const char *s, size_t len)
Writes kernel messages to the foreground terminal's screen.
struct vt * vt_foreground(void)
Returns the terminal in the foreground.
static ssize_t vcs_read(struct devid *dd, off_t offset, struct iobuf *io)
Reads a terminal's screen.
static int vt_in_use(struct vt *vt)
Returns whether a terminal is in use, so that VT_OPENQRY doesn't give it.
static void vt_check(struct vt *vt)
Undoes the modes that a process which is gone had set on a terminal (graphics mode,...
void vt_switch(int id)
Switches to another terminal, for Alt+Fn and VT_ACTIVATE.
void vt_keypress(void)
Undoes, at a key press, the modes that a process which is gone had set on the foreground terminal: ot...
void waitq_wakeup(struct waitq *waitq)
Wakes up every thread sleeping on a wait queue, and every thread polling it.
int waitq_sleep(struct waitq *waitq)
Puts the current thread to sleep on a wait queue until it is woken up.