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