From 7285f9e9ade4bafe7d5c032b833ebc4dfbb21976 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 2 Mar 2019 20:31:33 +1100 Subject: [PATCH] [c-host] add app_quit for clean shutdown support --- c-host/app.c | 9 +++++++-- c-host/app.h | 3 ++- c-host/linux/platform.c | 12 ++++++++++-- c-host/windows/platform.c | 7 +++---- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/c-host/app.c b/c-host/app.c index b7ee6365..5124938a 100644 --- a/c-host/app.c +++ b/c-host/app.c @@ -127,7 +127,7 @@ static bool captureStart(struct CaptureInterface * iface) return startThreads(); } -int app_main(bool * termSignal) +int app_main() { unsigned int shmemSize = os_shmemSize(); uint8_t * shmemMap = NULL; @@ -191,7 +191,7 @@ int app_main(bool * termSignal) goto exit; } - while(!*termSignal) + while(app.running) { bool hasFrameUpdate = false; bool hasPointerUpdate = false; @@ -240,4 +240,9 @@ exit: fail: os_shmemUnmap(); return exitcode; +} + +void app_quit() +{ + app.running = false; } \ No newline at end of file diff --git a/c-host/app.h b/c-host/app.h index 08c8a3ca..1c4c894c 100644 --- a/c-host/app.h +++ b/c-host/app.h @@ -21,7 +21,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include -int app_main(bool * termSignal); +int app_main(); +void app_quit(); // these must be implemented for each OS diff --git a/c-host/linux/platform.c b/c-host/linux/platform.c index 24d1ef97..38608b31 100644 --- a/c-host/linux/platform.c +++ b/c-host/linux/platform.c @@ -28,6 +28,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include #include +#include struct app { @@ -57,6 +58,12 @@ struct osThreadHandle int resultCode; }; +void sigHandler(int signo) +{ + DEBUG_INFO("SIGINT"); + app_quit(); +} + int main(int argc, char * argv[]) { static struct option longOptions[] = @@ -160,8 +167,9 @@ int main(int argc, char * argv[]) DEBUG_INFO("KVMFR Device : %s", file); } - bool termSig = false; - int result = app_main(&termSig); + signal(SIGINT, sigHandler); + + int result = app_main(); os_shmemUnmap(); close(app.shmFD); diff --git a/c-host/windows/platform.c b/c-host/windows/platform.c index b4e1b28c..f233f764 100644 --- a/c-host/windows/platform.c +++ b/c-host/windows/platform.c @@ -28,7 +28,6 @@ Place, Suite 330, Boston, MA 02111-1307 USA static HANDLE shmemHandle = INVALID_HANDLE_VALUE; static bool shmemOwned = false; static IVSHMEM_MMAP shmemMap = {0}; -static bool termSignal = false; static HWND messageWnd; struct osThreadHandle @@ -61,7 +60,7 @@ LRESULT CALLBACK DummyWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) static int appThread(void * opaque) { - int result = app_main(&termSignal); + int result = app_main(); SendMessage(messageWnd, WM_CLOSE, 0, 0); return result; } @@ -162,7 +161,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine goto finish_shmem; } - while(!termSignal) + while(true) { MSG msg; BOOL bRet = GetMessage(&msg, NULL, 0, 0); @@ -183,7 +182,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } shutdown: - termSignal = true; + app_quit(); if (!os_joinThread(thread, &result)) { DEBUG_ERROR("Failed to join the main application thread");