BufferImage.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. /** @file
  2. Defines HBufferImage - the view of the file that is visible at any point,
  3. as well as the event handlers for editing the file
  4. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "HexEditor.h"
  8. extern EFI_HANDLE HImageHandleBackup;
  9. extern HEFI_EDITOR_FILE_IMAGE HFileImage;
  10. extern HEFI_EDITOR_DISK_IMAGE HDiskImage;
  11. extern HEFI_EDITOR_MEM_IMAGE HMemImage;
  12. extern HEFI_EDITOR_FILE_IMAGE HFileImageBackupVar;
  13. extern HEFI_EDITOR_DISK_IMAGE HDiskImageBackupVar;
  14. extern HEFI_EDITOR_MEM_IMAGE HMemImageBackupVar;
  15. extern BOOLEAN HEditorMouseAction;
  16. extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditor;
  17. extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditorBackupVar;
  18. HEFI_EDITOR_BUFFER_IMAGE HBufferImage;
  19. HEFI_EDITOR_BUFFER_IMAGE HBufferImageBackupVar;
  20. //
  21. // for basic initialization of HBufferImage
  22. //
  23. HEFI_EDITOR_BUFFER_IMAGE HBufferImageConst = {
  24. NULL,
  25. NULL,
  26. 0,
  27. NULL,
  28. {
  29. 0,
  30. 0
  31. },
  32. {
  33. 0,
  34. 0
  35. },
  36. {
  37. 0,
  38. 0
  39. },
  40. 0,
  41. TRUE,
  42. FALSE,
  43. FileTypeNone,
  44. NULL,
  45. NULL,
  46. NULL
  47. };
  48. //
  49. // the whole edit area needs to be refreshed
  50. //
  51. BOOLEAN HBufferImageNeedRefresh;
  52. //
  53. // only the current line in edit area needs to be refresh
  54. //
  55. BOOLEAN HBufferImageOnlyLineNeedRefresh;
  56. BOOLEAN HBufferImageMouseNeedRefresh;
  57. /**
  58. Initialization function for HBufferImage
  59. @retval EFI_SUCCESS The operation was successful.
  60. @retval EFI_LOAD_ERROR A load error occurred.
  61. **/
  62. EFI_STATUS
  63. HBufferImageInit (
  64. VOID
  65. )
  66. {
  67. EFI_STATUS Status;
  68. //
  69. // basically initialize the HBufferImage
  70. //
  71. CopyMem (&HBufferImage, &HBufferImageConst, sizeof (HBufferImage));
  72. //
  73. // INIT listhead
  74. //
  75. HBufferImage.ListHead = AllocateZeroPool (sizeof (LIST_ENTRY));
  76. if (HBufferImage.ListHead == NULL) {
  77. return EFI_LOAD_ERROR;
  78. }
  79. InitializeListHead (HBufferImage.ListHead);
  80. HBufferImage.DisplayPosition.Row = 2;
  81. HBufferImage.DisplayPosition.Column = 10;
  82. HBufferImage.MousePosition.Row = 2;
  83. HBufferImage.MousePosition.Column = 10;
  84. HBufferImage.FileImage = &HFileImage;
  85. HBufferImage.DiskImage = &HDiskImage;
  86. HBufferImage.MemImage = &HMemImage;
  87. HBufferImageNeedRefresh = FALSE;
  88. HBufferImageOnlyLineNeedRefresh = FALSE;
  89. HBufferImageMouseNeedRefresh = FALSE;
  90. HBufferImageBackupVar.FileImage = &HFileImageBackupVar;
  91. HBufferImageBackupVar.DiskImage = &HDiskImageBackupVar;
  92. HBufferImageBackupVar.MemImage = &HMemImageBackupVar;
  93. Status = HFileImageInit ();
  94. if (EFI_ERROR (Status)) {
  95. return EFI_LOAD_ERROR;
  96. }
  97. Status = HDiskImageInit ();
  98. if (EFI_ERROR (Status)) {
  99. return EFI_LOAD_ERROR;
  100. }
  101. Status = HMemImageInit ();
  102. if (EFI_ERROR (Status)) {
  103. return EFI_LOAD_ERROR;
  104. }
  105. return EFI_SUCCESS;
  106. }
  107. /**
  108. Backup function for HBufferImage. Only a few fields need to be backup.
  109. This is for making the file buffer refresh as few as possible.
  110. @retval EFI_SUCCESS The operation was successful.
  111. **/
  112. EFI_STATUS
  113. HBufferImageBackup (
  114. VOID
  115. )
  116. {
  117. HBufferImageBackupVar.MousePosition = HBufferImage.MousePosition;
  118. HBufferImageBackupVar.BufferPosition = HBufferImage.BufferPosition;
  119. HBufferImageBackupVar.Modified = HBufferImage.Modified;
  120. HBufferImageBackupVar.BufferType = HBufferImage.BufferType;
  121. HBufferImageBackupVar.LowVisibleRow = HBufferImage.LowVisibleRow;
  122. HBufferImageBackupVar.HighBits = HBufferImage.HighBits;
  123. //
  124. // three kinds of buffer supported
  125. // file buffer
  126. // disk buffer
  127. // memory buffer
  128. //
  129. switch (HBufferImage.BufferType) {
  130. case FileTypeFileBuffer:
  131. HFileImageBackup ();
  132. break;
  133. case FileTypeDiskBuffer:
  134. HDiskImageBackup ();
  135. break;
  136. case FileTypeMemBuffer:
  137. HMemImageBackup ();
  138. break;
  139. default:
  140. break;
  141. }
  142. return EFI_SUCCESS;
  143. }
  144. /**
  145. Free all the lines in HBufferImage.
  146. Fields affected:
  147. Lines
  148. CurrentLine
  149. NumLines
  150. ListHead
  151. @retval EFI_SUCCESS The operation was successful.
  152. **/
  153. EFI_STATUS
  154. HBufferImageFreeLines (
  155. VOID
  156. )
  157. {
  158. HFreeLines (HBufferImage.ListHead, HBufferImage.Lines);
  159. HBufferImage.Lines = NULL;
  160. HBufferImage.CurrentLine = NULL;
  161. HBufferImage.NumLines = 0;
  162. return EFI_SUCCESS;
  163. }
  164. /**
  165. Cleanup function for HBufferImage
  166. @retval EFI_SUCCESS The operation was successful.
  167. **/
  168. EFI_STATUS
  169. HBufferImageCleanup (
  170. VOID
  171. )
  172. {
  173. EFI_STATUS Status;
  174. //
  175. // free all the lines
  176. //
  177. Status = HBufferImageFreeLines ();
  178. SHELL_FREE_NON_NULL (HBufferImage.ListHead);
  179. HBufferImage.ListHead = NULL;
  180. HFileImageCleanup ();
  181. HDiskImageCleanup ();
  182. return Status;
  183. }
  184. /**
  185. Print Line on Row
  186. @param[in] Line The lline to print.
  187. @param[in] Row The row on screen ( begin from 1 ).
  188. @param[in] FRow The FRow.
  189. @param[in] Orig The original color.
  190. @param[in] New The color to print with.
  191. @retval EFI_SUCCESS The operation was successful.
  192. **/
  193. EFI_STATUS
  194. HBufferImagePrintLine (
  195. IN HEFI_EDITOR_LINE *Line,
  196. IN UINTN Row,
  197. IN UINTN FRow,
  198. IN HEFI_EDITOR_COLOR_UNION Orig,
  199. IN HEFI_EDITOR_COLOR_UNION New
  200. )
  201. {
  202. UINTN Index;
  203. UINTN Pos;
  204. BOOLEAN Selected;
  205. BOOLEAN BeNewColor;
  206. UINTN RowStart;
  207. UINTN RowEnd;
  208. UINTN ColStart;
  209. UINTN ColEnd;
  210. //
  211. // variable initialization
  212. //
  213. ColStart = 0;
  214. ColEnd = 0;
  215. Selected = FALSE;
  216. //
  217. // print the selected area in opposite color
  218. //
  219. if (HMainEditor.SelectStart != 0 && HMainEditor.SelectEnd != 0) {
  220. RowStart = (HMainEditor.SelectStart - 1) / 0x10 + 1;
  221. RowEnd = (HMainEditor.SelectEnd - 1) / 0x10 + 1;
  222. ColStart = (HMainEditor.SelectStart - 1) % 0x10 + 1;
  223. ColEnd = (HMainEditor.SelectEnd - 1) % 0x10 + 1;
  224. if (FRow >= RowStart && FRow <= RowEnd) {
  225. Selected = TRUE;
  226. }
  227. if (FRow > RowStart) {
  228. ColStart = 1;
  229. }
  230. if (FRow < RowEnd) {
  231. ColEnd = 0x10;
  232. }
  233. }
  234. if (!HEditorMouseAction) {
  235. ShellPrintEx (
  236. 0,
  237. (INT32)Row - 1,
  238. L"%8X ",
  239. ((INT32)Row - 2 + HBufferImage.LowVisibleRow - 1) * 0x10
  240. );
  241. }
  242. for (Index = 0; Index < 0x08 && Index < Line->Size; Index++) {
  243. BeNewColor = FALSE;
  244. if (Selected) {
  245. if (Index + 1 >= ColStart && Index + 1 <= ColEnd) {
  246. BeNewColor = TRUE;
  247. }
  248. }
  249. if (BeNewColor) {
  250. gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);
  251. } else {
  252. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);
  253. }
  254. Pos = 10 + (Index * 3);
  255. if (Line->Buffer[Index] < 0x10) {
  256. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"0");
  257. Pos++;
  258. }
  259. if (Index < 0x07) {
  260. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"%x ", Line->Buffer[Index]);
  261. } else {
  262. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"%x ", Line->Buffer[Index]);
  263. }
  264. }
  265. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);
  266. while (Index < 0x08) {
  267. Pos = 10 + (Index * 3);
  268. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L" ");
  269. Index++;
  270. }
  271. while (Index < 0x10 && Index < Line->Size) {
  272. BeNewColor = FALSE;
  273. if (Selected) {
  274. if (Index + 1 >= ColStart && Index + 1 <= ColEnd) {
  275. BeNewColor = TRUE;
  276. }
  277. }
  278. if (BeNewColor) {
  279. gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);
  280. } else {
  281. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);
  282. }
  283. Pos = 10 + (Index * 3) + 1;
  284. if (Line->Buffer[Index] < 0x10) {
  285. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"0");
  286. Pos++;
  287. }
  288. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"%x ", Line->Buffer[Index]);
  289. Index++;
  290. }
  291. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);
  292. while (Index < 0x10) {
  293. Pos = 10 + (Index * 3) + 1;
  294. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L" ");
  295. Index++;
  296. }
  297. //
  298. // restore the original color
  299. //
  300. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data & 0x7F);
  301. //
  302. // PRINT the buffer content
  303. //
  304. if (!HEditorMouseAction) {
  305. for (Index = 0; Index < 0x10 && Index < Line->Size; Index++) {
  306. Pos = ASCII_POSITION + Index;
  307. //
  308. // learned from shelle.h -- IsValidChar
  309. //
  310. if (Line->Buffer[Index] >= L' ') {
  311. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"%c", (CHAR16) Line->Buffer[Index]);
  312. } else {
  313. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L"%c", '.');
  314. }
  315. }
  316. while (Index < 0x10) {
  317. Pos = ASCII_POSITION + Index;
  318. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L" ");
  319. Index++;
  320. }
  321. }
  322. //
  323. // restore the abundant blank in hex edit area to original color
  324. //
  325. if (Selected) {
  326. if (ColEnd <= 7) {
  327. Pos = 10 + (ColEnd - 1) * 3 + 2;
  328. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L" ");
  329. } else if (ColEnd == 8) {
  330. Pos = 10 + (ColEnd - 1) * 3 + 2;
  331. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L" ");
  332. } else {
  333. Pos = 10 + (ColEnd - 1) * 3 + 3;
  334. ShellPrintEx ((INT32)Pos - 1, (INT32)Row - 1, L" ");
  335. }
  336. }
  337. return EFI_SUCCESS;
  338. }
  339. /**
  340. Function to decide if a column number is stored in the high bits.
  341. @param[in] Column The column to examine.
  342. @param[out] FCol The actual column number.
  343. @retval TRUE The actual column was in high bits and is now in FCol.
  344. @retval FALSE There was not a column number in the high bits.
  345. **/
  346. BOOLEAN
  347. HBufferImageIsAtHighBits (
  348. IN UINTN Column,
  349. OUT UINTN *FCol
  350. )
  351. {
  352. Column -= 10;
  353. //
  354. // NOW AFTER THE SUB, Column start from 0
  355. // 23 AND 24 ARE BOTH BLANK
  356. //
  357. if (Column == 24) {
  358. *FCol = 0;
  359. return FALSE;
  360. }
  361. if (Column > 24) {
  362. Column--;
  363. }
  364. *FCol = (Column / 3) + 1;
  365. if (Column % 3 == 0) {
  366. return TRUE;
  367. }
  368. if ((Column % 3 == 2)) {
  369. *FCol = 0;
  370. }
  371. return FALSE;
  372. }
  373. /**
  374. Decide if a point is in the already selected area.
  375. @param[in] MouseRow The row of the point to test.
  376. @param[in] MouseCol The col of the point to test.
  377. @retval TRUE The point is in the selected area.
  378. @retval FALSE The point is not in the selected area.
  379. **/
  380. BOOLEAN
  381. HBufferImageIsInSelectedArea (
  382. IN UINTN MouseRow,
  383. IN UINTN MouseCol
  384. )
  385. {
  386. UINTN FRow;
  387. UINTN RowStart;
  388. UINTN RowEnd;
  389. UINTN ColStart;
  390. UINTN ColEnd;
  391. UINTN MouseColStart;
  392. UINTN MouseColEnd;
  393. //
  394. // judge mouse position whether is in selected area
  395. //
  396. //
  397. // not select
  398. //
  399. if (HMainEditor.SelectStart == 0 || HMainEditor.SelectEnd == 0) {
  400. return FALSE;
  401. }
  402. //
  403. // calculate the select area
  404. //
  405. RowStart = (HMainEditor.SelectStart - 1) / 0x10 + 1;
  406. RowEnd = (HMainEditor.SelectEnd - 1) / 0x10 + 1;
  407. ColStart = (HMainEditor.SelectStart - 1) % 0x10 + 1;
  408. ColEnd = (HMainEditor.SelectEnd - 1) % 0x10 + 1;
  409. FRow = HBufferImage.LowVisibleRow + MouseRow - 2;
  410. if (FRow < RowStart || FRow > RowEnd) {
  411. return FALSE;
  412. }
  413. if (FRow > RowStart) {
  414. ColStart = 1;
  415. }
  416. if (FRow < RowEnd) {
  417. ColEnd = 0x10;
  418. }
  419. MouseColStart = 10 + (ColStart - 1) * 3;
  420. if (ColStart > 8) {
  421. MouseColStart++;
  422. }
  423. MouseColEnd = 10 + (ColEnd - 1) * 3 + 1;
  424. if (ColEnd > 8) {
  425. MouseColEnd++;
  426. }
  427. if (MouseCol < MouseColStart || MouseCol > MouseColEnd) {
  428. return FALSE;
  429. }
  430. return TRUE;
  431. }
  432. /**
  433. Set mouse position according to HBufferImage.MousePosition.
  434. @retval EFI_SUCCESS The operation was successful.
  435. **/
  436. EFI_STATUS
  437. HBufferImageRestoreMousePosition (
  438. VOID
  439. )
  440. {
  441. HEFI_EDITOR_COLOR_UNION Orig;
  442. HEFI_EDITOR_COLOR_UNION New;
  443. UINTN FRow;
  444. UINTN FColumn;
  445. BOOLEAN HasCharacter;
  446. HEFI_EDITOR_LINE *CurrentLine;
  447. HEFI_EDITOR_LINE *Line;
  448. UINT8 Value;
  449. BOOLEAN HighBits;
  450. Line = NULL;
  451. if (HMainEditor.MouseSupported) {
  452. if (HBufferImageMouseNeedRefresh) {
  453. HBufferImageMouseNeedRefresh = FALSE;
  454. //
  455. // if mouse position not moved and only mouse action
  456. // so do not need to refresh mouse position
  457. //
  458. if ((
  459. HBufferImage.MousePosition.Row == HBufferImageBackupVar.MousePosition.Row &&
  460. HBufferImage.MousePosition.Column == HBufferImageBackupVar.MousePosition.Column
  461. ) &&
  462. HEditorMouseAction
  463. ) {
  464. return EFI_SUCCESS;
  465. }
  466. //
  467. // backup the old screen attributes
  468. //
  469. Orig = HMainEditor.ColorAttributes;
  470. New.Data = 0;
  471. New.Colors.Foreground = Orig.Colors.Background & 0xF;
  472. New.Colors.Background = Orig.Colors.Foreground & 0x7;
  473. //
  474. // if in selected area,
  475. // so do not need to refresh mouse
  476. //
  477. if (!HBufferImageIsInSelectedArea (
  478. HBufferImageBackupVar.MousePosition.Row,
  479. HBufferImageBackupVar.MousePosition.Column
  480. )) {
  481. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
  482. } else {
  483. gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);
  484. }
  485. //
  486. // clear the old mouse position
  487. //
  488. FRow = HBufferImage.LowVisibleRow + HBufferImageBackupVar.MousePosition.Row - 2;
  489. HighBits = HBufferImageIsAtHighBits (
  490. HBufferImageBackupVar.MousePosition.Column,
  491. &FColumn
  492. );
  493. HasCharacter = TRUE;
  494. if (FRow > HBufferImage.NumLines || FColumn == 0) {
  495. HasCharacter = FALSE;
  496. } else {
  497. CurrentLine = HBufferImage.CurrentLine;
  498. Line = HMoveLine (FRow - HBufferImage.BufferPosition.Row);
  499. if (Line == NULL || FColumn > Line->Size) {
  500. HasCharacter = FALSE;
  501. }
  502. HBufferImage.CurrentLine = CurrentLine;
  503. }
  504. ShellPrintEx (
  505. (INT32)HBufferImageBackupVar.MousePosition.Column - 1,
  506. (INT32)HBufferImageBackupVar.MousePosition.Row - 1,
  507. L" "
  508. );
  509. if (HasCharacter) {
  510. if (HighBits) {
  511. Value = (UINT8) (Line->Buffer[FColumn - 1] & 0xf0);
  512. Value = (UINT8) (Value >> 4);
  513. } else {
  514. Value = (UINT8) (Line->Buffer[FColumn - 1] & 0xf);
  515. }
  516. ShellPrintEx (
  517. (INT32)HBufferImageBackupVar.MousePosition.Column - 1,
  518. (INT32)HBufferImageBackupVar.MousePosition.Row - 1,
  519. L"%x",
  520. Value
  521. );
  522. }
  523. if (!HBufferImageIsInSelectedArea (
  524. HBufferImage.MousePosition.Row,
  525. HBufferImage.MousePosition.Column
  526. )) {
  527. gST->ConOut->SetAttribute (gST->ConOut, New.Data & 0x7F);
  528. } else {
  529. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
  530. }
  531. //
  532. // clear the old mouse position
  533. //
  534. FRow = HBufferImage.LowVisibleRow + HBufferImage.MousePosition.Row - 2;
  535. HighBits = HBufferImageIsAtHighBits (
  536. HBufferImage.MousePosition.Column,
  537. &FColumn
  538. );
  539. HasCharacter = TRUE;
  540. if (FRow > HBufferImage.NumLines || FColumn == 0) {
  541. HasCharacter = FALSE;
  542. } else {
  543. CurrentLine = HBufferImage.CurrentLine;
  544. Line = HMoveLine (FRow - HBufferImage.BufferPosition.Row);
  545. if (Line == NULL || FColumn > Line->Size) {
  546. HasCharacter = FALSE;
  547. }
  548. HBufferImage.CurrentLine = CurrentLine;
  549. }
  550. ShellPrintEx (
  551. (INT32)HBufferImage.MousePosition.Column - 1,
  552. (INT32)HBufferImage.MousePosition.Row - 1,
  553. L" "
  554. );
  555. if (HasCharacter) {
  556. if (HighBits) {
  557. Value = (UINT8) (Line->Buffer[FColumn - 1] & 0xf0);
  558. Value = (UINT8) (Value >> 4);
  559. } else {
  560. Value = (UINT8) (Line->Buffer[FColumn - 1] & 0xf);
  561. }
  562. ShellPrintEx (
  563. (INT32)HBufferImage.MousePosition.Column - 1,
  564. (INT32)HBufferImage.MousePosition.Row - 1,
  565. L"%x",
  566. Value
  567. );
  568. }
  569. //
  570. // end of HasCharacter
  571. //
  572. gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
  573. }
  574. //
  575. // end of MouseNeedRefresh
  576. //
  577. }
  578. //
  579. // end of MouseSupported
  580. //
  581. return EFI_SUCCESS;
  582. }
  583. /**
  584. Set cursor position according to HBufferImage.DisplayPosition.
  585. @retval EFI_SUCCESS The operation was successful.
  586. **/
  587. EFI_STATUS
  588. HBufferImageRestorePosition (
  589. VOID
  590. )
  591. {
  592. //
  593. // set cursor position
  594. //
  595. gST->ConOut->SetCursorPosition (
  596. gST->ConOut,
  597. HBufferImage.DisplayPosition.Column - 1,
  598. HBufferImage.DisplayPosition.Row - 1
  599. );
  600. return EFI_SUCCESS;
  601. }
  602. /**
  603. Refresh function for HBufferImage.
  604. @retval EFI_SUCCESS The operation was successful.
  605. @retval EFI_LOAD_ERROR A Load error occurred.
  606. **/
  607. EFI_STATUS
  608. HBufferImageRefresh (
  609. VOID
  610. )
  611. {
  612. LIST_ENTRY *Link;
  613. HEFI_EDITOR_LINE *Line;
  614. UINTN Row;
  615. HEFI_EDITOR_COLOR_UNION Orig;
  616. HEFI_EDITOR_COLOR_UNION New;
  617. UINTN StartRow;
  618. UINTN EndRow;
  619. UINTN FStartRow;
  620. UINTN Tmp;
  621. Orig = HMainEditor.ColorAttributes;
  622. New.Data = 0;
  623. New.Colors.Foreground = Orig.Colors.Background;
  624. New.Colors.Background = Orig.Colors.Foreground;
  625. //
  626. // if it's the first time after editor launch, so should refresh
  627. //
  628. if (HEditorFirst == FALSE) {
  629. //
  630. // no definite required refresh
  631. // and file position displayed on screen has not been changed
  632. //
  633. if (!HBufferImageNeedRefresh &&
  634. !HBufferImageOnlyLineNeedRefresh &&
  635. HBufferImageBackupVar.LowVisibleRow == HBufferImage.LowVisibleRow
  636. ) {
  637. HBufferImageRestoreMousePosition ();
  638. HBufferImageRestorePosition ();
  639. return EFI_SUCCESS;
  640. }
  641. }
  642. gST->ConOut->EnableCursor (gST->ConOut, FALSE);
  643. //
  644. // only need to refresh current line
  645. //
  646. if (HBufferImageOnlyLineNeedRefresh && HBufferImageBackupVar.LowVisibleRow == HBufferImage.LowVisibleRow) {
  647. HBufferImagePrintLine (
  648. HBufferImage.CurrentLine,
  649. HBufferImage.DisplayPosition.Row,
  650. HBufferImage.BufferPosition.Row,
  651. Orig,
  652. New
  653. );
  654. } else {
  655. //
  656. // the whole edit area need refresh
  657. //
  658. if (HEditorMouseAction && HMainEditor.SelectStart != 0 && HMainEditor.SelectEnd != 0) {
  659. if (HMainEditor.SelectStart != HMainEditorBackupVar.SelectStart) {
  660. if (HMainEditor.SelectStart >= HMainEditorBackupVar.SelectStart && HMainEditorBackupVar.SelectStart != 0) {
  661. StartRow = (HMainEditorBackupVar.SelectStart - 1) / 0x10 + 1;
  662. } else {
  663. StartRow = (HMainEditor.SelectStart - 1) / 0x10 + 1;
  664. }
  665. } else {
  666. StartRow = (HMainEditor.SelectStart - 1) / 0x10 + 1;
  667. }
  668. if (HMainEditor.SelectEnd <= HMainEditorBackupVar.SelectEnd) {
  669. EndRow = (HMainEditorBackupVar.SelectEnd - 1) / 0x10 + 1;
  670. } else {
  671. EndRow = (HMainEditor.SelectEnd - 1) / 0x10 + 1;
  672. }
  673. //
  674. // swap
  675. //
  676. if (StartRow > EndRow) {
  677. Tmp = StartRow;
  678. StartRow = EndRow;
  679. EndRow = Tmp;
  680. }
  681. FStartRow = StartRow;
  682. StartRow = 2 + StartRow - HBufferImage.LowVisibleRow;
  683. EndRow = 2 + EndRow - HBufferImage.LowVisibleRow;
  684. } else {
  685. //
  686. // not mouse selection actions
  687. //
  688. FStartRow = HBufferImage.LowVisibleRow;
  689. StartRow = 2;
  690. EndRow = (HMainEditor.ScreenSize.Row - 1);
  691. }
  692. //
  693. // no line
  694. //
  695. if (HBufferImage.Lines == NULL) {
  696. HBufferImageRestoreMousePosition ();
  697. HBufferImageRestorePosition ();
  698. gST->ConOut->EnableCursor (gST->ConOut, TRUE);
  699. return EFI_SUCCESS;
  700. }
  701. //
  702. // get the first line that will be displayed
  703. //
  704. Line = HMoveLine (FStartRow - HBufferImage.BufferPosition.Row);
  705. if (Line == NULL) {
  706. gST->ConOut->EnableCursor (gST->ConOut, TRUE);
  707. return EFI_LOAD_ERROR;
  708. }
  709. Link = &(Line->Link);
  710. Row = StartRow;
  711. do {
  712. Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  713. //
  714. // print line at row
  715. //
  716. HBufferImagePrintLine (
  717. Line,
  718. Row,
  719. HBufferImage.LowVisibleRow + Row - 2,
  720. Orig,
  721. New
  722. );
  723. Link = Link->ForwardLink;
  724. Row++;
  725. } while (Link != HBufferImage.ListHead && Row <= EndRow);
  726. while (Row <= EndRow) {
  727. EditorClearLine (Row, HMainEditor.ScreenSize.Column, HMainEditor.ScreenSize.Row);
  728. Row++;
  729. }
  730. //
  731. // while not file end and not screen full
  732. //
  733. }
  734. HBufferImageRestoreMousePosition ();
  735. HBufferImageRestorePosition ();
  736. HBufferImageNeedRefresh = FALSE;
  737. HBufferImageOnlyLineNeedRefresh = FALSE;
  738. gST->ConOut->EnableCursor (gST->ConOut, TRUE);
  739. return EFI_SUCCESS;
  740. }
  741. /**
  742. Read an image into a buffer friom a source.
  743. @param[in] FileName Pointer to the file name. OPTIONAL and ignored if not FileTypeFileBuffer.
  744. @param[in] DiskName Pointer to the disk name. OPTIONAL and ignored if not FileTypeDiskBuffer.
  745. @param[in] DiskOffset Offset into the disk. OPTIONAL and ignored if not FileTypeDiskBuffer.
  746. @param[in] DiskSize Size of the disk buffer. OPTIONAL and ignored if not FileTypeDiskBuffer.
  747. @param[in] MemOffset Offset into the Memory. OPTIONAL and ignored if not FileTypeMemBuffer.
  748. @param[in] MemSize Size of the Memory buffer. OPTIONAL and ignored if not FileTypeMemBuffer.
  749. @param[in] BufferType The type of buffer to save. IGNORED.
  750. @param[in] Recover TRUE for recovermode, FALSE otherwise.
  751. @return EFI_SUCCESS The operation was successful.
  752. **/
  753. EFI_STATUS
  754. HBufferImageRead (
  755. IN CONST CHAR16 *FileName,
  756. IN CONST CHAR16 *DiskName,
  757. IN UINTN DiskOffset,
  758. IN UINTN DiskSize,
  759. IN UINTN MemOffset,
  760. IN UINTN MemSize,
  761. IN EDIT_FILE_TYPE BufferType,
  762. IN BOOLEAN Recover
  763. )
  764. {
  765. EFI_STATUS Status;
  766. EDIT_FILE_TYPE BufferTypeBackup;
  767. //
  768. // variable initialization
  769. //
  770. Status = EFI_SUCCESS;
  771. HBufferImage.BufferType = BufferType;
  772. //
  773. // three types of buffer supported
  774. // file buffer
  775. // disk buffer
  776. // memory buffer
  777. //
  778. BufferTypeBackup = HBufferImage.BufferType;
  779. switch (BufferType) {
  780. case FileTypeFileBuffer:
  781. Status = HFileImageRead (FileName, Recover);
  782. break;
  783. case FileTypeDiskBuffer:
  784. Status = HDiskImageRead (DiskName, DiskOffset, DiskSize, Recover);
  785. break;
  786. case FileTypeMemBuffer:
  787. Status = HMemImageRead (MemOffset, MemSize, Recover);
  788. break;
  789. default:
  790. Status = EFI_NOT_FOUND;
  791. break;
  792. }
  793. if (EFI_ERROR (Status)) {
  794. HBufferImage.BufferType = BufferTypeBackup;
  795. }
  796. return Status;
  797. }
  798. /**
  799. Save the current image.
  800. @param[in] FileName Pointer to the file name. OPTIONAL and ignored if not FileTypeFileBuffer.
  801. @param[in] DiskName Pointer to the disk name. OPTIONAL and ignored if not FileTypeDiskBuffer.
  802. @param[in] DiskOffset Offset into the disk. OPTIONAL and ignored if not FileTypeDiskBuffer.
  803. @param[in] DiskSize Size of the disk buffer. OPTIONAL and ignored if not FileTypeDiskBuffer.
  804. @param[in] MemOffset Offset into the Memory. OPTIONAL and ignored if not FileTypeMemBuffer.
  805. @param[in] MemSize Size of the Memory buffer. OPTIONAL and ignored if not FileTypeMemBuffer.
  806. @param[in] BufferType The type of buffer to save. IGNORED.
  807. @return EFI_SUCCESS The operation was successful.
  808. **/
  809. EFI_STATUS
  810. HBufferImageSave (
  811. IN CHAR16 *FileName,
  812. IN CHAR16 *DiskName,
  813. IN UINTN DiskOffset,
  814. IN UINTN DiskSize,
  815. IN UINTN MemOffset,
  816. IN UINTN MemSize,
  817. IN EDIT_FILE_TYPE BufferType
  818. )
  819. {
  820. EFI_STATUS Status;
  821. EDIT_FILE_TYPE BufferTypeBackup;
  822. //
  823. // variable initialization
  824. //
  825. Status = EFI_SUCCESS;
  826. BufferTypeBackup = HBufferImage.BufferType;
  827. switch (HBufferImage.BufferType) {
  828. //
  829. // file buffer
  830. //
  831. case FileTypeFileBuffer:
  832. Status = HFileImageSave (FileName);
  833. break;
  834. //
  835. // disk buffer
  836. //
  837. case FileTypeDiskBuffer:
  838. Status = HDiskImageSave (DiskName, DiskOffset, DiskSize);
  839. break;
  840. //
  841. // memory buffer
  842. //
  843. case FileTypeMemBuffer:
  844. Status = HMemImageSave (MemOffset, MemSize);
  845. break;
  846. default:
  847. Status = EFI_NOT_FOUND;
  848. break;
  849. }
  850. if (EFI_ERROR (Status)) {
  851. HBufferImage.BufferType = BufferTypeBackup;
  852. }
  853. return Status;
  854. }
  855. /**
  856. Create a new line and append it to the line list.
  857. Fields affected:
  858. NumLines
  859. Lines
  860. @retval NULL create line failed.
  861. @return the line created.
  862. **/
  863. HEFI_EDITOR_LINE *
  864. HBufferImageCreateLine (
  865. VOID
  866. )
  867. {
  868. HEFI_EDITOR_LINE *Line;
  869. //
  870. // allocate for line structure
  871. //
  872. Line = AllocateZeroPool (sizeof (HEFI_EDITOR_LINE));
  873. if (Line == NULL) {
  874. return NULL;
  875. }
  876. Line->Signature = EFI_EDITOR_LINE_LIST;
  877. Line->Size = 0;
  878. HBufferImage.NumLines++;
  879. //
  880. // insert to line list
  881. //
  882. InsertTailList (HBufferImage.ListHead, &Line->Link);
  883. if (HBufferImage.Lines == NULL) {
  884. HBufferImage.Lines = CR (
  885. HBufferImage.ListHead->ForwardLink,
  886. HEFI_EDITOR_LINE,
  887. Link,
  888. EFI_EDITOR_LINE_LIST
  889. );
  890. }
  891. return Line;
  892. }
  893. /**
  894. Free the current image.
  895. @retval EFI_SUCCESS The operation was successful.
  896. **/
  897. EFI_STATUS
  898. HBufferImageFree (
  899. VOID
  900. )
  901. {
  902. //
  903. // free all lines
  904. //
  905. HBufferImageFreeLines ();
  906. return EFI_SUCCESS;
  907. }
  908. /**
  909. change char to int value based on Hex.
  910. @param[in] Char The input char.
  911. @return The character's index value.
  912. @retval -1 The operation failed.
  913. **/
  914. INTN
  915. HBufferImageCharToHex (
  916. IN CHAR16 Char
  917. )
  918. {
  919. //
  920. // change the character to hex
  921. //
  922. if (Char >= L'0' && Char <= L'9') {
  923. return (Char - L'0');
  924. }
  925. if (Char >= L'a' && Char <= L'f') {
  926. return (Char - L'a' + 10);
  927. }
  928. if (Char >= L'A' && Char <= L'F') {
  929. return (Char - L'A' + 10);
  930. }
  931. return -1;
  932. }
  933. /**
  934. Add character.
  935. @param[in] Char -- input char.
  936. @retval EFI_SUCCESS The operation was successful.
  937. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  938. **/
  939. EFI_STATUS
  940. HBufferImageAddChar (
  941. IN CHAR16 Char
  942. )
  943. {
  944. HEFI_EDITOR_LINE *Line;
  945. HEFI_EDITOR_LINE *NewLine;
  946. INTN Value;
  947. UINT8 Old;
  948. UINTN FRow;
  949. UINTN FCol;
  950. BOOLEAN High;
  951. Value = HBufferImageCharToHex (Char);
  952. //
  953. // invalid input
  954. //
  955. if (Value == -1) {
  956. return EFI_SUCCESS;
  957. }
  958. Line = HBufferImage.CurrentLine;
  959. FRow = HBufferImage.BufferPosition.Row;
  960. FCol = HBufferImage.BufferPosition.Column;
  961. High = HBufferImage.HighBits;
  962. //
  963. // only needs to refresh current line
  964. //
  965. HBufferImageOnlyLineNeedRefresh = TRUE;
  966. //
  967. // not a full line and beyond the last character
  968. //
  969. if (FCol > Line->Size) {
  970. //
  971. // cursor always at high 4 bits
  972. // and always put input to the low 4 bits
  973. //
  974. Line->Buffer[Line->Size] = (UINT8) Value;
  975. Line->Size++;
  976. High = FALSE;
  977. } else {
  978. Old = Line->Buffer[FCol - 1];
  979. //
  980. // always put the input to the low 4 bits
  981. //
  982. Old = (UINT8) (Old & 0x0f);
  983. Old = (UINT8) (Old << 4);
  984. Old = (UINT8) (Value + Old);
  985. Line->Buffer[FCol - 1] = Old;
  986. //
  987. // at the low 4 bits of the last character of a full line
  988. // so if no next line, need to create a new line
  989. //
  990. if (!High && FCol == 0x10) {
  991. HBufferImageOnlyLineNeedRefresh = FALSE;
  992. HBufferImageNeedRefresh = TRUE;
  993. if (Line->Link.ForwardLink == HBufferImage.ListHead) {
  994. //
  995. // last line
  996. //
  997. // create a new line
  998. //
  999. NewLine = HBufferImageCreateLine ();
  1000. if (NewLine == NULL) {
  1001. return EFI_OUT_OF_RESOURCES;
  1002. }
  1003. //
  1004. // end of NULL
  1005. //
  1006. }
  1007. //
  1008. // end of == ListHead
  1009. //
  1010. }
  1011. //
  1012. // end of == 0x10
  1013. //
  1014. // if already at end of this line, scroll it to the start of next line
  1015. //
  1016. if (FCol == 0x10 && !High) {
  1017. //
  1018. // definitely has next line
  1019. //
  1020. FRow++;
  1021. FCol = 1;
  1022. High = TRUE;
  1023. } else {
  1024. //
  1025. // if not at end of this line, just move to next column
  1026. //
  1027. if (!High) {
  1028. FCol++;
  1029. }
  1030. if (High) {
  1031. High = FALSE;
  1032. } else {
  1033. High = TRUE;
  1034. }
  1035. }
  1036. //
  1037. // end of ==FALSE
  1038. //
  1039. }
  1040. //
  1041. // move cursor to right
  1042. //
  1043. HBufferImageMovePosition (FRow, FCol, High);
  1044. if (!HBufferImage.Modified) {
  1045. HBufferImage.Modified = TRUE;
  1046. }
  1047. return EFI_SUCCESS;
  1048. }
  1049. /**
  1050. Delete the previous character.
  1051. @retval EFI_SUCCESS The operationw as successful.
  1052. **/
  1053. EFI_STATUS
  1054. HBufferImageDoBackspace (
  1055. VOID
  1056. )
  1057. {
  1058. HEFI_EDITOR_LINE *Line;
  1059. UINTN FileColumn;
  1060. UINTN FPos;
  1061. BOOLEAN LastLine;
  1062. //
  1063. // variable initialization
  1064. //
  1065. LastLine = FALSE;
  1066. //
  1067. // already the first character
  1068. //
  1069. if (HBufferImage.BufferPosition.Row == 1 && HBufferImage.BufferPosition.Column == 1) {
  1070. return EFI_SUCCESS;
  1071. }
  1072. FPos = (HBufferImage.BufferPosition.Row - 1) * 0x10 + HBufferImage.BufferPosition.Column - 1;
  1073. FileColumn = HBufferImage.BufferPosition.Column;
  1074. Line = HBufferImage.CurrentLine;
  1075. LastLine = FALSE;
  1076. if (Line->Link.ForwardLink == HBufferImage.ListHead && FileColumn > 1) {
  1077. LastLine = TRUE;
  1078. }
  1079. HBufferImageDeleteCharacterFromBuffer (FPos - 1, 1, NULL);
  1080. //
  1081. // if is the last line
  1082. // then only this line need to be refreshed
  1083. //
  1084. if (LastLine) {
  1085. HBufferImageNeedRefresh = FALSE;
  1086. HBufferImageOnlyLineNeedRefresh = TRUE;
  1087. } else {
  1088. HBufferImageNeedRefresh = TRUE;
  1089. HBufferImageOnlyLineNeedRefresh = FALSE;
  1090. }
  1091. if (!HBufferImage.Modified) {
  1092. HBufferImage.Modified = TRUE;
  1093. }
  1094. return EFI_SUCCESS;
  1095. }
  1096. /**
  1097. ASCII key + Backspace + return.
  1098. @param[in] Char The input char.
  1099. @retval EFI_SUCCESS The operation was successful.
  1100. @retval EFI_LOAD_ERROR A load error occurred.
  1101. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  1102. **/
  1103. EFI_STATUS
  1104. HBufferImageDoCharInput (
  1105. IN CHAR16 Char
  1106. )
  1107. {
  1108. EFI_STATUS Status;
  1109. Status = EFI_SUCCESS;
  1110. switch (Char) {
  1111. case 0:
  1112. break;
  1113. case 0x08:
  1114. Status = HBufferImageDoBackspace ();
  1115. break;
  1116. case 0x09:
  1117. case 0x0a:
  1118. case 0x0d:
  1119. //
  1120. // Tabs, Returns are thought as nothing
  1121. //
  1122. break;
  1123. default:
  1124. //
  1125. // DEAL WITH ASCII CHAR, filter out thing like ctrl+f
  1126. //
  1127. if (Char > 127 || Char < 32) {
  1128. Status = StatusBarSetStatusString (L"Unknown Command");
  1129. } else {
  1130. Status = HBufferImageAddChar (Char);
  1131. }
  1132. break;
  1133. }
  1134. return Status;
  1135. }
  1136. /**
  1137. Check user specified FileRow is above current screen.
  1138. @param[in] FileRow Row of file position ( start from 1 ).
  1139. @retval TRUE It is above the current screen.
  1140. @retval FALSE It is not above the current screen.
  1141. **/
  1142. BOOLEAN
  1143. HAboveCurrentScreen (
  1144. IN UINTN FileRow
  1145. )
  1146. {
  1147. if (FileRow < HBufferImage.LowVisibleRow) {
  1148. return TRUE;
  1149. }
  1150. return FALSE;
  1151. }
  1152. /**
  1153. Check user specified FileRow is under current screen.
  1154. @param[in] FileRow Row of file position ( start from 1 ).
  1155. @retval TRUE It is under the current screen.
  1156. @retval FALSE It is not under the current screen.
  1157. **/
  1158. BOOLEAN
  1159. HUnderCurrentScreen (
  1160. IN UINTN FileRow
  1161. )
  1162. {
  1163. if (FileRow > HBufferImage.LowVisibleRow + (HMainEditor.ScreenSize.Row - 2) - 1) {
  1164. return TRUE;
  1165. }
  1166. return FALSE;
  1167. }
  1168. /**
  1169. According to cursor's file position, adjust screen display.
  1170. @param[in] NewFilePosRow Row of file position ( start from 1 ).
  1171. @param[in] NewFilePosCol Column of file position ( start from 1 ).
  1172. @param[in] HighBits Cursor will on high4 bits or low4 bits.
  1173. **/
  1174. VOID
  1175. HBufferImageMovePosition (
  1176. IN UINTN NewFilePosRow,
  1177. IN UINTN NewFilePosCol,
  1178. IN BOOLEAN HighBits
  1179. )
  1180. {
  1181. INTN RowGap;
  1182. UINTN Abs;
  1183. BOOLEAN Above;
  1184. BOOLEAN Under;
  1185. UINTN NewDisplayCol;
  1186. //
  1187. // CALCULATE gap between current file position and new file position
  1188. //
  1189. RowGap = NewFilePosRow - HBufferImage.BufferPosition.Row;
  1190. Under = HUnderCurrentScreen (NewFilePosRow);
  1191. Above = HAboveCurrentScreen (NewFilePosRow);
  1192. HBufferImage.HighBits = HighBits;
  1193. //
  1194. // if is below current screen
  1195. //
  1196. if (Under) {
  1197. //
  1198. // display row will be unchanged
  1199. //
  1200. HBufferImage.BufferPosition.Row = NewFilePosRow;
  1201. } else {
  1202. if (Above) {
  1203. //
  1204. // has enough above line, so display row unchanged
  1205. // not has enough above lines, so the first line is
  1206. // at the first display line
  1207. //
  1208. if (NewFilePosRow < (HBufferImage.DisplayPosition.Row - 2 + 1)) {
  1209. HBufferImage.DisplayPosition.Row = NewFilePosRow + 2 - 1;
  1210. }
  1211. HBufferImage.BufferPosition.Row = NewFilePosRow;
  1212. } else {
  1213. //
  1214. // in current screen
  1215. //
  1216. HBufferImage.BufferPosition.Row = NewFilePosRow;
  1217. if (RowGap <= 0) {
  1218. Abs = (UINTN)ABS(RowGap);
  1219. HBufferImage.DisplayPosition.Row -= Abs;
  1220. } else {
  1221. HBufferImage.DisplayPosition.Row += RowGap;
  1222. }
  1223. }
  1224. }
  1225. HBufferImage.LowVisibleRow = HBufferImage.BufferPosition.Row - (HBufferImage.DisplayPosition.Row - 2);
  1226. //
  1227. // always in current screen
  1228. //
  1229. HBufferImage.BufferPosition.Column = NewFilePosCol;
  1230. NewDisplayCol = 10 + (NewFilePosCol - 1) * 3;
  1231. if (NewFilePosCol > 0x8) {
  1232. NewDisplayCol++;
  1233. }
  1234. if (!HighBits) {
  1235. NewDisplayCol++;
  1236. }
  1237. HBufferImage.DisplayPosition.Column = NewDisplayCol;
  1238. //
  1239. // let CurrentLine point to correct line;
  1240. //
  1241. HBufferImage.CurrentLine = HMoveCurrentLine (RowGap);
  1242. }
  1243. /**
  1244. Scroll cursor to right.
  1245. @retval EFI_SUCCESS The operation was successful.
  1246. **/
  1247. EFI_STATUS
  1248. HBufferImageScrollRight (
  1249. VOID
  1250. )
  1251. {
  1252. HEFI_EDITOR_LINE *Line;
  1253. UINTN FRow;
  1254. UINTN FCol;
  1255. //
  1256. // scroll right will always move to the high4 bits of the next character
  1257. //
  1258. HBufferImageNeedRefresh = FALSE;
  1259. HBufferImageOnlyLineNeedRefresh = FALSE;
  1260. Line = HBufferImage.CurrentLine;
  1261. FRow = HBufferImage.BufferPosition.Row;
  1262. FCol = HBufferImage.BufferPosition.Column;
  1263. //
  1264. // this line is not full and no next line
  1265. //
  1266. if (FCol > Line->Size) {
  1267. return EFI_SUCCESS;
  1268. }
  1269. //
  1270. // if already at end of this line, scroll it to the start of next line
  1271. //
  1272. if (FCol == 0x10) {
  1273. //
  1274. // has next line
  1275. //
  1276. if (Line->Link.ForwardLink != HBufferImage.ListHead) {
  1277. FRow++;
  1278. FCol = 1;
  1279. } else {
  1280. return EFI_SUCCESS;
  1281. }
  1282. } else {
  1283. //
  1284. // if not at end of this line, just move to next column
  1285. //
  1286. FCol++;
  1287. }
  1288. HBufferImageMovePosition (FRow, FCol, TRUE);
  1289. return EFI_SUCCESS;
  1290. }
  1291. /**
  1292. Scroll cursor to left.
  1293. @retval EFI_SUCCESS The operation was successful.
  1294. **/
  1295. EFI_STATUS
  1296. HBufferImageScrollLeft (
  1297. VOID
  1298. )
  1299. {
  1300. HEFI_EDITOR_LINE *Line;
  1301. UINTN FRow;
  1302. UINTN FCol;
  1303. HBufferImageNeedRefresh = FALSE;
  1304. HBufferImageOnlyLineNeedRefresh = FALSE;
  1305. Line = HBufferImage.CurrentLine;
  1306. FRow = HBufferImage.BufferPosition.Row;
  1307. FCol = HBufferImage.BufferPosition.Column;
  1308. //
  1309. // if already at start of this line, so move to the end of previous line
  1310. //
  1311. if (FCol <= 1) {
  1312. //
  1313. // has previous line
  1314. //
  1315. if (Line->Link.BackLink != HBufferImage.ListHead) {
  1316. FRow--;
  1317. Line = CR (Line->Link.BackLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  1318. FCol = Line->Size;
  1319. } else {
  1320. return EFI_SUCCESS;
  1321. }
  1322. } else {
  1323. //
  1324. // if not at start of this line, just move to previous column
  1325. //
  1326. FCol--;
  1327. }
  1328. HBufferImageMovePosition (FRow, FCol, TRUE);
  1329. return EFI_SUCCESS;
  1330. }
  1331. /**
  1332. Scroll cursor to the next line
  1333. @retval EFI_SUCCESS The operation was successful.
  1334. **/
  1335. EFI_STATUS
  1336. HBufferImageScrollDown (
  1337. VOID
  1338. )
  1339. {
  1340. HEFI_EDITOR_LINE *Line;
  1341. UINTN FRow;
  1342. UINTN FCol;
  1343. BOOLEAN HighBits;
  1344. Line = HBufferImage.CurrentLine;
  1345. FRow = HBufferImage.BufferPosition.Row;
  1346. FCol = HBufferImage.BufferPosition.Column;
  1347. HighBits = HBufferImage.HighBits;
  1348. //
  1349. // has next line
  1350. //
  1351. if (Line->Link.ForwardLink != HBufferImage.ListHead) {
  1352. FRow++;
  1353. Line = CR (Line->Link.ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  1354. //
  1355. // if the next line is not that long, so move to end of next line
  1356. //
  1357. if (FCol > Line->Size) {
  1358. FCol = Line->Size + 1;
  1359. HighBits = TRUE;
  1360. }
  1361. } else {
  1362. return EFI_SUCCESS;
  1363. }
  1364. HBufferImageMovePosition (FRow, FCol, HighBits);
  1365. return EFI_SUCCESS;
  1366. }
  1367. /**
  1368. Scroll cursor to previous line
  1369. @retval EFI_SUCCESS The operation was successful.
  1370. **/
  1371. EFI_STATUS
  1372. HBufferImageScrollUp (
  1373. VOID
  1374. )
  1375. {
  1376. HEFI_EDITOR_LINE *Line;
  1377. UINTN FRow;
  1378. UINTN FCol;
  1379. Line = HBufferImage.CurrentLine;
  1380. FRow = HBufferImage.BufferPosition.Row;
  1381. FCol = HBufferImage.BufferPosition.Column;
  1382. //
  1383. // has previous line
  1384. //
  1385. if (Line->Link.BackLink != HBufferImage.ListHead) {
  1386. FRow--;
  1387. } else {
  1388. return EFI_SUCCESS;
  1389. }
  1390. HBufferImageMovePosition (FRow, FCol, HBufferImage.HighBits);
  1391. return EFI_SUCCESS;
  1392. }
  1393. /**
  1394. Scroll cursor to next page
  1395. @retval EFI_SUCCESS The operation was successful.
  1396. **/
  1397. EFI_STATUS
  1398. HBufferImagePageDown (
  1399. VOID
  1400. )
  1401. {
  1402. HEFI_EDITOR_LINE *Line;
  1403. UINTN FRow;
  1404. UINTN FCol;
  1405. UINTN Gap;
  1406. BOOLEAN HighBits;
  1407. Line = HBufferImage.CurrentLine;
  1408. FRow = HBufferImage.BufferPosition.Row;
  1409. FCol = HBufferImage.BufferPosition.Column;
  1410. HighBits = HBufferImage.HighBits;
  1411. //
  1412. // has next page
  1413. //
  1414. if (HBufferImage.NumLines >= FRow + (HMainEditor.ScreenSize.Row - 2)) {
  1415. Gap = (HMainEditor.ScreenSize.Row - 2);
  1416. } else {
  1417. //
  1418. // MOVE CURSOR TO LAST LINE
  1419. //
  1420. Gap = HBufferImage.NumLines - FRow;
  1421. }
  1422. //
  1423. // get correct line
  1424. //
  1425. Line = HMoveLine (Gap);
  1426. //
  1427. // if that line, is not that long, so move to the end of that line
  1428. //
  1429. if (Line != NULL && FCol > Line->Size) {
  1430. FCol = Line->Size + 1;
  1431. HighBits = TRUE;
  1432. }
  1433. FRow += Gap;
  1434. HBufferImageMovePosition (FRow, FCol, HighBits);
  1435. return EFI_SUCCESS;
  1436. }
  1437. /**
  1438. Scroll cursor to previous page
  1439. @retval EFI_SUCCESS The operation was successful.
  1440. **/
  1441. EFI_STATUS
  1442. HBufferImagePageUp (
  1443. VOID
  1444. )
  1445. {
  1446. UINTN FRow;
  1447. UINTN FCol;
  1448. UINTN Gap;
  1449. INTN Retreat;
  1450. FRow = HBufferImage.BufferPosition.Row;
  1451. FCol = HBufferImage.BufferPosition.Column;
  1452. //
  1453. // has previous page
  1454. //
  1455. if (FRow > (HMainEditor.ScreenSize.Row - 2)) {
  1456. Gap = (HMainEditor.ScreenSize.Row - 2);
  1457. } else {
  1458. //
  1459. // the first line of file will displayed on the first line of screen
  1460. //
  1461. Gap = FRow - 1;
  1462. }
  1463. Retreat = Gap;
  1464. Retreat = -Retreat;
  1465. FRow -= Gap;
  1466. HBufferImageMovePosition (FRow, FCol, HBufferImage.HighBits);
  1467. return EFI_SUCCESS;
  1468. }
  1469. /**
  1470. Scroll cursor to start of line
  1471. @retval EFI_SUCCESS The operation was successful.
  1472. **/
  1473. EFI_STATUS
  1474. HBufferImageHome (
  1475. VOID
  1476. )
  1477. {
  1478. UINTN FRow;
  1479. UINTN FCol;
  1480. BOOLEAN HighBits;
  1481. //
  1482. // curosr will at the high bit
  1483. //
  1484. FRow = HBufferImage.BufferPosition.Row;
  1485. FCol = 1;
  1486. HighBits = TRUE;
  1487. //
  1488. // move cursor position
  1489. //
  1490. HBufferImageMovePosition (FRow, FCol, HighBits);
  1491. return EFI_SUCCESS;
  1492. }
  1493. /**
  1494. Scroll cursor to end of line.
  1495. @retval EFI_SUCCESS Teh operation was successful.
  1496. **/
  1497. EFI_STATUS
  1498. HBufferImageEnd (
  1499. VOID
  1500. )
  1501. {
  1502. HEFI_EDITOR_LINE *Line;
  1503. UINTN FRow;
  1504. UINTN FCol;
  1505. BOOLEAN HighBits;
  1506. //
  1507. // need refresh mouse
  1508. //
  1509. HBufferImageMouseNeedRefresh = TRUE;
  1510. Line = HBufferImage.CurrentLine;
  1511. FRow = HBufferImage.BufferPosition.Row;
  1512. if (Line->Size == 0x10) {
  1513. FCol = Line->Size;
  1514. HighBits = FALSE;
  1515. } else {
  1516. FCol = Line->Size + 1;
  1517. HighBits = TRUE;
  1518. }
  1519. //
  1520. // move cursor position
  1521. //
  1522. HBufferImageMovePosition (FRow, FCol, HighBits);
  1523. return EFI_SUCCESS;
  1524. }
  1525. /**
  1526. Get the size of the open buffer.
  1527. @retval The size in bytes.
  1528. **/
  1529. UINTN
  1530. HBufferImageGetTotalSize (
  1531. VOID
  1532. )
  1533. {
  1534. UINTN Size;
  1535. HEFI_EDITOR_LINE *Line;
  1536. //
  1537. // calculate the total size of whole line list's buffer
  1538. //
  1539. if (HBufferImage.Lines == NULL) {
  1540. return 0;
  1541. }
  1542. Line = CR (
  1543. HBufferImage.ListHead->BackLink,
  1544. HEFI_EDITOR_LINE,
  1545. Link,
  1546. EFI_EDITOR_LINE_LIST
  1547. );
  1548. //
  1549. // one line at most 0x10
  1550. //
  1551. Size = 0x10 * (HBufferImage.NumLines - 1) + Line->Size;
  1552. return Size;
  1553. }
  1554. /**
  1555. Delete character from buffer.
  1556. @param[in] Pos Position, Pos starting from 0.
  1557. @param[in] Count The Count of characters to delete.
  1558. @param[out] DeleteBuffer The DeleteBuffer.
  1559. @retval EFI_SUCCESS Success
  1560. **/
  1561. EFI_STATUS
  1562. HBufferImageDeleteCharacterFromBuffer (
  1563. IN UINTN Pos,
  1564. IN UINTN Count,
  1565. OUT UINT8 *DeleteBuffer
  1566. )
  1567. {
  1568. UINTN Index;
  1569. VOID *Buffer;
  1570. UINT8 *BufferPtr;
  1571. UINTN Size;
  1572. HEFI_EDITOR_LINE *Line;
  1573. LIST_ENTRY *Link;
  1574. UINTN OldFCol;
  1575. UINTN OldFRow;
  1576. UINTN OldPos;
  1577. UINTN NewPos;
  1578. EFI_STATUS Status;
  1579. Size = HBufferImageGetTotalSize ();
  1580. if (Size < Count) {
  1581. return EFI_LOAD_ERROR;
  1582. }
  1583. if (Size == 0) {
  1584. return EFI_SUCCESS;
  1585. }
  1586. //
  1587. // relocate all the HBufferImage fields
  1588. //
  1589. OldFRow = HBufferImage.BufferPosition.Row;
  1590. OldFCol = HBufferImage.BufferPosition.Column;
  1591. OldPos = (OldFRow - 1) * 0x10 + OldFCol - 1;
  1592. if (Pos > 0) {
  1593. //
  1594. // has character before it,
  1595. // so locate according to block's previous character
  1596. //
  1597. NewPos = Pos - 1;
  1598. } else {
  1599. //
  1600. // has no character before it,
  1601. // so locate according to block's next character
  1602. //
  1603. NewPos = 0;
  1604. }
  1605. HBufferImageMovePosition (NewPos / 0x10 + 1, NewPos % 0x10 + 1, TRUE);
  1606. Buffer = AllocateZeroPool (Size);
  1607. if (Buffer == NULL) {
  1608. return EFI_OUT_OF_RESOURCES;
  1609. }
  1610. HBufferImageListToBuffer (Buffer, Size);
  1611. BufferPtr = (UINT8 *) Buffer;
  1612. //
  1613. // pass deleted buffer out
  1614. //
  1615. if (DeleteBuffer != NULL) {
  1616. for (Index = 0; Index < Count; Index++) {
  1617. DeleteBuffer[Index] = BufferPtr[Pos + Index];
  1618. }
  1619. }
  1620. //
  1621. // delete the part from Pos
  1622. //
  1623. for (Index = Pos; Index < Size - Count; Index++) {
  1624. BufferPtr[Index] = BufferPtr[Index + Count];
  1625. }
  1626. Size -= Count;
  1627. HBufferImageFreeLines ();
  1628. Status = HBufferImageBufferToList (Buffer, Size);
  1629. FreePool (Buffer);
  1630. if (EFI_ERROR (Status)) {
  1631. return Status;
  1632. }
  1633. Link = HMainEditor.BufferImage->ListHead->ForwardLink;
  1634. for (Index = 0; Index < NewPos / 0x10; Index++) {
  1635. Link = Link->ForwardLink;
  1636. }
  1637. Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  1638. HBufferImage.CurrentLine = Line;
  1639. //
  1640. // if current cursor position if inside select area
  1641. // then move it to the block's NEXT character
  1642. //
  1643. if (OldPos >= Pos && OldPos < (Pos + Count)) {
  1644. NewPos = Pos;
  1645. } else {
  1646. if (OldPos < Pos) {
  1647. NewPos = OldPos;
  1648. } else {
  1649. NewPos = OldPos - Count;
  1650. }
  1651. }
  1652. HBufferImageMovePosition (NewPos / 0x10 + 1, NewPos % 0x10 + 1, TRUE);
  1653. return EFI_SUCCESS;
  1654. }
  1655. /**
  1656. Add character to buffer, add before pos.
  1657. @param[in] Pos Position, Pos starting from 0.
  1658. @param[in] Count Count of characters to add.
  1659. @param[in] AddBuffer Add buffer.
  1660. @retval EFI_SUCCESS Success.
  1661. **/
  1662. EFI_STATUS
  1663. HBufferImageAddCharacterToBuffer (
  1664. IN UINTN Pos,
  1665. IN UINTN Count,
  1666. IN UINT8 *AddBuffer
  1667. )
  1668. {
  1669. INTN Index;
  1670. VOID *Buffer;
  1671. UINT8 *BufferPtr;
  1672. UINTN Size;
  1673. HEFI_EDITOR_LINE *Line;
  1674. LIST_ENTRY *Link;
  1675. UINTN OldFCol;
  1676. UINTN OldFRow;
  1677. UINTN OldPos;
  1678. UINTN NewPos;
  1679. Size = HBufferImageGetTotalSize ();
  1680. //
  1681. // relocate all the HBufferImage fields
  1682. //
  1683. OldFRow = HBufferImage.BufferPosition.Row;
  1684. OldFCol = HBufferImage.BufferPosition.Column;
  1685. OldPos = (OldFRow - 1) * 0x10 + OldFCol - 1;
  1686. //
  1687. // move cursor before Pos
  1688. //
  1689. if (Pos > 0) {
  1690. NewPos = Pos - 1;
  1691. } else {
  1692. NewPos = 0;
  1693. }
  1694. HBufferImageMovePosition (NewPos / 0x10 + 1, NewPos % 0x10 + 1, TRUE);
  1695. Buffer = AllocateZeroPool (Size + Count);
  1696. if (Buffer == NULL) {
  1697. return EFI_OUT_OF_RESOURCES;
  1698. }
  1699. HBufferImageListToBuffer (Buffer, Size);
  1700. BufferPtr = (UINT8 *) Buffer;
  1701. //
  1702. // get a place to add
  1703. //
  1704. for (Index = (INTN) (Size + Count - 1); Index >= (INTN) Pos; Index--) {
  1705. BufferPtr[Index] = BufferPtr[Index - Count];
  1706. }
  1707. //
  1708. // add the buffer
  1709. //
  1710. for (Index = (INTN) 0; Index < (INTN) Count; Index++) {
  1711. BufferPtr[Index + Pos] = AddBuffer[Index];
  1712. }
  1713. Size += Count;
  1714. HBufferImageFreeLines ();
  1715. HBufferImageBufferToList (Buffer, Size);
  1716. FreePool (Buffer);
  1717. Link = HMainEditor.BufferImage->ListHead->ForwardLink;
  1718. for (Index = 0; Index < (INTN) NewPos / 0x10; Index++) {
  1719. Link = Link->ForwardLink;
  1720. }
  1721. Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  1722. HBufferImage.CurrentLine = Line;
  1723. if (OldPos >= Pos) {
  1724. NewPos = OldPos + Count;
  1725. } else {
  1726. NewPos = OldPos;
  1727. }
  1728. HBufferImageMovePosition (NewPos / 0x10 + 1, NewPos % 0x10 + 1, TRUE);
  1729. return EFI_SUCCESS;
  1730. }
  1731. /**
  1732. Delete current character from line.
  1733. @retval EFI_SUCCESS The operationw as successful.
  1734. **/
  1735. EFI_STATUS
  1736. HBufferImageDoDelete (
  1737. VOID
  1738. )
  1739. {
  1740. HEFI_EDITOR_LINE *Line;
  1741. BOOLEAN LastLine;
  1742. UINTN FileColumn;
  1743. UINTN FPos;
  1744. FPos = (HBufferImage.BufferPosition.Row - 1) * 0x10 + HBufferImage.BufferPosition.Column - 1;
  1745. FileColumn = HBufferImage.BufferPosition.Column;
  1746. Line = HBufferImage.CurrentLine;
  1747. //
  1748. // if beyond the last character
  1749. //
  1750. if (FileColumn > Line->Size) {
  1751. return EFI_SUCCESS;
  1752. }
  1753. LastLine = FALSE;
  1754. if (Line->Link.ForwardLink == HBufferImage.ListHead) {
  1755. LastLine = TRUE;
  1756. }
  1757. HBufferImageDeleteCharacterFromBuffer (FPos, 1, NULL);
  1758. //
  1759. // if is the last line
  1760. // then only this line need to be refreshed
  1761. //
  1762. if (LastLine) {
  1763. HBufferImageNeedRefresh = FALSE;
  1764. HBufferImageOnlyLineNeedRefresh = TRUE;
  1765. } else {
  1766. HBufferImageNeedRefresh = TRUE;
  1767. HBufferImageOnlyLineNeedRefresh = FALSE;
  1768. }
  1769. if (!HBufferImage.Modified) {
  1770. HBufferImage.Modified = TRUE;
  1771. }
  1772. return EFI_SUCCESS;
  1773. }
  1774. /**
  1775. Change the raw buffer to a list of lines for the UI.
  1776. @param[in] Buffer The pointer to the buffer to fill.
  1777. @param[in] Bytes The size of the buffer in bytes.
  1778. @retval EFI_SUCCESS The operation was successful.
  1779. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  1780. **/
  1781. EFI_STATUS
  1782. HBufferImageBufferToList (
  1783. IN VOID *Buffer,
  1784. IN UINTN Bytes
  1785. )
  1786. {
  1787. UINTN TempI;
  1788. UINTN TempJ;
  1789. UINTN Left;
  1790. HEFI_EDITOR_LINE *Line;
  1791. UINT8 *BufferPtr;
  1792. TempI = 0;
  1793. Left = 0;
  1794. BufferPtr = (UINT8 *) Buffer;
  1795. //
  1796. // parse file content line by line
  1797. //
  1798. while (TempI < Bytes) {
  1799. if (Bytes - TempI >= 0x10) {
  1800. Left = 0x10;
  1801. } else {
  1802. Left = Bytes - TempI;
  1803. }
  1804. //
  1805. // allocate a new line
  1806. //
  1807. Line = HBufferImageCreateLine ();
  1808. if (Line == NULL) {
  1809. return EFI_OUT_OF_RESOURCES;
  1810. }
  1811. Line->Size = Left;
  1812. for (TempJ = 0; TempJ < Left; TempJ++) {
  1813. Line->Buffer[TempJ] = BufferPtr[TempI];
  1814. TempI++;
  1815. }
  1816. }
  1817. //
  1818. // last line is a full line, SO create a new line
  1819. //
  1820. if (Left == 0x10 || Bytes == 0) {
  1821. Line = HBufferImageCreateLine ();
  1822. if (Line == NULL) {
  1823. return EFI_OUT_OF_RESOURCES;
  1824. }
  1825. }
  1826. return EFI_SUCCESS;
  1827. }
  1828. /**
  1829. Change the list of lines from the UI to a raw buffer.
  1830. @param[in] Buffer The pointer to the buffer to fill.
  1831. @param[in] Bytes The size of the buffer in bytes.
  1832. @retval EFI_SUCCESS The operation was successful.
  1833. **/
  1834. EFI_STATUS
  1835. HBufferImageListToBuffer (
  1836. IN VOID *Buffer,
  1837. IN UINTN Bytes
  1838. )
  1839. {
  1840. UINTN Count;
  1841. UINTN Index;
  1842. HEFI_EDITOR_LINE *Line;
  1843. LIST_ENTRY *Link;
  1844. UINT8 *BufferPtr;
  1845. //
  1846. // change the line list to a large buffer
  1847. //
  1848. if (HBufferImage.Lines == NULL) {
  1849. return EFI_SUCCESS;
  1850. }
  1851. Link = &HBufferImage.Lines->Link;
  1852. Count = 0;
  1853. BufferPtr = (UINT8 *) Buffer;
  1854. //
  1855. // deal line by line
  1856. //
  1857. while (Link != HBufferImage.ListHead) {
  1858. Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  1859. //@todo shouldn't this be an error???
  1860. if (Count + Line->Size > Bytes) {
  1861. return EFI_SUCCESS;
  1862. }
  1863. for (Index = 0; Index < Line->Size; Index++) {
  1864. BufferPtr[Index] = Line->Buffer[Index];
  1865. }
  1866. Count += Line->Size;
  1867. BufferPtr += Line->Size;
  1868. Link = Link->ForwardLink;
  1869. }
  1870. return EFI_SUCCESS;
  1871. }
  1872. /**
  1873. Move the mouse in the image buffer.
  1874. @param[in] TextX The x-coordinate.
  1875. @param[in] TextY The y-coordinate.
  1876. **/
  1877. VOID
  1878. HBufferImageAdjustMousePosition (
  1879. IN INT32 TextX,
  1880. IN INT32 TextY
  1881. )
  1882. {
  1883. UINTN TempX;
  1884. UINTN TempY;
  1885. UINTN AbsX;
  1886. UINTN AbsY;
  1887. //
  1888. // TextX and TextY is mouse movement data returned by mouse driver
  1889. // This function will change it to MousePosition
  1890. //
  1891. //
  1892. // get absolute TempX value
  1893. //
  1894. if (TextX >= 0) {
  1895. AbsX = TextX;
  1896. } else {
  1897. AbsX = -TextX;
  1898. }
  1899. //
  1900. // get absolute TempY value
  1901. //
  1902. if (TextY >= 0) {
  1903. AbsY = TextY;
  1904. } else {
  1905. AbsY = -TextY;
  1906. }
  1907. TempX = HBufferImage.MousePosition.Column;
  1908. TempY = HBufferImage.MousePosition.Row;
  1909. if (TextX >= 0) {
  1910. TempX += TextX;
  1911. } else {
  1912. if (TempX >= AbsX) {
  1913. TempX -= AbsX;
  1914. } else {
  1915. TempX = 0;
  1916. }
  1917. }
  1918. if (TextY >= 0) {
  1919. TempY += TextY;
  1920. } else {
  1921. if (TempY >= AbsY) {
  1922. TempY -= AbsY;
  1923. } else {
  1924. TempY = 0;
  1925. }
  1926. }
  1927. //
  1928. // check whether new mouse column position is beyond screen
  1929. // if not, adjust it
  1930. //
  1931. if (TempX >= 10 && TempX <= (10 + 0x10 * 3 - 1)) {
  1932. HBufferImage.MousePosition.Column = TempX;
  1933. } else if (TempX < 10) {
  1934. HBufferImage.MousePosition.Column = 10;
  1935. } else if (TempX > (10 + 0x10 * 3 - 1)) {
  1936. HBufferImage.MousePosition.Column = 10 + 0x10 * 3 - 1;
  1937. }
  1938. //
  1939. // check whether new mouse row position is beyond screen
  1940. // if not, adjust it
  1941. //
  1942. if (TempY >= 2 && TempY <= (HMainEditor.ScreenSize.Row - 1)) {
  1943. HBufferImage.MousePosition.Row = TempY;
  1944. } else if (TempY < 2) {
  1945. HBufferImage.MousePosition.Row = 2;
  1946. } else if (TempY > (HMainEditor.ScreenSize.Row - 1)) {
  1947. HBufferImage.MousePosition.Row = (HMainEditor.ScreenSize.Row - 1);
  1948. }
  1949. }
  1950. /**
  1951. Dispatch input to different handler
  1952. @param[in] Key The input key:
  1953. the keys can be:
  1954. ASCII KEY
  1955. Backspace/Delete
  1956. Direction key: up/down/left/right/pgup/pgdn
  1957. Home/End
  1958. INS
  1959. @retval EFI_SUCCESS The operation was successful.
  1960. @retval EFI_LOAD_ERROR A load error occurred.
  1961. @retval EFI_OUT_OF_RESOURCES A Memory allocation failed.
  1962. **/
  1963. EFI_STATUS
  1964. HBufferImageHandleInput (
  1965. IN EFI_INPUT_KEY *Key
  1966. )
  1967. {
  1968. EFI_STATUS Status;
  1969. Status = EFI_SUCCESS;
  1970. switch (Key->ScanCode) {
  1971. //
  1972. // ordinary key
  1973. //
  1974. case SCAN_NULL:
  1975. Status = HBufferImageDoCharInput (Key->UnicodeChar);
  1976. break;
  1977. //
  1978. // up arrow
  1979. //
  1980. case SCAN_UP:
  1981. Status = HBufferImageScrollUp ();
  1982. break;
  1983. //
  1984. // down arrow
  1985. //
  1986. case SCAN_DOWN:
  1987. Status = HBufferImageScrollDown ();
  1988. break;
  1989. //
  1990. // right arrow
  1991. //
  1992. case SCAN_RIGHT:
  1993. Status = HBufferImageScrollRight ();
  1994. break;
  1995. //
  1996. // left arrow
  1997. //
  1998. case SCAN_LEFT:
  1999. Status = HBufferImageScrollLeft ();
  2000. break;
  2001. //
  2002. // page up
  2003. //
  2004. case SCAN_PAGE_UP:
  2005. Status = HBufferImagePageUp ();
  2006. break;
  2007. //
  2008. // page down
  2009. //
  2010. case SCAN_PAGE_DOWN:
  2011. Status = HBufferImagePageDown ();
  2012. break;
  2013. //
  2014. // delete
  2015. //
  2016. case SCAN_DELETE:
  2017. Status = HBufferImageDoDelete ();
  2018. break;
  2019. //
  2020. // home
  2021. //
  2022. case SCAN_HOME:
  2023. Status = HBufferImageHome ();
  2024. break;
  2025. //
  2026. // end
  2027. //
  2028. case SCAN_END:
  2029. Status = HBufferImageEnd ();
  2030. break;
  2031. default:
  2032. Status = StatusBarSetStatusString (L"Unknown Command");
  2033. break;
  2034. }
  2035. return Status;
  2036. }