1 /** @file
2   Implements statusbar interface functions.
3 
4   Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved. <BR>
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include "EditStatusBar.h"
16 #include "UefiShellDebug1CommandsLib.h"
17 
18 CHAR16  *StatusString;
19 BOOLEAN StatusBarNeedRefresh;
20 BOOLEAN StatusStringChanged;
21 
22 /**
23   Initialization function for Status Bar.
24 
25   @retval EFI_SUCCESS           The operation was successful.
26   @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.
27   @sa StatusBarSetStatusString
28 **/
29 EFI_STATUS
StatusBarInit(VOID)30 StatusBarInit (
31   VOID
32   )
33 {
34   //
35   // initialize the statusbar
36   //
37   StatusString         = NULL;
38   StatusBarNeedRefresh = TRUE;
39   StatusStringChanged  = FALSE;
40 
41   //
42   // status string set to ""
43   //
44   return (StatusBarSetStatusString (L""));
45 }
46 
47 /**
48   Cleanup function for the status bar.
49 **/
50 VOID
StatusBarCleanup(VOID)51 StatusBarCleanup (
52   VOID
53   )
54 {
55   //
56   // free the status string and backvar's status string
57   //
58   SHELL_FREE_NON_NULL (StatusString);
59 }
60 
61 typedef struct {
62   UINT32  Foreground : 4;
63   UINT32  Background : 3;
64 } STATUS_BAR_COLOR_ATTRIBUTES;
65 
66 typedef union {
67   STATUS_BAR_COLOR_ATTRIBUTES  Colors;
68   UINTN                       Data;
69 } STATUS_BAR_COLOR_UNION;
70 
71 /**
72   Cause the status bar to refresh it's printing on the screen.
73 
74   @param[in] EditorFirst      TRUE to indicate the first launch of the editor.
75                               FALSE otherwise.
76   @param[in] LastRow          LastPrintable row.
77   @param[in] LastCol          Last printable column.
78   @param[in] FileRow          Row in the file.
79   @param[in] FileCol          Column in the file.
80   @param[in] InsertMode       TRUE to indicate InsertMode.  FALSE otherwise.
81 
82   @retval EFI_SUCCESS         The operation was successful.
83 **/
84 EFI_STATUS
StatusBarRefresh(IN BOOLEAN EditorFirst,IN UINTN LastRow,IN UINTN LastCol,IN UINTN FileRow,IN UINTN FileCol,IN BOOLEAN InsertMode)85 StatusBarRefresh (
86   IN BOOLEAN  EditorFirst,
87   IN UINTN    LastRow,
88   IN UINTN    LastCol,
89   IN UINTN    FileRow,
90   IN UINTN    FileCol,
91   IN BOOLEAN  InsertMode
92   )
93 {
94   STATUS_BAR_COLOR_UNION  Orig;
95   STATUS_BAR_COLOR_UNION  New;
96 
97   if (!StatusStringChanged && StatusBarNeedRefresh) {
98     StatusBarSetStatusString (L"\0");
99   }
100   //
101   // when it's called first time after editor launch, so refresh is mandatory
102   //
103   if (!StatusBarNeedRefresh && !StatusStringChanged) {
104     return EFI_SUCCESS;
105   }
106 
107   //
108   // back up the screen attributes
109   //
110   Orig.Data             = gST->ConOut->Mode->Attribute;
111   New.Data              = 0;
112   New.Colors.Foreground = Orig.Colors.Background & 0xF;
113   New.Colors.Background = Orig.Colors.Foreground & 0x7;
114 
115   gST->ConOut->EnableCursor (gST->ConOut, FALSE);
116   gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);
117 
118   //
119   // clear status bar
120   //
121   EditorClearLine (LastRow, LastCol, LastRow);
122 
123   //
124   // print row, column fields
125   //
126   if (FileRow != (UINTN)(-1) && FileCol != (UINTN)(-1)) {
127     ShellPrintEx (
128       0,
129       (INT32)(LastRow) - 1,
130       L" %d,%d       %s",
131       FileRow,
132       FileCol,
133       StatusString
134       );
135   } else {
136     ShellPrintEx (
137       0,
138       (INT32)(LastRow) - 1,
139       L"  %s",
140       StatusString
141       );
142   }
143 
144   //
145   // print insert mode field
146   //
147   if (InsertMode) {
148     ShellPrintEx ((INT32)(LastCol) - 21, (INT32)(LastRow) - 1, L"|%s|   Help: Ctrl-E", L"INS");
149   } else {
150     ShellPrintEx ((INT32)(LastCol) - 21, (INT32)(LastRow) - 1, L"|%s|   Help: Ctrl-E", L"OVR");
151   }
152   //
153   // restore the old screen attributes
154   //
155   gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
156 
157   //
158   // restore position in edit area
159   //
160   gST->ConOut->EnableCursor (gST->ConOut, TRUE);
161 
162   StatusBarNeedRefresh  = FALSE;
163   StatusStringChanged   = FALSE;
164 
165   return EFI_SUCCESS;
166 }
167 
168 /**
169   Set the status string text part.
170 
171   @param[in] Str                The string to use.
172 
173   @retval EFI_SUCCESS           The operation was successful.
174   @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.
175 **/
176 EFI_STATUS
StatusBarSetStatusString(IN CHAR16 * Str)177 StatusBarSetStatusString (
178   IN CHAR16 *Str
179   )
180 {
181   StatusStringChanged = TRUE;
182 
183   //
184   // free the old status string
185   //
186   SHELL_FREE_NON_NULL (StatusString);
187   StatusString = CatSPrint (NULL, L"%s", Str);
188   if (StatusString == NULL) {
189     return EFI_OUT_OF_RESOURCES;
190   }
191 
192   return EFI_SUCCESS;
193 }
194 
195 /**
196   Function to retrieve the current status string.
197 
198   @return The string that is used.
199 **/
200 CONST CHAR16*
StatusBarGetString(VOID)201 StatusBarGetString (
202   VOID
203   )
204 {
205   return (StatusString);
206 }
207 
208 /**
209   Function to set the need refresh boolean to TRUE.
210 **/
211 VOID
StatusBarSetRefresh(VOID)212 StatusBarSetRefresh(
213   VOID
214   )
215 {
216   StatusBarNeedRefresh = TRUE;
217 }
218 
219 /**
220   Function to get the need refresh boolean to TRUE.
221 
222   @retval TRUE    The status bar needs to be refreshed.
223 **/
224 BOOLEAN
StatusBarGetRefresh(VOID)225 StatusBarGetRefresh(
226   VOID
227   )
228 {
229   return (StatusBarNeedRefresh);
230 }
231