blob: 1468c5703ba930ff8522274d9ed0c8bbd599bb6a [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) {
46 ERROR("could not allocate keychords\n");
47 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)) {
72 ERROR("could not read keychord id\n");
73 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) {
Felipe Lemec64c9822016-07-28 13:26:07 -070081 NOTICE("Starting service '%s' from keychord %d\n", svc->name().c_str(), id);
Tom Cherrybac32992015-07-31 12:45:25 -070082 svc->Start();
Colin Crossa8666952010-04-13 19:20:44 -070083 } else {
Felipe Lemec64c9822016-07-28 13:26:07 -070084 ERROR("Service for keychord %d not found\n", id);
Colin Crossa8666952010-04-13 19:20:44 -070085 }
Felipe Lemec64c9822016-07-28 13:26:07 -070086 } else {
87 WARNING("Not starting service for keychord %d because ADB is disabled\n", id);
Colin Crossa8666952010-04-13 19:20:44 -070088 }
89}
90
Elliott Hughes929f4072015-04-24 21:13:44 -070091void keychord_init() {
Tom Cherrybac32992015-07-31 12:45:25 -070092 ServiceManager::GetInstance().ForEachService(add_service_keycodes);
Elliott Hughes929f4072015-04-24 21:13:44 -070093
94 // Nothing to do if no services require keychords.
95 if (!keychords) {
96 return;
97 }
98
99 keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC));
100 if (keychord_fd == -1) {
101 ERROR("could not open /dev/keychord: %s\n", strerror(errno));
102 return;
103 }
104
105 int ret = write(keychord_fd, keychords, keychords_length);
106 if (ret != keychords_length) {
107 ERROR("could not configure /dev/keychord %d: %s\n", ret, strerror(errno));
108 close(keychord_fd);
109 }
110
111 free(keychords);
112 keychords = nullptr;
113
114 register_epoll_handler(keychord_fd, handle_keychord);
Colin Crossa8666952010-04-13 19:20:44 -0700115}