Moved WiimoteNew into Dolphin and eliminated the old wiimote plugin. Removed wiimote...
[dolphin.git] / Source / Core / DolphinWX / Src / MainNoGUI.cpp
blob171fc2281de9f006d9f4ecfe6c52b381cb83f591
1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
22 #ifndef _WIN32
23 #include <sys/param.h>
24 #endif
26 #include "Common.h"
27 #include "FileUtil.h"
29 #if defined HAVE_X11 && HAVE_X11
30 #include <X11/keysym.h>
31 #include "State.h"
32 #include "X11Utils.h"
33 #endif
35 #ifdef __APPLE__
36 #import "cocoaApp.h"
37 #endif
39 #include "Core.h"
40 #include "Host.h"
41 #include "CPUDetect.h"
42 #include "cmdline.h"
43 #include "Thread.h"
44 #include "PowerPC/PowerPC.h"
46 #include "PluginManager.h"
47 #include "ConfigManager.h"
48 #include "LogManager.h"
49 #include "BootManager.h"
51 #if defined HAVE_X11 && HAVE_X11
52 bool running = true;
53 #endif
55 bool rendererHasFocus = true;
57 void Host_NotifyMapLoaded(){}
59 void Host_ShowJitResults(unsigned int address){}
61 Common::Event updateMainFrameEvent;
62 void Host_Message(int Id)
64 #if defined(HAVE_X11) && HAVE_X11
65 switch (Id)
67 case WM_USER_STOP:
68 running = false;
69 break;
71 #endif
74 void Host_UpdateTitle(const char* title){};
76 void Host_UpdateLogDisplay(){}
79 void Host_UpdateDisasmDialog(){}
82 void Host_UpdateMainFrame()
84 updateMainFrameEvent.Set();
87 void Host_UpdateBreakPointView(){}
90 void Host_UpdateMemoryView(){}
93 void Host_SetDebugMode(bool){}
95 void Host_RequestWindowSize(int& x, int& y, int& width, int& height)
97 x = SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowXPos;
98 y = SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowYPos;
99 width = SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowWidth;
100 height = SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowHeight;
103 bool Host_RendererHasFocus()
105 return rendererHasFocus;
108 void Host_SetWaitCursor(bool enable){}
111 void Host_UpdateStatusBar(const char* _pText, int Filed){}
113 void Host_SysMessage(const char *fmt, ...)
115 va_list list;
116 char msg[512];
118 va_start(list, fmt);
119 vsprintf(msg, fmt, list);
120 va_end(list);
122 size_t len = strlen(msg);
123 if (msg[len - 1] != '\n') {
124 msg[len - 1] = '\n';
125 msg[len] = '\0';
127 fprintf(stderr, "%s", msg);
130 void Host_SetWiiMoteConnectionState(int _State) {}
132 #if defined(HAVE_X11) && HAVE_X11
133 void X11_MainLoop()
135 bool fullscreen = SConfig::GetInstance().m_LocalCoreStartupParameter.bFullscreen;
136 while (Core::GetState() == Core::CORE_UNINITIALIZED)
137 updateMainFrameEvent.Wait();
139 Display *dpy = XOpenDisplay(0);
140 Window win = (Window)Core::GetWindowHandle();
141 XSelectInput(dpy, win, KeyPressMask | FocusChangeMask);
143 #if defined(HAVE_XRANDR) && HAVE_XRANDR
144 X11Utils::XRRConfiguration *XRRConfig = new X11Utils::XRRConfiguration(dpy, win);
145 #endif
147 Cursor blankCursor = None;
148 if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor)
150 // make a blank cursor
151 Pixmap Blank;
152 XColor DummyColor;
153 char ZeroData[1] = {0};
154 Blank = XCreateBitmapFromData (dpy, win, ZeroData, 1, 1);
155 blankCursor = XCreatePixmapCursor(dpy, Blank, Blank, &DummyColor, &DummyColor, 0, 0);
156 XFreePixmap (dpy, Blank);
157 XDefineCursor(dpy, win, blankCursor);
160 if (fullscreen)
162 X11Utils::EWMH_Fullscreen(dpy, _NET_WM_STATE_TOGGLE);
163 #if defined(HAVE_XRANDR) && HAVE_XRANDR
164 XRRConfig->ToggleDisplayMode(True);
165 #endif
168 // The actual loop
169 while (running)
171 XEvent event;
172 KeySym key;
173 for (int num_events = XPending(dpy); num_events > 0; num_events--)
175 XNextEvent(dpy, &event);
176 switch(event.type)
178 case KeyPress:
179 key = XLookupKeysym((XKeyEvent*)&event, 0);
180 if (key == XK_Escape)
182 if (Core::GetState() == Core::CORE_RUN)
184 if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor)
185 XUndefineCursor(dpy, win);
186 Core::SetState(Core::CORE_PAUSE);
188 else
190 if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor)
191 XDefineCursor(dpy, win, blankCursor);
192 Core::SetState(Core::CORE_RUN);
195 else if ((key == XK_Return) && (event.xkey.state & Mod1Mask))
197 fullscreen = !fullscreen;
198 X11Utils::EWMH_Fullscreen(dpy, _NET_WM_STATE_TOGGLE);
199 #if defined(HAVE_XRANDR) && HAVE_XRANDR
200 XRRConfig->ToggleDisplayMode(fullscreen);
201 #endif
203 else if (key >= XK_F1 && key <= XK_F8)
205 int slot_number = key - XK_F1 + 1;
206 if (event.xkey.state & ShiftMask)
207 State_Save(slot_number);
208 else
209 State_Load(slot_number);
211 else if (key == XK_F9)
212 Core::ScreenShot();
213 else if (key == XK_F11)
214 State_LoadLastSaved();
215 else if (key == XK_F12)
217 if (event.xkey.state & ShiftMask)
218 State_UndoLoadState();
219 else
220 State_UndoSaveState();
222 break;
223 case FocusIn:
224 rendererHasFocus = true;
225 if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor &&
226 Core::GetState() != Core::CORE_PAUSE)
227 XDefineCursor(dpy, win, blankCursor);
228 break;
229 case FocusOut:
230 rendererHasFocus = false;
231 if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor)
232 XUndefineCursor(dpy, win);
233 break;
236 if (!fullscreen)
238 Window winDummy;
239 unsigned int borderDummy, depthDummy;
240 XGetGeometry(dpy, win, &winDummy,
241 &SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowXPos,
242 &SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowYPos,
243 (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowWidth,
244 (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter.iRenderWindowHeight,
245 &borderDummy, &depthDummy);
247 usleep(100000);
250 #if defined(HAVE_XRANDR) && HAVE_XRANDR
251 delete XRRConfig;
252 #endif
253 if (SConfig::GetInstance().m_LocalCoreStartupParameter.bHideCursor)
254 XFreeCursor(dpy, blankCursor);
255 XCloseDisplay(dpy);
256 Core::Stop();
258 #endif
260 //for cocoa we need to hijack the main to get event
261 #ifdef __APPLE__
263 @interface CocoaThread : NSObject
265 NSThread *Thread;
267 - (void)cocoaThreadStart;
268 - (void)cocoaThreadRun:(id)sender;
269 - (void)cocoaThreadQuit:(NSNotification*)note;
270 - (bool)cocoaThreadRunning;
271 @end
273 static NSString *CocoaThreadHaveFinish = @"CocoaThreadHaveFinish";
275 int cocoaArgc;
276 char **cocoaArgv;
277 int appleMain(int argc, char *argv[]);
279 @implementation CocoaThread
281 - (void)cocoaThreadStart
284 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cocoaThreadQuit:) name:CocoaThreadHaveFinish object:nil];
285 [NSThread detachNewThreadSelector:@selector(cocoaThreadRun:) toTarget:self withObject:nil];
289 - (void)cocoaThreadRun:(id)sender
292 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
293 Thread = [NSThread currentThread];
294 //launch main
295 appleMain(cocoaArgc,cocoaArgv);
297 [[NSNotificationCenter defaultCenter] postNotificationName:CocoaThreadHaveFinish object:nil];
299 [pool release];
303 - (void)cocoaThreadQuit:(NSNotification*)note
306 [[NSNotificationCenter defaultCenter] removeObserver:self];
310 - (bool)cocoaThreadRunning
312 if([Thread isFinished])
313 return false;
314 else
315 return true;
318 @end
321 int main(int argc, char *argv[])
323 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
325 cocoaArgc = argc;
326 cocoaArgv = argv;
328 cocoaCreateApp();
330 CocoaThread *thread = [[CocoaThread alloc] init];
331 NSEvent *event = [[NSEvent alloc] init];
333 [thread cocoaThreadStart];
335 //cocoa event loop
336 while(1)
338 event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
339 if(cocoaSendEvent(event))
341 Core::Stop();
342 break;
344 if(![thread cocoaThreadRunning])
345 break;
349 [event release];
350 [thread release];
351 [pool release];
355 int appleMain(int argc, char *argv[])
356 #else
357 int main(int argc, char* argv[])
358 #endif
360 gengetopt_args_info args_info;
362 if (cmdline_parser(argc, argv, &args_info) != 0)
363 return(1);
365 if (args_info.inputs_num < 1)
367 fprintf(stderr, "Please supply at least one argument - the ISO to boot.\n");
368 return(1);
370 std::string bootFile(args_info.inputs[0]);
372 updateMainFrameEvent.Init();
374 LogManager::Init();
375 SConfig::Init();
376 CPluginManager::Init();
378 CPluginManager::GetInstance().ScanForPlugins();
380 #if defined HAVE_X11 && HAVE_X11
381 XInitThreads();
382 #endif
384 if (BootManager::BootCore(bootFile)) //no use running the loop when booting fails
386 #if defined(HAVE_X11) && HAVE_X11
387 X11_MainLoop();
388 #else
389 while (PowerPC::GetState() != PowerPC::CPU_POWERDOWN)
390 updateMainFrameEvent.Wait();
391 #endif
393 updateMainFrameEvent.Shutdown();
395 WiimoteReal::Shutdown();
396 CPluginManager::Shutdown();
397 SConfig::Shutdown();
398 LogManager::Shutdown();
400 cmdline_parser_free (&args_info);
401 return(0);