Recent

Author Topic: How to get user idle time in Linux?  (Read 4507 times)

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: How to get user idle time in Linux?
« Reply #15 on: June 23, 2022, 09:21:39 pm »
The easy way is use Tprocess or TprocessAsynch and call top and parse its output

I didn't quite understand what you meant. Could you explain, please?

AFFRIZA 亜風実

  • Full Member
  • ***
  • Posts: 144
Re: How to get user idle time in Linux?
« Reply #16 on: June 23, 2022, 09:25:51 pm »
It's a pity that there are no elegant solution for any Linux distribution and any graphical environment.
That's an unfortunate reality, since we're dealing with a broad community which is too diverse. The Qt vs GTK itself is already makes me frustrated.

Like I say, if you is okay to use XServer, that XEventWatcher implementation should work but the cost it won't work in Wayland. I probably would make a Wayland example if I'm free in this weekend with the package Fred vS mentioned.
« Last Edit: June 23, 2022, 09:27:55 pm by AFFRIZA 亜風実 »
Kyoukai Framework: https://github.com/afuriza/kyoukai_framework

Dukung kemerdekaan Donetsk dan Lugansk! Tidak membalas profil berbendera biru-kuning apalagi ber-Bandera.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: How to get user idle time in Linux?
« Reply #17 on: June 23, 2022, 09:34:42 pm »
It's a pity that there are no elegant solution for any Linux distribution and any graphical environment.
That's an unfortunate reality, since we're dealing with a broad community which is too diverse. The Qt vs GTK itself is already makes me frustrated.

Like I say, if you is okay to use XServer, that XEventWatcher implementation should work but the cost it won't work in Wayland. I probably would make a Wayland example if I'm free in this weekend with the package Fred vS mentioned.

OP asked for "user idle time", which I interpreted as processor load rather than the time of the last user input.

If what's really needed is to monitor /all/ user input, then use the kernel's event API through which all keyboard, mouse etc. events pass.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: How to get user idle time in Linux?
« Reply #18 on: June 23, 2022, 09:52:05 pm »
I meant this:
the time of the last user input.

I found code on Python: https://gist.github.com/jamesadney/1879754

How to write the same with Free Pascal?

AFFRIZA 亜風実

  • Full Member
  • ***
  • Posts: 144
Re: How to get user idle time in Linux?
« Reply #19 on: June 23, 2022, 09:57:58 pm »

OP asked for "user idle time", which I interpreted as processor load rather than the time of the last user input.

If what's really needed is to monitor /all/ user input, then use the kernel's event API through which all keyboard, mouse etc. events pass.

MarkMLl
Just look at the Python code, it exactly does what my unit does! User idle time is user don't input anything, while other all input is can make a resource jump even you're not inputting something like scheduled service or anything  >:D

I meant this:
the time of the last user input.

I found code on Python: https://gist.github.com/jamesadney/1879754

How to write the same with Free Pascal?
That's basically similar what my unit does!, you can customise that exactly like what that Python code does. But, remember, it only works in X11, just as that Python does using X11 library.
Did you even read this? https://github.com/kirana-a2district/kiranacore-pkg/blob/main/src/xeventwatcher.pas
« Last Edit: June 23, 2022, 10:46:45 pm by AFFRIZA 亜風実 »
Kyoukai Framework: https://github.com/afuriza/kyoukai_framework

Dukung kemerdekaan Donetsk dan Lugansk! Tidak membalas profil berbendera biru-kuning apalagi ber-Bandera.

AFFRIZA 亜風実

  • Full Member
  • ***
  • Posts: 144
Re: How to get user idle time in Linux?
« Reply #20 on: June 23, 2022, 10:11:12 pm »
This the example:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   // use the unit here, right.
  10.   XEventWatcher, DateUtils;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Timer1: TTimer;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.   private
  22.     // put the watcher here
  23.     Watcher: TXEventWatcherThread;
  24.     lasttime: TDateTime;
  25.  
  26.     procedure Update;
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   Watcher := TXEventWatcherThread.Create(True);
  43.   Watcher.NotifyUpdate := @Update;
  44.   Watcher.Start;
  45. end;
  46.  
  47. procedure TForm1.Update;
  48. begin
  49.   // update after use input
  50.   lasttime := Now;
  51. end;
  52.  
  53. procedure TForm1.FormDestroy(Sender: TObject);
  54. begin
  55.   Watcher.Terminate;
  56. end;
  57.  
  58. procedure TForm1.Timer1Timer(Sender: TObject);
  59. var
  60.   idle: integer;
  61. begin
  62.   // calculate seconds between
  63.   idle := SecondsBetween(lasttime, now);
  64.   Caption := 'Last user input: '+ DateTimeToStr(lasttime) + ', idle: ' +idle.ToString;
  65. end;
  66.  
  67. end.
  68.  
  69.  
updated
« Last Edit: June 23, 2022, 10:37:13 pm by AFFRIZA 亜風実 »
Kyoukai Framework: https://github.com/afuriza/kyoukai_framework

Dukung kemerdekaan Donetsk dan Lugansk! Tidak membalas profil berbendera biru-kuning apalagi ber-Bandera.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: How to get user idle time in Linux?
« Reply #21 on: June 23, 2022, 10:22:02 pm »
But, remember, it only works in X11.

Exactly. So I pointed out what API you'd use to work with /anything/, even if no GUI desktop is in use.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AFFRIZA 亜風実

  • Full Member
  • ***
  • Posts: 144
Re: How to get user idle time in Linux?
« Reply #22 on: June 23, 2022, 10:26:42 pm »
Exactly. So I pointed out what API you'd use to work with /anything/, even if no GUI desktop is in use.

MarkMLl
User give an example with TForm code, so I assumed it require GUI, especially to detect mouse. Just use the context of the discussion. Processor load in your comment doesn't even make any sense, because many things can causes processor load.
Kyoukai Framework: https://github.com/afuriza/kyoukai_framework

Dukung kemerdekaan Donetsk dan Lugansk! Tidak membalas profil berbendera biru-kuning apalagi ber-Bandera.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: How to get user idle time in Linux?
« Reply #23 on: June 24, 2022, 08:59:31 am »
Maybe you can look to unit I've made here if you're using XServer: https://github.com/kirana-a2district/kiranacore-pkg/blob/main/src/xeventwatcher.pas

Just to be sure in case this isn't obvious: if you have a system that uses Wayland instead of X11 then this won't help you.

For info, each Wayland system has, by default, XWayland installed that will (afaik) translate idle check.

No, this is not the case (e.g. ArchLinux doesn't install XWayland if you install Wayland) and even if it is installed this does not mean that it is running thus you can't assume that this works.

For info, each Wayland system has, by default, XWayland installed that will (afaik) translate idle check.
I've tried to make a sample app using my XEventWatcher unit in KDE Wayland and it doesn't register any input at all, it just looping in the XNextEvent for eternity.

If there's no X there can't be any X events...

For info, each Wayland system has, by default, XWayland installed that will (afaik) translate idle check.
I've tried to make a sample app using my XEventWatcher unit in KDE Wayland and it doesn't register any input at all, it just looping in the XNextEvent for eternity.

Hum, could you be kind and create a issue here? :
https://gitlab.freedesktop.org/xorg/xserver/-/issues

Why should there be an issue? If no XWayland is running it's perfectly normal for there to be no X11 events.

Hello.

https://github.com/andrewd207/fpc-wayland
Thanks. But, this is not official in FPC(yet)?

So? Not everything needs to be part of FPC. Especially not units to interface with third party components. They can be, but no one said they must. And considering FPC's life cycle even if we'd add them it might be one or two years till a version which contains them is released. So better use what is provided externally.

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: How to get user idle time in Linux?
« Reply #24 on: June 24, 2022, 10:22:17 am »
I assumed it require GUI

You are right. My program has window and can be minimized to tray icon.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: How to get user idle time in Linux?
« Reply #25 on: June 24, 2022, 11:16:42 am »
No, this is not the case (e.g. ArchLinux doesn't install XWayland if you install Wayland) and even if it is installed this does not mean that it is running thus you can't assume that this works.

Hello Sven.
You seem in bad mood today.
Anyway, if XWayland is not installed, no X11/Xorg applications can run or any call to X11 methods can be done so, indeed, idle check using XNextEvent cannot be done  ;)..

Why should there be an issue? If no XWayland is running it's perfectly normal for there to be no X11 events.

I was talking about a issue of XWayland who is part of X11/Xorg project and, of course, for XWayland running on a Wayland system.

So? Not everything needs to be part of FPC. Especially not units to interface with third party components. They can be, but no one said they must. And considering FPC's life cycle even if we'd add them it might be one or two years till a version which contains them is released. So better use what is provided externally.

OK, ok, you really need some holidays and chill out a few.

Peace Sven.

Fre;D
« Last Edit: June 24, 2022, 11:51:54 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

artem101

  • Jr. Member
  • **
  • Posts: 84
Re: How to get user idle time in Linux?
« Reply #26 on: June 24, 2022, 12:15:28 pm »
This the example:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   // use the unit here, right.
  10.   XEventWatcher, DateUtils;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     Timer1: TTimer;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.     procedure Timer1Timer(Sender: TObject);
  21.   private
  22.     // put the watcher here
  23.     Watcher: TXEventWatcherThread;
  24.     lasttime: TDateTime;
  25.  
  26.     procedure Update;
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   Watcher := TXEventWatcherThread.Create(True);
  43.   Watcher.NotifyUpdate := @Update;
  44.   Watcher.Start;
  45. end;
  46.  
  47. procedure TForm1.Update;
  48. begin
  49.   // update after use input
  50.   lasttime := Now;
  51. end;
  52.  
  53. procedure TForm1.FormDestroy(Sender: TObject);
  54. begin
  55.   Watcher.Terminate;
  56. end;
  57.  
  58. procedure TForm1.Timer1Timer(Sender: TObject);
  59. var
  60.   idle: integer;
  61. begin
  62.   // calculate seconds between
  63.   idle := SecondsBetween(lasttime, now);
  64.   Caption := 'Last user input: '+ DateTimeToStr(lasttime) + ', idle: ' +idle.ToString;
  65. end;
  66.  
  67. end.
  68.  
  69.  
updated

I tried this, but it detects any activity only when I resize form or move mouse over program tab in task bar. I use Linux Mint with Mate.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6683
Re: How to get user idle time in Linux?
« Reply #27 on: June 24, 2022, 12:24:21 pm »
Exactly. So I pointed out what API you'd use to work with /anything/, even if no GUI desktop is in use.

MarkMLl
User give an example with TForm code, so I assumed it require GUI, especially to detect mouse. Just use the context of the discussion. Processor load in your comment doesn't even make any sense, because many things can causes processor load.

Yes, but he doesn't say that he wants to only see GUI events. If he wants to see /everything/ he should be using the kernel event API, starting off with monitoring activity using evtest.

https://www.freedesktop.org/software/libevdev/doc/latest/

MarkMLl
« Last Edit: June 24, 2022, 12:52:43 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: How to get user idle time in Linux?
« Reply #28 on: June 24, 2022, 12:57:37 pm »
You seem in bad mood today.

I'm not in a bad mood, but I'm annoyed if people make assumptions and sell them as facts.

AFFRIZA 亜風実

  • Full Member
  • ***
  • Posts: 144
Re: How to get user idle time in Linux?
« Reply #29 on: June 24, 2022, 02:41:35 pm »
Yes, but he doesn't say that he wants to only see GUI events. If he wants to see /everything/ he should be using the kernel event API, starting off with monitoring activity using evtest.

https://www.freedesktop.org/software/libevdev/doc/latest/

MarkMLl
Then, give another solution, don't just boosting a forum stat here.


I tried this, but it detects any activity only when I resize form or move mouse over program tab in task bar. I use Linux Mint with Mate.
I'll check later.
« Last Edit: June 24, 2022, 02:44:14 pm by AFFRIZA 亜風実 »
Kyoukai Framework: https://github.com/afuriza/kyoukai_framework

Dukung kemerdekaan Donetsk dan Lugansk! Tidak membalas profil berbendera biru-kuning apalagi ber-Bandera.

 

TinyPortal © 2005-2018