emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] org-user-idle-seconds: Add support for logind
@ 2023-03-21 15:39 Nathaniel Nicandro
  2023-03-22 12:22 ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Nathaniel Nicandro @ 2023-03-21 15:39 UTC (permalink / raw)
  To: emacs-orgmode

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


Hello,

I would like to submit a patch that adds support for logind to
`org-user-idle-seconds`.  This patch has been working for me for a long
while now and I thought it time for me to submit it.  I've contributed
to Emacs in the past so my paperwork should be on file.

Let me know if any changes should be made.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-user-idle-seconds: Add support for logind seconds --]
[-- Type: text/x-patch, Size: 2288 bytes --]

lisp/org-clock.el | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index f869b0b..9d34a72 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -51,6 +51,9 @@ (declare-function org-table-goto-line "org-table" (n))
 (declare-function org-dynamic-block-define "org" (type func))
 (declare-function w32-notification-notify "w32fns.c" (&rest params))
 (declare-function w32-notification-close "w32fns.c" (&rest params))
+(declare-function dbus-list-activatable-names "dbus" (&optional bus))
+(declare-function dbus-call-method "dbus" (bus service path interface method &rest args))
+(declare-function dbus-get-property "dbus" (bus service path interface property))
 
 (defvar org-frame-title-format-backup nil)
 (defvar org-state)
@@ -1214,6 +1217,25 @@ (defun org-x11-idle-seconds ()
   "Return the current X11 idle time in seconds."
   (/ (string-to-number (shell-command-to-string org-clock-x11idle-program-name)) 1000))
 
+(defvar org-logind-dbus-session-path
+  (when (and (boundp 'dbus-runtime-version)
+             (require 'dbus nil t)
+             (member "org.freedesktop.login1" (dbus-list-activatable-names)))
+    (dbus-call-method
+     :system "org.freedesktop.login1"
+     "/org/freedesktop/login1"
+     "org.freedesktop.login1.Manager"
+     "GetSessionByPID" (emacs-pid))))
+
+(defun org-logind-user-idle-seconds ()
+  "Return the number of idle seconds for the user according to logind."
+  (- (float-time)
+     (/ (dbus-get-property
+         :system "org.freedesktop.login1"
+         org-logind-dbus-session-path
+         "org.freedesktop.login1.Session" "IdleSinceHint")
+        1e6)))
+
 (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 +1244,13 @@ (defun org-user-idle-seconds ()
     (org-mac-idle-seconds))
    ((and (eq window-system 'x) org-x11idle-exists-p)
     (org-x11-idle-seconds))
+   ((and
+     org-logind-dbus-session-path
+     (dbus-get-property
+      :system "org.freedesktop.login1"
+      org-logind-dbus-session-path
+      "org.freedesktop.login1.Session" "IdleHint"))
+    (org-logind-user-idle-seconds))
    (t
     (org-emacs-idle-seconds))))
 
-- 
2.39.1

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


-- 
Nathaniel

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

* Re: [PATCH] org-user-idle-seconds: Add support for logind
  2023-03-21 15:39 [PATCH] org-user-idle-seconds: Add support for logind Nathaniel Nicandro
@ 2023-03-22 12:22 ` Ihor Radchenko
  2023-03-22 17:47   ` Bastien Guerry
  2023-03-22 21:15   ` Nathaniel Nicandro
  0 siblings, 2 replies; 5+ messages in thread
From: Ihor Radchenko @ 2023-03-22 12:22 UTC (permalink / raw)
  To: Nathaniel Nicandro, Bastien; +Cc: emacs-orgmode

Nathaniel Nicandro <nathanielnicandro@gmail.com> writes:

> I would like to submit a patch that adds support for logind to
> `org-user-idle-seconds`.  This patch has been working for me for a long
> while now and I thought it time for me to submit it.  I've contributed
> to Emacs in the past so my paperwork should be on file.

Thanks!

Though I do not see any commits associated with Nathaniel Nicandro or
your email in Emacs git repo.
Bastien, may you check FSF records?

> Let me know if any changes should be made.
>
> lisp/org-clock.el | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)

Please provide the commit message. See
https://orgmode.org/worg/org-contribute.html#commit-messages

> +(defvar org-logind-dbus-session-path
> +  (when (and (boundp 'dbus-runtime-version)
> +             (require 'dbus nil t)
> +             (member "org.freedesktop.login1" (dbus-list-activatable-names)))
> +    (dbus-call-method
> +     :system "org.freedesktop.login1"
> +     "/org/freedesktop/login1"
> +     "org.freedesktop.login1.Manager"
> +     "GetSessionByPID" (emacs-pid))))

Please provide a docstring for the new variable.

Also, this new feature is worth documenting in ORG-NEWS.

Otherwise, the patch looks good.

-- 
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] 5+ messages in thread

* Re: [PATCH] org-user-idle-seconds: Add support for logind
  2023-03-22 12:22 ` Ihor Radchenko
@ 2023-03-22 17:47   ` Bastien Guerry
  2023-03-22 21:15   ` Nathaniel Nicandro
  1 sibling, 0 replies; 5+ messages in thread
From: Bastien Guerry @ 2023-03-22 17:47 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Nathaniel Nicandro, emacs-orgmode

Hi,

Ihor Radchenko <yantar92@posteo.net> writes:

> Though I do not see any commits associated with Nathaniel Nicandro or
> your email in Emacs git repo.
> Bastien, may you check FSF records?

Nathaniel's records are okay.

-- 
 Bastien Guerry


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

* Re: [PATCH] org-user-idle-seconds: Add support for logind
  2023-03-22 12:22 ` Ihor Radchenko
  2023-03-22 17:47   ` Bastien Guerry
@ 2023-03-22 21:15   ` Nathaniel Nicandro
  2023-03-23 11:46     ` Ihor Radchenko
  1 sibling, 1 reply; 5+ messages in thread
From: Nathaniel Nicandro @ 2023-03-22 21:15 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Nathaniel Nicandro, Bastien, emacs-orgmode

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


Ihor Radchenko <yantar92@posteo.net> writes:

> Nathaniel Nicandro <nathanielnicandro@gmail.com> writes:
>
>> I would like to submit a patch that adds support for logind to
>> `org-user-idle-seconds`.  This patch has been working for me for a long
>> while now and I thought it time for me to submit it.  I've contributed
>> to Emacs in the past so my paperwork should be on file.
>
> Thanks!
>
> Though I do not see any commits associated with Nathaniel Nicandro or
> your email in Emacs git repo.
> Bastien, may you check FSF records?
>
>> Let me know if any changes should be made.
>>
>> lisp/org-clock.el | 29 +++++++++++++++++++++++++++++
>>  1 file changed, 29 insertions(+)
>
> Please provide the commit message. See
> https://orgmode.org/worg/org-contribute.html#commit-messages

Done in the attached updated patch.

>
>> +(defvar org-logind-dbus-session-path
>> +  (when (and (boundp 'dbus-runtime-version)
>> +             (require 'dbus nil t)
>> +             (member "org.freedesktop.login1" (dbus-list-activatable-names)))
>> +    (dbus-call-method
>> +     :system "org.freedesktop.login1"
>> +     "/org/freedesktop/login1"
>> +     "org.freedesktop.login1.Manager"
>> +     "GetSessionByPID" (emacs-pid))))
>
> Please provide a docstring for the new variable.
>
> Also, this new feature is worth documenting in ORG-NEWS.

Done.

>
> Otherwise, the patch looks good.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Add support for logind v2 --]
[-- Type: text/x-patch, Size: 3424 bytes --]

From 1cc887d1378839c3322c93ebb5a524499b8d3073 Mon Sep 17 00:00:00 2001
From: Nathaniel Nicandro <nathanielnicandro@gmail.com>
Date: Wed, 22 Mar 2023 14:54:57 -0500
Subject: [PATCH] lisp/org-clock.el: Add support for logind

* lisp/org-timer.el (org-logind-dbus-session-path): New variable.
(org-logind-user-idle-seconds): New function.
(org-user-idle-seconds): Use them.

---
 etc/ORG-NEWS      |  7 +++++++
 lisp/org-clock.el | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 87ecd77..ef3a2ab 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -87,6 +87,13 @@ a date range in the agenda.  It inherits from the default face in
 order to remain backward-compatible.
 
 ** New features
+*** Add support for ~logind~ idle time in ~org-user-idle-seconds~
+
+When Emacs is built with =dbus= support and
+the =org.freedesktop.login1= interface is available, fallback to
+checking the =IdleSinceHint= property when
+determining =org-user-idle-seconds= as the penultimate step.
+
 *** ~org-metaup~ and ~org-metadown~ now act on headings in region
 
 When region is active and starts at a heading, ~org-metaup~ and
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index f869b0b..323efa4 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -51,6 +51,9 @@ (declare-function org-table-goto-line "org-table" (n))
 (declare-function org-dynamic-block-define "org" (type func))
 (declare-function w32-notification-notify "w32fns.c" (&rest params))
 (declare-function w32-notification-close "w32fns.c" (&rest params))
+(declare-function dbus-list-activatable-names "dbus" (&optional bus))
+(declare-function dbus-call-method "dbus" (bus service path interface method &rest args))
+(declare-function dbus-get-property "dbus" (bus service path interface property))
 
 (defvar org-frame-title-format-backup nil)
 (defvar org-state)
@@ -1214,6 +1217,26 @@ (defun org-x11-idle-seconds ()
   "Return the current X11 idle time in seconds."
   (/ (string-to-number (shell-command-to-string org-clock-x11idle-program-name)) 1000))
 
+(defvar org-logind-dbus-session-path
+  (when (and (boundp 'dbus-runtime-version)
+             (require 'dbus nil t)
+             (member "org.freedesktop.login1" (dbus-list-activatable-names)))
+    (dbus-call-method
+     :system "org.freedesktop.login1"
+     "/org/freedesktop/login1"
+     "org.freedesktop.login1.Manager"
+     "GetSessionByPID" (emacs-pid)))
+  "D-Bus session path for the elogind interface.")
+
+(defun org-logind-user-idle-seconds ()
+  "Return the number of idle seconds for the user according to logind."
+  (- (float-time)
+     (/ (dbus-get-property
+         :system "org.freedesktop.login1"
+         org-logind-dbus-session-path
+         "org.freedesktop.login1.Session" "IdleSinceHint")
+        1e6)))
+
 (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 +1245,13 @@ (defun org-user-idle-seconds ()
     (org-mac-idle-seconds))
    ((and (eq window-system 'x) org-x11idle-exists-p)
     (org-x11-idle-seconds))
+   ((and
+     org-logind-dbus-session-path
+     (dbus-get-property
+      :system "org.freedesktop.login1"
+      org-logind-dbus-session-path
+      "org.freedesktop.login1.Session" "IdleHint"))
+    (org-logind-user-idle-seconds))
    (t
     (org-emacs-idle-seconds))))
 
-- 
2.39.1


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


-- 
Nathaniel

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

* Re: [PATCH] org-user-idle-seconds: Add support for logind
  2023-03-22 21:15   ` Nathaniel Nicandro
@ 2023-03-23 11:46     ` Ihor Radchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2023-03-23 11:46 UTC (permalink / raw)
  To: Nathaniel Nicandro; +Cc: Bastien, emacs-orgmode

Nathaniel Nicandro <nathanielnicandro@gmail.com> writes:

> From 1cc887d1378839c3322c93ebb5a524499b8d3073 Mon Sep 17 00:00:00 2001
> From: Nathaniel Nicandro <nathanielnicandro@gmail.com>
> Date: Wed, 22 Mar 2023 14:54:57 -0500
> Subject: [PATCH] lisp/org-clock.el: Add support for logind

Thanks!
Applied, onto main. I added a changelog entry for ORG-NEWS.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=693df6fd9

I also updated our FSF copyright status records.
https://git.sr.ht/~bzg/worg/commit/dd4222ac

-- 
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] 5+ messages in thread

end of thread, other threads:[~2023-03-23 11:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-21 15:39 [PATCH] org-user-idle-seconds: Add support for logind Nathaniel Nicandro
2023-03-22 12:22 ` Ihor Radchenko
2023-03-22 17:47   ` Bastien Guerry
2023-03-22 21:15   ` Nathaniel Nicandro
2023-03-23 11:46     ` 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).