3
/****************************************************************************
4
Copyright (C) 2004-2014, rncbc aka Rui Nuno Capela. All rights reserved.
5
Copyright (C) 2007, 2008 Christian Schoenebeck
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License
9
as published by the Free Software Foundation; either version 2
10
of the License, or (at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License along
18
with this program; if not, write to the Free Software Foundation, Inc.,
19
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
*****************************************************************************/
23
#include "qsamplerAbout.h"
24
#include "qsamplerOptions.h"
25
#include "qsamplerMainForm.h"
27
#include <QApplication>
28
#include <QLibraryInfo>
29
#include <QTranslator>
32
#if defined(__APPLE__) // Toshi Nagata 20080105
36
#define CONFIG_QUOTE1(x) #x
37
#define CONFIG_QUOTED(x) CONFIG_QUOTE1(x)
40
#define CONFIG_DATADIR CONFIG_QUOTED(DATADIR)
42
#define CONFIG_DATADIR CONFIG_PREFIX "/share"
46
#define RELATIVE_LOCALE_DIR "/share/locale"
47
#elif defined(__APPLE__)
48
#define RELATIVE_LOCALE_DIR "/../Resources"
52
//-------------------------------------------------------------------------
53
// Singleton application instance stuff (Qt/X11 only atm.)
60
#include <X11/Xatom.h>
63
#define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"
67
class qsamplerApplication : public QApplication
72
qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
73
m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
75
// Load translation support.
77
if (loc.language() != QLocale::C) {
78
// Try own Qt translation...
79
m_pQtTranslator = new QTranslator(this);
80
QString sLocName = "qt_" + loc.name();
81
QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
82
if (m_pQtTranslator->load(sLocName, sLocPath)) {
83
QApplication::installTranslator(m_pQtTranslator);
85
#ifdef RELATIVE_LOCALE_DIR
86
sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
87
if (m_pQtTranslator->load(sLocName, sLocPath)) {
88
QApplication::installTranslator(m_pQtTranslator);
91
delete m_pQtTranslator;
94
qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
95
loc.name().toUtf8().constData(),
96
sLocPath.toUtf8().constData(),
97
sLocName.toUtf8().constData());
99
#ifdef RELATIVE_LOCALE_DIR
103
// Try own application translation...
104
m_pMyTranslator = new QTranslator(this);
105
sLocName = "qsampler_" + loc.name();
106
if (m_pMyTranslator->load(sLocName, sLocPath)) {
107
QApplication::installTranslator(m_pMyTranslator);
109
#ifdef RELATIVE_LOCALE_DIR
110
sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
112
sLocPath = CONFIG_DATADIR "/qsampler/translations";
114
if (m_pMyTranslator->load(sLocName, sLocPath)) {
115
QApplication::installTranslator(m_pMyTranslator);
117
delete m_pMyTranslator;
120
qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
121
loc.name().toUtf8().constData(),
122
sLocPath.toUtf8().constData(),
123
sLocName.toUtf8().constData());
128
#if defined(Q_WS_X11)
129
m_pDisplay = QX11Info::display();
130
m_aUnique = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
131
XGrabServer(m_pDisplay);
132
m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
133
XUngrabServer(m_pDisplay);
138
~qsamplerApplication()
140
if (m_pMyTranslator) delete m_pMyTranslator;
141
if (m_pQtTranslator) delete m_pQtTranslator;
144
// Main application widget accessors.
145
void setMainWidget(QWidget *pWidget)
148
#if defined(Q_WS_X11)
149
XGrabServer(m_pDisplay);
150
m_wOwner = m_pWidget->winId();
151
XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
152
XUngrabServer(m_pDisplay);
156
QWidget *mainWidget() const { return m_pWidget; }
158
// Check if another instance is running,
159
// and raise its proper main widget...
162
#if defined(Q_WS_X11)
163
if (m_wOwner != None) {
164
// First, notify any freedesktop.org WM
165
// that we're about to show the main widget...
166
Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);
167
int iScreen = XScreenNumberOfScreen(pScreen);
169
memset(&ev, 0, sizeof(ev));
170
ev.xclient.type = ClientMessage;
171
ev.xclient.display = m_pDisplay;
172
ev.xclient.window = m_wOwner;
173
ev.xclient.message_type = XInternAtom(m_pDisplay, "_NET_ACTIVE_WINDOW", false);
174
ev.xclient.format = 32;
175
ev.xclient.data.l[0] = 0; // Source indication.
176
ev.xclient.data.l[1] = 0; // Timestamp.
177
ev.xclient.data.l[2] = 0; // Requestor's currently active window (none)
178
ev.xclient.data.l[3] = 0;
179
ev.xclient.data.l[4] = 0;
180
XSelectInput(m_pDisplay, m_wOwner, StructureNotifyMask);
181
XSendEvent(m_pDisplay, RootWindow(m_pDisplay, iScreen), false,
182
(SubstructureNotifyMask | SubstructureRedirectMask), &ev);
183
XSync(m_pDisplay, false);
184
XRaiseWindow(m_pDisplay, m_wOwner);
185
// And then, let it get caught on destination
186
// by QApplication::x11EventFilter...
187
QByteArray value = QSAMPLER_XUNIQUE;
194
(unsigned char *) value.data(),
203
#if defined(Q_WS_X11)
204
bool x11EventFilter(XEvent *pEv)
206
if (m_pWidget && m_wOwner != None
207
&& pEv->type == PropertyNotify
208
&& pEv->xproperty.window == m_wOwner
209
&& pEv->xproperty.state == PropertyNewValue) {
210
// Always check whether our property-flag is still around...
213
unsigned long iItems = 0;
214
unsigned long iAfter = 0;
215
unsigned char *pData = 0;
216
if (XGetWindowProperty(
228
&& aType == m_aUnique && iItems > 0 && iAfter == 0) {
229
// Avoid repeating it-self...
230
XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
231
// Just make it always shows up fine...
234
m_pWidget->activateWindow();
236
// Free any left-overs...
237
if (iItems > 0 && pData)
240
return QApplication::x11EventFilter(pEv);
246
// Translation support.
247
QTranslator *m_pQtTranslator;
248
QTranslator *m_pMyTranslator;
250
// Instance variables.
253
#if defined(Q_WS_X11)
261
//-------------------------------------------------------------------------
262
// stacktrace - Signal crash handler.
265
#ifdef CONFIG_STACKTRACE
266
#if defined(__GNUC__) && defined(Q_OS_LINUX)
272
#include <sys/wait.h>
274
void stacktrace ( int signo )
281
// Reinstall default handler; prevent race conditions...
282
signal(signo, SIG_DFL);
284
static const char *shell = "/bin/sh";
285
static const char *format = "gdb -q --batch --pid=%d"
286
" --eval-command='thread apply all bt'";
288
snprintf(cmd, sizeof(cmd), format, (int) getpid());
298
execl(shell, shell, "-c", cmd, NULL);
303
// Parent here: wait for child to terminate...
304
do { rc = waitpid(pid, &status, 0); }
305
while ((rc < 0) && (errno == EINTR));
307
// Dispatch any logging, if any...
308
QApplication::processEvents(QEventLoop::AllEvents, 3000);
310
// Make sure everyone terminates...
319
//-------------------------------------------------------------------------
320
// main - The main program trunk.
323
int main ( int argc, char **argv )
325
Q_INIT_RESOURCE(qsampler);
326
#ifdef CONFIG_STACKTRACE
327
#if defined(__GNUC__) && defined(Q_OS_LINUX)
328
signal(SIGILL, stacktrace);
329
signal(SIGFPE, stacktrace);
330
signal(SIGSEGV, stacktrace);
331
signal(SIGABRT, stacktrace);
332
signal(SIGBUS, stacktrace);
335
qsamplerApplication app(argc, argv);
337
#if defined(__APPLE__) // Toshi Nagata 20080105
339
// Set the plugin path to @exetutable_path/../plugins
340
QDir dir(QApplication::applicationDirPath());
341
dir.cdUp(); // "Contents" directory
342
QApplication::setLibraryPaths(QStringList(dir.absolutePath() + "/plugins"));
344
// Set the PATH environment variable to include @executable_path/../../..
347
QString path(getenv("PATH"));
348
path = dir.absolutePath() + ":" + path;
349
setenv("PATH", path.toUtf8().constData(), 1);
353
// Construct default settings; override with command line arguments.
354
QSampler::Options options;
355
if (!options.parse_args(app.arguments())) {
360
// Have another instance running?
366
// Dark themes grayed/disabled color group fix...
367
QPalette pal(app.palette());
368
if (pal.base().color().value() < 0x7f) {
369
#if QT_VERSION >= 0x050000
370
const QColor& color = pal.window().color();
371
const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;
372
for (int i = 0; i < iGroups; ++i) {
373
const QPalette::ColorGroup group = QPalette::ColorGroup(i);
374
pal.setBrush(group, QPalette::Light, color.lighter(150));
375
pal.setBrush(group, QPalette::Midlight, color.lighter(120));
376
pal.setBrush(group, QPalette::Dark, color.darker(150));
377
pal.setBrush(group, QPalette::Mid, color.darker(120));
378
pal.setBrush(group, QPalette::Shadow, color.darker(200));
380
// pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());
382
pal.setColorGroup(QPalette::Disabled,
383
pal.windowText().color().darker(),
388
pal.text().color().darker(),
389
pal.text().color().lighter(),
395
// Set default base font...
396
if (options.iBaseFontSize > 0)
397
app.setFont(QFont(app.font().family(), options.iBaseFontSize));
399
// Construct, setup and show the main form.
400
QSampler::MainForm w;
404
// Settle this one as application main widget...
405
app.setMainWidget(&w);
407
// Register the quit signal/slot.
408
// app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
414
// end of qsampler.cpp