Ticket #???? (clock support)

Initial step for the clock support. Based on Olegarch (olegarch@linuxinside.com) patches.

Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
This commit is contained in:
Ilia Maslakov 2012-07-01 21:34:31 +04:00
parent 3e66455662
commit 0855a4157e
7 changed files with 174 additions and 0 deletions

View File

@ -1,3 +1,4 @@
/*
Global structure for some library-related variables
@ -46,6 +47,17 @@
/*** global variables ****************************************************************************/
#ifdef SIGACT
struct sigaction clock_new, clock_old, clock_dummy;
#else
void (*clock_alarm) (int);
#endif
unsigned int clock_remain;
int op_clock_type = HOUR_MIN_SEC;
int clock_ticks;
int clock_type;
/* *INDENT-OFF* */
mc_global_t mc_global = {
.mc_run_mode = MC_RUN_FULL,

View File

@ -146,6 +146,10 @@
/*** enums ***************************************************************************************/
#define NO_CLOCK 0
#define HOUR_MIN_SEC 1
#define HOUR_MIN 2
/* run mode and params */
typedef enum
{
@ -274,7 +278,17 @@ typedef struct
/*** global variables defined in .c file *********************************************************/
extern unsigned int clock_remain;
extern mc_global_t mc_global;
extern int op_clock_type;
extern int clock_ticks;
extern int clock_type;
#ifdef SIGACT
struct sigaction clock_new, clock_old, clock_dummy;
#else
void (*clock_alarm) (int);
#endif
/*** declarations of public functions ************************************************************/

View File

@ -205,6 +205,7 @@ tty_init (gboolean mouse_enable, gboolean is_xterm)
noecho ();
keypad (stdscr, TRUE);
nodelay (stdscr, FALSE);
clock_init ();
tty_setup_sigwinch (sigwinch_handler);
}

View File

@ -342,6 +342,7 @@ tty_init (gboolean mouse_enable, gboolean is_xterm)
do_enter_ca_mode ();
tty_keypad (TRUE);
tty_nodelay (FALSE);
clock_init ();
tty_setup_sigwinch (sigwinch_handler);
}

View File

@ -78,6 +78,139 @@ sigintr_handler (int signo)
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
void
show_clock (void)
{
int lngstr;
time_t curtime;
int yt, xt;
char timestr[11] = "";
clock_type = op_clock_type;
if (clock_type)
{
curtime = time (0);
if (clock_type == HOUR_MIN)
{
lngstr = 6;
if (!(strftime (timestr, lngstr, "%R", localtime (&curtime))))
strcpy (timestr, "??:??");
}
else if (clock_type == HOUR_MIN_SEC)
{
lngstr = 9;
if (!(strftime (timestr, lngstr, "%X", localtime (&curtime))))
strcpy (timestr, "??:??:??");
}
tty_getyx (&yt, &xt);
tty_gotoyx (0, COLS - lngstr);
tty_print_string (timestr);
tty_gotoyx (yt, xt);
}
}
/* --------------------------------------------------------------------------------------------- */
void
clock_interrupt_handler (void)
{
if (clock_type)
{
show_clock ();
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)
mc_refresh ();
#endif
#ifdef SIGACT
sigaction (SIGALRM, &clock_new, &clock_dummy);
#else
signal (SIGALRM, clock_interrupt_handler);
#endif
alarm (clock_ticks);
}
}
/* --------------------------------------------------------------------------------------------- */
void
set_clock_type (int ct)
{
if (ct == HOUR_MIN_SEC)
{
/* update every seconds */
clock_type = ct;
clock_ticks = 1;
}
else if (ct == HOUR_MIN)
{
/* update every minits */
clock_type = ct;
clock_ticks = 60;
}
else
{
/* no clock at all */
clock_type = 0;
clock_ticks = 0;
}
}
/* --------------------------------------------------------------------------------------------- */
void
clock_init (void)
{
if (op_clock_type)
{
set_clock_type (op_clock_type);
show_clock ();
#ifdef SIGACT
clock_new.sa_handler = clock_interrupt_handler;
sigemptyset (&clock_new.sa_mask);
clock_new.sa_flags = 0;
sigaction (SIGALRM, &clock_new, &clock_old);
#else
clock_alarm = signal (SIGALRM, clock_interrupt_handler);
#endif
clock_remain = alarm (clock_ticks);
}
}
/* --------------------------------------------------------------------------------------------- */
void
clock_cancel (void)
{
if (clock_type)
{
#ifdef SIGACT
sigaction (SIGALRM, &clock_old, &clock_dummy);
if (clock_old.sa_handler != SIG_IGN)
alarm (clock_remain);
#else
signal (SIGALRM, clock_alarm);
if (clock_alarm != SIG_IGN)
alarm (clock_remain);
#endif
}
}
/* --------------------------------------------------------------------------------------------- */
void
clock_resume (void)
{
if (clock_type)
clock_interrupt_handler ();
}
/* --------------------------------------------------------------------------------------------- */
/**
* Check terminal type. If $TERM is not set or value is empty, mc finishes with EXIT_FAILURE.
*

View File

@ -71,6 +71,13 @@ extern int reset_hp_softkeys;
/*** declarations of public functions ************************************************************/
void clock_interrupt_handler (void);
void set_clock_type (int ct);
void clock_init (void);
void clock_cancel (void);
void clock_resume (void);
void show_clock (void);
extern void tty_beep (void);
/* {{{ Input }}} */

View File

@ -226,6 +226,12 @@ menubar_draw (WMenuBar * menubar)
tty_print_char (' ');
}
if (op_clock_type != NO_CLOCK)
{
mc_log ("show_clock\n");
clock_resume ();
show_clock ();
}
if (menubar->is_dropped)
menubar_draw_drop (menubar);