Home | History | Annotate | Download | only in virtual_touchpad
      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:
     13   VirtualTouchpadClientImpl() {}
     14   ~VirtualTouchpadClientImpl() override {
     15     if (service_ != nullptr) {
     16       Detach();
     17     }
     18   }
     19 
     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 {
     50     if (service_ == nullptr) {
     51       return NO_INIT;
     52     }
     53     return service_->touch(touchpad, x, y, pressure).transactionError();
     54   }
     55 
     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 
     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 
     69  private:
     70   sp<IVirtualTouchpadService> service_;
     71 };
     72 
     73 }  // anonymous namespace
     74 
     75 std::unique_ptr<VirtualTouchpad> VirtualTouchpadClient::Create() {
     76   return std::unique_ptr<VirtualTouchpad>(new VirtualTouchpadClientImpl());
     77 }
     78 
     79 }  // namespace dvr
     80 }  // namespace android
     81