2
* Copyright (c) 2007 Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
3
* Copyright (c) 2002,2003 Hamish Rodda <rodda@kde.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
#include "ktimerdialog.h"
30
#include <kapplication.h>
31
#include <kiconloader.h>
32
#include <QtGui/QLabel>
33
#include <QDesktopWidget>
36
#include "legacyrandrscreen.h"
38
LegacyRandRScreen::LegacyRandRScreen(int screenIndex)
40
, m_screen(screenIndex)
47
LegacyRandRScreen::~LegacyRandRScreen()
50
XRRFreeScreenConfigInfo(m_config);
53
void LegacyRandRScreen::loadSettings()
56
XRRFreeScreenConfigInfo(m_config);
58
m_config = XRRGetScreenInfo(QX11Info::display(), rootWindow());
62
m_currentSize = m_proposedSize = XRRConfigCurrentConfiguration(m_config, &rotation);
63
m_currentRotation = m_proposedRotation = rotation;
68
XRRScreenSize* sizes = XRRSizes(QX11Info::display(), m_screen, &numSizes);
69
for (int i = 0; i < numSizes; i++) {
70
m_pixelSizes.append(QSize(sizes[i].width, sizes[i].height));
71
m_mmSizes.append(QSize(sizes[i].mwidth, sizes[i].mheight));
74
m_rotations = XRRRotations(QX11Info::display(), m_screen, &rotation);
76
m_currentRefreshRate = m_proposedRefreshRate = refreshRateHzToIndex(m_currentSize, XRRConfigCurrentRate(m_config));
79
void LegacyRandRScreen::setOriginal()
81
m_originalSize = m_currentSize;
82
m_originalRotation = m_currentRotation;
83
m_originalRefreshRate = m_currentRefreshRate;
86
bool LegacyRandRScreen::applyProposed()
88
//kDebug() << " size " << (SizeID)proposedSize() << ", rotation " << proposedRotation() << ", refresh " << refreshRateIndexToHz(proposedSize(), proposedRefreshRate());
92
if (proposedRefreshRate() < 0)
93
status = XRRSetScreenConfig(QX11Info::display(), m_config, rootWindow(), (SizeID)proposedSize(), (Rotation)proposedRotation(), CurrentTime);
95
if( refreshRateIndexToHz(proposedSize(), proposedRefreshRate()) <= 0 ) {
96
m_proposedRefreshRate = 0;
98
status = XRRSetScreenConfigAndRate(QX11Info::display(), m_config, rootWindow(), (SizeID)proposedSize(), (Rotation)proposedRotation(), refreshRateIndexToHz(proposedSize(), proposedRefreshRate()), CurrentTime);
101
//kDebug() << "New size: " << WidthOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), screen)) << ", " << HeightOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), screen));
103
if (status == RRSetConfigSuccess) {
104
m_currentSize = m_proposedSize;
105
m_currentRotation = m_proposedRotation;
106
m_currentRefreshRate = m_proposedRefreshRate;
113
bool LegacyRandRScreen::applyProposedAndConfirm()
115
if (proposedChanged()) {
118
if (applyProposed()) {
119
if (!RandR::confirm()) {
132
Window LegacyRandRScreen::rootWindow() const
134
return RootWindow(QX11Info::display(), m_screen);
137
QString LegacyRandRScreen::changedMessage() const
139
if (refreshRate() == -1)
140
return i18n("New configuration:\nResolution: %1 x %2\nOrientation: %3",
141
currentPixelSize().width(),
142
currentPixelSize().height(),
143
currentRotationDescription());
145
return i18n("New configuration:\nResolution: %1 x %2\nOrientation: %3\nRefresh rate: %4",
146
currentPixelSize().width(),
147
currentPixelSize().height(),
148
currentRotationDescription(),
149
currentRefreshRateDescription());
152
bool LegacyRandRScreen::changedFromOriginal() const
154
return m_currentSize != m_originalSize || m_currentRotation != m_originalRotation || m_currentRefreshRate != m_originalRefreshRate;
157
void LegacyRandRScreen::proposeOriginal()
159
m_proposedSize = m_originalSize;
160
m_proposedRotation = m_originalRotation;
161
m_proposedRefreshRate = m_originalRefreshRate;
164
bool LegacyRandRScreen::proposedChanged() const
166
return m_currentSize != m_proposedSize || m_currentRotation != m_proposedRotation || m_currentRefreshRate != m_proposedRefreshRate;
169
QString LegacyRandRScreen::currentRotationDescription() const
171
QString ret = RandR::rotationName(m_currentRotation & RandR::RotateMask);
173
if (m_currentRotation != (m_currentRotation & RandR::RotateMask)) {
174
if (m_currentRotation & RR_Rotate_0)
175
ret = RandR::rotationName(m_currentRotation & (RR_Reflect_X + RR_Reflect_X), true, true);
177
ret += ", " + RandR::rotationName(m_currentRotation & (RR_Reflect_X + RR_Reflect_X), true, false);
183
int LegacyRandRScreen::rotationIndexToDegree(int rotation) const
185
switch (rotation & RandR::RotateMask) {
200
int LegacyRandRScreen::rotationDegreeToIndex(int degree) const
207
return RR_Rotate_180;
210
return RR_Rotate_270;
217
const QSize& LegacyRandRScreen::currentPixelSize() const
219
return m_pixelSizes[m_currentSize];
222
RateList LegacyRandRScreen::refreshRates(int size) const
225
short* rrates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
228
for (int i = 0; i < nrates; i++)
229
rateList.append(rrates[i]);
234
QString LegacyRandRScreen::refreshRateDirectDescription(int rate) const
236
return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", rate);
239
QString LegacyRandRScreen::refreshRateIndirectDescription(int size, int index) const
241
return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", refreshRateIndexToHz(size, index));
244
QString LegacyRandRScreen::refreshRateDescription(int size, int index) const
246
return ki18n("%1 Hz").subs(refreshRates(size)[index], 0, 'f', 1).toString();
249
bool LegacyRandRScreen::proposeRefreshRate(int index)
251
if (index >= 0 && (int)refreshRates(proposedSize()).count() > index) {
252
m_proposedRefreshRate = index;
259
int LegacyRandRScreen::refreshRate() const
261
return m_currentRefreshRate;
264
QString LegacyRandRScreen::currentRefreshRateDescription() const
266
return refreshRateIndirectDescription(m_currentSize, m_currentRefreshRate);
269
int LegacyRandRScreen::proposedRefreshRate() const
271
return m_proposedRefreshRate;
274
int LegacyRandRScreen::refreshRateHzToIndex(int size, int hz) const
277
short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
279
for (int i = 0; i < nrates; i++)
290
int LegacyRandRScreen::refreshRateIndexToHz(int size, int index) const
293
short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
295
if (nrates == 0 || index < 0)
305
int LegacyRandRScreen::numSizes() const
307
return m_pixelSizes.count();
310
const QSize& LegacyRandRScreen::pixelSize(int index) const
312
return m_pixelSizes[index];
315
const QSize& LegacyRandRScreen::mmSize(int index) const
317
return m_mmSizes[index];
320
int LegacyRandRScreen::sizeIndex(const QSize &pixelSize) const
322
for (int i = 0; i < m_pixelSizes.count(); i++)
323
if (m_pixelSizes[i] == pixelSize)
329
int LegacyRandRScreen::rotations() const
334
int LegacyRandRScreen::rotation() const
336
return m_currentRotation;
339
int LegacyRandRScreen::size() const
341
return m_currentSize;
344
int LegacyRandRScreen::proposedRotation() const
346
return m_proposedRotation;
349
void LegacyRandRScreen::proposeRotation(int newRotation)
351
m_proposedRotation = newRotation & RandR::OrientationMask;
354
int LegacyRandRScreen::proposedSize() const
356
return m_proposedSize;
359
bool LegacyRandRScreen::proposeSize(int newSize)
361
if ((int)m_pixelSizes.count() > newSize) {
362
m_proposedSize = newSize;
369
void LegacyRandRScreen::load(KConfig& config)
371
KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));
373
if (proposeSize(sizeIndex(group.readEntry("size", currentPixelSize()))))
374
proposeRefreshRate(refreshRateHzToIndex(proposedSize(), group.readEntry("refresh", refreshRate())));
376
proposeRotation(rotationDegreeToIndex( group.readEntry("rotation", 0)) +
377
(group.readEntry("reflectX", false) ? RandR::ReflectX : 0) +
378
(group.readEntry("reflectY",false) ? RandR::ReflectY : 0));
381
void LegacyRandRScreen::save(KConfig& config) const
383
KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));
384
group.writeEntry("size", currentPixelSize());
385
group.writeEntry("refresh", refreshRateIndexToHz(size(), refreshRate()));
386
group.writeEntry("rotation", rotationIndexToDegree(rotation()));
387
group.writeEntry("reflectX", (bool)(rotation() & RandR::ReflectMask) == RandR::ReflectX);
388
group.writeEntry("reflectY", (bool)(rotation() & RandR::ReflectMask) == RandR::ReflectY);
391
QStringList LegacyRandRScreen::startupCommands() const
393
QString command = QString("xrandr -s %1x%2 -r %3 ").arg( currentPixelSize().width(),
394
currentPixelSize().height(), refreshRateIndexToHz(size(), refreshRate()));
395
switch( rotation()) {
406
if((rotation() & RandR::ReflectMask) == RandR::ReflectX)
408
if((bool)(rotation() & RandR::ReflectMask) == RandR::ReflectY)
410
return QStringList() << command;
413
int LegacyRandRScreen::pixelCount( int index ) const
415
QSize sz = pixelSize(index);
416
return sz.width() * sz.height();
419
SizeList LegacyRandRScreen::pixelSizes() const
424
#include "legacyrandrscreen.moc"