Re: gkrellmd question

"Benjamin R. Haskell" <[email protected]> Fri, 15 Jan 2010 08:19:23 -0500 (EST)
Newsgroups gmane.comp.gnome.apps.gkrellm
Message-ID <[email protected]>
On Fri, 15 Jan 2010, Rio wrote:

> hehe after rebooting, even in local startup file it doesn't work because gkrellmd is 
> started before X is initialized i think, and xhost doesnt work there since it cannot find 
> a screen yet... i had to move gkrellmd startup to kde's Autostart directory which started 
> it with proper visibility. only caveat is the client gives an error it cannot find the 
> server but i hit try again and then it works. probably due to desktop inits starting 
> before the autostart script.

I'm teaching myself X11 programming right now.  The attached program 
will busy-loop until an X11 display is available.

Compile:

gcc $(pkg-config x11 --cflags) -o x11_is_active x11_is_active.c $(pkg-config x11 --libs)

Run:

Simple: x11_is_active && dosomething-with-X

All options:

x11_is_active -delay 1 -maxtry 10 -display :0 -v
 -delay - specifies length of 'sleep' between failed attempts
 -maxtry - maximum tries to connect
 -display - name of X11 display (if not specified, uses X11 lib defaults)
 -v - for verbose (without '-v' there's no output at all)

Maybe useful.  Maybe not.  Maybe it already exists.

Best,
Ben
x11_is_active.c (text/x-c, 1.5 KB)
#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_DISPLAY 1024

void arg_error() {
	fprintf(stderr,"Options: [-display DISPLAY] [-delay N] [-maxtry N] [-v]\n");
	exit(2);
}

void print_display(char *display_name) {
	if (!display_name) printf("default ");
	printf("display");
	if (display_name) printf("=%s", display_name);
}

int main(int argc, char **argv) {
	Display *disp;
	char *arg;
	int i, tries = 0;

	char *display_name = NULL;
	int delay = 1, max_try = 0, verbose = 0;

	for (i = 1; i < argc; i++) {
		arg = argv[i];
		if (!strcmp(arg,"-display")) {
			if (i+1 >= argc) arg_error();
			display_name = (char *)malloc(MAX_DISPLAY * sizeof(char));
			strncpy(display_name, argv[++i], MAX_DISPLAY);
			display_name[MAX_DISPLAY-1] = '\0';
		} else if (!strcmp(arg,"-delay")) {
			if (i+1 >= argc) arg_error();
			delay = atoi(argv[++i]);
		} else if (!strcmp(arg,"-maxtry")) {
			if (i+1 >= argc) arg_error();
			max_try = atoi(argv[++i]);
		} else if (!strcmp(arg,"-v")) {
			verbose++;
		} else arg_error();
	}

	while (1) {
		tries++;
		disp = XOpenDisplay(display_name);
		if (disp) {
			if (verbose) {
				printf("Opened ");
				print_display(display_name);
				printf("\n");
			}
			XCloseDisplay(disp);
			break;
		}
		if (max_try && tries >= max_try) {
			if (verbose) {
				printf("Exceeded -maxtry %d while opening ", max_try);
				print_display(display_name);
				printf("\n");
			}
			exit(1);
		}
		sleep(delay);
	}

	return 0;
}