head     1.1;
access   ;
symbols  ;
locks    ;
comment  @ * @;


1.1
date     89.06.20.20.33.23;  author pkern;  state Exp;
branches ;
next     ;


desc
@@



1.1
log
@Initial revision
@
text
@/*                        Copyright (c) 1987 Bellcore
 *                            All Rights Reserved
 *       Permission is granted to copy or use this program, EXCEPT that it
 *       may not be sold for profit, the copyright notice must be reproduced
 *       on copies, and credit should be given to Bellcore where it is due.
 *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
 */
/*	$Header: clock2.c,v 4.2 88/06/22 14:37:27 bianchi Exp $
	$Source: /tmp/mgrsrc/demo/misc/RCS/clock2.c,v $
*/
static char	RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/clock2.c,v $$Revision: 4.2 $";

/* get today's date  (analog clock version) */

#include <errno.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>

#define OFFSET		(500<<10)		/* shift points to 1st quad */

/* sin, cos tables: 0-360 deg. in 6 deg. increments (scaled to +/- 1024) */

int sin[] = {
	0, 107, 212, 316, 416, 512,
	601, 685, 760, 828, 886, 935,
	973, 1001, 1018, 1023, 1018, 1001,
	973, 935, 886, 828, 760, 685,
	601, 511, 416, 316, 212, 107,
	0, -107, -212, -316, -416, -512,
	-601, -685, -760, -828, -886, -935,
	-973, -1001, -1018, -1023, -1018, -1001,
	-973, -935, -886, -828, -760, -685,
	-601, -511, -416, -316, -212, -107,
	0 };


int cos[] = {
	1023, 1018, 1001, 973, 935, 886,
	828, 760, 685, 601, 511, 416,
	316, 212, 107, 0, -107, -212,
	-316, -416, -512, -601, -685, -760,
	-828, -886, -935, -973, -1001, -1018,
	-1023, -1018, -1001, -973, -935, -886,
	-828, -760, -685, -601, -511, -416,
	-316, -212, -107, 0, 107, 212,
	316, 416, 512, 601, 685, 760,
	828, 886, 935, 973, 1001, 1018,
	1023 };

typedef struct coord {
	int x,y;
} coord;

/* coordinates of the hands at 12:00 */

coord second[] = { 1,-30,  0,445 };
coord minute[] = { 10,-10,  0,400,  -10,-10,  10,-10 };
coord hour[] = { 35,-10,  0,270,  -35,-10,  35,-10 };

coord big_tic[] = { -11,485,  0,450,  11,485 };
coord tic[] = { 0,485,  0,460 };

int h, old_h, m, old_m, s, old_s;

extern gfx_limits();
int ymax=1000, xmax=1000, cmax=8;
int yofs=0, xofs=0;
#define ypix(a)	(ymax*(a)/1000)
#define xpix(a)	(xmax*(a)/1000)

main(argc,argv)
int argc;
char **argv;
{
	long now;
	int i, cal=0, clean();

	gfx_limits(&xmax, &ymax, &cmax);

	signal(SIGTERM,clean);
	signal(SIGINT,clean);
	signal(SIGHUP,clean);

	if (argc > 1) {
		char *s, *index();
		int xalt, yalt;

		if (argv[1][0] == '-') {
			fprintf(stderr, "usage: %s [<width>x<height> [<x-offset>,<y-offset>]]\n", argv[0]);
			exit(1);
		}
		if (argc > 2) {
			xofs = atoi(argv[2]);
			s = index(argv[2], ',');
			yofs = (s) ? atoi(++s) : xofs;
			if (0 > xofs || xofs > xmax) xofs = 0;
			if (0 > yofs || yofs > ymax) yofs = 0;
		}

		xalt = atoi(argv[1]);
		s = index(argv[1], 'x');
		yalt = (s) ? atoi(++s) : xalt;
		if (0 < xalt && xalt <= xmax) {
			if (xalt+xofs > xmax) xofs = xmax-xalt;
			xmax = xalt;
		}
		if (0 < yalt && yalt <= ymax) {
			if (yalt+yofs > ymax) yofs = ymax-yalt;
			ymax = yalt;
		}
	}

	/* clear graphics, colour=cmax-1, go & draw border */
	printf("\033@@J\033@@%dm\033@@%d;%dH\033@@%d;%dL",
		cmax-1, ypix(500)+yofs, xpix(500)+xofs,
		ypix(490), xpix(490));

	for(i=0;i<60;i+=5) 		/* the tic marks */
		if (i%15==0)
			draw(big_tic,2,i);
		else
			draw(tic,1,i);

	get_time(&h, &m, &s);

	draw(hour,3,h);
	draw(minute,3,m);
	draw(second,1,s);

	while(1) {
		old_h=h, old_m=m, old_s=s;
		get_time(&h, &m, &s);

		if (old_s == s) continue;

#ifndef XOR
		printf("\033@@0m");
#endif
		draw(second,1,old_s);
		if (old_m != m) draw(minute,3,old_m);
		if (old_h != h) draw(hour,3,old_h);

#ifdef XOR
		draw(second,1,s);
		if (old_m != m) draw(minute,3,m);
		if (old_h != h) draw(hour,3,h);
#else
		printf("\033@@%dm", cmax-1);
		draw(second,1,s);
		draw(minute,3,m);
		draw(hour,3,h);
#endif
		fflush(stdout);
	}
}

/* rotate and draw hands */

#define X(i)	(what[i].x)
#define Y(i)	(what[i].y)

draw(what, n, theta)
coord what[];			/* array of points */
int n;				/* number of points */
int theta;			/* angle (in 160th of a circle) */
{
	register int i;
	long p, q, r, s;

	for(i=0;i<n;i++) {
		p = ypix((X(i)*sin[theta]-Y(i)*cos[theta] + OFFSET)>>10);
		q = xpix((X(i)*cos[theta]+Y(i)*sin[theta] + OFFSET)>>10);
		r = ypix((X(i+1)*sin[theta]-Y(i+1)*cos[theta] + OFFSET)>>10);
		s = xpix((X(i+1)*cos[theta]+Y(i+1)*sin[theta] + OFFSET)>>10);
		printf("\033@@%d;%dH\033@@%d;%dh",
			p+yofs, q+xofs, r+yofs, s+xofs);
	}
}

/* convert time to angles */

get_time(h,m,s)
int *h, *m, *s;		/* hours, minutes, seconds */
{
	struct tm *tme, *localtime();
	long tmp,time();

	tmp = time(0);
	tme = localtime(&tmp);

	*m = tme->tm_min;
	*s = tme->tm_sec;
	if (tme->tm_hour > 11)
		tme->tm_hour -= 12;
	*h = tme->tm_hour*5 + (2+tme->tm_min)/12;
}

/* clean up and exit */

clean(n)
int	n;
{
	printf("\033@@J");
	exit(1);
}
@
