1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <stddef.h>
8#include <stdint.h>
9
10#define TC_MAX_ARG 16
11#define TC_MAX_ARG_LENGTH 8 // matches vc title length
12
13typedef struct textcon textcon_t;
14typedef uint16_t vc_char_t;
15
16inline vc_char_t vc_char_make(uint8_t ch, uint8_t fg_color, uint8_t bg_color) {
17    return static_cast<vc_char_t>(ch | ((fg_color & 0xf) << 8)
18                                  | ((bg_color & 0xf) << 12));
19}
20
21inline uint8_t vc_char_get_char(vc_char_t ch) {
22    return static_cast<uint8_t>(ch & 0xff);
23}
24
25inline uint8_t vc_char_get_fg_color(vc_char_t ch) {
26    return static_cast<uint8_t>((ch >> 8) & 0xf);
27}
28
29inline uint8_t vc_char_get_bg_color(vc_char_t ch) {
30    return static_cast<uint8_t>((ch >> 12) & 0xf);
31}
32
33typedef enum textcon_param {
34    TC_INVALID,
35    TC_SET_TITLE,
36    TC_SHOW_CURSOR,
37    TC_HIDE_CURSOR,
38} textcon_param_t;
39
40struct textcon {
41    void (*putc)(textcon_t* tc, uint8_t c);
42
43    // backing data
44    vc_char_t* data;
45
46    // dimensions of display
47    int w;
48    int h;
49
50    // cursor position
51    int x;  // 0 < x <= w; cursor may be one position beyond right edge
52    int y;  // 0 < y < h
53
54    // callbacks to update visible display
55    void (*invalidate)(void* cookie, int x, int y, int w, int h);
56    void (*movecursor)(void* cookie, int x, int y);
57    void (*push_scrollback_line)(void* cookie, int y);
58    void (*copy_lines)(void* cookie, int y_dest, int y_src, int count);
59    void (*setparam)(void* cookie, int param, uint8_t* arg, size_t arglen);
60    void* cookie;
61
62    // scrolling region
63    int scroll_y0;
64    int scroll_y1;
65
66    // saved cursor position
67    int save_x;
68    int save_y;
69
70    uint8_t fg;
71    uint8_t bg;
72
73    // Escape sequence parameter parsing
74    // Numeric arguments
75    int num;  // Argument currently being read
76    int argn_count;  // Number of arguments read into argn[]
77    int argn[TC_MAX_ARG];
78    // String argument (e.g. for console title)
79    int argstr_size;  // Number of characters read into argstr[]
80    uint8_t argstr[TC_MAX_ARG_LENGTH + 1];
81};
82
83void tc_init(textcon_t* tc, int w, int h, vc_char_t* data,
84             uint8_t fg, uint8_t bg, int cursor_x, int cursor_y);
85
86void tc_copy_lines(textcon_t* tc, int y_dest, int y_src, int line_count);
87
88static inline void tc_putc(textcon_t* tc, uint8_t c) {
89    tc->putc(tc, c);
90}
91
92void tc_seth(textcon_t* tc, int h);
93