Blender V5.0
wm_platform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "WM_api.hh" /* Own include. */
12
13#ifdef WIN32
14# include "BLI_winstuff.h"
15#elif defined(__APPLE__)
16/* Pass. */
17#else
18# ifdef WITH_PYTHON
19# include "BLI_string.h"
20
21# include "BKE_context.hh"
22
23# include "BPY_extern_run.hh"
24# endif
25#endif
26
27/* -------------------------------------------------------------------- */
30
31bool WM_platform_associate_set(bool do_register, bool all_users, char **r_error_msg)
32{
33 bool result = false;
34 *r_error_msg = nullptr;
35#ifdef WIN32
36 {
37 if (all_users) {
38 if (do_register) {
39 result = BLI_windows_execute_self("--register-allusers", true, true, true);
40 }
41 else {
42 result = BLI_windows_execute_self("--unregister-allusers", true, true, true);
43 }
44 }
45 else {
46 if (do_register) {
48 }
49 else {
51 }
52 }
53 }
54#elif defined(__APPLE__)
55 /* Pass. */
56 UNUSED_VARS(do_register, all_users);
57#else
58 {
59# ifdef WITH_PYTHON
60 BPy_RunErrInfo err_info = {};
61 err_info.use_single_line_error = true;
62 err_info.r_string = r_error_msg;
63
64 const char *imports[] = {"_bpy_internal", "_bpy_internal.freedesktop", nullptr};
65 char expr_buf[128];
66
67 SNPRINTF(expr_buf,
68 "_bpy_internal.freedesktop.%s(all_users=%d)",
69 do_register ? "register" : "unregister",
70 int(all_users));
71
72 /* NOTE: this could be null, however the running a script without `bpy.context` access
73 * is a rare enough situation that it's better to keep this a requirement of the API and
74 * pass in a temporary context instead of making an exception for this one case. */
75 bContext *C_temp = CTX_create();
76 char *value = nullptr;
77 if (BPY_run_string_as_string_or_none(C_temp, imports, expr_buf, &err_info, &value)) {
78 result = (value == nullptr);
79 *r_error_msg = value;
80 }
81 /* Else `r_error_msg` will be set to a single line exception. */
82 CTX_free(C_temp);
83# else
84 /* Pass. */
85 UNUSED_VARS(do_register, all_users);
86# endif
87 }
88#endif
89 return result;
90}
91
void CTX_free(bContext *C)
bContext * CTX_create()
#define SNPRINTF(dst, format,...)
Definition BLI_string.h:604
#define UNUSED_VARS(...)
Compatibility-like things for windows.
bool BLI_windows_unregister_blend_extension(bool all_users)
bool BLI_windows_execute_self(const char *parameters, const bool wait, const bool elevated, const bool silent)
bool BLI_windows_register_blend_extension(bool all_users)
bool bool bool bool bool bool BPY_run_string_as_string_or_none(bContext *C, const char *imports[], const char *expr, BPy_RunErrInfo *err_info, char **r_value) ATTR_NONNULL(1
bool WM_platform_associate_set(bool do_register, bool all_users, char **r_error_msg)