Mise en place d'un système de menu & de fenêtre

This commit is contained in:
Horde Nicolas 2021-07-03 19:32:11 +02:00
parent 9df472a3c8
commit 33a45f1bd4
2 changed files with 340 additions and 73 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
test
*.old

401
test.cpp
View File

@ -1,73 +1,321 @@
#include <final/final.h>
#include <iostream>
#include <string>
#include <sstream>
#include <keystone/keystone.h>
#include <unicorn/unicorn.h>
#include <capstone/capstone.h>
#include <stdio.h>
using namespace std;
#define CODE "INC cx; DEC dx"
using FKey = finalcut::FKey;
using finalcut::FPoint;
using finalcut::FSize;
#define ADDRESS 0x1000
int main (int argc, char* argv[])
//----------------------------------------------------------------------
// class TextFixedWindow
//----------------------------------------------------------------------
class TextFixedWindow final : public finalcut::FDialog
{
ks_engine *ks;
ks_err err;
size_t count;
unsigned char *encode;
size_t sizecode;
public:
// Constructor
explicit TextFixedWindow (finalcut::FWidget* = nullptr);
// Disable copy constructor
TextFixedWindow (const TextFixedWindow&) = delete;
// Destructor
~TextFixedWindow() override = default;
// Disable copy assignment operator (=)
TextFixedWindow& operator = (const TextFixedWindow&) = delete;
// Method
void refresh ();
private:
// Method
void initLayout() override;
void adjustSize() override;
// Data members
finalcut::FLabel fixedtext{this};
};
err = ks_open(KS_ARCH_X86, KS_MODE_16, &ks);
if (err != KS_ERR_OK) {
printf("ERROR: failed on ks_open(), quit\n");
return -1;
//----------------------------------------------------------------------
TextFixedWindow::TextFixedWindow (finalcut::FWidget* parent)
: finalcut::FDialog{parent}
{
fixedtext.setText("pour voir");
fixedtext.ignorePadding();
fixedtext.setFocus();
}
//----------------------------------------------------------------------
void TextFixedWindow::refresh ()
{
}
//----------------------------------------------------------------------
void TextFixedWindow::initLayout()
{
fixedtext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
setMinimumSize (FSize{51, 6});
FDialog::initLayout();
}
//----------------------------------------------------------------------
void TextFixedWindow::adjustSize()
{
finalcut::FDialog::adjustSize();
fixedtext.setGeometry (FPoint{1, 2}, FSize(getWidth(), getHeight() - 1));
}
//----------------------------------------------------------------------
// class TextWindow
//----------------------------------------------------------------------
class TextWindow final : public finalcut::FDialog
{
public:
// Constructor
explicit TextWindow (finalcut::FWidget* = nullptr);
// Disable copy constructor
TextWindow (const TextWindow&) = delete;
// Destructor
~TextWindow() override = default;
// Disable copy assignment operator (=)
TextWindow& operator = (const TextWindow&) = delete;
// Method
void append (const finalcut::FString&);
private:
// Method
void initLayout() override;
void adjustSize() override;
// Data members
finalcut::FTextView scrolltext{this};
};
//----------------------------------------------------------------------
TextWindow::TextWindow (finalcut::FWidget* parent)
: finalcut::FDialog{parent}
{
scrolltext.ignorePadding();
scrolltext.setFocus();
}
//----------------------------------------------------------------------
void TextWindow::append (const finalcut::FString& str)
{
scrolltext.append(str);
}
//----------------------------------------------------------------------
void TextWindow::initLayout()
{
scrolltext.setGeometry (FPoint{1, 2}, FSize{getWidth(), getHeight() - 1});
setMinimumSize (FSize{51, 6});
FDialog::initLayout();
}
//----------------------------------------------------------------------
void TextWindow::adjustSize()
{
finalcut::FDialog::adjustSize();
scrolltext.setGeometry (FPoint{1, 2}, FSize(getWidth(), getHeight() - 1));
}
//----------------------------------------------------------------------
// class Menu
//----------------------------------------------------------------------
class Menu final : public finalcut::FDialog
{
public:
// Constructor
explicit Menu (finalcut::FWidget* = nullptr);
// Disable copy constructor
Menu (const Menu&) = delete;
// Destructor
~Menu() override = default;
// Disable copy assignment operator (=)
Menu& operator = (const Menu&) = delete;
// Structures
struct Goal {
string title;
string description;
};
// Methods
void loadGoal(Goal *g);
private:
// Methods
void configureFileMenuItems();
void defaultCallback (const finalcut::FMenuList*);
void initLayout() override;
void adjustSize() override;
// Event handler
void onClose (finalcut::FCloseEvent*) override;
// Callback method
void cb_message (const finalcut::FMenuItem*);
// Data members
finalcut::FString line{13, finalcut::UniChar::BoxDrawingsHorizontal};
finalcut::FMenuBar Menubar{this};
finalcut::FMenu File{"&Menu", &Menubar};
finalcut::FMenuItem Line2{&File};
finalcut::FMenuItem Quit{"&Quit", &File};
finalcut::FMenuItem Window{"&Windows", &Menubar};
finalcut::FLabel Info{this};
finalcut::FStatusBar Statusbar{this};
};
//----------------------------------------------------------------------
Menu::Menu (finalcut::FWidget* parent)
: finalcut::FDialog{parent}
{
File.setStatusbarMessage ("main menu");
Window.setDisable();
configureFileMenuItems();
defaultCallback (&Menubar);
Statusbar.setMessage("Status bar message");
Info << " █████ █████████ ████████ ████████ \n"
<< "░░███ ███░░░░░███ ███░░░░███ ███░░░░███\n"
<< " ░███ ░███ ░███ ░███ ░███ ░███ ░░░ \n"
<< " ░███ ░███████████ ░░████████ ░█████████ \n"
<< " ░███ ░███░░░░░███ ███░░░░███ ░███░░░░███\n"
<< " ░███ ░███ ░███ ░███ ░███ ░███ ░███\n"
<< " █████ █████ █████░░████████ ░░████████ \n"
<< "░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░░░░ \n"
<< "THE EVEN MORE PEDAGOGICAL SYSTEM !!\n"
<< "\n"
<< "Episode 1 : Apprendre l'assembleur X86";
}
//----------------------------------------------------------------------
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());
}
if (ks_asm(ks, CODE, 0, &encode, &sizecode, &count) != KS_ERR_OK) {
printf("ERROR: ks_asm() failed & count = %lu, error = %u\n",
count, ks_errno(ks));
} else {
size_t i;
printf("%s = ", CODE);
for (i = 0; i < sizecode; i++) {
printf("%02x ", encode[i]);
}
printf("\n");
printf("Compiled: %lu bytes, statements: %lu\n", sizecode, count);
}
//----------------------------------------------------------------------
void Menu::initLayout()
{
Info.setGeometry(FPoint{2, 1}, FSize{43, 12});
FDialog::initLayout();
}
//----------------------------------------------------------------------
void Menu::adjustSize()
{
const auto pw = int(getDesktopWidth());
const auto ph = int(getDesktopHeight());
setX (1);
setY (1);
finalcut::FDialog::adjustSize();
}
//----------------------------------------------------------------------
void Menu::onClose (finalcut::FCloseEvent* ev)
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
void Menu::cb_message (const finalcut::FMenuItem* menuitem)
{
auto text = menuitem->getText();
text = text.replace('&', "");
finalcut::FMessageBox::info ( this
, "Info"
, "You have chosen \"" + text + "\"" );
}
void Menu::loadGoal(Goal *g)
{
const auto& view = new TextWindow(this);
view->setText ("Objectif: "+g->title);
view->setGeometry ( FPoint { 10, 10 }, FSize{60, 12} );
view->setResizeable();
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();
}
//----------------------------------------------------------------------
// class VMEngine
//----------------------------------------------------------------------
class VMEngine
{
public:
VMEngine(TextWindow *T);
private:
uc_engine *uc;
uc_err err;
TextWindow *T;
std::ostringstream out;
};
VMEngine::VMEngine(TextWindow *T) : T(T)
{
err = uc_open(UC_ARCH_X86, UC_MODE_16, &uc);
if (err != UC_ERR_OK) {
out << "Failed on uc_open() with error returned: " << err;
T->append(out.str());
}
ks_free(encode);
ks_close(ks);
csh handle;
cs_insn *insn;
out << "Failed on uc_open() with error returned: " << "ok";
T->append(out.str());
if (cs_open(CS_ARCH_X86, CS_MODE_16, &handle) != CS_ERR_OK)
return -1;
count = cs_disasm(handle, encode, sizecode, ADDRESS, 0, &insn);
if (count > 0) {
size_t j;
for (j = 0; j < count; j++) {
printf("0x%"PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
insn[j].op_str);
}
}
cs_free(insn, count);
} else
printf("ERROR: Failed to disassemble given code!\n");
/*VMEngine::Configure(int address)
{
cs_close(&handle);
}
uc_engine *uc;
uc_err error;
int r_cx = 0x1234;
int r_dx = 0x7890;
int r_ip = 0x0000;
printf("Emulate i386 code\n");
error = uc_open(UC_ARCH_X86, UC_MODE_16, &uc);
if (error != UC_ERR_OK) {
printf("Failed on uc_open() with error returned: %u\n", error);
return -1;
}
uc_mem_map(uc, ADDRESS, 1 * 1024 * 1024, UC_PROT_ALL);
VMEngine::Run()
{
}
/*uc_mem_map(uc, ADDRESS, 1 * 1024 * 1024, UC_PROT_ALL);
if (uc_mem_write(uc, ADDRESS, encode, sizecode)) {
printf("Failed to write emulation code to memory, quit!\n");
return -1;
@ -75,13 +323,10 @@ uc_engine *uc;
uc_reg_write(uc, UC_X86_REG_CX, &r_cx);
uc_reg_write(uc, UC_X86_REG_DX, &r_dx);
uc_reg_read(uc, UC_X86_REG_IP, &r_ip);
printf(">>> CX = 0x%x\n", r_cx);
printf(">>> DX = 0x%x\n", r_dx);
printf(">>> IP = 0x%x\n", r_ip);
error=uc_emu_start(uc, ADDRESS, ADDRESS + sizecode, 0, 0);
if (error) {
printf("Failed on uc_emu_start() with error returned %u: %s\n",
error, uc_strerror(error));
err, uc_strerror(error));
}
printf("Emulation done. Below is the CPU context\n");
uc_reg_read(uc, UC_X86_REG_CX, &r_cx);
@ -89,15 +334,35 @@ uc_engine *uc;
uc_reg_read(uc, UC_X86_REG_IP, &r_ip);
printf(">>> CX = 0x%x\n", r_cx);
printf(">>> DX = 0x%x\n", r_dx);
printf(">>> IP = 0x%x\n", r_ip);
uc_close(uc);
finalcut::FApplication app(argc, argv);
finalcut::FDialog dialog(&app);
dialog.setText ("A dialog");
const finalcut::FPoint position{25, 5};
const finalcut::FSize size{30, 10};
dialog.setGeometry (position, size);
finalcut::FWidget::setMainWidget(&dialog);
dialog.show();
printf(">>> IP = 0x%x\n", r_ip);*/
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
Menu::Goal goals[]={ {"Numération","Le première objectif vise à savoir convertir des nombres d'une base de numération à une autre."},{"L'instruction MOV",""},{"La mémoire",""}, };
finalcut::FApplication app {argc, argv};
Menu main_dlg {&app};
main_dlg.setText ("IA86 - Main window");
main_dlg.setSize ({50, 14});
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();
finalcut::FWidget::setMainWidget (&main_dlg);
main_dlg.show();
main_dlg.loadGoal(&goals[0]);
VMEngine vm {&log};
return app.exec();
}