You are not logged in.

#1 2011-06-30 19:19:29

silvik
Member
From: Bucharest/Romania
Registered: 2006-11-08
Posts: 110

[SOLVED] detect X11 user inactivity

Hi there,
I want to write a script that shuts down the computer if I don't use X11 keyboard and mouse and no video player is running after 12PM. I hate waking up in the morning with the computer running after falling asleep during a movie.

How can I detect if I'm using X11? Is there any way to check if dpms kicked in or the mouse and keyboard aren't used for some time? Or some screensaver can tell over dbus if it's active or not? (right now i don't use any screensaver, only X11 dpms).
Pidgin has a way of detecting user inactivity based on keyboard and mouse usage, so probably there is a way to do this. Any ideas?

Thanks a lot!

Last edited by silvik (2011-07-04 09:06:19)

Offline

#2 2011-06-30 21:59:38

kofrad
Member
From: South Florida, USA
Registered: 2011-05-15
Posts: 45

Re: [SOLVED] detect X11 user inactivity

Why not just use the power management options? Like setting the computer to hibernate/suspend after an hour or two. The computer will use far less power, take quicker to resume and turn off at anytime when idle, not just at night. I'm not sure of another way to do it though without xscreensaver, or whatever method you use to set your power settings.


Desktop: Arch Linux | AMD Athlon 64 X2 Dual Core 5000+ | 3GB RAM | GeForce 6800 XT 512MB | 1TB+
Audio Studio: XP SP3 | Intel Pentium 4 3GHz Prescott | 1GB DDR | GeForce 6800 128MB | 120GB

Offline

#3 2011-07-01 00:38:45

silvik
Member
From: Bucharest/Romania
Registered: 2006-11-08
Posts: 110

Re: [SOLVED] detect X11 user inactivity

Except ondemand frequency settings I don't use any power management, it's just a plain openbox desktop with a panel.
Last time I checked resume didn't work. Probably still doesn't. Anyway, it's not really what I'm looking for.

Maybe somebody has any other ideas? Thanks.

Offline

#4 2011-07-01 04:51:45

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED] detect X11 user inactivity

If you know how to program in C, I believe you can use Xorg's xscreensaver API.  Do a 'man xscreensaver' for details.

I've never tried to use this, so I'm not exactly sure how it works; I'd have to play around with it some to figure it out.

Offline

#5 2011-07-01 14:38:43

ConnorBehan
Trusted User (TU)
From: Long Island NY
Registered: 2007-07-05
Posts: 1,359
Website

Re: [SOLVED] detect X11 user inactivity

FYI, the Pidgin code that does this is in "pidgin/gtkidle.c" in the pidgin source. Hope that helps!


6EA3 F3F3 B908 2632 A9CB E931 D53A 0445 B47A 0DAB
Great things come in tar.xz packages.

Offline

#6 2011-07-02 00:59:00

Azriel
Member
Registered: 2008-01-23
Posts: 58

Re: [SOLVED] detect X11 user inactivity

Do you really want something this complicated ? If it's just for watching movies, you could consider just typing

sleep 2h; halt

in a terminal as root before starting the movie (or a longer time if the movie is a long one).

Offline

#7 2011-07-02 01:36:07

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] detect X11 user inactivity

18 lines of C...

/* gcc -o getIdleTime getIdleTime.c -lXss */
#include <X11/extensions/scrnsaver.h>
#include <stdio.h>

int main(void) {
  Display *dpy = XOpenDisplay(NULL);

  if (!dpy) {
    return(1);
  }

  XScreenSaverInfo *info = XScreenSaverAllocInfo();
  XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
  printf("%u", info->idle);

  return(0);
}

Last edited by falconindy (2011-07-03 14:09:11)

Offline

#8 2011-07-02 08:58:49

xduugu
Member
Registered: 2008-10-16
Posts: 292

Re: [SOLVED] detect X11 user inactivity

sinac might work for you, but for movies I would just use

$ mplayer <video file>; shutdown -h now

Offline

#9 2011-07-03 14:03:52

silvik
Member
From: Bucharest/Romania
Registered: 2006-11-08
Posts: 110

Re: [SOLVED] detect X11 user inactivity

Thanks for all answers!

@falconindy: Probably that's what I need, but I get some errors while compiling (sorry, I'm not a programmer):

silvik@morgana:/build$ gcc -o getIdleTime getIdleTime.c -lXss
getIdleTime.c: In function ‘main’:
getIdleTime.c:13:30: error: ‘display’ undeclared (first use in this function)
getIdleTime.c:13:30: note: each undeclared identifier is reported only once for each function it appears in

Offline

#10 2011-07-03 14:09:33

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] detect X11 user inactivity

and fixed in my original post. odd.

Offline

#11 2011-07-03 14:12:44

Foucault
Member
From: Athens, Greece
Registered: 2010-04-06
Posts: 214

Re: [SOLVED] detect X11 user inactivity

Well this

XScreenSaverQueryInfo(dpy, DefaultRootWindow(display), info);

should be like this

XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);

Edit: Sorry, too slow sad

Last edited by Foucault (2011-07-03 14:13:29)

Offline

#12 2011-07-04 09:05:57

silvik
Member
From: Bucharest/Romania
Registered: 2006-11-08
Posts: 110

Re: [SOLVED] detect X11 user inactivity

thanks. problem solved. falconindy's program work great! (output in milliseconds)

Offline

#13 2021-11-03 19:20:54

Topsy
Member
Registered: 2021-11-03
Posts: 3

Re: [SOLVED] detect X11 user inactivity

2021 version:

/* https://bbs.archlinux.org/viewtopic.php?pid=956113#p956113 */
/* install libxss-dev, libx11-dev */
/* LANG=C gcc -Wall -Wextra -Werror -o getIdleTime getIdleTime.c -lXss -lX11 */
#include <X11/extensions/scrnsaver.h>
#include <X11/Xlib.h>
#include <stdio.h>

int main(void) {
  Display *dpy = XOpenDisplay(NULL);

  if (!dpy) {
    return(1);
  }

  XScreenSaverInfo *info = XScreenSaverAllocInfo();
  XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);

  printf("%lu", info->idle);

  XFree(info);
  XCloseDisplay(dpy);
  return(0);
}

Last edited by Topsy (2021-11-03 21:30:39)

Offline

#14 2021-11-03 22:27:01

2ManyDogs
Forum Moderator
Registered: 2012-01-15
Posts: 3,803

Re: [SOLVED] detect X11 user inactivity

Closing this old solved topic.

Offline

Board footer

Powered by FluxBB