Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 1 | /* |
| 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 Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 20 | #include <string.h> |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <linux/keychord.h> |
Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 25 | |
| 26 | #include "init.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 27 | #include "log.h" |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 28 | #include "property_service.h" |
| 29 | |
| 30 | static struct input_keychord *keychords = 0; |
| 31 | static int keychords_count = 0; |
| 32 | static int keychords_length = 0; |
| 33 | static int keychord_fd = -1; |
| 34 | |
| 35 | void add_service_keycodes(struct service *svc) |
| 36 | { |
| 37 | struct input_keychord *keychord; |
| 38 | int i, size; |
| 39 | |
| 40 | if (svc->keycodes) { |
| 41 | /* add a new keychord to the list */ |
| 42 | size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]); |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 43 | keychords = (input_keychord*) realloc(keychords, keychords_length + size); |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 44 | if (!keychords) { |
| 45 | ERROR("could not allocate keychords\n"); |
| 46 | keychords_length = 0; |
| 47 | keychords_count = 0; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | keychord = (struct input_keychord *)((char *)keychords + keychords_length); |
| 52 | keychord->version = KEYCHORD_VERSION; |
| 53 | keychord->id = keychords_count + 1; |
| 54 | keychord->count = svc->nkeycodes; |
| 55 | svc->keychord_id = keychord->id; |
| 56 | |
| 57 | for (i = 0; i < svc->nkeycodes; i++) { |
| 58 | keychord->keycodes[i] = svc->keycodes[i]; |
| 59 | } |
| 60 | keychords_count++; |
| 61 | keychords_length += size; |
| 62 | } |
| 63 | } |
| 64 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame^] | 65 | static void handle_keychord() { |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 66 | struct service *svc; |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 67 | char adb_enabled[PROP_VALUE_MAX]; |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 68 | int ret; |
| 69 | __u16 id; |
| 70 | |
Jeff Sharkey | b4d52a4 | 2013-04-04 10:44:41 -0700 | [diff] [blame] | 71 | // Only handle keychords if adb is enabled. |
Colin Cross | 1a6f4c3 | 2013-01-28 17:13:35 -0800 | [diff] [blame] | 72 | property_get("init.svc.adbd", adb_enabled); |
Colin Cross | f7ca604 | 2011-01-04 18:18:45 -0800 | [diff] [blame] | 73 | ret = read(keychord_fd, &id, sizeof(id)); |
| 74 | if (ret != sizeof(id)) { |
| 75 | ERROR("could not read keychord id\n"); |
| 76 | return; |
| 77 | } |
| 78 | |
Colin Cross | 99c1a41 | 2013-06-17 18:19:28 -0700 | [diff] [blame] | 79 | if (!strcmp(adb_enabled, "running")) { |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 80 | svc = service_find_by_keychord(id); |
| 81 | if (svc) { |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 82 | INFO("Starting service %s from keychord\n", svc->name); |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 83 | service_start(svc, NULL); |
| 84 | } else { |
| 85 | ERROR("service for keychord %d not found\n", id); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Elliott Hughes | 929f407 | 2015-04-24 21:13:44 -0700 | [diff] [blame^] | 90 | void keychord_init() { |
| 91 | service_for_each(add_service_keycodes); |
| 92 | |
| 93 | // Nothing to do if no services require keychords. |
| 94 | if (!keychords) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | keychord_fd = TEMP_FAILURE_RETRY(open("/dev/keychord", O_RDWR | O_CLOEXEC)); |
| 99 | if (keychord_fd == -1) { |
| 100 | ERROR("could not open /dev/keychord: %s\n", strerror(errno)); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | int ret = write(keychord_fd, keychords, keychords_length); |
| 105 | if (ret != keychords_length) { |
| 106 | ERROR("could not configure /dev/keychord %d: %s\n", ret, strerror(errno)); |
| 107 | close(keychord_fd); |
| 108 | } |
| 109 | |
| 110 | free(keychords); |
| 111 | keychords = nullptr; |
| 112 | |
| 113 | register_epoll_handler(keychord_fd, handle_keychord); |
Colin Cross | a866695 | 2010-04-13 19:20:44 -0700 | [diff] [blame] | 114 | } |