--- qsampler/trunk/src/qsampler.cpp 2019/03/07 12:34:44 3492 +++ qsampler/trunk/src/qsampler.cpp 2019/03/09 21:47:46 3493 @@ -20,11 +20,11 @@ *****************************************************************************/ -#include "qsamplerAbout.h" +#include "qsampler.h" + #include "qsamplerOptions.h" #include "qsamplerMainForm.h" -#include #include #include #include @@ -52,21 +52,10 @@ // Singleton application instance stuff (Qt/X11 only atm.) // -#if QT_VERSION < 0x050000 -#if defined(Q_WS_X11) -#define CONFIG_X11 -#endif -#else -#if defined(QT_X11EXTRAS_LIB) -#define CONFIG_X11 -#endif -#endif - - #ifdef CONFIG_X11 #ifdef CONFIG_XUNIQUE -#include +#include /* for gethostname() */ #include #include @@ -80,8 +69,6 @@ #include -class qsamplerApplication; - class qsamplerXcbEventFilter : public QAbstractNativeEventFilter { public: @@ -91,7 +78,17 @@ : QAbstractNativeEventFilter(), m_pApp(pApp) {} // XCB event filter (virtual processor). - bool nativeEventFilter(const QByteArray& eventType, void *message, long *); + bool nativeEventFilter(const QByteArray& eventType, void *message, long *) + { + if (eventType == "xcb_generic_event_t") { + xcb_property_notify_event_t *pEv + = static_cast (message); + if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY + && pEv->state == XCB_PROPERTY_NEW_VALUE) + m_pApp->x11PropertyNotify(pEv->window); + } + return false; + } private: @@ -105,130 +102,134 @@ #endif // CONFIG_X11 -class qsamplerApplication : public QApplication +// Constructor. +qsamplerApplication::qsamplerApplication ( int& argc, char **argv ) + : QApplication(argc, argv), + m_pQtTranslator(NULL), m_pMyTranslator(NULL), m_pWidget(NULL) { -public: - - // Constructor. - qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv), - m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0) - { - // Load translation support. - QLocale loc; - if (loc.language() != QLocale::C) { - // Try own Qt translation... - m_pQtTranslator = new QTranslator(this); - QString sLocName = "qt_" + loc.name(); - QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath); + // Load translation support. + QLocale loc; + if (loc.language() != QLocale::C) { + // Try own Qt translation... + m_pQtTranslator = new QTranslator(this); + QString sLocName = "qt_" + loc.name(); + QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath); + if (m_pQtTranslator->load(sLocName, sLocPath)) { + QApplication::installTranslator(m_pQtTranslator); + } else { + #ifdef RELATIVE_LOCALE_DIR + sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; if (m_pQtTranslator->load(sLocName, sLocPath)) { QApplication::installTranslator(m_pQtTranslator); } else { - #ifdef RELATIVE_LOCALE_DIR - sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; - if (m_pQtTranslator->load(sLocName, sLocPath)) { - QApplication::installTranslator(m_pQtTranslator); - } else { - #endif - delete m_pQtTranslator; - m_pQtTranslator = 0; + #endif + delete m_pQtTranslator; + m_pQtTranslator = 0; + #ifdef CONFIG_DEBUG + qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", + loc.name().toUtf8().constData(), + sLocPath.toUtf8().constData(), + sLocName.toUtf8().constData()); + #endif + #ifdef RELATIVE_LOCALE_DIR + } + #endif + } + // Try own application translation... + m_pMyTranslator = new QTranslator(this); + sLocName = "qsampler_" + loc.name(); + if (m_pMyTranslator->load(sLocName, sLocPath)) { + QApplication::installTranslator(m_pMyTranslator); + } else { + #ifdef RELATIVE_LOCALE_DIR + sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; + #else + sLocPath = CONFIG_DATADIR "/qsampler/translations"; + #endif + if (m_pMyTranslator->load(sLocName, sLocPath)) { + QApplication::installTranslator(m_pMyTranslator); + } else { + delete m_pMyTranslator; + m_pMyTranslator = 0; #ifdef CONFIG_DEBUG qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", loc.name().toUtf8().constData(), sLocPath.toUtf8().constData(), sLocName.toUtf8().constData()); #endif - #ifdef RELATIVE_LOCALE_DIR - } - #endif - } - // Try own application translation... - m_pMyTranslator = new QTranslator(this); - sLocName = "qsampler_" + loc.name(); - if (m_pMyTranslator->load(sLocName, sLocPath)) { - QApplication::installTranslator(m_pMyTranslator); - } else { - #ifdef RELATIVE_LOCALE_DIR - sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR; - #else - sLocPath = CONFIG_DATADIR "/qsampler/translations"; - #endif - if (m_pMyTranslator->load(sLocName, sLocPath)) { - QApplication::installTranslator(m_pMyTranslator); - } else { - delete m_pMyTranslator; - m_pMyTranslator = 0; - #ifdef CONFIG_DEBUG - qWarning("Warning: no translation found for '%s' locale: %s/%s.qm", - loc.name().toUtf8().constData(), - sLocPath.toUtf8().constData(), - sLocName.toUtf8().constData()); - #endif - } } } - #ifdef CONFIG_X11 - #ifdef CONFIG_XUNIQUE - m_pDisplay = NULL; - m_aUnique = 0; - m_wOwner = 0; - #if QT_VERSION >= 0x050100 - m_pXcbEventFilter = new qsamplerXcbEventFilter(this); - installNativeEventFilter(m_pXcbEventFilter); - if (QX11Info::isPlatformX11()) { - #endif - // Instance uniqueness initialization... - m_pDisplay = QX11Info::display(); - m_aUnique = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false); - XGrabServer(m_pDisplay); - m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique); - XUngrabServer(m_pDisplay); - #if QT_VERSION >= 0x050100 - } - #endif - #endif // CONFIG_XUNIQUE - #endif // CONFIG_X11 } +#ifdef CONFIG_X11 +#ifdef CONFIG_XUNIQUE + m_pDisplay = NULL; + m_aUnique = 0; + m_wOwner = 0; +#if QT_VERSION >= 0x050100 + m_pXcbEventFilter = new qsamplerXcbEventFilter(this); + installNativeEventFilter(m_pXcbEventFilter); +#endif +#endif // CONFIG_XUNIQUE +#endif // CONFIG_X11 +} - // Destructor. - ~qsamplerApplication() - { - #ifdef CONFIG_X11 - #ifdef CONFIG_XUNIQUE - #if QT_VERSION >= 0x050100 - removeNativeEventFilter(m_pXcbEventFilter); - delete m_pXcbEventFilter; - #endif - #endif // CONFIG_XUNIQUE - #endif // CONFIG_X11 - if (m_pMyTranslator) delete m_pMyTranslator; - if (m_pQtTranslator) delete m_pQtTranslator; - } - // Main application widget accessors. - void setMainWidget(QWidget *pWidget) - { - m_pWidget = pWidget; - #ifdef CONFIG_X11 - #ifdef CONFIG_XUNIQUE - m_wOwner = m_pWidget->winId(); - if (m_pDisplay && m_wOwner) { - XGrabServer(m_pDisplay); - XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime); - XUngrabServer(m_pDisplay); - } - #endif // CONFIG_XUNIQUE - #endif // CONFIG_X11 +// Destructor. +qsamplerApplication::~qsamplerApplication (void) +{ +#ifdef CONFIG_X11 +#ifdef CONFIG_XUNIQUE +#if QT_VERSION >= 0x050100 + removeNativeEventFilter(m_pXcbEventFilter); + delete m_pXcbEventFilter; +#endif +#endif // CONFIG_XUNIQUE +#endif // CONFIG_X11 + if (m_pMyTranslator) delete m_pMyTranslator; + if (m_pQtTranslator) delete m_pQtTranslator; +} + + +// Main application widget accessors. +void qsamplerApplication::setMainWidget ( QWidget *pWidget ) +{ + m_pWidget = pWidget; +#ifdef CONFIG_X11 +#ifdef CONFIG_XUNIQUE + m_wOwner = m_pWidget->winId(); + if (m_pDisplay && m_wOwner) { + XGrabServer(m_pDisplay); + XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime); + XUngrabServer(m_pDisplay); } +#endif // CONFIG_XUNIQUE +#endif // CONFIG_X11 +} - QWidget *mainWidget() const { return m_pWidget; } - // Check if another instance is running, - // and raise its proper main widget... - bool setup() - { - #ifdef CONFIG_X11 - #ifdef CONFIG_XUNIQUE - if (m_pDisplay && m_wOwner != None) { +// Check if another instance is running, +// and raise its proper main widget... +bool qsamplerApplication::setup (void) +{ +#ifdef CONFIG_X11 +#ifdef CONFIG_XUNIQUE +#if QT_VERSION >= 0x050100 + if (!QX11Info::isPlatformX11()) + return false; +#endif + m_pDisplay = QX11Info::display(); + if (m_pDisplay) { + QString sUnique = QSAMPLER_XUNIQUE; + char szHostName[255]; + if (::gethostname(szHostName, sizeof(szHostName)) == 0) { + sUnique += '@'; + sUnique += szHostName; + } + m_aUnique = XInternAtom(m_pDisplay, sUnique.toUtf8().constData(), false); + XGrabServer(m_pDisplay); + m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique); + XUngrabServer(m_pDisplay); + if (m_wOwner != None) { // First, notify any freedesktop.org WM // that we're about to show the main widget... Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay); @@ -264,98 +265,61 @@ // Done. return true; } - #endif // CONFIG_XUNIQUE - #endif // CONFIG_X11 - return false; - } - -#ifdef CONFIG_X11 -#ifdef CONFIG_XUNIQUE - void x11PropertyNotify(Window w) - { - if (m_pDisplay && m_pWidget && m_wOwner == w) { - // Always check whether our property-flag is still around... - Atom aType; - int iFormat = 0; - unsigned long iItems = 0; - unsigned long iAfter = 0; - unsigned char *pData = 0; - if (XGetWindowProperty( - m_pDisplay, - m_wOwner, - m_aUnique, - 0, 1024, - false, - m_aUnique, - &aType, - &iFormat, - &iItems, - &iAfter, - &pData) == Success - && aType == m_aUnique && iItems > 0 && iAfter == 0) { - // Avoid repeating it-self... - XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique); - // Just make it always shows up fine... - m_pWidget->show(); - m_pWidget->raise(); - m_pWidget->activateWindow(); - } - // Free any left-overs... - if (iItems > 0 && pData) - XFree(pData); - } - } -#if QT_VERSION < 0x050000 - bool x11EventFilter(XEvent *pEv) - { - if (pEv->type == PropertyNotify - && pEv->xproperty.state == PropertyNewValue) - x11PropertyNotify(pEv->xproperty.window); - return QApplication::x11EventFilter(pEv); } -#endif #endif // CONFIG_XUNIQUE #endif // CONFIG_X11 - -private: - - // Translation support. - QTranslator *m_pQtTranslator; - QTranslator *m_pMyTranslator; + return false; +} - // Instance variables. - QWidget *m_pWidget; #ifdef CONFIG_X11 #ifdef CONFIG_XUNIQUE - Display *m_pDisplay; - Atom m_aUnique; - Window m_wOwner; -#if QT_VERSION >= 0x050100 - qsamplerXcbEventFilter *m_pXcbEventFilter; -#endif -#endif // CONFIG_XUNIQUE -#endif // CONFIG_X11 -}; +void qsamplerApplication::x11PropertyNotify ( Window w ) +{ + if (m_pDisplay && m_pWidget && m_wOwner == w) { + // Always check whether our property-flag is still around... + Atom aType; + int iFormat = 0; + unsigned long iItems = 0; + unsigned long iAfter = 0; + unsigned char *pData = 0; + if (XGetWindowProperty( + m_pDisplay, + m_wOwner, + m_aUnique, + 0, 1024, + false, + m_aUnique, + &aType, + &iFormat, + &iItems, + &iAfter, + &pData) == Success + && aType == m_aUnique && iItems > 0 && iAfter == 0) { + // Avoid repeating it-self... + XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique); + // Just make it always shows up fine... + m_pWidget->show(); + m_pWidget->raise(); + m_pWidget->activateWindow(); + } + // Free any left-overs... + if (iItems > 0 && pData) + XFree(pData); + } +} -#ifdef CONFIG_X11 -#ifdef CONFIG_XUNIQUE -#if QT_VERSION >= 0x050100 -// XCB Event filter (virtual processor). -bool qsamplerXcbEventFilter::nativeEventFilter ( - const QByteArray& eventType, void *message, long * ) +#if QT_VERSION < 0x050000 +bool qsamplerApplication::x11EventFilter ( XEvent *pEv ) { - if (eventType == "xcb_generic_event_t") { - xcb_property_notify_event_t *pEv - = static_cast (message); - if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY - && pEv->state == XCB_PROPERTY_NEW_VALUE) - m_pApp->x11PropertyNotify(pEv->window); - } - return false; + if (pEv->type == PropertyNotify + && pEv->xproperty.state == PropertyNewValue) + x11PropertyNotify(pEv->xproperty.window); + return QApplication::x11EventFilter(pEv); } #endif + #endif // CONFIG_XUNIQUE #endif // CONFIG_X11