@ -37,27 +37,29 @@ void
init ( void )
{
struct sigaction sig ;
setlocale ( LC_TIME , " " ) ;
ttyclock . bg = COLOR_BLACK ;
/* Init ncurses */
if ( ttyclock . tty ) {
FILE * ftty = fopen ( ttyclock . tty , " r+ " ) ;
if ( ! ftty ) {
fprintf ( stderr , " tty-clock: error: '%s' couldn't be opened: %s. \n " ,
ttyclock . tty , strerror ( errno ) ) ;
exit ( EXIT_FAILURE ) ;
}
ttyclock . ttyscr = newterm ( NULL , ftty , ftty ) ;
assert ( ttyclock . ttyscr ! = NULL ) ;
set_term ( ttyclock . ttyscr ) ;
FILE * ftty = fopen ( ttyclock . tty , " r+ " ) ;
if ( ! ftty ) {
fprintf ( stderr , " tty-clock: error: '%s' couldn't be opened: %s. \n " ,
ttyclock . tty , strerror ( errno ) ) ;
exit ( EXIT_FAILURE ) ;
}
ttyclock . ttyscr = newterm ( NULL , ftty , ftty ) ;
assert ( ttyclock . ttyscr ! = NULL ) ;
set_term ( ttyclock . ttyscr ) ;
} else
initscr ( ) ;
initscr ( ) ;
cbreak ( ) ;
noecho ( ) ;
keypad ( stdscr , T rue) ;
keypad ( stdscr , t rue) ;
start_color ( ) ;
curs_set ( F alse) ;
curs_set ( f alse) ;
clear ( ) ;
/* Init default terminal color */
@ -68,7 +70,6 @@ init(void)
init_pair ( 0 , ttyclock . bg , ttyclock . bg ) ;
init_pair ( 1 , ttyclock . bg , ttyclock . option . color ) ;
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(1, ttyclock.bg, ttyclock.option.color);
// init_pair(2, ttyclock.option.color, ttyclock.bg);
@ -77,13 +78,12 @@ init(void)
/* Init signal handler */
sig . sa_handler = signal_handler ;
sig . sa_flags = 0 ;
sigaction ( SIGWINCH , & sig , NULL ) ;
sigaction ( SIGTERM , & sig , NULL ) ;
sigaction ( SIGINT , & sig , NULL ) ;
sigaction ( SIGSEGV , & sig , NULL ) ;
/* Init global struct */
ttyclock . running = T rue;
ttyclock . running = t rue;
if ( ! ttyclock . geo . x )
ttyclock . geo . x = 0 ;
if ( ! ttyclock . geo . y )
@ -96,18 +96,18 @@ init(void)
ttyclock . geo . h = 7 ;
ttyclock . tm = localtime ( & ( ttyclock . lt ) ) ;
if ( ttyclock . option . utc ) {
ttyclock . tm = gmtime ( & ( ttyclock . lt ) ) ;
ttyclock . tm = gmtime ( & ( ttyclock . lt ) ) ;
}
ttyclock . lt = time ( NULL ) ;
update_hour ( ) ;
/* Create clock win */
ttyclock . framewin = newwin ( ttyclock . geo . h ,
ttyclock . geo . w ,
ttyclock . geo . x ,
ttyclock . geo . y ) ;
ttyclock . geo . w ,
ttyclock . geo . x ,
ttyclock . geo . y ) ;
if ( ttyclock . option . box ) {
box ( ttyclock . framewin , 0 , 0 ) ;
box ( ttyclock . framewin , 0 , 0 ) ;
}
if ( ttyclock . option . bold )
@ -117,17 +117,17 @@ init(void)
/* Create the date win */
ttyclock . datewin = newwin ( DATEWINH , strlen ( ttyclock . date . datestr ) + 2 ,
ttyclock . geo . x + ttyclock . geo . h - 1 ,
ttyclock . geo . y + ( ttyclock . geo . w / 2 ) -
( strlen ( ttyclock . date . datestr ) / 2 ) - 1 ) ;
ttyclock . geo . x + ttyclock . geo . h - 1 ,
ttyclock . geo . y + ( ttyclock . geo . w / 2 ) -
( strlen ( ttyclock . date . datestr ) / 2 ) - 1 ) ;
if ( ttyclock . option . box & & ttyclock . option . date ) {
box ( ttyclock . datewin , 0 , 0 ) ;
}
clearok ( ttyclock . datewin , T rue) ;
clearok ( ttyclock . datewin , t rue) ;
set_center ( ttyclock . option . center ) ;
nodelay ( stdscr , T rue) ;
nodelay ( stdscr , t rue) ;
if ( ttyclock . option . date )
{
@ -144,16 +144,11 @@ signal_handler(int signal)
{
switch ( signal )
{
case SIGWINCH :
endwin ( ) ;
init ( ) ;
break ;
/* Interruption signal */
case SIGINT :
case SIGTERM :
ttyclock . running = False ;
/* Segmentation fault signal */
ttyclock . running = false ;
break ;
/* Segmentation fault signal */
case SIGSEGV :
endwin ( ) ;
fprintf ( stderr , " Segmentation fault. \n " ) ;
@ -167,10 +162,10 @@ signal_handler(int signal)
void
cleanup ( void )
{
if ( ttyclock . ttyscr )
delscreen ( ttyclock . ttyscr ) ;
if ( ttyclock . ttyscr )
delscreen ( ttyclock . ttyscr ) ;
free ( ttyclock . tty ) ;
free ( ttyclock . tty ) ;
}
void
@ -182,13 +177,13 @@ update_hour(void)
ttyclock . lt = time ( NULL ) ;
ttyclock . tm = localtime ( & ( ttyclock . lt ) ) ;
if ( ttyclock . option . utc ) {
ttyclock . tm = gmtime ( & ( ttyclock . lt ) ) ;
ttyclock . tm = gmtime ( & ( ttyclock . lt ) ) ;
}
ihour = ttyclock . tm - > tm_hour ;
if ( ttyclock . option . twelve )
ttyclock . meridiem = ( ( ihour > 12 ) ? PMSIGN : AMSIGN ) ;
ttyclock . meridiem = ( ( ihour > = 12 ) ? PMSIGN : AMSIGN ) ;
else
ttyclock . meridiem = " \0 " ;
@ -205,6 +200,7 @@ update_hour(void)
ttyclock . date . minute [ 1 ] = ttyclock . tm - > tm_min % 10 ;
/* Set date string */
strcpy ( ttyclock . date . old_datestr , ttyclock . date . datestr ) ;
strftime ( tmpstr ,
sizeof ( tmpstr ) ,
ttyclock . option . format ,
@ -247,10 +243,19 @@ draw_number(int n, int x, int y)
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_number ( ttyclock . date . hour [ 0 ] , 1 , 1 ) ;
draw_number ( ttyclock . date . hour [ 1 ] , 1 , 8 ) ;
chtype dotcolor = COLOR_PAIR ( 3 ) ;
chtype dotcolor = COLOR_PAIR ( 1 ) ;
if ( ttyclock . option . blink & & time ( NULL ) % 2 = = 0 )
dotcolor = COLOR_PAIR ( 2 ) ;
@ -272,11 +277,11 @@ draw_clock(void)
if ( ttyclock . option . date )
{
wbkgdset ( ttyclock . datewin , ( COLOR_PAIR ( 2 ) ) ) ;
mvwprintw ( ttyclock . datewin , ( DATEWINH / 2 ) , 1 , ttyclock . date . datestr ) ;
mvwprintw ( ttyclock . datewin , ( DATEWINH / 2 ) , 1 , " %s " , ttyclock . date . datestr ) ;
wrefresh ( ttyclock . datewin ) ;
}
/* Draw second if the option is enable */
/* Draw second if the option is enabled */
if ( ttyclock . option . second )
{
/* Again 2 dot for number separation */
@ -323,13 +328,13 @@ clock_move(int x, int y, int w, int h)
wresize ( ttyclock . datewin , DATEWINH , strlen ( ttyclock . date . datestr ) + 2 ) ;
if ( ttyclock . option . box ) {
box ( ttyclock . datewin , 0 , 0 ) ;
box ( ttyclock . datewin , 0 , 0 ) ;
}
}
if ( ttyclock . option . box )
{
box ( ttyclock . framewin , 0 , 0 ) ;
box ( ttyclock . framewin , 0 , 0 ) ;
}
wrefresh ( ttyclock . framewin ) ;
@ -377,11 +382,11 @@ set_second(void)
}
void
set_center ( B ool b )
set_center ( b ool b )
{
if ( ( ttyclock . option . center = b ) )
{
ttyclock . option . rebound = F alse;
ttyclock . option . rebound = f alse;
clock_move ( ( LINES / 2 - ( ttyclock . geo . h / 2 ) ) ,
( COLS / 2 - ( ttyclock . geo . w / 2 ) ) ,
@ -393,7 +398,7 @@ set_center(Bool b)
}
void
set_box ( B ool b )
set_box ( b ool b )
{
ttyclock . option . box = b ;
@ -401,14 +406,14 @@ set_box(Bool b)
wbkgdset ( ttyclock . datewin , COLOR_PAIR ( 0 ) ) ;
if ( ttyclock . option . box ) {
wbkgdset ( ttyclock . framewin , COLOR_PAIR ( 0 ) ) ;
wbkgdset ( ttyclock . datewin , COLOR_PAIR ( 0 ) ) ;
box ( ttyclock . framewin , 0 , 0 ) ;
box ( ttyclock . datewin , 0 , 0 ) ;
wbkgdset ( ttyclock . framewin , COLOR_PAIR ( 0 ) ) ;
wbkgdset ( ttyclock . datewin , COLOR_PAIR ( 0 ) ) ;
box ( ttyclock . framewin , 0 , 0 ) ;
box ( ttyclock . datewin , 0 , 0 ) ;
}
else {
wborder ( ttyclock . framewin , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ) ;
wborder ( ttyclock . datewin , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ) ;
wborder ( ttyclock . framewin , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ) ;
wborder ( ttyclock . datewin , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ) ;
}
wrefresh ( ttyclock . datewin ) ;
@ -422,12 +427,16 @@ key_event(void)
struct timespec length = { ttyclock . option . delay , ttyclock . option . nsdelay } ;
fd_set rfds ;
FD_ZERO ( & rfds ) ;
FD_SET ( STDIN_FILENO , & rfds ) ;
if ( ttyclock . option . screensaver )
{
c = wgetch ( stdscr ) ;
if ( c ! = ERR & & ttyclock . option . noquit = = False )
if ( c ! = ERR & & ttyclock . option . noquit = = f alse)
{
ttyclock . running = F alse;
ttyclock . running = f alse;
}
else
{
@ -446,6 +455,11 @@ key_event(void)
switch ( c = wgetch ( stdscr ) )
{
case KEY_RESIZE :
endwin ( ) ;
init ( ) ;
break ;
case KEY_UP :
case ' k ' :
case ' K ' :
@ -480,8 +494,8 @@ key_event(void)
case ' q ' :
case ' Q ' :
if ( ttyclock . option . noquit = = F alse)
ttyclock . running = F alse;
if ( ttyclock . option . noquit = = f alse)
ttyclock . running = f alse;
break ;
case ' s ' :
@ -511,7 +525,7 @@ key_event(void)
case ' R ' :
ttyclock . option . rebound = ! ttyclock . option . rebound ;
if ( ttyclock . option . rebound & & ttyclock . option . center )
ttyclock . option . center = F alse;
ttyclock . option . center = f alse;
break ;
case ' x ' :
@ -519,16 +533,16 @@ key_event(void)
set_box ( ! ttyclock . option . box ) ;
break ;
default :
nanosleep ( & length , NULL ) ;
for ( i = 0 ; i < 8 ; + + i )
if ( c = = ( i + ' 0 ' ) )
{
ttyclock . option . color = i ;
init_pair ( 1 , ttyclock . bg , i ) ;
init_pair ( 2 , i , ttyclock . bg ) ;
}
case ' 0 ' : case ' 1 ' : case ' 2 ' : case ' 3 ' :
case ' 4 ' : case ' 5 ' : case ' 6 ' : case ' 7 ' :
i = c - ' 0 ' ;
ttyclock . option . color = i ;
init_pair ( 1 , ttyclock . bg , i ) ;
init_pair ( 2 , i , ttyclock . bg ) ;
break ;
default :
pselect ( 1 , & rfds , NULL , NULL , & length , NULL ) ;
}
return ;
@ -542,21 +556,20 @@ main(int argc, char **argv)
/* Alloc ttyclock */
memset ( & ttyclock , 0 , sizeof ( ttyclock_t ) ) ;
ttyclock . option . date = T rue;
ttyclock . option . date = t rue;
/* Default date format */
strncpy ( ttyclock . option . format , " %F " , sizeof ( ttyclock . option . format ) ) ;
/* Default color */
ttyclock . option . color = COLOR_GREEN ; /* COLOR_GREEN = 2 */
ttyclock . option . alt_color = 100 ; /* COLOR_GREEN = 2 */
/* Default delay */
ttyclock . option . delay = 1 ; /* 1FPS */
ttyclock . option . nsdelay = 0 ; /* -0FPS */
ttyclock . option . blink = F alse;
ttyclock . option . blink = f alse;
atexit ( cleanup ) ;
while ( ( c = getopt ( argc , argv , " iuvsScbtrhBxnDX: C:f:d:T:a: " ) ) ! = - 1 )
while ( ( c = getopt ( argc , argv , " iuvsScbtrhBxnDC:f:d:T:a: " ) ) ! = - 1 )
{
switch ( c )
{
@ -568,14 +581,13 @@ main(int argc, char **argv)
" -x Show box \n "
" -c Set the clock at the center of the terminal \n "
" -C [0-7] Set the clock color \n "
" -X [0-7] Set the alternate clock color \n "
" -b Use bold colors \n "
" -t Set the hour in 12h format \n "
" -u Use UTC time \n "
" -T tty Display the clock on the specified terminal \n "
" -T tty Display the clock on the specified terminal \n "
" -r Do rebound the clock \n "
" -f format Set the date format \n "
" -n Don't quit on keypress \n "
" -n Don't quit on keypress \n "
" -v Show tty-clock version \n "
" -i Show some info about tty-clock \n "
" -h Show this page \n "
@ -590,37 +602,33 @@ main(int argc, char **argv)
exit ( EXIT_SUCCESS ) ;
break ;
case ' u ' :
ttyclock . option . utc = T rue;
ttyclock . option . utc = t rue;
break ;
case ' v ' :
puts ( " TTY-Clock 2 © devel version " ) ;
exit ( EXIT_SUCCESS ) ;
break ;
case ' s ' :
ttyclock . option . second = T rue;
ttyclock . option . second = t rue;
break ;
case ' S ' :
ttyclock . option . screensaver = T rue;
ttyclock . option . screensaver = t rue;
break ;
case ' c ' :
ttyclock . option . center = T rue;
ttyclock . option . center = t rue;
break ;
case ' b ' :
ttyclock . option . bold = T rue;
ttyclock . option . bold = t rue;
break ;
case ' C ' :
if ( atoi ( optarg ) > = 0 & & atoi ( optarg ) < 8 )
ttyclock . option . color = atoi ( optarg ) ;
break ;
case ' X ' :
if ( atoi ( optarg ) > = 0 & & atoi ( optarg ) < 8 )
ttyclock . option . alt_color = atoi ( optarg ) ;
break ;
case ' t ' :
ttyclock . option . twelve = T rue;
ttyclock . option . twelve = true ;
break ;
case ' r ' :
ttyclock . option . rebound = T rue;
ttyclock . option . rebound = t rue;
break ;
case ' f ' :
strncpy ( ttyclock . option . format , optarg , 100 ) ;
@ -630,17 +638,17 @@ main(int argc, char **argv)
ttyclock . option . delay = atol ( optarg ) ;
break ;
case ' D ' :
ttyclock . option . date = F alse;
ttyclock . option . date = f alse;
break ;
case ' B ' :
ttyclock . option . blink = T rue;
ttyclock . option . blink = t rue;
break ;
case ' a ' :
if ( atol ( optarg ) > = 0 & & atol ( optarg ) < 1000000000 )
ttyclock . option . nsdelay = atol ( optarg ) ;
break ;
break ;
case ' x ' :
ttyclock . option . box = T rue;
ttyclock . option . box = t rue;
break ;
case ' T ' : {
struct stat sbuf ;
@ -653,19 +661,16 @@ main(int argc, char **argv)
optarg ) ;
exit ( EXIT_FAILURE ) ;
} else {
free ( ttyclock . tty ) ;
ttyclock . tty = strdup ( optarg ) ;
free ( ttyclock . tty ) ;
ttyclock . tty = strdup ( optarg ) ;
} }
break ;
case ' n ' :
ttyclock . option . noquit = T rue;
ttyclock . option . noquit = t rue;
break ;
}
}
if ( ttyclock . option . alt_color = = 100 ) {
ttyclock . option . alt_color = ttyclock . option . color ;
}
init ( ) ;
attron ( A_BLINK ) ;
while ( ttyclock . running )
@ -681,4 +686,4 @@ main(int argc, char **argv)
return 0 ;
}
// vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4
// vim: expandtab tabstop=5 softtabstop=5 shiftwidth=5