GetIdleTime()

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

GetIdleTime()

Post by infratec »

Returns the idle time in seconds on all OSs

Was needed for an automatic logout after a defined time of no user action at the PC.

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


CompilerSelect #PB_Compiler_OS
    
  CompilerCase #PB_OS_Windows
    
    Procedure.i GetIdleTime()
      
      Static LastTime.q
      Static LastDwTime.l
      Protected lii.LASTINPUTINFO
      Protected Result.i
      
      
      lii\cbSize = SizeOf(LASTINPUTINFO)
      
      If GetLastInputInfo_(@lii)
        
        If LastTime = 0
          LastTime = Date()
        EndIf
        If LastDwTime = 0
          LastDwTime = lii\dwTime
        EndIf
        
        ;Debug "lii\dwTime: " + Str(lii\dwTime)
        If lii\dwTime <> LastDwTime
          LastDwTime = lii\dwTime
          ;Debug "LastDwTime: " + Str(LastDwTime)
          LastTime = Date()
        EndIf
        Result = Date() - LastTime
        
      EndIf
      
      ProcedureReturn Result
      
    EndProcedure
    
  CompilerCase #PB_OS_Linux
    
    Structure XScreenSaverInfo Align #PB_Structure_AlignC
      window.i        ; screen saver window
      state.l         ; ScreenSaver{Off,On,Disabled}
      kind.l          ; ScreenSaver{Blanked,Internal,External}
      til_or_since.i  ; milliseconds
      idle.i          ; milliseconds
      eventMask.l     ; events
    EndStructure
    
    Macro DefaultRootWindow(disp)
      XDefaultRootWindow(disp)
    EndMacro
    
    ImportC "-lX11"
      XOpenDisplay.i(display.p-utf8)
      XCloseDisplay.i(*display)
      XDefaultRootWindow.i(*display)
    EndImport
    
    ImportC "-lXss"
      XScreenSaverQueryExtension.i(*dpy, *event_base_return, *error_base_return)
      XScreenSaverQueryInfo(*dpy, drawable.i, *saver_info.XScreenSaverInfo)
    EndImport
    
    
    Procedure.i GetIdleTime()
      
      Protected *display
      Protected event_base.i, error_base.i
      Protected info.XScreenSaverInfo
      Protected Result.i
      
      
      *display = XOpenDisplay("")
      If *display
        
        If XScreenSaverQueryExtension(*display, @event_base, @error_base)
          
          If XScreenSaverQueryInfo(*display, DefaultRootWindow(*display), @info)
            
            ;       Debug info\window
            ;       Debug info\state
            ;       Debug info\kind
            ;       Debug info\til_or_since
            ;       Debug info\idle
            ;       Debug info\eventMask
            
            Result = info\idle / 1000
          EndIf
          
        Else
          Debug "Error: XScreenSaver Extension not present"
        EndIf
        
        XCloseDisplay(*display)
      EndIf
      
      ProcedureReturn Result
      
    EndProcedure
    
  CompilerCase #PB_OS_MacOS
    
    #kCGAnyInputEventType = ~0
    #hidSystemState = 1
    
    ImportC ""
      CGEventSourceSecondsSinceLastEventType.d(CGEventSourceStateID.l,CGEventType.l)
    EndImport

    
    Procedure.i GetIdleTime()
      
      ProcedureReturn Int(CGEventSourceSecondsSinceLastEventType(#hidSystemState,#kCGAnyInputEventType))
      
    EndProcedure
    
CompilerEndSelect


;-Demo
CompilerIf #PB_Compiler_IsMainFile
  
  Define i.i
  
  
  For i = 0 To 10
    Debug GetIdleTime()
    Delay(1000)
  Next i
  
CompilerEndIf
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: GetIdleTime()

Post by Kwai chang caine »

Works nice here :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: GetIdleTime()

Post by idle »

It's 8:51 am :lol:

thanks, that's useful to have
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: GetIdleTime()

Post by Quin »

Thanks loads, especially for the Mac method. I never found anything about it weirdly, I always found people telling me to use ioreg.
Quin
Addict
Addict
Posts: 1122
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: GetIdleTime()

Post by Quin »

I've noticed a slight bug that I'm not entirely sure if you can work around. On Windows, when changing the timezone, the idle time calculation gets screwed up, thinking I was away for over an hour when I wasn't. You could always try something like GetTickCount64() so we're not limited to 49.7 days
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: GetIdleTime()

Post by RichAlgeni »

If only this could work for QuickBooks!!! When people don't logout, we can't do backups.
Post Reply