Bug 275761 - x11/xidle: xidle triggers twice
Summary: x11/xidle: xidle triggers twice
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: amd64 Any
: --- Affects Many People
Assignee: Roman Bogorodskiy
URL:
Keywords: needs-patch
Depends on:
Blocks:
 
Reported: 2023-12-14 10:52 UTC by Amr
Modified: 2023-12-24 09:58 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Amr 2023-12-14 10:52:16 UTC
To Build a minimal FreeBSD Desktop with DWM and XORG One needs in order to save power to create a script to auto suspend the system when the user is idle for a period of time, in order to use that there is many native tools for unix , the problem is this tool xidle that is available to install on FreeBSD has a bug which defeat the purpose
the Bug it that it will trigger twice 
The First one when The user is idle for a specified time 
The second one is when the user try to interact with the system using keyboard or mouse after the first trigger

so for example 
 $ xidle -timeout 600 -program '/usr/sbin/zzz'

it will auto suspend the system after being idle for 10 minutes but when system wakes up and user press any key or move the mouse it will trigger again and the system will enter suspend for a second time.

for testing you may use this code to reproduce the bug

% xidle -timeout 2 -program '/bin/echo OK'
OK // started after 2 seconds
OK // started when I moved mouse again

So there seems to be some bug with xidle ...

OS: FreeBSD 14.0-p3
HW: Thinkpad x270
Cheers
Amr
Comment 1 Slawomir Wojciech Wojtczak 2023-12-15 06:31:08 UTC
I have the same problem it that helps in any way.

The 'program' instruction is executed after specified idle period and then again when you move the mouse (idle period is stopped).

Regards,
vermaden
Comment 2 Amr 2023-12-15 16:27:51 UTC
Do suggest that it's a bug in idel period ?
Comment 3 Roman Bogorodskiy freebsd_committer freebsd_triage 2023-12-17 16:48:54 UTC
Hm, sorry, I'm not an X11 programming expert, so my assumptions below could be incorrect.

From what I can see, xidle uses the XScreenServer extension for handling execution on timeout. I took the suggested repro with "xidle -timeout 2 -program '/bin/echo OK'" and could notice the issue too. I was curios what happens when it sees the second event, and both of them are from if (ev.type == x.saver_event) block in the default case of the "switch (ev.type)". The difference between these two events, however, is that the first event (before locking) has state "ScreenSaverOn" and the second has "ScreenSaverOff".

From what I can see, we don't need to execute anything on "ScreenSaverOff" case. So I've patched xidle.c like this:

--- xidle.c.orig        2023-12-17 17:06:14.826105000 +0100
+++ xidle.c     2023-12-17 17:32:45.625797000 +0100
@@ -426,7 +426,7 @@
                                 * Was for real or due to terminal
                                 * switching or a locking program?
                                 */
-                               if (timeout > 0 && se->forced == False)
+                               if (timeout > 0 && se->forced == False && se->state == ScreenSaverOn)
                                        action(&x, args);
                        }
                        break;

and it seem to work fine for me. Could you please give it a try?
Comment 4 Slawomir Wojciech Wojtczak 2023-12-17 16:54:53 UTC
(In reply to Roman Bogorodskiy from comment #3)

The patch solved the problem on my side.

Thank You.

Regards,
vermaden
Comment 5 Roman Bogorodskiy freebsd_committer freebsd_triage 2023-12-17 17:00:21 UTC
(In reply to Slawomir Wojciech Wojtczak from comment #4)

Thanks for testing, I'll also wait of Amr. In the meantime have to think whether this change could introduce regressions to some other features.
Comment 6 Amr 2023-12-20 22:08:26 UTC
Thank you, I can confirm that patch fixed it for me , would you please introduce this patch to /usr/ports/x11/xidle ?
Comment 7 commit-hook freebsd_committer freebsd_triage 2023-12-24 09:56:56 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=08ff7700f244f5157f03dbb1a67ade35d32ed4c4

commit 08ff7700f244f5157f03dbb1a67ade35d32ed4c4
Author:     Roman Bogorodskiy <novel@FreeBSD.org>
AuthorDate: 2023-12-24 09:46:21 +0000
Commit:     Roman Bogorodskiy <novel@FreeBSD.org>
CommitDate: 2023-12-24 09:46:21 +0000

    x11/xidle: fix double triggering on screen unlock

    A problem reported that xidle triggers twice: the first one when it
    reaches idle timeout and locks the screen (which is expected), and the
    second time immediately when the screen is unlocked (which is
    undesired).
    The problem is that, apparently, the XScreenServer extension, which
    xidle uses, sends ScreenSaverOn on locking and ScreenSaverOff on
    unlocking. Currently, xidle doesn't filter events by locking state, so
    it locks the screen again when the ScreenSaverOff event comes in.

    The fix is to run a screensaver only on the ScreenSaverOn event.

    PR:     275761

 x11/xidle/Makefile                  |  1 +
 x11/xidle/files/patch-xidle.c (new) | 11 +++++++++++
 2 files changed, 12 insertions(+)
Comment 8 Roman Bogorodskiy freebsd_committer freebsd_triage 2023-12-24 09:58:04 UTC
The fix is merged, thanks for reporting.