1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <QApplication>
|
---|
20 | #include <QFile>
|
---|
21 | #include <QTime>
|
---|
22 |
|
---|
23 | #include "smplayer.h"
|
---|
24 | #include "global.h"
|
---|
25 | #include "helper.h"
|
---|
26 | #include "paths.h"
|
---|
27 |
|
---|
28 | #include <stdio.h>
|
---|
29 |
|
---|
30 | #define USE_LOCKS 1
|
---|
31 | #if USE_LOCKS
|
---|
32 | #ifdef USE_QXT
|
---|
33 | #define USE_QXT_LOCKS 1
|
---|
34 | #endif // USE_QXT
|
---|
35 | #endif // USE_LOCKS
|
---|
36 |
|
---|
37 | #if USE_LOCKS && USE_QXT_LOCKS
|
---|
38 | #include <QxtFileLock>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | using namespace Global;
|
---|
42 |
|
---|
43 | BaseGui * basegui_instance = 0;
|
---|
44 |
|
---|
45 | QFile output_log;
|
---|
46 |
|
---|
47 | void myMessageOutput( QtMsgType type, const char *msg ) {
|
---|
48 | static QStringList saved_lines;
|
---|
49 | static QString orig_line;
|
---|
50 | static QString line2;
|
---|
51 | static QRegExp rx_log;
|
---|
52 |
|
---|
53 | if (pref) {
|
---|
54 | if (!pref->log_smplayer) return;
|
---|
55 | rx_log.setPattern(pref->log_filter);
|
---|
56 | } else {
|
---|
57 | rx_log.setPattern(".*");
|
---|
58 | }
|
---|
59 |
|
---|
60 | line2.clear();
|
---|
61 |
|
---|
62 | orig_line = QString::fromUtf8(msg);
|
---|
63 |
|
---|
64 | switch ( type ) {
|
---|
65 | case QtDebugMsg:
|
---|
66 | if (rx_log.indexIn(orig_line) > -1) {
|
---|
67 | #ifndef NO_DEBUG_ON_CONSOLE
|
---|
68 | fprintf( stderr, "Debug: %s\n", orig_line.toLocal8Bit().data() );
|
---|
69 | #endif
|
---|
70 | line2 = orig_line;
|
---|
71 | }
|
---|
72 | break;
|
---|
73 | case QtWarningMsg:
|
---|
74 | #ifndef NO_DEBUG_ON_CONSOLE
|
---|
75 | fprintf( stderr, "Warning: %s\n", orig_line.toLocal8Bit().data() );
|
---|
76 | #endif
|
---|
77 | line2 = "WARNING: " + orig_line;
|
---|
78 | break;
|
---|
79 | case QtFatalMsg:
|
---|
80 | #ifndef NO_DEBUG_ON_CONSOLE
|
---|
81 | fprintf( stderr, "Fatal: %s\n", orig_line.toLocal8Bit().data() );
|
---|
82 | #endif
|
---|
83 | line2 = "FATAL: " + orig_line;
|
---|
84 | abort(); // deliberately core dump
|
---|
85 | case QtCriticalMsg:
|
---|
86 | #ifndef NO_DEBUG_ON_CONSOLE
|
---|
87 | fprintf( stderr, "Critical: %s\n", orig_line.toLocal8Bit().data() );
|
---|
88 | #endif
|
---|
89 | line2 = "CRITICAL: " + orig_line;
|
---|
90 | break;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (line2.isEmpty()) return;
|
---|
94 |
|
---|
95 | line2 = "["+ QTime::currentTime().toString("hh:mm:ss:zzz") +"] "+ line2;
|
---|
96 |
|
---|
97 | if (basegui_instance) {
|
---|
98 | if (!saved_lines.isEmpty()) {
|
---|
99 | // Send saved lines first
|
---|
100 | for (int n=0; n < saved_lines.count(); n++) {
|
---|
101 | basegui_instance->recordSmplayerLog(saved_lines[n]);
|
---|
102 | }
|
---|
103 | saved_lines.clear();
|
---|
104 | }
|
---|
105 | basegui_instance->recordSmplayerLog(line2);
|
---|
106 | } else {
|
---|
107 | // GUI is not created yet, save lines for later
|
---|
108 | saved_lines.append(line2);
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (pref) {
|
---|
112 | if (pref->save_smplayer_log) {
|
---|
113 | // Save log to file
|
---|
114 | if (!output_log.isOpen()) {
|
---|
115 | // FIXME: the config path may not be initialized if USE_LOCKS is not defined
|
---|
116 | output_log.setFileName( Paths::configPath() + "/smplayer_log.txt" );
|
---|
117 | output_log.open(QIODevice::WriteOnly);
|
---|
118 | }
|
---|
119 | if (output_log.isOpen()) {
|
---|
120 | QString l = line2 + "\r\n";
|
---|
121 | output_log.write(l.toUtf8().constData());
|
---|
122 | output_log.flush();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | #if USE_LOCKS
|
---|
129 | #if USE_QXT_LOCKS
|
---|
130 | bool create_lock(QFile * f, QxtFileLock * lock) {
|
---|
131 | bool success = false;
|
---|
132 | if (f->open(QIODevice::ReadWrite)) {
|
---|
133 | if (lock->lock()) {
|
---|
134 | f->write("smplayer lock file");
|
---|
135 | success = true;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | if (!success) f->close();
|
---|
139 | return success;
|
---|
140 | }
|
---|
141 |
|
---|
142 | void clean_lock(QFile * f, QxtFileLock * lock) {
|
---|
143 | qDebug("main: clean_lock: %s", f->fileName().toUtf8().data());
|
---|
144 | lock->unlock();
|
---|
145 | f->close();
|
---|
146 | f->remove();
|
---|
147 | }
|
---|
148 | #else
|
---|
149 | void remove_lock(QString lock_file) {
|
---|
150 | if (QFile::exists(lock_file)) {
|
---|
151 | qDebug("main: remove_lock: %s", lock_file.toUtf8().data());
|
---|
152 | QFile::remove(lock_file);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | #endif
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | class MyApplication : public QApplication
|
---|
159 | {
|
---|
160 | public:
|
---|
161 | MyApplication ( int & argc, char ** argv ) : QApplication(argc, argv) {};
|
---|
162 | virtual void commitData ( QSessionManager & /*manager*/ ) {
|
---|
163 | // Nothing to do, let the application to close
|
---|
164 | }
|
---|
165 | };
|
---|
166 |
|
---|
167 | int main( int argc, char ** argv )
|
---|
168 | {
|
---|
169 | MyApplication a( argc, argv );
|
---|
170 | a.setQuitOnLastWindowClosed(false);
|
---|
171 | //a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
|
---|
172 |
|
---|
173 | // Sets the config path
|
---|
174 | QString config_path;
|
---|
175 |
|
---|
176 | #ifdef PORTABLE_APP
|
---|
177 | config_path = a.applicationDirPath();
|
---|
178 | #else
|
---|
179 | // If a smplayer.ini exists in the app path, will use that path
|
---|
180 | // for the config file by default
|
---|
181 | if (QFile::exists( a.applicationDirPath() + "/smplayer.ini" ) ) {
|
---|
182 | config_path = a.applicationDirPath();
|
---|
183 | qDebug("main: using existing %s", QString(config_path + "/smplayer.ini").toUtf8().data());
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | QStringList args = a.arguments();
|
---|
188 | int pos = args.indexOf("-config-path");
|
---|
189 | if ( pos != -1) {
|
---|
190 | if (pos+1 < args.count()) {
|
---|
191 | pos++;
|
---|
192 | config_path = args[pos];
|
---|
193 | // Delete from list
|
---|
194 | args.removeAt(pos);
|
---|
195 | args.removeAt(pos-1);
|
---|
196 | } else {
|
---|
197 | printf("Error: expected parameter for -config-path\r\n");
|
---|
198 | return SMPlayer::ErrorArgument;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | qInstallMsgHandler( myMessageOutput );
|
---|
203 |
|
---|
204 | #if USE_LOCKS
|
---|
205 | //setIniPath will be set later in global_init, but we need it here
|
---|
206 | if (!config_path.isEmpty()) Paths::setConfigPath(config_path);
|
---|
207 |
|
---|
208 | QString lock_file = Paths::iniPath() + "/smplayer_init.lock";
|
---|
209 | qDebug("main: lock_file: %s", lock_file.toUtf8().data());
|
---|
210 |
|
---|
211 | #if USE_QXT_LOCKS
|
---|
212 | QFile f(lock_file);
|
---|
213 | QxtFileLock write_lock(&f, 0x10, 30, QxtFileLock::WriteLock);
|
---|
214 |
|
---|
215 | bool lock_ok = create_lock(&f, &write_lock);
|
---|
216 |
|
---|
217 | if (!lock_ok) {
|
---|
218 | //lock failed
|
---|
219 | qDebug("main: lock failed");
|
---|
220 |
|
---|
221 | // Wait 10 secs max.
|
---|
222 | int n = 100;
|
---|
223 | while ( n > 0) {
|
---|
224 | Helper::msleep(100); // wait 100 ms
|
---|
225 | //if (!QFile::exists(lock_file)) break;
|
---|
226 | if (create_lock(&f, &write_lock)) break;
|
---|
227 | n--;
|
---|
228 | if ((n % 10) == 0) qDebug("main: waiting %d...", n);
|
---|
229 | }
|
---|
230 | // Continue startup
|
---|
231 | }
|
---|
232 | #else
|
---|
233 | if (QFile::exists(lock_file)) {
|
---|
234 | qDebug("main: %s exists, waiting...", lock_file.toUtf8().data());
|
---|
235 | // Wait 10 secs max.
|
---|
236 | int n = 100;
|
---|
237 | while ( n > 0) {
|
---|
238 | Helper::msleep(100); // wait 100 ms
|
---|
239 | if (!QFile::exists(lock_file)) break;
|
---|
240 | n--;
|
---|
241 | if ((n % 10) == 0) qDebug("main: waiting %d...", n);
|
---|
242 | }
|
---|
243 | remove_lock(lock_file);
|
---|
244 | } else {
|
---|
245 | // Create lock file
|
---|
246 | QFile f(lock_file);
|
---|
247 | if (f.open(QIODevice::WriteOnly)) {
|
---|
248 | f.write("smplayer lock file");
|
---|
249 | f.close();
|
---|
250 | } else {
|
---|
251 | qWarning("main: can't open %s for writing", lock_file.toUtf8().data());
|
---|
252 | }
|
---|
253 |
|
---|
254 | }
|
---|
255 | #endif // USE_QXT_LOCKS
|
---|
256 | #endif // USE_LOCKS
|
---|
257 |
|
---|
258 | SMPlayer * smplayer = new SMPlayer(config_path);
|
---|
259 | SMPlayer::ExitCode c = smplayer->processArgs( args );
|
---|
260 | if (c != SMPlayer::NoExit) {
|
---|
261 | #if USE_LOCKS
|
---|
262 | #if USE_QXT_LOCKS
|
---|
263 | clean_lock(&f, &write_lock);
|
---|
264 | #else
|
---|
265 | remove_lock(lock_file);
|
---|
266 | #endif
|
---|
267 | #endif
|
---|
268 | return c;
|
---|
269 | }
|
---|
270 |
|
---|
271 | basegui_instance = smplayer->gui();
|
---|
272 | a.connect(smplayer->gui(), SIGNAL(quitSolicited()), &a, SLOT(quit()));
|
---|
273 | smplayer->start();
|
---|
274 |
|
---|
275 | #if USE_LOCKS
|
---|
276 | #if USE_QXT_LOCKS
|
---|
277 | clean_lock(&f, &write_lock);
|
---|
278 | #else
|
---|
279 | remove_lock(lock_file);
|
---|
280 | #endif
|
---|
281 | #endif
|
---|
282 |
|
---|
283 | int r = a.exec();
|
---|
284 |
|
---|
285 | basegui_instance = 0;
|
---|
286 | delete smplayer;
|
---|
287 |
|
---|
288 | if (output_log.isOpen()) output_log.close();
|
---|
289 |
|
---|
290 | return r;
|
---|
291 | }
|
---|
292 |
|
---|