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 | 4b64dd4 | 2017-03-07 13:06:25 -0500 | [diff] [blame] | 63 | 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 Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 69 | private: |
| 70 | sp<IVirtualTouchpadService> service_; |
| 71 | }; |
| 72 | |
| 73 | } // anonymous namespace |
| 74 | |
Kevin Schoedel | de1cdae | 2017-03-17 11:07:06 -0400 | [diff] [blame^] | 75 | std::unique_ptr<VirtualTouchpad> VirtualTouchpadClient::Create() { |
| 76 | return std::unique_ptr<VirtualTouchpad>(new VirtualTouchpadClientImpl()); |
Kevin Schoedel | 89af70b | 2017-03-03 18:11:37 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace dvr |
| 80 | } // namespace android |