• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <map>
20 #include <string>
21 #include <variant>
22 #include <vector>
23 
24 #include "include/gestures.h"
25 #include "input/PropertyMap.h"
26 
27 namespace android {
28 
29 // Struct containing functions that wrap PropertyProvider in a C-compatible interface.
30 extern const GesturesPropProvider gesturePropProvider;
31 
32 // Implementation of a gestures library property provider, which provides configuration parameters.
33 class PropertyProvider {
34 public:
35     bool hasProperty(const std::string& name) const;
36     GesturesProp& getProperty(const std::string& name);
37     std::string dump() const;
38 
39     void loadPropertiesFromIdcFile(const PropertyMap& idcProperties);
40 
41     // Methods to be called by the gestures library:
42     GesturesProp* createIntArrayProperty(const std::string& name, int* loc, size_t count,
43                                          const int* init);
44     GesturesProp* createBoolArrayProperty(const std::string& name, GesturesPropBool* loc,
45                                           size_t count, const GesturesPropBool* init);
46     GesturesProp* createRealArrayProperty(const std::string& name, double* loc, size_t count,
47                                           const double* init);
48     GesturesProp* createStringProperty(const std::string& name, const char** loc,
49                                        const char* const init);
50 
51     void freeProperty(GesturesProp* prop);
52 
53 private:
54     std::map<std::string, GesturesProp> mProperties;
55 };
56 
57 } // namespace android
58 
59 // Represents a single gesture property.
60 //
61 // Pointers to this struct will be used by the gestures library (though it can never deference
62 // them). The library's API requires this to be in the top-level namespace.
63 struct GesturesProp {
64 public:
65     template <typename T>
66     GesturesProp(std::string name, T* dataPointer, size_t count, const T* initialValues);
67     GesturesProp(std::string name, const char** dataPointer, const char* const initialValue);
68 
69     std::string dump() const;
70 
getNameGesturesProp71     std::string getName() const { return mName; }
72 
getCountGesturesProp73     size_t getCount() const { return mCount; }
74 
75     void registerHandlers(void* handlerData, GesturesPropGetHandler getter,
76                           GesturesPropSetHandler setter);
77 
78     std::vector<int> getIntValues() const;
79     std::vector<bool> getBoolValues() const;
80     std::vector<double> getRealValues() const;
81     std::string getStringValue() const;
82 
83     void setIntValues(const std::vector<int>& values);
84     void setBoolValues(const std::vector<bool>& values);
85     void setRealValues(const std::vector<double>& values);
86     // Setting string values isn't supported since we don't have a use case yet and the memory
87     // management adds additional complexity.
88 
89     void trySetFromIdcProperty(const android::PropertyMap& idcProperties,
90                                const std::string& propertyName);
91 
92 private:
93     // Two type parameters are required for these methods, rather than one, due to the gestures
94     // library using its own bool type.
95     template <typename T, typename U>
96     const std::vector<T> getValues(U* dataPointer) const;
97     template <typename T, typename U>
98     void setValues(T* dataPointer, const std::vector<U>& values);
99 
100     std::string mName;
101     size_t mCount;
102     std::variant<int*, GesturesPropBool*, const char**, double*> mDataPointer;
103     void* mHandlerData = nullptr;
104     GesturesPropGetHandler mGetter = nullptr;
105     GesturesPropSetHandler mSetter = nullptr;
106 };
107