@@ -13,10 +13,23 @@ static int lua_callback = LUA_NOREF;
1313static bool graphicsOpen = false ;
1414static bool shouldGraphicsClose = false ;
1515static const CTRPF::Screen *currentScreen = NULL ;
16+ static Core::GraphicsFrameCallback graphicsFrameCallback = NULL ;
17+ static Core::GraphicsExitCallback graphicsExitCallback = NULL ;
1618
17- void Core::GraphicsHandlerCallback () {
19+ static void processEventHandler (CTRPF::Process::Event event) {
20+ if (event == CTRPF::Process::Event::SLEEP_ENTER)
21+ shouldGraphicsClose = true ;
22+ }
23+
24+ void Core::GraphicsOpen (GraphicsFrameCallback frameCallback, GraphicsExitCallback exitCallback) {
25+ graphicsFrameCallback = frameCallback;
26+ graphicsExitCallback = exitCallback;
27+ gmenu->Callback (Core::GraphicsHandlerMainloop); // Better add it as Callback to avoid script exahustion
28+ }
29+
30+ void Core::GraphicsHandlerMainloop () {
1831 CTRPF::Process::Pause ();
19- lua_State *L = Lua_global ;
32+ CTRPF::Process::SetProcessEventCallback (processEventHandler) ;
2033
2134 graphicsOpen = true ;
2235 bool exit = false ;
@@ -29,33 +42,57 @@ void Core::GraphicsHandlerCallback() {
2942 if (CTRPF::OSD::TryLock ())
3043 continue ;
3144
32- const CTRPF::Screen& topScreen = CTRPF::OSD::GetTopScreen ();
33- currentScreen = &topScreen;
34- lua_rawgeti (L, LUA_REGISTRYINDEX, lua_callback);
35- lua_pushstring (L, " top" );
36- if (lua_pcall (L, 1 , 0 , 0 ))
37- lua_pop (L, 1 );
38-
39- const CTRPF::Screen& bottomScreen = CTRPF::OSD::GetBottomScreen ();
40- currentScreen = &bottomScreen;
41- lua_rawgeti (L, LUA_REGISTRYINDEX, lua_callback);
42- lua_pushstring (L, " bottom" );
43- if (lua_pcall (L, 1 , 0 , 0 ))
44- lua_pop (L, 1 );
45+ if (graphicsFrameCallback == NULL ) {
46+ exit = true ;
47+ continue ;
48+ }
49+
50+ graphicsFrameCallback ();
4551
4652 CTRPF::OSD::SwapBuffers ();
4753 CTRPF::OSD::Unlock ();
4854
49- exit = CTRPF::Controller::IsKeyReleased (CTRPF::Key::Select);
55+ if (CTRPF::Controller::IsKeyReleased (CTRPF::Key::Select)) {
56+ exit = true ;
57+ gmenu->ForceOpen ();
58+ }
5059 }
51- luaL_unref (L, LUA_REGISTRYINDEX, lua_callback);
52- lua_callback = LUA_NOREF;
53- *gmenu -= Core::GraphicsHandlerCallback;
60+
61+ // Exit Graphics Mainloop
62+ if (graphicsExitCallback != NULL )
63+ graphicsExitCallback ();
64+ CTRPF::Process::SetProcessEventCallback (nullptr );
65+ *gmenu -= Core::GraphicsHandlerMainloop;
66+ graphicsFrameCallback = NULL ;
67+ graphicsExitCallback = NULL ;
5468 graphicsOpen = false ;
5569 shouldGraphicsClose = false ;
5670 CTRPF::Process::Play ();
5771}
5872
73+ static void LuaGraphicsFrameCallback () {
74+ lua_State *L = Lua_global;
75+ const CTRPF::Screen& topScreen = CTRPF::OSD::GetTopScreen ();
76+ currentScreen = &topScreen;
77+ lua_rawgeti (L, LUA_REGISTRYINDEX, lua_callback);
78+ lua_pushstring (L, " top" );
79+ if (lua_pcall (L, 1 , 0 , 0 ))
80+ lua_pop (L, 1 );
81+
82+ const CTRPF::Screen& bottomScreen = CTRPF::OSD::GetBottomScreen ();
83+ currentScreen = &bottomScreen;
84+ lua_rawgeti (L, LUA_REGISTRYINDEX, lua_callback);
85+ lua_pushstring (L, " bottom" );
86+ if (lua_pcall (L, 1 , 0 , 0 ))
87+ lua_pop (L, 1 );
88+ }
89+
90+ static void LuaGraphicsExitCallback () {
91+ lua_State *L = Lua_global;
92+ luaL_unref (L, LUA_REGISTRYINDEX, lua_callback);
93+ lua_callback = LUA_NOREF;
94+ }
95+
5996// ----------------------------------------------------------------------------
6097
6198// $Core.Graphics
@@ -74,10 +111,15 @@ static int l_Graphics_open(lua_State *L) {
74111
75112 if (graphicsOpen)
76113 return 0 ;
114+
115+ if (lua_callback != LUA_NOREF) {
116+ luaL_unref (L, LUA_REGISTRYINDEX, lua_callback);
117+ lua_callback = LUA_NOREF;
118+ }
77119
78120 lua_pushvalue (L, 1 );
79121 lua_callback = luaL_ref (L, LUA_REGISTRYINDEX);
80- gmenu-> Callback ( Core::GraphicsHandlerCallback); // Better add it as Callback to avoid script exahustion
122+ Core::GraphicsOpen (LuaGraphicsFrameCallback, LuaGraphicsExitCallback);
81123 return 0 ;
82124}
83125
0 commit comments