Skip to content

Commit

Permalink
Merge pull request #53 from smasherprog/Development
Browse files Browse the repository at this point in the history
Merging for .v35 release
  • Loading branch information
smasherprog authored Jul 10, 2016
2 parents 85bf1a8 + c7c5ba2 commit 082133d
Show file tree
Hide file tree
Showing 200 changed files with 10,119 additions and 30,097 deletions.
1 change: 1 addition & 0 deletions .build-debug/Core
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stam
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ bld/
[Jj]ni[Ll]ibs/
.vs/
.codelite/
.build-*/
*.tlog
.vs/
# Roslyn cache directories
*.ide/

Expand Down Expand Up @@ -69,6 +69,8 @@ dlldata.c
*.pidb
*.svclog
*.scc
*.mk
*.txt

# Chutzpah Test files
_Chutzpah*
Expand Down
8 changes: 4 additions & 4 deletions Core/ApplicationDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::string executable_path_fallback(const char *argv0)
return p.make_preferred().string();
}

#if (_WIN32) // {
#if (_WIN32)

# include <Windows.h>

Expand Down Expand Up @@ -70,7 +70,7 @@ std::string executable_path(const char *argv0)
# error "Unknown Apple platform"
#endif

#elif (__linux__) // } {
#elif (__linux__)

# include <unistd.h>

Expand All @@ -90,13 +90,13 @@ std::string executable_path(const char *argv0)
return p.make_preferred().string();
}

#else // } {
#else

std::string executable_path(const char *argv0)
{
return executable_path_fallback(argv0);
}

#endif // }
#endif

#endif
84 changes: 55 additions & 29 deletions Core/ClientNetworkDriver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "stdafx.h"
#include "ClientNetworkDriver.h"
#include "WebSocket.h"
#include "Socket.h"
#include "Shapes.h"
#include "IClientDriver.h"
#include "Image.h"
Expand All @@ -9,8 +9,8 @@
#include "Mouse.h"
#include "Logging.h"
#include "Keyboard.h"
#include "ISocket.h"
#include "Client_Config.h"
#include "ISocket.h"

#include <assert.h>

Expand All @@ -24,21 +24,19 @@ namespace SL {
std::shared_ptr<Network::Client_Config> _Config;
std::shared_ptr<Network::ISocket> _Socket;
std::string _dst_host;

bool _ConectedToSelf;


void MouseImage(const std::shared_ptr<ISocket>& socket, std::shared_ptr<Packet>& p) {
Utilities::Point _LastMousePosition;

void MouseImage(std::shared_ptr<Packet>& p) {
auto imgsize = (Utilities::Point*)p->Payload;
auto img(Utilities::Image::CreateImage(imgsize->Y, imgsize->X, p->Payload + sizeof(Utilities::Rect), 4));
_IClientDriver->OnReceive_MouseImage(socket, img);
_IClientDriver->OnReceive_MouseImage(img);
}

void MousePos(const std::shared_ptr<ISocket>& socket, std::shared_ptr<Packet>& p) {
void MousePos(std::shared_ptr<Packet>& p) {
assert(p->Payload_Length == sizeof(Utilities::Point));
_IClientDriver->OnReceive_MousePos(socket, (Utilities::Point*)p->Payload);
_IClientDriver->OnReceive_MousePos((Utilities::Point*)p->Payload);
}
void ImageDif(const std::shared_ptr<ISocket>& socket, std::shared_ptr<Packet>& p) {
void ScreenImageDif(std::shared_ptr<Packet>& p) {
auto imgrect = (Utilities::Rect*)p->Payload;
auto compfree = [](void* handle) {tjDestroy(handle); };
auto _jpegDecompressor(std::unique_ptr<void, decltype(compfree)>(tjInitDecompress(), compfree));
Expand All @@ -54,10 +52,10 @@ namespace SL {
if (tjDecompress2(_jpegDecompressor.get(), src, static_cast<unsigned long>(p->Payload_Length - sizeof(Utilities::Rect)), (unsigned char*)img->data(), outwidth, 0, outheight, TJPF_RGBX, TJFLAG_FASTDCT | TJFLAG_NOREALLOC) == -1) {
SL_RAT_LOG(Utilities::Logging_Levels::ERROR_log_level, tjGetErrorStr());
}
_IClientDriver->OnReceive_ImageDif(socket, imgrect->Origin, img);
_IClientDriver->OnReceive_ImageDif(imgrect->Origin, img);

}
void Image(const std::shared_ptr<ISocket>& socket, std::shared_ptr<Packet>& p) {
void ScreenImage(std::shared_ptr<Packet>& p) {

auto compfree = [](void* handle) {tjDestroy(handle); };
auto _jpegDecompressor(std::unique_ptr<void, decltype(compfree)>(tjInitDecompress(), compfree));
Expand All @@ -72,26 +70,32 @@ namespace SL {
if (tjDecompress2(_jpegDecompressor.get(), src, static_cast<unsigned long>(p->Payload_Length - sizeof(Utilities::Rect)), (unsigned char*)img->data(), outwidth, 0, outheight, TJPF_RGBX, TJFLAG_FASTDCT | TJFLAG_NOREALLOC) == -1) {
SL_RAT_LOG(Utilities::Logging_Levels::ERROR_log_level, tjGetErrorStr());
}
_IClientDriver->OnReceive_Image(socket, img);
_IClientDriver->OnReceive_Image(img);

}

void ClipboardTextEvent(std::shared_ptr<Packet>& p) {

_IClientDriver->OnReceive_ClipboardText(p->Payload, p->Payload_Length);
}

public:
ClientNetworkDriverImpl(IClientDriver* r, std::shared_ptr<Network::Client_Config> config, const char * dst_host) :
_IClientDriver(r), _Config(config), _dst_host(dst_host), _ConectedToSelf(false){

_IClientDriver(r), _Config(config), _dst_host(dst_host){
memset(&_LastMousePosition, 0, sizeof(_LastMousePosition));
}

void Start() {
Stop();
WebSocket::Connect(_Config.get(), this, _dst_host.c_str());
_ConectedToSelf = (std::string("127.0.0.1") == _dst_host) || (std::string("localhost") == _dst_host) || (std::string("::1") == _dst_host);
Connect(_Config.get(), this, _dst_host.c_str());


}

void Stop() {
if (_Socket) _Socket->close("Stopping Listener");
_Socket.reset();//decrement count
if (_Socket) {
_Socket->close("Stopping Listener");
_Socket.reset();//decrement count
}
}
virtual ~ClientNetworkDriverImpl() {
Stop();
Expand All @@ -112,16 +116,19 @@ namespace SL {

switch (p->Packet_Type) {
case static_cast<unsigned int>(PACKET_TYPES::SCREENIMAGE) :
Image(socket, p);
ScreenImage(p);
break;
case static_cast<unsigned int>(PACKET_TYPES::SCREENIMAGEDIF) :
ImageDif(socket, p);
ScreenImageDif(p);
break;
case static_cast<unsigned int>(PACKET_TYPES::MOUSEIMAGE) :
MouseImage(socket, p);
MouseImage(p);
break;
case static_cast<unsigned int>(PACKET_TYPES::MOUSEPOS) :
MousePos(socket, p);
MousePos(p);
break;
case static_cast<unsigned int>(PACKET_TYPES::CLIPBOARDTEXTEVENT) :
ClipboardTextEvent(p);
break;
default:
_IClientDriver->OnReceive(socket, p);//pass up the chain
Expand All @@ -134,6 +141,12 @@ namespace SL {
SL_RAT_LOG(Utilities::Logging_Levels::INFO_log_level, "SendMouse called on a socket that is not open yet");
return;
}
if (_Socket->is_loopback()) return;//dont send mouse info to ourselfs as this will cause a loop
//do checks to prevent sending redundant mouse information about its position
if (m.EventData == Input::Mouse::NO_EVENTDATA && _LastMousePosition == m.Pos && m.PressData == Input::Mouse::NO_PRESS_DATA && m.ScrollDelta == 0) {
return;//already did this event
}
_LastMousePosition = m.Pos;
Packet p(static_cast<unsigned int>(PACKET_TYPES::MOUSEEVENT), sizeof(m));
memcpy(p.Payload, &m, sizeof(m));
_Socket->send(p);
Expand All @@ -147,9 +160,18 @@ namespace SL {
memcpy(p.Payload, &m, sizeof(m));
_Socket->send(p);
}
bool ConnectedToSelf() const {
return _ConectedToSelf;
void SendClipboardText(const char* data, unsigned int len) {
if (!_Socket) {
SL_RAT_LOG(Utilities::Logging_Levels::INFO_log_level, "SendKey called on a socket that is not open yet");
return;
}
if (_Socket->is_loopback()) return;//dont send clipboard info to ourselfs as it will cause a loop
Packet p(static_cast<unsigned int>(PACKET_TYPES::CLIPBOARDTEXTEVENT), len, (char*)data, false);
_Socket->send(p);
}
std::shared_ptr<ISocket> get_Socket()const{
return _Socket;
}
};
}
}
Expand Down Expand Up @@ -187,6 +209,10 @@ void SL::Remote_Access_Library::Network::ClientNetworkDriver::SendMouse(const In
_ClientNetworkDriverImpl->SendMouse(m);
}

bool SL::Remote_Access_Library::Network::ClientNetworkDriver::ConnectedToSelf() const {
return _ClientNetworkDriverImpl->ConnectedToSelf();
std::shared_ptr<SL::Remote_Access_Library::Network::ISocket> SL::Remote_Access_Library::Network::ClientNetworkDriver::get_Socket()const{
return _ClientNetworkDriverImpl->get_Socket();
}

void SL::Remote_Access_Library::Network::ClientNetworkDriver::SendClipboardText(const char* data, unsigned int len) {
return _ClientNetworkDriverImpl->SendClipboardText(data, len);
}
5 changes: 4 additions & 1 deletion Core/ClientNetworkDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ namespace SL {

void SendKey(const Input::KeyEvent& m);
void SendMouse(const Input::MouseEvent& m);
bool ConnectedToSelf() const;
void SendClipboardText(const char* data, unsigned int len);

std::shared_ptr<ISocket> get_Socket()const;

};
}
}
Expand Down
2 changes: 2 additions & 0 deletions Core/Client_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace SL {
unsigned short WebSocketTLSLPort = 6001;
unsigned int Read_Timeout = 5;//in seconds
unsigned int Write_Timeout = 5;//in seconds
bool Share_Clipboard = true;//share your clipboard?
bool Scale_Image = false;

std::string Password;//this is the password to connect to the endpoint or that the server requrires before allowing the connection

Expand Down
101 changes: 101 additions & 0 deletions Core/Clipboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "stdafx.h"
#include "Clipboard.h"
#include "Logging.h"

#include <FL/Fl.H>
#include <FL/Fl_Widget.H>

#include <mutex>
#include <vector>

namespace SL {
namespace Remote_Access_Library {
namespace Capturing {
class ClipboardImpl : public Fl_Widget {
public:
const bool* Is_ClipboardShared;
std::function<void(const char*, int)> OnChange;
//contention on this mutex is low.. no need to try an optimize
std::mutex LastClipDataLock;
std::vector<char> LastClipData;
ClipboardImpl(const bool* is_clipshared, std::function<void(const char*, int)>&& onchange) : Fl_Widget(0, 0, 0, 0), Is_ClipboardShared(is_clipshared), OnChange(std::move(onchange)) {
Fl::add_clipboard_notify(clip_callback, this);
}
virtual ~ClipboardImpl() {
Fl::remove_clipboard_notify(clip_callback);
}
virtual void draw() override {}//do nothing
virtual int handle(int event) override {
if (event == FL_PASTE) {

if (strcmp(Fl::event_clipboard_type(), Fl::clipboard_image) == 0) { // an image is being pasted
//To Be implemeneted...... Probably not though because images can be large and seems like its an edge case any way
}
else {//text is being pasted
bool emitclipevent = false;
auto data = Fl::event_text();
auto len = Fl::event_length();
{
std::lock_guard<std::mutex> lock(LastClipDataLock);
emitclipevent = !is_datasame(data, len);
if (emitclipevent) UpdateBuffer(data, len);
}
if (emitclipevent) {
OnChange(data, len);
}

}

}
return 1;
}

static void clip_callback(int source, void *data) {
auto p = (ClipboardImpl*)data;

if (source == 1 && *(p->Is_ClipboardShared)) {
SL_RAT_LOG(Utilities::Logging_Levels::INFO_log_level, "Clipboard Changed!");
if (Fl::clipboard_contains(Fl::clipboard_plain_text)) {
SL_RAT_LOG(Utilities::Logging_Levels::INFO_log_level, "Contains plain text");
Fl::paste(*p, 1, Fl::clipboard_plain_text);
}
}
}
//caller should lock the mutex to prevent races
bool is_datasame(const char* data, int len) {
auto size = static_cast<size_t>(len);
if (size != LastClipData.size()) return false;
if (memcmp(LastClipData.data(), data, size) == 0) return true;
return false;
}
//caller should lock the mutex to prevent races
void UpdateBuffer(const char* data, int len) {
auto size = static_cast<size_t>(len);
LastClipData.resize(size);
memcpy(LastClipData.data(), data, size);
}
void copy_to_clipboard(const char* data, int len) {
bool updateclipboard = false;
{
std::lock_guard<std::mutex> lock(LastClipDataLock);
updateclipboard = !is_datasame(data, len);
if (updateclipboard) UpdateBuffer(data, len);
}
if (updateclipboard) Fl::copy(data, static_cast<int>(len), 1);
}
};
}
}
}
SL::Remote_Access_Library::Capturing::Clipboard::Clipboard(const bool* is_clipshared, std::function<void(const char*, int)>&& onchange)
{
_ClipboardImpl = new ClipboardImpl(is_clipshared, std::forward<std::function<void(const char*, int)>>(onchange));
}

SL::Remote_Access_Library::Capturing::Clipboard::~Clipboard()
{
delete _ClipboardImpl;
}
void SL::Remote_Access_Library::Capturing::Clipboard::copy_to_clipboard(const char* data, int len) {
_ClipboardImpl->copy_to_clipboard(data, len);
}
19 changes: 19 additions & 0 deletions Core/Clipboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <functional>


namespace SL {
namespace Remote_Access_Library {
namespace Capturing {
class ClipboardImpl;
class Clipboard {
ClipboardImpl* _ClipboardImpl = nullptr;
public:
//user must guarantee that is_clipshared is valid for the lifetime of this clipboard object!
Clipboard(const bool* is_clipshared, std::function<void(const char*, int)>&& onchange);
~Clipboard();
void copy_to_clipboard(const char* data, int len);
};
}
}
}
Loading

0 comments on commit 082133d

Please sign in to comment.