ftp.nice.ch/pub/next/developer/apps/DrawingServant.0.2.N.bs.tar.gz#/DrawingServant/tester.c

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

/* a simple program to illustrate use of DrawingServant */
/* ask the user to click somewhere inside the drawing window, then draw
1000 lines with the click as one endpoint */
/* this is NOT a good example of anything except how a vanilla looking
c program can invoke DrawingServant as a server to the NeXT window server */

#import <math.h>
#import <signal.h>
#import <stdlib.h>

#define DEFHEIGHT 500
#define DEFWIDTH 500
#define DRAWSERVER "DrawingServant"
static int outpipe[2],inpipe[2];
static int pid;
char outstring[200];
int height=DEFHEIGHT;
int width=DEFWIDTH;

#define SENDPS write(outpipe[1],outstring,strlen(outstring)+1)
#define GETACK do{\
		read(inpipe[0],outstring,sizeof(outstring)-1);}\
		while(strncmp(outstring,"Ok",2))

main()
{
	char hstr[20],wstr[20],instr[20],outstr[20];
	int i;
	float lx,ly;
	void next_line();
	void killDS();
	void DSdied();

	float allones = (float)pow((double)2.,(double)31.);
#define FRAND ((float)rand())/allones

	/* fork a process and assign some pipes.  return if
	unsuccessful */
	if( pipe(outpipe)== -1)
			return -1;
	if( pipe(inpipe)== -1)
			return -1;
	if( (pid=fork())== -1)
			return -1;

	if(pid==0) {
		sprintf(hstr,"h %d ",height);
		sprintf(wstr,"w %d ",width);
		sprintf(outstr,"i %d",outpipe[0]);
		sprintf(instr,"o %d",inpipe[1]);
		execl(DRAWSERVER,DRAWSERVER,hstr,wstr,instr,outstr,0);
		exit(0);
	}

	/* try to coordinate the activities of the caller and the
	callee.  If the servant died, this will signal main to exit
	gracefully.  atexit kills the servant when the main goes
	nighty-night; signal kills the child or itself on ^C of
	"quit" selected from the menu  */
	atexit(killDS);
	signal(SIGINT,killDS);
	signal(SIGCHLD,DSdied);
  
	/* send the servant window all the way to the front - even above
	the terminal window */
	sprintf(outstring,"DSfront\n");
	SENDPS;
	GETACK;
	/* get a string for the mouse; on a mouse click, we will get a
	string which is either "left mouse at ..." or "right mouse at ..."*/
	sprintf(outstring,"DSmouseon\n");
	SENDPS;
	GETACK;

	printf("Click inside the drawing window to become main,\n");
	printf("Then again for one end point.\n");

	/* do nothing until the string comes */
	do {
		read(inpipe[0],outstring,sizeof(outstring)-1);
	} while (sscanf(outstring,"%*s mouse %f %f",&lx,&ly)!=2 );

	for(i=0;i<1000;i++) {
		next_line(lx,ly,500*FRAND,500*FRAND);
	}
	printf("Click inside the terminal window\nThen Return to continue...");
	getchar();
}

void next_line(i,j,k,l)
float i,j,k,l;
{
	sprintf(outstring,"newpath %f %f moveto %f %f lineto stroke\n",
								i,j,k,l);
	SENDPS;
	GETACK;
}		
void DSdied()
{
	/* call _exit directly and avoid atexit call */
	_exit();
}
void killDS()
{
	sprintf(outstring,"DSquit\n");
	SENDPS;
}
	

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