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

Contents of /qsampler/trunk/src/qsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2998 - (show annotations) (download)
Mon Sep 26 09:37:11 2016 UTC (7 years, 6 months ago) by capela
File size: 13250 byte(s)
-
1 // qsampler.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2016, rncbc aka Rui Nuno Capela. All rights reserved.
5 Copyright (C) 2007, 2008 Christian Schoenebeck
6
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 #include "qsamplerAbout.h"
24 #include "qsamplerOptions.h"
25 #include "qsamplerMainForm.h"
26
27 #include <QApplication>
28 #include <QLibraryInfo>
29 #include <QTranslator>
30 #include <QLocale>
31
32 #if defined(__APPLE__) // Toshi Nagata 20080105
33 #include <QDir>
34 #endif
35
36 #ifndef CONFIG_PREFIX
37 #define CONFIG_PREFIX "/usr/local"
38 #endif
39
40 #ifndef CONFIG_DATADIR
41 #define CONFIG_DATADIR CONFIG_PREFIX "/share"
42 #endif
43
44 #if WIN32
45 #define RELATIVE_LOCALE_DIR "/share/locale"
46 #elif defined(__APPLE__)
47 #define RELATIVE_LOCALE_DIR "/../Resources"
48 #endif
49
50
51 //-------------------------------------------------------------------------
52 // Singleton application instance stuff (Qt/X11 only atm.)
53 //
54
55 #if QT_VERSION < 0x050000
56 #if defined(Q_WS_X11)
57 #define CONFIG_X11
58 #endif
59 #else
60 #if defined(QT_X11EXTRAS_LIB)
61 #define CONFIG_X11
62 #endif
63 #endif
64
65
66 #ifdef CONFIG_X11
67 #ifdef CONFIG_XUNIQUE
68
69 #include <QX11Info>
70
71 #include <X11/Xatom.h>
72 #include <X11/Xlib.h>
73
74 #define QSAMPLER_XUNIQUE "qsamplerApplication"
75
76 #if QT_VERSION >= 0x050100
77
78 #include <xcb/xcb.h>
79 #include <xcb/xproto.h>
80
81 #include <QAbstractNativeEventFilter>
82
83 class qsamplerApplication;
84
85 class qsamplerXcbEventFilter : public QAbstractNativeEventFilter
86 {
87 public:
88
89 // Constructor.
90 qsamplerXcbEventFilter(qsamplerApplication *pApp)
91 : QAbstractNativeEventFilter(), m_pApp(pApp) {}
92
93 // XCB event filter (virtual processor).
94 bool nativeEventFilter(const QByteArray& eventType, void *message, long *);
95
96 private:
97
98 // Instance variable.
99 qsamplerApplication *m_pApp;
100 };
101
102 #endif
103
104 #endif // CONFIG_XUNIQUE
105 #endif // CONFIG_X11
106
107
108 class qsamplerApplication : public QApplication
109 {
110 public:
111
112 // Constructor.
113 qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
114 m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
115 {
116 // Load translation support.
117 QLocale loc;
118 if (loc.language() != QLocale::C) {
119 // Try own Qt translation...
120 m_pQtTranslator = new QTranslator(this);
121 QString sLocName = "qt_" + loc.name();
122 QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
123 if (m_pQtTranslator->load(sLocName, sLocPath)) {
124 QApplication::installTranslator(m_pQtTranslator);
125 } else {
126 #ifdef RELATIVE_LOCALE_DIR
127 sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
128 if (m_pQtTranslator->load(sLocName, sLocPath)) {
129 QApplication::installTranslator(m_pQtTranslator);
130 } else {
131 #endif
132 delete m_pQtTranslator;
133 m_pQtTranslator = 0;
134 #ifdef CONFIG_DEBUG
135 qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
136 loc.name().toUtf8().constData(),
137 sLocPath.toUtf8().constData(),
138 sLocName.toUtf8().constData());
139 #endif
140 #ifdef RELATIVE_LOCALE_DIR
141 }
142 #endif
143 }
144 // Try own application translation...
145 m_pMyTranslator = new QTranslator(this);
146 sLocName = "qsampler_" + loc.name();
147 if (m_pMyTranslator->load(sLocName, sLocPath)) {
148 QApplication::installTranslator(m_pMyTranslator);
149 } else {
150 #ifdef RELATIVE_LOCALE_DIR
151 sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
152 #else
153 sLocPath = CONFIG_DATADIR "/qsampler/translations";
154 #endif
155 if (m_pMyTranslator->load(sLocName, sLocPath)) {
156 QApplication::installTranslator(m_pMyTranslator);
157 } else {
158 delete m_pMyTranslator;
159 m_pMyTranslator = 0;
160 #ifdef CONFIG_DEBUG
161 qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
162 loc.name().toUtf8().constData(),
163 sLocPath.toUtf8().constData(),
164 sLocName.toUtf8().constData());
165 #endif
166 }
167 }
168 }
169 #ifdef CONFIG_X11
170 #ifdef CONFIG_XUNIQUE
171 // Instance uniqueness initialization...
172 m_pDisplay = QX11Info::display();
173 m_aUnique = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
174 XGrabServer(m_pDisplay);
175 m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
176 XUngrabServer(m_pDisplay);
177 #if QT_VERSION >= 0x050100
178 m_pXcbEventFilter = new qsamplerXcbEventFilter(this);
179 installNativeEventFilter(m_pXcbEventFilter);
180 #endif
181 #endif // CONFIG_XUNIQUE
182 #endif // CONFIG_X11
183 }
184
185 // Destructor.
186 ~qsamplerApplication()
187 {
188 #ifdef CONFIG_X11
189 #ifdef CONFIG_XUNIQUE
190 #if QT_VERSION >= 0x050100
191 removeNativeEventFilter(m_pXcbEventFilter);
192 delete m_pXcbEventFilter;
193 #endif
194 #endif // CONFIG_XUNIQUE
195 #endif // CONFIG_X11
196 if (m_pMyTranslator) delete m_pMyTranslator;
197 if (m_pQtTranslator) delete m_pQtTranslator;
198 }
199
200 // Main application widget accessors.
201 void setMainWidget(QWidget *pWidget)
202 {
203 m_pWidget = pWidget;
204 #ifdef CONFIG_X11
205 #ifdef CONFIG_XUNIQUE
206 XGrabServer(m_pDisplay);
207 m_wOwner = m_pWidget->winId();
208 XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
209 XUngrabServer(m_pDisplay);
210 #endif // CONFIG_XUNIQUE
211 #endif // CONFIG_X11
212 }
213
214 QWidget *mainWidget() const { return m_pWidget; }
215
216 // Check if another instance is running,
217 // and raise its proper main widget...
218 bool setup()
219 {
220 #ifdef CONFIG_X11
221 #ifdef CONFIG_XUNIQUE
222 if (m_wOwner != None) {
223 // First, notify any freedesktop.org WM
224 // that we're about to show the main widget...
225 Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);
226 int iScreen = XScreenNumberOfScreen(pScreen);
227 XEvent ev;
228 memset(&ev, 0, sizeof(ev));
229 ev.xclient.type = ClientMessage;
230 ev.xclient.display = m_pDisplay;
231 ev.xclient.window = m_wOwner;
232 ev.xclient.message_type = XInternAtom(m_pDisplay, "_NET_ACTIVE_WINDOW", false);
233 ev.xclient.format = 32;
234 ev.xclient.data.l[0] = 0; // Source indication.
235 ev.xclient.data.l[1] = 0; // Timestamp.
236 ev.xclient.data.l[2] = 0; // Requestor's currently active window (none)
237 ev.xclient.data.l[3] = 0;
238 ev.xclient.data.l[4] = 0;
239 XSelectInput(m_pDisplay, m_wOwner, StructureNotifyMask);
240 XSendEvent(m_pDisplay, RootWindow(m_pDisplay, iScreen), false,
241 (SubstructureNotifyMask | SubstructureRedirectMask), &ev);
242 XSync(m_pDisplay, false);
243 XRaiseWindow(m_pDisplay, m_wOwner);
244 // And then, let it get caught on destination
245 // by QApplication::native/x11EventFilter...
246 const QByteArray value = QSAMPLER_XUNIQUE;
247 XChangeProperty(
248 m_pDisplay,
249 m_wOwner,
250 m_aUnique,
251 m_aUnique, 8,
252 PropModeReplace,
253 (unsigned char *) value.data(),
254 value.length());
255 // Done.
256 return true;
257 }
258 #endif // CONFIG_XUNIQUE
259 #endif // CONFIG_X11
260 return false;
261 }
262
263 #ifdef CONFIG_X11
264 #ifdef CONFIG_XUNIQUE
265 void x11PropertyNotify(Window w)
266 {
267 if (m_pWidget && m_wOwner == w) {
268 // Always check whether our property-flag is still around...
269 Atom aType;
270 int iFormat = 0;
271 unsigned long iItems = 0;
272 unsigned long iAfter = 0;
273 unsigned char *pData = 0;
274 if (XGetWindowProperty(
275 m_pDisplay,
276 m_wOwner,
277 m_aUnique,
278 0, 1024,
279 false,
280 m_aUnique,
281 &aType,
282 &iFormat,
283 &iItems,
284 &iAfter,
285 &pData) == Success
286 && aType == m_aUnique && iItems > 0 && iAfter == 0) {
287 // Avoid repeating it-self...
288 XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
289 // Just make it always shows up fine...
290 m_pWidget->show();
291 m_pWidget->raise();
292 m_pWidget->activateWindow();
293 }
294 // Free any left-overs...
295 if (iItems > 0 && pData)
296 XFree(pData);
297 }
298 }
299 #if QT_VERSION < 0x050000
300 bool x11EventFilter(XEvent *pEv)
301 {
302 if (pEv->type == PropertyNotify
303 && pEv->xproperty.state == PropertyNewValue)
304 x11PropertyNotify(pEv->xproperty.window);
305 return QApplication::x11EventFilter(pEv);
306 }
307 #endif
308 #endif // CONFIG_XUNIQUE
309 #endif // CONFIG_X11
310
311 private:
312
313 // Translation support.
314 QTranslator *m_pQtTranslator;
315 QTranslator *m_pMyTranslator;
316
317 // Instance variables.
318 QWidget *m_pWidget;
319
320 #ifdef CONFIG_X11
321 #ifdef CONFIG_XUNIQUE
322 Display *m_pDisplay;
323 Atom m_aUnique;
324 Window m_wOwner;
325 #if QT_VERSION >= 0x050100
326 qsamplerXcbEventFilter *m_pXcbEventFilter;
327 #endif
328 #endif // CONFIG_XUNIQUE
329 #endif // CONFIG_X11
330 };
331
332
333 #ifdef CONFIG_X11
334 #ifdef CONFIG_XUNIQUE
335 #if QT_VERSION >= 0x050100
336 // XCB Event filter (virtual processor).
337 bool qsamplerXcbEventFilter::nativeEventFilter (
338 const QByteArray& eventType, void *message, long * )
339 {
340 if (eventType == "xcb_generic_event_t") {
341 xcb_property_notify_event_t *pEv
342 = static_cast<xcb_property_notify_event_t *> (message);
343 if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY
344 && pEv->state == XCB_PROPERTY_NEW_VALUE)
345 m_pApp->x11PropertyNotify(pEv->window);
346 }
347 return false;
348 }
349 #endif
350 #endif // CONFIG_XUNIQUE
351 #endif // CONFIG_X11
352
353
354 //-------------------------------------------------------------------------
355 // stacktrace - Signal crash handler.
356 //
357
358 #ifdef CONFIG_STACKTRACE
359 #if defined(__GNUC__) && defined(Q_OS_LINUX)
360
361 #include <stdio.h>
362 #include <errno.h>
363 #include <signal.h>
364 #include <unistd.h>
365 #include <sys/wait.h>
366
367 void stacktrace ( int signo )
368 {
369 pid_t pid;
370 int rc;
371 int status = 0;
372 char cmd[80];
373
374 // Reinstall default handler; prevent race conditions...
375 signal(signo, SIG_DFL);
376
377 static const char *shell = "/bin/sh";
378 static const char *format = "gdb -q --batch --pid=%d"
379 " --eval-command='thread apply all bt'";
380
381 snprintf(cmd, sizeof(cmd), format, (int) getpid());
382
383 pid = fork();
384
385 // Fork failure!
386 if (pid < 0)
387 return;
388
389 // Fork child...
390 if (pid == 0) {
391 execl(shell, shell, "-c", cmd, NULL);
392 _exit(1);
393 return;
394 }
395
396 // Parent here: wait for child to terminate...
397 do { rc = waitpid(pid, &status, 0); }
398 while ((rc < 0) && (errno == EINTR));
399
400 // Dispatch any logging, if any...
401 QApplication::processEvents(QEventLoop::AllEvents, 3000);
402
403 // Make sure everyone terminates...
404 kill(pid, SIGTERM);
405 _exit(1);
406 }
407
408 #endif
409 #endif
410
411
412 //-------------------------------------------------------------------------
413 // main - The main program trunk.
414 //
415
416 int main ( int argc, char **argv )
417 {
418 Q_INIT_RESOURCE(qsampler);
419 #ifdef CONFIG_STACKTRACE
420 #if defined(__GNUC__) && defined(Q_OS_LINUX)
421 signal(SIGILL, stacktrace);
422 signal(SIGFPE, stacktrace);
423 signal(SIGSEGV, stacktrace);
424 signal(SIGABRT, stacktrace);
425 signal(SIGBUS, stacktrace);
426 #endif
427 #endif
428 qsamplerApplication app(argc, argv);
429
430 #if defined(__APPLE__) // Toshi Nagata 20080105
431 {
432 // Set the plugin path to @exetutable_path/../plugins
433 QDir dir(QApplication::applicationDirPath());
434 dir.cdUp(); // "Contents" directory
435 QApplication::setLibraryPaths(QStringList(dir.absolutePath() + "/plugins"));
436
437 // Set the PATH environment variable to include @executable_path/../../..
438 dir.cdUp();
439 dir.cdUp();
440 QString path(getenv("PATH"));
441 path = dir.absolutePath() + ":" + path;
442 setenv("PATH", path.toUtf8().constData(), 1);
443 }
444 #endif
445
446 // Construct default settings; override with command line arguments.
447 QSampler::Options options;
448 if (!options.parse_args(app.arguments())) {
449 app.quit();
450 return 1;
451 }
452
453 // Have another instance running?
454 if (app.setup()) {
455 app.quit();
456 return 2;
457 }
458
459 // Dark themes grayed/disabled color group fix...
460 QPalette pal(app.palette());
461 if (pal.base().color().value() < 0x7f) {
462 #if QT_VERSION >= 0x050000
463 const QColor& color = pal.window().color();
464 const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;
465 for (int i = 0; i < iGroups; ++i) {
466 const QPalette::ColorGroup group = QPalette::ColorGroup(i);
467 pal.setBrush(group, QPalette::Light, color.lighter(150));
468 pal.setBrush(group, QPalette::Midlight, color.lighter(120));
469 pal.setBrush(group, QPalette::Dark, color.darker(150));
470 pal.setBrush(group, QPalette::Mid, color.darker(120));
471 pal.setBrush(group, QPalette::Shadow, color.darker(200));
472 }
473 // pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());
474 #endif
475 pal.setColorGroup(QPalette::Disabled,
476 pal.windowText().color().darker(),
477 pal.button(),
478 pal.light(),
479 pal.dark(),
480 pal.mid(),
481 pal.text().color().darker(),
482 pal.text().color().lighter(),
483 pal.base(),
484 pal.window());
485 app.setPalette(pal);
486 }
487
488 // Set default base font...
489 if (options.iBaseFontSize > 0)
490 app.setFont(QFont(app.font().family(), options.iBaseFontSize));
491
492 // Construct, setup and show the main form.
493 QSampler::MainForm w;
494 w.setup(&options);
495 w.show();
496
497 // Settle this one as application main widget...
498 app.setMainWidget(&w);
499
500 // Register the quit signal/slot.
501 // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
502
503 return app.exec();
504 }
505
506
507 // end of qsampler.cpp

  ViewVC Help
Powered by ViewVC