11 #include <sys/types.h>
13 #define LISTSIZE(x) (sizeof(x)/sizeof(*x)-1)
14 #define DPYNAME_MAXLEN 255
17 static jmp_buf xopendpy
;
18 unsigned int timeout
= 2;
19 unsigned int verbose
= 0;
20 unsigned int found_a_valid_display
= 0;
23 void alarm_handler(int sig
)
25 siglongjmp(xopendpy
, 1);
28 unsigned int is_valid_xdisplay(char* dpyname
)
32 if(verbose
) fprintf(stderr
, "Opening X display %s\n", dpyname
);
35 if(!sigsetjmp(xopendpy
, 1))
37 dpy
= XOpenDisplay(dpyname
);
47 found_a_valid_display
= 1;
48 printf("%s\n", dpyname
== NULL
? getenv("DISPLAY") : dpyname
);
54 int main(int argc
, char** argv
)
57 char dpyname
[DPYNAME_MAXLEN
];
60 int search_abstract
= 1;
61 int search_sockets
= 1;
62 int search_inet_max
= 30;
70 while((c
= getopt(argc
, argv
, "hvASt:x:")) != -1)
75 timeout
= atoi(optarg
);
87 search_inet_max
= atoi(optarg
);
90 printf("Usage: xdpys [-h|-v|-A|-S] [-t <sec>] [-x <max>] [display [display [...]]]\n"
92 " -v verbose output to stderr\n"
93 " -A don't test abstract unix sockets\n"
94 " -S don't test unix socket files\n"
95 " -t <n> timeout for individual XDisplay open calls (%d)\n"
96 " -x <n> search this many X.org display in INET namespace (%d)\n",
108 signal(SIGALRM
, alarm_handler
);
114 fh
= fopen("/proc/net/unix", "r");
119 fgets(socksbuf
, 91, fh
);
120 // e5d1fa40: 00000002 00000000 00010000 0001 01 187671 @/tmp/.X11-unix/X1003
121 // e697ba80: 00000002 00000000 00010000 0001 01 20247 @/tmp/.X11-unix/X1000
122 if(sscanf(socksbuf
, "%*[^ ] %*[^ ] %*[^ ] %*[^ ] %*[^ ] 01 %*[^ ] @/tmp/.X11-unix/X%u", &u
) == 1)
124 snprintf(dpyname
, DPYNAME_MAXLEN
, ":%u", u
);
125 is_valid_xdisplay(dpyname
);
127 while(socksbuf
[strlen(socksbuf
)-1] != '\n')
129 fgets(socksbuf
, 91, fh
);
138 dh
= opendir("/tmp/.X11-unix");
139 while((dent
= readdir(dh
)) != NULL
)
141 if(dent
->d_type
== DT_SOCK
&& sscanf(dent
->d_name
, "X%u", &u
) == 1)
143 snprintf(dpyname
, DPYNAME_MAXLEN
, ":%u", u
);
144 is_valid_xdisplay(dpyname
);
150 for(num
= 0; num
< search_inet_max
; num
++)
152 snprintf(dpyname
, DPYNAME_MAXLEN
, "localhost:%d", num
);
153 is_valid_xdisplay(dpyname
);
160 is_valid_xdisplay(argv
[optind
]);
165 return found_a_valid_display
? EXIT_SUCCESS
: EXIT_FAILURE
;