1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 02:23:02 +01:00

improve call duration string & remove a couple unused functions

This commit is contained in:
Jfreegman 2014-06-24 01:54:08 -04:00
parent 490c80dae9
commit 7ac7713268
5 changed files with 61 additions and 114 deletions

View File

@ -546,33 +546,37 @@ void chat_onPeerTimeout (ToxWindow *self, ToxAv *av, int call_index)
static void init_infobox(ToxWindow *self) static void init_infobox(ToxWindow *self)
{ {
ChatContext *ctx = self->chatwin;
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
memset(&self->chatwin->infobox, 0, sizeof(struct infobox)); memset(&ctx->infobox, 0, sizeof(struct infobox));
self->chatwin->infobox.win = newwin(INFOBOX_HEIGHT, INFOBOX_WIDTH + 1, 1, x2 - INFOBOX_WIDTH); ctx->infobox.win = newwin(INFOBOX_HEIGHT, INFOBOX_WIDTH + 1, 1, x2 - INFOBOX_WIDTH);
self->chatwin->infobox.calltime = get_unix_time(); ctx->infobox.starttime = get_unix_time();
self->chatwin->infobox.vad_lvl = VAD_THRESHOLD_DEFAULT; ctx->infobox.vad_lvl = VAD_THRESHOLD_DEFAULT;
self->chatwin->infobox.active = true; ctx->infobox.active = true;
strcpy(self->chatwin->infobox.timestr, "00:00:00"); strcpy(ctx->infobox.timestr, "00");
} }
static void kill_infobox(ToxWindow *self) static void kill_infobox(ToxWindow *self)
{ {
if (!self->chatwin->infobox.win) ChatContext *ctx = self->chatwin;
if (!ctx->infobox.win)
return; return;
delwin(self->chatwin->infobox.win); delwin(ctx->infobox.win);
memset(&self->chatwin->infobox, 0, sizeof(struct infobox)); memset(&ctx->infobox, 0, sizeof(struct infobox));
} }
/* update infobox info and draw in respective chat window */ /* update infobox info and draw in respective chat window */
static void draw_infobox(ToxWindow *self) static void draw_infobox(ToxWindow *self)
{ {
struct infobox infobox = self->chatwin->infobox; struct infobox *infobox = &self->chatwin->infobox;
if (infobox.win == NULL) if (infobox->win == NULL)
return; return;
int x2, y2; int x2, y2;
@ -584,43 +588,41 @@ static void draw_infobox(ToxWindow *self)
uint64_t curtime = get_unix_time(); uint64_t curtime = get_unix_time();
/* update elapsed time string once per second */ /* update elapsed time string once per second */
if (curtime > infobox.lastupdate) { if (curtime > infobox->lastupdate)
infobox.calltime = curtime - infobox.calltime; get_elapsed_time_str(infobox->timestr, sizeof(infobox->timestr), curtime - infobox->starttime);
get_elapsed_time_str(infobox.timestr, sizeof(infobox.timestr), infobox.calltime);
}
infobox.lastupdate = curtime; infobox->lastupdate = curtime;
const char *in_is_muted = infobox.in_is_muted ? "yes" : "no"; const char *in_is_muted = infobox->in_is_muted ? "yes" : "no";
const char *out_is_muted = infobox.out_is_muted ? "yes" : "no"; const char *out_is_muted = infobox->out_is_muted ? "yes" : "no";
wmove(infobox.win, 1, 1); wmove(infobox->win, 1, 1);
wattron(infobox.win, COLOR_PAIR(RED) | A_BOLD); wattron(infobox->win, COLOR_PAIR(RED) | A_BOLD);
wprintw(infobox.win, " Call Active\n"); wprintw(infobox->win, " Call Active\n");
wattroff(infobox.win, COLOR_PAIR(RED) | A_BOLD); wattroff(infobox->win, COLOR_PAIR(RED) | A_BOLD);
wattron(infobox.win, A_BOLD); wattron(infobox->win, A_BOLD);
wprintw(infobox.win, " Time: "); wprintw(infobox->win, " Duration: ");
wattroff(infobox.win, A_BOLD); wattroff(infobox->win, A_BOLD);
wprintw(infobox.win, "%s\n", infobox.timestr); wprintw(infobox->win, "%s\n", infobox->timestr);
wattron(infobox.win, A_BOLD); wattron(infobox->win, A_BOLD);
wprintw(infobox.win, " In muted: "); wprintw(infobox->win, " In muted: ");
wattroff(infobox.win, A_BOLD); wattroff(infobox->win, A_BOLD);
wprintw(infobox.win, "%s\n", in_is_muted); wprintw(infobox->win, "%s\n", in_is_muted);
wattron(infobox.win, A_BOLD); wattron(infobox->win, A_BOLD);
wprintw(infobox.win, " Out muted: "); wprintw(infobox->win, " Out muted: ");
wattroff(infobox.win, A_BOLD); wattroff(infobox->win, A_BOLD);
wprintw(infobox.win, "%s\n", out_is_muted); wprintw(infobox->win, "%s\n", out_is_muted);
wattron(infobox.win, A_BOLD); wattron(infobox->win, A_BOLD);
wprintw(infobox.win, " VAD level: "); wprintw(infobox->win, " VAD level: ");
wattroff(infobox.win, A_BOLD); wattroff(infobox->win, A_BOLD);
wprintw(infobox.win, "%.2f\n", infobox.vad_lvl); wprintw(infobox->win, "%.2f\n", infobox->vad_lvl);
wborder(infobox.win, ACS_VLINE, ' ', ACS_HLINE, ACS_HLINE, ACS_TTEE, ' ', ACS_LLCORNER, ' '); wborder(infobox->win, ACS_VLINE, ' ', ACS_HLINE, ACS_HLINE, ACS_TTEE, ' ', ACS_LLCORNER, ' ');
wrefresh(infobox.win); wrefresh(infobox->win);
} }
#endif /* _SUPPORT_AUDIO */ #endif /* _SUPPORT_AUDIO */

View File

@ -29,8 +29,6 @@
#include "windows.h" #include "windows.h"
#include "file_senders.h" #include "file_senders.h"
#define TIME_STR_SIZE 16
struct FileReceiver { struct FileReceiver {
uint8_t filenames[MAX_FILES][MAX_STR_SIZE]; uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
FILE *files[MAX_FILES]; FILE *files[MAX_FILES];

View File

@ -59,14 +59,14 @@ struct tm *get_time(void)
return timeinfo; return timeinfo;
} }
/*Puts the current time in buf in the format of [Hour:Min:Sec] */ /*Puts the current time in buf in the format of [HH:mm:ss] */
void get_time_str(uint8_t *buf, int bufsize) void get_time_str(uint8_t *buf, int bufsize)
{ {
const char *t = user_settings->time == TIME_12 ? "[%-I:%M:%S] " : "[%H:%M:%S] "; const char *t = user_settings->time == TIME_12 ? "[%-I:%M:%S] " : "[%H:%M:%S] ";
strftime(buf, bufsize, t, get_time()); strftime(buf, bufsize, t, get_time());
} }
/* Converts seconds to hours:minutes:seconds string */ /* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */
void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs) void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs)
{ {
if (!secs) if (!secs)
@ -76,7 +76,12 @@ void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs)
uint64_t minutes = (secs % 3600) / 60; uint64_t minutes = (secs % 3600) / 60;
uint64_t hours = secs / 3600; uint64_t hours = secs / 3600;
snprintf(buf, bufsize, "%.2ld:%.2ld:%.2ld", hours, minutes, seconds); if (!minutes && !hours)
snprintf(buf, bufsize, "%.2ld", seconds);
else if (!hours)
snprintf(buf, bufsize, "%ld:%.2ld", minutes, seconds);
else
snprintf(buf, bufsize, "%ld:%.2ld:%.2ld", hours, minutes, seconds);
} }
char *hex_string_to_bin(const char *hex_string) char *hex_string_to_bin(const char *hex_string)
@ -84,11 +89,8 @@ char *hex_string_to_bin(const char *hex_string)
size_t len = strlen(hex_string); size_t len = strlen(hex_string);
char *val = malloc(len); char *val = malloc(len);
if (val == NULL) { if (val == NULL)
endwin(); exit_toxic_err("failed in hex_string_to_bin", FATALERR_MEMORY);
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
size_t i; size_t i;
@ -104,7 +106,7 @@ int string_is_empty(char *string)
return string[0] == '\0'; return string[0] == '\0';
} }
/* convert a multibyte string to a wide character string (must provide buffer) */ /* convert a multibyte string to a wide character string and puts in buf. */
int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n) int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n)
{ {
size_t len = mbstowcs(NULL, string, 0) + 1; size_t len = mbstowcs(NULL, string, 0) + 1;
@ -112,14 +114,13 @@ int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n)
if (n < len) if (n < len)
return -1; return -1;
if ((len = mbstowcs(buf, string, n)) == (size_t) - 1) if ((len = mbstowcs(buf, string, n)) == (size_t) -1)
return -1; return -1;
return len; return len;
} }
/* converts wide character string into a multibyte string. /* converts wide character string into a multibyte string and puts in buf. */
Same thing as wcs_to_mbs() but caller must provide its own buffer */
int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n) int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n)
{ {
size_t len = wcstombs(NULL, string, 0) + 1; size_t len = wcstombs(NULL, string, 0) + 1;
@ -127,59 +128,12 @@ int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n)
if (n < len) if (n < len)
return -1; return -1;
if ((len = wcstombs(buf, string, n)) == (size_t) - 1) if ((len = wcstombs(buf, string, n)) == (size_t) -1)
return -1; return -1;
return len; return len;
} }
/* convert wide characters to multibyte string: string returned must be freed */
uint8_t *wcs_to_mbs(wchar_t *string)
{
uint8_t *ret = NULL;
size_t len = wcstombs(NULL, string, 0);
if (len != (size_t) - 1) {
ret = malloc(++len);
if (ret != NULL) {
if (wcstombs(ret, string, len) == (size_t) - 1)
return NULL;
}
} else {
ret = malloc(2);
if (ret != NULL) {
ret[0] = ' ';
ret[1] = '\0';
}
}
if (ret == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
return ret;
}
/* convert a wide char to multibyte string */
char *wc_to_char(wchar_t ch)
{
static char ret[MB_LEN_MAX + 1];
int len = wctomb(ret, ch);
if (len == -1) {
ret[0] = ' ';
ret[1] = '\0';
} else {
ret[len] = '\0';
}
return ret;
}
/* Returns 1 if connection has timed out, 0 otherwise */ /* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout) int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout)
{ {

View File

@ -39,10 +39,10 @@ char *hex_string_to_bin(const char *hex_string);
/* get the current unix time */ /* get the current unix time */
uint64_t get_unix_time(void); uint64_t get_unix_time(void);
/*Puts the current time in buf in the format of [Hour:Min:Sec] */ /*Puts the current time in buf in the format of [HH:mm:ss] */
void get_time_str(uint8_t *buf, int bufsize); void get_time_str(uint8_t *buf, int bufsize);
/* Converts seconds to hours:minutes:seconds string */ /* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */
void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs); void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs);
/* get the current local time */ /* get the current local time */
@ -57,19 +57,12 @@ int string_is_empty(char *string);
/* convert a multibyte string to a wide character string (must provide buffer) */ /* convert a multibyte string to a wide character string (must provide buffer) */
int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n); int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n);
/* converts wide character string into a multibyte string. /* converts wide character string into a multibyte string and puts in buf. */
Same thing as wcs_to_mbs() but caller must provide its own buffer */
int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n); int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n);
/* convert a multibyte string to a wide character string (must provide buffer) */ /* convert a multibyte string to a wide character string and puts in buf) */
int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n); int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n);
/* convert wide characters to multibyte string: string returned must be free'd */
uint8_t *wcs_to_mbs(wchar_t *string);
/* convert a wide char to multibyte char */
char *wc_to_char(wchar_t ch);
/* Returns 1 if connection has timed out, 0 otherwise */ /* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t timeout, uint64_t curtime); int timed_out(uint64_t timestamp, uint64_t timeout, uint64_t curtime);

View File

@ -153,8 +153,8 @@ struct infobox {
bool hide; bool hide;
bool active; bool active;
uint64_t calltime;
uint64_t lastupdate; uint64_t lastupdate;
uint64_t starttime;
char timestr[TIME_STR_SIZE]; char timestr[TIME_STR_SIZE];
WINDOW *win; WINDOW *win;