style: adjust clang-format rules (#2186)

Co-authored-by: Vithorio Polten <reach@vithor.io>
This commit is contained in:
ReenigneArcher 2025-01-19 22:34:47 -05:00 committed by GitHub
commit c2420427b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
158 changed files with 8754 additions and 9994 deletions

View file

@ -3,19 +3,16 @@
* @brief Handles collecting audio device information from Windows.
*/
#define INITGUID
#include "src/utility.h"
#include <audioclient.h>
#include <codecvt>
#include <iostream>
#include <locale>
#include <mmdeviceapi.h>
#include <roapi.h>
#include <codecvt>
#include <locale>
#include <synchapi.h>
#include <iostream>
#include "src/utility.h"
DEFINE_PROPERTYKEY(PKEY_Device_DeviceDesc, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 2); // DEVPROP_TYPE_STRING
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14); // DEVPROP_TYPE_STRING
DEFINE_PROPERTYKEY(PKEY_DeviceInterface_FriendlyName, 0x026e516e, 0xb814, 0x414b, 0x83, 0xcd, 0x85, 0x6d, 0x6f, 0xef, 0x48, 0x22, 2);
@ -25,15 +22,13 @@ using namespace std::literals;
int device_state_filter = DEVICE_STATE_ACTIVE;
namespace audio {
template <class T>
void
Release(T *p) {
template<class T>
void Release(T *p) {
p->Release();
}
template <class T>
void
co_task_free(T *p) {
template<class T>
void co_task_free(T *p) {
CoTaskMemFree((LPVOID) p);
}
@ -64,8 +59,7 @@ namespace audio {
PROPVARIANT prop;
};
const wchar_t *
no_null(const wchar_t *str) {
const wchar_t *no_null(const wchar_t *str) {
return str ? str : L"Unknown";
}
@ -74,48 +68,47 @@ namespace audio {
int channels;
int channel_mask;
} formats[] {
{ "Mono"sv,
1,
SPEAKER_FRONT_CENTER },
{ "Stereo"sv,
2,
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT },
{ "Quadraphonic"sv,
4,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_BACK_LEFT |
SPEAKER_BACK_RIGHT },
{ "Surround 5.1 (Side)"sv,
6,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER |
SPEAKER_LOW_FREQUENCY |
SPEAKER_SIDE_LEFT |
SPEAKER_SIDE_RIGHT },
{ "Surround 5.1 (Back)"sv,
6,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER |
SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT |
SPEAKER_BACK_RIGHT },
{ "Surround 7.1"sv,
8,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER |
SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT |
SPEAKER_BACK_RIGHT |
SPEAKER_SIDE_LEFT |
SPEAKER_SIDE_RIGHT }
{"Mono"sv,
1,
SPEAKER_FRONT_CENTER},
{"Stereo"sv,
2,
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT},
{"Quadraphonic"sv,
4,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_BACK_LEFT |
SPEAKER_BACK_RIGHT},
{"Surround 5.1 (Side)"sv,
6,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER |
SPEAKER_LOW_FREQUENCY |
SPEAKER_SIDE_LEFT |
SPEAKER_SIDE_RIGHT},
{"Surround 5.1 (Back)"sv,
6,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER |
SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT |
SPEAKER_BACK_RIGHT},
{"Surround 7.1"sv,
8,
SPEAKER_FRONT_LEFT |
SPEAKER_FRONT_RIGHT |
SPEAKER_FRONT_CENTER |
SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT |
SPEAKER_BACK_RIGHT |
SPEAKER_SIDE_LEFT |
SPEAKER_SIDE_RIGHT}
};
void
set_wave_format(audio::wave_format_t &wave_format, const format_t &format) {
void set_wave_format(audio::wave_format_t &wave_format, const format_t &format) {
wave_format->nChannels = format.channels;
wave_format->nBlockAlign = wave_format->nChannels * wave_format->wBitsPerSample / 8;
wave_format->nAvgBytesPerSec = wave_format->nSamplesPerSec * wave_format->nBlockAlign;
@ -125,14 +118,14 @@ namespace audio {
}
}
audio_client_t
make_audio_client(device_t &device, const format_t &format) {
audio_client_t make_audio_client(device_t &device, const format_t &format) {
audio_client_t audio_client;
auto status = device->Activate(
IID_IAudioClient,
CLSCTX_ALL,
nullptr,
(void **) &audio_client);
(void **) &audio_client
);
if (FAILED(status)) {
std::cout << "Couldn't activate Device: [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
@ -154,9 +147,11 @@ namespace audio {
status = audio_client->Initialize(
AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_LOOPBACK | AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
0, 0,
0,
0,
wave_format.get(),
nullptr);
nullptr
);
if (status) {
return nullptr;
@ -165,8 +160,7 @@ namespace audio {
return audio_client;
}
void
print_device(device_t &device) {
void print_device(device_t &device) {
audio::wstring_t wstring;
DWORD device_state;
@ -227,16 +221,14 @@ namespace audio {
}
} // namespace audio
void
print_help() {
void print_help() {
std::cout
<< "==== Help ===="sv << std::endl
<< "Usage:"sv << std::endl
<< " audio-info [Active|Disabled|Unplugged|Not-Present]" << std::endl;
}
int
main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
CoInitializeEx(nullptr, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY);
auto fg = util::fail_guard([]() {
@ -260,17 +252,13 @@ main(int argc, char *argv[]) {
if (argv[x] == "active"sv) {
device_state_filter |= DEVICE_STATE_ACTIVE;
}
else if (argv[x] == "disabled"sv) {
} else if (argv[x] == "disabled"sv) {
device_state_filter |= DEVICE_STATE_DISABLED;
}
else if (argv[x] == "unplugged"sv) {
} else if (argv[x] == "unplugged"sv) {
device_state_filter |= DEVICE_STATE_UNPLUGGED;
}
else if (argv[x] == "not-present"sv) {
} else if (argv[x] == "not-present"sv) {
device_state_filter |= DEVICE_STATE_NOTPRESENT;
}
else {
} else {
print_help();
return 2;
}
@ -284,7 +272,8 @@ main(int argc, char *argv[]) {
nullptr,
CLSCTX_ALL,
IID_IMMDeviceEnumerator,
(void **) &device_enum);
(void **) &device_enum
);
if (FAILED(status)) {
std::cout << "Couldn't create Device Enumerator: [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
@ -313,4 +302,4 @@ main(int argc, char *argv[]) {
}
return 0;
}
}

View file

@ -3,18 +3,17 @@
* @brief Displays information about connected displays and GPUs
*/
#define WINVER 0x0A00
#include <d3dcommon.h>
#include <dxgi.h>
#include <iostream>
#include "src/utility.h"
#include <d3dcommon.h>
#include <dxgi.h>
#include <iostream>
using namespace std::literals;
namespace dxgi {
template <class T>
void
Release(T *dxgi) {
template<class T>
void Release(T *dxgi) {
dxgi->Release();
}
@ -24,8 +23,7 @@ namespace dxgi {
} // namespace dxgi
int
main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
HRESULT status;
// Set ourselves as per-monitor DPI aware for accurate resolution values on High DPI systems
@ -33,7 +31,7 @@ main(int argc, char *argv[]) {
dxgi::factory1_t::pointer factory_p {};
status = CreateDXGIFactory1(IID_IDXGIFactory1, (void **) &factory_p);
dxgi::factory1_t factory { factory_p };
dxgi::factory1_t factory {factory_p};
if (FAILED(status)) {
std::cout << "Failed to create DXGIFactory1 [0x"sv << util::hex(status).to_string_view() << ']' << std::endl;
return -1;
@ -41,7 +39,7 @@ main(int argc, char *argv[]) {
dxgi::adapter_t::pointer adapter_p {};
for (int x = 0; factory->EnumAdapters1(x, &adapter_p) != DXGI_ERROR_NOT_FOUND; ++x) {
dxgi::adapter_t adapter { adapter_p };
dxgi::adapter_t adapter {adapter_p};
DXGI_ADAPTER_DESC1 adapter_desc;
adapter->GetDesc1(&adapter_desc);
@ -61,7 +59,7 @@ main(int argc, char *argv[]) {
dxgi::output_t::pointer output_p {};
for (int y = 0; adapter->EnumOutputs(y, &output_p) != DXGI_ERROR_NOT_FOUND; ++y) {
dxgi::output_t output { output_p };
dxgi::output_t output {output_p};
DXGI_OUTPUT_DESC desc;
output->GetDesc(&desc);

View file

@ -3,11 +3,10 @@
* @brief Handles launching Sunshine.exe into user sessions as SYSTEM
*/
#define WIN32_LEAN_AND_MEAN
#include <string>
#include <Windows.h>
#include <wtsapi32.h>
#include <string>
// PROC_THREAD_ATTRIBUTE_JOB_LIST is currently missing from MinGW headers
#ifndef PROC_THREAD_ATTRIBUTE_JOB_LIST
#define PROC_THREAD_ATTRIBUTE_JOB_LIST ProcThreadAttributeValue(13, FALSE, TRUE, FALSE)
@ -20,8 +19,7 @@ HANDLE session_change_event;
#define SERVICE_NAME "SunshineService"
DWORD WINAPI
HandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) {
DWORD WINAPI HandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) {
switch (dwControl) {
case SERVICE_CONTROL_INTERROGATE:
return NO_ERROR;
@ -52,8 +50,7 @@ HandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpConte
}
}
HANDLE
CreateJobObjectForChildProcess() {
HANDLE CreateJobObjectForChildProcess() {
HANDLE job_handle = CreateJobObjectW(NULL, NULL);
if (!job_handle) {
return NULL;
@ -77,8 +74,7 @@ CreateJobObjectForChildProcess() {
return job_handle;
}
LPPROC_THREAD_ATTRIBUTE_LIST
AllocateProcThreadAttributeList(DWORD attribute_count) {
LPPROC_THREAD_ATTRIBUTE_LIST AllocateProcThreadAttributeList(DWORD attribute_count) {
SIZE_T size;
InitializeProcThreadAttributeList(NULL, attribute_count, 0, &size);
@ -95,8 +91,7 @@ AllocateProcThreadAttributeList(DWORD attribute_count) {
return list;
}
HANDLE
DuplicateTokenForSession(DWORD console_session_id) {
HANDLE DuplicateTokenForSession(DWORD console_session_id) {
HANDLE current_token;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &current_token)) {
return NULL;
@ -120,8 +115,7 @@ DuplicateTokenForSession(DWORD console_session_id) {
return new_token;
}
HANDLE
OpenLogFileHandle() {
HANDLE OpenLogFileHandle() {
WCHAR log_file_name[MAX_PATH];
// Create sunshine.log in the Temp folder (usually %SYSTEMROOT%\Temp)
@ -129,20 +123,13 @@ OpenLogFileHandle() {
wcscat_s(log_file_name, L"sunshine.log");
// The file handle must be inheritable for our child process to use it
SECURITY_ATTRIBUTES security_attributes = { sizeof(security_attributes), NULL, TRUE };
SECURITY_ATTRIBUTES security_attributes = {sizeof(security_attributes), NULL, TRUE};
// Overwrite the old sunshine.log
return CreateFileW(log_file_name,
GENERIC_WRITE,
FILE_SHARE_READ,
&security_attributes,
CREATE_ALWAYS,
0,
NULL);
return CreateFileW(log_file_name, GENERIC_WRITE, FILE_SHARE_READ, &security_attributes, CREATE_ALWAYS, 0, NULL);
}
bool
RunTerminationHelper(HANDLE console_token, DWORD pid) {
bool RunTerminationHelper(HANDLE console_token, DWORD pid) {
WCHAR module_path[MAX_PATH];
GetModuleFileNameW(NULL, module_path, _countof(module_path));
std::wstring command;
@ -159,17 +146,7 @@ RunTerminationHelper(HANDLE console_token, DWORD pid) {
// Execute ourselves as a detached process in the user session with the --terminate argument.
// This will allow us to attach to Sunshine's console and send it a Ctrl-C event.
PROCESS_INFORMATION process_info;
if (!CreateProcessAsUserW(console_token,
module_path,
(LPWSTR) command.c_str(),
NULL,
NULL,
FALSE,
CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS,
NULL,
NULL,
&startup_info,
&process_info)) {
if (!CreateProcessAsUserW(console_token, module_path, (LPWSTR) command.c_str(), NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT | DETACHED_PROCESS, NULL, NULL, &startup_info, &process_info)) {
return false;
}
@ -188,8 +165,7 @@ RunTerminationHelper(HANDLE console_token, DWORD pid) {
return exit_code == 0;
}
VOID WINAPI
ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
service_status_handle = RegisterServiceCtrlHandlerEx(SERVICE_NAME, HandlerEx, NULL);
if (service_status_handle == NULL) {
// Nothing we can really do here but terminate ourselves
@ -256,13 +232,7 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
}
// Only allow Sunshine.exe to inherit the log file handle, not all inheritable handles
UpdateProcThreadAttribute(startup_info.lpAttributeList,
0,
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
&log_file_handle,
sizeof(log_file_handle),
NULL,
NULL);
UpdateProcThreadAttribute(startup_info.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, &log_file_handle, sizeof(log_file_handle), NULL, NULL);
// Tell SCM we're running (and stoppable now)
service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PRESHUTDOWN | SERVICE_ACCEPT_SESSIONCHANGE;
@ -290,26 +260,10 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
}
// Start Sunshine.exe inside our job object
UpdateProcThreadAttribute(startup_info.lpAttributeList,
0,
PROC_THREAD_ATTRIBUTE_JOB_LIST,
&job_handle,
sizeof(job_handle),
NULL,
NULL);
UpdateProcThreadAttribute(startup_info.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_JOB_LIST, &job_handle, sizeof(job_handle), NULL, NULL);
PROCESS_INFORMATION process_info;
if (!CreateProcessAsUserW(console_token,
L"Sunshine.exe",
NULL,
NULL,
NULL,
TRUE,
CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW | EXTENDED_STARTUPINFO_PRESENT,
NULL,
NULL,
(LPSTARTUPINFOW) &startup_info,
&process_info)) {
if (!CreateProcessAsUserW(console_token, L"Sunshine.exe", NULL, NULL, NULL, TRUE, CREATE_UNICODE_ENVIRONMENT | CREATE_NO_WINDOW | EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, (LPSTARTUPINFOW) &startup_info, &process_info)) {
CloseHandle(console_token);
CloseHandle(job_handle);
continue;
@ -318,7 +272,7 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
bool still_running;
do {
// Wait for the stop event to be set, Sunshine.exe to terminate, or the console session to change
const HANDLE wait_objects[] = { stop_event, process_info.hProcess, session_change_event };
const HANDLE wait_objects[] = {stop_event, process_info.hProcess, session_change_event};
switch (WaitForMultipleObjects(_countof(wait_objects), wait_objects, FALSE, INFINITE)) {
case WAIT_OBJECT_0 + 2:
if (WTSGetActiveConsoleSessionId() == console_session_id) {
@ -338,17 +292,18 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
still_running = false;
break;
case WAIT_OBJECT_0 + 1: {
// Sunshine terminated itself.
case WAIT_OBJECT_0 + 1:
{
// Sunshine terminated itself.
DWORD exit_code;
if (GetExitCodeProcess(process_info.hProcess, &exit_code) && exit_code == ERROR_SHUTDOWN_IN_PROGRESS) {
// Sunshine is asking for us to shut down, so gracefully stop ourselves.
SetEvent(stop_event);
DWORD exit_code;
if (GetExitCodeProcess(process_info.hProcess, &exit_code) && exit_code == ERROR_SHUTDOWN_IN_PROGRESS) {
// Sunshine is asking for us to shut down, so gracefully stop ourselves.
SetEvent(stop_event);
}
still_running = false;
break;
}
still_running = false;
break;
}
}
} while (still_running);
@ -364,8 +319,7 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
}
// This will run in a child process in the user session
int
DoGracefulTermination(DWORD pid) {
int DoGracefulTermination(DWORD pid) {
// Attach to Sunshine's console
if (!AttachConsole(pid)) {
return GetLastError();
@ -382,11 +336,10 @@ DoGracefulTermination(DWORD pid) {
return 0;
}
int
main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
static const SERVICE_TABLE_ENTRY service_table[] = {
{ (LPSTR) SERVICE_NAME, ServiceMain },
{ NULL, NULL }
{(LPSTR) SERVICE_NAME, ServiceMain},
{NULL, NULL}
};
// Check if this is a reinvocation of ourselves to send Ctrl-C to Sunshine.exe