Déplacement vers Menu depuis Main, ajout de menus, réglage de la fenêtre principale

This commit is contained in:
Horde Nicolas 2021-07-04 19:20:46 +02:00
parent 5e93ab4600
commit 4d308668d1
2 changed files with 163 additions and 158 deletions

View File

@ -1,6 +1,7 @@
CC=g++ -O2 CC=g++ -O2
LFLAGS=-lfinal -lkeystone -lstdc++ -lm -lcapstone -lunicorn LFLAGS=-lfinal -lkeystone -lstdc++ -lm -lcapstone -lunicorn
DOCKER=docker run -it --name maker --rm -v $$(pwd):/data maker DOCKER=docker run -it -e COLUMNS="$$(tput cols)" -e LINES="$$(tput lines)" --name maker --rm -v $$(pwd):/data maker
XTERM=terminator -f -e
all: dockerfile files run all: dockerfile files run
@ -13,7 +14,7 @@ test: ./test.cpp
$(DOCKER) $(CC) -o $@ $^ $(LFLAGS) $(DOCKER) $(CC) -o $@ $^ $(LFLAGS)
run: run:
$(DOCKER) ./test $(XTERM) '$(DOCKER) bash -c "sleep 0.4;./test"'
rerun: delete files run rerun: delete files run

316
test.cpp
View File

@ -1,4 +1,5 @@
#include <final/final.h> #include <final/final.h>
#include <unistd.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <sstream> #include <sstream>
@ -15,7 +16,9 @@ using FKey = finalcut::FKey;
using finalcut::FPoint; using finalcut::FPoint;
using finalcut::FSize; using finalcut::FSize;
//----------------------------------------------------------------------
// Types & classes mineures
//----------------------------------------------------------------------
typedef union { typedef union {
struct { struct {
union { union {
@ -137,7 +140,35 @@ class Goal {
}; };
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class TextFixedWindow // Objectifs de jeux
//----------------------------------------------------------------------
// Ordre des registres ... IP DI SI BP SP BX DX CX AX
Goal goals[]=
{
{
"L'instruction MOV et les registres","Le but est de bouger du registre AX au registre BX, l' ensemble des données", "Aide....", "inc ax\ndec cx\nmov ax,0x33",
{
{
{},
{.bx=0x0002,.ax=0x1920},
0x00000000
},
{}
},
{
{
{},
{.bx=25,.dx=0b101,.cx=0x4650, .ax=0xCCDD},
0x00000000
},
{}
}
}
};
//----------------------------------------------------------------------
// Classe TextFixedWindow
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class TextFixedWindow final : public finalcut::FDialog class TextFixedWindow final : public finalcut::FDialog
@ -161,7 +192,6 @@ class TextFixedWindow final : public finalcut::FDialog
finalcut::FLabel fixedtext{this}; finalcut::FLabel fixedtext{this};
}; };
//----------------------------------------------------------------------
TextFixedWindow::TextFixedWindow (finalcut::FWidget* parent) TextFixedWindow::TextFixedWindow (finalcut::FWidget* parent)
: finalcut::FDialog{parent} : finalcut::FDialog{parent}
{ {
@ -171,13 +201,11 @@ TextFixedWindow::TextFixedWindow (finalcut::FWidget* parent)
fixedtext.setFocus(); fixedtext.setFocus();
} }
//----------------------------------------------------------------------
void TextFixedWindow::refresh() void TextFixedWindow::refresh()
{ {
} }
//----------------------------------------------------------------------
void TextFixedWindow::initLayout() void TextFixedWindow::initLayout()
{ {
fixedtext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1}); fixedtext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
@ -185,7 +213,6 @@ void TextFixedWindow::initLayout()
FDialog::initLayout(); FDialog::initLayout();
} }
//----------------------------------------------------------------------
void TextFixedWindow::adjustSize() void TextFixedWindow::adjustSize()
{ {
finalcut::FDialog::adjustSize(); finalcut::FDialog::adjustSize();
@ -193,9 +220,8 @@ void TextFixedWindow::adjustSize()
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class TextEditWindow // Classe TextEditWindow
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class TextEditWindow final : public finalcut::FDialog class TextEditWindow final : public finalcut::FDialog
{ {
public: public:
@ -209,7 +235,9 @@ class TextEditWindow final : public finalcut::FDialog
TextEditWindow& operator = (const TextEditWindow&) = delete; TextEditWindow& operator = (const TextEditWindow&) = delete;
// Method // Method
std::string get(); std::string get();
void set(std::string str);
private: private:
std::stringstream out;
// Method // Method
void initLayout() override; void initLayout() override;
void adjustSize() override; void adjustSize() override;
@ -217,41 +245,38 @@ class TextEditWindow final : public finalcut::FDialog
finalcut::FLabel fixedtext{this}; finalcut::FLabel fixedtext{this};
}; };
//----------------------------------------------------------------------
TextEditWindow::TextEditWindow (finalcut::FWidget* parent) TextEditWindow::TextEditWindow (finalcut::FWidget* parent)
: finalcut::FDialog{parent} : finalcut::FDialog{parent}
{ {
fixedtext.setText("pour voir");
fixedtext.ignorePadding(); fixedtext.ignorePadding();
fixedtext.setFocus(); fixedtext.setFocus();
} }
//----------------------------------------------------------------------
std::string TextEditWindow::get() std::string TextEditWindow::get()
{ {
return "inc ax\ndec cx\nmov ax,0x33"; out << fixedtext.getText();
return out.str();
}
void TextEditWindow::set(std::string str)
{
fixedtext.setText(str);
} }
//----------------------------------------------------------------------
void TextEditWindow::initLayout() void TextEditWindow::initLayout()
{ {
fixedtext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1}); fixedtext.setGeometry (FPoint{2, 3}, FSize(12, 12));
setMinimumSize (FSize{51, 6});
FDialog::initLayout(); FDialog::initLayout();
} }
//----------------------------------------------------------------------
void TextEditWindow::adjustSize() void TextEditWindow::adjustSize()
{ {
finalcut::FDialog::adjustSize(); finalcut::FDialog::adjustSize();
fixedtext.setGeometry (FPoint{1, 2}, FSize(getWidth(), getHeight() - 1));
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class TextWindow // Classe TextWindow
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class TextWindow final : public finalcut::FDialog class TextWindow final : public finalcut::FDialog
{ {
public: public:
@ -274,25 +299,24 @@ class TextWindow final : public finalcut::FDialog
finalcut::FTextView scrolltext{this}; finalcut::FTextView scrolltext{this};
}; };
//----------------------------------------------------------------------
TextWindow::TextWindow (finalcut::FWidget* parent) TextWindow::TextWindow (finalcut::FWidget* parent)
: finalcut::FDialog{parent} : finalcut::FDialog{parent}
{ {
scrolltext.ignorePadding(); scrolltext.ignorePadding();
scrolltext.setFocus(); scrolltext.setFocus();
} }
//----------------------------------------------------------------------
void TextWindow::onClose(finalcut::FCloseEvent*) void TextWindow::onClose(finalcut::FCloseEvent*)
{ {
return; return;
} }
//----------------------------------------------------------------------
void TextWindow::append(const finalcut::FString& str) void TextWindow::append(const finalcut::FString& str)
{ {
scrolltext.append(str); scrolltext.append(str);
} }
//----------------------------------------------------------------------
void TextWindow::initLayout() void TextWindow::initLayout()
{ {
scrolltext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1}); scrolltext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
@ -300,7 +324,6 @@ void TextWindow::initLayout()
FDialog::initLayout(); FDialog::initLayout();
} }
//----------------------------------------------------------------------
void TextWindow::adjustSize() void TextWindow::adjustSize()
{ {
finalcut::FDialog::adjustSize(); finalcut::FDialog::adjustSize();
@ -308,9 +331,8 @@ void TextWindow::adjustSize()
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class Asm // Classe Assembler
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class Assembler class Assembler
{ {
public: public:
@ -349,7 +371,7 @@ unsigned char *Assembler::Assemble(uint32_t address)
err2=ks_asm(ks, reinterpret_cast<const char*>(src_char), address, &code, &codesize, &srcsize); err2=ks_asm(ks, reinterpret_cast<const char*>(src_char), address, &code, &codesize, &srcsize);
if (err2 != KS_ERR_OK) if (err2 != KS_ERR_OK)
{ {
out << "Erreur de compilation de l'assembleur: " << err; out << "Erreur de compilation de l'assembleur";
log->append(out.str()); log->append(out.str());
} }
else else
@ -369,11 +391,9 @@ unsigned char *Assembler::Assemble(uint32_t address)
return reinterpret_cast<unsigned char*>(&code); return reinterpret_cast<unsigned char*>(&code);
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class VMEngine // Classe VMEngine
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class VMEngine class VMEngine
{ {
public: public:
@ -398,9 +418,6 @@ VMEngine::VMEngine(TextWindow *log) : log(log)
log->append("Initialisation de l'ordinateur IA86"); log->append("Initialisation de l'ordinateur IA86");
} }
//IP DI SI BP SP BX DX CX AX
void VMEngine::Configure(State *init) void VMEngine::Configure(State *init)
{ {
out << "Configuration initiale de l'ordinateur IA86:\n "; out << "Configuration initiale de l'ordinateur IA86:\n ";
@ -498,9 +515,8 @@ void VMEngine::Run()
error=uc_emu_start(uc, ADDRESS, ADDRESS + sizecode, 0, 0);*/ error=uc_emu_start(uc, ADDRESS, ADDRESS + sizecode, 0, 0);*/
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class Menu // Classe Menu
//---------------------------------------------------------------------- //----------------------------------------------------------------------
class Menu final : public finalcut::FDialog class Menu final : public finalcut::FDialog
{ {
public: public:
@ -512,14 +528,21 @@ class Menu final : public finalcut::FDialog
~Menu() override = default; ~Menu() override = default;
// Disable copy assignment operator (=) // Disable copy assignment operator (=)
Menu& operator = (const Menu&) = delete; Menu& operator = (const Menu&) = delete;
// Structures
// Structures
// Methods // Methods
void loadGoal(Goal *g, VMEngine *vm); void setGoal(int num);
void loadGoal();
private: private:
// Methods int scenario=0;
void configureFileMenuItems(); void configureFileMenuItems();
void defaultCallback (const finalcut::FMenuList*); void initMenusCallBack ();
void initMenus();
void initMisc();
void initNow();
void initCore();
void compile();
void exec();
void initWindows();
void splash();
void initLayout() override; void initLayout() override;
void adjustSize() override; void adjustSize() override;
// Event handler // Event handler
@ -529,24 +552,90 @@ class Menu final : public finalcut::FDialog
// Data members // Data members
finalcut::FString line{13, finalcut::UniChar::BoxDrawingsHorizontal}; finalcut::FString line{13, finalcut::UniChar::BoxDrawingsHorizontal};
finalcut::FMenuBar Menubar{this}; finalcut::FMenuBar Menubar{this};
finalcut::FMenu File{"&Menu", &Menubar}; finalcut::FMenu File{"&Fichier", &Menubar};
finalcut::FMenuItem New{"&Nouvelle partie", &File};
finalcut::FMenuItem Line2{&File}; finalcut::FMenuItem Line2{&File};
finalcut::FMenuItem Quit{"&Quit", &File}; finalcut::FMenuItem Quit{"&Quitter", &File};
finalcut::FMenuItem Window{"&Windows", &Menubar}; finalcut::FMenu Compile{"&Compilation", &Menubar};
finalcut::FMenuItem Start{"&Lancer", &Compile};
finalcut::FMenu Debug{"&Deboguage", &Menubar};
finalcut::FMenuItem Exec{"&Executer", &Debug};
finalcut::FMenuItem Step{"Pas &détaillé", &Debug};
finalcut::FMenuItem StepOver{"&Pas", &Debug};
finalcut::FDialogListMenu Window{"&Fenêtres", &Menubar};
finalcut::FLabel Info{this}; finalcut::FLabel Info{this};
finalcut::FStatusBar Statusbar{this}; finalcut::FStatusBar Statusbar{this};
TextWindow log{this};
TextWindow view{this};
TextEditWindow edit{this};
VMEngine vm{&log};
Assembler asmer{&edit,&log};
}; };
//----------------------------------------------------------------------
Menu::Menu (finalcut::FWidget* parent) Menu::Menu (finalcut::FWidget* parent)
: finalcut::FDialog{parent} : finalcut::FDialog{parent}
{ {
File.setStatusbarMessage ("main menu"); initNow();
Window.setDisable(); }
configureFileMenuItems();
defaultCallback (&Menubar); void Menu::initNow()
Statusbar.setMessage("Status bar message"); {
initWindows();
initMisc();
initMenus();
initMenusCallBack();
initCore();
}
void Menu::initCore()
{
setGoal(0);
}
void Menu::initWindows()
{
log.setText ("Journaux");
log.setGeometry ( FPoint { 62, 45 }, FSize{60, 12} );
log.setResizeable();
log.append("Lancement des journaux");
log.show();
edit.setText ("Code source");
edit.setGeometry ( FPoint { 01, 20 }, FSize{60, 25} );
edit.setResizeable();
edit.show();
view.setText ("Objectif");
view.setGeometry ( FPoint { 01, 45 }, FSize{60, 12} );
view.setResizeable();
view.show();
}
void Menu::initMenus()
{
File.setStatusbarMessage ("Menu principal");
Line2.setSeparator();
Quit.addAccelerator (FKey::Meta_x);
Quit.setStatusbarMessage ("Quitter le programme");
}
void Menu::initMenusCallBack()
{
Quit.addCallback
(
"clicked",
finalcut::getFApplication(),
&finalcut::FApplication::cb_exitApp,
this
);
Start.addCallback
(
"clicked",
this,
&Menu::compile
);
}
void Menu::initMisc()
{
Info << " █████ █████████ ████████ ████████ \n" Info << " █████ █████████ ████████ ████████ \n"
<< "░░███ ███░░░░░███ ███░░░░███ ███░░░░███\n" << "░░███ ███░░░░░███ ███░░░░███ ███░░░░███\n"
<< " ░███ ░███ ░███ ░███ ░███ ░███ ░░░ \n" << " ░███ ░███ ░███ ░███ ░███ ░███ ░░░ \n"
@ -558,127 +647,55 @@ Menu::Menu (finalcut::FWidget* parent)
<< "THE EVEN MORE PEDAGOGICAL SYSTEM !!\n" << "THE EVEN MORE PEDAGOGICAL SYSTEM !!\n"
<< "\n" << "\n"
<< "Episode 1 : Apprendre l'assembleur X86"; << "Episode 1 : Apprendre l'assembleur X86";
Statusbar.setMessage("THE EVEN MORE PEDAGOGICAL SYSTEM !!");
} }
//----------------------------------------------------------------------
void Menu::configureFileMenuItems()
{
Line2.setSeparator();
Quit.addAccelerator (FKey::Meta_x);
Quit.setStatusbarMessage ("Quit the program");
Quit.addCallback
(
"clicked",
finalcut::getFApplication(),
&finalcut::FApplication::cb_exitApp,
this
);
}
//----------------------------------------------------------------------
void Menu::defaultCallback (const finalcut::FMenuList* mb)
{
for (uInt i{1}; i <= mb->getCount(); i++)
{
auto item = mb->getItem(int(i));
if ( item
&& item->isEnabled()
&& item->acceptFocus()
&& item->isVisible()
&& ! item->isSeparator()
&& item->getText() != "&Quit" )
{
item->addCallback
(
"clicked",
this, &Menu::cb_message,
item
);
if ( item->hasMenu() )
defaultCallback (item->getMenu());
}
}
}
//----------------------------------------------------------------------
void Menu::initLayout() void Menu::initLayout()
{ {
Info.setGeometry(FPoint{2, 1}, FSize{43, 12}); Info.setGeometry(FPoint{2, 1}, FSize{43, 12});
FDialog::initLayout(); FDialog::initLayout();
} }
//----------------------------------------------------------------------
void Menu::adjustSize() void Menu::adjustSize()
{ {
const auto pw = int(getDesktopWidth()); const auto pw = int(getDesktopWidth());
const auto ph = int(getDesktopHeight()); const auto ph = int(getDesktopHeight());
setX (1); setX (1 + (pw - int(getWidth())) / 2, false);
setY (1); setY (1 + (ph - int(getHeight())) / 4, false);
finalcut::FDialog::adjustSize(); finalcut::FDialog::adjustSize();
} }
//----------------------------------------------------------------------
void Menu::onClose (finalcut::FCloseEvent* ev) void Menu::onClose (finalcut::FCloseEvent* ev)
{ {
finalcut::FApplication::closeConfirmationDialog (this, ev); finalcut::FApplication::closeConfirmationDialog (this, ev);
} }
//---------------------------------------------------------------------- void Menu::setGoal(int num)
void Menu::cb_message (const finalcut::FMenuItem* menuitem)
{ {
auto text = menuitem->getText(); scenario=num;
text = text.replace('&', ""); loadGoal();
finalcut::FMessageBox::info ( this
, "Info"
, "You have chosen \"" + text + "\"" );
} }
void Menu::loadGoal(Goal *g, VMEngine *vm) void Menu::loadGoal()
{ {
const auto& view = new TextWindow(this); view.setText("Objectif: "+goals[scenario].title);
view->setText ("Objectif: "+g->title); view.append(goals[scenario].description);
view->setGeometry ( FPoint { 10, 10 }, FSize{60, 12} ); edit.set(goals[scenario].code);
view->setResizeable(); vm.Configure(&goals[scenario].init);
view->append(g->description);
view->show();
const auto& test = new TextFixedWindow(this);
test->setText ("test");
test->setGeometry ( FPoint { 20, 10 }, FSize{30, 12} );
test->show();
vm->Configure(&g->init);
} }
//IP DI SI BP SP BX DX CX AX void Menu::compile()
Goal goals[]={
{ {
"L'instruction MOV et les registres","Le but est de bouger du registre AX au registre BX, l' ensemble des données", "Aide....", "mov ax,immédiat", asmer.Assemble(goals[scenario].init.dump.regs.eip);
{
{
{},
{.bx=0x0002,.ax=0x1920},
0x00000000
},
{}
},
{
{
{},
{.bx=25,.dx=0b101,.cx=0x4650, .ax=0xCCDD},
0x00000000
},
{}
} }
void Menu::exec()
{
} }
};
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// main part // Fonction Main
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int main (int argc, char* argv[]) int main (int argc, char* argv[])
{ {
@ -687,22 +704,9 @@ int main (int argc, char* argv[])
main_dlg.setText ("IA86"); main_dlg.setText ("IA86");
main_dlg.setSize ({50, 14}); main_dlg.setSize ({50, 14});
main_dlg.setShadow(); main_dlg.setShadow();
TextWindow log {&main_dlg};
log.setText ("Journaux");
log.setGeometry ( FPoint { 30, 10 }, FSize{60, 12} );
log.setResizeable();
log.append("Lancement des journaux");
log.show();
TextEditWindow edit {&main_dlg};
edit.setText ("Code source");
edit.setGeometry ( FPoint { 30, 10 }, FSize{60, 12} );
edit.setResizeable();
edit.show();
finalcut::FWidget::setMainWidget (&main_dlg);
main_dlg.show(); main_dlg.show();
VMEngine vm {&log}; finalcut::FWidget::setMainWidget (&main_dlg);
Assembler asmer {&edit,&log}; //usleep(5 * 1000000);
asmer.Assemble(0x0000); main_dlg.hide();
main_dlg.loadGoal(&goals[0],&vm);
return app.exec(); return app.exec();
} }