|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2024 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // メイン画面をコンソールにする
9: //
10:
11: #pragma once
12:
13: #include "device.h"
14: #include "event.h"
15: #include "keyboard.h"
16: #include "renderer.h"
17: #include <vector>
18:
19: class BitmapRGBX;
20: class COMDriver;
1.1.1.2 ! root 21: class COMDriverConsole;
1.1 root 22:
23: class ConsoleDevice : public Device
24: {
25: using inherited = Device;
26:
27: static const uint width = 80;
28: static const uint height = 30;
29: static const uint font_width = 8;
30: static const uint font_height = 16;
31:
32: // 1文字 32ビットのうち下位8ビットが文字コード、上位24ビットが各種属性。
33: //
34: //
35: // 3 2 1
36: // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
37: // +---------------+---------------+---------------+---------------+
38: // | bgcolor | fgcolor |R| |D|O|S|U|I|B| 文字コード |
39: // +---------------+---------------+---------------+---------------+
40: //
41: // bgcolor, fgcolor はカラーパレット番号。
42:
43: static const uint32 ATTR_BOLD = 0x0000'0100; // B: ボールド
44: static const uint32 ATTR_ITALIC = 0x0000'0200; // I: イタリック
45: static const uint32 ATTR_UNDERLINE = 0x0000'0400; // U: 下線
46: static const uint32 ATTR_STRIKE = 0x0000'0800; // S: 打ち消し線
47: static const uint32 ATTR_OVERLINE = 0x0000'1000; // O: 上線
48: static const uint32 ATTR_DECSG = 0x0000'2000; // D: DEC 特殊文字
49: static const uint32 ATTR_REVERSE = 0x0000'8000; // R: 反転(暫定)
50:
51: static const uint32 ATTR_LINEMASK = (ATTR_UNDERLINE |
52: ATTR_STRIKE |
53: ATTR_OVERLINE);
54:
55: // フチ [pixel]
56: static const uint Padding = ConsoleRenderer::Padding;
57:
58: public:
59: ConsoleDevice();
60: ~ConsoleDevice() override;
61:
62: bool Init() override;
63: void ResetHard(bool poweron) override;
64:
65: void Attach(COMDriver *);
1.1.1.2 ! root 66: void Detach() { Attach(NULL); }
! 67:
1.1 root 68: void Putchar(uint32);
69:
70: bool Render(BitmapRGBX& dst);
71:
72: private:
73: void VSyncCallback(Event&);
1.1.1.2 ! root 74: void SetPalette(bool console_is_active);
1.1 root 75: void ResetTerminal();
76: void ResetState();
77: void PutcharPlain(uint32);
78: void PutcharDebug(uint32);
79: bool PutcharESC(uint32);
80: bool PutcharCSI(uint32);
81: bool PutcharCSIQ(uint32);
82:
83: void Clear();
84:
85: void LocateX(int x);
86: void LocateY(int y);
87: void Locate(int x, int y) {
88: LocateX(x);
89: LocateY(y);
90: }
91:
92: void Ascend();
93: void CR();
94: void LF();
95: void CRLF() {
96: CR();
97: LF();
98: }
99: void ScrollUp();
100: void ScrollDown();
101:
102: // 現在位置に、現在の属性で文字 ch を出力する。出力後カーソルは移動する。
103: void Putc(uint32 ch);
104:
105: // (x, y) の位置に chattr を出力する。カーソルは移動しない。
106: void Setc(int x, int y, uint32 chattr);
107:
108: // 現在の属性を設定する。
109: void SetAttr(uint32 new_attr);
110:
111: std::string Dump() const;
112: std::string Dump(uint32 ch) const;
113: static std::string Dump(const std::vector<uint8>&);
114:
115: // 画面は 1文字分が uint32 の [width * height] の配列。
116: std::vector<uint32> screen {};
117: // カーソル位置。
118: int cur_x {};
119: int cur_y {};
120:
121: // VM スレッド内での行ごとの更新情報。
122: std::vector<bool> dirty {};
123: // レンダラスレッドで未処理の行。
124: // VM スレッドからレンダラスレッドへの連絡用なので mtx で保護する。
125: std::vector<bool> pending {};
126: std::mutex mtx {};
127:
128: // 画面初期化。
129: bool init_screen {};
130:
131: // 現在の状態。
132: char state {};
133: // 現在の属性。
134: uint32 cur_attr {};
135:
1.1.1.2 ! root 136: // パレット。
! 137: // [0..1] だと通常色、[1..2] だと反転色になる。
! 138: Color palette[3] {};
! 139:
1.1 root 140: // 現在の1文字前までのエスケープシーケンス。(現在の文字は ch)
141: std::vector<uint8> seq {};
142: // パラメータ部分。
143: std::string arg {};
144:
145: // カーソル位置保存。
146: int save_curx {};
147: int save_cury {};
148:
149: // スクロール範囲。[0, height) の座標系。
150: int scroll_top {};
151: int scroll_btm {};
152:
153: // デバッグログ。
154: FILE *log {};
155: bool log_newline {};
156:
157: Event vsync_event { this };
158:
159: Renderer *renderer {};
160: COMDriver *comdriver {};
161:
162: static const uint8 decsg8x16[15 * font_height];
163: };
164:
165: // 入力担当デバイス
166: class ConsoleKeyboard : public DumbKeyboard
167: {
168: using inherited = DumbKeyboard;
169: public:
170: ConsoleKeyboard();
171: ~ConsoleKeyboard() override;
172:
1.1.1.2 ! root 173: void Attach(COMDriverConsole *);
! 174: void Detach() { Attach(NULL); }
1.1 root 175:
176: void CharInput(uint charcode) override;
177:
178: private:
1.1.1.2 ! root 179: COMDriverConsole *comdriver {};
1.1 root 180: };
181:
182: static inline ConsoleDevice *GetConsoleDevice() {
183: return Object::GetObject<ConsoleDevice>(OBJ_CONSOLE);
184: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.