grpc-proxy: Start the gRPC server and waiting thread much sooner
[src/agl-compositor.git] / grpc-proxy / main-grpc.cpp
index a59c282..2f9350a 100644 (file)
@@ -120,16 +120,31 @@ agl_shell_app_state(void *data, struct agl_shell *agl_shell,
        }
 }
 
+static void
+agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
+               const char *app_id, const char *output_name)
+{
+       (void) agl_shell;
+       (void) output_name;
+       (void) data;
+       (void) app_id;
+
+       LOG("got app_on_output event app_id %s on output %s\n", app_id, output_name);
+}
+
+
 static const struct agl_shell_listener shell_listener = {
-        agl_shell_bound_ok,
-        agl_shell_bound_fail,
-        agl_shell_app_state,
+       agl_shell_bound_ok,
+       agl_shell_bound_fail,
+       agl_shell_app_state,
+       agl_shell_app_on_output,
 };
 
 static const struct agl_shell_listener shell_listener_init = {
-        agl_shell_bound_ok_init,
-        agl_shell_bound_fail_init,
-        nullptr,
+       agl_shell_bound_ok_init,
+       agl_shell_bound_fail_init,
+       nullptr,
+       nullptr,
 };
 
 static void
@@ -260,7 +275,7 @@ global_add(void *data, struct wl_registry *reg, uint32_t id,
                sh->shell =
                        static_cast<struct agl_shell *>(wl_registry_bind(reg, id,
                                &agl_shell_interface,
-                               std::min(static_cast<uint32_t>(3), version)));
+                               std::min(static_cast<uint32_t>(10), version)));
                agl_shell_add_listener(sh->shell, &shell_listener, data);
                sh->version = version;
        } else if (strcmp(interface, "wl_output") == 0) {
@@ -284,7 +299,7 @@ global_add_init(void *data, struct wl_registry *reg, uint32_t id,
                sh->shell =
                        static_cast<struct agl_shell *>(wl_registry_bind(reg, id,
                                &agl_shell_interface,
-                               std::min(static_cast<uint32_t>(3), version)));
+                               std::min(static_cast<uint32_t>(10), version)));
                agl_shell_add_listener(sh->shell, &shell_listener_init, data);
                sh->version = version;
        }
@@ -371,6 +386,10 @@ __register_shell_init(void)
        struct shell_data_init *sh = new struct shell_data_init;
 
        wl_display = wl_display_connect(NULL);
+       if (!wl_display) {
+               ret = -1;
+               goto err_failed_display;
+       }
        registry = wl_display_get_registry(wl_display);
        sh->wait_for_bound = true;
        sh->bound_fail = false;
@@ -398,6 +417,8 @@ __register_shell_init(void)
 err:
        wl_registry_destroy(registry);
        wl_display_disconnect(wl_display);
+
+err_failed_display:
        delete sh;
        return ret;
 }
@@ -465,6 +486,10 @@ start_agl_shell_client(void)
 
        struct shell_data *sh = new struct shell_data;
 
+       if (!wl_display) {
+               goto err;
+       }
+
        sh->wl_display = wl_display;
        sh->wait_for_doas = true;
        sh->wait_for_bound = true;
@@ -517,8 +542,19 @@ err:
 }
 
 static void
-start_grpc_server(Shell *aglShell)
+start_grpc_server(std::shared_ptr<grpc::Server> server)
+{
+       LOG("gRPC server listening\n");
+       server->Wait();
+}
+
+int main(int argc, char **argv)
 {
+       (void) argc;
+       (void) argv;
+       Shell *aglShell = nullptr;
+       int ret = 0;
+
        // instantiante the grpc server
        std::string server_address(kDefaultGrpcServiceAddress);
        GrpcServiceImpl service{aglShell};
@@ -530,18 +566,8 @@ start_grpc_server(Shell *aglShell)
        builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
        builder.RegisterService(&service);
 
-       std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
-       LOG("gRPC server listening on %s\n", server_address.c_str());
-
-       server->Wait();
-}
-
-int main(int argc, char **argv)
-{
-       (void) argc;
-       (void) argv;
-       Shell *aglShell;
-       int ret = 0;
+       std::shared_ptr<grpc::Server> server(builder.BuildAndStart());
+       std::thread thread(start_grpc_server, server);
 
        // this blocks until we detect that another shell client started
        // running
@@ -556,13 +582,15 @@ int main(int argc, char **argv)
        std::shared_ptr<struct agl_shell> agl_shell{sh->shell, agl_shell_destroy};
        aglShell = new Shell(agl_shell, sh);
 
-       std::thread thread(start_grpc_server, aglShell);
+       // now that we have aglShell, set it to the gRPC proxy as well
+       service.setAglShell(aglShell);
 
        // serve wayland requests
        while (running && ret != -1) {
                ret = wl_display_dispatch(sh->wl_display);
        }
 
+       thread.join();
        destroy_shell_data(sh);
        return 0;
 }