[comp.sys.sun] How to discover screen depth

dna@emmy.umd.edu (Doug Arnold) (11/23/88)

>>  Is there an easy way to find out the screen depth?

I use the following simple program, which I call displaytype.  It works
like this:

emmy% displaytype
Sun 2 or 3 color  900 x 1152 x 8 pixels
emmy% rsh athena displaytype
Sun 2 or 3 monochrome  900 x 1152 x 1 pixels

There should be a better way to handle color 3/60's or other machines with
two framebuffers correctly.   It currently reports monochrome.  Perhaps
someone knows how to do it?

  -- Doug Arnold (dna@emmy.umd.edu or arnold@eneevax.umd.edu)

/*
 *  displaytype  --  identify type of display connected to Sun workstation
 *       compile with "cc -o displaytype displaytype.c"
 */

#include <stdio.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sun/fbio.h>

extern int      errno;

char           *displaytype[] =
{
    "Sun 1 monochrome",
    "Sun 1 color",
    "Sun 2 or 3 monochrome",
    "Sun 2 or 3 color",
    "Sun 2 or 3 graphics processor",
    "type unknown",
    "type unknown",
    "Sun 4 monochrome",
    "Sun 4 color",
    ""
};

main()
{
    int             fd;
    struct fbtype   fbinfo;	/* struct for frame buffer info */

    fd = open("/dev/fb", O_RDONLY);
    if (fd < 0)
    {
	printf("Error opening /dev/fb, error #%d\n", errno);
	exit(2);
    }
    if (ioctl(fd, FBIOGTYPE, &fbinfo) == -1)
    {
	printf("Ioctl error on /dev/fb, error #%d\n", errno);
	exit(3);
    }
    printf("%s  %d x %d x %d pixels\n",
	   displaytype[fbinfo.fb_type],
	   fbinfo.fb_height, fbinfo.fb_width, fbinfo.fb_depth);
    exit(0);
}

mmm%informatics.rutherford.ac.uk@nss.cs.ucl.ac.uk (11/24/88)

In Sun-Spots v7n12 Matthew Turk (mit-amt!turk@mit-amt.media.mit.edu) asks
> I have 1-bit and 8-bit monitors hooked up to a Sun.  Is there an easy way
> to find out the screen depth (under Suntools) without creating a new
> window?

I use the following fragment:

	struct pixrect *sc;
	int retn;
	sc = pr_open("/dev/fb");
	if(sc==0)return(0);
	switch(what){
	case ASKXSCREEN:
		retn = sc->pr_size.x;
		break;
	case ASKYSCREEN:
		retn = sc->pr_size.y;
		break;
	case ASKDEPTH:
		retn = sc->pr_depth;
		break;
	}
	pr_close(sc);

Mark M Martin			janet:	mmm@uk.ac.rl.inf
Informatics			arpa:	mmm%inf.rl.ac.uk@nss.cs.ucl.ac.uk
Rutherford Appleton Lab		uucp:	..!mcvax!ukc!rlinf!mmm
Didcot, Oxon, UK. OX11 0QX		mmm@rlinf.uucp
Tel +44 235 44 6756		Fax +44 235 44 5831

josh@cadnetix.com (12/02/88)

Here is a code fragment that determines the depth of a screen:

#include        <suntool/sunview.h>

	...
	Pixrect *screen;
	...
	/* open frame buffer */
	if ((screen = pr_open("/dev/fb")) == 0)
		error("Can't open screen!\n");

	if (screen->pr_depth == 1) {
		/* Black and white screen */
	}
	else if (screen->pr_depth == 8) {
		/* 8 Bit (gray scale or color) */
	}
	else {
		error("Weird monitor depth: %d\n", screen->pr_depth);
	}
	...

    Joshua Goldstein	UUCP: cadnetix!josh
    Cadnetix Corp.	      {uunet,boulder,sunpeaks}!cadnetix!josh
    5775 Flatiron Pkwy
    Boulder, CO 80301

mkhaw@teknowledge-vaxc.arpa (Mike Khaw) (12/04/88)

>>>  Is there an easy way to find out the screen depth?
...
> There should be a better way to handle color 3/60's or other machines with
> two framebuffers correctly.   It currently reports monochrome.  Perhaps
> someone knows how to do it?

You can go through a lot of gyrations to open the root window's "screen"
and look at the bits for 8bit_color_only, or overlay_only, etc.  Or, if
you assume you are in suntools when you want to find out, it looks like a
simple program like this will do it:

<--- cut here --->
/*
 * fbdepth - works for cgfours under suntools.
 * compile with -lsuntool -lsunwindow -lpixrect
 */
#include <suntool/sunview.h>

main()
{
	Frame	base;
	Pixwin	*pw;

	if ((base = window_create(0, FRAME, WIN_SHOW, FALSE, 0)) == NULL)
	  {
		fprintf(stderr, "suntools not running!?\n");
		exit(1);
	  }
	pw = (Pixwin *) window_get(base, WIN_PIXWIN);
	printf("desktop bitplane depth = %d\n", pw->pw_pixrect->pr_depth);
	exit(0);
}
<--- cut here --->

This will return "1" if your have a monochrome frame buffer or are running
on the b&w desktop of a cgfour; otherwise it should return "8".

Now the more elaborate program that will work even if you're not running
under suntools.  This one returns "monochrome" or "color" if it can figure
it out, or "prism" if you have a cgfour and it can't tell which planes are
in use:

<--- cut here --->
/*
 * fbtype - compile with -lsunwindow -lpixrect
 */
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sun/fbio.h>
#include <sunwindow/window_hs.h>

main()
{
	int     fd;
	char	windevname[12];

	struct fbgattr	attr_buff;

	errno = 0;
	if ((fd = open("/dev/fb", O_RDONLY)) < 0)
		exit(errno);

	errno = 0;
	if (ioctl(fd, FBIOGATTR, &attr_buff) < 0)
		if (ioctl(fd, FBIOGTYPE, &attr_buff.fbtype) < 0)
			exit(errno);
	(void) close(fd);

	if (attr_buff.fbtype.fb_type != FBTYPE_SUN4COLOR)
		switch (attr_buff.fbtype.fb_depth)
		  {
		  case 1:
			printf("monochrome\n");
			break;

		  case 8:
			printf("color\n");
			break;

		  default:
			printf("unknown\n");
			break;
		  }
	else if (we_getparentwindow(windevname))
		printf("prism\n");	/* not in suntools? */
	else
	  {
		struct screen	screen;

		errno = 0;
		if ((fd = open(windevname)) < 0)
			exit(errno);

		win_screenget(fd, &screen);
		(void) close(fd);

		if (screen.scr_flags & SCR_8BITCOLORONLY)
			printf("color\n");
		else if (strncmp("/dev/bw", screen.scr_fbname, 7) == 0 ||
		  screen.scr_flags & SCR_OVERLAYONLY)
			printf("monochrome\n");
		else
			printf("prism\n");
	  }
	exit(0);
}
<--- cut here --->

Mike Khaw
-- 
internet: mkhaw@teknowledge.arpa
uucp:	  {uunet|sun|ucbvax|decwrl|ames|hplabs}!mkhaw%teknowledge.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

rich@sun.com (Richard Saunders) (12/10/88)

Doug Arnold's program (v7n24) does an open("/dev/fb",) followed by an
ioctl(,FBIOGTYPE,) to get the fbinfo.  I use a modified version of a
program called fbres.c that came from Chuq von Rospach which does
pr_open() instead.

I ran the two on a 3/110.  displaytype gave:

	Sun 2 or 3 monochrome  900 x 1152 x 1 pixels

whereas fbres gave:

	1152x900C

Here is the source for fbres and how its output can be used in your .login
file (set your path to get /usr/local/bin/`arch`):

/*
 *	fbres.c
 *
 *	Prints out the resolution of the fb pixrect in a
 *	form useful for shell scripts.
 *
 *	Usage: fbres [fb-name]
 */

#include	<stdio.h>
#include	<pixrect/pixrect_hs.h>

main(argc,argv)
int		argc;
char		*argv[];
{
	Pixrect		*fbpr;
	char		*fbname;

	if (argc > 1) fbname = argv[1];
	else fbname = "/dev/fb";

	if ((fbpr = pr_open(fbname)) == NULL) {
		fprintf(stderr, "Couldn't open %s pixrect\n", fbname);
		exit(1);
	}
	printf("%dx%d", fbpr->pr_size.x, fbpr->pr_size.y);
	printf((fbpr->pr_depth > 1 ? "C" : "M"));
	printf("\n");
}

# if you are on the console (you DON'T want to do this when rlogged in!)
# initialize the inputs from the data in your .defaults file
# uses fbres program to check for screen type. Needs SunOS 3.4.
if (`tty` == /dev/console) then
	switch (`fbres`)
	case "1152x900M":
		setenv DEFAULTS_FILE ~/.defaults.lores
		alias st 'exec suntools -pattern ~/icons/rrichter.icon -s ~/.suntools.lores'
		breaksw
	case "1152x900C":
		setenv DEFAULTS_FILE ~/.defaults.lores
		alias st 'exec suntools -pattern ~/icons/rrichter.icon -s ~/.suntools.color'
		breaksw
	case "1600x1280M":
		setenv DEFAULTS_FILE ~/.defaults.hires
		alias st 'exec suntools -pattern ~/icons/rrichter.icon -s ~/.suntools.hires'
		breaksw
	endsw
endif

dupuy@douglass.cs.columbia.edu (Alexander Dupuy) (01/12/89)

>>>  Is there an easy way to find out the screen depth?
>
>I use the following simple program, which I call displaytype.  It works
>like this:...

Here's an updated version of my screen(1) program for Suns which does all
this and so much more (I took some ideas from the emulation(1) program
posted to sun-spots some time ago).  The names for the various color
displays are somewhat confusing: a CG3 is not the same as a cgthree (the
latter on 386i's).  I have used the names from the October Sun STB article
on color boards.

 sun% screen -v
 /dev/fb is on
 sun% screen type
 is a 1152 x 900 x 1 Monochrome 2 (BW2) display
 emulated by a 1152 x 900 x 10 (256 color) Color Graphics 4 (CG4) display
 sun% screen cgtwo

@alex

-- 
inet: dupuy@columbia.edu
uucp: ...!rutgers!columbia!dupuy

[[ It has replaced the old version in the archives.  It is under
"sun-source" and is called "screen.shar".  It is 8686 bytes long.  It can
be retrieved via anonymous FTP from the host "titan.rice.edu" or via the
archive server.  For more information about the archive server, send a
mail message containing the word "help" to the address
"archive-server@rice.edu".  --wnl ]]