home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / curses-clock < prev    next >
Text File  |  1989-10-22  |  4KB  |  225 lines

  1. Subject:  v20i045:  Curses-based digital clock
  2. Newsgroups: comp.sources.unix
  3. Sender: sources
  4. Approved: rsalz@uunet.UU.NET
  5.  
  6. Submitted-by: John Lupien <jrl@mvuxr.att.com>
  7. Posting-number: Volume 20, Issue 45
  8. Archive-name: curses-clock
  9.  
  10. [  Yes, Virginia, not everyone was bitmapped graphics... /r$  ]
  11.  
  12. Amos Shapir wrote this digital clock program for VT100 compatibles,
  13. and posted it without makefile to some relatively lax newsgroup.
  14.  
  15. I upgraded my terminal to an AT&T 5620, and of course the clock
  16. stopped working, so I converted the screen management stuff to curses.
  17. It is short and sweet, and now runs quietly in my topmost window.
  18.  
  19. -------------------cut here--------------------cut here---------------------
  20. #To unpack, delete all lines before this and feed to /bin/sh
  21. echo Makefile 1>&2
  22. sed -e 's/^X//' >Makefile <<'END'
  23. X# Makefile for gdc curses version 10-18-89
  24. X
  25. Xgdc: gdc.c
  26. X    cc -o gdc -O gdc.c -lcurses
  27. X
  28. END
  29. echo gdc.6 1>&2
  30. sed -e 's/^X//' >gdc.6 <<'END'
  31. X.TH GDC 6
  32. X.SH NAME
  33. Xgdc \- grand digital clock (curses)
  34. X.SH SYNOPSIS
  35. X.B gdc
  36. X[-s] [
  37. X.I n
  38. X]
  39. X.SH DESCRIPTION
  40. X.I Gdc
  41. Xruns a digital clock made of reverse-video blanks on a curses
  42. Xcompatible VDU screen. With an optional numeric argument
  43. X.I n
  44. Xit stops after
  45. X.I n
  46. Xseconds (default never).
  47. XThe optional
  48. X.B -s
  49. Xflag makes digits scroll as they change. In this curses mode implementation,
  50. Xthe scrolling option has trouble keeping up.
  51. X.SH AUTHOR
  52. XAmos Shapir, modified for curses by John Lupien.
  53. END
  54. echo gdc.c 1>&2
  55. sed -e 's/^X//' >gdc.c <<'END'
  56. X/*
  57. X * Grand digital clock for curses compatible terminals
  58. X * Usage: gdc [-s] [n]   -- run for n seconds (default infinity)
  59. X * Flags: -s: scroll
  60. X *
  61. X * modified 10-18-89 for curses (jrl)
  62. X * 10-18-89 added signal handling
  63. X */
  64. X
  65. X#include <stdio.h>
  66. X#include <time.h>
  67. X#include <curses.h>
  68. X
  69. X/* it won't be */
  70. Xlong now; /* yeah! */
  71. Xstruct tm *tm;
  72. X
  73. Xshort disp[11] = {
  74. X    075557, 011111, 071747, 071717, 055711,
  75. X    074717, 074757, 071111, 075757, 075717, 002020
  76. X};
  77. Xlong old[6], next[6], new[6], mask;
  78. Xchar scrol;
  79. X
  80. Xint sigtermed=0;
  81. X
  82. Xvoid sighndl(signo)
  83. Xint signo;
  84. X{
  85. X    signal(signo, sighndl);
  86. X    sigtermed=signo;
  87. X}
  88. X
  89. Xmain(argc, argv)
  90. X    char **argv;
  91. X{
  92. X    register long t, a;
  93. X    register i, j, s, n, k;
  94. X    signal(1,sighndl);
  95. X    signal(2,sighndl);
  96. X    signal(3,sighndl);
  97. X    signal(15,sighndl);
  98. X
  99. X    initscr();
  100. X    clr();
  101. X    refresh();
  102. X    while(--argc > 0) {
  103. X        if(**++argv == '-')
  104. X            scrol = 1;
  105. X        else
  106. X            n = atoi(*argv);
  107. X    }
  108. X    do {
  109. X        mask = 0;
  110. X        time(&now);
  111. X        tm = localtime(&now);
  112. X        set(tm->tm_sec%10, 0);
  113. X        set(tm->tm_sec/10, 4);
  114. X        set(tm->tm_min%10, 10);
  115. X        set(tm->tm_min/10, 14);
  116. X        set(tm->tm_hour%10, 20);
  117. X        set(tm->tm_hour/10, 24);
  118. X        set(10, 7);
  119. X        set(10, 17);
  120. X        for(k=0; k<6; k++) {
  121. X            if(scrol) {
  122. X                for(i=0; i<5; i++)
  123. X                    new[i] = new[i]&~mask | new[i+1]&mask;
  124. X                new[5] = new[5]&~mask | next[k]&mask;
  125. X            } else
  126. X                new[k] = new[k]&~mask | next[k]&mask;
  127. X            next[k] = 0;
  128. X            for(s=1; s>=0; s--)
  129. X            {
  130. X                standt(s);
  131. X                for(i=0; i<6; i++)
  132. X                {
  133. X                    if(a = (new[i]^old[i])&(s ? new : old)[i])
  134. X                    {
  135. X                        for(j=0,t=1<<26; t; t>>=1,j++)
  136. X                        {
  137. X                            if(a&t)
  138. X                            {
  139. X                                if(!(a&(t<<1)))
  140. X                                {
  141. X                                    movto(i, 2*j);
  142. X                                }
  143. X                                addstr("  ");
  144. X                            }
  145. X                        }
  146. X                    }
  147. X                    if(!s)
  148. X                    {
  149. X                        old[i] = new[i];
  150. X                    }
  151. X                }
  152. X                if(!s)
  153. X                {
  154. X                    refresh();
  155. X                }
  156. X            }
  157. X        }
  158. X        movto(6, 0);
  159. X        refresh();
  160. X        sleep(1);
  161. X        if (sigtermed)
  162. X        {
  163. X            standend();
  164. X            clear();
  165. X            refresh();
  166. X            endwin();
  167. X            fprintf(stderr, "gcl terminated by signal %d\n", sigtermed);
  168. X            exit(1);
  169. X        }
  170. X    } while(--n);
  171. X    standend();
  172. X    clear();
  173. X    refresh();
  174. X    endwin();
  175. X    return(0);
  176. X}
  177. X
  178. Xset(t, n)
  179. X    register n;
  180. X{
  181. X    register i, m;
  182. X
  183. X    m = 7<<n;
  184. X    for(i=0; i<5; i++) {
  185. X        next[i] |= ((disp[t]>>(4-i)*3)&07)<<n;
  186. X        mask |= (next[i]^old[i])&m;
  187. X    }
  188. X    if(mask&m)
  189. X        mask |= m;
  190. X}
  191. X
  192. X/* terminal-dependent routines */
  193. Xclr()
  194. X{
  195. X    clear();
  196. X    refresh();
  197. X}
  198. X
  199. Xstandt(on)
  200. Xint on;
  201. X{
  202. X    if (on)
  203. X    {
  204. X        standout();
  205. X    }
  206. X    else
  207. X    {
  208. X        standend();
  209. X    }
  210. X}
  211. X
  212. Xmovto(line,col)
  213. X{
  214. X    move(line, col);
  215. X}
  216. X
  217. END
  218.  
  219. -- 
  220.     -John Lupien
  221.     mvuxr!jrl
  222.     jrl@mvuxr.att.com
  223.  
  224.  
  225.