summaryrefslogtreecommitdiff
path: root/EDK/MiniSetup/BootOnly/buffer.c
blob: e49203a5b34c1073a1e8654d42c644cf784ac8f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2011, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
// $Archive: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/BootOnly/buffer.c $
//
// $Author: Arunsb $
//
// $Revision: 10 $
//
// $Date: 3/18/13 2:40a $
//
//*****************************************************************//
//*****************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/BootOnly/buffer.c $
// 
// 10    3/18/13 2:40a Arunsb
// [TAG]  		EIP116669
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Mouse shadow can't  refresh when switch Multi languages
// under BIOS Setup.
// [RootCause]  	Mouse not stopped when mouse pointer is at the top(0 row)
// of the screen.
// [Solution]  	Mouse stopped properly in all the position.
// [Files]  		buffer.c
// 
// 9     10/18/12 5:58a Arunsb
// Updated for 2.16.1235 QA submission
// 
// 13    10/10/12 12:36p Arunsb
// Synched the source for v2.16.1232, backup with Aptio
// 
// 8     1/18/12 4:55a Rajashakerg
// [TAG]  		EIP79962 
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	Mouse pointer corruption in GTSE
// [RootCause]  	For GTSE, the cordanites comparison logic went wrong for 
// stopping the mouse pointer when it is present while 
// updating the screen buffer.
// [Solution]  	Corrected the logic to stop the mouse when mouse pointer
//  is present on screen we are updating.
// [Files]  		buffer.c, CommonHelper.c, Mouse.c
// 
// 7     11/30/11 11:24a Premkumara
// [TAG]  		EIP67695
// [Category]  	Improvement
// [Description]  	No cursor visible in setup, while editing text input
// box
// [Files]  		Buffer.c, Postmgmtext.c, PopupString.c, PopupPassword.c
// 
// 6     11/20/11 7:02a Rajashakerg
// [TAG]  		EIP62763
// [Category]  	Improvement
// [Description]  	Utilize the Improvements done from mouse driver in
// AMITSE
// [Files]  		HookAnchor.h, TseCommon.h, AMITSE.sdl, CommonHelper.c,
// commonoem.c, commonoem.h, buffer.c, globals.c, HookAnchor.c,
// minisetup.h, notify.c, postmgmt.c, protocol.c, ezport.c, stylecommon.c,
// Mouse.c, Action.c, Date.c, frame.c, MessageBox.c, minisetupext.c,
// minisetupext.h, numeric.c, numeric.h, page.c, PopupEdit.c, PopupEdit.h,
// PopupPassword.c, postmgmtext.c, time.c.
// 
// 5     9/27/10 7:48a Mallikarjunanv
// EIP 40555 : To avoid Compilation issues with Fareast Windows.
// 
// 4     2/19/10 1:01p Madhans
// Updated for TSE 2.01. Refer Changelog.log for File change history.
// 
// 9     2/19/10 8:14a Mallikarjunanv
// updated year in copyright message
// 
// 8     1/29/10 4:37p Madhans
// To not to depend on ConsoleControl and ConOut.
// 
// 7     10/28/09 5:36p Madhans
// To undo the changes.
// 
// 6     9/29/09 5:51p Madhans
// Added more comment.
// 
// 5     9/15/09 9:27a Sudhirv
// added a flag for FlushPause
// 
// 4     8/17/09 4:31a Mallikarjunanv
// EIP:24991 - updated the DoRealFlush lines function with the changes
// suggested by Murase
// 
// 3     6/23/09 6:56p Blaines
// Coding standard update, 
// Remove spaces from file header to allow proper chm function list
// creation.
// 
// 2     6/12/09 7:41p Presannar
// Initial implementation of coding standards
// 
// 1     6/04/09 8:05p Madhans
// 
// 2     4/29/09 9:02p Madhans
// Bug Fixes after unit Testing..
// 
// 1     4/28/09 11:11p Madhans
// Tse 2.0 Code complete Checkin.
// 
// 4     4/28/09 9:39p Madhans
// Tse 2.0 Code complete Checkin.
// 
// 3     2/05/09 10:15a Madhans
// Style Module created.
// 
// 2     1/30/09 6:06p Madhans
// Function headers added. 
// 
// 1     12/18/08 7:58p Madhans
// Intial version of TSE Lite sources
//*****************************************************************//
//*****************************************************************//

//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:		Buffer.c
//
// Description:	This file contains code to manage buffers
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "minisetup.h"
VOID GetCoordinates(INT32 *x, INT32 *y, INT32 *z);
INT32 GetactualScreentop(VOID);
BOOLEAN IsMouseSupported(VOID);
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	InitializeScreenBuffer
//
// Description:	function to initialize the screen buffer
//
// Input:		UINT8 attrib
//
// Output:		status
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS InitializeScreenBuffer( UINT8 attrib )
{
	EFI_STATUS Status = EFI_OUT_OF_RESOURCES;

	MemFreePointer( (VOID **)&gFlushBuffer );
	gFlushBuffer = EfiLibAllocateZeroPool( sizeof(SCREEN_BUFFER) );
	if ( gFlushBuffer == NULL )
		return Status;

	MemFreePointer( (VOID **)&gActiveBuffer );
	gActiveBuffer = EfiLibAllocatePool( sizeof(SCREEN_BUFFER) );
	if ( gActiveBuffer != NULL )
	{
		ClearScreenWithoutFlush( attrib );
		Status = EFI_SUCCESS;
	}

	return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SlowFlushLines
//
// Description:	function to slow flush lines
//
// Input:		UINTN Top, UINTN Bottom
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID SlowFlushLines( UINTN Top, UINTN Bottom )
{
	UINTN	Index;
	UINTN	Offset = Top * gMaxCols;
	UINTN	End = (Bottom - Top + 1) * gMaxCols + Offset;
	UINT8	LastAttrib = 0xFF;
	CHAR16	String[2] = { 0, 0 };

	gST->ConOut->SetCursorPosition( gST->ConOut, 0, Top );
	for ( Index = Offset; Index < End; Index++ )
	{
		if ( gActiveBuffer->Attribs[Index] != LastAttrib )
		{
			LastAttrib = gActiveBuffer->Attribs[Index];
			gST->ConOut->SetAttribute( gST->ConOut, LastAttrib );
		}
		*String = gActiveBuffer->Chars[Index];

		gST->ConOut->OutputString( gST->ConOut, String );
	}
}

#define INVALID_FLUSH_LINE 0xFFFF
UINTN	FlushTop=INVALID_FLUSH_LINE;
UINTN	FlushBottom=INVALID_FLUSH_LINE;
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	FlushLines
//
// Description:	function to set the positions for flush lines
//
// Input:		UINTN Top, UINTN Bottom
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID FlushLines( UINTN Top, UINTN Bottom )
{
	if( FlushTop==INVALID_FLUSH_LINE) 
		FlushTop = Top;

	if(FlushBottom==INVALID_FLUSH_LINE)
		FlushBottom = Bottom;

	if(FlushTop>Top)
		FlushTop = Top;

	if(FlushBottom < Bottom)
		FlushBottom = Bottom;
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	DoRealFlushLines
//
// Description:	function to flush lines
//
// Input:		NIL
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID DoRealFlushLines()
{
	UINTN	Index;
	UINTN	ByteStart, ByteEnd;
	UINTN	Offset = 0;
	UINTN	End = 0;
	UINT8	LastAttrib,LastSetAttrib=0;
	CHAR16	*String;
	UINTN	X=0,Y=0,TempOffSet,PrintLen;
	UINTN	CurrentX=0;
	BOOLEAN	FlushIt;
	UINTN	Top, Bottom;
	UINT32  MousePointerX=0,MousePointerY=0,MousePointerZ=0;//EIP 62763 : Mouse poiner declaration

	if( (FlushTop==INVALID_FLUSH_LINE) || (FlushBottom==INVALID_FLUSH_LINE))
		return;

	if(gST->ConOut == NULL)
		return;
	Top = FlushTop;
	Bottom = FlushBottom;

	if ( Top >= gMaxRows )
		return;

	if ( Top > Bottom )
	{
		UINTN Swap = Bottom;
		Bottom = Top;
		Top = Swap;
	}

	if ( Bottom >= gMaxRows )
		Bottom = gMaxRows - 1;

	ByteStart = Top * gMaxCols;
	ByteEnd = (Bottom - Top + 1) * gMaxCols + ByteStart;

	String = EfiLibAllocateZeroPool( (gMaxCols + 1) * sizeof(CHAR16) );

	if ( String == NULL )
	{
		SlowFlushLines( Top, Bottom );
		return;
	}

	// Flush from Top to Bottom
	// inside the loop 'Top' refers current line
	for ( ; Top <= Bottom; Top++ )
	{
		Offset = Top * gMaxCols;
		End = Offset + gMaxCols;

		// If All the Chars and attributes are equal in current line. Go to next line.
		if ( ( ! EfiCompareMem( &gActiveBuffer->Attribs[Offset], &gFlushBuffer->Attribs[Offset], gMaxCols ) ) &&
				( ! EfiCompareMem( &gActiveBuffer->Chars[Offset], &gFlushBuffer->Chars[Offset], gMaxCols * sizeof(CHAR16) ) ) )
			continue;

		// Don't Print the last char in MaxRow X MaxCol
		// As it will scroll if we print.
		if ( End >= gMaxRows * gMaxCols )
			End = gMaxRows * gMaxCols - 1;

		// Attribute for current print.
		LastAttrib = gActiveBuffer->Attribs[Offset];

		// To improve the flush lines code.
		CurrentX=0;

		// To support Terminal host with hight resolution.(EIP-25288)
		gST->ConOut->EnableCursor(gST->ConOut, FALSE);			//EIP:67695-Make the cursor invisible since the cursor position is changing and visible outside popup box
		gST->ConOut->SetCursorPosition( gST->ConOut, CurrentX, Top );

		PrintLen = 0;
		FlushIt = TRUE; // decides printing

		if(IsMouseSupported())
		GetCoordinates(&MousePointerX, &MousePointerY, &MousePointerZ);

		while ( Offset < End )
		{
			// Collect the string till the Attribute is differnt as it may need setAttribute
			for ( Index = 0, TempOffSet = Offset; (gActiveBuffer->Attribs[Offset] == LastAttrib) && (Offset < End); Offset++, Index++ )
			{
				String[Index] = gActiveBuffer->Chars[Offset];

				if(TRUE == IsCharWide(String[Index]))
					Offset++;

			}
			String[Index] = 0;
//EIP 79962 : Getting the actual Screen top from the binary for stopping the mouse
		  if(
			(IsMouseSupported() && 
         ((0 == Top) || (((MousePointerY/HiiGetGlyphHeight()) >= ((Top + ( GetactualScreentop()/ HiiGetGlyphHeight()))-1)) && ( (MousePointerY/HiiGetGlyphHeight()) <= ((Top + (GetactualScreentop()/ HiiGetGlyphHeight()))+1)))) )
         )		//EIP116669
		  {
		     MouseStop(); 
   		  }

			// We are trying to print the strings with 'LastAttrib'
			// NOW - TempOffSet refers begining of the buffer
			// 		 Offset refers end if the print buffer for currnet print.
			
			// if the String and attribute are equal to flush lines and it is more then 10 then we don't need to Print.
			// The reason for 10 is to avoid sending setposition. IF we skip less then 10 then we will end up calling setposition that takes 9 bytes
			// for serial redirection.
			if(((Offset-TempOffSet)>10)&&
				( ! EfiCompareMem( &gActiveBuffer->Attribs[TempOffSet], &gFlushBuffer->Attribs[TempOffSet], Offset-TempOffSet ) ))
			{
				// If all the charecters are same between active and flush buffer, let not print it
				if ( ( ! EfiCompareMem( &gActiveBuffer->Chars[TempOffSet], &gFlushBuffer->Chars[TempOffSet], (Offset-TempOffSet) * sizeof(CHAR16) ) ) )
					FlushIt = FALSE;
				else
				{
					UINTN i,j=0;
					UINTN FirstDiff, LastDiff;
					FirstDiff = LastDiff = (Offset-TempOffSet); // Fail case default
					
					// loop to find out that is the first and last different offset in the printable string.
					for(i=0,j=0; i<(Offset-TempOffSet);i++,j++)
					{
						if(gActiveBuffer->Chars[TempOffSet+i] != gFlushBuffer->Chars[TempOffSet+i])
						{
							if(FirstDiff>i) // First Diff set
								FirstDiff = i;
							LastDiff = i;
							Index = j;
						}
						if(TRUE == IsCharWide(String[j]))
							i++;
					}

//EIP:24991 - START
                    // Modification to erase wide characters correctly.
                    //  Issue:
                    //   If we write "a" over some wide character, the right half of the wide character is sometimes not erased.
                    //   e.g.)
                    //    When we write "aaaa    " over "aaa?   ", the right half of "?" is not erased.
                    if (!IsCharWide(String[Index]) && IsCharWide(gFlushBuffer->Chars[TempOffSet + LastDiff]) && LastDiff+1<Offset-TempOffSet){
                        LastDiff++;
                        Index++;
                    }else if (IsCharWide(String[Index]) && IsCharWide(gFlushBuffer->Chars[TempOffSet + LastDiff + 1]) && LastDiff+2<Offset-TempOffSet){
                        LastDiff += 2;
                        Index++;
                    }
//EIP:24991 - End
					if(FirstDiff>10) // Is it worth skiping?
					{
						Offset = TempOffSet+FirstDiff;
						String[Index] = 0; // No need. But lets do it anyway
						FlushIt = FALSE;
					}
					else  if(((Offset-TempOffSet)- LastDiff) > 10) // Only print the Different sting up to LastDiff
					{
//EIP:24991 - START
						// Make the offset to point to next char to be printed for next iteration
						Offset = TempOffSet+LastDiff+1;
						if(TRUE == IsCharWide(String[Index]))
							Offset++;
//EIP:24991 - END
						String[Index+1] = 0; 
					}
				}
			}

			if(FlushIt)
			{
				// Set of it is different from previous setatribute.
				if(LastSetAttrib!=LastAttrib)
				{
					gST->ConOut->SetAttribute( gST->ConOut, LastAttrib );
					LastSetAttrib = LastAttrib;
				}
				//do the set position if the cursor is not at expected loc. 
				if(((X+PrintLen) != CurrentX) || (Y != Top) || ((CurrentX == 0) && (Top==0)))
				{
					gST->ConOut->SetCursorPosition( gST->ConOut, CurrentX, Top );
				}

				X=CurrentX;
				Y=Top;

				gST->ConOut->OutputString( gST->ConOut, String );
				PrintLen = Offset-TempOffSet;

				// if we print till end of line then increament the y and make x=0.
				if((X+PrintLen)>=gMaxCols)
				{
					Y++;X=0;
				}
			}

			CurrentX += Offset-TempOffSet;
			FlushIt = TRUE;
			LastAttrib = gActiveBuffer->Attribs[Offset];
		}
	}

	MemCopy( &gFlushBuffer->Attribs[ByteStart], &gActiveBuffer->Attribs[ByteStart], ByteEnd - ByteStart );
	MemCopy( &gFlushBuffer->Chars[ByteStart], &gActiveBuffer->Chars[ByteStart], (ByteEnd - ByteStart) * sizeof(CHAR16) );
	FlushTop=INVALID_FLUSH_LINE;
	FlushBottom=INVALID_FLUSH_LINE;
	MemFreePointer( (VOID **)&String );
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SetDesiredTextMode
//
// Description:	function to set the test mode
//
// Input:		nil
//
// Output:		void
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void SetDesiredTextMode()
{
	EFI_STATUS Status;

	if(gST->ConOut==NULL)
		return;
    Status = gST->ConOut->Reset( gST->ConOut, FALSE );
	Status = gST->ConOut->SetMode( gST->ConOut, StyleGetTextMode( gMaxRows, gMaxCols ) );

	if ( EFI_ERROR( Status ) )
	{
		gMaxRows = StyleGetStdMaxRows(); 
		gMaxCols = StyleGetStdMaxCols();
		gST->ConOut->SetMode( gST->ConOut, 0 );
	}
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**     5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093     **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************