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 }
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; }
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 (); }
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); }
int main(int argc, char *argv[]) { XScreenSaverInfo *info = XScreenSaverAllocInfo(); Display *display = XOpenDisplay(NULL); //empty argument means to use $DISPLAY variable unsigned long int time_idle; int time_pws = 10*60*1000; // time to wait to go in power save (10 minutes) if(fork() == 0) { while(1) { time_idle = get_idle(info,display,0); // get idle time in milliseconds printf("%lu ms\n", time_idle); if (time_idle < time_pws ) { sleep((time_pws - time_idle)/1000 + 5); // add 5 seconds of delay to prevent very little difference } else { // GO IN POWER SAVE MODE printf("GO IN POWER SAVE %lu ms\n", time_idle); set_powersave(1); setpriority(PRIO_PROCESS, 0, -15); // change priority to low while (time_idle >= time_pws ) { // check until we have to exit from the power saving mode sleep(5); time_idle = get_idle(info,display,1); } setpriority(PRIO_PROCESS, 0, 0); // change priority to normal // EXIT FROM POWER SAVE MODE printf("EXIT FROM POWER SAVE %lu ms\n", time_idle); set_powersave(0); } } } return 0; }
/** * 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; }
HippoIdleMonitor* hippo_idle_add (GdkDisplay *display, HippoDataCache *cache, HippoIdleChangedFunc func, void *data) { int event_base, error_base; Display *xdisplay; HippoIdleMonitor *monitor; xdisplay = GDK_DISPLAY_XDISPLAY(display); if (!XScreenSaverQueryExtension(xdisplay, &event_base, &error_base)) { g_warning("Screensaver extension not found on X display, can't detect user idleness"); return NULL; } monitor = g_new0(HippoIdleMonitor, 1); monitor->display = g_object_ref(display); monitor->cache = g_object_ref(cache); monitor->info = XScreenSaverAllocInfo(); monitor->activity_time = get_time(); monitor->last_idle = 0; monitor->currently_idle = FALSE; monitor->func = func; monitor->data = data; monitor->poll_id = g_timeout_add(5000, poll_for_idleness, monitor); monitor->applications_by_app_id = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)g_free); monitor->applications_by_wm_class = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)g_free); return monitor; }
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 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 XScreensaverBasedPoller::getIdleTime() { XScreenSaverInfo *mitInfo = 0; mitInfo = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(QX11Info::display(), DefaultRootWindow(QX11Info::display()), mitInfo); int ret = mitInfo->idle; XFree(mitInfo); return ret; }
XSSIdleQuerier::XSSIdleQuerier() : display(NULL), info(NULL) { display = XOpenDisplay(NULL); assert(display); rootWindow = DefaultRootWindow(display); int event, error; available = XScreenSaverQueryExtension(display, &event, &error); if (available) { info = XScreenSaverAllocInfo(); } else { std::cerr << "Warning: XScreenSaver extension not found. Idle time detection will not work." << std::endl; } }
Idle::Idle() { if (ss_info == 0) { old_handler = XSetErrorHandler(xerrhandler); int event_base, error_base; if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) { ss_info = XScreenSaverAllocInfo(); } } }
int det_user_interrupt(const int sample_period, const int debounces, bool verbose){ //const static int TIME_DIFF_MSECONDS = 0.5; int time1; int time2; int diff; int counter = 0; //sleep(5); //if(putenv("DISPLAY=:0") != 0){ // printf("putenv() error: %s\n", strerror(errno)); // return -1; //} Display *dpy = XOpenDisplay(NULL); if (!dpy) { if(verbose) printf("err: display NULL\n"); return(-1); } XScreenSaverInfo *info = XScreenSaverAllocInfo(); if (!info) { if(verbose) printf("err: info NULL\n"); return(-1); } while(1){ //pierwszy pomiar time1 = GetIdleTime(dpy, info); //przerwa usleep(sample_period*1000); //drugi pomiar time2 = GetIdleTime(dpy, info); //byl ruch diff = time2 - time1; if(diff < 0){ counter = counter + 1; if(verbose) printf("counter set to: %d\n", counter); } if(counter == debounces) break; } if(verbose) printf("loop broken...\n"); if(verbose) printf("time1 is: %d\n", time1); if(verbose) printf("time2 is: %d\n", time2); if(verbose) printf("diff is: %d\n", diff); if(dpy != NULL) XFree(dpy); if(info != NULL) XFree(info); if(verbose) printf("cleaned...\n"); return 0; }
bool IdlePlatform::init() { if(d->ss_info) return true; old_handler = XSetErrorHandler(xerrhandler); int event_base, error_base; if(XScreenSaverQueryExtension(X11Info::display(), &event_base, &error_base)) { d->ss_info = XScreenSaverAllocInfo(); return true; } return false; }
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; }
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(){ 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 }
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; }
bool IdlePlatform::init() const { if(d->ss_info) return true; old_handler = XSetErrorHandler(xerrhandler); int event_base, error_base; #if (QT_VERSION < 0x050000) if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) { d->ss_info = XScreenSaverAllocInfo(); return true; } #else // FIXME: Need a replacement for this? #endif return false; }
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(); }
void initfn() { int event_basep, error_basep; dpy = XOpenDisplay(NULL); if (NULL == dpy) { release(); return; } if (!XScreenSaverQueryExtension(dpy, &event_basep, &error_basep)) { release(); return; } ssi = XScreenSaverAllocInfo(); if (ssi == NULL) { release(); return; } }
EAPI int ecore_x_screensaver_idle_time_get(void) { #ifdef ECORE_XSS XScreenSaverInfo *xss; int idle; LOGFN(__FILE__, __LINE__, __FUNCTION__); xss = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(_ecore_x_disp, RootWindow(_ecore_x_disp, DefaultScreen( _ecore_x_disp)), xss); idle = xss->idle / 1000; XFree(xss); return idle; #else return 0; #endif /* ifdef ECORE_XSS */ }
/* * Class: net_java_sip_communicator_impl_sysactivity_SystemActivityNotifications * Method: getLastInput * Signature: ()J */ JNIEXPORT jlong JNICALL Java_net_java_sip_communicator_impl_sysactivity_SystemActivityNotifications_getLastInput (JNIEnv* jniEnv, jclass clazz) { static XScreenSaverInfo *mit_info = NULL; static int has_extension = -1; int event_base, error_base; if(GDK_DISPLAY()) has_extension = XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base); if (has_extension != -1) { mit_info = XScreenSaverAllocInfo(); XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info); return (mit_info->idle); } else return 0; }
static gboolean drw_monitor_setup (DrwMonitor *monitor) { DrwMonitorPriv *priv; int event_base; int error_base; priv = monitor->priv; if (!XScreenSaverQueryExtension (GDK_DISPLAY (), &event_base, &error_base)) { return FALSE; } priv->ss_info = XScreenSaverAllocInfo (); priv->last_activity = time (NULL); priv->timeout_id = g_timeout_add_seconds (3, (GSourceFunc) drw_monitor_timeout, monitor); return TRUE; }