Suppress compiler warnings about unused function parameters
[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 Q_UNUSED(ctx)
61 QByteArray msgBuf = msgStr.toUtf8();
62 const char* msg = msgBuf.constData();
63 QDir dir(XdgDirs::configHome().toUtf8() + QLatin1String("/lxqt"));
64 dir.mkpath(".");
65
66 const char* typestr;
67 const char* color;
68 switch (type) {
69 case QtDebugMsg:
70 typestr = "Debug";
71 color = COLOR_DEBUG;
72 break;
73 case QtWarningMsg:
74 typestr = "Warning";
75 color = COLOR_WARN;
76 break;
77 case QtFatalMsg:
78 typestr = "Fatal";
79 color = COLOR_FATAL;
80 break;
81 default: // QtCriticalMsg
82 typestr = "Critical";
83 color = COLOR_CRITICAL;
84 }
85
86 QByteArray dt = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toUtf8();
87 if (isatty(STDERR_FILENO))
88 fprintf(stderr, "%s %s(%p) %s: %s%s\n", color, QAPP_NAME, qApp, typestr, msg, COLOR_RESET);
89 else
90 fprintf(stderr, "%s(%p) %s: %s\n", QAPP_NAME, qApp, typestr, msg);
91
92 FILE *f = fopen(dir.absoluteFilePath("debug.log").toUtf8().constData(), "a+");
93 fprintf(f, "%s %s(%p) %s: %s\n", dt.constData(), QAPP_NAME, qApp, typestr, msg);
94 fclose(f);
95
96 if (type == QtFatalMsg)
97 abort();
98 }
99
100 Application::Application(int &argc, char** argv)
101 : QApplication(argc, argv)
102 {
103 #ifdef DEBUG
104 qInstallMessageHandler(dbgMessageOutput);
105 #else
106 if (!qgetenv("LXQT_DEBUG").isNull())
107 qInstallMessageHandler(dbgMessageOutput);
108 #endif
109
110 setWindowIcon(QIcon(QString(LXQT_GRAPHICS_DIR) + "/lxqt_logo.png"));
111 connect(Settings::globalSettings(), SIGNAL(lxqtThemeChanged()), this, SLOT(updateTheme()));
112 updateTheme();
113 }
114
115 Application::Application(int &argc, char** argv, bool handleQuitSignals)
116 : Application(argc, argv)
117 {
118 if (handleQuitSignals)
119 {
120 QList<int> signo_list = {SIGINT, SIGTERM, SIGHUP};
121 connect(this, &Application::unixSignal, [this, signo_list] (int signo)
122 {
123 if (signo_list.contains(signo))
124 quit();
125 });
126 listenToUnixSignals(signo_list);
127 }
128 }
129
130 void Application::updateTheme()
131 {
132 const QString styleSheetKey = QFileInfo(applicationFilePath()).fileName();
133 setStyleSheet(lxqtTheme.qss(styleSheetKey));
134 emit themeChanged();
135 }
136
137 namespace
138 {
139 class SignalHandler
140 {
141 public:
142 static void signalHandler(int signo)
143 {
144 const int ret = write(instance->mSignalSock[0], &signo, sizeof (int));
145 if (sizeof (int) != ret)
146 qCritical() << QStringLiteral("unable to write into socketpair, %1").arg(strerror(errno));
147 }
148
149 public:
150 template <class Lambda>
151 SignalHandler(Lambda signalEmitter)
152 : mSignalSock{-1, -1}
153 {
154 if (0 != socketpair(AF_UNIX, SOCK_STREAM, 0, mSignalSock))
155 {
156 qCritical() << QStringLiteral("unable to create socketpair for correct signal handling: %1)").arg(strerror(errno));
157 return;
158 }
159
160 mNotifier.reset(new QSocketNotifier(mSignalSock[1], QSocketNotifier::Read));
161 QObject::connect(mNotifier.data(), &QSocketNotifier::activated, [this, signalEmitter] {
162 int signo = 0;
163 int ret = read(mSignalSock[1], &signo, sizeof (int));
164 if (sizeof (int) != ret)
165 qCritical() << QStringLiteral("unable to read signal from socketpair, %1").arg(strerror(errno));
166 signalEmitter(signo);
167 });
168 }
169
170 ~SignalHandler()
171 {
172 close(mSignalSock[0]);
173 close(mSignalSock[1]);
174 }
175
176 void listenToSignals(QList<int> const & signoList)
177 {
178 struct sigaction sa;
179 sa.sa_handler = signalHandler;
180 sigemptyset(&sa.sa_mask);
181 sa.sa_flags = 0;
182 for (auto const & signo : signoList)
183 sigaction(signo, &sa, nullptr);
184 }
185
186 public:
187 static QScopedPointer<SignalHandler> instance;
188
189 private:
190 int mSignalSock[2];
191 QScopedPointer<QSocketNotifier> mNotifier;
192 };
193
194 QScopedPointer<SignalHandler> SignalHandler::instance;
195 }
196
197 void Application::listenToUnixSignals(QList<int> const & signoList)
198 {
199 static QScopedPointer<QSocketNotifier> signal_notifier;
200
201 if (SignalHandler::instance.isNull())
202 SignalHandler::instance.reset(new SignalHandler{[this] (int signo) { emit unixSignal(signo); }});
203 SignalHandler::instance->listenToSignals(signoList);
204 }