hardware/libhardware_legacy
Revision | 1db28bbc387a52b50187d70f80820299cff27db5 (tree) |
---|---|
Time | 2012-02-22 13:21:00 |
Author | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
powerbtnd: add poweroff.doubleclick feature
On poweroff.doubleclick=0, one click to power button invokes poweroff
dialog directly.
On poweroff.doubleclick=1, one click to power button suspends the system,
while double click in one second invokes poweroff dialog.
@@ -51,7 +51,7 @@ LOCAL_SRC_FILES := power/powerbtnd.c | ||
51 | 51 | |
52 | 52 | LOCAL_MODULE := powerbtnd |
53 | 53 | LOCAL_MODULE_TAGS := optional |
54 | -LOCAL_SHARED_LIBRARIES := liblog | |
54 | +LOCAL_SHARED_LIBRARIES := liblog libcutils | |
55 | 55 | |
56 | 56 | include $(BUILD_EXECUTABLE) |
57 | 57 |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /** |
2 | 2 | * A daemon to simulate power button of Android |
3 | 3 | * |
4 | - * Copyright (C) 2011 The Android-x86 Open Source Project | |
4 | + * Copyright (C) 2011-2012 The Android-x86 Open Source Project | |
5 | 5 | * |
6 | 6 | * by Chih-Wei Huang <cwhuang@linux.org.tw> |
7 | 7 | * |
@@ -19,6 +19,7 @@ | ||
19 | 19 | #include <cutils/log.h> |
20 | 20 | #include <linux/input.h> |
21 | 21 | #include <linux/uinput.h> |
22 | +#include <cutils/properties.h> | |
22 | 23 | |
23 | 24 | const int MAX_POWERBTNS = 3; |
24 | 25 |
@@ -35,7 +36,7 @@ int openfds(struct pollfd pfds[]) | ||
35 | 36 | continue; |
36 | 37 | char name[PATH_MAX]; |
37 | 38 | snprintf(name, PATH_MAX, "%s/%s", dirname, de->d_name); |
38 | - fd = open(name, O_RDWR); | |
39 | + fd = open(name, O_RDWR | O_NONBLOCK); | |
39 | 40 | if (fd < 0) { |
40 | 41 | LOGE("could not open %s, %s", name, strerror(errno)); |
41 | 42 | continue; |
@@ -64,11 +65,33 @@ int openfds(struct pollfd pfds[]) | ||
64 | 65 | return cnt; |
65 | 66 | } |
66 | 67 | |
68 | +void send_power(int ufd, int down) | |
69 | +{ | |
70 | + struct input_event iev; | |
71 | + iev.type = EV_KEY; | |
72 | + iev.code = KEY_POWER; | |
73 | + iev.value = down; | |
74 | + write(ufd, &iev, sizeof(iev)); | |
75 | + iev.type = EV_SYN; | |
76 | + iev.code = SYN_REPORT; | |
77 | + iev.value = 0; | |
78 | + write(ufd, &iev, sizeof(iev)); | |
79 | +} | |
80 | + | |
81 | +void simulate_powerkey(int ufd, int longpress) | |
82 | +{ | |
83 | + send_power(ufd, 1); | |
84 | + if (longpress) | |
85 | + sleep(2); | |
86 | + send_power(ufd, 0); | |
87 | +} | |
88 | + | |
67 | 89 | int main() |
68 | 90 | { |
69 | 91 | struct pollfd pfds[MAX_POWERBTNS]; |
70 | 92 | int cnt = openfds(pfds); |
71 | - int pollres; | |
93 | + int timeout = -1; | |
94 | + char prop[PROPERTY_VALUE_MAX]; | |
72 | 95 | |
73 | 96 | int ufd = open("/dev/uinput", O_WRONLY | O_NDELAY); |
74 | 97 | if (ufd >= 0) { |
@@ -84,12 +107,21 @@ int main() | ||
84 | 107 | return -1; |
85 | 108 | } |
86 | 109 | |
87 | - while ((pollres = poll(pfds, cnt, -1))) { | |
110 | + property_get("poweroff.doubleclick", prop, NULL); | |
111 | + | |
112 | + for (;;) { | |
88 | 113 | int i; |
114 | + int pollres = poll(pfds, cnt, timeout) ; | |
115 | + LOGV("pollres=%d %d\n", pollres, timeout); | |
89 | 116 | if (pollres < 0) { |
90 | 117 | LOGE("poll error: %s", strerror(errno)); |
91 | 118 | break; |
92 | 119 | } |
120 | + if (pollres == 0) { // timeout, send one power key | |
121 | + simulate_powerkey(ufd, 0); | |
122 | + timeout = -1; | |
123 | + continue; | |
124 | + } | |
93 | 125 | for (i = 0; i < cnt; ++i) { |
94 | 126 | if (pfds[i].revents & POLLIN) { |
95 | 127 | struct input_event iev; |
@@ -99,17 +131,14 @@ int main() | ||
99 | 131 | continue; |
100 | 132 | } |
101 | 133 | LOGV("type=%d scancode=%d value=%d from fd=%d", iev.type, iev.code, iev.value, pfds[i].fd); |
102 | - if (iev.type == EV_KEY) { | |
103 | - switch (iev.code) | |
104 | - { | |
105 | - case KEY_POWER: | |
106 | - if (!iev.value) | |
107 | - sleep(2); | |
108 | - break; | |
134 | + if (iev.type == EV_KEY && iev.code == KEY_POWER && !iev.value) { | |
135 | + if (prop[0] != '1' || timeout > 0) { | |
136 | + simulate_powerkey(ufd, 1); | |
137 | + timeout = -1; | |
138 | + } else { | |
139 | + timeout = 1000; // one second | |
109 | 140 | } |
110 | 141 | } |
111 | - | |
112 | - write(ufd, &iev, sizeof(iev)); | |
113 | 142 | } |
114 | 143 | } |
115 | 144 | } |