94 |
|
|
95 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
96 |
|
|
|
#define QSAMPLER_XUNIQUE "qsamplerApplication" |
|
|
|
|
97 |
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
98 |
#ifdef CONFIG_X11 |
#ifdef CONFIG_X11 |
99 |
|
|
100 |
|
#define QSAMPLER_XUNIQUE "qsamplerApplication" |
101 |
|
|
102 |
#include <unistd.h> /* for gethostname() */ |
#include <unistd.h> /* for gethostname() */ |
103 |
|
|
104 |
#include <X11/Xatom.h> |
#include <X11/Xatom.h> |
122 |
qsamplerApplication::qsamplerApplication ( int& argc, char **argv ) |
qsamplerApplication::qsamplerApplication ( int& argc, char **argv ) |
123 |
: QApplication(argc, argv), |
: QApplication(argc, argv), |
124 |
m_pQtTranslator(nullptr), m_pMyTranslator(nullptr), m_pWidget(nullptr) |
m_pQtTranslator(nullptr), m_pMyTranslator(nullptr), m_pWidget(nullptr) |
125 |
|
#ifdef CONFIG_XUNIQUE |
126 |
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
127 |
|
#ifdef CONFIG_X11 |
128 |
|
, m_pDisplay(nullptr) |
129 |
|
, m_aUnique(0) |
130 |
|
, m_wOwner(0) |
131 |
|
#endif // CONFIG_X11 |
132 |
|
#else |
133 |
|
, m_pMemory(nullptr) |
134 |
|
, m_pServer(nullptr) |
135 |
|
#endif |
136 |
|
#endif // CONFIG_XUNIQUE |
137 |
{ |
{ |
138 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) |
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) |
139 |
QApplication::setApplicationName(QSAMPLER_TITLE); |
QApplication::setApplicationName(QSAMPLER_TITLE); |
220 |
} |
} |
221 |
} |
} |
222 |
} |
} |
|
#ifdef CONFIG_XUNIQUE |
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
|
|
#ifdef CONFIG_X11 |
|
|
m_pDisplay = nullptr; |
|
|
m_aUnique = 0; |
|
|
m_wOwner = 0; |
|
|
#endif // CONFIG_X11 |
|
|
#else |
|
|
m_pMemory = nullptr; |
|
|
m_pServer = nullptr; |
|
|
#endif |
|
|
#endif // CONFIG_XUNIQUE |
|
223 |
} |
} |
224 |
|
|
225 |
|
|
228 |
{ |
{ |
229 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
230 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) |
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) |
231 |
if (m_pServer) { |
clearServer(); |
|
m_pServer->close(); |
|
|
delete m_pServer; |
|
|
m_pServer = nullptr; |
|
|
} |
|
|
if (m_pMemory) { |
|
|
delete m_pMemory; |
|
|
m_pMemory = nullptr; |
|
|
} |
|
232 |
#endif |
#endif |
233 |
#endif // CONFIG_XUNIQUE |
#endif // CONFIG_XUNIQUE |
234 |
if (m_pMyTranslator) delete m_pMyTranslator; |
if (m_pMyTranslator) delete m_pMyTranslator; |
321 |
#endif // CONFIG_X11 |
#endif // CONFIG_X11 |
322 |
return false; |
return false; |
323 |
#else |
#else |
324 |
|
return setupServer(); |
325 |
|
#endif |
326 |
|
#else |
327 |
|
return false; |
328 |
|
#endif // !CONFIG_XUNIQUE |
329 |
|
} |
330 |
|
|
331 |
|
|
332 |
|
#ifdef CONFIG_XUNIQUE |
333 |
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
334 |
|
#ifdef CONFIG_X11 |
335 |
|
|
336 |
|
void qsamplerApplication::x11PropertyNotify ( Window w ) |
337 |
|
{ |
338 |
|
if (m_pDisplay && m_pWidget && m_wOwner == w) { |
339 |
|
// Always check whether our property-flag is still around... |
340 |
|
Atom aType; |
341 |
|
int iFormat = 0; |
342 |
|
unsigned long iItems = 0; |
343 |
|
unsigned long iAfter = 0; |
344 |
|
unsigned char *pData = 0; |
345 |
|
if (XGetWindowProperty( |
346 |
|
m_pDisplay, |
347 |
|
m_wOwner, |
348 |
|
m_aUnique, |
349 |
|
0, 1024, |
350 |
|
false, |
351 |
|
m_aUnique, |
352 |
|
&aType, |
353 |
|
&iFormat, |
354 |
|
&iItems, |
355 |
|
&iAfter, |
356 |
|
&pData) == Success |
357 |
|
&& aType == m_aUnique && iItems > 0 && iAfter == 0) { |
358 |
|
// Avoid repeating it-self... |
359 |
|
XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique); |
360 |
|
// Just make it always shows up fine... |
361 |
|
m_pWidget->showNormal(); |
362 |
|
m_pWidget->raise(); |
363 |
|
m_pWidget->activateWindow(); |
364 |
|
// FIXME: Do our best speciality, although it should be |
365 |
|
// done iif configuration says so, we'll do it anyway! |
366 |
|
qsamplerMainForm *pMainForm = qsamplerMainForm::getInstance(); |
367 |
|
if (pMainForm) |
368 |
|
pMainForm->startAllEngines(); |
369 |
|
} |
370 |
|
// Free any left-overs... |
371 |
|
if (iItems > 0 && pData) |
372 |
|
XFree(pData); |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
bool qsamplerApplication::x11EventFilter ( XEvent *pEv ) |
377 |
|
{ |
378 |
|
if (pEv->type == PropertyNotify |
379 |
|
&& pEv->xproperty.state == PropertyNewValue) |
380 |
|
x11PropertyNotify(pEv->xproperty.window); |
381 |
|
return QApplication::x11EventFilter(pEv); |
382 |
|
} |
383 |
|
|
384 |
|
#endif // CONFIG_X11 |
385 |
|
#else |
386 |
|
|
387 |
|
// Local server/shmem setup. |
388 |
|
bool qsamplerApplication::setupServer (void) |
389 |
|
{ |
390 |
|
clearServer(); |
391 |
|
|
392 |
m_sUnique = QCoreApplication::applicationName(); |
m_sUnique = QCoreApplication::applicationName(); |
393 |
QString sUserName = QString::fromUtf8(::getenv("USER")); |
QString sUserName = QString::fromUtf8(::getenv("USER")); |
394 |
if (sUserName.isEmpty()) |
if (sUserName.isEmpty()) |
401 |
m_sUnique += QHostInfo::localHostName(); |
m_sUnique += QHostInfo::localHostName(); |
402 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) |
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0) |
403 |
const QNativeIpcKey nativeKey |
const QNativeIpcKey nativeKey |
404 |
= QSharedMemory::legacyNativeKey(m_sUnique); |
= QSharedMemory::legacyNativeKey(m_sUnique); |
405 |
|
#if defined(Q_OS_UNIX) |
406 |
|
m_pMemory = new QSharedMemory(nativeKey); |
407 |
|
m_pMemory->attach(); |
408 |
|
delete m_pMemory; |
409 |
|
#endif |
410 |
m_pMemory = new QSharedMemory(nativeKey); |
m_pMemory = new QSharedMemory(nativeKey); |
411 |
#else |
#else |
412 |
#if defined(Q_OS_UNIX) |
#if defined(Q_OS_UNIX) |
416 |
#endif |
#endif |
417 |
m_pMemory = new QSharedMemory(m_sUnique); |
m_pMemory = new QSharedMemory(m_sUnique); |
418 |
#endif |
#endif |
419 |
|
|
420 |
bool bServer = false; |
bool bServer = false; |
421 |
const qint64 pid = QCoreApplication::applicationPid(); |
const qint64 pid = QCoreApplication::applicationPid(); |
422 |
struct Data { qint64 pid; }; |
struct Data { qint64 pid; }; |
437 |
bServer = (pData->pid == pid); |
bServer = (pData->pid == pid); |
438 |
m_pMemory->unlock(); |
m_pMemory->unlock(); |
439 |
} |
} |
440 |
|
|
441 |
if (bServer) { |
if (bServer) { |
442 |
QLocalServer::removeServer(m_sUnique); |
QLocalServer::removeServer(m_sUnique); |
443 |
m_pServer = new QLocalServer(); |
m_pServer = new QLocalServer(); |
444 |
m_pServer->setSocketOptions(QLocalServer::UserAccessOption); |
m_pServer->setSocketOptions(QLocalServer::UserAccessOption); |
445 |
m_pServer->listen(m_sUnique); |
m_pServer->listen(m_sUnique); |
446 |
QObject::connect(m_pServer, |
QObject::connect(m_pServer, |
447 |
SIGNAL(newConnection()), |
SIGNAL(newConnection()), |
448 |
SLOT(newConnectionSlot())); |
SLOT(newConnectionSlot())); |
449 |
} else { |
} else { |
450 |
QLocalSocket socket; |
QLocalSocket socket; |
451 |
socket.connectToServer(m_sUnique); |
socket.connectToServer(m_sUnique); |
457 |
socket.waitForBytesWritten(200); |
socket.waitForBytesWritten(200); |
458 |
} |
} |
459 |
} |
} |
460 |
|
|
461 |
return !bServer; |
return !bServer; |
|
#endif |
|
|
#else |
|
|
return false; |
|
|
#endif // !CONFIG_XUNIQUE |
|
462 |
} |
} |
463 |
|
|
464 |
|
|
465 |
#ifdef CONFIG_XUNIQUE |
// Local server/shmem cleanup. |
466 |
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
void qsamplerApplication::clearServer (void) |
|
#ifdef CONFIG_X11 |
|
|
|
|
|
void qsamplerApplication::x11PropertyNotify ( Window w ) |
|
467 |
{ |
{ |
468 |
if (m_pDisplay && m_pWidget && m_wOwner == w) { |
if (m_pServer) { |
469 |
// Always check whether our property-flag is still around... |
m_pServer->close(); |
470 |
Atom aType; |
delete m_pServer; |
471 |
int iFormat = 0; |
m_pServer = nullptr; |
|
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->showNormal(); |
|
|
m_pWidget->raise(); |
|
|
m_pWidget->activateWindow(); |
|
|
} |
|
|
// Free any left-overs... |
|
|
if (iItems > 0 && pData) |
|
|
XFree(pData); |
|
472 |
} |
} |
|
} |
|
473 |
|
|
474 |
|
if (m_pMemory) { |
475 |
|
delete m_pMemory; |
476 |
|
m_pMemory = nullptr; |
477 |
|
} |
478 |
|
|
479 |
bool qsamplerApplication::x11EventFilter ( XEvent *pEv ) |
m_sUnique.clear(); |
|
{ |
|
|
if (pEv->type == PropertyNotify |
|
|
&& pEv->xproperty.state == PropertyNewValue) |
|
|
x11PropertyNotify(pEv->xproperty.window); |
|
|
return QApplication::x11EventFilter(pEv); |
|
480 |
} |
} |
481 |
|
|
|
#endif // CONFIG_X11 |
|
|
#else |
|
482 |
|
|
483 |
// Local server conection slot. |
// Local server conection slot. |
484 |
void qsamplerApplication::newConnectionSlot (void) |
void qsamplerApplication::newConnectionSlot (void) |
503 |
m_pWidget->raise(); |
m_pWidget->raise(); |
504 |
m_pWidget->activateWindow(); |
m_pWidget->activateWindow(); |
505 |
} |
} |
506 |
|
// Reset the server... |
507 |
|
setupServer(); |
508 |
} |
} |
509 |
} |
} |
510 |
} |
} |