126 lines
3.2 KiB
C
126 lines
3.2 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);
|
|
|
|
|
|
start_color(); //Enables multi color mode
|
|
|
|
/* Initialize custom color pairs */
|
|
/* Color pair index must be > 0 */
|
|
/* init_pair(index, foreground color, background color); */
|
|
/* Color pairs are called by attron(COLOR_PAIR(1)); */
|
|
init_pair(1, COLOR_CYAN, COLOR_BLACK);
|
|
init_pair(2, COLOR_GREEN, COLOR_BLACK);
|
|
init_pair(3, COLOR_RED, COLOR_BLACK);
|
|
init_pair(4, COLOR_YELLOW, COLOR_BLACK);
|
|
|
|
|
|
|
|
|
|
//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) {
|
|
static int currentColor = 0;
|
|
if (++currentColor > 4) {
|
|
currentColor = 1;
|
|
}
|
|
int i, j, numarr[2];
|
|
for (i = 0; i < 2; i++) {
|
|
numarr[i] = num % 10;
|
|
num /= 10;
|
|
}
|
|
enColor(currentColor);
|
|
for (j = 0; j < 2; j++) {
|
|
for (i = 0; i < 5; i++) {
|
|
mvprintw(starty + i, startx + (6 * j), number[numarr[1 - j]][i]);
|
|
}
|
|
}
|
|
disColor(currentColor);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
void enColor(int color) {
|
|
attron(COLOR_PAIR(color));
|
|
}
|
|
|
|
void disColor(int color) {
|
|
attroff(COLOR_PAIR(color));
|
|
}
|