remove build warning
[platform/adaptation/samsung_exynos/device-manager-plugin-exynos5433.git] / hw / touchscreen / touchscreen.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <linux/limits.h>
25 #include <dirent.h>
26
27 #include <hw/touchscreen.h>
28 #include "../shared.h"
29
30 #define INPUT_PATH      "/sys/class/input/"
31 #define KEY_CAPABILITIES_PATH  "/device/capabilities/key"
32 #define ENABLED_PATH           "/device/enabled"
33 #define TOUCHSCREEN_CAPABILITY 400
34
35 #define TURNON_TOUCHSCREEN     1
36 #define TURNOFF_TOUCHSCREEN    0
37
38 static char *touchscreen_node;
39
40 static int touchscreen_probe(void)
41 {
42         DIR *d;
43         struct dirent *dir;
44         char buf[PATH_MAX];
45         int val, ret = -ENOTSUP;
46
47         d = opendir(INPUT_PATH);
48         if (!d)
49                 return -errno;
50
51         while ((dir = readdir(d))) {
52                 if (dir->d_name[0] == '.')
53                         continue;
54                 snprintf(buf, sizeof(buf), "%s%s%s", INPUT_PATH,
55                                 dir->d_name, KEY_CAPABILITIES_PATH);
56
57                 ret = sys_get_int(buf, &val);
58                 if (ret < 0 || val != TOUCHSCREEN_CAPABILITY)
59                         continue;
60
61                 snprintf(buf, sizeof(buf), "%s%s%s", INPUT_PATH,
62                                 dir->d_name, ENABLED_PATH);
63
64                 touchscreen_node = strndup(buf, strlen(buf));
65                 if (touchscreen_node) {
66                         _I("touchscreen node (%s)", touchscreen_node);
67                         ret = 0;
68                 } else {
69                         _E("strndup() failed");
70                         ret = -ENOMEM;
71                 }
72                 break;
73         }
74         closedir(d);
75
76         return ret;
77 }
78
79 static int touchscreen_get_state(enum touchscreen_state *state)
80 {
81         int ret, val;
82
83         if (!touchscreen_node || !(*touchscreen_node))
84                 return -ENOENT;
85
86         if (!state)
87                 return -EINVAL;
88
89         ret = sys_get_int(touchscreen_node, &val);
90         if (ret < 0) {
91                 _E("Failed to get touchscreen state (%d)", ret);
92                 return ret;
93         }
94
95         switch (val) {
96         case TURNOFF_TOUCHSCREEN:
97                 *state = TOUCHSCREEN_OFF;
98                 break;
99         case TURNON_TOUCHSCREEN:
100                 *state = TOUCHSCREEN_ON;
101                 break;
102         default:
103                 _E("Failed to get touchscreen state (%d)", val);
104                 return -EINVAL;
105         }
106
107         return 0;
108 }
109
110 static int touchscreen_set_state(enum touchscreen_state state)
111 {
112         int ret, val;
113
114         if (!touchscreen_node || !(*touchscreen_node))
115                 return -ENOENT;
116
117         switch (state) {
118         case TOUCHSCREEN_OFF:
119                 val = TURNOFF_TOUCHSCREEN;
120                 break;
121         case TOUCHSCREEN_ON:
122                 val = TURNON_TOUCHSCREEN;
123                 break;
124         default:
125                 _E("Invalid input (%d)", state);
126                 return -EINVAL;
127         }
128
129         ret = sys_set_int(touchscreen_node, val);
130         if (ret < 0)
131                 _E("Failed to change touchscreen state (%d)", ret);
132
133         return ret;
134 }
135
136 static int touchscreen_open(struct hw_info *info,
137                 const char *id, struct hw_common **common)
138 {
139         struct touchscreen_device *touchscreen_dev;
140
141         if (!info || !common)
142                 return -EINVAL;
143
144         if (touchscreen_probe() < 0)
145                 return -ENOTSUP;
146
147         touchscreen_dev = calloc(1, sizeof(struct touchscreen_device));
148         if (!touchscreen_dev)
149                 return -ENOMEM;
150
151         touchscreen_dev->common.info = info;
152         touchscreen_dev->get_state = touchscreen_get_state;
153         touchscreen_dev->set_state = touchscreen_set_state;
154
155         *common = (struct hw_common *)touchscreen_dev;
156         return 0;
157 }
158
159 static int touchscreen_close(struct hw_common *common)
160 {
161         if (!common)
162                 return -EINVAL;
163
164         free(common);
165         free(touchscreen_node);
166         return 0;
167 }
168
169 HARDWARE_MODULE_STRUCTURE = {
170         .magic = HARDWARE_INFO_TAG,
171         .hal_version = HARDWARE_INFO_VERSION,
172         .device_version = TOUCHSCREEN_HARDWARE_DEVICE_VERSION,
173         .id = TOUCHSCREEN_HARDWARE_DEVICE_ID,
174         .name = "touchscreen",
175         .open = touchscreen_open,
176         .close = touchscreen_close,
177 };