Compare commits

...

27 Commits

Author SHA1 Message Date
anarcat f2f847cf2c
Merge pull request #94 from arieboven/master 4 years ago
anarcat 06d0d0b4c5
Merge pull request #100 from trofi/master 4 years ago
Sergei Trofimovich 4cfd73080d ttyclock.c: always use "%s"-style format for printf()-style functions 4 years ago
aboven 84fd64b77c Update window when date string change 5 years ago
anarcat 9e00c32098
Merge pull request #93 from Dhruv-Vanjari/dev 5 years ago
Dhruv-Vanjari 9109657637 Makefile: added ttyclock.h in dependencies for building the binary 5 years ago
anarcat 01b48842af
Merge pull request #91 from MoonCactus/locale 5 years ago
Jeremie Francois (on omega) 08f457257b Allow external locale (github #66) 5 years ago
anarcat 9b2c86d904
Merge pull request #84 from jwilk-forks/stdbool 6 years ago
Jakub Wilk 2b79d36ff0 Use standard bool type 6 years ago
anarcat e7eaef18ca
Merge pull request #82 from jwilk-forks/pselect 6 years ago
anarcat 9278106c23
Merge pull request #83 from jwilk-forks/sigwinch 6 years ago
Jakub Wilk b24d49fc3c Get rid of unsafe SIGWINCH handler 6 years ago
Jakub Wilk 542016a26b React to keyboard input without delay 6 years ago
anarcat 8fec43ef43
Merge pull request #81 from jwilk-forks/comment 6 years ago
anarcat 577c4ec1b5
Merge pull request #79 from jwilk-forks/indent 6 years ago
anarcat 601c38c567
Merge pull request #80 from jwilk-forks/pedant 6 years ago
Jakub Wilk 4c1ee2e66b Fix comment placement 6 years ago
Jakub Wilk af41346aa4 Sort #include lines 6 years ago
Jakub Wilk 901ae6801b Makefile: fix uneven indentation 6 years ago
anarcat cfd4effa8b
Merge pull request #78 from jwilk-forks/spelling 6 years ago
anarcat 2ade059031
Merge pull request #77 from jwilk-forks/indent 6 years ago
anarcat ab7dc4badd
Merge pull request #74 from SanchithHegde/master 6 years ago
Jakub Wilk b20b5c9158 Fix typo 6 years ago
Jakub Wilk aab718ad40 Fix uneven indentation 6 years ago
Jakub Wilk e20a6efebd Update vim modelines to better match reality 6 years ago
Sanchith Hegde f2a79ca51f Fixes AM/PM displayed incorrectly for 12:00 - 12:59 7 years ago
  1. 10
      Makefile
  2. 179
      ttyclock.c
  3. 50
      ttyclock.h

@ -2,7 +2,7 @@
#Under BSD License
#See clock.c for the license detail.
SRC = ttyclock.c
SRC = ttyclock.c ttyclock.h
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,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, True);
keypad(stdscr, true);
start_color();
curs_set(False);
curs_set(false);
clear();
/* Init default terminal color */
@ -76,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 = True;
ttyclock.running = true;
if(!ttyclock.geo.x)
ttyclock.geo.x = 0;
if(!ttyclock.geo.y)
@ -95,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)
@ -116,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)
{
@ -143,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");
@ -166,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
@ -181,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";
@ -204,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,
@ -246,6 +243,15 @@ 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);
@ -271,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 */
@ -322,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);
@ -376,11 +382,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)),
@ -392,7 +398,7 @@ set_center(Bool b)
}
void
set_box(Bool b)
set_box(bool b)
{
ttyclock.option.box = b;
@ -400,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);
@ -421,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 == false)
{
ttyclock.running = False;
ttyclock.running = false;
}
else
{
@ -445,6 +455,11 @@ key_event(void)
switch(c = wgetch(stdscr))
{
case KEY_RESIZE:
endwin();
init();
break;
case KEY_UP:
case 'k':
case 'K':
@ -479,8 +494,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':
@ -510,7 +525,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':
@ -518,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;
@ -541,7 +556,7 @@ 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));
@ -550,7 +565,7 @@ main(int argc, char **argv)
/* Default delay */
ttyclock.option.delay = 1; /* 1FPS */
ttyclock.option.nsdelay = 0; /* -0FPS */
ttyclock.option.blink = False;
ttyclock.option.blink = false;
atexit(cleanup);
@ -569,10 +584,10 @@ main(int argc, char **argv)
" -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"
@ -587,33 +602,33 @@ 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 '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);
@ -623,17 +638,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;
@ -646,12 +661,12 @@ 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;
}
}
@ -671,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

@ -34,17 +34,20 @@
#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 <errno.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <locale.h>
#include <time.h>
#include <signal.h>
#include <ncurses.h>
#include <unistd.h>
#include <getopt.h>
#include <ncurses.h>
/* Macro */
#define NORMFRAMEW 35
@ -53,13 +56,11 @@
#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;
@ -69,20 +70,20 @@ 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;
bool bold;
long delay;
Bool blink;
bool blink;
long nsdelay;
} option;
@ -101,6 +102,7 @@ typedef struct
unsigned int minute[2];
unsigned int second[2];
char datestr[256];
char old_datestr[256];
} date;
/* time.h utils */
@ -122,15 +124,15 @@ 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 */
@ -146,4 +148,4 @@ const Bool number[][15] =
#endif /* TTYCLOCK_H_INCLUDED */
// vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4
// vim: expandtab tabstop=5 softtabstop=5 shiftwidth=5

Loading…
Cancel
Save