Skip to content

Commit

Permalink
Rework hotkey logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bozbez committed Sep 20, 2021
1 parent be7907f commit 06309ff
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions src/audio-capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
return open_process_proc(desired_access, inherit_handle, process_id);
}

static inline bool process_is_alive(DWORD pid)
{
HANDLE process = open_process(PROCESS_QUERY_INFORMATION | SYNCHRONIZE,
false, pid);
if (process == NULL)
return false;

DWORD code;
bool success = GetExitCodeProcess(process, &code);

safe_close_handle(&process);
return success && code == STILL_ACTIVE;
}

static void destroy_data(audio_capture_context_t *ctx)
{
if (ctx->data != NULL) {
Expand Down Expand Up @@ -265,11 +279,19 @@ static void audio_capture_worker_update(audio_capture_context_t *ctx)
ctx->exclude_process_tree = ctx->config.exclude_process_tree;

if (ctx->config.mode == MODE_HOTKEY) {
if (ctx->config.hotkey_window != NULL) {
ctx->window_selected = true;
GetWindowThreadProcessId(ctx->config.hotkey_window,
&ctx->next_process_id);
} else {
if (ctx->config.hotkey_window == NULL) {
ctx->window_selected = false;
ctx->next_process_id = 0;
goto exit;
}

ctx->window_selected = true;
GetWindowThreadProcessId(ctx->config.hotkey_window,
&ctx->next_process_id);

if (!process_is_alive(ctx->next_process_id)) {
ctx->config.hotkey_window = NULL;

ctx->window_selected = false;
ctx->next_process_id = 0;
}
Expand Down Expand Up @@ -316,7 +338,7 @@ static void audio_capture_worker_forward(audio_capture_context_t *ctx)

for (int packet = 0; packet < ctx->data->num_packets; ++packet) {
struct obs_source_audio audio = {
.data[0] = (uint8_t*)ctx->data->data[packet],
.data[0] = (uint8_t *)ctx->data->data[packet],
.frames = ctx->data->frames[packet],

.speakers = ctx->data->speakers,
Expand Down Expand Up @@ -521,8 +543,11 @@ static bool hotkey_start(void *data, obs_hotkey_pair_id id,
EnterCriticalSection(&ctx->config_section);
if (pressed && ctx->config.mode == MODE_HOTKEY) {
debug("activate hotkey pressed");
ctx->config.hotkey_window = GetForegroundWindow();
needs_update = true;
HWND new_window = GetForegroundWindow();
if (ctx->config.hotkey_window != new_window) {
ctx->config.hotkey_window = new_window;
needs_update = true;
}
}
LeaveCriticalSection(&ctx->config_section);

Expand All @@ -545,6 +570,7 @@ static bool hotkey_stop(void *data, obs_hotkey_pair_id id, obs_hotkey_t *hotkey,
if (pressed && ctx->config.mode == MODE_HOTKEY) {
debug("deactivate hotkey pressed");
ctx->config.hotkey_window = NULL;
needs_update = true;
}
LeaveCriticalSection(&ctx->config_section);

Expand Down

0 comments on commit 06309ff

Please sign in to comment.