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>
29 #if defined HAVE_X11 && HAVE_X11
30 #include <X11/keysym.h>
41 #include "CPUDetect.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
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
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
, ...)
119 vsprintf(msg
, fmt
, list
);
122 size_t len
= strlen(msg
);
123 if (msg
[len
- 1] != '\n') {
127 fprintf(stderr
, "%s", msg
);
130 void Host_SetWiiMoteConnectionState(int _State
) {}
132 #if defined(HAVE_X11) && HAVE_X11
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
);
147 Cursor blankCursor
= None
;
148 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
150 // make a blank cursor
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
);
162 X11Utils::EWMH_Fullscreen(dpy
, _NET_WM_STATE_TOGGLE
);
163 #if defined(HAVE_XRANDR) && HAVE_XRANDR
164 XRRConfig
->ToggleDisplayMode(True
);
173 for (int num_events
= XPending(dpy
); num_events
> 0; num_events
--)
175 XNextEvent(dpy
, &event
);
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
);
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
);
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
);
209 State_Load(slot_number
);
211 else if (key
== XK_F9
)
213 else if (key
== XK_F11
)
214 State_LoadLastSaved();
215 else if (key
== XK_F12
)
217 if (event
.xkey
.state
& ShiftMask
)
218 State_UndoLoadState();
220 State_UndoSaveState();
224 rendererHasFocus
= true;
225 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
&&
226 Core::GetState() != Core::CORE_PAUSE
)
227 XDefineCursor(dpy
, win
, blankCursor
);
230 rendererHasFocus
= false;
231 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
232 XUndefineCursor(dpy
, win
);
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
);
250 #if defined(HAVE_XRANDR) && HAVE_XRANDR
253 if (SConfig::GetInstance().m_LocalCoreStartupParameter
.bHideCursor
)
254 XFreeCursor(dpy
, blankCursor
);
260 //for cocoa we need to hijack the main to get event
263 @interface CocoaThread
: NSObject
267 - (void)cocoaThreadStart
;
268 - (void)cocoaThreadRun
:(id
)sender
;
269 - (void)cocoaThreadQuit
:(NSNotification
*)note
;
270 - (bool)cocoaThreadRunning
;
273 static NSString
*CocoaThreadHaveFinish
= @
"CocoaThreadHaveFinish";
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
];
295 appleMain(cocoaArgc
,cocoaArgv
);
297 [[NSNotificationCenter defaultCenter
] postNotificationName
:CocoaThreadHaveFinish object
:nil
];
303 - (void)cocoaThreadQuit
:(NSNotification
*)note
306 [[NSNotificationCenter defaultCenter
] removeObserver
:self
];
310 - (bool)cocoaThreadRunning
312 if([Thread isFinished
])
321 int main(int argc
, char *argv
[])
323 NSAutoreleasePool
*pool
= [[NSAutoreleasePool alloc
] init
];
330 CocoaThread
*thread
= [[CocoaThread alloc
] init
];
331 NSEvent
*event
= [[NSEvent alloc
] init
];
333 [thread cocoaThreadStart
];
338 event
= [NSApp nextEventMatchingMask
:NSAnyEventMask untilDate
:[NSDate distantPast
] inMode
:NSDefaultRunLoopMode dequeue
:YES
];
339 if(cocoaSendEvent(event
))
344 if(![thread cocoaThreadRunning
])
355 int appleMain(int argc
, char *argv
[])
357 int main(int argc
, char* argv
[])
360 gengetopt_args_info args_info
;
362 if (cmdline_parser(argc
, argv
, &args_info
) != 0)
365 if (args_info
.inputs_num
< 1)
367 fprintf(stderr
, "Please supply at least one argument - the ISO to boot.\n");
370 std::string
bootFile(args_info
.inputs
[0]);
372 updateMainFrameEvent
.Init();
376 CPluginManager::Init();
378 CPluginManager::GetInstance().ScanForPlugins();
380 #if defined HAVE_X11 && HAVE_X11
384 if (BootManager::BootCore(bootFile
)) //no use running the loop when booting fails
386 #if defined(HAVE_X11) && HAVE_X11
389 while (PowerPC::GetState() != PowerPC::CPU_POWERDOWN
)
390 updateMainFrameEvent
.Wait();
393 updateMainFrameEvent
.Shutdown();
395 WiimoteReal::Shutdown();
396 CPluginManager::Shutdown();
398 LogManager::Shutdown();
400 cmdline_parser_free (&args_info
);