-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sensors_input_cache for caching input devices
The input cache will keep a list of input devices for swift lookup of device names. It also includes some optimizations for startup time. This code was moved to its own file from sensor_util. Signed-off-by: Oskar Andero <oskar.andero@sonymobile.com>
- Loading branch information
Oskar Andero
committed
Sep 21, 2012
1 parent
5ca57b5
commit 94ddfeb
Showing
4 changed files
with
186 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
#include <sys/ioctl.h> | ||
#include <dirent.h> | ||
#include <pthread.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <linux/input.h> | ||
#include "sensors_log.h" | ||
#include "sensor_util.h" | ||
#include "sensor_util_list.h" | ||
#include "sensors_input_cache.h" | ||
|
||
#define MAX_EVENT_DRIVERS 100 | ||
|
||
static pthread_mutex_t util_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
struct input_dev_list { | ||
struct list_node node; | ||
struct sensors_input_cache_entry_t entry; | ||
}; | ||
|
||
static struct list_node head; | ||
static int list_initialized; | ||
|
||
static void *close_input_dev_fd(void *arg) | ||
{ | ||
close((int) arg); /* pass by copy */ | ||
|
||
return NULL; | ||
} | ||
|
||
static struct input_dev_list *lookup(const char *name, const char *path) | ||
{ | ||
struct list_node *member; | ||
struct input_dev_list *temp; | ||
|
||
for (member = head.n; member != &head; member = member->n) { | ||
temp = container_of(member, struct input_dev_list, node); | ||
if (name && !strncmp(temp->entry.dev_name, name, | ||
sizeof(temp->entry.dev_name) - 1)) | ||
return temp; | ||
if (path && !strncmp(temp->entry.event_path, path, | ||
sizeof(temp->entry.event_path) - 1)) | ||
return temp; | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
const struct sensors_input_cache_entry_t *sensors_input_cache_get( | ||
const char *name) | ||
{ | ||
int rc; | ||
int fd; | ||
DIR *dir; | ||
struct dirent * item; | ||
struct list_node *member; | ||
struct input_dev_list *temp; | ||
pthread_t id[MAX_EVENT_DRIVERS]; | ||
unsigned int i = 0; | ||
unsigned int threads = 0; | ||
const struct sensors_input_cache_entry_t *found = NULL; | ||
|
||
pthread_mutex_lock(&util_mutex); | ||
if (!list_initialized) { | ||
node_init(&head); | ||
list_initialized = 1; | ||
} | ||
|
||
temp = lookup(name, NULL); | ||
if (temp) { | ||
found = &temp->entry; | ||
goto exit; | ||
} | ||
|
||
dir = opendir(INPUT_EVENT_DIR); | ||
if (!dir) { | ||
LOGE("%s: error opening '%s'\n", __func__, | ||
INPUT_EVENT_DIR); | ||
goto exit; | ||
} | ||
|
||
while ((item = readdir(dir)) != NULL) { | ||
if (strncmp(item->d_name, INPUT_EVENT_BASENAME, | ||
sizeof(INPUT_EVENT_BASENAME) - 1) != 0) { | ||
continue; | ||
} | ||
|
||
temp = (temp ? temp : malloc(sizeof(*temp))); | ||
if (temp == NULL) { | ||
LOGE("%s: malloc error!\n", __func__); | ||
break; | ||
} | ||
|
||
/* skip already cached entries */ | ||
snprintf(temp->entry.event_path, sizeof(temp->entry.event_path), | ||
"%s%s", INPUT_EVENT_DIR, item->d_name); | ||
if (lookup(NULL, temp->entry.event_path)) | ||
continue; | ||
|
||
/* make sure we have access */ | ||
fd = open(temp->entry.event_path, O_RDONLY); | ||
if (fd < 0) { | ||
LOGE("%s: cant open %s", __func__, | ||
item->d_name); | ||
continue; | ||
} | ||
|
||
rc = ioctl(fd, EVIOCGNAME(sizeof(temp->entry.dev_name)), | ||
temp->entry.dev_name); | ||
|
||
/* close in parallell to optimize boot time */ | ||
pthread_create(&id[threads++], NULL, | ||
close_input_dev_fd, (void*) fd); | ||
|
||
if (rc < 0) { | ||
LOGE("%s: cant get name from %s", __func__, | ||
item->d_name); | ||
continue; | ||
} | ||
|
||
temp->entry.nr = atoi(item->d_name + | ||
sizeof(INPUT_EVENT_BASENAME) - 1); | ||
|
||
node_add(&head, &temp->node); | ||
if (!found && !strncmp(temp->entry.dev_name, name, | ||
sizeof(temp->entry.dev_name) - 1)) | ||
found = &temp->entry; | ||
temp = NULL; | ||
} | ||
|
||
closedir(dir); | ||
|
||
for(i = 0; i < threads; ++i) | ||
pthread_join(id[i], NULL); | ||
|
||
exit: | ||
pthread_mutex_unlock(&util_mutex); | ||
return found; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef SENSORS_INPUT_CACHE_H_ | ||
#define SENSORS_INPUT_CACHE_H_ | ||
|
||
#define INPUT_EVENT_DIR "/dev/input/" | ||
#define INPUT_EVENT_BASENAME "event" | ||
#define INPUT_EVENT_PATH INPUT_EVENT_DIR INPUT_EVENT_BASENAME | ||
#define MAX_INT_STRING_SIZE sizeof("4294967295") | ||
|
||
struct sensors_input_cache_entry_t { | ||
int nr; | ||
char dev_name[32]; | ||
char event_path[sizeof(INPUT_EVENT_PATH) + MAX_INT_STRING_SIZE]; | ||
}; | ||
|
||
const struct sensors_input_cache_entry_t *sensors_input_cache_get( | ||
const char *name); | ||
|
||
#endif |