Annotation of nono/host/comdriver_cons.cpp, revision 1.1.1.3

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: #include "comdriver_cons.h"
                     12: #include "console.h"
                     13: #include "hostcom.h"
                     14: 
                     15: // コンストラクタ
1.1.1.2   root       16: COMDriverConsole::COMDriverConsole(HostDevice *hostdev_)
                     17:        : inherited(hostdev_, "console")
1.1       root       18: {
                     19: }
                     20: 
                     21: // デストラクタ
1.1.1.2   root       22: COMDriverConsole::~COMDriverConsole()
1.1       root       23: {
1.1.1.2   root       24:        if (rxpipe.Valid()) {
                     25:                hostdev->DelOuter(rxpipe);
                     26:        }
                     27: 
                     28:        if (console) {
                     29:                console->Detach();
1.1.1.3 ! root       30:                console = NULL;
1.1.1.2   root       31:        }
                     32:        auto keyboard = gMainApp.FindObject<ConsoleKeyboard>(OBJ_KEYBOARD);
                     33:        if (keyboard) {
                     34:                keyboard->Detach();
                     35:        }
1.1       root       36: }
                     37: 
                     38: // ドライバ初期化
                     39: bool
1.1.1.2   root       40: COMDriverConsole::InitDriver(bool startup)
1.1       root       41: {
                     42:        int fds[2];
                     43:        if (pipe(fds) < 0) {
1.1.1.2   root       44:                errmsg = string_format("pipe: %s", strerror(errno));
                     45:                putmsg(1, "%s", errmsg.c_str());
1.1       root       46:                return false;
                     47:        }
                     48:        rxpipe = fds[0];
                     49:        txpipe = fds[1];
                     50: 
                     51:        if (hostdev->AddOuter(rxpipe) < 0) {
1.1.1.2   root       52:                errmsg = string_format("AddOuter: %s", strerror(errno));
                     53:                putmsg(1, "%s", errmsg.c_str());
1.1       root       54:                return false;
                     55:        }
                     56: 
1.1.1.2   root       57:        console = GetConsoleDevice();
                     58:        console->Attach(this);
                     59: 
                     60:        auto keyboard = gMainApp.GetObject<ConsoleKeyboard>(OBJ_KEYBOARD);
                     61:        keyboard->Attach(this);
                     62: 
1.1       root       63:        return true;
                     64: }
                     65: 
1.1.1.3 ! root       66: // (ConsoleDevice 側からの) デタッチ。
        !            67: // この場合 ConsoleDevice.Detach() は向こうで処理済み。
        !            68: void
        !            69: COMDriverConsole::DetachConsole()
        !            70: {
        !            71:        console = NULL;
        !            72: }
        !            73: 
1.1       root       74: // 外部への1バイト書き出し
                     75: void
1.1.1.2   root       76: COMDriverConsole::Write(uint32 ch)
1.1       root       77: {
                     78:        console->Putchar(ch);
                     79: }
                     80: 
                     81: // 外部からの1バイト読み込み。
                     82: // HostCOM スレッドから呼ばれる。
                     83: int
1.1.1.2   root       84: COMDriverConsole::Read()
1.1       root       85: {
                     86:        uint8 buf[1];
                     87:        int r;
                     88: 
                     89:        r = read(rxpipe, buf, sizeof(buf));
                     90:        if (r < 0) {
                     91:                putmsg(0, "read: %s", strerror(errno));
                     92:                return NODATA;
                     93:        }
                     94:        if (r == 0) {
                     95:                return NODATA;
                     96:        }
                     97:        return buf[0];
                     98: }
                     99: 
                    100: // VM からのキー入力。
                    101: // ConsoleKeyboard から呼ばれる。この文字をパイプに書き込んで帰る。
                    102: void
1.1.1.2   root      103: COMDriverConsole::EnqueueChar(uint charcode)
1.1       root      104: {
                    105:        char buf[1];
                    106:        int r;
                    107: 
                    108:        buf[0] = charcode;
                    109:        r = write(txpipe, buf, sizeof(buf));
                    110:        if (r < 0) {
                    111:                putmsg(0, "write: %s", strerror(errno));
                    112:                return;
                    113:        }
                    114:        if (r == 0) {
                    115:                putmsg(0, "write: short");
                    116:                return;
                    117:        }
                    118: }

unix.superglobalmegacorp.com

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