not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / randr / legacyrandrscreen.cpp
blob6d70f2bee0782a95da2081a48d69edda4d33a9f6
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);
180 return ret;
183 int LegacyRandRScreen::rotationIndexToDegree(int rotation) const
185 switch (rotation & RandR::RotateMask) {
186 case RR_Rotate_90:
187 return 90;
189 case RR_Rotate_180:
190 return 180;
192 case RR_Rotate_270:
193 return 270;
195 default:
196 return 0;
200 int LegacyRandRScreen::rotationDegreeToIndex(int degree) const
202 switch (degree) {
203 case 90:
204 return RR_Rotate_90;
206 case 180:
207 return RR_Rotate_180;
209 case 270:
210 return RR_Rotate_270;
212 default:
213 return RR_Rotate_0;
217 const QSize& LegacyRandRScreen::currentPixelSize() const
219 return m_pixelSizes[m_currentSize];
222 RateList LegacyRandRScreen::refreshRates(int size) const
224 int nrates;
225 short* rrates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
227 RateList rateList;
228 for (int i = 0; i < nrates; i++)
229 rateList.append(rrates[i]);
231 return rateList;
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;
253 return true;
256 return false;
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
276 int nrates;
277 short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
279 for (int i = 0; i < nrates; i++)
280 if (hz == rates[i])
281 return i;
283 if (nrates != 0)
284 // Wrong input Hz!
285 Q_ASSERT(false);
287 return -1;
290 int LegacyRandRScreen::refreshRateIndexToHz(int size, int index) const
292 int nrates;
293 short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
295 if (nrates == 0 || index < 0)
296 return 0;
298 // Wrong input Hz!
299 if(index >= nrates)
300 return 0;
302 return rates[index];
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)
324 return i;
326 return -1;
329 int LegacyRandRScreen::rotations() const
331 return m_rotations;
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;
363 return true;
366 return false;
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 int LegacyRandRScreen::pixelCount( int index ) const
393 QSize sz = pixelSize(index);
394 return sz.width() * sz.height();
397 SizeList LegacyRandRScreen::pixelSizes() const
399 return m_pixelSizes;
402 #include "legacyrandrscreen.moc"