ff190f7688441bee8a6c26787732a8fd90b13b0c
[apps/homescreen.git] / homescreen / src / AglShellGrpcClient.h
1 #pragma once
2
3 #include <cstdio>
4
5 #include <mutex>
6 #include <condition_variable>
7 #include <grpc/grpc.h>
8 #include <grpcpp/grpcpp.h>
9 #include <grpcpp/server.h>
10 #include <grpcpp/server_builder.h>
11 #include <grpcpp/server_context.h>
12
13 #include <grpcpp/ext/proto_server_reflection_plugin.h>
14 #include <grpcpp/health_check_service_interface.h>
15
16 #include "agl_shell.grpc.pb.h"
17
18 typedef void (*Callback)(agl_shell_ipc::AppStateResponse app_response);
19
20 class Reader : public grpc::ClientReadReactor<::agl_shell_ipc::AppStateResponse> {
21 public:
22         Reader(agl_shell_ipc::AglShellManagerService::Stub *stub)
23                 : m_stub(stub)
24         {
25         }
26
27         void AppStatusState(Callback callback)
28         {
29                 ::agl_shell_ipc::AppStateRequest request;
30
31                 // set up the callback
32                 m_callback = callback;
33                 m_stub->async()->AppStatusState(&m_context, &request, this);
34
35                 StartRead(&m_app_state);
36                 StartCall();
37         }
38
39         void OnReadDone(bool ok) override
40         {
41                 if (ok) {
42                         m_callback(m_app_state);
43
44                         // blocks in StartRead() if the server doesn't send
45                         // antyhing
46                         StartRead(&m_app_state);
47                 }
48         }
49
50         void SetDone()
51         {
52                 fprintf(stderr, "%s()\n", __func__);
53                 std::unique_lock<std::mutex> l(m_mutex);
54                 m_done = true;
55         }
56
57         void OnDone(const grpc::Status& s) override
58         {
59                 fprintf(stderr, "%s()\n", __func__);
60                 std::unique_lock<std::mutex> l(m_mutex);
61
62                 m_status = s;
63
64                 fprintf(stderr, "%s() done\n", __func__);
65                 m_cv.notify_one();
66         }
67
68         grpc::Status Await()
69         {
70                 std::unique_lock<std::mutex> l(m_mutex);
71
72                 m_cv.wait(l, [this] { return m_done; });
73
74                 return std::move(m_status);
75         }
76 private:
77         grpc::ClientContext m_context;
78         ::agl_shell_ipc::AppStateResponse m_app_state;
79         agl_shell_ipc::AglShellManagerService::Stub *m_stub;
80
81         Callback m_callback;
82
83
84         std::mutex m_mutex;
85         std::condition_variable m_cv;
86         grpc::Status m_status;
87         bool m_done = false;
88 };
89
90 class GrpcClient {
91 public:
92         GrpcClient();
93         bool ActivateApp(const std::string& app_id, const std::string& output_name);
94         bool DeactivateApp(const std::string& app_id);
95         bool SetAppFloat(const std::string& app_id, int32_t x_pos, int32_t y_pos);
96         bool SetAppFullscreen(const std::string& app_id);
97         bool SetAppOnOutput(const std::string& app_id, const std::string& output);
98         bool SetAppNormal(const std::string& app_id);
99         bool SetAppPosition(const std::string& app_id, int32_t x, int32_t y);
100         bool SetAppScale(const std::string& app_id, int32_t width, int32_t height);
101         std::vector<std::string> GetOutputs();
102         void GetAppState();
103         void AppStatusState(Callback callback);
104         grpc::Status Wait();
105
106 private:
107         Reader *reader;
108         std::unique_ptr<agl_shell_ipc::AglShellManagerService::Stub> m_stub;
109 };
110