Debian Codegraph @

examples/frameless-window/main.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include <QGuiApplication>
6 #include <QWindow>
7 #include <QX11Info>
8 #include <QMouseEvent>
9
10 #include <qpa/qplatformwindow.h>
11 #include <xcb/xcb.h>
12
13 void setWindowProperty(quint32 WId, xcb_atom_t propAtom, xcb_atom_t typeAtom, const void *data, quint32 len, uint8_t format)
14 {
15 xcb_connection_t* conn = QX11Info::connection();
16 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, WId, propAtom, typeAtom, format, len, data);
17 xcb_flush(conn);
18 }
19
20 void clearWindowProperty(quint32 WId, xcb_atom_t propAtom)
21 {
22 xcb_delete_property_checked(QX11Info::connection(), WId, propAtom);
23 }
24
25 xcb_atom_t internAtom(xcb_connection_t *connection, const char *name, bool only_if_exists)
26 {
27 if (!name || *name == 0)
28 return XCB_NONE;
29
30 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(connection, only_if_exists, strlen(name), name);
31 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, 0);
32
33 if (!reply)
34 return XCB_NONE;
35
36 xcb_atom_t atom = reply->atom;
37 free(reply);
38
39 return atom;
40 }
41
42 xcb_atom_t internAtom(const char *name, bool only_if_exists)
43 {
44 return internAtom(QX11Info::connection(), name, only_if_exists);
45 }
46
47 bool isSupported(xcb_atom_t targetAtom)
48 {
49 // get _NET_SUPPORTED from root window
50 QVector<xcb_atom_t> net_wm_atoms;
51
52 xcb_window_t root = QX11Info::appRootWindow();
53 int offset = 0;
54 int remaining = 0;
55 xcb_connection_t *xcb_connection = QX11Info::connection();
56 auto _net_supported = internAtom(QT_STRINGIFY(_NET_SUPPORTED), false);
57
58 do {
59 xcb_get_property_cookie_t cookie = xcb_get_property(xcb_connection, false, root, _net_supported,
60 XCB_ATOM_ATOM, offset, 1024);
61 xcb_get_property_reply_t *reply = xcb_get_property_reply(xcb_connection, cookie, NULL);
62 if (!reply)
63 break;
64
65 remaining = 0;
66
67 if (reply->type == XCB_ATOM_ATOM && reply->format == 32) {
68 int len = xcb_get_property_value_length(reply)/sizeof(xcb_atom_t);
69 xcb_atom_t *atoms = (xcb_atom_t *)xcb_get_property_value(reply);
70 int s = net_wm_atoms.size();
71 net_wm_atoms.resize(s + len);
72 memcpy(net_wm_atoms.data() + s, atoms, len*sizeof(xcb_atom_t));
73
74 remaining = reply->bytes_after;
75 offset += len;
76 }
77
78 free(reply);
79 } while (remaining > 0);
80
81 return net_wm_atoms.contains(targetAtom);
82 }
83
84 bool setNoTitlebar(quint32 WId, bool on)
85 {
86 xcb_atom_t _deepin_no_titlebar = internAtom(QT_STRINGIFY(_DEEPIN_NO_TITLEBAR), false);
87 if (!isSupported(_deepin_no_titlebar))
88 return false;
89
90 quint8 value = on;
91 setWindowProperty(WId, _deepin_no_titlebar, XCB_ATOM_CARDINAL, &value, 1, 8);
92
93 // force enable window decorations, becouse we need the window shadow and border, its contains in decorations.
94 xcb_atom_t _deepin_force_decorate = internAtom(QT_STRINGIFY(_DEEPIN_FORCE_DECORATE), false);
95 if (on) {
96 quint8 value = on;
97 setWindowProperty(WId, _deepin_force_decorate, XCB_ATOM_CARDINAL, &value, 1, 8);
98 } else {
99 clearWindowProperty(WId, _deepin_force_decorate);
100 }
101
102 return true;
103 }
104
105 QObject *createNativeSettingsFor(QWindow *window)
106 {
107 // The platform function in dxcb plugin:
108 // https://github.com/linuxdeepin/qt5platform-plugins/blob/master/src/dnativesettings.h
109 static QFunctionPointer build_function = qApp->platformFunction("_d_buildNativeSettings");
110
111 if (!build_function) {
112 return nullptr;
113 }
114
115 QObject *obj = new QObject(window);
116 bool ok = reinterpret_cast<bool(*)(QObject*, quint32)>(build_function)(obj, window->winId());
117 if (!ok) {
118 delete obj;
119 return nullptr;
120 }
121
122 return obj;
123 }
124
125 class Window : public QWindow
126 {
127 public:
128 bool handleMoveing = false;
129
130 private:
131 void mouseMoveEvent(QMouseEvent *e) override {
132 if (handleMoveing && e->buttons() == Qt::LeftButton) {
133 handle()->startSystemMove();
134 } else {
135 QWindow::mouseMoveEvent(e);
136 }
137 }
138 };
139
140 int main(int argc, char *argv[])
141 {
142 QGuiApplication app(argc, argv);
143 Window window;
144 window.resize(200, 200);
145
146 if (setNoTitlebar(window.winId(), true)) {
147 // If enabled no titlebar mode, the window is no min/max/close buttons, you need to implement it by Qt widgets.
148 // Like as https://github.com/linuxdeepin/dtkwidget/blob/master/src/widgets/dtitlebar.cpp
149 window.handleMoveing = true;
150
151 auto nativeSettings = createNativeSettingsFor(&window);
152 if (nativeSettings) {
153 nativeSettings->setProperty("DTK/WindowRadius", "100,100");
154 nativeSettings->setProperty("borderWidth", 2);
155 nativeSettings->setProperty("borderColor", QColor(Qt::red));
156 nativeSettings->setProperty("shadowOffset", "30,30");
157 nativeSettings->setProperty("shadowRadius", 30);
158 nativeSettings->setProperty("shadowColor", QColor(Qt::blue));
159 // See more properties in DPlatformTheme
160 }
161 }
162
163 window.show();
164
165 return app.exec();
166 }