Rabbit Remote Control 0.0.35
Loading...
Searching...
No Matches
ConnectDesktop.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QApplication>
4#include <QClipboard>
5#include <QTimer>
6#include <QLoggingCategory>
7#include <QWheelEvent>
8#include <QVideoFrame>
9#include <QDesktopServices>
10
11#include "ConnectDesktop.h"
12#include "ConnecterThread.h"
13
14static Q_LOGGING_CATEGORY(log, "Client.Connect.Desktop")
15static Q_LOGGING_CATEGORY(logMouse, "Client.Connect.Desktop.Mouse")
16
17#define TypeRecordVideo (QEvent::User + 1)
18class QRecordVideoEvent : public QEvent
19{
20public:
21 QRecordVideoEvent(const QImage& img): QEvent((QEvent::Type)TypeRecordVideo)
22 {
23 m_Image = img;
24 }
26 {
27 qDebug(log) << Q_FUNC_INFO;
28 }
29
30 QImage GetImage()
31 {
32 return m_Image;
33 }
34private:
35 QImage m_Image;
36};
37
38int g_QtKeyboardModifiers = qRegisterMetaType<Qt::KeyboardModifiers>("KeyboardModifiers");
39int g_QtMouseButtons = qRegisterMetaType<Qt::MouseButtons>("MouseButtons");
40int g_QtMouseButton = qRegisterMetaType<Qt::MouseButton>("MouseButton");
41int g_QMessageBox_Icon = qRegisterMetaType<Qt::MouseButton>("QMessageBox::Icon");
42
43CConnectDesktop::CConnectDesktop(CConnecter *pConnecter, bool bDirectConnection)
44 : CConnect(pConnecter)
45#if HAVE_QT6_RECORD
46 , m_pParameterRecord(nullptr)
47 , m_VideoFrameInput(this)
48 , m_AudioBufferInput(this)
49 , m_AudioBufferOutput(this)
50#endif
51{
52 if(pConnecter) {
53 CFrmScroll* pScroll = qobject_cast<CFrmScroll*>(pConnecter->GetViewer());
54 if(pScroll) {
55 CFrmViewer* pView = pScroll->GetViewer();
56 if(pView)
57 SetViewer(pView, pConnecter, bDirectConnection);
58 else {
59 QString szErr = pConnecter->metaObject()->className();
60 szErr += "::GetViewer() is not CFrmViewer";
61 qWarning(log) << szErr.toStdString().c_str();
62 }
63 } else {
64 QString szErr = pConnecter->metaObject()->className();
65 szErr += "::GetViewer() is not CFrmScroll";
66 qWarning(log) << szErr.toStdString().c_str();
67 }
68 SetConnecter(pConnecter);
69 }
70
71#if HAVE_QT6_RECORD
72 bool check = connect(
73 &m_Recorder, &QMediaRecorder::errorOccurred,
74 this, [&](QMediaRecorder::Error error, const QString &errorString) {
75 qDebug(log) << "Recorder error occurred:" << error << errorString;
76 slotRecord(false);
77 emit sigError(error, errorString);
78 });
79 Q_ASSERT(check);
80 check = connect(
81 &m_Recorder, &QMediaRecorder::recorderStateChanged,
82 this, [&](QMediaRecorder::RecorderState state){
83 qDebug(log) << "Recorder state changed:" << state;
84 if(QMediaRecorder::StoppedState == state)
85 {
86 slotRecord(false);
87 if(m_pParameterRecord) {
88 qDebug(log) << "End action:"
89 << m_pParameterRecord->GetEndAction()
90 << m_Recorder.actualLocation();
91 switch(m_pParameterRecord->GetEndAction())
92 {
93 case CParameterRecord::ENDACTION::OpenFile:
94 QDesktopServices::openUrl(m_Recorder.actualLocation());
95 break;
96 case CParameterRecord::ENDACTION::OpenFolder: {
97 QFileInfo fi(m_Recorder.actualLocation().toLocalFile());
98 QDesktopServices::openUrl(
99 QUrl::fromLocalFile(fi.absolutePath()));
100 break;
101 }
102 default:
103 break;
104 }
105 }
106 }
107 });
108 Q_ASSERT(check);
109 check = connect(&m_Recorder, &QMediaRecorder::actualLocationChanged,
110 this, [&](const QUrl &location){
111 qInfo(log) << "Recorder actual location changed:" << location;
112 });
113 Q_ASSERT(check);
114#endif
115}
116
117CConnectDesktop::~CConnectDesktop()
118{
119 qDebug(log) << "CConnectDesktop::~CConnectDesktop()";
120}
121
122int CConnectDesktop::SetConnecter(CConnecter* pConnecter)
123{
124 qDebug(log) << "CConnectDesktop::SetConnecter" << pConnecter;
125 Q_ASSERT(pConnecter);
126 if(!pConnecter) return -1;
127
128 bool check = false;
129 check = connect(this, SIGNAL(sigServerName(const QString&)),
130 pConnecter, SLOT(slotSetServerName(const QString&)));
131 Q_ASSERT(check);
132 check = connect(pConnecter, SIGNAL(sigClipBoardChanged()),
133 this, SLOT(slotClipBoardChanged()));
134 Q_ASSERT(check);
135 check = connect(this, SIGNAL(sigSetClipboard(QMimeData*)),
136 pConnecter, SLOT(slotSetClipboard(QMimeData*)));
137 Q_ASSERT(check);
138#if HAVE_QT6_RECORD
139 CConnecterThread* p = qobject_cast<CConnecterThread*>(pConnecter);
140 if(p) {
141 m_pParameterRecord = &p->GetParameter()->m_Record;
142 check = connect(p, SIGNAL(sigRecord(bool)),
143 this, SLOT(slotRecord(bool)));
144 Q_ASSERT(check);
145
146 check = connect(p, SIGNAL(sigRecordPause(bool)),
147 this, SLOT(slotRecordPause(bool)));
148 Q_ASSERT(check);
149 check = connect(
150 &m_Recorder,
151 SIGNAL(recorderStateChanged(QMediaRecorder::RecorderState)),
152 p, SLOT(slotRecorderStateChanged(QMediaRecorder::RecorderState)));
153 Q_ASSERT(check);
154 }
155#endif
156 return 0;
157}
158
159int CConnectDesktop::SetViewer(CFrmViewer *pView,
160 CConnecter* pConnecter, bool bDirectConnection)
161{
162 Q_ASSERT(pView);
163 if(!pView) return -1;
164
165 bool check = false;
166 check = connect(this, SIGNAL(sigConnected()), pView, SLOT(slotConnected()));
167 Q_ASSERT(check);
168 check = connect(this, SIGNAL(sigSetDesktopSize(int, int)),
169 pView, SLOT(slotSetDesktopSize(int, int)));
170 Q_ASSERT(check);
171 check = connect(this, SIGNAL(sigServerName(const QString&)),
172 pView, SLOT(slotSetName(const QString&)));
173 Q_ASSERT(check);
174
175 check = connect(this, SIGNAL(sigUpdateRect(const QRect&, const QImage&)),
176 pView, SLOT(slotUpdateRect(const QRect&, const QImage&)));
177 Q_ASSERT(check);
178 check = connect(this, SIGNAL(sigUpdateRect(const QImage&)),
179 pView, SLOT(slotUpdateRect(const QImage&)));
180 Q_ASSERT(check);
181 check = connect(this, SIGNAL(sigUpdateCursor(const QCursor&)),
182 pView, SLOT(slotUpdateCursor(const QCursor&)));
183 Q_ASSERT(check);
184 check = connect(this, SIGNAL(sigUpdateCursorPosition(const QPoint&)),
185 pView, SLOT(slotUpdateCursorPosition(const QPoint&)));
186 Q_ASSERT(check);
187 check = connect(this, SIGNAL(sigUpdateLedState(unsigned int)),
188 pView, SLOT(slotUpdateLedState(unsigned int)));
189 Q_ASSERT(check);
190
191#if HAVE_QT6_RECORD
192 check = connect(this, SIGNAL(sigRecordVideo(bool)),
193 pView, SLOT(slotRecordVideo(bool)));
194 Q_ASSERT(check);
195 check = connect(pView, SIGNAL(sigRecordVideo(QImage)),
196 this, SLOT(slotRecordVideo(QImage)),
197 Qt::DirectConnection);
198 Q_ASSERT(check);
199#endif
200 if(bDirectConnection)
201 {
202 /* \~chinese 因为连接可能是在另一个线程中的非Qt事件处理,
203 * 它可能会阻塞线程,那会导致键盘、鼠标事件延迟,
204 * 所以这里用 Qt::DirectConnection
205 * \~english Because the connection may be a non-Qt event processing in another thread,
206 * it may block the thread, which will cause the keyboard and mouse events to be delayed.
207 * So here use Qt::DirectConnection
208 */
209 check = connect(pView, SIGNAL(sigMousePressEvent(QMouseEvent*, QPoint)),
210 this, SLOT(slotMousePressEvent(QMouseEvent*, QPoint)),
211 Qt::DirectConnection);
212 Q_ASSERT(check);
213 check = connect(pView, SIGNAL(sigMouseReleaseEvent(QMouseEvent*, QPoint)),
214 this, SLOT(slotMouseReleaseEvent(QMouseEvent*, QPoint)),
215 Qt::DirectConnection);
216 Q_ASSERT(check);
217 check = connect(pView, SIGNAL(sigMouseMoveEvent(QMouseEvent*, QPoint)),
218 this, SLOT(slotMouseMoveEvent(QMouseEvent*, QPoint)),
219 Qt::DirectConnection);
220 Q_ASSERT(check);
221 check = connect(pView, SIGNAL(sigWheelEvent(QWheelEvent*, QPoint)),
222 this, SLOT(slotWheelEvent(QWheelEvent*, QPoint)),
223 Qt::DirectConnection);
224 Q_ASSERT(check);
225 check = connect(pView, SIGNAL(sigKeyPressEvent(QKeyEvent*)),
226 this, SLOT(slotKeyPressEvent(QKeyEvent*)),
227 Qt::DirectConnection);
228 Q_ASSERT(check);
229 check = connect(pView, SIGNAL(sigKeyReleaseEvent(QKeyEvent*)),
230 this, SLOT(slotKeyReleaseEvent(QKeyEvent*)),
231 Qt::DirectConnection);
232 Q_ASSERT(check);
233 } else {
234 check = connect(pView, SIGNAL(sigMousePressEvent(QMouseEvent*, QPoint)),
235 this, SLOT(slotMousePressEvent(QMouseEvent*, QPoint)));
236 Q_ASSERT(check);
237 check = connect(pView, SIGNAL(sigMouseReleaseEvent(QMouseEvent*, QPoint)),
238 this, SLOT(slotMouseReleaseEvent(QMouseEvent*, QPoint)));
239 Q_ASSERT(check);
240 check = connect(pView, SIGNAL(sigMouseMoveEvent(QMouseEvent*, QPoint)),
241 this, SLOT(slotMouseMoveEvent(QMouseEvent*, QPoint)));
242 Q_ASSERT(check);
243 check = connect(pView, SIGNAL(sigWheelEvent(QWheelEvent*, QPoint)),
244 this, SLOT(slotWheelEvent(QWheelEvent*, QPoint)));
245 Q_ASSERT(check);
246 check = connect(pView, SIGNAL(sigKeyPressEvent(QKeyEvent*)),
247 this, SLOT(slotKeyPressEvent(QKeyEvent*)));
248 Q_ASSERT(check);
249 check = connect(pView, SIGNAL(sigKeyReleaseEvent(QKeyEvent*)),
250 this, SLOT(slotKeyReleaseEvent(QKeyEvent*)));
251 Q_ASSERT(check);
252 }
253
254 return 0;
255}
256
257void CConnectDesktop::slotWheelEvent(QWheelEvent *event, QPoint pos)
258{
259 QWheelEvent* e = new QWheelEvent(
260 pos,
261#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
262 event->globalPosition(),
263#else
264 event->globalPos(),
265#endif
266 event->pixelDelta(), event->angleDelta(), event->buttons(),
267 event->modifiers(), event->phase(), event->inverted(), event->source());
268 QCoreApplication::postEvent(this, e);
269 WakeUp();
270}
271
272void CConnectDesktop::slotMouseMoveEvent(QMouseEvent *event, QPoint pos)
273{
274 QMouseEvent* e = new QMouseEvent(event->type(), pos, event->button(),
275 event->buttons(), event->modifiers());
276 QCoreApplication::postEvent(this, e);
277 WakeUp();
278}
279
280void CConnectDesktop::slotMousePressEvent(QMouseEvent *event, QPoint pos)
281{
282 QMouseEvent* e = new QMouseEvent(event->type(), pos, event->button(),
283 event->buttons(), event->modifiers());
284 QCoreApplication::postEvent(this, e);
285 WakeUp();
286}
287
288void CConnectDesktop::slotMouseReleaseEvent(QMouseEvent *event, QPoint pos)
289{
290 QMouseEvent* e = new QMouseEvent(event->type(), pos, event->button(),
291 event->buttons(), event->modifiers());
292 QCoreApplication::postEvent(this, e);
293 WakeUp();
294}
295
296void CConnectDesktop::slotKeyPressEvent(QKeyEvent *event)
297{
298 QKeyEvent* e = new QKeyEvent(event->type(), event->key(),
299 event->modifiers(), event->text());
300 QCoreApplication::postEvent(this, e);
301 WakeUp();
302}
303
304void CConnectDesktop::slotKeyReleaseEvent(QKeyEvent *event)
305{
306 QKeyEvent* e = new QKeyEvent(event->type(), event->key(),
307 event->modifiers(), event->text());
308 QCoreApplication::postEvent(this, e);
309 WakeUp();
310}
311
312void CConnectDesktop::mouseMoveEvent(QMouseEvent *event)
313{
314 qDebug(logMouse) << "Need to implement CConnectDesktop::mouseMoveEvent";
315}
316
317void CConnectDesktop::mousePressEvent(QMouseEvent *event)
318{
319 qDebug(logMouse) << "Need to implement CConnectDesktop::mousePressEvent";
320}
321
322void CConnectDesktop::mouseReleaseEvent(QMouseEvent *event)
323{
324 qDebug(logMouse) << "Need to implement CConnectDesktop::mouseReleaseEvent";
325}
326
327void CConnectDesktop::wheelEvent(QWheelEvent *event)
328{
329 qDebug(logMouse) << "Need to implement CConnectDesktop::wheelEvent";
330}
331
332void CConnectDesktop::keyPressEvent(QKeyEvent *event)
333{
334 qDebug(logMouse) << "Need to implement CConnectDesktop::keyPressEvent";
335}
336
337void CConnectDesktop::keyReleaseEvent(QKeyEvent *event)
338{
339 qDebug(logMouse) << "Need to implement CConnectDesktop::keyReleaseEvent";
340}
341
343{
344 return 0;
345}
346
347bool CConnectDesktop::event(QEvent *event)
348{
349 //qDebug(log) << "CConnectDesktop::event" << event;
350 switch (event->type()) {
351 case QEvent::MouseButtonPress:
352 case QEvent::MouseButtonDblClick:
353 mousePressEvent((QMouseEvent*)event);
354 break;
355 case QEvent::MouseButtonRelease:
356 mouseReleaseEvent((QMouseEvent*)event);
357 break;
358 case QEvent::MouseMove:
359 mouseMoveEvent((QMouseEvent*)event);
360 break;
361 case QEvent::Wheel:
362 wheelEvent((QWheelEvent*)event);
363 break;
364 case QEvent::KeyPress:
365 keyPressEvent((QKeyEvent*)event);
366 break;
367 case QEvent::KeyRelease:
368 keyReleaseEvent((QKeyEvent*)event);
369 break;
370#if HAVE_QT6_RECORD
371 case TypeRecordVideo:
372 RecordVideo((QRecordVideoEvent*)event);
373 break;
374#endif
375 default:
376 return QObject::event(event);
377 }
378
379 event->accept();
380 return true;
381}
382
383#if HAVE_QT6_RECORD
384void CConnectDesktop::slotRecord(bool bRecord)
385{
386 qDebug(log) << Q_FUNC_INFO << bRecord;
387 if(bRecord) {
388 if(QMediaRecorder::RecordingState == m_Recorder.recorderState())
389 return;
390 (*m_pParameterRecord) >> m_Recorder;
391 m_CaptureSession.setVideoFrameInput(&m_VideoFrameInput);
392 m_CaptureSession.setRecorder(&m_Recorder);
393 m_Recorder.record();
394 } else {
395 m_Recorder.stop();
396 m_CaptureSession.setVideoFrameInput(nullptr);
397 m_CaptureSession.setAudioBufferInput(nullptr);
398 m_CaptureSession.setRecorder(nullptr);
399 }
400 emit sigRecordVideo(bRecord);
401}
402
403void CConnectDesktop::slotRecordPause(bool bPause)
404{
405 qDebug(log) << Q_FUNC_INFO << bPause;
406 if(bPause) {
407 if(m_Recorder.recorderState() == QMediaRecorder::RecordingState)
408 m_Recorder.pause();
409 } else {
410 if(m_Recorder.recorderState() == QMediaRecorder::PausedState)
411 m_Recorder.record();
412 }
413}
414
415void CConnectDesktop::slotRecordVideo(const QImage &img)
416{
418 if(!e) return;
419 QCoreApplication::postEvent(this, e);
420 WakeUp();
421}
422
423void CConnectDesktop::RecordVideo(QRecordVideoEvent *e)
424{
425 qDebug(log) << "Update image";
426 if(!e) return;
427 if(QMediaRecorder::RecordingState != m_Recorder.recorderState()) {
428 qCritical(log) << "Recorder is inavailable";
429 return;
430 }
431 QVideoFrame frame(e->GetImage());
432 bool bRet = m_VideoFrameInput.sendVideoFrame(frame);
433 if(!bRet) {
434 //TODO: 放入未成功发送队列,
435 // 当 QVideoFrameInput::readyToSendVideoFrame() 时,再发送
436 qDebug(log) << "m_VideoFrameInput.sendVideoFrame fail";
437 }
438}
439
441{
442 slotRecord(false);
443 return CConnect::Disconnect();
444}
445#endif
CConnectDesktop(CConnecter *pConnecter, bool bDirectConnection=true)
void sigUpdateRect(const QRect &r, const QImage &image)
Notify the CFrmView update image.
virtual int WakeUp()
Wake up Connect thread(background thread)
virtual void slotClipBoardChanged()=0
Be called when the clip board change.
Connect interface.
Definition Connect.h:45
void sigError(const int nError, const QString &szError=QString())
Triggered when an error is generated.
void sigConnected()
Emitted when the plugin is successfully connected.
virtual int Disconnect()
Disconnect.
Definition Connect.cpp:89
virtual CParameterBase * GetParameter()
Get parameter.
It starts a background thread by default.
Connecter interface.
Definition Connecter.h:62
virtual QWidget * GetViewer()=0
Get Viewer.
The scroll form class.
Definition FrmScroll.h:17
A widget which displays output image from a CConnectDesktop and sends input keypresses and mouse acti...
Definition FrmViewer.h:49