Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 61 additions & 75 deletions apps/DesktopStreamer/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ typedef __int32 int32_t;
#endif

#define SHARE_DESKTOP_UPDATE_DELAY 1
#define SERVUS_BROWSE_DELAY 100
#define FAILURE_UPDATE_DELAY 100
#define FRAME_RATE_AVERAGE_NUM_FRAMES 10
#define CURSOR_IMAGE_FILE ":/cursor.png"
#define CURSOR_IMAGE_SIZE 20
Expand All @@ -81,13 +81,9 @@ const std::vector< std::pair< QString, QString > > defaultHosts = {
};

MainWindow::MainWindow()
: _stream( 0 )
, _cursor( QImage( CURSOR_IMAGE_FILE ).scaled(
: _cursor( QImage( CURSOR_IMAGE_FILE ).scaled(
CURSOR_IMAGE_SIZE * devicePixelRatio(),
CURSOR_IMAGE_SIZE * devicePixelRatio(), Qt::KeepAspectRatio ))
#ifdef DEFLECT_USE_SERVUS
, _servus( deflect::Server::serviceName )
#endif
{
setupUi( this );

Expand Down Expand Up @@ -141,73 +137,42 @@ MainWindow::MainWindow()
connect( &_updateTimer, &QTimer::timeout, this, &MainWindow::_update );
}

MainWindow::~MainWindow() {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm


void MainWindow::_startStreaming()
{
if( _stream )
return;

QString streamHost;
if( _hostnameComboBox->findText( _hostnameComboBox->currentText( )) == -1 )
streamHost = _hostnameComboBox->currentText();
else
streamHost = _hostnameComboBox->currentData().toString();

_stream = new deflect::Stream( _streamnameLineEdit->text().toStdString(),
streamHost.toStdString( ));
if( !_stream->isConnected( ))
{
_handleStreamingError( "Could not connect to host" );
return;
}

#ifdef DEFLECT_USE_QT5MACEXTRAS
_windowIndex = _listView->currentIndex();
if( !_windowIndex.isValid( ))
{
_handleStreamingError( "No window to stream is selected" );
return;
}
#endif

#ifdef STREAM_EVENTS_SUPPORTED
if( _desktopInteractionCheckBox->isChecked( ))
_stream->registerForEvents();
#endif

#ifdef __APPLE__
_napSuspender.suspend();
#endif
#ifdef DEFLECT_USE_SERVUS
_browseTimer.stop();
#endif
_updateTimer.start( SHARE_DESKTOP_UPDATE_DELAY );
}

void MainWindow::_stopStreaming()
{
_updateTimer.stop();
_statusbar->clearMessage();

delete _stream;
_stream = 0;
_stream.reset();
if( _streamButton->isChecked( ))
_updateTimer.start( FAILURE_UPDATE_DELAY );
else
{
_updateTimer.stop();
_statusbar->clearMessage();

#ifdef __APPLE__
_napSuspender.resume();
_napSuspender.resume();
#endif
_streamButton->setChecked( false );
_streamButton->setChecked( false );
}
}

void MainWindow::_handleStreamingError( const QString& errorMessage )
{
std::cerr << errorMessage.toStdString() << std::endl;
QMessageBox::warning( this, "Error", errorMessage, QMessageBox::Ok,
QMessageBox::Ok );

_statusbar->showMessage( errorMessage );
_stopStreaming();
}

void MainWindow::closeEvent( QCloseEvent* closeEvt )
{
_streamButton->setChecked( false );
_stopStreaming();
QMainWindow::closeEvent( closeEvt );
}
Expand All @@ -222,23 +187,60 @@ void MainWindow::_shareDesktop( const bool set )

void MainWindow::_update()
{
if( _stream->isRegisteredForEvents( ))
if( _streamButton->isChecked( ))
{
_checkStream();
_processStreamEvents();
_shareDesktopUpdate();
_shareDesktopUpdate();
}
else
_stopStreaming();
}

void MainWindow::_checkStream()
{
if( _stream )
return;

QString streamHost;
if( _hostnameComboBox->findText( _hostnameComboBox->currentText( )) == -1 )
streamHost = _hostnameComboBox->currentText();
else
streamHost = _hostnameComboBox->currentData().toString();

_stream.reset(
new deflect::Stream( _streamnameLineEdit->text().toStdString(),
streamHost.toStdString( )));
if( !_stream->isConnected( ))
{
_handleStreamingError( "Could not connect to host" );
return;
}

#ifdef DEFLECT_USE_QT5MACEXTRAS
_windowIndex = _listView->currentIndex();
if( !_windowIndex.isValid( ))
{
_handleStreamingError( "No window to stream is selected" );
return;
}
#endif

#ifdef STREAM_EVENTS_SUPPORTED
if( _desktopInteractionCheckBox->isChecked( ))
_stream->registerForEvents();
#endif
}

void MainWindow::_processStreamEvents()
{
while( _stream->hasEvent( ))
while( _stream && _stream->isRegisteredForEvents() && _stream->hasEvent( ))
{
const deflect::Event& wallEvent = _stream->getEvent();
// Once registered for events they must be consumed, otherwise they
// queue up. Until unregister is implemented, just ignore them.
if( !_desktopInteractionCheckBox->checkState( ))
break;
#ifndef NDEBUG
std::cout << "----------" << std::endl;
#endif
switch( wallEvent.type )
{
case deflect::Event::EVT_CLOSE:
Expand Down Expand Up @@ -302,7 +304,7 @@ void MainWindow::_shareDesktopUpdate()

if( pixmap.isNull( ))
{
_handleStreamingError( "Got NULL desktop pixmap" );
_handleStreamingError( "Got no pixmap for desktop or window" );
return;
}
QImage image = pixmap.toImage();
Expand All @@ -325,7 +327,7 @@ void MainWindow::_shareDesktopUpdate()
_stream->finishFrame();
if( !success )
{
_handleStreamingError( "Streaming failure, connection closed." );
_handleStreamingError( "Streaming failure, connection closed" );
return;
}

Expand Down Expand Up @@ -403,10 +405,6 @@ void MainWindow::_sendMousePressEvent( const float x, const float y )
CGPoint point;
point.x = _windowRect.topLeft().x() + x * _windowRect.width();
point.y = _windowRect.topLeft().y() + y * _windowRect.height();
#ifndef NDEBUG
std::cout << "Press " << point.x << ", " << point.y << " ("
<< x << ", " << y << ")"<< std::endl;
#endif
sendMouseEvent( kCGEventLeftMouseDown, kCGMouseButtonLeft, point );
}

Expand All @@ -415,10 +413,6 @@ void MainWindow::_sendMouseMoveEvent( const float x, const float y )
CGPoint point;
point.x = _windowRect.topLeft().x() + x * _windowRect.width();
point.y = _windowRect.topLeft().y() + y * _windowRect.height();
#ifndef NDEBUG
std::cout << "Move " << point.x << ", " << point.y << " ("
<< x << ", " << y << ")"<< std::endl;
#endif
sendMouseEvent( kCGEventMouseMoved, kCGMouseButtonLeft, point );
}

Expand All @@ -427,10 +421,6 @@ void MainWindow::_sendMouseReleaseEvent( const float x, const float y )
CGPoint point;
point.x = _windowRect.topLeft().x() + x * _windowRect.width();
point.y = _windowRect.topLeft().y() + y * _windowRect.height();
#ifndef NDEBUG
std::cout << "Release " << point.x << ", " << point.y << " ("
<< x << ", " << y << ")"<< std::endl;
#endif
sendMouseEvent( kCGEventLeftMouseUp, kCGMouseButtonLeft, point );
}

Expand All @@ -441,10 +431,6 @@ void MainWindow::_sendMouseDoubleClickEvent( const float x, const float y )
point.y = _windowRect.topLeft().y() + y * _windowRect.height();
CGEventRef event = CGEventCreateMouseEvent( 0, kCGEventLeftMouseDown,
point, kCGMouseButtonLeft );
#ifndef NDEBUG
std::cout << "Double click " << point.x << ", " << point.y << " ("
<< x << ", " << y << ")"<< std::endl;
#endif

CGEventSetIntegerValueField( event, kCGMouseEventClickState, 2 );
CGEventPost( kCGHIDEventTap, event );
Expand Down
8 changes: 3 additions & 5 deletions apps/DesktopStreamer/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindow

public:
MainWindow();
~MainWindow();

protected:
void closeEvent( QCloseEvent* event ) final;
Expand All @@ -71,7 +72,7 @@ private slots:
void _openAboutWidget();

private:
deflect::Stream* _stream;
std::unique_ptr< deflect::Stream > _stream;

QImage _cursor;
QTimer _updateTimer;
Expand All @@ -87,13 +88,10 @@ private slots:
#ifdef __APPLE__
AppNapSuspender _napSuspender;
#endif
#ifdef DEFLECT_USE_SERVUS
QTimer _browseTimer;
servus::Servus _servus;
#endif

void _startStreaming();
void _stopStreaming();
void _checkStream();
void _handleStreamingError( const QString& errorMessage );
void _processStreamEvents();
void _shareDesktopUpdate();
Expand Down
Loading