• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 // !tty_utility.rs: functions for shell input convert
16 
17 #[cfg(target_os = "windows")]
18 use std::collections::HashMap;
19 
20 #[cfg(target_os = "windows")]
21 extern "C" {
getch() -> libc::c_int22     fn getch() -> libc::c_int;
23 }
24 
25 #[cfg(target_os = "windows")]
26 const UNICODE_ONE_BYTES: i32 = 0b00000000;
27 #[cfg(target_os = "windows")]
28 const UNICODE_ONE_BYTES_JUDGE: i32 = 0b10000000;
29 #[cfg(target_os = "windows")]
30 const UNICODE_TWO_BYTES: i32 = 0b11000000;
31 #[cfg(target_os = "windows")]
32 const UNICODE_THREE_BYTES: i32 = 0b11100000;
33 #[cfg(target_os = "windows")]
34 const UNICODE_FOUR_BYTES: i32 = 0b11110000;
35 
36 #[cfg(target_os = "windows")]
37 const VK_UP: i32 = 0x48;
38 
39 #[cfg(target_os = "windows")]
40 lazy_static! {
41     static ref VIRTIAL_KEY_MAP: HashMap<i32, String> = {
42         let mut map = HashMap::new();
43 
44         map.insert(VK_UP, "[A".to_string());
45         map
46     };
47 }
48 
49 #[cfg(target_os = "windows")]
convert_to_control_code() -> Vec<u8>50 pub fn convert_to_control_code() -> Vec<u8> {
51     // 获取控制字符后的字符
52     let control_char = unsafe { getch() };
53 
54     let mut unicode_byte: Vec<u8> = Vec::new();
55     // linux下的控制码以33开头
56     unicode_byte.push(0x1b_u8);
57     // 根据VIRTIAL_KEY_MAP中保存的对应关系把win下的vitual key转换成linux对用的按键码
58     match VIRTIAL_KEY_MAP.get(&control_char) {
59         Some(virtual_key_string) => {
60             for (_index, c) in virtual_key_string.chars().enumerate() {
61                 unicode_byte.push(c as u8);
62             }
63         }
64         None => hdc::info!("current control code is not support now"),
65     }
66 
67     unicode_byte
68 }
69 
70 #[cfg(target_os = "windows")]
get_unicode_len(input_char: i32) -> u3271 fn get_unicode_len(input_char: i32) -> u32 {
72     let mut len = 0;
73     if input_char & UNICODE_ONE_BYTES_JUDGE == UNICODE_ONE_BYTES {
74         len = 1;
75     }
76     if input_char & UNICODE_TWO_BYTES == UNICODE_TWO_BYTES {
77         len = 2;
78     }
79     if input_char & UNICODE_THREE_BYTES == UNICODE_THREE_BYTES {
80         len = 3;
81     }
82     if input_char & UNICODE_FOUR_BYTES == UNICODE_FOUR_BYTES {
83         len = 4;
84     }
85     len
86 }
87 
88 #[cfg(target_os = "windows")]
89 // 通过第一个字符判断unicode长度,并读取组装完成的unicode
unicode_assemble(first_char: i32) -> Vec<u8>90 pub fn unicode_assemble(first_char: i32) -> Vec<u8> {
91     let mut len = get_unicode_len(first_char);
92     hdc::info!("unicode bytes len is {:?}", len);
93 
94     let mut unicode_byte: Vec<u8> = Vec::new();
95     unicode_byte.push(first_char as u8);
96     if len > 1 {
97         len -= 1;
98         while len > 0 {
99             let left_char = unsafe { getch() };
100             unicode_byte.push(left_char as u8);
101             len -= 1;
102         }
103     }
104 
105     unicode_byte
106 }
107