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

Diff of /qsampler/trunk/src/qsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3493 by capela, Sat Mar 9 21:47:46 2019 UTC revision 3835 by capela, Mon Nov 9 10:18:55 2020 UTC
# Line 1  Line 1 
1  // qsampler.cpp  // qsampler.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2004-2019, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2004-2020, rncbc aka Rui Nuno Capela. All rights reserved.
5     Copyright (C) 2007,2008,2015 Christian Schoenebeck     Copyright (C) 2007,2008,2015,2019 Christian Schoenebeck
6    
7     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# Line 25  Line 25 
25  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
26  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
27    
28    #include "qsamplerPaletteForm.h"
29    
30    #include <QDir>
31    
32    #include <QStyleFactory>
33    
34  #include <QLibraryInfo>  #include <QLibraryInfo>
35  #include <QTranslator>  #include <QTranslator>
36  #include <QLocale>  #include <QLocale>
# Line 41  Line 47 
47  #define CONFIG_DATADIR  CONFIG_PREFIX "/share"  #define CONFIG_DATADIR  CONFIG_PREFIX "/share"
48  #endif  #endif
49    
50    #ifndef CONFIG_LIBDIR
51    #if defined(__x86_64__)
52    #define CONFIG_LIBDIR  CONFIG_PREFIX "/lib64"
53    #else
54    #define CONFIG_LIBDIR  CONFIG_PREFIX "/lib"
55    #endif
56    #endif
57    
58    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
59    #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt4/plugins"
60    #else
61    #define CONFIG_PLUGINSDIR CONFIG_LIBDIR "/qt5/plugins"
62    #endif
63    
64  #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32)  #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32)
65  #define RELATIVE_LOCALE_DIR "/share/locale"  #define RELATIVE_LOCALE_DIR "/share/locale"
66  #elif defined(__APPLE__)  #elif defined(__APPLE__)
# Line 52  Line 72 
72  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
73  //  //
74    
 #ifdef CONFIG_X11  
75  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
76    
 #include <unistd.h> /* for gethostname() */  
   
 #include <X11/Xatom.h>  
 #include <X11/Xlib.h>  
   
77  #define QSAMPLER_XUNIQUE "qsamplerApplication"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
78    
79  #if QT_VERSION >= 0x050100  #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
80    #ifdef CONFIG_X11
 #include <xcb/xcb.h>  
 #include <xcb/xproto.h>  
   
 #include <QAbstractNativeEventFilter>  
   
 class qsamplerXcbEventFilter : public QAbstractNativeEventFilter  
 {  
 public:  
   
         // Constructor.  
         qsamplerXcbEventFilter(qsamplerApplication *pApp)  
                 : QAbstractNativeEventFilter(), m_pApp(pApp) {}  
   
         // XCB event filter (virtual processor).  
         bool nativeEventFilter(const QByteArray& eventType, void *message, long *)  
         {  
                 if (eventType == "xcb_generic_event_t") {  
                         xcb_property_notify_event_t *pEv  
                                 = static_cast<xcb_property_notify_event_t *> (message);  
                         if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY  
                                 && pEv->state == XCB_PROPERTY_NEW_VALUE)  
                                 m_pApp->x11PropertyNotify(pEv->window);  
                 }  
                 return false;  
         }  
81    
82  private:  #include <unistd.h> /* for gethostname() */
83    
84          // Instance variable.  #include <X11/Xatom.h>
85          qsamplerApplication *m_pApp;  #include <X11/Xlib.h>
 };  
86    
87    #endif  // CONFIG_X11
88    #else
89    #include <QSharedMemory>
90    #include <QLocalServer>
91    #include <QLocalSocket>
92    #include <QHostInfo>
93  #endif  #endif
94    
95  #endif  // CONFIG_XUNIQUE  #endif  // CONFIG_XUNIQUE
 #endif  // CONFIG_X11  
96    
97    
98  // Constructor.  // Constructor.
99  qsamplerApplication::qsamplerApplication ( int& argc, char **argv )  qsamplerApplication::qsamplerApplication ( int& argc, char **argv )
100          : QApplication(argc, argv),          : QApplication(argc, argv),
101                  m_pQtTranslator(NULL), m_pMyTranslator(NULL), m_pWidget(NULL)                  m_pQtTranslator(nullptr), m_pMyTranslator(nullptr), m_pWidget(nullptr)
102  {  {
103    #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
104            QApplication::setApplicationName(QSAMPLER_TITLE);
105            QApplication::setApplicationDisplayName(QSAMPLER_TITLE);
106            //      QSAMPLER_TITLE " - " + QObject::tr(QSAMPLER_SUBTITLE));
107    #endif
108          // Load translation support.          // Load translation support.
109          QLocale loc;          QLocale loc;
110          if (loc.language() != QLocale::C) {          if (loc.language() != QLocale::C) {
111                  // Try own Qt translation...                  // Try own Qt translation...
112                  m_pQtTranslator = new QTranslator(this);                  m_pQtTranslator = new QTranslator(this);
113                  QString sLocName = "qt_" + loc.name();                  QString sLocName = "qt_" + loc.name();
114            #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
115                    QString sLocPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
116            #else
117                  QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);                  QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
118            #endif
119                  if (m_pQtTranslator->load(sLocName, sLocPath)) {                  if (m_pQtTranslator->load(sLocName, sLocPath)) {
120                          QApplication::installTranslator(m_pQtTranslator);                          QApplication::installTranslator(m_pQtTranslator);
121                  } else {                  } else {
# Line 124  qsamplerApplication::qsamplerApplication Line 126  qsamplerApplication::qsamplerApplication
126                          } else {                          } else {
127                  #endif                  #endif
128                          delete m_pQtTranslator;                          delete m_pQtTranslator;
129                          m_pQtTranslator = 0;                          m_pQtTranslator = nullptr;
130                  #ifdef CONFIG_DEBUG                  #ifdef CONFIG_DEBUG
131                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
132                                  loc.name().toUtf8().constData(),                                  loc.name().toUtf8().constData(),
# Line 150  qsamplerApplication::qsamplerApplication Line 152  qsamplerApplication::qsamplerApplication
152                                  QApplication::installTranslator(m_pMyTranslator);                                  QApplication::installTranslator(m_pMyTranslator);
153                          } else {                          } else {
154                                  delete m_pMyTranslator;                                  delete m_pMyTranslator;
155                                  m_pMyTranslator = 0;                                  m_pMyTranslator = nullptr;
156                          #ifdef CONFIG_DEBUG                          #ifdef CONFIG_DEBUG
157                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
158                                          loc.name().toUtf8().constData(),                                          loc.name().toUtf8().constData(),
# Line 160  qsamplerApplication::qsamplerApplication Line 162  qsamplerApplication::qsamplerApplication
162                          }                          }
163                  }                  }
164          }          }
 #ifdef CONFIG_X11  
165  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
166          m_pDisplay = NULL;  #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
167    #ifdef CONFIG_X11
168            m_pDisplay = nullptr;
169          m_aUnique = 0;          m_aUnique = 0;
170          m_wOwner = 0;          m_wOwner = 0;
171  #if QT_VERSION >= 0x050100  #endif  // CONFIG_X11
172          m_pXcbEventFilter = new qsamplerXcbEventFilter(this);  #else
173          installNativeEventFilter(m_pXcbEventFilter);          m_pMemory = nullptr;
174            m_pServer = nullptr;
175  #endif  #endif
176  #endif  // CONFIG_XUNIQUE  #endif  // CONFIG_XUNIQUE
 #endif  // CONFIG_X11  
177  }  }
178    
179    
180  // Destructor.  // Destructor.
181  qsamplerApplication::~qsamplerApplication (void)  qsamplerApplication::~qsamplerApplication (void)
182  {  {
 #ifdef CONFIG_X11  
183  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
184  #if QT_VERSION >= 0x050100  #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
185          removeNativeEventFilter(m_pXcbEventFilter);          if (m_pServer) {
186          delete m_pXcbEventFilter;                  m_pServer->close();
187                    delete m_pServer;
188                    m_pServer = nullptr;
189            }
190            if (m_pMemory) {
191                    delete m_pMemory;
192                    m_pMemory = nullptr;
193    }
194  #endif  #endif
195  #endif  // CONFIG_XUNIQUE  #endif  // CONFIG_XUNIQUE
 #endif  // CONFIG_X11  
196          if (m_pMyTranslator) delete m_pMyTranslator;          if (m_pMyTranslator) delete m_pMyTranslator;
197          if (m_pQtTranslator) delete m_pQtTranslator;          if (m_pQtTranslator) delete m_pQtTranslator;
198  }  }
# Line 194  qsamplerApplication::~qsamplerApplicatio Line 202  qsamplerApplication::~qsamplerApplicatio
202  void qsamplerApplication::setMainWidget ( QWidget *pWidget )  void qsamplerApplication::setMainWidget ( QWidget *pWidget )
203  {  {
204          m_pWidget = pWidget;          m_pWidget = pWidget;
 #ifdef CONFIG_X11  
205  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
206    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
207    #ifdef CONFIG_X11
208          m_wOwner = m_pWidget->winId();          m_wOwner = m_pWidget->winId();
209          if (m_pDisplay && m_wOwner) {          if (m_pDisplay && m_wOwner) {
210                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
211                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
212                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
213          }          }
 #endif  // CONFIG_XUNIQUE  
214  #endif  // CONFIG_X11  #endif  // CONFIG_X11
215    #endif
216    #endif  // CONFIG_XUNIQUE
217  }  }
218    
219    
# Line 211  void qsamplerApplication::setMainWidget Line 221  void qsamplerApplication::setMainWidget
221  // and raise its proper main widget...  // and raise its proper main widget...
222  bool qsamplerApplication::setup (void)  bool qsamplerApplication::setup (void)
223  {  {
 #ifdef CONFIG_X11  
224  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
225  #if QT_VERSION >= 0x050100  #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
226          if (!QX11Info::isPlatformX11())  #ifdef CONFIG_X11
                 return false;  
 #endif  
227          m_pDisplay = QX11Info::display();          m_pDisplay = QX11Info::display();
228          if (m_pDisplay) {          if (m_pDisplay) {
229                  QString sUnique = QSAMPLER_XUNIQUE;                  QString sUnique = QSAMPLER_XUNIQUE;
# Line 266  bool qsamplerApplication::setup (void) Line 273  bool qsamplerApplication::setup (void)
273                          return true;                          return true;
274                  }                  }
275          }          }
 #endif  // CONFIG_XUNIQUE  
276  #endif  // CONFIG_X11  #endif  // CONFIG_X11
277          return false;          return false;
278    #else
279            m_sUnique = QCoreApplication::applicationName();
280            m_sUnique += '@';
281            m_sUnique += QHostInfo::localHostName();
282    #ifdef Q_OS_UNIX
283            m_pMemory = new QSharedMemory(m_sUnique);
284            m_pMemory->attach();
285            delete m_pMemory;
286    #endif
287            m_pMemory = new QSharedMemory(m_sUnique);
288            bool bServer = false;
289            const qint64 pid = QCoreApplication::applicationPid();
290            struct Data { qint64 pid; };
291            if (m_pMemory->create(sizeof(Data))) {
292                    m_pMemory->lock();
293                    Data *pData = static_cast<Data *> (m_pMemory->data());
294                    if (pData) {
295                            pData->pid = pid;
296                            bServer = true;
297                    }
298                    m_pMemory->unlock();
299            }
300            else
301            if (m_pMemory->attach()) {
302                    m_pMemory->lock(); // maybe not necessary?
303                    Data *pData = static_cast<Data *> (m_pMemory->data());
304                    if (pData)
305                            bServer = (pData->pid == pid);
306                    m_pMemory->unlock();
307            }
308            if (bServer) {
309                    QLocalServer::removeServer(m_sUnique);
310                    m_pServer = new QLocalServer();
311                    m_pServer->setSocketOptions(QLocalServer::UserAccessOption);
312                    m_pServer->listen(m_sUnique);
313                    QObject::connect(m_pServer,
314                            SIGNAL(newConnection()),
315                            SLOT(newConnectionSlot()));
316            } else {
317                    QLocalSocket socket;
318                    socket.connectToServer(m_sUnique);
319                    if (socket.state() == QLocalSocket::ConnectingState)
320                            socket.waitForConnected(200);
321                    if (socket.state() == QLocalSocket::ConnectedState) {
322                            socket.write(QCoreApplication::arguments().join(' ').toUtf8());
323                            socket.flush();
324                            socket.waitForBytesWritten(200);
325                    }
326            }
327            return !bServer;
328    #endif
329    #else
330            return false;
331    #endif  // !CONFIG_XUNIQUE
332  }  }
333    
334    
 #ifdef CONFIG_X11  
335  #ifdef CONFIG_XUNIQUE  #ifdef CONFIG_XUNIQUE
336    #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
337    #ifdef CONFIG_X11
338    
339  void qsamplerApplication::x11PropertyNotify ( Window w )  void qsamplerApplication::x11PropertyNotify ( Window w )
340  {  {
341          if (m_pDisplay && m_pWidget && m_wOwner == w) {          if (m_pDisplay && m_pWidget && m_wOwner == w) {
# Line 310  void qsamplerApplication::x11PropertyNot Line 372  void qsamplerApplication::x11PropertyNot
372  }  }
373    
374    
 #if QT_VERSION < 0x050000  
375  bool qsamplerApplication::x11EventFilter ( XEvent *pEv )  bool qsamplerApplication::x11EventFilter ( XEvent *pEv )
376  {  {
377          if (pEv->type == PropertyNotify          if (pEv->type == PropertyNotify
# Line 318  bool qsamplerApplication::x11EventFilter Line 379  bool qsamplerApplication::x11EventFilter
379                  x11PropertyNotify(pEv->xproperty.window);                  x11PropertyNotify(pEv->xproperty.window);
380          return QApplication::x11EventFilter(pEv);          return QApplication::x11EventFilter(pEv);
381  }  }
 #endif  
382    
 #endif  // CONFIG_XUNIQUE  
383  #endif  // CONFIG_X11  #endif  // CONFIG_X11
384    #else
385    
386    // Local server conection slot.
387    void qsamplerApplication::newConnectionSlot (void)
388    {
389            QLocalSocket *pSocket = m_pServer->nextPendingConnection();
390            QObject::connect(pSocket,
391                    SIGNAL(readyRead()),
392                    SLOT(readyReadSlot()));
393    }
394    
395    // Local server data-ready slot.
396    void qsamplerApplication::readyReadSlot (void)
397    {
398            QLocalSocket *pSocket = qobject_cast<QLocalSocket *> (sender());
399            if (pSocket) {
400                    const qint64 nread = pSocket->bytesAvailable();
401                    if (nread > 0) {
402                            const QByteArray data = pSocket->read(nread);
403                            // Just make it always shows up fine...
404                            m_pWidget->hide();
405                            m_pWidget->show();
406                            m_pWidget->raise();
407                            m_pWidget->activateWindow();
408                    }
409            }
410    }
411    
412    #endif
413    #endif  // CONFIG_XUNIQUE
414    
415    
416  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
# Line 345  void stacktrace ( int signo ) Line 434  void stacktrace ( int signo )
434          char cmd[80];          char cmd[80];
435    
436          // Reinstall default handler; prevent race conditions...          // Reinstall default handler; prevent race conditions...
437          signal(signo, SIG_DFL);          ::signal(signo, SIG_DFL);
438    
439          static const char *shell  = "/bin/sh";          static const char *shell  = "/bin/sh";
440          static const char *format = "gdb -q --batch --pid=%d"          static const char *format = "gdb -q --batch --pid=%d"
# Line 361  void stacktrace ( int signo ) Line 450  void stacktrace ( int signo )
450    
451          // Fork child...          // Fork child...
452          if (pid == 0) {          if (pid == 0) {
453                  execl(shell, shell, "-c", cmd, NULL);                  execl(shell, shell, "-c", cmd, nullptr);
454                  _exit(1);                  _exit(1);
455                  return;                  return;
456          }          }
# Line 391  int main ( int argc, char **argv ) Line 480  int main ( int argc, char **argv )
480          Q_INIT_RESOURCE(qsampler);          Q_INIT_RESOURCE(qsampler);
481  #ifdef CONFIG_STACKTRACE  #ifdef CONFIG_STACKTRACE
482  #if defined(__GNUC__) && defined(Q_OS_LINUX)  #if defined(__GNUC__) && defined(Q_OS_LINUX)
483          signal(SIGILL,  stacktrace);          ::signal(SIGILL,  stacktrace);
484          signal(SIGFPE,  stacktrace);          ::signal(SIGFPE,  stacktrace);
485          signal(SIGSEGV, stacktrace);          ::signal(SIGSEGV, stacktrace);
486          signal(SIGABRT, stacktrace);          ::signal(SIGABRT, stacktrace);
487          signal(SIGBUS,  stacktrace);          ::signal(SIGBUS,  stacktrace);
488  #endif  #endif
489  #endif  #endif
490          qsamplerApplication app(argc, argv);  #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
491  #if QT_VERSION >= 0x050600  #if QT_VERSION <  QT_VERSION_CHECK(6, 0, 0)
492          app.setAttribute(Qt::AA_EnableHighDpiScaling);          QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
493  #endif  #endif
494    #endif
495            qsamplerApplication app(argc, argv);
496    
497          #if defined(__APPLE__)  //  Toshi Nagata 20080105          #if defined(__APPLE__)  //  Toshi Nagata 20080105
498          {          {
499                  //  Set the plugin path to @exetutable_path/../plugins                  //  Set the plugin path to @exetutable_path/../plugins
# Line 431  int main ( int argc, char **argv ) Line 523  int main ( int argc, char **argv )
523                  return 2;                  return 2;
524          }          }
525    
526          // Dark themes grayed/disabled color group fix...          // Special custom styles...
527            if (QDir(CONFIG_PLUGINSDIR).exists())
528                    app.addLibraryPath(CONFIG_PLUGINSDIR);
529            if (!options.sCustomStyleTheme.isEmpty())
530                    app.setStyle(QStyleFactory::create(options.sCustomStyleTheme));
531    
532            // Custom color theme (eg. "KXStudio")...
533          QPalette pal(app.palette());          QPalette pal(app.palette());
534          if (pal.base().color().value() < 0x7f) {          if (QSampler::PaletteForm::namedPalette(
535          #if QT_VERSION >= 0x050000                          &options.settings(), options.sCustomColorTheme, pal))
                 const QColor& color = pal.window().color();  
                 const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;  
                 for (int i = 0; i < iGroups; ++i) {  
                         const QPalette::ColorGroup group = QPalette::ColorGroup(i);  
                         pal.setBrush(group, QPalette::Light,    color.lighter(150));  
                         pal.setBrush(group, QPalette::Midlight, color.lighter(120));  
                         pal.setBrush(group, QPalette::Dark,     color.darker(150));  
                         pal.setBrush(group, QPalette::Mid,      color.darker(120));  
                         pal.setBrush(group, QPalette::Shadow,   color.darker(200));  
                 }  
         //      pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());  
         #endif  
                 pal.setColorGroup(QPalette::Disabled,  
                         pal.windowText().color().darker(),  
                         pal.button(),  
                         pal.light(),  
                         pal.dark(),  
                         pal.mid(),  
                         pal.text().color().darker(),  
                         pal.text().color().lighter(),  
                         pal.base(),  
                         pal.window());  
536                  app.setPalette(pal);                  app.setPalette(pal);
         }  
537    
538          // Set default base font...          // Set default base font...
539          if (options.iBaseFontSize > 0)          if (options.iBaseFontSize > 0)

Legend:
Removed from v.3493  
changed lines
  Added in v.3835

  ViewVC Help
Powered by ViewVC