summaryrefslogblamecommitdiffstats
path: root/src/main.cpp
blob: ece6b874b8219d95372afc1ef82a942b8c58c58e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                             

                   
 


                                                                     



                       
                    
 



                                                                                         
 
                                      
 
                                      



                                
                
                                                                          

                                                                         






                                                        




                                                                     
                                                         


                                                           

                                                                            


                                                                                






















                                                                          









                                                                         
                                 
                                


                                         
                                                                                            
                  
                                 
                            







                                                         
                                             
                                                                                                                                  










                                                                     
 
/*
* 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 <fcntl.h>              /* Obtain O_* constant definitions */
#include <unistd.h>

#include <iostream>

#include "settings.h"
#include "mainwindow.h"
#include "x11util.h"

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[])
{
    //dup2(2, 1)
    // 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);

    QApplication a(argc, argv);

    if (! Settings().iconThemeName().isEmpty()) {
        QIcon::setThemeName(Settings().iconThemeName());
    }

    // 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;
	QSize desktopSize = QApplication::desktop()->size();
    qWarning() << "Desktop full size is " << desktopSize;
	entire = QImage(desktopSize, QImage::Format_RGB32);
	QPainter painter(&entire);

    const bool testMode = argc > 1 && QString(argv[1]) == QString("--test");

	// 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 (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);
            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();
	}

    // Now set up all the screens
    MainWindow *focusWindow = 0;
    QMapIterator<int, QRect> it(screens);
    while (it.hasNext()) {
    	it.next();
        MainWindow *w = new MainWindow(primary == it.key(), it.key(), it.value(), testMode);
        w->show();
        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();
}