A way to print current idle time
meedstrom opened this issue · 6 comments
I'm seeking a Wayland replacement for the little command xprintidle. (I'm developing an Emacs package that needs to be able to poll idle time.) Far as I've seen, only GNOME and KDE provide ways to poll idle time, and I've not found any other utility.
It seems a natural fit for swayidle, if there was a command line flag that simply prints idle time to stdout and exits. What do you think?
No, the Wayland protocol doesn't support that feature (by design).
What if while swayidle runs as a daemon, we get a way to poll it?
I guess I could hack something by having it just increment some sort of counter in a file in /tmp
every 1000 ms, but that seems potentially wasteful of laptop battery, or it's at least inelegant.
May I ask what is your use-case for querying the idle time?
Some code here: https://github.com/meedstrom/eva/blob/master/eva.el#L865
Basically, it's a virtual assistant which should do a number of things:
- Do certain stuff periodically only while NOT idle. When becoming idle, stop doing that stuff.
- Do other stuff when the user returns from idle (including maybe showing an interactive prompt to the user, but that depends on the state of other variables)
- Record chunks of idle time to a log file, with a timestamp and a length in minutes
- Also count as "idle time", any length of time when the computer was off.
- This just means I continually update a timestamp, and when it makes a big leap then that counts as an idle chunk. So point 3 could theoretically be taken care of without any swayidle involvement.
- Also count as "idle time", any length of time when the computer was off.
For point 1 and 2, maybe there's an alternative approach, by having swayidle poke Emacs into action instead of Emacs querying idle time, although I feel like that complicates things as I'd have to provide README instructions to set up every kind of idle daemon (instead of just "make sure this program is installed"). It's one thing as a developer to figure out how to poll idle time in 5-10 different desktop environments, but it becomes exponentially more work if I have to figure out how to kill and restart their idle daemons with different parameters.
I understand if you prefer that programs can't poll idle time so that all idle-related logic is handled by the compositor or swayidle, and consequently the sysadmin gets a better overview of what can happen and what can't. That's a sort of Waylandish principle.
However, I'm not doing anything that could interfere with locking the screen or suspending the system, and I suspect most software that polls for idle time are likewise polite, only using it for internal decisions. And just imagine if someone has two or more programs that all need to configure swayidle (or another idle daemon) for their purposes!
Sorry, swayidle is just an utility for users, not a building block for applications like Emacs. I'd recommend building your own Wayland client using the idle-notify protocol.
From my perspective, I am an user, maybe even the typical user, of a program like swayidle: I'm configuring my desktop environment so it will behave as I want it to. Would it be different if my virtual assistant were a bash script hooking into wofi, dunst, swaybar etc, rather than a file of Emacs Lisp hooking into the Emacs equivalents?
I care about sharing my program code, which is why I package it, but I don't think that makes it an "application", it's more like an oh-my-zsh plugin.
Anyway, I looked into writing a simple "wayland-idle-tracker" program as you said, but it would have taken me long because coding in Wayland is all very new to me, and I don't know how to write a daemon in C that listens to a signal and sends back a message. Instead, I came up with the following script.
For anyone interested, it adds 60 every minute to a file /tmp/idle
. So you can just read that file to see how many minutes you've been idle.
#!/usr/bin/env bash
increment() {
echo `expr $(cat /tmp/idle) + 60` > /tmp/idle
}
swayidle timeout 15 "echo 'watching idle'" resume "echo 0 > /tmp/idle" &
echo 0 > /tmp/idle
while true; do sleep 60 && increment; done