~mir-team/mir/in-process-egl+input-conglomeration

« back to all changes in this revision

Viewing changes to 3rd_party/android-input/android/hardware/libhardware_legacy/power/power.c

Merged trunk and fixed issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2008 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
 
#include <hardware_legacy/power.h>
17
 
#include <fcntl.h>
18
 
#include <errno.h>
19
 
#include <stdlib.h>
20
 
#include <stdio.h>
21
 
#include <unistd.h>
22
 
#include <sys/time.h>
23
 
#include <time.h>
24
 
#include <errno.h>
25
 
#include <string.h>
26
 
#include <sys/stat.h>
27
 
#include <sys/types.h>
28
 
#include <pthread.h>
29
 
 
30
 
#define LOG_TAG "power"
31
 
#include <utils/Log.h>
32
 
 
33
 
#ifdef HAVE_WAKE_LOCK
34
 
 
35
 
enum {
36
 
    ACQUIRE_PARTIAL_WAKE_LOCK = 0,
37
 
    RELEASE_WAKE_LOCK,
38
 
    OUR_FD_COUNT
39
 
};
40
 
 
41
 
const char * const OLD_PATHS[] = {
42
 
    "/sys/android_power/acquire_partial_wake_lock",
43
 
    "/sys/android_power/release_wake_lock",
44
 
};
45
 
 
46
 
const char * const NEW_PATHS[] = {
47
 
    "/sys/power/wake_lock",
48
 
    "/sys/power/wake_unlock",
49
 
};
50
 
 
51
 
//XXX static pthread_once_t g_initialized = THREAD_ONCE_INIT;
52
 
static int g_initialized = 0;
53
 
static int g_fds[OUR_FD_COUNT];
54
 
static int g_error = 1;
55
 
 
56
 
static int64_t systemTime()
57
 
{
58
 
    struct timespec t;
59
 
    t.tv_sec = t.tv_nsec = 0;
60
 
    clock_gettime(CLOCK_MONOTONIC, &t);
61
 
    return t.tv_sec*1000000000LL + t.tv_nsec;
62
 
}
63
 
 
64
 
static int
65
 
open_file_descriptors(const char * const paths[])
66
 
{
67
 
    int i;
68
 
    for (i=0; i<OUR_FD_COUNT; i++) {
69
 
        int fd = open(paths[i], O_RDWR);
70
 
        if (fd < 0) {
71
 
            fprintf(stderr, "fatal error opening \"%s\"\n", paths[i]);
72
 
            g_error = errno;
73
 
            return -1;
74
 
        }
75
 
        g_fds[i] = fd;
76
 
    }
77
 
 
78
 
    g_error = 0;
79
 
    return 0;
80
 
}
81
 
 
82
 
static inline void
83
 
initialize_fds(void)
84
 
{
85
 
    // XXX: should be this:
86
 
    //pthread_once(&g_initialized, open_file_descriptors);
87
 
    // XXX: not this:
88
 
    if (g_initialized == 0) {
89
 
        if(open_file_descriptors(NEW_PATHS) < 0)
90
 
            open_file_descriptors(OLD_PATHS);
91
 
        g_initialized = 1;
92
 
    }
93
 
}
94
 
 
95
 
int
96
 
acquire_wake_lock(int lock, const char* id)
97
 
{
98
 
    initialize_fds();
99
 
 
100
 
//    ALOGI("acquire_wake_lock lock=%d id='%s'\n", lock, id);
101
 
 
102
 
    if (g_error) return g_error;
103
 
 
104
 
    int fd;
105
 
 
106
 
    if (lock == PARTIAL_WAKE_LOCK) {
107
 
        fd = g_fds[ACQUIRE_PARTIAL_WAKE_LOCK];
108
 
    }
109
 
    else {
110
 
        return EINVAL;
111
 
    }
112
 
 
113
 
    return write(fd, id, strlen(id));
114
 
}
115
 
 
116
 
int
117
 
release_wake_lock(const char* id)
118
 
{
119
 
    initialize_fds();
120
 
 
121
 
//    ALOGI("release_wake_lock id='%s'\n", id);
122
 
 
123
 
    if (g_error) return g_error;
124
 
 
125
 
    ssize_t len = write(g_fds[RELEASE_WAKE_LOCK], id, strlen(id));
126
 
    return len >= 0;
127
 
}
128
 
 
129
 
#else // HAVE_WAKE_LOCK
130
 
 
131
 
int
132
 
acquire_wake_lock(int lock, const char* id)
133
 
{
134
 
  return strlen(id);
135
 
}
136
 
 
137
 
int
138
 
release_wake_lock(const char* id)
139
 
{
140
 
  return 1;
141
 
}
142
 
 
143
 
#endif // HAVE_WAKE_LOCK