22 #include <winpr/assert.h>
23 #include <winpr/wtypes.h>
24 #include <winpr/path.h>
29 #include <freerdp/log.h>
31 #define TAG CLIENT_TAG("xfreerdp.utils")
33 static const DWORD log_level = WLOG_TRACE;
35 static void write_log(wLog* log, DWORD level,
const char* fname,
const char* fkt,
size_t line, ...)
39 WLog_PrintMessageVA(log, WLOG_MESSAGE_TEXT, level, line, fname, fkt, ap);
43 char* Safe_XGetAtomNameEx(wLog* log, Display* display, Atom atom,
const char* varname)
45 WLog_Print(log, log_level,
"XGetAtomName(%s, 0x%08" PRIx32
")", varname, atom);
47 return strdup(
"Atom_None");
48 return XGetAtomName(display, atom);
51 Atom Logging_XInternAtom(wLog* log, Display* display, _Xconst
char* atom_name, Bool only_if_exists)
53 Atom atom = XInternAtom(display, atom_name, only_if_exists);
54 if (WLog_IsLevelActive(log, log_level))
56 WLog_Print(log, log_level,
"XInternAtom(0x%08" PRIx32
", %s, %s) -> 0x%08" PRIx32, display,
57 atom_name, only_if_exists ?
"True" :
"False", atom);
62 int LogTagAndXChangeProperty_ex(
const char* tag,
const char* file,
const char* fkt,
size_t line,
63 Display* display, Window w, Atom property, Atom type,
int format,
64 int mode,
const unsigned char* data,
int nelements)
66 wLog* log = WLog_Get(tag);
67 return LogDynAndXChangeProperty_ex(log, file, fkt, line, display, w, property, type, format,
68 mode, data, nelements);
71 int LogDynAndXChangeProperty_ex(wLog* log,
const char* file,
const char* fkt,
size_t line,
72 Display* display, Window w, Atom property, Atom type,
int format,
73 int mode,
const unsigned char* data,
int nelements)
75 if (WLog_IsLevelActive(log, log_level))
77 char* propstr = Safe_XGetAtomName(log, display, property);
78 char* typestr = Safe_XGetAtomName(log, display, type);
79 write_log(log, log_level, file, fkt, line,
80 "XChangeProperty(%p, %d, %s [%d], %s [%d], %d, %d, %p, %d)", display, w, propstr,
81 property, typestr, type, format, mode, data, nelements);
85 return XChangeProperty(display, w, property, type, format, mode, data, nelements);
88 int LogTagAndXDeleteProperty_ex(
const char* tag,
const char* file,
const char* fkt,
size_t line,
89 Display* display, Window w, Atom property)
91 wLog* log = WLog_Get(tag);
92 return LogDynAndXDeleteProperty_ex(log, file, fkt, line, display, w, property);
95 int LogDynAndXDeleteProperty_ex(wLog* log,
const char* file,
const char* fkt,
size_t line,
96 Display* display, Window w, Atom property)
98 if (WLog_IsLevelActive(log, log_level))
100 char* propstr = Safe_XGetAtomName(log, display, property);
101 write_log(log, log_level, file, fkt, line,
"XDeleteProperty(%p, %d, %s [%d])", display, w,
105 return XDeleteProperty(display, w, property);
108 int LogTagAndXConvertSelection_ex(
const char* tag,
const char* file,
const char* fkt,
size_t line,
109 Display* display, Atom selection, Atom target, Atom property,
110 Window requestor, Time time)
112 wLog* log = WLog_Get(tag);
113 return LogDynAndXConvertSelection_ex(log, file, fkt, line, display, selection, target, property,
117 int LogDynAndXConvertSelection_ex(wLog* log,
const char* file,
const char* fkt,
size_t line,
118 Display* display, Atom selection, Atom target, Atom property,
119 Window requestor, Time time)
121 if (WLog_IsLevelActive(log, log_level))
123 char* selectstr = Safe_XGetAtomName(log, display, selection);
124 char* targetstr = Safe_XGetAtomName(log, display, target);
125 char* propstr = Safe_XGetAtomName(log, display, property);
126 write_log(log, log_level, file, fkt, line,
127 "XConvertSelection(%p, %s [%d], %s [%d], %s [%d], %d, %lu)", display, selectstr,
128 selection, targetstr, target, propstr, property, requestor, time);
133 return XConvertSelection(display, selection, target, property, requestor, time);
136 int LogTagAndXGetWindowProperty_ex(
const char* tag,
const char* file,
const char* fkt,
size_t line,
137 Display* display, Window w, Atom property,
long long_offset,
138 long long_length,
int delete, Atom req_type,
139 Atom* actual_type_return,
int* actual_format_return,
140 unsigned long* nitems_return,
unsigned long* bytes_after_return,
141 unsigned char** prop_return)
143 wLog* log = WLog_Get(tag);
144 return LogDynAndXGetWindowProperty_ex(
145 log, file, fkt, line, display, w, property, long_offset, long_length,
delete, req_type,
146 actual_type_return, actual_format_return, nitems_return, bytes_after_return, prop_return);
149 int LogDynAndXGetWindowProperty_ex(wLog* log,
const char* file,
const char* fkt,
size_t line,
150 Display* display, Window w, Atom property,
long long_offset,
151 long long_length,
int delete, Atom req_type,
152 Atom* actual_type_return,
int* actual_format_return,
153 unsigned long* nitems_return,
unsigned long* bytes_after_return,
154 unsigned char** prop_return)
156 if (WLog_IsLevelActive(log, log_level))
158 char* propstr = Safe_XGetAtomName(log, display, property);
159 char* req_type_str = Safe_XGetAtomName(log, display, req_type);
160 write_log(log, log_level, file, fkt, line,
161 "XGetWindowProperty(%p, %d, %s [%d], %ld, %ld, %d, %s [%d], %p, %p, %p, %p, %p)",
162 display, w, propstr, property, long_offset, long_length,
delete, req_type_str,
163 req_type, actual_type_return, actual_format_return, nitems_return,
164 bytes_after_return, prop_return);
168 return XGetWindowProperty(display, w, property, long_offset, long_length,
delete, req_type,
169 actual_type_return, actual_format_return, nitems_return,
170 bytes_after_return, prop_return);
175 char* env = getenv(
"DESKTOP_SESSION");
176 return (env != NULL && strcmp(env,
"gnome") == 0);
179 BOOL run_action_script(xfContext* xfc,
const char* what,
const char* arg, fn_action_script_run fkt,
183 FILE* keyScript = NULL;
186 rdpSettings* settings = xfc->common.context.settings;
187 WINPR_ASSERT(settings);
191 xfc->actionScriptExists = winpr_PathFileExists(ActionScript);
193 if (!xfc->actionScriptExists)
196 char command[2048] = { 0 };
197 (void)sprintf_s(command,
sizeof(command),
"%s %s", ActionScript, what);
198 keyScript = popen(command,
"r");
202 WLog_ERR(TAG,
"Failed to execute '%s'", command);
206 BOOL read_data = FALSE;
207 char buffer[2048] = { 0 };
208 while (fgets(buffer,
sizeof(buffer), keyScript) != NULL)
210 char* context = NULL;
211 (void)strtok_s(buffer,
"\n", &context);
215 if (!fkt(xfc, buffer, strnlen(buffer,
sizeof(buffer)), user, what, arg))
224 xfc->actionScriptExists = FALSE;
230 const char* x11_error_to_string(xfContext* xfc,
int error,
char* buffer,
size_t size)
233 WINPR_ASSERT(size <= INT32_MAX);
234 const int rc = XGetErrorText(xfc->display, error, buffer, (
int)size);
236 WLog_WARN(TAG,
"XGetErrorText returned %d", rc);
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.