Compare commits

..

5 Commits

Author SHA1 Message Date
Jacopo De Simoi 09fd87526e Modify the glyph for 1 6 years ago
Jacopo De Simoi 26222b33fd Use one dot for number separation 6 years ago
Jacopo De Simoi 142a0bd1d6 experiment a bit 6 years ago
Jacopo De Simoi 3ac053b940 Use alternate color for dot 6 years ago
Jacopo De Simoi 57dda7c317 Setup alt_color 6 years ago
  1. 10
      Makefile
  2. 207
      ttyclock.c
  3. 53
      ttyclock.h

@ -2,7 +2,7 @@
#Under BSD License
#See clock.c for the license detail.
SRC = ttyclock.c ttyclock.h
SRC = ttyclock.c
CC ?= gcc
BIN ?= tty-clock
PREFIX ?= /usr/local
@ -13,14 +13,14 @@ ifeq ($(shell sh -c 'which ncurses6-config>/dev/null 2>/dev/null && echo y'), y)
CFLAGS += -Wall -g $$(ncurses6-config --cflags)
LDFLAGS += $$(ncurses6-config --libs)
else ifeq ($(shell sh -c 'which ncursesw6-config>/dev/null 2>/dev/null && echo y'), y)
CFLAGS += -Wall -g $$(ncursesw6-config --cflags)
LDFLAGS += $$(ncursesw6-config --libs)
CFLAGS += -Wall -g $$(ncursesw6-config --cflags)
LDFLAGS += $$(ncursesw6-config --libs)
else ifeq ($(shell sh -c 'which ncurses5-config>/dev/null 2>/dev/null && echo y'), y)
CFLAGS += -Wall -g $$(ncurses5-config --cflags)
LDFLAGS += $$(ncurses5-config --libs)
else ifeq ($(shell sh -c 'which ncursesw5-config>/dev/null 2>/dev/null && echo y'), y)
CFLAGS += -Wall -g $$(ncursesw5-config --cflags)
LDFLAGS += $$(ncursesw5-config --libs)
CFLAGS += -Wall -g $$(ncursesw5-config --cflags)
LDFLAGS += $$(ncursesw5-config --libs)
else
CFLAGS += -Wall -g $$(pkg-config --cflags ncurses)
LDFLAGS += $$(pkg-config --libs ncurses)

@ -37,29 +37,27 @@ 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, true);
keypad(stdscr, True);
start_color();
curs_set(false);
curs_set(False);
clear();
/* Init default terminal color */
@ -68,8 +66,9 @@ init(void)
/* Init color pair */
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(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);
@ -78,12 +77,13 @@ 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 = true;
ttyclock.running = True;
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, true);
clearok(ttyclock.datewin, True);
set_center(ttyclock.option.center);
nodelay(stdscr, true);
nodelay(stdscr, True);
if (ttyclock.option.date)
{
@ -144,11 +144,16 @@ signal_handler(int signal)
{
switch(signal)
{
case SIGWINCH:
endwin();
init();
break;
/* Interruption signal */
case SIGINT:
case SIGTERM:
ttyclock.running = false;
break;
ttyclock.running = False;
/* Segmentation fault signal */
break;
case SIGSEGV:
endwin();
fprintf(stderr, "Segmentation fault.\n");
@ -162,10 +167,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
@ -177,13 +182,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";
@ -200,7 +205,6 @@ 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,
@ -233,8 +237,13 @@ draw_number(int n, int x, int y)
wattroff(ttyclock.framewin, A_BLINK);
wbkgdset(ttyclock.framewin, COLOR_PAIR(number[n][i/2]));
if (number[n][i/2] == 1)
wattron(ttyclock.framewin, A_REVERSE);
else
wattroff(ttyclock.framewin, A_REVERSE);
mvwaddch(ttyclock.framewin, x, sy, ' ');
}
wattroff(ttyclock.framewin, A_REVERSE);
wrefresh(ttyclock.framewin);
return;
@ -243,26 +252,16 @@ 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(1);
chtype dotcolor = COLOR_PAIR(3);
if (ttyclock.option.blink && time(NULL) % 2 == 0)
dotcolor = COLOR_PAIR(2);
/* 2 dot for number separation */
/* 1 dot for number separation */
wbkgdset(ttyclock.framewin, dotcolor);
mvwaddstr(ttyclock.framewin, 2, 16, " ");
mvwaddstr(ttyclock.framewin, 4, 16, " ");
mvwaddstr(ttyclock.framewin, 3, 16, " ");
/* Draw minute numbers */
draw_number(ttyclock.date.minute[0], 1, 20);
@ -277,11 +276,11 @@ draw_clock(void)
if (ttyclock.option.date)
{
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);
}
/* Draw second if the option is enabled */
/* Draw second if the option is enable */
if(ttyclock.option.second)
{
/* Again 2 dot for number separation */
@ -328,17 +327,17 @@ 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);
wrefresh(ttyclock.datewin);
wrefresh(ttyclock.datewin);
return;
}
@ -382,11 +381,11 @@ set_second(void)
}
void
set_center(bool b)
set_center(Bool b)
{
if((ttyclock.option.center = b))
{
ttyclock.option.rebound = false;
ttyclock.option.rebound = False;
clock_move((LINES / 2 - (ttyclock.geo.h / 2)),
(COLS / 2 - (ttyclock.geo.w / 2)),
@ -398,7 +397,7 @@ set_center(bool b)
}
void
set_box(bool b)
set_box(Bool b)
{
ttyclock.option.box = b;
@ -406,14 +405,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);
@ -426,17 +425,13 @@ key_event(void)
int i, c;
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 == False)
{
ttyclock.running = false;
ttyclock.running = False;
}
else
{
@ -455,11 +450,6 @@ key_event(void)
switch(c = wgetch(stdscr))
{
case KEY_RESIZE:
endwin();
init();
break;
case KEY_UP:
case 'k':
case 'K':
@ -494,8 +484,8 @@ key_event(void)
case 'q':
case 'Q':
if (ttyclock.option.noquit == false)
ttyclock.running = false;
if (ttyclock.option.noquit == False)
ttyclock.running = False;
break;
case 's':
@ -525,7 +515,7 @@ key_event(void)
case 'R':
ttyclock.option.rebound = !ttyclock.option.rebound;
if(ttyclock.option.rebound && ttyclock.option.center)
ttyclock.option.center = false;
ttyclock.option.center = False;
break;
case 'x':
@ -533,16 +523,16 @@ key_event(void)
set_box(!ttyclock.option.box);
break;
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);
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);
}
break;
}
return;
@ -556,20 +546,21 @@ main(int argc, char **argv)
/* Alloc ttyclock */
memset(&ttyclock, 0, sizeof(ttyclock_t));
ttyclock.option.date = true;
ttyclock.option.date = True;
/* 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 = false;
ttyclock.option.blink = False;
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)
{
@ -581,13 +572,14 @@ 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"
@ -602,33 +594,37 @@ main(int argc, char **argv)
exit(EXIT_SUCCESS);
break;
case 'u':
ttyclock.option.utc = true;
ttyclock.option.utc = True;
break;
case 'v':
puts("TTY-Clock 2 © devel version");
exit(EXIT_SUCCESS);
break;
case 's':
ttyclock.option.second = true;
ttyclock.option.second = True;
break;
case 'S':
ttyclock.option.screensaver = true;
ttyclock.option.screensaver = True;
break;
case 'c':
ttyclock.option.center = true;
ttyclock.option.center = True;
break;
case 'b':
ttyclock.option.bold = true;
ttyclock.option.bold = True;
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 = true;
ttyclock.option.twelve = True;
break;
case 'r':
ttyclock.option.rebound = true;
ttyclock.option.rebound = True;
break;
case 'f':
strncpy(ttyclock.option.format, optarg, 100);
@ -638,17 +634,17 @@ main(int argc, char **argv)
ttyclock.option.delay = atol(optarg);
break;
case 'D':
ttyclock.option.date = false;
ttyclock.option.date = False;
break;
case 'B':
ttyclock.option.blink = true;
ttyclock.option.blink = True;
break;
case 'a':
if(atol(optarg) >= 0 && atol(optarg) < 1000000000)
ttyclock.option.nsdelay = atol(optarg);
break;
break;
case 'x':
ttyclock.option.box = true;
ttyclock.option.box = True;
break;
case 'T': {
struct stat sbuf;
@ -661,16 +657,19 @@ 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 = true;
ttyclock.option.noquit = True;
break;
}
}
if ( ttyclock.option.alt_color == 100) {
ttyclock.option.alt_color = ttyclock.option.color;
}
init();
attron(A_BLINK);
while(ttyclock.running)
@ -686,4 +685,4 @@ main(int argc, char **argv)
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
#define TTYCLOCK_H_INCLUDED
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <locale.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <ncurses.h>
#include <unistd.h>
#include <getopt.h>
/* Macro */
#define NORMFRAMEW 35
@ -56,11 +53,13 @@
#define AMSIGN " [AM]"
#define PMSIGN " [PM]"
typedef enum { False, True } Bool;
/* Global ttyclock struct */
typedef struct
{
/* while() boolean */
bool running;
Bool running;
/* terminal variables */
SCREEN *ttyscr;
@ -70,20 +69,21 @@ typedef struct
/* Running option */
struct
{
bool second;
bool screensaver;
bool twelve;
bool center;
bool rebound;
bool date;
bool utc;
bool box;
bool noquit;
Bool second;
Bool screensaver;
Bool twelve;
Bool center;
Bool rebound;
Bool date;
Bool utc;
Bool box;
Bool noquit;
char format[100];
int color;
bool bold;
int alt_color;
Bool bold;
long delay;
bool blink;
Bool blink;
long nsdelay;
} option;
@ -102,7 +102,6 @@ typedef struct
unsigned int minute[2];
unsigned int second[2];
char datestr[256];
char old_datestr[256];
} date;
/* time.h utils */
@ -124,18 +123,18 @@ void draw_number(int n, int x, int y);
void draw_clock(void);
void clock_move(int x, int y, int w, int h);
void set_second(void);
void set_center(bool b);
void set_box(bool b);
void set_center(Bool b);
void set_box(Bool b);
void key_event(void);
/* Global variable */
ttyclock_t ttyclock;
/* 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 */
{0,0,1,0,0,1,0,0,1,0,0,1,0,0,1}, /* 1 */
{1,1,0,0,1,0,0,1,0,0,1,0,1,1,1}, /* 1 */
{1,1,1,0,0,1,1,1,1,1,0,0,1,1,1}, /* 2 */
{1,1,1,0,0,1,1,1,1,0,0,1,1,1,1}, /* 3 */
{1,0,1,1,0,1,1,1,1,0,0,1,0,0,1}, /* 4 */
@ -148,4 +147,4 @@ const bool number[][15] =
#endif /* TTYCLOCK_H_INCLUDED */
// vim: expandtab tabstop=5 softtabstop=5 shiftwidth=5
// vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4

Loading…
Cancel
Save