Skip to content

Commit 1a1fc6c

Browse files
committed
adding a method to patch linux idle check with required libraries.
1 parent eac3e3a commit 1a1fc6c

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

client/hostinfo_unix.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <vector>
4040
#include <string>
4141
#include <cstring>
42+
4243
#endif
4344

4445
#ifdef __GLIBC__
@@ -179,6 +180,12 @@ extern "C" {
179180
//
180181
#if (defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)) && !defined(__HAIKU__)
181182
#define LINUX_LIKE_SYSTEM 1
183+
// not sure if that's the correct place to put it
184+
#include <libevdev/libevdev.h>
185+
#include <libevdev/libevdev-uinput.h>
186+
#include <fcntl.h>
187+
// #include <unistd.h> || library already included
188+
#include <glob.h>
182189
#endif
183190

184191
#if WASM
@@ -2003,6 +2010,75 @@ const vector<string> X_display_values_initialize() {
20032010
return display_values;
20042011
}
20052012

2013+
void getIdleTime() {
2014+
long idleTime = 0;
2015+
// Use glob to enumerate input devices in /dev/input/
2016+
glob_t globbuf;
2017+
if (glob("/dev/input/event*", GLOB_NOSORT, nullptr, &globbuf) != 0) {
2018+
std::cerr << "Failed to enumerate input devices. " << std::endl;
2019+
return;
2020+
}
2021+
2022+
// Create libevdev structures for each device
2023+
libevdev* devices[globbuf.gl_pathc];
2024+
2025+
// Open and initialize each device
2026+
for (size_t i = 0; i < globbuf.gl_pathc; ++i) {
2027+
const char* devicePath = globbuf.gl_pathv[i];
2028+
int fd = open(devicePath, O_RDONLY | O_NONBLOCK);
2029+
if (fd < 0) {
2030+
std::cerr << "Failed to open device (Permission denied?): " << devicePath << std::endl;
2031+
return;
2032+
}
2033+
2034+
if (libevdev_new_from_fd(fd, &devices[i]) < 0) {
2035+
std::cerr << "Failed to initialize libevdev for device: " << devicePath << std::endl;
2036+
return;
2037+
}
2038+
}
2039+
2040+
// Main loop to monitor input events
2041+
while (true) {
2042+
bool systemInUse = false;
2043+
2044+
// Read events from all devices
2045+
for (size_t i = 0; i < globbuf.gl_pathc; ++i) {
2046+
struct input_event ev;
2047+
int rc;
2048+
2049+
while ((rc = libevdev_next_event(devices[i], LIBEVDEV_READ_FLAG_NORMAL, &ev)) == 1) {
2050+
// Handle input events as needed
2051+
// For this example, we simply print event information
2052+
std::cout << "Event type: " << ev.type << ", code: " << ev.code << ", value: " << ev.value << std::endl;
2053+
2054+
// Set systemInUse to true if an event is detected
2055+
systemInUse = true;
2056+
}
2057+
2058+
if (rc < 0 && rc != -EAGAIN) {
2059+
std::cerr << "Error reading from device: " << globbuf.gl_pathv[i] << std::endl;
2060+
return;
2061+
}
2062+
}
2063+
if (systemInUse) {
2064+
std::cout << "System is being used." << std::endl;
2065+
idleTime = 0;
2066+
} else {
2067+
std::cout << "System is not being used." << std::endl;
2068+
idleTime += 5;
2069+
}
2070+
// You can add a sleep here to reduce CPU usage
2071+
sleep(5);
2072+
printf("Idle time: %ld\n", idleTime);
2073+
}
2074+
for (size_t i = 0; i < globbuf.gl_pathc; ++i) {
2075+
libevdev_free(devices[i]);
2076+
}
2077+
globfree(&globbuf);
2078+
2079+
return;
2080+
}
2081+
20062082
// Ask the X server for user idle time (using XScreenSaver API)
20072083
// Return min of idle times.
20082084
// This function assumes that the boinc user has been
@@ -2011,6 +2087,7 @@ const vector<string> X_display_values_initialize() {
20112087
// One may drop a file in /etc/X11/Xsession.d/ that runs the xhost command
20122088
// for all Xservers on a machine when the Xservers start up.
20132089
//
2090+
20142091
long xss_idle() {
20152092
long idle_time = USER_IDLE_TIME_INF;
20162093
const vector<string> display_values = X_display_values_initialize();

0 commit comments

Comments
 (0)