File indexing completed on 2025-01-05 04:55:53

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     utils/systeminfo.cpp
0003 
0004     This file is part of libkleopatra
0005     SPDX-FileCopyrightText: 2022 g10 Code GmbH
0006     SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include <config-libkleo.h>
0012 
0013 #include "systeminfo.h"
0014 
0015 #include <QByteArray>
0016 #include <QtSystemDetection>
0017 
0018 // #include "libkleo_debug.h"
0019 #ifdef Q_OS_WIN
0020 #include "windows.h"
0021 // #include "gnupg-registry.h"
0022 #endif
0023 
0024 #ifdef Q_OS_WIN
0025 namespace
0026 {
0027 bool win_isHighContrastModeActive()
0028 {
0029     HIGHCONTRAST result;
0030     result.cbSize = sizeof(HIGHCONTRAST);
0031     if (SystemParametersInfo(SPI_GETHIGHCONTRAST, result.cbSize, &result, 0)) {
0032         return (result.dwFlags & HCF_HIGHCONTRASTON);
0033     }
0034     return false;
0035 }
0036 
0037 bool win_isDarkModeActive()
0038 {
0039     /* This is a bit complicated. Qt does not detect this correctly
0040      * for high contrast mode with white contrast. */
0041 
0042     // First check for white background. That is set in High contrast
0043     // white theme.
0044     DWORD color = GetSysColor(COLOR_WINDOW);
0045     if (color == 0xFFFFFF) {
0046         return false;
0047     }
0048     // Windows 10 has only one white High Contrast mode. The other
0049     // three are dark.
0050     if (win_isHighContrastModeActive()) {
0051         return true;
0052     }
0053 
0054 #if 0
0055     // This is not enabled because although Qt does check for this, the theme
0056     // does not switch accordingly in tests. So we would have white icons on a
0057     // bright window.
0058     //
0059     // The user may have customized a dark theme. Then AppsUseLightTheme is 0
0060     char *val = read_w32_registry_string ("HKEY_CURRENT_USER",
0061                    "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
0062                    "AppsUseLightTheme");
0063     bool ret = false;
0064     if (val) {
0065         ret = !((DWORD) *val);
0066         free (val);
0067         return ret;
0068     }
0069 #endif
0070     // Nothing set -> default to bright.
0071     return false;
0072 }
0073 }
0074 #endif
0075 
0076 bool Kleo::SystemInfo::isHighContrastModeActive()
0077 {
0078     static bool forceHighContrastMode = qgetenv("KLEO_HIGH_CONTRAST_MODE").toInt();
0079 #ifdef Q_OS_WIN
0080     static bool highContrastModeActive = forceHighContrastMode || win_isHighContrastModeActive();
0081     return highContrastModeActive;
0082 #else
0083     return forceHighContrastMode;
0084 #endif
0085 }
0086 
0087 bool Kleo::SystemInfo::isDarkModeActive()
0088 {
0089 #ifdef Q_OS_WIN
0090     return win_isDarkModeActive();
0091 #else
0092     // Don't know
0093     return isHighContrastModeActive();
0094 #endif
0095 }