Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a method to patch linux idle check with required libraries. #5354

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get -qq update
sudo apt-get install -y libftgl-dev freeglut3-dev libcurl4-openssl-dev libxmu-dev libxi-dev libfcgi-dev libxss-dev libnotify-dev libxcb-util0-dev libgtk-3-dev libsecret-1-dev libgcrypt20-dev libsystemd-dev libwebkit2gtk-4.0-dev p7zip-full libxxf86vm-dev ocl-icd-opencl-dev zip
sudo apt-get install -y libftgl-dev freeglut3-dev libcurl4-openssl-dev libxmu-dev libxi-dev libfcgi-dev libxss-dev libnotify-dev libxcb-util0-dev libgtk-3-dev libsecret-1-dev libgcrypt20-dev libsystemd-dev libwebkit2gtk-4.0-dev p7zip-full libxxf86vm-dev ocl-icd-opencl-dev zip libevdev-dev

- name: Install dependencies for arm64
if: success() && endsWith(matrix.type, 'arm64')
Expand Down
87 changes: 87 additions & 0 deletions client/hostinfo_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ extern "C" {
#define LINUX_LIKE_SYSTEM 1
#endif

#if (defined(__linux__) && !defined(__HAIKU__) && !defined(__ANDROID__))
#include <glob.h>
#include <libevdev-1.0/libevdev/libevdev.h>
#include <fcntl.h>
// variables for getting linux IDLE time on linux.
size_t deviceCount = 0;
libevdev* devices[256]; // Assuming a maximum of 256 input devices
time_t linuxUserLastSeen = time(nullptr);
#endif

#if WASM
#include <emscripten.h>
#endif
Expand Down Expand Up @@ -2003,6 +2013,83 @@ const vector<string> X_display_values_initialize() {
return display_values;
}

// Function to initialize libevdev for all input devices in /dev/input/ on linux systems
// call example:
// long linuxIdleTimeInSeconds = checkLinuxInputEventsAndGetIdleTime(&linuxUserLastSeen);
bool initializeLinuxInputDevices(libevdev** devices, size_t* deviceCount) {
// Use glob to enumerate input devices in /dev/input/
glob_t globbuf;
if (glob("/dev/input/event*", GLOB_NOSORT, nullptr, &globbuf) != 0) {
msg_printf(NULL, MSG_INFO,
"[idle_detection] Failed to enumerate input devices."
);
return false;
}

*deviceCount = globbuf.gl_pathc;
// Open and initialize each device
for (size_t i = 0; i < *deviceCount; ++i) {
const char* devicePath = globbuf.gl_pathv[i];
int fd = open(devicePath, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
msg_printf(NULL, MSG_INFO,
"[idle_detection] Failed to open device: %s", devicePath
);
return false;
}

if (libevdev_new_from_fd(fd, &devices[i]) < 0) {
msg_printf(NULL, MSG_INFO,
"[idle_detection] Failed to initialize libevdev for device: %s", devicePath
);
return false;
}
}

// Free the glob buffer
globfree(&globbuf);

return true;
}

// Function to check input events and calculate idle time for linux systems
long checkLinuxInputEventsAndGetIdleTime(libevdev** devices, size_t deviceCount, time_t* lastSeen) {
long idleTime = 0;
bool systemInUse = false;

// Read events from all devices
for (size_t i = 0; i < deviceCount; ++i) {
struct input_event ev;
int rc;

while ((rc = libevdev_next_event(devices[i], LIBEVDEV_READ_FLAG_NORMAL, &ev)) >= 0) {
// Set systemInUse to true if an event is detected
systemInUse = true;
}

if (rc < 0 && rc != -EAGAIN) {
return 0;
}
}

if (systemInUse) {
*lastSeen = time(nullptr);
idleTime = 0;
} else {
idleTime = time(nullptr) - *lastSeen;
}

return idleTime;
}

void cleanupLinuxInputDevices(libevdev** devices, size_t deviceCount) {
// Close and free libevdev structures
for (size_t i = 0; i < deviceCount; ++i) {
libevdev_free(devices[i]);
}
}


// 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 Down
15 changes: 15 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,21 @@ dnl ======================================================================

CLIENTLIBS=

AC_COMPILE_IFELSE( [AC_LANG_PROGRAM([[#include <libevdev-1.0/libevdev/libevdev.h>]],
[[

]])],
[
AC_MSG_RESULT([yes])
CLIENTLIBS="$CLIENTLIBS -levdev"
],
[
AC_MSG_RESULT([no])
]
)



SAH_CHECK_LIB([m],[sin], [
AC_DEFINE([HAVE_LIBM],[1],[Define to 1 if you have the math library])
CLIENTLIBS="${sah_lib_last} ${CLIENTLIBS}"])
Expand Down
5 changes: 5 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ parts:
- libxrandr-dev
- libdbus-1-dev
- libxtst-dev
- libevdev-dev
- libevdev2
# vcpkg dependencies
- build-essential
- pkg-config
Expand Down Expand Up @@ -102,6 +104,9 @@ parts:
export PATH=/snap/cmake/current/bin/:$PATH
cmake --version

# to recongize libevdev
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/

# aws
ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
sudo add-apt-repository ppa:lizthegrey/misc
Expand Down
Loading