1 /* 2 * Implementation of GPTData class derivative with curses-based text-mode 3 * interaction 4 * Copyright (C) 2011-2018 Roderick W. Smith 5 * 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. 10 * 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. 15 * 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. 19 * 20 */ 21 22 #ifndef __GPT_CURSES 23 #define __GPT_CURSES 24 25 #include <iostream> 26 #include <string> 27 #include "gptpart.h" 28 #include "gpt.h" 29 30 struct MenuItem { 31 int key; // Keyboard shortcut 32 std::string name; // Item name; 8 characters 33 std::string desc; // Description 34 }; 35 36 static struct MenuItem menuMain[] = { 37 { 'a', "Align ", "Set partition alignment policy" }, 38 { 'b', "Backup", "Back up the partition table" }, 39 { 'd', "Delete", "Delete the current partition" }, 40 { 'h', " Help ", "Print help screen" }, 41 { 'i', " Info ", "Display information about the partition" }, 42 { 'l', " Load ", "Load partition table backup from file" }, 43 { 'm', " naMe ", "Change the partition's name" }, 44 { 'n', " New ", "Create new partition from free space" }, 45 { 'q', " Quit ", "Quit program without writing partition table" }, 46 { 't', " Type ", "Change the filesystem type code GUID" }, 47 { 'v', "Verify", "Verify the integrity of the disk's data structures" }, 48 { 'w', "Write ", "Write partition table to disk (this might destroy data)" }, 49 { 0, "", "" } 50 }; 51 52 #define EMPTY_SPACE_OPTIONS "abhlnqvw" 53 #define PARTITION_OPTIONS "abdhilmqtvw" 54 55 // Constants for how to highlight a selected menu item 56 #define USE_CURSES 1 57 #define USE_ARROW 2 58 59 // A "Space" is a partition or an unallocated chunk of disk space, maintained 60 // in a doubly-linked-list data structure to facilitate creating displays of 61 // partitions and unallocated chunks of space on the disk in the main 62 // cgdisk partition list. This list MUST be correctly maintained and in order, 63 // and the numSpaces variable in the main GPTDataCurses class must specify 64 // how many Spaces are in the main linked list of Spaces. 65 struct Space { 66 uint64_t firstLBA; 67 uint64_t lastLBA; 68 GPTPart *origPart; 69 int partNum; 70 Space *nextSpace; 71 Space *prevSpace; 72 }; 73 74 class GPTDataCurses : public GPTData { 75 protected: 76 static int numInstances; 77 GPTPart emptySpace; 78 Space *firstSpace; 79 Space *lastSpace; 80 Space *currentSpace; 81 int currentSpaceNum; 82 std::string whichOptions; 83 char currentKey; 84 int numSpaces; 85 int displayType; 86 87 // Functions relating to Spaces data structures 88 void EmptySpaces(void); 89 int MakeSpacesFromParts(void); 90 void AddEmptySpace(uint64_t firstLBA, uint64_t lastLBA); 91 int AddEmptySpaces(void); 92 void UnlinkSpace(Space *theSpace); 93 void LinkToEnd(Space *theSpace); 94 void SortSpaces(void); 95 void IdentifySpaces(void); 96 97 // Data display functions 98 Space* ShowSpace(int spaceNum, int lineNum); 99 int DisplayParts(int selected); 100 public: 101 GPTDataCurses(void); 102 ~GPTDataCurses(void); 103 // Functions corresponding to main menu items 104 void DeletePartition(int partNum); 105 void ShowInfo(int partNum); 106 void ChangeName(int partNum); 107 void ChangeType(int partNum); 108 void SetAlignment(void); 109 void Verify(void); 110 void MakeNewPart(void); 111 void SaveData(void); 112 void Backup(void); 113 void LoadBackup(void); 114 void ShowHelp(void); 115 // User input and menuing functions SetDisplayType(int dt)116 void SetDisplayType(int dt) {displayType = dt;} 117 void ChangeSpaceSelection(int delta); 118 void MoveSelection(int delta); 119 void DisplayOptions(char selectedKey); 120 void AcceptInput(); 121 int Dispatch(char operation); 122 void DrawMenu(void); 123 int MainMenu(void); 124 }; // class GPTDataCurses 125 126 // Non-class support functions (mostly to do simple curses stuff).... 127 128 void ClearLine(int lineNum); 129 void ClearBottom(void); 130 void PromptToContinue(void); 131 void Report(std::string theText); 132 void ShowTypes(void); 133 134 #endif 135