app: some comments and erorrs regarding IDs configuration
[staging/windowmanager.git] / src / util.cpp
1 #include "util.hpp"
2
3 #include <cerrno>
4 #include <cstdarg>
5 #include <cstdio>
6 #include <cstdlib>
7 #include <ctime>
8
9 #include <unistd.h>
10
11 void Poller::add_fd(int fd, std::function<int(int)> handler) {
12    pfds.emplace_back(pollfd{.fd = fd, .events = POLLIN, .revents = 0});
13    handlers.emplace_back(std::move(handler));
14 }
15
16 int Poller::check_events() {
17    int ret = 0;
18    if ((ret = poll(this->pfds.data(), this->pfds.size(), -1)) != -1 &&
19        errno != EINTR) {
20       for (unsigned i = 0; i < pfds.size(); i++) {
21          if ((pfds[i].revents & POLLIN) != 0) {
22             if (handlers[i](pfds[i].fd) == -1) {
23                return -1;
24             }
25             pfds[i].revents = 0;
26             pfds[i].events = POLLIN;
27          }
28       }
29    }
30    return ret;
31 }
32
33 unique_fd::~unique_fd() {
34    if (this->fd != -1) {
35       close(this->fd);
36    }
37 }