Annotation of nono/vm/console.h, revision 1.1.1.1

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;
                     21: class COMDriverCons;
                     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 *);
                     66:        void Putchar(uint32);
                     67: 
                     68:        bool Render(BitmapRGBX& dst);
                     69: 
                     70:  private:
                     71:        void VSyncCallback(Event&);
                     72:        void ResetTerminal();
                     73:        void ResetState();
                     74:        void PutcharPlain(uint32);
                     75:        void PutcharDebug(uint32);
                     76:        bool PutcharESC(uint32);
                     77:        bool PutcharCSI(uint32);
                     78:        bool PutcharCSIQ(uint32);
                     79: 
                     80:        void Clear();
                     81: 
                     82:        void LocateX(int x);
                     83:        void LocateY(int y);
                     84:        void Locate(int x, int y) {
                     85:                LocateX(x);
                     86:                LocateY(y);
                     87:        }
                     88: 
                     89:        void Ascend();
                     90:        void CR();
                     91:        void LF();
                     92:        void CRLF() {
                     93:                CR();
                     94:                LF();
                     95:        }
                     96:        void ScrollUp();
                     97:        void ScrollDown();
                     98: 
                     99:        // 現在位置に、現在の属性で文字 ch を出力する。出力後カーソルは移動する。
                    100:        void Putc(uint32 ch);
                    101: 
                    102:        // (x, y) の位置に chattr を出力する。カーソルは移動しない。
                    103:        void Setc(int x, int y, uint32 chattr);
                    104: 
                    105:        // 現在の属性を設定する。
                    106:        void SetAttr(uint32 new_attr);
                    107: 
                    108:        std::string Dump() const;
                    109:        std::string Dump(uint32 ch) const;
                    110:        static std::string Dump(const std::vector<uint8>&);
                    111: 
                    112:        // 画面は 1文字分が uint32 の [width * height] の配列。
                    113:        std::vector<uint32> screen {};
                    114:        // カーソル位置。
                    115:        int cur_x {};
                    116:        int cur_y {};
                    117: 
                    118:        // VM スレッド内での行ごとの更新情報。
                    119:        std::vector<bool> dirty {};
                    120:        // レンダラスレッドで未処理の行。
                    121:        // VM スレッドからレンダラスレッドへの連絡用なので mtx で保護する。
                    122:        std::vector<bool> pending {};
                    123:        std::mutex mtx {};
                    124: 
                    125:        // 画面初期化。
                    126:        bool init_screen {};
                    127: 
                    128:        // 現在の状態。
                    129:        char state {};
                    130:        // 現在の属性。
                    131:        uint32 cur_attr {};
                    132: 
                    133:        // 現在の1文字前までのエスケープシーケンス。(現在の文字は ch)
                    134:        std::vector<uint8> seq {};
                    135:        // パラメータ部分。
                    136:        std::string arg {};
                    137: 
                    138:        // カーソル位置保存。
                    139:        int save_curx {};
                    140:        int save_cury {};
                    141: 
                    142:        // スクロール範囲。[0, height) の座標系。
                    143:        int scroll_top {};
                    144:        int scroll_btm {};
                    145: 
                    146:        // デバッグログ。
                    147:        FILE *log {};
                    148:        bool log_newline {};
                    149: 
                    150:        Event vsync_event { this };
                    151: 
                    152:        Renderer *renderer {};
                    153:        COMDriver *comdriver {};
                    154: 
                    155:        static const uint8 decsg8x16[15 * font_height];
                    156:        static const Color palette[3];
                    157: };
                    158: 
                    159: // 入力担当デバイス
                    160: class ConsoleKeyboard : public DumbKeyboard
                    161: {
                    162:        using inherited = DumbKeyboard;
                    163:  public:
                    164:        ConsoleKeyboard();
                    165:        ~ConsoleKeyboard() override;
                    166: 
                    167:        void Attach(COMDriverCons *);
                    168: 
                    169:        void CharInput(uint charcode) override;
                    170: 
                    171:  private:
                    172:        COMDriverCons *comdriver {};
                    173: };
                    174: 
                    175: static inline ConsoleDevice *GetConsoleDevice() {
                    176:        return Object::GetObject<ConsoleDevice>(OBJ_CONSOLE);
                    177: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.