-
Notifications
You must be signed in to change notification settings - Fork 23
/
xrandreventwatcher.pas
100 lines (74 loc) · 1.94 KB
/
xrandreventwatcher.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
unit XRandREventWatcher;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, x, xlib, xrandr, ctypes;
type
TNotifyProc = procedure(const AEvent: TXEvent) of object;
{ TXRandREventWatcherThread }
TXRandREventWatcherThread = class(TThread)
private
FNotifier: TNotifyProc;
FEventMask: cint;
FLastEvent: TXEvent;
procedure Notify;
protected
procedure Execute; override;
public
Constructor Create({ACreateSuspended: boolean;} AEventMask: cint;
ANotifier: TNotifyProc);
destructor Destroy; override;
end;
implementation
uses LazLoggerBase;
{ TXRandREventWatcherThread }
procedure TXRandREventWatcherThread.Notify;
begin
if Assigned(FNotifier) then
FNotifier(FLastEvent);
end;
procedure TXRandREventWatcherThread.Execute;
var
//DisplayName: String;
Display: PDisplay;
RootWnd: TWindow;
begin
DebugLn(ClassName, '::Execute() started');
//DisplayName := GetEnvironmentVariable('DISPLAY');
Display := XOpenDisplay({PChar(DisplayName)} Nil);
RootWnd := RootWindow(Display, DefaultScreen(Display));
XRRSelectInput(Display, RootWnd, FEventMask);
while True do
begin
if XPending(Display) > 0 then
begin
XNextEvent(Display, @FLastEvent);
DebugLn(['Recieved event with type ', FLastEvent._type]);
Synchronize(@Notify);
end;
if Terminated then
Break;
Sleep(200);
end;
XCloseDisplay(Display);
DebugLn(ClassName, '::Execute() ended');
end;
constructor TXRandREventWatcherThread.Create(AEventMask: cint;
ANotifier: TNotifyProc);
begin
FNotifier := ANotifier;
FEventMask := AEventMask;
FreeOnTerminate := {True} False;
inherited Create(False);
NameThreadForDebugging(ClassName, ThreadID);
DebugLn('Thread ', ClassName ,' started');
end;
destructor TXRandREventWatcherThread.Destroy;
begin
DebugLn('Prepare to destroy thread ', ClassName);
Terminate;
WaitFor;
inherited Destroy;
DebugLn('Thread ', ClassName, 'destroyed');
end;
end.