• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-workspace API Reference
  • KDE Home
  • Contact Us
 

KWin

  • kde-4.x
  • kde-workspace
  • kwin
screens.cpp
Go to the documentation of this file.
1 /********************************************************************
2  KWin - the KDE window manager
3  This file is part of the KDE project.
4 
5 Copyright (C) 2013 Martin Gräßlin <[email protected]>
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include "screens.h"
21 #include "client.h"
22 #include "cursor.h"
23 #include "settings.h"
24 #include "workspace.h"
25 #if HAVE_WAYLAND
26 #include "wayland_backend.h"
27 #include <xcb/randr.h>
28 #endif
29 
30 #include <QApplication>
31 #include <QDesktopWidget>
32 #include <QTimer>
33 
34 namespace KWin
35 {
36 
37 Screens *Screens::s_self = nullptr;
38 Screens *Screens::create(QObject *parent)
39 {
40  Q_ASSERT(!s_self);
41 #if HAVE_WAYLAND
42  if (kwinApp()->operationMode() == Application::OperationModeWaylandAndX11) {
43  s_self = new WaylandScreens(parent);
44  return s_self;
45  }
46 #endif
47  if (kwinApp()->operationMode() == Application::OperationModeX11) {
48  s_self = new DesktopWidgetScreens(parent);
49  }
50  s_self->init();
51  return s_self;
52 }
53 
54 Screens::Screens(QObject *parent)
55  : QObject(parent)
56  , m_count(0)
57  , m_current(0)
58  , m_currentFollowsMouse(false)
59  , m_changedTimer(new QTimer(this))
60 {
61 }
62 
63 Screens::~Screens()
64 {
65  s_self = NULL;
66 }
67 
68 void Screens::init()
69 {
70  m_changedTimer->setSingleShot(true);
71  m_changedTimer->setInterval(100);
72  connect(m_changedTimer, SIGNAL(timeout()), SLOT(updateCount()));
73  connect(m_changedTimer, SIGNAL(timeout()), SIGNAL(changed()));
74  connect(this, &Screens::countChanged, this, &Screens::changed);
75  connect(this, &Screens::changed, this, &Screens::updateSize);
76  connect(this, &Screens::sizeChanged, this, &Screens::geometryChanged);
77 
78  Settings settings;
79  settings.setDefaults();
80  m_currentFollowsMouse = settings.activeMouseScreen();
81 }
82 
83 void Screens::reconfigure()
84 {
85  if (!m_config) {
86  return;
87  }
88  Settings settings(m_config);
89  settings.read();
90  setCurrentFollowsMouse(settings.activeMouseScreen());
91 }
92 
93 void Screens::updateSize()
94 {
95  QRect bounding;
96  for (int i = 0; i < count(); ++i) {
97  bounding = bounding.united(geometry(i));
98  }
99  if (m_boundingSize != bounding.size()) {
100  m_boundingSize = bounding.size();
101  emit sizeChanged();
102  }
103 }
104 
105 void Screens::setCount(int count)
106 {
107  if (m_count == count) {
108  return;
109  }
110  const int previous = m_count;
111  m_count = count;
112  emit countChanged(previous, count);
113 }
114 
115 void Screens::setCurrent(int current)
116 {
117  if (m_current == current) {
118  return;
119  }
120  m_current = current;
121  emit currentChanged();
122 }
123 
124 void Screens::setCurrent(const QPoint &pos)
125 {
126  setCurrent(number(pos));
127 }
128 
129 void Screens::setCurrent(const Client *c)
130 {
131  if (!c->isActive()) {
132  return;
133  }
134  if (!c->isOnScreen(m_current)) {
135  setCurrent(c->screen());
136  }
137 }
138 
139 void Screens::setCurrentFollowsMouse(bool follows)
140 {
141  if (m_currentFollowsMouse == follows) {
142  return;
143  }
144  m_currentFollowsMouse = follows;
145 }
146 
147 int Screens::current() const
148 {
149  if (m_currentFollowsMouse) {
150  return number(Cursor::pos());
151  }
152  Client *client = Workspace::self()->activeClient();
153  if (client && !client->isOnScreen(m_current)) {
154  return client->screen();
155  }
156  return m_current;
157 }
158 
159 int Screens::intersecting(const QRect &r) const
160 {
161  int cnt = 0;
162  for (int i = 0; i < count(); ++i) {
163  if (geometry(i).intersects(r)) {
164  ++cnt;
165  }
166  }
167  return cnt;
168 }
169 
170 DesktopWidgetScreens::DesktopWidgetScreens(QObject *parent)
171  : Screens(parent)
172  , m_desktop(QApplication::desktop())
173 {
174 }
175 
176 DesktopWidgetScreens::~DesktopWidgetScreens()
177 {
178 }
179 
180 void DesktopWidgetScreens::init()
181 {
182  Screens::init();
183  connect(m_desktop, SIGNAL(screenCountChanged(int)), SLOT(startChangedTimer()));
184  connect(m_desktop, SIGNAL(resized(int)), SLOT(startChangedTimer()));
185  updateCount();
186 }
187 
188 QRect DesktopWidgetScreens::geometry(int screen) const
189 {
190  if (Screens::self()->isChanging())
191  const_cast<DesktopWidgetScreens*>(this)->updateCount();
192  return m_desktop->screenGeometry(screen);
193 }
194 
195 QSize DesktopWidgetScreens::size(int screen) const
196 {
197  return geometry(screen).size();
198 }
199 
200 int DesktopWidgetScreens::number(const QPoint &pos) const
201 {
202  if (Screens::self()->isChanging())
203  const_cast<DesktopWidgetScreens*>(this)->updateCount();
204  return m_desktop->screenNumber(pos);
205 }
206 
207 void DesktopWidgetScreens::updateCount()
208 {
209  setCount(m_desktop->screenCount());
210 }
211 
212 #if HAVE_WAYLAND
213 WaylandScreens::WaylandScreens(QObject* parent)
214  : Screens(parent)
215 {
216 }
217 
218 WaylandScreens::~WaylandScreens()
219 {
220 }
221 
222 void WaylandScreens::init()
223 {
224  Screens::init();
225  connect(Wayland::WaylandBackend::self(), &Wayland::WaylandBackend::outputsChanged,
226  this, &WaylandScreens::startChangedTimer);
227  updateCount();
228 }
229 
230 QRect WaylandScreens::geometry(int screen) const
231 {
232  if (screen >= m_geometries.size()) {
233  return QRect();
234  }
235  return m_geometries.at(screen);
236 }
237 
238 QSize WaylandScreens::size(int screen) const
239 {
240  return geometry(screen).size();
241 }
242 
243 int WaylandScreens::number(const QPoint &pos) const
244 {
245  int bestScreen = 0;
246  int minDistance = INT_MAX;
247  for (int i = 0; i < m_geometries.size(); ++i) {
248  const QRect &geo = m_geometries.at(i);
249  if (geo.contains(pos)) {
250  return i;
251  }
252  int distance = QPoint(geo.topLeft() - pos).manhattanLength();
253  distance = qMin(distance, QPoint(geo.topRight() - pos).manhattanLength());
254  distance = qMin(distance, QPoint(geo.bottomRight() - pos).manhattanLength());
255  distance = qMin(distance, QPoint(geo.bottomLeft() - pos).manhattanLength());
256  if (distance < minDistance) {
257  minDistance = distance;
258  bestScreen = i;
259  }
260  }
261  return bestScreen;
262 }
263 
264 void WaylandScreens::updateCount()
265 {
266  m_geometries.clear();
267  int count = 0;
268  const QList<Wayland::Output*> &outputs = Wayland::WaylandBackend::self()->outputs();
269  for (auto it = outputs.begin(); it != outputs.end(); ++it) {
270  if ((*it)->pixelSize().isEmpty()) {
271  continue;
272  }
273  count++;
274  m_geometries.append(QRect((*it)->globalPosition(), (*it)->pixelSize()));
275  }
276  if (m_geometries.isEmpty()) {
277  // we need a fake screen
278  m_geometries.append(QRect(0, 0, displayWidth(), displayHeight()));
279  setCount(1);
280  return;
281  }
282  updateXRandr();
283  emit changed();
284 }
285 
286 namespace RandR
287 {
288 typedef Xcb::Wrapper<xcb_randr_get_screen_resources_current_reply_t,
289  xcb_randr_get_screen_resources_current_cookie_t,
290  &xcb_randr_get_screen_resources_current_reply,
291  &xcb_randr_get_screen_resources_current_unchecked> CurrentResources;
292 }
293 
294 static bool setNewScreenSize(const QSize &size)
295 {
296  auto c = xcb_randr_set_screen_size_checked(connection(), rootWindow(), size.width(), size.height(), 1, 1);
297  ScopedCPointer<xcb_generic_error_t> error(xcb_request_check(connection(), c));
298  if (!error.isNull()) {
299  qDebug() << "Error setting screen size: " << error->error_code;
300  return false;
301  }
302  return true;
303 }
304 
305 static xcb_randr_crtc_t getCrtc(const xcb_randr_get_screen_resources_current_reply_t* r)
306 {
307  if (xcb_randr_get_screen_resources_current_crtcs_length(r) == 0) {
308  qDebug() << "No CRTCs";
309  return XCB_NONE;
310  }
311  return xcb_randr_get_screen_resources_current_crtcs(r)[0];
312 }
313 
314 static xcb_randr_output_t getOutputForCrtc(xcb_randr_crtc_t crtc)
315 {
316  ScopedCPointer<xcb_randr_get_crtc_info_reply_t> info(xcb_randr_get_crtc_info_reply(
317  connection(), xcb_randr_get_crtc_info(connection(), crtc, XCB_TIME_CURRENT_TIME), nullptr));
318  if (info->num_outputs == 0) {
319  return XCB_NONE;
320  }
321  return xcb_randr_get_crtc_info_outputs(info.data())[0];
322 }
323 
324 static xcb_randr_mode_t createNewMode(const QSize &size)
325 {
326  // need to create the new mode
327  qDebug() << "Creating a new mode";
328  QString name(QString::number(size.width()));
329  name.append('x');
330  name.append(QString::number(size.height()));
331  xcb_randr_mode_info_t newInfo;
332  newInfo.dot_clock = 0;
333  newInfo.height = size.height();
334  newInfo.hskew = 0;
335  newInfo.hsync_end = 0;
336  newInfo.hsync_start = 0;
337  newInfo.htotal = size.width();
338  newInfo.id = 0;
339  newInfo.mode_flags = 0;
340  newInfo.vsync_end = 0;
341  newInfo.vsync_start = 0;
342  newInfo.vtotal = size.height();
343  newInfo.width = size.width();
344  newInfo.name_len = name.length();
345  auto cookie = xcb_randr_create_mode_unchecked(connection(), rootWindow(), newInfo, name.length(), name.toUtf8().constData());
346  ScopedCPointer<xcb_randr_create_mode_reply_t> reply(xcb_randr_create_mode_reply(connection(), cookie, nullptr));
347  if (!reply.isNull()) {
348  return reply->mode;
349  }
350  return XCB_NONE;
351 }
352 
353 static xcb_randr_mode_t getModeForSize(const QSize &size, const xcb_randr_get_screen_resources_current_reply_t* r)
354 {
355  xcb_randr_mode_info_t *infos = xcb_randr_get_screen_resources_current_modes(r);
356  const int modeInfoLength = xcb_randr_get_screen_resources_current_modes_length(r);
357  // check available modes
358  for (int i = 0; i < modeInfoLength; ++i) {
359  xcb_randr_mode_info_t modeInfo = infos[i];
360  if (modeInfo.width == size.width() && modeInfo.height == size.height()) {
361  qDebug() << "Found our required mode";
362  return modeInfo.id;
363  }
364  }
365  // did not find our mode, so create it
366  return createNewMode(size);
367 }
368 
369 static bool addModeToOutput(xcb_randr_output_t output, xcb_randr_mode_t mode)
370 {
371  ScopedCPointer<xcb_randr_get_output_info_reply_t> info(xcb_randr_get_output_info_reply(connection(),
372  xcb_randr_get_output_info(connection(), output, XCB_TIME_CURRENT_TIME), nullptr));
373  xcb_randr_mode_t *modes = xcb_randr_get_output_info_modes(info.data());
374  for (int i = 0; i < info->num_modes; ++i) {
375  if (modes[i] == mode) {
376  return true;
377  }
378  }
379  qDebug() << "Need to add the mode to output";
380  auto c = xcb_randr_add_output_mode_checked(connection(), output, mode);
381  ScopedCPointer<xcb_generic_error_t> error(xcb_request_check(connection(), c));
382  if (!error.isNull()) {
383  qDebug() << "Error while adding mode to output: " << error->error_code;
384  return false;
385  }
386  return true;
387 }
388 
389 void WaylandScreens::updateXRandr()
390 {
391  if (!Xcb::Extensions::self()->isRandrAvailable()) {
392  qDebug() << "No RandR extension available, cannot sync with X";
393  return;
394  }
395  QRegion screens;
396  foreach (const QRect &rect, m_geometries) {
397  screens = screens.united(rect);
398  }
399  const QSize &size = screens.boundingRect().size();
400  if (size.isEmpty()) {
401  return;
402  }
403 
404  RandR::CurrentResources currentResources(rootWindow());
405  xcb_randr_crtc_t crtc = getCrtc(currentResources.data());
406  if (crtc == XCB_NONE) {
407  return;
408  }
409  xcb_randr_output_t output = getOutputForCrtc(crtc);
410  if (output == XCB_NONE) {
411  return;
412  }
413  // first disable the first CRTC
414  xcb_randr_set_crtc_config(connection(), crtc, XCB_TIME_CURRENT_TIME, XCB_TIME_CURRENT_TIME,
415  0, 0, XCB_NONE, XCB_RANDR_ROTATION_ROTATE_0, 0, nullptr);
416 
417  // then set new screen size
418  if (!setNewScreenSize(size)) {
419  return;
420  }
421 
422  xcb_randr_mode_t mode = getModeForSize(size, currentResources.data());
423  if (mode == XCB_NONE) {
424  return;
425  }
426 
427  if (!addModeToOutput(output, mode)) {
428  return;
429  }
430  // enable CRTC again
431  xcb_randr_set_crtc_config(connection(), crtc, XCB_TIME_CURRENT_TIME, XCB_TIME_CURRENT_TIME,
432  0, 0, mode, XCB_RANDR_ROTATION_ROTATE_0, 1, &output);
433 }
434 
435 #endif
436 
437 } // namespace
KWin::Settings::activeMouseScreen
bool activeMouseScreen() const
Get ActiveMouseScreen.
Definition: settings.h:287
QRect::size
QSize size() const
QDesktopWidget::screenGeometry
const QRect screenGeometry(int screen) const
QRect::topRight
QPoint topRight() const
QSize::width
int width() const
KWin::DesktopWidgetScreens::init
void init() override
Called once the singleton instance has been created.
Definition: screens.cpp:180
KWin::Client::isActive
bool isActive() const
Definition: client.h:1105
KWin::Toplevel::screen
int screen
Definition: toplevel.h:62
KWin::DesktopWidgetScreens::updateCount
void updateCount()
Definition: screens.cpp:207
QSize::isEmpty
bool isEmpty() const
KWin::Application::OperationModeWaylandAndX11
KWin uses X11 for managing windows, but renders to a Wayland compositor.
Definition: main.h:74
KWin::kwinApp
static Application * kwinApp()
Definition: main.h:114
cursor.h
QApplication
QRect::bottomRight
QPoint bottomRight() const
QRect::bottomLeft
QPoint bottomLeft() const
KWin::Wayland::WaylandBackend::outputsChanged
void outputsChanged()
QRegion::boundingRect
QRect boundingRect() const
QPoint
screens.h
KWin::Screens::init
virtual void init()
Called once the singleton instance has been created.
Definition: screens.cpp:68
QDesktopWidget::screenNumber
int screenNumber(const QWidget *widget) const
KWin::screens
Screens * screens()
Definition: screens.h:221
KWin::DesktopWidgetScreens::~DesktopWidgetScreens
virtual ~DesktopWidgetScreens()
Definition: screens.cpp:176
workspace.h
wayland_backend.h
QRect
KWin::DesktopWidgetScreens::number
virtual int number(const QPoint &pos) const
Definition: screens.cpp:200
QString::number
QString number(int n, int base)
QList::append
void append(const T &value)
QTimer
QObject
KWin::DesktopWidgetScreens
Definition: screens.h:147
QRect::contains
bool contains(const QPoint &point, bool proper) const
QString
QList
KWin::Settings
Definition: settings.h:15
QList::end
iterator end()
QSize
KWin::Screens::isChanging
bool isChanging()
Definition: screens.h:93
settings.h
QDesktopWidget::screenCount
screenCount
KWin::Toplevel::isOnScreen
bool isOnScreen(int screen) const
Definition: toplevel.cpp:337
QSize::height
int height() const
QRect::topLeft
QPoint topLeft() const
KWin::Application::OperationModeX11
KWin uses only X11 for managing windows and compositing.
Definition: main.h:69
client.h
KWin::Screens::geometry
QRect geometry() const
The bounding geometry of all screens combined.
Definition: screens.h:215
KWin::Screens::setCount
void setCount(int count)
Definition: screens.cpp:105
KWin::Client
The Client class encapsulates a window decoration frame.
Definition: client.h:71
KWin::Xcb::Extensions::self
static Extensions * self()
Definition: xcbutils.cpp:72
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QRect::united
QRect united(const QRect &rectangle) const
QRegion::united
QRegion united(const QRegion &r) const
QList::begin
iterator begin()
KWin::Screens
Definition: screens.h:41
KWin::Screens::size
QSize size() const
The bounding size of all screens combined.
Definition: screens.h:209
QRegion
KWin::Screens::startChangedTimer
void startChangedTimer()
Definition: screens.h:203
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Dec 12 2019 02:20:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KWin

Skip menu "KWin"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kde-workspace API Reference

Skip menu "kde-workspace API Reference"
  • KWin
  •   KWin Decoration Library
  •   KWin Effects Library
  • Plasma
  • Plasma
  •   Applets
  •   Engines
  •   libkworkspace
  •   libtaskmanager
  • System Settings
  •   SystemSettingsView

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal