URI:
       TTY-Clock2 beta released - tty-clock - port of tty-clock to OpenBSD, with pledge/unveil added as goodie.
  HTML git clone https://git.drkhsh.at/tty-clock.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit ddcbbd187331fbfde3dfe38b6b7939db1e375d62
   DIR parent 58abe96b0e160fb27f4286727190b54d215e9cec
  HTML Author: Martin Duquesnoy <xorg62@gmail.com>
       Date:   Sun,  8 Mar 2009 20:22:17 +0100
       
       TTY-Clock2 beta released
       
       Diffstat:
         M Makefile                            |      19 +++++++++----------
         M README                              |       9 +++------
         D clock.c                             |     458 -------------------------------
         A ttyclock.c                          |     438 +++++++++++++++++++++++++++++++
         A ttyclock.h                          |     132 +++++++++++++++++++++++++++++++
       
       5 files changed, 582 insertions(+), 474 deletions(-)
       ---
   DIR diff --git a/Makefile b/Makefile
       @@ -2,36 +2,35 @@
        #Under BSD License
        #See clock.c for the license detail.
        
       -SRC = clock.c
       +SRC = ttyclock.c
        CC = cc
       -NCURSESFLAG = -lncurses
        BIN = tty-clock
        INSTALLPATH = /usr/local/bin/
       -CFLAGS = -Wall ${NCURSESFLAG}
       +CFLAGS = -Wall -g
       +LDFLAGS = -lncurses
        
        
        tty-clock : ${SRC}
        
                @echo "build ${SRC}"
       -        @echo "CC ${CFLAGS} ${SRC}"
       -        @${CC} ${CFLAGS} ${SRC} -o ${BIN}
       +        @echo "CC ${CFLAGS} ${LDFLAGS} ${SRC}"
       +        @${CC} ${CFLAGS} ${LDFLAGS} ${SRC} -o ${BIN}
        
        install : ${BIN}
        
                @echo "installing binary file to ${INSTALLPATH}${BIN}"
                @cp ${BIN} ${INSTALLPATH}
                @chmod 755 ${INSTALLPATH}${BIN}
       -        @echo "installed."
       +        @echo "installed"
        
        uninstall :
        
                @echo "uninstalling binary file (${INSTALLPATH}${BIN})"
                @rm -f ${INSTALLPATH}${BIN}
       -        @echo "uninstalled :'(."
       -
       +        @echo "${BIN} uninstalled"
        clean :
        
       -        @echo "cleaning"
       +        @echo "cleaning ${BIN}"
                @rm ${BIN}
       -        @echo "cleaned."
       +        @echo "${BIN} cleaned"
        
   DIR diff --git a/README b/README
       @@ -1,10 +1,7 @@
       -tty-clock usage : tty-clock -[option] -[option] <arg>
       +tty-clock usage : tty-clock -[option] <arg>
          -s, --second        Show seconds
       -  -b, --block         Lock the keyboard\n\
       -  -t, --tw            Set the hour in 12h format
       -  -x  <integer>       Set the clock to X
       -  -y  <integer>       Set the clock to Y
       +  -b, --lock          Lock the keyboard
       +  -t, --twelve        Set the hour in 12h format
          -v, --version       Show tty-clock version
          -i, --info          Show some info about tty-clock
          -h, --help          Show this page
       -Try keypad arrow for move the clock ;)
   DIR diff --git a/clock.c b/clock.c
       @@ -1,458 +0,0 @@
       -/*
       - *      clock.c TTY-CLOCK Main file.
       - *      Copyright © 2008 Martin Duquesnoy <xorg62@gmail.com>
       - *      All rights reserved.
       - *
       - *      Redistribution and use in source and binary forms, with or without
       - *      modification, are permitted provided that the following conditions are
       - *      met:
       - *
       - *      * Redistributions of source code must retain the above copyright
       - *        notice, this list of conditions and the following disclaimer.
       - *      * Redistributions in binary form must reproduce the above
       - *        copyright notice, this list of conditions and the following disclaimer
       - *        in the documentation and/or other materials provided with the
       - *        distribution.
       - *      * Neither the name of the  nor the names of its
       - *        contributors may be used to endorse or promote products derived from
       - *        this software without specific prior written permission.
       - *
       - *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       - *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       - *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       - *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       - *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       - *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       - *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       - *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       - *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       - *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       - *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       - */
       -
       -#include <stdlib.h>
       -#include <time.h>
       -#include <signal.h>
       -#include <ncurses.h>
       -#include <unistd.h>
       -#include <getopt.h>
       -
       -#define HELPSTR "tty-clock usage : tty-clock -[option] -[option] <arg>\n\n\
       -  -s, --second           Show seconds                                 \n \
       -  -b, --block            Lock the keyboard                            \n \
       -  -c, --center           Set the clock at the center of the terminal  \n \
       -  -t, --tw               Set the hour in 12h format                   \n \
       -  -x  <integer>          Set the clock to X                           \n \
       -  -y  <integer>          Set the clock to Y                           \n \
       -  -v, --version          Show tty-clock version                       \n \
       -  -i, --info             Show some info about tty-clock               \n \
       -  -h, --help             Show this page                             \n\n \
       -Try keypad arrow for move the clock :-)                               \n \
       -push S for enable the second and T for enable the 12H hours format.\n"
       -
       -#define LGNUM   30
       -#define DIFFSEC 19
       -#define DEPTHB  -1
       -#define MAXW    getmaxx(stdscr)
       -#define MAXH    getmaxy(stdscr)
       -
       -typedef enum { False, True } Bool;
       -
       -void start(void);
       -void check_key(Bool);
       -void get_time(void);
       -void set_center(void);
       -void handle_sig(int);
       -void run(void);
       -
       -/* BIG NUMBER INIT */
       -static const Bool number[][LGNUM] =
       -{
       -     {1,1,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1}, /* 0 */
       -     {0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1}, /* 1 */
       -     {1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1}, /* 2 */
       -     {1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1}, /* 3 */
       -     {1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1}, /* 4 */
       -     {1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1}, /* 5 */
       -     {1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1}, /* 6 */
       -     {1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1}, /* 7 */
       -     {1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1}, /* 8 */
       -     {1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1}  /* 9 */
       -};
       -
       -/* VARIABLE INIT */
       -static struct option long_options[] =
       -{
       -     {"help",    0, NULL, 'h'},
       -     {"version", 0, NULL, 'v'},
       -     {"info",    0, NULL, 'i'},
       -     {"second",  0, NULL, 's'},
       -     {"twelve",  0, NULL, 't'},
       -     {"block",   0, NULL, 'b'},
       -     {"center",  0, NULL, 'c'},
       -     {NULL,      0, NULL, 0}
       -};
       -
       -typedef struct
       -{
       -     Bool second;
       -     Bool twelve;
       -     Bool keylock;
       -     Bool center;
       -} option_t;
       -
       -typedef struct
       -{
       -     int x;
       -     int y;
       -     int width;
       -     int height;
       -} geo_t;
       -
       -typedef struct
       -{
       -     unsigned int hour[2];
       -     unsigned int minute[2];
       -     unsigned int second[2];
       -     unsigned int month_day;
       -     unsigned int month;
       -     unsigned int year;
       -} date_t;
       -
       -option_t option = { False, False, True };
       -geo_t geo = { 1, 1, 33, 5 }; /* Base position of the clock */
       -Bool running = True;
       -date_t sdate;
       -char *meridiem;
       -int bg, i;
       -struct tm *tm;
       -time_t lt;
       -
       -/* STARTING FUNCTION */
       -void
       -start(void)
       -{
       -     struct sigaction sig;
       -
       -     /* Init curses stuff */
       -     initscr();
       -     noecho();
       -     keypad(stdscr, TRUE);
       -     start_color();
       -     refresh();
       -     bg = (use_default_colors() == OK) ? -1 : COLOR_BLACK; /* -1 = default term bg */
       -     init_pair(0, bg, bg);
       -     init_pair(1, COLOR_BLACK, COLOR_GREEN);
       -     init_pair(2, COLOR_GREEN, bg);
       -     curs_set(0);
       -     clear();
       -
       -     /* Set signal handle */
       -     sig.sa_handler = handle_sig;
       -     sig.sa_flags = 0;
       -     sigaction(SIGWINCH, &sig, NULL);
       -     sigaction(SIGTERM, &sig, NULL);
       -     sigaction(SIGINT, &sig, NULL);
       -}
       -
       -/* BIG NUMBER PRINTING FUNCTION */
       -void
       -print_number(int num, int x, int y)
       -{
       -     int sy = y;
       -
       -     for(i = 0; i < LGNUM; ++i, ++sy)
       -     {
       -          if(sy == y + 6)
       -          {
       -               sy = y;
       -               ++x;
       -          }
       -          move(x, sy);
       -          attron(COLOR_PAIR(number[num][i]));
       -          addch(' ');
       -          attroff(COLOR_PAIR(number[num][i]));
       -     }
       -
       -}
       -
       -/* ARRANGE FINAL POSITION OF NUMBER */
       -void
       -arrange_clock(int h1, int h2,
       -              int m1, int m2,
       -              int s1, int s2)
       -{
       -
       -     print_number(h1, geo.x, geo.y);
       -     print_number(h2, geo.x, geo.y + 7);
       -
       -     attron(COLOR_PAIR(1));
       -     mvaddstr(geo.x + 1, geo.y + 15, "  ");
       -     mvaddstr(geo.x + 3, geo.y + 15, "  ");
       -     attroff(COLOR_PAIR(1));
       -
       -     print_number(m1, geo.x, geo.y + 19);
       -     print_number(m2, geo.x, geo.y + 26);
       -
       -     if(option.second)
       -     {
       -          attron(COLOR_PAIR(1));
       -          mvaddstr(geo.x + 1, geo.y + 34, "  ");
       -          mvaddstr(geo.x + 3, geo.y + 34, "  ");
       -          attroff(COLOR_PAIR(1));
       -
       -          print_number(s1, geo.x, geo.y + 38);
       -          print_number(s2, geo.x, geo.y + 45);
       -     }
       -
       -     /* Frame border */
       -     for(i = geo.y + DEPTHB; i < geo.y + geo.width; ++i)
       -     {
       -          mvaddch(geo.x + DEPTHB, i, ACS_HLINE);
       -          mvaddch(geo.x + geo.height, i, ACS_HLINE);
       -     }
       -
       -     for (i = geo.x + DEPTHB; i < geo.x + geo.height; ++i)
       -     {
       -          mvaddch(i, geo.y + DEPTHB,    ACS_VLINE);
       -          mvaddch(i, geo.y + geo.width, ACS_VLINE);
       -     }
       -
       -     /* Frame corner */
       -     mvaddch(geo.x + DEPTHB,     geo.y + DEPTHB,    ACS_ULCORNER);
       -     mvaddch(geo.x + geo.height, geo.y + DEPTHB,    ACS_LLCORNER);
       -     mvaddch(geo.x + DEPTHB,     geo.y + geo.width, ACS_URCORNER);
       -     mvaddch(geo.x + geo.height, geo.y + geo.width, ACS_LRCORNER);
       -
       -     /* Print the date */
       -     move(geo.x + geo.height + 1, geo.y + ((option.second) ? 21 : 12));
       -     attron(COLOR_PAIR(2));
       -     printw("%.2d/%.2d/%d %s",
       -            sdate.month_day,
       -            sdate.month,
       -            sdate.year,
       -            meridiem);
       -     attroff(COLOR_PAIR(2));
       -
       -}
       -
       -/* KEY CHECKING FUNCTION */
       -void
       -check_key(Bool keylock)
       -{
       -     if(!keylock)
       -          return;
       -
       -     switch(getch())
       -     {
       -     case KEY_UP:
       -     case 'k':
       -     case 'K':
       -          if(!option.center)
       -          {
       -               if(geo.x > 1)
       -                    --geo.x;
       -               clear();
       -          }
       -          break;
       -     case KEY_DOWN:
       -     case 'j':
       -     case 'J':
       -          if(!option.center)
       -          {
       -               if(geo.x + geo.height + 2 < MAXH)
       -                    ++geo.x;
       -               clear();
       -          }
       -          break;
       -     case KEY_LEFT:
       -     case 'h':
       -     case 'H':
       -          if(!option.center)
       -          {
       -               if(geo.y > 1)
       -                    --geo.y;
       -               clear();
       -          }
       -          break;
       -     case KEY_RIGHT:
       -     case 'l':
       -     case 'L':
       -          if(!option.center)
       -          {
       -               if(geo.y + geo.width + 1 < MAXW)
       -                    ++geo.y;
       -               clear();
       -          }
       -          break;
       -     case 's':
       -     case 'S':
       -          if(!option.second)
       -               geo.width += DIFFSEC;
       -          else
       -               geo.width -= DIFFSEC;
       -          clear();
       -          option.second = !option.second;
       -          if(option.center)
       -          {
       -               option.center = False;
       -               set_center();
       -          }
       -          break;
       -     case 't':
       -     case 'T':
       -          clear();
       -          option.twelve = !option.twelve;
       -          break;
       -     case 'c':
       -     case 'C':
       -          clear();
       -          set_center();
       -          break;
       -     case 'q':
       -     case 'Q':
       -          running = False;
       -          break;
       -     }
       -}
       -
       -/* GETTING TIME FUNCTION */
       -void
       -get_time(void)
       -{
       -     int ihour;
       -
       -     tm = localtime(&lt);
       -     lt = time(NULL);
       -     ihour = tm->tm_hour;
       -
       -     if(option.twelve)
       -          meridiem = (ihour > 12) ? "(PM)" : "(AM)";
       -     else
       -          meridiem = " ";
       -
       -     ihour = (option.twelve && ihour > 12) ? ihour - 12 : ihour;
       -     ihour = (option.twelve && !ihour) ? 12 : ihour;
       -
       -     sdate.hour[0] = ihour / 10;
       -     sdate.hour[1] = ihour % 10;
       -
       -     sdate.minute[0] = tm->tm_min / 10;
       -     sdate.minute[1] = tm->tm_min % 10;
       -
       -     sdate.month_day = tm->tm_mday;
       -     sdate.month = tm->tm_mon + 1;
       -     sdate.year = tm->tm_year + 1900;
       -
       -     if(option.second)
       -     {
       -          sdate.second[0] = tm->tm_sec / 10;
       -          sdate.second[1] = tm->tm_sec % 10;
       -     }
       -}
       -
       -/* SET CENTER FUNCTION */
       -void
       -set_center(void)
       -{
       -     if(!option.center)
       -     {
       -          if(option.second && geo.width != 52)
       -               geo.width += DIFFSEC;
       -          geo.y = MAXW / 2 - (geo.width / 2);
       -          geo.x = MAXH / 2 - (geo.height / 2);
       -          option.center = True;
       -     }
       -     else
       -          option.center = !option.center;
       -}
       -
       -/* SIGNAL HANDLE FUNCTION */
       -void
       -handle_sig(int num)
       -{
       -     if(num == SIGWINCH && option.center)
       -     {
       -          endwin();
       -          start();
       -          option.center = !option.center;
       -          set_center();
       -     }
       -     else if(num == SIGINT || num == SIGTERM)
       -          running = False;
       -}
       -
       -/* RUN FUCTION */
       -void
       -run(void)
       -{
       -     get_time();
       -     arrange_clock(sdate.hour[0], sdate.hour[1],
       -                   sdate.minute[0], sdate.minute[1],
       -                   sdate.second[0], sdate.second[1]);
       -     refresh();
       -     halfdelay(1);
       -}
       -
       -/* MAIN FUCTION */
       -
       -int
       -main(int argc, char **argv)
       -{
       -     int c;
       -
       -     while ((c = getopt_long(argc,argv,"tx:y:vsbcih",
       -                             long_options, NULL)) != -1)
       -     {
       -          switch(c)
       -          {
       -          case 'h':
       -          default:
       -               printf(HELPSTR);
       -               exit(EXIT_SUCCESS);
       -               break;
       -          case 'i':
       -               printf("TTY-Clock by Martin Duquesnoy (xorg62@gmail.com)\n");
       -               exit(EXIT_SUCCESS);
       -               break;
       -          case 'v':
       -               printf("TTY-Clock devel\n");
       -               exit(EXIT_SUCCESS);
       -               break;
       -          case 'x':
       -               if(atoi(optarg) > 0)
       -                    geo.x = atoi(optarg) + 1;
       -
       -               break;
       -          case 'y':
       -               if(atoi(optarg) > 0)
       -                    geo.y = atoi(optarg) + 1;
       -               break;
       -          case 's':
       -               option.second = True;
       -               if(option.center)
       -               {
       -                    option.center = !option.center;
       -                    set_center();
       -               }
       -               break;
       -          case 't': option.twelve = True; break;
       -          case 'b': option.keylock = False; break;
       -          case 'c': start(); set_center(); break;
       -          }
       -     }
       -
       -     start();
       -     run();
       -
       -     while(running)
       -     {
       -          usleep(10000);
       -          check_key(option.keylock);
       -          run();
       -     }
       -
       -     endwin();
       -
       -     return 0;
       -}
   DIR diff --git a/ttyclock.c b/ttyclock.c
       @@ -0,0 +1,438 @@
       +/*
       + *      TTY-CLOCK Main file.
       + *      Copyright © 2008 Martin Duquesnoy <xorg62@gmail.com>
       + *      All rights reserved.
       + *
       + *      Redistribution and use in source and binary forms, with or without
       + *      modification, are permitted provided that the following conditions are
       + *      met:
       + *
       + *      * Redistributions of source code must retain the above copyright
       + *        notice, this list of conditions and the following disclaimer.
       + *      * Redistributions in binary form must reproduce the above
       + *        copyright notice, this list of conditions and the following disclaimer
       + *        in the documentation and/or other materials provided with the
       + *        distribution.
       + *      * Neither the name of the  nor the names of its
       + *        contributors may be used to endorse or promote products derived from
       + *        this software without specific prior written permission.
       + *
       + *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       + *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       + *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       + *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       + *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       + *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       + *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       + *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       + *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       + *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       + *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       + */
       +
       +#include "ttyclock.h"
       +
       +void
       +init(void)
       +{
       +     struct sigaction sig;
       +     int bg = COLOR_BLACK;
       +
       +     /* Init ncurses */
       +     initscr();
       +     noecho();
       +     keypad(stdscr, True);
       +     start_color();
       +     curs_set(False);
       +     clear();
       +
       +     /* Init default terminal color */
       +     if(use_default_colors() == OK)
       +          bg = -1;
       +
       +     /* Init color pair */
       +     init_pair(0, bg, bg);
       +     init_pair(1, bg, COLOR_GREEN);
       +     init_pair(2, COLOR_GREEN, bg);
       +     refresh();
       +
       +     /* 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->geo.x = 0;
       +     ttyclock->geo.y = 0;
       +     ttyclock->geo.w = (ttyclock->option.second) ? SECFRAMEW : NORMFRAMEW;
       +     ttyclock->geo.h = 7;
       +     ttyclock->tm = localtime(&(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);
       +     box(ttyclock->framewin, 0, 0);
       +
       +     /* 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);
       +     box(ttyclock->datewin, 0, 0);
       +     clearok(ttyclock->datewin, True);
       +
       +     set_center(ttyclock->option.center);
       +
       +     wrefresh(ttyclock->datewin);
       +     wrefresh(ttyclock->framewin);
       +
       +     return;
       +}
       +
       +void
       +signal_handler(int signal)
       +{
       +     switch(signal)
       +     {
       +     case SIGWINCH:
       +          /* If the terminal is resizing */
       +          if(ttyclock->option.center)
       +          {
       +               endwin();
       +               init();
       +          }
       +          break;
       +          /* Interruption signal */
       +     case SIGINT:
       +     case SIGTERM:
       +          ttyclock->running = False;
       +          /* Segmentation fault signal */
       +          break;
       +     case SIGSEGV:
       +          endwin();
       +          fprintf(stderr, "Segmentation fault.\n");
       +          exit(EXIT_FAILURE);
       +          break;
       +     }
       +
       +     return;
       +}
       +
       +void
       +update_hour(void)
       +{
       +     int ihour;
       +
       +     ttyclock->tm = localtime(&(ttyclock->lt));
       +     ttyclock->lt = time(NULL);
       +
       +     ihour = ttyclock->tm->tm_hour;
       +
       +     if(ttyclock->option.twelve)
       +          ttyclock->meridiem = (ihour > 12) ? " [PM]" : " [AM]";
       +     else
       +          ttyclock->meridiem = "";
       +
       +     /* Manage hour for twelve mode */
       +     ihour = ((ttyclock->option.twelve && ihour > 12)  ? (ihour - 12) : ihour);
       +     ihour = ((ttyclock->option.twelve && !ihour) ? 12 : ihour);
       +
       +     /* Set hour */
       +     ttyclock->date.hour[0] = ihour / 10;
       +     ttyclock->date.hour[1] = ihour % 10;
       +
       +     /* Set minutes */
       +     ttyclock->date.minute[0] = ttyclock->tm->tm_min / 10;
       +     ttyclock->date.minute[1] = ttyclock->tm->tm_min % 10;
       +
       +     /* Set date string */
       +     sprintf(ttyclock->date.datestr,
       +             "%.2d/%.2d/%d%s",
       +             ttyclock->tm->tm_mday,
       +             ttyclock->tm->tm_mon + 1,
       +             ttyclock->tm->tm_year + 1900,
       +             ttyclock->meridiem);
       +
       +     /* Set seconds */
       +     ttyclock->date.second[0] = ttyclock->tm->tm_sec / 10;
       +     ttyclock->date.second[1] = ttyclock->tm->tm_sec % 10;
       +
       +
       +
       +     return;
       +}
       +
       +void
       +draw_number(int n, int x, int y)
       +{
       +     int i, sy = y;
       +
       +     for(i = 0; i < 30; ++i, ++sy)
       +     {
       +          if(sy == y + 6)
       +          {
       +               sy = y;
       +               ++x;
       +          }
       +          wbkgdset(ttyclock->framewin, COLOR_PAIR(number[n][i/2]));
       +          mvwaddch(ttyclock->framewin, x, sy, ' ');
       +     }
       +     wrefresh(ttyclock->framewin);
       +
       +     return;
       +}
       +
       +void
       +draw_clock(void)
       +{
       +     /* Draw hour numbers */
       +     draw_number(ttyclock->date.hour[0], 1, 1);
       +     draw_number(ttyclock->date.hour[1], 1, 8);
       +
       +     /* 2 dot for number separation */
       +     wbkgdset(ttyclock->framewin, COLOR_PAIR(1));
       +     mvwaddstr(ttyclock->framewin, 2, 16, "  ");
       +     mvwaddstr(ttyclock->framewin, 4, 16, "  ");
       +
       +     /* Draw minute numbers */
       +     draw_number(ttyclock->date.minute[0], 1, 20);
       +     draw_number(ttyclock->date.minute[1], 1, 27);
       +
       +
       +     /* Draw the date */
       +     wbkgdset(ttyclock->datewin, (COLOR_PAIR(2)));
       +     mvwprintw(ttyclock->datewin, (DATEWINH - (DATEWINH / 2) - 1), 1, ttyclock->date.datestr);
       +     wrefresh(ttyclock->datewin);
       +
       +     /* Draw second if the option is enable */
       +     if(ttyclock->option.second)
       +     {
       +          /* Again 2 dot for number separation */
       +          wbkgdset(ttyclock->framewin, COLOR_PAIR(1));
       +          mvwaddstr(ttyclock->framewin, 2, NORMFRAMEW, "  ");
       +          mvwaddstr(ttyclock->framewin, 4, NORMFRAMEW, "  ");
       +
       +          /* Draw second numbers */
       +          draw_number(ttyclock->date.second[0], 1, 39);
       +          draw_number(ttyclock->date.second[1], 1, 46);
       +     }
       +
       +     return;
       +}
       +
       +void
       +clock_move(int x, int y, int w, int h)
       +{
       +
       +     /* Erase border for a clean move */
       +     wbkgdset(ttyclock->framewin, COLOR_PAIR(0));
       +     wborder(ttyclock->framewin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
       +     wbkgdset(ttyclock->datewin, COLOR_PAIR(0));
       +     wborder(ttyclock->datewin, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
       +
       +     /* Clean windows */
       +     werase(ttyclock->framewin);
       +     werase(ttyclock->datewin);
       +     wrefresh(ttyclock->framewin);
       +     wrefresh(ttyclock->datewin);
       +
       +
       +     /* Make new property */
       +     ttyclock->framewin = newwin((ttyclock->geo.h = h),
       +                                 (ttyclock->geo.w = w),
       +                                 (ttyclock->geo.x = x),
       +                                 (ttyclock->geo.y = y));
       +
       +     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);
       +
       +     box(ttyclock->framewin, 0, 0);
       +     box(ttyclock->datewin,  0, 0);
       +
       +     wrefresh(ttyclock->datewin);
       +     wrefresh(ttyclock->framewin);
       +
       +
       +     return;
       +}
       +
       +void
       +set_second(void)
       +{
       +     /* Reverse option value */
       +     if((ttyclock->option.second = !ttyclock->option.second))
       +          clock_move(ttyclock->geo.x, ttyclock->geo.y, (ttyclock->geo.w = SECFRAMEW), ttyclock->geo.h);
       +     else
       +     {
       +          /* Clean old second number on the terminal */
       +          wbkgdset(ttyclock->framewin, COLOR_PAIR(0));
       +          wclear(ttyclock->framewin);
       +          clock_move(ttyclock->geo.x, ttyclock->geo.y, (ttyclock->geo.w = NORMFRAMEW), ttyclock->geo.h);
       +     }
       +
       +     set_center(ttyclock->option.center);
       +
       +     return;
       +}
       +
       +void
       +set_center(Bool b)
       +{
       +     if((ttyclock->option.center = b))
       +          clock_move((ttyclock->geo.x = (LINES / 2 - (ttyclock->geo.h / 2))),
       +                     (ttyclock->geo.y = (COLS  / 2 - (ttyclock->geo.w / 2))),
       +                     ttyclock->geo.w,
       +                     ttyclock->geo.h);
       +
       +     return;
       +}
       +
       +void
       +key_event(void)
       +{
       +     if(ttyclock->option.keylock)
       +          return;
       +
       +     halfdelay(1);
       +
       +     switch(getch())
       +     {
       +     case KEY_UP:
       +     case 'k':
       +     case 'K':
       +          if(ttyclock->geo.x >= 1
       +             && !ttyclock->option.center)
       +               clock_move(ttyclock->geo.x - 1, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h);
       +          break;
       +
       +     case KEY_DOWN:
       +     case 'j':
       +     case 'J':
       +          if(ttyclock->geo.x <= (LINES - ttyclock->geo.h - DATEWINH)
       +             && !ttyclock->option.center)
       +               clock_move(ttyclock->geo.x + 1, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h);
       +          break;
       +
       +     case KEY_LEFT:
       +     case 'h':
       +     case 'H':
       +          if(ttyclock->geo.y >= 1
       +             && !ttyclock->option.center)
       +               clock_move(ttyclock->geo.x, ttyclock->geo.y - 1, ttyclock->geo.w, ttyclock->geo.h);
       +          break;
       +
       +     case KEY_RIGHT:
       +     case 'l':
       +     case 'L':
       +          if(ttyclock->geo.y <= (COLS - ttyclock->geo.w - 1)
       +             && !ttyclock->option.center)
       +               clock_move(ttyclock->geo.x, ttyclock->geo.y + 1, ttyclock->geo.w, ttyclock->geo.h);
       +          break;
       +
       +     case 'q':
       +     case 'Q':
       +          ttyclock->running = False;
       +          break;
       +
       +     case 's':
       +     case 'S':
       +          set_second();
       +          break;
       +
       +     case 't':
       +     case 'T':
       +          ttyclock->option.twelve = !ttyclock->option.twelve;
       +          /* Set the new ttyclock->date.datestr for resize date window */
       +          update_hour();
       +          clock_move(ttyclock->geo.x, ttyclock->geo.y, ttyclock->geo.w, ttyclock->geo.h);
       +          break;
       +
       +     case 'c':
       +     case 'C':
       +          set_center(!ttyclock->option.center);
       +          break;
       +     }
       +
       +     return;
       +}
       +
       +int
       +main(int argc, char **argv)
       +{
       +     int c;
       +
       +     struct option long_options[] =
       +          {
       +               {"help",    0, NULL, 'h'},
       +               {"version", 0, NULL, 'v'},
       +               {"info",    0, NULL, 'i'},
       +               {"second",  0, NULL, 's'},
       +               {"twelve",  0, NULL, 't'},
       +               {"lock",    0, NULL, 'l'},
       +               {"center",  0, NULL, 'c'},
       +               {NULL,      0, NULL, 0}
       +          };
       +
       +     /* Alloc ttyclock */
       +     ttyclock = malloc(sizeof(ttyclock_t));
       +
       +     while ((c = getopt_long(argc,argv,"tvslcih",
       +                             long_options, NULL)) != -1)
       +     {
       +          switch(c)
       +          {
       +          case 'h':
       +          default:
       +               puts(HELPSTR);
       +               free(ttyclock);
       +               exit(EXIT_SUCCESS);
       +               break;
       +          case 'i':
       +               puts("TTY-Clock 2 © by Martin Duquesnoy (xorg62@gmail.com)");
       +               free(ttyclock);
       +               exit(EXIT_SUCCESS);
       +               break;
       +          case 'v':
       +               puts("TTY-Clock 2 © devel version");
       +               free(ttyclock);
       +               exit(EXIT_SUCCESS);
       +               break;
       +          case 's':
       +               ttyclock->option.second = True;
       +               break;
       +          case 'c':
       +               ttyclock->option.center = True;
       +               break;
       +          case 't':
       +               ttyclock->option.twelve = True;
       +               break;
       +          case 'l':
       +               ttyclock->option.keylock = True;
       +               break;
       +          }
       +     }
       +
       +     init();
       +
       +     while(ttyclock->running)
       +     {
       +          update_hour();
       +          draw_clock();
       +          key_event();
       +     }
       +
       +     free(ttyclock);
       +     endwin();
       +
       +     return 0;
       +}
   DIR diff --git a/ttyclock.h b/ttyclock.h
       @@ -0,0 +1,132 @@
       +/*
       + *      TTY-CLOCK headers file.
       + *      Copyright © 2008 Martin Duquesnoy <xorg62@gmail.com>
       + *      All rights reserved.
       + *
       + *      Redistribution and use in source and binary forms, with or without
       + *      modification, are permitted provided that the following conditions are
       + *      met:
       + *
       + *      * Redistributions of source code must retain the above copyright
       + *        notice, this list of conditions and the following disclaimer.
       + *      * Redistributions in binary form must reproduce the above
       + *        copyright notice, this list of conditions and the following disclaimer
       + *        in the documentation and/or other materials provided with the
       + *        distribution.
       + *      * Neither the name of the  nor the names of its
       + *        contributors may be used to endorse or promote products derived from
       + *        this software without specific prior written permission.
       + *
       + *      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       + *      "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       + *      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       + *      A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       + *      OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       + *      SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       + *      LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       + *      DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       + *      THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       + *      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       + *      OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       + */
       +
       +#ifndef TTYCLOCK_H_INCLUDED
       +#define TTYCLOCK_H_INCLUDED
       +
       +#include <stdlib.h>
       +#include <string.h>
       +#include <time.h>
       +#include <signal.h>
       +#include <ncurses.h>
       +#include <unistd.h>
       +#include <getopt.h>
       +
       +/* Macro */
       +#define NORMFRAMEW 35
       +#define SECFRAMEW  54
       +#define UPDATETIME 10000
       +#define DATEWINH   3
       +
       +/* Help string */
       +#define HELPSTR "tty-clock usage : tty-clock -[option] <arg>    \n\
       +   -s, --second     Show seconds                                \n\
       +   -l, --lock       Lock the keyboard                           \n\
       +   -c, --center     Set the clock at the center of the terminal \n\
       +   -t, --twelve     Set the hour in 12h format                  \n\
       +   -v, --version    Show tty-clock version                      \n\
       +   -i, --info       Show some info about tty-clock              \n\
       +   -h, --help       Show this page                              "
       +
       +typedef enum { False, True } Bool;
       +
       +/* Global ttyclock struct */
       +typedef struct
       +{
       +     /* while() boolean */
       +     Bool running;
       +
       +     /* Running option */
       +     struct
       +     {
       +          Bool second;
       +          Bool twelve;
       +          Bool keylock;
       +          Bool center;
       +     } option;
       +
       +     /* Clock geometry */
       +     struct
       +     {
       +          int x, y, w, h;
       +     } geo;
       +
       +     /* Date content ([2] = number by number) */
       +     struct
       +     {
       +          unsigned int hour[2];
       +          unsigned int minute[2];
       +          unsigned int second[2];
       +          char datestr[56];
       +     } date;
       +
       +     /* time.h utils */
       +     struct tm *tm;
       +     time_t lt;
       +
       +     /* Clock member */
       +     char *meridiem;
       +     WINDOW* framewin;
       +     WINDOW* datewin;
       +
       +} ttyclock_t;
       +
       +/* Prototypes */
       +void init(void);
       +void signal_handler(int signal);
       +void update_hour(void);
       +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 key_event(void);
       +
       +/* Global variable */
       +ttyclock_t *ttyclock;
       +
       +/* Number matrix */
       +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,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 */
       +     {1,1,1,1,0,0,1,1,1,0,0,1,1,1,1}, /* 5 */
       +     {1,1,1,1,0,0,1,1,1,1,0,1,1,1,1}, /* 6 */
       +     {1,1,1,0,0,1,0,0,1,0,0,1,0,0,1}, /* 7 */
       +     {1,1,1,1,0,1,1,1,1,1,0,1,1,1,1}, /* 8 */
       +     {1,1,1,1,0,1,1,1,1,0,0,1,1,1,1}, /* 9 */
       +};
       +
       +#endif /* TTYCLOCK_H_INCLUDED */