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/process.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 5 deletions(-) (limited to 'src/utils/process.cpp') 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 -- cgit v1.2.3-70-g09d2