summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/clock.c
blob: 4b0b52abab97b6a650d2ad0fc812be55ff65d7d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* CLOCK.H:  prototypes/defines for the CLOCK.C file                    */
/*
    This file written in 1990 Jonathan R. Guthrie and placed in the
    public domain
*/

int     far     startclock(int x, int y, int attr);
void    far     stopclock(void);

#define CLOCK_OK        0   /* Clock was installed properly             */
#define CLOCK_ERR_INST  -1  /* Clock is already installed               */
#define CLOCK_ERR_OOS   -2  /* Clock would be off the screen            */


/* CLOCK.C:  An on-screen clock generator                               */
/*
    This file written in 1990 Jonathan R. Guthrie and placed in the
    public domain
*/

#include    <stdlib.h>
#include    <conio.h>
#include    <time.h>
#include    <dos.h>
#include    "clock.h"

#define CLOCK       0x1c
#define TRUE        1
#define FALSE       0

/* Now, the stuff needed for the for the on-screen clock                */

static  void    interrupt   (*oldvector)(void);
static  void    interrupt   do_clock(void);
static  void    setstr(char *, int, size_t);

static  char    timestuff[22], hours, oddfives, ticks, ticklimit;

static  int     clockx, clocky, clockattr, installed = FALSE;

int     far     startclock(int x, int y, int attr)
{
      time_t  temptime;
      struct  tm  *struct_time;
      int     bigx;
      struct  text_info   r;

      /* The clock starting routine */

      /* First, see if it's already installed */

      if(installed)
            return  CLOCK_ERR_INST;
      else  installed = TRUE;

      /* Now, set assorted important module constants */

      clockx = x;
      clocky = y;
      clockattr = attr;

      gettextinfo(&r);

      if((clockx < 0) || (clockx > r.screenwidth - 10) ||
         (clocky < 0) || (clocky > r.screenheight))
      {
            installed = FALSE;
            return CLOCK_ERR_OOS;
      }

      /* Now, set the program's clock */

      setstr(timestuff, clockattr, 22);

      time(&temptime);
      struct_time = localtime(&temptime);

      oddfives    = 0;
      ticklimit   = 17;
      hours         = (struct_time->tm_hour + 11) % 12 + 1;
      timestuff[0]  = (hours > 9) ? '1' : ' ';
      timestuff[2]  = '0' + hours % 10;
      timestuff[4]  = ':';
      timestuff[6]  = '0' + struct_time->tm_min / 10;
      timestuff[8]  = '0' + struct_time->tm_min % 10;
      timestuff[10] = ':';
      timestuff[12] = '0' + struct_time->tm_sec / 10;
      timestuff[14] = '0' + struct_time->tm_sec % 10;
      timestuff[16] = ' ';
      timestuff[18] = (struct_time->tm_hour > 11) ? 'P' : 'A';
      timestuff[20] = 'M';

      /* Now, initialize the clock as displayed on the screen */

      puttext(clockx, clocky, clockx+10, clocky, timestuff);

      /* Finally, set the vector to point to the clock routine */

      disable();
      oldvector = getvect(CLOCK);
      setvect(CLOCK, do_clock);
      enable();
      return  CLOCK_OK;
}

static  void    interrupt   do_clock(void)
{
      ++ticks;
      if(ticks > ticklimit)   /* Then, it's time to update the seconds */
      {
            ticks = 0;

            /* First, handle the fractional Hz part */

            ++oddfives;
            if (5 == oddfives)
            {
                  oddfives = 0;
                  ticklimit = 18;
            }
            else
            {
                  ticklimit = 17;
            }
   
            /* Now, handle the seconds count */

            ++timestuff[14];

            if (timestuff[14] > '9')
            {
                  timestuff[14] = '0';

                  /* Now, handle the tens of seconds count */

                  ++timestuff[12];
                  if (timestuff[12] > '5')
                  {
                        timestuff[12] = '0';

                        /* Now, handle the minutes count */

                        ++timestuff[8];
                        if (timestuff[8] > '9')
                        {
                              timestuff[8] = '0';

                              /* Now, handle the ten minutes count */

                              ++timestuff[6];
                              if (timestuff[6] > '5')
                              {
                                    timestuff[6] = '0';

                                    /* Now, handle the hours count */

                                    ++hours;
                                    if(12 == hours)
                                          if ('P' == timestuff[18])
                                                timestuff[18] = 'A';
                                          else  timestuff[18] = 'P';

                                    if(hours > 12)
                                          hours = 1;

                                    timestuff[0] = (hours > 9) ? '1' : ' ';
                                    timestuff[2] = '0' + hours % 10;
                              }
                        }
                  }
            }

            /* Now, update the display */

            puttext(clockx, clocky, clockx+10, clocky, timestuff);
      }
}

void    far     stopclock(void)
{
      if(installed)
      {
            disable();
            setvect(CLOCK, oldvector);
            enable();
            installed = FALSE;
      }
}

static  void    setstr(char *s, int ch, size_t n)
{
      size_t  i;
    
      for(i=0 ; i<n ; ++i)
      {
            s[i] = ch;
      }

      s[i] = '\0';
}