Xref: utzoo comp.unix.microport:944 comp.unix.xenix:2599
Path: utzoo!attcan!uunet!husc6!mailrus!umix!metavax!oxtrap!b-tech!m2-net!lewis
From: lewis@m2-net.UUCP (Dave Lewis)
Newsgroups: comp.unix.microport,comp.unix.xenix
Subject: Graphics Library (part 3 of 5)
Keywords: Graphics EGA CGA Hercules Printers Microport Xenix 286 386
Message-ID: <1883@m2-net.UUCP>
Date: 1 Jul 88 04:07:31 GMT
References: <1881@m2-net.UUCP>
Reply-To: lewis@m-net.UUCP (Dave Lewis)
Organization: M-NET, Ann Arbor, MI
Lines: 1125

------------------------------  cut here  ------------------------------
# To recover, type "sh archive"
echo restoring Makefile
sed 's/^X//' > Makefile < demo.c <
X#include 
X#include 
X#include 
X#include 
X
X/*  Sun Nov 29 16:20:03 EST 1987 */
X/*  Sat Mar 21 22:59:10 EST 1987 */
X/*  Sun Feb 21 11:50:45 EST 1988 */
X
X#include "graphics.h"
X
Xextern struct SVAT_graphics graphics;
X
Xmain(argc,argv)  
Xint argc;
Xchar *argv[];
X{
X	int testpattern();
X
X	char *mode_to_use[8];
X	int idx;
X	int init(), grafstr(), finish(), istat;
X	void null_function(); 
X
X	if (argc < 2)  {
X		printf ("Usage:  demo \n\nWhere  can be 4, 6, 8, EGA or print)\n");
X		exit(0);
X		}
X
X	if (strcmp(argv[1],"4") == 0) {
X		graphics.grafmode = CGA_COLOR_MODE;
X		strcpy(mode_to_use,"mode 4");
X	}
X	else if (strcmp(argv[1],"6") == 0) {
X		graphics.grafmode = CGA_HI_RES_MODE;
X		strcpy(mode_to_use,"mode 6");
X	}
X	else if (strcmp(argv[1],"8") == 0) {
X		graphics.grafmode = HERC_GRAF_MODE;
X		strcpy(mode_to_use,"mode 8");
X	}
X	else if (strcmp(argv[1],"16") == 0) {
X		graphics.grafmode = EGA_COLOR_MODE;
X		strcpy(mode_to_use,"mode 16");
X	}
X	else if (strcmp(argv[1],"EGA") == 0) { 
X		graphics.grafmode = EGA_COLOR_MODE;
X		strcpy(mode_to_use,"mode 16");
X	}
X	else if (strcmp(argv[1],"print") == 0) { 
X		graphics.grafmode = IBM_PRINTER;
X		strcpy(mode_to_use," ");
X	}
X	else  {
X		printf ("Valid modes are 4, 6, 8, EGA, and print.\n");
X		exit (1);
X	}
X
X        /* Set video mode. */
X
X	istat = init(graphics.grafmode);
X
X	system(mode_to_use);
X
X	istat = clear();
X
X	istat = testpattern();
X
X	if (graphics.grafmode != IBM_PRINTER)  {
X
X		/* Kill some time... */
X		signal(SIGALRM,null_function);
X		alarm(4);
X		pause();
X
X		istat = clear();
X
X		/* void grafstr(strng,x,y,vsize,hsize,spacing)  */
X		istat = grafstr("AbCdEfGhIjKlMnOpQrStUvWxYz",10,10000,400,180,1250);
X		istat = grafstr("AbCdEfGhIjKlMnOpQrStUvWxYz",100,25000,150,80,667);
X		istat = grafstr("AbCdEfGhIjKlMnO",100,15000,800,360,2500);
X
X		istat = grafstr("Z#\$%^&*()+",100,20000,600,400,2600);
X
X	if (graphics.grafmode != IBM_PRINTER)  {
X		/* Kill some time... */
X		signal(SIGALRM,null_function);
X		alarm(4);
X		pause();
X	}
X
X		istat = clear();
X
X		istat = cursor(1,1);
X		istat = cellstr("This should be (1,1)");
X
X		istat = cursor(2,30);
X		istat = cellstr("This should be (2,30)");
X
X		istat = cursor(3,60);
X		istat = cellstr("This should be (3,60)");
X
X		istat = cursor(10,40);
X		istat = cellstr("This should be (10,40)");
X
X		istat = cursor(25,40);
X		cellstr("X");
X
X		istat = cursor(25,80);
X		cellstr("X");
X
X		istat = cursor(43,90);
X		cellstr("X");
X
X		cursor(2,1); cellstr("This");
X		cursor(3,1); cellstr(" Is ");
X		cursor(4,1); cellstr("  A ");
X		cursor(5,1); cellstr("TEST");
X		cursor(6,1); cellstr("XXXX");
X		cursor(7,1); cellstr("XXXX");
X
X		for (idx=1;idx<=43;idx++)  {
X			cursor(idx,20);
X			cellstr("XHIHX");
X		}
X
X		/* Kill some time... */
X		signal(SIGALRM,null_function);
X		alarm(4);
X		pause();
X
X		system ("mode 2");
X	}
X
X	istat = finish();
X}
X
Xstatic void null_function()  {
X	/* Do nothing.  Dummy signal handling routine for SIGALRM. */
X}
X
XxX--EOF--XxX
echo restoring finish.c
sed 's/^X//' > finish.c <
X#include 
X#include 
X#include 
X#include "graphics.h"
X
Xint finish()
X
X{
X	extern struct SVAT_graphics graphics;
X	int shmdt(), fprintf(), fputs(), fwrite();
X	int shmid;
X	int clear();
X	void free();
X	char *getenv();
X	extern errno;
X	int idx;
X	char printprog[60];
X	char *envptr;
X
X	FILE *popen(), *fhandle;
X	int pclose();
X
X	switch (graphics.grafmode)  {
X
X		case CGA_HI_RES_MODE:
X			/* Erase graphics memory.		*/
X			if (clear()) return(1);
X			/* Detach the shared memory segment.	*/
X			if (shmdt(graphics.cgamem) < 0) return(errno);
X			break;
X
X		case CGA_COLOR_MODE:
X			/* Erase graphics memory.		*/
X			if (clear()) return(1);
X			/* Detach the shared memory segment.	*/
X			if (shmdt(graphics.cgamem) < 0) return(errno);
X			break;
X
X		case EGA_COLOR_MODE:
X			/* Erase graphics memory.		*/
X			if (clear()) return(1);
X			/* Detach the shared memory segment.	*/
X			if (shmdt(graphics.egamem) < 0) return(errno);
X			break;
X
X		case HERC_GRAF_MODE:
X			/* Erase graphics memory.		*/
X			if (clear()) return(1);
X			/* Detach the shared memory segment.	*/
X			if (shmdt(graphics.hercmem) < 0) return(errno);
X			break;
X
X		case IBM_PRINTER:
X	
X			/* Print the buffer */
X
X			/* Use the environment variable PLOTDEV if set. */
X
X			strcpy(printprog, PRINTPROG);
X			strcat(printprog, " ");
X			if ((envptr = getenv("PLOTDEV")) != NULL)  {
X				strcat(printprog, "-d");
X				strcat(printprog, envptr);
X			}
X	
X			if ((fhandle = popen(printprog, "w")) == NULL) {
X				fprintf(stderr,
X					"finish():  Error piping to %s\n", 
X					printprog);
X				return(1); 
X				}
X
X			/* Dump the print image to the file */
X
X			/* Set line feed pitch to 7/72" */
X
X			GRAFPITCH(fhandle);
X
X			/* Write the lines of data (3 buffers full) */
X			for (idx=0; idxbuf[idx][0]),
X					sizeof(char), PRINTDENSITY, fhandle);
X				CRLF(fhandle);
X			}
X			for (idx=0; idxbuf[idx][0]),
X					sizeof(char), PRINTDENSITY, fhandle);
X				CRLF(fhandle);
X			}
X			for (idx=0; idxbuf[idx][0]),
X					sizeof(char), PRINTDENSITY, fhandle);
X				CRLF(fhandle);
X			}
X
X			/* Set line feed pitch back to 12/72" for text */
X			TEXTPITCH(fhandle);
X
X			if (pclose(fhandle) < 0) return(1);
X
X			/* Free the allocated memory. */
X			free(graphics.printbuf1);
X			free(graphics.printbuf2);
X			free(graphics.printbuf3);
X
X			break;
X
X		default:
X			return(1);
X	}
X	return(0);
X}
X
X
XxX--EOF--XxX
echo restoring linedraw.c
sed 's/^X//' > linedraw.c < mode.c <
X
Xmain(argc,argv) 
Xint argc;
Xchar *argv[];
X{
X	int setmode();
X	int modevalue, stat;
X	char *inbuf[20];
X
X	if (argc > 2) {
X		printf("Usage:  mode n\n");
X		exit(0);
X	}
X
X	else if (argc == 2)  {
X		modevalue = atoi(argv[1]);
X		if (modevalue < 0 || modevalue > 11) {
X			printf("Mode must be between 0 and 11\n");
X			exit(0);
X		}
X		setmode(modevalue);
X	}
X	else if (argc == 1)  {
X#ifdef DEBUG
X		for ( ; ; ) {
X			stat=scanf("%d", &modevalue);
X			if (stat == EOF) exit(0);
X			/* Clear input stream if bad read. */
X			if (stat == 0) gets(inbuf, 20, stdin); 
X			if (stat == 1 && modevalue >= 0 && modevalue <=11) 
X				setmode(modevalue);
X			}
X#endif /* DEBUG */
X		setmode(NORMAL_MODE);
X		}
X	}
XxX--EOF--XxX
echo restoring mode.sh
sed 's/^X//' > mode.sh < movpen.c < setmode.c <
X#if UNIX
X#include 
X#include 
X#endif
X
Xextern	errno;
X
Xint	fd;
X
X/* Define the mode register settings. */
Xunsigned char	creg[12] = {
X	0x2c, 0x28, 0x2d, 0x2d, 0x2a, 0x2e, 0x1e, 0x2d, 0x0e, 0x8e, 0x2d, 0x2d
X};
X/* Define the Everex extended graphics register settings */
Xunsigned char	ext_graf_reg[12] = {
X	0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x06, 0x06
X};
X
Xunsigned char	params[192] = {
X/*0*/	0x38, 0x28, 0x2d, 0x0a, 0x1f, 0x06, 0x19, 0x1c,   /* 40x25 b/w */
X	0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*1*/	0x38, 0x28, 0x2d, 0x0a, 0x1f, 0x06, 0x19, 0x1c,   /* 40x25 color */
X	0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*2*/	0x61, 0x50, 0x52, 0x0f, 0x19, 0x06, 0x19, 0x19,	  /* 80x25 b/w */
X	0x02, 0x0d, 0x0b, 0x0c, 0x00, 0x00, 0x00, 0x00,
X
X/*3*/	0x71, 0x50, 0x5a, 0x0a, 0x1f, 0x06, 0x19, 0x1c,	  /* 80x25 color */
X	0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*4*/	0x38, 0x28, 0x2d, 0x0a, 0x7f, 0x06, 0x64, 0x70,	  /* 320x200 color */
X	0x02, 0x01, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*5*/	0x38, 0x28, 0x2d, 0x0a, 0x7f, 0x06, 0x64, 0x70,	  /* 320x200 b/w */
X	0x02, 0x01, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*6*/	0x38, 0x28, 0x2d, 0x0a, 0x7f, 0x06, 0x64, 0x70,	  /* 640x200 b/w */
X	0x02, 0x01, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*7*/	0x61, 0x50, 0x52, 0x0f, 0x19, 0x06, 0x19, 0x19,	  /* mono */
X	0x02, 0x0d, 0x0b, 0x0c, 0x00, 0x00, 0x00, 0x00,
X
X/*8*/	0x36, 0x2d, 0x2e, 0x07, 0x5a, 0x02, 0x57, 0x57,   /* Hercules 720x348 */
X	0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
X
X/*9*/	0x37, 0x2d, 0x2f, 0x05, 0x57, 0x00, 0x57, 0x57,   /* Hercules 720x348 */
X	0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
X
X/*10*/	0x9a, 0x84, 0x86, 0x0f, 0x19, 0x06, 0x19, 0x19,   /* Everex 132x25b&w */
X	0x02, 0x0d, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00,
X
X/*11*/	0x9a, 0x84, 0x86, 0x0f, 0x2d, 0x02, 0x2c, 0x2c,   /* Everex 132x44b&w */
X	0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00
X};
X
Xsetmode(mode)
Xint	mode;
X{
X	int	i;
X	int	j;
X	int	bport;
X	int	cgaaddr  = 0x3d4;		/* cga 6845 port address */
X	int	monoaddr = 0x3b4;		/* mda 6845 port address */
X
X#if UNIX
X	if ((fd = open("/dev/mem", 2)) < 0) {
X		fprintf(stderr, "mode: open of /dev/mem failed\n");
X		perror("mode");
X		exit(1);
X		}
X#endif
X	if ((mode == 2 | mode == 7) | (mode == 8) | (mode == 9) |
X			(mode == 10) | (mode == 11))  {
X		/* Select mono mode on Everex Edge. */
X		bport = cgaaddr;
X		outb(bport+9, 2);		/* Extended Graphics Register */
X		bport = monoaddr;
X		}
X	else  {
X		/* Select CGA mode on Everex Edge. */
X		bport = monoaddr;
X		outb(bport+9, 0);		/* Extended Graphics Register */
X		bport = cgaaddr;
X		}
X
X	outb(bport+4, 0);			/* Reset mode register */
X
X	outb(bport+11, 0x06);			/* Enable all 64k of Everex 
X						** Edge video memory, for mono 
X						** adapter. 
X						*/
X
X	outb(bport+9, ext_graf_reg[mode]);	/* set Everex "extended 
X						graphics register" */
X	j = (mode+1)*16;
X	for(i=mode*16; i < j; i++) {
X		outb(bport, i);			/* select register */
X		outb(bport+1, params[i]);	/* send value */
X		}
X
X	outb(bport+4, creg[mode]);		/* enable */
X	if (mode == 6)
X		outb(bport+5, 0x3f);	/* set overscan for 640x200 */
X	else
X		outb(bport+5, 0x30);	/* set normal overscan */
X	close(fd);
X	return(0);
X}
X
X#if UNIX
Xoutb(portno, data)
Xint	portno;
Xint	data;
X{
X	io_op_t	iop;
X
X	iop.io_port = portno;
X	iop.io_byte = data;
X	if (ioctl(fd, IOCIOP_WB, &iop) < 0) {
X		fprintf(stderr, "mode: error on IOCIOP_WB\n");
X		perror("mode");
X		exit(1);
X	}
X}
X#endif
X
X
X#if MSDOS
Xoutb(portno, data)
Xint portno;
Xint data;
X{
X	outp(portno,data);
X}
X#endif
X
XxX--EOF--XxX
echo restoring testpat.c
sed 's/^X//' > testpat.c < write_pix.c <> 1;
X		col = x >> 2;
X
X		/* Select a color mask, consisting of four 2 bit patterns. */
X		switch (graphics.color) {
X			case 0:	bitmask = 0;
X				break;
X			case 1: bitmask = 0x055;
X				break;
X			case 2: bitmask = 0x0AA;
X				break;
X			case 3:	bitmask = 0x0FF;
X				break;
X			default:bitmask = 0x0FF;
X		}
X
X		/* Apply the color mask to the two bit pixel of interest. */
X		switch (x & 0x0003)  {
X			case 0:	bitmask &= PIX0;
X				break;
X			case 1:	bitmask &= PIX1;
X				break;
X			case 2:	bitmask &= PIX2;
X				break;
X			case 3:	bitmask &= PIX3;
X				break;
X		}
X
X		/* Write pixel to the appropriate page for this line. */
X		if (y & 0x0001) 
X			switch (graphics.wrt_mode)  {
X				case OR:
X					(graphics.cgamem)->page2[row][col] 
X						|= bitmask;
X					break;
X				case XOR:
X					(graphics.cgamem)->page2[row][col] 
X						^= bitmask;
X					break;
X			}
X		else
X			switch (graphics.wrt_mode)  {
X				case OR:
X					(graphics.cgamem)->page1[row][col] 
X						|= bitmask;
X					break;
X				case XOR:
X					(graphics.cgamem)->page1[row][col] 
X						^= bitmask;
X					break;
X			}
X		break;
X
X	case CGA_HI_RES_MODE:
X
X		row = y >> 1;
X		col = x >> 3;
X
X		bitmask = 0x80;
X		shiftcount = x & 0x0007;
X		bitmask = bitmask >> shiftcount;
X
X		if (y & 0x0001) {
X			switch (graphics.wrt_mode)  {
X				case OR:
X					(graphics.cgamem)->page2[row][col] 
X						|= bitmask;
X					break;
X				case XOR:
X					(graphics.cgamem)->page2[row][col] 
X						^= bitmask;
X					break;
X			}
X		}
X		else {
X			switch (graphics.wrt_mode)  {
X				case OR:
X					(graphics.cgamem)->page1[row][col] 
X						|= bitmask;
X					break;
X				case XOR:
X					(graphics.cgamem)->page1[row][col] 
X						^= bitmask;
X					break;
X			}
X		}
X		break;
X
X	case EGA_COLOR_MODE:
X
X		col = x >> 3;
X
X		bitmask = 0x80;
X		shiftcount = x & 0x0007;
X		bitmask = bitmask >> shiftcount;
X
X		(graphics.egamem)->mem[y][col] |= bitmask;
X		break;
X
X	case HERC_GRAF_MODE:
X
X		row = y >> 2;
X		col = x >> 3;
X
X		bitmask = 0x80 >> (x & 0x0007);
X
X		switch (y & 0x0003) {
X
X			case 0:
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.hercmem)
X						->page1[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.hercmem)
X						->page1[row][col] 
X						^= bitmask;
X						break;
X				}
X				break;
X			case 1:
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.hercmem)
X						->page2[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.hercmem)
X						->page2[row][col] 
X						^= bitmask;
X						break;
X				}
X				break;
X			case 2:
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.hercmem)
X						->page3[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.hercmem)
X						->page3[row][col] 
X						^= bitmask;
X						break;
X				}
X				break;
X			case 3:
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.hercmem)
X						->page4[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.hercmem)
X						->page4[row][col] 
X						^= bitmask;
X						break;
X				}
X				break;
X		}
X		break;
X
X	case IBM_PRINTER:
X
X		col = PRINTDENSITY - y - 1;
X
X		row = (x >> 3);
X		page = 0;
X		while (row >= PRINTLINES)  {
X			row -= PRINTLINES;
X			++page;
X		}
X
X		bitmask = 0x80 >> (x & 0x0007);
X
X		switch (page) {
X
X			case 0:	/* Page 0 */
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.printbuf1)
X						->buf[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.printbuf1)
X						->buf[row][col] 
X						^= bitmask;
X						break;
X					}
X				break;
X			case 1:	/* Page 1 */
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.printbuf2)
X						->buf[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.printbuf2)
X						->buf[row][col] 
X						^= bitmask;
X						break;
X					}
X				break;
X			case 2: /* Page 2 */
X				switch (graphics.wrt_mode)  {
X					case OR:
X						(graphics.printbuf3)
X						->buf[row][col] 
X						|= bitmask;
X						break;
X					case XOR:
X						(graphics.printbuf3)
X						->buf[row][col] 
X						^= bitmask;
X						break;
X					}
X				break;
X		}
X		break;
X	default:
X		return(1);
X	}
X	return(0);
X}
XxX--EOF--XxX
-- 
Dave Lewis
Ann Arbor, MI
...![ itivax umix ]!m-net!dtlewis!lewis