srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Implementation of GPTData class derivative with curses-based text-mode |
| 3 | * interaction |
| 4 | * Copyright (C) 2011 Roderick W. Smith |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 5 | * |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 10 | * |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 15 | * |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 19 | * |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <string> |
| 24 | #include "gptpart.h" |
| 25 | #include "gpt.h" |
| 26 | |
| 27 | #ifndef __GPT_CURSES |
| 28 | #define __GPT_CURSES |
| 29 | |
| 30 | using namespace std; |
| 31 | |
| 32 | struct MenuItem { |
| 33 | int key; // Keyboard shortcut |
| 34 | string name; // Item name; 8 characters |
| 35 | string desc; // Description |
| 36 | }; |
| 37 | |
| 38 | static struct MenuItem menuMain[] = { |
| 39 | { 'a', "Align ", "Set partition alignment policy" }, |
| 40 | { 'b', "Backup", "Back up the partition table" }, |
| 41 | { 'd', "Delete", "Delete the current partition" }, |
| 42 | { 'h', " Help ", "Print help screen" }, |
| 43 | { 'i', " Info ", "Display information about the partition" }, |
| 44 | { 'l', " Load ", "Load partition table backup from file" }, |
| 45 | { 'm', " naMe ", "Change the partition's name" }, |
| 46 | { 'n', " New ", "Create new partition from free space" }, |
| 47 | { 'q', " Quit ", "Quit program without writing partition table" }, |
| 48 | { 't', " Type ", "Change the filesystem type code GUID" }, |
| 49 | { 'v', "Verify", "Verify the integrity of the disk's data structures" }, |
| 50 | { 'w', "Write ", "Write partition table to disk (this might destroy data)" }, |
| 51 | { 0, "", "" } |
| 52 | }; |
| 53 | |
| 54 | #define EMPTY_SPACE_OPTIONS "abhlnqvw" |
| 55 | #define PARTITION_OPTIONS "abdhilmqtvw" |
| 56 | |
| 57 | |
| 58 | // A "Space" is a partition or an unallocated chunk of disk space, maintained |
| 59 | // in a doubly-linked-list data structure to facilitate creating displays of |
| 60 | // partitions and unallocated chunks of space on the disk in the main |
| 61 | // cgdisk partition list. This list MUST be correctly maintained and in order, |
| 62 | // and the numSpaces variable in the main GPTDataCurses class must specify |
| 63 | // how many Spaces are in the main linked list of Spaces. |
| 64 | struct Space { |
| 65 | uint64_t firstLBA; |
| 66 | uint64_t lastLBA; |
| 67 | GPTPart *origPart; |
| 68 | int partNum; |
| 69 | Space *nextSpace; |
| 70 | Space *prevSpace; |
| 71 | }; |
| 72 | |
| 73 | class GPTDataCurses : public GPTData { |
| 74 | protected: |
| 75 | static int numInstances; |
| 76 | GPTPart emptySpace; |
| 77 | Space *firstSpace; |
| 78 | Space *lastSpace; |
| 79 | Space *currentSpace; |
| 80 | int currentSpaceNum; |
| 81 | string whichOptions; |
| 82 | char currentKey; |
| 83 | int numSpaces; |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 84 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 85 | // Functions relating to Spaces data structures |
| 86 | void EmptySpaces(void); |
| 87 | int MakeSpacesFromParts(void); |
| 88 | void AddEmptySpace(uint64_t firstLBA, uint64_t lastLBA); |
| 89 | int AddEmptySpaces(void); |
| 90 | void UnlinkSpace(Space *theSpace); |
| 91 | void LinkToEnd(Space *theSpace); |
| 92 | void SortSpaces(void); |
| 93 | void IdentifySpaces(void); |
srs5694 | d1b11e8 | 2011-09-18 21:12:28 -0400 | [diff] [blame] | 94 | |
srs5694 | 3860cbe | 2011-09-10 20:29:53 -0400 | [diff] [blame] | 95 | // Data display functions |
| 96 | Space* ShowSpace(int spaceNum, int lineNum); |
| 97 | int DisplayParts(int selected); |
| 98 | public: |
| 99 | GPTDataCurses(void); |
| 100 | ~GPTDataCurses(void); |
| 101 | // Functions corresponding to main menu items |
| 102 | void DeletePartition(int partNum); |
| 103 | void ShowInfo(int partNum); |
| 104 | void ChangeName(int partNum); |
| 105 | void ChangeType(int partNum); |
| 106 | void SetAlignment(void); |
| 107 | void Verify(void); |
| 108 | void MakeNewPart(void); |
| 109 | void SaveData(void); |
| 110 | void Backup(void); |
| 111 | void LoadBackup(void); |
| 112 | void ShowHelp(void); |
| 113 | // User input and menuing functions |
| 114 | void ChangeSpaceSelection(int delta); |
| 115 | void MoveSelection(int delta); |
| 116 | void DisplayOptions(char selectedKey); |
| 117 | void AcceptInput(); |
| 118 | int Dispatch(char operation); |
| 119 | void DrawMenu(void); |
| 120 | int MainMenu(void); |
| 121 | }; // class GPTDataCurses |
| 122 | |
| 123 | // Non-class support functions (mostly to do simple curses stuff).... |
| 124 | |
| 125 | void ClearLine(int lineNum); |
| 126 | void ClearBottom(void); |
| 127 | void PromptToContinue(void); |
| 128 | void Report(string theText); |
| 129 | void ShowTypes(void); |
| 130 | |
| 131 | #endif |