int main(void) { Display *dpy = XOpenDisplay(NULL); if (!dpy) { return(1); } /* bool _idleDetectionPossible; int event_base, error_base; if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) _idleDetectionPossible = true; else _idleDetectionPossible = false printf("%u\n", _idleDetectionPossible); */ while(true) { XScreenSaverInfo *info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info); unsigned int microseconds = 2000000; usleep(microseconds); printf("%u\n", (info->idle + 500) / 1000); } return(0); }
const wxTimeSpan IdleDetector::GetIdleTime() { #ifdef WIN32 LASTINPUTINFO info; info.cbSize = sizeof(info); if (::GetLastInputInfo (&info)) { long timeIdle = ::GetTickCount() - info.dwTime; // Handle overflow if (timeIdle < 0) timeIdle = timeIdle + UINT_MAX + 1; return wxTimeSpan(0, 0, 0, timeIdle); } #else if( screen_info == NULL ) { screen_info = XScreenSaverAllocInfo(); display = XOpenDisplay(0); } XScreenSaverQueryInfo(display, DefaultRootWindow(display), screen_info); return wxTimeSpan( 0, 0, 0, screen_info->idle ); #endif return wxTimeSpan (); }
unsigned long int get_idle(XScreenSaverInfo *info, Display *display, int prev_state) { // avg cpu load in the last 1,5,15 minutes // details here http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages double load[3]; double t1=0.5; double t2=4; if (getloadavg(load, 3) != -1) { // if we are not in idle the limit is 0.5 if (prev_state == 0 && load[0] > t1) return 0; // if we are in idle we are at 200MHz thus if all the cores are full loaded // we have to exit from idle state if (prev_state == 1 && load[0] > t2) return 0; } //display could be null if there is no X server running if (info == NULL || display == NULL) return -1; //X11 is running, try to retrieve info if (XScreenSaverQueryInfo(display, DefaultRootWindow(display), info) == 0) return -1; return info->idle; }
bool XInterface::QuerySSExtension() { #ifdef HAVE_XSS static XScreenSaverInfo *xssi = 0; if (needsCheck) { needsCheck = false; int notused = 0; hasXss = XScreenSaverQueryExtension(_display, ¬used, ¬used); dprintf(D_ALWAYS, "X server %s have the screen saver extension\n", hasXss ? "does" : "does not"); } if (hasXss) { if (!xssi) { xssi = XScreenSaverAllocInfo(); } XScreenSaverQueryInfo(_display,DefaultRootWindow(_display), xssi); dprintf(D_FULLDEBUG, "Screen Saver extension claims idle time is %ld ms\n", xssi->idle); if (xssi->idle < 20000l) { return true; } } return false; #else return false; #endif }
/** * Get the number of seconds since the last time the user * sent an input to the system. */ int platformIdleTime( ) { time_t idle_time; static XScreenSaverInfo *mit_info; Display *display; int screen; /* Allocate screensaver info */ mit_info = XScreenSaverAllocInfo(); if (mit_info == NULL) return HVE_EXTERNAL_ERROR; /* Open display */ if ((display=XOpenDisplay(NULL)) == NULL) return HVE_EXTERNAL_ERROR; /* Fetch default screen */ screen = DefaultScreen(display); /* Query info */ if (XScreenSaverQueryInfo(display, RootWindow(display,screen), mit_info) == 0) return HVE_NOT_SUPPORTED; /* Calculate idle time in seconds */ idle_time = (mit_info->idle) / 1000; /* Cleanup */ XFree(mit_info); XCloseDisplay(display); return idle_time; }
long Tk_GetUserInactiveTime( Display *dpy) /* The display for which to query the inactive * time. */ { long inactiveTime = -1; #ifdef HAVE_XSS int eventBase, errorBase, major, minor; /* * Calling XScreenSaverQueryVersion seems to be needed to prevent a crash * on some buggy versions of XFree86. */ if (HaveXSSLibrary() && XScreenSaverQueryExtension(dpy, &eventBase, &errorBase) && XScreenSaverQueryVersion(dpy, &major, &minor)) { XScreenSaverInfo *info = XScreenSaverAllocInfo(); if (info == NULL) { /* * We are out of memory. */ Tcl_Panic("Out of memory: XScreenSaverAllocInfo failed in Tk_GetUserInactiveTime"); } if (XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info)) { inactiveTime = info->idle; } XFree(info); } #endif /* HAVE_XSS */ return inactiveTime; }
uint32_t Platform::getIdleTime() { uint32_t idleTime = 0; Display *display = XOpenDisplay(NULL); if (!display) { qDebug() << "XOpenDisplay(NULL) failed"; return 0; } int32_t x11event = 0, x11error = 0; static int32_t hasExtension = XScreenSaverQueryExtension(display, &x11event, &x11error); if (hasExtension) { XScreenSaverInfo *info = XScreenSaverAllocInfo(); if (info) { XScreenSaverQueryInfo(display, DefaultRootWindow(display), info); idleTime = info->idle; XFree(info); } else qDebug() << "XScreenSaverAllocInfo() failed"; } XCloseDisplay(display); return idleTime; }
JNIEXPORT jlong JNICALL Java_com_jivesoftware_idlelinux_LinuxIdle_idleTime(JNIEnv *, jclass) { if(display == NULL) { display = XOpenDisplay(NULL); if (display == NULL) return 0; } if (window == 0) { window = DefaultRootWindow(display); if (window == 0) return 0; } int event_base, error_base; if (XScreenSaverQueryExtension(display, &event_base, &error_base)) { if (mit_info == NULL) { mit_info = XScreenSaverAllocInfo(); if (mit_info == NULL) return 0; } XScreenSaverQueryInfo(display, window, mit_info); // Return the idle time in milliseconds as type long return mit_info->idle; } else { return 0; } }
int IdlePlatform::secondsIdle() { if (!d->ss_info) return 0; if (!XScreenSaverQueryInfo(QApplication::desktop()->screen()->x11Display(), QX11Info::appRootWindow(), d->ss_info)) return 0; return d->ss_info->idle / 1000; }
int IdlePlatform::secondsIdle() { if(!d->ss_info) return 0; if(!XScreenSaverQueryInfo(X11Info::display(), X11Info::appRootWindow(), d->ss_info)) return 0; return d->ss_info->idle / 1000; }
int main(int argc, char* argv[]) { parse_options(argc, argv); // Daemonize, unless we're passed -d if(daemonize) { pid_t pid = fork(); if(pid < 0) { fprintf(stderr, "Fork failed with %d\n", pid); } else if(pid > 0) { return EXIT_SUCCESS; // child has forked off correctly, we terminate immediately. } } // we will be sent a USR1 by acpid when the power adapter is plugged/unplugged. at that point we reread its state. // this approach saves us from having to poll that every few seconds to see if it's changed // (one of the few places where it's practical to avoid polling) signal(SIGUSR1, refresh_adapter_state); XScreenSaverInfo* info = XScreenSaverAllocInfo(); info->idle = 0; // ensure this is initialised since we'll calculate on it shortly Display* display = XOpenDisplay(0); if(display == NULL) { fprintf(stderr, "Couldn't connect to X display\n"); return EXIT_FAILURE; } // do some initial updates to make sure everything's been read at first // this will set the initial brightness values for us as well refresh_adapter_state(); update_light_sensor(); // NB. ideally we would use select() or something to wait for the applesmc sysfs entry // to change, but it doesn't seem to work. Not sure that's possible on this kind of hardware sensor? while(1) { // we've just gone idle. wait in 2 second chunks to keep checking the light sensor int i; for(i = 0; i < time_before_dim * 1000 - info->idle; i += 2000) { sleep(2); update_light_sensor(); dprintf("Time until dimming planned to begin: %ld\n", time_before_dim - info->idle/1000 - i/1000); } // now check the idle time again XScreenSaverQueryInfo(display, DefaultRootWindow(display), info); if(info->idle < time_before_dim*1000) { // we must have been woken in between. go back to waiting. continue; } // here we have waited the requisite amount of time. dim the display. dprintf("Dimming display\n"); if(!continuous_dim_backlight(display, info)) { wait_for_event(display, info); } // once we get here, we are undimming because something's happened is_dimmed = 0; adjust_brightness(1.0); } }
int XSSIdleQuerier::getIdleTimeSeconds() { if (available) { XScreenSaverQueryInfo(display, rootWindow, info); return info->idle / 1000; } else { return 0; } }
float get_session_idle_time(Display *display) { XScreenSaverInfo info; float seconds; XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info); seconds = (float)info.idle/1000.0f; return(seconds); }
int XScreensaverBasedPoller::getIdleTime() { XScreenSaverInfo *mitInfo = 0; mitInfo = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(QX11Info::display(), DefaultRootWindow(QX11Info::display()), mitInfo); int ret = mitInfo->idle; XFree(mitInfo); return ret; }
int Idle::secondsIdle() { if(ss_info == 0) return -1; if (!XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), ss_info)) return -1; return ss_info->idle / 1000; }
long get() { if (NULL == dpy || NULL == ssi) { return -2; } if (!XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), ssi)) { return -1; } return workaroundCreepyXServer(dpy, ssi->idle); }
std::chrono::milliseconds Xidle::operator()() const { XScreenSaverInfo ssi; if (!XScreenSaverQueryInfo(display, DefaultRootWindow(display), &ssi)) { throw std::runtime_error("could not get screen saver info"); } auto milliseconds = workaroundCreepyXServer(display, ssi.idle); return std::chrono::milliseconds{milliseconds}; }
long GtkUIBinding::GetIdleTime() { Display *display = gdk_x11_get_default_xdisplay(); if (display == NULL) return -1; int screen = gdk_x11_get_default_screen(); XScreenSaverInfo *mit_info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(display, RootWindow(display, screen), mit_info); long idle_time = mit_info->idle; XFree(mit_info); return idle_time; }
int IdlePlatform::secondsIdle() const { if(!d->ss_info) return 0; #if (QT_VERSION < 0x050000) if(!XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), d->ss_info)) return 0; #else // FIXME: Need a replacement for this? return 0; #endif return d->ss_info->idle / 1000; }
unsigned AutoAwayPlugin::getIdleTime() { #ifdef WIN32 unsigned idle_time = 0; if (_GetLastInputInfo == NULL){ POINT p; GetCursorPos(&p); time_t now; time(&now); if ((p.x != oldX) || (p.y != oldY)){ oldX = p.x; oldY = p.y; lastTime = now; } return now - lastTime; } LASTINPUTINFO lii; ZeroMemory(&lii,sizeof(lii)); lii.cbSize=sizeof(lii); _GetLastInputInfo(&lii); return (GetTickCount()-lii.dwTime) / 1000; #else QWidgetList *list = QApplication::topLevelWidgets(); QWidgetListIt it(*list); QWidget *w = it.current(); delete list; if (w == NULL) return 0; static XScreenSaverInfo *mit_info = NULL; if (mit_info == NULL) { int event_base, error_base; if(XScreenSaverQueryExtension(w->x11Display(), &event_base, &error_base)) { mit_info = XScreenSaverAllocInfo (); } } if (mit_info == NULL){ log(L_WARN, "No XScreenSaver extension found on current XServer, disabling auto-away."); m_timer->stop(); return 0; } if (!XScreenSaverQueryInfo(w->x11Display(), qt_xrootwin(), mit_info)) { log(L_WARN, "XScreenSaverQueryInfo failed, disabling auto-away."); m_timer->stop(); return 0; } return (mit_info->idle / 1000); #endif }
int main(int argc, char *argv[]) { XScreenSaverInfo info; Display *dpy = XOpenDisplay(NULL); if(NULL == dpy) { fprintf(stderr, "failed to open display\n"); return 1; } XScreenSaverQueryInfo(dpy, RootWindow(dpy, DefaultScreen(dpy)), &info); printf("%ld\n", info.idle / 1000); return 0; }
int main(){ Display *dis; XScreenSaverInfo *info; dis = XOpenDisplay((char *)0); Window win = DefaultRootWindow(dis); info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo( dis, win, info ); /* if the screensaver is on return -1, else return idle time */ if( info->state == ScreenSaverOn ){ printf( "-1" ); }else{ printf( "%d", info->idle/1000 ); } }
void IdleNotificationDialog::timeout() { Display *display = XOpenDisplay(NULL); if (!display) { return; } XScreenSaverInfo *info = XScreenSaverAllocInfo(); if (XScreenSaverQueryInfo(display, DefaultRootWindow(display), info)) { uint64_t idleSeconds = info->idle / 1000; TogglApi::instance->setIdleSeconds(idleSeconds); } XFree(info); XCloseDisplay(display); }
static void ExtInitSS(int available) { if (!available) return; if (EDebug(EDBUG_TYPE_VERBOSE)) { XScreenSaverInfo *xssi = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(disp, WinGetXwin(VROOT), xssi); Eprintf(" Screen saver window=%#lx\n", xssi->window); XFree(xssi); } XScreenSaverSelectInput(disp, WinGetXwin(VROOT), ScreenSaverNotifyMask | ScreenSaverCycleMask); }
void IdleTimeDetector::check() { kDebug(5970) << "Entering function"; #if defined(HAVE_LIBXSS) && defined(Q_WS_X11) kDebug(5970) << "kompiled for libxss and x11, idledetectionpossible is " << _idleDetectionPossible; if (_idleDetectionPossible) { _mit_info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), _mit_info); idleminutes = (_mit_info->idle/1000)/secsPerMinute; kDebug(5970) << "The desktop has been idle for " << idleminutes << " minutes."; kDebug(5970) << "The idle time in miliseconds is " << _mit_info->idle; if (idleminutes >= _maxIdle) informOverrun(); } #endif // HAVE_LIBXSS }
unsigned AutoAwayPlugin::getIdleTime() { #ifdef WIN32 if (_GetLastInputInfo){ LASTINPUTINFO lii; ZeroMemory(&lii,sizeof(lii)); lii.cbSize=sizeof(lii); _GetLastInputInfo(&lii); return (GetTickCount()-lii.dwTime) / 1000; } if (_IdleUIGetLastInputTime) return _IdleUIGetLastInputTime() / 1000; return 0; #else #ifdef HAVE_CARBON_CARBON_H return mSecondsIdle; #else QWidgetList *list = QApplication::topLevelWidgets(); QWidgetListIt it(*list); QWidget *w = it.current(); delete list; if (w == NULL) return 0; static XScreenSaverInfo *mit_info = NULL; if (mit_info == NULL) { int event_base, error_base; if(XScreenSaverQueryExtension(w->x11Display(), &event_base, &error_base)) { mit_info = XScreenSaverAllocInfo (); } } if (mit_info == NULL){ log(L_WARN, "No XScreenSaver extension found on current XServer, disabling auto-away."); m_timer->stop(); return 0; } if (!XScreenSaverQueryInfo(w->x11Display(), qt_xrootwin(), mit_info)) { log(L_WARN, "XScreenSaverQueryInfo failed, disabling auto-away."); m_timer->stop(); return 0; } return (mit_info->idle / 1000); #endif #endif }
// implements the gradual dimming of the backlight once the machine has gone idle int continuous_dim_backlight(Display* display, XScreenSaverInfo* info) { unsigned long initial_idle = info->idle; struct timespec tm_remaining = { 0, 0 }; struct timespec ten_milliseconds = { 0, 10000000 }; double proportion; for(proportion = 1.0; proportion >= 0.0; proportion -= 0.001) { XScreenSaverQueryInfo(display, DefaultRootWindow(display), info); if(info->idle < initial_idle) { // obviously we've come out of idle in the last sleep. bail here. return 1; } adjust_brightness(proportion); nanosleep(&ten_milliseconds, &tm_remaining); } adjust_brightness(0.0); is_dimmed = 1; return 0; }
unsigned long ui_get_idle_time(void) { // if compiled with libxss, get the x sessions idle time #ifdef HAVE_LIBXSS XScreenSaverInfo *info = XScreenSaverAllocInfo(); if (info != NULL && display != NULL) { XScreenSaverQueryInfo(display, DefaultRootWindow(display), info); unsigned long result = info->idle; XFree(info); return result; } // if no libxss or xss idle time failed, use profanity idle time #endif gdouble seconds_elapsed = g_timer_elapsed(ui_idle_time, NULL); unsigned long ms_elapsed = seconds_elapsed * 1000.0; return ms_elapsed; }
long idleTime() { long idlesecs; #ifdef LINUX bool _idleDetectionPossible; XScreenSaverInfo *_mit_info; int event_base, error_base; if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) _idleDetectionPossible = true; else _idleDetectionPossible = false; _mit_info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), _mit_info); idlesecs = (_mit_info->idle/1000); #endif #ifdef WINDOWS LASTINPUTINFO lif; lif.cbSize = sizeof(LASTINPUTINFO); GetLastInputInfo(&lif); DWORD tickCount = GetTickCount(); idlesecs = (tickCount - lif.dwTime) / 1000; #endif #ifdef MAC idlesecs = -1;//int64_t io_iterator_t iter = 0; if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) { io_registry_entry_t entry = IOIteratorNext(iter); if (entry) { CFMutableDictionaryRef dict = NULL; if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) { CFNumberRef obj = (CFNumberRef)CFDictionaryGetValue(dict, CFSTR("HIDIdleTime")); if (obj) { int64_t nanoseconds = 0; if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) { idlesecs = (nanoseconds >> 30); // Divide by 10^9 to convert from nanoseconds to seconds. } } CFRelease(dict); }
void X11IdleDetector::checkIdleness() { #if QT_VERSION < QT_VERSION_CHECK(5,0,0) XScreenSaverInfo* _mit_info = XScreenSaverAllocInfo(); if (!_mit_info) return; XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), _mit_info); const int idleSecs = _mit_info->idle / 1000; XFree(_mit_info); if (idleSecs >= idlenessDuration()) maybeIdle( IdlePeriod(QDateTime::currentDateTime().addSecs( -idleSecs ), QDateTime::currentDateTime() ) ); if ( m_heartbeat.secsTo( QDateTime::currentDateTime() ) > idlenessDuration() ) maybeIdle( IdlePeriod( m_heartbeat, QDateTime::currentDateTime() ) ); #endif m_heartbeat = QDateTime::currentDateTime(); }