1 /*****************************************************************************
2  *   Copyright 2007 - 2010 Craig Drummond <craig.p.drummond@gmail.com>       *
3  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
4  *                                                                           *
5  *   This program is free software; you can redistribute it and/or modify    *
6  *   it under the terms of the GNU Lesser General Public License as          *
7  *   published by the Free Software Foundation; either version 2.1 of the    *
8  *   License, or (at your option) version 3, or any later version accepted   *
9  *   by the membership of KDE e.V. (or its successor approved by the         *
10  *   membership of KDE e.V.), which shall act as a proxy defined in          *
11  *   Section 6 of version 3 of the license.                                  *
12  *                                                                           *
13  *   This program is distributed in the hope that it will be useful,         *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
16  *   Lesser General Public License for more details.                         *
17  *                                                                           *
18  *   You should have received a copy of the GNU Lesser General Public        *
19  *   License along with this library. If not,                                *
20  *   see <http://www.gnu.org/licenses/>.                                     *
21  *****************************************************************************/
22 
23 #include <qtcurve-utils/log.h>
24 #include "utils.h"
25 #include <qtcurve-utils/x11utils.h>
26 #include <qtcurve-utils/qtutils.h>
27 #include <QApplication>
28 #include <QDesktopWidget>
29 #include <QWindow>
30 
31 #ifdef QTC_QT5_ENABLE_KDE
32 #  include <kdeversion.h>
33 #  include <KDE/KWindowSystem>
34 #endif
35 
36 namespace QtCurve {
37 namespace Utils {
38 
39 bool
compositingActive()40 compositingActive()
41 {
42 #ifndef QTC_QT5_ENABLE_KDE
43     // DO NOT condition compile on QTC_ENABLE_X11.
44     // There's no direct linkage on X11 and the following code will just do
45     // nothing if X11 is not enabled (either at compile time or at run time).
46     return qtcX11CompositingActive();
47 #else
48     return KWindowSystem::compositingActive();
49 #endif
50 }
51 
52 static inline WId
findWid(const QWidget * w)53 findWid(const QWidget *w)
54 {
55     do {
56         WId wid = qtcGetWid(w);
57         if (wid || w->isWindow()) {
58             return wid;
59         }
60     } while ((w = w->parentWidget()));
61     return (WId)0;
62 }
63 
64 static QWindow*
findWindowHandle(const QWidget * w)65 findWindowHandle(const QWidget *w)
66 {
67     do {
68         QWindow *window = w->windowHandle();
69         if (window || w->isWindow()) {
70             return window;
71         }
72     } while ((w = w->parentWidget()));
73     return nullptr;
74 }
75 
76 bool
hasAlphaChannel(const QWidget * widget)77 hasAlphaChannel(const QWidget *widget)
78 {
79     if (!widget)
80         return false;
81     if (QWindow *window = findWindowHandle(widget)) {
82         return window->format().alphaBufferSize() > 0;
83     }
84     // DO NOT condition compile on QTC_ENABLE_X11.
85     // There's no direct linkage on X11 and the following code will just do
86     // nothing if X11 is not enabled (either at compile time or at run time).
87     if (qtcX11Enabled()) {
88         if (WId wid = findWid(widget)) {
89             return qtcX11HasAlpha(wid);
90         }
91     }
92     return compositingActive();
93 }
94 
95 }
96 }
97