52 |
// Singleton application instance stuff (Qt/X11 only atm.) |
// Singleton application instance stuff (Qt/X11 only atm.) |
53 |
// |
// |
54 |
|
|
|
#ifdef CONFIG_X11 |
|
55 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
56 |
|
|
|
#include <unistd.h> /* for gethostname() */ |
|
|
|
|
|
#include <X11/Xatom.h> |
|
|
#include <X11/Xlib.h> |
|
|
|
|
57 |
#define QSAMPLER_XUNIQUE "qsamplerApplication" |
#define QSAMPLER_XUNIQUE "qsamplerApplication" |
58 |
|
|
59 |
#if QT_VERSION >= 0x050100 |
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
60 |
|
#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; |
|
|
} |
|
61 |
|
|
62 |
private: |
#include <unistd.h> /* for gethostname() */ |
63 |
|
|
64 |
// Instance variable. |
#include <X11/Xatom.h> |
65 |
qsamplerApplication *m_pApp; |
#include <X11/Xlib.h> |
|
}; |
|
66 |
|
|
67 |
|
#endif // CONFIG_X11 |
68 |
|
#else |
69 |
|
#include <QSharedMemory> |
70 |
|
#include <QLocalServer> |
71 |
|
#include <QLocalSocket> |
72 |
|
#include <QHostInfo> |
73 |
#endif |
#endif |
74 |
|
|
75 |
#endif // CONFIG_XUNIQUE |
#endif // CONFIG_XUNIQUE |
|
#endif // CONFIG_X11 |
|
76 |
|
|
77 |
|
|
78 |
// Constructor. |
// Constructor. |
79 |
qsamplerApplication::qsamplerApplication ( int& argc, char **argv ) |
qsamplerApplication::qsamplerApplication ( int& argc, char **argv ) |
80 |
: QApplication(argc, argv), |
: QApplication(argc, argv), |
81 |
m_pQtTranslator(NULL), m_pMyTranslator(NULL), m_pWidget(NULL) |
m_pQtTranslator(nullptr), m_pMyTranslator(nullptr), m_pWidget(nullptr) |
82 |
{ |
{ |
83 |
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) |
84 |
|
QApplication::setApplicationName(QSAMPLER_TITLE); |
85 |
|
QApplication::setApplicationDisplayName(QSAMPLER_TITLE); |
86 |
|
// QSAMPLER_TITLE " - " + QObject::tr(QSAMPLER_SUBTITLE)); |
87 |
|
#endif |
88 |
// Load translation support. |
// Load translation support. |
89 |
QLocale loc; |
QLocale loc; |
90 |
if (loc.language() != QLocale::C) { |
if (loc.language() != QLocale::C) { |
138 |
} |
} |
139 |
} |
} |
140 |
} |
} |
|
#ifdef CONFIG_X11 |
|
141 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
142 |
m_pDisplay = NULL; |
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
143 |
|
#ifdef CONFIG_X11 |
144 |
|
m_pDisplay = nullptr; |
145 |
m_aUnique = 0; |
m_aUnique = 0; |
146 |
m_wOwner = 0; |
m_wOwner = 0; |
147 |
#if QT_VERSION >= 0x050100 |
#endif // CONFIG_X11 |
148 |
m_pXcbEventFilter = new qsamplerXcbEventFilter(this); |
#else |
149 |
installNativeEventFilter(m_pXcbEventFilter); |
m_pMemory = nullptr; |
150 |
|
m_pServer = nullptr; |
151 |
#endif |
#endif |
152 |
#endif // CONFIG_XUNIQUE |
#endif // CONFIG_XUNIQUE |
|
#endif // CONFIG_X11 |
|
153 |
} |
} |
154 |
|
|
155 |
|
|
156 |
// Destructor. |
// Destructor. |
157 |
qsamplerApplication::~qsamplerApplication (void) |
qsamplerApplication::~qsamplerApplication (void) |
158 |
{ |
{ |
|
#ifdef CONFIG_X11 |
|
159 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
160 |
#if QT_VERSION >= 0x050100 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) |
161 |
removeNativeEventFilter(m_pXcbEventFilter); |
if (m_pServer) { |
162 |
delete m_pXcbEventFilter; |
m_pServer->close(); |
163 |
|
delete m_pServer; |
164 |
|
m_pServer = nullptr; |
165 |
|
} |
166 |
|
if (m_pMemory) { |
167 |
|
delete m_pMemory; |
168 |
|
m_pMemory = nullptr; |
169 |
|
} |
170 |
#endif |
#endif |
171 |
#endif // CONFIG_XUNIQUE |
#endif // CONFIG_XUNIQUE |
|
#endif // CONFIG_X11 |
|
172 |
if (m_pMyTranslator) delete m_pMyTranslator; |
if (m_pMyTranslator) delete m_pMyTranslator; |
173 |
if (m_pQtTranslator) delete m_pQtTranslator; |
if (m_pQtTranslator) delete m_pQtTranslator; |
174 |
} |
} |
178 |
void qsamplerApplication::setMainWidget ( QWidget *pWidget ) |
void qsamplerApplication::setMainWidget ( QWidget *pWidget ) |
179 |
{ |
{ |
180 |
m_pWidget = pWidget; |
m_pWidget = pWidget; |
|
#ifdef CONFIG_X11 |
|
181 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
182 |
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
183 |
|
#ifdef CONFIG_X11 |
184 |
m_wOwner = m_pWidget->winId(); |
m_wOwner = m_pWidget->winId(); |
185 |
if (m_pDisplay && m_wOwner) { |
if (m_pDisplay && m_wOwner) { |
186 |
XGrabServer(m_pDisplay); |
XGrabServer(m_pDisplay); |
187 |
XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime); |
XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime); |
188 |
XUngrabServer(m_pDisplay); |
XUngrabServer(m_pDisplay); |
189 |
} |
} |
|
#endif // CONFIG_XUNIQUE |
|
190 |
#endif // CONFIG_X11 |
#endif // CONFIG_X11 |
191 |
|
#endif |
192 |
|
#endif // CONFIG_XUNIQUE |
193 |
} |
} |
194 |
|
|
195 |
|
|
197 |
// and raise its proper main widget... |
// and raise its proper main widget... |
198 |
bool qsamplerApplication::setup (void) |
bool qsamplerApplication::setup (void) |
199 |
{ |
{ |
|
#ifdef CONFIG_X11 |
|
200 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
201 |
#if QT_VERSION >= 0x050100 |
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
202 |
if (!QX11Info::isPlatformX11()) |
#ifdef CONFIG_X11 |
|
return false; |
|
|
#endif |
|
203 |
m_pDisplay = QX11Info::display(); |
m_pDisplay = QX11Info::display(); |
204 |
if (m_pDisplay) { |
if (m_pDisplay) { |
205 |
QString sUnique = QSAMPLER_XUNIQUE; |
QString sUnique = QSAMPLER_XUNIQUE; |
249 |
return true; |
return true; |
250 |
} |
} |
251 |
} |
} |
|
#endif // CONFIG_XUNIQUE |
|
252 |
#endif // CONFIG_X11 |
#endif // CONFIG_X11 |
253 |
return false; |
return false; |
254 |
|
#else |
255 |
|
m_sUnique = QCoreApplication::applicationName(); |
256 |
|
m_sUnique += '@'; |
257 |
|
m_sUnique += QHostInfo::localHostName(); |
258 |
|
#ifdef Q_OS_UNIX |
259 |
|
m_pMemory = new QSharedMemory(m_sUnique); |
260 |
|
m_pMemory->attach(); |
261 |
|
delete m_pMemory; |
262 |
|
#endif |
263 |
|
m_pMemory = new QSharedMemory(m_sUnique); |
264 |
|
bool bServer = false; |
265 |
|
const qint64 pid = QCoreApplication::applicationPid(); |
266 |
|
struct Data { qint64 pid; }; |
267 |
|
if (m_pMemory->create(sizeof(Data))) { |
268 |
|
m_pMemory->lock(); |
269 |
|
Data *pData = static_cast<Data *> (m_pMemory->data()); |
270 |
|
if (pData) { |
271 |
|
pData->pid = pid; |
272 |
|
bServer = true; |
273 |
|
} |
274 |
|
m_pMemory->unlock(); |
275 |
|
} |
276 |
|
else |
277 |
|
if (m_pMemory->attach()) { |
278 |
|
m_pMemory->lock(); // maybe not necessary? |
279 |
|
Data *pData = static_cast<Data *> (m_pMemory->data()); |
280 |
|
if (pData) |
281 |
|
bServer = (pData->pid == pid); |
282 |
|
m_pMemory->unlock(); |
283 |
|
} |
284 |
|
if (bServer) { |
285 |
|
QLocalServer::removeServer(m_sUnique); |
286 |
|
m_pServer = new QLocalServer(); |
287 |
|
m_pServer->setSocketOptions(QLocalServer::UserAccessOption); |
288 |
|
m_pServer->listen(m_sUnique); |
289 |
|
QObject::connect(m_pServer, |
290 |
|
SIGNAL(newConnection()), |
291 |
|
SLOT(newConnectionSlot())); |
292 |
|
} else { |
293 |
|
QLocalSocket socket; |
294 |
|
socket.connectToServer(m_sUnique); |
295 |
|
if (socket.state() == QLocalSocket::ConnectingState) |
296 |
|
socket.waitForConnected(200); |
297 |
|
if (socket.state() == QLocalSocket::ConnectedState) { |
298 |
|
socket.write(QCoreApplication::arguments().join(' ').toUtf8()); |
299 |
|
socket.flush(); |
300 |
|
socket.waitForBytesWritten(200); |
301 |
|
} |
302 |
|
} |
303 |
|
return !bServer; |
304 |
|
#endif |
305 |
|
#else |
306 |
|
return false; |
307 |
|
#endif // !CONFIG_XUNIQUE |
308 |
} |
} |
309 |
|
|
310 |
|
|
|
#ifdef CONFIG_X11 |
|
311 |
#ifdef CONFIG_XUNIQUE |
#ifdef CONFIG_XUNIQUE |
312 |
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) |
313 |
|
#ifdef CONFIG_X11 |
314 |
|
|
315 |
void qsamplerApplication::x11PropertyNotify ( Window w ) |
void qsamplerApplication::x11PropertyNotify ( Window w ) |
316 |
{ |
{ |
317 |
if (m_pDisplay && m_pWidget && m_wOwner == w) { |
if (m_pDisplay && m_pWidget && m_wOwner == w) { |
348 |
} |
} |
349 |
|
|
350 |
|
|
|
#if QT_VERSION < 0x050000 |
|
351 |
bool qsamplerApplication::x11EventFilter ( XEvent *pEv ) |
bool qsamplerApplication::x11EventFilter ( XEvent *pEv ) |
352 |
{ |
{ |
353 |
if (pEv->type == PropertyNotify |
if (pEv->type == PropertyNotify |
355 |
x11PropertyNotify(pEv->xproperty.window); |
x11PropertyNotify(pEv->xproperty.window); |
356 |
return QApplication::x11EventFilter(pEv); |
return QApplication::x11EventFilter(pEv); |
357 |
} |
} |
|
#endif |
|
358 |
|
|
|
#endif // CONFIG_XUNIQUE |
|
359 |
#endif // CONFIG_X11 |
#endif // CONFIG_X11 |
360 |
|
#else |
361 |
|
|
362 |
|
// Local server conection slot. |
363 |
|
void qsamplerApplication::newConnectionSlot (void) |
364 |
|
{ |
365 |
|
QLocalSocket *pSocket = m_pServer->nextPendingConnection(); |
366 |
|
QObject::connect(pSocket, |
367 |
|
SIGNAL(readyRead()), |
368 |
|
SLOT(readyReadSlot())); |
369 |
|
} |
370 |
|
|
371 |
|
// Local server data-ready slot. |
372 |
|
void qsamplerApplication::readyReadSlot (void) |
373 |
|
{ |
374 |
|
QLocalSocket *pSocket = qobject_cast<QLocalSocket *> (sender()); |
375 |
|
if (pSocket) { |
376 |
|
const qint64 nread = pSocket->bytesAvailable(); |
377 |
|
if (nread > 0) { |
378 |
|
const QByteArray data = pSocket->read(nread); |
379 |
|
// Just make it always shows up fine... |
380 |
|
m_pWidget->hide(); |
381 |
|
m_pWidget->show(); |
382 |
|
m_pWidget->raise(); |
383 |
|
m_pWidget->activateWindow(); |
384 |
|
} |
385 |
|
} |
386 |
|
} |
387 |
|
|
388 |
|
#endif |
389 |
|
#endif // CONFIG_XUNIQUE |
390 |
|
|
391 |
|
|
392 |
//------------------------------------------------------------------------- |
//------------------------------------------------------------------------- |
410 |
char cmd[80]; |
char cmd[80]; |
411 |
|
|
412 |
// Reinstall default handler; prevent race conditions... |
// Reinstall default handler; prevent race conditions... |
413 |
signal(signo, SIG_DFL); |
::signal(signo, SIG_DFL); |
414 |
|
|
415 |
static const char *shell = "/bin/sh"; |
static const char *shell = "/bin/sh"; |
416 |
static const char *format = "gdb -q --batch --pid=%d" |
static const char *format = "gdb -q --batch --pid=%d" |
426 |
|
|
427 |
// Fork child... |
// Fork child... |
428 |
if (pid == 0) { |
if (pid == 0) { |
429 |
execl(shell, shell, "-c", cmd, NULL); |
execl(shell, shell, "-c", cmd, nullptr); |
430 |
_exit(1); |
_exit(1); |
431 |
return; |
return; |
432 |
} |
} |
456 |
Q_INIT_RESOURCE(qsampler); |
Q_INIT_RESOURCE(qsampler); |
457 |
#ifdef CONFIG_STACKTRACE |
#ifdef CONFIG_STACKTRACE |
458 |
#if defined(__GNUC__) && defined(Q_OS_LINUX) |
#if defined(__GNUC__) && defined(Q_OS_LINUX) |
459 |
signal(SIGILL, stacktrace); |
::signal(SIGILL, stacktrace); |
460 |
signal(SIGFPE, stacktrace); |
::signal(SIGFPE, stacktrace); |
461 |
signal(SIGSEGV, stacktrace); |
::signal(SIGSEGV, stacktrace); |
462 |
signal(SIGABRT, stacktrace); |
::signal(SIGABRT, stacktrace); |
463 |
signal(SIGBUS, stacktrace); |
::signal(SIGBUS, stacktrace); |
464 |
#endif |
#endif |
465 |
#endif |
#endif |
466 |
qsamplerApplication app(argc, argv); |
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
467 |
#if QT_VERSION >= 0x050600 |
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); |
|
app.setAttribute(Qt::AA_EnableHighDpiScaling); |
|
468 |
#endif |
#endif |
469 |
|
qsamplerApplication app(argc, argv); |
470 |
|
|
471 |
#if defined(__APPLE__) // Toshi Nagata 20080105 |
#if defined(__APPLE__) // Toshi Nagata 20080105 |
472 |
{ |
{ |
473 |
// Set the plugin path to @exetutable_path/../plugins |
// Set the plugin path to @exetutable_path/../plugins |
500 |
// Dark themes grayed/disabled color group fix... |
// Dark themes grayed/disabled color group fix... |
501 |
QPalette pal(app.palette()); |
QPalette pal(app.palette()); |
502 |
if (pal.base().color().value() < 0x7f) { |
if (pal.base().color().value() < 0x7f) { |
503 |
#if QT_VERSION >= 0x050000 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) |
504 |
const QColor& color = pal.window().color(); |
const QColor& color = pal.window().color(); |
505 |
const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1; |
const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1; |
506 |
for (int i = 0; i < iGroups; ++i) { |
for (int i = 0; i < iGroups; ++i) { |