1 /*
2  *    Implementation of GPTData class derivative with curses-based text-mode
3  *    interaction
4  *    Copyright (C) 2011-2013 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 #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 // Constants for how to highlight a selected menu item
58 #define USE_CURSES 1
59 #define USE_ARROW 2
60 
61 // A "Space" is a partition or an unallocated chunk of disk space, maintained
62 // in a doubly-linked-list data structure to facilitate creating displays of
63 // partitions and unallocated chunks of space on the disk in the main
64 // cgdisk partition list. This list MUST be correctly maintained and in order,
65 // and the numSpaces variable in the main GPTDataCurses class must specify
66 // how many Spaces are in the main linked list of Spaces.
67 struct Space {
68    uint64_t firstLBA;
69    uint64_t lastLBA;
70    GPTPart *origPart;
71    int partNum;
72    Space *nextSpace;
73    Space *prevSpace;
74 };
75 
76 class GPTDataCurses : public GPTData {
77 protected:
78    static int numInstances;
79    GPTPart emptySpace;
80    Space *firstSpace;
81    Space *lastSpace;
82    Space *currentSpace;
83    int currentSpaceNum;
84    string whichOptions;
85    char currentKey;
86    int numSpaces;
87    int displayType;
88 
89    // Functions relating to Spaces data structures
90    void EmptySpaces(void);
91    int MakeSpacesFromParts(void);
92    void AddEmptySpace(uint64_t firstLBA, uint64_t lastLBA);
93    int AddEmptySpaces(void);
94    void UnlinkSpace(Space *theSpace);
95    void LinkToEnd(Space *theSpace);
96    void SortSpaces(void);
97    void IdentifySpaces(void);
98 
99    // Data display functions
100    Space* ShowSpace(int spaceNum, int lineNum);
101    int DisplayParts(int selected);
102 public:
103    GPTDataCurses(void);
104    ~GPTDataCurses(void);
105    // Functions corresponding to main menu items
106    void DeletePartition(int partNum);
107    void ShowInfo(int partNum);
108    void ChangeName(int partNum);
109    void ChangeType(int partNum);
110    void SetAlignment(void);
111    void Verify(void);
112    void MakeNewPart(void);
113    void SaveData(void);
114    void Backup(void);
115    void LoadBackup(void);
116    void ShowHelp(void);
117    // User input and menuing functions
SetDisplayType(int dt)118    void SetDisplayType(int dt) {displayType = dt;}
119    void ChangeSpaceSelection(int delta);
120    void MoveSelection(int delta);
121    void DisplayOptions(char selectedKey);
122    void AcceptInput();
123    int Dispatch(char operation);
124    void DrawMenu(void);
125    int MainMenu(void);
126 }; // class GPTDataCurses
127 
128 // Non-class support functions (mostly to do simple curses stuff)....
129 
130 void ClearLine(int lineNum);
131 void ClearBottom(void);
132 void PromptToContinue(void);
133 void Report(string theText);
134 void ShowTypes(void);
135 
136 #endif
137