File indexing completed on 2024-05-19 15:27:57

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 <QX11Info>
0011 #include <xcb/xcb.h>
0012 #include <xcb/xcb_atom.h>
0013 #endif
0014 
0015 DisplayColorSpace::DisplayColorSpace(QObject *parent)
0016     : QObject(parent)
0017 {
0018     m_colorSpace = QColorSpace{QColorSpace::SRgb};
0019     update();
0020 }
0021 
0022 QColorSpace DisplayColorSpace::colorSpace() const
0023 {
0024     return m_colorSpace;
0025 }
0026 
0027 void DisplayColorSpace::update()
0028 {
0029 #ifdef HAVE_X11
0030     if (QX11Info::isPlatformX11()) {
0031         static const char *icc_profile = "_ICC_PROFILE";
0032         auto atom_cookie = xcb_intern_atom(QX11Info::connection(), 0, sizeof(icc_profile), icc_profile);
0033         auto atom_reply = xcb_intern_atom_reply(QX11Info::connection(), atom_cookie, nullptr);
0034         if (!atom_reply) {
0035             return;
0036         }
0037 
0038         auto icc_atom = atom_reply->atom;
0039         free(atom_reply);
0040 
0041         auto cookie = xcb_get_property(QX11Info::connection(), // connection
0042                                        0, // delete
0043                                        QX11Info::appRootWindow(), // window
0044                                        icc_atom, // property
0045                                        XCB_ATOM_CARDINAL, // type
0046                                        0, // offset
0047                                        0); // length
0048         auto result = xcb_get_property_reply(QX11Info::connection(), cookie, nullptr);
0049         if (!result) {
0050             return;
0051         }
0052 
0053         auto length = xcb_get_property_value_length(result);
0054         if (length <= 0) {
0055             return;
0056         }
0057 
0058         auto data = QByteArray(static_cast<const char *>(xcb_get_property_value(result)), length);
0059         auto colorSpace = QColorSpace::fromIccProfile(data);
0060         if (colorSpace.isValid()) {
0061             m_colorSpace = colorSpace;
0062         }
0063 
0064         free(result);
0065     }
0066 #endif
0067 }