diff --git a/README.md b/README.md index 32972d7..d949b45 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Toxic [![Build Status](https://travis-ci.org/Tox/toxic.png?branch=master)](https://travis-ci.org/Tox/toxic) Toxic is a [Tox](https://tox.im)-based instant messenging client which formerly resided in the [Tox core repository](https://github.com/irungentoo/toxcore), and is now available as a standalone application. -![Toxic Screenshot](https://i.imgur.com/ueK1Tdj.png "Home Screen"). +![Toxic Screenshot](https://i.imgur.com/ryaEmQZ.png "Home Screen"). ## Installation diff --git a/build/Makefile b/build/Makefile index 8061608..99f0d22 100644 --- a/build/Makefile +++ b/build/Makefile @@ -10,7 +10,7 @@ SND_DIR = ../sounds PREFIX = /usr/local BINDIR = $(PREFIX)/bin DATADIR = $(PREFIX)/share/toxic -MANDIR = $(PREFIX)/man +MANDIR = $(PREFIX)/share/man DATAFILES = DHTnodes toxic.conf.example MANFILES = toxic.1 toxic.conf.5 SNDFILES = ContactLogsIn.wav ContactLogsOut.wav Error.wav IncomingCall.wav diff --git a/src/chat.c b/src/chat.c index 9d6ce2e..92c16c3 100644 --- a/src/chat.c +++ b/src/chat.c @@ -363,8 +363,14 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec switch (control_type) { case TOX_FILECONTROL_ACCEPT: if (receive_send == 1) { - snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1f%%)", filename, 0.0); - file_senders[i].line_id = self->chatwin->hst->line_end->id + 1; + const char *r_msg = "File transfer for '%s' accepted."; + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, r_msg, filename); + + /* prep progress bar line */ + char progline[MAX_STR_SIZE]; + prep_prog_line(progline); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, progline); + file_senders[i].line_id = self->chatwin->hst->line_end->id + 2; notify(self, silent, NT_NOFOCUS | NT_BEEP | NT_WNDALERT_2); } @@ -389,7 +395,8 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec break; } - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); + if (msg[0]) + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); } static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum, const char *data, @@ -408,19 +415,17 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu } } - long double remain = (long double) tox_file_data_remaining(m, num, filenum, 1); + friends[num].file_receiver.bps[filenum] += length; + double remain = (double) tox_file_data_remaining(m, num, filenum, 1); uint64_t curtime = get_unix_time(); - /* refresh line with percentage complete */ + /* refresh line with percentage complete and transfer speed (must be called once per second) */ if (!remain || timed_out(friends[num].file_receiver.last_progress[filenum], curtime, 1)) { friends[num].file_receiver.last_progress[filenum] = curtime; uint64_t size = friends[num].file_receiver.size[filenum]; - long double pct_remain = remain ? (1 - (remain / size)) * 100 : 100; - - char msg[MAX_STR_SIZE]; - const char *name = friends[num].file_receiver.filenames[filenum]; - snprintf(msg, sizeof(msg), "Saving file as: '%s' (%.1Lf%%)", name, pct_remain); - line_info_set(self, friends[num].file_receiver.line_id[filenum], msg); + double pct_remain = remain ? (1 - (remain / size)) * 100 : 100; + print_progress_bar(self, filenum, num, pct_remain); + friends[num].file_receiver.bps[filenum] = 0; } } @@ -858,7 +863,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m) } wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); - wprintw(statusbar->topline, " O"); + wprintw(statusbar->topline, " %s", ONLINE_CHAR); wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); if (friends[self->num].is_typing) @@ -871,7 +876,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m) if (friends[self->num].is_typing) wattroff(statusbar->topline, COLOR_PAIR(YELLOW)); } else { - wprintw(statusbar->topline, " o"); + wprintw(statusbar->topline, " %s", OFFLINE_CHAR); wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " %s ", statusbar->nick); wattroff(statusbar->topline, A_BOLD); diff --git a/src/chat_commands.c b/src/chat_commands.c index 26f9ec8..73d5bc8 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -127,9 +127,14 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv const char *filename = friends[self->num].file_receiver.filenames[filenum]; if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) { - const char *msg = "Saving file as: '%s' (%.1f%%)"; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg, filename, 0.0); - friends[self->num].file_receiver.line_id[filenum] = self->chatwin->hst->line_end->id + 1; + const char *msg = "Saving file as: '%s'"; + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg, filename); + + /* prep progress bar line */ + char progline[MAX_STR_SIZE]; + prep_prog_line(progline); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, progline); + friends[self->num].file_receiver.line_id[filenum] = self->chatwin->hst->line_end->id + 2; if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) { errmsg = "* Error writing to file."; diff --git a/src/file_senders.c b/src/file_senders.c index a62b69b..6d1e6d3 100644 --- a/src/file_senders.c +++ b/src/file_senders.c @@ -27,6 +27,7 @@ #include "toxic.h" #include "windows.h" +#include "friendlist.h" #include "file_senders.h" #include "line_info.h" #include "misc_tools.h" @@ -34,6 +35,75 @@ FileSender file_senders[MAX_FILES]; uint8_t max_file_senders_index; +extern ToxicFriend friends[MAX_FRIENDS_NUM]; + +#define KiB 1024 +#define MiB 1048576 /* 1024 ^ 2 */ +#define GiB 1073741824 /* 1024 ^ 3 */ + +/* creates initial progress line that will be updated during file transfer. */ +void prep_prog_line(char *progline) +{ + strcpy(progline, "0.0 B/s ["); + int i; + + for (i = 0; i < NUM_PROG_MARKS; ++i) + strcat(progline, " "); + + strcat(progline, "] 0%%"); +} + +/* prints a progress bar for file transfers. + if friendnum is -1 we're sending the file, otherwise we're receiving. */ +void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_remain) +{ + double bps; + uint32_t line_id; + + if (friendnum < 0) { + bps = file_senders[idx].bps; + line_id = file_senders[idx].line_id; + } else { + bps = friends[friendnum].file_receiver.bps[idx]; + line_id = friends[friendnum].file_receiver.line_id[idx]; + } + + const char *unit; + + if (bps < KiB) { + unit = "B/s"; + } else if (bps < MiB) { + unit = "KiB/s"; + bps /= (double) KiB; + } else if (bps < GiB) { + unit = "MiB/s"; + bps /= (double) MiB; + } else { + unit = "GiB/s"; + bps /= (double) GiB; + } + + char msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "%.1f %s [", bps, unit); + int n = pct_remain / (100 / NUM_PROG_MARKS); + int i; + + for (i = 0; i < n; ++i) + strcat(msg, "#"); + + int j; + + for (j = i; j < NUM_PROG_MARKS; ++j) + strcat(msg, " "); + + strcat(msg, "] "); + + char pctstr[16]; + snprintf(pctstr, sizeof(pctstr), "%.2f%%", pct_remain); + strcat(msg, pctstr); + + line_info_set(self, line_id, msg); +} static void set_max_file_senders_index(void) { @@ -105,19 +175,18 @@ void do_file_senders(Tox *m) uint64_t curtime = get_unix_time(); file_senders[i].timestamp = curtime; + file_senders[i].bps += file_senders[i].piecelen; file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, friendnum), fp); - long double remain = (long double) tox_file_data_remaining(m, friendnum, filenum, 0); + double remain = (double) tox_file_data_remaining(m, friendnum, filenum, 0); - /* refresh line with percentage complete */ + /* refresh line with percentage complete and transfer speed (must be called once per second) */ if ((self->chatwin != NULL && timed_out(file_senders[i].last_progress, curtime, 1)) || !remain) { file_senders[i].last_progress = curtime; - uint64_t size = file_senders[i].size; - long double pct_remain = remain ? (1 - (remain / size)) * 100 : 100; - - snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1Lf%%)", pathname, pct_remain); - line_info_set(self, file_senders[i].line_id, msg); + double pct_remain = remain ? (1 - (remain / file_senders[i].size)) * 100 : 100; + print_progress_bar(self, i, -1, pct_remain); + file_senders[i].bps = 0; } if (file_senders[i].piecelen == 0) { diff --git a/src/file_senders.h b/src/file_senders.h index 4611179..a6cc5ee 100644 --- a/src/file_senders.h +++ b/src/file_senders.h @@ -29,6 +29,7 @@ #define FILE_PIECE_SIZE 2048 /* must be >= (MAX_CRYPTO_DATA_SIZE - 2) in toxcore/net_crypto.h */ #define MAX_FILES 255 #define TIMEOUT_FILESENDER 120 +#define NUM_PROG_MARKS 50 /* number of "#"'s in file transfer progress bar. Keep well below MAX_STR_SIZE */ typedef struct { FILE *file; @@ -41,10 +42,18 @@ typedef struct { char pathname[MAX_STR_SIZE]; uint64_t timestamp; uint64_t last_progress; + double bps; uint64_t size; uint32_t line_id; } FileSender; +/* creates progress line that will be updated during file transfer. */ +void prep_prog_line(char *progline); + +/* prints a progress bar for file transfers. + if friendnum is -1 we're sending the file, otherwise we're receiving. */ +void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_remain); + void close_all_file_senders(Tox *m); void do_file_senders(Tox *m); diff --git a/src/friendlist.c b/src/friendlist.c index 569bc79..7dc69c8 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -447,7 +447,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) } wattron(self->window, COLOR_PAIR(colour) | A_BOLD); - wprintw(self->window, "O "); + wprintw(self->window, "%s ", ONLINE_CHAR); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); if (f_selected) @@ -487,7 +487,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, "\n"); } else { - wprintw(self->window, "o "); + wprintw(self->window, "%s ", OFFLINE_CHAR); if (f_selected) wattron(self->window, COLOR_PAIR(BLUE)); diff --git a/src/friendlist.h b/src/friendlist.h index b108b79..b4f6f7c 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -34,6 +34,7 @@ struct FileReceiver { FILE *files[MAX_FILES]; bool pending[MAX_FILES]; uint64_t size[MAX_FILES]; + double bps[MAX_FILES]; uint64_t last_progress[MAX_FILES]; uint32_t line_id[MAX_FILES]; }; diff --git a/src/help.c b/src/help.c index cbfdae3..ba8603c 100644 --- a/src/help.c +++ b/src/help.c @@ -181,7 +181,7 @@ static void help_draw_chat(ToxWindow *self) wprintw(win, " /call : Audio call\n"); wprintw(win, " /cancel : Cancel call\n"); - wprintw(win, " /answer : Answer incomming call\n"); + wprintw(win, " /answer : Answer incoming call\n"); wprintw(win, " /reject : Reject incoming call\n"); wprintw(win, " /hangup : Hangup active call\n"); wprintw(win, " /sdev : Change active device\n"); diff --git a/src/line_info.c b/src/line_info.c index c47b00e..2e11022 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -133,7 +133,8 @@ static struct line_info *line_info_ret_queue(struct history *hst) return ret; } -/* creates new line_info line and puts it in the queue */ +/* creates new line_info line and puts it in the queue. + SYS_MSG lines may contain an arbitrary number of arguments for string formatting */ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint8_t type, uint8_t bold, uint8_t colour, const char *msg, ...) { @@ -144,11 +145,16 @@ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint exit_toxic_err("failed in line_info_add", FATALERR_MEMORY); char frmt_msg[MAX_STR_SIZE] = {0}; - va_list args; - va_start(args, msg); - vsnprintf(frmt_msg, sizeof(frmt_msg), msg, args); - va_end(args); + /* WARNING: SYS_MSG lines must not contain untrusted input */ + if (type == SYS_MSG) { + va_list args; + va_start(args, msg); + vsnprintf(frmt_msg, sizeof(frmt_msg), msg, args); + va_end(args); + } else { + snprintf(frmt_msg, sizeof(frmt_msg), "%s", msg); + } int len = 1; /* there will always be a newline */ diff --git a/src/line_info.h b/src/line_info.h index c2aa01d..30229d8 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -67,7 +67,8 @@ struct history { int queue_sz; }; -/* creates new line_info line and puts it in the queue */ +/* creates new line_info line and puts it in the queue. + SYS_MSG lines may contain an arbitrary number of arguments for string formatting */ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint8_t type, uint8_t bold, uint8_t colour, const char *msg, ...); diff --git a/src/misc_tools.c b/src/misc_tools.c index 36bd140..a0315c3 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -245,4 +245,4 @@ int char_rfind(const char *s, char ch, int len) } return i; -} \ No newline at end of file +} diff --git a/src/prompt.c b/src/prompt.c index ef19b2e..ae1eae8 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -302,7 +302,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); - char *msg; + const char *msg; if (status == 1) { msg = "has come online"; @@ -367,11 +367,9 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) char nick[TOX_MAX_NAME_LENGTH]; char statusmsg[MAX_STR_SIZE]; - pthread_mutex_lock(&Winthread.lock); uint16_t n_len = tox_get_self_name(m, (uint8_t *) nick); uint16_t s_len = tox_get_self_status_message(m, (uint8_t *) statusmsg, MAX_STR_SIZE); uint8_t status = tox_get_self_user_status(m); - pthread_mutex_unlock(&Winthread.lock); nick[n_len] = '\0'; statusmsg[s_len] = '\0'; diff --git a/src/toxic.c b/src/toxic.c index b1edff8..085f0bd 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -613,6 +613,7 @@ int main(int argc, char *argv[]) load_data(m, DATA_FILE); prompt = init_windows(m); + prompt_init_statusbar(prompt, m); /* thread for ncurses stuff */ if (pthread_mutex_init(&Winthread.lock, NULL) != 0) @@ -624,8 +625,8 @@ int main(int argc, char *argv[]) #ifdef _AUDIO av = init_audio(prompt, m); - - + + set_primary_device(input, user_settings_->audio_in_dev); set_primary_device(output, user_settings_->audio_out_dev); #elif _SOUND_NOTIFY @@ -640,7 +641,7 @@ int main(int argc, char *argv[]) notify(prompt, self_log_in, 0); #endif /* _SOUND_NOTIFY */ - char *msg; + const char *msg; if (config_err) { msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile..."; @@ -653,7 +654,6 @@ int main(int argc, char *argv[]) } sort_friendlist_index(); - prompt_init_statusbar(prompt, m); uint64_t last_save = (uint64_t) time(NULL); diff --git a/src/toxic.h b/src/toxic.h index fa5dc4a..314b89f 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -64,6 +64,9 @@ #define T_KEY_C_H 0x08 /* ctrl-h */ #define T_KEY_C_Y 0x19 /* ctrl-y */ +#define ONLINE_CHAR "*" +#define OFFLINE_CHAR "*" + typedef enum _FATAL_ERRS { FATALERR_MEMORY = -1, /* malloc() or calloc() failed */ FATALERR_FREAD = -2, /* fread() failed on critical read */ diff --git a/src/toxic_strings.c b/src/toxic_strings.c index b7eb880..033bbf6 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -111,7 +111,7 @@ int yank_buf(ChatContext *ctx) if (!ctx->yank[0]) return -1; - if (ctx->yank_len + ctx->len >= MAX_STR_SIZE - 1) + if (ctx->yank_len + ctx->len >= MAX_STR_SIZE) return -1; wmemmove(&ctx->line[ctx->pos + ctx->yank_len], &ctx->line[ctx->pos], ctx->len - ctx->pos);