Skip to content

Commit bfae103

Browse files
committed
client: Use /dev/input/* to determine idle input detection on Linux.
This would also cover digitizers and bluetooth devices.
1 parent 36cd72b commit bfae103

File tree

1 file changed

+67
-101
lines changed

1 file changed

+67
-101
lines changed

client/hostinfo_unix.cpp

Lines changed: 67 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
// You should have received a copy of the GNU Lesser General Public License
1616
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
1717

18-
// XIdleTime:
19-
// Copyright (C) 2011 Universidade Federal de Campina Grande
20-
// Initial version: Magnus Henoch
21-
// Contributors: Danny Kukawka, Eivind Magnus Hvidevold
22-
// LGPL Version of xidletime: https://github.com/rodrigods/xidletime
23-
2418
// There is a reason that having a file called "cpp.h" that includes config.h
2519
// and some of the C++ header files is bad. That reason is because there are
2620
// #defines that alter the behiour of the standard C and C++ headers. In
@@ -1627,7 +1621,6 @@ vector<string> get_tty_list() {
16271621
} while (tty_patterns[i].dir != NULL);
16281622
return tty_list;
16291623
}
1630-
16311624

16321625
inline bool all_tty_idle(time_t t) {
16331626
static vector<string> tty_list;
@@ -1638,7 +1631,67 @@ inline bool all_tty_idle(time_t t) {
16381631
for (i=0; i<tty_list.size(); i++) {
16391632
// ignore errors
16401633
if (!stat(tty_list[i].c_str(), &sbuf)) {
1641-
// printf("%s %d %d\n",tty_list[i].c_str(),sbuf.st_atime,t);
1634+
// printf("tty: %s %d %d\n",tty_list[i].c_str(),sbuf.st_atime,t);
1635+
if (sbuf.st_atime >= t) {
1636+
return false;
1637+
}
1638+
}
1639+
}
1640+
return true;
1641+
}
1642+
1643+
static const struct dir_dev {
1644+
const char *dir;
1645+
const char *dev;
1646+
} input_patterns[] = {
1647+
#ifdef unix
1648+
{ "/dev/input","event" },
1649+
{ "/dev/input","mouse" },
1650+
{ "/dev/input/mice","" },
1651+
#endif
1652+
// add other ifdefs here as necessary.
1653+
{ NULL, NULL },
1654+
};
1655+
1656+
vector<string> get_input_list() {
1657+
// Create a list of all terminal devices on the system.
1658+
char devname[1024];
1659+
char fullname[1024];
1660+
int done,i=0;
1661+
vector<string> input_list;
1662+
1663+
do {
1664+
DIRREF dev=dir_open(input_patterns[i].dir);
1665+
if (dev) {
1666+
do {
1667+
// get next file
1668+
done=dir_scan(devname,dev,1024);
1669+
// does it match our tty pattern? If so, add it to the tty list.
1670+
if (!done && (strstr(devname,input_patterns[i].dev) == devname)) {
1671+
// don't add anything starting with .
1672+
if (devname[0] != '.') {
1673+
sprintf(fullname,"%s/%s",input_patterns[i].dir,devname);
1674+
input_list.push_back(fullname);
1675+
}
1676+
}
1677+
} while (!done);
1678+
dir_close(dev);
1679+
}
1680+
i++;
1681+
} while (input_patterns[i].dir != NULL);
1682+
return input_list;
1683+
}
1684+
1685+
inline bool all_input_idle(time_t t) {
1686+
static vector<string> input_list;
1687+
struct stat sbuf;
1688+
unsigned int i;
1689+
1690+
if (input_list.size()==0) input_list=get_input_list();
1691+
for (i=0; i<input_list.size(); i++) {
1692+
// ignore errors
1693+
if (!stat(input_list[i].c_str(), &sbuf)) {
1694+
// printf("input: %s %d %d\n",input_list[i].c_str(),sbuf.st_atime,t);
16421695
if (sbuf.st_atime >= t) {
16431696
return false;
16441697
}
@@ -1865,91 +1918,6 @@ bool interrupts_idle(time_t t) {
18651918
}
18661919
return last_irq < t;
18671920
}
1868-
1869-
#if HAVE_XSS
1870-
// Ask the X server for user idle time (using XScreenSaver API)
1871-
// Return true if the idle time exceeds idle_threshold.
1872-
//
1873-
bool xss_idle(long idle_threshold) {
1874-
static XScreenSaverInfo* xssInfo = NULL;
1875-
static Display* disp = NULL;
1876-
static bool error = false;
1877-
// some X call failed - always return not idle
1878-
1879-
if (error) return false;
1880-
1881-
long idle_time = 0;
1882-
1883-
if (disp == NULL) {
1884-
disp = XOpenDisplay(NULL);
1885-
// XOpenDisplay may return NULL if there is no running X
1886-
// or DISPLAY points to wrong/invalid display
1887-
//
1888-
if (disp == NULL) {
1889-
error = true;
1890-
return false;
1891-
}
1892-
int event_base_return, error_base_return;
1893-
xssInfo = XScreenSaverAllocInfo();
1894-
if (!XScreenSaverQueryExtension(
1895-
disp, &event_base_return, &error_base_return
1896-
)){
1897-
error = true;
1898-
return false;
1899-
}
1900-
}
1901-
1902-
XScreenSaverQueryInfo(disp, DefaultRootWindow(disp), xssInfo);
1903-
idle_time = xssInfo->idle;
1904-
1905-
#if HAVE_DPMS
1906-
// XIdleTime Detection
1907-
// See header for location and copywrites.
1908-
//
1909-
int dummy;
1910-
CARD16 standby, suspend, off;
1911-
CARD16 state;
1912-
BOOL onoff;
1913-
1914-
if (DPMSQueryExtension(disp, &dummy, &dummy)) {
1915-
if (DPMSCapable(disp)) {
1916-
DPMSGetTimeouts(disp, &standby, &suspend, &off);
1917-
DPMSInfo(disp, &state, &onoff);
1918-
1919-
if (onoff) {
1920-
switch (state) {
1921-
case DPMSModeStandby:
1922-
// this check is a littlebit paranoid, but be sure
1923-
if (idle_time < (unsigned) (standby * 1000)) {
1924-
idle_time += (standby * 1000);
1925-
}
1926-
break;
1927-
case DPMSModeSuspend:
1928-
if (idle_time < (unsigned) ((suspend + standby) * 1000)) {
1929-
idle_time += ((suspend + standby) * 1000);
1930-
}
1931-
break;
1932-
case DPMSModeOff:
1933-
if (idle_time < (unsigned) ((off + suspend + standby) * 1000)) {
1934-
idle_time += ((off + suspend + standby) * 1000);
1935-
}
1936-
break;
1937-
case DPMSModeOn:
1938-
default:
1939-
break;
1940-
}
1941-
}
1942-
}
1943-
}
1944-
#endif
1945-
1946-
// convert from milliseconds to seconds
1947-
//
1948-
idle_time = idle_time / 1000;
1949-
1950-
return idle_threshold < idle_time;
1951-
}
1952-
#endif // HAVE_XSS
19531921
#endif // LINUX_LIKE_SYSTEM
19541922

19551923
bool HOST_INFO::users_idle(bool check_all_logins, double idle_time_to_run) {
@@ -1976,16 +1944,14 @@ bool HOST_INFO::users_idle(bool check_all_logins, double idle_time_to_run) {
19761944
}
19771945

19781946
// Lets at least check the dev entries which should be correct for
1979-
// USB mice. The tty check will catch keyboards if they are entering
1980-
// data into a tty.
1981-
if (!device_idle(idle_time, "/dev/input/mice")) return false;
1982-
1983-
#if HAVE_XSS
1984-
if (!xss_idle((long)(idle_time_to_run * 60))) {
1947+
// USB keyboards and mice. If the linux kernel doc is correct it should
1948+
// also work for bluetooth input devices as well.
1949+
//
1950+
// See: https://www.kernel.org/doc/Documentation/input/input.txt
1951+
//
1952+
if (!all_input_idle(idle_time)) {
19851953
return false;
19861954
}
1987-
#endif
1988-
19891955
#else
19901956
// We should find out which of the following are actually relevant
19911957
// on which systems (if any)

0 commit comments

Comments
 (0)