1 #include "VirtualTouchpadClient.h"
2 
3 #include <android/dvr/IVirtualTouchpadService.h>
4 #include <binder/IServiceManager.h>
5 
6 namespace android {
7 namespace dvr {
8 
9 namespace {
10 
11 class VirtualTouchpadClientImpl : public VirtualTouchpadClient {
12  public:
VirtualTouchpadClientImpl()13   VirtualTouchpadClientImpl() {}
~VirtualTouchpadClientImpl()14   ~VirtualTouchpadClientImpl() override {
15     if (service_ != nullptr) {
16       Detach();
17     }
18   }
19 
Attach()20   status_t Attach() {
21     if (service_ != nullptr) {
22       return ALREADY_EXISTS;
23     }
24     sp<IServiceManager> sm = defaultServiceManager();
25     if (sm == nullptr) {
26       ALOGE("no service manager");
27       return NO_INIT;
28     }
29     sp<IVirtualTouchpadService> service =
30         interface_cast<IVirtualTouchpadService>(
31             sm->getService(IVirtualTouchpadService::SERVICE_NAME()));
32     if (service == nullptr) {
33       ALOGE("failed to get service");
34       return NAME_NOT_FOUND;
35     }
36     service_ = service;
37     return service_->attach().transactionError();
38   }
39 
Detach()40   status_t Detach() {
41     if (service_ == nullptr) {
42       return NO_INIT;
43     }
44     status_t status = service_->detach().transactionError();
45     service_ = nullptr;
46     return status;
47   }
48 
Touch(int touchpad,float x,float y,float pressure)49   status_t Touch(int touchpad, float x, float y, float pressure) override {
50     if (service_ == nullptr) {
51       return NO_INIT;
52     }
53     return service_->touch(touchpad, x, y, pressure).transactionError();
54   }
55 
ButtonState(int touchpad,int buttons)56   status_t ButtonState(int touchpad, int buttons) override {
57     if (service_ == nullptr) {
58       return NO_INIT;
59     }
60     return service_->buttonState(touchpad, buttons).transactionError();
61   }
62 
Scroll(int touchpad,float x,float y)63   status_t Scroll(int touchpad, float x, float y) override {
64     if (service_ == nullptr) {
65       return NO_INIT;
66     }
67     return service_->scroll(touchpad, x, y).transactionError();
68   }
69 
dumpInternal(String8 & result)70   void dumpInternal(String8& result) override {
71     result.append("[virtual touchpad]\n");
72     result.appendFormat("connected = %s\n\n",
73                         service_ != nullptr ? "true" : "false");
74   }
75 
76  private:
77   sp<IVirtualTouchpadService> service_;
78 };
79 
80 }  // anonymous namespace
81 
Create()82 std::unique_ptr<VirtualTouchpad> VirtualTouchpadClient::Create() {
83   return std::unique_ptr<VirtualTouchpad>(new VirtualTouchpadClientImpl());
84 }
85 
86 }  // namespace dvr
87 }  // namespace android
88