aboutsummaryrefslogtreecommitdiff
path: root/gptcurses.h
blob: 8d2ecafcf261ffe439f944a16300de3eff62f5a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 *    Implementation of GPTData class derivative with curses-based text-mode
 *    interaction
 *    Copyright (C) 2011-2018 Roderick W. Smith
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef __GPT_CURSES
#define __GPT_CURSES

#include <iostream>
#include <string>
#include "gptpart.h"
#include "gpt.h"

struct MenuItem {
   int key; // Keyboard shortcut
   std::string name; // Item name; 8 characters
   std::string desc; // Description
};

static struct MenuItem menuMain[] = {
   { 'a', "Align ", "Set partition alignment policy" },
   { 'b', "Backup", "Back up the partition table" },
   { 'd', "Delete", "Delete the current partition" },
   { 'h', " Help ", "Print help screen" },
   { 'i', " Info ", "Display information about the partition" },
   { 'l', " Load ", "Load partition table backup from file" },
   { 'm', " naMe ", "Change the partition's name" },
   { 'n', " New  ", "Create new partition from free space" },
   { 'q', " Quit ", "Quit program without writing partition table" },
   { 't', " Type ", "Change the filesystem type code GUID" },
   { 'v', "Verify", "Verify the integrity of the disk's data structures" },
   { 'w', "Write ", "Write partition table to disk (this might destroy data)" },
   { 0, "", "" }
};

#define EMPTY_SPACE_OPTIONS "abhlnqvw"
#define PARTITION_OPTIONS "abdhilmqtvw"

// Constants for how to highlight a selected menu item
#define USE_CURSES 1
#define USE_ARROW 2

// A "Space" is a partition or an unallocated chunk of disk space, maintained
// in a doubly-linked-list data structure to facilitate creating displays of
// partitions and unallocated chunks of space on the disk in the main
// cgdisk partition list. This list MUST be correctly maintained and in order,
// and the numSpaces variable in the main GPTDataCurses class must specify
// how many Spaces are in the main linked list of Spaces.
struct Space {
   uint64_t firstLBA;
   uint64_t lastLBA;
   GPTPart *origPart;
   int partNum;
   Space *nextSpace;
   Space *prevSpace;
};

class GPTDataCurses : public GPTData {
protected:
   static int numInstances;
   GPTPart emptySpace;
   Space *firstSpace;
   Space *lastSpace;
   Space *currentSpace;
   int currentSpaceNum;
   std::string whichOptions;
   char currentKey;
   int numSpaces;
   int displayType;

   // Functions relating to Spaces data structures
   void EmptySpaces(void);
   int MakeSpacesFromParts(void);
   void AddEmptySpace(uint64_t firstLBA, uint64_t lastLBA);
   int AddEmptySpaces(void);
   void UnlinkSpace(Space *theSpace);
   void LinkToEnd(Space *theSpace);
   void SortSpaces(void);
   void IdentifySpaces(void);

   // Data display functions
   Space* ShowSpace(int spaceNum, int lineNum);
   int DisplayParts(int selected);
public:
   GPTDataCurses(void);
   ~GPTDataCurses(void);
   // Functions corresponding to main menu items
   void DeletePartition(int partNum);
   void ShowInfo(int partNum);
   void ChangeName(int partNum);
   void ChangeType(int partNum);
   void SetAlignment(void);
   void Verify(void);
   void MakeNewPart(void);
   void SaveData(void);
   void Backup(void);
   void LoadBackup(void);
   void ShowHelp(void);
   // User input and menuing functions
   void SetDisplayType(int dt) {displayType = dt;}
   void ChangeSpaceSelection(int delta);
   void MoveSelection(int delta);
   void DisplayOptions(char selectedKey);
   void AcceptInput();
   int Dispatch(char operation);
   void DrawMenu(void);
   int MainMenu(void);
}; // class GPTDataCurses

// Non-class support functions (mostly to do simple curses stuff)....

void ClearLine(int lineNum);
void ClearBottom(void);
void PromptToContinue(void);
void Report(std::string theText);
void ShowTypes(void);

#endif