#pragma once
#include "ppsspp_config.h"
#if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP)
#include <atomic>
#include <thread>
#include "Common/Log.h"
#include "Common/CommonWindows.h"
struct LogMessage;
class ConsoleListener {
public:
ConsoleListener();
~ConsoleListener();
void Init(bool AutoOpen = true, int Width = 200, int Height = 100);
void Open();
void UpdateHandle();
void Close();
bool IsOpen();
void LetterSpace(int Width, int Height);
void BufferWidthHeight(int BufferWidth, int BufferHeight, int ScreenWidth, int ScreenHeight, bool BufferFirst);
void PixelSpace(int Left, int Top, int Width, int Height, bool);
COORD GetCoordinates(int BytesRead, int BufferWidth);
void Log(const LogMessage &message);
void ClearScreen(bool Cursor = true);
void Show(bool bShow);
bool Hidden() const { return hidden_; }
private:
HWND hWnd = nullptr;
HANDLE hConsole = nullptr;
void LogWriterThread();
void SendToThread(LogLevel Level, const char *Text);
void WriteToConsole(LogLevel Level, const char *Text, size_t Len);
std::thread thread_;
HANDLE hTriggerEvent = nullptr;
CRITICAL_SECTION criticalSection{};
char *logPending_ = nullptr;
std::atomic<uint32_t> logPendingReadPos_;
std::atomic<uint32_t> logPendingWritePos_;
int openWidth_ = 0;
int openHeight_ = 0;
bool hidden_ = false;
bool useColor_ = true;
bool useThread_ = true;
};
#endif