diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index c4461df8f08..d9750dfae04 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -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') diff --git a/client/hostinfo_unix.cpp b/client/hostinfo_unix.cpp index cec9401f5f7..248c9da4c04 100644 --- a/client/hostinfo_unix.cpp +++ b/client/hostinfo_unix.cpp @@ -181,6 +181,16 @@ extern "C" { #define LINUX_LIKE_SYSTEM 1 #endif +#if (defined(__linux__) && !defined(__HAIKU__) && !defined(__ANDROID__)) +#include +#include +#include +// 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 #endif @@ -2003,6 +2013,83 @@ const vector 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 diff --git a/configure.ac b/configure.ac index 7fc01b960af..69da9a55232 100644 --- a/configure.ac +++ b/configure.ac @@ -1153,6 +1153,21 @@ dnl ====================================================================== CLIENTLIBS= +AC_COMPILE_IFELSE( [AC_LANG_PROGRAM([[#include ]], + [[ + + ]])], + [ + 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}"]) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 44593f6454e..6c922e7f52a 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -57,6 +57,8 @@ parts: - libxrandr-dev - libdbus-1-dev - libxtst-dev + - libevdev-dev + - libevdev2 # vcpkg dependencies - build-essential - pkg-config @@ -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