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