diff lcdemu/xrm.c @ 903:312778104f54

lcdemu started, compiles and runs w/o actual functionality
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 07 Sep 2015 08:34:37 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lcdemu/xrm.c	Mon Sep 07 08:34:37 2015 +0000
@@ -0,0 +1,97 @@
+/*
+ * 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);
+}