Re: org-clock idle time in pgtk Emacs

2023-02-04 Thread Ihor Radchenko
Max Nikulin  writes:

> As to Org my opinion is still that a defcustom for user-defined function 
> returning idle may be a better option.

Agree. If pgtk does not have to use something that absolutely needs to
be pgtk-specific, we should rather introduce a generic customization.
For example, a new `org-clock-idle-function' customization may accept
Elisp function of a string (for a shell command), defaulting to
`org-emacs-idle-seconds'.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: org-clock idle time in pgtk Emacs

2023-02-03 Thread Max Nikulin

On 03/02/2023 19:17, Ihor Radchenko wrote:

Julien Cubizolles writes:


May I know where "jc-idle-time" is coming from? Is it a built-in command
on wayland?


Sorry, I forgot to mention. It's a custom python program, working both
in X11 and wayland. I didn't find a built-in command.


I would prefer something built-in or, at least, available via OS'
package manager.


It seems, one can obtain the python package using

pip install idle-time

https://pypi.org/project/idle-time/

The issue is that the project has just 2 commits, a pull request for 
wider support of various environments is ignored by the author. The 
package already has CLI interface, but output format is not script-friendly:


https://github.com/escaped/idle_time/blob/master/idle_time/__main__.py
print(f"Idle time: {monitor.get_idle_time()}s")

python3 -m idle-time


This does not look pgtk-specific. Is it?


I see X11 and Windows in the implementation. xprintidle might be more 
reliable though.


As to Org my opinion is still that a defcustom for user-defined function 
returning idle may be a better option.




Re: org-clock idle time in pgtk Emacs

2023-02-03 Thread Ihor Radchenko
Julien Cubizolles  writes:

>> May I know where "jc-idle-time" is coming from? Is it a built-in command
>> on wayland?
>
> Sorry, I forgot to mention. It's a custom python program, working both
> in X11 and wayland. I didn't find a built-in command.

I would prefer something built-in or, at least, available via OS'
package manager.

> --8<---cut here---start->8---
> #!/usr/bin/env python3
>
> from idle_time import IdleMonitor
>
> monitor = IdleMonitor.get_monitor()
> print(f"{1000*monitor.get_idle_time():.0f}")
> --8<---cut here---end--->8---

This does not look pgtk-specific. Is it?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: org-clock idle time in pgtk Emacs

2023-02-02 Thread Julien Cubizolles
Ihor Radchenko  writes:


>> +(defcustom org-clock-pgtkidle-program-name
>> +  (if (executable-find "jc-idle-time")
>> +  "jc-idle-time")
>> +  "Name of the program which prints idle time in milliseconds.
>
> May I know where "jc-idle-time" is coming from? Is it a built-in command
> on wayland?

Sorry, I forgot to mention. It's a custom python program, working both
in X11 and wayland. I didn't find a built-in command.

--8<---cut here---start->8---
#!/usr/bin/env python3

from idle_time import IdleMonitor

monitor = IdleMonitor.get_monitor()
print(f"{1000*monitor.get_idle_time():.0f}")
--8<---cut here---end--->8---

-- 
Julien Cubizolles




Re: org-clock idle time in pgtk Emacs

2023-02-02 Thread Ihor Radchenko
Max Nikulin  writes:

>>> +(defvar org-pgtkidle-exists-p
>>> +  ;; Check that org-clock-pgtkidle-program-name exists.  But don't do that 
>>> on DOS/Windows,
>>> +  ;; since the command definitely does NOT exist there, and invoking
>>> +  ;; COMMAND.COM on MS-Windows is a bad idea -- it hangs.
>>> +  (and (null (memq system-type '(windows-nt ms-dos)))
>>> +   (eq 0 (call-process-shell-command
>>> +  (format "command -v %s" org-clock-pgtkidle-program-name)))
>>> +   ;; Check that x11idle can retrieve the idle time
>>> +   ;; FIXME: Why "..-shell-command" rather than just `call-process'?
>>> +   (eq 0 (call-process-shell-command 
>>> org-clock-pgtkidle-program-name
>> 
>> We may as well resolve this FIXME since we are at it.
>> Instead of the awkward combination of two `call-process-shell-command'
>> prompts, we can simply use `executable-find' + `call-process'.
>
> Is there a reasonable way to avoid code duplication? I agree that 
> `executable-find' may be better since `org-clock-pgtkidle-program-name' 
> might contain spaces or shell specials causing issues with current code.

I do not think that we should avoid code duplication here.
pgtk and x11 is principally different. The fact that the same code works
for them is rather a co-incidence. It is unlikely that a new
window-system is going to use the same code again. And de-duplicating
jut two functions is not worth it.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: org-clock idle time in pgtk Emacs

2023-02-01 Thread Max Nikulin

On 01/02/2023 20:15, Ihor Radchenko wrote:

+(defcustom org-clock-pgtkidle-program-name
+  (if (executable-find "jc-idle-time")
+  "jc-idle-time")
+  "Name of the program which prints idle time in milliseconds.


May I know where "jc-idle-time" is coming from? Is it a built-in command
on wayland?


During discussion in the later thread

Julien Cubizolles to emacs-orgmode. org-x11idle-exists-p with emacs 
--daemon. Fri, 28 Oct 2022 00:31:23 +0200. 
https://list.orgmode.org/871qqs6gqs@free.fr


I noticed a suggestion to use a d-bus method, unfortunately it is 
Gnome-specific


https://unix.stackexchange.com/questions/396911/how-can-i-tell-if-a-user-is-idle-in-wayland/
dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor 
/org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime


Another interface likely may give not really reliable results:

org.freedesktop.ScreenSaver GetSessionIdleTime

https://lists.freedesktop.org/archives/xdg/2007-March/009187.html
Stephan Kulow. screensaver dbus interfaces. Mon Mar 5 05:16:39 PST 2007

Unsure if some workaround like in xprintidle should be used on this way:
https://github.com/g0hl1n/xprintidle/blob/master/xprintidle.c#L150
 * Workaround: Check if if XServer is in a dpms state, check the
 * current timeout for this state and add this value to
 * the current idle time and return.
 */


+(defvar org-pgtkidle-exists-p
+  ;; Check that org-clock-pgtkidle-program-name exists.  But don't do that on 
DOS/Windows,
+  ;; since the command definitely does NOT exist there, and invoking
+  ;; COMMAND.COM on MS-Windows is a bad idea -- it hangs.
+  (and (null (memq system-type '(windows-nt ms-dos)))
+   (eq 0 (call-process-shell-command
+  (format "command -v %s" org-clock-pgtkidle-program-name)))
+   ;; Check that x11idle can retrieve the idle time
+   ;; FIXME: Why "..-shell-command" rather than just `call-process'?
+   (eq 0 (call-process-shell-command org-clock-pgtkidle-program-name


We may as well resolve this FIXME since we are at it.
Instead of the awkward combination of two `call-process-shell-command'
prompts, we can simply use `executable-find' + `call-process'.


Is there a reasonable way to avoid code duplication? I agree that 
`executable-find' may be better since `org-clock-pgtkidle-program-name' 
might contain spaces or shell specials causing issues with current code.





Re: org-clock idle time in pgtk Emacs

2023-02-01 Thread Ihor Radchenko
Julien Cubizolles  writes:

> Ihor Radchenko  writes:
>
>>
>> As Tim pointed out, we cannot guarantee that things working on 'x build
>> will also work on 'pgtk. Instead of abusing settings for 'x window
>> system, can you please introduce a new function org-pgtk-idle-seconds
>> using a new variable org-clock-pgtkidle-program-name, similar to
>> org-x11-idle-seconds, and then update org-user-idle-seconds?
>
> Sorry for the delay, here is a patch to that effect.

Thanks!

> +(defcustom org-clock-pgtkidle-program-name
> +  (if (executable-find "jc-idle-time")
> +  "jc-idle-time")
> +  "Name of the program which prints idle time in milliseconds.

May I know where "jc-idle-time" is coming from? Is it a built-in command
on wayland?

> +(defvar org-pgtkidle-exists-p
> +  ;; Check that org-clock-pgtkidle-program-name exists.  But don't do that 
> on DOS/Windows,
> +  ;; since the command definitely does NOT exist there, and invoking
> +  ;; COMMAND.COM on MS-Windows is a bad idea -- it hangs.
> +  (and (null (memq system-type '(windows-nt ms-dos)))
> +   (eq 0 (call-process-shell-command
> +  (format "command -v %s" org-clock-pgtkidle-program-name)))
> +   ;; Check that x11idle can retrieve the idle time
> +   ;; FIXME: Why "..-shell-command" rather than just `call-process'?
> +   (eq 0 (call-process-shell-command org-clock-pgtkidle-program-name

We may as well resolve this FIXME since we are at it.
Instead of the awkward combination of two `call-process-shell-command'
prompts, we can simply use `executable-find' + `call-process'.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: org-clock idle time in pgtk Emacs

2023-01-31 Thread Julien Cubizolles
Ihor Radchenko  writes:

>
> As Tim pointed out, we cannot guarantee that things working on 'x build
> will also work on 'pgtk. Instead of abusing settings for 'x window
> system, can you please introduce a new function org-pgtk-idle-seconds
> using a new variable org-clock-pgtkidle-program-name, similar to
> org-x11-idle-seconds, and then update org-user-idle-seconds?

Sorry for the delay, here is a patch to that effect.

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index ceb1fc833..81be37448 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -453,6 +453,15 @@ https://orgmode.org/worg/code/scripts/x11idle.c;
   :package-version '(Org . "9.7")
   :type 'string)
 
+(defcustom org-clock-pgtkidle-program-name
+  (if (executable-find "jc-idle-time")
+  "jc-idle-time")
+  "Name of the program which prints idle time in milliseconds.
+Case of a pgtk Emacs instance."
+  :group 'org-clock
+  :package-version '(Org . "9.7")
+  :type 'string)
+
 (defcustom org-clock-goto-before-context 2
   "Number of lines of context to display before currently clocked-in entry.
 This applies when using `org-clock-goto'."
@@ -1199,6 +1208,17 @@ If `only-dangling-p' is non-nil, only ask to resolve dangling
   "Return the current Mac idle time in seconds."
   (string-to-number (shell-command-to-string "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/10; print $idle; last}'")))
 
+(defvar org-pgtkidle-exists-p
+  ;; Check that org-clock-pgtkidle-program-name exists.  But don't do that on DOS/Windows,
+  ;; since the command definitely does NOT exist there, and invoking
+  ;; COMMAND.COM on MS-Windows is a bad idea -- it hangs.
+  (and (null (memq system-type '(windows-nt ms-dos)))
+   (eq 0 (call-process-shell-command
+  (format "command -v %s" org-clock-pgtkidle-program-name)))
+   ;; Check that x11idle can retrieve the idle time
+   ;; FIXME: Why "..-shell-command" rather than just `call-process'?
+   (eq 0 (call-process-shell-command org-clock-pgtkidle-program-name
+
 (defvar org-x11idle-exists-p
   ;; Check that x11idle exists.  But don't do that on DOS/Windows,
   ;; since the command definitely does NOT exist there, and invoking
@@ -1214,6 +1234,10 @@ If `only-dangling-p' is non-nil, only ask to resolve dangling
   "Return the current X11 idle time in seconds."
   (/ (string-to-number (shell-command-to-string org-clock-x11idle-program-name)) 1000))
 
+(defun org-pgtk-idle-seconds ()
+  "Return the current X11 idle time in seconds."
+  (/ (string-to-number (shell-command-to-string org-clock-pgtkidle-program-name)) 1000))
+
 (defun org-user-idle-seconds ()
   "Return the number of seconds the user has been idle for.
 This routine returns a floating point number."
@@ -1222,6 +1246,8 @@ This routine returns a floating point number."
 (org-mac-idle-seconds))
((and (eq window-system 'x) org-x11idle-exists-p)
 (org-x11-idle-seconds))
+   ((and (eq window-system 'pgtk) org-pgtkidle-exists-p)
+(org-pgtk-idle-seconds))
(t
 (org-emacs-idle-seconds
 

> Please, make sure that the pgtk option works on Wayland as well (or not,
> but we will then need to wait until someone tests the patch on Wayland).

It's working in Wayland, with a pgtk build.

-- 
Julien Cubizolles


Re: org-clock idle time in pgtk Emacs

2022-05-25 Thread Ihor Radchenko
Julien Cubizolles  writes:

> org-clock checks for the 'x window-system in order to use the program
> set up by org-clock-x11idle-program-name. Recent Emacs versions use the
> 'pgtk instead of 'x and as such will default to using
> org-emacs-idle-seconds in org-user-idle-seconds.
>
> The following patch provides a crude workaround.
>
> I'm using a python program (included below) to report idletime in
> wayland, using the idle-time module. It can be used for
> org-clock-x11idle-program-name.

Thanks for the patch!

As Tim pointed out, we cannot guarantee that things working on 'x build
will also work on 'pgtk. Instead of abusing settings for 'x window
system, can you please introduce a new function org-pgtk-idle-seconds
using a new variable org-clock-pgtkidle-program-name, similar to
org-x11-idle-seconds, and then update org-user-idle-seconds?

Please, make sure that the pgtk option works on Wayland as well (or not,
but we will then need to wait until someone tests the patch on Wayland).

Best,
Ihor



Re: org-clock idle time in pgtk Emacs

2022-05-24 Thread Tim Cross


Julien Cubizolles  writes:

> org-clock checks for the 'x window-system in order to use the program
> set up by org-clock-x11idle-program-name. Recent Emacs versions use the
> 'pgtk instead of 'x and as such will default to using
> org-emacs-idle-seconds in org-user-idle-seconds.
>

I"m not sure this is accurate. You should only use the pgtk build of
Emacs if your running wayland. You definitely should not use it if your
running under X.

The big issue is that some important key input facilities don't work
correctly if you run the pgtk build under X (actually, I'm not sure they
work correctly under Wayland either, but then again, Wayland is a
different beast to X and differences can be expected).

The pgtk build is not a replacement for the current xlib+gtk build,
which will remain the correct build when running under X.
Unfortunately, this does mean that if you use both X and Wayland, you
likely will need two builds of Emacs.

There was a fairly long discussion thread about this on emacs-devel
about a month or so ago. The upshot was flagging the need to update the
documentation to clarify that people should not use the pgtk build when
running under X windows.

I suspect this means the below patch will need further refinement. 



> The following patch provides a crude workaround.
>
> I'm using a python program (included below) to report idletime in
> wayland, using the idle-time module. It can be used for
> org-clock-x11idle-program-name.
>
> modified   lisp/org-clock.el
> @@ -1196,7 +1196,7 @@ If `only-dangling-p' is non-nil, only ask to resolve 
> dangling
>  
>  (defvar org-x11idle-exists-p
>;; Check that x11idle exists
> -  (and (eq window-system 'x)
> +  (and (or (eq window-system 'pgtk) (eq window-system 'x))
> (eq 0 (call-process-shell-command
>(format "command -v %s" org-clock-x11idle-program-name)))
> ;; Check that x11idle can retrieve the idle time
> @@ -1213,7 +1213,7 @@ This routine returns a floating point number."
>(cond
> ((eq system-type 'darwin)
>  (org-mac-idle-seconds))
> -   ((and (eq window-system 'x) org-x11idle-exists-p)
> +   ((and (or (eq window-system 'x) (eq window-system 'pgtk)) 
> org-x11idle-exists-p)
>  (org-x11-idle-seconds))
> (t
>  (org-emacs-idle-seconds
>
> #!/usr/bin/env python3
>
> from idle_time import IdleMonitor
>
> monitor = IdleMonitor.get_monitor()
> print(f"{1000*monitor.get_idle_time():.0f}")