23#include <freerdp/utils/helpers.h>
25#include <winpr/path.h>
26#include <winpr/file.h>
27#include <winpr/build-config.h>
28#include <freerdp/version.h>
29#include <freerdp/build-config.h>
31#include "../core/utils.h"
33static INIT_ONCE s_freerdp_app_details_once = INIT_ONCE_STATIC_INIT;
34static char s_freerdp_vendor_string[MAX_PATH] = { 0 };
35static char s_freerdp_product_string[MAX_PATH] = { 0 };
36static char s_freerdp_details_string[3ull * MAX_PATH] = { 0 };
37static WCHAR s_freerdp_details_string_w[3ull * MAX_PATH] = { 0 };
38static SSIZE_T s_freerdp_version = -1;
39static BOOL s_freerdp_app_details_are_custom = FALSE;
41static void updateDetailsString(
void)
43 const char* vendor = s_freerdp_vendor_string;
44 const char* product = s_freerdp_product_string;
45 const SSIZE_T version = s_freerdp_version;
48 WINPR_ASSERT(product);
49 if (s_freerdp_app_details_are_custom)
52 (void)_snprintf(s_freerdp_details_string,
sizeof(s_freerdp_details_string) - 1,
"%s-%s",
55 (
void)_snprintf(s_freerdp_details_string,
sizeof(s_freerdp_details_string) - 1,
56 "%s-%s%" PRIdz, vendor, product, version);
60 (void)_snprintf(s_freerdp_details_string,
sizeof(s_freerdp_details_string) - 1,
"%s",
64 (
void)_snprintf(s_freerdp_details_string,
sizeof(s_freerdp_details_string) - 1,
"%s%" PRIdz,
67 (void)ConvertUtf8NToWChar(s_freerdp_details_string,
sizeof(s_freerdp_details_string),
68 s_freerdp_details_string_w,
sizeof(s_freerdp_details_string_w) - 1);
71static BOOL CALLBACK init_app_details(WINPR_ATTR_UNUSED
PINIT_ONCE once,
72 WINPR_ATTR_UNUSED PVOID param,
73 WINPR_ATTR_UNUSED PVOID* context)
75 const size_t vlen =
sizeof(FREERDP_VENDOR_STRING);
76 const size_t plen =
sizeof(FREERDP_PRODUCT_STRING);
77 const char* rvlen = strncpy(s_freerdp_vendor_string, FREERDP_VENDOR_STRING, vlen);
78 const char* rplen = strncpy(s_freerdp_product_string, FREERDP_PRODUCT_STRING, plen);
82#if defined(WITH_RESOURCE_VERSIONING)
83 s_freerdp_version = FREERDP_VERSION_MAJOR;
85 s_freerdp_version = -1;
87 updateDetailsString();
91static WINPR_ATTR_NODISCARD BOOL initializeApplicationDetails(
void)
93 InitOnceExecuteOnce(&s_freerdp_app_details_once, init_app_details, NULL, NULL);
97BOOL freerdp_setApplicationDetails(
const char* vendor,
const char* product, SSIZE_T version)
99 if (!initializeApplicationDetails())
102 if (!vendor || !product)
104 const size_t vlen = strnlen(vendor, MAX_PATH);
105 const size_t plen = strnlen(product, MAX_PATH);
106 if ((vlen == MAX_PATH) || (plen == MAX_PATH))
109 if (!strncpy(s_freerdp_vendor_string, vendor, vlen + 1))
112 if (!strncpy(s_freerdp_product_string, product, plen + 1))
115 s_freerdp_version = version;
116 s_freerdp_app_details_are_custom = TRUE;
118 const char separator = PathGetSeparatorA(PATH_STYLE_NATIVE);
119 char* str = freerdp_getApplicatonDetailsCombined(separator);
123 const BOOL rc = winpr_setApplicationDetails(str,
"WinPR", -1);
125 updateDetailsString();
129const char* freerdp_getApplicationDetailsVendor(
void)
131 if (!initializeApplicationDetails())
133 return s_freerdp_vendor_string;
136const char* freerdp_getApplicationDetailsProduct(
void)
138 if (!initializeApplicationDetails())
140 return s_freerdp_product_string;
143char* freerdp_getApplicatonDetailsRegKey(
const char* fmt)
145 char* val = freerdp_getApplicatonDetailsCombined(
'\\');
151 (void)winpr_asprintf(&str, &slen, fmt, val);
156char* freerdp_getApplicatonDetailsCombined(
char separator)
158 const SSIZE_T version = freerdp_getApplicationDetailsVersion();
159 const char* vendor = freerdp_getApplicationDetailsVendor();
160 const char* product = freerdp_getApplicationDetailsProduct();
166 (void)winpr_asprintf(&str, &slen,
"%s%c%s", vendor, separator, product);
170 (void)winpr_asprintf(&str, &slen,
"%s%c%s%" PRIdz, vendor, separator, product, version);
176SSIZE_T freerdp_getApplicationDetailsVersion(
void)
178 if (!initializeApplicationDetails())
180 return s_freerdp_version;
183const char* freerdp_getApplicationDetailsString(
void)
185 return s_freerdp_details_string;
188const WCHAR* freerdp_getApplicationDetailsStringW(
void)
190 return s_freerdp_details_string_w;
193BOOL freerdp_areApplicationDetailsCustomized(
void)
195 return s_freerdp_app_details_are_custom;
198#if !defined(WITH_FULL_CONFIG_PATH)
199WINPR_ATTR_MALLOC(free, 1)
201static
char* freerdp_settings_get_legacy_config_path(const
char* filename)
203 char product[
sizeof(FREERDP_PRODUCT_STRING)] = { 0 };
205 for (
size_t i = 0; i <
sizeof(product); i++)
206 product[i] = (
char)tolower(FREERDP_PRODUCT_STRING[i]);
208 char* path = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, product);
213 char* filepath = GetCombinedPath(path, filename);
220WINPR_ATTR_MALLOC(free, 1) static
char* getCustomConfigPath(BOOL system, const
char* filename)
222 eKnownPathTypes
id = system ? KNOWN_PATH_SYSTEM_CONFIG_HOME : KNOWN_PATH_XDG_CONFIG_HOME;
224 const char* vendor = freerdp_getApplicationDetailsVendor();
225 const char* product = freerdp_getApplicationDetailsProduct();
226 const SSIZE_T version = freerdp_getApplicationDetailsVersion();
228 if (!vendor || !product)
231 char* config = GetKnownSubPathV(
id,
"%s", vendor);
237 base = GetCombinedPathV(config,
"%s", product);
239 base = GetCombinedPathV(config,
"%s%" PRIdz, product, version);
248 char* path = GetCombinedPathV(base,
"%s", filename);
253char* freerdp_GetConfigFilePath(BOOL system,
const char* filename)
255#if defined(FREERDP_USE_VENDOR_PRODUCT_CONFIG_DIR)
256 const BOOL customized = TRUE;
258 const BOOL customized = freerdp_areApplicationDetailsCustomized();
261 return getCustomConfigPath(system, filename);
263 eKnownPathTypes
id = system ? KNOWN_PATH_SYSTEM_CONFIG_HOME : KNOWN_PATH_XDG_CONFIG_HOME;
265 const char* vendor = freerdp_getApplicationDetailsVendor();
266 const char* product = freerdp_getApplicationDetailsProduct();
267 const SSIZE_T version = freerdp_getApplicationDetailsVersion();
269 if (!vendor || !product)
272#if !defined(WITH_FULL_CONFIG_PATH)
273 if (!system && (_stricmp(vendor, product) == 0))
274 return freerdp_settings_get_legacy_config_path(filename);
277 char* config = GetKnownPath(
id);
283 base = GetCombinedPathV(config,
"%s", product);
285 base = GetCombinedPathV(config,
"%s%" PRIdz, product, version);
294 char* path = GetCombinedPathV(base,
"%s", filename);
299WINPR_JSON* freerdp_GetJSONConfigFile(BOOL system,
const char* filename)
301 char* path = freerdp_GetConfigFilePath(system, filename);
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.