File: lightdm-config-helper.cpp

package info (click to toggle)
ukui-settings-daemon 4.0.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,412 kB
  • sloc: cpp: 39,120; ansic: 3,240; xml: 1,570; sh: 88; makefile: 4
file content (145 lines) | stat: -rw-r--r-- 5,462 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QRegularExpression>
#include "lightdm-config-helper.h"
#include "QGSettings/qgsettings.h"

LightDmConfigHelper::LightDmConfigHelper(QObject *parent)
{
    m_proc = new QProcess(this);
    connect(m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(doProcessResult(int,QProcess::ExitStatus)));
}

LightDmConfigHelper::~LightDmConfigHelper()
{

}

void LightDmConfigHelper::syncSettingsToLightDmDir()
{
    syncXsettingsToLightDm();
    syncMouseToLightDm();

    Q_EMIT applyFinish();
}

void LightDmConfigHelper::setConfigToXorg(OutputsConfig &outputsConfig, QString userName)
{
    bool ret;
    int  icursorSize;
    double scaleFactor = 0;

    QString cursorTheme = "";

    scaleFactor = UsdBaseClass::readUserConfigToLightDM("xsettings","scaling-factor",userName).toString().toDouble();
    icursorSize = UsdBaseClass::readUserConfigToLightDM("xsettings","cursor-size",userName).toString().toInt();
    cursorTheme = UsdBaseClass::readUserConfigToLightDM("xsettings","cursor-theme",userName).toString();

    icursorSize = icursorSize == 0 ? 24: icursorSize;//cursor size default value are 24
    cursorTheme = cursorTheme == "" ? "dark-sense":cursorTheme;// default cursor theme dark-sense

    USD_LOG(LOG_DEBUG,"icursorSize:%d cursorTheme:%s", icursorSize, cursorTheme.toLatin1().data());

    if (scaleFactor==0) {
        scaleFactor = getDefaultDpiScalingFactor(outputsConfig);
        UsdBaseClass::writeUserConfigToLightDM("xsettings","scaling-factor", scaleFactor, userName);
    }

    QString xParam = QString("Xft.dpi:\t%1\nXcursor.size:\t%2\nXcursor.theme:\t%3\n")
            .arg(scaleFactor*96)
            .arg(scaleFactor*icursorSize)
            .arg(cursorTheme);

    Display    *dpy;//why must reopen display
    dpy = XOpenDisplay(NULL);
    ret = XChangeProperty(dpy, RootWindow(dpy, 0), XA_RESOURCE_MANAGER, XA_STRING, 8,
                          PropModeReplace, (unsigned char *) xParam.toLatin1().data(), xParam.length());
    XCloseDisplay(dpy);
    USD_LOG(LOG_DEBUG,"ready set %s =%d",xParam.toLatin1().data(),ret);
}

double LightDmConfigHelper::getDefaultDpiScalingFactor(OutputsConfig &outputsConfig)
{
    double defaultScale = 999.0;

    for (int k = 0; k < outputsConfig.m_outputList.count(); k++) {
        double calScale = 0;
        int width = outputsConfig.m_outputList[k]->property(OUTPUT_WIDTH).toInt();
        int height = outputsConfig.m_outputList[k]->property(OUTPUT_HEIGHT).toInt();

        if (width <= 1920 * 1.1 || height <= 1200 * 1.1) {
            calScale = 1;
        } else if (width <= 2560*1.1 || height <=1500*1.1) {//Compatible with 16:10 resolution
            calScale = 1.5;
        } else {
            calScale = 2;
        }

        defaultScale = defaultScale > calScale ?  calScale : defaultScale;
    }

    return defaultScale == 999.0 ? 1 : defaultScale;
}

void LightDmConfigHelper::doProcessResult(int index, QProcess::ExitStatus exitstatus)
{
    QVariant value;
    if (index == 0) {

        if (m_processKey == SCALING_FACTOR_KEY) {
            value = QString(m_proc->readAllStandardOutput().data()).toDouble();
        } else if (m_processKey == CURSOR_SIZE_KEY) {
            value = QString(m_proc->readAllStandardOutput().data()).toInt();
        }else {
            QRegularExpression re("(?<=').+(?=\')");
            QRegularExpressionMatch reMatch;
            QString settingsValue(m_proc->readAllStandardOutput().data());
            reMatch = re.match(settingsValue);
            USD_LOG(LOG_DEBUG,"settingsValue :%s",reMatch.captured(0).toLatin1().data());
            value = reMatch.captured(0);
        }
        UsdBaseClass::writeUserConfigToLightDM(m_processModule, m_processKey, value, m_userName);
    } else {
        SYS_LOG(LOG_DEBUG,"index:%d,exitstatus:%d restlue:%s",index, exitstatus, m_proc->readAllStandardOutput().data());
    }
}

void LightDmConfigHelper::syncXsettingsToLightDm()
{
    readGsettingsInRoot("xsettings", m_userName, GSETTINGS_XSETTINGS_SCHEMA, SCALING_FACTOR_KEY);
}

void LightDmConfigHelper::syncMouseToLightDm()
{
    readGsettingsInRoot("xsettings", m_userName, GSETTINGS_MOUSE_SCHEMA, CURSOR_THEME_KEY);
    readGsettingsInRoot("xsettings", m_userName, GSETTINGS_MOUSE_SCHEMA, CURSOR_SIZE_KEY);
}

QString LightDmConfigHelper::readGsettingsInRoot(QString module,QString userName, QString schema, QString key)
{
     QString cmd = QString("sudo -u %3 gsettings get %1 %2").arg(schema).arg(key).arg(userName);
     SYS_LOG(LOG_DEBUG,"cmd:%s",cmd.toLatin1().data());
     m_processSchema = schema;
     m_processKey = key;
     m_processModule = module;
     m_proc->start(cmd);
     m_proc->waitForFinished(1000);
     return cmd;
}