Skip to content

Commit

Permalink
libobs, win-capture: Share window helper code
Browse files Browse the repository at this point in the history
Add "ms_" prefix as makeshift namespace.
  • Loading branch information
jpark37 authored and jp9000 committed Jul 24, 2022
1 parent 9b46795 commit 69ff026
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 182 deletions.
6 changes: 5 additions & 1 deletion libobs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ if(OS_WINDOWS)
util/threading-windows.h
util/pipe-windows.c
util/platform-windows.c
util/windows/obfuscate.c
util/windows/obfuscate.h
util/windows/win-registry.h
util/windows/win-version.h
util/windows/window-helpers.c
util/windows/window-helpers.h
util/windows/ComPtr.hpp
util/windows/CoTaskMemPtr.hpp
util/windows/HRError.hpp
Expand All @@ -300,7 +304,7 @@ if(OS_WINDOWS)
libobs PRIVATE UNICODE _UNICODE _CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_WARNINGS)

target_link_libraries(libobs PRIVATE Avrt winmm)
target_link_libraries(libobs PRIVATE Avrt Dwmapi winmm)

if(MSVC)
target_link_libraries(libobs PRIVATE OBS::w32-pthreads)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static void deobfuscate_str(char *str, uint64_t val)
}
}

void *get_obfuscated_func(HMODULE module, const char *str, uint64_t val)
void *ms_get_obfuscated_func(HMODULE module, const char *str, uint64_t val)
{
char new_name[128];
strcpy(new_name, str);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#pragma once

#include <stdint.h>
#include "../c99defs.h"
#include <Windows.h>

#ifdef __cplusplus
extern "C" {
#endif

/* this is a workaround to A/Vs going crazy whenever certain functions (such as
* OpenProcess) are used */
extern void *get_obfuscated_func(HMODULE module, const char *str, uint64_t val);
EXPORT void *ms_get_obfuscated_func(HMODULE module, const char *str,
uint64_t val);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <obs.h>
#include "window-helpers.h"

#include <util/dstr.h>
#include <util/windows/obfuscate.h>

#include <dwmapi.h>
#include <psapi.h>
#include <windows.h>
#include "window-helpers.h"
#include "obfuscate.h"

static inline void encode_dstr(struct dstr *str)
{
Expand All @@ -22,8 +21,8 @@ static inline char *decode_str(const char *src)
return str.array;
}

extern void build_window_strings(const char *str, char **class, char **title,
char **exe)
void ms_build_window_strings(const char *str, char **class, char **title,
char **exe)
{
char **strlist;

Expand All @@ -46,6 +45,58 @@ extern void build_window_strings(const char *str, char **class, char **title,
strlist_free(strlist);
}

static void insert_preserved_val(obs_property_t *p, const char *val, size_t idx)
{
char *window_class = NULL;
char *title = NULL;
char *executable = NULL;
struct dstr desc = {0};

ms_build_window_strings(val, &window_class, &title, &executable);

dstr_printf(&desc, "[%s]: %s", executable, title);
obs_property_list_insert_string(p, idx, desc.array, val);
obs_property_list_item_disable(p, idx, true);

dstr_free(&desc);
bfree(window_class);
bfree(title);
bfree(executable);
}

bool ms_check_window_property_setting(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings, const char *val,
size_t idx)
{
const char *cur_val;
bool match = false;
size_t i = 0;

cur_val = obs_data_get_string(settings, val);
if (!cur_val) {
return false;
}

for (;;) {
const char *val = obs_property_list_item_string(p, i++);
if (!val)
break;

if (strcmp(val, cur_val) == 0) {
match = true;
break;
}
}

if (cur_val && *cur_val && !match) {
insert_preserved_val(p, cur_val, idx);
return true;
}

UNUSED_PARAMETER(ppts);
return false;
}

static HMODULE kernel32(void)
{
static HMODULE kernel32_handle = NULL;
Expand All @@ -60,13 +111,13 @@ static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
typedef HANDLE(WINAPI * PFN_OpenProcess)(DWORD, BOOL, DWORD);
static PFN_OpenProcess open_process_proc = NULL;
if (!open_process_proc)
open_process_proc = (PFN_OpenProcess)get_obfuscated_func(
open_process_proc = (PFN_OpenProcess)ms_get_obfuscated_func(
kernel32(), "B}caZyah`~q", 0x2D5BEBAF6DDULL);

return open_process_proc(desired_access, inherit_handle, process_id);
}

bool get_window_exe(struct dstr *name, HWND window)
bool ms_get_window_exe(struct dstr *name, HWND window)
{
wchar_t wname[MAX_PATH];
struct dstr temp = {0};
Expand Down Expand Up @@ -103,7 +154,7 @@ bool get_window_exe(struct dstr *name, HWND window)
return true;
}

void get_window_title(struct dstr *name, HWND hwnd)
void ms_get_window_title(struct dstr *name, HWND hwnd)
{
int len;

Expand All @@ -130,7 +181,7 @@ void get_window_title(struct dstr *name, HWND hwnd)
}
}

void get_window_class(struct dstr *class, HWND hwnd)
void ms_get_window_class(struct dstr *class, HWND hwnd)
{
wchar_t temp[256];

Expand Down Expand Up @@ -192,21 +243,21 @@ static void add_window(obs_property_t *p, HWND hwnd, add_window_cb callback)
struct dstr encoded = {0};
struct dstr desc = {0};

if (!get_window_exe(&exe, hwnd))
if (!ms_get_window_exe(&exe, hwnd))
return;
if (is_microsoft_internal_window_exe(exe.array)) {
dstr_free(&exe);
return;
}

get_window_title(&title, hwnd);
ms_get_window_title(&title, hwnd);
if (dstr_cmp(&exe, "explorer.exe") == 0 && dstr_is_empty(&title)) {
dstr_free(&exe);
dstr_free(&title);
return;
}

get_window_class(&class, hwnd);
ms_get_window_class(&class, hwnd);

if (callback && !callback(title.array, class.array, exe.array)) {
dstr_free(&title);
Expand Down Expand Up @@ -268,7 +319,7 @@ static bool check_window_valid(HWND window, enum window_search_mode mode)
return true;
}

bool is_uwp_window(HWND hwnd)
bool ms_is_uwp_window(HWND hwnd)
{
wchar_t name[256];

Expand All @@ -279,7 +330,7 @@ bool is_uwp_window(HWND hwnd)
return wcscmp(name, L"ApplicationFrameWindow") == 0;
}

HWND get_uwp_actual_window(HWND parent)
HWND ms_get_uwp_actual_window(HWND parent)
{
DWORD parent_id = 0;
HWND child;
Expand Down Expand Up @@ -319,8 +370,8 @@ static HWND next_window(HWND window, enum window_search_mode mode, HWND *parent,
break;
}

if (is_uwp_window(window)) {
HWND child = get_uwp_actual_window(window);
if (ms_is_uwp_window(window)) {
HWND child = ms_get_uwp_actual_window(window);
if (child) {
*parent = window;
return child;
Expand Down Expand Up @@ -357,8 +408,8 @@ static HWND first_window(enum window_search_mode mode, HWND *parent,
}
}

if (is_uwp_window(window)) {
HWND child = get_uwp_actual_window(window);
if (ms_is_uwp_window(window)) {
HWND child = ms_get_uwp_actual_window(window);
if (child) {
*parent = window;
return child;
Expand All @@ -368,8 +419,8 @@ static HWND first_window(enum window_search_mode mode, HWND *parent,
return window;
}

void fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback)
void ms_fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback)
{
HWND parent;
bool use_findwindowex = false;
Expand All @@ -391,10 +442,10 @@ static int window_rating(HWND window, enum window_priority priority,
struct dstr cur_exe = {0};
int val = 0x7FFFFFFF;

if (!get_window_exe(&cur_exe, window))
if (!ms_get_window_exe(&cur_exe, window))
return 0x7FFFFFFF;
get_window_title(&cur_title, window);
get_window_class(&cur_class, window);
ms_get_window_title(&cur_title, window);
ms_get_window_class(&cur_class, window);

bool class_matches = dstr_cmpi(&cur_class, class) == 0;
bool exe_matches = dstr_cmpi(&cur_exe, exe) == 0;
Expand Down Expand Up @@ -457,8 +508,8 @@ static bool is_generic_class(const char *current_class)
return false;
}

HWND find_window(enum window_search_mode mode, enum window_priority priority,
const char *class, const char *title, const char *exe)
HWND ms_find_window(enum window_search_mode mode, enum window_priority priority,
const char *class, const char *title, const char *exe)
{
HWND parent;
bool use_findwindowex = false;
Expand Down Expand Up @@ -520,9 +571,9 @@ BOOL CALLBACK enum_windows_proc(HWND window, LPARAM lParam)
return rating > 0;
}

HWND find_window_top_level(enum window_search_mode mode,
enum window_priority priority, const char *class,
const char *title, const char *exe)
HWND ms_find_window_top_level(enum window_search_mode mode,
enum window_priority priority, const char *class,
const char *title, const char *exe)
{
if (!class)
return NULL;
Expand Down
56 changes: 56 additions & 0 deletions libobs/util/windows/window-helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <obs-properties.h>
#include <util/c99defs.h>
#include <Windows.h>

#ifdef __cplusplus
extern "C" {
#endif

enum window_priority {
WINDOW_PRIORITY_CLASS,
WINDOW_PRIORITY_TITLE,
WINDOW_PRIORITY_EXE,
};

enum window_search_mode {
INCLUDE_MINIMIZED,
EXCLUDE_MINIMIZED,
};

EXPORT bool ms_get_window_exe(struct dstr *name, HWND window);
EXPORT void ms_get_window_title(struct dstr *name, HWND hwnd);
EXPORT void ms_get_window_class(struct dstr *window_class, HWND hwnd);
EXPORT bool ms_is_uwp_window(HWND hwnd);
EXPORT HWND ms_get_uwp_actual_window(HWND parent);

typedef bool (*add_window_cb)(const char *title, const char *window_class,
const char *exe);

EXPORT void ms_fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback);

EXPORT void ms_build_window_strings(const char *str, char **window_class,
char **title, char **exe);

EXPORT bool ms_check_window_property_setting(obs_properties_t *ppts,
obs_property_t *p,
obs_data_t *settings,
const char *val, size_t idx);

EXPORT void ms_build_window_strings(const char *str, char **window_class,
char **title, char **exe);

EXPORT HWND ms_find_window(enum window_search_mode mode,
enum window_priority priority,
const char *window_class, const char *title,
const char *exe);
EXPORT HWND ms_find_window_top_level(enum window_search_mode mode,
enum window_priority priority,
const char *window_class,
const char *title, const char *exe);

#ifdef __cplusplus
}
#endif
8 changes: 2 additions & 6 deletions plugins/win-capture/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ target_sources(
monitor-capture.c
nt-stuff.c
nt-stuff.h
obfuscate.c
obfuscate.h
window-capture.c
window-helpers.c
window-helpers.h)
window-capture.c)

target_link_libraries(win-capture PRIVATE OBS::libobs OBS::ipc-util Dwmapi)
target_link_libraries(win-capture PRIVATE OBS::libobs OBS::ipc-util)

set_target_properties(win-capture PROPERTIES FOLDER "plugins/win-capture")

Expand Down
Loading

0 comments on commit 69ff026

Please sign in to comment.