blob: c7c8184c5309edd1b6decc7a95f85cb7cfd27f91 [file] [log] [blame]
Kevin Schoedel89af70b2017-03-03 18:11:37 -05001#include "VirtualTouchpadClient.h"
2
3#include <android/dvr/IVirtualTouchpadService.h>
4#include <binder/IServiceManager.h>
5
6namespace android {
7namespace dvr {
8
9namespace {
10
11class VirtualTouchpadClientImpl : public VirtualTouchpadClient {
12 public:
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050013 VirtualTouchpadClientImpl() {}
14 ~VirtualTouchpadClientImpl() override {
15 if (service_ != nullptr) {
16 Detach();
17 }
18 }
Kevin Schoedel89af70b2017-03-03 18:11:37 -050019
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050020 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
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
49 status_t Touch(int touchpad, float x, float y, float pressure) override {
Kevin Schoedel89af70b2017-03-03 18:11:37 -050050 if (service_ == nullptr) {
51 return NO_INIT;
52 }
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050053 return service_->touch(touchpad, x, y, pressure).transactionError();
Kevin Schoedel89af70b2017-03-03 18:11:37 -050054 }
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050055
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050056 status_t ButtonState(int touchpad, int buttons) override {
Kevin Schoedel89af70b2017-03-03 18:11:37 -050057 if (service_ == nullptr) {
58 return NO_INIT;
59 }
Kevin Schoedel3002b8a2017-03-06 14:34:39 -050060 return service_->buttonState(touchpad, buttons).transactionError();
Kevin Schoedel89af70b2017-03-03 18:11:37 -050061 }
62
Kevin Schoedel4b64dd42017-03-07 13:06:25 -050063 void dumpInternal(String8& result) override {
64 result.append("[virtual touchpad]\n");
65 result.appendFormat("connected = %s\n\n",
66 service_ != nullptr ? "true" : "false");
67 }
68
Kevin Schoedel89af70b2017-03-03 18:11:37 -050069 private:
70 sp<IVirtualTouchpadService> service_;
71};
72
73} // anonymous namespace
74
Kevin Schoedelde1cdae2017-03-17 11:07:06 -040075std::unique_ptr<VirtualTouchpad> VirtualTouchpadClient::Create() {
76 return std::unique_ptr<VirtualTouchpad>(new VirtualTouchpadClientImpl());
Kevin Schoedel89af70b2017-03-03 18:11:37 -050077}
78
79} // namespace dvr
80} // namespace android