From 0047ba0e9f664b4c2ca202b7aa6c2dd63ed4ca81 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 8 Aug 2015 12:55:46 -0400 Subject: [PATCH 01/13] fix rare bug preventing toxcore from sleeping --- src/toxic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/toxic.c b/src/toxic.c index 1d64efc..9258c47 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -1048,7 +1048,7 @@ static int init_default_data_files(void) /* Adjusts usleep value so that tox_do runs close to the recommended number of times per second */ static useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, uint64_t cur_time, useconds_t msleepval) { - useconds_t new_sleep = msleepval; + useconds_t new_sleep = MAX(msleepval, 3); ++(*loopcount); if (*looptimer == cur_time) From f559bdabfe28cf63b217c32cd6c8ec208bfb5536 Mon Sep 17 00:00:00 2001 From: JFreegman Date: Sat, 8 Aug 2015 16:51:04 -0400 Subject: [PATCH 02/13] Remove links to libtoxcore.so from readme --- README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/README.md b/README.md index 3e2dedb..4301139 100644 --- a/README.md +++ b/README.md @@ -7,20 +7,6 @@ Toxic is a [Tox](https://tox.chat)-based instant messenging client which formerl [Use our repositories](https://wiki.tox.chat/doku.php?id=developers:binaries#other_linux)
[Compile it yourself](/INSTALL.md) -## Downloads -If you don't like installation methods listed above, you can still download precompiled binaries from [jenkins](https://jenkins.libtoxcore.so): -* [Linux 32 bit](https://jenkins.libtoxcore.so/job/toxic_linux_i386/lastSuccessfulBuild/artifact/toxic_linux_i386.tar.xz) [![Build Status](https://jenkins.libtoxcore.so/job/toxic_linux_i386/badge/icon)](https://jenkins.libtoxcore.so/job/toxic_linux_i386/lastSuccessfulBuild/artifact/toxic_linux_i386.tar.xz) -* [Linux 64 bit](https://jenkins.libtoxcore.so/job/toxic_linux_amd64/lastSuccessfulBuild/artifact/toxic_linux_amd64.tar.xz) [![Build Status](https://jenkins.libtoxcore.so/job/toxic_linux_amd64/badge/icon)](https://jenkins.libtoxcore.so/job/toxic_linux_amd64/lastSuccessfulBuild/artifact/toxic_linux_amd64.tar.xz) -* [~~Linux ARMv6~~](https://jenkins.libtoxcore.so/job/toxic_linux_armv6/lastSuccessfulBuild/artifact/toxic_linux_armv6.tar.xz) **CURRENTLY DISABLED** [![Build Status](https://jenkins.libtoxcore.so/job/toxic_linux_armv6/badge/icon)](https://jenkins.libtoxcore.so/job/toxic_linux_armv6/lastSuccessfulBuild/artifact/toxic_linux_armv6.tar.xz) - -#### DEBs packages -* [toxic-i386.deb](https://jenkins.libtoxcore.so/job/toxic-linux-pkg/lastSuccessfulBuild/artifact/toxic-i386.deb) -* [toxic-x86_64.deb](https://jenkins.libtoxcore.so/job/toxic-linux-pkg/lastSuccessfulBuild/artifact/toxic-x86_64.deb) - -#### RPMs packages -* [toxic-i386.rpm](https://jenkins.libtoxcore.so/job/toxic-linux-pkg/lastSuccessfulBuild/artifact/toxic-i386.rpm) -* [toxic-x86_64.rpm](https://jenkins.libtoxcore.so/job/toxic-linux-pkg/lastSuccessfulBuild/artifact/toxic-x86_64.rpm) - ## Settings Running Toxic for the first time creates an empty file called toxic.conf in your home configuration directory ("~/.config/tox" for Linux users). Adding options to this file allows you to enable auto-logging, change the time format (12/24 hour), and much more. You can view our example config file [here](misc/toxic.conf.example). From 32e541bd1cf291dcfb7726315f73739f453685c1 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 10 Aug 2015 14:22:13 -0400 Subject: [PATCH 03/13] Fix file transfers breaking when friends go offline --- src/chat.c | 101 ++++++++++++++++++++++++++++++++++++++----- src/chat_commands.c | 1 + src/file_transfers.c | 4 +- src/file_transfers.h | 7 +-- 4 files changed, 97 insertions(+), 16 deletions(-) diff --git a/src/chat.c b/src/chat.c index f41958d..89cbe93 100644 --- a/src/chat.c +++ b/src/chat.c @@ -188,8 +188,8 @@ static void chat_onMessage(ToxWindow *self, Tox *m, uint32_t num, TOX_MESSAGE_TY return recv_action_helper(self, m, num, msg, len, nick, timefrmt); } -static void chat_resume_file_transfers(Tox *m, uint32_t fnum); -static void chat_stop_file_senders(Tox *m, uint32_t friendnum); +static void chat_pause_file_transfers(Tox *m, uint32_t friendnum); +static void chat_resume_file_senders(ToxWindow *self, Tox *m, uint32_t fnum); static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_CONNECTION connection_status) { @@ -209,7 +209,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C if (connection_status != TOX_CONNECTION_NONE && statusbar->connection == TOX_CONNECTION_NONE) { Friends.list[num].is_typing = user_settings->show_typing_other == SHOW_TYPING_ON ? tox_friend_get_typing(m, num, NULL) : false; - chat_resume_file_transfers(m, num); + chat_resume_file_senders(self, m, num); msg = "has come online"; line_info_add(self, timefrmt, nick, NULL, CONNECTION, 0, GREEN, msg); @@ -220,7 +220,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C if (self->chatwin->self_is_typing) set_self_typingstatus(self, m, 0); - chat_stop_file_senders(m, num); + chat_pause_file_transfers(m, num); msg = "has gone offline"; line_info_add(self, timefrmt, nick, NULL, DISCONNECTION, 0, RED, msg); @@ -277,17 +277,44 @@ static void chat_onReadReceipt(ToxWindow *self, Tox *m, uint32_t num, uint32_t r cqueue_remove(self, m, receipt); } -/* Stops active file senders for this friend. Call when a friend goes offline */ -static void chat_stop_file_senders(Tox *m, uint32_t friendnum) +/* Stops active file transfers for this friend. Called when a friend goes offline */ +static void chat_pause_file_transfers(Tox *m, uint32_t friendnum) { - // TODO: core purges file transfers when a friend goes offline. Ideally we want to repair/resume - kill_all_file_transfers_friend(m, friendnum); + ToxicFriend *friend = &Friends.list[friendnum]; + + size_t i; + + for (i = 0; i < MAX_FILES; ++i) { + if (friend->file_sender[i].state >= FILE_TRANSFER_STARTED) + friend->file_sender[i].state = FILE_TRANSFER_PAUSED; + + if (friend->file_receiver[i].state >= FILE_TRANSFER_STARTED) + friend->file_receiver[i].state = FILE_TRANSFER_PAUSED; + } } -/* Tries to resume broken file transfers. Call when a friend comes online */ -static void chat_resume_file_transfers(Tox *m, uint32_t fnum) +/* Tries to resume broken file senders. Called when a friend comes online */ +static void chat_resume_file_senders(ToxWindow *self, Tox *m, uint32_t friendnum) { - // TODO + size_t i; + + for (i = 0; i < MAX_FILES; ++i) { + struct FileTransfer *ft = &Friends.list[friendnum].file_sender[i]; + + if (ft->state != FILE_TRANSFER_PAUSED) + continue; + + TOX_ERR_FILE_SEND err; + ft->filenum = tox_file_send(m, friendnum, TOX_FILE_KIND_DATA, ft->file_size, ft->file_id, + (uint8_t *) ft->file_name, strlen(ft->file_name), &err); + + if (err != TOX_ERR_FILE_SEND_OK) { + char msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name); + close_file_transfer(self, m, ft, -1, msg, notif_error); + continue; + } + } } static void chat_onFileChunkRequest(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, uint64_t position, @@ -428,12 +455,63 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint } } +/* Attempts to resume a broken inbound file transfer. + * + * Returns true if resume is successful. + */ +static bool chat_resume_broken_ft(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum) +{ + char msg[MAX_STR_SIZE]; + uint8_t file_id[TOX_FILE_ID_LENGTH]; + + if (!tox_file_get_file_id(m, friendnum, filenum, file_id, NULL)) + return false; + + bool resuming = false; + struct FileTransfer *ft = NULL; + size_t i; + + for (i = 0; i < MAX_FILES; ++i) { + ft = &Friends.list[friendnum].file_receiver[i]; + + if (ft->state == FILE_TRANSFER_INACTIVE) + continue; + + if (memcmp(ft->file_id, file_id, TOX_FILE_ID_LENGTH) == 0) { + ft->filenum = filenum; + resuming = true; + break; + } + } + + if (!resuming || !ft) + return false; + + if (!tox_file_seek(m, ft->friendnum, ft->filenum, ft->position, NULL)) + goto on_error; + + if (!tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_RESUME, NULL)) + goto on_error; + + ft->state = FILE_TRANSFER_STARTED; + return true; + +on_error: + snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name); + close_file_transfer(self, m, ft, -1, msg, notif_error); + return false; +} + static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, uint64_t file_size, const char *filename, size_t name_length) { if (self->num != friendnum) return; + /* first check if we need to resume a broken transfer */ + if (chat_resume_broken_ft(self, m, friendnum, filenum)) + return; + struct FileTransfer *ft = get_new_file_receiver(friendnum); if (!ft) { @@ -500,6 +578,7 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_ ft->file_type = TOX_FILE_KIND_DATA; snprintf(ft->file_path, sizeof(ft->file_path), "%s", file_path); snprintf(ft->file_name, sizeof(ft->file_name), "%s", filename); + tox_file_get_file_id(m, friendnum, filenum, ft->file_id, NULL); if (self->active_box != -1) box_notify2(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box, diff --git a/src/chat_commands.c b/src/chat_commands.c index 3352f47..ef9dbe0 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -278,6 +278,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv ft->friendnum = self->num; ft->direction = FILE_TRANSFER_SEND; ft->file_type = TOX_FILE_KIND_DATA; + tox_file_get_file_id(m, self->num, filenum, ft->file_id, NULL); char sizestr[32]; bytes_convert_str(sizestr, sizeof(sizestr), filesize); diff --git a/src/file_transfers.c b/src/file_transfers.c index 771f958..f4ce0e5 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -209,8 +209,6 @@ void close_file_transfer(ToxWindow *self, Tox *m, struct FileTransfer *ft, int C if (ft->file) fclose(ft->file); - memset(ft, 0, sizeof(struct FileTransfer)); - if (CTRL >= 0) tox_file_control(m, ft->friendnum, ft->filenum, (TOX_FILE_CONTROL) CTRL, NULL); @@ -222,6 +220,8 @@ void close_file_transfer(ToxWindow *self, Tox *m, struct FileTransfer *ft, int C line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", message); } + + memset(ft, 0, sizeof(struct FileTransfer)); } /* Kills all active file transfers for friendnum */ diff --git a/src/file_transfers.h b/src/file_transfers.h index 52b19e1..39b2c30 100644 --- a/src/file_transfers.h +++ b/src/file_transfers.h @@ -30,8 +30,8 @@ #include "notify.h" #define KiB 1024 -#define MiB 1048576 /* 1024 ^ 2 */ -#define GiB 1073741824 /* 1024 ^ 3 */ +#define MiB 1048576 /* 1024^2 */ +#define GiB 1073741824 /* 1024^3 */ #define MAX_FILES 32 #define TIMEOUT_FILESENDER 120 @@ -40,7 +40,7 @@ typedef enum FILE_TRANSFER_STATE { FILE_TRANSFER_INACTIVE, FILE_TRANSFER_PENDING, FILE_TRANSFER_STARTED, - FILE_TRANSFER_PAUSED + FILE_TRANSFER_PAUSED, } FILE_TRANSFER_STATE; typedef enum FILE_TRANSFER_DIRECTION { @@ -63,6 +63,7 @@ struct FileTransfer { uint64_t position; uint64_t last_progress; uint32_t line_id; + uint8_t file_id[TOX_FILE_ID_LENGTH]; }; /* creates initial progress line that will be updated during file transfer. From c39f8909cd284c0480e7f2516bd42a7feef6a796 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 10 Aug 2015 20:41:11 -0400 Subject: [PATCH 04/13] Send file control cancel on failure to resume transfer --- src/chat.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chat.c b/src/chat.c index 89cbe93..640c908 100644 --- a/src/chat.c +++ b/src/chat.c @@ -311,7 +311,7 @@ static void chat_resume_file_senders(ToxWindow *self, Tox *m, uint32_t friendnum if (err != TOX_ERR_FILE_SEND_OK) { char msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name); - close_file_transfer(self, m, ft, -1, msg, notif_error); + close_file_transfer(self, m, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); continue; } } @@ -490,7 +490,7 @@ static bool chat_resume_broken_ft(ToxWindow *self, Tox *m, uint32_t friendnum, u if (!tox_file_seek(m, ft->friendnum, ft->filenum, ft->position, NULL)) goto on_error; - if (!tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_RESUME, NULL)) + if (!tox_file_control(m, ft->friendnum, ft->filenum, TOX_FILE_CONTROL_RESUME, NULL)) goto on_error; ft->state = FILE_TRANSFER_STARTED; @@ -498,7 +498,7 @@ static bool chat_resume_broken_ft(ToxWindow *self, Tox *m, uint32_t friendnum, u on_error: snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name); - close_file_transfer(self, m, ft, -1, msg, notif_error); + close_file_transfer(self, m, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); return false; } From 48ffae68a9f2c15ddcd0719654f768a8f8687648 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 11 Aug 2015 19:22:57 -0400 Subject: [PATCH 05/13] msg[0] == '>') wattron(win, COLOR_PAIR(GREEN)); + else if (line->msg[0] == '<') + wattron(win, COLOR_PAIR(RED)); wprintw(win, "%s", line->msg); if (line->msg[0] == '>') wattroff(win, COLOR_PAIR(GREEN)); + else if (line->msg[0] == '<') + wattroff(win, COLOR_PAIR(RED)); if (type == OUT_MSG && timed_out(line->timestamp, get_unix_time(), NOREAD_FLAG_TIMEOUT)) { wattron(win, COLOR_PAIR(RED)); From 083611f18eb5e927d6bd041d2d4923b3307804e4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 13 Aug 2015 15:03:02 -0400 Subject: [PATCH 06/13] Update DHTnodes --- misc/DHTnodes | 1 - 1 file changed, 1 deletion(-) diff --git a/misc/DHTnodes b/misc/DHTnodes index cd1c92c..705f56a 100644 --- a/misc/DHTnodes +++ b/misc/DHTnodes @@ -5,7 +5,6 @@ 178.21.112.187 33445 4B2C19E924972CB9B57732FB172F8A8604DE13EEDA2A6234E348983344B23057 195.154.119.113 33445 E398A69646B8CEACA9F0B84F553726C1C49270558C57DF5F3C368F05A7D71354 192.210.149.121 33445 F404ABAA1C99A9D37D61AB54898F56793E1DEF8BD46B1038B9D822E8460FAB67 -104.219.184.206 443 8CD087E31C67568103E8C2A28653337E90E6B8EDA0D765D57C6B5172B4F1F04C 76.191.23.96 33445 93574A3FAB7D612FEA29FD8D67D3DD10DFD07A075A5D62E8AF3DD9F5D0932E11 46.38.239.179 33445 F5A1A38EFB6BD3C2C8AF8B10D85F0F89E931704D349F1D0720C3C4059AF2440A 178.62.250.138 33445 788236D34978D1D5BD822F0A5BEBD2C53C64CC31CD3149350EE27D4D9A2F9B6B From 48eaf8a14f5f72f0a9989f05baed837344a4234b Mon Sep 17 00:00:00 2001 From: Khalikin Viachaslau Date: Sat, 15 Aug 2015 15:42:32 +0000 Subject: [PATCH 07/13] fix a broken link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4301139..f4bbaee 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Toxic is a [Tox](https://tox.chat)-based instant messenging client which formerl [![Toxic Screenshot](https://i.imgur.com/san99Z2.png "Home Screen")](https://i.imgur.com/san99Z2.png) ## Installation -[Use our repositories](https://wiki.tox.chat/doku.php?id=developers:binaries#other_linux)
+[Use our repositories](https://wiki.tox.chat/binaries#other_linux)
[Compile it yourself](/INSTALL.md) ## Settings From 327259c4c823acb924ea79ff788c0b2e72168d4b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 18 Aug 2015 01:46:22 -0400 Subject: [PATCH 08/13] simplify timeout function --- src/file_transfers.c | 11 +++++------ src/groupchat.c | 8 ++++---- src/line_info.c | 4 ++-- src/log.c | 6 ++---- src/message_queue.c | 6 ++---- src/misc_tools.c | 4 ++-- src/misc_tools.h | 2 +- src/toxic.c | 7 +++---- 8 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/file_transfers.c b/src/file_transfers.c index f4ce0e5..3b1db20 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -80,13 +80,13 @@ void print_progress_bar(ToxWindow *self, double bps, double pct_done, uint32_t l line_info_set(self, line_id, msg); } -static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft, uint64_t curtime) +static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft) { if (ft->state == FILE_TRANSFER_INACTIVE) return; /* Timeout must be set to 1 second to show correct bytes per second */ - if (!timed_out(ft->last_progress, curtime, 1)) + if (!timed_out(ft->last_progress, 1)) return; double remain = ft->file_size - ft->position; @@ -94,18 +94,17 @@ static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft, ui print_progress_bar(self, ft->bps, pct_done, ft->line_id); ft->bps = 0; - ft->last_progress = curtime; + ft->last_progress = get_unix_time(); } /* refreshes active file receiver status bars for friendnum */ void refresh_file_transfer_progress(ToxWindow *self, Tox *m, uint32_t friendnum) { - uint64_t curtime = get_unix_time(); size_t i; for (i = 0; i < MAX_FILES; ++i) { - refresh_progress_helper(self, &Friends.list[friendnum].file_receiver[i], curtime); - refresh_progress_helper(self, &Friends.list[friendnum].file_sender[i], curtime); + refresh_progress_helper(self, &Friends.list[friendnum].file_receiver[i]); + refresh_progress_helper(self, &Friends.list[friendnum].file_sender[i]); } } diff --git a/src/groupchat.c b/src/groupchat.c index 751679e..9eeac25 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -323,7 +323,7 @@ static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum, get_time_str(timefrmt, sizeof(timefrmt)); /* don't announce title when we join the room */ - if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT)) + if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT)) return; char nick[TOX_MAX_NAME_LENGTH]; @@ -402,7 +402,7 @@ void *group_add_wait(void *data) pthread_mutex_lock(&Winthread.lock); get_group_nick_truncate(m, peername, thrd->peernum, thrd->groupnum); - if (strcmp(peername, DEFAULT_TOX_NAME) || timed_out(thrd->timestamp, get_unix_time(), GROUP_EVENT_WAIT)) { + if (strcmp(peername, DEFAULT_TOX_NAME) || timed_out(thrd->timestamp, GROUP_EVENT_WAIT)) { pthread_mutex_unlock(&Winthread.lock); break; } @@ -478,7 +478,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu switch (change) { case TOX_CHAT_CHANGE_PEER_ADD: - if (!timed_out(groupchats[groupnum].start_time, get_unix_time(), GROUP_EVENT_WAIT)) + if (!timed_out(groupchats[groupnum].start_time, GROUP_EVENT_WAIT)) break; struct group_add_thrd *thrd = malloc(sizeof(struct group_add_thrd)); @@ -518,7 +518,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu break; case TOX_CHAT_CHANGE_PEER_NAME: - if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT)) + if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT)) return; /* ignore initial name change (TODO: this is a bad way to do this) */ diff --git a/src/line_info.c b/src/line_info.c index 05bbcb7..2283908 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -332,7 +332,7 @@ void line_info_print(ToxWindow *self) else if (line->msg[0] == '<') wattroff(win, COLOR_PAIR(RED)); - if (type == OUT_MSG && timed_out(line->timestamp, get_unix_time(), NOREAD_FLAG_TIMEOUT)) { + if (type == OUT_MSG && timed_out(line->timestamp, NOREAD_FLAG_TIMEOUT)) { wattron(win, COLOR_PAIR(RED)); wprintw(win, " x", line->msg); wattroff(win, COLOR_PAIR(RED)); @@ -359,7 +359,7 @@ void line_info_print(ToxWindow *self) wprintw(win, "%s %s %s", user_settings->line_normal, line->name1, line->msg); wattroff(win, COLOR_PAIR(YELLOW)); - if (type == OUT_ACTION && timed_out(line->timestamp, get_unix_time(), NOREAD_FLAG_TIMEOUT)) { + if (type == OUT_ACTION && timed_out(line->timestamp, NOREAD_FLAG_TIMEOUT)) { wattron(win, COLOR_PAIR(RED)); wprintw(win, " x", line->msg); wattroff(win, COLOR_PAIR(RED)); diff --git a/src/log.c b/src/log.c index f04c83d..0924eb1 100644 --- a/src/log.c +++ b/src/log.c @@ -139,11 +139,9 @@ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool e strftime(s, MAX_STR_SIZE, t, get_time()); fprintf(log->file, "%s %s %s\n", s, name_frmt, msg); - uint64_t curtime = get_unix_time(); - - if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) { + if (timed_out(log->lastwrite, LOG_FLUSH_LIMIT)) { fflush(log->file); - log->lastwrite = curtime; + log->lastwrite = get_unix_time(); } } diff --git a/src/message_queue.c b/src/message_queue.c index 70ccadb..881e512 100644 --- a/src/message_queue.c +++ b/src/message_queue.c @@ -140,9 +140,7 @@ void cqueue_try_send(ToxWindow *self, Tox *m) if (!msg) return; - uint64_t curtime = get_unix_time(); - - if (msg->receipt != 0 && !timed_out(msg->last_send_try, curtime, CQUEUE_TRY_SEND_INTERVAL)) + if (msg->receipt != 0 && !timed_out(msg->last_send_try, CQUEUE_TRY_SEND_INTERVAL)) return; uint32_t receipt = 0; @@ -150,7 +148,7 @@ void cqueue_try_send(ToxWindow *self, Tox *m) TOX_MESSAGE_TYPE type = msg->type == OUT_MSG ? TOX_MESSAGE_TYPE_NORMAL : TOX_MESSAGE_TYPE_ACTION; receipt = tox_friend_send_message(m, self->num, type, (uint8_t *) msg->message, msg->len, NULL); - msg->last_send_try = curtime; + msg->last_send_try = get_unix_time(); msg->receipt = receipt; return; } diff --git a/src/misc_tools.c b/src/misc_tools.c index 0d0a749..3734aa2 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -65,9 +65,9 @@ uint64_t get_unix_time(void) } /* 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 timeout) { - return timestamp + timeout <= curtime; + return timestamp + timeout <= get_unix_time(); } /* Get the current local time */ diff --git a/src/misc_tools.h b/src/misc_tools.h index 6b20862..325411c 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -75,7 +75,7 @@ int wcs_to_mbs_buf(char *buf, const wchar_t *string, size_t n); int mbs_to_wcs_buf(wchar_t *buf, const char *string, size_t n); /* 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); /* Colours the window tab according to type. Beeps if is_beep is true */ void alert_window(ToxWindow *self, int type, bool is_beep); diff --git a/src/toxic.c b/src/toxic.c index 9258c47..29cc80a 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -755,9 +755,8 @@ static uint64_t last_bootstrap_time = 0; static void do_bootstrap(Tox *m) { static int conn_err = 0; - uint64_t curtime = get_unix_time(); - if (!timed_out(last_bootstrap_time, curtime, TRY_BOOTSTRAP_INTERVAL)) + if (!timed_out(last_bootstrap_time, TRY_BOOTSTRAP_INTERVAL)) return; if (tox_self_get_connection_status(m) != TOX_CONNECTION_NONE) @@ -766,7 +765,7 @@ static void do_bootstrap(Tox *m) if (conn_err != 0) return; - last_bootstrap_time = curtime; + last_bootstrap_time = get_unix_time(); conn_err = init_connection(m); if (conn_err != 0) @@ -1189,7 +1188,7 @@ int main(int argc, char *argv[]) do_toxic(m, prompt); uint64_t cur_time = get_unix_time(); - if (timed_out(last_save, cur_time, AUTOSAVE_FREQ)) { + if (timed_out(last_save, AUTOSAVE_FREQ)) { pthread_mutex_lock(&Winthread.lock); if (store_data(m, DATA_FILE) != 0) line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, "WARNING: Failed to save to data file"); From 2a787c109725731fba6cc2a07d1577a1dfffa1cf Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 18 Aug 2015 15:12:48 -0400 Subject: [PATCH 09/13] Fix some filetransfer issues - File transfers now timeout properly - Small refactor related to creating new transfers --- src/avatars.c | 8 +--- src/chat.c | 27 ++++++------- src/chat_commands.c | 9 +---- src/file_transfers.c | 93 +++++++++++++++++++++++++++++++++++++++----- src/file_transfers.h | 27 ++++++------- src/toxic.c | 1 + 6 files changed, 116 insertions(+), 49 deletions(-) diff --git a/src/avatars.c b/src/avatars.c index a17c3be..d58b27e 100644 --- a/src/avatars.c +++ b/src/avatars.c @@ -63,7 +63,7 @@ int avatar_send(Tox *m, uint32_t friendnum) return -1; } - struct FileTransfer *ft = get_new_file_sender(friendnum); + struct FileTransfer *ft = new_file_transfer(NULL, friendnum, filenum, FILE_TRANSFER_SEND, TOX_FILE_KIND_AVATAR); if (!ft) return -1; @@ -75,11 +75,6 @@ int avatar_send(Tox *m, uint32_t friendnum) snprintf(ft->file_name, sizeof(ft->file_name), "%s", Avatar.name); ft->file_size = Avatar.size; - ft->state = FILE_TRANSFER_PENDING; - ft->filenum = filenum; - ft->friendnum = friendnum; - ft->direction = FILE_TRANSFER_SEND; - ft->file_type = TOX_FILE_KIND_AVATAR; return 0; } @@ -206,4 +201,5 @@ void on_avatar_chunk_request(Tox *m, struct FileTransfer *ft, uint64_t position, fprintf(stderr, "tox_file_send_chunk failed in avatar callback (error %d)\n", err); ft->position += send_length; + ft->last_keep_alive = get_unix_time(); } diff --git a/src/chat.c b/src/chat.c index 640c908..720c86c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -373,6 +373,7 @@ static void chat_onFileChunkRequest(ToxWindow *self, Tox *m, uint32_t friendnum, ft->position += send_length; ft->bps += send_length; + ft->last_keep_alive = get_unix_time(); } static void chat_onFileRecvChunk(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, uint64_t position, @@ -412,6 +413,7 @@ static void chat_onFileRecvChunk(ToxWindow *self, Tox *m, uint32_t friendnum, ui ft->bps += length; ft->position += length; + ft->last_keep_alive = get_unix_time(); } static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, TOX_FILE_CONTROL control) @@ -427,14 +429,16 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint char msg[MAX_STR_SIZE]; switch (control) { - case TOX_FILE_CONTROL_RESUME: + case TOX_FILE_CONTROL_RESUME: { + ft->last_keep_alive = get_unix_time(); + /* transfer is accepted */ if (ft->state == FILE_TRANSFER_PENDING) { ft->state = FILE_TRANSFER_STARTED; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%d] for '%s' accepted.", ft->index, ft->file_name); char progline[MAX_STR_SIZE]; - prep_prog_line(progline); + init_progress_bar(progline); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline); sound_notify(self, silent, NT_NOFOCUS | NT_BEEP | NT_WNDALERT_2, NULL); ft->line_id = self->chatwin->hst->line_end->id + 2; @@ -443,15 +447,16 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint } break; - - case TOX_FILE_CONTROL_PAUSE: + } + case TOX_FILE_CONTROL_PAUSE: { ft->state = FILE_TRANSFER_PAUSED; break; - - case TOX_FILE_CONTROL_CANCEL: + } + case TOX_FILE_CONTROL_CANCEL: { snprintf(msg, sizeof(msg), "File transfer for '%s' was aborted.", ft->file_name); close_file_transfer(self, m, ft, -1, msg, notif_error); break; + } } } @@ -479,6 +484,8 @@ static bool chat_resume_broken_ft(ToxWindow *self, Tox *m, uint32_t friendnum, u if (memcmp(ft->file_id, file_id, TOX_FILE_ID_LENGTH) == 0) { ft->filenum = filenum; + ft->state = FILE_TRANSFER_STARTED; + ft->last_keep_alive = get_unix_time(); resuming = true; break; } @@ -493,7 +500,6 @@ static bool chat_resume_broken_ft(ToxWindow *self, Tox *m, uint32_t friendnum, u if (!tox_file_control(m, ft->friendnum, ft->filenum, TOX_FILE_CONTROL_RESUME, NULL)) goto on_error; - ft->state = FILE_TRANSFER_STARTED; return true; on_error: @@ -512,7 +518,7 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_ if (chat_resume_broken_ft(self, m, friendnum, filenum)) return; - struct FileTransfer *ft = get_new_file_receiver(friendnum); + struct FileTransfer *ft = new_file_transfer(self, friendnum, filenum, FILE_TRANSFER_RECV, TOX_FILE_KIND_DATA); if (!ft) { tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_CANCEL, NULL); @@ -570,12 +576,7 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_ line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %d' to accept the file transfer.", ft->index); - ft->state = FILE_TRANSFER_PENDING; - ft->direction = FILE_TRANSFER_RECV; ft->file_size = file_size; - ft->friendnum = friendnum; - ft->filenum = filenum; - ft->file_type = TOX_FILE_KIND_DATA; snprintf(ft->file_path, sizeof(ft->file_path), "%s", file_path); snprintf(ft->file_name, sizeof(ft->file_name), "%s", filename); tox_file_get_file_id(m, friendnum, filenum, ft->file_id, NULL); diff --git a/src/chat_commands.c b/src/chat_commands.c index ef9dbe0..2a4b8e7 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -181,7 +181,7 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv /* prep progress bar line */ char progline[MAX_STR_SIZE]; - prep_prog_line(progline); + init_progress_bar(progline); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline); ft->line_id = self->chatwin->hst->line_end->id + 2; @@ -263,7 +263,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv if (err != TOX_ERR_FILE_SEND_OK) goto on_send_error; - struct FileTransfer *ft = get_new_file_sender(self->num); + struct FileTransfer *ft = new_file_transfer(self, self->num, filenum, FILE_TRANSFER_SEND, TOX_FILE_KIND_DATA); if (!ft) { err = TOX_ERR_FILE_SEND_TOO_MANY; @@ -271,13 +271,8 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv } memcpy(ft->file_name, file_name, namelen + 1); - ft->state = FILE_TRANSFER_PENDING; ft->file = file_to_send; ft->file_size = filesize; - ft->filenum = filenum; - ft->friendnum = self->num; - ft->direction = FILE_TRANSFER_SEND; - ft->file_type = TOX_FILE_KIND_DATA; tox_file_get_file_id(m, self->num, filenum, ft->file_id, NULL); char sizestr[32]; diff --git a/src/file_transfers.c b/src/file_transfers.c index 3b1db20..7cd915f 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -35,11 +35,52 @@ extern FriendsList Friends; -#define NUM_PROG_MARKS 50 /* number of "#"'s in file transfer progress bar. Keep well below MAX_STR_SIZE */ +/* number of "#"'s in file transfer progress bar. Keep well below MAX_STR_SIZE */ +#define NUM_PROG_MARKS 50 + +/* Checks for timed out file transfers and closes them. */ +#define CHECK_FILE_TIMEOUT_INTERAVAL 5 +void check_file_transfer_timeouts(Tox *m) +{ + char msg[MAX_STR_SIZE]; + static uint64_t last_check = 0; + + if (!timed_out(last_check, CHECK_FILE_TIMEOUT_INTERAVAL)) + return; + + last_check = get_unix_time(); + + size_t i, j; + + for (i = 0; i < Friends.max_idx; ++i) { + if (!Friends.list[i].active) + continue; + + for (j = 0; j < MAX_FILES; ++j) { + struct FileTransfer *ft_send = &Friends.list[i].file_sender[j]; + + if (ft_send->state > FILE_TRANSFER_PAUSED) { + if (timed_out(ft_send->last_keep_alive, TIMEOUT_FILESENDER)) { + snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", ft_send->file_name); + close_file_transfer(ft_send->window, m, ft_send, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + } + } + + struct FileTransfer *ft_recv = &Friends.list[i].file_receiver[j]; + + if (ft_recv->state > FILE_TRANSFER_PAUSED) { + if (timed_out(ft_recv->last_keep_alive, TIMEOUT_FILESENDER)) { + snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", ft_recv->file_name); + close_file_transfer(ft_recv->window, m, ft_recv, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + } + } + } + } +} /* creates initial progress line that will be updated during file transfer. Assumes progline is of size MAX_STR_SIZE */ -void prep_prog_line(char *progline) +void init_progress_bar(char *progline) { strcpy(progline, "0.0 B/s ["); int i; @@ -80,13 +121,13 @@ void print_progress_bar(ToxWindow *self, double bps, double pct_done, uint32_t l line_info_set(self, line_id, msg); } -static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft) +static void refresh_progress_helper(ToxWindow *self, Tox *m, struct FileTransfer *ft) { if (ft->state == FILE_TRANSFER_INACTIVE) return; /* Timeout must be set to 1 second to show correct bytes per second */ - if (!timed_out(ft->last_progress, 1)) + if (!timed_out(ft->last_line_progress, 1)) return; double remain = ft->file_size - ft->position; @@ -94,17 +135,17 @@ static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft) print_progress_bar(self, ft->bps, pct_done, ft->line_id); ft->bps = 0; - ft->last_progress = get_unix_time(); + ft->last_line_progress = get_unix_time(); } -/* refreshes active file receiver status bars for friendnum */ +/* refreshes active file transfer status bars. */ void refresh_file_transfer_progress(ToxWindow *self, Tox *m, uint32_t friendnum) { size_t i; for (i = 0; i < MAX_FILES; ++i) { - refresh_progress_helper(self, &Friends.list[friendnum].file_receiver[i]); - refresh_progress_helper(self, &Friends.list[friendnum].file_sender[i]); + refresh_progress_helper(self, m, &Friends.list[friendnum].file_receiver[i]); + refresh_progress_helper(self, m, &Friends.list[friendnum].file_sender[i]); } } @@ -156,7 +197,7 @@ struct FileTransfer *get_file_transfer_struct_index(uint32_t friendnum, uint32_t /* Returns a pointer to an unused file sender. * Returns NULL if all file senders are in use. */ -struct FileTransfer *get_new_file_sender(uint32_t friendnum) +static struct FileTransfer *new_file_sender(ToxWindow *window, uint32_t friendnum, uint32_t filenum, uint8_t type) { size_t i; @@ -164,7 +205,15 @@ struct FileTransfer *get_new_file_sender(uint32_t friendnum) struct FileTransfer *ft = &Friends.list[friendnum].file_sender[i]; if (ft->state == FILE_TRANSFER_INACTIVE) { + memset(ft, 0, sizeof(struct FileTransfer)); + ft->window = window; ft->index = i; + ft->friendnum = friendnum; + ft->filenum = filenum; + ft->file_type = type; + ft->last_keep_alive = get_unix_time(); + ft->state = FILE_TRANSFER_PENDING; + ft->direction = FILE_TRANSFER_SEND; return ft; } } @@ -175,7 +224,7 @@ struct FileTransfer *get_new_file_sender(uint32_t friendnum) /* Returns a pointer to an unused file receiver. * Returns NULL if all file receivers are in use. */ -struct FileTransfer *get_new_file_receiver(uint32_t friendnum) +static struct FileTransfer *new_file_receiver(ToxWindow *window, uint32_t friendnum, uint32_t filenum, uint8_t type) { size_t i; @@ -183,7 +232,15 @@ struct FileTransfer *get_new_file_receiver(uint32_t friendnum) struct FileTransfer *ft = &Friends.list[friendnum].file_receiver[i]; if (ft->state == FILE_TRANSFER_INACTIVE) { + memset(ft, 0, sizeof(struct FileTransfer)); + ft->window = window; ft->index = i; + ft->friendnum = friendnum; + ft->filenum = filenum; + ft->file_type = type; + ft->last_keep_alive = get_unix_time(); + ft->state = FILE_TRANSFER_PENDING; + ft->direction = FILE_TRANSFER_RECV; return ft; } } @@ -191,6 +248,22 @@ struct FileTransfer *get_new_file_receiver(uint32_t friendnum) return NULL; } +/* Initializes an unused file transfer and returns its pointer. + * Returns NULL on failure. + */ +struct FileTransfer *new_file_transfer(ToxWindow *window, uint32_t friendnum, uint32_t filenum, + FILE_TRANSFER_DIRECTION direction, uint8_t type) +{ + if (direction == FILE_TRANSFER_RECV) + return new_file_receiver(window, friendnum, filenum, type); + + if (direction == FILE_TRANSFER_SEND) + return new_file_sender(window, friendnum, filenum, type); + + return NULL; +} + + /* Closes file transfer ft. * * Set CTRL to -1 if we don't want to send a control signal. diff --git a/src/file_transfers.h b/src/file_transfers.h index 39b2c30..f5181a6 100644 --- a/src/file_transfers.h +++ b/src/file_transfers.h @@ -38,9 +38,9 @@ typedef enum FILE_TRANSFER_STATE { FILE_TRANSFER_INACTIVE, + FILE_TRANSFER_PAUSED, FILE_TRANSFER_PENDING, FILE_TRANSFER_STARTED, - FILE_TRANSFER_PAUSED, } FILE_TRANSFER_STATE; typedef enum FILE_TRANSFER_DIRECTION { @@ -49,31 +49,36 @@ typedef enum FILE_TRANSFER_DIRECTION { } FILE_TRANSFER_DIRECTION; struct FileTransfer { + ToxWindow *window; FILE *file; FILE_TRANSFER_STATE state; FILE_TRANSFER_DIRECTION direction; uint8_t file_type; char file_name[TOX_MAX_FILENAME_LENGTH + 1]; char file_path[PATH_MAX + 1]; /* Not used by senders */ - double bps; + double bps; uint32_t filenum; uint32_t friendnum; size_t index; uint64_t file_size; uint64_t position; - uint64_t last_progress; + uint64_t last_line_progress; /* The last time we updated the progress bar */ + uint64_t last_keep_alive; /* The last time we sent or received data */ uint32_t line_id; uint8_t file_id[TOX_FILE_ID_LENGTH]; }; +/* Checks for timed out file transfers and closes them. */ +void check_file_transfer_timeouts(Tox *m); + /* creates initial progress line that will be updated during file transfer. progline must be at lesat MAX_STR_SIZE bytes */ -void prep_prog_line(char *progline); +void init_progress_bar(char *progline); /* prints a progress bar for file transfers */ void print_progress_bar(ToxWindow *self, double pct_done, double bps, uint32_t line_id); -/* refreshes active file transfer status bars for friendnum */ +/* refreshes active file transfer status bars. */ void refresh_file_transfer_progress(ToxWindow *self, Tox *m, uint32_t friendnum); /* Returns a pointer to friendnum's FileTransfer struct associated with filenum. @@ -88,15 +93,11 @@ struct FileTransfer *get_file_transfer_struct(uint32_t friendnum, uint32_t filen struct FileTransfer *get_file_transfer_struct_index(uint32_t friendnum, uint32_t index, FILE_TRANSFER_DIRECTION direction); -/* Returns a pointer to an unused file sender. - * Returns NULL if all file senders are in use. +/* Initializes an unused file transfer and returns its pointer. + * Returns NULL on failure. */ -struct FileTransfer *get_new_file_sender(uint32_t friendnum); - -/* Returns a pointer to an unused file receiver. - * Returns NULL if all file receivers are in use. - */ -struct FileTransfer *get_new_file_receiver(uint32_t friendnum); +struct FileTransfer *new_file_transfer(ToxWindow *window, uint32_t friendnum, uint32_t filenum, + FILE_TRANSFER_DIRECTION direction, uint8_t type); /* Closes file transfer ft. * diff --git a/src/toxic.c b/src/toxic.c index 29cc80a..c6d1f55 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -780,6 +780,7 @@ static void do_toxic(Tox *m, ToxWindow *prompt) pthread_mutex_lock(&Winthread.lock); tox_iterate(m); do_bootstrap(m); + check_file_transfer_timeouts(m); pthread_mutex_unlock(&Winthread.lock); } From 92d76c7f99f22f0ab9b4e91c508857c22cfdf62a Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 19 Aug 2015 00:42:28 -0400 Subject: [PATCH 10/13] Display a user warning when log fails to initialize --- src/chat.c | 4 +++- src/global_commands.c | 12 +++++++----- src/groupchat.c | 4 +++- src/log.c | 10 +++++++--- src/log.h | 8 ++++++-- src/prompt.c | 4 +++- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/chat.c b/src/chat.c index 720c86c..2671feb 100644 --- a/src/chat.c +++ b/src/chat.c @@ -1158,11 +1158,13 @@ static void chat_onInit(ToxWindow *self, Tox *m) char myid[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) myid); - log_enable(nick, myid, Friends.list[self->num].pub_key, ctx->log, LOG_CHAT); + int log_ret = log_enable(nick, myid, Friends.list[self->num].pub_key, ctx->log, LOG_CHAT); load_chat_history(self, ctx->log); if (!Friends.list[self->num].logging_on) log_disable(ctx->log); + else if (log_ret == -1) + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); diff --git a/src/global_commands.c b/src/global_commands.c index fc7e3b8..b823b80 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -379,16 +379,18 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX char myid[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) myid); + int log_ret = -1; + if (self->is_chat) { Friends.list[self->num].logging_on = true; - log_enable(self->name, myid, Friends.list[self->num].pub_key, log, LOG_CHAT); + log_ret = log_enable(self->name, myid, Friends.list[self->num].pub_key, log, LOG_CHAT); } else if (self->is_prompt) { - log_enable(self->name, myid, NULL, log, LOG_PROMPT); + log_ret = log_enable(self->name, myid, NULL, log, LOG_PROMPT); } else if (self->is_groupchat) { - log_enable(self->name, myid, NULL, log, LOG_GROUP); + log_ret = log_enable(self->name, myid, NULL, log, LOG_GROUP); } - msg = "Logging enabled"; + msg = log_ret == 0 ? "Logging enabled." : "Warning: Log failed to initialize."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { @@ -397,7 +399,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX log_disable(log); - msg = "Logging disabled"; + msg = "Logging disabled."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } diff --git a/src/groupchat.c b/src/groupchat.c index 9eeac25..9375e2d 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -729,7 +729,9 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) if (user_settings->autolog == AUTOLOG_ON) { char myid[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) myid); - log_enable(self->name, myid, NULL, ctx->log, LOG_GROUP); + + if (log_enable(self->name, myid, NULL, ctx->log, LOG_GROUP) == -1) + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); } execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); diff --git a/src/log.c b/src/log.c index 0924eb1..edd49ea 100644 --- a/src/log.c +++ b/src/log.c @@ -153,15 +153,19 @@ void log_disable(struct chatlog *log) memset(log, 0, sizeof(struct chatlog)); } -void log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype) +int log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype) { log->log_on = true; if (log->file != NULL) - return; + return 0; - if (init_logging_session(name, selfkey, otherkey, log, logtype) == -1) + if (init_logging_session(name, selfkey, otherkey, log, logtype) == -1) { log_disable(log); + return -1; + } + + return 0; } /* Loads previous history from chat log */ diff --git a/src/log.h b/src/log.h index 103aafd..4bfa708 100644 --- a/src/log.h +++ b/src/log.h @@ -39,8 +39,12 @@ enum { /* formats/writes line to log file */ void write_to_log(const char *msg, const char *name, struct chatlog *log, bool event); -/* enables logging for specified log and creates/fetches file if necessary */ -void log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype); +/* enables logging for specified log and creates/fetches file if necessary. + * + * Returns 0 on success. + * Returns -1 on failure. + */ +int log_enable(char *name, const char *selfkey, const char *otherkey, struct chatlog *log, int logtype); /* disables logging for specified log and closes file */ void log_disable(struct chatlog *log); diff --git a/src/prompt.c b/src/prompt.c index 7a189b0..17369de 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -477,7 +477,9 @@ static void prompt_onInit(ToxWindow *self, Tox *m) if (user_settings->autolog == AUTOLOG_ON) { char myid[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) myid); - log_enable(self->name, myid, NULL, ctx->log, LOG_PROMPT); + + if (log_enable(self->name, myid, NULL, ctx->log, LOG_PROMPT) == -1) + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize."); } scrollok(ctx->history, 0); From 11701d22a103e65d58a04d99e4adfda0bd59c773 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 27 Aug 2015 03:38:08 -0400 Subject: [PATCH 11/13] Lots of bug fixes and general code cleanup --- src/autocomplete.c | 14 +++--- src/chat.c | 6 +-- src/dns.c | 18 ++++--- src/file_transfers.c | 2 +- src/friendlist.c | 26 +++++----- src/global_commands.c | 13 +++-- src/groupchat.c | 86 ++++++++++++++++----------------- src/input.c | 6 +-- src/line_info.c | 3 ++ src/log.c | 4 +- src/misc_tools.c | 30 +++++++----- src/misc_tools.h | 10 +++- src/notify.c | 24 +++++---- src/prompt.c | 4 +- src/prompt.h | 1 + src/term_mplex.c | 8 ++- src/toxic.c | 51 ++++++++++++++++---- src/windows.c | 4 +- src/xtra.c | 110 +++++++++++++++++++++--------------------- 19 files changed, 247 insertions(+), 173 deletions(-) diff --git a/src/autocomplete.c b/src/autocomplete.c index 8cf703c..6bc3c6a 100644 --- a/src/autocomplete.c +++ b/src/autocomplete.c @@ -93,7 +93,7 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size) { ChatContext *ctx = self->chatwin; - if (ctx->pos <= 0 || ctx->len <= 0 || ctx->len >= MAX_STR_SIZE || size > MAX_STR_SIZE) + if (ctx->pos <= 0 || ctx->len <= 0 || ctx->pos > ctx->len || ctx->len >= MAX_STR_SIZE || size > MAX_STR_SIZE) return -1; const char *L = (char *) list; @@ -136,7 +136,7 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size) } } - if (string_is_empty(sub)) { + if (!sub[0]) { free(sub); return -1; } @@ -185,7 +185,7 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size) return -1; char tmpend[MAX_STR_SIZE]; - strcpy(tmpend, &ubuf[ctx->pos]); + snprintf(tmpend, sizeof(tmpend), "%s", &ubuf[ctx->pos]); strcpy(&ubuf[strt], match); strcpy(&ubuf[strt + m_len], endchrs); strcpy(&ubuf[strt + m_len + n_endchrs], tmpend); @@ -193,7 +193,7 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size) /* convert to widechar and copy back to original buf */ wchar_t newbuf[MAX_STR_SIZE]; - if (mbs_to_wcs_buf(newbuf, ubuf, MAX_STR_SIZE) == -1) + if (mbs_to_wcs_buf(newbuf, ubuf, sizeof(newbuf) / sizeof(wchar_t)) == -1) return -1; wcscpy(ctx->line, newbuf); @@ -218,7 +218,7 @@ static void complt_home_dir(ToxWindow *self, char *path, int pathsize, const cha wchar_t wline[MAX_STR_SIZE]; - if (mbs_to_wcs_buf(wline, newline, sizeof(wline)) == -1) + if (mbs_to_wcs_buf(wline, newline, sizeof(wline) / sizeof(wchar_t)) == -1) return; int newlen = wcslen(wline); @@ -261,10 +261,10 @@ int dir_match(ToxWindow *self, Tox *m, const wchar_t *line, const wchar_t *cmd) } else if (!si && b_path[0] != '/') { /* look for matches in pwd */ char tmp[MAX_STR_SIZE]; snprintf(tmp, sizeof(tmp), ".%s", b_path); - strcpy(b_path, tmp); + snprintf(b_path, sizeof(b_path), "%s", tmp); } - strcpy(b_name, &b_path[si + 1]); + snprintf(b_name, sizeof(b_name), "%s", &b_path[si + 1]); b_path[si + 1] = '\0'; int b_name_len = strlen(b_name); DIR *dp = opendir(b_path); diff --git a/src/chat.c b/src/chat.c index 2671feb..97ab127 100644 --- a/src/chat.c +++ b/src/chat.c @@ -541,7 +541,7 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_ snprintf(file_path, sizeof(file_path), "%s", filename); } - if (path_len >= sizeof(ft->file_path) || name_length >= sizeof(ft->file_name)) { + if (path_len >= sizeof(file_path) || path_len >= sizeof(ft->file_path) || name_length >= sizeof(ft->file_name)) { tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_CANCEL, NULL); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer faield: File path too long."); return; @@ -937,7 +937,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) if (diff != -1) { if (x + diff > x2 - 1) { - int wlen = wcswidth(ctx->line, sizeof(ctx->line)); + int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t))); ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { @@ -1091,7 +1091,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m) int y, x; getyx(self->window, y, x); (void) x; - int new_x = ctx->start ? x2 - 1 : wcswidth(ctx->line, ctx->pos); + int new_x = ctx->start ? x2 - 1 : MAX(0, wcswidth(ctx->line, ctx->pos)); wmove(self->window, y + 1, new_x); wrefresh(self->window); diff --git a/src/dns.c b/src/dns.c index ade723c..0403f6c 100644 --- a/src/dns.c +++ b/src/dns.c @@ -234,12 +234,16 @@ static int parse_dns_response(ToxWindow *self, u_char *answer, int ans_len, char and the domain in dombuf. return length of username on success, -1 on failure */ -static int parse_addr(const char *addr, char *namebuf, char *dombuf) +static int parse_addr(const char *addr, char *namebuf, size_t namebuf_sz, char *dombuf, size_t dombuf_sz) { - char tmpaddr[MAX_STR_SIZE]; - char *tmpname, *tmpdom; + if (strlen(addr) >= MAX_STR_SIZE) + return -1; - strcpy(tmpaddr, addr); + char tmpaddr[MAX_STR_SIZE]; + char *tmpname = NULL; + char *tmpdom = NULL; + + snprintf(tmpaddr, sizeof(tmpaddr), "%s", addr); tmpname = strtok(tmpaddr, "@"); tmpdom = strtok(NULL, ""); @@ -247,8 +251,8 @@ static int parse_addr(const char *addr, char *namebuf, char *dombuf) return -1; str_to_lower(tmpdom); - strcpy(namebuf, tmpname); - strcpy(dombuf, tmpdom); + snprintf(namebuf, namebuf_sz, "%s", tmpname); + snprintf(dombuf, dombuf_sz, "%s", tmpdom); return strlen(namebuf); } @@ -295,7 +299,7 @@ void *dns3_lookup_thread(void *data) char inputdomain[MAX_STR_SIZE]; char name[MAX_STR_SIZE]; - int namelen = parse_addr(t_data.addr, name, inputdomain); + int namelen = parse_addr(t_data.addr, name, sizeof(name), inputdomain, sizeof(inputdomain)); if (namelen == -1) { dns_error(self, "Must be a Tox ID or an address in the form username@domain"); diff --git a/src/file_transfers.c b/src/file_transfers.c index 7cd915f..64261cc 100644 --- a/src/file_transfers.c +++ b/src/file_transfers.c @@ -285,7 +285,7 @@ void close_file_transfer(ToxWindow *self, Tox *m, struct FileTransfer *ft, int C tox_file_control(m, ft->friendnum, ft->filenum, (TOX_FILE_CONTROL) CTRL, NULL); if (message && self) { - if (self->active_box != -1) + if (self->active_box != -1 && sound_type != silent) box_notify2(self, sound_type, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", message); else box_notify(self, sound_type, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", message); diff --git a/src/friendlist.c b/src/friendlist.c index c8df965..0e0b394 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -139,8 +139,10 @@ static int save_blocklist(char *path) int count = 0; for (i = 0; i < Blocked.max_idx; ++i) { - if (count > Blocked.num_blocked) - goto on_error; + if (count > Blocked.num_blocked) { + free(data); + return -1; + } if (Blocked.list[i].active) { BlockedFriend tmp; @@ -161,19 +163,20 @@ static int save_blocklist(char *path) FILE *fp = fopen(path, "wb"); - if (fp == NULL) - goto on_error; + if (fp == NULL) { + free(data); + return -1; + } - if (fwrite(data, len, 1, fp) != 1) - goto on_error; + if (fwrite(data, len, 1, fp) != 1) { + fclose(fp); + free(data); + return -1; + } fclose(fp); free(data); return 0; - -on_error: - free(data); - return -1; } static void sort_blocklist_index(void); @@ -221,9 +224,10 @@ int load_blocklist(char *path) int i; for (i = 0; i < num; ++i) { + BlockedFriend tmp; + memset(&tmp, 0, sizeof(BlockedFriend)); memset(&Blocked.list[i], 0, sizeof(BlockedFriend)); - BlockedFriend tmp; memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend)); Blocked.list[i].active = true; Blocked.list[i].num = i; diff --git a/src/global_commands.c b/src/global_commands.c index b823b80..170a9ff 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -249,19 +249,22 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv) const char *ip = argv[1]; const char *port = argv[2]; - const char *key = argv[3]; + const char *ascii_key = argv[3]; if (atoi(port) == 0) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid port."); return; } - char *binary_string = hex_string_to_bin(key); + char key_binary[TOX_PUBLIC_KEY_SIZE * 2 + 1]; + if (hex_string_to_bin(ascii_key, strlen(ascii_key), key_binary, TOX_PUBLIC_KEY_SIZE) == -1) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid key."); + return; + } TOX_ERR_BOOTSTRAP err; - tox_bootstrap(m, ip, atoi(port), (uint8_t *) binary_string, &err); - tox_add_tcp_relay(m, ip, atoi(port), (uint8_t *) binary_string, &err); - free(binary_string); + tox_bootstrap(m, ip, atoi(port), (uint8_t *) key_binary, &err); + tox_add_tcp_relay(m, ip, atoi(port), (uint8_t *) key_binary, &err); switch (err) { case TOX_ERR_BOOTSTRAP_BAD_HOST: diff --git a/src/groupchat.c b/src/groupchat.c index 9375e2d..ce568d8 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -593,7 +593,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) if (diff != -1) { if (x + diff > x2 - 1) { - int wlen = wcswidth(ctx->line, sizeof(ctx->line)); + int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t))); ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { @@ -698,7 +698,7 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m) int y, x; getyx(self->window, y, x); (void) x; - int new_x = ctx->start ? x2 - 1 : wcswidth(ctx->line, ctx->pos); + int new_x = ctx->start ? x2 - 1 : MAX(0, wcswidth(ctx->line, ctx->pos)); wmove(self->window, y + 1, new_x); wrefresh(self->window); @@ -795,66 +795,66 @@ static int group_audio_close_out_device(int groupnum) return 0; } -static int group_audio_write(int peernum, int groupnum, const int16_t *pcm, unsigned int samples, uint8_t channels, - unsigned int sample_rate) -{ - if (!pcm) - return -1; +// static int group_audio_write(int peernum, int groupnum, const int16_t *pcm, unsigned int samples, uint8_t channels, +// unsigned int sample_rate) +// { +// if (!pcm) +// return -1; - if (channels == 0 || channels > 2) - return -2; +// if (channels == 0 || channels > 2) +// return -2; - ALuint bufid; - ALint processed = 0, queued = 0; +// ALuint bufid; +// ALint processed = 0, queued = 0; - alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_PROCESSED, &processed); - alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_QUEUED, &queued); - fprintf(stderr, "source: %d, queued: %d, processed: %d\n", groupchats[groupnum].audio.source, queued, processed); +// alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_PROCESSED, &processed); +// alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_QUEUED, &queued); +// fprintf(stderr, "source: %d, queued: %d, processed: %d\n", groupchats[groupnum].audio.source, queued, processed); - if (processed) { - ALuint bufids[processed]; - alSourceUnqueueBuffers(groupchats[groupnum].audio.source, processed, bufids); - alDeleteBuffers(processed - 1, bufids + 1); - bufid = bufids[0]; - } else if (queued < 16) { - alGenBuffers(1, &bufid); - } else { - return -3; - } +// if (processed) { +// ALuint bufids[processed]; +// alSourceUnqueueBuffers(groupchats[groupnum].audio.source, processed, bufids); +// alDeleteBuffers(processed - 1, bufids + 1); +// bufid = bufids[0]; +// } else if (queued < 16) { +// alGenBuffers(1, &bufid); +// } else { +// return -3; +// } - int length = samples * channels * sizeof(int16_t); +// int length = samples * channels * sizeof(int16_t); - alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, length, sample_rate); - alSourceQueueBuffers(groupchats[groupnum].audio.source, 1, &bufid); +// alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, length, sample_rate); +// alSourceQueueBuffers(groupchats[groupnum].audio.source, 1, &bufid); - ALint state; - alGetSourcei(groupchats[groupnum].audio.source, AL_SOURCE_STATE, &state); +// ALint state; +// alGetSourcei(groupchats[groupnum].audio.source, AL_SOURCE_STATE, &state); - if (state != AL_PLAYING) - alSourcePlay(groupchats[groupnum].audio.source); +// if (state != AL_PLAYING) +// alSourcePlay(groupchats[groupnum].audio.source); - return 0; -} +// return 0; +// } static void groupchat_onWriteDevice(ToxWindow *self, Tox *m, int groupnum, int peernum, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate) { return; - if (groupnum != self->num) - return; + // if (groupnum != self->num) + // return; - if (peernum < 0) - return; + // if (peernum < 0) + // return; - if (groupchats[groupnum].audio.dvhandle == NULL) - fprintf(stderr, "dvhandle is null)\n"); + // if (groupchats[groupnum].audio.dvhandle == NULL) + // fprintf(stderr, "dvhandle is null)\n"); - if (groupchats[groupnum].audio.dvctx == NULL) - fprintf(stderr, "ctx is null\n"); + // if (groupchats[groupnum].audio.dvctx == NULL) + // fprintf(stderr, "ctx is null\n"); - int ret = group_audio_write(peernum, groupnum, pcm, samples, channels, sample_rate); - fprintf(stderr, "write: %d\n", ret); + // int ret = group_audio_write(peernum, groupnum, pcm, samples, channels, sample_rate); + // fprintf(stderr, "write: %d\n", ret); } #endif /* AUDIO */ diff --git a/src/input.c b/src/input.c index 86f94f1..4186c4d 100644 --- a/src/input.c +++ b/src/input.c @@ -125,7 +125,7 @@ static void input_yank(ToxWindow *self, int x, int mx_x) if (x + yank_cols >= mx_x) { int rmdr = MAX(0, (x + yank_cols) - mx_x); - int s_len = wcswidth(&ctx->line[ctx->start], rmdr); + int s_len = MAX(0, wcswidth(&ctx->line[ctx->start], rmdr)); ctx->start += s_len + 1; } } @@ -137,7 +137,7 @@ static void input_mv_end(ToxWindow *self, int y, int mx_x) ctx->pos = ctx->len; - int wlen = wcswidth(ctx->line, sizeof(ctx->line)); + int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t))); ctx->start = MAX(0, 1 + (mx_x * (wlen / mx_x) - mx_x) + (wlen % mx_x)); } @@ -196,7 +196,7 @@ static void input_history(ToxWindow *self, wint_t key, int mx_x) ChatContext *ctx = self->chatwin; fetch_hist_item(ctx, key); - int wlen = wcswidth(ctx->line, sizeof(ctx->line)); + int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t))); ctx->start = wlen < mx_x ? 0 : wlen - mx_x + 1; } diff --git a/src/line_info.c b/src/line_info.c index 2283908..6a84282 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -133,6 +133,9 @@ static struct line_info *line_info_ret_queue(struct history *hst) void line_info_add(ToxWindow *self, const char *timestr, const char *name1, const char *name2, uint8_t type, uint8_t bold, uint8_t colour, const char *msg, ...) { + if (!self) + return; + struct history *hst = self->chatwin->hst; if (hst->queue_sz >= MAX_LINE_INFO_QUEUE) diff --git a/src/log.c b/src/log.c index edd49ea..2d48dbc 100644 --- a/src/log.c +++ b/src/log.c @@ -179,7 +179,7 @@ void load_chat_history(ToxWindow *self, struct chatlog *log) if (sz <= 0) return; - char *hstbuf = malloc(sz); + char *hstbuf = malloc(sz + 1); if (hstbuf == NULL) exit_toxic_err("failed in load_chat_history", FATALERR_MEMORY); @@ -196,6 +196,8 @@ void load_chat_history(ToxWindow *self, struct chatlog *log) return; } + hstbuf[sz] = '\0'; + /* Number of history lines to load: must not be larger than MAX_LINE_INFO_QUEUE - 2 */ int L = MIN(MAX_LINE_INFO_QUEUE - 2, user_settings->history_size); int start, count = 0; diff --git a/src/misc_tools.c b/src/misc_tools.c index 3734aa2..bcf422f 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -109,22 +109,27 @@ void get_elapsed_time_str(char *buf, int bufsize, uint64_t secs) snprintf(buf, bufsize, "%ld:%.2ld:%.2ld", hours, minutes, seconds); } -char *hex_string_to_bin(const char *hex_string) +/* + * Converts a hexidecimal string of length hex_len to binary format and puts the result in output. + * output_size must be exactly half of hex_len. + * + * Returns 0 on success. + * Returns -1 on failure. + */ +int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size_t output_size) { - size_t len = strlen(hex_string); - char *val = malloc(len); + if (output_size == 0 || hex_len != output_size * 2) + return -1; - if (val == NULL) - exit_toxic_err("failed in hex_string_to_bin", FATALERR_MEMORY); + for (size_t i = 0; i < output_size; ++i) { + sscanf(hex_string, "%2hhx", &output[i]); + hex_string += 2; + } - size_t i; - - for (i = 0; i < len; ++i, hex_string += 2) - sscanf(hex_string, "%2hhx", &val[i]); - - return val; + return 0; } + int hex_string_to_bytes(char *buf, int size, const char *keystr) { if (size % 2 != 0) @@ -147,6 +152,9 @@ int hex_string_to_bytes(char *buf, int size, const char *keystr) /* Returns 1 if the string is empty, 0 otherwise */ int string_is_empty(const char *string) { + if (!string) + return true; + return string[0] == '\0'; } diff --git a/src/misc_tools.h b/src/misc_tools.h index 325411c..e64b896 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -41,8 +41,14 @@ void hst_to_net(uint8_t *num, uint16_t numbytes); -/* convert a hex string to binary */ -char *hex_string_to_bin(const char *hex_string); +/* + * Converts a hexidecimal string of length hex_len to binary format and puts the result in output. + * output_size must be exactly half of hex_len. + * + * Returns 0 on success. + * Returns -1 on failure. + */ +int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size_t output_size); /* convert a hex string to bytes. returns 0 on success, -1 on failure */ int hex_string_to_bytes(char *buf, int size, const char *keystr); diff --git a/src/notify.c b/src/notify.c index 8560659..35535cf 100644 --- a/src/notify.c +++ b/src/notify.c @@ -90,7 +90,7 @@ struct _ActiveNotifications { #ifdef BOX_NOTIFY NotifyNotification* box; char messages[MAX_BOX_MSG_LEN + 1][MAX_BOX_MSG_LEN + 1]; - char title[24]; + char title[64]; size_t size; time_t n_timeout; #endif @@ -153,25 +153,26 @@ bool is_playing(int source) static bool device_opened = false; time_t last_opened_update = 0; -bool m_open_device() +/* Opens primary device. Returns true on succe*/ +void m_open_device() { last_opened_update = get_unix_time(); - if (device_opened) return true; + if (device_opened) return; /* Blah error check */ open_primary_device(output, &Control.device_idx, 48000, 20, 1); - return (device_opened = true); + device_opened = true; } -bool m_close_device() +void m_close_device() { - if (!device_opened) return true; + if (!device_opened) return; close_device(output, Control.device_idx); - return !(device_opened = false); + device_opened = false; } /* Terminate all sounds but wait for them to finish first */ @@ -598,9 +599,12 @@ int box_notify(ToxWindow* self, Notification notif, uint64_t flags, int* id_indi actives[id].id_indicator = id_indicator; if (id_indicator) *id_indicator = id; } -#endif +#else + if (id == -1) + return -1; +#endif /* SOUND_NOTIFY */ - strncpy(actives[id].title, title, 24); + snprintf(actives[id].title, sizeof(actives[id].title), "%s", title); if (strlen(title) > 23) strcpy(actives[id].title + 20, "..."); va_list __ARGS__; va_start (__ARGS__, format); @@ -699,7 +703,7 @@ int box_silent_notify(ToxWindow* self, uint64_t flags, int* id_indicator, const *id_indicator = id; } - strncpy(actives[id].title, title, 24); + snprintf(actives[id].title, sizeof(actives[id].title), "%s", title); if (strlen(title) > 23) strcpy(actives[id].title + 20, "..."); va_list __ARGS__; va_start (__ARGS__, format); diff --git a/src/prompt.c b/src/prompt.c index 17369de..c032711 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -212,7 +212,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) if (diff != -1) { if (x + diff > x2 - 1) { - int wlen = wcswidth(ctx->line, sizeof(ctx->line)); + int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t))); ctx->start = wlen < x2 ? 0 : wlen - x2 + 1; } } else { @@ -330,7 +330,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) getyx(self->window, y, x); (void) x; - int new_x = ctx->start ? x2 - 1 : wcswidth(ctx->line, ctx->pos); + int new_x = ctx->start ? x2 - 1 : MAX(0, wcswidth(ctx->line, ctx->pos)); wmove(self->window, y + 1, new_x); wrefresh(self->window); diff --git a/src/prompt.h b/src/prompt.h index 4842f6c..1917375 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -41,6 +41,7 @@ typedef struct { } FriendRequests; ToxWindow new_prompt(void); + void prep_prompt_win(void); void prompt_init_statusbar(ToxWindow *self, Tox *m); void prompt_update_nick(ToxWindow *prompt, const char *nick); diff --git a/src/term_mplex.c b/src/term_mplex.c index 130e868..35bf082 100644 --- a/src/term_mplex.c +++ b/src/term_mplex.c @@ -167,6 +167,10 @@ static int detect_gnu_screen () free (dyn_buffer); dyn_buffer = NULL; + + if (strlen(socket_path) + strlen(PATH_SEP_S) + strlen(socket_name) >= sizeof(mplex_data)) + goto nomplex; + strcpy (mplex_data, socket_path); strcat (mplex_data, PATH_SEP_S); strcat (mplex_data, socket_name); @@ -181,6 +185,8 @@ nomplex: pclose (session_info_stream); if (dyn_buffer) free (dyn_buffer); + if (socket_path) + free(socket_path); return 0; } @@ -196,7 +202,7 @@ static int detect_tmux () return 0; /* store the session number string for later use */ - strcpy (mplex_data, pos + 1); + snprintf (mplex_data, sizeof(mplex_data), "%s", pos + 1); mplex = MPLEX_TMUX; return 1; } diff --git a/src/toxic.c b/src/toxic.c index c6d1f55..50eb4e3 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -77,6 +77,9 @@ char *DATA_FILE = NULL; char *BLOCK_FILE = NULL; ToxWindow *prompt = NULL; +#define DATANAME "data" +#define BLOCKNAME "data-blocklist" + #define AUTOSAVE_FREQ 60 #define MIN_PASSWORD_LEN 6 #define MAX_PASSWORD_LEN 64 @@ -119,6 +122,24 @@ static void init_signal_catchers(void) signal(SIGSEGV, catch_SIGSEGV); } +void free_global_data(void) +{ + if (DATA_FILE) { + free(DATA_FILE); + DATA_FILE = NULL; + } + + if (BLOCK_FILE) { + free(BLOCK_FILE); + BLOCK_FILE = NULL; + } + + if (user_settings) { + free(user_settings); + user_settings = NULL; + } +} + void exit_toxic_success(Tox *m) { store_data(m, DATA_FILE); @@ -131,10 +152,7 @@ void exit_toxic_success(Tox *m) terminate_audio(); #endif /* AUDIO */ - free(DATA_FILE); - free(BLOCK_FILE); - free(user_settings); - + free_global_data(); tox_kill(m); endwin(); @@ -150,6 +168,7 @@ void exit_toxic_success(Tox *m) void exit_toxic_err(const char *errmsg, int errcode) { + free_global_data(); freopen("/dev/tty", "w", stderr); endwin(); fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, errmsg); @@ -289,9 +308,11 @@ static int load_nodelist(const char *filename) toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0; toxNodes.ports[toxNodes.lines] = atoi(port); - char *key_binary = hex_string_to_bin(key_ascii); + char key_binary[TOX_PUBLIC_KEY_SIZE + 2 + 1]; + if (hex_string_to_bin(key_ascii, strlen(key_ascii), key_binary, TOX_PUBLIC_KEY_SIZE) == -1) + continue; + memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_PUBLIC_KEY_SIZE); - free(key_binary); toxNodes.lines++; } @@ -933,10 +954,22 @@ static void parse_args(int argc, char *argv[]) case 'f': arg_opts.use_custom_data = 1; - DATA_FILE = strdup(optarg); + + if (DATA_FILE) + free(DATA_FILE); + + if (BLOCK_FILE) + free(BLOCK_FILE); + + DATA_FILE = malloc(strlen(optarg) + 1); + strcpy(DATA_FILE, optarg); + + if (DATA_FILE == NULL) + exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); - if (DATA_FILE == NULL || BLOCK_FILE == NULL) + if (BLOCK_FILE == NULL) exit_toxic_err("failed in parse_args", FATALERR_MEMORY); strcpy(BLOCK_FILE, optarg); @@ -1007,8 +1040,6 @@ static void parse_args(int argc, char *argv[]) } } -#define DATANAME "data" -#define BLOCKNAME "data-blocklist" static int init_default_data_files(void) { if (arg_opts.use_custom_data) diff --git a/src/windows.c b/src/windows.c index 142bebc..6324b38 100644 --- a/src/windows.c +++ b/src/windows.c @@ -568,7 +568,7 @@ ToxWindow *get_window_ptr(int i) { ToxWindow *toxwin = NULL; - if (i >= 0 && i <= MAX_WINDOWS_NUM && windows[i].active) + if (i >= 0 && i < MAX_WINDOWS_NUM && windows[i].active) toxwin = &windows[i]; return toxwin; @@ -595,7 +595,7 @@ void kill_all_windows(Tox *m) if (windows[i].is_chat) kill_chat_window(&windows[i], m); else if (windows[i].is_groupchat) - close_groupchat(&windows[i], m, i); + close_groupchat(&windows[i], m, windows[i].num); } kill_prompt_window(prompt); diff --git a/src/xtra.c b/src/xtra.c index 51eeb86..05004c3 100644 --- a/src/xtra.c +++ b/src/xtra.c @@ -72,24 +72,24 @@ Property read_property(Window s, Atom p) unsigned long read_num; unsigned long left_bytes; unsigned char *data = NULL; - + int read_bytes = 1024; - + /* Keep trying to read the property until there are no bytes unread */ do { if (data) XFree(data); - - XGetWindowProperty(Xtra.display, s, - p, 0, - read_bytes, + + XGetWindowProperty(Xtra.display, s, + p, 0, + read_bytes, False, AnyPropertyType, - &read_type, &read_format, - &read_num, &left_bytes, + &read_type, &read_format, + &read_num, &left_bytes, &data); - + read_bytes *= 2; } while (left_bytes != 0); - + Property property = {data, read_format, read_num, read_type}; return property; } @@ -107,7 +107,7 @@ Atom get_dnd_type(long *a, int l) static void handle_xdnd_enter(XClientMessageEvent* e) { Xtra.handling_version = (e->data.l[1] >> 24); - + if ((e->data.l[1] & 1)) { // Fetch the list of possible conversions Property p = read_property(e->data.l[0], XdndTypeList); @@ -120,7 +120,7 @@ static void handle_xdnd_enter(XClientMessageEvent* e) } static void handle_xdnd_position(XClientMessageEvent* e) -{ +{ XEvent ev = { .xclient = { .type = ClientMessage, @@ -130,15 +130,15 @@ static void handle_xdnd_position(XClientMessageEvent* e) .format = 32, .data = { .l = { - Xtra.proxy_window, - (Xtra.expecting_type != XtraNil), - 0, 0, + Xtra.proxy_window, + (Xtra.expecting_type != XtraNil), + 0, 0, XdndActionCopy } } } }; - + XSendEvent(Xtra.display, e->data.l[0], False, NoEventMask, &ev); XFlush(Xtra.display); } @@ -159,7 +159,7 @@ static void handle_xdnd_drop(XClientMessageEvent* e) } } }; - + XSendEvent(Xtra.display, e->data.l[0], False, NoEventMask, &ev); } else { Xtra.source_window = e->data.l[0]; @@ -188,57 +188,57 @@ static void handle_xdnd_selection(XSelectionEvent* e) } }; XSendEvent(Xtra.display, Xtra.source_window, False, NoEventMask, &ev); - + Property p = read_property(Xtra.proxy_window, XdndSelection); DropType dt; - + if (strcmp(XGetAtomName(Xtra.display, p.read_type), "text/uri-list") == 0) dt = DT_file_list; else /* text/uri-list */ dt = DT_plain; - - + + /* Call callback for every entry */ if (Xtra.on_drop && p.read_num) { char *sptr; char *str = strtok_r((char *) p.data, "\n\r", &sptr); - + if (str) Xtra.on_drop(str, dt); while ((str = strtok_r(NULL, "\n\r", &sptr))) Xtra.on_drop(str, dt); } - + if (p.data) XFree(p.data); } void *event_loop(void* p) { /* Handle events like a real nigga */ - + (void) p; /* DINDUNOTHIN */ - + XEvent event; int pending; - + while (Xtra.display) { /* NEEDMOEVENTSFODEMPROGRAMS */ - + XLockDisplay(Xtra.display); if((pending = XPending(Xtra.display))) XNextEvent(Xtra.display, &event); - + if (!pending) { XUnlockDisplay(Xtra.display); usleep(10000); continue; } - + if (event.type == ClientMessage) { Atom type = event.xclient.message_type; - + if (type == XdndEnter) handle_xdnd_enter(&event.xclient); else if (type == XdndPosition) handle_xdnd_position(&event.xclient); else if (type == XdndDrop) handle_xdnd_drop(&event.xclient); @@ -247,11 +247,11 @@ void *event_loop(void* p) else if (event.type == SelectionNotify) handle_xdnd_selection(&event.xselection); /* AINNOBODYCANHANDLEDEMEVENTS*/ else XSendEvent(Xtra.display, Xtra.terminal_window, 0, 0, &event); - + XUnlockDisplay(Xtra.display); } - - /* Actual XTRA termination + + /* Actual XTRA termination * Please call xtra_terminate() at exit * otherwise HEWUSAGUDBOI happens */ @@ -262,23 +262,23 @@ void *event_loop(void* p) int init_xtra(drop_callback d) { memset(&Xtra, 0, sizeof(Xtra)); - + if (!d) return -1; else Xtra.on_drop = d; - + XInitThreads(); if ( !(Xtra.display = XOpenDisplay(NULL))) return -1; - + Xtra.terminal_window = focused_window_id(); - + { /* Create an invisible window which will act as proxy for the DnD operation. */ XSetWindowAttributes attr = {0}; - attr.event_mask = EnterWindowMask | - LeaveWindowMask | - ButtonMotionMask | - ButtonPressMask | - ButtonReleaseMask | + attr.event_mask = EnterWindowMask | + LeaveWindowMask | + ButtonMotionMask | + ButtonPressMask | + ButtonReleaseMask | ResizeRedirectMask; attr.do_not_propagate_mask = NoEventMask; @@ -286,14 +286,14 @@ int init_xtra(drop_callback d) Window root; int x, y; unsigned int wht, hht, b, d; - + /* Since we cannot capture resize events for parent window we will have to create - * this window to have maximum size as defined in root window + * this window to have maximum size as defined in root window */ XGetGeometry(Xtra.display, XDefaultRootWindow(Xtra.display), &root, &x, &y, &wht, &hht, &b, &d); - + if (! (Xtra.proxy_window = XCreateWindow (Xtra.display, Xtra.terminal_window, /* Parent */ 0, 0, /* Position */ @@ -306,7 +306,7 @@ int init_xtra(drop_callback d) &attr)) ) /* Attributes for value mask */ return -1; } - + XMapWindow(Xtra.display, Xtra.proxy_window); /* Show window (sandwich) */ XLowerWindow(Xtra.display, Xtra.proxy_window); /* Don't interfere with parent lmao */ @@ -321,7 +321,7 @@ int init_xtra(drop_callback d) XdndTypeList = XInternAtom(Xtra.display, "XdndTypeList", False); XdndActionCopy = XInternAtom(Xtra.display, "XdndActionCopy", False); XdndFinished = XInternAtom(Xtra.display, "XdndFinished", False); - + /* Inform my nigga windows that we are aware of dnd */ Atom XdndVersion = 3; XChangeProperty(Xtra.display, @@ -331,18 +331,20 @@ int init_xtra(drop_callback d) 32, PropModeReplace, (unsigned char*)&XdndVersion, 1); - + pthread_t id; - pthread_create(&id, NULL, event_loop, NULL); + if (pthread_create(&id, NULL, event_loop, NULL) != 0) + return -1; + pthread_detach(id); - + return 0; } void terminate_xtra() { if (!Xtra.display) return; - + XEvent terminate = { .xclient = { .type = ClientMessage, @@ -350,19 +352,19 @@ void terminate_xtra() .message_type = XtraTerminate, } }; - + XLockDisplay(Xtra.display); XDeleteProperty(Xtra.display, Xtra.proxy_window, XdndAware); XSendEvent(Xtra.display, Xtra.proxy_window, 0, NoEventMask, &terminate); XUnlockDisplay(Xtra.display); - + while (Xtra.display); /* Wait for termination */ } long unsigned int focused_window_id() { if (!Xtra.display) return 0; - + Window focus; int revert; XLockDisplay(Xtra.display); From 28dd43608d7a595d7a349dc83d3dea3bf538c55e Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 27 Aug 2015 15:13:13 -0400 Subject: [PATCH 12/13] Fix a few notify/threading bugs and fix DHTnodes loading bug --- src/misc_tools.c | 3 +-- src/notify.c | 53 ++++++++++++++++++++++++++++++++++++------------ src/toxic.c | 20 ++++++++++++++---- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/misc_tools.c b/src/misc_tools.c index bcf422f..54b3811 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -129,7 +129,6 @@ int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size return 0; } - int hex_string_to_bytes(char *buf, int size, const char *keystr) { if (size % 2 != 0) @@ -225,7 +224,7 @@ void filter_str(char *str, size_t len) size_t i; for (i = 0; i < len; ++i) { - if (str[i] == '\n' || str[i] == '\r' || str[i] == '\t' || str[i] == '\v') + if (str[i] == '\n' || str[i] == '\r' || str[i] == '\t' || str[i] == '\v' || str[i] == '\0') str[i] = ' '; } } diff --git a/src/notify.c b/src/notify.c index 35535cf..dc58e94 100644 --- a/src/notify.c +++ b/src/notify.c @@ -117,9 +117,12 @@ static void tab_notify(ToxWindow *self, uint64_t flags) static bool notifications_are_disabled(uint64_t flags) { - bool res = flags & NT_RESTOL && Control.cooldown > get_unix_time(); + if (user_settings->alerts != ALERTS_ENABLED) + return true; + + bool res = (flags & NT_RESTOL) && (Control.cooldown > get_unix_time()); #ifdef X11 - return res || (flags & NT_NOFOCUS && is_focused()); + return res || ((flags & NT_NOFOCUS) && is_focused()); #else return res; #endif @@ -178,9 +181,11 @@ void m_close_device() /* Terminate all sounds but wait for them to finish first */ void graceful_clear() { - int i; control_lock(); + while (1) { + int i; + for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) { if (actives[i].active) { #ifdef BOX_NOTIFY @@ -212,18 +217,24 @@ void graceful_clear() usleep(1000); } + + control_unlock(); } void* do_playing(void* _p) { (void)_p; - int i; - bool has_looping = false; - - while(Control.poll_active) { + while(true) { control_lock(); + if (!Control.poll_active) { + control_unlock(); + break; + } + + bool has_looping = false; + int i; for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) { @@ -300,9 +311,17 @@ int play_source(uint32_t source, uint32_t buffer, bool looping) void* do_playing(void* _p) { (void)_p; - int i; - while(Control.poll_active) { + + while(true) { control_lock(); + + if (!Control.poll_active) { + control_unlock(); + break; + } + + int i; + for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) { if (actives[i].box && get_unix_time() >= actives[i].n_timeout) { @@ -362,14 +381,15 @@ int init_notify(int login_cooldown, int notification_timeout) if (pthread_mutex_init(Control.poll_mutex, NULL) != 0) return -1; + Control.poll_active = 1; pthread_t thread; if (pthread_create(&thread, NULL, do_playing, NULL) != 0 || pthread_detach(thread) != 0 ) { pthread_mutex_destroy(Control.poll_mutex); + Control.poll_active = 0; return -1; } - Control.poll_active = 1; #endif Control.cooldown = get_unix_time() + login_cooldown; @@ -384,8 +404,15 @@ int init_notify(int login_cooldown, int notification_timeout) void terminate_notify() { #if defined(SOUND_NOTIFY) || defined(BOX_NOTIFY) - if ( !Control.poll_active ) return; + control_lock(); + + if ( !Control.poll_active ) { + control_unlock(); + return; + } + Control.poll_active = 0; + control_unlock(); graceful_clear(); #endif @@ -504,7 +531,7 @@ int sound_notify(ToxWindow* self, Notification notif, uint64_t flags, int* id_in int id = -1; control_lock(); - if (self && (!self->stb || self->stb->status != TOX_USER_STATUS_BUSY) && user_settings->alerts == ALERTS_ENABLED) + if (self && (!self->stb || self->stb->status != TOX_USER_STATUS_BUSY)) id = m_play_sound(notif, flags); else if (flags & NT_ALWAYS) id = m_play_sound(notif, flags); @@ -627,7 +654,7 @@ int box_notify(ToxWindow* self, Notification notif, uint64_t flags, int* id_indi return id; #else return sound_notify(self, notif, flags, id_indicator); -#endif +#endif /* BOX_NOTIFY */ } int box_notify2(ToxWindow* self, Notification notif, uint64_t flags, int id, const char* format, ...) diff --git a/src/toxic.c b/src/toxic.c index 50eb4e3..4e938b8 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -295,21 +295,33 @@ static int load_nodelist(const char *filename) char line[MAX_NODE_LINE]; while (fgets(line, sizeof(line), fp) && toxNodes.lines < MAXNODES) { - if (strlen(line) > MIN_NODE_LINE) { + size_t line_len = strlen(line); + + if (line_len >= MIN_NODE_LINE && line_len <= MAX_NODE_LINE) { const char *name = strtok(line, " "); const char *port = strtok(NULL, " "); const char *key_ascii = strtok(NULL, " "); - /* invalid line */ if (name == NULL || port == NULL || key_ascii == NULL) continue; + size_t key_len = strlen(key_ascii); + size_t name_len = strlen(name); + + if (key_len < TOX_PUBLIC_KEY_SIZE * 2 || name_len >= NODELEN) + continue; + snprintf(toxNodes.nodes[toxNodes.lines], sizeof(toxNodes.nodes[toxNodes.lines]), "%s", name); toxNodes.nodes[toxNodes.lines][NODELEN - 1] = 0; toxNodes.ports[toxNodes.lines] = atoi(port); - char key_binary[TOX_PUBLIC_KEY_SIZE + 2 + 1]; - if (hex_string_to_bin(key_ascii, strlen(key_ascii), key_binary, TOX_PUBLIC_KEY_SIZE) == -1) + /* remove possible trailing newline from key string */ + char key_binary[TOX_PUBLIC_KEY_SIZE * 2 + 1]; + memcpy(key_binary, key_ascii, TOX_PUBLIC_KEY_SIZE * 2); + key_len = TOX_PUBLIC_KEY_SIZE * 2; + key_binary[key_len] = '\0'; + + if (hex_string_to_bin(key_ascii, key_len, key_binary, TOX_PUBLIC_KEY_SIZE) == -1) continue; memcpy(toxNodes.keys[toxNodes.lines], key_binary, TOX_PUBLIC_KEY_SIZE); From b4464eda4d3db3959334a8e6fb5ef80ffb7cf866 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 27 Aug 2015 21:29:34 -0400 Subject: [PATCH 13/13] Fix more threading issues --- src/device.c | 162 +++++++++++++++++++++++++---------------------- src/friendlist.c | 55 +++++++++++++--- src/misc_tools.c | 1 + src/misc_tools.h | 6 +- src/notify.c | 8 +-- src/prompt.c | 24 +++++-- src/toxic.c | 26 +++++--- src/windows.c | 9 +++ 8 files changed, 187 insertions(+), 104 deletions(-) diff --git a/src/device.c b/src/device.c index 4a6a8a5..9ef26de 100644 --- a/src/device.c +++ b/src/device.c @@ -58,14 +58,14 @@ typedef struct Device { DataHandleCallback cb; /* Use this to handle data from input device usually */ void* cb_data; /* Data to be passed to callback */ int32_t call_idx; /* ToxAv call index */ - + uint32_t source, buffers[OPENAL_BUFS]; /* Playback source/buffers */ uint32_t ref_count; int32_t selection; bool enable_VAD; bool muted; pthread_mutex_t mutex[1]; - uint32_t sample_rate; + uint32_t sample_rate; uint32_t frame_duration; int32_t sound_mode; #ifdef AUDIO @@ -89,7 +89,7 @@ static ToxAv* av = NULL; pthread_mutex_t mutex; -bool thread_running = true, +bool thread_running = true, thread_paused = true; /* Thread control */ void* thread_poll(void*); @@ -105,19 +105,19 @@ DeviceError init_devices() size[input] = 0; if ( (stringed_device_list = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER)) ) { ddevice_names[input] = alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER); - + for ( ; *stringed_device_list && size[input] < MAX_DEVICES; ++size[input] ) { - devices_names[input][size[input]] = stringed_device_list; + devices_names[input][size[input]] = stringed_device_list; stringed_device_list += strlen( stringed_device_list ) + 1; } } - + size[output] = 0; if ( (stringed_device_list = alcGetString(NULL, ALC_DEVICE_SPECIFIER)) ) { ddevice_names[output] = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); - + for ( ; *stringed_device_list && size[output] < MAX_DEVICES; ++size[output] ) { - devices_names[output][size[output]] = stringed_device_list; + devices_names[output][size[output]] = stringed_device_list; stringed_device_list += strlen( stringed_device_list ) + 1; } } @@ -125,27 +125,30 @@ DeviceError init_devices() // Start poll thread if (pthread_mutex_init(&mutex, NULL) != 0) return de_InternalError; - + pthread_t thread_id; - if ( pthread_create(&thread_id, NULL, thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) - return de_InternalError; - + if ( pthread_create(&thread_id, NULL, thread_poll, NULL) != 0 || pthread_detach(thread_id) != 0) + return de_InternalError; + #ifdef AUDIO av = av_; #endif /* AUDIO */ - + return (DeviceError) de_None; } DeviceError terminate_devices() { /* Cleanup if needed */ + lock; thread_running = false; + unlock; + usleep(20000); - + if (pthread_mutex_destroy(&mutex) != 0) return (DeviceError) de_InternalError; - + return (DeviceError) de_None; } @@ -153,16 +156,16 @@ DeviceError device_mute(DeviceType type, uint32_t device_idx) { if (device_idx >= MAX_DEVICES) return de_InvalidSelection; lock; - + Device* device = running[type][device_idx]; - - if (!device) { + + if (!device) { unlock; return de_DeviceNotActive; } - + device->muted = !device->muted; - + unlock; return de_None; } @@ -175,7 +178,7 @@ DeviceError device_set_VAD_treshold(uint32_t device_idx, float value) Device* device = running[input][device_idx]; - if (!device) { + if (!device) { unlock; return de_DeviceNotActive; } @@ -192,7 +195,7 @@ DeviceError set_primary_device(DeviceType type, int32_t selection) { if (size[type] <= selection || selection < 0) return de_InvalidSelection; primary_device[type] = selection; - + return de_None; } @@ -212,88 +215,88 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx if (size[type] <= selection || selection < 0) return de_InvalidSelection; if (channels != 1 && channels != 2) return de_UnsupportedMode; - + lock; const uint32_t frame_size = (sample_rate * frame_duration / 1000); - + uint32_t i; for (i = 0; i < MAX_DEVICES && running[type][i] != NULL; ++i); - + if (i == MAX_DEVICES) { unlock; return de_AllDevicesBusy; } else *device_idx = i; - + for (i = 0; i < MAX_DEVICES; i ++) { /* Check if any device has the same selection */ if ( running[type][i] && running[type][i]->selection == selection ) { // printf("a%d-%d:%p ", selection, i, running[type][i]->dhndl); - - running[type][*device_idx] = running[type][i]; + + running[type][*device_idx] = running[type][i]; running[type][i]->ref_count ++; - + unlock; return de_None; } } - + Device* device = running[type][*device_idx] = calloc(1, sizeof(Device)); device->selection = selection; - + device->sample_rate = sample_rate; device->frame_duration = frame_duration; device->sound_mode = channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16; - + if (pthread_mutex_init(device->mutex, NULL) != 0) { free(device); unlock; return de_InternalError; } - + if (type == input) { - device->dhndl = alcCaptureOpenDevice(devices_names[type][selection], + device->dhndl = alcCaptureOpenDevice(devices_names[type][selection], sample_rate, device->sound_mode, frame_size * 2); #ifdef AUDIO device->VAD_treshold = user_settings->VAD_treshold; #endif } - else { + else { device->dhndl = alcOpenDevice(devices_names[type][selection]); - if ( !device->dhndl ) { + if ( !device->dhndl ) { free(device); running[type][*device_idx] = NULL; unlock; return de_FailedStart; } - + device->ctx = alcCreateContext(device->dhndl, NULL); alcMakeContextCurrent(device->ctx); - + alGenBuffers(OPENAL_BUFS, device->buffers); alGenSources((uint32_t)1, &device->source); alSourcei(device->source, AL_LOOPING, AL_FALSE); - + uint16_t zeros[frame_size]; memset(zeros, 0, frame_size*2); - + for ( i = 0; i < OPENAL_BUFS; ++i ) { alBufferData(device->buffers[i], device->sound_mode, zeros, frame_size*2, sample_rate); } - + alSourceQueueBuffers(device->source, OPENAL_BUFS, device->buffers); alSourcePlay(device->source); } - + if (alcGetError(device->dhndl) != AL_NO_ERROR) { free(device); running[type][*device_idx] = NULL; unlock; return de_FailedStart; } - + if (type == input) { alcCaptureStart(device->dhndl); thread_paused = false; } - + unlock; return de_None; } @@ -301,47 +304,47 @@ DeviceError open_device(DeviceType type, int32_t selection, uint32_t* device_idx DeviceError close_device(DeviceType type, uint32_t device_idx) { if (device_idx >= MAX_DEVICES) return de_InvalidSelection; - + lock; Device* device = running[type][device_idx]; DeviceError rc = de_None; - - if (!device) { + + if (!device) { unlock; return de_DeviceNotActive; } - + running[type][device_idx] = NULL; - + if ( !device->ref_count ) { - + // printf("Closed device "); - + if (type == input) { if ( !alcCaptureCloseDevice(device->dhndl) ) rc = de_AlError; } - else { + else { if (alcGetCurrentContext() != device->ctx) alcMakeContextCurrent(device->ctx); - + alDeleteSources(1, &device->source); alDeleteBuffers(OPENAL_BUFS, device->buffers); - + alcMakeContextCurrent(NULL); if ( device->ctx ) alcDestroyContext(device->ctx); if ( !alcCloseDevice(device->dhndl) ) rc = de_AlError; } - + free(device); } else device->ref_count--; - + unlock; return rc; } DeviceError register_device_callback( int32_t call_idx, uint32_t device_idx, DataHandleCallback callback, void* data, bool enable_VAD) -{ - if (size[input] <= device_idx || !running[input][device_idx] || running[input][device_idx]->dhndl == NULL) +{ + if (size[input] <= device_idx || !running[input][device_idx] || running[input][device_idx]->dhndl == NULL) return de_InvalidSelection; lock; @@ -375,9 +378,9 @@ inline__ DeviceError write_out(uint32_t device_idx, const int16_t* data, uint32_ alSourceUnqueueBuffers(device->source, processed, bufids); alDeleteBuffers(processed - 1, bufids + 1); bufid = bufids[0]; - } + } else if(queued < 16) alGenBuffers(1, &bufid); - else { + else { pthread_mutex_unlock(device->mutex); return de_Busy; } @@ -404,44 +407,51 @@ void* thread_poll (void* arg) // TODO: maybe use thread for every input source (void)arg; uint32_t i; int32_t sample = 0; - - - while (thread_running) + + + while (true) { + lock; + if (!thread_running) { + unlock; + break; + } + unlock; + if (thread_paused) usleep(10000); /* Wait for unpause. */ else { - for (i = 0; i < size[input]; ++i) + for (i = 0; i < size[input]; ++i) { lock; - if (running[input][i] != NULL) + if (running[input][i] != NULL) { alcGetIntegerv(running[input][i]->dhndl, ALC_CAPTURE_SAMPLES, sizeof(int32_t), &sample); - + int f_size = (running[input][i]->sample_rate * running[input][i]->frame_duration / 1000); - - if (sample < f_size) { + + if (sample < f_size) { unlock; continue; } Device* device = running[input][i]; - + int16_t frame[16000]; alcCaptureSamples(device->dhndl, frame, f_size); - - if (device->muted) { + + if (device->muted) { unlock; continue; } - + if ( device->cb ) device->cb(frame, f_size, device->cb_data); - } + } unlock; } usleep(5000); } } - + pthread_exit(NULL); } @@ -462,8 +472,8 @@ DeviceError selection_valid(DeviceType type, int32_t selection) void* get_device_callback_data(uint32_t device_idx) { - if (size[input] <= device_idx || !running[input][device_idx] || running[input][device_idx]->dhndl == NULL) + if (size[input] <= device_idx || !running[input][device_idx] || running[input][device_idx]->dhndl == NULL) return NULL; - + return running[input][device_idx]->cb_data; } diff --git a/src/friendlist.c b/src/friendlist.c index 0e0b394..be2856c 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -624,10 +624,12 @@ static void draw_del_popup(void) wprintw(PendingDelete.popup, "Delete contact "); wattron(PendingDelete.popup, A_BOLD); + pthread_mutex_lock(&Winthread.lock); if (blocklist_view == 0) wprintw(PendingDelete.popup, "%s", Friends.list[PendingDelete.num].name); else wprintw(PendingDelete.popup, "%s", Blocked.list[PendingDelete.num].name); + pthread_mutex_unlock(&Winthread.lock); wattroff(PendingDelete.popup, A_BOLD); wprintw(PendingDelete.popup, "? y/n"); @@ -888,12 +890,13 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) return; } - uint64_t cur_time = get_unix_time(); - struct tm cur_loc_tm = *localtime((const time_t *) &cur_time); + uint64_t cur_time = time(NULL); + struct tm cur_loc_tm = *localtime((const time_t *) &cur_time); wattron(self->window, A_BOLD); wprintw(self->window, " Online: "); wattroff(self->window, A_BOLD); + wprintw(self->window, "%d/%d \n\n", Friends.num_online, Friends.num_friends); if ((y2 - FLIST_OFST) <= 0) @@ -902,18 +905,30 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) uint32_t selected_num = 0; /* Determine which portion of friendlist to draw based on current position */ + pthread_mutex_lock(&Winthread.lock); int page = Friends.num_selected / (y2 - FLIST_OFST); + pthread_mutex_unlock(&Winthread.lock); + int start = (y2 - FLIST_OFST) * page; int end = y2 - FLIST_OFST + start; + pthread_mutex_lock(&Winthread.lock); + size_t num_friends = Friends.num_friends; + pthread_mutex_unlock(&Winthread.lock); + int i; - for (i = start; i < Friends.num_friends && i < end; ++i) { + for (i = start; i < num_friends && i < end; ++i) { + pthread_mutex_lock(&Winthread.lock); uint32_t f = Friends.index[i]; + bool is_active = Friends.list[f].active; + int num_selected = Friends.num_selected; + pthread_mutex_unlock(&Winthread.lock); + bool f_selected = false; - if (Friends.list[f].active) { - if (i == Friends.num_selected) { + if (is_active) { + if (i == num_selected) { wattron(self->window, A_BOLD); wprintw(self->window, " > "); wattroff(self->window, A_BOLD); @@ -923,8 +938,12 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, " "); } - if (Friends.list[f].connection_status != TOX_CONNECTION_NONE) { - TOX_USER_STATUS status = Friends.list[f].status; + pthread_mutex_lock(&Winthread.lock); + TOX_CONNECTION connection_status = Friends.list[f].connection_status; + TOX_USER_STATUS status = Friends.list[f].status; + pthread_mutex_unlock(&Winthread.lock); + + if (connection_status != TOX_CONNECTION_NONE) { int colour = MAGENTA; switch (status) { @@ -947,7 +966,9 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(BLUE)); wattron(self->window, A_BOLD); + pthread_mutex_lock(&Winthread.lock); wprintw(self->window, "%s", Friends.list[f].name); + pthread_mutex_unlock(&Winthread.lock); wattroff(self->window, A_BOLD); if (f_selected) @@ -960,17 +981,23 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) pthread_mutex_lock(&Winthread.lock); tox_friend_get_status_message(m, Friends.list[f].num, (uint8_t *) statusmsg, NULL); size_t s_len = tox_friend_get_status_message_size(m, Friends.list[f].num, NULL); - statusmsg[s_len] = '\0'; pthread_mutex_unlock(&Winthread.lock); + statusmsg[s_len] = '\0'; + filter_str(statusmsg, s_len); + + pthread_mutex_lock(&Winthread.lock); snprintf(Friends.list[f].statusmsg, sizeof(Friends.list[f].statusmsg), "%s", statusmsg); Friends.list[f].statusmsg_len = strlen(Friends.list[f].statusmsg); + pthread_mutex_unlock(&Winthread.lock); } /* Truncate note if it doesn't fit on one line */ size_t maxlen = x2 - getcurx(self->window) - 2; + pthread_mutex_lock(&Winthread.lock); + if (Friends.list[f].statusmsg_len > maxlen) { Friends.list[f].statusmsg[maxlen - 3] = '\0'; strcat(Friends.list[f].statusmsg, "..."); @@ -981,6 +1008,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) if (Friends.list[f].statusmsg_len > 0) wprintw(self->window, " %s", Friends.list[f].statusmsg); + pthread_mutex_unlock(&Winthread.lock); + wprintw(self->window, "\n"); } else { wprintw(self->window, "%s ", OFFLINE_CHAR); @@ -989,21 +1018,29 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(BLUE)); wattron(self->window, A_BOLD); + pthread_mutex_lock(&Winthread.lock); wprintw(self->window, "%s", Friends.list[f].name); + pthread_mutex_unlock(&Winthread.lock); wattroff(self->window, A_BOLD); if (f_selected) wattroff(self->window, COLOR_PAIR(BLUE)); + pthread_mutex_lock(&Winthread.lock); uint64_t last_seen = Friends.list[f].last_online.last_on; + pthread_mutex_unlock(&Winthread.lock); if (last_seen != 0) { + pthread_mutex_lock(&Winthread.lock); + int day_dist = ( cur_loc_tm.tm_yday - Friends.list[f].last_online.tm.tm_yday + ((cur_loc_tm.tm_year - Friends.list[f].last_online.tm.tm_year) * 365) ); const char *hourmin = Friends.list[f].last_online.hour_min_str; + pthread_mutex_unlock(&Winthread.lock); + switch (day_dist) { case 0: wprintw(self->window, " Last seen: Today %s\n", hourmin); @@ -1026,7 +1063,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) self->x = x2; - if (Friends.num_friends) { + if (num_friends) { wmove(self->window, y2 - 1, 1); wattron(self->window, A_BOLD); diff --git a/src/misc_tools.c b/src/misc_tools.c index 54b3811..68f1963 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -54,6 +54,7 @@ void hst_to_net(uint8_t *num, uint16_t numbytes) return; } +/* Note: The time functions are not thread safe */ void update_unix_time(void) { current_unix_time = (uint64_t) time(NULL); diff --git a/src/misc_tools.h b/src/misc_tools.h index e64b896..605555f 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -53,16 +53,16 @@ int hex_string_to_bin(const char *hex_string, size_t hex_len, char *output, size /* convert a hex string to bytes. returns 0 on success, -1 on failure */ int hex_string_to_bytes(char *buf, int size, const char *keystr); -/* get the current unix time */ +/* get the current unix time (not thread safe) */ uint64_t get_unix_time(void); -/* Puts the current time in buf in the format of [HH:mm:ss] */ +/* Puts the current time in buf in the format of [HH:mm:ss] (not thread safe) */ void get_time_str(char *buf, int bufsize); /* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */ void get_elapsed_time_str(char *buf, int bufsize, uint64_t secs); -/* get the current local time */ +/* get the current local time (not thread safe) */ struct tm *get_time(void); /* updates current unix time (should be run once per do_toxic loop) */ diff --git a/src/notify.c b/src/notify.c index dc58e94..b7aae76 100644 --- a/src/notify.c +++ b/src/notify.c @@ -257,7 +257,7 @@ void* do_playing(void* _p) } } #ifdef BOX_NOTIFY - else if (actives[i].box && get_unix_time() >= actives[i].n_timeout) + else if (actives[i].box && time(NULL) >= actives[i].n_timeout) { GError* ignore; notify_notification_close(actives[i].box, &ignore); @@ -278,7 +278,7 @@ void* do_playing(void* _p) /* device is opened and no activity in under DEVICE_COOLDOWN time, close device*/ if (device_opened && !has_looping && - (get_unix_time() - last_opened_update) > DEVICE_COOLDOWN) { + (time(NULL) - last_opened_update) > DEVICE_COOLDOWN) { m_close_device(); } has_looping = false; @@ -323,7 +323,7 @@ void* do_playing(void* _p) int i; for (i = 0; i < ACTIVE_NOTIFS_MAX; i ++) { - if (actives[i].box && get_unix_time() >= actives[i].n_timeout) + if (actives[i].box && time(NULL) >= actives[i].n_timeout) { GError* ignore; notify_notification_close(actives[i].box, &ignore); @@ -391,7 +391,7 @@ int init_notify(int login_cooldown, int notification_timeout) } #endif - Control.cooldown = get_unix_time() + login_cooldown; + Control.cooldown = time(NULL) + login_cooldown; #ifdef BOX_NOTIFY diff --git a/src/prompt.c b/src/prompt.c index c032711..872481b 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -260,14 +260,23 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) mvwprintw(ctx->linewin, 1, 0, "%ls", &ctx->line[ctx->start]); StatusBar *statusbar = self->stb; + mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2); wmove(statusbar->topline, 0, 0); - if (statusbar->connection != TOX_CONNECTION_NONE) { + pthread_mutex_lock(&Winthread.lock); + TOX_CONNECTION connection = statusbar->connection; + pthread_mutex_unlock(&Winthread.lock); + + if (connection != TOX_CONNECTION_NONE) { int colour = MAGENTA; const char *status_text = "ERROR"; - switch (statusbar->status) { + pthread_mutex_lock(&Winthread.lock); + TOX_USER_STATUS status = statusbar->status; + pthread_mutex_unlock(&Winthread.lock); + + switch (status) { case TOX_USER_STATUS_NONE: status_text = "Online"; colour = GREEN; @@ -287,12 +296,16 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wattron(statusbar->topline, A_BOLD); + pthread_mutex_lock(&Winthread.lock); wprintw(statusbar->topline, " %s", statusbar->nick); + pthread_mutex_unlock(&Winthread.lock); wattroff(statusbar->topline, A_BOLD); } else { wprintw(statusbar->topline, " [Offline]"); wattron(statusbar->topline, A_BOLD); + pthread_mutex_lock(&Winthread.lock); wprintw(statusbar->topline, " %s", statusbar->nick); + pthread_mutex_unlock(&Winthread.lock); wattroff(statusbar->topline, A_BOLD); } @@ -304,10 +317,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) size_t slen = tox_self_get_status_message_size(m); tox_self_get_status_message (m, (uint8_t*) statusmsg); statusmsg[slen] = '\0'; - pthread_mutex_unlock(&Winthread.lock); - snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); statusbar->statusmsg_len = strlen(statusbar->statusmsg); + pthread_mutex_unlock(&Winthread.lock); } self->x = x2; @@ -315,6 +327,8 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) /* Truncate note if it doesn't fit in statusbar */ uint16_t maxlen = x2 - getcurx(statusbar->topline) - 3; + pthread_mutex_lock(&Winthread.lock); + if (statusbar->statusmsg_len > maxlen) { statusbar->statusmsg[maxlen - 3] = '\0'; strcat(statusbar->statusmsg, "..."); @@ -324,6 +338,8 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) if (statusbar->statusmsg[0]) wprintw(statusbar->topline, " : %s", statusbar->statusmsg); + pthread_mutex_unlock(&Winthread.lock); + mvwhline(self->window, y2 - CHATBOX_HEIGHT, 0, ACS_HLINE, x2); int y, x; diff --git a/src/toxic.c b/src/toxic.c index 4e938b8..a0796da 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -807,10 +807,14 @@ static void do_bootstrap(Tox *m) static void do_toxic(Tox *m, ToxWindow *prompt) { - if (arg_opts.no_connect) - return; - pthread_mutex_lock(&Winthread.lock); + update_unix_time(); + + if (arg_opts.no_connect) { + pthread_mutex_unlock(&Winthread.lock); + return; + } + tox_iterate(m); do_bootstrap(m); check_file_transfer_timeouts(m); @@ -822,6 +826,7 @@ static void do_toxic(Tox *m, ToxWindow *prompt) void *thread_winref(void *data) { Tox *m = (Tox *) data; + uint8_t draw_count = 0; init_signal_catchers(); @@ -1105,14 +1110,16 @@ static useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, ui return new_sleep; } +// this doesn't do anything (yet) #ifdef X11 -// FIXME void DnD_callback(const char* asdv, DropType dt) { - if (dt != DT_plain) - return; + // if (dt != DT_plain) + // return; - line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv); + // pthread_mutex_lock(&Winthread.lock); + // line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv); + pthread_mutex_unlock(&Winthread.lock); } #endif /* X11 */ @@ -1181,6 +1188,7 @@ int main(int argc, char *argv[]) if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) m) != 0) exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + #ifdef AUDIO av = init_audio(prompt, m); @@ -1214,7 +1222,10 @@ int main(int argc, char *argv[]) if (init_mplex_away_timer(m) == -1) queue_init_message("Failed to init mplex auto-away."); + pthread_mutex_lock(&Winthread.lock); print_init_messages(prompt); + pthread_mutex_unlock(&Winthread.lock); + cleanup_init_messages(); /* set user avatar from config file. if no path is supplied tox_unset_avatar is called */ @@ -1228,7 +1239,6 @@ int main(int argc, char *argv[]) uint64_t loopcount = 0; while (true) { - update_unix_time(); do_toxic(m, prompt); uint64_t cur_time = get_unix_time(); diff --git a/src/windows.c b/src/windows.c index 6324b38..f0f37be 100644 --- a/src/windows.c +++ b/src/windows.c @@ -454,10 +454,16 @@ void on_window_resize(void) static void draw_window_tab(ToxWindow *toxwin) { + pthread_mutex_lock(&Winthread.lock); if (toxwin->alert != WINDOW_ALERT_NONE) attron(COLOR_PAIR(toxwin->alert)); + pthread_mutex_unlock(&Winthread.lock); + clrtoeol(); printw(" [%s]", toxwin->name); + + pthread_mutex_lock(&Winthread.lock); if (toxwin->alert != WINDOW_ALERT_NONE) attroff(COLOR_PAIR(toxwin->alert)); + pthread_mutex_unlock(&Winthread.lock); } static void draw_bar(void) @@ -505,7 +511,10 @@ static void draw_bar(void) void draw_active_window(Tox *m) { ToxWindow *a = active_window; + + pthread_mutex_lock(&Winthread.lock); a->alert = WINDOW_ALERT_NONE; + pthread_mutex_unlock(&Winthread.lock); wint_t ch = 0;