diff --git a/client/hostinfo_unix.cpp b/client/hostinfo_unix.cpp index cec9401f5f7..194ef841b53 100644 --- a/client/hostinfo_unix.cpp +++ b/client/hostinfo_unix.cpp @@ -39,6 +39,7 @@ #include #include #include + #endif #ifdef __GLIBC__ @@ -179,6 +180,12 @@ extern "C" { // #if (defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)) && !defined(__HAIKU__) #define LINUX_LIKE_SYSTEM 1 +// not sure if that's the correct place to put it +#include +#include +#include +// #include || library already included +#include #endif #if WASM @@ -2003,6 +2010,75 @@ const vector X_display_values_initialize() { return display_values; } +void getIdleTime() { + long idleTime = 0; + // Use glob to enumerate input devices in /dev/input/ + glob_t globbuf; + if (glob("/dev/input/event*", GLOB_NOSORT, nullptr, &globbuf) != 0) { + std::cerr << "Failed to enumerate input devices. " << std::endl; + return; + } + + // Create libevdev structures for each device + libevdev* devices[globbuf.gl_pathc]; + + // Open and initialize each device + for (size_t i = 0; i < globbuf.gl_pathc; ++i) { + const char* devicePath = globbuf.gl_pathv[i]; + int fd = open(devicePath, O_RDONLY | O_NONBLOCK); + if (fd < 0) { + std::cerr << "Failed to open device (Permission denied?): " << devicePath << std::endl; + return; + } + + if (libevdev_new_from_fd(fd, &devices[i]) < 0) { + std::cerr << "Failed to initialize libevdev for device: " << devicePath << std::endl; + return; + } + } + + // Main loop to monitor input events + while (true) { + bool systemInUse = false; + + // Read events from all devices + for (size_t i = 0; i < globbuf.gl_pathc; ++i) { + struct input_event ev; + int rc; + + while ((rc = libevdev_next_event(devices[i], LIBEVDEV_READ_FLAG_NORMAL, &ev)) == 1) { + // Handle input events as needed + // For this example, we simply print event information + std::cout << "Event type: " << ev.type << ", code: " << ev.code << ", value: " << ev.value << std::endl; + + // Set systemInUse to true if an event is detected + systemInUse = true; + } + + if (rc < 0 && rc != -EAGAIN) { + std::cerr << "Error reading from device: " << globbuf.gl_pathv[i] << std::endl; + return; + } + } + if (systemInUse) { + std::cout << "System is being used." << std::endl; + idleTime = 0; + } else { + std::cout << "System is not being used." << std::endl; + idleTime += 5; + } + // You can add a sleep here to reduce CPU usage + sleep(5); + printf("Idle time: %ld\n", idleTime); + } + for (size_t i = 0; i < globbuf.gl_pathc; ++i) { + libevdev_free(devices[i]); + } + globfree(&globbuf); + + return; +} + // Ask the X server for user idle time (using XScreenSaver API) // Return min of idle times. // This function assumes that the boinc user has been @@ -2011,6 +2087,7 @@ const vector X_display_values_initialize() { // One may drop a file in /etc/X11/Xsession.d/ that runs the xhost command // for all Xservers on a machine when the Xservers start up. // + long xss_idle() { long idle_time = USER_IDLE_TIME_INF; const vector display_values = X_display_values_initialize();