blob: 1cfdd80f56b99cb3b718899675d43acb79f61c6d [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
26#include "init.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070027#include "log.h"
Colin Crossa8666952010-04-13 19:20:44 -070028#include "property_service.h"
Tom Cherrybac32992015-07-31 12:45:25 -070029#include "service.h"
Colin Crossa8666952010-04-13 19:20:44 -070030
31static struct input_keychord *keychords = 0;
32static int keychords_count = 0;
33static int keychords_length = 0;
34static int keychord_fd = -1;
35
Tom Cherrybac32992015-07-31 12:45:25 -070036void add_service_keycodes(Service* svc)
Colin Crossa8666952010-04-13 19:20:44 -070037{
38 struct input_keychord *keychord;
Tom Cherrybac32992015-07-31 12:45:25 -070039 size_t i, size;
Colin Crossa8666952010-04-13 19:20:44 -070040
Tom Cherrybac32992015-07-31 12:45:25 -070041 if (!svc->keycodes().empty()) {
Colin Crossa8666952010-04-13 19:20:44 -070042 /* add a new keychord to the list */
Tom Cherrybac32992015-07-31 12:45:25 -070043 size = sizeof(*keychord) + svc->keycodes().size() * sizeof(keychord->keycodes[0]);
Elliott Hughesf3cf4382015-02-03 17:12:07 -080044 keychords = (input_keychord*) realloc(keychords, keychords_length + size);
Colin Crossa8666952010-04-13 19:20:44 -070045 if (!keychords) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070046 PLOG(ERROR) << "could not allocate keychords";
Colin Crossa8666952010-04-13 19:20:44 -070047 keychords_length = 0;
48 keychords_count = 0;
49 return;
50 }
51
52 keychord = (struct input_keychord *)((char *)keychords + keychords_length);
53 keychord->version = KEYCHORD_VERSION;
54 keychord->id = keychords_count + 1;
Tom Cherrybac32992015-07-31 12:45:25 -070055 keychord->count = svc->keycodes().size();
56 svc->set_keychord_id(keychord->id);
Colin Crossa8666952010-04-13 19:20:44 -070057
Tom Cherrybac32992015-07-31 12:45:25 -070058 for (i = 0; i < svc->keycodes().size(); i++) {
59 keychord->keycodes[i] = svc->keycodes()[i];
Colin Crossa8666952010-04-13 19:20:44 -070060 }
61 keychords_count++;
62 keychords_length += size;
63 }
64}
65
Elliott Hughes929f4072015-04-24 21:13:44 -070066static void handle_keychord() {
Colin Crossa8666952010-04-13 19:20:44 -070067 int ret;
68 __u16 id;
69
Colin Crossf7ca6042011-01-04 18:18:45 -080070 ret = read(keychord_fd, &id, sizeof(id));
71 if (ret != sizeof(id)) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070072 PLOG(ERROR) << "could not read keychord id";
Colin Crossf7ca6042011-01-04 18:18:45 -080073 return;
74 }
75
Yabin Cui74edcea2015-07-24 10:11:05 -070076 // Only handle keychords if adb is enabled.
77 std::string adb_enabled = property_get("init.svc.adbd");
78 if (adb_enabled == "running") {
Tom Cherrybac32992015-07-31 12:45:25 -070079 Service* svc = ServiceManager::GetInstance().FindServiceByKeychord(id);
Colin Crossa8666952010-04-13 19:20:44 -070080 if (svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070081 LOG(INFO) << "Starting service " << svc->name() << " from keychord...";
Tom Cherrybac32992015-07-31 12:45:25 -070082 svc->Start();
Colin Crossa8666952010-04-13 19:20:44 -070083 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070084 LOG(ERROR) << "service for keychord " << id << " not found";
Colin Crossa8666952010-04-13 19:20:44 -070085 }
86 }
87}
88
Elliott Hughes929f4072015-04-24 21:13:44 -070089void keychord_init() {
Tom Cherrybac32992015-07-31 12:45:25 -070090 ServiceManager::GetInstance().ForEachService(add_service_keycodes);
Elliott Hughes929f4072015-04-24 21:13:44 -070091
92 // Nothing to do if no services require keychords.
93 if (!keychords) {
94 return;
95 }
96
97 keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
98 if (keychord_fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070099 PLOG(ERROR) << "could not open /dev/keychord";
Elliott Hughes929f4072015-04-24 21:13:44 -0700100 return;
101 }
102
103 int ret = write(keychord_fd, keychords, keychords_length);
104 if (ret != keychords_length) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700105 PLOG(ERROR) << "could not configure /dev/keychord " << ret;
Elliott Hughes929f4072015-04-24 21:13:44 -0700106 close(keychord_fd);
107 }
108
109 free(keychords);
110 keychords = nullptr;
111
112 register_epoll_handler(keychord_fd, handle_keychord);
Colin Crossa8666952010-04-13 19:20:44 -0700113}