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

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (c) 2007      Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
 * Copyright (c) 2002,2003 Hamish Rodda <rodda@kde.org>
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */



#include <QTimer>
//Added by qt3to4:
#include <QPixmap>
#include "ktimerdialog.h"

#include <kdebug.h>
#include <klocale.h>
#include <kglobal.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <QtGui/QLabel>
#include <QDesktopWidget>

#include <QX11Info>
#include "legacyrandrscreen.h"

LegacyRandRScreen::LegacyRandRScreen(int screenIndex)
	: m_config(0L)
	, m_screen(screenIndex)
	, m_shownDialog(NULL)
{
	loadSettings();
	setOriginal();
}

LegacyRandRScreen::~LegacyRandRScreen()
{
	if (m_config)
		XRRFreeScreenConfigInfo(m_config);
}

void LegacyRandRScreen::loadSettings()
{
	if (m_config)
		XRRFreeScreenConfigInfo(m_config);

	m_config = XRRGetScreenInfo(QX11Info::display(), rootWindow());
	Q_ASSERT(m_config);

	Rotation rotation;
	m_currentSize = m_proposedSize = XRRConfigCurrentConfiguration(m_config, &rotation);
	m_currentRotation = m_proposedRotation = rotation;

	m_pixelSizes.clear();
	m_mmSizes.clear();
	int numSizes;
	XRRScreenSize* sizes = XRRSizes(QX11Info::display(), m_screen, &numSizes);
	for (int i = 0; i < numSizes; i++) {
		m_pixelSizes.append(QSize(sizes[i].width, sizes[i].height));
		m_mmSizes.append(QSize(sizes[i].mwidth, sizes[i].mheight));
	}

	m_rotations = XRRRotations(QX11Info::display(), m_screen, &rotation);

	m_currentRefreshRate = m_proposedRefreshRate = refreshRateHzToIndex(m_currentSize, XRRConfigCurrentRate(m_config));
}

void LegacyRandRScreen::setOriginal()
{
	m_originalSize = m_currentSize;
	m_originalRotation = m_currentRotation;
	m_originalRefreshRate = m_currentRefreshRate;
}

bool LegacyRandRScreen::applyProposed()
{
	//kDebug() << " size " << (SizeID)proposedSize() << ", rotation " << proposedRotation() << ", refresh " << refreshRateIndexToHz(proposedSize(), proposedRefreshRate());

	Status status;

	if (proposedRefreshRate() < 0)
		status = XRRSetScreenConfig(QX11Info::display(), m_config, rootWindow(), (SizeID)proposedSize(), (Rotation)proposedRotation(), CurrentTime);
	else {
		if( refreshRateIndexToHz(proposedSize(), proposedRefreshRate()) <= 0 ) {
			m_proposedRefreshRate = 0;
		}
		status = XRRSetScreenConfigAndRate(QX11Info::display(), m_config, rootWindow(), (SizeID)proposedSize(), (Rotation)proposedRotation(), refreshRateIndexToHz(proposedSize(), proposedRefreshRate()), CurrentTime);
	}

	//kDebug() << "New size: " << WidthOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), screen)) << ", " << HeightOfScreen(ScreenOfDisplay(QPaintDevice::x11AppDisplay(), screen));

	if (status == RRSetConfigSuccess) {
		m_currentSize = m_proposedSize;
		m_currentRotation = m_proposedRotation;
		m_currentRefreshRate = m_proposedRefreshRate;
		return true;
	}

	return false;
}

bool LegacyRandRScreen::applyProposedAndConfirm()
{
	if (proposedChanged()) {
		setOriginal();

		if (applyProposed()) {
			if (!RandR::confirm()) {
				proposeOriginal();
				applyProposed();
				return false;
			}
		} else {
			return false;
		}
	}

	return true;
}

Window LegacyRandRScreen::rootWindow() const
{
	return RootWindow(QX11Info::display(), m_screen);
}

QString LegacyRandRScreen::changedMessage() const
{
	if (refreshRate() == -1)
		return i18n("New configuration:\nResolution: %1 x %2\nOrientation: %3",
			 currentPixelSize().width(),
			 currentPixelSize().height(),
			 currentRotationDescription());
	else
		return i18n("New configuration:\nResolution: %1 x %2\nOrientation: %3\nRefresh rate: %4",
			 currentPixelSize().width(),
			 currentPixelSize().height(),
			 currentRotationDescription(),
			 currentRefreshRateDescription());
}

bool LegacyRandRScreen::changedFromOriginal() const
{
	return m_currentSize != m_originalSize || m_currentRotation != m_originalRotation || m_currentRefreshRate != m_originalRefreshRate;
}

void LegacyRandRScreen::proposeOriginal()
{
	m_proposedSize = m_originalSize;
	m_proposedRotation = m_originalRotation;
	m_proposedRefreshRate = m_originalRefreshRate;
}

bool LegacyRandRScreen::proposedChanged() const
{
	return m_currentSize != m_proposedSize || m_currentRotation != m_proposedRotation || m_currentRefreshRate != m_proposedRefreshRate;
}

QString LegacyRandRScreen::currentRotationDescription() const
{
	QString ret = RandR::rotationName(m_currentRotation & RandR::RotateMask);

	if (m_currentRotation != (m_currentRotation & RandR::RotateMask)) {
		if (m_currentRotation & RR_Rotate_0)
			ret = RandR::rotationName(m_currentRotation & (RR_Reflect_X + RR_Reflect_X), true, true);
		else
			ret += ", " + RandR::rotationName(m_currentRotation & (RR_Reflect_X + RR_Reflect_X), true, false);
	}
	
	return ret;
}

int LegacyRandRScreen::rotationIndexToDegree(int rotation) const
{
	switch (rotation & RandR::RotateMask) {
		case RR_Rotate_90:
			return 90;

		case RR_Rotate_180:
			return 180;

		case RR_Rotate_270:
			return 270;

		default:
			return 0;
	}
}

int LegacyRandRScreen::rotationDegreeToIndex(int degree) const
{
	switch (degree) {
		case 90:
			return RR_Rotate_90;

		case 180:
			return RR_Rotate_180;

		case 270:
			return RR_Rotate_270;

		default:
			return RR_Rotate_0;
	}
}

const QSize& LegacyRandRScreen::currentPixelSize() const
{
	return m_pixelSizes[m_currentSize];
}

RateList LegacyRandRScreen::refreshRates(int size) const
{
	int nrates;
	short* rrates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);

	RateList rateList;
	for (int i = 0; i < nrates; i++)
		rateList.append(rrates[i]);

	return rateList;
}

QString LegacyRandRScreen::refreshRateDirectDescription(int rate) const
{
	return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", rate);
}

QString LegacyRandRScreen::refreshRateIndirectDescription(int size, int index) const
{
	return i18nc("Refresh rate in Hertz (Hz)", "%1 Hz", refreshRateIndexToHz(size, index));
}

QString LegacyRandRScreen::refreshRateDescription(int size, int index) const
{
	return ki18n("%1 Hz").subs(refreshRates(size)[index], 0, 'f', 1).toString();
}

bool LegacyRandRScreen::proposeRefreshRate(int index)
{
	if (index >= 0 && (int)refreshRates(proposedSize()).count() > index) {
		m_proposedRefreshRate = index;
		return true;
	}

	return false;
}

int LegacyRandRScreen::refreshRate() const
{
	return m_currentRefreshRate;
}

QString LegacyRandRScreen::currentRefreshRateDescription() const
{
	return refreshRateIndirectDescription(m_currentSize, m_currentRefreshRate);
}

int LegacyRandRScreen::proposedRefreshRate() const
{
	return m_proposedRefreshRate;
}

int LegacyRandRScreen::refreshRateHzToIndex(int size, int hz) const
{
	int nrates;
	short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);

	for (int i = 0; i < nrates; i++)
		if (hz == rates[i])
			return i;

	if (nrates != 0)
		// Wrong input Hz!
		Q_ASSERT(false);

	return -1;
}

int LegacyRandRScreen::refreshRateIndexToHz(int size, int index) const
{
	int nrates;
	short* rates = XRRRates(QX11Info::display(), m_screen, (SizeID)size, &nrates);

	if (nrates == 0 || index < 0)
		return 0;

	// Wrong input Hz!
	if(index >= nrates)
		return 0;

	return rates[index];
}

int LegacyRandRScreen::numSizes() const
{
	return m_pixelSizes.count();
}

const QSize& LegacyRandRScreen::pixelSize(int index) const
{
	return m_pixelSizes[index];
}

const QSize& LegacyRandRScreen::mmSize(int index) const
{
	return m_mmSizes[index];
}

int LegacyRandRScreen::sizeIndex(const QSize &pixelSize) const
{
	for (int i = 0; i < m_pixelSizes.count(); i++)
		if (m_pixelSizes[i] == pixelSize)
			return i;

	return -1;
}

int LegacyRandRScreen::rotations() const
{
	return m_rotations;
}

int LegacyRandRScreen::rotation() const
{
	return m_currentRotation;
}

int LegacyRandRScreen::size() const
{
	return m_currentSize;
}

int LegacyRandRScreen::proposedRotation() const
{
	return m_proposedRotation;
}

void LegacyRandRScreen::proposeRotation(int newRotation)
{
	m_proposedRotation = newRotation & RandR::OrientationMask;
}

int LegacyRandRScreen::proposedSize() const
{
	return m_proposedSize;
}

bool LegacyRandRScreen::proposeSize(int newSize)
{
	if ((int)m_pixelSizes.count() > newSize) {
		m_proposedSize = newSize;
		return true;
	}

	return false;
}

void LegacyRandRScreen::load(KConfig& config)
{
	KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));

	if (proposeSize(sizeIndex(group.readEntry("size", currentPixelSize()))))
		proposeRefreshRate(refreshRateHzToIndex(proposedSize(), group.readEntry("refresh", refreshRate())));

	proposeRotation(rotationDegreeToIndex(	group.readEntry("rotation", 0)) + 
						(group.readEntry("reflectX", false) ? RandR::ReflectX : 0) + 
						(group.readEntry("reflectY",false) ? RandR::ReflectY : 0));
}

void LegacyRandRScreen::save(KConfig& config) const
{
	KConfigGroup group = config.group(QString("Screen%1").arg(m_screen));
	group.writeEntry("size", currentPixelSize());
	group.writeEntry("refresh", refreshRateIndexToHz(size(), refreshRate()));
	group.writeEntry("rotation", rotationIndexToDegree(rotation()));
	group.writeEntry("reflectX", (bool)(rotation() & RandR::ReflectMask) == RandR::ReflectX);
	group.writeEntry("reflectY", (bool)(rotation() & RandR::ReflectMask) == RandR::ReflectY);
}

QStringList LegacyRandRScreen::startupCommands() const
{
	QString command = QString("xrandr -s %1x%2 -r %3 ").arg( currentPixelSize().width(),
	    currentPixelSize().height(), refreshRateIndexToHz(size(), refreshRate()));
	switch( rotation()) {
		case RR_Rotate_90:
			command += " -o 1 ";
			break;
		case RR_Rotate_180:
			command += " -o 2 ";
			break;
		case RR_Rotate_270:
			command += " -o 3 ";
			break;
	}
	if((rotation() & RandR::ReflectMask) == RandR::ReflectX)
		command += " -x ";
	if((bool)(rotation() & RandR::ReflectMask) == RandR::ReflectY)
		command += " -y ";
	return QStringList() << command;
}

int LegacyRandRScreen::pixelCount( int index ) const
{
	QSize sz = pixelSize(index);
	return sz.width() * sz.height();
}

SizeList LegacyRandRScreen::pixelSizes() const
{
	return m_pixelSizes;
}

#include "legacyrandrscreen.moc"