2 * Copyright © 2022 Collabora, Ltd.
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:
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.
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
32 #include <condition_variable>
36 #include "main-grpc.h"
37 #include "grpc-async-cb.h"
39 struct shell_data_init {
40 struct agl_shell *shell;
47 static int running = 1;
50 agl_shell_bound_ok_init(void *data, struct agl_shell *agl_shell)
54 struct shell_data_init *sh = static_cast<struct shell_data_init *>(data);
55 sh->wait_for_bound = false;
61 agl_shell_bound_fail_init(void *data, struct agl_shell *agl_shell)
65 struct shell_data_init *sh = static_cast<struct shell_data_init *>(data);
66 sh->wait_for_bound = false;
68 sh->bound_fail = true;
72 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
76 struct shell_data *sh = static_cast<struct shell_data *>(data);
77 sh->wait_for_bound = false;
83 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
87 struct shell_data *sh = static_cast<struct shell_data *>(data);
88 sh->wait_for_bound = false;
94 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
95 const char *app_id, uint32_t state)
98 struct shell_data *sh = static_cast<struct shell_data *>(data);
99 LOG("got app_state event app_id %s, state %d\n", app_id, state);
101 if (sh->server_context_list.empty())
104 ::agl_shell_ipc::AppStateResponse app;
106 sh->current_app_state.set_app_id(std::string(app_id));
107 sh->current_app_state.set_state(state);
109 auto start = sh->server_context_list.begin();
110 while (start != sh->server_context_list.end()) {
111 // hold on if we're still detecting another in-flight writting
112 if (start->second->Writting()) {
113 LOG("skip writing to lister %p\n", static_cast<void *>(start->second));
117 LOG("writing to lister %p\n", static_cast<void *>(start->second));
118 start->second->NextWrite();
123 static const struct agl_shell_listener shell_listener = {
125 agl_shell_bound_fail,
129 static const struct agl_shell_listener shell_listener_init = {
130 agl_shell_bound_ok_init,
131 agl_shell_bound_fail_init,
136 agl_shell_ext_doas_done(void *data, struct agl_shell_ext *agl_shell_ext, uint32_t status)
138 (void) agl_shell_ext;
140 struct shell_data *sh = static_cast<struct shell_data *>(data);
141 sh->wait_for_doas = false;
143 if (status == AGL_SHELL_EXT_DOAS_SHELL_CLIENT_STATUS_SUCCESS)
147 static const struct agl_shell_ext_listener shell_ext_listener = {
148 agl_shell_ext_doas_done,
152 display_handle_geometry(void *data, struct wl_output *wl_output,
153 int x, int y, int physical_width, int physical_height,
154 int subpixel, const char *make, const char *model, int transform)
160 (void) physical_width;
161 (void) physical_height;
169 display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
170 int width, int height, int refresh)
181 display_handle_done(void *data, struct wl_output *wl_output)
188 display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor)
197 display_handle_name(void *data, struct wl_output *wl_output, const char *name)
201 struct window_output *woutput = static_cast<struct window_output *>(data);
202 woutput->name = strdup(name);
206 display_handle_description(void *data, struct wl_output *wl_output, const char *description)
213 static const struct wl_output_listener output_listener = {
214 display_handle_geometry,
217 display_handle_scale,
219 display_handle_description,
223 display_add_output(struct shell_data *sh, struct wl_registry *reg,
224 uint32_t id, uint32_t version)
226 struct window_output *w_output;
228 w_output = new struct window_output;
229 w_output->shell_data = sh;
232 static_cast<struct wl_output *>(wl_registry_bind(reg, id,
233 &wl_output_interface,
234 std::min(version, static_cast<uint32_t>(4))));
236 wl_list_insert(&sh->output_list, &w_output->link);
237 wl_output_add_listener(w_output->output, &output_listener, w_output);
241 destroy_output(struct window_output *w_output)
243 free(w_output->name);
244 wl_list_remove(&w_output->link);
249 global_add(void *data, struct wl_registry *reg, uint32_t id,
250 const char *interface, uint32_t version)
253 struct shell_data *sh = static_cast<struct shell_data *>(data);
258 if (strcmp(interface, agl_shell_interface.name) == 0) {
259 // bind to at least v3 to get events
261 static_cast<struct agl_shell *>(wl_registry_bind(reg, id,
262 &agl_shell_interface,
263 std::min(static_cast<uint32_t>(3), version)));
264 agl_shell_add_listener(sh->shell, &shell_listener, data);
265 sh->version = version;
266 } else if (strcmp(interface, "wl_output") == 0) {
267 display_add_output(sh, reg, id, version);
271 // the purpose of this _init is to make sure we're not the first shell client
272 // running to allow the 'main' shell client take over.
274 global_add_init(void *data, struct wl_registry *reg, uint32_t id,
275 const char *interface, uint32_t version)
278 struct shell_data_init *sh = static_cast<struct shell_data_init *>(data);
283 if (strcmp(interface, agl_shell_interface.name) == 0) {
285 static_cast<struct agl_shell *>(wl_registry_bind(reg, id,
286 &agl_shell_interface,
287 std::min(static_cast<uint32_t>(3), version)));
288 agl_shell_add_listener(sh->shell, &shell_listener_init, data);
289 sh->version = version;
294 global_remove(void *data, struct wl_registry *reg, uint32_t id)
303 global_add_ext(void *data, struct wl_registry *reg, uint32_t id,
304 const char *interface, uint32_t version)
306 struct shell_data *sh = static_cast<struct shell_data *>(data);
311 if (strcmp(interface, agl_shell_ext_interface.name) == 0) {
313 static_cast<struct agl_shell_ext *>(wl_registry_bind(reg, id,
314 &agl_shell_ext_interface,
315 std::min(static_cast<uint32_t>(1), version)));
316 agl_shell_ext_add_listener(sh->shell_ext,
317 &shell_ext_listener, data);
321 static const struct wl_registry_listener registry_ext_listener = {
326 static const struct wl_registry_listener registry_listener = {
331 static const struct wl_registry_listener registry_listener_init = {
337 register_shell_ext(struct wl_display *wl_display, struct shell_data *sh)
339 struct wl_registry *registry;
341 registry = wl_display_get_registry(wl_display);
343 wl_registry_add_listener(registry, ®istry_ext_listener, sh);
345 wl_display_roundtrip(wl_display);
346 wl_registry_destroy(registry);
350 register_shell(struct wl_display *wl_display, struct shell_data *sh)
352 struct wl_registry *registry;
354 wl_list_init(&sh->output_list);
356 registry = wl_display_get_registry(wl_display);
358 wl_registry_add_listener(registry, ®istry_listener, sh);
360 wl_display_roundtrip(wl_display);
361 wl_registry_destroy(registry);
365 __register_shell_init(void)
368 struct wl_registry *registry;
369 struct wl_display *wl_display;
371 struct shell_data_init *sh = new struct shell_data_init;
373 wl_display = wl_display_connect(NULL);
374 registry = wl_display_get_registry(wl_display);
375 sh->wait_for_bound = true;
376 sh->bound_fail = false;
377 sh->bound_ok = false;
379 wl_registry_add_listener(registry, ®istry_listener_init, sh);
380 wl_display_roundtrip(wl_display);
382 if (!sh->shell || sh->version < 3) {
387 while (ret !=- 1 && sh->wait_for_bound) {
388 ret = wl_display_dispatch(wl_display);
390 if (sh->wait_for_bound)
394 ret = sh->bound_fail;
396 agl_shell_destroy(sh->shell);
397 wl_display_flush(wl_display);
399 wl_registry_destroy(registry);
400 wl_display_disconnect(wl_display);
405 // we expect this client to be up & running *after* the shell client has
406 // already set-up panels/backgrounds.
407 // this means the very first try to bind to agl_shell we wait for
408 // 'bound_fail' event, which would tell us when it's ok to attempt to
409 // bind agl_shell_ext, call doas request, then attempt to bind (one
410 // more time) to agl_shell but this time wait for 'bound_ok' event.
412 register_shell_init(void)
414 struct timespec ts = {};
416 clock_gettime(CLOCK_MONOTONIC, &ts);
419 ts.tv_nsec = 250 * 1000 * 1000; // 250 ms
421 // verify if 'bound_fail' was received
424 int r = __register_shell_init();
427 LOG("agl-shell extension not found or version too low\n");
430 // we need to get a 'bound_fail' event, if we get a 'bound_ok'
431 // it means we're the first shell to start so wait until the
432 // shell client actually started
433 LOG("Found another shell client running. "
434 "Going further to bind to the agl_shell_ext interface\n");
438 LOG("No shell client detected running. Will wait until one starts up...\n");
439 nanosleep(&ts, NULL);
445 destroy_shell_data(struct shell_data *sh)
447 struct window_output *w_output, *w_output_next;
449 wl_list_for_each_safe(w_output, w_output_next, &sh->output_list, link)
450 destroy_output(w_output);
452 wl_display_flush(sh->wl_display);
453 wl_display_disconnect(sh->wl_display);
458 static struct shell_data *
459 start_agl_shell_client(void)
462 struct wl_display *wl_display;
464 wl_display = wl_display_connect(NULL);
466 struct shell_data *sh = new struct shell_data;
468 sh->wl_display = wl_display;
469 sh->wait_for_doas = true;
470 sh->wait_for_bound = true;
472 register_shell_ext(wl_display, sh);
474 // check for agl_shell_ext
475 if (!sh->shell_ext) {
476 LOG("Failed to bind to agl_shell_ext interface\n");
480 if (wl_list_empty(&sh->output_list)) {
481 LOG("Failed get any outputs!\n");
485 agl_shell_ext_doas_shell_client(sh->shell_ext);
486 while (ret != -1 && sh->wait_for_doas) {
487 ret = wl_display_dispatch(sh->wl_display);
488 if (sh->wait_for_doas)
493 LOG("Failed to get doas_done event\n");
498 register_shell(wl_display, sh);
499 while (ret != -1 && sh->wait_for_bound) {
500 ret = wl_display_dispatch(sh->wl_display);
501 if (sh->wait_for_bound)
505 // at this point, we can't do anything about it
507 LOG("Failed to get bound_ok event!\n");
511 LOG("agl_shell/agl_shell_ext interface OK\n");
520 start_grpc_server(Shell *aglShell)
522 // instantiante the grpc server
523 std::string server_address(kDefaultGrpcServiceAddress);
524 GrpcServiceImpl service{aglShell};
526 grpc::EnableDefaultHealthCheckService(true);
527 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
529 grpc::ServerBuilder builder;
530 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
531 builder.RegisterService(&service);
533 std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
534 LOG("gRPC server listening on %s\n", server_address.c_str());
539 int main(int argc, char **argv)
546 // this blocks until we detect that another shell client started
548 register_shell_init();
550 struct shell_data *sh = start_agl_shell_client();
552 LOG("Failed to initialize agl-shell/agl-shell-ext\n");
556 std::shared_ptr<struct agl_shell> agl_shell{sh->shell, agl_shell_destroy};
557 aglShell = new Shell(agl_shell, sh);
559 std::thread thread(start_grpc_server, aglShell);
561 // serve wayland requests
562 while (running && ret != -1) {
563 ret = wl_display_dispatch(sh->wl_display);
566 destroy_shell_data(sh);