home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume20 / cursescl.ock < prev    next >
Encoding:
Text File  |  1989-10-23  |  4.3 KB  |  232 lines

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