ftp.nice.ch/pub/next/developer/objc/screen/ScreenMunger.2.0.N.bs.tar.gz#/ScreenMunger/zingtwat.c

This is zingtwat.c in view mode; [Download] [Up]

#import <stdio.h>
#import <nextdev/video.h>
#import <sys/file.h>
#import <sys/ioctl.h>
#import <objc/objc.h>
#import <stdlib.h>
#import <string.h>
#import <sys/signal.h>

/* zingtwat.c -- recursively divide up the screen, and mess with it
 *
 * 5/92, CWS -- Added support for signal trapping.  Cleaned up the code.
 * This now runs forever until the user kills it.
 */

#define PC_WIDTH 56
#define PC_HEIGHT 52
#define X_TILES 20
#define Y_TILES 16

extern open();
extern ioctl();
extern close();
extern sleep();

long num_bytes = VIDEO_MW * VIDEO_H / NPPB, bytes_per_row = VIDEO_MW / NPPB;
unsigned char *old_screen = NULL, *vid_base = NULL;
unsigned char piece_to_move[VIDEO_MW / NPPB], d[VIDEO_MW / NPPB]; 

// various small utility routines for moving memory around
long coordtoindex(x,y)
     long x, y;
{
  return (y*bytes_per_row + x);
}

void get_piece(x, y, piece, source)
     long x,y;
     unsigned char *piece, *source;
{
  long i,j;
  
  for(j=0;j<PC_HEIGHT;j++){
    for(i=0;i<(PC_WIDTH/4);i++){
      piece[coordtoindex(i,j)]=
	source[coordtoindex(((x*(PC_WIDTH/4))+i),((y*PC_HEIGHT)+j))];
    }
  }
}

void place_piece(x,y,piece,dest)
     long x,y;
     unsigned char *piece, *dest;
{
  long i,j;
  for(j=0;j<PC_HEIGHT;j++){
    for(i=0;i<(PC_WIDTH/4);i++){
      dest[coordtoindex(((x*(PC_WIDTH/4))+i),((y*PC_HEIGHT)+j))]=
	piece[coordtoindex(i,j)];
    }
  }
}

void erase_piece(x,y,dest)
     long x,y;
     unsigned char *dest;
{
  long i,j;
  for(j=0;j<PC_HEIGHT;j++){
    for(i=0;i<(PC_WIDTH/4);i++){
      dest[coordtoindex(((x*(PC_WIDTH/4))+i),((y*PC_HEIGHT)+j))]=255;
    }
  }
}

#define MINWIDTH 200

void swap_square(x, y, width, height)
     long x, y, width, height;
{
  long i,j,k;
  
  if(width>MINWIDTH){
    swap_square(x, y, width/2, height/2);
    swap_square(x + width/2, y, width/2, height/2);
    swap_square(x, y + height/2, width/2, height/2);
    swap_square(x + width/2, y + height/2, width/2, height/2);
  } else {
    for(k=0; k<(height); k++){
      bcopy(&vid_base[coordtoindex(x, y + (height - k))],
	    &d,
	    width/2);
      bcopy(&vid_base[coordtoindex(x + width/2, y+k)],
	    &vid_base[coordtoindex(x, y + (height-k))],
	    width/2);
      bcopy(&d,
	    &vid_base[coordtoindex(x + width/2, y+k)],
	    width/2);

    }
  }
}

// general signal handler; simply restores screen memory and exits.
void handle(sig)
   int sig;
{
    bcopy(old_screen, vid_base, num_bytes * sizeof(unsigned char));
    free(old_screen);
    fprintf(stdout, "\nCaught signal %d...\n", sig);
    exit(0);
}

// handle SIGTSTP; restores screen memory, then calls SIGSTOP
void pause(sig)
   int sig;
{
    bcopy(old_screen, vid_base, num_bytes * sizeof(unsigned char));
    raise(SIGSTOP);
}



void main() {
  int fd, i, j, k, e;

// do the signal trapping stuff
  if (signal(SIGINT, &handle)) {
      fprintf(stdout, "Couldn't establish new error handler for SIGINT.\n");
      exit(1);
  }

  if (signal(SIGTERM, &handle)) {
      fprintf(stdout, "Couldn't establish new error handler for SIGTERM.\n");
      exit(2);
  }

  if (signal(SIGTSTP, &pause)) {
      fprintf(stdout, "Couldn't establish new errorhandler for SIGTSTP.\n");
      exit(3);
  }

// open the /dev/vid0, and get the address of screen memory
  fd = open("/dev/vid0", O_RDWR, NULL);
  
  if (fd <= 0) {
    fprintf(stdout,"open result:%d\n", errno);
    exit(1);
  }
  
  if (ioctl(fd, DKIOCGADDR, &vid_base) < 0) {
    fprintf(stdout, "ioctl errno: %d\n", errno);
    exit(1);
  }

  old_screen=(unsigned char *)malloc(num_bytes * sizeof(unsigned char));
  bcopy(vid_base, old_screen, num_bytes);

  for(;;){
    swap_square(0, 0, VIDEO_W, VIDEO_H);
  }

      
//    for(i=0;i<VIDEO_H;i++){
//      bcopy(&old_screen[coordtoindex(0,i)],
//	    &vid_base[coordtoindex(0,((VIDEO_H-i-1)*2)%VIDEO_H)],
//	    bytes_per_row);
//      bcopy(&old_screen[coordtoindex(0,((VIDEO_H-i-1)*2)%VIDEO_H)],
//	    &vid_base[coordtoindex(0,(i+1))],
//	    bytes_per_row);
//	  }
		 
//  sleep(5);

  bcopy(old_screen, vid_base, num_bytes);
  free(old_screen);
  close(fd);
  exit(0);
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.