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
|
/*
* Copyright (C) 2018 Tianjin KYLIN Information Technology 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, 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 <QCoreApplication>
#include <QDBusConnection>
#include <QDebug>
#include "interface.h"
#include "sessionwatcher.h"
#include "screensaveradaptor.h"
#include "types.h"
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
void sig_chld(int /*signo*/)
{
pid_t pid;
while( (pid = waitpid(-1, NULL, WNOHANG)) > 0)
qDebug() << "child" << pid << "terminated";
return;
}
int main(int argc, char *argv[])
{
// if(signal(SIGCHLD, sig_chld) == SIG_ERR) {
// perror("signal error");
// exit(EXIT_FAILURE);
// }
QCoreApplication a(argc, argv);
// 检查该程序是否已经有实例在运行
QDBusInterface *checkInterface =
new QDBusInterface("org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
QDBusConnection::sessionBus());
QDBusReply<bool> ret = checkInterface->call("NameHasOwner",
"cn.kylinos.ScreenSaver");
if(ret.value()) {
qDebug() << "There is an instance running";
exit(EXIT_FAILURE);
}
// 如果已经有实例在运行则kill, 主要是针对注销后重新登录时之前的实例没有被kill掉
char cmd[128] = {0};
char str[16];
FILE *fp;
int pid;
int n = sprintf(cmd, "ps aux | grep ukui-screensaver-backend | grep %s | grep -v grep | awk '{print $2}'", getenv("USER"));
Q_UNUSED(n)
fp = popen(cmd, "r");
while(fgets(str, sizeof(str)-1, fp)) {
pid = atoi(str);
if(pid > 0 && pid != getpid()) {
qDebug() << "existing instance pid: " << pid;
kill(pid, SIGKILL);
}
}
pclose(fp);
// for PowerManager
fp = popen("xset s 0 0", "r");
fclose(fp);
// Q_UNUSED(fp)
// 注册DBus
Interface *interface = new Interface();
ScreenSaverAdaptor adaptor(interface);
QDBusConnection service = QDBusConnection::sessionBus();
if(!service.registerService(SS_DBUS_SERVICE)) {
qDebug() << service.lastError().message();
exit(EXIT_FAILURE);
}
if(!service.registerObject(SS_DBUS_PATH, SS_DBUS_SERVICE, &adaptor,
QDBusConnection::ExportAllSlots |
QDBusConnection::ExportAllSignals)) {
qDebug() << service.lastError().message();
exit(EXIT_FAILURE);
}
qDebug() << service.baseService();
// 发送DBus信号
SessionWatcher *watcher = new SessionWatcher;
QObject::connect(watcher, &SessionWatcher::sessionIdle,
interface, &Interface::onSessionIdleReceived);
QObject::connect(watcher, &SessionWatcher::sessionIdle,
&a, [&]{
QDBusMessage message = QDBusMessage::createSignal(SS_DBUS_PATH,
SS_DBUS_INTERFACE,
"SessionIdle");
service.send(message);
});
QObject::connect(checkInterface, SIGNAL(NameLost(QString)),
interface, SLOT(onNameLost(QString)));
return a.exec();
}
|