-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxrrscreenresources.cpp
More file actions
220 lines (194 loc) · 6.98 KB
/
xrrscreenresources.cpp
File metadata and controls
220 lines (194 loc) · 6.98 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright 2020 Pascal COMBES <pascom@orange.fr>
*
* This file is part of ShutdownMonitor.
*
* ShutdownMonitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ShutdownMonitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ShutdownMonitor. If not, see <http://www.gnu.org/licenses/>
*/
#include "xrrscreenresources.h"
#include "xrroutput.h"
#include "xrrcrtc.h"
#if QT_VERSION >= 0x060000
# include <QtGui>
#else // QT_VERSION
# include <QX11Info>
#endif // QT_VERSION
#include <QtDebug>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
QString XRandRScreenResources::name = "X11";
QScreenResources* XRandRScreenResources::create(bool forceBackend)
{
#if QT_VERSION >= 0x060000
QNativeInterface::QX11Application* x11App = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
if (x11App != nullptr)
return XRandRScreenResources::getCurrent(x11App->display());
#else // QT_VERSION
if (QX11Info::isPlatformX11())
return XRandRScreenResources::getCurrent(QX11Info::display());
#endif // QT_VERSION
if (forceBackend)
qWarning() << QObject::tr("This backend only supports X11");
return nullptr;
}
XRandRScreenResources* XRandRScreenResources::get(Display* display)
{
Window root = DefaultRootWindow(display);
return new XRandRScreenResources(display, XRRGetScreenResources(display, root));
}
XRandRScreenResources* XRandRScreenResources::getCurrent(Display* display)
{
Window root = DefaultRootWindow(display);
return new XRandRScreenResources(display, XRRGetScreenResourcesCurrent(display, root));
}
XRandRScreenResources::XRandRScreenResources(Display *display, XRRScreenResources *resources)
: QScreenResources(XRandRScreenResources::name), mDisplay(display), mResources(resources)
{}
XRandRScreenResources::~XRandRScreenResources(void)
{
qDeleteAll(mCrtcs);
if (mResources != nullptr)
XRRFreeScreenResources(mResources);
}
void XRandRScreenResources::refreshOutputs(void)
{
qDeleteAll(mOutputs);
for (int o = 0; o < mResources->noutput; o++) {
XRROutputInfo* info = XRRGetOutputInfo(mDisplay, mResources, mResources->outputs[o]);
mOutputs.insert(mResources->outputs[o], new XRandROutput(this, info));
XRRFreeOutputInfo(info);
}
}
XRandRCrtc* XRandRScreenResources::crtc(RRCrtc crtcId)
{
if (crtcId == None)
return nullptr;
if (!mCrtcs.contains(crtcId)) {
XRRCrtcInfo* info = XRRGetCrtcInfo(mDisplay, mResources, crtcId);
mCrtcs.insert(crtcId, new XRandRCrtc(this, info));
XRRFreeCrtcInfo(info);
}
return mCrtcs.value(crtcId);
}
bool XRandRScreenResources::enableOutput(QOutput *output, bool grab)
{
// Cast output to internal type:
XRandROutput* xOutput = dynamic_cast<XRandROutput*>(output);
if (xOutput == nullptr)
return false;
// The output is already enabled:
if (xOutput->mEnabled)
return true;
// The output CRTC should be in the CRTC map:
if (!mCrtcs.contains(xOutput->mCrtcId))
return false;
// Update the CRTCs:
bool ans = true;
bool oldOutputState = xOutput->mEnabled;
xOutput->mEnabled = true;
QRect totalScreen = computeTotalScreen();
QRect newScreen = computeScreen();
if (grab)
XGrabServer(mDisplay);
for (auto it = mCrtcs.constBegin(); it != mCrtcs.constEnd(); it++)
ans &= updateCrtcOrigin(it.key(), it.value()->rect().topLeft() - newScreen.topLeft() + totalScreen.topLeft());
if (grab)
XUngrabServer(mDisplay);
if (!ans)
xOutput->mEnabled = oldOutputState;
return ans;
}
bool XRandRScreenResources::disableOutput(QOutput *output, bool grab)
{
// Cast output to internal type:
XRandROutput* xOutput = dynamic_cast<XRandROutput*>(output);
if (xOutput == nullptr)
return false;
// The output is already disabled:
if (!xOutput->mEnabled)
return true;
// The output CRTC should be in the CRTC map:
if (!mCrtcs.contains(xOutput->mCrtcId))
return false;
// Update the CRTCs:
bool ans = true;
bool oldOutputState = xOutput->mEnabled;
xOutput->mEnabled = false;
QRect totalScreen = computeTotalScreen();
QRect newScreen = computeScreen();
if (newScreen.isNull()) {
xOutput->mEnabled = true;
return false;
}
if (grab)
XGrabServer(mDisplay);
for (auto it = mCrtcs.constBegin(); it != mCrtcs.constEnd(); it++)
ans &= updateCrtcOrigin(it.key(), it.value()->rect().topLeft() - newScreen.topLeft() + totalScreen.topLeft());
if (grab)
XUngrabServer(mDisplay);
if (!ans)
xOutput->mEnabled = oldOutputState;
return ans;
}
QRect XRandRScreenResources::computeTotalScreen(void) const
{
QRect screen;
foreach (QOutput* output, mOutputs) {
XRandROutput* xOutput = dynamic_cast<XRandROutput*>(output);
if (xOutput == nullptr)
continue;
if (!mCrtcs.contains(xOutput->mCrtcId))
continue;
screen |= mCrtcs.value(xOutput->mCrtcId)->rect();
}
return screen;
}
QRect XRandRScreenResources::computeScreen(void) const
{
QRect screen;
foreach (QOutput* output, mOutputs) {
XRandROutput* xOutput = dynamic_cast<XRandROutput*>(output);
if (xOutput == nullptr)
continue;
if (!mCrtcs.contains(xOutput->mCrtcId))
continue;
if (xOutput->mEnabled)
screen |= mCrtcs.value(xOutput->mCrtcId)->rect();
}
return screen;
}
bool XRandRScreenResources::updateCrtcOrigin(RRCrtc crtcId, const QPoint& newOrigin)
{
// Get the CRTC internal representation:
XRandRCrtc* crtc = mCrtcs.value(crtcId);
// Get the associated enabled outputs:
QList<RROutput> crtcOutputs = crtc->outputs;
for (auto it = crtcOutputs.begin(); it != crtcOutputs.end();) {
XRandROutput* o = dynamic_cast<XRandROutput*>(output(*it));
if ((o == nullptr) || !o->mEnabled)
it = crtcOutputs.erase(it);
else
it++;
}
// Update the CRTC origin or disable it if there is not any associated enabled output:
Status s;
QVector<RROutput> outputs = QVector<RROutput>::fromList(crtcOutputs);
if (outputs.isEmpty())
s = XRRSetCrtcConfig(mDisplay, mResources, crtcId, CurrentTime,
0, 0, None, RR_Rotate_0, NULL, 0);
else
s = XRRSetCrtcConfig(mDisplay, mResources, crtcId, CurrentTime,
newOrigin.x(), newOrigin.y(), crtc->mode, crtc->rotation,
outputs.data(), outputs.size());
return (s == RRSetConfigSuccess);
}