/[svn]/qsampler/trunk/src/qsampler.cpp
ViewVC logotype

Annotation of /qsampler/trunk/src/qsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4030 - (hide annotations) (download)
Fri Mar 4 15:17:30 2022 UTC (2 years, 1 month ago) by capela
File size: 15893 byte(s)
- Set QApplication::desktopFileName()...
- Main application icon is now presented in scalable format (SVG).
1 capela 2074 // qsampler.cpp
2     //
3     /****************************************************************************
4 capela 4024 Copyright (C) 2004-2022, rncbc aka Rui Nuno Capela. All rights reserved.
5 capela 3681 Copyright (C) 2007,2008,2015,2019 Christian Schoenebeck
6 capela 2074
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.
11    
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.
16    
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.
20    
21     *****************************************************************************/
22    
23 capela 3493 #include "qsampler.h"
24    
25 capela 2074 #include "qsamplerOptions.h"
26     #include "qsamplerMainForm.h"
27    
28 capela 3648 #include "qsamplerPaletteForm.h"
29    
30     #include <QDir>
31    
32     #include <QStyleFactory>
33    
34 capela 2074 #include <QLibraryInfo>
35     #include <QTranslator>
36     #include <QLocale>
37 capela 2459
38 capela 4024 #if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
39     #include <lscp/client.h>
40     #ifdef CONFIG_LIBGIG
41     #if defined(Q_CC_GNU) || defined(Q_CC_MINGW)
42     #pragma GCC diagnostic push
43     #pragma GCC diagnostic ignored "-Wunused-parameter"
44     #endif
45     #include <gig.h>
46     #if defined(Q_CC_GNU) || defined(Q_CC_MINGW)
47     #pragma GCC diagnostic pop
48     #endif
49     #endif
50     #endif
51    
52 capela 2074 #if defined(__APPLE__) // Toshi Nagata 20080105
53     #include <QDir>
54     #endif
55    
56 capela 2998 #ifndef CONFIG_PREFIX
57     #define CONFIG_PREFIX "/usr/local"
58     #endif
59    
60 capela 4024 #ifndef CONFIG_BINDIR
61     #define CONFIG_BINDIR CONFIG_PREFIX "/bin"
62     #endif
63    
64 capela 2998 #ifndef CONFIG_DATADIR
65     #define CONFIG_DATADIR CONFIG_PREFIX "/share"
66     #endif
67    
68 capela 3648 #ifndef CONFIG_LIBDIR
69     #if defined(__x86_64__)
70     #define CONFIG_LIBDIR CONFIG_PREFIX "/lib64"
71     #else
72     #define CONFIG_LIBDIR CONFIG_PREFIX "/lib"
73     #endif
74     #endif
75    
76     #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
77     #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt4/plugins"
78 capela 3996 #elif QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
79     #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt5/plugins"
80 capela 3648 #else
81 capela 3996 #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt6/plugins"
82 capela 3648 #endif
83    
84 capela 3358 #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32)
85 persson 2210 #define RELATIVE_LOCALE_DIR "/share/locale"
86     #elif defined(__APPLE__)
87     #define RELATIVE_LOCALE_DIR "/../Resources"
88     #endif
89 capela 2074
90 persson 2210
91 capela 2074 //-------------------------------------------------------------------------
92     // Singleton application instance stuff (Qt/X11 only atm.)
93     //
94    
95 capela 2839 #ifdef CONFIG_XUNIQUE
96    
97 capela 3496 #define QSAMPLER_XUNIQUE "qsamplerApplication"
98    
99 capela 3520 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
100 capela 3496 #ifdef CONFIG_X11
101    
102 capela 3493 #include <unistd.h> /* for gethostname() */
103 capela 2074
104     #include <X11/Xatom.h>
105     #include <X11/Xlib.h>
106    
107 capela 3496 #endif // CONFIG_X11
108     #else
109     #include <QSharedMemory>
110     #include <QLocalServer>
111     #include <QLocalSocket>
112     #include <QHostInfo>
113 capela 2074 #endif
114    
115 capela 2839 #endif // CONFIG_XUNIQUE
116    
117    
118 capela 3493 // Constructor.
119     qsamplerApplication::qsamplerApplication ( int& argc, char **argv )
120     : QApplication(argc, argv),
121 capela 3555 m_pQtTranslator(nullptr), m_pMyTranslator(nullptr), m_pWidget(nullptr)
122 capela 2074 {
123 capela 3558 #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
124     QApplication::setApplicationName(QSAMPLER_TITLE);
125 capela 3560 QApplication::setApplicationDisplayName(QSAMPLER_TITLE);
126     // QSAMPLER_TITLE " - " + QObject::tr(QSAMPLER_SUBTITLE));
127 capela 4030 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
128     QApplication::setDesktopFileName(
129     QString("org.rncbc.%1").arg(PACKAGE_TARNAME));
130     #endif
131 capela 4024 QString sVersion(CONFIG_BUILD_VERSION);
132     sVersion += '\n';
133     sVersion += QString("Qt: %1").arg(qVersion());
134     #if defined(QT_STATIC)
135     sVersion += "-static";
136 capela 3558 #endif
137 capela 4024 sVersion += '\n';
138     #ifdef CONFIG_LIBGIG
139     sVersion += QString("%1: %2")
140     .arg(gig::libraryName().c_str())
141     .arg(gig::libraryVersion().c_str());
142     sVersion += '\n';
143     #endif
144     sVersion += QString("%1: %2")
145     .arg(::lscp_client_package())
146     .arg(::lscp_client_version());
147     QApplication::setApplicationVersion(sVersion);
148     #endif
149 capela 3493 // Load translation support.
150     QLocale loc;
151     if (loc.language() != QLocale::C) {
152     // Try own Qt translation...
153     m_pQtTranslator = new QTranslator(this);
154     QString sLocName = "qt_" + loc.name();
155 capela 3823 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
156     QString sLocPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
157     #else
158 capela 3493 QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
159 capela 3823 #endif
160 capela 3493 if (m_pQtTranslator->load(sLocName, sLocPath)) {
161     QApplication::installTranslator(m_pQtTranslator);
162     } else {
163 capela 4024 sLocPath = QApplication::applicationDirPath();
164 capela 3493 #ifdef RELATIVE_LOCALE_DIR
165 capela 4024 sLocPath.append(RELATIVE_LOCALE_DIR);
166     #else
167     sLocPath.remove(CONFIG_BINDIR);
168     sLocPath.append(CONFIG_DATADIR "/qsampler/translations");
169     #endif
170 capela 2074 if (m_pQtTranslator->load(sLocName, sLocPath)) {
171     QApplication::installTranslator(m_pQtTranslator);
172     } else {
173 capela 4024 delete m_pQtTranslator;
174     m_pQtTranslator = nullptr;
175     #ifdef CONFIG_DEBUG
176     qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
177     loc.name().toUtf8().constData(),
178     sLocPath.toUtf8().constData(),
179     sLocName.toUtf8().constData());
180     #endif
181 capela 3493 }
182     }
183     // Try own application translation...
184     m_pMyTranslator = new QTranslator(this);
185     sLocName = "qsampler_" + loc.name();
186     if (m_pMyTranslator->load(sLocName, sLocPath)) {
187     QApplication::installTranslator(m_pMyTranslator);
188     } else {
189     #ifdef RELATIVE_LOCALE_DIR
190     sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
191     #else
192     sLocPath = CONFIG_DATADIR "/qsampler/translations";
193     #endif
194     if (m_pMyTranslator->load(sLocName, sLocPath)) {
195     QApplication::installTranslator(m_pMyTranslator);
196     } else {
197     delete m_pMyTranslator;
198 capela 3835 m_pMyTranslator = nullptr;
199 capela 2631 #ifdef CONFIG_DEBUG
200 capela 2074 qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
201     loc.name().toUtf8().constData(),
202     sLocPath.toUtf8().constData(),
203     sLocName.toUtf8().constData());
204 capela 2631 #endif
205 capela 2074 }
206     }
207     }
208 capela 3496 #ifdef CONFIG_XUNIQUE
209 capela 3520 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
210 capela 3493 #ifdef CONFIG_X11
211 capela 3555 m_pDisplay = nullptr;
212 capela 3493 m_aUnique = 0;
213     m_wOwner = 0;
214 capela 3496 #endif // CONFIG_X11
215     #else
216 capela 3555 m_pMemory = nullptr;
217     m_pServer = nullptr;
218 capela 3493 #endif
219     #endif // CONFIG_XUNIQUE
220     }
221 capela 2074
222    
223 capela 3493 // Destructor.
224     qsamplerApplication::~qsamplerApplication (void)
225     {
226     #ifdef CONFIG_XUNIQUE
227 capela 3520 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
228 capela 3496 if (m_pServer) {
229     m_pServer->close();
230     delete m_pServer;
231 capela 3555 m_pServer = nullptr;
232 capela 3496 }
233     if (m_pMemory) {
234     delete m_pMemory;
235 capela 3555 m_pMemory = nullptr;
236 capela 3496 }
237 capela 3493 #endif
238     #endif // CONFIG_XUNIQUE
239     if (m_pMyTranslator) delete m_pMyTranslator;
240     if (m_pQtTranslator) delete m_pQtTranslator;
241     }
242    
243    
244     // Main application widget accessors.
245     void qsamplerApplication::setMainWidget ( QWidget *pWidget )
246     {
247     m_pWidget = pWidget;
248 capela 3496 #ifdef CONFIG_XUNIQUE
249 capela 3520 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
250 capela 3493 #ifdef CONFIG_X11
251     m_wOwner = m_pWidget->winId();
252     if (m_pDisplay && m_wOwner) {
253     XGrabServer(m_pDisplay);
254     XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
255     XUngrabServer(m_pDisplay);
256 capela 2074 }
257 capela 3496 #endif // CONFIG_X11
258     #endif
259 capela 3493 #endif // CONFIG_XUNIQUE
260     }
261 capela 2074
262    
263 capela 3493 // Check if another instance is running,
264     // and raise its proper main widget...
265     bool qsamplerApplication::setup (void)
266     {
267 capela 3496 #ifdef CONFIG_XUNIQUE
268 capela 3520 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
269 capela 3493 #ifdef CONFIG_X11
270     m_pDisplay = QX11Info::display();
271     if (m_pDisplay) {
272     QString sUnique = QSAMPLER_XUNIQUE;
273     char szHostName[255];
274     if (::gethostname(szHostName, sizeof(szHostName)) == 0) {
275     sUnique += '@';
276     sUnique += szHostName;
277     }
278     m_aUnique = XInternAtom(m_pDisplay, sUnique.toUtf8().constData(), false);
279     XGrabServer(m_pDisplay);
280     m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
281     XUngrabServer(m_pDisplay);
282     if (m_wOwner != None) {
283 capela 2074 // First, notify any freedesktop.org WM
284     // that we're about to show the main widget...
285     Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);
286     int iScreen = XScreenNumberOfScreen(pScreen);
287     XEvent ev;
288     memset(&ev, 0, sizeof(ev));
289     ev.xclient.type = ClientMessage;
290     ev.xclient.display = m_pDisplay;
291     ev.xclient.window = m_wOwner;
292     ev.xclient.message_type = XInternAtom(m_pDisplay, "_NET_ACTIVE_WINDOW", false);
293     ev.xclient.format = 32;
294     ev.xclient.data.l[0] = 0; // Source indication.
295     ev.xclient.data.l[1] = 0; // Timestamp.
296     ev.xclient.data.l[2] = 0; // Requestor's currently active window (none)
297     ev.xclient.data.l[3] = 0;
298     ev.xclient.data.l[4] = 0;
299     XSelectInput(m_pDisplay, m_wOwner, StructureNotifyMask);
300     XSendEvent(m_pDisplay, RootWindow(m_pDisplay, iScreen), false,
301     (SubstructureNotifyMask | SubstructureRedirectMask), &ev);
302     XSync(m_pDisplay, false);
303     XRaiseWindow(m_pDisplay, m_wOwner);
304     // And then, let it get caught on destination
305 capela 2839 // by QApplication::native/x11EventFilter...
306     const QByteArray value = QSAMPLER_XUNIQUE;
307 capela 2074 XChangeProperty(
308     m_pDisplay,
309     m_wOwner,
310     m_aUnique,
311     m_aUnique, 8,
312     PropModeReplace,
313     (unsigned char *) value.data(),
314     value.length());
315     // Done.
316     return true;
317     }
318     }
319 capela 2839 #endif // CONFIG_X11
320 capela 3493 return false;
321 capela 3496 #else
322     m_sUnique = QCoreApplication::applicationName();
323     m_sUnique += '@';
324     m_sUnique += QHostInfo::localHostName();
325     #ifdef Q_OS_UNIX
326     m_pMemory = new QSharedMemory(m_sUnique);
327     m_pMemory->attach();
328     delete m_pMemory;
329     #endif
330     m_pMemory = new QSharedMemory(m_sUnique);
331     bool bServer = false;
332     const qint64 pid = QCoreApplication::applicationPid();
333     struct Data { qint64 pid; };
334     if (m_pMemory->create(sizeof(Data))) {
335     m_pMemory->lock();
336     Data *pData = static_cast<Data *> (m_pMemory->data());
337     if (pData) {
338     pData->pid = pid;
339     bServer = true;
340     }
341     m_pMemory->unlock();
342     }
343     else
344     if (m_pMemory->attach()) {
345     m_pMemory->lock(); // maybe not necessary?
346     Data *pData = static_cast<Data *> (m_pMemory->data());
347     if (pData)
348     bServer = (pData->pid == pid);
349     m_pMemory->unlock();
350     }
351     if (bServer) {
352     QLocalServer::removeServer(m_sUnique);
353     m_pServer = new QLocalServer();
354     m_pServer->setSocketOptions(QLocalServer::UserAccessOption);
355     m_pServer->listen(m_sUnique);
356     QObject::connect(m_pServer,
357     SIGNAL(newConnection()),
358     SLOT(newConnectionSlot()));
359     } else {
360     QLocalSocket socket;
361     socket.connectToServer(m_sUnique);
362     if (socket.state() == QLocalSocket::ConnectingState)
363     socket.waitForConnected(200);
364     if (socket.state() == QLocalSocket::ConnectedState) {
365     socket.write(QCoreApplication::arguments().join(' ').toUtf8());
366     socket.flush();
367     socket.waitForBytesWritten(200);
368     }
369     }
370     return !bServer;
371     #endif
372     #else
373     return false;
374     #endif // !CONFIG_XUNIQUE
375 capela 3493 }
376 capela 2074
377    
378 capela 3496 #ifdef CONFIG_XUNIQUE
379 capela 3520 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
380 capela 2839 #ifdef CONFIG_X11
381 capela 3496
382 capela 3493 void qsamplerApplication::x11PropertyNotify ( Window w )
383     {
384     if (m_pDisplay && m_pWidget && m_wOwner == w) {
385     // Always check whether our property-flag is still around...
386     Atom aType;
387     int iFormat = 0;
388     unsigned long iItems = 0;
389     unsigned long iAfter = 0;
390     unsigned char *pData = 0;
391     if (XGetWindowProperty(
392     m_pDisplay,
393     m_wOwner,
394     m_aUnique,
395     0, 1024,
396     false,
397     m_aUnique,
398     &aType,
399     &iFormat,
400     &iItems,
401     &iAfter,
402     &pData) == Success
403     && aType == m_aUnique && iItems > 0 && iAfter == 0) {
404     // Avoid repeating it-self...
405     XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
406     // Just make it always shows up fine...
407     m_pWidget->show();
408     m_pWidget->raise();
409     m_pWidget->activateWindow();
410     }
411     // Free any left-overs...
412     if (iItems > 0 && pData)
413     XFree(pData);
414     }
415     }
416 capela 2074
417    
418 capela 3493 bool qsamplerApplication::x11EventFilter ( XEvent *pEv )
419 capela 2839 {
420 capela 3493 if (pEv->type == PropertyNotify
421     && pEv->xproperty.state == PropertyNewValue)
422     x11PropertyNotify(pEv->xproperty.window);
423     return QApplication::x11EventFilter(pEv);
424 capela 2839 }
425 capela 3493
426 capela 2839 #endif // CONFIG_X11
427 capela 3496 #else
428 capela 2839
429 capela 3496 // Local server conection slot.
430     void qsamplerApplication::newConnectionSlot (void)
431     {
432     QLocalSocket *pSocket = m_pServer->nextPendingConnection();
433     QObject::connect(pSocket,
434     SIGNAL(readyRead()),
435     SLOT(readyReadSlot()));
436     }
437 capela 2839
438 capela 3496 // Local server data-ready slot.
439     void qsamplerApplication::readyReadSlot (void)
440     {
441     QLocalSocket *pSocket = qobject_cast<QLocalSocket *> (sender());
442     if (pSocket) {
443     const qint64 nread = pSocket->bytesAvailable();
444     if (nread > 0) {
445 capela 3556 const QByteArray data = pSocket->read(nread);
446 capela 3496 // Just make it always shows up fine...
447     m_pWidget->hide();
448     m_pWidget->show();
449     m_pWidget->raise();
450     m_pWidget->activateWindow();
451     }
452     }
453     }
454    
455     #endif
456     #endif // CONFIG_XUNIQUE
457    
458    
459 capela 2074 //-------------------------------------------------------------------------
460     // stacktrace - Signal crash handler.
461     //
462    
463     #ifdef CONFIG_STACKTRACE
464     #if defined(__GNUC__) && defined(Q_OS_LINUX)
465    
466     #include <stdio.h>
467     #include <errno.h>
468     #include <signal.h>
469 capela 2341 #include <unistd.h>
470 capela 2074 #include <sys/wait.h>
471    
472     void stacktrace ( int signo )
473     {
474     pid_t pid;
475     int rc;
476     int status = 0;
477     char cmd[80];
478    
479     // Reinstall default handler; prevent race conditions...
480 capela 3508 ::signal(signo, SIG_DFL);
481 capela 2074
482     static const char *shell = "/bin/sh";
483 capela 2181 static const char *format = "gdb -q --batch --pid=%d"
484     " --eval-command='thread apply all bt'";
485 capela 2074
486     snprintf(cmd, sizeof(cmd), format, (int) getpid());
487    
488     pid = fork();
489    
490     // Fork failure!
491     if (pid < 0)
492     return;
493    
494     // Fork child...
495     if (pid == 0) {
496 capela 3555 execl(shell, shell, "-c", cmd, nullptr);
497 capela 2074 _exit(1);
498     return;
499     }
500    
501     // Parent here: wait for child to terminate...
502     do { rc = waitpid(pid, &status, 0); }
503     while ((rc < 0) && (errno == EINTR));
504    
505     // Dispatch any logging, if any...
506     QApplication::processEvents(QEventLoop::AllEvents, 3000);
507    
508     // Make sure everyone terminates...
509     kill(pid, SIGTERM);
510     _exit(1);
511     }
512    
513     #endif
514     #endif
515    
516    
517     //-------------------------------------------------------------------------
518     // main - The main program trunk.
519     //
520    
521     int main ( int argc, char **argv )
522     {
523     Q_INIT_RESOURCE(qsampler);
524     #ifdef CONFIG_STACKTRACE
525     #if defined(__GNUC__) && defined(Q_OS_LINUX)
526 capela 3508 ::signal(SIGILL, stacktrace);
527     ::signal(SIGFPE, stacktrace);
528     ::signal(SIGSEGV, stacktrace);
529     ::signal(SIGABRT, stacktrace);
530     ::signal(SIGBUS, stacktrace);
531 capela 2074 #endif
532     #endif
533 capela 4029 #if defined(Q_OS_LINUX) && !defined(CONFIG_WAYLAND)
534 capela 3910 ::setenv("QT_QPA_PLATFORM", "xcb", 0);
535 capela 3911 #endif
536 capela 3520 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
537 capela 3823 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
538 capela 3558 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
539 capela 3465 #endif
540 capela 3823 #endif
541 capela 3558 qsamplerApplication app(argc, argv);
542    
543 capela 2074 #if defined(__APPLE__) // Toshi Nagata 20080105
544     {
545     // Set the plugin path to @exetutable_path/../plugins
546     QDir dir(QApplication::applicationDirPath());
547     dir.cdUp(); // "Contents" directory
548     QApplication::setLibraryPaths(QStringList(dir.absolutePath() + "/plugins"));
549    
550     // Set the PATH environment variable to include @executable_path/../../..
551     dir.cdUp();
552     dir.cdUp();
553     QString path(getenv("PATH"));
554     path = dir.absolutePath() + ":" + path;
555     setenv("PATH", path.toUtf8().constData(), 1);
556     }
557     #endif
558    
559     // Construct default settings; override with command line arguments.
560     QSampler::Options options;
561     if (!options.parse_args(app.arguments())) {
562     app.quit();
563     return 1;
564     }
565    
566     // Have another instance running?
567     if (app.setup()) {
568     app.quit();
569     return 2;
570     }
571    
572 capela 3648 // Special custom styles...
573     if (QDir(CONFIG_PLUGINSDIR).exists())
574     app.addLibraryPath(CONFIG_PLUGINSDIR);
575     if (!options.sCustomStyleTheme.isEmpty())
576     app.setStyle(QStyleFactory::create(options.sCustomStyleTheme));
577    
578     // Custom color theme (eg. "KXStudio")...
579 capela 2074 QPalette pal(app.palette());
580 capela 3648 if (QSampler::PaletteForm::namedPalette(
581     &options.settings(), options.sCustomColorTheme, pal))
582 capela 2074 app.setPalette(pal);
583    
584     // Set default base font...
585     if (options.iBaseFontSize > 0)
586 capela 2677 app.setFont(QFont(app.font().family(), options.iBaseFontSize));
587 capela 2074
588     // Construct, setup and show the main form.
589     QSampler::MainForm w;
590     w.setup(&options);
591     w.show();
592    
593     // Settle this one as application main widget...
594     app.setMainWidget(&w);
595    
596     // Register the quit signal/slot.
597     // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
598    
599     return app.exec();
600     }
601    
602    
603     // end of qsampler.cpp
604 capela 3065

  ViewVC Help
Powered by ViewVC