robotgo/base/xdisplay_c.h
Arvid E. Picciani 47245e8862 respect DISPLAY on linux
Signed-off-by: Arvid E. Picciani <aep@exys.org>
2017-04-18 14:33:04 +02:00

62 lines
1.2 KiB
C

#include "xdisplay.h"
#include <stdio.h> /* For fputs() */
#include <stdlib.h> /* For atexit() */
static Display *mainDisplay = NULL;
static int registered = 0;
static char *displayName = NULL;
static int hasDisplayNameChanged = 0;
Display *XGetMainDisplay(void)
{
/* Close the display if displayName has changed */
if (hasDisplayNameChanged) {
XCloseMainDisplay();
hasDisplayNameChanged = 0;
}
if (mainDisplay == NULL) {
/* First try the user set displayName */
mainDisplay = XOpenDisplay(displayName);
/* Then try using environment variable DISPLAY */
if (mainDisplay == NULL) {
mainDisplay = XOpenDisplay(NULL);
}
/* Fall back to the most likely :0.0*/
if (mainDisplay == NULL) {
mainDisplay = XOpenDisplay(":0.0");
}
if (mainDisplay == NULL) {
fputs("Could not open main display\n", stderr);
} else if (!registered) {
atexit(&XCloseMainDisplay);
registered = 1;
}
}
return mainDisplay;
}
void XCloseMainDisplay(void)
{
if (mainDisplay != NULL) {
XCloseDisplay(mainDisplay);
mainDisplay = NULL;
}
}
void setXDisplay(char *name)
{
displayName = strdup(name);
hasDisplayNameChanged = 1;
}
char *getXDisplay(void)
{
return displayName;
}