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/
23 #include <sys/param.h>
32 #include <sys/param.h>
35 #if defined HAVE_X11 && HAVE_X11
36 #include <X11/keysym.h>
41 #if defined(HAVE_COCOA) && HAVE_COCOA
49 #include "CPUDetect.h"
52 #include "PowerPC/PowerPC.h"
54 #include "PluginManager.h"
55 #include "ConfigManager.h"
56 #include "LogManager.h"
57 #include "BootManager.h"
59 #if defined HAVE_X11 && HAVE_X11
61 bool rendererHasFocus
= true;
64 void Host_NotifyMapLoaded(){}
66 void Host_ShowJitResults(unsigned int address
){}
68 Common::Event updateMainFrameEvent
;
69 void Host_Message(int Id
)
71 #if defined(HAVE_X11) && HAVE_X11
81 void Host_UpdateTitle(const char* title
){};
83 void Host_UpdateLogDisplay(){}
86 void Host_UpdateDisasmDialog(){}
89 void Host_UpdateMainFrame()
91 updateMainFrameEvent
.Set();
94 void Host_UpdateBreakPointView(){}
97 void Host_UpdateMemoryView(){}
100 void Host_SetDebugMode(bool){}
102 void Host_RequestWindowSize(int& x
, int& y
, int& width
, int& height
)
104 x
= SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowXPos
;
105 y
= SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowYPos
;
106 width
= SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowWidth
;
107 height
= SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowHeight
;
110 bool Host_RendererHasFocus()
112 return rendererHasFocus
;
115 void Host_SetWaitCursor(bool enable
){}
118 void Host_UpdateStatusBar(const char* _pText
, int Filed
){}
120 void Host_SysMessage(const char *fmt
, ...)
126 vsprintf(msg
, fmt
, list
);
129 size_t len
= strlen(msg
);
130 if (msg
[len
- 1] != '\n') {
134 fprintf(stderr
, "%s", msg
);
137 void Host_SetWiiMoteConnectionState(int _State
) {}
139 #if defined(HAVE_X11) && HAVE_X11
142 bool fullscreen
= SConfig::GetInstance().m_LocalCoreStartupParameter
.bFullscreen
;
143 while (Core::GetState() == Core::CORE_UNINITIALIZED
)
144 updateMainFrameEvent
.Wait();
146 Display
*dpy
= XOpenDisplay(0);
147 Window win
= *(Window
*)Core::GetXWindow();
148 XSelectInput(dpy
, win
, KeyPressMask
| KeyReleaseMask
| FocusChangeMask
);
150 #if defined(HAVE_XRANDR) && HAVE_XRANDR
151 X11Utils::XRRConfiguration
*XRRConfig
= new X11Utils::XRRConfiguration(dpy
, win
);
154 Cursor blankCursor
= NULL
;
155 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
157 // make a blank cursor
160 char ZeroData
[1] = {0};
161 Blank
= XCreateBitmapFromData (dpy
, win
, ZeroData
, 1, 1);
162 blankCursor
= XCreatePixmapCursor(dpy
, Blank
, Blank
, &DummyColor
, &DummyColor
, 0, 0);
163 XFreePixmap (dpy
, Blank
);
164 XDefineCursor(dpy
, win
, blankCursor
);
169 X11Utils::EWMH_Fullscreen(_NET_WM_STATE_TOGGLE
);
170 #if defined(HAVE_XRANDR) && HAVE_XRANDR
171 XRRConfig
->ToggleDisplayMode(True
);
180 for (int num_events
= XPending(dpy
); num_events
> 0; num_events
--)
182 XNextEvent(dpy
, &event
);
186 key
= XLookupKeysym((XKeyEvent
*)&event
, 0);
187 if (key
== XK_Escape
)
189 if (Core::GetState() == Core::CORE_RUN
)
191 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
192 XUndefineCursor(dpy
, win
);
193 Core::SetState(Core::CORE_PAUSE
);
197 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
198 XDefineCursor(dpy
, win
, blankCursor
);
199 Core::SetState(Core::CORE_RUN
);
202 else if ((key
== XK_Return
) && (event
.xkey
.state
& Mod1Mask
))
204 fullscreen
= !fullscreen
;
205 X11Utils::EWMH_Fullscreen(_NET_WM_STATE_TOGGLE
);
206 #if defined(HAVE_XRANDR) && HAVE_XRANDR
207 XRRConfig
->ToggleDisplayMode(fullscreen
);
210 else if (key
>= XK_F1
&& key
<= XK_F8
)
212 int slot_number
= key
- XK_F1
+ 1;
213 if (event
.xkey
.state
& ShiftMask
)
214 State_Save(slot_number
);
216 State_Load(slot_number
);
218 else if (key
== XK_F9
)
220 else if (key
== XK_F11
)
221 State_LoadLastSaved();
222 else if (key
== XK_F12
)
224 if (event
.xkey
.state
& ShiftMask
)
225 State_UndoLoadState();
227 State_UndoSaveState();
231 rendererHasFocus
= true;
232 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
&&
233 Core::GetState() != Core::CORE_PAUSE
)
234 XDefineCursor(dpy
, win
, blankCursor
);
237 rendererHasFocus
= false;
238 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
239 XUndefineCursor(dpy
, win
);
246 unsigned int borderDummy
, depthDummy
;
247 XGetGeometry(dpy
, win
, &winDummy
,
248 &SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowXPos
,
249 &SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowYPos
,
250 (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowWidth
,
251 (unsigned int *)&SConfig::GetInstance().m_LocalCoreStartupParameter
.iRenderWindowHeight
,
252 &borderDummy
, &depthDummy
);
257 #if defined(HAVE_XRANDR) && HAVE_XRANDR
260 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
261 XFreeCursor(dpy
, blankCursor
);
267 //for cocoa we need to hijack the main to get event
268 #if defined(HAVE_COCOA) && HAVE_COCOA
270 @interface CocoaThread
: NSObject
274 - (void)cocoaThreadStart
;
275 - (void)cocoaThreadRun
:(id
)sender
;
276 - (void)cocoaThreadQuit
:(NSNotification
*)note
;
277 - (bool)cocoaThreadRunning
;
280 static NSString
*CocoaThreadHaveFinish
= @
"CocoaThreadHaveFinish";
284 int appleMain(int argc
, char *argv
[]);
286 @implementation CocoaThread
288 - (void)cocoaThreadStart
291 [[NSNotificationCenter defaultCenter
] addObserver
:self selector
:@
selector(cocoaThreadQuit
:) name
:CocoaThreadHaveFinish object
:nil
];
292 [NSThread detachNewThreadSelector
:@
selector(cocoaThreadRun
:) toTarget
:self withObject
:nil
];
296 - (void)cocoaThreadRun
:(id
)sender
299 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
300 Thread
= [NSThread currentThread
];
302 appleMain(cocoaArgc
,cocoaArgv
);
304 [[NSNotificationCenter defaultCenter
] postNotificationName
:CocoaThreadHaveFinish object
:nil
];
310 - (void)cocoaThreadQuit
:(NSNotification
*)note
313 [[NSNotificationCenter defaultCenter
] removeObserver
:self
];
317 - (bool)cocoaThreadRunning
319 if([Thread isFinished
])
328 int main(int argc
, char *argv
[])
336 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
338 CocoaThread
*thread
= [[CocoaThread alloc
] init
];
339 NSEvent
*event
= [[NSEvent alloc
] init
];
341 [thread cocoaThreadStart
];
346 event
= [NSApp nextEventMatchingMask
:NSAnyEventMask untilDate
:[NSDate distantPast
] inMode
:NSDefaultRunLoopMode dequeue
:YES
];
347 if(cocoaSendEvent(event
))
352 if(![thread cocoaThreadRunning
])
363 int appleMain(int argc
, char *argv
[])
365 // Include SDL header so it can hijack main().
366 #if defined(USE_SDL) && USE_SDL
369 int main(int argc
, char* argv
[])
372 gengetopt_args_info args_info
;
374 if (cmdline_parser(argc
, argv
, &args_info
) != 0)
377 if (args_info
.inputs_num
< 1)
379 fprintf(stderr
, "Please supply at least one argument - the ISO to boot.\n");
382 std::string
bootFile(args_info
.inputs
[0]);
384 updateMainFrameEvent
.Init();
387 EventHandler::Init();
389 CPluginManager::Init();
391 CPluginManager::GetInstance().ScanForPlugins();
393 #if defined HAVE_X11 && HAVE_X11
397 if (BootManager::BootCore(bootFile
)) //no use running the loop when booting fails
399 #if defined(HAVE_X11) && HAVE_X11
402 while (PowerPC::GetState() != PowerPC::CPU_POWERDOWN
)
403 updateMainFrameEvent
.Wait();
406 updateMainFrameEvent
.Shutdown();
408 CPluginManager::Shutdown();
410 EventHandler::Shutdown();
411 LogManager::Shutdown();
413 cmdline_parser_free (&args_info
);