grpc-proxy/shell: Add width for split window
[src/agl-compositor.git] / grpc-proxy / grpc-async-cb.cpp
1 /*
2  * Copyright © 2022, 2023 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <cstdio>
27 #include <ctime>
28 #include <algorithm>
29 #include <queue>
30
31 #define GRPC_CALLBACK_API_NONEXPERIMENTAL
32
33 #include <grpc/grpc.h>
34 #include <grpcpp/grpcpp.h>
35 #include <grpcpp/server.h>
36 #include <grpcpp/server_builder.h>
37 #include <grpcpp/server_context.h>
38
39 #include <grpcpp/ext/proto_server_reflection_plugin.h>
40 #include <grpcpp/health_check_service_interface.h>
41
42 #include "log.h"
43 #include "agl_shell.grpc.pb.h"
44 #include "grpc-async-cb.h"
45
46 Lister::Lister(Shell *shell) : m_shell(shell)
47 {
48         // don't call NextWrite() just yet we do it explicitly when getting
49         // the events from the compositor
50         m_writting = false;
51 }
52
53 void
54 Lister::OnDone()
55 {
56         delete this;
57 }
58
59 void Lister::OnWriteDone(bool ok)
60 {
61         LOG("got ok %d\n", ok);
62         if (ok) {
63                 LOG("done writting %d\n", m_writting);
64                 m_writting = false;
65         }
66 }
67
68 void
69 Lister::NextWrite(void)
70 {
71         if (m_writting) {
72                 LOG(">>>>> still in writting\n");
73                 return;
74         }
75         m_writting = true;
76         StartWrite(&m_shell->m_shell_data->current_app_state);
77 }
78
79 bool
80 Lister::Writting(void)
81 {
82         return m_writting;
83 }
84
85 grpc::ServerUnaryReactor *
86 GrpcServiceImpl::ActivateApp(grpc::CallbackServerContext *context,
87                             const ::agl_shell_ipc::ActivateRequest* request,
88                             ::agl_shell_ipc::ActivateResponse* /*response*/)
89 {
90         LOG("activating app %s on output %s\n", request->app_id().c_str(),
91                                                 request->output_name().c_str());
92         if (!m_aglShell) {
93                 LOG("m_aglShell not set-up\n");
94                 return nullptr;
95         }
96
97         m_aglShell->ActivateApp(request->app_id(), request->output_name());
98
99         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
100         reactor->Finish(grpc::Status::OK);
101         return reactor;
102 }
103
104 grpc::ServerUnaryReactor *
105 GrpcServiceImpl::DeactivateApp(grpc::CallbackServerContext *context,
106                               const ::agl_shell_ipc::DeactivateRequest* request,
107                               ::agl_shell_ipc::DeactivateResponse* /*response*/)
108 {
109         if (!m_aglShell) {
110                 LOG("m_aglShell not set-up\n");
111                 return nullptr;
112         }
113
114         m_aglShell->DeactivateApp(request->app_id());
115
116         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
117         reactor->Finish(grpc::Status::OK);
118         return reactor;
119 }
120
121 grpc::ServerUnaryReactor *
122 GrpcServiceImpl::SetAppFloat(grpc::CallbackServerContext *context,
123                             const ::agl_shell_ipc::FloatRequest* request,
124                             ::agl_shell_ipc::FloatResponse* /* response */)
125 {
126         if (!m_aglShell) {
127                 LOG("m_aglShell not set-up\n");
128                 return nullptr;
129         }
130         m_aglShell->SetAppFloat(request->app_id(),
131                                 request->x_pos(), request->y_pos());
132
133         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
134         reactor->Finish(grpc::Status::OK);
135         return reactor;
136 }
137
138 grpc::ServerUnaryReactor *
139 GrpcServiceImpl::SetAppNormal(grpc::CallbackServerContext *context,
140                             const ::agl_shell_ipc::NormalRequest* request,
141                             ::agl_shell_ipc::NormalResponse* /* response */)
142 {
143         if (!m_aglShell) {
144                 LOG("m_aglShell not set-up\n");
145                 return nullptr;
146         }
147         m_aglShell->SetAppNormal(request->app_id());
148
149         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
150         reactor->Finish(grpc::Status::OK);
151         return reactor;
152 }
153
154 grpc::ServerUnaryReactor *
155 GrpcServiceImpl::SetAppFullscreen(grpc::CallbackServerContext *context,
156                             const ::agl_shell_ipc::FullscreenRequest* request,
157                             ::agl_shell_ipc::FullscreenResponse* /* response */)
158 {
159         if (!m_aglShell) {
160                 LOG("m_aglShell not set-up\n");
161                 return nullptr;
162         }
163         m_aglShell->SetAppFullscreen(request->app_id());
164
165         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
166         reactor->Finish(grpc::Status::OK);
167         return reactor;
168 }
169
170 grpc::ServerUnaryReactor *
171 GrpcServiceImpl::SetAppOnOutput(grpc::CallbackServerContext *context,
172                             const ::agl_shell_ipc::AppOnOutputRequest* request,
173                             ::agl_shell_ipc::AppOnOutputResponse* /* response */)
174 {
175         if (!m_aglShell) {
176                 LOG("m_aglShell not set-up\n");
177                 return nullptr;
178         }
179         m_aglShell->SetAppOnOutput(request->app_id(), request->output());
180
181         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
182         reactor->Finish(grpc::Status::OK);
183         return reactor;
184 }
185
186 grpc::ServerUnaryReactor *
187 GrpcServiceImpl::SetAppPosition(grpc::CallbackServerContext *context,
188                             const ::agl_shell_ipc::AppPositionRequest* request,
189                             ::agl_shell_ipc::AppPositionResponse* /* response */)
190 {
191         if (!m_aglShell) {
192                 LOG("m_aglShell not set-up\n");
193                 return nullptr;
194         }
195         m_aglShell->SetAppPosition(request->app_id(), request->x(), request->y());
196
197         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
198         reactor->Finish(grpc::Status::OK);
199         return reactor;
200 }
201
202 grpc::ServerUnaryReactor *
203 GrpcServiceImpl::SetAppScale(grpc::CallbackServerContext *context,
204                             const ::agl_shell_ipc::AppScaleRequest* request,
205                             ::agl_shell_ipc::AppScaleResponse* /* response */)
206 {
207         if (!m_aglShell) {
208                 LOG("m_aglShell not set-up\n");
209                 return nullptr;
210         }
211         m_aglShell->SetAppScale(request->app_id(),
212                                 request->width(), request->height());
213
214         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
215         reactor->Finish(grpc::Status::OK);
216         return reactor;
217 }
218
219 grpc::ServerUnaryReactor *
220 GrpcServiceImpl::SetAppSplit(grpc::CallbackServerContext *context,
221            const ::agl_shell_ipc::SplitRequest* request,
222            ::agl_shell_ipc::SplitResponse* /*response*/)
223 {
224         if (!m_aglShell) {
225                 LOG("m_aglShell not set-up\n");
226                 return nullptr;
227         }
228         m_aglShell->SetAppSplit(request->app_id(), request->tile_orientation(), request->width(), request->output_name());
229
230         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
231         reactor->Finish(grpc::Status::OK);
232         return reactor;
233 }
234
235 grpc::ServerUnaryReactor *
236 GrpcServiceImpl::GetOutputs(grpc::CallbackServerContext *context,
237            const ::agl_shell_ipc::OutputRequest* /* request */,
238            ::agl_shell_ipc::ListOutputResponse* response)
239 {
240         struct window_output *output;
241
242         if (!m_aglShell) {
243                 LOG("m_aglShell not set-up\n");
244                 return nullptr;
245         }
246
247         struct wl_list *list = &m_aglShell->m_shell_data->output_list;
248         wl_list_for_each(output, list, link) {
249                 auto m_output = response->add_outputs();
250                 m_output->set_name(output->name);
251         }
252
253         grpc::ServerUnaryReactor* reactor = context->DefaultReactor();
254         reactor->Finish(grpc::Status::OK);
255         return reactor;
256 }
257
258 grpc::ServerWriteReactor<::agl_shell_ipc::AppStateResponse>*
259 GrpcServiceImpl::AppStatusState(grpc::CallbackServerContext* context,
260                                  const ::agl_shell_ipc::AppStateRequest* /*request */)
261 {
262
263
264         if (!m_aglShell) {
265                 LOG(">> m_aglShell not set-up\n");
266                 return nullptr;
267         }
268
269         Lister *n = new Lister(m_aglShell);
270         m_aglShell->m_shell_data->server_context_list.push_back(std::pair(context, n));
271         LOG("added lister %p\n", static_cast<void *>(n));
272
273         // just return a Lister to keep the channel open
274         return n;
275 }