2 Utilities for Interactive I/O Functions.
4 The functions assume that isatty() is TRUE at the time they are called.
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.
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.
16 #include <Protocol/SimpleTextOut.h>
18 #include <LibConfig.h>
22 #include <sys/syslimits.h>
23 #include <sys/termios.h>
24 #include <Device/IIO.h>
26 #include "IIOutilities.h"
28 /** Get the low-level UEFI protocol associated with an open file.
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.
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.
41 struct __filedes
**filp
46 struct __filedes
*pfil
;
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
;
60 /** Get a character either from the input buffer or from hardware.
62 @param[in] filp Pointer to a file descriptor structure.
63 @param[in] First Set to TRUE to identify the initial read.
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.
72 struct __filedes
*filp
,
91 BufCnt
= InBuf
->Count(InBuf
, AsElements
);
94 Status
= InBuf
->Read(InBuf
, &InChar
, 1);
99 NumRead
= filp
->f_ops
->fo_read(filp
, &filp
->f_offset
, sizeof(wchar_t), &InChar
);
105 RetVal
= (wint_t)InChar
;
110 /** Get the current cursor position.
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.
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.
122 IIO_GetCursorPosition (
128 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*Proto
;
129 struct __filedes
*pStdOut
;
134 Proto
= (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)IIO_GetDeviceProto(fd
, &pStdOut
);
136 if(((pStdOut
->f_iflags
& _S_ITTY
) != 0) && // file is a TTY
137 ((pStdOut
->Oflags
& O_ACCMODE
) != 0)) // and it is open for output
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
) {
153 /** Set the cursor position.
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.
159 @retval -1 fd is not an IIO output device
160 @retval 0 Cursor position set successfully.
164 IIO_SetCursorPosition (
165 struct __filedes
*filp
,
169 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*Proto
;
176 This
= filp
->devdata
;
177 Proto
= (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)IIO_GetDeviceProto(filp
->MyFD
, 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
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
;
194 /** Get Output screen size and mode.
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.
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
213 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*Proto
;
214 struct __filedes
*pStdOut
;
223 Proto
= (EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*)IIO_GetDeviceProto(fd
, &pStdOut
);
225 if(((pStdOut
->f_iflags
& _S_ITTY
) != 0) && // file is a TTY
226 ((pStdOut
->Oflags
& O_ACCMODE
) != 0)) // and it is open for output
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
)) {
238 RetVal
= (int)TempMode
;
248 /** Calculate the number of character positions between two X/Y coordinate pairs.
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.
254 RowDelta is the computed difference between the ending and starting rows.
255 If RowDelta < 0, then EndXY is NOT after StartXY, so assert.
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.
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.
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.
280 RowDelta
= (int)EndXY
->Row
- (int)StartXY
->Row
;
282 assert(RowDelta
>= 0); // assert if EndXY is NOT after StartXY
284 ColumnDelta
= (int)((This
->MaxColumn
* RowDelta
) + EndXY
->Column
);
285 ColumnDelta
-= (int)StartXY
->Column
;