Skip to content

Commit 3703137

Browse files
committed
fixed CTRL-C
1 parent 2db44d1 commit 3703137

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

platforms/config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SDL_SHA=f6864924f76e1a0b4abaefc76ae2ed22b1a8916e
66
SDL_IMAGE_SHA=11154afb7855293159588b245b446a4ef09e574f
77
PINMAME_SHA=a41dd2db4691c0fd4e812416ac403d434b29c1a4
88
LIBPPUC_SHA=0df00b506af9afb5e190056cdd2b294cd356e749
9-
LIBDMDUTIL_SHA=41eff53ce8e00cd4285bd403065a3e19f062be76
9+
LIBDMDUTIL_SHA=706cdf37fdc738ddb3edafc30da6dc8d1afdcfa0
1010

1111

1212
if [ -z "${BUILD_TYPE}" ]; then

src/backbox.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ SDL_AudioStream* m_pstream = nullptr;
2121
SDL_AudioSpec audioSpec;
2222

2323
DMDUtil::DMD* pDmd;
24+
DMDUtil::DMDServer* pServer;
2425

2526
SDL_Window* pTransliteWindow;
2627
SDL_Renderer* pTransliteRenderer;
2728
SDL_Texture* pTransliteTexture;
2829
SDL_Texture* pTransliteAttractTexture;
2930
SDL_Window* pVirtualDMDWindow;
3031
SDL_Renderer* pVirtualDMDRenderer;
32+
SDL_Event quitEvent;
3133

3234
bool opt_debug = false;
3335
bool opt_no_sound = false;
@@ -136,11 +138,30 @@ void DMDUTILCALLBACK LogCallback(DMDUtil_LogLevel logLevel, const char* format,
136138
fflush(output);
137139
}
138140

139-
void signal_handler(int sig) { running = false; }
141+
void signal_handler(int sig)
142+
{
143+
printf("\nReceived signal %d, shutting down...\n", sig);
144+
running = false;
145+
quitEvent.type = SDL_EVENT_QUIT;
146+
SDL_PushEvent(&quitEvent);
147+
148+
// CTRL-C should terminate the process hard after 2 seconds if normal shutdown fails.
149+
if (sig == SIGINT)
150+
{
151+
std::thread(
152+
[]
153+
{
154+
std::this_thread::sleep_for(std::chrono::seconds(2));
155+
exit(0);
156+
})
157+
.detach();
158+
}
159+
}
140160

141161
int main(int argc, char* argv[])
142162
{
143163
signal(SIGHUP, signal_handler);
164+
signal(SIGINT, signal_handler);
144165
signal(SIGKILL, signal_handler);
145166
signal(SIGTERM, signal_handler);
146167
signal(SIGQUIT, signal_handler);
@@ -362,30 +383,35 @@ int main(int argc, char* argv[])
362383
pDmd->FindDisplays();
363384
while (pDmd->IsFinding()) std::this_thread::sleep_for(std::chrono::milliseconds(100));
364385

365-
DMDUtil::DMDServer server(pDmd);
386+
pServer = new DMDUtil::DMDServer(pDmd);
366387

367-
if (!server.Start(dmdConfig->GetDMDServerAddr(), dmdConfig->GetDMDServerPort()))
388+
if (!pServer->Start(dmdConfig->GetDMDServerAddr(), dmdConfig->GetDMDServerPort()))
368389
{
369390
return 1;
370391
}
371392

372393
while (running)
373394
{
374395
SDL_Event event;
375-
if (SDL_PollEvent(&event))
396+
397+
// Use SDL_WaitEventTimeout instead of SDL_PollEvent to reduce CPU usage
398+
// and ensure we can still process signals
399+
if (SDL_WaitEventTimeout(&event, 100)) // Wait up to 100ms for an event
376400
{
377401
switch (event.type)
378402
{
379403
case SDL_EVENT_QUIT:
404+
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
380405
running = false;
381406
break;
382407
}
383408
}
384409

385-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
410+
SDL_Delay(10);
386411
}
387412

388-
server.Stop();
413+
pServer->Stop();
414+
delete pServer;
389415

390416
std::unique_lock<std::mutex> lock(threadMutex);
391417
currentThreadId = 0;

0 commit comments

Comments
 (0)