Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

631 lines
14 KiB

  1. /*++
  2. Copyright (c) 1999 Intel Corporation
  3. Module Name:
  4. libMenuBar.c
  5. Abstract:
  6. Defines the MenuBar data type and the operations to be performed on it.
  7. --*/
  8. #ifndef _LIB_MENU_BAR
  9. #define _LIB_MENU_BAR
  10. #include "libMisc.h"
  11. STATIC UINTN HexSelStart;
  12. STATIC UINTN HexSelEnd;
  13. STATIC BOOLEAN EditorIsExiting = FALSE;
  14. #define IS_EDITOR_EXITING (EditorIsExiting == TRUE)
  15. STATIC EFI_STATUS MainMenuInit (VOID);
  16. STATIC EFI_STATUS MainMenuCleanup (VOID);
  17. STATIC EFI_STATUS MainMenuRefresh (VOID);
  18. STATIC EFI_STATUS MainMenuHandleInput (EFI_INPUT_KEY*);
  19. STATIC EFI_STATUS MenuHide(VOID);
  20. STATIC EFI_STATUS OpenFile(VOID);
  21. STATIC EFI_STATUS BufferClose(VOID);
  22. STATIC EFI_STATUS BufferSave(VOID);
  23. STATIC EFI_STATUS Exit(VOID);
  24. STATIC EFI_STATUS DiskOpen(VOID);
  25. STATIC EFI_STATUS MemOpen(VOID);
  26. STATIC EFI_STATUS SelectStart(VOID);
  27. STATIC EFI_STATUS SelectEnd(VOID);
  28. STATIC EFI_STATUS CopyHex(VOID);
  29. STATIC EFI_STATUS CutHex(VOID);
  30. STATIC EFI_STATUS PasteHex(VOID);
  31. STATIC EFI_STATUS GotoOffset(VOID);
  32. EE_MENU_ITEM MenuItems[] = {
  33. { L"Open File", L"F1", OpenFile },
  34. { L"Open Disk", L"F2", DiskOpen },
  35. { L"Open Memory", L"F3", MemOpen },
  36. { L"Save Buffer", L"F4", BufferSave },
  37. { L"Select Start", L"F5", SelectStart },
  38. { L"Select End", L"F6", SelectEnd },
  39. { L"Cut", L"F7", CutHex },
  40. { L"Paste", L"F8", PasteHex },
  41. { L"Go To Offset", L"F9", GotoOffset },
  42. { L"Exit", L"F10", Exit },
  43. { L"", L"", NULL }
  44. };
  45. EE_MENU_BAR MainMenuBar = {
  46. MenuItems,
  47. MainMenuInit,
  48. MainMenuCleanup,
  49. MainMenuRefresh,
  50. MenuHide,
  51. MainMenuHandleInput
  52. };
  53. STATIC
  54. EFI_STATUS
  55. MainMenuInit (VOID)
  56. {
  57. HexSelStart = 0;
  58. HexSelEnd = 0;
  59. return EFI_SUCCESS;
  60. }
  61. STATIC
  62. EFI_STATUS
  63. MainMenuCleanup (VOID)
  64. {
  65. return EFI_SUCCESS;
  66. }
  67. STATIC
  68. EFI_STATUS
  69. MainMenuRefresh (VOID)
  70. {
  71. EE_MENU_ITEM *Item;
  72. UINTN Col = 0;
  73. UINTN Row = MENU_BAR_LOCATION;
  74. UINTN Width;
  75. MenuHide ();
  76. for (Item = MainMenuBar.MenuItems; Item->Function;Item++) {
  77. Width = max((StrLen(Item->Name)+6),18);
  78. if ((Col + Width) >= MAX_TEXT_COLUMNS ) {
  79. Row++;
  80. Col = 0;
  81. }
  82. PrintAt(Col,Row,L"%E%s%N %H%s%N ",Item->FunctionKey,Item->Name);
  83. Col += Width;
  84. }
  85. MainEditor.FileBuffer->RestorePosition();
  86. return EFI_SUCCESS;
  87. }
  88. STATIC
  89. EFI_STATUS
  90. MainMenuHandleInput (EFI_INPUT_KEY *Key)
  91. {
  92. EE_MENU_ITEM *Item;
  93. EFI_STATUS Status;
  94. UINTN ItemIndex = 0;
  95. UINTN NumItems;
  96. NumItems = sizeof(MainMenuBar.MenuItems)/sizeof(EE_MENU_ITEM) - 1;
  97. Item = MainMenuBar.MenuItems;
  98. ItemIndex = Key->ScanCode - SCAN_CODE_F1;
  99. if (ItemIndex > (NumItems - 1)) {
  100. return EFI_SUCCESS;
  101. }
  102. Item = &MainMenuBar.MenuItems[ItemIndex];
  103. Status = Item->Function();
  104. MainMenuRefresh();
  105. return (IS_EDITOR_EXITING) ? EFI_LOAD_ERROR : Status;
  106. }
  107. STATIC
  108. EFI_STATUS
  109. OpenFile (VOID)
  110. {
  111. EFI_STATUS Status = EFI_SUCCESS;
  112. CHAR16 *Filename;
  113. MainEditor.BufferImage->FileImage->Init();
  114. MainEditor.InputBar->SetPrompt(L"File Name to Open: ");
  115. MainEditor.InputBar->SetStringSize (125);
  116. Status = MainEditor.InputBar->Refresh ();
  117. if ( EFI_ERROR(Status) ) {
  118. return EFI_SUCCESS;
  119. }
  120. Filename = MainEditor.InputBar->ReturnString;
  121. if ( Filename == NULL ) {
  122. MainEditor.StatusBar->SetStatusString(L"Filename was NULL");
  123. return EFI_SUCCESS;
  124. }
  125. Status = MainEditor.BufferImage->FileImage->SetFilename (Filename);
  126. if ( EFI_ERROR(Status) ) {
  127. EditorError(Status,L"Could Not Set Filename");
  128. return EFI_NOT_FOUND;
  129. }
  130. Status = MainEditor.BufferImage->Open ();
  131. if ( EFI_ERROR(Status) ) {
  132. EditorError(Status,L"Could Not Open File");
  133. return EFI_NOT_FOUND;
  134. }
  135. Status = MainEditor.BufferImage->Read ();
  136. if ( EFI_ERROR(Status) ) {
  137. EditorError(Status,L"Could Not Read File");
  138. return EFI_NOT_FOUND;
  139. }
  140. Status = MainEditor.FileBuffer->Refresh();
  141. if ( EFI_ERROR(Status) ) {
  142. EditorError(Status,L"Could Not Refresh");
  143. return EFI_NOT_FOUND;
  144. }
  145. MainEditor.TitleBar->SetTitleString(Filename);
  146. MainEditor.Refresh();
  147. return EFI_SUCCESS;
  148. }
  149. STATIC
  150. EFI_STATUS
  151. BufferClose (VOID)
  152. {
  153. EFI_INPUT_KEY Key;
  154. EFI_STATUS Status = EFI_SUCCESS;
  155. BOOLEAN Done = FALSE;
  156. if (MainEditor.FileModified) {
  157. MenuHide();
  158. PrintAt(0,MENU_BAR_LOCATION,L"%E%Y%H Yes%N %EN%N %HNo%N %EQ%N %HCancel%N");
  159. MainEditor.StatusBar->SetStatusString(L"Buffer Modified. Save? ");
  160. while (!Done) {
  161. WaitForSingleEvent(In->WaitForKey,0);
  162. Status = In->ReadKeyStroke(In,&Key);
  163. if ( EFI_ERROR(Status) || Key.ScanCode != 0 ) {
  164. continue;
  165. }
  166. switch (Key.UnicodeChar) {
  167. case 'q':
  168. case 'Q':
  169. Status = EFI_NOT_READY;
  170. Done = TRUE;
  171. break;
  172. case 'n':
  173. case 'N':
  174. Status = EFI_SUCCESS;
  175. Done = TRUE;
  176. break;
  177. case 'y':
  178. case 'Y':
  179. Status = BufferSave();
  180. Done = TRUE;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. MainMenuRefresh();
  187. }
  188. if (!EFI_ERROR(Status)) {
  189. MainEditor.BufferImage->Close();
  190. MainEditor.FileBuffer->Refresh ();
  191. MainEditor.TitleBar->SetTitleString(L"");
  192. } else {
  193. EditorIsExiting = FALSE;
  194. }
  195. MainEditor.StatusBar->Refresh();
  196. MainMenuRefresh();
  197. return EFI_SUCCESS;
  198. }
  199. STATIC
  200. EFI_STATUS
  201. FileGetName (
  202. VOID
  203. )
  204. {
  205. CHAR16 *Output;
  206. EFI_STATUS Status;
  207. Output = PoolPrint (L"File To Save: [%s]",MainEditor.BufferImage->FileImage->FileName);
  208. MainEditor.InputBar->SetPrompt(Output);
  209. MainEditor.InputBar->SetStringSize(255);
  210. Status = MainEditor.InputBar->Refresh();
  211. FreePool(Output);
  212. if (!EFI_ERROR(Status) && MainEditor.InputBar->StringSize > 0) {
  213. MainEditor.BufferImage->FileImage->SetFilename(MainEditor.InputBar->ReturnString);
  214. }
  215. return Status;
  216. }
  217. STATIC
  218. EFI_STATUS
  219. BufferSave (VOID)
  220. {
  221. EFI_STATUS Status;
  222. if (!MainEditor.FileModified) {
  223. return EFI_SUCCESS;
  224. }
  225. if (MainEditor.BufferImage->BufferType == FILE_BUFFER) {
  226. FileGetName();
  227. }
  228. Status = MainEditor.BufferImage->Write();
  229. if (EFI_ERROR(Status)) {
  230. EditorError(Status,L"BufferSave: Problems Writing");
  231. }
  232. MainEditor.FileModified = FALSE;
  233. return Status;
  234. }
  235. STATIC
  236. EFI_STATUS
  237. Exit (
  238. VOID
  239. )
  240. {
  241. EditorIsExiting = TRUE;
  242. BufferClose();
  243. return EFI_SUCCESS;
  244. }
  245. STATIC
  246. EFI_STATUS
  247. MenuHide (VOID)
  248. {
  249. MainEditor.FileBuffer->ClearLine (MENU_BAR_LOCATION);
  250. MainEditor.FileBuffer->ClearLine (MENU_BAR_LOCATION+1);
  251. MainEditor.FileBuffer->ClearLine (MENU_BAR_LOCATION+2);
  252. return EFI_SUCCESS;
  253. }
  254. STATIC
  255. EFI_STATUS
  256. SelectStart (
  257. VOID
  258. )
  259. {
  260. HexSelStart = MainEditor.FileBuffer->Offset;
  261. HexSelEnd = HexSelStart;
  262. return EFI_SUCCESS;
  263. }
  264. STATIC
  265. EFI_STATUS
  266. SelectEnd (
  267. VOID
  268. )
  269. {
  270. UINTN Offset;
  271. Offset = MainEditor.FileBuffer->Offset;
  272. if (Offset > HexSelStart) {
  273. HexSelEnd = Offset;
  274. }
  275. return EFI_SUCCESS;
  276. }
  277. STATIC
  278. EFI_STATUS
  279. CopyHex (
  280. VOID
  281. )
  282. {
  283. MainEditor.Clipboard->Copy(HexSelStart,HexSelEnd);
  284. return EFI_SUCCESS;
  285. }
  286. STATIC
  287. EFI_STATUS
  288. CutHex (
  289. VOID
  290. )
  291. {
  292. MainEditor.StatusBar->SetStatusString (L"EditMenuCut: Entered Function");
  293. MainEditor.Clipboard->Cut (HexSelStart,HexSelEnd);
  294. return EFI_SUCCESS;
  295. }
  296. STATIC
  297. EFI_STATUS
  298. PasteHex (
  299. VOID
  300. )
  301. {
  302. EFI_STATUS Status;
  303. Status = MainEditor.Clipboard->Paste();
  304. return Status;
  305. }
  306. STATIC
  307. EFI_STATUS
  308. GotoOffset (
  309. VOID
  310. )
  311. {
  312. CHAR16 *Str;
  313. UINTN Offset;
  314. UINTN Current;
  315. UINTN MaxBytes;
  316. UINTN RowDiff;
  317. UINTN LineOffset;
  318. BOOLEAN Refresh = FALSE;
  319. MenuHide ();
  320. Str = PoolPrint(L"Go To Offset: ");
  321. MainEditor.InputBar->SetPrompt(Str);
  322. FreePool(Str);
  323. /* Inputing the offset as the style "0000XXXX" displayed on the editor screen will cause
  324. * an assert error related to pool, the gived string size may be short. */
  325. MainEditor.InputBar->SetStringSize(20);
  326. MainEditor.InputBar->Refresh();
  327. if (MainEditor.InputBar->StringSize > 0) {
  328. Offset = xtoi(MainEditor.InputBar->ReturnString);
  329. } else {
  330. return EFI_SUCCESS;
  331. }
  332. FreePool(MainEditor.InputBar->ReturnString);
  333. if (Offset > MainEditor.BufferImage->NumBytes) {
  334. return EFI_SUCCESS;
  335. }
  336. Current = MainEditor.FileBuffer->Offset;
  337. MaxBytes = MainEditor.FileBuffer->MaxVisibleBytes;
  338. if (Offset == Current ) {
  339. return EFI_SUCCESS;
  340. }
  341. if (Offset < Current) {
  342. RowDiff = (Current - Offset) / 0x10;
  343. LineRetreat(RowDiff);
  344. if (Offset < MainEditor.FileBuffer->LowVisibleOffset ) {
  345. MainEditor.FileBuffer->LowVisibleOffset = Offset & 0xfffffff0;
  346. MainEditor.FileBuffer->HighVisibleOffset = (Offset + MaxBytes) & 0xfffffff0;
  347. Refresh = TRUE;
  348. }
  349. } else {
  350. RowDiff = (Offset - Current) / 0x10;
  351. LineAdvance(RowDiff);
  352. if (Offset > MainEditor.FileBuffer->HighVisibleOffset) {
  353. MainEditor.FileBuffer->LowVisibleOffset = Offset & 0xfffffff0;
  354. MainEditor.FileBuffer->HighVisibleOffset = (Offset + MaxBytes) & 0xfffffff0;
  355. Refresh = TRUE;
  356. }
  357. }
  358. Current = MainEditor.FileBuffer->LowVisibleOffset;
  359. LineOffset = (Offset % 0x10) * 3 + HEX_POSITION;
  360. if ((Offset % 0x10) > 0x07) {
  361. ++LineOffset;
  362. }
  363. MainEditor.FileBuffer->Offset = Offset;
  364. MainEditor.FileBuffer->SetPosition(DISP_START_ROW+(Offset-Current)/0x10,LineOffset);
  365. MainEditor.StatusBar->SetOffset(Offset);
  366. if (Refresh) {
  367. MainEditor.FileBuffer->Refresh();
  368. }
  369. return EFI_SUCCESS;
  370. }
  371. STATIC
  372. EFI_STATUS
  373. DiskOpen (
  374. VOID
  375. )
  376. {
  377. UINTN n;
  378. CHAR16 *Str;
  379. EFI_STATUS Status;
  380. Status = BufferClose ();
  381. if (EFI_ERROR(Status)){
  382. return Status;
  383. }
  384. MainEditor.BufferImage->DiskImage->Init();
  385. MenuHide();
  386. Str = PoolPrint(L"Enter Block Device: ");
  387. MainEditor.InputBar->SetPrompt(Str);
  388. FreePool(Str);
  389. MainEditor.InputBar->SetStringSize(25);
  390. MainEditor.InputBar->Refresh();
  391. if (MainEditor.InputBar->StringSize > 0) {
  392. Status = MainEditor.BufferImage->DiskImage->SetDevice(MainEditor.InputBar->ReturnString);
  393. FreePool(MainEditor.InputBar->ReturnString);
  394. if (EFI_ERROR(Status)) {
  395. return EFI_SUCCESS;
  396. }
  397. } else {
  398. return EFI_SUCCESS;
  399. }
  400. Str = PoolPrint(L"Starting Offset: ");
  401. MainEditor.InputBar->SetPrompt(Str);
  402. FreePool(Str);
  403. MainEditor.InputBar->SetStringSize(16);
  404. MainEditor.InputBar->Refresh();
  405. if (MainEditor.InputBar->StringSize > 0) {
  406. n = xtoi(MainEditor.InputBar->ReturnString);
  407. FreePool(MainEditor.InputBar->ReturnString);
  408. }
  409. MainEditor.BufferImage->DiskImage->SetOffset(n);
  410. Str = PoolPrint(L"Number of Blocks: ");
  411. MainEditor.InputBar->SetPrompt(Str);
  412. FreePool(Str);
  413. MainEditor.InputBar->SetStringSize(8);
  414. MainEditor.InputBar->Refresh();
  415. if (MainEditor.InputBar->StringSize > 0) {
  416. n = xtoi(MainEditor.InputBar->ReturnString);
  417. FreePool(MainEditor.InputBar->ReturnString);
  418. }
  419. MainEditor.BufferImage->DiskImage->SetSize(n);
  420. Status = MainEditor.BufferImage->Open ();
  421. if ( EFI_ERROR(Status) ) {
  422. EditorError(Status,L"Could Not Open Device");
  423. return EFI_NOT_FOUND;
  424. }
  425. Status = MainEditor.BufferImage->Read ();
  426. if ( EFI_ERROR(Status) ) {
  427. EditorError(Status,L"Could Not Read Device");
  428. return EFI_NOT_FOUND;
  429. }
  430. Status = MainEditor.FileBuffer->Refresh();
  431. if ( EFI_ERROR(Status) ) {
  432. EditorError(Status,L"Could Not Refresh Buffer");
  433. return EFI_NOT_FOUND;
  434. }
  435. MainEditor.Refresh();
  436. return EFI_SUCCESS;
  437. }
  438. STATIC
  439. EFI_STATUS
  440. MemOpen (
  441. VOID
  442. )
  443. {
  444. UINTN Num = 0;
  445. CHAR16 *Str;
  446. EFI_STATUS Status;
  447. Status = BufferClose ();
  448. if (EFI_ERROR(Status)){
  449. return Status;
  450. }
  451. MainEditor.BufferImage->MemImage->Init();
  452. Str = PoolPrint(L"Starting Offset: ");
  453. MainEditor.InputBar->SetPrompt(Str);
  454. FreePool(Str);
  455. MainEditor.InputBar->SetStringSize(20);
  456. MainEditor.InputBar->Refresh();
  457. if (MainEditor.InputBar->StringSize > 0) {
  458. Num = xtoi(MainEditor.InputBar->ReturnString);
  459. FreePool(MainEditor.InputBar->ReturnString);
  460. }
  461. MainEditor.BufferImage->MemImage->SetOffset(Num);
  462. Str = PoolPrint(L"Buffer Size: ");
  463. MainEditor.InputBar->SetPrompt(Str);
  464. FreePool(Str);
  465. MainEditor.InputBar->SetStringSize(20);
  466. MainEditor.InputBar->Refresh();
  467. if (MainEditor.InputBar->StringSize > 0) {
  468. Num = xtoi(MainEditor.InputBar->ReturnString);
  469. FreePool(MainEditor.InputBar->ReturnString);
  470. }
  471. MainEditor.BufferImage->MemImage->SetSize(Num);
  472. Status = MainEditor.BufferImage->Open ();
  473. if ( EFI_ERROR(Status) ) {
  474. EditorError(Status,L"Could Not Open Device");
  475. return EFI_NOT_FOUND;
  476. }
  477. Status = MainEditor.BufferImage->Read ();
  478. if ( EFI_ERROR(Status) ) {
  479. EditorError(Status,L"Could Not Read Device");
  480. return EFI_NOT_FOUND;
  481. }
  482. Status = MainEditor.FileBuffer->Refresh();
  483. if ( EFI_ERROR(Status) ) {
  484. EditorError(Status,L"Could Not Refresh Buffer");
  485. return EFI_NOT_FOUND;
  486. }
  487. MainEditor.Refresh();
  488. return EFI_SUCCESS;
  489. }
  490. #endif /* _LIB_MENU_BAR */