cb9a4c627c71146142854c36bb42e075c9df6077
[lxde/liblxqt.git] / lxqtapplication.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
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
28 #include <QDir>
29
30 #include "lxqtapplication.h"
31 #include "lxqtsettings.h"
32
33 #include <XdgDirs>
34
35 using namespace LXQt;
36
37 #define COLOR_DEBUG "\033[32;2m"
38 #define COLOR_WARN "\033[33;2m"
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
45 #include <cstdio>
46 #include <unistd.h>
47 #include <cstring>
48 #include <csignal>
49 #include <cerrno>
50 #include <sys/socket.h>
51 #include <QDateTime>
52 #include <QDebug>
53 #include <QSocketNotifier>
54 /*! \brief Log qDebug input to file
55 Used only in pure Debug builds or when is the system environment
56 variable LXQT_DEBUG set
57 */
58 void dbgMessageOutput(QtMsgType type, const QMessageLogContext &ctx, const QString & msgStr)
59 {
60 QByteArray msgBuf = msgStr.toUtf8();
61 const char* msg = msgBuf.constData();
62 QDir dir(XdgDirs::configHome().toUtf8() + QLatin1String("/lxqt"));
63 dir.mkpath(".");
64
65 const char* typestr;
66 const char* color;
67 switch (type) {
68 case QtDebugMsg:
69 typestr = "Debug";
70 color = COLOR_DEBUG;
71 break;
72 case QtWarningMsg:
73 typestr = "Warning";
74 color = COLOR_WARN;
75 break;
76 case QtFatalMsg:
77 typestr = "Fatal";
78 color = COLOR_FATAL;
79 break;
80 default: // QtCriticalMsg
81 typestr = "Critical";
82 color = COLOR_CRITICAL;
83 }
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);
93 fclose(f);
94
95 if (type == QtFatalMsg)
96 abort();
97 }
98
99 Application::Application(int &argc, char** argv)
100 : QApplication(argc, argv)
101 {
102 #ifdef DEBUG
103 qInstallMessageHandler(dbgMessageOutput);
104 #else
105 if (!qgetenv("LXQT_DEBUG").isNull())
106 qInstallMessageHandler(dbgMessageOutput);
107 #endif
108
109 setWindowIcon(QIcon(QString(LXQT_GRAPHICS_DIR) + "/lxqt_logo.png"));
110 connect(Settings::globalSettings(), SIGNAL(lxqtThemeChanged()), this, SLOT(updateTheme()));
111 updateTheme();
112 }
113
114 Application::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 }
128
129 void Application::updateTheme()
130 {
131 const QString styleSheetKey = QFileInfo(applicationFilePath()).fileName();
132 setStyleSheet(lxqtTheme.qss(styleSheetKey));
133 emit themeChanged();
134 }
135
136 namespace
137 {
138 class SignalHandler
139 {
140 public:
141 static void signalHandler(int signo)
142 {
143 const 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 }
158
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 }
168
169 ~SignalHandler()
170 {
171 close(mSignalSock[0]);
172 close(mSignalSock[1]);
173 }
174
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
196 void Application::listenToUnixSignals(QList<int> const & signoList)
197 {
198 static QScopedPointer<QSocketNotifier> signal_notifier;
199
200 if (SignalHandler::instance.isNull())
201 SignalHandler::instance.reset(new SignalHandler{[this] (int signo) { emit unixSignal(signo); }});
202 SignalHandler::instance->listenToSignals(signoList);
203 }