blob: bc286dd185615db50f1b4ff4a144a7071b457852 [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>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <linux/keychord.h>
23
24#include "init.h"
25#include "property_service.h"
26
27static struct input_keychord *keychords = 0;
28static int keychords_count = 0;
29static int keychords_length = 0;
30static int keychord_fd = -1;
31
32void add_service_keycodes(struct service *svc)
33{
34 struct input_keychord *keychord;
35 int i, size;
36
37 if (svc->keycodes) {
38 /* add a new keychord to the list */
39 size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]);
40 keychords = realloc(keychords, keychords_length + size);
41 if (!keychords) {
42 ERROR("could not allocate keychords\n");
43 keychords_length = 0;
44 keychords_count = 0;
45 return;
46 }
47
48 keychord = (struct input_keychord *)((char *)keychords + keychords_length);
49 keychord->version = KEYCHORD_VERSION;
50 keychord->id = keychords_count + 1;
51 keychord->count = svc->nkeycodes;
52 svc->keychord_id = keychord->id;
53
54 for (i = 0; i < svc->nkeycodes; i++) {
55 keychord->keycodes[i] = svc->keycodes[i];
56 }
57 keychords_count++;
58 keychords_length += size;
59 }
60}
61
62void keychord_init()
63{
64 int fd, ret;
65
66 service_for_each(add_service_keycodes);
67
68 /* nothing to do if no services require keychords */
69 if (!keychords)
70 return;
71
72 fd = open("/dev/keychord", O_RDWR);
73 if (fd < 0) {
74 ERROR("could not open /dev/keychord\n");
75 return;
76 }
77 fcntl(fd, F_SETFD, FD_CLOEXEC);
78
79 ret = write(fd, keychords, keychords_length);
80 if (ret != keychords_length) {
81 ERROR("could not configure /dev/keychord %d (%d)\n", ret, errno);
82 close(fd);
83 fd = -1;
84 }
85
86 free(keychords);
87 keychords = 0;
88
89 keychord_fd = fd;
90}
91
92void handle_keychord()
93{
94 struct service *svc;
95 const char* debuggable;
96 const char* adb_enabled;
97 int ret;
98 __u16 id;
99
100 // only handle keychords if ro.debuggable is set or adb is enabled.
101 // the logic here is that bugreports should be enabled in userdebug or eng builds
102 // and on user builds for users that are developers.
103 debuggable = property_get("ro.debuggable");
104 adb_enabled = property_get("init.svc.adbd");
105 if ((debuggable && !strcmp(debuggable, "1")) ||
106 (adb_enabled && !strcmp(adb_enabled, "running"))) {
107 ret = read(keychord_fd, &id, sizeof(id));
108 if (ret != sizeof(id)) {
109 ERROR("could not read keychord id\n");
110 return;
111 }
112
113 svc = service_find_by_keychord(id);
114 if (svc) {
115 INFO("starting service %s from keychord\n", svc->name);
116 service_start(svc, NULL);
117 } else {
118 ERROR("service for keychord %d not found\n", id);
119 }
120 }
121}
122
123int get_keychord_fd()
124{
125 return keychord_fd;
126}