summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 7973a680b1959ee65539b8877c00a872757bb368 (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* 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 <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 void createSimpleBackground();

static void sigUsr1(int);

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
    		}
    	}
    }

    // Build background for X server, in case we start a session that
	// doesn't set one on its own this will make sure it's not simply
	// black
	QImage entire;

	// Get a list of non-overlapping screens, as this might lead to a broken
	// greeter with main windows covering other login forms
	QMap<int, QRect> screens;
    if (Global::testMode()) {
        screens.insert(0, QRect(0, 0, 1024, 768));
    } else {
    	QSize desktopSize = QApplication::desktop()->size();
    	qWarning() << "Desktop full size is " << desktopSize;
    	entire = QImage(desktopSize, QImage::Format_RGB32);
        for (int i = 0; i < QApplication::desktop()->screenCount(); ++i) {
            QRect r = QApplication::desktop()->screenGeometry(i);
            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
        }
    }

	// Determine primary screen
	int primary;
	if (screens.contains(QApplication::desktop()->primaryScreen())) {
		// Primary screen still in selection
		primary = QApplication::desktop()->primaryScreen();
	} else {
		// Fallback to first one
		primary = screens.begin().key();
	}

	struct sigaction usr1;

	usr1.sa_handler = &sigUsr1;
	sigemptyset(&usr1.sa_mask);
	usr1.sa_flags = SA_RESTART;

	QSocketNotifier *sn = NULL;
	if (sigaction(SIGUSR1, &usr1, 0) == 0 && ::socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0) {
		sn = new QSocketNotifier(sockets[1], QSocketNotifier::Read);
		QObject::connect(sn, &QSocketNotifier::activated, [](int fd) {
			char tmp[1000];
			read(fd, tmp, sizeof tmp);
		});
	}

    // Now set up all the screens
	QPainter painter(&entire);
    MainWindow *focusWindow = 0;
    QMapIterator<int, QRect> it(screens);
    while (it.hasNext()) {
    	it.next();
        MainWindow *w = new MainWindow(primary == it.key(), it.key(), it.value());
        w->show();
		if (sn != NULL) {
			QObject::connect(sn, SIGNAL(activated(int)), w, SLOT(showStandby()));
		}
        if (w->showLoginForm()) {
            focusWindow = w;
        }
        if (!entire.isNull()) {
        	QPoint p = it.value().topLeft();
        	painter.drawImage(p, w->getBackground());
        }
    }

    if (!entire.isNull()) {
        qWarning() << "Setting x background";
    	AddPixmapToBackground(entire.constBits(), entire.width(), entire.height(), 24, entire.bytesPerLine(), entire.byteCount());
    }

    // Ensure we set the primary screen's widget as active when there
    // are more screens
    if (focusWindow) {
        focusWindow->setFocus(Qt::OtherFocusReason);
        focusWindow->activateWindow();
    }

    return a.exec();
}

static void createSimpleBackground()
{
    QImage img = Global::getConfigGradient();
    if (img.isNull())
        return;
    img = img.scaled(QApplication::desktop()->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    AddPixmapToBackground(img.constBits(), img.width(), img.height(), 24, img.bytesPerLine(), img.byteCount());
}

static void sigUsr1(int)
{
	char a = 1;
	::write(sockets[0], &a, sizeof(a));
}