summaryrefslogtreecommitdiffstats
path: root/src/widget.cpp
blob: 751c69d61dd51ad72c479a960fa7720fc583acb5 (plain) (blame)
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
#include "widget.h"
#include "ui_widget.h"
#include "timeoutdialog.h"
#include "x.h"
#include "main.h"

#include <QDebug>
#include <QtWidgets/QAction>

//______________________________________________________________________________
Widget::Widget(QWidget *parent) :
	QWidget(parent),
	_ui(new Ui::Widget)
{
	_ui->setupUi(this);
	setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
}

void Widget::bringToTopTimer()
{
	auto combos = this->findChildren<QComboBox*>();
	for (auto combo : combos) {
		if (combo->view()->isVisible())
			return;
	}
	raise();
}

//______________________________________________________________________________
Widget::~Widget() {

}

void Widget::showEvent(QShowEvent *event)
{
	QWidget::showEvent(event);
	ScreenMode mode = ScreenSetup::inst()->getCurrentMode();
	if (ScreenSetup::inst()->getOutputCount() == 2 || mode == ScreenMode::Dual) {
		if (_ui->tabWidget->widget(1) != _ui->tabDual) {
			_ui->tabWidget->insertTab(1, _ui->tabDual, tr("Dual Screen"));
		}
	} else {
		if (_ui->tabWidget->widget(1) == _ui->tabDual) {
			_ui->tabWidget->removeTab(1);
		}
	}
	switch (mode) {
	case ScreenMode::Single:
	case ScreenMode::Clone:
		_ui->tabWidget->setCurrentWidget(_ui->tabClone);
		break;
	case ScreenMode::Dual:
		_ui->tabWidget->setCurrentWidget(_ui->tabDual);
		break;
	case ScreenMode::Advanced:
		_ui->tabWidget->setCurrentWidget(_ui->tabAdvanced);
		break;
	}
	initControls();
}

void Widget::initControls()
{
	auto modes = ScreenSetup::inst()->getCommonModes();
	for (auto mode : modes) {
		_ui->cboCloneResolution->addItem(QString::asprintf("%ux%u", mode.first, mode.second));
	}
	// Clear advanced controls
	auto widgets = _ui->advancedCombos->findChildren<QWidget*>();
	for (auto w : widgets) {
		_ui->advancedCombos->removeWidget(w);
		w->deleteLater();
	}
	// Create new
	QHash<QString, int> positions = ScreenSetup::inst()->getScreenPositions();
	ScreenSetup::ResolutionVector resolutions = ScreenSetup::inst()->getVirtualResolutions();
	int row = 0;
	for (auto it = positions.begin(); it != positions.end(); ++it) {
		_ui->advancedCombos->addWidget(new QLabel(it.key()), row, 0, 1, -1);
		_ui->advancedCombos->addWidget(new QComboBox(), row, 2);
		row++;
	}
}

//______________________________________________________________________________
void Widget::handleButton() {
	// Apply

	/*

  // Get the modes which has to be applied from QComboBox
  QList<QVariant> modes =
		  _ui->comboBox->itemData( _ui->comboBox->currentIndex()).toList();
  XRRModeInfo* monitorMode = _modeMap[modes[0].toULongLong()];
  XRRModeInfo* beamerMode  = _modeMap[modes[1].toULongLong()];

  // First disconnect all crts to avoid conflicts
  for(CrtcMap::iterator it = _crtcMap.begin(); it != _crtcMap.end(); ++it) {
	Status st = XRRSetCrtcConfig(_display, _screenResources, it.key(), CurrentTime,
					 0, 0, None, RR_Rotate_0, nullptr, 0);
	qDebug() << "Disconnecting" << it.key() << ":" << st;
  }

  // Set screensize
  XRRSetScreenSize(_display, DefaultRootWindow(_display),
				   monitorMode->width, monitorMode->height,
				   25.4 * monitorMode->width / 96,  // standard dpi that X uses
				   25.4 * monitorMode->height / 96); // standard dpi that X uses

  // Apply the modes
  Status stMon = XRRSetCrtcConfig(_display,
				   _screenResources,
				   _outputMap[_monitor]->crtc,
				   CurrentTime,
				   0, 0, monitorMode->id,
				   RR_Rotate_0,
				   &_monitor, 1);
  Status stBem = XRRSetCrtcConfig(_display,
				   _screenResources,
				   _outputMap[_beamer]->crtc,
				   CurrentTime,
				   0, 0, beamerMode->id,
				   RR_Rotate_0,
				   &_beamer, 1);

  // Sync... whatever...
  XSync (_display, False);
  qDebug() << "Monitor set:" << stMon << ", beamer set: " << stBem;

  // Center widget on screenbottom
  this->move( monitorMode->width/2  - this->width()/2,
			  monitorMode->height - this->height() );

  // Set the mouse pointer in the middle of the screen
  QCursor::setPos(monitorMode->width/2, monitorMode->height/2);

  */

	/*************************** ASK for confirmtion ****************************/

	// Show a dialog asking if the res should be kept
	TimeOutDialog keepDialog(15, this);
	keepDialog.setWindowModality(Qt::ApplicationModal);
	keepDialog.setWindowTitle(" ");
	keepDialog.setLabelText(trUtf8("Do you want to keep this resolution?"));
	keepDialog.setCancelButtonText(trUtf8("Keep"));
	/*
  keepDialog.move(monitorMode->width/2  - this->width()/2,
				   monitorMode->height/2 - this->height());
				   */
	keepDialog.show();

	while (keepDialog.isActive()) {
		QCoreApplication::processEvents();
	}

	/*
  // If the dialog was not canceled revert the resolution
  if ( !keepDialog.wasCanceled()) {
	  qDebug() << "User did not cancel timeout, reverting!";

	// First disconnect all crts to avoid conflicts
	for(CrtcMap::iterator it = _crtcMap.begin(); it != _crtcMap.end(); ++it) {
	  XRRSetCrtcConfig(_display, _screenResources, it.key(), CurrentTime,
					   0, 0, None, RR_Rotate_0, nullptr, 0);
	}

	// Then calc backed up screensize
	QSize ScreenSize(0,0);
	for ( CrtcMap::iterator it = backup.begin();
		  it != backup.end(); ++it ) {
	  // Dangerzone. Only access existing modes!
	  if ( it.value()->mode != None ) {
		ScreenSize.setWidth(
			  std::max((uint)ScreenSize.width(),
					   it.value()->x+_modeMap[it.value()->mode]->width));
		ScreenSize.setHeight(
			  std::max((uint)ScreenSize.height(),
					   it.value()->y+_modeMap[it.value()->mode]->height));
	  }
	}

	// Set screensize
	XRRSetScreenSize(_display, DefaultRootWindow(_display),
					 ScreenSize.width(), ScreenSize.height(),
					 25.4 * ScreenSize.width() / 96,  //  dpi used by X
					 25.4 * ScreenSize.height() / 96); // dpi used by X

	// Apply the backup
	for ( CrtcMap::iterator it = backup.begin();
		  it != backup.end(); ++it ) {
	  XRRSetCrtcConfig(_display,
					   _screenResources,
					   it.key(),
					   CurrentTime,
					   it.value()->x,
					   it.value()->y,
					   it.value()->mode,
					   it.value()->rotation,
					   it.value()->outputs,
					   it.value()->noutput);
	}

	// Sync... whatever...
	XSync (_display, False);

	// center dialog on screenbottom
	qDebug() <<  "Again center dialog on screenbottom";
	this->move( ScreenSize.width()/2  - this->width()/2,
				ScreenSize.height() - this->height());

	// Set the mouse pointer in the middle of the screen
	QCursor::setPos(monitorMode->width/2, monitorMode->height/2);
  }

  // Delete the backup
  for ( CrtcMap::iterator it = backup.begin();
		it != backup.end(); ++it ) {
	delete[] it.value()->outputs;
	delete it.value();
  }

  */
}

////////////////////////////////////////////////////////////////////////////////