From 7ac771326832d2a93df4717378b518d621cb154d Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 24 Jun 2014 01:54:08 -0400 Subject: [PATCH] improve call duration string & remove a couple unused functions --- src/chat.c | 82 +++++++++++++++++++++++++----------------------- src/friendlist.h | 2 -- src/misc_tools.c | 74 +++++++++---------------------------------- src/misc_tools.h | 15 +++------ src/windows.h | 2 +- 5 files changed, 61 insertions(+), 114 deletions(-) diff --git a/src/chat.c b/src/chat.c index 8cbc2b7..3fe24c0 100644 --- a/src/chat.c +++ b/src/chat.c @@ -546,33 +546,37 @@ void chat_onPeerTimeout (ToxWindow *self, ToxAv *av, int call_index) static void init_infobox(ToxWindow *self) { + ChatContext *ctx = self->chatwin; + int x2, y2; 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); - self->chatwin->infobox.calltime = get_unix_time(); - self->chatwin->infobox.vad_lvl = VAD_THRESHOLD_DEFAULT; - self->chatwin->infobox.active = true; - strcpy(self->chatwin->infobox.timestr, "00:00:00"); + ctx->infobox.win = newwin(INFOBOX_HEIGHT, INFOBOX_WIDTH + 1, 1, x2 - INFOBOX_WIDTH); + ctx->infobox.starttime = get_unix_time(); + ctx->infobox.vad_lvl = VAD_THRESHOLD_DEFAULT; + ctx->infobox.active = true; + strcpy(ctx->infobox.timestr, "00"); } static void kill_infobox(ToxWindow *self) { - if (!self->chatwin->infobox.win) + ChatContext *ctx = self->chatwin; + + if (!ctx->infobox.win) return; - delwin(self->chatwin->infobox.win); - memset(&self->chatwin->infobox, 0, sizeof(struct infobox)); + delwin(ctx->infobox.win); + memset(&ctx->infobox, 0, sizeof(struct infobox)); } /* update infobox info and draw in respective chat window */ 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; int x2, y2; @@ -584,43 +588,41 @@ static void draw_infobox(ToxWindow *self) uint64_t curtime = get_unix_time(); /* update elapsed time string once per second */ - if (curtime > infobox.lastupdate) { - infobox.calltime = curtime - infobox.calltime; - get_elapsed_time_str(infobox.timestr, sizeof(infobox.timestr), infobox.calltime); - } + if (curtime > infobox->lastupdate) + get_elapsed_time_str(infobox->timestr, sizeof(infobox->timestr), curtime - infobox->starttime); - infobox.lastupdate = curtime; + infobox->lastupdate = curtime; - const char *in_is_muted = infobox.in_is_muted ? "yes" : "no"; - const char *out_is_muted = infobox.out_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"; - wmove(infobox.win, 1, 1); - wattron(infobox.win, COLOR_PAIR(RED) | A_BOLD); - wprintw(infobox.win, " Call Active\n"); - wattroff(infobox.win, COLOR_PAIR(RED) | A_BOLD); + wmove(infobox->win, 1, 1); + wattron(infobox->win, COLOR_PAIR(RED) | A_BOLD); + wprintw(infobox->win, " Call Active\n"); + wattroff(infobox->win, COLOR_PAIR(RED) | A_BOLD); - wattron(infobox.win, A_BOLD); - wprintw(infobox.win, " Time: "); - wattroff(infobox.win, A_BOLD); - wprintw(infobox.win, "%s\n", infobox.timestr); + wattron(infobox->win, A_BOLD); + wprintw(infobox->win, " Duration: "); + wattroff(infobox->win, A_BOLD); + wprintw(infobox->win, "%s\n", infobox->timestr); - wattron(infobox.win, A_BOLD); - wprintw(infobox.win, " In muted: "); - wattroff(infobox.win, A_BOLD); - wprintw(infobox.win, "%s\n", in_is_muted); + wattron(infobox->win, A_BOLD); + wprintw(infobox->win, " In muted: "); + wattroff(infobox->win, A_BOLD); + wprintw(infobox->win, "%s\n", in_is_muted); - wattron(infobox.win, A_BOLD); - wprintw(infobox.win, " Out muted: "); - wattroff(infobox.win, A_BOLD); - wprintw(infobox.win, "%s\n", out_is_muted); + wattron(infobox->win, A_BOLD); + wprintw(infobox->win, " Out muted: "); + wattroff(infobox->win, A_BOLD); + wprintw(infobox->win, "%s\n", out_is_muted); - wattron(infobox.win, A_BOLD); - wprintw(infobox.win, " VAD level: "); - wattroff(infobox.win, A_BOLD); - wprintw(infobox.win, "%.2f\n", infobox.vad_lvl); + wattron(infobox->win, A_BOLD); + wprintw(infobox->win, " VAD level: "); + wattroff(infobox->win, A_BOLD); + wprintw(infobox->win, "%.2f\n", infobox->vad_lvl); - wborder(infobox.win, ACS_VLINE, ' ', ACS_HLINE, ACS_HLINE, ACS_TTEE, ' ', ACS_LLCORNER, ' '); - wrefresh(infobox.win); + wborder(infobox->win, ACS_VLINE, ' ', ACS_HLINE, ACS_HLINE, ACS_TTEE, ' ', ACS_LLCORNER, ' '); + wrefresh(infobox->win); } #endif /* _SUPPORT_AUDIO */ diff --git a/src/friendlist.h b/src/friendlist.h index f6cc9fd..8de55c0 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -29,8 +29,6 @@ #include "windows.h" #include "file_senders.h" -#define TIME_STR_SIZE 16 - struct FileReceiver { uint8_t filenames[MAX_FILES][MAX_STR_SIZE]; FILE *files[MAX_FILES]; diff --git a/src/misc_tools.c b/src/misc_tools.c index a90ce3f..568724a 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -59,14 +59,14 @@ struct tm *get_time(void) 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) { const char *t = user_settings->time == TIME_12 ? "[%-I:%M:%S] " : "[%H:%M:%S] "; 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) { 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 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) @@ -84,11 +89,8 @@ char *hex_string_to_bin(const char *hex_string) size_t len = strlen(hex_string); char *val = malloc(len); - if (val == NULL) { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } + if (val == NULL) + exit_toxic_err("failed in hex_string_to_bin", FATALERR_MEMORY); size_t i; @@ -104,7 +106,7 @@ int string_is_empty(char *string) 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) { 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) return -1; - if ((len = mbstowcs(buf, string, n)) == (size_t) - 1) + if ((len = mbstowcs(buf, string, n)) == (size_t) -1) return -1; return len; } -/* converts wide character string into a multibyte string. - Same thing as wcs_to_mbs() but caller must provide its own buffer */ +/* converts wide character string into a multibyte string and puts in buf. */ int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n) { 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) return -1; - if ((len = wcstombs(buf, string, n)) == (size_t) - 1) + if ((len = wcstombs(buf, string, n)) == (size_t) -1) return -1; 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 */ int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout) { diff --git a/src/misc_tools.h b/src/misc_tools.h index 32eeb54..622c746 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -39,10 +39,10 @@ char *hex_string_to_bin(const char *hex_string); /* get the current unix time */ 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); -/* 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); /* 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) */ int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n); -/* converts wide character string into a multibyte string. - Same thing as wcs_to_mbs() but caller must provide its own buffer */ +/* converts wide character string into a multibyte string and puts in buf. */ 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); -/* 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 */ int timed_out(uint64_t timestamp, uint64_t timeout, uint64_t curtime); diff --git a/src/windows.h b/src/windows.h index 704bd51..ec8dc09 100644 --- a/src/windows.h +++ b/src/windows.h @@ -153,8 +153,8 @@ struct infobox { bool hide; bool active; - uint64_t calltime; uint64_t lastupdate; + uint64_t starttime; char timestr[TIME_STR_SIZE]; WINDOW *win;