2022-06-02 16:31:24 +08:00
|
|
|
|
/*
|
2024-01-19 14:12:25 +08:00
|
|
|
|
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
2022-06-02 16:31:24 +08:00
|
|
|
|
*
|
|
|
|
|
* 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, or (at your option)
|
|
|
|
|
* 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 "screensaver.h"
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QTranslator>
|
|
|
|
|
#include <QWindow>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QX11Info>
|
|
|
|
|
#include <QtX11Extras>
|
|
|
|
|
#include <QVariant>
|
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#define WORKING_DIRECTORY "/usr/share/ukui-screensaver"
|
|
|
|
|
bool bControlFlg = false;//是否控制面板窗口
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
#if(QT_VERSION>=QT_VERSION_CHECK(5,6,0))
|
|
|
|
|
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
|
|
|
|
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
|
|
|
|
#endif
|
|
|
|
|
QApplication a(argc, argv);
|
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGHUP);
|
|
|
|
|
//加载翻译文件
|
|
|
|
|
QString locale = QLocale::system().name();
|
|
|
|
|
QTranslator translator;
|
|
|
|
|
QString qmFile = QString(WORKING_DIRECTORY"/i18n_qm/%1.qm").arg(locale);
|
|
|
|
|
translator.load(qmFile);
|
|
|
|
|
a.installTranslator(&translator);
|
|
|
|
|
qDebug() << "load translation file " << qmFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QCommandLineParser parser;
|
|
|
|
|
QString windowId;
|
2024-01-15 16:03:34 +08:00
|
|
|
|
Screensaver s(false);
|
2022-06-02 16:31:24 +08:00
|
|
|
|
XWindowAttributes xwa;
|
|
|
|
|
|
|
|
|
|
parser.setApplicationDescription("Test helper");
|
|
|
|
|
parser.addHelpOption();
|
|
|
|
|
parser.addVersionOption();
|
|
|
|
|
parser.addPositionalArgument("source", QCoreApplication::translate("main", "Screensaver for ukui-screensaver"));
|
|
|
|
|
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
|
|
|
|
parser.addOptions({
|
|
|
|
|
{{"r", "root"},
|
|
|
|
|
QCoreApplication::translate("main", "show on root window")},
|
|
|
|
|
{{"w", "window-id"},
|
|
|
|
|
QCoreApplication::translate("main", "show on window."),
|
|
|
|
|
QCoreApplication::translate("main", "window id")},
|
|
|
|
|
});
|
|
|
|
|
parser.process(a);
|
|
|
|
|
|
|
|
|
|
bool onWindow = parser.isSet("window-id");
|
|
|
|
|
bool onRoot = parser.isSet("root");
|
|
|
|
|
|
|
|
|
|
double scale = 1;
|
|
|
|
|
QScreen *screen = QApplication::primaryScreen();
|
|
|
|
|
scale = screen->devicePixelRatio();
|
|
|
|
|
|
|
|
|
|
if(onWindow){
|
2024-01-15 16:03:34 +08:00
|
|
|
|
windowId = parser.value("window-id");
|
2022-06-02 16:31:24 +08:00
|
|
|
|
WId wid = windowId.toULong();
|
2024-01-15 16:03:34 +08:00
|
|
|
|
|
|
|
|
|
/*获取窗口属性失败时程序退出,这是为了避免应用调用屏保的一瞬间崩溃,导致
|
|
|
|
|
屏保获取到一个不存在的winid,从而产生一个屏保窗口*/
|
|
|
|
|
|
|
|
|
|
if(!XGetWindowAttributes (QX11Info::display(), wid, &xwa))
|
|
|
|
|
{
|
|
|
|
|
qDebug()<<"XGetWindowAttributes failed";
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-02 16:31:24 +08:00
|
|
|
|
QWindow* window = QWindow::fromWinId(wid);
|
2024-01-15 16:03:34 +08:00
|
|
|
|
s.setProperty("_q_embedded_native_parent_handle",QVariant(wid));
|
|
|
|
|
/*设置焦点穿透*/
|
|
|
|
|
s.setWindowFlag(Qt::WindowTransparentForInput, true);
|
2022-06-02 16:31:24 +08:00
|
|
|
|
s.winId();
|
2024-01-15 16:03:34 +08:00
|
|
|
|
s.windowHandle()->setParent(window);
|
2022-06-02 16:31:24 +08:00
|
|
|
|
#ifndef USE_INTEL
|
|
|
|
|
XClassHint ch;
|
|
|
|
|
ch.res_name = NULL;
|
|
|
|
|
ch.res_class = NULL;
|
|
|
|
|
XGetClassHint (QX11Info::display(), wid, &ch);
|
|
|
|
|
if(ch.res_name && strcmp(ch.res_name,"ukui-control-center")==0){
|
|
|
|
|
bControlFlg = true;
|
|
|
|
|
s.addClickedEvent();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//获取屏保所在屏幕对应的缩放比例。
|
|
|
|
|
for(auto screen : QGuiApplication::screens())
|
|
|
|
|
{
|
|
|
|
|
QPoint pos(xwa.x,xwa.y);
|
|
|
|
|
if(screen->geometry().contains(pos)){
|
|
|
|
|
scale = screen->devicePixelRatio();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.resize(xwa.width/scale + 1,xwa.height/scale + 1);
|
|
|
|
|
s.move(0,0);
|
|
|
|
|
s.show();
|
|
|
|
|
}
|
|
|
|
|
else if(onRoot){
|
|
|
|
|
bControlFlg = false;
|
|
|
|
|
WId wid = QX11Info::appRootWindow();
|
|
|
|
|
QWindow* window = QWindow::fromWinId(wid);
|
|
|
|
|
window->setProperty("_q_embedded_native_parent_handle",QVariant(wid));
|
|
|
|
|
s.winId();
|
|
|
|
|
s.windowHandle()->setParent(window);
|
|
|
|
|
XGetWindowAttributes (QX11Info::display(), wid, &xwa);
|
|
|
|
|
qDebug()<<"xwa.width = "<<xwa.width<<"xwa.height = "<<xwa.height;
|
|
|
|
|
s.resize(xwa.width/scale + 1,xwa.height/scale + 1);
|
|
|
|
|
s.move(0,0);
|
|
|
|
|
s.show();
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
s.resize(1366,768);
|
|
|
|
|
s.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return a.exec();
|
|
|
|
|
}
|