|
|
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();
! 30: }
! 31: auto keyboard = gMainApp.FindObject<ConsoleKeyboard>(OBJ_KEYBOARD);
! 32: if (keyboard) {
! 33: keyboard->Detach();
! 34: }
1.1 root 35: }
36:
37: // ドライバ初期化
38: bool
1.1.1.2 ! root 39: COMDriverConsole::InitDriver(bool startup)
1.1 root 40: {
41: int fds[2];
42: if (pipe(fds) < 0) {
1.1.1.2 ! root 43: errmsg = string_format("pipe: %s", strerror(errno));
! 44: putmsg(1, "%s", errmsg.c_str());
1.1 root 45: return false;
46: }
47: rxpipe = fds[0];
48: txpipe = fds[1];
49:
50: if (hostdev->AddOuter(rxpipe) < 0) {
1.1.1.2 ! root 51: errmsg = string_format("AddOuter: %s", strerror(errno));
! 52: putmsg(1, "%s", errmsg.c_str());
1.1 root 53: return false;
54: }
55:
1.1.1.2 ! root 56: console = GetConsoleDevice();
! 57: console->Attach(this);
! 58:
! 59: auto keyboard = gMainApp.GetObject<ConsoleKeyboard>(OBJ_KEYBOARD);
! 60: keyboard->Attach(this);
! 61:
1.1 root 62: return true;
63: }
64:
65: // 外部への1バイト書き出し
66: void
1.1.1.2 ! root 67: COMDriverConsole::Write(uint32 ch)
1.1 root 68: {
69: console->Putchar(ch);
70: }
71:
72: // 外部からの1バイト読み込み。
73: // HostCOM スレッドから呼ばれる。
74: int
1.1.1.2 ! root 75: COMDriverConsole::Read()
1.1 root 76: {
77: uint8 buf[1];
78: int r;
79:
80: r = read(rxpipe, buf, sizeof(buf));
81: if (r < 0) {
82: putmsg(0, "read: %s", strerror(errno));
83: return NODATA;
84: }
85: if (r == 0) {
86: return NODATA;
87: }
88: return buf[0];
89: }
90:
91: // VM からのキー入力。
92: // ConsoleKeyboard から呼ばれる。この文字をパイプに書き込んで帰る。
93: void
1.1.1.2 ! root 94: COMDriverConsole::EnqueueChar(uint charcode)
1.1 root 95: {
96: char buf[1];
97: int r;
98:
99: buf[0] = charcode;
100: r = write(txpipe, buf, sizeof(buf));
101: if (r < 0) {
102: putmsg(0, "write: %s", strerror(errno));
103: return;
104: }
105: if (r == 0) {
106: putmsg(0, "write: short");
107: return;
108: }
109: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.