1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2013 Martin Gräßlin <mgraesslin@kde.org>
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.
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.
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_wayland.h"
22
#include "wayland_backend.h"
23
#include <KWayland/Client/output.h>
30
#include <xcb/randr.h>
35
WaylandScreens::WaylandScreens(Wayland::WaylandBackend *backend, QObject* parent)
41
WaylandScreens::~WaylandScreens()
45
void WaylandScreens::init()
48
connect(m_backend, &Wayland::WaylandBackend::outputsChanged,
49
this, &WaylandScreens::startChangedTimer);
53
QRect WaylandScreens::geometry(int screen) const
55
if (screen >= m_geometries.size()) {
58
return m_geometries.at(screen);
61
QSize WaylandScreens::size(int screen) const
63
return geometry(screen).size();
66
int WaylandScreens::number(const QPoint &pos) const
69
int minDistance = INT_MAX;
70
for (int i = 0; i < m_geometries.size(); ++i) {
71
const QRect &geo = m_geometries.at(i);
72
if (geo.contains(pos)) {
75
int distance = QPoint(geo.topLeft() - pos).manhattanLength();
76
distance = qMin(distance, QPoint(geo.topRight() - pos).manhattanLength());
77
distance = qMin(distance, QPoint(geo.bottomRight() - pos).manhattanLength());
78
distance = qMin(distance, QPoint(geo.bottomLeft() - pos).manhattanLength());
79
if (distance < minDistance) {
80
minDistance = distance;
87
void WaylandScreens::updateCount()
91
const QList<KWayland::Client::Output*> &outputs = m_backend->outputs();
92
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
93
if ((*it)->pixelSize().isEmpty()) {
97
m_geometries.append(QRect((*it)->globalPosition(), (*it)->pixelSize()));
99
if (m_geometries.isEmpty()) {
100
// we need a fake screen
101
m_geometries.append(QRect(0, 0, displayWidth(), displayHeight()));
105
setCount(m_geometries.count());
113
XCB_WRAPPER(CurrentResources, xcb_randr_get_screen_resources_current, xcb_window_t)
116
static bool setNewScreenSize(const QSize &size)
118
auto c = xcb_randr_set_screen_size_checked(connection(), rootWindow(), size.width(), size.height(), 1, 1);
119
ScopedCPointer<xcb_generic_error_t> error(xcb_request_check(connection(), c));
120
if (!error.isNull()) {
121
qCDebug(KWIN_CORE) << "Error setting screen size: " << error->error_code;
127
static xcb_randr_crtc_t getCrtc(const xcb_randr_get_screen_resources_current_reply_t* r)
129
if (xcb_randr_get_screen_resources_current_crtcs_length(r) == 0) {
130
qCDebug(KWIN_CORE) << "No CRTCs";
133
return xcb_randr_get_screen_resources_current_crtcs(r)[0];
136
static xcb_randr_output_t getOutputForCrtc(xcb_randr_crtc_t crtc)
138
ScopedCPointer<xcb_randr_get_crtc_info_reply_t> info(xcb_randr_get_crtc_info_reply(
139
connection(), xcb_randr_get_crtc_info(connection(), crtc, XCB_TIME_CURRENT_TIME), nullptr));
140
if (info->num_outputs == 0) {
143
return xcb_randr_get_crtc_info_outputs(info.data())[0];
146
static xcb_randr_mode_t createNewMode(const QSize &size)
148
// need to create the new mode
149
qCDebug(KWIN_CORE) << "Creating a new mode";
150
QString name(QString::number(size.width()));
152
name.append(QString::number(size.height()));
153
xcb_randr_mode_info_t newInfo;
154
newInfo.dot_clock = 0;
155
newInfo.height = size.height();
157
newInfo.hsync_end = 0;
158
newInfo.hsync_start = 0;
159
newInfo.htotal = size.width();
161
newInfo.mode_flags = 0;
162
newInfo.vsync_end = 0;
163
newInfo.vsync_start = 0;
164
newInfo.vtotal = size.height();
165
newInfo.width = size.width();
166
newInfo.name_len = name.length();
167
auto cookie = xcb_randr_create_mode_unchecked(connection(), rootWindow(), newInfo, name.length(), name.toUtf8().constData());
168
ScopedCPointer<xcb_randr_create_mode_reply_t> reply(xcb_randr_create_mode_reply(connection(), cookie, nullptr));
169
if (!reply.isNull()) {
175
static xcb_randr_mode_t getModeForSize(const QSize &size, const xcb_randr_get_screen_resources_current_reply_t* r)
177
xcb_randr_mode_info_t *infos = xcb_randr_get_screen_resources_current_modes(r);
178
const int modeInfoLength = xcb_randr_get_screen_resources_current_modes_length(r);
179
// check available modes
180
for (int i = 0; i < modeInfoLength; ++i) {
181
xcb_randr_mode_info_t modeInfo = infos[i];
182
if (modeInfo.width == size.width() && modeInfo.height == size.height()) {
183
qCDebug(KWIN_CORE) << "Found our required mode";
187
// did not find our mode, so create it
188
return createNewMode(size);
191
static bool addModeToOutput(xcb_randr_output_t output, xcb_randr_mode_t mode)
193
ScopedCPointer<xcb_randr_get_output_info_reply_t> info(xcb_randr_get_output_info_reply(connection(),
194
xcb_randr_get_output_info(connection(), output, XCB_TIME_CURRENT_TIME), nullptr));
195
xcb_randr_mode_t *modes = xcb_randr_get_output_info_modes(info.data());
196
for (int i = 0; i < info->num_modes; ++i) {
197
if (modes[i] == mode) {
201
qCDebug(KWIN_CORE) << "Need to add the mode to output";
202
auto c = xcb_randr_add_output_mode_checked(connection(), output, mode);
203
ScopedCPointer<xcb_generic_error_t> error(xcb_request_check(connection(), c));
204
if (!error.isNull()) {
205
qCDebug(KWIN_CORE) << "Error while adding mode to output: " << error->error_code;
211
void WaylandScreens::updateXRandr()
213
if (kwinApp()->operationMode() == Application::OperationModeXwayland) {
214
// no need to update, will be done automagically by Xwayland
217
if (!Xcb::Extensions::self()->isRandrAvailable()) {
218
qCDebug(KWIN_CORE) << "No RandR extension available, cannot sync with X";
222
foreach (const QRect &rect, m_geometries) {
223
screens = screens.united(rect);
225
const QSize &size = screens.boundingRect().size();
226
if (size.isEmpty()) {
230
RandR::CurrentResources currentResources(rootWindow());
231
xcb_randr_crtc_t crtc = getCrtc(currentResources.data());
232
if (crtc == XCB_NONE) {
235
xcb_randr_output_t output = getOutputForCrtc(crtc);
236
if (output == XCB_NONE) {
239
// first disable the first CRTC
240
xcb_randr_set_crtc_config(connection(), crtc, XCB_TIME_CURRENT_TIME, XCB_TIME_CURRENT_TIME,
241
0, 0, XCB_NONE, XCB_RANDR_ROTATION_ROTATE_0, 0, nullptr);
243
// then set new screen size
244
if (!setNewScreenSize(size)) {
248
xcb_randr_mode_t mode = getModeForSize(size, currentResources.data());
249
if (mode == XCB_NONE) {
253
if (!addModeToOutput(output, mode)) {
257
xcb_randr_set_crtc_config(connection(), crtc, XCB_TIME_CURRENT_TIME, XCB_TIME_CURRENT_TIME,
258
0, 0, mode, XCB_RANDR_ROTATION_ROTATE_0, 1, &output);