-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
223 lines (186 loc) · 6.73 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// This file is part of Twiccian.
//
// Twiccian is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Twiccian is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Twiccian. If not, see <http://www.gnu.org/licenses/>.
#include "main.h"
#include "socketreader.h"
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdexcept>
#include <clocale>
#include <string>
#include <QObject>
#include <QtGlobal>
#include <QProcess>
#include <QOpenGLContext>
#include <QGuiApplication>
#include <QtGui/QOpenGLFramebufferObject>
#include <QtQuick/QQuickWindow>
#include <QtQuick/QQuickView>
#include <QQmlApplicationEngine>
class MpvRenderer : public QQuickFramebufferObject::Renderer
{
static void *get_proc_address(void *ctx, const char *name) {
(void)ctx;
QOpenGLContext *glctx = QOpenGLContext::currentContext();
if (!glctx)
return NULL;
return (void *)glctx->getProcAddress(QByteArray(name));
}
mpv::qt::Handle mpv;
QQuickWindow *window;
mpv_opengl_cb_context *mpv_gl;
public:
// Create mpv opengl-cb instance
MpvRenderer(const MpvObject *obj)
: mpv(obj->mpv), window(obj->window()), mpv_gl(obj->mpv_gl)
{
int r = mpv_opengl_cb_init_gl(mpv_gl, NULL, get_proc_address, NULL);
if (r < 0)
throw std::runtime_error("Could not initialize mpv video renderer");
}
// Destroys the mpv renderer
virtual ~MpvRenderer()
{
// Until this call is done, we need to make sure the player remains
// alive. This is done implicitly with the mpv::qt::Handle instance
// in this class.
mpv_opengl_cb_uninit_gl(mpv_gl);
}
void render()
{
QOpenGLFramebufferObject *fbo = framebufferObject();
window->resetOpenGLState();
mpv_opengl_cb_draw(mpv_gl, fbo->handle(), fbo->width(), fbo->height());
window->resetOpenGLState();
}
};
MpvObject::MpvObject(QQuickItem * parent)
: QQuickFramebufferObject(parent), mpv_gl(0)
{
mpv = mpv::qt::Handle::FromRawHandle(mpv_create());
if (!mpv)
throw std::runtime_error("could not create mpv context");
// Debug output for MPV
mpv_set_option_string(mpv, "terminal", "no");
mpv_set_option_string(mpv, "msg-level", "all=no");
if (mpv_initialize(mpv) < 0)
throw std::runtime_error("could not initialize mpv context");
// Make use of the MPV_SUB_API_OPENGL_CB API.
mpv::qt::set_option_variant(mpv, "vo", "opengl-cb");
// Request hw decoding, just for testing.
mpv::qt::set_option_variant(mpv, "hwdec", "auto");
// Setup the callback that will make QtQuick update and redraw if there
// is a new video frame. Use a queued connection: this makes sure the
// doUpdate() function is run on the GUI thread.
mpv_gl = (mpv_opengl_cb_context *)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB);
if (!mpv_gl)
throw std::runtime_error("OpenGL not compiled in");
mpv_opengl_cb_set_update_callback(mpv_gl, MpvObject::on_update, (void *)this);
connect(this, &MpvObject::onUpdate, this, &MpvObject::doUpdate,
Qt::QueuedConnection);
}
MpvObject::~MpvObject()
{
if (mpv_gl)
mpv_opengl_cb_set_update_callback(mpv_gl, NULL, NULL);
}
void MpvObject::on_update(void *ctx)
{
MpvObject *self = (MpvObject *)ctx;
emit self->onUpdate();
}
// connected to onUpdate(); signal makes sure it runs on the GUI thread
void MpvObject::doUpdate()
{
update();
}
void MpvObject::command(const QVariant& params)
{
mpv::qt::command_variant(mpv, params);
}
void MpvObject::setProperty(const QString& name, const QVariant& value)
{
mpv::qt::set_property_variant(mpv, name, value);
}
QQuickFramebufferObject::Renderer *MpvObject::createRenderer() const
{
window()->setPersistentOpenGLContext(true);
window()->setPersistentSceneGraph(true);
return new MpvRenderer(this);
}
int main(int argc, char **argv)
{
//bool daemon_running = false;
// Store the current working directory for the qt application
char cwd[1024];
getcwd(cwd, sizeof(cwd));
// Check if the daemon is running or not
chdir("/proc");
DIR* procdir = opendir("/proc");
struct dirent *dent;
// Loop through all the directories in /proc
while ((dent = readdir(procdir))) {
// if the directory is a proccess id, search it
int pid = strtol(dent->d_name, NULL, 10);
if (pid != 0) {
// Enter the directory and read the cmdline entry
chdir(dent->d_name);
char cmdline[1024];
int fd = open ("cmdline", O_RDONLY);
int len = read(fd, cmdline, (sizeof cmdline)-1);
cmdline[len] = '\0';
close(fd);
// If the command is twicciand, we found our match; break out
if (strncmp(cmdline, "twicciand", 9) == 0) {
// WARNING
//daemon_running = true;
printf("Daemon is already running!\n");
break;
}
chdir("/proc");
}
}
closedir(procdir);
// Run the daemon if necessary
QString program = "twicciand";
QStringList args;
QProcess *daemon = new QProcess();
daemon->start(program, args);
// Return to the qt application's working directory
chdir(cwd);
QGuiApplication app(argc, argv);
// Qt sets the locale in the QGuiApplication constructor, but libmpv
// requires the LC_NUMERIC category to be set to "C", so change it back.
std::setlocale(LC_NUMERIC, "C");
// Register the custom type MpvObject to use in our qml
qmlRegisterType<MpvObject>("twiccian", 1, 0, "MpvObject");
qmlRegisterType<SubmitUrlObj>("twiccian", 1, 0, "Api");
QList<QObject*> dataList;
QList<QObject*> searchList;
//dataList.append(new Result());
// Render the qml using a QQmlApplicationEngine
QQmlApplicationEngine engine("main.qml");
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
ctxt->setContextProperty("searchModel", QVariant::fromValue(searchList));
QObject *rootObject = engine.rootObjects()[0];
SubmitUrlObj *api = rootObject->findChild<SubmitUrlObj*>(QString("apiobj"));
api->setContext(ctxt);
return app.exec();
}