2017-03-06 74 views
0

我刚刚尝试过libev的官方示例,如下所示。编译并运行后,我看到一旦我从stdin输入任何东西,事件就会被触发,没有问题。但我输入的内容仍被视为固定输入,然后出现在我的控制台上。我的问题是:有没有办法避免这个控制台输入提示控制台,就像libev抓住并存储它?linux C++ libev官方示例显示冗余控制台行为

任何方式libev可以做到这一点?

我贴在这里正式例如:

// a single header file is required 
    #include <ev.h> 

    #include <stdio.h> // for puts 

    // every watcher type has its own typedef'd struct 
    // with the name ev_TYPE 
    ev_io stdin_watcher; 
    ev_timer timeout_watcher; 

    // all watcher callbacks have a similar signature 
    // this callback is called when data is readable on stdin 
    static void 
    stdin_cb (EV_P_ ev_io *w, int revents) 
    { 
     puts ("stdin ready"); 
     // for one-shot events, one must manually stop the watcher 
     // with its corresponding stop function. 
     ev_io_stop (EV_A_ w); 

     // this causes all nested ev_run's to stop iterating 
     ev_break (EV_A_ EVBREAK_ALL); 
    } 

    // another callback, this time for a time-out 
    static void 
    timeout_cb (EV_P_ ev_timer *w, int revents) 
    { 
     puts ("timeout"); 
     // this causes the innermost ev_run to stop iterating 
     ev_break (EV_A_ EVBREAK_ONE); 
    } 

    int 
    main (void) 
    { 
     // use the default event loop unless you have special needs 
     struct ev_loop *loop = EV_DEFAULT; 

     // initialise an io watcher, then start it 
     // this one will watch for stdin to become readable 
     ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); 
     ev_io_start (loop, &stdin_watcher); 

     // initialise a timer watcher, then start it 
     // simple non-repeating 5.5 second timeout 
     ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); 
     ev_timer_start (loop, &timeout_watcher); 

     // now wait for events to arrive 
     ev_run (loop, 0); 

     // break was called, so exit 
     return 0; 
    } 
+0

你问如何禁用控制台回声? –

回答

1

我假设你的意思是你写什么的呼应?这是终端程序的默认行为。您可以使用termios函数和标志来禁用回显。请记住在退出程序之前启用它。

1

ev_io_init你正在设置你的触发器是什么。例如,可以选择使用套接字中的fd,而不是设置STDIN_FILENO。不知道这是你在找什么。这里你有我说的example