Annotation of nono/host/comdriver_cons.cpp, 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: #include "comdriver_cons.h"
                     12: #include "console.h"
                     13: #include "hostcom.h"
                     14: 
                     15: // コンストラクタ
                     16: COMDriverCons::COMDriverCons(HostDevice *hostdev_)
                     17:        : inherited(hostdev_, "Console")
                     18: {
                     19: }
                     20: 
                     21: // デストラクタ
                     22: COMDriverCons::~COMDriverCons()
                     23: {
                     24: }
                     25: 
                     26: // ドライバ初期化
                     27: bool
                     28: COMDriverCons::InitDriver()
                     29: {
                     30:        console = GetConsoleDevice();
                     31:        console->Attach(this);
                     32: 
                     33:        auto keyboard = gMainApp.GetObject<ConsoleKeyboard>(OBJ_KEYBOARD);
                     34:        keyboard->Attach(this);
                     35: 
                     36:        int fds[2];
                     37:        if (pipe(fds) < 0) {
                     38:                putmsg(0, "pipe failed: %s", strerror(errno));
                     39:                return false;
                     40:        }
                     41:        rxpipe = fds[0];
                     42:        txpipe = fds[1];
                     43: 
                     44:        if (hostdev->AddOuter(rxpipe) < 0) {
                     45:                putmsg(0, "AddOuter: %s", strerror(errno));
                     46:                return false;
                     47:        }
                     48: 
                     49:        return true;
                     50: }
                     51: 
                     52: // 外部への1バイト書き出し
                     53: void
                     54: COMDriverCons::Write(uint32 ch)
                     55: {
                     56:        console->Putchar(ch);
                     57: }
                     58: 
                     59: // 外部からの1バイト読み込み。
                     60: // HostCOM スレッドから呼ばれる。
                     61: int
                     62: COMDriverCons::Read()
                     63: {
                     64:        uint8 buf[1];
                     65:        int r;
                     66: 
                     67:        r = read(rxpipe, buf, sizeof(buf));
                     68:        if (r < 0) {
                     69:                putmsg(0, "read: %s", strerror(errno));
                     70:                return NODATA;
                     71:        }
                     72:        if (r == 0) {
                     73:                return NODATA;
                     74:        }
                     75:        return buf[0];
                     76: }
                     77: 
                     78: // VM からのキー入力。
                     79: // ConsoleKeyboard から呼ばれる。この文字をパイプに書き込んで帰る。
                     80: void
                     81: COMDriverCons::EnqueueChar(uint charcode)
                     82: {
                     83:        char buf[1];
                     84:        int r;
                     85: 
                     86:        buf[0] = charcode;
                     87:        r = write(txpipe, buf, sizeof(buf));
                     88:        if (r < 0) {
                     89:                putmsg(0, "write: %s", strerror(errno));
                     90:                return;
                     91:        }
                     92:        if (r == 0) {
                     93:                putmsg(0, "write: short");
                     94:                return;
                     95:        }
                     96: }

unix.superglobalmegacorp.com

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