]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Uefi/InteractiveIO/IIOutilities.c
StdLib: Some deployed versions of the Simple Text Input Protocol randomly return...
[mirror_edk2.git] / StdLib / LibC / Uefi / InteractiveIO / IIOutilities.c
1 /** @file
2 Utilities for Interactive I/O Functions.
3
4 The functions assume that isatty() is TRUE at the time they are called.
5
6 Copyright (c) 2012 - 2014, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 **/
15 #include <Uefi.h>
16 #include <Protocol/SimpleTextOut.h>
17
18 #include <LibConfig.h>
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <sys/syslimits.h>
23 #include <sys/termios.h>
24 #include <Device/IIO.h>
25 #include <MainData.h>
26 #include "IIOutilities.h"
27
28 /** Get the low-level UEFI protocol associated with an open file.
29
30 @param[in] fd File descriptor for an open file.
31 @param[out] filp NULL, or a pointer to where a pointer to the file's
32 file descriptor structure is to be stored.
33
34 @return Returns NULL if fd is not a valid file descriptor, otherwise
35 a pointer to the file's associated UEFI protocol is returned.
36 **/
37 void *
38 EFIAPI
39 IIO_GetDeviceProto (
40 int fd,
41 struct __filedes **filp
42 )
43 {
44 void *Proto;
45 ConInstance *Stream;
46 struct __filedes *pfil;
47
48 Proto = NULL;
49 if(ValidateFD( fd, VALID_OPEN)) {
50 pfil = &gMD->fdarray[fd];
51 Stream = BASE_CR(pfil->f_ops, ConInstance, Abstraction);
52 Proto = (void *)Stream->Dev;
53 if(filp != NULL) {
54 *filp = pfil;
55 }
56 }
57 return Proto;
58 }
59
60 /** Get a character either from the input buffer or from hardware.
61
62 @param[in] filp Pointer to a file descriptor structure.
63 @param[in] First Set to TRUE to identify the initial read.
64
65 @return Returns a character read from either the input buffer
66 or from the open file (device) identified by filp.
67 A return value of WEOF indicates an error has occurred.
68 **/
69 wint_t
70 EFIAPI
71 IIO_GetInChar (
72 struct __filedes *filp,
73 BOOLEAN First
74 )
75 {
76 cIIO *This;
77 cFIFO *InBuf;
78 EFI_STATUS Status;
79 ssize_t NumRead;
80 wint_t RetVal;
81 wchar_t InChar;
82
83 static size_t BufCnt;
84
85 This = filp->devdata;
86 InBuf = This->InBuf;
87
88 NumRead = -1;
89 InChar = 0;
90 if(First) {
91 BufCnt = InBuf->Count(InBuf, AsElements);
92 }
93 if(BufCnt > 0) {
94 Status = InBuf->Read(InBuf, &InChar, 1);
95 --BufCnt;
96 NumRead = 1;
97 }
98 else {
99 NumRead = filp->f_ops->fo_read(filp, &filp->f_offset, sizeof(wchar_t), &InChar);
100 }
101 if(NumRead <= 0) {
102 RetVal = WEOF;
103 }
104 else {
105 RetVal = (wint_t)InChar;
106 }
107 return RetVal;
108 }
109
110 /** Get the current cursor position.
111
112 @param[in] fd File descriptor for an open file.
113 @param[out] Column Pointer to where the current cursor column is to be stored.
114 @param[out] Row Pointer to where the current cursor row is to be stored.
115
116 @retval -1 fd is not an IIO output device.
117 @retval 0 Cursor position retrieved, Cursor is Not Visible.
118 @retval 1 Cursor position retrieved, Cursor is Visible.
119 **/
120 int
121 EFIAPI
122 IIO_GetCursorPosition (
123 int fd,
124 UINT32 *Column,
125 UINT32 *Row
126 )
127 {
128 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Proto;
129 struct __filedes *pStdOut;
130 int RetVal;
131
132 RetVal = -1;
133
134 Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)IIO_GetDeviceProto(fd, &pStdOut);
135 if(Proto != NULL) {
136 if(((pStdOut->f_iflags & _S_ITTY) != 0) && // file is a TTY
137 ((pStdOut->Oflags & O_ACCMODE) != 0)) // and it is open for output
138 {
139 // fd is for a TTY or "Interactive IO" device
140 *Column = Proto->Mode->CursorColumn;
141 *Row = Proto->Mode->CursorRow;
142 if(Proto->Mode->CursorVisible) {
143 RetVal = 1;
144 }
145 else {
146 RetVal = 0;
147 }
148 }
149 }
150 return RetVal;
151 }
152
153 /** Set the cursor position.
154
155 @param[in] filp Pointer to the output device's file descriptor structure.
156 @param[in] StartXY Pointer to a cursor coordinate (XY) structure indicating
157 the desired coordinate to move the cursor to.
158
159 @retval -1 fd is not an IIO output device
160 @retval 0 Cursor position set successfully.
161 **/
162 int
163 EFIAPI
164 IIO_SetCursorPosition (
165 struct __filedes *filp,
166 CURSOR_XY *CursorXY
167 )
168 {
169 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Proto;
170 cIIO *This;
171 EFI_STATUS Status;
172 int RetVal;
173
174 RetVal = -1;
175
176 This = filp->devdata;
177 Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)IIO_GetDeviceProto(filp->MyFD, NULL);
178 if(Proto != NULL) {
179 if(((filp->f_iflags & _S_ITTY) != 0) && // file is a TTY
180 ((filp->Oflags & O_ACCMODE) != 0)) // and it is open for output
181 {
182 // fd is for a TTY or "Interactive IO" device
183 Status = Proto->SetCursorPosition(Proto, CursorXY->Column, CursorXY->Row);
184 if(Status == EFI_SUCCESS) {
185 This->CurrentXY.Column = CursorXY->Column;
186 This->CurrentXY.Row = CursorXY->Row;
187 RetVal = 0;
188 }
189 }
190 }
191 return RetVal;
192 }
193
194 /** Get Output screen size and mode.
195
196 @param[in] fd File descriptor of the output device.
197 @param[out] Col Pointer to where to store the MAX Column, or NULL.
198 @param[out] Row Pointer to where to store the MAX Row, or NULL.
199
200 @retval <0 An error occurred. The reason is in errno and EFIerrno.
201 * EIO UEFI QueryMode failed
202 * ENOTTY fd does not refer to an interactive output device
203 @retval >=0 Current output mode
204 **/
205 int
206 EFIAPI
207 IIO_GetOutputSize (
208 int fd,
209 UINTN *Col,
210 UINTN *Row
211 )
212 {
213 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *Proto;
214 struct __filedes *pStdOut;
215 EFI_STATUS Status;
216 UINTN TempCol;
217 UINTN TempRow;
218 UINTN TempMode;
219 int RetVal;
220
221 RetVal = -1;
222
223 Proto = (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *)IIO_GetDeviceProto(fd, &pStdOut);
224 if(Proto != NULL) {
225 if(((pStdOut->f_iflags & _S_ITTY) != 0) && // file is a TTY
226 ((pStdOut->Oflags & O_ACCMODE) != 0)) // and it is open for output
227 {
228 // fd is for a TTY or "Interactive IO" device
229 TempMode = Proto->Mode->Mode;
230 Status = Proto->QueryMode(Proto, TempMode, &TempCol, &TempRow);
231 if(EFI_ERROR(Status)) {
232 EFIerrno = Status;
233 errno = EIO;
234 }
235 else {
236 *Col = TempCol;
237 *Row = TempRow;
238 RetVal = (int)TempMode;
239 }
240 }
241 else {
242 errno = ENOTTY;
243 }
244 }
245 return RetVal;
246 }
247
248 /** Calculate the number of character positions between two X/Y coordinate pairs.
249
250 Using the current output device characteristics, calculate the number of
251 characters between two coordinates. It is assumed that EndXY points to
252 an output location that occurs after StartXY.
253
254 RowDelta is the computed difference between the ending and starting rows.
255 If RowDelta < 0, then EndXY is NOT after StartXY, so assert.
256
257 ColumnDelta is the computed number of character positions (columns) between
258 the starting position and the ending position. If ColumnDelta is < 0,
259 then EndXY is NOT after StartXY, so assert.
260
261 @param[in] This Pointer to the IIO instance to be examined.
262 @param[in] StartXY Pointer to the starting coordinate pair.
263 @param[in] EndXY Pointer to the ending coordinate pair.
264
265 @return Returns the difference between the starting and ending coordinates.
266 The return value is positive if the coordinates contained in EndXY
267 are larger than StartXY, otherwise the return value is negative.
268 **/
269 int
270 EFIAPI
271 IIO_CursorDelta (
272 cIIO *This,
273 CURSOR_XY *StartXY,
274 CURSOR_XY *EndXY
275 )
276 {
277 int ColumnDelta;
278 int RowDelta;
279
280 RowDelta = (int)EndXY->Row - (int)StartXY->Row;
281
282 assert(RowDelta >= 0); // assert if EndXY is NOT after StartXY
283
284 ColumnDelta = (int)((This->MaxColumn * RowDelta) + EndXY->Column);
285 ColumnDelta -= (int)StartXY->Column;
286
287 return ColumnDelta;
288 }