Replace X11 display number with a display string

As far as possible, use of the X11 display number is kept to
X11-specific routines. This is to make it easier to restructure
the code to add non-X11 display support.
This commit is contained in:
matt335672
2026-02-03 15:36:20 +00:00
parent d56408a891
commit c4727ad8f3
40 changed files with 726 additions and 527 deletions
+66 -2
View File
@@ -162,8 +162,8 @@ g_text2bool(const char *s)
}
/*****************************************************************************/
int
g_get_display_num_from_display(const char *display_text)
static int
get_display_num_from_display(const char *display_text)
{
int rv = -1;
const char *p;
@@ -191,6 +191,70 @@ g_get_display_num_from_display(const char *display_text)
return rv;
}
/*****************************************************************************/
int
g_get_display_string_from_x11_display(int display_num,
char buff[], unsigned int bufflen)
{
unsigned int res = g_snprintf(buff, bufflen, "X11-%d", display_num);
return (res >= bufflen) ? -1 : 0;
}
/*****************************************************************************/
int
g_get_x11_display_from_display_string(const char *display_str)
{
int result = -1;
if (display_str != NULL)
{
if (display_str[0] == 'X' && display_str[1] == '1' &&
display_str[2] == '1' && display_str[3] == '-' &&
isdigit(display_str[4]))
{
result = g_atoi(display_str + 4);
}
}
return result;
}
/*****************************************************************************/
int
g_get_display_string(char buff[], unsigned int bufflen)
{
const char *str;
const char *p = NULL;
int rv = 0;
if ((str = g_getenv("WAYLAND_DISPLAY")) != NULL)
{
// Return the unqualified part of the name
p = strrchr(str, '/');
p = (p != NULL) ? (p + 1) : str;
strlcpy(buff, p, bufflen);
}
else if ((str = g_getenv("DISPLAY")) != NULL)
{
int n = get_display_num_from_display(str);
if (n >= 0)
{
g_snprintf(buff, bufflen, "X11-%d", n);
}
else
{
rv = g_get_display_string_from_x11_display(n, buff, bufflen);
}
}
else
{
rv = -1;
}
return rv;
}
/*****************************************************************************/
/* returns length of text */
int
+28 -5
View File
@@ -190,14 +190,37 @@ char *
g_bytes_to_hexdump(const char *src, int len);
/**
* Extracts the display number from an X11 display string
* Converts an X11 display number to a display string
*
* @param display_text Display string (i.e. g_getenv("DISPLAY"))
*
* @result <0 if the string could not be parsed, or >=0 for a display number
* @param display_num X11 display unmber
* @param buff Buffer for result
* @param bufflen size of above
* @return != 0 if the result doesn't fit
*/
int
g_get_display_num_from_display(const char *display_text);
g_get_display_string_from_x11_display(int display_num,
char buff[], unsigned int bufflen);
/**
* Converts a display string to an X11 display number
*
* @param display_str Display string
* @return X11 display number, or -1 if the display string isn't X11
*/
int
g_get_x11_display_from_display_string(const char *display_str);
/**
* Extracts the display string from either DISPLAY or WAYLAND_DISPLAY
*
* @param buff Buffer for result
* @param bufflen Length of above buffer
*
* Returned buffer will be (e.g.) "X11-10" for X11 or "wayland-1" for Wayland
*
* @result != 0 if the string could not be found or parsed
*/
int
g_get_display_string(char buff[], unsigned int bufflen);
/**
* Converts a bitmask into a string for output purposes
+6
View File
@@ -49,6 +49,12 @@
**/
#define MAX_XRDP_INSTANCE_NAMELEN 80
/**
* Max length of buffer containing a Wayland or X11 display
* name
**/
#define MAX_DISPLAY_NAME_SIZE 32
/**
* Size of buffer including terminator for a socket description, as
* returned by g_sck_get_peer_description()
+7 -7
View File
@@ -45,13 +45,13 @@
#define SCP_LISTEN_PORT_BASE_STR "sesman.socket"
/* names of socket files within XRDP_SOCKET_PATH, qualified by
* display number */
#define XRDP_CHANSRV_BASE_STR "xrdp_chansrv_socket_%d"
#define CHANSRV_PORT_OUT_BASE_STR "xrdp_chansrv_audio_out_socket_%d"
#define CHANSRV_PORT_IN_BASE_STR "xrdp_chansrv_audio_in_socket_%d"
#define CHANSRV_API_BASE_STR "xrdpapi_%d"
#define XRDP_X11RDP_BASE_STR "xrdp_display_%d"
#define XRDP_DISCONNECT_BASE_STR "xrdp_disconnect_display_%d"
* display name */
#define XRDP_CHANSRV_BASE_STR "xrdp_chansrv_socket_%s"
#define CHANSRV_PORT_OUT_BASE_STR "xrdp_chansrv_audio_out_socket_%s"
#define CHANSRV_PORT_IN_BASE_STR "xrdp_chansrv_audio_in_socket_%s"
#define CHANSRV_API_BASE_STR "xrdpapi_%s"
#define XRDP_X11RDP_BASE_STR "xrdp_display_%s"
#define XRDP_DISCONNECT_BASE_STR "xrdp_disconnect_display_%s"
/* fullpath declarations */
#define XRDP_CHANSRV_STR XRDP_SOCKET_PATH "/" XRDP_CHANSRV_BASE_STR