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 "defines.h"
17 
18 #include <binder/IPCThreadState.h>
19 #include <binder/IServiceManager.h>
20 #include <binder/ProcessState.h>
21 #include <iostream>
22 #include <signal.h>
23 #include <utils/Log.h>
24 #include "eventgatherer.h"
25 #include "keymap.h"
26 #include "eventprovider.h"
27 
28 
29 using namespace android;
30 using namespace com::android::car::keventreader;
31 
32 /**
33  * This tool expects to be able to read input events in the Linux kernel input_event format
34  * (see linux/input.h); that format is available by means of /dev/input/event* files (which are
35  * mapped 1-to-1 to input sources that provide keypresses as input (e.g. keyboards)
36  * The tool will hook up to each such file passed as input
37  */
38 static const char* SYNTAX_INSTRUCTIONS =
39     "invalid command line arguments - provide one or more /dev/input/event files";
40 
error(int code)41 static void error(int code) {
42     ALOGE("%s", SYNTAX_INSTRUCTIONS);
43     std::cerr << SYNTAX_INSTRUCTIONS << '\n';
44     exit(code);
45 }
46 
main(int argc,const char ** argv)47 int main(int argc, const char** argv) {
48     if (argc < 2 || argv[1] == nullptr || argv[1][0] == 0) {
49         error(1);
50     }
51 
52     EventGatherer gatherer(argc, argv);
53     if (0 == gatherer.size()) {
54         error(2);
55     }
56 
57     ALOGI("starting " LOG_TAG);
58     signal(SIGPIPE, SIG_IGN);
59 
60     sp<ProcessState> processSelf(ProcessState::self());
61     sp<IServiceManager> serviceManager = defaultServiceManager();
62     auto service = std::make_unique<EventProviderImpl>(std::move(gatherer));
63     serviceManager->addService(String16(SERVICE_NAME), service.get());
64 
65     processSelf->startThreadPool();
66 
67     ALOGI(LOG_TAG " started");
68 
69     auto svcthread = service->startLoop();
70     IPCThreadState::self()->joinThreadPool();
71 
72     ALOGW(LOG_TAG " joined and going down");
73     exit(0);
74 }
75