~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/randr/legacyrandrscreen.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007      Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
 
3
 * Copyright (c) 2002,2003 Hamish Rodda <rodda@kde.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 */
 
19
 
 
20
 
 
21
 
 
22
#include <QTimer>
 
23
//Added by qt3to4:
 
24
#include <QPixmap>
 
25
#include "ktimerdialog.h"
 
26
 
 
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>
 
34
 
 
35
#include <QX11Info>
 
36
#include "legacyrandrscreen.h"
 
37
 
 
38
LegacyRandRScreen::LegacyRandRScreen(int screenIndex)
 
39
        : m_config(0L)
 
40
        , m_screen(screenIndex)
 
41
        , m_shownDialog(NULL)
 
42
{
 
43
        loadSettings();
 
44
        setOriginal();
 
45
}
 
46
 
 
47
LegacyRandRScreen::~LegacyRandRScreen()
 
48
{
 
49
        if (m_config)
 
50
                XRRFreeScreenConfigInfo(m_config);
 
51
}
 
52
 
 
53
void LegacyRandRScreen::loadSettings()
 
54
{
 
55
        if (m_config)
 
56
                XRRFreeScreenConfigInfo(m_config);
 
57
 
 
58
        m_config = XRRGetScreenInfo(QX11Info::display(), rootWindow());
 
59
        Q_ASSERT(m_config);
 
60
 
 
61
        Rotation rotation;
 
62
        m_currentSize = m_proposedSize = XRRConfigCurrentConfiguration(m_config, &rotation);
 
63
        m_currentRotation = m_proposedRotation = rotation;
 
64
 
 
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));
 
72
        }
 
73
 
 
74
        m_rotations = XRRRotations(QX11Info::display(), m_screen, &rotation);
 
75
 
 
76
        m_currentRefreshRate = m_proposedRefreshRate = refreshRateHzToIndex(m_currentSize, XRRConfigCurrentRate(m_config));
 
77
}
 
78
 
 
79
void LegacyRandRScreen::setOriginal()
 
80
{
 
81
        m_originalSize = m_currentSize;
 
82
        m_originalRotation = m_currentRotation;
 
83
        m_originalRefreshRate = m_currentRefreshRate;
 
84
}
 
85
 
 
86
bool LegacyRandRScreen::applyProposed()
 
87
{
 
88
        //kDebug() << " size " << (SizeID)proposedSize() << ", rotation " << proposedRotation() << ", refresh " << refreshRateIndexToHz(proposedSize(), proposedRefreshRate());
 
89
 
 
90
        Status status;
 
91
 
 
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;
 
97
                }
 
98
                status = XRRSetScreenConfigAndRate(QX11Info::display(), m_config, rootWindow(), (SizeID)proposedSize(), (Rotation)proposedRotation(), refreshRateIndexToHz(proposedSize(), proposedRefreshRate()), CurrentTime);
 
99
        }
 
100
 
 
101
        //kDebug() << "New size: " << WidthOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), screen)) << ", " << HeightOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), screen));
 
102
 
 
103
        if (status == RRSetConfigSuccess) {
 
104
                m_currentSize = m_proposedSize;
 
105
                m_currentRotation = m_proposedRotation;
 
106
                m_currentRefreshRate = m_proposedRefreshRate;
 
107
                return true;
 
108
        }
 
109
 
 
110
        return false;
 
111
}
 
112
 
 
113
bool LegacyRandRScreen::applyProposedAndConfirm()
 
114
{
 
115
        if (proposedChanged()) {
 
116
                setOriginal();
 
117
 
 
118
                if (applyProposed()) {
 
119
                        if (!RandR::confirm()) {
 
120
                                proposeOriginal();
 
121
                                applyProposed();
 
122
                                return false;
 
123
                        }
 
124
                } else {
 
125
                        return false;
 
126
                }
 
127
        }
 
128
 
 
129
        return true;
 
130
}
 
131
 
 
132
Window LegacyRandRScreen::rootWindow() const
 
133
{
 
134
        return RootWindow(QX11Info::display(), m_screen);
 
135
}
 
136
 
 
137
QString LegacyRandRScreen::changedMessage() const
 
138
{
 
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());
 
150
}
 
151
 
 
152
bool LegacyRandRScreen::changedFromOriginal() const
 
153
{
 
154
        return m_currentSize != m_originalSize || m_currentRotation != m_originalRotation || m_currentRefreshRate != m_originalRefreshRate;
 
155
}
 
156
 
 
157
void LegacyRandRScreen::proposeOriginal()
 
158
{
 
159
        m_proposedSize = m_originalSize;
 
160
        m_proposedRotation = m_originalRotation;
 
161
        m_proposedRefreshRate = m_originalRefreshRate;
 
162
}
 
163
 
 
164
bool LegacyRandRScreen::proposedChanged() const
 
165
{
 
166
        return m_currentSize != m_proposedSize || m_currentRotation != m_proposedRotation || m_currentRefreshRate != m_proposedRefreshRate;
 
167
}
 
168
 
 
169
QString LegacyRandRScreen::currentRotationDescription() const
 
170
{
 
171
        QString ret = RandR::rotationName(m_currentRotation & RandR::RotateMask);
 
172
 
 
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);
 
178
        }
 
179
        
 
180
        return ret;
 
181
}
 
182
 
 
183
int LegacyRandRScreen::rotationIndexToDegree(int rotation) const
 
184
{
 
185
        switch (rotation & RandR::RotateMask) {
 
186
                case RR_Rotate_90:
 
187
                        return 90;
 
188
 
 
189
                case RR_Rotate_180:
 
190
                        return 180;
 
191
 
 
192
                case RR_Rotate_270:
 
193
                        return 270;
 
194
 
 
195
                default:
 
196
                        return 0;
 
197
        }
 
198
}
 
199
 
 
200
int LegacyRandRScreen::rotationDegreeToIndex(int degree) const
 
201
{
 
202
        switch (degree) {
 
203
                case 90:
 
204
                        return RR_Rotate_90;
 
205
 
 
206
                case 180:
 
207
                        return RR_Rotate_180;
 
208
 
 
209
                case 270:
 
210
                        return RR_Rotate_270;
 
211
 
 
212
                default:
 
213
                        return RR_Rotate_0;
 
214
        }
 
215
}
 
216
 
 
217
const QSize& LegacyRandRScreen::currentPixelSize() const
 
218
{
 
219
        return m_pixelSizes[m_currentSize];
 
220
}
 
221
 
 
222
RateList LegacyRandRScreen::refreshRates(int size) const
 
223
{
 
224
        int nrates;
 
225
        short* rrates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
 
226
 
 
227
        RateList rateList;
 
228
        for (int i = 0; i < nrates; i++)
 
229
                rateList.append(rrates[i]);
 
230
 
 
231
        return rateList;
 
232
}
 
233
 
 
234
QString LegacyRandRScreen::refreshRateDirectDescription(int rate) const
 
235
{
 
236
        return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", rate);
 
237
}
 
238
 
 
239
QString LegacyRandRScreen::refreshRateIndirectDescription(int size, int index) const
 
240
{
 
241
        return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", refreshRateIndexToHz(size, index));
 
242
}
 
243
 
 
244
QString LegacyRandRScreen::refreshRateDescription(int size, int index) const
 
245
{
 
246
        return ki18n("%1 Hz").subs(refreshRates(size)[index], 0, 'f', 1).toString();
 
247
}
 
248
 
 
249
bool LegacyRandRScreen::proposeRefreshRate(int index)
 
250
{
 
251
        if (index >= 0 && (int)refreshRates(proposedSize()).count() > index) {
 
252
                m_proposedRefreshRate = index;
 
253
                return true;
 
254
        }
 
255
 
 
256
        return false;
 
257
}
 
258
 
 
259
int LegacyRandRScreen::refreshRate() const
 
260
{
 
261
        return m_currentRefreshRate;
 
262
}
 
263
 
 
264
QString LegacyRandRScreen::currentRefreshRateDescription() const
 
265
{
 
266
        return refreshRateIndirectDescription(m_currentSize, m_currentRefreshRate);
 
267
}
 
268
 
 
269
int LegacyRandRScreen::proposedRefreshRate() const
 
270
{
 
271
        return m_proposedRefreshRate;
 
272
}
 
273
 
 
274
int LegacyRandRScreen::refreshRateHzToIndex(int size, int hz) const
 
275
{
 
276
        int nrates;
 
277
        short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
 
278
 
 
279
        for (int i = 0; i < nrates; i++)
 
280
                if (hz == rates[i])
 
281
                        return i;
 
282
 
 
283
        if (nrates != 0)
 
284
                // Wrong input Hz!
 
285
                Q_ASSERT(false);
 
286
 
 
287
        return -1;
 
288
}
 
289
 
 
290
int LegacyRandRScreen::refreshRateIndexToHz(int size, int index) const
 
291
{
 
292
        int nrates;
 
293
        short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);
 
294
 
 
295
        if (nrates == 0 || index < 0)
 
296
                return 0;
 
297
 
 
298
        // Wrong input Hz!
 
299
        if(index >= nrates)
 
300
                return 0;
 
301
 
 
302
        return rates[index];
 
303
}
 
304
 
 
305
int LegacyRandRScreen::numSizes() const
 
306
{
 
307
        return m_pixelSizes.count();
 
308
}
 
309
 
 
310
const QSize& LegacyRandRScreen::pixelSize(int index) const
 
311
{
 
312
        return m_pixelSizes[index];
 
313
}
 
314
 
 
315
const QSize& LegacyRandRScreen::mmSize(int index) const
 
316
{
 
317
        return m_mmSizes[index];
 
318
}
 
319
 
 
320
int LegacyRandRScreen::sizeIndex(const QSize &pixelSize) const
 
321
{
 
322
        for (int i = 0; i < m_pixelSizes.count(); i++)
 
323
                if (m_pixelSizes[i] == pixelSize)
 
324
                        return i;
 
325
 
 
326
        return -1;
 
327
}
 
328
 
 
329
int LegacyRandRScreen::rotations() const
 
330
{
 
331
        return m_rotations;
 
332
}
 
333
 
 
334
int LegacyRandRScreen::rotation() const
 
335
{
 
336
        return m_currentRotation;
 
337
}
 
338
 
 
339
int LegacyRandRScreen::size() const
 
340
{
 
341
        return m_currentSize;
 
342
}
 
343
 
 
344
int LegacyRandRScreen::proposedRotation() const
 
345
{
 
346
        return m_proposedRotation;
 
347
}
 
348
 
 
349
void LegacyRandRScreen::proposeRotation(int newRotation)
 
350
{
 
351
        m_proposedRotation = newRotation & RandR::OrientationMask;
 
352
}
 
353
 
 
354
int LegacyRandRScreen::proposedSize() const
 
355
{
 
356
        return m_proposedSize;
 
357
}
 
358
 
 
359
bool LegacyRandRScreen::proposeSize(int newSize)
 
360
{
 
361
        if ((int)m_pixelSizes.count() > newSize) {
 
362
                m_proposedSize = newSize;
 
363
                return true;
 
364
        }
 
365
 
 
366
        return false;
 
367
}
 
368
 
 
369
void LegacyRandRScreen::load(KConfig& config)
 
370
{
 
371
        KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));
 
372
 
 
373
        if (proposeSize(sizeIndex(group.readEntry("size", currentPixelSize()))))
 
374
                proposeRefreshRate(refreshRateHzToIndex(proposedSize(), group.readEntry("refresh", refreshRate())));
 
375
 
 
376
        proposeRotation(rotationDegreeToIndex(  group.readEntry("rotation", 0)) + 
 
377
                                                (group.readEntry("reflectX", false) ? RandR::ReflectX : 0) + 
 
378
                                                (group.readEntry("reflectY",false) ? RandR::ReflectY : 0));
 
379
}
 
380
 
 
381
void LegacyRandRScreen::save(KConfig& config) const
 
382
{
 
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);
 
389
}
 
390
 
 
391
QStringList LegacyRandRScreen::startupCommands() const
 
392
{
 
393
        QString command = QString("xrandr -s %1x%2 -r %3 ").arg( currentPixelSize().width(),
 
394
            currentPixelSize().height(), refreshRateIndexToHz(size(), refreshRate()));
 
395
        switch( rotation()) {
 
396
                case RR_Rotate_90:
 
397
                        command += " -o 1 ";
 
398
                        break;
 
399
                case RR_Rotate_180:
 
400
                        command += " -o 2 ";
 
401
                        break;
 
402
                case RR_Rotate_270:
 
403
                        command += " -o 3 ";
 
404
                        break;
 
405
        }
 
406
        if((rotation() & RandR::ReflectMask) == RandR::ReflectX)
 
407
                command += " -x ";
 
408
        if((bool)(rotation() & RandR::ReflectMask) == RandR::ReflectY)
 
409
                command += " -y ";
 
410
        return QStringList() << command;
 
411
}
 
412
 
 
413
int LegacyRandRScreen::pixelCount( int index ) const
 
414
{
 
415
        QSize sz = pixelSize(index);
 
416
        return sz.width() * sz.height();
 
417
}
 
418
 
 
419
SizeList LegacyRandRScreen::pixelSizes() const
 
420
{
 
421
        return m_pixelSizes;
 
422
}
 
423
 
 
424
#include "legacyrandrscreen.moc"