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 
Help(const std::string & title="")27 static void Help(const std::string &title = "")
28 {
29     std::cout << title << std::endl;
30     printf("Usage\n"
31         "-q     --query                             Query input device and display infomation\n"
32         "-s     --set 'inputDeivceId displayId'     Set inputDeivceId and displayId of the input device\n");
33 }
34 
main(int argc,char * argv[])35 int main(int argc, char *argv[])
36 {
37     struct option headOptions[] = {
38         {"query", no_argument, nullptr, 'q'},
39         {"set", required_argument, nullptr, 's'},
40         {"help", no_argument, nullptr, 'h'},
41         {nullptr, 0, nullptr, 0}
42     };
43 
44     if (argc < 2) {
45         Help();
46         return 0;
47     }
48 
49     int32_t optionIndex = 0;
50     optind = 0;
51     int32_t cases = 0;
52     if ((cases = getopt_long(argc, argv, "qs:h?", headOptions, &optionIndex)) != -1) {
53         switch (cases) {
54             case 'q': {
55                 printf("query\n");
56                 OHOS::MMI::DisplayBindInfos infos;
57                 auto ret = OHOS::MMI::InputManager::GetInstance()->GetDisplayBindInfo(infos);
58                 if (ret != 0) {
59                     printf("Get display bind info failed.\n");
60                     return -1;
61                 }
62                 std::vector<std::array<std::string, 5>> arrStrings;
63                 constexpr int32_t n = 5;
64                 std::array<std::string, n> arr0 = { "No.", "Input device Id", "Input device Name", "Display id",
65                     "Display name" };
66                 arrStrings.push_back(arr0);
67                 for (size_t i = 0; i < infos.size(); ++i) {
68                     const auto &info = infos[i];
69                     std::array<std::string, 5> arr;
70                     arr[0] = std::to_string(i + 1);
71                     arr[1] = (info.inputDeviceId == -1) ? "" : std::to_string(info.inputDeviceId);
72                     arr[2] = info.inputDeviceName;
73                     arr[3] = (info.displayId == -1) ? "" : std::to_string(info.displayId);
74                     arr[4] = info.displayName;
75                     arrStrings.push_back(arr);
76                 }
77                 std::array<size_t, 5> arrWidth{};
78                 for (const auto &[a, b, c, d, e] : arrStrings) {
79                     arrWidth[0] = std::max(arrWidth[0], a.length());
80                     arrWidth[1] = std::max(arrWidth[1], b.length());
81                     arrWidth[2] = std::max(arrWidth[2], c.length());
82                     arrWidth[3] = std::max(arrWidth[3], d.length());
83                     arrWidth[4] = std::max(arrWidth[4], e.length());
84                 }
85                 for (const auto &[a, b, c, d, e] : arrStrings) {
86                     std::cout << "|"
87                               << " " << std::setw(arrWidth[0]) << std::setfill(' ') << std::left << a << " "
88                               << "|"
89                               << " " << std::setw(arrWidth[1]) << std::setfill(' ') << std::left << b << " "
90                               << "|"
91                               << " " << std::setw(arrWidth[2]) << std::setfill(' ') << std::left << c << " "
92                               << "|"
93                               << " " << std::setw(arrWidth[3]) << std::setfill(' ') << std::left << d << " "
94                               << "|"
95                               << " " << std::setw(arrWidth[4]) << std::setfill(' ') << std::left << e << " "
96                               << "|" << std::endl;
97                 }
98                 return 0;
99             }
100             case 's': {
101                 std::istringstream iss(optarg);
102                 int32_t deviceId = -1;
103                 int32_t displayId = -1;
104                 iss >> deviceId >> displayId;
105                 if (iss.fail()) {
106                     Help("Arg is not right");
107                     return -1;
108                 }
109                 printf("args: deviceId:%d, displayId:%d\n", deviceId, displayId);
110                 std::string msg;
111                 auto ret = OHOS::MMI::InputManager::GetInstance()->SetDisplayBind(deviceId, displayId, msg);
112                 if (ret != 0) {
113                     printf("Set display bind failed, %s\n", msg.c_str());
114                     return -1;
115                 }
116                 return 0;
117             }
118             case 'h':
119             default: {
120                 Help();
121                 break;
122             }
123         }
124     };
125     return 0;
126 }