/* @(#)Resltn.c	2.1.2.2 96/07/09 */

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

#include <stdio.h>
#define numfuncs	4
#define space		' '
#define tab		'\t'
#include "WZTools.h"

static
	int dpx = 0, dpy = 0, Wdpx = 0, Wdpy = 0, Xdpx = 0, Xdpy = 0;
extern void
	InitFunc(), DpiX(), DpiY(), WDpiX(), WDpiY(), ExitFunc();
void (*initfunc)() = InitFunc;

ROUT    rout = {
        numfuncs, ExitFunc,
        {
                { DpiX, "\004DpiX", 0 },
		{ DpiY, "\004DpiY", 0 },
		{ WDpiX, "\005WDpiX", 0 },
		{ WDpiY, "\005WDpiY", 0 },
        }
};

void
InitFunc(pret, parg)
PVAL	pret, parg;
{
	Display *display;
	char *display_name = NULL;
	int i, c, ln = 0, screen;
	char s[135], cmnd[256],
		fname[256], path[256];
	FILE *fopen(), *fp;

	sprintf(path, "/tmp/%s", getlogin());
		/* The Variable path will temporarly play the role of a file name. */
	sprintf(fname, "/usr/lib/X11/app-defaults/Wingz");
	if (fp = fopen(fname, "r")) {
		fclose(fp);
		sprintf(cmnd, "cat %s | grep Wingz | grep dpix > %s", fname, path);
		system(cmnd);
		fp = fopen(path, "r");
		i = 1;
		while ((c = getc(fp)) != EOF && c != '\n' && i < 80) {
			s[i] = c;
			++i;
		}
		fclose(fp);
		unlink(path);
		s[i] = '\0';
		ln = i ;
		i = 1;
		if (ln > 1) {
			while (s[i] == space || s[i] == tab)
				++i;
			if (s[i] != '!' && s[i] != '#'){
				while (s[i] < '0' || s[i] > '9')
					++i;
				while (s[i] >= '0' && s[i] <= '9') {
					Wdpx = Wdpx * 10 + s[i] - '0';
					++i;
				}
			}
		}
		sprintf(cmnd, "cat %s | grep Wingz | grep dpiy > %s", fname, path);
		system(cmnd);
		fp = fopen(path, "r");
		i = 1;
		while ((c = getc(fp)) != EOF && c != '\n' && i < 80) {
			s[i] = c;
			++i;
		}
		fclose(fp);
		unlink(path);
		s[i] = '\0';
		ln = i ;
		i = 1;
		if (ln > 1) {
			while (s[i] == space || s[i] == tab)
				++i;
			if (s[i] != '!' && s[i] != '#'){
				while (s[i] < '0' || s[i] > '9')
					++i;
				while (s[i] >= '0' && s[i] <= '9') {
					Wdpy = Wdpy * 10 + s[i] - '0';
					++i;
				}
			}
		}
	}
        if((display=XOpenDisplay(display_name)) == NULL)
        {
                (void) fprintf(stderr,
                        "BasicWin: cannot connect to X server %s\n",
                        XDisplayName(display_name));
	}
	screen = DefaultScreen(display);
	dpx = DisplayWidth(display,screen) / (DisplayWidthMM(display,screen) / 25.4 );
	dpy = DisplayHeight(display,screen) / (DisplayHeightMM(display,screen) / 25.4 );
	sprintf(fname, "/tmp/%s", getlogin());
	sprintf(cmnd, "xrdb -query | grep Wingz | grep dpix > %s", fname);
	system(cmnd);
	fp = fopen(fname,"r");
	i = 1;
	while ((c = getc(fp)) != EOF && c != '\n' && i < 80) {
		s[i] = c;
		++i;
	}
	fclose(fp);
	unlink(fname);
	ln =i;
	i = 1;
	while (s[i] < '0' || s[i] > '9')
		++i;
	while (s[i] >= '0' && s[i] <= '9' && i < ln) {
		Xdpx = Xdpx * 10 + s[i] - '0';
		++i;
	}
	sprintf(fname, "/tmp/%s", getlogin());
	sprintf(cmnd, "xrdb -query | grep Wingz | grep dpiy > %s", fname);
	system(cmnd);
	fp = fopen(fname,"r");
	i = 1;
	while ((c = getc(fp)) != EOF && c != '\n' && i < 80) {
		s[i] = c;
		++i;
	}
	fclose(fp);
	unlink(fname);
	ln =i;
	i = 1;
	while (s[i] < '0' || s[i] > '9')
		++i;
	while (s[i] >= '0' && s[i] <= '9' && i < ln) {
		Xdpy = Xdpy * 10 + s[i] - '0';
		++i;
	}
	if (Xdpx != 0)
		Wdpx = Xdpx;
	if (Xdpy != 0)
		Wdpy = Xdpy;
}

void
DpiX(pret, parg)
PVAL	pret, parg;
{
	pret->flag = NUMERIC;
	pret->val.numeric = dpx;
	return;
}

void
DpiY(pret, parg)
PVAL	pret, parg;
{
	pret->flag = NUMERIC;
	pret->val.numeric = dpy;
	return;
}

void
WDpiX(pret, parg)
PVAL	pret, parg;
{
	pret->flag = NUMERIC;
	pret->val.numeric = Wdpx;
	return;
}

void
WDpiY(pret, parg)
PVAL	pret, parg;
{
	pret->flag = NUMERIC;
	pret->val.numeric = Wdpy;
	return;
}

void
ExitFunc(pret, parg)
PVAL	pret, parg;
{
}
