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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2282 - (show annotations) (download)
Sun Nov 13 20:29:46 2011 UTC (12 years, 4 months ago) by capela
File size: 10759 byte(s)
* Fixed Makefile.in handling of installation directories to
  the configure script eg. --bindir, --datadir, --localedir.
1 // qsampler.cpp
2 //
3 /****************************************************************************
4 Copyright (C) 2004-2010, 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 #if defined(__APPLE__) // Toshi Nagata 20080105
32 #include <QDir>
33 #endif
34
35 #if QT_VERSION < 0x040300
36 #define lighter(x) light(x)
37 #define darker(x) dark(x)
38 #endif
39
40 #define CONFIG_QUOTE1(x) #x
41 #define CONFIG_QUOTED(x) CONFIG_QUOTE1(x)
42
43 #if defined(DATADIR)
44 #define CONFIG_DATADIR CONFIG_QUOTED(DATADIR)
45 #else
46 #define CONFIG_DATADIR CONFIG_PREFIX "/share"
47 #endif
48
49 #if defined(LOCALEDIR)
50 #define CONFIG_LOCALEDIR CONFIG_QUOTED(LOCALEDIR)
51 #else
52 #define CONFIG_LOCALEDIR CONFIG_DATADIR "/locale"
53 #endif
54
55 #if WIN32
56 #define RELATIVE_LOCALE_DIR "/share/locale"
57 #elif defined(__APPLE__)
58 #define RELATIVE_LOCALE_DIR "/../Resources"
59 #endif
60
61
62 //-------------------------------------------------------------------------
63 // Singleton application instance stuff (Qt/X11 only atm.)
64 //
65
66 #if defined(Q_WS_X11)
67
68 #include <QX11Info>
69
70 #include <X11/Xatom.h>
71 #include <X11/Xlib.h>
72
73 #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"
74
75 #endif
76
77 class qsamplerApplication : public QApplication
78 {
79 public:
80
81 // Constructor.
82 qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
83 m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
84 {
85 // Load translation support.
86 QLocale loc;
87 if (loc.language() != QLocale::C) {
88 // Try own Qt translation...
89 m_pQtTranslator = new QTranslator(this);
90 QString sLocName = "qt_" + loc.name();
91 QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
92 if (m_pQtTranslator->load(sLocName, sLocPath)) {
93 QApplication::installTranslator(m_pQtTranslator);
94 } else {
95 #ifdef RELATIVE_LOCALE_DIR
96 sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
97 if (m_pQtTranslator->load(sLocName, sLocPath)) {
98 QApplication::installTranslator(m_pQtTranslator);
99 } else {
100 #endif
101 delete m_pQtTranslator;
102 m_pQtTranslator = 0;
103 #ifdef CONFIG_DEBUG
104 qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
105 loc.name().toUtf8().constData(),
106 sLocPath.toUtf8().constData(),
107 sLocName.toUtf8().constData());
108 #endif
109 #ifdef RELATIVE_LOCALE_DIR
110 }
111 #endif
112 }
113 // Try own application translation...
114 m_pMyTranslator = new QTranslator(this);
115 sLocName = "qsampler_" + loc.name();
116 if (m_pMyTranslator->load(sLocName, sLocPath)) {
117 QApplication::installTranslator(m_pMyTranslator);
118 } else {
119 #ifdef RELATIVE_LOCALE_DIR
120 sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
121 #else
122 sLocPath = CONFIG_LOCALEDIR;
123 #endif
124 if (m_pMyTranslator->load(sLocName, sLocPath)) {
125 QApplication::installTranslator(m_pMyTranslator);
126 } else {
127 delete m_pMyTranslator;
128 m_pMyTranslator = 0;
129 #ifdef CONFIG_DEBUG
130 qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
131 loc.name().toUtf8().constData(),
132 sLocPath.toUtf8().constData(),
133 sLocName.toUtf8().constData());
134 #endif
135 }
136 }
137 }
138 #if defined(Q_WS_X11)
139 m_pDisplay = QX11Info::display();
140 m_aUnique = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
141 XGrabServer(m_pDisplay);
142 m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
143 XUngrabServer(m_pDisplay);
144 #endif
145 }
146
147 // Destructor.
148 ~qsamplerApplication()
149 {
150 if (m_pMyTranslator) delete m_pMyTranslator;
151 if (m_pQtTranslator) delete m_pQtTranslator;
152 }
153
154 // Main application widget accessors.
155 void setMainWidget(QWidget *pWidget)
156 {
157 m_pWidget = pWidget;
158 #if defined(Q_WS_X11)
159 XGrabServer(m_pDisplay);
160 m_wOwner = m_pWidget->winId();
161 XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
162 XUngrabServer(m_pDisplay);
163 #endif
164 }
165
166 QWidget *mainWidget() const { return m_pWidget; }
167
168 // Check if another instance is running,
169 // and raise its proper main widget...
170 bool setup()
171 {
172 #if defined(Q_WS_X11)
173 if (m_wOwner != None) {
174 // First, notify any freedesktop.org WM
175 // that we're about to show the main widget...
176 Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);
177 int iScreen = XScreenNumberOfScreen(pScreen);
178 XEvent ev;
179 memset(&ev, 0, sizeof(ev));
180 ev.xclient.type = ClientMessage;
181 ev.xclient.display = m_pDisplay;
182 ev.xclient.window = m_wOwner;
183 ev.xclient.message_type = XInternAtom(m_pDisplay, "_NET_ACTIVE_WINDOW", false);
184 ev.xclient.format = 32;
185 ev.xclient.data.l[0] = 0; // Source indication.
186 ev.xclient.data.l[1] = 0; // Timestamp.
187 ev.xclient.data.l[2] = 0; // Requestor's currently active window (none)
188 ev.xclient.data.l[3] = 0;
189 ev.xclient.data.l[4] = 0;
190 XSelectInput(m_pDisplay, m_wOwner, StructureNotifyMask);
191 XSendEvent(m_pDisplay, RootWindow(m_pDisplay, iScreen), false,
192 (SubstructureNotifyMask | SubstructureRedirectMask), &ev);
193 XSync(m_pDisplay, false);
194 XRaiseWindow(m_pDisplay, m_wOwner);
195 // And then, let it get caught on destination
196 // by QApplication::x11EventFilter...
197 QByteArray value = QSAMPLER_XUNIQUE;
198 XChangeProperty(
199 m_pDisplay,
200 m_wOwner,
201 m_aUnique,
202 m_aUnique, 8,
203 PropModeReplace,
204 (unsigned char *) value.data(),
205 value.length());
206 // Done.
207 return true;
208 }
209 #endif
210 return false;
211 }
212
213 #if defined(Q_WS_X11)
214 bool x11EventFilter(XEvent *pEv)
215 {
216 if (m_pWidget && m_wOwner != None
217 && pEv->type == PropertyNotify
218 && pEv->xproperty.window == m_wOwner
219 && pEv->xproperty.state == PropertyNewValue) {
220 // Always check whether our property-flag is still around...
221 Atom aType;
222 int iFormat = 0;
223 unsigned long iItems = 0;
224 unsigned long iAfter = 0;
225 unsigned char *pData = 0;
226 if (XGetWindowProperty(
227 m_pDisplay,
228 m_wOwner,
229 m_aUnique,
230 0, 1024,
231 false,
232 m_aUnique,
233 &aType,
234 &iFormat,
235 &iItems,
236 &iAfter,
237 &pData) == Success
238 && aType == m_aUnique && iItems > 0 && iAfter == 0) {
239 // Avoid repeating it-self...
240 XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
241 // Just make it always shows up fine...
242 m_pWidget->show();
243 m_pWidget->raise();
244 m_pWidget->activateWindow();
245 }
246 // Free any left-overs...
247 if (iItems > 0 && pData)
248 XFree(pData);
249 }
250 return QApplication::x11EventFilter(pEv);
251 }
252 #endif
253
254 private:
255
256 // Translation support.
257 QTranslator *m_pQtTranslator;
258 QTranslator *m_pMyTranslator;
259
260 // Instance variables.
261 QWidget *m_pWidget;
262
263 #if defined(Q_WS_X11)
264 Display *m_pDisplay;
265 Atom m_aUnique;
266 Window m_wOwner;
267 #endif
268 };
269
270
271 //-------------------------------------------------------------------------
272 // stacktrace - Signal crash handler.
273 //
274
275 #ifdef CONFIG_STACKTRACE
276 #if defined(__GNUC__) && defined(Q_OS_LINUX)
277
278 #include <stdio.h>
279 #include <errno.h>
280 #include <signal.h>
281 #include <sys/wait.h>
282
283 void stacktrace ( int signo )
284 {
285 pid_t pid;
286 int rc;
287 int status = 0;
288 char cmd[80];
289
290 // Reinstall default handler; prevent race conditions...
291 signal(signo, SIG_DFL);
292
293 static const char *shell = "/bin/sh";
294 static const char *format = "gdb -q --batch --pid=%d"
295 " --eval-command='thread apply all bt'";
296
297 snprintf(cmd, sizeof(cmd), format, (int) getpid());
298
299 pid = fork();
300
301 // Fork failure!
302 if (pid < 0)
303 return;
304
305 // Fork child...
306 if (pid == 0) {
307 execl(shell, shell, "-c", cmd, NULL);
308 _exit(1);
309 return;
310 }
311
312 // Parent here: wait for child to terminate...
313 do { rc = waitpid(pid, &status, 0); }
314 while ((rc < 0) && (errno == EINTR));
315
316 // Dispatch any logging, if any...
317 QApplication::processEvents(QEventLoop::AllEvents, 3000);
318
319 // Make sure everyone terminates...
320 kill(pid, SIGTERM);
321 _exit(1);
322 }
323
324 #endif
325 #endif
326
327
328 //-------------------------------------------------------------------------
329 // main - The main program trunk.
330 //
331
332 int main ( int argc, char **argv )
333 {
334 Q_INIT_RESOURCE(qsampler);
335 #ifdef CONFIG_STACKTRACE
336 #if defined(__GNUC__) && defined(Q_OS_LINUX)
337 signal(SIGILL, stacktrace);
338 signal(SIGFPE, stacktrace);
339 signal(SIGSEGV, stacktrace);
340 signal(SIGABRT, stacktrace);
341 signal(SIGBUS, stacktrace);
342 #endif
343 #endif
344 qsamplerApplication app(argc, argv);
345
346 #if defined(__APPLE__) // Toshi Nagata 20080105
347 {
348 // Set the plugin path to @exetutable_path/../plugins
349 QDir dir(QApplication::applicationDirPath());
350 dir.cdUp(); // "Contents" directory
351 QApplication::setLibraryPaths(QStringList(dir.absolutePath() + "/plugins"));
352
353 // Set the PATH environment variable to include @executable_path/../../..
354 dir.cdUp();
355 dir.cdUp();
356 QString path(getenv("PATH"));
357 path = dir.absolutePath() + ":" + path;
358 setenv("PATH", path.toUtf8().constData(), 1);
359 }
360 #endif
361
362 // Construct default settings; override with command line arguments.
363 QSampler::Options options;
364 if (!options.parse_args(app.arguments())) {
365 app.quit();
366 return 1;
367 }
368
369 // Have another instance running?
370 if (app.setup()) {
371 app.quit();
372 return 2;
373 }
374
375 // Dark themes grayed/disabled color group fix...
376 QPalette pal(app.palette());
377 if (pal.base().color().value() < 0x7f) {
378 pal.setColorGroup(QPalette::Disabled,
379 pal.windowText().color().darker(),
380 pal.button(),
381 pal.light(),
382 pal.dark(),
383 pal.mid(),
384 pal.text().color().darker(),
385 pal.text().color().lighter(),
386 pal.base(),
387 pal.window());
388 app.setPalette(pal);
389 }
390
391 // Set default base font...
392 int iBaseFontSize = app.font().pointSize();
393 if (options.iBaseFontSize > 0)
394 iBaseFontSize = options.iBaseFontSize;
395 app.setFont(QFont(app.font().family(), iBaseFontSize));
396
397 // Construct, setup and show the main form.
398 QSampler::MainForm w;
399 w.setup(&options);
400 w.show();
401
402 // Settle this one as application main widget...
403 app.setMainWidget(&w);
404
405 // Register the quit signal/slot.
406 // app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
407
408 return app.exec();
409 }
410
411
412 // end of qsampler.cpp

  ViewVC Help
Powered by ViewVC