Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | // SPDX-FileCopyrightText: © 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
3 | | |
4 | | #include <linux/kstrtox.h> |
5 | | #include <linux/minmax.h> |
6 | | #include <errno.h> |
7 | | #include <clock.h> |
8 | | #include <console.h> |
9 | | #include <term.h> |
10 | | |
11 | | void term_setpos(int x, int y) |
12 | 0 | { |
13 | 0 | printf("\x1b[%d;%dH", y + 2, x + 1); |
14 | 0 | } |
15 | | |
16 | | static void term_cdev_drain(struct console_device *cdev) |
17 | 0 | { |
18 | 0 | while (cdev->tstc(cdev)) |
19 | 0 | cdev->getc(cdev); |
20 | 0 | } |
21 | | |
22 | | /** |
23 | | * term_cdev_read_reply - read and parse a CSI response from a console |
24 | | * @cdev: console device to read from |
25 | | * @params: output array of parsed integer parameters |
26 | | * @num: number of expected parameters |
27 | | * @end_char: character that terminates the response (e.g. 'R') |
28 | | * |
29 | | * Reads a CSI response of the form ESC [ Pn ; Pn ; ... <end_char> |
30 | | * with a single 100ms timeout for the entire response. |
31 | | * |
32 | | * Return: 0 on success, -ETIMEDOUT or -EINVAL on failure. |
33 | | */ |
34 | | static int term_cdev_read_reply(struct console_device *cdev, |
35 | | int *params, int num, char end_char) |
36 | 0 | { |
37 | 0 | char buf[64]; |
38 | 0 | uint64_t start; |
39 | 0 | char *endp; |
40 | 0 | int n = 0, i; |
41 | |
|
42 | 0 | start = get_time_ns(); |
43 | |
|
44 | 0 | while (1) { |
45 | 0 | if (is_timeout(start, 100 * MSECOND)) |
46 | 0 | return -ETIMEDOUT; |
47 | | |
48 | 0 | if (!cdev->tstc(cdev)) |
49 | 0 | continue; |
50 | | |
51 | 0 | buf[n] = cdev->getc(cdev); |
52 | 0 | if (buf[n] == end_char) |
53 | 0 | break; |
54 | | |
55 | 0 | if (++n >= sizeof(buf) - 1) |
56 | 0 | return -EINVAL; |
57 | 0 | } |
58 | | |
59 | 0 | if (n < 2 || buf[0] != '\e' || buf[1] != '[') |
60 | 0 | return -EINVAL; |
61 | | |
62 | 0 | endp = buf + 2; |
63 | 0 | for (i = 0; i < num; i++) { |
64 | 0 | params[i] = simple_strtoul(endp, &endp, 10); |
65 | 0 | if (i < num - 1) { |
66 | 0 | if (*endp != ';') |
67 | 0 | return -EINVAL; |
68 | 0 | endp++; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | | int term_cdev_get_size(struct console_device *cdev, |
76 | | int *screenwidth, int *screenheight) |
77 | 0 | { |
78 | 0 | const char esc[] = "\e7" "\e[r" "\e[999;999H" "\e[6n"; |
79 | 0 | const char restore[] = "\e8"; |
80 | 0 | int ret, params[2]; |
81 | |
|
82 | 0 | if (!(cdev->f_active & CONSOLE_STDOUT)) |
83 | 0 | return -ENOENT; |
84 | | |
85 | | /* ask the terminal only if the console can't tell size */ |
86 | 0 | if (cdev->get_size && !cdev->get_size(cdev, screenwidth, screenheight)) |
87 | 0 | return 0; |
88 | | |
89 | 0 | if (!(cdev->f_active & CONSOLE_STDIN)) |
90 | 0 | return -ENOENT; |
91 | | |
92 | 0 | term_cdev_drain(cdev); |
93 | |
|
94 | 0 | console_puts(cdev, esc); |
95 | |
|
96 | 0 | ret = term_cdev_read_reply(cdev, params, 2, 'R'); |
97 | |
|
98 | 0 | console_puts(cdev, restore); |
99 | |
|
100 | 0 | if (ret) |
101 | 0 | return ret; |
102 | | |
103 | 0 | *screenheight = params[0]; |
104 | 0 | *screenwidth = params[1]; |
105 | |
|
106 | 0 | return 0; |
107 | 0 | } |
108 | | |
109 | | int term_getsize(int *screenwidth, int *screenheight) |
110 | 0 | { |
111 | 0 | int width = INT_MAX, height = INT_MAX; |
112 | 0 | bool found = false; |
113 | |
|
114 | 0 | for_each_console(cdev) { |
115 | 0 | int w, h; |
116 | |
|
117 | 0 | if (term_cdev_get_size(cdev, &w, &h)) |
118 | 0 | continue; |
119 | | |
120 | 0 | width = min(w, width); |
121 | 0 | height = min(h, height); |
122 | 0 | found = true; |
123 | 0 | } |
124 | | |
125 | 0 | if (!found) |
126 | 0 | return -ENOENT; |
127 | | |
128 | 0 | if (screenwidth) |
129 | 0 | *screenwidth = width; |
130 | 0 | if (screenheight) |
131 | 0 | *screenheight = height; |
132 | |
|
133 | 0 | return 0; |
134 | 0 | } |