Compare commits

..

2 Commits

Author SHA1 Message Date
Jacopo De Simoi 3ac053b940 Use alternate color for dot 6 years ago
Jacopo De Simoi 57dda7c317 Setup alt_color 6 years ago
  1. 2
      Makefile
  2. 117
      ttyclock.c
  3. 51
      ttyclock.h

@ -2,7 +2,7 @@
#Under BSD License #Under BSD License
#See clock.c for the license detail. #See clock.c for the license detail.
SRC = ttyclock.c ttyclock.h SRC = ttyclock.c
CC ?= gcc CC ?= gcc
BIN ?= tty-clock BIN ?= tty-clock
PREFIX ?= /usr/local PREFIX ?= /usr/local

@ -37,8 +37,6 @@ void
init(void) init(void)
{ {
struct sigaction sig; struct sigaction sig;
setlocale(LC_TIME,"");
ttyclock.bg = COLOR_BLACK; ttyclock.bg = COLOR_BLACK;
/* Init ncurses */ /* Init ncurses */
@ -57,9 +55,9 @@ init(void)
cbreak(); cbreak();
noecho(); noecho();
keypad(stdscr, true); keypad(stdscr, True);
start_color(); start_color();
curs_set(false); curs_set(False);
clear(); clear();
/* Init default terminal color */ /* Init default terminal color */
@ -70,6 +68,7 @@ init(void)
init_pair(0, ttyclock.bg, ttyclock.bg); init_pair(0, ttyclock.bg, ttyclock.bg);
init_pair(1, ttyclock.bg, ttyclock.option.color); init_pair(1, ttyclock.bg, ttyclock.option.color);
init_pair(2, ttyclock.option.color, ttyclock.bg); init_pair(2, ttyclock.option.color, ttyclock.bg);
init_pair(3, ttyclock.bg, ttyclock.option.alt_color);
// init_pair(0, ttyclock.bg, ttyclock.bg); // init_pair(0, ttyclock.bg, ttyclock.bg);
// init_pair(1, ttyclock.bg, ttyclock.option.color); // init_pair(1, ttyclock.bg, ttyclock.option.color);
// init_pair(2, ttyclock.option.color, ttyclock.bg); // init_pair(2, ttyclock.option.color, ttyclock.bg);
@ -78,12 +77,13 @@ init(void)
/* Init signal handler */ /* Init signal handler */
sig.sa_handler = signal_handler; sig.sa_handler = signal_handler;
sig.sa_flags = 0; sig.sa_flags = 0;
sigaction(SIGWINCH, &sig, NULL);
sigaction(SIGTERM, &sig, NULL); sigaction(SIGTERM, &sig, NULL);
sigaction(SIGINT, &sig, NULL); sigaction(SIGINT, &sig, NULL);
sigaction(SIGSEGV, &sig, NULL); sigaction(SIGSEGV, &sig, NULL);
/* Init global struct */ /* Init global struct */
ttyclock.running = true; ttyclock.running = True;
if(!ttyclock.geo.x) if(!ttyclock.geo.x)
ttyclock.geo.x = 0; ttyclock.geo.x = 0;
if(!ttyclock.geo.y) if(!ttyclock.geo.y)
@ -123,11 +123,11 @@ init(void)
if(ttyclock.option.box && ttyclock.option.date) { if(ttyclock.option.box && ttyclock.option.date) {
box(ttyclock.datewin, 0, 0); box(ttyclock.datewin, 0, 0);
} }
clearok(ttyclock.datewin, true); clearok(ttyclock.datewin, True);
set_center(ttyclock.option.center); set_center(ttyclock.option.center);
nodelay(stdscr, true); nodelay(stdscr, True);
if (ttyclock.option.date) if (ttyclock.option.date)
{ {
@ -144,11 +144,16 @@ signal_handler(int signal)
{ {
switch(signal) switch(signal)
{ {
case SIGWINCH:
endwin();
init();
break;
/* Interruption signal */
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
ttyclock.running = false; ttyclock.running = False;
break;
/* Segmentation fault signal */ /* Segmentation fault signal */
break;
case SIGSEGV: case SIGSEGV:
endwin(); endwin();
fprintf(stderr, "Segmentation fault.\n"); fprintf(stderr, "Segmentation fault.\n");
@ -183,7 +188,7 @@ update_hour(void)
ihour = ttyclock.tm->tm_hour; ihour = ttyclock.tm->tm_hour;
if(ttyclock.option.twelve) if(ttyclock.option.twelve)
ttyclock.meridiem = ((ihour >= 12) ? PMSIGN : AMSIGN); ttyclock.meridiem = ((ihour > 12) ? PMSIGN : AMSIGN);
else else
ttyclock.meridiem = "\0"; ttyclock.meridiem = "\0";
@ -200,7 +205,6 @@ update_hour(void)
ttyclock.date.minute[1] = ttyclock.tm->tm_min % 10; ttyclock.date.minute[1] = ttyclock.tm->tm_min % 10;
/* Set date string */ /* Set date string */
strcpy(ttyclock.date.old_datestr, ttyclock.date.datestr);
strftime(tmpstr, strftime(tmpstr,
sizeof(tmpstr), sizeof(tmpstr),
ttyclock.option.format, ttyclock.option.format,
@ -243,19 +247,10 @@ draw_number(int n, int x, int y)
void void
draw_clock(void) draw_clock(void)
{ {
if (ttyclock.option.date && !ttyclock.option.rebound &&
strcmp(ttyclock.date.datestr, ttyclock.date.old_datestr) != 0)
{
clock_move(ttyclock.geo.x,
ttyclock.geo.y,
ttyclock.geo.w,
ttyclock.geo.h);
}
/* Draw hour numbers */ /* Draw hour numbers */
draw_number(ttyclock.date.hour[0], 1, 1); draw_number(ttyclock.date.hour[0], 1, 1);
draw_number(ttyclock.date.hour[1], 1, 8); draw_number(ttyclock.date.hour[1], 1, 8);
chtype dotcolor = COLOR_PAIR(1); chtype dotcolor = COLOR_PAIR(3);
if (ttyclock.option.blink && time(NULL) % 2 == 0) if (ttyclock.option.blink && time(NULL) % 2 == 0)
dotcolor = COLOR_PAIR(2); dotcolor = COLOR_PAIR(2);
@ -277,11 +272,11 @@ draw_clock(void)
if (ttyclock.option.date) if (ttyclock.option.date)
{ {
wbkgdset(ttyclock.datewin, (COLOR_PAIR(2))); wbkgdset(ttyclock.datewin, (COLOR_PAIR(2)));
mvwprintw(ttyclock.datewin, (DATEWINH / 2), 1, "%s", ttyclock.date.datestr); mvwprintw(ttyclock.datewin, (DATEWINH / 2), 1, ttyclock.date.datestr);
wrefresh(ttyclock.datewin); wrefresh(ttyclock.datewin);
} }
/* Draw second if the option is enabled */ /* Draw second if the option is enable */
if(ttyclock.option.second) if(ttyclock.option.second)
{ {
/* Again 2 dot for number separation */ /* Again 2 dot for number separation */
@ -382,11 +377,11 @@ set_second(void)
} }
void void
set_center(bool b) set_center(Bool b)
{ {
if((ttyclock.option.center = b)) if((ttyclock.option.center = b))
{ {
ttyclock.option.rebound = false; ttyclock.option.rebound = False;
clock_move((LINES / 2 - (ttyclock.geo.h / 2)), clock_move((LINES / 2 - (ttyclock.geo.h / 2)),
(COLS / 2 - (ttyclock.geo.w / 2)), (COLS / 2 - (ttyclock.geo.w / 2)),
@ -398,7 +393,7 @@ set_center(bool b)
} }
void void
set_box(bool b) set_box(Bool b)
{ {
ttyclock.option.box = b; ttyclock.option.box = b;
@ -427,16 +422,12 @@ key_event(void)
struct timespec length = { ttyclock.option.delay, ttyclock.option.nsdelay }; struct timespec length = { ttyclock.option.delay, ttyclock.option.nsdelay };
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
if (ttyclock.option.screensaver) if (ttyclock.option.screensaver)
{ {
c = wgetch(stdscr); c = wgetch(stdscr);
if(c != ERR && ttyclock.option.noquit == false) if(c != ERR && ttyclock.option.noquit == False)
{ {
ttyclock.running = false; ttyclock.running = False;
} }
else else
{ {
@ -455,11 +446,6 @@ key_event(void)
switch(c = wgetch(stdscr)) switch(c = wgetch(stdscr))
{ {
case KEY_RESIZE:
endwin();
init();
break;
case KEY_UP: case KEY_UP:
case 'k': case 'k':
case 'K': case 'K':
@ -494,8 +480,8 @@ key_event(void)
case 'q': case 'q':
case 'Q': case 'Q':
if (ttyclock.option.noquit == false) if (ttyclock.option.noquit == False)
ttyclock.running = false; ttyclock.running = False;
break; break;
case 's': case 's':
@ -525,7 +511,7 @@ key_event(void)
case 'R': case 'R':
ttyclock.option.rebound = !ttyclock.option.rebound; ttyclock.option.rebound = !ttyclock.option.rebound;
if(ttyclock.option.rebound && ttyclock.option.center) if(ttyclock.option.rebound && ttyclock.option.center)
ttyclock.option.center = false; ttyclock.option.center = False;
break; break;
case 'x': case 'x':
@ -533,16 +519,16 @@ key_event(void)
set_box(!ttyclock.option.box); set_box(!ttyclock.option.box);
break; break;
case '0': case '1': case '2': case '3': default:
case '4': case '5': case '6': case '7': nanosleep(&length, NULL);
i = c - '0'; for(i = 0; i < 8; ++i)
if(c == (i + '0'))
{
ttyclock.option.color = i; ttyclock.option.color = i;
init_pair(1, ttyclock.bg, i); init_pair(1, ttyclock.bg, i);
init_pair(2, i, ttyclock.bg); init_pair(2, i, ttyclock.bg);
}
break; break;
default:
pselect(1, &rfds, NULL, NULL, &length, NULL);
} }
return; return;
@ -556,20 +542,21 @@ main(int argc, char **argv)
/* Alloc ttyclock */ /* Alloc ttyclock */
memset(&ttyclock, 0, sizeof(ttyclock_t)); memset(&ttyclock, 0, sizeof(ttyclock_t));
ttyclock.option.date = true; ttyclock.option.date = True;
/* Default date format */ /* Default date format */
strncpy(ttyclock.option.format, "%F", sizeof (ttyclock.option.format)); strncpy(ttyclock.option.format, "%F", sizeof (ttyclock.option.format));
/* Default color */ /* Default color */
ttyclock.option.color = COLOR_GREEN; /* COLOR_GREEN = 2 */ ttyclock.option.color = COLOR_GREEN; /* COLOR_GREEN = 2 */
ttyclock.option.alt_color = 100; /* COLOR_GREEN = 2 */
/* Default delay */ /* Default delay */
ttyclock.option.delay = 1; /* 1FPS */ ttyclock.option.delay = 1; /* 1FPS */
ttyclock.option.nsdelay = 0; /* -0FPS */ ttyclock.option.nsdelay = 0; /* -0FPS */
ttyclock.option.blink = false; ttyclock.option.blink = False;
atexit(cleanup); atexit(cleanup);
while ((c = getopt(argc, argv, "iuvsScbtrhBxnDC:f:d:T:a:")) != -1) while ((c = getopt(argc, argv, "iuvsScbtrhBxnDX:C:f:d:T:a:")) != -1)
{ {
switch(c) switch(c)
{ {
@ -581,6 +568,7 @@ main(int argc, char **argv)
" -x Show box \n" " -x Show box \n"
" -c Set the clock at the center of the terminal \n" " -c Set the clock at the center of the terminal \n"
" -C [0-7] Set the clock color \n" " -C [0-7] Set the clock color \n"
" -X [0-7] Set the alternate clock color \n"
" -b Use bold colors \n" " -b Use bold colors \n"
" -t Set the hour in 12h format \n" " -t Set the hour in 12h format \n"
" -u Use UTC time \n" " -u Use UTC time \n"
@ -602,33 +590,37 @@ main(int argc, char **argv)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'u': case 'u':
ttyclock.option.utc = true; ttyclock.option.utc = True;
break; break;
case 'v': case 'v':
puts("TTY-Clock 2 © devel version"); puts("TTY-Clock 2 © devel version");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 's': case 's':
ttyclock.option.second = true; ttyclock.option.second = True;
break; break;
case 'S': case 'S':
ttyclock.option.screensaver = true; ttyclock.option.screensaver = True;
break; break;
case 'c': case 'c':
ttyclock.option.center = true; ttyclock.option.center = True;
break; break;
case 'b': case 'b':
ttyclock.option.bold = true; ttyclock.option.bold = True;
break; break;
case 'C': case 'C':
if(atoi(optarg) >= 0 && atoi(optarg) < 8) if(atoi(optarg) >= 0 && atoi(optarg) < 8)
ttyclock.option.color = atoi(optarg); ttyclock.option.color = atoi(optarg);
break; break;
case 'X':
if(atoi(optarg) >= 0 && atoi(optarg) < 8)
ttyclock.option.alt_color = atoi(optarg);
break;
case 't': case 't':
ttyclock.option.twelve = true; ttyclock.option.twelve = True;
break; break;
case 'r': case 'r':
ttyclock.option.rebound = true; ttyclock.option.rebound = True;
break; break;
case 'f': case 'f':
strncpy(ttyclock.option.format, optarg, 100); strncpy(ttyclock.option.format, optarg, 100);
@ -638,17 +630,17 @@ main(int argc, char **argv)
ttyclock.option.delay = atol(optarg); ttyclock.option.delay = atol(optarg);
break; break;
case 'D': case 'D':
ttyclock.option.date = false; ttyclock.option.date = False;
break; break;
case 'B': case 'B':
ttyclock.option.blink = true; ttyclock.option.blink = True;
break; break;
case 'a': case 'a':
if(atol(optarg) >= 0 && atol(optarg) < 1000000000) if(atol(optarg) >= 0 && atol(optarg) < 1000000000)
ttyclock.option.nsdelay = atol(optarg); ttyclock.option.nsdelay = atol(optarg);
break; break;
case 'x': case 'x':
ttyclock.option.box = true; ttyclock.option.box = True;
break; break;
case 'T': { case 'T': {
struct stat sbuf; struct stat sbuf;
@ -666,11 +658,14 @@ main(int argc, char **argv)
}} }}
break; break;
case 'n': case 'n':
ttyclock.option.noquit = true; ttyclock.option.noquit = True;
break; break;
} }
} }
if ( ttyclock.option.alt_color == 100) {
ttyclock.option.alt_color = ttyclock.option.color;
}
init(); init();
attron(A_BLINK); attron(A_BLINK);
while(ttyclock.running) while(ttyclock.running)
@ -686,4 +681,4 @@ main(int argc, char **argv)
return 0; return 0;
} }
// vim: expandtab tabstop=5 softtabstop=5 shiftwidth=5 // vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4

@ -34,20 +34,17 @@
#ifndef TTYCLOCK_H_INCLUDED #ifndef TTYCLOCK_H_INCLUDED
#define TTYCLOCK_H_INCLUDED #define TTYCLOCK_H_INCLUDED
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/select.h> #include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <locale.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <signal.h>
#include <ncurses.h> #include <ncurses.h>
#include <unistd.h>
#include <getopt.h>
/* Macro */ /* Macro */
#define NORMFRAMEW 35 #define NORMFRAMEW 35
@ -56,11 +53,13 @@
#define AMSIGN " [AM]" #define AMSIGN " [AM]"
#define PMSIGN " [PM]" #define PMSIGN " [PM]"
typedef enum { False, True } Bool;
/* Global ttyclock struct */ /* Global ttyclock struct */
typedef struct typedef struct
{ {
/* while() boolean */ /* while() boolean */
bool running; Bool running;
/* terminal variables */ /* terminal variables */
SCREEN *ttyscr; SCREEN *ttyscr;
@ -70,20 +69,21 @@ typedef struct
/* Running option */ /* Running option */
struct struct
{ {
bool second; Bool second;
bool screensaver; Bool screensaver;
bool twelve; Bool twelve;
bool center; Bool center;
bool rebound; Bool rebound;
bool date; Bool date;
bool utc; Bool utc;
bool box; Bool box;
bool noquit; Bool noquit;
char format[100]; char format[100];
int color; int color;
bool bold; int alt_color;
Bool bold;
long delay; long delay;
bool blink; Bool blink;
long nsdelay; long nsdelay;
} option; } option;
@ -102,7 +102,6 @@ typedef struct
unsigned int minute[2]; unsigned int minute[2];
unsigned int second[2]; unsigned int second[2];
char datestr[256]; char datestr[256];
char old_datestr[256];
} date; } date;
/* time.h utils */ /* time.h utils */
@ -124,15 +123,15 @@ void draw_number(int n, int x, int y);
void draw_clock(void); void draw_clock(void);
void clock_move(int x, int y, int w, int h); void clock_move(int x, int y, int w, int h);
void set_second(void); void set_second(void);
void set_center(bool b); void set_center(Bool b);
void set_box(bool b); void set_box(Bool b);
void key_event(void); void key_event(void);
/* Global variable */ /* Global variable */
ttyclock_t ttyclock; ttyclock_t ttyclock;
/* Number matrix */ /* Number matrix */
const bool number[][15] = const Bool number[][15] =
{ {
{1,1,1,1,0,1,1,0,1,1,0,1,1,1,1}, /* 0 */ {1,1,1,1,0,1,1,0,1,1,0,1,1,1,1}, /* 0 */
{0,0,1,0,0,1,0,0,1,0,0,1,0,0,1}, /* 1 */ {0,0,1,0,0,1,0,0,1,0,0,1,0,0,1}, /* 1 */
@ -148,4 +147,4 @@ const bool number[][15] =
#endif /* TTYCLOCK_H_INCLUDED */ #endif /* TTYCLOCK_H_INCLUDED */
// vim: expandtab tabstop=5 softtabstop=5 shiftwidth=5 // vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4

Loading…
Cancel
Save