ManaPlus
naclmessages.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2015-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifdef __native_client__
23 
24 #include "utils/naclmessages.h"
25 
26 #include <ppapi_simple/ps.h>
27 #include <ppapi_simple/ps_event.h>
28 #include <ppapi/cpp/instance.h>
29 #include <ppapi/cpp/var.h>
30 
31 #include <mutex>
32 #include <condition_variable>
33 
34 #include "debug.h"
35 
36 struct NaclMessageHandle final
37 {
38  NaclMessageHandle() :
39  handled(false),
40  type(),
41  message(),
42  condv()
43  { }
44 
45  A_DELETE_COPY(NaclMessageHandle)
46 
47  bool handled;
48  std::string type;
49  std::string message;
50  std::condition_variable condv;
51 };
52 
53 void naclPostMessage(const std::string &type,
54  const std::string &message)
55 {
56  pp::Var msgVar = pp::Var(std::string(type).append(":").append(message));
57  pp::Instance(PSGetInstanceId()).PostMessage(msgVar);
58 }
59 
60 static void naclMessageHandlerFunc(struct PP_Var key,
61  struct PP_Var value,
62  void* user_data)
63 {
64  NaclMessageHandle *handle = reinterpret_cast<NaclMessageHandle *>(
65  user_data);
66 
67  if (key.type != PP_VARTYPE_STRING || value.type != PP_VARTYPE_STRING)
68  return;
69  if (pp::Var(key).AsString() != handle->type)
70  return;
71 
72  handle->handled = true;
73  handle->message = pp::Var(value).AsString();
74 
75  handle->condv.notify_one();
76 }
77 
78 NaclMessageHandle *naclRegisterMessageHandler(const std::string &type)
79 {
80  NaclMessageHandle *handle = new NaclMessageHandle;
81  handle->handled = false;
82  handle->type = type;
83 
84  PSEventRegisterMessageHandler(type.c_str(),
85  naclMessageHandlerFunc,
86  reinterpret_cast<void *>(handle));
87  return handle;
88 }
89 
90 void naclUnregisterMessageHandler(NaclMessageHandle *handle)
91 {
92  PSEventRegisterMessageHandler(handle->type.c_str(),
93  nullptr,
94  nullptr);
95  delete handle;
96 }
97 
98 std::string naclWaitForMessage(NaclMessageHandle *handle)
99 {
100  std::mutex mtx;
101  std::unique_lock <std::mutex> lck(mtx);
102 
103  while (!handle->handled)
104  handle->condv.wait(lck);
105 
106  handle->handled = false;
107  return handle->message;
108 }
109 
110 #endif // __native_client__
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53