]> arthur.barton.de Git - aclock.git/blob - aclock-unix-termcap.c
Add missing includes, GCC 6.3 errored out
[aclock.git] / aclock-unix-termcap.c
1 /*
2  * aclock - ascii clock for UNIX Console - termcap port
3  *
4  * Copyright (c) 1994-2013 Antoni Sawicki <as@tenoware.com>
5  * Version 2.3 (unix-termcap); Mountain View, July 2013
6  *
7  * Compilation: cc aclock-unix-termcap.c -o aclock -ltermcap -lm
8  * Built on: BeOS, Zeta, SkyOS, Haiku, Amiga-GG, UnixWare, Risc/OS
9  *
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <termios.h>
15 #include <termcap.h>
16 #include <math.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include <sys/ioctl.h>
20
21 #ifndef M_PI
22 #define M_PI 3.14159265358979323846
23 #endif
24
25 #define FontWH_Ratio 2  
26
27 char *cm, *cl;
28
29 void my_putchar(int c) {
30     putchar(c);
31 }
32
33 void cls(void) {
34     tputs(tgoto(cl, 0, 0), 1, (void*)my_putchar);
35 }
36
37 void draw_point(int x, int y, char c) {
38     tputs(tgoto(cm, x, y), 1, (void*)my_putchar);
39     putchar(c);
40 }
41
42 void draw_text(int x, int y, char *string) {
43     tputs(tgoto(cm, x, y), 1, (void*)my_putchar);
44     puts(string);
45 }
46
47
48 void draw_circle(int hand_max, int sYcen, int sXcen){
49     int x,y,r;
50     char c;
51
52     for(r=0;r<60;r++){
53         x=cos(r*M_PI/180*6)*hand_max*FontWH_Ratio+sXcen;
54         y=sin(r*M_PI/180*6)*hand_max+sYcen;
55         switch (r) {
56             case 0:
57             case 5:
58             case 10:
59             case 15:
60             case 20:
61             case 25:
62             case 30:
63             case 35:
64             case 40:
65             case 45:
66             case 50:
67             case 55:
68                 c='o';
69                 break;
70             default:
71                 c='.';
72                 break;
73         }
74         draw_point(x,y,c);
75     }
76 }
77
78 void draw_hand(int minute, int hlenght, char c, int sXcen, int sYcen){
79     int x,y,n;
80     float r=(minute-15)*(M_PI/180)*6;
81
82     for(n=1; n<hlenght; n++){
83         x=cos(r)*n*FontWH_Ratio+sXcen;
84         y=sin(r)*n+sYcen;
85         draw_point(x,y,c);
86     }
87 }
88
89
90 int main(int argc, char **argv){
91     char INFO[]="Copyright (c) 1994-2013 Antoni Sawicki <as@tenoware.com>\n"
92                 "Version 2.3 (unix-termcap); Mountain View, July 2013\n";
93     char digital_time[32];
94     int sXmax, sYmax, sXmaxo, sYmaxo, smax, hand_max, sXcen, sYcen;
95     time_t t;
96     struct tm *ltime;
97     char *term_env;
98     char term_buffer[2048];
99     struct winsize ws;
100     struct termios tios;
101
102
103
104     term_env=getenv("TERM");
105
106     if(!term_env) {
107         fprintf(stderr, "no terminal type defined\n");
108         exit(1);
109     }
110
111     if(!tgetent(NULL, term_env)) {
112         fprintf(stderr, "unknown terminal type\n");
113         exit(1);
114     }
115
116     cm=(char*)tgetstr("cm", NULL);
117     cl=(char*)tgetstr("cl", NULL);
118
119     sXmaxo=sYmaxo=sXmax=sYmax=0;
120
121     while(1){
122         sXmaxo=sXmax;
123         sYmaxo=sYmax;
124
125         ioctl(1, TIOCGWINSZ, &ws);
126
127         sXmax = ws.ws_col;
128         sYmax = ws.ws_row;
129
130         if((sXmax!=sXmaxo) || (sYmax!=sYmaxo)) {
131             if(sXmax/2<=sYmax)
132                     smax=sXmax/2;
133             else
134                 smax=sYmax;
135
136             hand_max = (smax/2)-1;
137
138             sXcen = sXmax/2;
139             sYcen = sYmax/2;
140
141             cls();
142             draw_circle(hand_max, sYcen, sXcen);
143         }
144
145         time(&t);
146         ltime=localtime(&t);
147
148         draw_hand((ltime->tm_hour*5)+(ltime->tm_min/10), 2*hand_max/3, 'h', sXcen, sYcen);
149         draw_hand(ltime->tm_min, hand_max-2, 'm', sXcen, sYcen);
150         draw_hand(ltime->tm_sec, hand_max-1, '.', sXcen, sYcen);
151
152         draw_text(sXcen-5, sYcen-(3*hand_max/5), ".:ACLOCK:.");
153         sprintf(digital_time, "[%02d:%02d:%02d]", ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
154         draw_text(sXcen-5, sYcen+(3*hand_max/5), digital_time);
155         
156         fflush(stdout);
157         sleep(1);
158
159         draw_hand((ltime->tm_hour*5)+(ltime->tm_min/10), 2*hand_max/3, ' ', sXcen, sYcen);
160         draw_hand(ltime->tm_min, hand_max-2, ' ', sXcen, sYcen);
161         draw_hand(ltime->tm_sec, hand_max-1, ' ', sXcen, sYcen);
162     }
163     return 0;
164 }