2010-04-14 04:20:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2010-11-17 20:47:23 +01:00
|
|
|
#include <string.h>
|
2010-04-14 04:20:44 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <linux/keychord.h>
|
2010-11-17 20:47:23 +01:00
|
|
|
#include <unistd.h>
|
2010-04-14 04:20:44 +02:00
|
|
|
|
|
|
|
#include "init.h"
|
2010-04-20 02:05:34 +02:00
|
|
|
#include "log.h"
|
2010-04-14 04:20:44 +02:00
|
|
|
#include "property_service.h"
|
|
|
|
|
|
|
|
static struct input_keychord *keychords = 0;
|
|
|
|
static int keychords_count = 0;
|
|
|
|
static int keychords_length = 0;
|
|
|
|
static int keychord_fd = -1;
|
|
|
|
|
|
|
|
void add_service_keycodes(struct service *svc)
|
|
|
|
{
|
|
|
|
struct input_keychord *keychord;
|
|
|
|
int i, size;
|
|
|
|
|
|
|
|
if (svc->keycodes) {
|
|
|
|
/* add a new keychord to the list */
|
|
|
|
size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]);
|
2015-02-04 02:12:07 +01:00
|
|
|
keychords = (input_keychord*) realloc(keychords, keychords_length + size);
|
2010-04-14 04:20:44 +02:00
|
|
|
if (!keychords) {
|
|
|
|
ERROR("could not allocate keychords\n");
|
|
|
|
keychords_length = 0;
|
|
|
|
keychords_count = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
keychord = (struct input_keychord *)((char *)keychords + keychords_length);
|
|
|
|
keychord->version = KEYCHORD_VERSION;
|
|
|
|
keychord->id = keychords_count + 1;
|
|
|
|
keychord->count = svc->nkeycodes;
|
|
|
|
svc->keychord_id = keychord->id;
|
|
|
|
|
|
|
|
for (i = 0; i < svc->nkeycodes; i++) {
|
|
|
|
keychord->keycodes[i] = svc->keycodes[i];
|
|
|
|
}
|
|
|
|
keychords_count++;
|
|
|
|
keychords_length += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void keychord_init()
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
|
|
|
|
service_for_each(add_service_keycodes);
|
|
|
|
|
|
|
|
/* nothing to do if no services require keychords */
|
|
|
|
if (!keychords)
|
|
|
|
return;
|
|
|
|
|
2015-02-02 23:37:22 +01:00
|
|
|
fd = open("/dev/keychord", O_RDWR | O_CLOEXEC);
|
2010-04-14 04:20:44 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
ERROR("could not open /dev/keychord\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write(fd, keychords, keychords_length);
|
|
|
|
if (ret != keychords_length) {
|
2015-03-21 01:05:56 +01:00
|
|
|
ERROR("could not configure /dev/keychord %d: %s\n", ret, strerror(errno));
|
2010-04-14 04:20:44 +02:00
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(keychords);
|
|
|
|
keychords = 0;
|
|
|
|
|
|
|
|
keychord_fd = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handle_keychord()
|
|
|
|
{
|
|
|
|
struct service *svc;
|
2013-01-29 02:13:35 +01:00
|
|
|
char adb_enabled[PROP_VALUE_MAX];
|
2010-04-14 04:20:44 +02:00
|
|
|
int ret;
|
|
|
|
__u16 id;
|
|
|
|
|
2013-04-04 19:44:41 +02:00
|
|
|
// Only handle keychords if adb is enabled.
|
2013-01-29 02:13:35 +01:00
|
|
|
property_get("init.svc.adbd", adb_enabled);
|
2011-01-05 03:18:45 +01:00
|
|
|
ret = read(keychord_fd, &id, sizeof(id));
|
|
|
|
if (ret != sizeof(id)) {
|
|
|
|
ERROR("could not read keychord id\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:19:28 +02:00
|
|
|
if (!strcmp(adb_enabled, "running")) {
|
2010-04-14 04:20:44 +02:00
|
|
|
svc = service_find_by_keychord(id);
|
|
|
|
if (svc) {
|
2015-03-28 07:20:44 +01:00
|
|
|
INFO("Starting service %s from keychord\n", svc->name);
|
2010-04-14 04:20:44 +02:00
|
|
|
service_start(svc, NULL);
|
|
|
|
} else {
|
|
|
|
ERROR("service for keychord %d not found\n", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_keychord_fd()
|
|
|
|
{
|
|
|
|
return keychord_fd;
|
|
|
|
}
|