Debian Codegraph @

kysdk-qtwidgets/src/xatom-helper.cpp

raw
1 /*
2 * KWin Style UKUI
3 *
4 * Copyright (C) 2020, KylinSoft Co., Ltd.
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 * (at your option) 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 <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Yue Lan <[email protected]>
20 */
21
22 #include <QGuiApplication>
23 #include <X11/X.h>
24 #include <X11/Xatom.h>
25 #include <limits.h>
26 #include <xcb/xcb.h>
27 #include "xatom-helper.h"
28 // #include <NETWM>
29 using namespace kdk;
30 static XAtomHelper *global_instance = nullptr;
31
32 class X11Display
33 {
34 public:
35 static Display *display();
36 static void closeDisplay();
37
38 private:
39 static Display *s_display;
40 };
41
42 Display *X11Display::s_display = nullptr;
43
44 Display *X11Display::display()
45 {
46 if (!s_display) {
47 s_display = XOpenDisplay(nullptr);
48 if (s_display) {
49 atexit(closeDisplay);
50 }
51 }
52 return s_display;
53 }
54
55 void X11Display::closeDisplay()
56 {
57 if (s_display) {
58 XCloseDisplay(s_display);
59 s_display = nullptr;
60 }
61 }
62
63 XAtomHelper *XAtomHelper::getInstance()
64 {
65 if (!global_instance)
66 global_instance = new XAtomHelper;
67 return global_instance;
68 }
69
70 bool XAtomHelper::isFrameLessWindow(int winId)
71 {
72 auto hints = getInstance()->getWindowMotifHint(winId);
73 if (hints.flags == MWM_HINTS_DECORATIONS && hints.functions == 1) {
74 return true;
75 }
76 return false;
77 }
78
79 bool XAtomHelper::isWindowDecorateBorderOnly(int winId)
80 {
81 return isWindowMotifHintDecorateBorderOnly(getInstance()->getWindowMotifHint(winId));
82 }
83
84 bool XAtomHelper::isWindowMotifHintDecorateBorderOnly(const MotifWmHints &hint)
85 {
86 bool isDeco = false;
87 if (hint.flags & MWM_HINTS_DECORATIONS && hint.flags != MWM_HINTS_DECORATIONS) {
88 if (hint.decorations == MWM_DECOR_BORDER)
89 isDeco = true;
90 }
91 return isDeco;
92 }
93
94 bool XAtomHelper::isUKUICsdSupported()
95 {
96 // fixme:
97 return false;
98 }
99
100 bool XAtomHelper::isUKUIDecorationWindow(int winId)
101 {
102 if (m_ukuiDecorationAtion == None)
103 return false;
104
105 Atom type;
106 int format;
107 ulong nitems;
108 ulong bytes_after;
109 uchar *data;
110
111 bool isUKUIDecoration = false;
112
113 XGetWindowProperty(X11Display::display(), winId, m_ukuiDecorationAtion,
114 0, LONG_MAX, false,
115 m_ukuiDecorationAtion, &type,
116 &format, &nitems,
117 &bytes_after, &data);
118
119 if (type == m_ukuiDecorationAtion) {
120 if (nitems == 1) {
121 isUKUIDecoration = data[0];
122 }
123 }
124
125 return isUKUIDecoration;
126 }
127
128 UnityCorners XAtomHelper::getWindowBorderRadius(int winId)
129 {
130 UnityCorners corners;
131
132 Atom type;
133 int format;
134 ulong nitems;
135 ulong bytes_after;
136 uchar *data;
137
138 if (m_unityBorderRadiusAtom != None) {
139 XGetWindowProperty(X11Display::display(), winId, m_unityBorderRadiusAtom,
140 0, LONG_MAX, false,
141 XA_CARDINAL, &type,
142 &format, &nitems,
143 &bytes_after, &data);
144
145 if (type == XA_CARDINAL) {
146 if (nitems == 4) {
147 corners.topLeft = static_cast<ulong>(data[0]);
148 corners.topRight = static_cast<ulong>(data[1 * sizeof(ulong)]);
149 corners.bottomLeft = static_cast<ulong>(data[2 * sizeof(ulong)]);
150 corners.bottomRight = static_cast<ulong>(data[3 * sizeof(ulong)]);
151 }
152 XFree(data);
153 }
154 }
155
156 return corners;
157 }
158
159 void XAtomHelper::setWindowBorderRadius(int winId, const UnityCorners &data)
160 {
161 if (m_unityBorderRadiusAtom == None)
162 return;
163
164 ulong corners[4] = {data.topLeft, data.topRight, data.bottomLeft, data.bottomRight};
165
166 XChangeProperty(X11Display::display(), winId, m_unityBorderRadiusAtom, XA_CARDINAL,
167 32, 0, (const unsigned char *)&corners, sizeof(corners) / sizeof(corners[0]));
168 }
169
170 void XAtomHelper::setWindowBorderRadius(int winId, int topLeft, int topRight, int bottomLeft, int bottomRight)
171 {
172 if (m_unityBorderRadiusAtom == None)
173 return;
174
175 ulong corners[4] = {(ulong)topLeft, (ulong)topRight, (ulong)bottomLeft, (ulong)bottomRight};
176
177 XChangeProperty(X11Display::display(), winId, m_unityBorderRadiusAtom, XA_CARDINAL,
178 32, 0, (const unsigned char *)&corners, sizeof(corners) / sizeof(corners[0]));
179 }
180
181 void XAtomHelper::setUKUIDecoraiontHint(int winId, bool set)
182 {
183 if (m_ukuiDecorationAtion == None)
184 return;
185
186 XChangeProperty(X11Display::display(), winId, m_ukuiDecorationAtion, m_ukuiDecorationAtion, 32, 0, (const unsigned char *)&set, 1);
187 }
188
189 void XAtomHelper::setWindowMotifHint(int winId, const MotifWmHints &hints)
190 {
191 if (m_unityBorderRadiusAtom == None)
192 return;
193
194 XChangeProperty(X11Display::display(), winId, m_motifWMHintsAtom, m_motifWMHintsAtom,
195 32, 0, (const unsigned char *)&hints, sizeof(MotifWmHints) / sizeof(ulong));
196 }
197
198 MotifWmHints XAtomHelper::getWindowMotifHint(int winId)
199 {
200 MotifWmHints hints;
201
202 if (m_unityBorderRadiusAtom == None)
203 return hints;
204
205 uchar *data;
206 Atom type;
207 int format;
208 ulong nitems;
209 ulong bytes_after;
210
211 XGetWindowProperty(X11Display::display(), winId, m_motifWMHintsAtom,
212 0, sizeof(MotifWmHints) / sizeof(long), false, AnyPropertyType, &type,
213 &format, &nitems, &bytes_after, &data);
214
215 if (type == None) {
216 return hints;
217 } else {
218 hints = *(MotifWmHints *)data;
219 XFree(data);
220 }
221 return hints;
222 }
223
224 XAtomHelper::XAtomHelper(QObject *parent)
225 : QObject(parent)
226 {
227 if (QGuiApplication::platformName() != "xcb")
228 return;
229
230 m_motifWMHintsAtom = XInternAtom(X11Display::display(), "_MOTIF_WM_HINTS", true);
231 m_unityBorderRadiusAtom = XInternAtom(X11Display::display(), "_UNITY_GTK_BORDER_RADIUS", false);
232 m_ukuiDecorationAtion = XInternAtom(X11Display::display(), "_KWIN_UKUI_DECORAION", false);
233 }
234
235 Atom XAtomHelper::registerUKUICsdNetWmSupportAtom()
236 {
237 // fixme:
238 return None;
239 }
240
241 void XAtomHelper::unregisterUKUICsdNetWmSupportAtom()
242 {
243 // fixme:
244 }