/* $Id$ */ /* * aclock - ascii clock for UNIX Console * * Copyright (c) 2002 Antoni Sawicki * Version 1.8 (unix-curses); Dublin, June 2002 * * Copmpilation: cc aclock.c -o aclock -lcurses -lm * */ #include #include #include #include #include #include #include #include int die = 0; int redraw = 0; enum {RED = 1, CYAN, BLUE, GREEN}; char *strtime(char *, size_t, struct tm *); void sevenseg(char [3][80], size_t, char *); void catch(int sig) { switch (sig) { case SIGWINCH: redraw = 1; break; case SIGINT: die = 1; break; default: break; } } void settimer(int sec, int usec) { struct itimerval itv; itv.it_value.tv_sec = sec; itv.it_value.tv_usec = usec; itv.it_interval = itv.it_value; setitimer(ITIMER_REAL, &itv, NULL); } void draw_circle(int hand_max, int sYcen, int sXcen, int FontHW) { int x, y, r; for (r = 0; r < 60; r++) { x = cos(r * M_PI / 180 * 6) * hand_max * FontHW + sXcen; y = sin(r * M_PI / 180 * 6) * hand_max + sYcen; if (r % 5) { attron(COLOR_PAIR(CYAN)); mvaddch(y, x, '.'); attroff(COLOR_PAIR(CYAN)); } else { attron(COLOR_PAIR(RED)); mvaddch(y, x, 'o'); attroff(COLOR_PAIR(RED)); } } } void draw_hand(int minute, int hlenght, char c, int sXcen, int sYcen, int FontHW) { int x, y, n; float r = (minute - 15) * (M_PI / 180) * 6; for (n = 1; n < hlenght; n++) { x = cos(r) * n * FontHW + sXcen; y = sin(r) * n + sYcen; mvaddch(y, x, c); } } const char INFO[] = "Copyright (c) 2002 by Antek Sawicki \n" "Version 1.8; Dublin, June 2002\n"; int main(int argc, char **argv) { char digital_time[32], stime[80], dtime[3][80]; int i, ch; int dflag = 0, sflag = 0; int FontHW = 2; int sXmax, sYmax, smax, hand_max, sXcen, sYcen; time_t t; struct tm *ltime; struct winsize ws; struct timeval tv; int hascolors; while ((ch = getopt(argc, argv, "ds")) != -1) { switch (ch) { case 'd': dflag = 1; break; case 's': sflag = 1; break; default: break; } } argc -= optind; argv += optind; signal(SIGWINCH, catch); signal(SIGALRM, catch); signal(SIGINT, catch); initscr(); curs_set(0); if ((hascolors = has_colors())) start_color(); init_pair(RED, COLOR_RED, COLOR_BLACK); init_pair(CYAN, COLOR_CYAN, COLOR_BLACK); init_pair(BLUE, COLOR_BLUE, COLOR_BLACK); init_pair(GREEN, COLOR_GREEN, COLOR_BLACK); while (!die) { gettimeofday(&tv, NULL); settimer(0, 1000000 - tv.tv_usec); if (redraw && ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) { resizeterm(ws.ws_row, ws.ws_col); clear(); redraw = 0; } time(&t); ltime = localtime(&t); sXmax = COLS; sYmax = LINES; if (sXmax / 2 <= sYmax) smax = sXmax / 2; else smax = sYmax; hand_max = (smax / 2) - 1; sXcen = sXmax / 2; sYcen = sYmax / 2 - 1; erase(); draw_circle(hand_max, sYcen, sXcen, FontHW); if (dflag) { attron(COLOR_PAIR(GREEN)); strftime(digital_time, sizeof(digital_time), "%d-%m-%y", ltime); sevenseg(dtime, sizeof(dtime[0]), digital_time); for (i = 0; i < 3; i++) mvprintw(i + sYcen - sYmax / 4 - 1, sXcen - strlen(dtime[i]) / 2, dtime[i]); strftime(digital_time, sizeof(digital_time), "%H:%M:%S", ltime); sevenseg(dtime, sizeof(dtime[0]), digital_time); for (i = 0; i < 3; i++) mvprintw(i + sYcen + sYmax / 4 - 1, sXcen - strlen(dtime[i]) / 2, dtime[i]); attroff(COLOR_PAIR(GREEN)); } if (hascolors) attron(A_BOLD); attron(COLOR_PAIR(RED)); draw_hand((ltime->tm_hour * 5) + (ltime->tm_min / 10), 2 * hand_max / 3, '#', sXcen, sYcen, FontHW); attroff(COLOR_PAIR(RED)); attron(COLOR_PAIR(CYAN)); draw_hand(ltime->tm_min, hand_max - 2, '*', sXcen, sYcen, FontHW); attroff(COLOR_PAIR(CYAN)); attron(COLOR_PAIR(BLUE)); draw_hand(ltime->tm_sec, hand_max - 1, '.', sXcen, sYcen, FontHW); attroff(COLOR_PAIR(BLUE)); if (sflag) { attron(COLOR_PAIR(BLUE)); strtime(stime, sizeof(stime), ltime); mvprintw(sYmax - 1, sXcen - strlen(stime) / 2, stime); attroff(COLOR_PAIR(BLUE)); } if (hascolors) attroff(A_BOLD); refresh(); pause(); } curs_set(1); endwin(); return 0; }