Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
Bool
query_mit_saver_extension (saver_info *si)
{
  return XScreenSaverQueryExtension (si->dpy,
				     &si->mit_saver_ext_event_number,
				     &si->mit_saver_ext_error_number);
}
Exemplo n.º 4
0
void
X11_SuspendScreenSaver(_THIS)
{
#if SDL_VIDEO_DRIVER_X11_SCRNSAVER
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    int dummy;
    int major_version, minor_version;

    if (SDL_X11_HAVE_XSS) {
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
            !XScreenSaverQueryVersion(data->display,
                                      &major_version, &minor_version) ||
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
            return;
        }

        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
        XResetScreenSaver(data->display);
    }
#endif

#ifdef GNOME_SCREENSAVER_HACK
    if (_this->suspend_screensaver) {
        gnome_screensaver_disable();
    } else {
        gnome_screensaver_enable();
    }
#endif
}
Exemplo n.º 5
0
bool
XInterface::QuerySSExtension()
{
#ifdef HAVE_XSS
	static XScreenSaverInfo *xssi = 0;

	if (needsCheck) {
		needsCheck = false;

		int notused = 0;
		hasXss = XScreenSaverQueryExtension(_display, &notused, &notused);
		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
}
Exemplo n.º 6
0
bool X11IdleDetector::idleCheckPossible()
{
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
    int event_base, error_base;
    if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base))
        return true;
#endif
    return false;
}
Exemplo n.º 7
0
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;
    }
}
Exemplo n.º 8
0
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();
		}
	}
}
Exemplo n.º 9
0
Xidle::Xidle()
{
  int event_basep, error_basep;

  display = XOpenDisplay(NULL);
  if (display == nullptr) {
    throw std::runtime_error("could not open display");
  }

  if (!XScreenSaverQueryExtension(display, &event_basep, &error_basep)) {
    throw std::runtime_error("screen saver extension not supported");
  }
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
IdleTimeDetector::IdleTimeDetector(int maxIdle)
{
    _maxIdle = maxIdle;

#if defined(HAVE_LIBXSS) && defined(Q_WS_X11)
    int event_base, error_base;
    if(XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) _idleDetectionPossible = true;
    else _idleDetectionPossible = false;
    _timer = new QTimer(this);
    connect(_timer, SIGNAL(timeout()), this, SLOT(check()));
#else
    _idleDetectionPossible = false;
#endif // HAVE_LIBXSS
}
Exemplo n.º 12
0
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
}
Exemplo n.º 13
0
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
}
Exemplo n.º 14
0
	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;
	}
Exemplo n.º 15
0
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);
            }
Exemplo n.º 16
0
int main(int argc, char *argv[])
{
    Display *display;
    int event_base, error_base;
    XScreenSaverInfo info;
    unsigned int ms;

    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s [DISPLAY] [Xauthority]\n", argv[0]);
        return 1;
    }

    setenv("DISPLAY", argv[1], 1);
    setenv("XAUTHORITY", argv[2], 2);
    display = XOpenDisplay("");

    if(display)
    {
        if (XScreenSaverQueryExtension(display, &event_base, &error_base))
        {
            XScreenSaverQueryInfo(display, DefaultRootWindow(display), &info);

            ms = (unsigned int)info.idle;
            printf("%u\n", ms);
            return(0);
        }
        else
        {
            fprintf(stderr,"Error: XScreenSaver Extension not present\n");
            return(1);

        }
    }
    else
    {
        fprintf(stderr,"Error: Invalid Display\n");
        return(1);
    }

    return 0;
}
Exemplo n.º 17
0
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;
    }
}
Exemplo n.º 18
0
int
main(int argc, char *argv[])
{
    XScreenSaverInfo info;
    Display *dpy = XOpenDisplay(NULL);
 
    if(NULL == dpy) {
        fprintf(stderr, "failed to open display\n");
        return 1;
    }
 
    int a = 0;
    int b = 0;
    XScreenSaverQueryExtension(dpy, &a, &b);
    printf("%d %d\n", a, b);
 
    XScreenSaverQueryInfo(dpy, RootWindow(dpy, DefaultScreen(dpy)), &info);
    printf("%d %d %d %d\n", info.state, info.til_or_since, info.idle, info.kind);
    return 0;
}
/*
 * 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;
}
static void screenSaverSetXScreenSaver( CompDisplay *d, Bool enable )
{
	SCREENSAVER_DISPLAY(d);

	if( enable && !sd->XSScontext.init )
	{

		int dummy;
		if( !XScreenSaverQueryExtension(d->display, &sd->XSScontext.first_event, &dummy ) )
		{
			compLogMessage ("screensaver", CompLogLevelWarn,
					"XScreenSaver Extension not available");
			return;
		}
		sd->XSScontext.init = True;

		XGetScreenSaver( d->display, &sd->XSScontext.timeout, &sd->XSScontext.interval, \
			&sd->XSScontext.prefer_blanking, &sd->XSScontext.allow_exposures);
		XSetScreenSaver( d->display, (int)(screensaverGetAfter(d)*60.0), sd->XSScontext.interval, 0, AllowExposures);

		Window root = DefaultRootWindow (d->display);
		XSetWindowAttributes attr;
		int mask = 0;
		XScreenSaverSetAttributes( d->display,root, -100,-100,1,1,0 ,CopyFromParent,CopyFromParent,CopyFromParent,mask,&attr);
		XScreenSaverSelectInput( d->display, root, ScreenSaverNotifyMask );

	}
	if( !enable && sd->XSScontext.init )
	{
		sd->XSScontext.init = False;

		XSetScreenSaver( d->display, sd->XSScontext.timeout, sd->XSScontext.interval, \
			sd->XSScontext.prefer_blanking, sd->XSScontext.allow_exposures);

		Window root = DefaultRootWindow (d->display);
		XScreenSaverSelectInput( d->display, root, 0 );
		XScreenSaverUnsetAttributes( d->display, root );
	}
}
Exemplo n.º 22
0
int main(int argc, char **argv) {
  long started, wait_idle_ms, timeout_ms;
  int event_base, error_base, timed_out = 0;

  if (argc > 1) wait_idle_ms = parse_long(argv[1], DEFAULT_WAIT_MS);
  if (argc > 2) timeout_ms = parse_long(argv[2], DEFAULT_TIMEOUT_MS);

  started = now();
  Display *disp = XOpenDisplay(NULL);
  OK(disp);
  OK(XScreenSaverQueryExtension(disp, &event_base, &error_base));
  XScreenSaverInfo *info = XScreenSaverAllocInfo();
  OK(info);
  while (1) {
    if (now() >= started + timeout_ms * NANOS / MILLIS) BREAK(timed_out);
    OK(XScreenSaverQueryInfo(disp, DefaultRootWindow(disp), info));
    if (info->idle >= wait_idle_ms) break;
  }
  XFree(info);
  XCloseDisplay(disp);
  exit(timed_out ? EXIT_FAILURE : EXIT_SUCCESS);
}
Exemplo n.º 23
0
int main(int argc, char *argv[])
{
  XScreenSaverInfo *ssi;
  Display *dpy;
  int event_basep, error_basep;

  if (argc != 1) {
    usage(argv[0]);
    return 1;
  }
  
  dpy = XOpenDisplay(NULL);
  if (dpy == NULL) {
    fprintf(stderr, "couldn't open display\n");
    return 1;
  }
  
  if (!XScreenSaverQueryExtension(dpy, &event_basep, &error_basep)) {
    fprintf(stderr, "screen saver extension not supported\n");
    return 1;
  }

  ssi = XScreenSaverAllocInfo();
  if (ssi == NULL) {
    fprintf(stderr, "couldn't allocate screen saver info\n");
    return 1;
  }   
  
  if (!XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), ssi)) {
    fprintf(stderr, "couldn't query screen saver info\n");
    return 1;
  }
  
  printf("%lu\n", workaroundCreepyXServer(dpy, ssi->idle));

  XFree(ssi);
  XCloseDisplay(dpy);
  return 0;
}
Exemplo n.º 24
0
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 (
#ifdef __APPLE__
 	XScreenSaverQueryInfo != NULL && /* Support for weak-linked libXss. */
#endif
	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;
}
Exemplo n.º 25
0
int
main(int argc, char *argv[])
{
	XEvent		 ev;
	struct sigaction act = { };
	int		 evbase, errbase;

	if (argc < 2)
		errx(2, "usage: xssstart command [argument ...]\n");
	if ((dpy = XOpenDisplay(NULL)) == NULL)
		errx(1, "no display");
	atexit(closedisplay);
	if (!XScreenSaverQueryExtension(dpy, &evbase, &errbase))
		errx(1, "X11 extension MIT-SCREEN-SAVER not supported");
	XSetErrorHandler(eh);
	XScreenSaverSelectInput(dpy, DefaultRootWindow(dpy),
	    ScreenSaverNotifyMask);
	act.sa_handler = sigchld;
	act.sa_flags = SA_NOCLDSTOP | SA_RESTART;
	if (sigaction(SIGCHLD, &act, NULL) == -1)
		err(1, "sigaction");
	while (XNextEvent(dpy, &ev) == 0) {
		if (((XScreenSaverNotifyEvent *)&ev)->state == ScreenSaverOn &&
		    child == 0) {
			switch ((child = fork())) {
			case -1:
				err(1, "fork");
			case 0:
				execvp(argv[1], argv + 1);
				dpy = NULL;
				err(1, "exec");
			}
		}
	}
	return 0;
}
Exemplo n.º 26
0
unsigned long getIdleTime()
{
  XScreenSaverInfo ssi;
  Display *dpy;
  int event_basep, error_basep;

  dpy = XOpenDisplay(NULL);
  if (dpy == NULL) {
    //fprintf(stderr, "Couldn't open display\n");
    return -1;
  }
  
  if (!XScreenSaverQueryExtension(dpy, &event_basep, &error_basep)) {
    //fprintf(stderr, "Screen saver extension not supported\n");
    return -1;
  }
  
  if (!XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), &ssi)) {
    //fprintf(stderr, "Couldn't query screen saver info\n");
    return -1;
  }
  
  return workaroundCreepyXServer(dpy, ssi.idle);
}
Exemplo n.º 27
0
int main(int argc, char** argv)
{
  gdk_init(&argc, &argv);

#if defined(HAVE_LIBXSS) && defined(MOZ_WIDGET_GTK)
  int event_base, error_base;
  Bool have_xscreensaver =
    XScreenSaverQueryExtension(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
                               &event_base, &error_base);

  if (!have_xscreensaver) {
    fprintf(stderr, "No XScreenSaver extension on display\n");
  } else {
    XScreenSaverInfo* info = XScreenSaverAllocInfo();
    if (!info) {
      fprintf(stderr, "%s: Out of memory\n", argv[0]);
      return 1;
    }
    XScreenSaverQueryInfo(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
                          GDK_ROOT_WINDOW(), info);

    const char* state;
    const char* til_or_since = nullptr;
    switch (info->state) {
    case ScreenSaverOff:
      state = "Off";
      til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n";
      break;
    case ScreenSaverOn:
      state = "On";
      if (info->til_or_since) {
        til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n";
      } else {
        til_or_since = "XScreenSaver idle activation is disabled\n";
      }
      break;
    case ScreenSaverDisabled:
      state = "Disabled";
      break;
    default:
      state = "unknown";
    }

    const char* kind;
    switch (info->kind) {
    case ScreenSaverBlanked:
      kind = "Blanked";
      break;
    case ScreenSaverInternal:
      state = "Internal";
      break;
    case ScreenSaverExternal:
      state = "External";
      break;
    default:
      state = "unknown";
    }

    fprintf(stderr, "XScreenSaver state: %s\n", state);

    if (til_or_since) {
      fprintf(stderr, "XScreenSaver kind: %s\n", kind);
      fprintf(stderr, til_or_since, info->til_or_since / 1000);
    }

    fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000);

    XFree(info);
  }
#endif

  GdkPixbuf* screenshot = nullptr;
  GdkWindow* window = gdk_get_default_root_window();
#if (MOZ_WIDGET_GTK == 2)
  screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr,
                                            0, 0, 0, 0,
                                            gdk_screen_width(),
                                            gdk_screen_height());
#else
  screenshot = gdk_pixbuf_get_from_window(window, 0, 0,
                                          gdk_window_get_width(window),
                                          gdk_window_get_height(window));
#endif
  if (!screenshot) {
    fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]);
    return 1;
  }

  GError* error = nullptr;
  if (argc > 1) {
    gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr);
  } else {
    gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr,
                                "png", &error, nullptr);
  }
  if (error) {
    fprintf(stderr, "%s: failed to write screenshot as png: %s\n",
            argv[0], error->message);
    return error->code;
  }

  return 0;
}
Exemplo n.º 28
0
uint32_t
get_idle_milliseconds(Display *d)
{
    XScreenSaverInfo *ssi;
    int evbase, errbase;

    if (!XScreenSaverQueryExtension(d, &evbase, &errbase))
    {
        fprintf(stderr, "screen saver extension not supported\n");
        return 1;
    }
  
    ssi = XScreenSaverAllocInfo();
    if (ssi == NULL)
    {
        fprintf(stderr, "couldn't allocate screen saver info\n");
        return 1;
    }   
  
    if (!XScreenSaverQueryInfo(d, DefaultRootWindow(d), ssi))
    {
        fprintf(stderr, "couldn't query screen saver info\n");
        return 1;
    }
  
    uint32_t idle = (uint32_t) ssi->idle;

    XFree(ssi);

    // ugh. ssi->idle is wrong if DPMS is enabled, we have
    // to compensate.
    if (DPMSQueryExtension(d, &evbase, &errbase))
    {
        if (DPMSCapable(d))
        {
            CARD16 standby, suspend, off, state;
            BOOL onoff;

            DPMSGetTimeouts(d, &standby, &suspend, &off);
            DPMSInfo(d, &state, &onoff);

            if (onoff)
            {
                uint32_t offset = 0;
                switch (state)
                {
                case DPMSModeStandby:
                    offset = (uint32_t) (standby * 1000);
                    break;

                case DPMSModeSuspend:
                    offset = (uint32_t) ((suspend + standby) * 1000);
                    break;

                case DPMSModeOff:
                    offset = (uint32_t) ((off + suspend + standby) * 1000);
                    break;

                default:
                    /*nothing*/;
                }
                if (offset != 0 && idle < offset)
                    idle += offset;
            }
        } 
    }

    return idle;
}
Exemplo n.º 29
0
// Check if the XScreenSaver extension is supported by the X server
static void check_xss_supported(int event_basep, int error_basep) {
    if(!XScreenSaverQueryExtension(dpy, &event_basep, &error_basep)) {
        fprintf(stderr, _("Error: screen saver extension not supported\n"));
        exit(EXIT_FAILURE);
    }
}
Exemplo n.º 30
0
// ===================================
// main...
// -----------------------------------
int main (int argc,char *argv[]) {
	XSetWindowAttributes attr;
	Display* dpy;
	Window root;
	char mydisp  [256] = "";
	int  fg            = 0;
	int  screen;
	int  c;
	// ============================
	// get commandline options...
	// ----------------------------
	while (1) {
	int option_index = 0;
	static struct option long_options[] =
	{
		{"display"    , 1 , 0 , 'd'},
		{"pid"        , 1 , 0 , 'p'},
		{"help"       , 0 , 0 , 'h'},
		{"timeout"    , 1 , 0 , 't'},
		{"foreground" , 0 , 0 , 'f'},
		{"output"     , 0 , 0 , 'o'},
		{0            , 0 , 0 , 0  }
	};
	c = getopt_long (
		argc, argv, "hd:p:t:fo",long_options, &option_index
	);
	if (c == -1)
	break;

	switch (c) {
	case 'h':
		usage();
		exit(0);

	case 't':
		timeout = atoi(optarg);
	break;

	case 'd':
		strcpy(mydisp,optarg);
	break;

	case 'p':
		pid = atoi(optarg);
	break;

	case 'o':
		output = 1;
	break;

	case 'f':
		fg = 1;
	break;

	default:
		usage();
		exit(1);
	}
	}

	// prove pid...
	// --------------
	if (pid == 0) {
		printf("xidle: no valid pid defined\n");
		exit(1);
	}

	// open display...
	// ------------------
	if (strcmp(mydisp,"") == 0) {
		dpy = XOpen("null");
	} else {
		dpy = XOpen(mydisp);
	}
	if (dpy == NULL) {
		printf("xidle: unable to open display: %s\n",mydisp); 
		exit(1);
	}

	// go for daemon...
	// -----------------
	if (fg == 0) {
		daemon(1,0);
	}

	// install signal handler...
	// --------------------------
	signal( SIGHUP  , handlesighup   );
	signal( SIGCHLD , handlesigchld  );
	signal( SIGTRAP , timerstatus    );
	signal( SIGCONT , handlesigreset );

	// default window is the root window...
	// -------------------------------------
	root   = DefaultRootWindow(dpy);
	screen = DefaultScreen(dpy);

	// look if the MIT extension is available...
	// -------------------------------------------
	if (!XScreenSaverQueryExtension (dpy, &ss_event, &ss_error)) {
		exit(1);
	}
	XScreenSaverSelectInput (dpy, root, ScreenSaverNotifyMask);
	XSync (dpy, False);

	// select keyboard for press and release...
	// ------------------------------------------
	attr.event_mask = KeyPressMask | PointerMotionMask;
	XSelectInput(dpy, DefaultRootWindow(dpy), attr.event_mask);

	// set internal screen saver timeout...
	// --------------------------------------- 
	timersignal();
	setsaver(dpy,timeout);

	while(1) {
		XEvent event; 
		XScreenSaverNotifyEvent *sevent;
		XNextEvent (dpy, &event);
		// ==========================================
		// reset timer for pointer motion events...
		// ------------------------------------------
		if (event.type == MotionNotify) {
			triggerreset();
		}
		// =====================================================
		// reset timer for keyevents differently from Escape...
		// -----------------------------------------------------
		else if (event.type == KeyPress) {
			XKeyEvent *e = (XKeyEvent*)&event;
			if (e->keycode == ESC) {
				Exit();
			} else {
				triggerreset();
			}
		}
		// ==========================================
		// catch screen saver event...
		// ------------------------------------------
		else if (event.type == ss_event) {
			sevent = (XScreenSaverNotifyEvent *) &event;
			if (sevent->state == ScreenSaverOn) {
			// get screen saver event...
			// --------------------------
			Exit();
			}
		}
		// ==============================================
		// put back event to the queue if not handled...
		// ----------------------------------------------
		else {
			XPutBackEvent(dpy,&event);
		}
	}
	XCloseDisplay(dpy);
	return(0);
}