[comp.sources.x] v04i093: xdb - "X Display Bitmap", Part01/01

argv@island.uu.net (Dan Heller) (08/18/89)

Submitted-by: uunet!riacs.edu!davy
Posting-number: Volume 4, Issue 93
Archive-name: xdb/part01




xdb is a trivial hack I put together to display (monochrome) bitmaps
in an X window.  I didn't like xsetroot's tiling when displaying small
bitmaps, I just wanted a single copy of the picture.  So, I came up
with this.  It just makes a window and sets the background pixmap to
the bitmap.  Click the mouse in the window to make it go away.

Dave Curry

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	xdb
# This archive created: Tue Aug 15 15:50:16 1989
export PATH; PATH=/bin:$PATH
if test ! -d 'xdb'
then
	echo shar: creating directory "'xdb'"
	mkdir 'xdb'
fi
echo shar: entering directory "'xdb'"
cd 'xdb'
echo shar: extracting "'xdb.c'" '(4750 characters)'
if test -f 'xdb.c'
then
	echo shar: will not over-write existing file "'xdb.c'"
else
sed 's/^X//' << \SHAR_EOF > 'xdb.c'
X/*
X * xdb - display an X bitmap in a window
X *
X * Much of this code is ripped off from xsetroot.  I wanted a program
X * to display a bitmap in a window instead of on the root.  There's
X * not much to this - you can specify a geometry, display, and reverse
X * video.  A mouse click in the window exits the program.
X *
X * Usage: xdb [-display display] [-geometry geometry] [-rv] bitmapfile
X *
X * David A. Curry
X * davy@riacs.edu
X */
X#include <X11/Xlib.h>
X#include <X11/Xutil.h>
X#include <X11/Xatom.h>
X#include <stdio.h>
X
Xchar	*pname = NULL;
Xchar	*display = NULL;
Xchar	*bitmapfile = NULL;
X
XDisplay	*dpy;
X
XWindow	root;
XPixmap	pixmap;
X
Xint	screen;
Xint	reverse = 0;
X
Xmain(argc, argv)
Xchar **argv;
Xint argc;
X{
X	char *s;
X	int mask;
X	Window win;
X	XEvent event;
X	Pixmap bitmap;
X	char *rindex();
X	Pixmap ReadBitmapFile();
X	unsigned int x, y, width, height;
X
X	pname = *argv;
X	x = y = 0;
X
X	/*
X	 * Process arguments.
X	 */
X	while (--argc) {
X		argv++;
X
X		/*
X		 * Display name.
X		 */
X		if (!strcmp(*argv, "-display") || !strcmp(*argv, "-d")) {
X			if (--argc <= 0)
X				usage();
X
X			display = *++argv;
X			continue;
X		}
X
X		/*
X		 * Geometry spec.
X		 */
X		if (!strcmp(*argv, "-geometry") || !strcmp(*argv, "-g")) {
X			if (--argc <= 0)
X				usage();
X
X			/*
X			 * Parse the geometry.
X			 */
X			mask = XParseGeometry(*++argv, &x, &y, &width,
X					      &height);
X
X			/*
X			 * If they didn't give a complete window
X			 * position, forget it.
X			 */
X			if ((mask & (XValue|YValue)) != (XValue|YValue))
X				x = y = 0;
X		}
X
X		/*
X		 * Reverse video.
X		 */
X		if (!strcmp(*argv, "-reverse") || !strcmp(*argv, "-rv")) {
X			reverse = 1;
X			continue;
X		}
X
X		bitmapfile = *argv;
X	}
X
X	/*
X	 * Gotta have a bitmap file.
X	 */
X	if (bitmapfile == NULL)
X		usage();
X
X	/*
X	 * Open the display.
X	 */
X	if ((dpy = XOpenDisplay(display)) == NULL) {
X		fprintf(stderr, "%s: uable to open display \"%s\".\n",
X			pname, XDisplayName(display));
X		exit(1);
X	}
X
X	/*
X	 * Get screen and root window.
X	 */
X	screen = DefaultScreen(dpy);
X	root = RootWindow(dpy, screen);
X
X	/*
X	 * Get the basename of the bitmap.
X	 */
X	if ((s = rindex(bitmapfile, '/')) == NULL)
X		s = bitmapfile;
X	else
X		s++;
X
X	/*
X	 * Read the bitmap file.
X	 */
X	bitmap = ReadBitmapFile(bitmapfile, &width, &height);
X
X	/*
X	 * Create a window to put it into.
X	 */
X	win = XCreateSimpleWindow(dpy, root, x, y, width, height,
X				  2, None, BlackPixel(dpy, screen));
X
X	/*
X	 * Select Button press events, and set the
X	 * name of the window.
X	 */
X	XSelectInput(dpy, win, ButtonPressMask);
X	XStoreName(dpy, win, s);
X
X	/*
X	 * Set the window's background.
X	 */
X	SetBackgroundToBitmap(win, bitmap, width, height);
X
X	/*
X	 * Map the window.
X	 */
X	XMapWindow(dpy, win);
X	XFlush(dpy);
X
X	/*
X	 * Wait for a button press, and exit.
X	 */
X	XWindowEvent(dpy, win, ButtonPressMask, &event);
X	XDestroyWindow(dpy, win);
X	XFreePixmap(dpy, pixmap);
X	XCloseDisplay(dpy);
X	exit(0);
X}
X
X/*
X * ReadBitmapFile - read in a bitmap file.  Ripped off from xsetroot.
X */
XPixmap ReadBitmapFile(filename, width, height)
Xunsigned int *width, *height;
Xchar *filename;
X{
X	int xh, yh;
X	int status;
X	Pixmap bitmap;
X
X	/*
X	 * Read the file.
X	 */
X	status = XReadBitmapFile(dpy, root, filename, width, height, &bitmap,
X				 &xh, &yh);
X
X	/*
X	 * Figure out what happened.
X	 */
X	switch (status) {
X	case BitmapSuccess:
X		return(bitmap);
X	case BitmapOpenFailed:
X		fprintf(stderr, "%s: cannot open \"%s\".\n", pname, filename);
X		exit(1);
X	case BitmapFileInvalid:
X		fprintf(stderr, "%s: \"%s\" is not a bitmap file.\n", pname,
X			filename);
X		exit(1);
X	default:
X		fprintf(stderr, "%s: insufficient memory for bitmap.\n",
X			pname);
X		exit(1);
X	}
X}
X
X/*
X * SetBackgroundToBitmap - set the bitmap as a background.  Ripped off
X *			   from xsetroot.
X */
XSetBackgroundToBitmap(win, bitmap, width, height)
Xunsigned int width, height;
XPixmap bitmap;
XWindow win;
X{
X	GC gc;
X	XGCValues gc_init;
X
X	/*
X	 * Set foreground/background colors.
X	 */
X	if (reverse) {
X		gc_init.background = BlackPixel(dpy, screen);
X		gc_init.foreground = WhitePixel(dpy, screen);
X	}
X	else {
X		gc_init.foreground = BlackPixel(dpy, screen);
X		gc_init.background = WhitePixel(dpy, screen);
X	}
X
X	/*
X	 * Create the GC and the pixmap.
X	 */
X	gc = XCreateGC(dpy, win, GCForeground|GCBackground, &gc_init);
X	pixmap = XCreatePixmap(dpy, win, width, height,
X			    (unsigned int) DefaultDepth(dpy, screen));
X
X	/*
X	 * Copy the bitmap to the pixmap.
X	 */
X	XCopyPlane(dpy, bitmap, pixmap, gc, 0, 0, width, height, 0, 0,
X		   (unsigned long) 1);
X
X	/*
X	 * Set the background.
X	 */
X	XSetWindowBackgroundPixmap(dpy, win, pixmap);
X	XFreePixmap(dpy, bitmap);
X	XFreeGC(dpy, gc);
X}
X
X/*
X * usage - print usage message.
X */
Xusage()
X{
X	fprintf(stderr, "%s [-display display] [-geometry geometry] ", pname);
X	fprintf(stderr, "[-rv] bitmapfile\n");
X	exit(1);
X}
SHAR_EOF
if test 4750 -ne "`wc -c < 'xdb.c'`"
then
	echo shar: error transmitting "'xdb.c'" '(should have been 4750 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'xdb.man'" '(608 characters)'
if test -f 'xdb.man'
then
	echo shar: will not over-write existing file "'xdb.man'"
else
sed 's/^X//' << \SHAR_EOF > 'xdb.man'
X.TH XDB 1 "10 August 1989"
X.SH NAME
Xxdb \- display X bitmap in a window
X.SH SYNOPSIS
X.B xdb
X[
X.B \-display
X.I display
X] [
X.B \-geometry
X.I geometry
X] [
X.B \-rv
X]
X.I bitmapfile
X.SH DESCRIPTION
X.PP
X.B Xdb
Xdisplays an X11 bitmap file in a window.
XIt does this by creating a window whose background pixmap contains the
Xbitmap.
XA geometry may be specified,
Xbut only the window positioning part is used.
XThe window will always be the width and height of the bitmap.
X.PP
XTo exit
X.BR xdb ,
Xpress any mouse button in the window.
X.SH SEE ALSO
X.BR bitmap (1),
X.BR xsetroot (1)
X.SH AUTHOR
XDavid A. Curry, davy@riacs.edu
SHAR_EOF
if test 608 -ne "`wc -c < 'xdb.man'`"
then
	echo shar: error transmitting "'xdb.man'" '(should have been 608 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'Imakefile'" '(101 characters)'
if test -f 'Imakefile'
then
	echo shar: will not over-write existing file "'Imakefile'"
else
sed 's/^X//' << \SHAR_EOF > 'Imakefile'
XLOCAL_LIBRARIES = $(XLIB)
X           SRCS = xdb.c
X           OBJS = xdb.o
X
XComplexProgramTarget(xdb)
SHAR_EOF
if test 101 -ne "`wc -c < 'Imakefile'`"
then
	echo shar: error transmitting "'Imakefile'" '(should have been 101 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'Makefile'" '(7181 characters)'
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile'
X# Makefile generated by imake - do not edit!
X# $XConsortium: imake.c,v 1.37 88/10/08 20:08:30 jim Exp $
X#
X# The cpp used on this machine replaces all newlines and multiple tabs and
X# spaces in a macro expansion with a single space.  Imake tries to compensate
X# for this, but is not always successful.
X#
X
X###########################################################################
X# X Window System Makefile generated from template file Imake.tmpl
X# $XConsortium: Imake.tmpl,v 1.91 88/10/23 22:37:10 jim Exp $
X#
X# Do not change the body of the imake template file.  Server-specific
X# parameters may be set in the appropriate .macros file; site-specific
X# parameters (but shared by all servers) may be set in site.def.  If you
X# make any changes, you'll need to rebuild the makefiles using
X# "make World" (at best) or "make Makefile; make Makefiles" (at least) in
X# the top level directory.
X#
X# If your C preprocessor doesn't define any unique symbols, you'll need
X# to set BOOTSTRAPCFLAGS when rebuilding imake (usually when doing
X# "make Makefile", "make Makefiles", or "make World").
X#
X# If you absolutely can't get imake to work, you'll need to set the
X# variables at the top of each Makefile as well as the dependencies at the
X# bottom (makedepend will do this automatically).
X#
X
X###########################################################################
X# platform-specific configuration parameters - edit Sun.macros to change
X
X# platform:  $XConsortium: Sun.macros,v 1.52 88/10/23 11:00:55 jim Exp $
X# operating system:  SunOS 4.0
X
XBOOTSTRAPCFLAGS =
X             AS = as
X             CC = cc
X            CPP = /lib/cpp
X             LD = ld
X           LINT = lint
X        INSTALL = install
X           TAGS = ctags
X             RM = rm -f
X             MV = mv
X             LN = ln -s
X         RANLIB = ranlib
XRANLIBINSTFLAGS = -t
X             AR = ar clq
X             LS = ls
X       LINTOPTS = -axz
X    LINTLIBFLAG = -C
X           MAKE = make
XSTD_CPP_DEFINES =
X    STD_DEFINES =
X
X###########################################################################
X# site-specific configuration parameters - edit site.def to change
X
X# site:  $XConsortium: site.def,v 1.16 88/10/12 10:30:24 jim Exp $
X
X###########################################################################
X# definitions common to all Makefiles - do not edit
X
X          SHELL = /bin/sh
X
X        DESTDIR =
X      USRLIBDIR = $(DESTDIR)/usr/local/lib/X.V11R3
X         BINDIR = $(DESTDIR)/usr/local/bin/X.V11R3
X         INCDIR = $(DESTDIR)/usr/local/include/X.V11R3
X         ADMDIR = $(DESTDIR)/usr/local/adm/X.V11R3
X         LIBDIR = $(DESTDIR)/usr/local/lib/X.V11R3
X     LINTLIBDIR = $(USRLIBDIR)/lint
X        FONTDIR = $(LIBDIR)/fonts
X       XINITDIR = $(LIBDIR)/xinit
X         XDMDIR = $(LIBDIR)/xdm
X         UWMDIR = $(LIBDIR)/uwm
X         AWMDIR = $(LIBDIR)/awm
X         TWMDIR = $(LIBDIR)/twm
X        MANPATH = $(DESTDIR)/usr/man
X  MANSOURCEPATH = $(MANPATH)/man
X         MANDIR = $(MANSOURCEPATH)n
X      LIBMANDIR = $(MANSOURCEPATH)3
X    XAPPLOADDIR = $(LIBDIR)/app-defaults
X
X   INSTBINFLAGS = -m 0755
X   INSTUIDFLAGS = -m 4755
X   INSTLIBFLAGS = -m 0664
X   INSTINCFLAGS = -m 0444
X   INSTMANFLAGS = -m 0444
X   INSTAPPFLAGS = -m 0444
X  INSTKMEMFLAGS = -m 4755
X        FCFLAGS = -t
X    CDEBUGFLAGS = -O
X
X        PATHSEP = /
X         DEPEND = $(DEPENDSRC)/makedepend
X          IMAKE = $(IMAKESRC)/imake
X            RGB = $(RGBSRC)/rgb
X             FC = $(BDFTOSNFSRC)/bdftosnf
X      MKFONTDIR = $(MKFONTDIRSRC)/mkfontdir
X      MKDIRHIER = $(SCRIPTSSRC)/mkdirhier.sh
X
X         CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES) $(DEFINES)
X      LINTFLAGS = $(LINTOPTS) $(INCLUDES) $(STD_DEFINES) $(DEFINES) -DLINT
X        LDFLAGS = $(CDEBUGFLAGS) $(SYS_LIBRARIES) $(SYSAUX_LIBRARIES)
X            TOP = ../../..
X      CLIENTSRC = $(TOP)/clients
X        DEMOSRC = $(TOP)/demos
X         LIBSRC = $(TOP)/lib
X        FONTSRC = $(TOP)/fonts
X     INCLUDESRC = $(TOP)/X11
X      SERVERSRC = $(TOP)/server
X        UTILSRC = $(TOP)/util
X     SCRIPTSSRC = $(UTILSRC)/scripts
X     EXAMPLESRC = $(TOP)/examples
X     CONTRIBSRC = $(TOP)/contrib
X         DOCSRC = $(TOP)/doc
X         RGBSRC = $(TOP)/rgb
X      DEPENDSRC = $(UTILSRC)/makedepend
X       IMAKESRC = $(UTILSRC)/imake
X       IRULESRC = $(UTILSRC)/imake.includes
X        XLIBSRC = $(LIBSRC)/X
X         XMUSRC = $(LIBSRC)/Xmu
X     TOOLKITSRC = $(LIBSRC)/Xt
X     AWIDGETSRC = $(LIBSRC)/Xaw
X     OLDXLIBSRC = $(LIBSRC)/oldX
X    BDFTOSNFSRC = $(FONTSRC)/bdftosnf
X   MKFONTDIRSRC = $(FONTSRC)/mkfontdir
X   EXTENSIONSRC = $(TOP)/extensions
X   EXTENSIONLIB = $(EXTENSIONSRC)/lib/libXext.a
X           XLIB = $(XLIBSRC)/libX11.a
X         XMULIB = $(XMUSRC)/libXmu.a
X        OLDXLIB = $(OLDXLIBSRC)/liboldX.a
X       XTOOLLIB = $(TOOLKITSRC)/libXt.a
X         XAWLIB = $(AWIDGETSRC)/libXaw.a
X       LINTXLIB = $(XLIBSRC)/llib-lX11.ln
X        LINTXMU = $(XMUSRC)/llib-lXmu.ln
X      LINTXTOOL = $(TOOLKITSRC)/llib-lXt.ln
X        LINTXAW = $(AWIDGETSRC)/llib-lXaw.ln
X       INCLUDES = -I$(TOP)
X      MACROFILE = Sun.macros
X   ICONFIGFILES = $(IRULESRC)/Imake.tmpl \
X			$(IRULESRC)/$(MACROFILE) $(IRULESRC)/site.def
X  IMAKE_DEFINES =
X      IMAKE_CMD = $(NEWTOP)$(IMAKE) -TImake.tmpl -I$(NEWTOP)$(IRULESRC) \
X			-s Makefile $(IMAKE_DEFINES)
X         RM_CMD = $(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a \
X			.emacs_* tags TAGS make.log MakeOut
X
X###########################################################################
X# rules:  $XConsortium: Imake.rules,v 1.71 88/10/23 22:46:34 jim Exp $
X
X###########################################################################
X# start of Imakefile
X
XLOCAL_LIBRARIES = $(XLIB)
X           SRCS = xdb.c
X           OBJS = xdb.o
X
X PROGRAM = xdb
X
Xall:: xdb
X
Xxdb: $(OBJS) $(LOCAL_LIBRARIES)
X	$(RM) $@
X	$(CC) -o $@ $(OBJS) $(LOCAL_LIBRARIES) $(LDFLAGS) $(SYSLAST_LIBRARIES)
X
Xrelink::
X	$(RM) $(PROGRAM)
X	$(MAKE) $(MFLAGS) $(PROGRAM)
X
Xinstall:: xdb
X	$(INSTALL) -c $(INSTALLFLAGS) xdb $(BINDIR)
X
Xinstall.man:: xdb.man
X	$(INSTALL) -c $(INSTMANFLAGS) xdb.man $(MANDIR)/xdb.n
X
Xdepend:: $(DEPEND)
X
Xdepend::
X	$(DEPEND) -s "# DO NOT DELETE" -- $(CFLAGS) -- $(SRCS)
X
X$(DEPEND):
X	@echo "making $@"; \
X	cd $(DEPENDSRC); $(MAKE)
X
Xclean::
X	$(RM) $(PROGRAM)
X
X###########################################################################
X# Imake.tmpl common rules for all Makefiles - do not edit
X
Xemptyrule::
X
Xclean::
X	$(RM_CMD) \#*
X
XMakefile:: $(IMAKE)
X
XMakefile:: Imakefile \
X	$(IRULESRC)/Imake.tmpl \
X	$(IRULESRC)/Imake.rules \
X	$(IRULESRC)/site.def \
X	$(IRULESRC)/$(MACROFILE)
X	-@if [ -f Makefile ]; then \
X	echo "$(RM) Makefile.bak; $(MV) Makefile Makefile.bak"; \
X	$(RM) Makefile.bak; $(MV) Makefile Makefile.bak; \
X	else exit 0; fi
X	$(IMAKE_CMD) -DTOPDIR=$(TOP)
X
X$(IMAKE):
X	@echo "making $@"; \
X	cd $(IMAKESRC); $(MAKE) BOOTSTRAPCFLAGS=$(BOOTSTRAPCFLAGS)
X
Xtags::
X	$(TAGS) -w *.[ch]
X	$(TAGS) -xw *.[ch] > TAGS
X
X###########################################################################
X# empty rules for directories that do not have SUBDIRS - do not edit
X
Xinstall::
X	@echo "install done"
X
Xinstall.man::
X	@echo "install.man done"
X
XMakefiles::
X
X###########################################################################
X# dependencies generated by makedepend
X
SHAR_EOF
if test 7181 -ne "`wc -c < 'Makefile'`"
then
	echo shar: error transmitting "'Makefile'" '(should have been 7181 characters)'
fi
fi # end of overwriting check
echo shar: done with directory "'xdb'"
cd ..
#	End of shell archive
exit 0