diff --git a/Source/Core/DolphinWX/MainNoGUI.cpp b/Source/Core/DolphinWX/MainNoGUI.cpp index 8c091a0391..2b966876e2 100644 --- a/Source/Core/DolphinWX/MainNoGUI.cpp +++ b/Source/Core/DolphinWX/MainNoGUI.cpp @@ -18,24 +18,26 @@ #include "Core/Core.h" #include "Core/CoreParameter.h" #include "Core/Host.h" +#include "Core/State.h" #include "Core/HW/Wiimote.h" #include "Core/PowerPC/PowerPC.h" #include "VideoCommon/VideoBackendBase.h" -#if HAVE_X11 -#include -#include "Core/State.h" -#include "DolphinWX/X11Utils.h" -#endif - -#ifdef __APPLE__ -#import -#endif - static bool rendererHasFocus = true; static bool running = true; +class Platform +{ +public: + virtual void Init() = 0; + virtual void MainLoop() = 0; + virtual void Shutdown() = 0; + virtual ~Platform() {}; +}; + +static Platform* platform; + void Host_NotifyMapLoaded() {} void Host_RefreshDSPDebuggerWindow() {} @@ -121,54 +123,67 @@ void Host_SetWiiMoteConnectionState(int _State) {} void Host_ShowVideoConfig(void*, const std::string&, const std::string&) {} #if HAVE_X11 -static void X11_MainLoop() +#include +#include "DolphinWX/X11Utils.h" + +class PlatformX11 : public Platform { - bool fullscreen = SConfig::GetInstance().m_LocalCoreStartupParameter.bFullscreen; - while (!Core::IsRunning()) - updateMainFrameEvent.Wait(); - - Display *dpy = XOpenDisplay(0); - Window win = (Window)Core::GetWindowHandle(); - XSelectInput(dpy, win, KeyPressMask | FocusChangeMask); - - if (SConfig::GetInstance().m_LocalCoreStartupParameter.bDisableScreenSaver) - X11Utils::InhibitScreensaver(dpy, win, true); - -#if defined(HAVE_XRANDR) && HAVE_XRANDR - X11Utils::XRRConfiguration *XRRConfig = new X11Utils::XRRConfiguration(dpy, win); -#endif - + Display *dpy; Cursor blankCursor = None; - if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor) - { - // make a blank cursor - Pixmap Blank; - XColor DummyColor; - char ZeroData[1] = {0}; - Blank = XCreateBitmapFromData (dpy, win, ZeroData, 1, 1); - blankCursor = XCreatePixmapCursor(dpy, Blank, Blank, &DummyColor, &DummyColor, 0, 0); - XFreePixmap (dpy, Blank); - XDefineCursor(dpy, win, blankCursor); - } - - if (fullscreen) - { - X11Utils::EWMH_Fullscreen(dpy, _NET_WM_STATE_TOGGLE); #if defined(HAVE_XRANDR) && HAVE_XRANDR - XRRConfig->ToggleDisplayMode(True); + X11Utils::XRRConfiguration *XRRConfig; #endif + + void Init() override + { + XInitThreads(); + dpy = XOpenDisplay(NULL); } - // The actual loop - while (running) + void MainLoop() override { - XEvent event; - KeySym key; - for (int num_events = XPending(dpy); num_events > 0; num_events--) + Window win = (Window)Core::GetWindowHandle(); + XSelectInput(dpy, win, KeyPressMask | FocusChangeMask); + + if (SConfig::GetInstance().m_LocalCoreStartupParameter.bDisableScreenSaver) + X11Utils::InhibitScreensaver(dpy, win, true); + +#if defined(HAVE_XRANDR) && HAVE_XRANDR + XRRConfig = new X11Utils::XRRConfiguration(dpy, win); +#endif + + if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor) { - XNextEvent(dpy, &event); - switch (event.type) + // make a blank cursor + Pixmap Blank; + XColor DummyColor; + char ZeroData[1] = {0}; + Blank = XCreateBitmapFromData (dpy, win, ZeroData, 1, 1); + blankCursor = XCreatePixmapCursor(dpy, Blank, Blank, &DummyColor, &DummyColor, 0, 0); + XFreePixmap (dpy, Blank); + XDefineCursor(dpy, win, blankCursor); + } + + bool fullscreen = SConfig::GetInstance().m_LocalCoreStartupParameter.bFullscreen; + + if (fullscreen) + { + X11Utils::EWMH_Fullscreen(dpy, _NET_WM_STATE_TOGGLE); +#if defined(HAVE_XRANDR) && HAVE_XRANDR + XRRConfig->ToggleDisplayMode(True); +#endif + } + + // The actual loop + while (running) + { + XEvent event; + KeySym key; + for (int num_events = XPending(dpy); num_events > 0; num_events--) { + XNextEvent(dpy, &event); + switch (event.type) + { case KeyPress: key = XLookupKeysym((XKeyEvent*)&event, 0); if (key == XK_Escape) @@ -217,7 +232,7 @@ static void X11_MainLoop() case FocusIn: rendererHasFocus = true; if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor && - Core::GetState() != Core::CORE_PAUSE) + Core::GetState() != Core::CORE_PAUSE) XDefineCursor(dpy, win, blankCursor); break; case FocusOut: @@ -225,44 +240,47 @@ static void X11_MainLoop() if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor) XUndefineCursor(dpy, win); break; + } } + if (!fullscreen) + { + Window winDummy; + unsigned int borderDummy, depthDummy; + XGetGeometry(dpy, win, &winDummy, + &SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowXPos, + &SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowYPos, + (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowWidth, + (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowHeight, + &borderDummy, &depthDummy); + } + usleep(100000); } - if (!fullscreen) - { - Window winDummy; - unsigned int borderDummy, depthDummy; - XGetGeometry(dpy, win, &winDummy, - &SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowXPos, - &SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowYPos, - (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowWidth, - (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowHeight, - &borderDummy, &depthDummy); - } - usleep(100000); } + void Shutdown() override + { #if defined(HAVE_XRANDR) && HAVE_XRANDR - delete XRRConfig; + delete XRRConfig; #endif - if (SConfig::GetInstance().m_LocalCoreStartupParameter.bDisableScreenSaver) - X11Utils::InhibitScreensaver(dpy, win, false); - if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor) - XFreeCursor(dpy, blankCursor); - XCloseDisplay(dpy); - Core::Stop(); -} + if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor) + XFreeCursor(dpy, blankCursor); + + XCloseDisplay(dpy); + } +}; #endif +static Platform* GetPlatform() +{ +#if HAVE_X11 + return new PlatformX11(); +#endif + return nullptr; +} + int main(int argc, char* argv[]) { -#ifdef __APPLE__ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSEvent *event = [[NSEvent alloc] init]; - [NSApplication sharedApplication]; - [NSApp activateIgnoringOtherApps: YES]; - [NSApp finishLaunching]; -#endif int ch, help = 0; struct option longopts[] = { { "exec", no_argument, nullptr, 'e' }, @@ -298,6 +316,13 @@ int main(int argc, char* argv[]) return 1; } + platform = GetPlatform(); + if (!platform) + { + fprintf(stderr, "No platform found\n"); + return 1; + } + LogManager::Init(); SConfig::Init(); VideoBackend::PopulateList(); @@ -305,45 +330,30 @@ int main(int argc, char* argv[]) m_LocalCoreStartupParameter.m_strVideoBackend); WiimoteReal::LoadSettings(); - // No use running the loop when booting fails - if (BootManager::BootCore(argv[optind])) + platform->Init(); + + if (!BootManager::BootCore(argv[optind])) { -#if HAVE_X11 - XInitThreads(); - X11_MainLoop(); -#endif -#ifdef __APPLE__ - while (running) - { - event = [NSApp nextEventMatchingMask: NSAnyEventMask - untilDate: [NSDate distantFuture] - inMode: NSDefaultRunLoopMode dequeue: YES]; - - if ([event type] == NSKeyDown && - [event modifierFlags] & NSCommandKeyMask && - [[event characters] UTF8String][0] == 'q') - { - Core::Stop(); - break; - } - - if ([event type] != NSKeyDown) - [NSApp sendEvent: event]; - } - - [event release]; - [pool release]; -#else - while (PowerPC::GetState() != PowerPC::CPU_POWERDOWN) - updateMainFrameEvent.Wait(); -#endif + fprintf(stderr, "Could not boot %s\n", argv[optind]); + return 1; } + while (!Core::IsRunning()) + updateMainFrameEvent.Wait(); + + platform->MainLoop(); + Core::Stop(); + while (PowerPC::GetState() != PowerPC::CPU_POWERDOWN) + updateMainFrameEvent.Wait(); + + platform->Shutdown(); Core::Shutdown(); WiimoteReal::Shutdown(); VideoBackend::ClearList(); SConfig::Shutdown(); LogManager::Shutdown(); + delete platform; + return 0; }