blob: 5801ea88fd31b43fb904e1e47d59c0bdf196b2f7 [file] [log] [blame]
Colin Crossa8666952010-04-13 19:20:44 -07001/*
2 * Copyright (C) 2010 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
17#include <errno.h>
18#include <fcntl.h>
19#include <stdlib.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080020#include <string.h>
Colin Crossa8666952010-04-13 19:20:44 -070021#include <sys/stat.h>
22#include <sys/types.h>
23#include <linux/keychord.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -080024#include <unistd.h>
Colin Crossa8666952010-04-13 19:20:44 -070025
Tom Cherryccf23532017-03-28 16:40:41 -070026#include <android-base/properties.h>
27
Colin Crossa8666952010-04-13 19:20:44 -070028#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070029#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070030#include "service.h"
Colin Crossa8666952010-04-13 19:20:44 -070031
32static struct input_keychord *keychords = 0;
33static int keychords_count = 0;
34static int keychords_length = 0;
35static int keychord_fd = -1;
36
Tom Cherrybac32992015-07-31 12:45:25 -070037void add_service_keycodes(Service* svc)
Colin Crossa8666952010-04-13 19:20:44 -070038{
39 struct input_keychord *keychord;
Tom Cherrybac32992015-07-31 12:45:25 -070040 size_t i, size;
Colin Crossa8666952010-04-13 19:20:44 -070041
Tom Cherrybac32992015-07-31 12:45:25 -070042 if (!svc->keycodes().empty()) {
Colin Crossa8666952010-04-13 19:20:44 -070043 /* add a new keychord to the list */
Tom Cherrybac32992015-07-31 12:45:25 -070044 size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
Elliott Hughesf3cf4382015-02-03 17:12:07 -080045 keychords = (input_keychord*) realloc(keychords, keychords_length + size);
Colin Crossa8666952010-04-13 19:20:44 -070046 if (!keychords) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070047 PLOG(ERROR) << "could not allocate keychords";
Colin Crossa8666952010-04-13 19:20:44 -070048 keychords_length = 0;
49 keychords_count = 0;
50 return;
51 }
52
53 keychord = (struct input_keychord *)((char *)keychords + keychords_length);
54 keychord->version = KEYCHORD_VERSION;
55 keychord->id = keychords_count + 1;
Tom Cherrybac32992015-07-31 12:45:25 -070056 keychord->count = svc->keycodes().size();
57 svc->set_keychord_id(keychord->id);
Colin Crossa8666952010-04-13 19:20:44 -070058
Tom Cherrybac32992015-07-31 12:45:25 -070059 for (i = 0; i < svc->keycodes().size(); i++) {
60 keychord->keycodes[i] = svc->keycodes()[i];
Colin Crossa8666952010-04-13 19:20:44 -070061 }
62 keychords_count++;
63 keychords_length += size;
64 }
65}
66
Elliott Hughes929f4072015-04-24 21:13:44 -070067static void handle_keychord() {
Colin Crossa8666952010-04-13 19:20:44 -070068 int ret;
69 __u16 id;
70
Colin Crossf7ca6042011-01-04 18:18:45 -080071 ret = read(keychord_fd, &id, sizeof(id));
72 if (ret != sizeof(id)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070073 PLOG(ERROR) << "could not read keychord id";
Colin Crossf7ca6042011-01-04 18:18:45 -080074 return;
75 }
76
Yabin Cui74edcea2015-07-24 10:11:05 -070077 // Only handle keychords if adb is enabled.
Tom Cherryccf23532017-03-28 16:40:41 -070078 std::string adb_enabled = android::base::GetProperty("init.svc.adbd", "");
Yabin Cui74edcea2015-07-24 10:11:05 -070079 if (adb_enabled == "running") {
Tom Cherrybac32992015-07-31 12:45:25 -070080 Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id);
Colin Crossa8666952010-04-13 19:20:44 -070081 if (svc) {
Felipe Leme704fe2d2016-07-29 08:34:39 -070082 LOG(INFO) << "Starting service " << svc->name() << " from keychord " << id;
Tom Cherrybac32992015-07-31 12:45:25 -070083 svc->Start();
Colin Crossa8666952010-04-13 19:20:44 -070084 } else {
Felipe Leme704fe2d2016-07-29 08:34:39 -070085 LOG(ERROR) << "Service for keychord " << id << " not found";
Colin Crossa8666952010-04-13 19:20:44 -070086 }
Felipe Lemec64c9822016-07-28 13:26:07 -070087 } else {
Felipe Leme704fe2d2016-07-29 08:34:39 -070088 LOG(WARNING) << "Not starting service for keychord " << id << " because ADB is disabled";
Colin Crossa8666952010-04-13 19:20:44 -070089 }
90}
91
Elliott Hughes929f4072015-04-24 21:13:44 -070092void keychord_init() {
Tom Cherrybac32992015-07-31 12:45:25 -070093 ServiceManager::GetInstance().ForEachService(add_service_keycodes);
Elliott Hughes929f4072015-04-24 21:13:44 -070094
95 // Nothing to do if no services require keychords.
96 if (!keychords) {
97 return;
98 }
99
100 keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
101 if (keychord_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700102 PLOG(ERROR) << "could not open /dev/keychord";
Elliott Hughes929f4072015-04-24 21:13:44 -0700103 return;
104 }
105
106 int ret = write(keychord_fd, keychords, keychords_length);
107 if (ret != keychords_length) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700108 PLOG(ERROR) << "could not configure /dev/keychord " << ret;
Elliott Hughes929f4072015-04-24 21:13:44 -0700109 close(keychord_fd);
110 }
111
112 free(keychords);
113 keychords = nullptr;
114
115 register_epoll_handler(keychord_fd, handle_keychord);
Colin Crossa8666952010-04-13 19:20:44 -0700116}