view lcdemu/xrm.c @ 40:7ecb70b0ac36

c139explore: vibe command added that works like buz
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 26 Oct 2016 01:59:32 +0000
parents e7502631a0f9
children
line wrap: on
line source

/*
 * LCDemu based on HECterm by the same author
 * Xrm functions
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include "globals.h"

static char appdefaults_pathname[] =
		"/usr/local/share/freecalypso/lcdemu-defaults";

load_resources()
{
	xrmquark_topclass = XrmStringToQuark("LCDemu");
	xrmquark_topinstance = XrmStringToQuark(proginstancename);
	xrmdb_defaults = XrmGetFileDatabase(appdefaults_pathname);
	xrmdb_displayres =
		XrmGetStringDatabase(XResourceManagerString(mydisplay));
}

/*
 * The following function looks up a resource in all of our databases
 * and returns a pointer (char *) to the value in a malloced buffer that
 * can be freed when it is no longer needed.  My reading of X11R4
 * documentation indicates that resource values returned from Xrm functions
 * are not necessarily NUL-terminated (no claim is made that they are
 * and XrmValue structure has a size field), which is why I copy to
 * my own buffer and NUL-terminate it there.
 *
 * Returns NULL pointer if not found in any of the databases.
 */
char *
xrm_lookup(instquarks, classquarks)
	XrmQuark *instquarks, *classquarks;
{
	XrmRepresentation reptype;
	XrmValue value;
	register char *buf;

	if (XrmQGetResource(xrmdb_cmdline, instquarks, classquarks, &reptype,
	    &value))
		goto found;
	if (XrmQGetResource(xrmdb_displayres, instquarks, classquarks, &reptype,
	    &value))
		goto found;
	if (XrmQGetResource(xrmdb_defaults, instquarks, classquarks, &reptype,
	    &value))
		goto found;
	return(NULL);
found:	buf = malloc(value.size + 1);
	if (!buf) {
		perror("malloc");
		exit(1);
	}
	bcopy(value.addr, buf, value.size);
	buf[value.size] = '\0';
	return(buf);
}

parse_boolean_resource(str)
	register char *str;
{
	if (!strcasecmp(str, "on") || !strcasecmp(str, "true") ||
	    !strcasecmp(str, "yes"))
		return(1);
	if (!strcasecmp(str, "off") || !strcasecmp(str, "false") ||
	    !strcasecmp(str, "no"))
		return(0);
	return(atoi(str));
}

get_boolean_resource(resource, def)
	char *resource;
	int def;
{
	XrmQuark instquarks[3], classquarks[3];
	register char *cp;
	register int i;

	instquarks[0] = xrmquark_topinstance;
	classquarks[0] = xrmquark_topclass;
	classquarks[1] = instquarks[1] = XrmStringToQuark(resource);
	instquarks[2] = classquarks[2] = NULLQUARK;
	cp = xrm_lookup(instquarks, classquarks);
	if (cp) {
		i = parse_boolean_resource(cp);
		free(cp);
	} else
		i = def;
	return(i);
}