diff --git a/src/chat.c b/src/chat.c index a902f84..be5eb4a 100644 --- a/src/chat.c +++ b/src/chat.c @@ -409,8 +409,9 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, ui n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1); name[n_len] = '\0'; - snprintf(msg, sizeof(msg), "%s has invited you to a group chat.\n" - "Type \"/join\" to join the chat.", name); + snprintf(msg, sizeof(msg), "%s has invited you to a group chat.", name); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + snprintf(msg, sizeof(msg), "Type \"/join\" to join the chat.", name); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); memcpy(friends[friendnumber].pending_groupchat, group_pub_key, TOX_CLIENT_ID_SIZE); @@ -429,7 +430,7 @@ void chat_onInvite (ToxWindow *self, ToxAv *av, int call_index) self->call_index = call_index; - line_info_add(self, NULL, NULL, NULL, "Incoming audio call!\nType: \"/answer\" or \"/reject\"", SYS_MSG, 0, 0); + line_info_add(self, NULL, NULL, NULL, "Incoming audio call! Type: \"/answer\" or \"/reject\"", SYS_MSG, 0, 0); alert_window(self, WINDOW_ALERT_0, true); } @@ -439,7 +440,7 @@ void chat_onRinging (ToxWindow *self, ToxAv *av, int call_index) if ( self->call_index != call_index || self->num != toxav_get_peer_id(av, call_index, 0)) return; - line_info_add(self, NULL, NULL, NULL, "Ringing...\n\"cancel\" ?", SYS_MSG, 0, 0); + line_info_add(self, NULL, NULL, NULL, "Ringing...\"cancel\" ?", SYS_MSG, 0, 0); } void chat_onStarting (ToxWindow *self, ToxAv *av, int call_index) @@ -447,7 +448,7 @@ void chat_onStarting (ToxWindow *self, ToxAv *av, int call_index) if ( self->call_index != call_index || self->num != toxav_get_peer_id(av, call_index, 0)) return; - line_info_add(self, NULL, NULL, NULL, "Call started!\nType: \"/hangup\" to end it.", SYS_MSG, 0, 0); + line_info_add(self, NULL, NULL, NULL, "Call started! Type: \"/hangup\" to end it.", SYS_MSG, 0, 0); } void chat_onEnding (ToxWindow *self, ToxAv *av, int call_index) @@ -471,7 +472,7 @@ void chat_onStart (ToxWindow *self, ToxAv *av, int call_index) if ( self->call_index != call_index || self->num != toxav_get_peer_id(av, call_index, 0)) return; - line_info_add(self, NULL, NULL, NULL, "Call started!\nType: \"/hangup\" to end it.", SYS_MSG, 0, 0); + line_info_add(self, NULL, NULL, NULL, "Call started! Type: \"/hangup\" to end it.", SYS_MSG, 0, 0); } void chat_onCancel (ToxWindow *self, ToxAv *av, int call_index) diff --git a/src/chat_commands.c b/src/chat_commands.c index 3e70ea1..fc4e1c6 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -186,7 +186,7 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Saving file as: '%s' (%.1f%%)", filename, 0.0); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); - friends[self->num].file_receiver.line_id[filenum] = self->chatwin->hst->line_end->id; + friends[self->num].file_receiver.line_id[filenum] = self->chatwin->hst->line_end->id + 1; if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) { errmsg = "* Error writing to file."; diff --git a/src/global_commands.c b/src/global_commands.c index 9221e90..9763070 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -423,9 +423,11 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a for (i = 0; i < NUMLINES; ++i) line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0); - msg = " * Argument messages must be enclosed in quotation marks.\n" - " * Use ctrl-o and ctrl-p to navigate through the tabs.\n"; + msg = " * Argument messages must be enclosed in quotation marks."; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); + msg = " * Use ctrl-o and ctrl-p to navigate through the tabs."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); + line_info_add(self, NULL, NULL, NULL, "", SYS_MSG, 0, 0); hst->line_start = start; } diff --git a/src/groupchat.c b/src/groupchat.c index d888714..fe78c1e 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -149,10 +149,11 @@ static void print_groupchat_help(ToxWindow *self) msg = " * Use Page Up/Page Down keys to scroll chat history"; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); - msg = " * Scroll peer list with the ctrl-] and ctrl-[ keys.\n"; + msg = " * Scroll peer list with the ctrl-] and ctrl-[ keys."; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); - msg = " * Notice, some friends will be missing names while finding peers\n"; + msg = " * Notice, some friends will be missing names while finding peers"; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, 0); + line_info_add(self, NULL, NULL, NULL, "", SYS_MSG, 0, 0); hst->line_start = start; } diff --git a/src/line_info.c b/src/line_info.c index 0297429..4c13e9b 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -49,6 +49,7 @@ void line_info_init(struct history *hst) memset(hst->line_root, 0, sizeof(struct line_info)); hst->line_start = hst->line_root; hst->line_end = hst->line_start; + hst->queue_sz = 0; } /* resets line_start */ @@ -60,7 +61,7 @@ static void line_info_reset_start(ToxWindow *self, struct history *hst) struct line_info *line = hst->line_end; uint16_t lncnt = 0; int side_offst = self->is_groupchat ? SIDEBAR_WIDTH : 0; - int top_offst = self->is_chat ? 4 : 1; /* leave one blank space at bottom */ + int top_offst = self->is_chat ? 3 : 0; int max_y = (y2 - CHATBOX_HEIGHT - top_offst); while (line->prev && lncnt < max_y) { @@ -98,6 +99,33 @@ static void line_info_root_fwd(struct history *hst) hst->line_root = tmp; } +/* adds a line_info line to queue */ +static void line_info_add_queue(struct history *hst, struct line_info *line) +{ + if (hst->queue_sz >= MAX_QUEUE) + return; + + hst->queue[hst->queue_sz++] = line; +} + +/* returns ptr to queue item 0 and removes it from queue */ +static struct line_info *line_info_ret_queue(struct history *hst) +{ + if (hst->queue_sz <= 0) + return NULL; + + struct line_info *ret = hst->queue[0]; + + int i; + + for (i = 0; i < hst->queue_sz; ++i) + hst->queue[i] = hst->queue[i + 1]; + + --hst->queue_sz; + + return ret; +} + void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, uint8_t type, uint8_t bold, uint8_t colour) { @@ -149,24 +177,28 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na new_line->type = type; new_line->bold = bold; new_line->colour = colour; - new_line->id = hst->line_end->id + 1; - new_line->prev = hst->line_end; - hst->line_end->next = new_line; - hst->line_end = new_line; + line_info_add_queue(hst, new_line); +} + +/* adds a single queue item to hst if possible. only called once per call to line_info_print() */ +static void line_info_check_queue(ToxWindow *self) +{ + struct history *hst = self->chatwin->hst; + struct line_info *line = line_info_ret_queue(hst); + + if (line == NULL) + return; if (++hst->line_items > user_settings->history_size) { --hst->line_items; line_info_root_fwd(hst); } - int newlines = 0; - int i; - - for (i = 0; msg[i]; ++i) { - if (msg[i] == '\n') - ++newlines; - } + line->id = hst->line_end->id + 1; + line->prev = hst->line_end; + hst->line_end->next = line; + hst->line_end = line; int y, y2, x, x2; getmaxyx(self->window, y2, x2); @@ -176,17 +208,14 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na return; int offst = self->is_groupchat ? SIDEBAR_WIDTH : 0; /* offset width of groupchat sidebar */ - int lines = (1 + newlines + (len / (x2 - offst))); - hst->queue_lns += lines; - ++hst->queue; - + int lines = 1 + (line->len / (x2 - offst)); int max_y = self->is_prompt ? y2 : y2 - CHATBOX_HEIGHT; /* move line_start forward proportionate to the number of new lines */ - if (y + hst->queue_lns - hst->queue >= max_y) { + if (y >= max_y) { while (lines > 0) { ++hst->start_id; - lines -= (1 + hst->line_start->len / (x2 - offst)); + lines -= 1 + (hst->line_start->len / (x2 - offst)); if (hst->line_start->next) hst->line_start = hst->line_start->next; @@ -201,11 +230,12 @@ void line_info_print(ToxWindow *self) if (ctx == NULL) return; + struct history *hst = ctx->hst; + + /* Only allow one new item to be added to chat window per call to this function */ + line_info_check_queue(self); + WINDOW *win = ctx->history; - - ctx->hst->queue = 0; - ctx->hst->queue_lns = 0; - wclear(win); int y2, x2; getmaxyx(self->window, y2, x2); @@ -221,7 +251,7 @@ void line_info_print(ToxWindow *self) else wmove(win, 2, 0); - struct line_info *line = ctx->hst->line_start->next; + struct line_info *line = hst->line_start->next; int offst = self->is_groupchat ? SIDEBAR_WIDTH : 0; int numlines = 0; @@ -337,6 +367,10 @@ void line_info_print(ToxWindow *self) line = line->next; } + + /* keep calling until queue is empty */ + if (hst->queue_sz > 0) + line_info_print(self); } void line_info_set(ToxWindow *self, uint32_t id, uint8_t *msg) diff --git a/src/line_info.h b/src/line_info.h index 86bd194..f44a47d 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -28,6 +28,7 @@ #define MAX_HISTORY 10000 #define MIN_HISTORY 20 +#define MAX_QUEUE 32 enum { SYS_MSG, @@ -62,9 +63,8 @@ struct history { uint32_t start_id; /* keeps track of where line_start should be when at bottom of history */ uint32_t line_items; - /* keeps track of lines added between window refreshes */ - uint32_t queue; - uint32_t queue_lns; + struct line_info *queue[MAX_QUEUE]; + int queue_sz; /* -1 if no queue items */ }; /* adds a line to history (also moves line_start and/or line_root forward if necessary) */