• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include "eventgatherer.h"
17 #include "defines.h"
18 #include <utils/Log.h>
19 
20 using namespace com::android::car::keventreader;
21 
EventGatherer(int argc,const char ** argv)22 EventGatherer::EventGatherer(int argc, const char** argv) {
23     for (auto i = 1; i < argc; ++i) {
24         auto dev = std::make_unique<InputSource>(argv[i]);
25         if (dev && *dev) {
26             ALOGD("opened input source file %s", argv[i]);
27             mFds.push_back({dev->descriptor(), POLLIN, 0});
28             mDevices.emplace(dev->descriptor(), std::move(dev));
29         } else {
30             ALOGW("failed to open input source file %s", argv[i]);
31         }
32     }
33 }
34 
size() const35 size_t EventGatherer::size() const {
36     return mDevices.size();
37 }
38 
read()39 std::vector<com::android::car::keventreader::KeypressEvent> EventGatherer::read() {
40     constexpr int FOREVER = -1;
41     std::vector<com::android::car::keventreader::KeypressEvent> result;
42 
43     int count = poll(&mFds[0], mFds.size(), FOREVER);
44     if (count < 0) {
45         ALOGE("poll failed: errno = %d", errno);
46         return result;
47     }
48 
49     for (auto& fd : mFds) {
50         if (fd.revents != 0) {
51             if (fd.revents == POLLIN) {
52                 if (auto dev = mDevices[fd.fd].get()) {
53                     while(auto evt = dev->read()) {
54                         result.push_back(*evt);
55                     }
56                     --count; // found one source of events we care abour
57                 }
58             }
59             fd.revents = 0;
60             if (count == 0) break; // if we read all events, no point in continuing the scan
61         }
62     }
63 
64     return result;
65 }
66