1 | /* $Id: DisplayServerType.cpp 100217 2023-06-19 17:34:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Guest / Host common code - Session type detection + handling.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/env.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/ldr.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 |
|
---|
38 | #include <VBox/GuestHost/DisplayServerType.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Implementation *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Returns the VBGHDISPLAYSERVERTYPE as a string.
|
---|
47 | *
|
---|
48 | * @returns VBGHDISPLAYSERVERTYPE as a string.
|
---|
49 | * @param enmType VBGHDISPLAYSERVERTYPE to return as a string.
|
---|
50 | */
|
---|
51 | const char *VBGHDisplayServerTypeToStr(VBGHDISPLAYSERVERTYPE enmType)
|
---|
52 | {
|
---|
53 | switch (enmType)
|
---|
54 | {
|
---|
55 | RT_CASE_RET_STR(VBGHDISPLAYSERVERTYPE_NONE);
|
---|
56 | RT_CASE_RET_STR(VBGHDISPLAYSERVERTYPE_AUTO);
|
---|
57 | RT_CASE_RET_STR(VBGHDISPLAYSERVERTYPE_X11);
|
---|
58 | RT_CASE_RET_STR(VBGHDISPLAYSERVERTYPE_PURE_WAYLAND);
|
---|
59 | RT_CASE_RET_STR(VBGHDISPLAYSERVERTYPE_XWAYLAND);
|
---|
60 | default: break;
|
---|
61 | }
|
---|
62 |
|
---|
63 | AssertFailedReturn("<invalid>");
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Tries to load a (system) library via a set of different names / versions.
|
---|
68 | *
|
---|
69 | * @returns VBox status code.
|
---|
70 | * @returns VERR_NOT_FOUND if the library was not found.
|
---|
71 | *
|
---|
72 | * @param apszLibs Array of library (version) names to search for.
|
---|
73 | * Descending order (e.g. "libfoo.so", "libfoo.so.2", "libfoo.so.2.6").
|
---|
74 | * @param cLibs Number of library names in \a apszLibs.
|
---|
75 | * @param phLdrMod Where to return the library handle on success.
|
---|
76 | *
|
---|
77 | * @note Will print loading statuses to verbose release log.
|
---|
78 | */
|
---|
79 | static int vbghDisplayServerTryLoadLib(const char **apszLibs, size_t cLibs, PRTLDRMOD phLdrMod)
|
---|
80 | {
|
---|
81 | for (size_t i = 0; i < cLibs; i++)
|
---|
82 | {
|
---|
83 | const char *pszLib = apszLibs[i];
|
---|
84 | int rc2 = RTLdrLoadSystem(pszLib, /* fNoUnload = */ true, phLdrMod);
|
---|
85 | if (RT_SUCCESS(rc2))
|
---|
86 | {
|
---|
87 | LogRel2(("Loaded display server system library '%s'\n", pszLib));
|
---|
88 | return VINF_SUCCESS;
|
---|
89 | }
|
---|
90 | else
|
---|
91 | LogRel2(("Unable to load display server system library '%s': %Rrc\n", pszLib, rc2));
|
---|
92 | }
|
---|
93 |
|
---|
94 | return VERR_NOT_FOUND;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Tries to detect the desktop display server type the process is running in.
|
---|
99 | *
|
---|
100 | * @returns A value of VBGHDISPLAYSERVERTYPE, or VBGHDISPLAYSERVERTYPE_NONE if detection was not successful.
|
---|
101 | *
|
---|
102 | * @note Precedence is:
|
---|
103 | * - Connecting to Wayland (via libwayland-client.so) and/or X11 (via libX11.so).
|
---|
104 | * - VBGH_ENV_WAYLAND_DISPLAY
|
---|
105 | * - VBGH_ENV_XDG_SESSION_TYPE
|
---|
106 | * - VBGH_ENV_XDG_CURRENT_DESKTOP.
|
---|
107 | *
|
---|
108 | * Will print a warning to the release log if configuration mismatches.
|
---|
109 | */
|
---|
110 | VBGHDISPLAYSERVERTYPE VBGHDisplayServerTypeDetect(void)
|
---|
111 | {
|
---|
112 | LogRel2(("Detecting display server ...\n"));
|
---|
113 |
|
---|
114 | /* Try to connect to the wayland display, assuming it succeeds only when a wayland compositor is active: */
|
---|
115 | bool fHasWayland = false;
|
---|
116 | RTLDRMOD hWaylandClient = NIL_RTLDRMOD;
|
---|
117 |
|
---|
118 | /* Array of libwayland-client.so versions to search for.
|
---|
119 | * Descending precedence. */
|
---|
120 | const char* aLibsWayland[] =
|
---|
121 | {
|
---|
122 | "libwayland-client.so",
|
---|
123 | "libwayland-client.so.0" /* Needed for Ubuntu */
|
---|
124 | };
|
---|
125 |
|
---|
126 | #define GET_SYMBOL(a_Mod, a_Name, a_Fn) \
|
---|
127 | if (RT_SUCCESS(rc)) \
|
---|
128 | rc = RTLdrGetSymbol(a_Mod, a_Name, (void **)&a_Fn); \
|
---|
129 | if (RT_FAILURE(rc)) \
|
---|
130 | LogRel2(("Symbol '%s' unable to load, rc=%Rrc\n", a_Name, rc));
|
---|
131 |
|
---|
132 | int rc = vbghDisplayServerTryLoadLib(aLibsWayland, RT_ELEMENTS(aLibsWayland), &hWaylandClient);
|
---|
133 | if (RT_SUCCESS(rc))
|
---|
134 | {
|
---|
135 | void * (*pWaylandDisplayConnect)(const char *) = NULL;
|
---|
136 | GET_SYMBOL(hWaylandClient, "wl_display_connect", pWaylandDisplayConnect);
|
---|
137 | void (*pWaylandDisplayDisconnect)(void *) = NULL;
|
---|
138 | GET_SYMBOL(hWaylandClient, "wl_display_disconnect", pWaylandDisplayDisconnect);
|
---|
139 | if (RT_SUCCESS(rc))
|
---|
140 | {
|
---|
141 | AssertPtrReturn(pWaylandDisplayConnect, VBGHDISPLAYSERVERTYPE_NONE);
|
---|
142 | AssertPtrReturn(pWaylandDisplayDisconnect, VBGHDISPLAYSERVERTYPE_NONE);
|
---|
143 | void *pDisplay = pWaylandDisplayConnect(NULL);
|
---|
144 | if (pDisplay)
|
---|
145 | {
|
---|
146 | fHasWayland = true;
|
---|
147 | pWaylandDisplayDisconnect(pDisplay);
|
---|
148 | }
|
---|
149 | else
|
---|
150 | LogRel2(("Connecting to Wayland display failed\n"));
|
---|
151 | }
|
---|
152 | RTLdrClose(hWaylandClient);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Array of libX11.so versions to search for.
|
---|
156 | * Descending precedence. */
|
---|
157 | const char* aLibsX11[] =
|
---|
158 | {
|
---|
159 | "libX11.so",
|
---|
160 | "libX11.so.6"
|
---|
161 | };
|
---|
162 |
|
---|
163 | /* Also try to connect to the default X11 display to determine if Xserver is running: */
|
---|
164 | bool fHasX = false;
|
---|
165 | RTLDRMOD hX11 = NIL_RTLDRMOD;
|
---|
166 | rc = vbghDisplayServerTryLoadLib(aLibsX11, RT_ELEMENTS(aLibsX11), &hX11);
|
---|
167 | if (RT_SUCCESS(rc))
|
---|
168 | {
|
---|
169 | void * (*pfnOpenDisplay)(const char *) = NULL;
|
---|
170 | GET_SYMBOL(hX11, "XOpenDisplay", pfnOpenDisplay);
|
---|
171 | int (*pfnCloseDisplay)(void *) = NULL;
|
---|
172 | GET_SYMBOL(hX11, "XCloseDisplay", pfnCloseDisplay);
|
---|
173 | if (RT_SUCCESS(rc))
|
---|
174 | {
|
---|
175 | AssertPtrReturn(pfnOpenDisplay, VBGHDISPLAYSERVERTYPE_NONE);
|
---|
176 | AssertPtrReturn(pfnCloseDisplay, VBGHDISPLAYSERVERTYPE_NONE);
|
---|
177 | void *pDisplay = pfnOpenDisplay(NULL);
|
---|
178 | if (pDisplay)
|
---|
179 | {
|
---|
180 | fHasX = true;
|
---|
181 | pfnCloseDisplay(pDisplay);
|
---|
182 | }
|
---|
183 | else
|
---|
184 | LogRel2(("Opening X display failed\n"));
|
---|
185 | }
|
---|
186 |
|
---|
187 | RTLdrClose(hX11);
|
---|
188 | }
|
---|
189 |
|
---|
190 | #undef GET_SYMBOL
|
---|
191 |
|
---|
192 | /* If both wayland and X11 display can be connected then we should have XWayland: */
|
---|
193 | VBGHDISPLAYSERVERTYPE retSessionType = VBGHDISPLAYSERVERTYPE_NONE;
|
---|
194 | if (fHasWayland && fHasX)
|
---|
195 | retSessionType = VBGHDISPLAYSERVERTYPE_XWAYLAND;
|
---|
196 | else if (fHasWayland)
|
---|
197 | retSessionType = VBGHDISPLAYSERVERTYPE_PURE_WAYLAND;
|
---|
198 | else if (fHasX)
|
---|
199 | retSessionType = VBGHDISPLAYSERVERTYPE_X11;
|
---|
200 |
|
---|
201 | LogRel2(("Detected via connection: %s\n", VBGHDisplayServerTypeToStr(retSessionType)));
|
---|
202 |
|
---|
203 | return retSessionType;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Returns true if @a enmType is indicating running X.
|
---|
208 | *
|
---|
209 | * @returns \c true if @a enmType is running X, \c false if not.
|
---|
210 | * @param enmType Type to check.
|
---|
211 | */
|
---|
212 | bool VBGHDisplayServerTypeIsXAvailable(VBGHDISPLAYSERVERTYPE enmType)
|
---|
213 | {
|
---|
214 | return enmType == VBGHDISPLAYSERVERTYPE_XWAYLAND
|
---|
215 | || enmType == VBGHDISPLAYSERVERTYPE_X11;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Returns true if @a enmType is indicating running Wayland.
|
---|
220 | *
|
---|
221 | * @returns \c true if @a enmType is running Wayland, \c false if not.
|
---|
222 | * @param enmType Type to check.
|
---|
223 | */
|
---|
224 | bool VBGHDisplayServerTypeIsWaylandAvailable(VBGHDISPLAYSERVERTYPE enmType)
|
---|
225 | {
|
---|
226 | return enmType == VBGHDISPLAYSERVERTYPE_XWAYLAND
|
---|
227 | || enmType == VBGHDISPLAYSERVERTYPE_PURE_WAYLAND;
|
---|
228 | }
|
---|
229 |
|
---|