                                                                >  #include <X/mit-copyright.h>

                                                                <  
/* $Header: xclock.c,v 10.18 86/11/19 18:40:33 jg Rel $ */         /* $Header: xclock.c,v 10.18 86/11/19 18:40:33 jg Rel $ */
/* Copyright 1985 Massachusetts Institute of Technology */         /* Copyright 1985 Massachusetts Institute of Technology */

/*                                                                 /*
 * xclock.c MIT Project Athena, X Window system clock.              * xclock.c MIT Project Athena, X Window system clock.
 *                                                                  *
 *  This program provides the user with a small                     *  This program provides the user with a small
 * window contining a digital clock with day and date.              * window contining a digital clock with day and date.
 * Parameters are variable from the command line.                   * Parameters are variable from the command line.
 *                                                                  *
 *  Author:	Tony Della Fera, DEC                                *  Author:	Tony Della Fera, DEC
 *		September, 1984                                     *		September, 1984
 * Hacked up by a cast of thousands....                             * Hacked up by a cast of thousands....
 */                                                                 */

#include <stdio.h>                                                 #include <stdio.h>
#include <math.h>                                                  #include <math.h>
#include <sys/time.h>                                              #include <sys/time.h>
#include <X/Xlib.h>                                                #include <X/Xlib.h>

#define VERTICES_IN_HANDS	4	/* to draw triangle */     #define VERTICES_IN_HANDS	4	/* to draw triangle */
#define PHI			(PI / 16.)	/* half angle      #define PHI			(PI / 16.)	/* half angle 
#define PI			3.14159265358979                   #define PI			3.14159265358979
#define PI4			(PI / 4.)                          #define PI4			(PI / 4.)
#define	SINPHI			0.19509032201613                   #define	SINPHI			0.19509032201613
#define TWOPI			(2. * PI)                          #define TWOPI			(2. * PI)

#define SEG_BUFF_SIZE		128                                #define SEG_BUFF_SIZE		128

#define SECOND_HAND_FRACT	90                                 #define SECOND_HAND_FRACT	90
#define MINUTE_HAND_FRACT	70                                 #define MINUTE_HAND_FRACT	70
#define HOUR_HAND_FRACT		40                                 #define HOUR_HAND_FRACT		40
#define HAND_WIDTH_FRACT	7                                  #define HAND_WIDTH_FRACT	7
#define SECOND_WIDTH_FRACT	5                                  #define SECOND_WIDTH_FRACT	5
#define SECOND_HAND_TIME	30                                 #define SECOND_HAND_TIME	30

#define DEF_UPDATE		60                                 #define DEF_UPDATE		60

#define DEF_BORDER		2                                  #define DEF_BORDER		2
#define DEF_VECTOR_HEIGHT	1                                  #define DEF_VECTOR_HEIGHT	1
#define DEF_VECTOR_WIDTH	1                                  #define DEF_VECTOR_WIDTH	1

#define DEF_DIGITAL_PADDING	10                                 #define DEF_DIGITAL_PADDING	10
#define DEF_DIGITAL_FONT	"6x10"                             #define DEF_DIGITAL_FONT	"6x10"
#define DEF_ANALOG_PADDING	8                                  #define DEF_ANALOG_PADDING	8
#define DEF_ANALOG_WIDTH	164                                #define DEF_ANALOG_WIDTH	164
#define DEF_ANALOG_HEIGHT	164                                #define DEF_ANALOG_HEIGHT	164

#define UNINIT			-1                                 #define UNINIT			-1
#define FAILURE			0                                  #define FAILURE			0

#define max(a, b) ((a) > (b) ? (a) : (b))                          #define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))                          #define min(a, b) ((a) < (b) ? (a) : (b))
#define abs(a) ((a) < 0 ? -(a) : (a))                              #define abs(a) ((a) < 0 ? -(a) : (a))

typedef enum _bool {FALSE, TRUE} Bool;                             typedef enum _bool {FALSE, TRUE} Bool;

Bool XClockDebug = FALSE;                                          Bool XClockDebug = FALSE;
Bool AnalogClock = FALSE;                                          Bool AnalogClock = FALSE;
Bool ShowSecondHand = FALSE;                                       Bool ShowSecondHand = FALSE;
Bool Reverse = FALSE;                                              Bool Reverse = FALSE;
Bool chime = FALSE;                                                Bool chime = FALSE;

int CenterX = 0;                                                   int CenterX = 0;
int CenterY = 0;                                                   int CenterY = 0;
int NumSegs = 0;                                                   int NumSegs = 0;

/*                                                              <  
 * actually the -vw (vector width) option sets both height and  <  
 * a square brush shape is recomended.                          <  
 */                                                             <  
int vector_width = DEF_VECTOR_WIDTH;                            <  
int vector_height = DEF_VECTOR_HEIGHT;                          <  
                                                                <  
int FrgrndColor;                                                   int FrgrndColor;
int BkgrndColor;                                                   int BkgrndColor;
int BorderColor;                                                   int BorderColor;
int HighColor;                                                     int HighColor;
int HandColor;                                                     int HandColor;

Vertex SegBuff[SEG_BUFF_SIZE];                                     Vertex SegBuff[SEG_BUFF_SIZE];
Vertex *SegBuffPtr;                                                Vertex *SegBuffPtr;

Window ClockWindow;                                                Window ClockWindow;

main(argc, argv)                                                   main(argc, argv)
	int argc;                                                  	int argc;
	char **argv;                                               	char **argv;
{                                                                  {
	char time_string[26];                                      	char time_string[26];
	register char *time_ptr = time_string;                     	register char *time_ptr = time_string;
#ifdef DEBUG                                                       #ifdef DEBUG
	register Bool debug = XClockDebug;                         	register Bool debug = XClockDebug;
#endif                                                             #endif
	register int i;                                            	register int i;
	register int radius;                                       	register int radius;
	register int padding = UNINIT;	/* UNINIT = parameter      	register int padding = UNINIT;	/* UNINIT = parameter 
	int border = UNINIT;		/* UNINIT = parameter      	int border = UNINIT;		/* UNINIT = parameter 
	int update = UNINIT;		/* UNINIT = parameter      	int update = UNINIT;		/* UNINIT = parameter 
	int win_width = UNINIT;         /* UNINIT = parameter      	int win_width = UNINIT;         /* UNINIT = parameter 
	int win_height = UNINIT;        /* UNINIT = parameter      	int win_height = UNINIT;        /* UNINIT = parameter 
	int second_hand_length;                                    	int second_hand_length;
	int minute_hand_length;                                    	int minute_hand_length;
	int hour_hand_length;                                      	int hour_hand_length;
	int hand_width;                                            	int hand_width;
	int second_width;                                          	int second_width;
	int readfds = 0;                                           	int readfds = 0;
	int maxfds = 0;                                            	int maxfds = 0;
	int string_width;                                          	int string_width;
	int status;                                                	int status;
	Bool even_update = FALSE;                                  	Bool even_update = FALSE;

	long time_value;                                           	long time_value;

	char *index();                                             	char *index();
	char *ctime(), asctim();                                   	char *ctime(), asctim();
	char *strind;                                              	char *strind;
	char *fn = DEF_DIGITAL_FONT;                               	char *fn = DEF_DIGITAL_FONT;
	char *fore_color;                                          	char *fore_color;
	char *back_color;                                          	char *back_color;
	char *high_color;                                          	char *high_color;
	char *hand_color;                                          	char *hand_color;
	char *brdr_color;                                          	char *brdr_color;
	char *geom = NULL;                                         	char *geom = NULL;
	char *display = NULL;                                      	char *display = NULL;
	int rvflag = 0;                                            	int rvflag = 0;
	char beeped = 0;		/* beeped already this     	char beeped = 0;		/* beeped already this

	Color cdef;                                                	Color cdef;

	struct tm *localtime();                                    	struct tm *localtime();
	struct timeval timeout;                                    	struct timeval timeout;
	struct tm tm;                                              	struct tm tm; 
	struct tm otm;                                             	struct tm otm; 

	Font font;                                                 	Font font;
	FontInfo font_info;                                        	FontInfo font_info; 
	XEvent event;                                              	XEvent event;
	char *def_val;                                             	char *def_val;

	def_val = XGetDefault(argv[0], "BorderWidth");             	def_val = XGetDefault(argv[0], "BorderWidth");
	if (def_val != NULL) border = atoi(def_val);               	if (def_val != NULL) border = atoi(def_val);

	def_val = XGetDefault(argv[0], "BodyFont");                	def_val = XGetDefault(argv[0], "BodyFont");
	if (def_val != NULL) fn = def_val;                         	if (def_val != NULL) fn = def_val;

	fore_color = XGetDefault(argv[0], "Foreground");           	fore_color = XGetDefault(argv[0], "Foreground");
	back_color = XGetDefault(argv[0], "Background");           	back_color = XGetDefault(argv[0], "Background");
	high_color = XGetDefault(argv[0], "Highlight");            	high_color = XGetDefault(argv[0], "Highlight");
	hand_color = XGetDefault(argv[0], "Hands");                	hand_color = XGetDefault(argv[0], "Hands");
	brdr_color = XGetDefault(argv[0], "Border");               	brdr_color = XGetDefault(argv[0], "Border");

	def_val = XGetDefault(argv[0], "ReverseVideo");            	def_val = XGetDefault(argv[0], "ReverseVideo");
	if(def_val != NULL && strcmp(def_val, "on") == 0) rvfl     	if(def_val != NULL && strcmp(def_val, "on") == 0) rvfl

	def_val = XGetDefault(argv[0], "InternalBorder");          	def_val = XGetDefault(argv[0], "InternalBorder");
	if (def_val != NULL) padding = atoi(def_val);              	if (def_val != NULL) padding = atoi(def_val);

	if ((def_val = XGetDefault(argv[0], "Mode")) != NULL)      	if ((def_val = XGetDefault(argv[0], "Mode")) != NULL) 
	    if (strcmp(def_val, "analog") == 0) AnalogClock =      	    if (strcmp(def_val, "analog") == 0) AnalogClock = 
	    if (strcmp(def_val, "digital") == 0) AnalogClock =     	    if (strcmp(def_val, "digital") == 0) AnalogClock =
	    if (strcmp(def_val, "Analog") == 0) AnalogClock =      	    if (strcmp(def_val, "Analog") == 0) AnalogClock = 
	    if (strcmp(def_val, "Digital") == 0) AnalogClock =     	    if (strcmp(def_val, "Digital") == 0) AnalogClock =
	}                                                          	}

	def_val = XGetDefault(argv[0],"Update");                   	def_val = XGetDefault(argv[0],"Update");
	if (def_val != NULL) update = atoi(def_val);               	if (def_val != NULL) update = atoi(def_val);

	for (i = 1; i < argc; i++) {                               	for (i = 1; i < argc; i++) {
		if (argv [i] [0] == '=') {                         		if (argv [i] [0] == '=') {
		        geom = argv[i];                            		        geom = argv[i];
		        continue;                                  		        continue;
		}                                                  		}
		strind = index(argv[i], ':');                      		strind = index(argv[i], ':');
		if(strind != NULL) {                               		if(strind != NULL) {
		        display = argv[i];                         		        display = argv[i];
			continue;                                  			continue;
		}                                                  		}
		strind = index(argv [i], '-');                     		strind = index(argv [i], '-');
		if (strind == NULL) Syntax(argv[0]);               		if (strind == NULL) Syntax(argv[0]);
		if (strcmp(argv [i], "-a") == 0 ||                 		if (strcmp(argv [i], "-a") == 0 ||
		    strcmp(argv [i], "-analog") == 0) {            		    strcmp(argv [i], "-analog") == 0) {
			AnalogClock = TRUE;                        			AnalogClock = TRUE;
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-d") == 0 ||                 		if (strcmp(argv [i], "-d") == 0 ||
		    strcmp(argv [i], "-digital") == 0) {           		    strcmp(argv [i], "-digital") == 0) {
			AnalogClock = FALSE;                       			AnalogClock = FALSE;
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-bw") == 0 ||                		if (strcmp(argv [i], "-bw") == 0 ||
		    strcmp(argv [i], "-border") == 0) {            		    strcmp(argv [i], "-border") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			border = atoi(argv [i]);                   			border = atoi(argv [i]);
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv[i], "-chime") == 0) {              		if (strcmp(argv[i], "-chime") == 0) {
			chime = TRUE;                              			chime = TRUE;
			continue;                                  			continue;
		}                                                  		}
#ifdef DEBUG                                                       #ifdef DEBUG
		if (strcmp(argv [i], "-debug") == 0) {             		if (strcmp(argv [i], "-debug") == 0) {
			XClockDebug = TRUE;                        			XClockDebug = TRUE;
			debug = TRUE;                              			debug = TRUE;
			continue;                                  			continue;
		}                                                  		}
#endif                                                             #endif
		if (strcmp(argv [i], "-fn") == 0 ||                		if (strcmp(argv [i], "-fn") == 0 ||
		    strcmp(argv [i], "-font") == 0) {              		    strcmp(argv [i], "-font") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			fn = argv [i];                             			fn = argv [i];
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-fg") == 0 ||                		if (strcmp(argv [i], "-fg") == 0 ||
		    strcmp(argv [i], "-foreground") == 0) {        		    strcmp(argv [i], "-foreground") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			fore_color = argv [i];                     			fore_color = argv [i];
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-bg") == 0 ||                		if (strcmp(argv [i], "-bg") == 0 ||
		    strcmp(argv [i], "-background") == 0) {        		    strcmp(argv [i], "-background") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			back_color = argv [i];                     			back_color = argv [i];
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-hl") == 0 ||                		if (strcmp(argv [i], "-hl") == 0 ||
		    strcmp(argv [i], "-highlight") == 0) {         		    strcmp(argv [i], "-highlight") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			high_color = argv [i];                     			high_color = argv [i];
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-hd") == 0 ||                		if (strcmp(argv [i], "-hd") == 0 ||
		    strcmp(argv [i], "-hands") == 0) {             		    strcmp(argv [i], "-hands") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			hand_color = argv [i];                     			hand_color = argv [i];
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-bd") == 0 ||                		if (strcmp(argv [i], "-bd") == 0 ||
		    strcmp(argv [i], "-bordercolor") == 0) {       		    strcmp(argv [i], "-bordercolor") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			brdr_color = argv [i];                     			brdr_color = argv [i];
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-help") == 0) {              		if (strcmp(argv [i], "-help") == 0) {
			Syntax(argv[0]);                           			Syntax(argv[0]);
		}                                                  		}
		if (strcmp(argv [i], "-p") == 0 ||                 		if (strcmp(argv [i], "-p") == 0 ||
		    strcmp(argv [i], "-padding") == 0) {           		    strcmp(argv [i], "-padding") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			padding = atoi(argv [i]);                  			padding = atoi(argv [i]);
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-rv") == 0 ||                		if (strcmp(argv [i], "-rv") == 0 ||
		    strcmp(argv [i], "-reverse") == 0) {           		    strcmp(argv [i], "-reverse") == 0) {
			rvflag++;                                  			rvflag++;
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-u") == 0 ||                 		if (strcmp(argv [i], "-u") == 0 ||
		    strcmp(argv [i], "-update") == 0) {            		    strcmp(argv [i], "-update") == 0) {
			if (++i >= argc) Syntax(argv[0]);          			if (++i >= argc) Syntax(argv[0]);
			update = atoi(argv [i]);                   			update = atoi(argv [i]);
			continue;                                  			continue;
		}                                                  		}
		if (strcmp(argv [i], "-vw") == 0 ||             <  
		    strcmp(argv [i], "-vectorwidth") == 0) {    <  
			if (++i >= argc) Syntax(argv[0]);       <  
			vector_width = vector_height = atoi(ar  <  
			continue;                               <  
		}                                               <  
		Syntax(argv[0]);                                   		Syntax(argv[0]);
	}                                                          	}

	/*                                                         	/*
	 * Open up the display.                                    	 * Open up the display.
	 */                                                        	 */
	if (XOpenDisplay(display) == NULL) {                       	if (XOpenDisplay(display) == NULL) {
		fprintf(stderr, "%s: Can't open display '%s'\n     		fprintf(stderr, "%s: Can't open display '%s'\n
		exit(1);                                           		exit(1);
	}                                                          	}
	if (rvflag) Reverse = TRUE;                                	if (rvflag) Reverse = TRUE;

	/*                                                         	/*
	 * Set up colors and pixmaps.                              	 * Set up colors and pixmaps.
	 */                                                        	 */
	if (                                                       	if (
	    DisplayCells() > 2 &&                                  	    DisplayCells() > 2 &&
	    brdr_color != NULL &&                                  	    brdr_color != NULL &&
	    XParseColor(brdr_color, &cdef) &&                      	    XParseColor(brdr_color, &cdef) &&
	    XGetHardwareColor(&cdef)                               	    XGetHardwareColor(&cdef)
	) BorderColor = cdef.pixel;                                	) BorderColor = cdef.pixel;
	else if (Reverse) BorderColor = WhitePixel;                	else if (Reverse) BorderColor = WhitePixel;
	else BorderColor = BlackPixel;                             	else BorderColor = BlackPixel;

	if (                                                       	if (
	    DisplayCells() > 2 &&                                  	    DisplayCells() > 2 &&
	    fore_color != NULL &&                                  	    fore_color != NULL &&
	    XParseColor(fore_color, &cdef) &&                      	    XParseColor(fore_color, &cdef) &&
	    XGetHardwareColor(&cdef)                               	    XGetHardwareColor(&cdef)
	) FrgrndColor = cdef.pixel;                                	) FrgrndColor = cdef.pixel;
	else if (Reverse) FrgrndColor = WhitePixel;                	else if (Reverse) FrgrndColor = WhitePixel;
	else FrgrndColor = BlackPixel;                             	else FrgrndColor = BlackPixel;

	if (                                                       	if (
	    DisplayCells() > 2 &&                                  	    DisplayCells() > 2 &&
	    back_color != NULL &&                                  	    back_color != NULL &&
	    XParseColor(back_color, &cdef) &&                      	    XParseColor(back_color, &cdef) &&
	    XGetHardwareColor(&cdef)                               	    XGetHardwareColor(&cdef)
	) BkgrndColor = cdef.pixel;                                	) BkgrndColor = cdef.pixel;
	else if (Reverse) BkgrndColor = BlackPixel;                	else if (Reverse) BkgrndColor = BlackPixel;
	else BkgrndColor = WhitePixel;                             	else BkgrndColor = WhitePixel;

	if (                                                       	if (
	    AnalogClock &&                                         	    AnalogClock &&
	    DisplayCells() > 2 &&                                  	    DisplayCells() > 2 &&
	    high_color != NULL &&                                  	    high_color != NULL &&
	    XParseColor(high_color, &cdef) &&                      	    XParseColor(high_color, &cdef) &&
	    XGetHardwareColor(&cdef)                               	    XGetHardwareColor(&cdef)
	) HighColor = cdef.pixel;                                  	) HighColor = cdef.pixel;
	else HighColor = FrgrndColor;                              	else HighColor = FrgrndColor;

	if (                                                       	if (
	    AnalogClock &&                                         	    AnalogClock &&
	    DisplayCells() > 2 &&                                  	    DisplayCells() > 2 &&
	    hand_color != NULL &&                                  	    hand_color != NULL &&
	    XParseColor(hand_color, &cdef) &&                      	    XParseColor(hand_color, &cdef) &&
	    XGetHardwareColor(&cdef)                               	    XGetHardwareColor(&cdef)
	) HandColor = cdef.pixel;                                  	) HandColor = cdef.pixel;
	else HandColor = BkgrndColor;                              	else HandColor = BkgrndColor;

	/*                                                         	/*
	 * Set up analog and digital specific defaults.            	 * Set up analog and digital specific defaults.
	 */                                                        	 */
	if (AnalogClock == TRUE) {                                 	if (AnalogClock == TRUE) {
		/*                                                 		/*
		 * Set analog defaults.                            		 * Set analog defaults.
		 */                                                		 */
		if (padding == UNINIT) padding = DEF_ANALOG_PA     		if (padding == UNINIT) padding = DEF_ANALOG_PA
		if (update == UNINIT) update = DEF_UPDATE;         		if (update == UNINIT) update = DEF_UPDATE;
		if (update <= SECOND_HAND_TIME) ShowSecondHand     		if (update <= SECOND_HAND_TIME) ShowSecondHand
	}                                                          	} 
	else {                                                     	else {
		/*                                                 		/* 
		 * Set digital defaults.                           		 * Set digital defaults.
		 */                                                		 */

		if (padding == UNINIT) padding = DEF_DIGITAL_P     		if (padding == UNINIT) padding = DEF_DIGITAL_P
		if (update == UNINIT) update = DEF_UPDATE;         		if (update == UNINIT) update = DEF_UPDATE;

		/*                                                 		/*
		 * Get font dependent information and determin     		 * Get font dependent information and determin
		 * size from a test string.                        		 * size from a test string.
		 */                                                		 */
		time(&time_value);                                 		time(&time_value);
		time_ptr = ctime(&time_value);                     		time_ptr = ctime(&time_value);
		time_ptr[strlen(time_ptr) - 1] = 0;                		time_ptr[strlen(time_ptr) - 1] = 0;

		font = XGetFont(fn);                               		font = XGetFont(fn);
		if (font == FAILURE) XClockError("Can't get fo     		if (font == FAILURE) XClockError("Can't get fo

		status = XQueryFont(font, &font_info);             		status = XQueryFont(font, &font_info);
		if (status == FAILURE) XClockError("Can't quer     		if (status == FAILURE) XClockError("Can't quer

		string_width = XQueryWidth (time_ptr, font);       		string_width = XQueryWidth (time_ptr, font);

		}                                                  		}


	/*                                                         	/* 
	 * Now set analog and digital independent defaults.        	 * Now set analog and digital independent defaults.
	 */                                                        	 */
	if (border == UNINIT) border = DEF_BORDER;                 	if (border == UNINIT) border = DEF_BORDER;
	if (update > 1 && ((60 / update) * update == 60)) even     	if (update > 1 && ((60 / update) * update == 60)) even

	/*                                                         	/*
	 * Open the main window.                                   	 * Open the main window.
	 */                                                        	 */
	{                                                          	{
	int min_width, min_height;                                 	int min_width, min_height;
	char default_geom[20];                                     	char default_geom[20];
	OpaqueFrame frame;                                         	OpaqueFrame frame;
	if (AnalogClock) {                                         	if (AnalogClock) {
		min_width = DEF_ANALOG_WIDTH/3;                    		min_width = DEF_ANALOG_WIDTH/3;
		min_height = DEF_ANALOG_HEIGHT/3;                  		min_height = DEF_ANALOG_HEIGHT/3;
		sprintf (default_geom,                             		sprintf (default_geom,
			 "%dx%d-0-0", DEF_ANALOG_WIDTH, DEF_AN     			 "%dx%d-0-0", DEF_ANALOG_WIDTH, DEF_AN
		}                                                  		}
	else {                                                     	else {
		min_width = string_width + (2 * padding);          		min_width = string_width + (2 * padding);
		min_height = font_info.height + (2 * padding);     		min_height = font_info.height + (2 * padding);
		sprintf (default_geom, "%dx%d-0-0", min_width,     		sprintf (default_geom, "%dx%d-0-0", min_width,
		}                                                  		}
	frame.bdrwidth = border;                                   	frame.bdrwidth = border;
	frame.border = XMakeTile (BorderColor);                    	frame.border = XMakeTile (BorderColor);
	frame.background = XMakeTile (BkgrndColor);                	frame.background = XMakeTile (BkgrndColor);
	ClockWindow = XCreate (                                    	ClockWindow = XCreate (
		AnalogClock ? "Analog XClock" : "Digital XCloc     		AnalogClock ? "Analog XClock" : "Digital XCloc
		argv[0],                                           		argv[0],
		geom,                                              		geom,
		default_geom,                                      		default_geom,
		&frame,                                            		&frame,
		min_width,                                         		min_width,
		min_height);                                       		min_height);
	if (ClockWindow == FAILURE)                                	if (ClockWindow == FAILURE)
		XClockError("Can't open clock window");            		XClockError("Can't open clock window");
	}                                                          	}

	/*                                                         	/*
	 * Select window exposure events to see if the content     	 * Select window exposure events to see if the content
	 * the window have been erased or altered.  Select unm     	 * the window have been erased or altered.  Select unm
	 * we can stop output when iconified.                      	 * we can stop output when iconified.
	 */                                                        	 */
	XSelectInput(ClockWindow, ExposeWindow|UnmapWindow|But  |  	XSelectInput(ClockWindow, ExposeWindow|UnmapWindow);

	/*                                                         	/*
	 * Map clock window to screen.                             	 * Map clock window to screen.
	 */                                                        	 */
	XMapWindow(ClockWindow);                                   	XMapWindow(ClockWindow);

	/*                                                         	/*
	 * Initialize the select system call's maximum file        	 * Initialize the select system call's maximum file
	 * descriptor number to be one more than the file desc     	 * descriptor number to be one more than the file desc
	 * number of the X connection.                             	 * number of the X connection.
	 */                                                        	 */
	maxfds = dpyno() + 1;                                      	maxfds = dpyno() + 1;

	/*                                                         	/*
	 * Initialize the select timeout structure.                	 * Initialize the select timeout structure.
	 */                                                        	 */
	timeout.tv_sec = update;                                   	timeout.tv_sec = update;
	timeout.tv_usec = 0;                                       	timeout.tv_usec = 0;

	/*                                                         	/*
	 * Synchronize X before proceeding.                        	 * Synchronize X before proceeding.
	 */                                                        	 */
	XSync(FALSE);                                              	XSync(FALSE);

	/*                                                         	/*
	 * Main clock loop.                                        	 * Main clock loop.
	 */                                                        	 */
	while (TRUE) {                                             	while (TRUE) {
		time(&time_value);                                 		time(&time_value);
		tm = *localtime(&time_value);                      		tm = *localtime(&time_value);
		if (even_update) {                                 		if (even_update) {
			/* Truncate to update interval, get ne     			/* Truncate to update interval, get ne
			timeout.tv_sec = update - tm.tm_sec;       			timeout.tv_sec = update - tm.tm_sec;
			tm.tm_sec = (tm.tm_sec / update) * upd     			tm.tm_sec = (tm.tm_sec / update) * upd
			timeout.tv_sec += tm.tm_sec;               			timeout.tv_sec += tm.tm_sec;
		}                                                  		}
		/*                                                 		/*
		 * Beep on the half hour; double-beep on the h     		 * Beep on the half hour; double-beep on the h
		 */                                                		 */
		if (chime == TRUE) {                               		if (chime == TRUE) {
			if (beeped && (tm.tm_min != 30) && (tm     			if (beeped && (tm.tm_min != 30) && (tm
				beeped = 0;                        				beeped = 0;
			if (((tm.tm_min == 30) || (tm.tm_min =     			if (((tm.tm_min == 30) || (tm.tm_min =
			    && (!beeped)) {                        			    && (!beeped)) {
				beeped = 1;                        				beeped = 1;
				XFeep(0);	                   				XFeep(0);	
				if (tm.tm_min == 0)                				if (tm.tm_min == 0)
					XFeep(0);                  					XFeep(0);
			}                                          			}
		}                                                  		}

		if (AnalogClock == FALSE) {	                   		if (AnalogClock == FALSE) {	
			/*                                         			/* 
			 * See if there are any events pending     			 * See if there are any events pending
			 * arn't supposed to be there.             			 * arn't supposed to be there.
			 */                                        			 */
			if (XPending() != 0) {                     			if (XPending() != 0) {
				/*                                 				/*
				 * There is an event pending s     				 * There is an event pending s
				 * check to see if it is an Ex     				 * check to see if it is an Ex
				 * event,                       |  				 * event, if it is anything el
                                                                >  				 * went wrong!
				 */                                				 */
				XNextEvent(&event);                				XNextEvent(&event);
				if (event.type == UnmapWindow)     				if (event.type == UnmapWindow)
					XPeekEvent(&event);        					XPeekEvent(&event);
					continue;                  					continue;
				}                                  				}
				if (event.type == ButtonPresse  <  
					exit(0);                <  
				if (event.type != ExposeWindow     				if (event.type != ExposeWindow
					XClockError("Unexpecte     					XClockError("Unexpecte
				}                                  				}
			}                                          			}
			time_ptr = asctime(&tm);                   			time_ptr = asctime(&tm);
			time_ptr[strlen(time_ptr) - 1] = 0;        			time_ptr[strlen(time_ptr) - 1] = 0;
			XTextPad (                                 			XTextPad (
				ClockWindow,                       				ClockWindow,
				padding, padding,                  				padding, padding, 
			    	time_ptr, strlen(time_ptr),        			    	time_ptr, strlen(time_ptr),
				font, 0, 0,                        				font, 0, 0, 
				FrgrndColor, BkgrndColor,          				FrgrndColor, BkgrndColor,
				GXcopy, AllPlanes                  				GXcopy, AllPlanes
			);                                         			);
		}                                                  		}
		else {                                             		else {
			/*                                         			/* 
			 * Look for an X_Event associated with     			 * Look for an X_Event associated with
			 */                                        			 */
			if (XPending() != 0) {                     			if (XPending() != 0) {
				/*                                 				/*
				 * There is an event pending s     				 * There is an event pending s
				 * check to see if it is an Ex     				 * check to see if it is an Ex
				 * event,                       |  				 * event, if it is anything el
                                                                >  				 * went wrong!
				 */                                				 */
				XNextEvent(&event);                				XNextEvent(&event);
				if (event.type == UnmapWindow)     				if (event.type == UnmapWindow)
					XPeekEvent(&event);        					XPeekEvent(&event);
					continue;                  					continue;
				}                                  				}
				if (event.type == ButtonPresse  <  
					exit(0);                <  
				if (event.type == ExposeWindow     				if (event.type == ExposeWindow
					/*                         					/*
					 * Ok, we have a windo     					 * Ok, we have a windo
					 * refresh the clock f     					 * refresh the clock f
					 * see if the window h     					 * see if the window h
					 */                        					 */
					XExposeWindowEvent *ex     					XExposeWindowEvent *ex
					 (XExposeWindowEvent *     					 (XExposeWindowEvent *
					if ((exp_event->width      					if ((exp_event->width 
				            (exp_event->height     				            (exp_event->height
						win_width = ex     						win_width = ex
						win_height = e     						win_height = e
						radius = (min(     						radius = (min(
						 win_height)-(     						 win_height)-(
						second_hand_le     						second_hand_le
							 ((SEC     							 ((SEC
							 radiu     							 radiu
							 / 100     							 / 100
						minute_hand_le     						minute_hand_le
							 ((MIN     							 ((MIN
							 radiu     							 radiu
						hour_hand_leng     						hour_hand_leng
							((HOUR     							((HOUR
							radius     							radius
						hand_width =       						hand_width =
							 ((HAN     							 ((HAN
							 radiu     							 radiu
						second_width =     						second_width =
							 ((SEC     							 ((SEC
							 radiu     							 radiu
						CenterX = win_     						CenterX = win_
						CenterY = win_     						CenterY = win_
					}                          					}
					DrawClockFace(second_h     					DrawClockFace(second_h
						radius);           						radius);
				}                                  				}
				else {                             				else {
					/*                         					/*
					 * We should never get     					 * We should never get
					 */                        					 */
					XClockError("Unexpecte     					XClockError("Unexpecte
				}                                  				}
			}                                          			}
			/*                                         			/*
			 * The second (or minute) hand is sec      			 * The second (or minute) hand is sec 
			 * sixtieths around the clock face. Th     			 * sixtieths around the clock face. Th
			 * (hour + min/60) twelfths of the way     			 * (hour + min/60) twelfths of the way
			 * clock-face.  The derivation is left     			 * clock-face.  The derivation is left
			 * for the reader.                         			 * for the reader.
			 */                                        			 */

			/*                                         			/*
			 * 12 hour clock.                          			 * 12 hour clock.
			 */                                        			 */
			if(tm.tm_hour > 12)                        			if(tm.tm_hour > 12)
				tm.tm_hour -= 12;                  				tm.tm_hour -= 12;

			/*                                         			/*
			 * Erase old hands.                        			 * Erase old hands.
			 */                                        			 */
			if(NumSegs > 0) {                          			if(NumSegs > 0) {
			    if (ShowSecondHand == TRUE) {          			    if (ShowSecondHand == TRUE) {
				XDraw(                             				XDraw(
				    ClockWindow,                   				    ClockWindow,
				    &SegBuff[2 * VERTICES_IN_H     				    &SegBuff[2 * VERTICES_IN_H
				    NumSegs - 2 * VERTICES_IN_     				    NumSegs - 2 * VERTICES_IN_
				    DEF_VECTOR_WIDTH,              				    DEF_VECTOR_WIDTH,
				    DEF_VECTOR_HEIGHT,             				    DEF_VECTOR_HEIGHT,
				    BkgrndColor, GXcopy, AllPl     				    BkgrndColor, GXcopy, AllPl
				);                                 				);
				if(HandColor != BkgrndColor) {     				if(HandColor != BkgrndColor) {
				    XDrawFilled(                   				    XDrawFilled(
					ClockWindow,               					ClockWindow,
					&SegBuff[2*VERTICES_IN     					&SegBuff[2*VERTICES_IN
					NumSegs - 2*VERTICES_I     					NumSegs - 2*VERTICES_I
					BkgrndColor, GXcopy, A     					BkgrndColor, GXcopy, A
				    );                             				    );
				}                                  				}
			    }                                      			    }
			    if(                                    			    if(
				tm.tm_min != otm.tm_min ||         				tm.tm_min != otm.tm_min ||
				tm.tm_hour != otm.tm_hour          				tm.tm_hour != otm.tm_hour
			    ) {                                    			    ) {
				XDraw(                             				XDraw(
				    ClockWindow, SegBuff,          				    ClockWindow, SegBuff,
				    2 * VERTICES_IN_HANDS,         				    2 * VERTICES_IN_HANDS,
				    DEF_VECTOR_WIDTH, DEF_VECT     				    DEF_VECTOR_WIDTH, DEF_VECT
				    BkgrndColor, GXcopy, AllPl     				    BkgrndColor, GXcopy, AllPl
				);                                 				);
				if(HandColor != BkgrndColor) {     				if(HandColor != BkgrndColor) {
				    XDrawFilled(                   				    XDrawFilled(
					ClockWindow,               					ClockWindow,
					SegBuff, VERTICES_IN_H     					SegBuff, VERTICES_IN_H
					BkgrndColor, GXcopy, A     					BkgrndColor, GXcopy, A
				    );                             				    );
				    XDrawFilled(                   				    XDrawFilled(
					ClockWindow,               					ClockWindow,
					&SegBuff[VERTICES_IN_H     					&SegBuff[VERTICES_IN_H
					VERTICES_IN_HANDS,         					VERTICES_IN_HANDS,
					BkgrndColor, GXcopy, A     					BkgrndColor, GXcopy, A
				    );                             				    );
				}                                  				}
			    }                                      			    }
			}                                          			}

			if (                                       			if (
			    NumSegs == 0 ||                        			    NumSegs == 0 ||
			    tm.tm_min != otm.tm_min ||             			    tm.tm_min != otm.tm_min ||
			    tm.tm_hour != otm.tm_hour              			    tm.tm_hour != otm.tm_hour
			) {                                        			) {
			    SegBuffPtr = SegBuff;                  			    SegBuffPtr = SegBuff;
			    NumSegs = 0;                           			    NumSegs = 0;
			    /*                                     			    /*
			     * Calculate the hour hand, fill i     			     * Calculate the hour hand, fill i
			     * color and then outline it.  Nex     			     * color and then outline it.  Nex
			     * with the minute hand.  This is      			     * with the minute hand.  This is 
			     * line algorithm.                     			     * line algorithm.
			     */                                    			     */
			    DrawHand(                              			    DrawHand(
				minute_hand_length, hand_width     				minute_hand_length, hand_width
				((double) tm.tm_min)/60.0          				((double) tm.tm_min)/60.0
			    );                                     			    );
			    if(HandColor != BkgrndColor)           			    if(HandColor != BkgrndColor)
				XDrawFilled(                       				XDrawFilled(
				    ClockWindow,                   				    ClockWindow,
				    SegBuff, VERTICES_IN_HANDS     				    SegBuff, VERTICES_IN_HANDS
				    HandColor, GXcopy, AllPlan     				    HandColor, GXcopy, AllPlan
				);                                 				);
			    XDraw(                                 			    XDraw(
				ClockWindow,                       				ClockWindow,
				SegBuff, VERTICES_IN_HANDS,        				SegBuff, VERTICES_IN_HANDS,
				DEF_VECTOR_WIDTH, DEF_VECTOR_H     				DEF_VECTOR_WIDTH, DEF_VECTOR_H
				HighColor, GXcopy, AllPlanes       				HighColor, GXcopy, AllPlanes
			    );                                     			    );

			    DrawHand(                              			    DrawHand(
				hour_hand_length, hand_width,      				hour_hand_length, hand_width,
				((((double)tm.tm_hour) +           				((((double)tm.tm_hour) + 
				    (((double)tm.tm_min)/60.0)     				    (((double)tm.tm_min)/60.0)
			    );                                     			    );
			    XDrawFilled(                           			    XDrawFilled(
				ClockWindow,                       				ClockWindow,
				&SegBuff[VERTICES_IN_HANDS], V     				&SegBuff[VERTICES_IN_HANDS], V
				HandColor, GXcopy, AllPlanes       				HandColor, GXcopy, AllPlanes
			    );                                     			    );

			    i = VERTICES_IN_HANDS;                 			    i = VERTICES_IN_HANDS;
			} else i = 2 * VERTICES_IN_HANDS;          			} else i = 2 * VERTICES_IN_HANDS;

			if (ShowSecondHand == TRUE) {              			if (ShowSecondHand == TRUE) {
			    NumSegs = 2 * VERTICES_IN_HANDS;       			    NumSegs = 2 * VERTICES_IN_HANDS;
			    SegBuffPtr = &SegBuff[NumSegs];        			    SegBuffPtr = &SegBuff[NumSegs];
			    DrawSecond(                            			    DrawSecond(
				second_hand_length - 2, second     				second_hand_length - 2, second
				minute_hand_length + 2,            				minute_hand_length + 2,
				((double) tm.tm_sec)/60.0          				((double) tm.tm_sec)/60.0
			    );                                     			    );
			    if(HandColor != BkgrndColor)           			    if(HandColor != BkgrndColor)
				XDrawFilled(                       				XDrawFilled(
				    ClockWindow,                   				    ClockWindow,
				    &SegBuff[2 * VERTICES_IN_H     				    &SegBuff[2 * VERTICES_IN_H
				    NumSegs - 2 * VERTICES_IN_     				    NumSegs - 2 * VERTICES_IN_
				    HandColor, GXcopy, AllPlan     				    HandColor, GXcopy, AllPlan
			    );                                     			    );
			}                                          			}
			XDraw(                                     			XDraw(
			    ClockWindow, &SegBuff[i], NumSegs      			    ClockWindow, &SegBuff[i], NumSegs 
			    DEF_VECTOR_WIDTH, DEF_VECTOR_HEIGH     			    DEF_VECTOR_WIDTH, DEF_VECTOR_HEIGH
			    HighColor, GXcopy, AllPlanes           			    HighColor, GXcopy, AllPlanes
			);                                         			);
			otm = tm;                                  			otm = tm;
		}                                                  		}
		XFlush ();                                         		XFlush ();

		/*                                                 		/*
		 * Use the select system call on the file desc     		 * Use the select system call on the file desc
		 * the display structure to determine if there     		 * the display structure to determine if there
		 * to be done.  If not block untill timeout.       		 * to be done.  If not block untill timeout.  
		 * reset the file descriptor before each selec     		 * reset the file descriptor before each selec
		 */                                                		 */
		readfds = 1 << dpyno();                            		readfds = 1 << dpyno();
		if (select(maxfds, &readfds, NULL, NULL, &time     		if (select(maxfds, &readfds, NULL, NULL, &time
		XClockError("Error in select on display file d     		XClockError("Error in select on display file d
	}                                                          	}
}                                                                  }

/*                                                                 /*
 * DrawLine - Draws a line.                                         * DrawLine - Draws a line.
 *                                                                  *
 * blank_length is the distance from the center which the line      * blank_length is the distance from the center which the 
 * length is the maximum length of the hand.                        * length is the maximum length of the hand.
 * Fraction_of_a_circle is a fraction between 0 and 1 (inclusi      * Fraction_of_a_circle is a fraction between 0 and 1 (inc
 * how far around the circle (clockwise) from high noon.            * how far around the circle (clockwise) from high noon.
 *                                                                  *
 * The blank_length feature is because I wanted to draw tick-m      * The blank_length feature is because I wanted to draw ti
 * circle (for seconds).  The obvious means of drawing lines f      * circle (for seconds).  The obvious means of drawing lin
 * to the perimeter, then erasing all but the outside most pix      * to the perimeter, then erasing all but the outside most
 * work because of round-off error (sigh).                          * work because of round-off error (sigh).
 */                                                                 */
DrawLine(blank_length, length, fraction_of_a_circle)               DrawLine(blank_length, length, fraction_of_a_circle)
	int blank_length;                                          	int blank_length;
	int length;                                                	int length;
	double fraction_of_a_circle;                               	double fraction_of_a_circle;
{                                                                  {

	register double angle, cosangle, sinangle;                 	register double angle, cosangle, sinangle;
	double cos();                                              	double cos();
	double sin();                                              	double sin();

	/*                                                         	/*
	 *  A full circle is 2 PI radians.                         	 *  A full circle is 2 PI radians.
	 *  Angles are measured from 12 o'clock, clockwise inc     	 *  Angles are measured from 12 o'clock, clockwise inc
	 *  Since in X, +x is to the right and +y is downward:     	 *  Since in X, +x is to the right and +y is downward:
	 *                                                         	 *
	 *	x = x0 + r * sin(theta)                            	 *	x = x0 + r * sin(theta)
	 *	y = y0 - r * cos(theta)                            	 *	y = y0 - r * cos(theta)
	 *                                                         	 *
	 */                                                        	 */
	angle = TWOPI * fraction_of_a_circle;                      	angle = TWOPI * fraction_of_a_circle;
	cosangle = cos(angle);                                     	cosangle = cos(angle);
	sinangle = sin(angle);                                     	sinangle = sin(angle);

	/*                                                         	/*
	 * Add the move instruction to the segment buffer and      	 * Add the move instruction to the segment buffer and 
	 * the "next" pointer.                                     	 * the "next" pointer.
	 */                                                        	 */
	SetSeg(CenterX + (int)(blank_length * sinangle),           	SetSeg(CenterX + (int)(blank_length * sinangle),
	 CenterY - (int)(blank_length * cosangle), VertexDontD     	 CenterY - (int)(blank_length * cosangle), VertexDontD

	/*                                                         	/*
	 * Add the new point to the buffer and increment the "     	 * Add the new point to the buffer and increment the "
	 * pointer.                                                	 * pointer.
	 */                                                        	 */
	SetSeg(CenterX + (int)(length * sinangle),                 	SetSeg(CenterX + (int)(length * sinangle),
	 CenterY - (int)(length * cosangle), VertexDrawLastPoi     	 CenterY - (int)(length * cosangle), VertexDrawLastPoi
}                                                                  }

/*                                                                 /*
 * DrawHand - Draws a hand.                                         * DrawHand - Draws a hand.
 *                                                                  *
 * length is the maximum length of the hand.                        * length is the maximum length of the hand.
 * width is the half-width of the hand.                             * width is the half-width of the hand.
 * Fraction_of_a_circle is a fraction between 0 and 1 (inclusi      * Fraction_of_a_circle is a fraction between 0 and 1 (inc
 * how far around the circle (clockwise) from high noon.            * how far around the circle (clockwise) from high noon.
 *                                                                  *
 */                                                                 */
DrawHand(length, width, fraction_of_a_circle)                      DrawHand(length, width, fraction_of_a_circle)
	int length, width;                                         	int length, width;
	double fraction_of_a_circle;                               	double fraction_of_a_circle;
{                                                                  {

	register double angle, cosangle, sinangle;                 	register double angle, cosangle, sinangle;
	register Vertex *tip;                                      	register Vertex *tip;
	register double ws, wc;                                    	register double ws, wc;
	double cos();                                              	double cos();
	double sin();                                              	double sin();

	/*                                                         	/*
	 *  A full circle is 2 PI radians.                         	 *  A full circle is 2 PI radians.
	 *  Angles are measured from 12 o'clock, clockwise inc     	 *  Angles are measured from 12 o'clock, clockwise inc
	 *  Since in X, +x is to the right and +y is downward:     	 *  Since in X, +x is to the right and +y is downward:
	 *                                                         	 *
	 *	x = x0 + r * sin(theta)                            	 *	x = x0 + r * sin(theta)
	 *	y = y0 - r * cos(theta)                            	 *	y = y0 - r * cos(theta)
	 *                                                         	 *
	 */                                                        	 */
	angle = TWOPI * fraction_of_a_circle;                      	angle = TWOPI * fraction_of_a_circle;
	cosangle = cos(angle);                                     	cosangle = cos(angle);
	sinangle = sin(angle);                                     	sinangle = sin(angle);
	/*                                                         	/*
	 * Order of points when drawing the hand.                  	 * Order of points when drawing the hand.
	 *                                                         	 *
	 *		1,4                                        	 *		1,4
	 *		/ \                                        	 *		/ \
	 *	       /   \                                       	 *	       /   \
	 *	      /     \                                      	 *	      /     \
	 *	    2 ------- 3                                    	 *	    2 ------- 3
	 */                                                        	 */
	tip = SegBuffPtr;                                          	tip = SegBuffPtr;
	SetSeg(CenterX + round(length * sinangle),                 	SetSeg(CenterX + round(length * sinangle),
	 CenterY - round(length * cosangle), VertexDontDraw);      	 CenterY - round(length * cosangle), VertexDontDraw);

	wc = width * cosangle;                                     	wc = width * cosangle;
	ws = width * sinangle;                                     	ws = width * sinangle;
	/* 2 */                                                    	/* 2 */
	SetSeg(CenterX - round(ws + wc), CenterY + round(wc -      	SetSeg(CenterX - round(ws + wc), CenterY + round(wc - 
	 VertexDrawLastPoint);                                     	 VertexDrawLastPoint);
	/* 3 */                                                    	/* 3 */
	SetSeg(CenterX - round(ws - wc), CenterY + round(wc +      	SetSeg(CenterX - round(ws - wc), CenterY + round(wc + 
	 VertexDrawLastPoint);                                     	 VertexDrawLastPoint);

	SetSeg(tip->x, tip->y, VertexDrawLastPoint);	/* 4 *     	SetSeg(tip->x, tip->y, VertexDrawLastPoint);	/* 4 *
}                                                                  }

/*                                                                 /*
 * DrawSecond - Draws the second hand (diamond).                    * DrawSecond - Draws the second hand (diamond).
 *                                                                  *
 * length is the maximum length of the hand.                        * length is the maximum length of the hand.
 * width is the half-width of the hand.                             * width is the half-width of the hand.
 * offset is direct distance from center to tail end.               * offset is direct distance from center to tail end.
 * Fraction_of_a_circle is a fraction between 0 and 1 (inclusi      * Fraction_of_a_circle is a fraction between 0 and 1 (inc
 * how far around the circle (clockwise) from high noon.            * how far around the circle (clockwise) from high noon.
 *                                                                  *
 */                                                                 */
DrawSecond(length, width, offset, fraction_of_a_circle)            DrawSecond(length, width, offset, fraction_of_a_circle)
	int length, width, offset;                                 	int length, width, offset;
	double fraction_of_a_circle;                               	double fraction_of_a_circle;
{                                                                  {

	register double angle, cosangle, sinangle;                 	register double angle, cosangle, sinangle;
	register Vertex *tip;                                      	register Vertex *tip;
	register double ms, mc, ws, wc;                            	register double ms, mc, ws, wc;
	register int mid;                                          	register int mid;
	double cos();                                              	double cos();
	double sin();                                              	double sin();

	/*                                                         	/*
	 *  A full circle is 2 PI radians.                         	 *  A full circle is 2 PI radians.
	 *  Angles are measured from 12 o'clock, clockwise inc     	 *  Angles are measured from 12 o'clock, clockwise inc
	 *  Since in X, +x is to the right and +y is downward:     	 *  Since in X, +x is to the right and +y is downward:
	 *                                                         	 *
	 *	x = x0 + r * sin(theta)                            	 *	x = x0 + r * sin(theta)
	 *	y = y0 - r * cos(theta)                            	 *	y = y0 - r * cos(theta)
	 *                                                         	 *
	 */                                                        	 */
	angle = TWOPI * fraction_of_a_circle;                      	angle = TWOPI * fraction_of_a_circle;
	cosangle = cos(angle);                                     	cosangle = cos(angle);
	sinangle = sin(angle);                                     	sinangle = sin(angle);
	/*                                                         	/*
	 * Order of points when drawing the hand.                  	 * Order of points when drawing the hand.
	 *                                                         	 *
	 *		1,5                                        	 *		1,5
	 *		/ \                                        	 *		/ \
	 *	       /   \                                       	 *	       /   \
	 *	      /     \                                      	 *	      /     \
	 *	    2<       >4                                    	 *	    2<       >4
	 *	      \     /                                      	 *	      \     /
	 *	       \   /                                       	 *	       \   /
	 *		\ /                                        	 *		\ /
	 *	-	 3                                         	 *	-	 3
	 *	|                                                  	 *	|
	 *	|                                                  	 *	|
	 *   offset                                                	 *   offset
	 *	|                                                  	 *	|
	 *	|                                                  	 *	|
	 *	-	 + center                                  	 *	-	 + center
	 */                                                        	 */
	tip = SegBuffPtr;                                          	tip = SegBuffPtr;
	SetSeg(CenterX + round(length * sinangle),                 	SetSeg(CenterX + round(length * sinangle),
	 CenterY - round(length * cosangle), VertexDontDraw);      	 CenterY - round(length * cosangle), VertexDontDraw);

	mid = (length + offset) / 2;                               	mid = (length + offset) / 2;
	mc = mid * cosangle;                                       	mc = mid * cosangle;
	ms = mid * sinangle;                                       	ms = mid * sinangle;
	wc = width * cosangle;                                     	wc = width * cosangle;
	ws = width * sinangle;                                     	ws = width * sinangle;
	/* 2 */                                                    	/* 2 */
	SetSeg(CenterX + round(ms - wc), CenterY - round(mc +      	SetSeg(CenterX + round(ms - wc), CenterY - round(mc + 
	 VertexDrawLastPoint);                                     	 VertexDrawLastPoint);
	SetSeg(CenterX + round(offset * sinangle),                 	SetSeg(CenterX + round(offset * sinangle),
	 CenterY - round(offset * cosangle), VertexDrawLastPoi     	 CenterY - round(offset * cosangle), VertexDrawLastPoi
	/* 4 */                                                    	/* 4 */
	SetSeg(CenterX + round(ms + wc), CenterY - round(mc -      	SetSeg(CenterX + round(ms + wc), CenterY - round(mc - 
	 VertexDrawLastPoint);                                     	 VertexDrawLastPoint);

	SetSeg(tip->x, tip->y, VertexDrawLastPoint);	/* 5 *     	SetSeg(tip->x, tip->y, VertexDrawLastPoint);	/* 5 *
}                                                                  }

SetSeg(x, y, flag)                                                 SetSeg(x, y, flag)
int x, y, flag;                                                    int x, y, flag;
{                                                                  {
	SegBuffPtr->x = x;                                         	SegBuffPtr->x = x;
	SegBuffPtr->y = y;                                         	SegBuffPtr->y = y;
	(SegBuffPtr++)->flags = flag;                              	(SegBuffPtr++)->flags = flag;
	NumSegs++;                                                 	NumSegs++;
}                                                                  }

/*                                                                 /*
 *  Draw the clock face (every fifth tick-mark is longer            *  Draw the clock face (every fifth tick-mark is longer
 *  than the others).                                               *  than the others).
 */                                                                 */
DrawClockFace(second_hand, radius)                                 DrawClockFace(second_hand, radius)
	int second_hand;                                           	int second_hand;
	int radius;                                                	int radius;
{                                                                  {
	register Bool debug = XClockDebug;                         	register Bool debug = XClockDebug;
	register int i;                                            	register int i;
	register int delta = (radius - second_hand) / 3;           	register int delta = (radius - second_hand) / 3;

	XClear(ClockWindow);                                       	XClear(ClockWindow);
	SegBuffPtr = SegBuff;                                      	SegBuffPtr = SegBuff;
	NumSegs = 0;                                               	NumSegs = 0;
	for (i = 0; i < 60; i++)                                   	for (i = 0; i < 60; i++)
		DrawLine((i % 5) == 0 ? second_hand : (radius      		DrawLine((i % 5) == 0 ? second_hand : (radius 
		 ((double) i)/60.);                                		 ((double) i)/60.);
	/*                                                         	/*
	 * Go ahead and draw it.                                   	 * Go ahead and draw it.
	 */                                                        	 */
	XDraw(ClockWindow, SegBuff, NumSegs, vector_width,      |  	XDraw(ClockWindow, SegBuff, NumSegs, DEF_VECTOR_WIDTH,
	vector_height, FrgrndColor, GXcopy, AllPlanes);         |  	 DEF_VECTOR_HEIGHT, FrgrndColor, GXcopy, AllPlanes);
	SegBuffPtr = SegBuff;                                      	SegBuffPtr = SegBuff;
	NumSegs = 0;                                               	NumSegs = 0;
}                                                                  }

round(x)                                                           round(x)
double x;                                                          double x;
{                                                                  {
	return(x >= 0 ? (int)(x + .5) : (int)(x - .5));            	return(x >= 0 ? (int)(x + .5) : (int)(x - .5));
}                                                                  }

/*                                                                 /*
 * Report the syntax for calling xclock.                            * Report the syntax for calling xclock.
 */                                                                 */
Syntax(call)                                                       Syntax(call)
	char *call;                                                	char *call;
{                                                                  {
	printf ("Usage: %s [-analog] [-bw <pixels>] [-digital]     	printf ("Usage: %s [-analog] [-bw <pixels>] [-digital]
	printf ("       [-fg <color>] [-bg <color>] [-hl <colo     	printf ("       [-fg <color>] [-bg <color>] [-hl <colo
	printf ("       [-fn <font_name>] [-help] [-padding <p     	printf ("       [-fn <font_name>] [-help] [-padding <p
	printf ("       [-rv] [-update <seconds>] [[<host>]:[<     	printf ("       [-rv] [-update <seconds>] [[<host>]:[<
	printf ("       [-vw] <vector width>\n");               <  
	printf ("       [=[<width>][x<height>][<+-><xoff>[<+->     	printf ("       [=[<width>][x<height>][<+-><xoff>[<+->
	printf ("Default: %s -digital -bw %d -font %s -padding     	printf ("Default: %s -digital -bw %d -font %s -padding
		call, DEF_BORDER, DEF_DIGITAL_FONT, DEF_DIGITA     		call, DEF_BORDER, DEF_DIGITAL_FONT, DEF_DIGITA
	printf ("Default: %s -analog -bw %d -padding %d -updat     	printf ("Default: %s -analog -bw %d -padding %d -updat
		call, DEF_BORDER, DEF_DIGITAL_PADDING, DEF_UPD     		call, DEF_BORDER, DEF_DIGITAL_PADDING, DEF_UPD
		DEF_ANALOG_WIDTH, DEF_ANALOG_HEIGHT);              		DEF_ANALOG_WIDTH, DEF_ANALOG_HEIGHT);
	printf ("Notes: The order in which switches are specif     	printf ("Notes: The order in which switches are specif
	printf ("       In analog mode the second hand only ap     	printf ("       In analog mode the second hand only ap
	printf ("       less than or equal to %d seconds.\n",      	printf ("       less than or equal to %d seconds.\n", 
	exit(0);                                                   	exit(0);
}                                                                  }


/*                                                                 /*
 * XClockError - Fatal xclock error.                                * XClockError - Fatal xclock error.
 */                                                                 */
XClockError (identifier)                                           XClockError (identifier)
	char *identifier;                                          	char *identifier;
{                                                                  {
	register Bool debug = XClockDebug;                         	register Bool debug = XClockDebug;

	if (debug) printf("XClockError: Fatal xclock error enc     	if (debug) printf("XClockError: Fatal xclock error enc
	perror("xclock");                                          	perror("xclock");
	fprintf(stderr, "xclock: %s\n", identifier);               	fprintf(stderr, "xclock: %s\n", identifier);

	exit(1);                                                   	exit(1);
}                                                                  }

/*                                                                 /*
 * End of xclock.c                                                  * End of xclock.c
 */                                                                 */
