Skip to content

Commit

Permalink
Fix creation of UAE window and process events in main thread
Browse files Browse the repository at this point in the history
- Due to bug on MacOS: NSWindow should only be instantiated on the main thread!
- A known bug is that shortcuts are now global instead of separate for each window.
  • Loading branch information
dartfnm committed Nov 28, 2024
1 parent 223fcc9 commit 327dc26
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 207 deletions.
7 changes: 4 additions & 3 deletions src/debugger/action/uae_actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct DisasmTraceStep : public Action {
virtual EFlow applyMsgProc(action::msg::Base* msg) override {
switch (msg->mId) {
case action::msg::DoAction::ID: {
getDbg()->setDebugMode(DebuggerMode_Break);
getDbg()->execConsoleCmd("t");
return EFlow::SUCCESS;
} break;
Expand Down Expand Up @@ -283,14 +284,14 @@ struct UaeWndAlwaysOnTop : public Action {
virtual EFlow applyMsgProc(action::msg::Base* msg) override {
switch (msg->mId) {
case msg::DoAction::ID: {
Uint32 flags = SDL_GetWindowFlags(app->s_window);
Uint32 flags = SDL_GetWindowFlags(app->mUaeWindow);
bool setOnTop = (flags & SDL_WINDOW_ALWAYS_ON_TOP) != 0;
SDL_SetWindowAlwaysOnTop(app->s_window, (SDL_bool)(!setOnTop));
SDL_SetWindowAlwaysOnTop(app->mUaeWindow, (SDL_bool)(!setOnTop));
return EFlow::SUCCESS;
} break;
case msg::MenuItemStateGet::ID: {
auto p = static_cast<msg::MenuItemStateGet*>(msg);
Uint32 flags = SDL_GetWindowFlags(app->s_window);
Uint32 flags = SDL_GetWindowFlags(app->mUaeWindow);
p->checked = (flags & SDL_WINDOW_ALWAYS_ON_TOP) ? 1 : 0;
return EFlow::SUCCESS;
} break;
Expand Down
178 changes: 92 additions & 86 deletions src/debugger/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,64 +28,7 @@

namespace qd {

Debugger* Debugger_create() {
uint32_t window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN;
SDL_Window* window =
SDL_CreateWindow("Quaesar: Debugger", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);

// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif

if (!window) {
fprintf(stderr, "Error creating window.\n");
return nullptr;
}

SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (!renderer) {
SDL_DestroyWindow(window);
SDL_Log("Error creating SDL_Renderer!");
return nullptr;
}

// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;

// Setup Dear ImGui style
UiStyle::get()->applyImGuiDarkStyle();

// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer2_Init(renderer);

Debugger* debugger = Debugger::get();
debugger->window = window;
debugger->renderer = renderer;

// create windows
debugger->create();
return debugger;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Debugger_update_event(SDL_Event* event) {
ImGui_ImplSDL2_ProcessEvent(event);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Debugger::update() {
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
Debugger* debugger = this;

qd::ShortcutsMgr::get()->update();

// Start the Dear ImGui frame
Expand All @@ -94,36 +37,8 @@ void Debugger::update() {
ImGui::NewFrame();

gui->drawImGuiMainFrame();

// Rendering
ImGuiIO& io = ImGui::GetIO();
ImGui::Render();
SDL_RenderSetScale(debugger->renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
SDL_SetRenderDrawColor(debugger->renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255),
(Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
SDL_RenderClear(debugger->renderer);
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(debugger->renderer);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool Debugger_is_window_visible(Debugger* debugger) {
uint32_t window_flags = SDL_GetWindowFlags(debugger->window);
if (window_flags & SDL_WINDOW_HIDDEN) {
return false;
} else {
return true;
}
}

void Debugger_toggle(Debugger* debugger, DebuggerMode mode) {
if (!Debugger_is_window_visible(debugger)) {
SDL_ShowWindow(debugger->window);
} else {
SDL_HideWindow(debugger->window);
}
}

//////////////////////////////////////////////////////////////////////////
namespace imp {
Expand Down Expand Up @@ -180,7 +95,9 @@ static bool uae_bp_reg_convert(int uae_reg, EReg& out) {

//////////////////////////////////////////////////////////////////////////

void Debugger::create() {
void Debugger::init() {
createRenderWindow();
initImGui();
vm = VM::setVmInst(new qd::vm::imp::UaeEmuVmImp());
vm->init();
gui = new GuiManager(this);
Expand All @@ -197,6 +114,52 @@ void Debugger::create() {
cs_option(*capstone, CS_OPT_DETAIL, CS_OPT_ON);
}


void Debugger::createRenderWindow() {
uint32_t window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN;
SDL_Window* window =
SDL_CreateWindow("Quaesar: Debugger", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);

// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif

if (!window) {
fprintf(stderr, "Error creating window.\n");
SDL_Quit();
return;
}

mRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (!mRenderer) {
SDL_DestroyWindow(window);
SDL_Log("Error creating SDL_Renderer!");
SDL_Quit();
return;
}
mWindow = window;

// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
}


void Debugger::initImGui() {
// Setup Dear ImGui style
UiStyle::get()->applyImGuiDarkStyle();

// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForSDLRenderer(mWindow, mRenderer);
ImGui_ImplSDLRenderer2_Init(mRenderer);
}


bool Debugger::isDebugActivated() {
return ::debugging > 0 && (::debugger_active > 0);
}
Expand Down Expand Up @@ -264,6 +227,49 @@ void Debugger::destroy() {
gui = nullptr;
vm = nullptr;
VM::destrotVmInst();

SDL_DestroyRenderer(mRenderer);
mRenderer = nullptr;
SDL_DestroyWindow(mWindow);
mWindow = nullptr;
}


void Debugger::render() {
Debugger* debugger = this;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImGuiIO& io = ImGui::GetIO();
ImGui::Render();
SDL_RenderSetScale(debugger->mRenderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
SDL_SetRenderDrawColor(debugger->mRenderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255),
(Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
SDL_RenderClear(debugger->mRenderer);
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(debugger->mRenderer);
}


bool Debugger::isVisible() const {
uint32_t window_flags = SDL_GetWindowFlags(mWindow);
if (window_flags & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) {
return false;
} else {
return true;
}
}


void Debugger::toggleWndVisible(DebuggerMode mode) {
if (!isVisible()) {
SDL_ShowWindow(mWindow);
} else {
SDL_HideWindow(mWindow);
}
}


void Debugger::sdlEventProc(SDL_Event* event) {
ImGui_ImplSDL2_ProcessEvent(event);
}


Expand Down
25 changes: 14 additions & 11 deletions src/debugger/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,30 @@ class BreakpointsSortedList {

//////////////////////////////////////////////////////////////////////////
class Debugger {
SDL_Window* mWindow = nullptr;
SDL_Renderer* mRenderer = nullptr;

public:
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
csh* capstone = nullptr;
VM* vm = nullptr;
GuiManager* gui = nullptr;
action::ActionManager* actions = nullptr;

SDL_Renderer* getRenderer() const {
return mRenderer;
}

private:
int mWaitScanLines = 1;

public:
void create();
void init();
void destroy();
void update();
void render();
bool isVisible() const;
void toggleWndVisible(DebuggerMode mode);
void sdlEventProc(SDL_Event* event);

qd::VM* getVm() const {
return vm;
Expand Down Expand Up @@ -119,18 +128,12 @@ class Debugger {
}

private:
void createRenderWindow();
void initImGui();
Debugger() = default;
~Debugger() = default;
}; // class Debugger
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Debugger* Debugger_create();
void Debugger_update_event(SDL_Event* event);
void Debugger_destroy(Debugger* debugger);
void Debugger_toggle(Debugger* debugger, DebuggerMode mode);
bool Debugger_is_window_visible(Debugger* debugger);
//////////////////////////////////////////////////////////////////////////


}; // namespace qd
2 changes: 1 addition & 1 deletion src/debugger/window/colors_wnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void ColorsWnd::drawContent() {

bool colorChanged = false;
for (size_t n = 0; n < palette.size(); n++) {
ImGui::PushID(n);
ImGui::PushID((int)n);
if ((n % 8) != 0)
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);

Expand Down
4 changes: 2 additions & 2 deletions src/debugger/window/mem_graph_wnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void MemoryGraphWnd::drawContent() {
if (mNewTextureSize.y > 1 && mNewTextureSize.y > 0) {
mTextureSize = mNewTextureSize;
SDL_Texture* scrTexture = nullptr;
scrTexture = SDL_CreateTexture(getDbg()->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
mTextureSize.x, mTextureSize.y);
scrTexture = SDL_CreateTexture(getDbg()->getRenderer(), SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, mTextureSize.x, mTextureSize.y);
if (scrTexture) {
mTextureId = (ImTextureID)(scrTexture);
SDL_SetTextureBlendMode(scrTexture, SDL_BLENDMODE_BLEND);
Expand Down
2 changes: 1 addition & 1 deletion src/debugger/window/screen_wnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void ScreenWnd::drawContent() {
if (SDL_Init(SDL_INIT_VIDEO) != 0)
return;

SDL_Texture* scrTexture = SDL_CreateTexture(getDbg()->renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_Texture* scrTexture = SDL_CreateTexture(getDbg()->getRenderer(), SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, amiga_width, amiga_height);
if (scrTexture) {
mTextureId = (ImTextureID)scrTexture;
Expand Down
Loading

0 comments on commit 327dc26

Please sign in to comment.