input: add DEVICE_INPUT_TYPE_TOUCHSCREEN enum type
[platform/core/api/device.git] / src / input-internal.c
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <stdlib.h>
18 #include <string.h>
19 #include <gio/gio.h>
20 #include <glib.h>
21
22 #include <libsyscommon/libgdbus.h>
23
24 #include "input-internal.h"
25 #include "common.h"
26
27 #define METHOD_INPUT_GET_DEFAULT_DEVICE         "InputGetDefaultDevice"
28 #define METHOD_INPUT_GET_DEVICES                "InputGetDevices"
29 #define METHOD_INPUT_SET_EVENT_STATE            "InputSetEventState"
30 #define METHOD_INPUT_GET_EVENT_STATE            "InputGetEventState"
31 #define METHOD_INPUT_GET_DEVICE_NAME            "InputGetDeviceName"
32
33 static bool input_is_supported_input_type(device_input_type_e input_device_type)
34 {
35         switch (input_device_type) {
36         case DEVICE_INPUT_TYPE_UNKNOWN:
37         case DEVICE_INPUT_TYPE_ALL:
38         case DEVICE_INPUT_TYPE_MOUSE:
39         case DEVICE_INPUT_TYPE_KEYBOARD:
40         case DEVICE_INPUT_TYPE_TOUCHSCREEN:
41         case DEVICE_INPUT_TYPE_CUSTOM_KNOB:
42                 break;
43         default:
44                 return false;
45         }
46         return true;
47 }
48
49 int device_input_get_default_device(device_input_type_e input_device_type, int *input_device_id)
50 {
51         int ret, reply;
52
53         if (!input_is_supported_input_type(input_device_type)) {
54                 _E("Undefined input device type");
55                 return DEVICE_ERROR_INVALID_PARAMETER;
56         }
57
58         if (!input_device_id || input_device_type == DEVICE_INPUT_TYPE_ALL) {
59                 _E("Invalid parameters");
60                 return DEVICE_ERROR_INVALID_PARAMETER;
61         }
62
63         ret = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
64                                 DEVICED_PATH_INPUT, DEVICED_INTERFACE_INPUT,
65                                 METHOD_INPUT_GET_DEFAULT_DEVICE,
66                                 g_variant_new("(i)", (int)input_device_type),
67                                 &reply);
68         if (ret < 0) {
69                 _E("Failed to call dbus method to get input default device, return value(%d)", ret);
70                 return errno_to_device_error(ret);
71         }
72
73         if (reply < 0) {
74                 _E("Failed to get default input device id of input type(%d)", input_device_type);
75                 return DEVICE_ERROR_OPERATION_FAILED;
76         }
77
78         *input_device_id = (int)reply;
79
80         return DEVICE_ERROR_NONE;
81 }
82
83 int device_input_get_devices(device_input_type_e input_device_type,
84         int **input_device_ids, int *num_input_device_ids)
85 {
86         int ret;
87         GVariant* reply;
88         GVariantIter *iter = NULL;
89         int index = 0;
90         int temp_input_device_id = 0;
91         int *temp_input_device_ids = NULL;
92
93         if (!input_is_supported_input_type(input_device_type)) {
94                 _E("Undefined input device type");
95                 return DEVICE_ERROR_INVALID_PARAMETER;
96         }
97
98         if (!input_device_ids || !num_input_device_ids) {
99                 _E("Invalid parameters");
100                 return DEVICE_ERROR_INVALID_PARAMETER;
101         }
102
103         ret = gdbus_call_sync_with_reply(DEVICED_BUS_NAME,
104                                 DEVICED_PATH_INPUT, DEVICED_INTERFACE_INPUT,
105                                 METHOD_INPUT_GET_DEVICES,
106                                 g_variant_new("(i)", (int)input_device_type),
107                                 &reply);
108         if (ret < 0) {
109                 _E("Failed to call dbus method to get input devices, return value(%d)", ret);
110                 return errno_to_device_error(ret);
111         }
112
113         g_variant_get(reply, "(ai)", &iter);
114         *num_input_device_ids = g_variant_iter_n_children(iter);
115         if (*num_input_device_ids == 0) {
116                 _W("There are no devices of input type(%d)", input_device_type);
117                 return DEVICE_ERROR_NONE;
118         }
119
120         temp_input_device_ids = calloc(*num_input_device_ids, sizeof(int));
121         if (!temp_input_device_ids) {
122                 _E("Cannot alloc memory for input device id list");
123                 return DEVICE_ERROR_OPERATION_FAILED;
124         }
125
126         while (g_variant_iter_loop(iter, "i", &temp_input_device_id))
127                 temp_input_device_ids[index++] = temp_input_device_id;
128         g_variant_iter_free(iter);
129
130         *input_device_ids = temp_input_device_ids;
131
132         return DEVICE_ERROR_NONE;
133 }
134
135 int device_input_set_event_state(int input_device_id, bool on)
136 {
137         int ret, reply;
138         ret = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
139                                 DEVICED_PATH_INPUT, DEVICED_INTERFACE_INPUT,
140                                 METHOD_INPUT_SET_EVENT_STATE, g_variant_new("(ii)",
141                                 input_device_id, (int)on),
142                                 &reply);
143         if (ret < 0)
144                 return errno_to_device_error(ret);
145
146         if (reply < 0) {
147                 _E("Failed to set input device event state");
148                 return DEVICE_ERROR_OPERATION_FAILED;
149         }
150
151         return DEVICE_ERROR_NONE;
152 }
153
154 int device_input_get_event_state(int input_device_id, bool* on)
155 {
156         int ret, reply;
157         ret = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
158                                 DEVICED_PATH_INPUT, DEVICED_INTERFACE_INPUT,
159                                 METHOD_INPUT_GET_EVENT_STATE, g_variant_new("(i)", input_device_id),
160                                 &reply);
161         if (ret < 0)
162                 return errno_to_device_error(ret);
163
164         if (reply < 0) {
165                 _E("Failed to get input device event state");
166                 return DEVICE_ERROR_OPERATION_FAILED;
167         }
168
169         *on = (bool)reply;
170
171         return DEVICE_ERROR_NONE;
172 }
173
174 int device_input_get_device_name(int input_device_id, char **input_device_name)
175 {
176         int ret;
177         GVariant* reply;
178         char* temp_input_device_name;
179         int ret_val;
180
181         if (!input_device_name) {
182                 _E("Invalid parameters");
183                 return DEVICE_ERROR_INVALID_PARAMETER;
184         }
185
186         ret = gdbus_call_sync_with_reply(DEVICED_BUS_NAME,
187                                 DEVICED_PATH_INPUT, DEVICED_INTERFACE_INPUT,
188                                 METHOD_INPUT_GET_DEVICE_NAME,
189                                 g_variant_new("(i)", (int)input_device_id),
190                                 &reply);
191         if (ret < 0) {
192                 _E("Failed to call dbus method to get device name, return value(%d)", ret);
193                 return errno_to_device_error(ret);
194         }
195
196         g_variant_get(reply, "(is)", &ret_val, &temp_input_device_name);
197         if (ret_val < 0) {
198                 _E("Failed to get name of input device id(%d)", input_device_id);
199                 return DEVICE_ERROR_OPERATION_FAILED;
200         }
201
202         *input_device_name = temp_input_device_name;
203
204         return DEVICE_ERROR_NONE;
205 }