1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <iostream>
17 #include <cstdio>
18 #include <sstream>
19 #include <getopt.h>
20 #include <vector>
21 #include <array>
22 #include <iomanip>
23 #include <algorithm>
24
25 #include "input_manager.h"
26
27 constexpr int32_t INVALID_ID { -1 };
28 enum Option {
29 QUERY = 'q',
30 SET = 's',
31 HELP = 'h'
32 };
33
PrintHelp(const std::string & title="")34 void PrintHelp(const std::string &title = "")
35 {
36 std::cout << title << std::endl;
37 printf("Usage\n"
38 "-q --query Query input device and display infomation\n"
39 "-s --set 'inputDeivceId displayId' Set inputDeivceId and displayId of the input device\n");
40 }
41
QueryDisplayBindInfo()42 void QueryDisplayBindInfo()
43 {
44 printf("Query\n");
45 OHOS::MMI::DisplayBindInfos infos;
46 auto ret = OHOS::MMI::InputManager::GetInstance()->GetDisplayBindInfo(infos);
47 if (ret != 0) {
48 printf("Get display bind info failed.\n");
49 return;
50 }
51 constexpr int32_t len = 5;
52 std::vector<std::array<std::string, len>> arrStrings;
53 std::array<std::string, len> arr0 = { "No.", "Input device Id", "Input device Name", "Display id", "Display name" };
54 arrStrings.push_back(arr0);
55 for (size_t i = 0; i < infos.size(); ++i) {
56 const auto &info = infos[i];
57 std::array<std::string, len> arr;
58 arr[0] = std::to_string(i + 1);
59 arr[1] = (info.inputDeviceId == INVALID_ID) ? "" : std::to_string(info.inputDeviceId);
60 arr[2] = info.inputDeviceName;
61 arr[3] = (info.displayId == INVALID_ID) ? "" : std::to_string(info.displayId);
62 arr[4] = info.displayName;
63 arrStrings.push_back(arr);
64 }
65 std::array<size_t, len> arrWidth{};
66 for (const auto &[a, b, c, d, e] : arrStrings) {
67 arrWidth[0] = std::max(arrWidth[0], a.length());
68 arrWidth[1] = std::max(arrWidth[1], b.length());
69 arrWidth[2] = std::max(arrWidth[2], c.length());
70 arrWidth[3] = std::max(arrWidth[3], d.length());
71 arrWidth[4] = std::max(arrWidth[4], e.length());
72 }
73 for (const auto &[a, b, c, d, e] : arrStrings) {
74 std::cout << "|"
75 << " " << std::setw(arrWidth[0]) << std::setfill(' ') << std::left << a << " "
76 << "|"
77 << " " << std::setw(arrWidth[1]) << std::setfill(' ') << std::left << b << " "
78 << "|"
79 << " " << std::setw(arrWidth[2]) << std::setfill(' ') << std::left << c << " "
80 << "|"
81 << " " << std::setw(arrWidth[3]) << std::setfill(' ') << std::left << d << " "
82 << "|"
83 << " " << std::setw(arrWidth[4]) << std::setfill(' ') << std::left << e << " "
84 << "|" << std::endl;
85 }
86 }
87
SetDisplayBind(const char * optarg)88 void SetDisplayBind(const char* optarg)
89 {
90 std::istringstream iss(optarg);
91 int32_t deviceId = INVALID_ID;
92 int32_t displayId = INVALID_ID;
93 iss >> deviceId >> displayId;
94 if (iss.fail()) {
95 PrintHelp("Arg is not right");
96 return;
97 }
98 printf("args: deviceId:%d, displayId:%d\n", deviceId, displayId);
99 std::string msg;
100 auto ret = OHOS::MMI::InputManager::GetInstance()->SetDisplayBind(deviceId, displayId, msg);
101 if (ret != 0) {
102 printf("Set display bind failed, %s\n", msg.c_str());
103 return;
104 }
105 }
106
HandleOptions(int argc,char * argv[])107 void HandleOptions(int argc, char* argv[])
108 {
109 static struct option headOptions[] = {
110 {"query", no_argument, nullptr, 'q'},
111 {"set", required_argument, nullptr, 's'},
112 {"help", no_argument, nullptr, 'h'},
113 {nullptr, 0, nullptr, 0}
114 };
115
116 if (argc < 2) {
117 PrintHelp();
118 return;
119 }
120
121 int32_t optionIndex = 0;
122 optind = 0;
123 int32_t cases = 0;
124 if ((cases = getopt_long(argc, argv, "qs:h?", headOptions, &optionIndex)) != -1) {
125 switch (cases) {
126 case QUERY: {
127 QueryDisplayBindInfo();
128 break;
129 }
130 case SET: {
131 SetDisplayBind(optarg);
132 break;
133 }
134 case HELP:
135 default: {
136 PrintHelp();
137 break;
138 }
139 }
140 };
141 }
142
main(int argc,char * argv[])143 int main(int argc, char* argv[])
144 {
145 HandleOptions(argc, argv);
146 return 0;
147 }