Skip to content

Commit

Permalink
adding a method to patch linux idle check with required libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0OZ committed Sep 9, 2023
1 parent eac3e3a commit 1a1fc6c
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions client/hostinfo_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <vector>
#include <string>
#include <cstring>

#endif

#ifdef __GLIBC__
Expand Down Expand Up @@ -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 <libevdev/libevdev.h>
#include <libevdev/libevdev-uinput.h>
#include <fcntl.h>
// #include <unistd.h> || library already included
#include <glob.h>
#endif

#if WASM
Expand Down Expand Up @@ -2003,6 +2010,75 @@ const vector<string> 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
Expand All @@ -2011,6 +2087,7 @@ const vector<string> 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<string> display_values = X_display_values_initialize();
Expand Down

0 comments on commit 1a1fc6c

Please sign in to comment.