emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] org-clock-auto-clockout does not actually use x11idle on X11 [9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)]
@ 2023-10-13  7:02 Vladimir Nikishkin
  2023-10-15  8:26 ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Vladimir Nikishkin @ 2023-10-13  7:02 UTC (permalink / raw)
  To: emacs-orgmode

For some time I have been wondering why auto-clockout does not use
x11idle on my machine. Today I finally got to studying this issue.

Here is `org-clock-auto-clockout-insinuate'

#+begin_src elisp
(defun org-clock-auto-clockout-insinuate ()
  "Set up hook for auto clocking out when Emacs is idle.
See `org-clock-auto-clockout-timer'.

This function is meant to be added to the user configuration."
  (require 'org-clock)
  (add-hook 'org-clock-in-hook #'org-clock-auto-clockout t))
#+end_src

Okay, the only thing it does is it adds a hook.
What does the hook do?

#+begin_src elisp
(defun org-clock-auto-clockout ()
  "Clock out the currently clocked in task if Emacs is idle.
See `org-clock-auto-clockout-timer' to set the idle time span.
This is only effective when `org-clock-auto-clockout-insinuate'
is present in the user configuration."
  (when (and (numberp org-clock-auto-clockout-timer)
	     org-clock-current-task)
    (run-with-idle-timer
     org-clock-auto-clockout-timer nil #'org-clock-out)))
#+end_src

just runs auto clockout on an emacs idle timer, which does not care
about X11 idleness either. It also runs only once, since the seconds
argument is nil.

#'org-clock-out does not use x11idle either.

So, at least in Org 9.6.8, the Manual's claim that
#+begin_quote
   (1) On computers using macOS, idleness is based on actual user
idleness, not just Emacs’ idle time.  For X11, you can install a utility
program ‘x11idle.c’, available in the ‘org-contrib/’ repository, or
install the xprintidle package and set it to the variable
‘org-clock-x11idle-program-name’ if you are running Debian, to get the
same general treatment of idleness.  On other systems, idle time refers
to Emacs idle time only.
#+end_quote

is false.

I suggest rewriting that timer expression like this:

#+begin_src elisp
(run-with-timer 60 60
                (lambda ()
                  (if (< org-clock-auto-clockout-timer (if org-x11idle-exists-p
                                                          (org-x11-idle-seconds)
                                                        (current-idle-time))))
                      (org-clock-out)
                    nil)))
#+end_src

or, even better:

#+begin_src elisp
(defun org-clock-auto-clockout-maybe ()
   (if (< org-clock-auto-clockout-timer (if org-x11idle-exists-p
                                           (org-x11-idle-seconds)
                                         (current-idle-time)))))
(defun org-clock-auto-clockout-insinuate ()
  "Set up hook for auto clocking out when Emacs is idle.
See `org-clock-auto-clockout-timer'.

This function is meant to be added to the user configuration."
  (require 'org-clock)
  (add-hook 'org-clock-in-hook #'org-clock-auto-clockout-maybe t)
  (add-hook 'org-clock-out-hook (lambda ()
       (cancel-function-timers 'org-clock-auto-clockout-maybe))))
#+end_src

Emacs  : GNU Emacs 30.0.50 (build 1, x86_64-slackware-linux-gnu, GTK+ Version 3.24.31, cairo version 1.16.0)
 of 2023-09-22
Package: Org mode version 9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)
-- 
Your sincerely,
Vladimir Nikishkin (MiEr, lockywolf)
(Laptop)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] org-clock-auto-clockout does not actually use x11idle on X11 [9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)]
  2023-10-13  7:02 [BUG] org-clock-auto-clockout does not actually use x11idle on X11 [9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)] Vladimir Nikishkin
@ 2023-10-15  8:26 ` Ihor Radchenko
  2023-12-04 13:53   ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2023-10-15  8:26 UTC (permalink / raw)
  To: Vladimir Nikishkin; +Cc: emacs-orgmode

Vladimir Nikishkin <lockywolf@gmail.com> writes:

> For some time I have been wondering why auto-clockout does not use
> x11idle on my machine.
> ...
> or, even better:
>
> #+begin_src elisp
> (defun org-clock-auto-clockout-maybe ()
>    (if (< org-clock-auto-clockout-timer (if org-x11idle-exists-p
>                                            (org-x11-idle-seconds)
>                                          (current-idle-time)))))
> (defun org-clock-auto-clockout-insinuate ()
>   "Set up hook for auto clocking out when Emacs is idle.
> See `org-clock-auto-clockout-timer'.
>
> This function is meant to be added to the user configuration."
>   (require 'org-clock)
>   (add-hook 'org-clock-in-hook #'org-clock-auto-clockout-maybe t)
>   (add-hook 'org-clock-out-hook (lambda ()
>        (cancel-function-timers 'org-clock-auto-clockout-maybe))))
> #+end_src

Makes sense.
Would you be interested to submit a patch?
See https://orgmode.org/worg/org-contribute.html

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [BUG] org-clock-auto-clockout does not actually use x11idle on X11 [9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)]
  2023-10-15  8:26 ` Ihor Radchenko
@ 2023-12-04 13:53   ` Ihor Radchenko
  2023-12-15 11:28     ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2023-12-04 13:53 UTC (permalink / raw)
  To: Vladimir Nikishkin; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 286 bytes --]

Ihor Radchenko <yantar92@posteo.net> writes:

> Vladimir Nikishkin <lockywolf@gmail.com> writes:
>
>> For some time I have been wondering why auto-clockout does not use
>> x11idle on my machine.
>> ...

Attaching a tentative fix.
It would help if someone can test it on other machine.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-clock-auto-clockout-Honor-system-idle-time.-Do-n.patch --]
[-- Type: text/x-patch, Size: 3391 bytes --]

From d655c095fda6d1789a20aa93d4ac3a5f269dadc9 Mon Sep 17 00:00:00 2001
Message-ID: <d655c095fda6d1789a20aa93d4ac3a5f269dadc9.1701697942.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Mon, 4 Dec 2023 14:47:53 +0100
Subject: [PATCH] org-clock-auto-clockout: Honor system idle time.  Do not
 spawn multiple timers

* lisp/org-clock.el (org-clock--auto-clockout-timer-obj): New internal
variable holding the auto-clockout timer.
(org-clock--auto-clockout-maybe): New function to be used in timer.
Use `org-user-idle-seconds' to check for idleness.  Make sure that we
correctly handle system idle time, which may be lesser than Emacs idle
time.
(org-clock-auto-clockout): Do not create duplicate timers.  Use the
new internal helper function.

Reported-by: Vladimir Nikishkin <lockywolf@gmail.com>
Link: https://orgmode.org/list/87ttqv6l2g.fsf@laptop.lockywolf.net
---
 lisp/org-clock.el | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index eda312d74..a042af4f3 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -1493,6 +1493,33 @@ (defun org-clock-in (&optional select start-time)
 	 (message "Clock starts at %s - %s" ts org--msg-extra)
 	 (run-hooks 'org-clock-in-hook))))))
 
+(defvar org-clock--auto-clockout-timer-obj nil
+  "Timer object holding the existing clockout timer.")
+(defun org-clock--auto-clockout-maybe ()
+  "Clock out the currently clocked in task when idle.
+See `org-clock-auto-clockout-timer' to set the idle time span.
+
+This function is to be called by a timer."
+  (when (and (numberp org-clock-auto-clockout-timer)
+	     org-clock-current-task)
+    (let ((user-idle-seconds (org-user-idle-seconds)))
+      (cond
+       ;; Already idle.  Clock out.
+       ((>= user-idle-seconds org-clock-auto-clockout-timer)
+        (setq org-clock--auto-clockout-timer-obj nil)
+        (org-clock-out))
+       ;; Emacs is idle but system is not.  Retry assuming that system will remain idle.
+       ((>= (org-emacs-idle-seconds) org-clock-auto-clockout-timer)
+        (setq org-clock--auto-clockout-timer-obj
+              (run-with-timer
+               (- org-clock-auto-clockout-timer user-idle-seconds)
+               nil #'org-clock--auto-clockout-maybe)))
+       ;; Emacs is not idle.  Check again next time we are idle.
+       (t
+        (setq org-clock--auto-clockout-timer-obj
+              (run-with-idle-timer
+               org-clock-auto-clockout-timer nil #'org-clock--auto-clockout-maybe)))))))
+
 (defun org-clock-auto-clockout ()
   "Clock out the currently clocked in task if Emacs is idle.
 See `org-clock-auto-clockout-timer' to set the idle time span.
@@ -1500,9 +1527,11 @@ (defun org-clock-auto-clockout ()
 This is only effective when `org-clock-auto-clockout-insinuate'
 is present in the user configuration."
   (when (and (numberp org-clock-auto-clockout-timer)
-	     org-clock-current-task)
-    (run-with-idle-timer
-     org-clock-auto-clockout-timer nil #'org-clock-out)))
+	     org-clock-current-task
+             (not (timerp org-clock--auto-clockout-timer-obj)))
+    (setq org-clock--auto-clockout-timer-obj
+          (run-with-idle-timer
+           org-clock-auto-clockout-timer nil #'org-clock--auto-clockout-maybe))))
 
 ;;;###autoload
 (defun org-clock-toggle-auto-clockout ()
-- 
2.42.0


[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [BUG] org-clock-auto-clockout does not actually use x11idle on X11 [9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)]
  2023-12-04 13:53   ` Ihor Radchenko
@ 2023-12-15 11:28     ` Ihor Radchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Ihor Radchenko @ 2023-12-15 11:28 UTC (permalink / raw)
  To: Vladimir Nikishkin; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

> Ihor Radchenko <yantar92@posteo.net> writes:
>
>> Vladimir Nikishkin <lockywolf@gmail.com> writes:
>>
>>> For some time I have been wondering why auto-clockout does not use
>>> x11idle on my machine.
>>> ...
>
> Attaching a tentative fix.
> It would help if someone can test it on other machine.

Applied, onto main.
Fixed.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=23291840b

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-12-15 11:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-13  7:02 [BUG] org-clock-auto-clockout does not actually use x11idle on X11 [9.6.8 (release_9.6.8-3-g21171d @ /usr/share/emacs/30.0.50/lisp/org/)] Vladimir Nikishkin
2023-10-15  8:26 ` Ihor Radchenko
2023-12-04 13:53   ` Ihor Radchenko
2023-12-15 11:28     ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).