optimize x11 display code

This commit is contained in:
vcaesar 2022-01-01 14:39:55 -04:00
parent b2702100cb
commit fe42b2a74c

View File

@ -4,11 +4,11 @@
static Display *mainDisplay = NULL; static Display *mainDisplay = NULL;
static int registered = 0; static int registered = 0;
static char *displayName = NULL; static char *displayName = NULL;
static int hasDisplayNameChanged = 0; static int hasDisplayNameChanged = 0;
Display *XGetMainDisplay(void) Display *XGetMainDisplay(void) {
{
/* Close the display if displayName has changed */ /* Close the display if displayName has changed */
if (hasDisplayNameChanged) { if (hasDisplayNameChanged) {
XCloseMainDisplay(); XCloseMainDisplay();
@ -20,7 +20,7 @@ Display *XGetMainDisplay(void)
mainDisplay = XOpenDisplay(displayName); mainDisplay = XOpenDisplay(displayName);
/* Then try using environment variable DISPLAY */ /* Then try using environment variable DISPLAY */
if (mainDisplay == NULL) { if (mainDisplay == NULL && displayName != NULL) {
mainDisplay = XOpenDisplay(NULL); mainDisplay = XOpenDisplay(NULL);
} }
@ -40,22 +40,19 @@ Display *XGetMainDisplay(void)
return mainDisplay; return mainDisplay;
} }
void XCloseMainDisplay(void) void XCloseMainDisplay(void) {
{
if (mainDisplay != NULL) { if (mainDisplay != NULL) {
XCloseDisplay(mainDisplay); XCloseDisplay(mainDisplay);
mainDisplay = NULL; mainDisplay = NULL;
} }
} }
void setXDisplay(char *name) void setXDisplay(char *name) {
{
displayName = strdup(name); displayName = strdup(name);
hasDisplayNameChanged = 1; hasDisplayNameChanged = 1;
} }
char *getXDisplay(void) char *getXDisplay(void) {
{
return displayName; return displayName;
} }