27 #include <xcb/randr.h>
30 #include <QApplication>
31 #include <QDesktopWidget>
37 Screens *Screens::s_self =
nullptr;
38 Screens *Screens::create(
QObject *parent)
43 s_self =
new WaylandScreens(parent);
48 s_self =
new DesktopWidgetScreens(parent);
54 Screens::Screens(
QObject *parent)
58 , m_currentFollowsMouse(false)
59 , m_changedTimer(new
QTimer(this))
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);
79 settings.setDefaults();
83 void Screens::reconfigure()
93 void Screens::updateSize()
96 for (
int i = 0; i < count(); ++i) {
97 bounding = bounding.
united(geometry(i));
99 if (m_boundingSize != bounding.
size()) {
100 m_boundingSize = bounding.
size();
105 void Screens::setCount(
int count)
107 if (m_count == count) {
110 const int previous = m_count;
112 emit countChanged(previous, count);
115 void Screens::setCurrent(
int current)
117 if (m_current == current) {
121 emit currentChanged();
124 void Screens::setCurrent(
const QPoint &pos)
126 setCurrent(number(pos));
129 void Screens::setCurrent(
const Client *c)
139 void Screens::setCurrentFollowsMouse(
bool follows)
141 if (m_currentFollowsMouse == follows) {
144 m_currentFollowsMouse = follows;
147 int Screens::current()
const
149 if (m_currentFollowsMouse) {
150 return number(Cursor::pos());
152 Client *client = Workspace::self()->activeClient();
153 if (client && !client->isOnScreen(m_current)) {
154 return client->screen();
159 int Screens::intersecting(
const QRect &r)
const
162 for (
int i = 0; i < count(); ++i) {
163 if (geometry(i).intersects(r)) {
170 DesktopWidgetScreens::DesktopWidgetScreens(
QObject *parent)
213 WaylandScreens::WaylandScreens(
QObject* parent)
218 WaylandScreens::~WaylandScreens()
222 void WaylandScreens::init()
226 this, &WaylandScreens::startChangedTimer);
230 QRect WaylandScreens::geometry(
int screen)
const
232 if (screen >= m_geometries.size()) {
235 return m_geometries.at(screen);
238 QSize WaylandScreens::size(
int screen)
const
240 return geometry(screen).size();
243 int WaylandScreens::number(
const QPoint &pos)
const
246 int minDistance = INT_MAX;
247 for (
int i = 0; i < m_geometries.size(); ++i) {
248 const QRect &geo = m_geometries.at(i);
252 int distance =
QPoint(geo.
topLeft() - pos).manhattanLength();
253 distance = qMin(distance,
QPoint(geo.
topRight() - pos).manhattanLength());
255 distance = qMin(distance,
QPoint(geo.
bottomLeft() - pos).manhattanLength());
256 if (distance < minDistance) {
257 minDistance = distance;
264 void WaylandScreens::updateCount()
266 m_geometries.clear();
269 for (
auto it = outputs.
begin(); it != outputs.
end(); ++it) {
270 if ((*it)->pixelSize().isEmpty()) {
274 m_geometries.
append(
QRect((*it)->globalPosition(), (*it)->pixelSize()));
276 if (m_geometries.isEmpty()) {
278 m_geometries.append(
QRect(0, 0, displayWidth(), displayHeight()));
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;
294 static bool setNewScreenSize(
const QSize &size)
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;
305 static xcb_randr_crtc_t getCrtc(
const xcb_randr_get_screen_resources_current_reply_t* r)
307 if (xcb_randr_get_screen_resources_current_crtcs_length(r) == 0) {
308 qDebug() <<
"No CRTCs";
311 return xcb_randr_get_screen_resources_current_crtcs(r)[0];
314 static xcb_randr_output_t getOutputForCrtc(xcb_randr_crtc_t crtc)
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) {
321 return xcb_randr_get_crtc_info_outputs(info.data())[0];
324 static xcb_randr_mode_t createNewMode(
const QSize &size)
327 qDebug() <<
"Creating a new mode";
331 xcb_randr_mode_info_t newInfo;
332 newInfo.dot_clock = 0;
333 newInfo.height = size.
height();
335 newInfo.hsync_end = 0;
336 newInfo.hsync_start = 0;
337 newInfo.htotal = size.
width();
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()) {
353 static xcb_randr_mode_t getModeForSize(
const QSize &size,
const xcb_randr_get_screen_resources_current_reply_t* r)
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);
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";
366 return createNewMode(size);
369 static bool addModeToOutput(xcb_randr_output_t output, xcb_randr_mode_t mode)
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) {
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;
389 void WaylandScreens::updateXRandr()
392 qDebug() <<
"No RandR extension available, cannot sync with X";
396 foreach (
const QRect &rect, m_geometries) {
397 screens = screens.
united(rect);
404 RandR::CurrentResources currentResources(rootWindow());
405 xcb_randr_crtc_t crtc = getCrtc(currentResources.data());
406 if (crtc == XCB_NONE) {
409 xcb_randr_output_t output = getOutputForCrtc(crtc);
410 if (output == XCB_NONE) {
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);
418 if (!setNewScreenSize(size)) {
422 xcb_randr_mode_t mode = getModeForSize(size, currentResources.data());
423 if (mode == XCB_NONE) {
427 if (!addModeToOutput(output, mode)) {
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);
bool activeMouseScreen() const
Get ActiveMouseScreen.
KWin uses X11 for managing windows, but renders to a Wayland compositor.
static Application * kwinApp()
QPoint bottomRight() const
QPoint bottomLeft() const
QRect boundingRect() const
virtual void init()
Called once the singleton instance has been created.
QString number(int n, int base)
void append(const T &value)
bool contains(const QPoint &point, bool proper) const
bool isOnScreen(int screen) const
KWin uses only X11 for managing windows and compositing.
QRect geometry() const
The bounding geometry of all screens combined.
The Client class encapsulates a window decoration frame.
static Extensions * self()
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QRect united(const QRect &rectangle) const
QRegion united(const QRegion &r) const
QSize size() const
The bounding size of all screens combined.