MauiKit Controls

linux/windowhelper.cpp
1/*
2 * Copyright (C) 2021 CutefishOS Team.
3 *
4 * Author: cutefish <cutefishos@foxmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "windowhelper.h"
21
22#include <QApplication>
23#include <QX11Info>
24#include <QCursor>
25
26#include <KWindowSystem>
27
28static uint qtEdgesToXcbMoveResizeDirection(Qt::Edges edges)
29{
30 if (edges == (Qt::TopEdge | Qt::LeftEdge))
31 return 0;
32 if (edges == Qt::TopEdge)
33 return 1;
34 if (edges == (Qt::TopEdge | Qt::RightEdge))
35 return 2;
36 if (edges == Qt::RightEdge)
37 return 3;
38 if (edges == (Qt::RightEdge | Qt::BottomEdge))
39 return 4;
40 if (edges == Qt::BottomEdge)
41 return 5;
42 if (edges == (Qt::BottomEdge | Qt::LeftEdge))
43 return 6;
44 if (edges == Qt::LeftEdge)
45 return 7;
46
47 return 0;
48}
49
50WindowHelper::WindowHelper(QObject *parent)
51 : QObject(parent)
52 , m_moveResizeAtom(0)
53 , m_compositing(false)
54{
55 // create move-resize atom
56 // ref: https://github.com/qt/qtbase/blob/9db7cc79a26ced4997277b5c206ca15949133240/src/plugins/platforms/xcb/qxcbwindow.cpp
57 xcb_connection_t* connection(QX11Info::connection());
58 const QString atomName(QStringLiteral("_NET_WM_MOVERESIZE"));
59 xcb_intern_atom_cookie_t cookie(xcb_intern_atom(connection, false, atomName.size(), qPrintable(atomName)));
60 QScopedPointer<xcb_intern_atom_reply_t> reply(xcb_intern_atom_reply(connection, cookie, nullptr));
61 m_moveResizeAtom = reply ? reply->atom : 0;
62
63 onCompositingChanged(KWindowSystem::compositingActive());
64 connect(KWindowSystem::self(), &KWindowSystem::compositingChanged, this, &WindowHelper::onCompositingChanged);
65}
66
67bool WindowHelper::compositing() const
68{
69 return m_compositing;
70}
71
72void WindowHelper::startSystemMove(QWindow *w)
73{
74 doStartSystemMoveResize(w, 16);
75}
76
77void WindowHelper::startSystemResize(QWindow *w, Qt::Edges edges)
78{
79 doStartSystemMoveResize(w, edges);
80}
81
82void WindowHelper::minimizeWindow(QWindow *w)
83{
84 KWindowSystem::minimizeWindow(w->winId());
85}
86
87void WindowHelper::doStartSystemMoveResize(QWindow *w, int edges)
88{
89 const qreal dpiRatio = qApp->devicePixelRatio();
90
91 xcb_connection_t *connection(QX11Info::connection());
92 xcb_client_message_event_t xev;
93 xev.response_type = XCB_CLIENT_MESSAGE;
94 xev.type = m_moveResizeAtom;
95 xev.sequence = 0;
96 xev.window = w->winId();
97 xev.format = 32;
98 xev.data.data32[0] = QCursor::pos().x() * dpiRatio;
99 xev.data.data32[1] = QCursor::pos().y() * dpiRatio;
100
101 if (edges == 16)
102 xev.data.data32[2] = 8; // move
103 else
104 xev.data.data32[2] = qtEdgesToXcbMoveResizeDirection(Qt::Edges(edges));
105
106 xev.data.data32[3] = XCB_BUTTON_INDEX_1;
107 xev.data.data32[4] = 0;
108 xcb_ungrab_pointer(connection, XCB_CURRENT_TIME);
109 xcb_send_event(connection, false, QX11Info::appRootWindow(),
110 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
111 (const char *)&xev);
112}
113
114void WindowHelper::onCompositingChanged(bool enabled)
115{
116 if (enabled != m_compositing) {
117 m_compositing = enabled;
118 emit compositingChanged();
119 }
120}
static KWindowSystem * self()
QPoint pos()
int x() const const
int y() const const
typedef Edges
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
WId winId() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.