| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 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: | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 13 | VirtualTouchpadClientImpl() {} | 
|  | 14 | ~VirtualTouchpadClientImpl() override { | 
|  | 15 | if (service_ != nullptr) { | 
|  | 16 | Detach(); | 
|  | 17 | } | 
|  | 18 | } | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 19 |  | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 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 |  | 
|  | 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 Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 50 | if (service_ == nullptr) { | 
|  | 51 | return NO_INIT; | 
|  | 52 | } | 
| Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 53 | return service_->touch(touchpad, x, y, pressure).transactionError(); | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 54 | } | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 55 |  | 
| Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 56 | status_t ButtonState(int touchpad, int buttons) override { | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 57 | if (service_ == nullptr) { | 
|  | 58 | return NO_INIT; | 
|  | 59 | } | 
| Kevin Schoedel | 3002b8a | 2017-03-06 14:34:39 -0500 | [diff] [blame] | 60 | return service_->buttonState(touchpad, buttons).transactionError(); | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
| Kevin Schoedel | d8fccf0 | 2017-06-05 11:13:20 -0400 | [diff] [blame] | 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 |  | 
| Kevin Schoedel | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 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 |  | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 76 | private: | 
|  | 77 | sp<IVirtualTouchpadService> service_; | 
|  | 78 | }; | 
|  | 79 |  | 
|  | 80 | }  // anonymous namespace | 
|  | 81 |  | 
| Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame] | 82 | std::unique_ptr<VirtualTouchpad> VirtualTouchpadClient::Create() { | 
|  | 83 | return std::unique_ptr<VirtualTouchpad>(new VirtualTouchpadClientImpl()); | 
| Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | }  // namespace dvr | 
|  | 87 | }  // namespace android |