blob: 14f43adbec36ba28b4271a3d38e0622d5e46b006 [file] [log] [blame]
srs56943860cbe2011-09-10 20:29:53 -04001/*
2 * Implementation of GPTData class derivative with curses-based text-mode
3 * interaction
4 * Copyright (C) 2011 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
30using namespace std;
31
32struct MenuItem {
33 int key; // Keyboard shortcut
34 string name; // Item name; 8 characters
35 string desc; // Description
36};
37
38static 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.
64struct Space {
65 uint64_t firstLBA;
66 uint64_t lastLBA;
67 GPTPart *origPart;
68 int partNum;
69 Space *nextSpace;
70 Space *prevSpace;
71};
72
73class GPTDataCurses : public GPTData {
74protected:
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;
84 // Functions relating to Spaces data structures
85 void EmptySpaces(void);
86 int MakeSpacesFromParts(void);
87 void AddEmptySpace(uint64_t firstLBA, uint64_t lastLBA);
88 int AddEmptySpaces(void);
89 void UnlinkSpace(Space *theSpace);
90 void LinkToEnd(Space *theSpace);
91 void SortSpaces(void);
92 void IdentifySpaces(void);
93 // Data display functions
94 Space* ShowSpace(int spaceNum, int lineNum);
95 int DisplayParts(int selected);
96public:
97 GPTDataCurses(void);
98 ~GPTDataCurses(void);
99 // Functions corresponding to main menu items
100 void DeletePartition(int partNum);
101 void ShowInfo(int partNum);
102 void ChangeName(int partNum);
103 void ChangeType(int partNum);
104 void SetAlignment(void);
105 void Verify(void);
106 void MakeNewPart(void);
107 void SaveData(void);
108 void Backup(void);
109 void LoadBackup(void);
110 void ShowHelp(void);
111 // User input and menuing functions
112 void ChangeSpaceSelection(int delta);
113 void MoveSelection(int delta);
114 void DisplayOptions(char selectedKey);
115 void AcceptInput();
116 int Dispatch(char operation);
117 void DrawMenu(void);
118 int MainMenu(void);
119}; // class GPTDataCurses
120
121// Non-class support functions (mostly to do simple curses stuff)....
122
123void ClearLine(int lineNum);
124void ClearBottom(void);
125void PromptToContinue(void);
126void Report(string theText);
127void ShowTypes(void);
128
129#endif