forked from KikoPlayProject/KikoPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
146 lines (143 loc) · 4.83 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "UI/mainwindow.h"
#include <QApplication>
#include <QMessageBox>
#include <QLocalSocket>
#include <QLocalServer>
#include "globalobjects.h"
#include "Play/Playlist/playlist.h"
#include "Play/Video/mpvplayer.h"
#include "Play/Danmu/danmupool.h"
#include "Play/Danmu/Render/danmurender.h"
#ifdef Q_OS_WIN
#include <Windows.h>
#include <DbgHelp.h>
#pragma comment (lib,"DbgHelp.lib")
LONG AppCrashHandler(EXCEPTION_POINTERS *pException)
{
HANDLE hDumpFile = CreateFile((LPCWSTR)(QCoreApplication::applicationDirPath() + QDateTime::currentDateTime().toString("\\yyyy-MM-dd-hh-mm-ss")+".dmp").utf16(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDumpFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
dumpInfo.ExceptionPointers = pException;
dumpInfo.ThreadId = GetCurrentThreadId();
dumpInfo.ClientPointers = TRUE;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
}
EXCEPTION_RECORD* record = pException->ExceptionRecord;
QString errCode(QString::number(record->ExceptionCode, 16)), errAdr(QString::number((uint)record->ExceptionAddress, 16)), errMod;
QMessageBox::critical(nullptr, "Error", QString("Error Code: %1 Error Address: %2").arg(errCode).arg(errAdr),QMessageBox::Ok);
return EXCEPTION_EXECUTE_HANDLER;
}
#endif
void decodeParam()
{
QStringList args=QCoreApplication::arguments();
if(args.count()<=1)return;
args.pop_front();
QStringList fileList;
for(const QString &path:args)
{
QFileInfo fi(path);
if(fi.isFile())
{
if(GlobalObjects::mpvplayer->videoFileFormats.contains("*."+fi.suffix().toLower()))
{
fileList.append(fi.filePath());
}
}
}
if(fileList.count()>0)
{
GlobalObjects::playlist->addItems(fileList,QModelIndex());
const PlayListItem *curItem = GlobalObjects::playlist->setCurrentItem(fileList.last());
if (curItem)
{
GlobalObjects::mpvplayer->setMedia(curItem->path);
}
}
}
bool isRunning()
{
QLocalSocket socket;
socket.connectToServer("KikoPlaySingleServer");
if (socket.waitForConnected())
{
QStringList args=QCoreApplication::arguments();
if(args.count()>1)
{
args.pop_front();
QStringList fileList;
for(const QString &path:args)
{
QFileInfo fi(path);
if(fi.isFile())
{
const QStringList videoFileFormats = {"*.mp4","*.mkv","*.avi","*.flv","*.wmv"};
if(videoFileFormats.contains("*."+fi.suffix().toLower()))
{
fileList.append(fi.filePath());
}
}
}
if(fileList.count()>0)
{
QByteArray data;
QDataStream s(&data,QIODevice::WriteOnly);
s << fileList;
socket.write(data);
socket.waitForBytesWritten();
}
}
return true;
}
return false;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName("KikoPlay");
#ifdef Q_OS_WIN
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)AppCrashHandler);
#endif
if(isRunning()) return 0;
GlobalObjects::init();
MainWindow w;
GlobalObjects::mainWindow = &w;
QLocalServer *singleServer=new QLocalServer(&a);
QObject::connect(singleServer, &QLocalServer::newConnection, [singleServer,&w](){
QLocalSocket *localSocket = singleServer->nextPendingConnection();
localSocket->waitForReadyRead();
QByteArray data(localSocket->readAll());
QStringList args;
QDataStream s(data);
s >> args;
if(args.count()>0)
{
GlobalObjects::playlist->addItems(args,QModelIndex());
int playTime=GlobalObjects::mpvplayer->getTime();
GlobalObjects::playlist->setCurrentPlayTime(playTime);
const PlayListItem *curItem = GlobalObjects::playlist->setCurrentItem(args.last());
if (curItem)
{
GlobalObjects::danmuPool->reset();
GlobalObjects::danmuRender->cleanup();
GlobalObjects::mpvplayer->setMedia(curItem->path);
}
}
w.raise();
w.activateWindow();
w.setWindowState((w.windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
w.show();
});
if(!singleServer->listen("KikoPlaySingleServer"))
{
if(singleServer->serverError() == QAbstractSocket::AddressInUseError)
{
QLocalServer::removeServer("KikoPlaySingleServer");
singleServer->listen("KikoPlaySingleServer");
}
}
w.show();
decodeParam();
return a.exec();
}