File indexing completed on 2024-05-12 04:21:05

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "displaycolorspace.h"
0008 
0009 #ifdef HAVE_X11
0010 #include "private/qtx11extras_p.h"
0011 #include <QGuiApplication>
0012 #include <xcb/xcb.h>
0013 #include <xcb/xcb_atom.h>
0014 #endif
0015 
0016 DisplayColorSpace::DisplayColorSpace(QObject *parent)
0017     : QObject(parent)
0018 {
0019     m_colorSpace = QColorSpace{QColorSpace::SRgb};
0020     update();
0021 }
0022 
0023 QColorSpace DisplayColorSpace::colorSpace() const
0024 {
0025     return m_colorSpace;
0026 }
0027 
0028 void DisplayColorSpace::update()
0029 {
0030 #ifdef HAVE_X11
0031     if (auto *x11Application = qGuiApp->nativeInterface<QNativeInterface::QX11Application>()) {
0032         static const char *icc_profile = "_ICC_PROFILE";
0033         auto atom_cookie = xcb_intern_atom(x11Application->connection(), 0, sizeof(icc_profile), icc_profile);
0034         auto atom_reply = xcb_intern_atom_reply(x11Application->connection(), atom_cookie, nullptr);
0035         if (!atom_reply) {
0036             return;
0037         }
0038 
0039         auto icc_atom = atom_reply->atom;
0040         free(atom_reply);
0041 
0042         auto cookie = xcb_get_property(x11Application->connection(), // connection
0043                                        0, // delete
0044                                        QX11Info::appRootWindow(), // window
0045                                        icc_atom, // property
0046                                        XCB_ATOM_CARDINAL, // type
0047                                        0, // offset
0048                                        0); // length
0049         auto result = xcb_get_property_reply(x11Application->connection(), cookie, nullptr);
0050         if (!result) {
0051             return;
0052         }
0053 
0054         auto length = xcb_get_property_value_length(result);
0055         if (length <= 0) {
0056             return;
0057         }
0058 
0059         auto data = QByteArray(static_cast<const char *>(xcb_get_property_value(result)), length);
0060         auto colorSpace = QColorSpace::fromIccProfile(data);
0061         if (colorSpace.isValid()) {
0062             m_colorSpace = colorSpace;
0063         }
0064 
0065         free(result);
0066     }
0067 #endif
0068 }