-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathidle.pas
126 lines (100 loc) · 2.99 KB
/
idle.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
unit Idle;
// FixMe: In Linux relies on X11. What about Wayland and others?
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
{ Returns how much time passed (in milliseconds) since last user activity
(mouse or keyboard events). Also works if current application window has
no focus (hidden or minimized). }
function UserIdleTime: Cardinal;
implementation
uses
{$IfDef Windows}
Windows
{$EndIf}
{$IfDef Linux}
x, xlib, dynlibs, ctypes
{$EndIf}
;
{$IfDef Windows}
type
TLastInputInfo = record
cbSize: UINT;
dwTime: DWORD;
end;
function GetLastInputInfo(var plii: TLastInputInfo): BOOL; stdcall; external 'user32' name 'GetLastInputInfo';
{$EndIf}
{$IfDef Linux}
type
TXScreenSaverInfo = record
Window: culong;
State: cint;
Kind: cint;
TilOrSince: culong;
Idle: culong;
EventMask: culong;
end;
PXScreenSaverInfo = ^TXScreenSaverInfo;
TXScreenSaverAllocInfoFunc = function: Pointer {PXScreenSaverInfo}; cdecl;
TXScreenSaverQueryInfoFunc = function(ADisplay: PXDisplay; ADrawable: TDrawable;
AScreenSaverInfo: PXScreenSaverInfo): TStatus; cdecl;
var
XScreenSaverLib: TLibHandle;
XScreenSaverAllocInfo: TXScreenSaverAllocInfoFunc;
XScreenSaverQueryInfo: TXScreenSaverQueryInfoFunc;
{$EndIf}
function UserIdleTime: Cardinal;
{$IfDef Windows}
var
LastInputInfo: TLastInputInfo;
begin
LastInputInfo.cbSize := SizeOf(TLastInputInfo);
if GetLastInputInfo(LastInputInfo) then
Result := GetTickCount - LastInputInfo.dwTime
else
Result := 0;
end;
{$EndIf}
{$IfDef Linux}
var
DisplayName: String;
Display: PDisplay;
RootWnd: TWindow;
XScreenSaverInfo: PXScreenSaverInfo;
begin
Result := 0;
if XScreenSaverLib = dynlibs.NilHandle then
Exit;
XScreenSaverInfo := XScreenSaverAllocInfo();
if Assigned(XScreenSaverInfo) then
begin
DisplayName := GetEnvironmentVariable('DISPLAY');
Display := XOpenDisplay(PChar(DisplayName));
RootWnd := RootWindow(Display, DefaultScreen(Display));
if XScreenSaverQueryInfo(Display, RootWnd, XScreenSaverInfo) <> 0 then
begin
Result := XScreenSaverInfo^.Idle;
end;
if (Assigned(Display)) then
XCloseDisplay(Display);
XFree(XScreenSaverInfo);
end;
end;
{$EndIf}
{$IfDef Linux}
initialization
XScreenSaverLib := LoadLibrary('libXss.so.1');
if XScreenSaverLib = dynlibs.NilHandle then
raise Exception.Create('Could not load library libXss');
XScreenSaverAllocInfo := TXScreenSaverAllocInfoFunc(GetProcedureAddress(XScreenSaverLib, 'XScreenSaverAllocInfo'));
if not Assigned(XScreenSaverAllocInfo) then
raise Exception.Create('Could not find XScreenSaverAllocInfo function in libXss');
XScreenSaverQueryInfo := TXScreenSaverQueryInfoFunc(GetProcedureAddress(XScreenSaverLib, 'XScreenSaverQueryInfo'));
if not Assigned(XScreenSaverQueryInfo) then
raise Exception.Create('Could not find XScreenSaverQueryInfo function in libXss');
finalization;
if XScreenSaverLib <> dynlibs.NilHandle then
UnloadLibrary(XScreenSaverLib);
{$EndIf}
end.