Browse Source

fixed states if drive is no longer present

threading
Hendrik Schutter 8 months ago
parent
commit
320e306d06
  1. 6
      include/reHDD.h
  2. 3
      include/tui.h
  3. 2
      makefile
  4. 129
      src/reHDD.cpp
  5. 58
      src/tui.cpp
  6. 3
      vcCodium.code-workspace

6
include/reHDD.h

@ -15,7 +15,9 @@
#define SHRED_ITERATIONS 1
#define SELECTED_DRIVE vecDrives.at(i32SelectedEntry)
#define READ 0
#define WRITE 1
@ -75,7 +77,7 @@ private:
static void handleESC();
static void handleAbort();
static void checkShredComplete(vector <Drive>* pvecDrives);
static Drive* getSelectedDrive();
};

3
include/tui.h

@ -36,7 +36,7 @@ public:
static void initTUI();
void updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry);
void updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry);
static enum UserInput readUserInput();
@ -55,6 +55,7 @@ private:
static void centerTitle(WINDOW *pwin, const char * title);
static WINDOW *createOverViewWindow( int iXSize, int iYSize);
static WINDOW *createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive drive);
static WINDOW *overwriteDetailViewWindow( int iXSize, int iYSize, int iXStart);
static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart,string sModelFamily, string sModelName, string sCapacity, string sState, bool bSelected);
static WINDOW *createSystemStats(int iXSize, int iYSize, int iYStart);
static WINDOW *createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate);

2
makefile

@ -10,7 +10,7 @@ SRC_PATH = src
# Space-separated pkg-config libraries used by this project
LIBS =
# General compiler flags
COMPILE_FLAGS = -std=c++11 -Wall -Wextra -g
COMPILE_FLAGS = -std=c++17 -Wall -Wextra -g
# Additional release-specific flags
RCOMPILE_FLAGS = -D NDEBUG
# Additional debug-specific flags

129
src/reHDD.cpp

@ -19,7 +19,7 @@ static vector <Drive> vecDrives; //stores all drive data from scann thread
TUI *ui;
static int32_t i32SelectedEntry;
static uint8_t u8SelectedEntry;
static fd_set selectSet;
@ -33,7 +33,7 @@ static fd_set selectSet;
reHDD::reHDD(void)
{
cout << "created app" << endl;
i32SelectedEntry = 0;
u8SelectedEntry = 0U;
}
/**
@ -78,14 +78,24 @@ void reHDD::app_logic(void)
checkShredComplete(&vecDrives);
}
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
} //endless loop
thDevices.join();
thUserInput.join();
}
Drive* reHDD::getSelectedDrive()
{
if(u8SelectedEntry < vecDrives.size() )
{
return &(vecDrives.at(u8SelectedEntry));
}
else
{
return {};
}
}
void reHDD::ThreadScannDevices()
{
while(true)
@ -112,12 +122,12 @@ void reHDD::ThreadUserInput()
case TUI::UserInput::DownKey:
//cout << "Down" << endl;
handleArrowKey(TUI::UserInput::DownKey);
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
case TUI::UserInput::UpKey:
//cout << "Up" << endl;
handleArrowKey(TUI::UserInput::UpKey);
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
case TUI::UserInput::Undefined:
//cout << "Undefined" << endl;
@ -125,33 +135,41 @@ void reHDD::ThreadUserInput()
case TUI::UserInput::Abort:
//cout << "Abort" << endl;
handleAbort();
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
case TUI::UserInput::Delete:
//cout << "Delete" << endl;
if(SELECTED_DRIVE.state == Drive::NONE)
if (getSelectedDrive() != nullptr)
{
SELECTED_DRIVE.state = Drive::DELETE_SELECTED;
if(getSelectedDrive()->state == Drive::NONE)
{
getSelectedDrive()->state = Drive::DELETE_SELECTED;
}
}
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
case TUI::UserInput::Shred:
//cout << "Shred" << endl;
if(SELECTED_DRIVE.state == Drive::NONE)
if (getSelectedDrive() != nullptr)
{
SELECTED_DRIVE.state = Drive::SHRED_SELECTED;
if(getSelectedDrive()->state == Drive::NONE)
{
getSelectedDrive()->state = Drive::SHRED_SELECTED;
}
}
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
case TUI::UserInput::Enter:
//cout << "Enter" << endl;
handleEnter();
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
case TUI::UserInput::ESC:
//cout << "ESC" << endl;
handleESC();
ui->updateTUI(&vecDrives, i32SelectedEntry);
ui->updateTUI(&vecDrives, u8SelectedEntry);
break;
default:
break;
@ -161,7 +179,10 @@ void reHDD::ThreadUserInput()
void reHDD::ThreadShred()
{
Shred::shredDrive(&SELECTED_DRIVE, &fdShredInformPipe[1]);
if (getSelectedDrive() != nullptr)
{
Shred::shredDrive(getSelectedDrive(), &fdShredInformPipe[1]);
}
}
void reHDD::filterNewDrives(vector <Drive>* pvecOldDrives, vector <Drive>* pvecNewDrives)
@ -358,69 +379,81 @@ void reHDD::addSMARTData(vector <Drive>* pvecDrives)
void reHDD::handleArrowKey(TUI::UserInput userInput)
{
int32_t i32EntrySize = (int32_t) vecDrives.size();
int8_t u8EntrySize = (int8_t) vecDrives.size();
switch (userInput)
{
case TUI::UserInput::DownKey:
i32SelectedEntry++;
if(i32SelectedEntry >= i32EntrySize)
u8SelectedEntry++;
if(u8SelectedEntry >= u8EntrySize)
{
i32SelectedEntry = 0;
u8SelectedEntry = 0;
}
break;
case TUI::UserInput::UpKey:
i32SelectedEntry--;
if(i32SelectedEntry < 0)
if(u8SelectedEntry == 0)
{
i32SelectedEntry = (i32EntrySize-1);
u8SelectedEntry = (u8EntrySize-1);
}
else
{
u8SelectedEntry--;
}
break;
default:
i32SelectedEntry = 0;
u8SelectedEntry = 0;
break;
}
}
void reHDD::handleEnter()
{
if(SELECTED_DRIVE.state == Drive::TaskState::SHRED_SELECTED)
if (getSelectedDrive() != nullptr)
{
SELECTED_DRIVE.state = Drive::TaskState::SHRED_ACTIVE;
//task for drive is running --> don´t show more task options
thread(ThreadShred).detach();
}
if(getSelectedDrive()->state == Drive::TaskState::SHRED_SELECTED)
{
getSelectedDrive()->state = Drive::TaskState::SHRED_ACTIVE;
//task for drive is running --> don´t show more task options
thread(ThreadShred).detach();
}
if(SELECTED_DRIVE.state == Drive::TaskState::DELETE_SELECTED)
{
SELECTED_DRIVE.state = Drive::TaskState::DELETE_ACTIVE;
//task for drive is running --> don´t show more task options
Delete::deleteDrive(&SELECTED_DRIVE); //blocking, no thread
SELECTED_DRIVE.state = Drive::TaskState::NONE; //delete finished
SELECTED_DRIVE.bWasDeleteted = true;
if(getSelectedDrive()->state == Drive::TaskState::DELETE_SELECTED)
{
getSelectedDrive()->state = Drive::TaskState::DELETE_ACTIVE;
//task for drive is running --> don´t show more task options
Delete::deleteDrive(getSelectedDrive()); //blocking, no thread
getSelectedDrive()->state = Drive::TaskState::NONE; //delete finished
getSelectedDrive()->bWasDeleteted = true;
}
}
}
void reHDD::handleESC()
{
if(SELECTED_DRIVE.state == Drive::TaskState::SHRED_SELECTED)
if (getSelectedDrive() != nullptr)
{
SELECTED_DRIVE.state = Drive::TaskState::NONE;
//task for drive is selected --> remove selection
}
if(getSelectedDrive()->state == Drive::TaskState::SHRED_SELECTED)
{
getSelectedDrive()->state = Drive::TaskState::NONE;
//task for drive is selected --> remove selection
}
if(SELECTED_DRIVE.state == Drive::TaskState::DELETE_SELECTED)
{
SELECTED_DRIVE.state = Drive::TaskState::NONE;
//task for drive is selected --> remove selection
if(getSelectedDrive()->state == Drive::TaskState::DELETE_SELECTED)
{
getSelectedDrive()->state = Drive::TaskState::NONE;
//task for drive is selected --> remove selection
}
}
}
void reHDD::handleAbort()
{
if(SELECTED_DRIVE.state == Drive::SHRED_ACTIVE || SELECTED_DRIVE.state == Drive::DELETE_ACTIVE )
if (getSelectedDrive() != nullptr)
{
SELECTED_DRIVE.state = Drive::NONE;
//task for drive is running --> remove selection
if(getSelectedDrive()->state == Drive::SHRED_ACTIVE || getSelectedDrive()->state == Drive::DELETE_ACTIVE )
{
getSelectedDrive()->state = Drive::NONE;
//task for drive is running --> remove selection
}
}
}

58
src/tui.cpp

@ -51,7 +51,7 @@ void TUI::initTUI()
mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 ");
}
void TUI::updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry)
void TUI::updateTUI(vector <Drive>* pvecDrives, uint8_t u8SelectedEntry)
{
int stdscrX, stdscrY;
getmaxyx(stdscr, stdscrY, stdscrX);
@ -79,10 +79,10 @@ void TUI::updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry)
bool bSelectedEntry = false;
if(i32SelectedEntry == (it - pvecDrives->begin()))
if(u8SelectedEntry == (it - pvecDrives->begin()))
{
bSelectedEntry = true; //mark this drive in entries list
displaySelectedDrive(pvecDrives->at(i32SelectedEntry), stdscrX, stdscrY);
displaySelectedDrive(pvecDrives->at(u8SelectedEntry), stdscrX, stdscrY);
}
@ -114,6 +114,23 @@ void TUI::updateTUI(vector <Drive>* pvecDrives, int32_t i32SelectedEntry)
WINDOW * tmp = createEntryWindow( ((int)(stdscrX/3) - 2), 5, 3, (5* (it - pvecDrives->begin()) )+3, sModelFamily, sModelName, sCapacity, sState, bSelectedEntry);
wrefresh(tmp);
}//end loop though drives
if(pvecDrives->size() == 0)
{
//no selected drive present
struct MenuState menustate;
menustate.bAbort = false;
menustate.bConfirmDelete = false;
menustate.bConfirmShred = false;
menustate.bDelete = false;
menustate.bShred = false;
menuview=createMenuView(((stdscrX)-(int)(stdscrX/3)-7), 10, (int)(stdscrX/3)+5,(stdscrY-11), menustate);
wrefresh(menuview);
detailview=overwriteDetailViewWindow(((stdscrX)-(int)(stdscrX/3)-7), (stdscrY-15), (int)(stdscrX/3)+5);
wrefresh(detailview);
}
}
@ -183,6 +200,7 @@ WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive
newWindow = newwin(iYSize, iXSize, 2, iXStart);
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_DETAIL));
box(newWindow, ACS_VLINE, ACS_HLINE);
string title = "Selected Drive: " + drive.getModelName() + " " + drive.sCapacityToText();
centerTitle(newWindow, title.c_str());
@ -242,6 +260,40 @@ WINDOW* TUI::createDetailViewWindow( int iXSize, int iYSize, int iXStart, Drive
return newWindow;
}
WINDOW* TUI::overwriteDetailViewWindow( int iXSize, int iYSize, int iXStart)
{
WINDOW *newWindow;
newWindow = newwin(iYSize, iXSize, 2, iXStart);
wbkgd(newWindow, COLOR_PAIR(COLOR_AREA_DETAIL));
box(newWindow, ACS_VLINE, ACS_HLINE);
string title = "About this tool";
centerTitle(newWindow, title.c_str());
string sLine01 = "Path: NextLine ";
string sLine02 = "Path: NextLine ";
string sLine03 = "Path: NextLine ";
string sLine04 = "Path: NextLine ";
string sLine05 = "Path: NextLine ";
string sLine06 = "Path: NextLine ";
string sLine07 = "Path: NextLine ";
uint16_t u16Line = 5;
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine01.size()/2), sLine01.c_str());
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine02.size()/2), sLine02.c_str());
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine03.size()/2), sLine03.c_str());
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine04.size()/2), sLine04.c_str());
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine05.size()/2), sLine05.c_str());
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine06.size()/2), sLine06.c_str());
mvwaddstr(newWindow,u16Line++, (iXSize/2)-(sLine07.size()/2), sLine07.c_str());
attroff(COLOR_PAIR(COLOR_AREA_DETAIL));
keypad(newWindow, TRUE);
return newWindow;
}
WINDOW* TUI::createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, string sModelFamily, string sModelName, string sCapacity, string sState, bool bSelected)
{
WINDOW *newWindow;

3
vcCodium.code-workspace

@ -60,7 +60,8 @@
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"iomanip": "cpp"
},
"git.ignoreLimitWarning": true
}
Loading…
Cancel
Save