/*
* Copyright (c) 2012-2015 Christian Surlykke, Petr Vanek
*
* This file is part of qt-lightdm-greeter
* It is distributed under the LGPL 2.1 or later license.
* Please refer to the LICENSE file for a copy of the license.
*/
#include <QtWidgets/QApplication>
#include <QDesktopWidget>
#include <QtGlobal>
#include <QtDebug>
#include <QSettings>
#include <QIcon>
#include <QPainter>
#include <QMap>
#include <QSocketNotifier>
#include <QScreen>
#include <fcntl.h> /* Obtain O_* constant definitions */
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include "settings.h"
#include "mainwindow.h"
#include "x11util.h"
#include "global.h"
Settings *s_settings = new Settings();
static int sockets[2];
static QMap<int, MainWindow*> currentWindows;
static QTimer setupDelay;
void createSimpleBackground();
static void watch(const QScreen *scrn);
static void setupScreens();
static void messageHandler(QtMsgType type, const QMessageLogContext&, const QString& msg)
{
std::cerr << type << ": " << msg.toUtf8().constData() << '\n';
}
static inline int size(const QRect& r)
{
return r.width() * r.height();
}
int main(int argc, char *argv[])
{
if (argc > 1 && QString(argv[1]) == QString("--test")) {
Global::enableTestMode();
}
QApplication a(argc, argv);
// I have no idea why, but Qt's stock qWarning() output never makes it
// to /var/log/lightdm/x-0-greeter.log, so we use std::cerr instead..
qInstallMessageHandler(messageHandler);
if (!Settings::iconThemeName().isEmpty()) {
QIcon::setThemeName(Settings::iconThemeName());
}
if (!Global::testMode() && !Settings::autoLoginCheckCmd().isEmpty()) {
QProcess p;
int ret = QProcess::execute(Settings::autoLoginCheckCmd());
if (ret == 0) {
if (Global::autoLoginGuest()) {
qWarning() << "Guest login ok";
createSimpleBackground();
return a.exec();
} else {
qWarning() << "Guest login failed";
// TODO: Set error message, display somewhere
}
}
}
QObject::connect(&setupDelay, &QTimer::timeout, []() {
setupScreens();
QTimer::singleShot(1000, []() {
setupScreens();
});
});
setupDelay.setInterval(300);
setupDelay.setSingleShot(true);
setupDelay.start();
if (!Global::testMode()) {
for (const auto scrn : QGuiApplication::screens()) {
watch(scrn);
}
QObject::connect(qApp, &QGuiApplication::screenAdded, [](const QScreen *scrn) {
setupDelay.start();
watch(scrn);
});
auto cb = [](const QScreen *) {
setupDelay.start();
};
QObject::connect(qApp, &QGuiApplication::screenRemoved, cb);
QObject::connect(qApp, &QGuiApplication::primaryScreenChanged, cb);
}
/*
if (!entire.isNull()) {
qWarning() << "Setting x background";
AddPixmapToBackground(entire.constBits(), entire.width(), entire.height(),
24, entire.bytesPerLine(), entire.byteCount());
}
*/
return a.exec();
}
static void watch(const QScreen *scrn)
{
QObject::connect(scrn, &QScreen::geometryChanged, [](const QRect &) {
setupDelay.start();
});
QObject::connect(scrn, &QScreen::virtualGeometryChanged, [](const QRect &) {
setupDelay.start();
});
}
void createSimpleBackground()
{
QImage img;
if (currentWindows.isEmpty()) {
img = Global::getConfigGradient();
if (img.isNull()) {
img = QImage(QApplication::desktop()->size(), QImage::Format::Format_RGB32);
} else {
img = img.scaled(QApplication::desktop()->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
} else {
img = QImage(QApplication::desktop()->size(), QImage::Format::Format_RGB32);
QPainter p(&img);
for (auto *w : currentWindows) {
p.drawImage(w->geometry(), w->background(), QRect(QPoint(0, 0), w->geometry().size()));
}
}
AddPixmapToBackground(img.constBits(), img.width(), img.height(),
24, img.bytesPerLine(), img.byteCount());
}
static QList<QRect> allOld;
static int primaryOld = -1;
void setupScreens()
{
// Get a list of non-overlapping screens, as this might lead to a broken
// greeter with main windows covering other login forms
qDebug() << "Reposistioning greeter";
QMap<int, QRect> screens;
QList<QRect> allNew;
if (Global::testMode()) {
screens.insert(0, QRect(0, 0, 1024, 768));
} else {
for (int i = 0; i < QApplication::desktop()->screenCount(); ++i) {
QRect r = QApplication::desktop()->screenGeometry(i);
if (r.width() < 200 || r.height() < 200)
continue; // Sanity check
qDebug() << "Have screen" << r;
allNew.append(r);
QMutableMapIterator<int, QRect>it(screens);
while (it.hasNext()) {
it.next();
if (!it.value().intersects(r))
continue;
// Overlap, bigger wins
if (size(it.value()) >= size(r)) {
// Existing is bigger
goto skip_rect;
}
// New is bigger, remove existing and keep going
it.remove();
}
// We reached here, so add new window
screens.insert(i, r);
skip_rect: ; // Do nothing
}
}
if (screens.isEmpty()) {
// Huh?
QRect r = QApplication::desktop()->geometry();
r.setTopLeft(QPoint(0, 0));
r = r.united(QRect(0, 0, 640, 480));
qDebug() << "No screens found, adding fake screen" << r;
screens.insert(0, r);
allNew.insert(0, r);
}
// Spurious event?
int primaryNew = QApplication::desktop()->primaryScreen();
if (primaryNew == primaryOld && allOld == allNew)
return;
primaryOld = primaryNew;
allOld = allNew;
// Determine primary screen
int primary;
if (screens.contains(primaryNew)) {
// Primary screen still in selection
primary = primaryNew;
} else {
// Fallback to first one
primary = screens.begin().key();
}
// Try to reuse as many mainwindows as possible
QMap<int, MainWindow*> remaining = currentWindows;
do {
currentWindows.clear();
QMutableMapIterator<int, QRect> it(screens);
while (it.hasNext()) {
it.next();
MainWindow *old = remaining.take(it.key());
if (old != nullptr) {
// Existing
if (old->showLoginForm() == (primary == it.key())) {
qDebug() << "Re-using existing window for" << it.value();
old->setGeometry(it.value());
currentWindows.insert(it.key(), old);
it.remove();
if (old->showLoginForm()) {
old->setFocus(Qt::OtherFocusReason);
old->activateWindow();
}
} else {
old->deleteLater();
}
}
}
} while(0);
// Now set up remaining screens
QMapIterator<int, QRect> it(screens);
while (it.hasNext()) {
it.next();
MainWindow *old = nullptr;
if (!remaining.empty()) {
old = remaining.take(remaining.firstKey());
if (old != nullptr) {
if (old->showLoginForm() == (primary == it.key())) {
qDebug() << "This should never happen, reusing old window II" << it.value();
old->setGeometry(it.value());
} else {
old->deleteLater();
old = nullptr;
}
}
}
if (old == nullptr) {
qDebug() << "Adding a window for" << it.value();
old = new MainWindow(primary == it.key(), it.key(), it.value());
old->show();
}
currentWindows.insert(it.key(), old);
if (old->showLoginForm()) {
old->setFocus(Qt::OtherFocusReason);
old->activateWindow();
}
}
for (MainWindow *old : remaining) {
old->deleteLater();
}
}