[client] cleanup and re-order startup/shutdown code

This commit is contained in:
Geoffrey McRae 2019-10-26 12:03:10 +11:00
parent 7d6e061ade
commit 0e7e918e2c
2 changed files with 132 additions and 130 deletions

View file

@ -1 +1 @@
B1-15-g66891aa536+1 B1-16-g7d6e061ade+1

View file

@ -48,6 +48,10 @@ static int cursorThread(void * unused);
static int renderThread(void * unused); static int renderThread(void * unused);
static int frameThread (void * unused); static int frameThread (void * unused);
static SDL_Thread *t_spice = NULL;
static SDL_Thread *t_render = NULL;
static SDL_Cursor *cursor = NULL;
struct AppState state; struct AppState state;
// this structure is initialized in config.c // this structure is initialized in config.c
@ -589,7 +593,10 @@ int eventFilter(void * userdata, SDL_Event * event)
case SDL_QUIT: case SDL_QUIT:
{ {
if (!params.ignoreQuit) if (!params.ignoreQuit)
{
DEBUG_INFO("Quit event received, exiting...");
state.running = false; state.running = false;
}
return 0; return 0;
} }
@ -998,11 +1005,8 @@ static void release_key_binds()
app_release_keybind(&state.kbCtrlAltFn[i]); app_release_keybind(&state.kbCtrlAltFn[i]);
} }
int run() static int lg_run()
{ {
DEBUG_INFO("Looking Glass (" BUILD_VERSION ")");
DEBUG_INFO("Locking Method: " LG_LOCK_MODE);
memset(&state, 0, sizeof(state)); memset(&state, 0, sizeof(state));
state.running = true; state.running = true;
state.scaleX = 1.0f; state.scaleX = 1.0f;
@ -1051,6 +1055,45 @@ int run()
signal(SIGINT , int_handler); signal(SIGINT , int_handler);
signal(SIGTERM, int_handler); signal(SIGTERM, int_handler);
// try map the shared memory
state.shm = (struct KVMFRHeader *)map_memory();
if (!state.shm)
{
DEBUG_ERROR("Failed to map memory");
return -1;
}
// try to connect to the spice server
if (params.useSpiceInput || params.useSpiceClipboard)
{
spice_set_clipboard_cb(
spiceClipboardNotice,
spiceClipboardData,
spiceClipboardRelease,
spiceClipboardRequest);
if (!spice_connect(params.spiceHost, params.spicePort, ""))
{
DEBUG_ERROR("Failed to connect to spice server");
return -1;
}
while(state.running && !spice_ready())
if (!spice_process())
{
state.running = false;
DEBUG_ERROR("Failed to process spice messages");
return -1;
}
if (!(t_spice = SDL_CreateThread(spiceThread, "spiceThread", NULL)))
{
DEBUG_ERROR("spice create thread failed");
return -1;
}
}
// select and init a renderer
LG_RendererParams lgrParams; LG_RendererParams lgrParams;
lgrParams.showFPS = params.showFPS; lgrParams.showFPS = params.showFPS;
Uint32 sdlFlags; Uint32 sdlFlags;
@ -1086,6 +1129,7 @@ int run()
return -1; return -1;
} }
// all our ducks are in a line, create the window
state.window = SDL_CreateWindow( state.window = SDL_CreateWindow(
params.windowTitle, params.windowTitle,
params.center ? SDL_WINDOWPOS_CENTERED : params.x, params.center ? SDL_WINDOWPOS_CENTERED : params.x,
@ -1179,12 +1223,6 @@ int run()
return -1; return -1;
} }
if (!state.window)
{
DEBUG_ERROR("failed to create window");
return -1;
}
if (state.lgc) if (state.lgc)
{ {
DEBUG_INFO("Using Clipboard: %s", state.lgc->getName()); DEBUG_INFO("Using Clipboard: %s", state.lgc->getName());
@ -1197,7 +1235,6 @@ int run()
state.cbRequestList = ll_new(); state.cbRequestList = ll_new();
} }
SDL_Cursor *cursor = NULL;
if (params.hideMouse) if (params.hideMouse)
{ {
// work around SDL_ShowCursor being non functional // work around SDL_ShowCursor being non functional
@ -1207,52 +1244,11 @@ int run()
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
} }
SDL_Thread *t_spice = NULL;
SDL_Thread *t_render = NULL;
while(1)
{
state.shm = (struct KVMFRHeader *)map_memory();
if (!state.shm)
{
DEBUG_ERROR("Failed to map memory");
break;
}
// start the renderThread so we don't just display junk // start the renderThread so we don't just display junk
if (!(t_render = SDL_CreateThread(renderThread, "renderThread", NULL))) if (!(t_render = SDL_CreateThread(renderThread, "renderThread", NULL)))
{ {
DEBUG_ERROR("render create thread failed"); DEBUG_ERROR("render create thread failed");
break; return -1;
}
if (params.useSpiceInput || params.useSpiceClipboard)
{
spice_set_clipboard_cb(
spiceClipboardNotice,
spiceClipboardData,
spiceClipboardRelease,
spiceClipboardRequest);
if (!spice_connect(params.spiceHost, params.spicePort, ""))
{
DEBUG_ERROR("Failed to connect to spice server");
break;
}
while(state.running && !spice_ready())
if (!spice_process())
{
state.running = false;
DEBUG_ERROR("Failed to process spice messages");
break;
}
if (!(t_spice = SDL_CreateThread(spiceThread, "spiceThread", NULL)))
{
DEBUG_ERROR("spice create thread failed");
break;
}
} }
// ensure mouse acceleration is identical in server mode // ensure mouse acceleration is identical in server mode
@ -1269,7 +1265,7 @@ int run()
SDL_WaitEventTimeout(NULL, 1000); SDL_WaitEventTimeout(NULL, 1000);
if (!state.running) if (!state.running)
break; return -1;
DEBUG_INFO("Host ready, starting session"); DEBUG_INFO("Host ready, starting session");
@ -1277,20 +1273,20 @@ int run()
if (memcmp(state.shm->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC)) != 0) if (memcmp(state.shm->magic, KVMFR_HEADER_MAGIC, sizeof(KVMFR_HEADER_MAGIC)) != 0)
{ {
DEBUG_ERROR("Invalid header magic, is the host running?"); DEBUG_ERROR("Invalid header magic, is the host running?");
break; return -1;
} }
if (state.shm->version != KVMFR_HEADER_VERSION) if (state.shm->version != KVMFR_HEADER_VERSION)
{ {
DEBUG_ERROR("KVMFR version missmatch, expected %u but got %u", KVMFR_HEADER_VERSION, state.shm->version); DEBUG_ERROR("KVMFR version missmatch, expected %u but got %u", KVMFR_HEADER_VERSION, state.shm->version);
DEBUG_ERROR("This is not a bug, ensure you have the right version of looking-glass-host.exe on the guest"); DEBUG_ERROR("This is not a bug, ensure you have the right version of looking-glass-host.exe on the guest");
break; return -1;
} }
if (!(state.t_frame = SDL_CreateThread(frameThread, "frameThread", NULL))) if (!(state.t_frame = SDL_CreateThread(frameThread, "frameThread", NULL)))
{ {
DEBUG_ERROR("frame create thread failed"); DEBUG_ERROR("frame create thread failed");
break; return -1;
} }
bool *closeAlert = NULL; bool *closeAlert = NULL;
@ -1321,9 +1317,11 @@ int run()
} }
} }
break; return 0;
} }
static void lg_shutdown()
{
state.running = false; state.running = false;
if (t_render) if (t_render)
@ -1370,12 +1368,15 @@ int run()
close(state.shmFD); close(state.shmFD);
} }
release_key_binds();
SDL_Quit(); SDL_Quit();
return 0;
} }
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
DEBUG_INFO("Looking Glass (" BUILD_VERSION ")");
DEBUG_INFO("Locking Method: " LG_LOCK_MODE);
if (!installCrashHandler("/proc/self/exe")) if (!installCrashHandler("/proc/self/exe"))
DEBUG_WARN("Failed to install the crash handler"); DEBUG_WARN("Failed to install the crash handler");
@ -1391,9 +1392,10 @@ int main(int argc, char * argv[])
if (params.grabKeyboard) if (params.grabKeyboard)
SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1"); SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
const int ret = run(); const int ret = lg_run();
release_key_binds(); lg_shutdown();
config_free(); config_free();
return ret; return ret;
} }