-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathx11rotation.pas
169 lines (148 loc) · 3.91 KB
/
x11rotation.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
unit x11rotation;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, xlib, x, xrandr, unixtype;
type
PXRRScreenChangeNotifyEvent = ^TXRRScreenChangeNotifyEvent;
TXRRScreenChangeNotifyEvent = record
_type: cint;
serial: culong;
send_event: TBool;
display: PDisplay;
window: TWindow;
root: TWindow;
timestamp: TTime;
config_timestamp: TTime;
size_index: cint;
subpixel_order: cint;
rotation: cuint;
width: cint;
height: cint;
mwidth: cint;
mheight: cint;
end;
TRotationEvent = procedure(Sender: TObject; IsPortrait: Boolean) of object;
TX11Rotation = class;
TRotationThread = class(TThread)
private
FOwner: TX11Rotation;
protected
procedure Execute; override;
public
constructor Create(AOwner: TX11Rotation);
end;
TX11Rotation = class
private
FDisplay: PDisplay;
FWindow: TWindow;
FScreen: cint;
FRootWindow: TWindow;
FRunning: Boolean;
FRotationAtom: culong; // Use culong for Atom
FOnRotation: TRotationEvent;
FThread: TRotationThread;
procedure HandleScreenChangeEvent(const Event: PXRRScreenChangeNotifyEvent);
public
constructor Create(AWindow: TWindow);
destructor Destroy; override;
procedure Start;
procedure Stop;
procedure SetRotationAtom(IsPortrait: Boolean);
property OnRotation: TRotationEvent read FOnRotation write FOnRotation;
end;
implementation
const
XA_CARDINAL = 6;
constructor TX11Rotation.Create(AWindow: TWindow);
begin
FDisplay := XOpenDisplay(nil);
if FDisplay = nil then
raise Exception.Create('Unable to open X display');
FWindow := AWindow;
FScreen := DefaultScreen(FDisplay);
FRootWindow := RootWindow(FDisplay, FScreen);
//FRotationAtom := XInternAtom(FDisplay, '_HILDON_PORTRAIT_MODE_SUPPORT', False);
WriteLn('Initialized TX11Rotation');
end;
destructor TX11Rotation.Destroy;
begin
Stop;
inherited Destroy;
end;
procedure TX11Rotation.HandleScreenChangeEvent(const Event: PXRRScreenChangeNotifyEvent);
var
IsPortrait: Boolean;
begin
IsPortrait := Event^.rotation in [RR_Rotate_90, RR_Rotate_270];
if Assigned(FOnRotation) then
FOnRotation(Self, IsPortrait);
//SetRotationAtom(IsPortrait);
WriteLn('Rotation detected: ', IsPortrait);
end;
procedure TX11Rotation.SetRotationAtom(IsPortrait: Boolean);
var
Value: LongInt;
begin
if IsPortrait then
Value := 1
else
Value := 0;
//XChangeProperty(FDisplay, FRootWindow, FRotationAtom, XA_CARDINAL, 32, PropModeReplace, @Value, 1);
WriteLn('Set rotation atom: ', Value);
end;
procedure TX11Rotation.Start;
begin
if not FRunning then
begin
FRunning := True;
FThread := TRotationThread.Create(Self);
FThread.Start;
WriteLn('Started listening for screen change events');
end;
end;
procedure TX11Rotation.Stop;
begin
if FRunning then
begin
FRunning := False;
if Assigned(FThread) then
begin
FThread.Terminate; // Request thread to terminate
FThread.WaitFor;
FreeAndNil(FThread);
end;
XCloseDisplay(FDisplay);
WriteLn('Stopped listening for screen change events');
end;
end;
constructor TRotationThread.Create(AOwner: TX11Rotation);
begin
inherited Create(True);
FOwner := AOwner;
FreeOnTerminate := False;
end;
procedure TRotationThread.Execute;
var
Event: TXEvent;
XRRNotifyEvent: PXRRScreenChangeNotifyEvent;
begin
XRRSelectInput(FOwner.FDisplay, FOwner.FRootWindow, RRScreenChangeNotifyMask);
WriteLn('Waiting for screen change events...');
while not Terminated do
begin
if XPending(FOwner.FDisplay) > 0 then
begin
writeln ('aaaaaaa');
XNextEvent(FOwner.FDisplay, @Event);
if Event._type = RRScreenChangeNotify then
begin
writeln ('bbbbbbb');
XRRNotifyEvent := PXRRScreenChangeNotifyEvent(@Event);
FOwner.HandleScreenChangeEvent(XRRNotifyEvent);
end;
end;
Sleep(100); // Add a small sleep to avoid busy waiting
end;
end;
end.