2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <libsyscommon/libgdbus.h>
24 #include "input-internal.h"
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"
33 static bool input_is_supported_input_type(device_input_type_e input_device_type)
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:
49 int device_input_get_default_device(device_input_type_e input_device_type, int *input_device_id)
53 if (!input_is_supported_input_type(input_device_type)) {
54 _E("Undefined input device type");
55 return DEVICE_ERROR_INVALID_PARAMETER;
58 if (!input_device_id || input_device_type == DEVICE_INPUT_TYPE_ALL) {
59 _E("Invalid parameters");
60 return DEVICE_ERROR_INVALID_PARAMETER;
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),
69 _E("Failed to call dbus method to get input default device, return value(%d)", ret);
70 return errno_to_device_error(ret);
74 _E("Failed to get default input device id of input type(%d)", input_device_type);
75 return DEVICE_ERROR_OPERATION_FAILED;
78 *input_device_id = (int)reply;
80 return DEVICE_ERROR_NONE;
83 int device_input_get_devices(device_input_type_e input_device_type,
84 int **input_device_ids, int *num_input_device_ids)
88 GVariantIter *iter = NULL;
90 int temp_input_device_id = 0;
91 int *temp_input_device_ids = NULL;
93 if (!input_is_supported_input_type(input_device_type)) {
94 _E("Undefined input device type");
95 return DEVICE_ERROR_INVALID_PARAMETER;
98 if (!input_device_ids || !num_input_device_ids) {
99 _E("Invalid parameters");
100 return DEVICE_ERROR_INVALID_PARAMETER;
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),
109 _E("Failed to call dbus method to get input devices, return value(%d)", ret);
110 return errno_to_device_error(ret);
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;
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;
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);
130 *input_device_ids = temp_input_device_ids;
132 return DEVICE_ERROR_NONE;
135 int device_input_set_event_state(int input_device_id, bool on)
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),
144 return errno_to_device_error(ret);
147 _E("Failed to set input device event state");
148 return DEVICE_ERROR_OPERATION_FAILED;
151 return DEVICE_ERROR_NONE;
154 int device_input_get_event_state(int input_device_id, bool* on)
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),
162 return errno_to_device_error(ret);
165 _E("Failed to get input device event state");
166 return DEVICE_ERROR_OPERATION_FAILED;
171 return DEVICE_ERROR_NONE;
174 int device_input_get_device_name(int input_device_id, char **input_device_name)
178 char* temp_input_device_name;
181 if (!input_device_name) {
182 _E("Invalid parameters");
183 return DEVICE_ERROR_INVALID_PARAMETER;
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),
192 _E("Failed to call dbus method to get device name, return value(%d)", ret);
193 return errno_to_device_error(ret);
196 g_variant_get(reply, "(is)", &ret_val, &temp_input_device_name);
198 _E("Failed to get name of input device id(%d)", input_device_id);
199 return DEVICE_ERROR_OPERATION_FAILED;
202 *input_device_name = temp_input_device_name;
204 return DEVICE_ERROR_NONE;