Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kcontrol / randr / legacyrandrscreen.cpp
blob6acc32af2cecd99fc904d0926635f4e12de45fdf
1 /*
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.
22 #include <QTimer>
23 //Added by qt3to4:
24 #include <QPixmap>
25 #include "ktimerdialog.h"
27 #include <kdebug.h>
28 #include <klocale.h>
29 #include <kglobal.h>
30 #include <kapplication.h>
31 #include <kiconloader.h>
32 #include <QtGui/QLabel>
33 #include <QDesktopWidget>
35 #include <QX11Info>
36 #include "legacyrandrscreen.h"
38 LegacyRandRScreen::LegacyRandRScreen(int screenIndex)
39 : m_config(0L)
40 , m_screen(screenIndex)
41 , m_shownDialog(NULL)
43 loadSettings();
44 setOriginal();
47 LegacyRandRScreen::~LegacyRandRScreen()
49 if (m_config)
50 XRRFreeScreenConfigInfo(m_config);
53 void LegacyRandRScreen::loadSettings()
55 if (m_config)
56 XRRFreeScreenConfigInfo(m_config);
58 m_config = XRRGetScreenInfo(QX11Info::display(), rootWindow());
59 Q_ASSERT(m_config);
61 Rotation rotation;
62 m_currentSize = m_proposedSize = XRRConfigCurrentConfiguration(m_config, &rotation);
63 m_currentRotation = m_proposedRotation = rotation;
65 m_pixelSizes.clear();
66 m_mmSizes.clear();
67 int numSizes;
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());
90 Status status;
92 if (proposedRefreshRate() < 0)
93 status = XRRSetScreenConfig(QX11Info::display(), m_config, rootWindow(), (SizeID)proposedSize(), (Rotation)proposedRotation(), CurrentTime);
94 else {
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;
107 return true;
110 return false;
113 bool LegacyRandRScreen::applyProposedAndConfirm()
115 if (proposedChanged()) {
116 setOriginal();
118 if (applyProposed()) {
119 if (!RandR::confirm()) {
120 proposeOriginal();
121 applyProposed();
122 return false;
124 } else {
125 return false;
129 return true;
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());
144 else
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);
176 else
177 ret += ", " + RandR::rotationName(m_currentRotation & (RR_Reflect_X + RR_Reflect_X), true, false);
179 return ret;
182 int LegacyRandRScreen::rotationIndexToDegree(int rotation) const
184 switch (rotation & RandR::RotateMask) {
185 case RR_Rotate_90:
186 return 90;
188 case RR_Rotate_180:
189 return 180;
191 case RR_Rotate_270:
192 return 270;
194 default:
195 return 0;
199 int LegacyRandRScreen::rotationDegreeToIndex(int degree) const
201 switch (degree) {
202 case 90:
203 return RR_Rotate_90;
205 case 180:
206 return RR_Rotate_180;
208 case 270:
209 return RR_Rotate_270;
211 default:
212 return RR_Rotate_0;
216 const QSize& LegacyRandRScreen::currentPixelSize() const
218 return m_pixelSizes[m_currentSize];
221 RateList LegacyRandRScreen::refreshRates(int size) const
223 int nrates;
224 short* rrates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
226 RateList rateList;
227 for (int i = 0; i < nrates; i++)
228 rateList.append(rrates[i]);
230 return rateList;
233 QString LegacyRandRScreen::refreshRateDirectDescription(int rate) const
235 return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", rate);
238 QString LegacyRandRScreen::refreshRateIndirectDescription(int size, int index) const
240 return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", refreshRateIndexToHz(size, index));
243 QString LegacyRandRScreen::refreshRateDescription(int size, int index) const
245 return ki18n("%1 Hz").subs(refreshRates(size)[index], 0, 'f', 1).toString();
248 bool LegacyRandRScreen::proposeRefreshRate(int index)
250 if (index >= 0 && (int)refreshRates(proposedSize()).count() > index) {
251 m_proposedRefreshRate = index;
252 return true;
255 return false;
258 int LegacyRandRScreen::refreshRate() const
260 return m_currentRefreshRate;
263 QString LegacyRandRScreen::currentRefreshRateDescription() const
265 return refreshRateIndirectDescription(m_currentSize, m_currentRefreshRate);
268 int LegacyRandRScreen::proposedRefreshRate() const
270 return m_proposedRefreshRate;
273 int LegacyRandRScreen::refreshRateHzToIndex(int size, int hz) const
275 int nrates;
276 short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
278 for (int i = 0; i < nrates; i++)
279 if (hz == rates[i])
280 return i;
282 if (nrates != 0)
283 // Wrong input Hz!
284 Q_ASSERT(false);
286 return -1;
289 int LegacyRandRScreen::refreshRateIndexToHz(int size, int index) const
291 int nrates;
292 short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
294 if (nrates == 0 || index < 0)
295 return 0;
297 // Wrong input Hz!
298 if(index >= nrates)
299 return 0;
301 return rates[index];
304 int LegacyRandRScreen::numSizes() const
306 return m_pixelSizes.count();
309 const QSize& LegacyRandRScreen::pixelSize(int index) const
311 return m_pixelSizes[index];
314 const QSize& LegacyRandRScreen::mmSize(int index) const
316 return m_mmSizes[index];
319 int LegacyRandRScreen::sizeIndex(const QSize &pixelSize) const
321 for (int i = 0; i < m_pixelSizes.count(); i++)
322 if (m_pixelSizes[i] == pixelSize)
323 return i;
325 return -1;
328 int LegacyRandRScreen::rotations() const
330 return m_rotations;
333 int LegacyRandRScreen::rotation() const
335 return m_currentRotation;
338 int LegacyRandRScreen::size() const
340 return m_currentSize;
343 int LegacyRandRScreen::proposedRotation() const
345 return m_proposedRotation;
348 void LegacyRandRScreen::proposeRotation(int newRotation)
350 m_proposedRotation = newRotation & RandR::OrientationMask;
353 int LegacyRandRScreen::proposedSize() const
355 return m_proposedSize;
358 bool LegacyRandRScreen::proposeSize(int newSize)
360 if ((int)m_pixelSizes.count() > newSize) {
361 m_proposedSize = newSize;
362 return true;
365 return false;
368 void LegacyRandRScreen::load(KConfig& config)
370 KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));
372 if (proposeSize(sizeIndex(group.readEntry("size", currentPixelSize()))))
373 proposeRefreshRate(refreshRateHzToIndex(proposedSize(), group.readEntry("refresh", refreshRate())));
375 proposeRotation(rotationDegreeToIndex( group.readEntry("rotation", 0)) +
376 (group.readEntry("reflectX", false) ? RandR::ReflectX : 0) +
377 (group.readEntry("reflectY",false) ? RandR::ReflectY : 0));
380 void LegacyRandRScreen::save(KConfig& config) const
382 KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));
383 group.writeEntry("size", currentPixelSize());
384 group.writeEntry("refresh", refreshRateIndexToHz(size(), refreshRate()));
385 group.writeEntry("rotation", rotationIndexToDegree(rotation()));
386 group.writeEntry("reflectX", (bool)(rotation() & RandR::ReflectMask) == RandR::ReflectX);
387 group.writeEntry("reflectY", (bool)(rotation() & RandR::ReflectMask) == RandR::ReflectY);
390 int LegacyRandRScreen::pixelCount( int index ) const
392 QSize sz = pixelSize(index);
393 return sz.width() * sz.height();
396 SizeList LegacyRandRScreen::pixelSizes() const
398 return m_pixelSizes;
401 #include "legacyrandrscreen.moc"