-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terminal_clock.c
365 lines (283 loc) · 8.38 KB
/
Terminal_clock.c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#define DATE_ROW 1
#define DATE_COL 15
#define ROW_SIZE 5 //declaring constants
#define COL_SIZE 6
#define LEVEL_SIZE 11
#define RESET 0
#define BRIGHT 1
#define COLOURSIZES 8
#define ZERO 0
#define BACKGROUND 0
#define DOTSIGN 10
/* prototype functions
*/
void timedisplay();
void printwindows();
void initializewindows();
WINDOW *create_newwin(int height, int width, int starty, int startx);
void timesetdigits(int hour,int min,int sec);
void displaydigit(int timedigits,WINDOW *win1,WINDOW *win2);
void digitprint(int numprint, WINDOW *win);
void refreshwins();
void printwindows(WINDOW *win,char* strngs);
void clearwins();
int colourchecker(char **argv,int argcount);
void errorprint(int argc, char **argv);
void intcolours();
void displaydays(int year,int mont,int day);
/* Declaring global variables
*/
const int Disp_array[LEVEL_SIZE][ROW_SIZE][COL_SIZE]={ //3d array for numbers 0-9 and symbol ':'
{{1,1,1,1,1,1},{1,1,0,0,1,1},{1,1,0,0,1,1},{1,1,0,0,1,1},{1,1,1,1,1,1}}, //Number 0
{{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1}}, //Number 1
{{1,1,1,1,1,1},{0,0,0,0,1,1},{1,1,1,1,1,1},{1,1,0,0,0,0},{1,1,1,1,1,1}}, //2
{{1,1,1,1,1,1},{0,0,0,0,1,1},{1,1,1,1,1,1},{0,0,0,0,1,1},{1,1,1,1,1,1}}, //3
{{1,1,0,0,1,1},{1,1,0,0,1,1},{1,1,1,1,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1}}, //4
{{1,1,1,1,1,1},{1,1,0,0,0,0},{1,1,1,1,1,1},{0,0,0,0,1,1},{1,1,1,1,1,1}}, //5
{{1,1,1,1,1,1},{1,1,0,0,0,0},{1,1,1,1,1,1},{1,1,0,0,1,1},{1,1,1,1,1,1}}, //6
{{1,1,1,1,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1},{0,0,0,0,1,1}}, //7
{{1,1,1,1,1,1},{1,1,0,0,1,1},{1,1,1,1,1,1},{1,1,0,0,1,1},{1,1,1,1,1,1}}, //8
{{1,1,1,1,1,1},{1,1,0,0,1,1},{1,1,1,1,1,1},{0,0,0,0,1,1},{1,1,1,1,1,1}}, //9
{{0,0,0,0,0,0},{0,0,1,1,0,0},{0,0,0,0,0,0},{0,0,1,1,0,0},{0,0,0,0,0,0}}, // symbol ':'
};
char *coloursavailable[]={"black","red","green","yellow","blue","magenta","cyan","white"}; //colours available
char colour='C';
char help='h';
int forecolour=7;
WINDOW *HOUR1,*HOUR2,*MIN1,*MIN2,*SEC1,*SEC2,*DATE,*DOT1,*DOT2; //declaring ncurses windows
void intcolours(){
/*
colour pairs ncurses library
*/
init_pair(1,BACKGROUND,forecolour);
init_pair(2,forecolour,BACKGROUND);
}
void displaydays(int year,int mont,int day){
/*
printing year day and month in the terminal
*/
wattron(DATE,COLOR_PAIR(2));
wprintw(DATE,"%d-%02d-%02d\n",1900+year,mont+1,day);
wattroff(DATE,COLOR_PAIR(2));
}
void timesetdigits(int hour,int min,int sec)
{
/*
setting the digits to deplay and ':' symbol between hour ,min and sec
*/
displaydigit(hour,HOUR1,HOUR2);
digitprint(DOTSIGN,DOT1);
displaydigit(min,MIN1,MIN2);
digitprint(DOTSIGN,DOT2);
displaydigit(sec,SEC1,SEC2);
}
void displaydigit(int timedigits,WINDOW *win1,WINDOW *win2){
/*
getting two digits from timedigits and print them in terminal
*/
int digit1,digit2;
digit1=timedigits/10;
digit2=timedigits%10;
digitprint(digit1,win1);
digitprint(digit2,win2);
}
void timedisplay(){
/*
function for get values from the system time day month and year
*/
int hour,min,sec,day,mont,year;
time_t settime;
struct tm* timevals;
time(&settime);
timevals=localtime(&settime);
hour=timevals->tm_hour;
min=timevals->tm_min;
sec=timevals->tm_sec;
day=timevals->tm_mday;
mont=timevals->tm_mon;
year=timevals->tm_year;
timesetdigits(hour,min,sec);
displaydays(year,mont,day);
}
void digitprint(int numprint, WINDOW *win){
/*
function to print digits 5x6 matrix size
*/
int row,col;
for (row = 0; row <ROW_SIZE; ++row)
{
for (col = 0; col <COL_SIZE; ++col)
{
if(Disp_array[numprint][row][col]==1){
wattron(win,COLOR_PAIR(1));
wprintw(win," ");
wattroff(win,COLOR_PAIR(1));
}else{
wprintw(win," ");
}
}
}
}
void initializewindows(){
/*
innitializing ncruses windows for each digits
*/
HOUR1= create_newwin(ROW_SIZE,COL_SIZE,1,1);
HOUR2= create_newwin(ROW_SIZE,COL_SIZE,1,8);
DOT1= create_newwin(ROW_SIZE,COL_SIZE,1,14);
MIN1= create_newwin(ROW_SIZE,COL_SIZE,1,20);
MIN2= create_newwin(ROW_SIZE,COL_SIZE,1,27);
DOT2= create_newwin(ROW_SIZE,COL_SIZE,1,33);
SEC1= create_newwin(ROW_SIZE,COL_SIZE,1,39);
SEC2= create_newwin(ROW_SIZE,COL_SIZE,1,46);
DATE= create_newwin(DATE_ROW,DATE_COL,7,22);
}
int colourchecker(char **argv,int argcount){
/*
function to check whether input colour is available or not,if available return colour value.
if there is no colour inputs it print error message how to use colours
*/
int count;
char *colourset=argv[argcount]; //asigning the -C or -C<colour> string
char *stringcolour;
for (count= 0; count<COLOURSIZES; ++count)
{
if(strstr(colourset,coloursavailable[count])!=NULL)
{ /* here checking if the -C<colour> is available colour or otherwise print error
message
*/
stringcolour=strstr(colourset,coloursavailable[count]);
if(strcmp(stringcolour,coloursavailable[count])==0){
printf("%s\n",stringcolour );
return count;
}
else{
fprintf(stderr, " '%s' :This is not a valid color, Please enter one of these colours: black, red, green, yellow, blue, magenta, cyan, white\n",stringcolour );
exit(0);
}
}
else if(count==COLOURSIZES-1){
int countar;
int stringlenght=(int)strlen(colourset);
//stringcolour[stringlenght-2]='\0';
for(countar=2;countar<stringlenght;countar++)
{
stringcolour[countar-2]=colourset[countar];
//printf("%c\n",stringcolour1[countar] );
}
stringcolour[stringlenght-2]='\0';
if(stringlenght!=2){
printf("%s\n",stringcolour);
fprintf(stderr, " '%s' :This is not a valid color, Please enter one of these colours: black, red, green, yellow, blue, magenta, cyan, white\n",stringcolour );
exit(0);
}
break;
}
}
if(argv[argcount+1]==NULL){
/*-C <colour> ; when there is no colour entered print error
*/
fprintf(stderr, "%s: option requires an argument -- 'C'\n",argv[0]);
fprintf(stdout, "usage : clock -C <black|red|green|yellow|blue|magenta|cyan|white]\n");
exit(0);
}
for (count= 0; count<COLOURSIZES; ++count)
{ //if there is -C <colour> available return the colour value
if(strcmp(coloursavailable[count],argv[argcount+1])==0)
{
return count;
}
}
//if there is -C <colour> available this error will print and exit the prog
fprintf(stderr, " '%s' :This is not a valid color, Please enter one of these colours: black, red, green, yellow, blue, magenta, cyan, white\n",argv[argcount+1] );
exit(0);
}
void errorprint(int argc, char **argv){
/*
this assign the colour from colourchecker func.if notprint errors when the args are wrong
*/
int argcount;
for (argcount = 0; argcount < argc; ++argcount)
{
if( argv[argcount][0]=='-'){
if(argv[argcount][1]==colour){
forecolour=colourchecker(argv,argcount);
break;
}else if( argv[argcount][1]==help){
fprintf(stdout, "usage : clock -C <black|red|green|yellow|blue|magenta|cyan|white]\n");
exit(0);
}else if( argv[argcount][0]=='-'){
int argrun;
for(argrun=1;argrun<strlen(argv[argcount]);argrun++){
fprintf(stderr, "%s: invalid option -- '%c'\n",argv[0],argv[argcount][argrun]);
}
fprintf(stdout, "usage : clock -C <black|red|green|yellow|blue|magenta|cyan|white]\n");
exit(0);
}
}
}
}
void refreshwins(){
/*
refreshing ncurses windows
*/
wrefresh(HOUR1);
wrefresh(HOUR2);
wrefresh(MIN1);
wrefresh(MIN2);
wrefresh(SEC1);
wrefresh(SEC2);
wrefresh(DOT1);
wrefresh(DOT2);
wrefresh(DATE);
}
void clearwins(){
/*
clearing ncurses windows
*/
wclear(HOUR1);
wclear(HOUR2);
wclear(MIN1);
wclear(MIN2);
wclear(SEC1);
wclear(SEC2);
wclear(DOT1);
wclear(DOT2);
wclear(DATE);
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0); /* 0, 0 gives default characters
* for the vertical and horizontal
* lines */
wrefresh(local_win); /* Show that box */
return local_win;
}
int main(int argc, char **argv){
/*
main function here making a clock loop to print
*/
errorprint(argc,argv);
initscr();
noecho();
curs_set(FALSE);
start_color();
initializewindows();
intcolours();
while(1){
clearwins();
timedisplay();
refreshwins();
//usleep(timer); //sleeping the clock for timer microsecond
}
endwin();
curs_set(TRUE);
return 0;
}