2 // Copyright (c) 2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include "fdhandler.h"
22 #include <sys/types.h>
26 namespace hwcomposer {
28 FDHandler::FDHandler() {
31 FDHandler::~FDHandler() {
34 bool FDHandler::AddFd(int fd) {
36 ETRACE("Cannot add negative fd: %d", fd);
40 auto it = fds_.find(fd);
41 if (it != fds_.end()) {
42 ETRACE("FD already being watched: %d\n", it->first);
46 fds_.emplace(fd, FDWatch());
51 bool FDHandler::RemoveFd(int fd) {
52 const auto &fd_iter = fds_.find(fd);
53 if (fd_iter == fds_.end()) {
54 ETRACE("FD %d is not being watched.\n", fd);
62 int FDHandler::Poll(int timeout) {
63 nfds_t nfds = fds_.size();
64 struct pollfd fds[nfds];
67 for (auto &it : fds_) {
69 fds[i].events = POLLIN;
74 int ret = poll(fds, nfds, timeout);
76 for (auto &it : fds_) {
77 it.second.revents = fds[it.second.idx].revents;
83 int FDHandler::IsReady(int fd) const {
84 const auto &it = fds_.find(fd);
85 if (it == fds_.end()) {
86 ETRACE("FD %d is being watched but we can't find it.\n", fd);
90 int revents = it->second.revents;
93 else if (revents & POLLERR)
95 else if (revents & POLLNVAL)
101 FDHandler::FDWatch::FDWatch() : revents(0), idx(0) {
104 } // namespace hwcomposer