Application: Add <cerrno> include
[lxde/liblxqt.git] / lxqtapplication.cpp
CommitLineData
8966cbec 1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
8966cbec 5 * http://razor-qt.org
6 *
7 * Copyright: 2012-2013 Razor team
8 * Authors:
9 * Petr Vanek <petr@scribus.info>
10 *
11 * This program or library is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20
21 * You should have received a copy of the GNU Lesser General
22 * Public License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
25 *
26 * END_COMMON_COPYRIGHT_HEADER */
27
e6389bfc 28#include <QDir>
f18f98db 29
077c1e76 30#include "lxqtapplication.h"
82830dc2 31#include "lxqtsettings.h"
f18f98db 32
e6389bfc 33#include <XdgDirs>
afd3b116 34
f05ba5af 35using namespace LXQt;
8966cbec 36
00f0a23a
AS
37#define COLOR_DEBUG "\033[32;2m"
38#define COLOR_WARN "\033[33;2m"
2f7229d2 39#define COLOR_CRITICAL "\033[31;1m"
40#define COLOR_FATAL "\033[33;1m"
41#define COLOR_RESET "\033[0m"
42
43#define QAPP_NAME qApp ? qApp->objectName().toUtf8().constData() : ""
44
619f3aee 45#include <cstdio>
3048adf5 46#include <unistd.h>
70dd4459
PK
47#include <cstring>
48#include <csignal>
686212f0 49#include <cerrno>
70dd4459 50#include <sys/socket.h>
619f3aee 51#include <QDateTime>
70dd4459
PK
52#include <QDebug>
53#include <QSocketNotifier>
bc341bc6 54/*! \brief Log qDebug input to file
5af80cab 55Used only in pure Debug builds or when is the system environment
db4aaddf 56variable LXQT_DEBUG set
619f3aee 57*/
2d13fe11
HJYP
58void dbgMessageOutput(QtMsgType type, const QMessageLogContext &ctx, const QString & msgStr)
59{
60 QByteArray msgBuf = msgStr.toUtf8();
61 const char* msg = msgBuf.constData();
cd4d87e4 62 QDir dir(XdgDirs::configHome().toUtf8() + QLatin1String("/lxqt"));
00f0a23a
AS
63 dir.mkpath(".");
64
3048adf5
AM
65 const char* typestr;
66 const char* color;
619f3aee 67 switch (type) {
68 case QtDebugMsg:
3048adf5
AM
69 typestr = "Debug";
70 color = COLOR_DEBUG;
619f3aee 71 break;
72 case QtWarningMsg:
3048adf5
AM
73 typestr = "Warning";
74 color = COLOR_WARN;
619f3aee 75 break;
76 case QtFatalMsg:
3048adf5
AM
77 typestr = "Fatal";
78 color = COLOR_FATAL;
79 break;
80 default: // QtCriticalMsg
81 typestr = "Critical";
82 color = COLOR_CRITICAL;
619f3aee 83 }
3048adf5
AM
84
85 QByteArray dt = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toUtf8();
86 if (isatty(STDERR_FILENO))
87 fprintf(stderr, "%s %s(%p) %s: %s%s\n", color, QAPP_NAME, qApp, typestr, msg, COLOR_RESET);
88 else
89 fprintf(stderr, "%s(%p) %s: %s\n", QAPP_NAME, qApp, typestr, msg);
90
91 FILE *f = fopen(dir.absoluteFilePath("debug.log").toUtf8().constData(), "a+");
92 fprintf(f, "%s %s(%p) %s: %s\n", dt.constData(), QAPP_NAME, qApp, typestr, msg);
619f3aee 93 fclose(f);
3048adf5
AM
94
95 if (type == QtFatalMsg)
96 abort();
619f3aee 97}
8966cbec 98
077c1e76 99Application::Application(int &argc, char** argv)
8966cbec 100 : QApplication(argc, argv)
101{
619f3aee 102#ifdef DEBUG
2d13fe11 103 qInstallMessageHandler(dbgMessageOutput);
5af80cab 104#else
db4aaddf 105 if (!qgetenv("LXQT_DEBUG").isNull())
2d13fe11 106 qInstallMessageHandler(dbgMessageOutput);
619f3aee 107#endif
108
825771a5 109 setWindowIcon(QIcon(QString(LXQT_GRAPHICS_DIR) + "/lxqt_logo.png"));
db4aaddf 110 connect(Settings::globalSettings(), SIGNAL(lxqtThemeChanged()), this, SLOT(updateTheme()));
63a934d5
AS
111 updateTheme();
112}
8966cbec 113
70dd4459
PK
114Application::Application(int &argc, char** argv, bool handleQuitSignals)
115 : Application(argc, argv)
116{
117 if (handleQuitSignals)
118 {
119 QList<int> signo_list = {SIGINT, SIGTERM, SIGHUP};
120 connect(this, &Application::unixSignal, [this, signo_list] (int signo)
121 {
122 if (signo_list.contains(signo))
123 quit();
124 });
125 listenToUnixSignals(signo_list);
126 }
127}
8966cbec 128
077c1e76 129void Application::updateTheme()
63a934d5
AS
130{
131 QString styleSheetKey = QFileInfo(applicationFilePath()).fileName();
db4aaddf 132 setStyleSheet(lxqtTheme.qss(styleSheetKey));
63a934d5 133 emit themeChanged();
8966cbec 134}
70dd4459
PK
135
136namespace
137{
d2c9e68d 138 class SignalHandler
70dd4459 139 {
d2c9e68d 140 public:
141 static void signalHandler(int signo)
142 {
143 int ret = write(instance->mSignalSock[0], &signo, sizeof (int));
144 if (sizeof (int) != ret)
145 qCritical() << QStringLiteral("unable to write into socketpair, %1").arg(strerror(errno));
146 }
147
148 public:
149 template <class Lambda>
150 SignalHandler(Lambda signalEmitter)
151 : mSignalSock{-1, -1}
152 {
153 if (0 != socketpair(AF_UNIX, SOCK_STREAM, 0, mSignalSock))
154 {
155 qCritical() << QStringLiteral("unable to create socketpair for correct signal handling: %1)").arg(strerror(errno));
156 return;
157 }
70dd4459 158
d2c9e68d 159 mNotifier.reset(new QSocketNotifier(mSignalSock[1], QSocketNotifier::Read));
160 QObject::connect(mNotifier.data(), &QSocketNotifier::activated, [this, signalEmitter] {
161 int signo = 0;
162 int ret = read(mSignalSock[1], &signo, sizeof (int));
163 if (sizeof (int) != ret)
164 qCritical() << QStringLiteral("unable to read signal from socketpair, %1").arg(strerror(errno));
165 signalEmitter(signo);
166 });
167 }
70dd4459 168
d2c9e68d 169 ~SignalHandler()
70dd4459 170 {
d2c9e68d 171 close(mSignalSock[0]);
172 close(mSignalSock[1]);
70dd4459
PK
173 }
174
d2c9e68d 175 void listenToSignals(QList<int> const & signoList)
176 {
177 struct sigaction sa;
178 sa.sa_handler = signalHandler;
179 sigemptyset(&sa.sa_mask);
180 sa.sa_flags = 0;
181 for (auto const & signo : signoList)
182 sigaction(signo, &sa, nullptr);
183 }
184
185 public:
186 static QScopedPointer<SignalHandler> instance;
187
188 private:
189 int mSignalSock[2];
190 QScopedPointer<QSocketNotifier> mNotifier;
191 };
192
193 QScopedPointer<SignalHandler> SignalHandler::instance;
194}
195
196void Application::listenToUnixSignals(QList<int> const & signoList)
197{
198 static QScopedPointer<QSocketNotifier> signal_notifier;
70dd4459 199
d2c9e68d 200 if (SignalHandler::instance.isNull())
201 SignalHandler::instance.reset(new SignalHandler{[this] (int signo) { emit unixSignal(signo); }});
202 SignalHandler::instance->listenToSignals(signoList);
70dd4459 203}