blob: 27894a2e896d401cf5c9c1c23f4f98ff94bc9de5 [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"
29
30static struct input_keychord *keychords = 0;
31static int keychords_count = 0;
32static int keychords_length = 0;
33static int keychord_fd = -1;
34
35void 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 Hughesf3cf4382015-02-03 17:12:07 -080043 keychords = (input_keychord*) realloc(keychords, keychords_length + size);
Colin Crossa8666952010-04-13 19:20:44 -070044 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
65void keychord_init()
66{
67 int fd, ret;
68
69 service_for_each(add_service_keycodes);
70
71 /* nothing to do if no services require keychords */
72 if (!keychords)
73 return;
74
Nick Kralevich45a884f2015-02-02 14:37:22 -080075 fd = open("/dev/keychord", O_RDWR | O_CLOEXEC);
Colin Crossa8666952010-04-13 19:20:44 -070076 if (fd < 0) {
77 ERROR("could not open /dev/keychord\n");
78 return;
79 }
Colin Crossa8666952010-04-13 19:20:44 -070080
81 ret = write(fd, keychords, keychords_length);
82 if (ret != keychords_length) {
Elliott Hughescd67f002015-03-20 17:05:56 -070083 ERROR("could not configure /dev/keychord %d: %s\n", ret, strerror(errno));
Colin Crossa8666952010-04-13 19:20:44 -070084 close(fd);
85 fd = -1;
86 }
87
88 free(keychords);
89 keychords = 0;
90
91 keychord_fd = fd;
92}
93
94void handle_keychord()
95{
96 struct service *svc;
Colin Cross1a6f4c32013-01-28 17:13:35 -080097 char adb_enabled[PROP_VALUE_MAX];
Colin Crossa8666952010-04-13 19:20:44 -070098 int ret;
99 __u16 id;
100
Jeff Sharkeyb4d52a42013-04-04 10:44:41 -0700101 // Only handle keychords if adb is enabled.
Colin Cross1a6f4c32013-01-28 17:13:35 -0800102 property_get("init.svc.adbd", adb_enabled);
Colin Crossf7ca6042011-01-04 18:18:45 -0800103 ret = read(keychord_fd, &id, sizeof(id));
104 if (ret != sizeof(id)) {
105 ERROR("could not read keychord id\n");
106 return;
107 }
108
Colin Cross99c1a412013-06-17 18:19:28 -0700109 if (!strcmp(adb_enabled, "running")) {
Colin Crossa8666952010-04-13 19:20:44 -0700110 svc = service_find_by_keychord(id);
111 if (svc) {
Elliott Hughesda40c002015-03-27 23:20:44 -0700112 INFO("Starting service %s from keychord\n", svc->name);
Colin Crossa8666952010-04-13 19:20:44 -0700113 service_start(svc, NULL);
114 } else {
115 ERROR("service for keychord %d not found\n", id);
116 }
117 }
118}
119
120int get_keychord_fd()
121{
122 return keychord_fd;
123}