Files
nClock/nclock.c
2017-11-27 19:43:08 +01:00

99 lines
2.5 KiB
C

#include <curses.h> //ncurses windows
#include <stdlib.h> //atexit
#include <string.h> //memset
#include <time.h> //time
#include "nclock.h"
#include "numbers.h"
#include <unistd.h> //sleep
#include <signal.h> //sigaction
void handle_winch(int sig) {
/* This function handles the console resizing */
endwin();
refresh();
}
int main(int argc, char **argv) {
/* enable ncurses standard screen */
initscr();
atexit(quitProgram); //cleanup at exit
/* Some look and feel changes */
nonl(); //no newline
keypad(stdscr, FALSE); //Disable the F1-12 keypad
curs_set(0); //Disable the cursor
//Windows resize handler
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handle_winch;
sigaction(SIGWINCH, &sa, NULL);
//Define the state struct
state s = {0};
//start the clock
draw(&s);
return 0;
}
void quitProgram(void) {
endwin();
}
void initState(state *s) {
getmaxyx(stdscr, s->maxy, s->maxx);
s->centery = --s->maxy / 2;
s->centerx = --s->maxx / 2;
}
void drawNumber(int starty, int startx, int num) {
int i, j, numarr[2];
for (i = 0; i < 2; i++) {
numarr[i] = num % 10;
num /= 10;
}
for (j = 0; j < 2; j++) {
for (i = 0; i < 5; i++) {
mvprintw(starty + i, startx + (6 * j), number[numarr[1 - j]][i]);
}
}
}
void drawColon(int starty, int startx) {
int i;
for (i = 0; i < 5; i++) {
mvprintw(starty + i, startx, colon[i]);
}
}
void drawTime(state *s, int hour, int min, int sec) {
int newcenty = s->centery - 3; //start above the center
/* center X: */
/* (3 Nuberpairs + 2 colons) / 2 */
int newcentx = s->centerx - ((3 * NUMBERPAIR + 2 * COLONSPACE) / 2);
/* hour */
drawNumber(newcenty, newcentx, hour);
drawColon(newcenty, newcentx + NUMBERPAIR);
/* minute */
drawNumber(newcenty, newcentx + NUMBERPAIR + COLONSPACE, min);
drawColon(newcenty, newcentx + NUMBERPAIR * 2 + COLONSPACE);
drawNumber(newcenty, newcentx + NUMBERPAIR * 2 + COLONSPACE * 2, sec);
}
void draw(state *s) {
time_t timer;
struct tm *curTime;
while (1) {
time(&timer);
curTime = localtime(&timer);
initState(s); //This will manage the refresh of center positions on resizing.
clear(); //because there were some artifacts
drawTime(s, curTime->tm_hour, curTime->tm_min, curTime->tm_sec);
refresh();
sleep(1);
}
}