From d090aa77c4d0b31aded37128cae011b882f3dd9b Mon Sep 17 00:00:00 2001 From: Vasily Date: Thu, 3 Dec 2015 03:25:30 +0300 Subject: Added NaCl messaging and clipboard functions --- src/utils/copynpaste.cpp | 18 ++++++++++++ src/utils/process.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++---- src/utils/process.h | 12 ++++++++ 3 files changed, 99 insertions(+), 5 deletions(-) (limited to 'src/utils') diff --git a/src/utils/copynpaste.cpp b/src/utils/copynpaste.cpp index 1f88d8abf..45729021e 100644 --- a/src/utils/copynpaste.cpp +++ b/src/utils/copynpaste.cpp @@ -470,6 +470,24 @@ static bool runxsel(std::string& text, const char *p1, const char *p2) return true; } +#elif defined __native_client__ + +#include "utils/process.h" + +bool retrieveBuffer(std::string& text, size_t& pos) +{ + NaclMessageHandle *handle = naclRegisterMessageHandler("clipboard-paste"); + naclPostMessage("clipboard-paste", ""); + std::string response = naclWaitForMessage(handle); + text.insert(pos, response); + return true; +} + +bool sendBuffer(std::string& text) +{ + naclPostMessage("clipboard-copy", text); + return true; +} #else bool retrieveBuffer(std::string&, size_t&) { diff --git a/src/utils/process.cpp b/src/utils/process.cpp index 74ac20b53..d3d154270 100644 --- a/src/utils/process.cpp +++ b/src/utils/process.cpp @@ -269,13 +269,9 @@ bool openBrowser(std::string url) return execFile("/usr/bin/xdg-open", "/usr/bin/xdg-open", url, ""); } #elif defined __native_client__ -#include -#include -#include bool openBrowser(std::string url) { - pp::Var msgVar = pp::Var(std::string("open-browser: ").append(url)); - pp::Instance(PSGetInstanceId()).PostMessage(msgVar); + naclPostMessage("open-browser", url); return true; } #else @@ -300,3 +296,71 @@ void setPriority(const bool big A_UNUSED) { } #endif + +#ifdef __native_client__ +#include +#include +#include +#include + +#include +#include + +struct _NaclMessageHandle { + bool handled; + std::string type; + std::string message; + std::condition_variable condv; +}; + +void naclPostMessage(const std::string &type, const std::string &message) +{ + pp::Var msgVar = pp::Var(std::string(type).append(":").append(message)); + pp::Instance(PSGetInstanceId()).PostMessage(msgVar); +} + +void naclMessageHandlerFunc(struct PP_Var key, + struct PP_Var value, + void* user_data) +{ + NaclMessageHandle *handle = (NaclMessageHandle *)user_data; + + if (key.type != PP_VARTYPE_STRING || value.type != PP_VARTYPE_STRING) + return; + if (pp::Var(key).AsString() != handle->type) + return; + + handle->handled = true; + handle->message = pp::Var(value).AsString(); + + handle->condv.notify_one(); +} + +NaclMessageHandle *naclRegisterMessageHandler(const std::string &type) +{ + NaclMessageHandle *handle = new NaclMessageHandle; + handle->handled = false; + handle->type = type; + + PSEventRegisterMessageHandler(type.c_str(), naclMessageHandlerFunc, (void *)handle); + return handle; +} + +void naclUnregisterMessageHandler(NaclMessageHandle *handle) +{ + PSEventRegisterMessageHandler(handle->type.c_str(), NULL, NULL); + delete handle; +} + +std::string naclWaitForMessage(NaclMessageHandle *handle) +{ + std::mutex mtx; + std::unique_lock lck(mtx); + + while (!handle->handled) + handle->condv.wait(lck); + + handle->handled = false; + return handle->message; +} +#endif diff --git a/src/utils/process.h b/src/utils/process.h index 5ceddd032..44c1d2286 100644 --- a/src/utils/process.h +++ b/src/utils/process.h @@ -34,4 +34,16 @@ bool openBrowser(std::string url); void setPriority(const bool big); +#ifdef __native_client__ + +typedef struct _NaclMessageHandle NaclMessageHandle; + +void naclPostMessage(const std::string &type, const std::string &message); + +NaclMessageHandle *naclRegisterMessageHandler(const std::string &type); +void naclUnregisterMessageHandler(NaclMessageHandle *handle); +std::string naclWaitForMessage(NaclMessageHandle *handle); + +#endif + #endif // UTILS_PROCESS_H -- cgit v1.2.3-70-g09d2