내 시스템에서 event2
은 마우스를 나타내며 다음과 같은 간단한 readloop
프로그램은 매력처럼 작동합니다.
이
#include <boost/asio.hpp>
#include <boost/asio/posix/stream_descriptor.hpp>
#include <boost/bind.hpp>
#include <iostream> // for debug output
#include <iomanip>
#include <linux/input.h> // for input_event
#include <boost/range/adaptor/sliced.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::asio;
struct input {
using error_code = boost::system::error_code;
using sliced = boost::adaptors::sliced;
input(const char* devspec) : svc(), sd(svc, open(devspec, O_RDONLY)) {
readloop(); // post work
}
void run() {
svc.run();
}
private:
io_service svc;
posix::stream_descriptor sd;
std::vector<input_event> events;
void handle_input(error_code ec, size_t bytes_transferred) {
if (!ec) {
auto const n = bytes_transferred/sizeof(input_event);
for (auto& ev : events | sliced(0,n)) {
using namespace boost::posix_time;
ptime ts({1970,1,1}, seconds(ev.time.tv_sec) + microsec(ev.time.tv_usec));
std::cout << std::dec << ts.time_of_day() << "\t" << std::hex
<< std::hex << ev.type << " " << ev.code << " " << ev.value << "\n";
}
std::cout << "\n";
readloop();
} else {
std::cerr << ec.message() << "\n";
}
}
void readloop() {
events.resize(32);
sd.async_read_some(buffer(events), boost::bind(&input::handle_input, this, placeholders::error, placeholders::bytes_transferred));
}
};
int main()
{
input monitor("/dev/input/event2");
monitor.run();
}
이
일반적인 출력 : 나는 장치를 열고 파일 기술자를 얻을 수
22:33:09.705346 2 0 ffffffff
22:33:09.705346 2 1 1
22:33:09.705346 0 0 0
22:33:09.713412 2 0 ffffffff
22:33:09.713412 2 1 1
22:33:09.713412 0 0 0
22:33:09.721308 2 0 ffffffff
22:33:09.721308 0 0 0
22:33:09.729328 2 0 ffffffff
22:33:09.729328 0 0 0
22:33:09.737346 2 1 1
22:33:09.737346 0 0 0
22:33:09.745328 2 0 ffffffff
22:33:09.745328 2 1 1
22:33:09.745328 0 0 0
22:33:11.897301 4 4 90001
22:33:11.897301 1 110 1
22:33:11.897301 0 0 0
22:33:12.065294 4 4 90001
22:33:12.065294 1 110 0
22:33:12.065294 0 0 0
는 다음 ASIO에서 그것을 사용하는 내가 – xinthose
'Boost.Asio' 생각 루트로
는실행 최신 C++ 인터페이스를 제공하지만 구현시 일반 C (또는 OS) 기능을 사용합니다. 먼저, 위의'read'가 왜 작동하지 않는지 이해할 가치가 있습니다. 어떤 행동을 관찰합니까? –
관련성이 높음 : 원시 장치 대신 라이브러리를 사용하는 것이 좋습니다. http://stackoverflow.com/a/25559167/85371 – sehe