Annotation of nono/cli/cuimessage.cpp, revision 1.1.1.2

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2021 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
1.1.1.2 ! root        7: //
        !             8: // UI スレッドへのメッセージ機構 (CLI 側)
        !             9: //
        !            10: 
1.1       root       11: #include "cuimessage.h"
1.1.1.2 ! root       12: #include "kevent.h"
1.1       root       13: 
                     14: std::array<CUIMessage::Func, UIMessage::ID_MAX> CUIMessage::func_table;
                     15: 
1.1.1.2 ! root       16: /*static*/ autofd CUIMessage::wpipe;
        !            17: /*static*/ autofd CUIMessage::rpipe;
        !            18: 
1.1       root       19: // UIMessage を func に接続する
                     20: /*static*/ void
                     21: CUIMessage::Connect(UIMessage::ID id, CUIMessage::Func func)
                     22: {
                     23:        func_table[id] = func;
                     24: }
                     25: 
1.1.1.2 ! root       26: // CUIMessage の初期化。パイプを用意して kqueue ディスクリプタを返す。
        !            27: int
        !            28: CUIMessage::Init()
        !            29: {
        !            30:        int kq;
        !            31:        int fds[2];
        !            32: 
        !            33:        kq = kqueue();
        !            34:        if (kq < 0) {
        !            35:                warn("CUIMessage::Init: kqueue");
        !            36:                return -1;
        !            37:        }
        !            38: 
        !            39:        if (pipe(fds) < 0) {
        !            40:                warn("CUIMessage::Init: pipe");
        !            41:                close(kq);
        !            42:                return -1;
        !            43:        }
        !            44: 
        !            45:        rpipe = fds[0];
        !            46:        wpipe = fds[1];
        !            47: 
        !            48:        if (kevent_add(kq, rpipe, EVFILT_READ, EV_ADD, 0) < 0) {
        !            49:                warn("CUIMessage::Init: kevent_add");
        !            50:                close(kq);
        !            51:                return -1;
        !            52:        }
        !            53: 
        !            54:        return kq;
        !            55: }
        !            56: 
1.1       root       57: // UIMessage を処理する
                     58: /*static*/ void
                     59: CUIMessage::Process(UIMessage::Queue& queue)
                     60: {
                     61:        UIMessage m;
                     62: 
                     63:        while (queue.Dequeue(&m)) {
                     64:                UIMessage::ID id = m.GetID();
1.1.1.2 ! root       65: 
1.1       root       66:                if (id < 0 || id >= UIMessage::ID_MAX) {
                     67:                        PANIC("Invalid UIMessage %d", id);
                     68:                }
1.1.1.2 ! root       69: 
        !            70:                // パイプで通知。クラス(構造体)をそのまま書き込む。
        !            71:                int r;
        !            72:                r = write(wpipe, &m, sizeof(m));
        !            73:                if (r < 0) {
        !            74:                        warn("CUIMessage::Process: write");
        !            75:                        continue;
        !            76:                }
        !            77:                if (r < sizeof(m)) {
        !            78:                        warnx("CUIMessage::Process: write: too short");
        !            79:                        continue;
1.1       root       80:                }
                     81:        }
                     82: }
1.1.1.2 ! root       83: 
        !            84: // UIMessage をディスパッチする (メインスレッドで呼ばれる)
        !            85: /*static*/ void
        !            86: CUIMessage::Dispatch(const UIMessage& m)
        !            87: {
        !            88:        auto func = func_table[m.GetID()];
        !            89:        if (func) {
        !            90:                func(m);
        !            91:        }
        !            92: }

unix.superglobalmegacorp.com

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