From e5b6e0ad9f0c2fa61495f6518739ac5f7763e440 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 24 Mar 2014 07:18:58 -0400 Subject: [PATCH 1/8] refactor chat history to allow scrolling --- build/Makefile.am | 4 +- src/audio_call.c | 1 - src/chat.c | 243 ++++++++++++++++----------------- src/chat_commands.c | 131 +++++++++++------- src/groupchat.c | 4 +- src/line_info.c | 321 ++++++++++++++++++++++++++++++++++++++++++++ src/line_info.h | 64 +++++++++ src/log.c | 4 +- src/misc_tools.c | 6 + src/misc_tools.h | 2 +- src/prompt.c | 4 +- src/toxic_strings.c | 2 +- src/toxic_strings.h | 5 - src/toxic_windows.h | 15 ++- 14 files changed, 621 insertions(+), 185 deletions(-) create mode 100644 src/line_info.c create mode 100644 src/line_info.h diff --git a/build/Makefile.am b/build/Makefile.am index 55359f8..d3940e1 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -29,7 +29,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/log.c \ $(top_srcdir)/src/log.h \ $(top_srcdir)/src/file_senders.c \ - $(top_srcdir)/src/file_senders.h + $(top_srcdir)/src/file_senders.h \ + $(top_srcdir)/src/line_info.c \ + $(top_srcdir)/src/line_info.h toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/src/audio_call.c b/src/audio_call.c index f6ecd6b..3fe6d40 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -252,7 +252,6 @@ void terminate_audio() toxav_kill(ASettins.av); } - int errors() { return ASettins.errors; diff --git a/src/chat.c b/src/chat.c index fb771e6..6dc4a93 100644 --- a/src/chat.c +++ b/src/chat.c @@ -34,6 +34,7 @@ #include "friendlist.h" #include "toxic_strings.h" #include "log.h" +#include "line_info.h" #ifdef _SUPPORT_AUDIO #include "audio_call.h" @@ -98,6 +99,7 @@ void kill_chat_window(ToxWindow *self) StatusBar *statusbar = self->stb; log_disable(ctx->log); + line_info_cleanup(ctx->hst); int f_num = self->num; delwin(ctx->linewin); @@ -106,6 +108,7 @@ void kill_chat_window(ToxWindow *self) disable_chatwin(f_num); free(ctx->log); + free(ctx->hst); free(ctx); free(statusbar); } @@ -121,17 +124,10 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t *msg, u tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - print_time(ctx->history); - wattron(ctx->history, COLOR_PAIR(CYAN)); - wprintw(ctx->history, "%s: ", nick); - wattroff(ctx->history, COLOR_PAIR(CYAN)); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); - if (msg[0] == '>') { - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s\n", msg); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - } else - wprintw(ctx->history, "%s\n", msg); + line_info_add(self, timefrmt, nick, NULL, msg, IN_MSG, 0, 0); write_to_log(msg, nick, ctx->log, false); alert_window(self, WINDOW_ALERT_1, true); @@ -172,11 +168,10 @@ static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, uint8_t *action, tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - print_time(ctx->history); - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "* %s %s\n", nick, action); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); + line_info_add(self, timefrmt, nick, NULL, action, ACTION, 0, 0); write_to_log(action, nick, ctx->log, true); alert_window(self, WINDOW_ALERT_1, true); } @@ -216,16 +211,19 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t if (self->num != num) return; - ChatContext *ctx = self->chatwin; + uint8_t msg[MAX_STR_SIZE]; + uint8_t *errmsg; uint8_t filename[MAX_STR_SIZE]; get_file_name(pathname, filename); - wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename, - (long long unsigned int)filesize); + snprintf(msg, sizeof(msg), "File transfer request for '%s' (%llu bytes).", filename, + (long long unsigned int)filesize); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); if (filenum >= MAX_FILES) { - wprintw(ctx->history, "Too many pending file requests; discarding.\n"); + errmsg = "Too many pending file requests; discarding."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -242,12 +240,14 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filename[len + strlen(d)] = '\0'; if (count > 999) { - wprintw(ctx->history, "Error saving file to disk.\n"); + errmsg = "Error saving file to disk."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } } - wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum); + snprintf(msg, sizeof(msg), "Type '/savefile %d' to accept the file transfer.", filenum); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); friends[num].file_receiver.pending[filenum] = true; strcpy(friends[num].file_receiver.filenames[filenum], filename); @@ -270,8 +270,8 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec if (self->num != num) return; - ChatContext *ctx = self->chatwin; const uint8_t *filename; + uint8_t msg[MAX_STR_SIZE] = {0}; if (receive_send == 0) filename = friends[num].file_receiver.filenames[filenum]; @@ -280,25 +280,23 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec switch (control_type) { case TOX_FILECONTROL_ACCEPT: - wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename); + snprintf(msg, sizeof(msg), "File transfer for '%s' accepted.", filename); break; /*case TOX_FILECONTROL_PAUSE: wprintw(ctx->history, "File transfer for '%s' paused.\n", filename); break; */ case TOX_FILECONTROL_KILL: - wprintw(ctx->history, "File transfer for '%s' failed.\n", filename); - + snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename); if (receive_send == 0) chat_close_file_receiver(num, filenum); - else - chat_close_file_receiver(num, filenum); break; case TOX_FILECONTROL_FINISHED: - wprintw(ctx->history, "File transfer for '%s' complete.\n", filename); + snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename); chat_close_file_receiver(num, filenum); break; } + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); alert_window(self, WINDOW_ALERT_2, true); } @@ -308,12 +306,9 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu if (self->num != num) return; - ChatContext *ctx = self->chatwin; - if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, "* Error writing to file.\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + uint8_t *msg = " * Error writing to file."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); chat_close_file_receiver(num, filenum); @@ -325,16 +320,17 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, ui if (self->num != friendnumber) return; - ChatContext *ctx = self->chatwin; - uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'}; + uint8_t name[TOX_MAX_NAME_LENGTH]; + uint8_t msg[MAX_STR_SIZE]; if (tox_get_name(m, friendnumber, name) == -1) return; - wprintw(ctx->history, "%s has invited you to a group chat.\n", name); + snprintf(msg, sizeof(msg), "%s has invited you to a group chat.\n" + "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); - wprintw(ctx->history, "Type \"/join\" to join the chat.\n"); alert_window(self, WINDOW_ALERT_2, true); } @@ -345,144 +341,140 @@ void chat_onInvite (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "Incoming audio call!\n" - "Answer: \"/answer\" \"/cancel\"\n"); + + uint8_t *msg = "Incoming audio call!\nType: \"/answer\" or \"/cancel\""; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); alert_window(self, WINDOW_ALERT_0, true); } + void chat_onRinging (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "Ringing...\n" - "\"/cancel\" ?\n"); + + uint8_t *msg = "Ringing...\n\"/cancel\" ?"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onStarting (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - + + uint8_t *msg; + if ( 0 != start_transmission() ) {/* YEAH! */ - wprintw(ctx->history, "Error starting transmission!\n"); + msg = "Error starting transmission!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } - - wprintw(ctx->history, "Call started! \n" - "Type: \"/hangup\" to end it.\n"); - + + msg = "Call started!\nType: \"/hangup\" to end it."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onEnding (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - + + uint8_t *msg = "Call ended!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + toxav_kill_transmission(av); - - wprintw(ctx->history, "Call ended! \n"); } + void chat_onError (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "Error! \n"); + + uint8_t *msg = "Error!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onStart (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - + + uint8_t *msg; + if ( 0 != start_transmission() ) {/* YEAH! */ - wprintw(ctx->history, "Error starting transmission!\n"); + msg = "Error starting transmission!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } - - wprintw(ctx->history, "Call started! \n" - "Type: \"/hangup\" to end it.\n"); + + msg = "Call started!\nType: \"/hangup\" to end it."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onCancel (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "Call canceled! \n"); + + uint8_t *msg = "Call canceled!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onReject (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "Rejected! \n"); + + uint8_t *msg = "Rejected!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onEnd (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - + toxav_kill_transmission(av); - - wprintw(ctx->history, "Call ended! \n"); + + uint8_t *msg = "Call ended!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } + void chat_onRequestTimeout (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "No answer! \n"); + + uint8_t *msg = "No answer!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } void chat_onPeerTimeout (ToxWindow *self, ToxAv *av) { if (self->num != toxav_get_peer_id(av, 0)) return; - - ChatContext *ctx = self->chatwin; - - wprintw(ctx->history, "Peer disconnected; call ended! \n"); + + uint8_t *msg = "Peer disconnected; call ended!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } #endif /* _SUPPORT_AUDIO */ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) { - if (action == NULL) { - wprintw(ctx->history, "Invalid syntax.\n"); + if (action == NULL) return; - } uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_get_self_name(m, selfname); - print_time(ctx->history); - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "* %s %s\n", selfname, action); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); + + line_info_add(self, timefrmt, selfname, NULL, action, ACTION, 0, 0); if (tox_send_action(m, self->num, action, strlen(action) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send action\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + uint8_t *errmsg = " * Failed to send action."; + line_info_add(self, NULL, selfname, NULL, errmsg, SYS_MSG, 0, RED); } else { write_to_log(action, selfname, ctx->log, true); } @@ -498,7 +490,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) getmaxyx(self->window, y2, x2); int cur_len = 0; - if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ + if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */ + bool scroll = ctx->hst->scroll_mode ? false : true; + line_info_toggle_scroll(self, scroll); + } + + /* If we're in scroll mode ignore rest of function */ + if (ctx->hst->scroll_mode) { + line_info_onKey(self, key); + return; + } + + if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key */ if (ctx->pos > 0) { cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); @@ -579,13 +582,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_UP) { /* fetches previous item in history */ fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_UP); + &ctx->hst_pos, MOVE_UP); mv_curs_end(self->window, ctx->len, y2, x2); } else if (key == KEY_DOWN) { /* fetches next item in history */ fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_DWN); + &ctx->hst_pos, MOVE_DOWN); mv_curs_end(self->window, ctx->len, y2, x2); } @@ -637,7 +640,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); - wclrtobot(self->window); if (!string_is_empty(line)) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); @@ -658,22 +660,14 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_get_self_name(m, selfname); - print_time(ctx->history); - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s: ", selfname); - wattroff(ctx->history, COLOR_PAIR(GREEN)); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); - if (line[0] == '>') { - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s\n", line); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - } else - wprintw(ctx->history, "%s\n", line); + line_info_add(self, timefrmt, selfname, NULL, line, OUT_MSG, 0, 0); if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send message.\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + uint8_t *errmsg = " * Failed to send message."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); } else { write_to_log(line, selfname, ctx->log, false); } @@ -688,13 +682,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) static void chat_onDraw(ToxWindow *self, Tox *m) { - curs_set(1); int x2, y2; getmaxyx(self->window, y2, x2); ChatContext *ctx = self->chatwin; wclear(ctx->linewin); + line_info_print(self); if (ctx->len > 0) { uint8_t line[MAX_STR_SIZE]; @@ -796,6 +790,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m) { + curs_set(1); int x2, y2; getmaxyx(self->window, y2, x2); self->x = x2; @@ -817,20 +812,23 @@ static void chat_onInit(ToxWindow *self, Tox *m) scrollok(ctx->history, 1); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0); + ctx->hst = malloc(sizeof(struct history)); ctx->log = malloc(sizeof(struct chatlog)); - if (ctx->log == NULL) { + if (ctx->log == NULL || ctx->hst == NULL) { endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } + memset(ctx->hst, 0, sizeof(struct history)); memset(ctx->log, 0, sizeof(struct chatlog)); + line_info_init(ctx->hst); + if (friends[self->num].logging_on) log_enable(self->name, friends[self->num].pub_key, ctx->log); - wprintw(ctx->history, "\n\n"); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); @@ -880,7 +878,10 @@ ToxWindow new_chat(Tox *m, int32_t friendnum) ret.name[TOXIC_MAX_NAME_LENGTH] = '\0'; ChatContext *chatwin = calloc(1, sizeof(ChatContext)); + memset(chatwin, 0, sizeof(ChatContext)); + StatusBar *stb = calloc(1, sizeof(StatusBar)); + memset(stb, 0, sizeof(StatusBar)); if (stb != NULL && chatwin != NULL) { ret.chatwin = chatwin; diff --git a/src/chat_commands.c b/src/chat_commands.c index fb4af67..4270343 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -31,6 +31,7 @@ #include "misc_tools.h" #include "friendlist.h" #include "execute.h" +#include "line_info.h" extern ToxWindow *prompt; @@ -48,80 +49,103 @@ void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg } } - wattron(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, "Chat commands:\n"); - wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); + uint8_t *msg = "Chat commands:"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); + +#ifdef _SUPPORT_AUDIO + #define NUMLINES 11 +#else + #define NUMLINES 7 +#endif + + uint8_t lines[NUMLINES][MAX_STR_SIZE] = { #ifdef _SUPPORT_AUDIO - wprintw(window, " /call : Audio call\n"); - wprintw(window, " /cancel : Cancel call\n"); - wprintw(window, " /answer : Answer incomming call\n"); - wprintw(window, " /hangup : Hangup active call\n"); + { " /call : Audio call" }, + { " /cancel : Cancel call" }, + { " /answer : Answer incomming call" }, + { " /hangup : Hangup active call" }, #endif /* _SUPPORT_AUDIO */ - wprintw(window, " /invite : Invite friend to a group chat\n"); - wprintw(window, " /join : Join a pending group chat\n"); - wprintw(window, " /log or : Enable/disable logging\n"); - wprintw(window, " /sendfile : Send a file\n"); - wprintw(window, " /savefile : Receive a file\n"); - wprintw(window, " /close : Close the current chat window\n"); - wprintw(window, " /help : Print this message again\n"); - wprintw(window, " /help global : Show a list of global commands\n"); - - wattron(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, " * Argument messages must be enclosed in quotation marks.\n\n"); - wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); + { " /invite : Invite friend to a group chat" }, + { " /join : Join a pending group chat" }, + { " /log or : Enable/disable logging" }, + { " /sendfile : Send a file" }, + { " /savefile : Receive a file" }, + { " /close : Close the current chat window" }, + { " /help : Print this message again" }, + //{ " /help global : Show a list of global commands" }, +}; + + int i; + + for (i = 0; i < NUMLINES; ++i) + line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0); + + msg = " * Use ESC key to toggle history scroll mode"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); } void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (argc < 1) { - wprintw(window, "Invalid syntax.\n"); - return; + errmsg = "Invalid syntax"; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); + return; } int groupnum = atoi(argv[1]); if (groupnum == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */ - wprintw(window, "Invalid syntax.\n"); + errmsg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (tox_invite_friend(m, self->num, groupnum) == -1) { - wprintw(window, "Failed to invite friend.\n"); + errmsg = "Failed to invite friend."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } - wprintw(window, "Invited friend to group chat %d.\n", groupnum); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Invited friend to Room #%d.", groupnum); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { - wattron(window, COLOR_PAIR(RED)); - wprintw(window, " * Warning: Too many windows are open.\n"); - wattron(window, COLOR_PAIR(RED)); + errmsg = " * Warning: Too many windows are open."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); return; } uint8_t *groupkey = friends[self->num].pending_groupchat; if (groupkey[0] == '\0') { - wprintw(window, "No pending group chat invite.\n"); + errmsg = "No pending group chat invite."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } int groupnum = tox_join_groupchat(m, self->num, groupkey); if (groupnum == -1) { - wprintw(window, "Group chat instance failed to initialize.\n"); + errmsg = "Group chat instance failed to initialize."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (init_groupchat_win(prompt, m, groupnum) == -1) { - wprintw(window, "Group chat window failed to initialize.\n"); + errmsg = "Group chat window failed to initialize."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); tox_del_groupchat(m, groupnum); return; } @@ -129,36 +153,43 @@ void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (argc != 1) { - wprintw(window, "Invalid syntax.\n"); + errmsg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } uint8_t filenum = atoi(argv[1]); if ((filenum == 0 && strcmp(argv[1], "0")) || filenum >= MAX_FILES) { - wprintw(window, "No pending file transfers with that number.\n"); + errmsg = "No pending file transfers with that number."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (!friends[self->num].file_receiver.pending[filenum]) { - wprintw(window, "No pending file transfers with that number.\n"); + errmsg = "No pending file transfers with that number."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } uint8_t *filename = friends[self->num].file_receiver.filenames[filenum]; if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) { - wprintw(window, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Accepted file transfer %u. Saving file as: '%s'", filenum, filename); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) { - wattron(window, COLOR_PAIR(RED)); - wprintw(window, "* Error writing to file.\n"); - wattroff(window, COLOR_PAIR(RED)); + errmsg = "* Error writing to file."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); } } else { - wprintw(window, "File transfer failed.\n"); + errmsg = "File transfer failed."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); } friends[self->num].file_receiver.pending[filenum] = false; @@ -166,20 +197,25 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (max_file_senders_index >= (MAX_FILES-1)) { - wprintw(window,"Please wait for some of your outgoing file transfers to complete.\n"); + errmsg = "Please wait for some of your outgoing file transfers to complete."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (argc < 1) { - wprintw(window, "Invalid syntax.\n"); + errmsg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } uint8_t *path = argv[1]; if (path[0] != '\"') { - wprintw(window, "File path must be enclosed in quotes.\n"); + errmsg = "File path must be enclosed in quotes."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -187,14 +223,16 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv int path_len = strlen(path); if (path_len > MAX_STR_SIZE) { - wprintw(window, "File path exceeds character limit.\n"); + errmsg = "File path exceeds character limit."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } FILE *file_to_send = fopen(path, "r"); if (file_to_send == NULL) { - wprintw(window, "File '%s' not found.\n", path); + errmsg = "File not found."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -207,7 +245,8 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv int filenum = tox_new_file_sender(m, self->num, filesize, filename, strlen(filename) + 1); if (filenum == -1) { - wprintw(window, "Error sending file.\n"); + errmsg = "Error sending file."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -225,7 +264,9 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, self->num), file_to_send); - wprintw(window, "Sending file: '%s'\n", path); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Sending file: '%s'", path); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); if (i == max_file_senders_index) ++max_file_senders_index; diff --git a/src/groupchat.c b/src/groupchat.c index 09faaf2..55d8a9d 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -427,13 +427,13 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_UP) { /* fetches previous item in history */ fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_UP); + &ctx->hst_pos, MOVE_UP); mv_curs_end(self->window, ctx->len, y2, x2); } else if (key == KEY_DOWN) { /* fetches next item in history */ fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, - &ctx->hst_pos, LN_HIST_MV_DWN); + &ctx->hst_pos, MOVE_DOWN); mv_curs_end(self->window, ctx->len, y2, x2); } diff --git a/src/line_info.c b/src/line_info.c new file mode 100644 index 0000000..bcf433b --- /dev/null +++ b/src/line_info.c @@ -0,0 +1,321 @@ +/* line_info.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "toxic_windows.h" +#include "line_info.h" + +void line_info_init(struct history *hst) +{ + hst->line_root = malloc(sizeof(struct line_info)); + memset(hst->line_root, 0, sizeof(struct line_info)); + + if (hst->line_root == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + hst->line_start = hst->line_root; + hst->line_end = hst->line_start; +} + +/* resets line_start when scroll mode is disabled */ +static void line_info_reset_start(struct history *hst) +{ + struct line_info *line = hst->line_end; + uint32_t start_id = hst->start_id; + + while (line) { + if (line->id == start_id) { + hst->line_start = line; + break; + } + + line = line->prev; + } +} + +void line_info_toggle_scroll(ToxWindow *self, bool scroll) +{ + ChatContext *ctx = self->chatwin; + struct history *hst = self->chatwin->hst; + + if (scroll) { + hst->scroll_mode = true; + scrollok(ctx->history, 0); + curs_set(0); + } else { + hst->scroll_mode = false; + scrollok(ctx->history, 1); + line_info_reset_start(hst); + curs_set(1); + } +} + +void line_info_cleanup(struct history *hst) +{ + struct line_info *tmp1 = hst->line_root; + + while (tmp1) { + struct line_info *tmp2 = tmp1->next; + free(tmp1); + tmp1 = tmp2; + } +} + +void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, + uint8_t msgtype, uint8_t bold, uint8_t colour) +{ + ChatContext *ctx = self->chatwin; + struct history *hst = self->chatwin->hst; + struct line_info *new_line = malloc(sizeof(struct line_info)); + memset(new_line, 0, sizeof(struct line_info)); + + if (new_line == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + int len = 1; /* there will always be a newline */ + + /* for type-specific formatting in print function */ + switch (msgtype) { + case OUT_MSG: + case IN_MSG: + len += 2; + break; + case ACTION: + len += 3; + break; + } + + if (msg) { + memcpy(new_line->msg, msg, MAX_STR_SIZE); + len += strlen(msg); + } if (tmstmp) { + memcpy(new_line->timestamp, tmstmp, TIME_STR_SIZE); + len += strlen(tmstmp); + } if (name1) { + memcpy(new_line->name1, name1, TOXIC_MAX_NAME_LENGTH); + len += strlen(name1); + } if (name2) { + memcpy(new_line->name2, name2, TOXIC_MAX_NAME_LENGTH); + len += strlen(name2); + } + + new_line->len = len; + new_line->msgtype = msgtype; + 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; + + /* If chat history exceeds limit move root forward and free old root */ + if (++(hst->line_items) >= MAX_HISTORY) { + --hst->line_items; + struct line_info *tmp = hst->line_root->next; + tmp->prev = NULL; + + if (hst->line_start->prev == NULL) { /* if line_start is root move it forward as well */ + hst->line_start = hst->line_start->next; + hst->line_start->prev = NULL; + } + + free(hst->line_root); + hst->line_root = tmp; + } + + int y, y2, x, x2; + getmaxyx(self->window, y2, x2); + getyx(ctx->history, y, x); + + /* move line_start forward proportionate to the number of new rows */ + if (y >= y2 - CHATBOX_HEIGHT) { + int i; + int lines = 1 + (len / x2); + + while (lines > 0 && hst->line_start->next) { + lines -= (1 + hst->line_start->len / x2); + hst->line_start = hst->line_start->next; + ++hst->start_id; + } + } +} + +void line_info_print(ToxWindow *self) +{ + ChatContext *ctx = self->chatwin; + wclear(ctx->history); + wmove(ctx->history, 1, 0); + int y2, x2; + getmaxyx(self->window, y2, x2); + + struct line_info *line = ctx->hst->line_start; + int numlines = 0; + + while(line && numlines <= x2) { + uint8_t type = line->msgtype; + numlines += line->len / x2; + + switch (type) { + case OUT_MSG: + case IN_MSG: + wattron(ctx->history, COLOR_PAIR(BLUE)); + wprintw(ctx->history, "%s", line->timestamp); + wattroff(ctx->history, COLOR_PAIR(BLUE)); + + int nameclr = GREEN; + + if (type == IN_MSG) + nameclr = CYAN; + if (line->colour) + nameclr = line->colour; + + wattron(ctx->history, COLOR_PAIR(nameclr)); + wprintw(ctx->history, "%s: ", line->name1); + wattroff(ctx->history, COLOR_PAIR(nameclr)); + + if (line->msg[0] == '>') + wattron(ctx->history, COLOR_PAIR(GREEN)); + + wprintw(ctx->history, "%s\n", line->msg); + + if (line->msg[0] == '>') + wattroff(ctx->history, COLOR_PAIR(GREEN)); + + break; + + case ACTION: + wattron(ctx->history, COLOR_PAIR(BLUE)); + wprintw(ctx->history, "%s", line->timestamp); + wattroff(ctx->history, COLOR_PAIR(BLUE)); + + wattron(ctx->history, COLOR_PAIR(YELLOW)); + wprintw(ctx->history, "* %s %s\n", line->name1, line->msg); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); + + break; + + case SYS_MSG: + if (line->timestamp[0]) { + wattron(ctx->history, COLOR_PAIR(BLUE)); + wprintw(ctx->history, "%s", line->timestamp); + wattroff(ctx->history, COLOR_PAIR(BLUE)); + } + + if (line->bold) + wattron(ctx->history, A_BOLD); + if (line->colour) + wattron(ctx->history, COLOR_PAIR(line->colour)); + + wprintw(ctx->history, "%s\n", line->msg); + + if (line->bold) + wattroff(ctx->history, A_BOLD); + if (line->colour) + wattroff(ctx->history, COLOR_PAIR(line->colour)); + + break; + } + + line = line->next; + } +} + +static void line_info_goto_root(struct history *hst) +{ + hst->line_start = hst->line_root; +} + +static void line_info_scroll_up(struct history *hst) +{ + if (hst->line_start->prev) + hst->line_start = hst->line_start->prev; + else beep(); +} + +static void line_info_scroll_down(struct history *hst) +{ + if (hst->line_start->next) + hst->line_start = hst->line_start->next; + else beep(); +} + +static void line_info_page_up(ToxWindow *self, struct history *hst) +{ + int x2, y2; + getmaxyx(self->window, y2, x2); + int jump_dist = y2 / 2; + int i; + + for (i = 0; i < jump_dist && hst->line_start->prev; ++i) + hst->line_start = hst->line_start->prev; +} + +static void line_info_page_down(ToxWindow *self, struct history *hst) +{ + int x2, y2; + getmaxyx(self->window, y2, x2); + int jump_dist = y2 / 2; + int i; + + for (i = 0; i < jump_dist && hst->line_start->next; ++i) + hst->line_start = hst->line_start->next; +} + +void line_info_onKey(ToxWindow *self, wint_t key) +{ + struct history *hst = self->chatwin->hst; + + switch (key) { + case KEY_PPAGE: + line_info_page_up(self, hst); + break; + case KEY_NPAGE: + line_info_page_down(self, hst); + break; + case KEY_UP: + line_info_scroll_up(hst); + break; + case KEY_DOWN: + line_info_scroll_down(hst); + break; + case KEY_HOME: + line_info_goto_root(hst); + break; + case KEY_END: + line_info_reset_start(hst); + break; + } +} diff --git a/src/line_info.h b/src/line_info.h new file mode 100644 index 0000000..51fd5b0 --- /dev/null +++ b/src/line_info.h @@ -0,0 +1,64 @@ +/* line_info.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +#define MAX_HISTORY 50 + +enum { + SYS_MSG, + IN_MSG, + OUT_MSG, + ACTION, + CONNECTION, +} LINE_TYPE; + +struct line_info { + uint8_t timestamp[TIME_STR_SIZE]; + uint8_t name1[TOXIC_MAX_NAME_LENGTH]; + uint8_t name2[TOXIC_MAX_NAME_LENGTH]; + uint8_t msg[MAX_STR_SIZE]; + uint8_t msgtype; + uint8_t bold; + uint8_t colour; + uint32_t id; + int len; /* combined len of all strings */ + + struct line_info *prev; + struct line_info *next; +}; + +/* Linked list containing chat history lines */ +struct history { + struct line_info *line_root; + struct line_info *line_start; /* the first line we want to start printing at */ + struct line_info *line_end; + uint32_t start_id; /* keeps track of where line_start should be when at bottom of history */ + uint32_t line_items; + bool scroll_mode; +}; + +void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, + uint8_t msgtype, uint8_t bold, uint8_t colour); +void line_info_cleanup(struct history *hst); +void line_info_toggle_scroll(ToxWindow *self, bool scroll); +void line_info_init(struct history *hst); +void line_info_print(ToxWindow *self); +void line_info_onKey(ToxWindow *self, wint_t key); diff --git a/src/log.c b/src/log.c index 33ac86b..c86f804 100644 --- a/src/log.c +++ b/src/log.c @@ -51,9 +51,7 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) sprintf(&ident[2], "%02X", key[1] & 0xff); ident[KEY_IDENT_DIGITS*2+1] = '\0'; } else { - uint8_t s[MAX_STR_SIZE]; - strftime(s, MAX_STR_SIZE, "%Y-%m-%d[%H:%M:%S]", get_time()); - snprintf(ident, sizeof(ident), "%s", s); + strftime(ident, sizeof(ident), "%Y-%m-%d[%H:%M:%S]", get_time()); path_len += strlen(ident) + 1; } diff --git a/src/misc_tools.c b/src/misc_tools.c index ee49a06..658e105 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -30,6 +30,7 @@ #include #include "toxic_windows.h" +#include "misc_tools.h" extern ToxWindow *prompt; @@ -65,6 +66,11 @@ void print_time(WINDOW *window) wattroff(window,COLOR_PAIR(BLUE)); } +void get_time_str(uint8_t *buf) +{ + strftime(buf, TIME_STR_SIZE, "[%H:%M:%S] ", get_time()); +} + /* XXX: FIX */ unsigned char *hex_string_to_bin(char hex_string[]) { diff --git a/src/misc_tools.h b/src/misc_tools.h index 57f1b83..e6af8e9 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -67,7 +67,7 @@ int qsort_strcasecmp_hlpr(const void *nick1, const void *nick2); - cannot be empty - cannot start with a space - must not contain contiguous spaces */ -bool valid_nick(uint8_t *nick); +int valid_nick(uint8_t *nick); /* Moves the cursor to the end of the line in given window */ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x); diff --git a/src/prompt.c b/src/prompt.c index 561ee2b..0bb1f9e 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -208,7 +208,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_UP) { /* fetches previous item in history */ wmove(self->window, prt->orig_y, X_OFST); fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, - &prt->hst_pos, LN_HIST_MV_UP); + &prt->hst_pos, MOVE_UP); /* adjust line y origin appropriately when window scrolls down */ if (prt->at_bottom && prt->len >= x2 - X_OFST) { @@ -230,7 +230,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_DOWN) { /* fetches next item in history */ wmove(self->window, prt->orig_y, X_OFST); fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, - &prt->hst_pos, LN_HIST_MV_DWN); + &prt->hst_pos, MOVE_DOWN); } else if (key == '\t') { /* TAB key: completes command */ diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 8935954..e535a18 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -148,7 +148,7 @@ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZ void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE], int hst_tot, int *hst_pos, int key_dir) { - if (key_dir == LN_HIST_MV_UP) { + if (key_dir == MOVE_UP) { if (--(*hst_pos) < 0) { *hst_pos = 0; beep(); diff --git a/src/toxic_strings.h b/src/toxic_strings.h index 3e28c0f..cb288e1 100644 --- a/src/toxic_strings.h +++ b/src/toxic_strings.h @@ -48,11 +48,6 @@ void reset_buf(wchar_t *buf, size_t *pos, size_t *len); Returns the difference between the old len and new len of buf on success, -1 if error */ int complete_line(wchar_t *buf, size_t *pos, size_t *len, const void *list, int n_items, int size); -enum { - LN_HIST_MV_UP, - LN_HIST_MV_DWN, -}; - /* adds a line to the ln_history buffer at hst_pos and sets hst_pos to last history item. */ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZE], int *hst_tot, int *hst_pos); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 04f5db7..c88be02 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -49,6 +49,7 @@ #define CURS_Y_OFFSET 3 /* y-axis cursor offset for chat contexts */ #define CHATBOX_HEIGHT 4 #define KEY_IDENT_DIGITS 2 /* number of hex digits to display for the pub-key based identifier */ +#define TIME_STR_SIZE 16 #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 @@ -60,6 +61,7 @@ #define T_KEY_PREV 0x0F /* ctrl-o */ #define T_KEY_C_E 0x05 /* ctrl-e */ #define T_KEY_C_A 0x01 /* ctrl-a */ +#define T_KEY_ESC 0x1B /* ESC key */ /* Curses foreground colours (background is black) */ enum { @@ -80,6 +82,11 @@ enum { WINDOW_ALERT_2, }; +enum { + MOVE_UP, + MOVE_DOWN, +}; + /* Fixes text color problem on some terminals. Uncomment if necessary */ /* #define URXVT_FIX */ @@ -176,13 +183,13 @@ struct ChatContext { size_t pos; size_t len; - wchar_t ln_history[MAX_LINE_HIST][MAX_STR_SIZE]; + wchar_t ln_history[MAX_LINE_HIST][MAX_STR_SIZE]; /* history for input lines/commands */ int hst_pos; int hst_tot; - uint8_t self_is_typing; - + struct history *hst; struct chatlog *log; + uint8_t self_is_typing; WINDOW *history; WINDOW *linewin; @@ -203,7 +210,9 @@ struct PromptBuf { int hst_pos; int hst_tot; + struct history *hst; struct chatlog *log; + WINDOW *linewin; }; From a40b6b1b1b83c146e916452e98a22b1ccbcb6307 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 24 Mar 2014 07:37:52 -0400 Subject: [PATCH 2/8] replace prompt's PromptBuf struct with a ChatContext for compatibility --- src/global_commands.c | 27 +++----- src/line_info.c | 62 ++++++++--------- src/main.c | 8 ++- src/prompt.c | 152 ++++++++++++++++++++++-------------------- src/toxic_windows.h | 19 +----- 5 files changed, 126 insertions(+), 142 deletions(-) diff --git a/src/global_commands.c b/src/global_commands.c index 3e1b749..7459548 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -222,15 +222,10 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + struct chatlog *log = self->chatwin->log; + if (argc == 0) { - bool on; - - if (self->is_chat || self->is_groupchat) - on = self->chatwin->log->log_on; - else if (self->is_prompt) - on = self->promptbuf->log->log_on; - - if (on) { + if (log->log_on) { wprintw(window, "Logging for this window is "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]"); @@ -253,13 +248,13 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX if (self->is_chat) { friends[self->num].logging_on = true; - log_enable(self->name, friends[self->num].pub_key, self->chatwin->log); + log_enable(self->name, friends[self->num].pub_key, log); } else if (self->is_prompt) { uint8_t myid[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(m, myid); - log_enable(self->name, &myid, self->promptbuf->log); + log_enable(self->name, &myid, log); } else if (self->is_groupchat) { - log_enable(self->name, NULL, self->chatwin->log); + log_enable(self->name, NULL, log); } wprintw(window, "Logging "); @@ -268,14 +263,10 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { - if (self->is_chat) { + if (self->is_chat) friends[self->num].logging_on = false; - log_disable(self->chatwin->log); - } else if (self->is_prompt) { - log_disable(self->promptbuf->log); - } else if (self->is_groupchat) { - log_disable(self->chatwin->log); - } + + log_disable(log); wprintw(window, "Logging "); wattron(window, COLOR_PAIR(RED) | A_BOLD); diff --git a/src/line_info.c b/src/line_info.c index bcf433b..9f9d626 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -65,17 +65,18 @@ static void line_info_reset_start(struct history *hst) void line_info_toggle_scroll(ToxWindow *self, bool scroll) { ChatContext *ctx = self->chatwin; + WINDOW *win = self->is_prompt ? self->window : ctx->history; struct history *hst = self->chatwin->hst; if (scroll) { hst->scroll_mode = true; - scrollok(ctx->history, 0); + scrollok(win, 0); curs_set(0); } else { hst->scroll_mode = false; - scrollok(ctx->history, 1); - line_info_reset_start(hst); + scrollok(win, 1); curs_set(1); + line_info_reset_start(hst); } } @@ -93,7 +94,7 @@ void line_info_cleanup(struct history *hst) void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, uint8_t msgtype, uint8_t bold, uint8_t colour) { - ChatContext *ctx = self->chatwin; + WINDOW *win = self->is_prompt ? self->window : self->chatwin->history; struct history *hst = self->chatwin->hst; struct line_info *new_line = malloc(sizeof(struct line_info)); memset(new_line, 0, sizeof(struct line_info)); @@ -158,7 +159,7 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na int y, y2, x, x2; getmaxyx(self->window, y2, x2); - getyx(ctx->history, y, x); + getyx(win, y, x); /* move line_start forward proportionate to the number of new rows */ if (y >= y2 - CHATBOX_HEIGHT) { @@ -176,8 +177,9 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na void line_info_print(ToxWindow *self) { ChatContext *ctx = self->chatwin; - wclear(ctx->history); - wmove(ctx->history, 1, 0); + WINDOW *win = self->is_prompt ? self->window : win; + wclear(win); + wmove(win, 1, 0); int y2, x2; getmaxyx(self->window, y2, x2); @@ -191,9 +193,9 @@ void line_info_print(ToxWindow *self) switch (type) { case OUT_MSG: case IN_MSG: - wattron(ctx->history, COLOR_PAIR(BLUE)); - wprintw(ctx->history, "%s", line->timestamp); - wattroff(ctx->history, COLOR_PAIR(BLUE)); + wattron(win, COLOR_PAIR(BLUE)); + wprintw(win, "%s", line->timestamp); + wattroff(win, COLOR_PAIR(BLUE)); int nameclr = GREEN; @@ -202,49 +204,49 @@ void line_info_print(ToxWindow *self) if (line->colour) nameclr = line->colour; - wattron(ctx->history, COLOR_PAIR(nameclr)); - wprintw(ctx->history, "%s: ", line->name1); - wattroff(ctx->history, COLOR_PAIR(nameclr)); + wattron(win, COLOR_PAIR(nameclr)); + wprintw(win, "%s: ", line->name1); + wattroff(win, COLOR_PAIR(nameclr)); if (line->msg[0] == '>') - wattron(ctx->history, COLOR_PAIR(GREEN)); + wattron(win, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s\n", line->msg); + wprintw(win, "%s\n", line->msg); if (line->msg[0] == '>') - wattroff(ctx->history, COLOR_PAIR(GREEN)); + wattroff(win, COLOR_PAIR(GREEN)); break; case ACTION: - wattron(ctx->history, COLOR_PAIR(BLUE)); - wprintw(ctx->history, "%s", line->timestamp); - wattroff(ctx->history, COLOR_PAIR(BLUE)); + wattron(win, COLOR_PAIR(BLUE)); + wprintw(win, "%s", line->timestamp); + wattroff(win, COLOR_PAIR(BLUE)); - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "* %s %s\n", line->name1, line->msg); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); + wattron(win, COLOR_PAIR(YELLOW)); + wprintw(win, "* %s %s\n", line->name1, line->msg); + wattroff(win, COLOR_PAIR(YELLOW)); break; case SYS_MSG: if (line->timestamp[0]) { - wattron(ctx->history, COLOR_PAIR(BLUE)); - wprintw(ctx->history, "%s", line->timestamp); - wattroff(ctx->history, COLOR_PAIR(BLUE)); + wattron(win, COLOR_PAIR(BLUE)); + wprintw(win, "%s", line->timestamp); + wattroff(win, COLOR_PAIR(BLUE)); } if (line->bold) - wattron(ctx->history, A_BOLD); + wattron(win, A_BOLD); if (line->colour) - wattron(ctx->history, COLOR_PAIR(line->colour)); + wattron(win, COLOR_PAIR(line->colour)); - wprintw(ctx->history, "%s\n", line->msg); + wprintw(win, "%s\n", line->msg); if (line->bold) - wattroff(ctx->history, A_BOLD); + wattroff(win, A_BOLD); if (line->colour) - wattroff(ctx->history, COLOR_PAIR(line->colour)); + wattroff(win, COLOR_PAIR(line->colour)); break; } diff --git a/src/main.c b/src/main.c index d1f6a19..47793d9 100644 --- a/src/main.c +++ b/src/main.c @@ -405,11 +405,13 @@ void exit_toxic(Tox *m) store_data(m, DATA_FILE); close_all_file_senders(); kill_all_windows(); - log_disable(prompt->promptbuf->log); + log_disable(prompt->chatwin->log); + line_info_cleanup(prompt->chatwin->hst); free(DATA_FILE); free(prompt->stb); - free(prompt->promptbuf->log); - free(prompt->promptbuf); + free(prompt->chatwin->log); + free(prompt->chatwin->hst); + free(prompt->chatwin); tox_kill(m); #ifdef _SUPPORT_AUDIO terminate_audio(); diff --git a/src/prompt.c b/src/prompt.c index 0bb1f9e..b2a8521 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -32,6 +32,7 @@ #include "execute.h" #include "misc_tools.h" #include "toxic_strings.h" +#include "line_info.h" uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; uint8_t num_frnd_requests = 0; @@ -68,18 +69,18 @@ const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = { TODO: This is only a partial fix */ void prep_prompt_win(void) { - PromptBuf *prt = prompt->promptbuf; + ChatContext *ctx = prompt->chatwin; - if (prt->len <= 0) + if (ctx->len <= 0) return; wprintw(prompt->window, "\n"); - if (!prt->at_bottom) { - wmove(prompt->window, prt->orig_y - 1, X_OFST); - ++prt->orig_y; + if (!ctx->at_bottom) { + wmove(prompt->window, ctx->orig_y - 1, X_OFST); + ++ctx->orig_y; } else { - wmove(prompt->window, prt->orig_y - 2, X_OFST); + wmove(prompt->window, ctx->orig_y - 2, X_OFST); } } @@ -138,7 +139,7 @@ static int add_friend_request(uint8_t *public_key) static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) { - PromptBuf *prt = self->promptbuf; + ChatContext *ctx = self->chatwin; int x, y, y2, x2; getyx(self->window, y, x); @@ -146,96 +147,96 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) /* BACKSPACE key: Remove one character from line */ if (key == 0x107 || key == 0x8 || key == 0x7f) { - if (prt->pos > 0) { - del_char_buf_bck(prt->line, &prt->pos, &prt->len); + if (ctx->pos > 0) { + del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); wmove(self->window, y, x-1); /* not necessary but fixes a display glitch */ - prt->scroll = false; + ctx->scroll = false; } else { beep(); } } else if (key == KEY_DC) { /* DEL key: Remove character at pos */ - if (prt->pos != prt->len) { - del_char_buf_frnt(prt->line, &prt->pos, &prt->len); - prt->scroll = false; + if (ctx->pos != ctx->len) { + del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); + ctx->scroll = false; } else { beep(); } } else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */ - if (prt->pos > 0) { - wmove(self->window, prt->orig_y, X_OFST); + if (ctx->pos > 0) { + wmove(self->window, ctx->orig_y, X_OFST); wclrtobot(self->window); - discard_buf(prt->line, &prt->pos, &prt->len); + discard_buf(ctx->line, &ctx->pos, &ctx->len); } else { beep(); } } else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */ - if (prt->len != prt->pos) - kill_buf(prt->line, &prt->pos, &prt->len); + if (ctx->len != ctx->pos) + kill_buf(ctx->line, &ctx->pos, &ctx->len); else beep(); } else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */ - if (prt->pos != 0) - prt->pos = 0; + if (ctx->pos != 0) + ctx->pos = 0; } else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */ - if (prt->pos != prt->len) - prt->pos = prt->len; + if (ctx->pos != ctx->len) + ctx->pos = ctx->len; } else if (key == KEY_LEFT) { - if (prt->pos > 0) - --prt->pos; + if (ctx->pos > 0) + --ctx->pos; else beep(); } else if (key == KEY_RIGHT) { - if (prt->pos < prt->len) - ++prt->pos; + if (ctx->pos < ctx->len) + ++ctx->pos; else beep(); } else if (key == KEY_UP) { /* fetches previous item in history */ - wmove(self->window, prt->orig_y, X_OFST); - fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, - &prt->hst_pos, MOVE_UP); + wmove(self->window, ctx->orig_y, X_OFST); + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, + &ctx->hst_pos, MOVE_UP); /* adjust line y origin appropriately when window scrolls down */ - if (prt->at_bottom && prt->len >= x2 - X_OFST) { - int px2 = prt->len >= x2 ? x2 : x2 - X_OFST; + if (ctx->at_bottom && ctx->len >= x2 - X_OFST) { + int px2 = ctx->len >= x2 ? x2 : x2 - X_OFST; int p_ofst = px2 != x2 ? 0 : X_OFST; if (px2 <= 0) return; - int k = prt->orig_y + ((prt->len + p_ofst) / px2); + int k = ctx->orig_y + ((ctx->len + p_ofst) / px2); if (k >= y2) { wprintw(self->window, "\n"); - --prt->orig_y; + --ctx->orig_y; } } } else if (key == KEY_DOWN) { /* fetches next item in history */ - wmove(self->window, prt->orig_y, X_OFST); - fetch_hist_item(prt->line, &prt->pos, &prt->len, prt->ln_history, prt->hst_tot, - &prt->hst_pos, MOVE_DOWN); + wmove(self->window, ctx->orig_y, X_OFST); + fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, + &ctx->hst_pos, MOVE_DOWN); } else if (key == '\t') { /* TAB key: completes command */ - if (prt->len > 1 && prt->line[0] == '/') { - if (complete_line(prt->line, &prt->pos, &prt->len, glob_cmd_list, AC_NUM_GLOB_COMMANDS, + if (ctx->len > 1 && ctx->line[0] == '/') { + if (complete_line(ctx->line, &ctx->pos, &ctx->len, glob_cmd_list, AC_NUM_GLOB_COMMANDS, MAX_CMDNAME_SIZE) == -1) beep(); } else { @@ -250,9 +251,9 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) if (isprint(key)) #endif { - if (prt->len < (MAX_STR_SIZE-1)) { - add_char_to_buf(prt->line, &prt->pos, &prt->len, key); - prt->scroll = true; + if (ctx->len < (MAX_STR_SIZE-1)) { + add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key); + ctx->scroll = true; } } /* RETURN key: execute command */ @@ -260,29 +261,29 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) wprintw(self->window, "\n"); uint8_t line[MAX_STR_SIZE]; - if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1) + if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) memset(&line, 0, sizeof(line)); if (!string_is_empty(line)) - add_line_to_hist(prt->line, prt->len, prt->ln_history, &prt->hst_tot, &prt->hst_pos); + add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); execute(self->window, self, m, line, GLOBAL_COMMAND_MODE); - reset_buf(prt->line, &prt->pos, &prt->len); + reset_buf(ctx->line, &ctx->pos, &ctx->len); } } static void prompt_onDraw(ToxWindow *self, Tox *m) { - PromptBuf *prt = self->promptbuf; + ChatContext *ctx = self->chatwin; - curs_set(1); int x, y, x2, y2; getyx(self->window, y, x); getmaxyx(self->window, y2, x2); wclrtobot(self->window); + line_info_print(self); /* if len is >= screen width offset max x by X_OFST to account for prompt char */ - int px2 = prt->len >= x2 ? x2 : x2 - X_OFST; + int px2 = ctx->len >= x2 ? x2 : x2 - X_OFST; if (px2 <= 0) return; @@ -290,32 +291,32 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) /* len offset to account for prompt char (0 if len is < width of screen) */ int p_ofst = px2 != x2 ? 0 : X_OFST; - if (prt->len > 0) { + if (ctx->len > 0) { uint8_t line[MAX_STR_SIZE]; - if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1) - reset_buf(prt->line, &prt->pos, &prt->len); + if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) + reset_buf(ctx->line, &ctx->pos, &ctx->len); else - mvwprintw(self->window, prt->orig_y, X_OFST, line); + mvwprintw(self->window, ctx->orig_y, X_OFST, line); - int k = prt->orig_y + ((prt->len + p_ofst) / px2); + int k = ctx->orig_y + ((ctx->len + p_ofst) / px2); - prt->at_bottom = k == y2 - 1; + ctx->at_bottom = k == y2 - 1; bool botm = k == y2; - bool edge = (prt->len + p_ofst) % px2 == 0; + bool edge = (ctx->len + p_ofst) % px2 == 0; /* move point of line origin up when input scrolls screen down */ - if (prt->scroll && edge && botm) { - --prt->orig_y; - prt->scroll = false; + if (ctx->scroll && edge && botm) { + --ctx->orig_y; + ctx->scroll = false; } } else { /* Mark point of origin for new line */ - prt->orig_y = y; + ctx->orig_y = y; } wattron(self->window, COLOR_PAIR(GREEN)); - mvwprintw(self->window, prt->orig_y, 0, "$ "); + mvwprintw(self->window, ctx->orig_y, 0, "$ "); wattroff(self->window, COLOR_PAIR(GREEN)); StatusBar *statusbar = self->stb; @@ -365,25 +366,28 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) wprintw(statusbar->topline, "\n"); /* put cursor back in correct spot */ - int y_m = prt->orig_y + ((prt->pos + p_ofst) / px2); - int x_m = (prt->pos + X_OFST) % x2; + int y_m = ctx->orig_y + ((ctx->pos + p_ofst) / px2); + int x_m = (ctx->pos + X_OFST) % x2; wmove(self->window, y_m, x_m); } static void prompt_onInit(ToxWindow *self, Tox *m) { + curs_set(1); scrollok(self->window, true); - PromptBuf *prt = self->promptbuf; + ChatContext *ctx = self->chatwin; - prt->log = malloc(sizeof(struct chatlog)); + ctx->log = malloc(sizeof(struct chatlog)); + ctx->hst = malloc(sizeof(struct history)); - if (prt->log == NULL) { + if (ctx->log == NULL || ctx->hst == NULL) { endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } - memset(prt->log, 0, sizeof(struct chatlog)); + memset(ctx->log, 0, sizeof(struct chatlog)); + memset(ctx->hst, 0, sizeof(struct history)); execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE); wclrtoeol(self->window); @@ -394,7 +398,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum if (friendnum < 0) return; - PromptBuf *prt = self->promptbuf; + ChatContext *ctx = self->chatwin; prep_prompt_win(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; @@ -419,7 +423,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum wprintw(self->window, "%s\n", msg); wattroff(self->window, COLOR_PAIR(GREEN)); - write_to_log(msg, nick, prt->log, true); + write_to_log(msg, nick, ctx->log, true); alert_window(self, WINDOW_ALERT_2, false); } else { msg = "has gone offline"; @@ -430,7 +434,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum wprintw(self->window, "%s\n", msg); wattroff(self->window, COLOR_PAIR(RED)); - write_to_log(msg, nick, prt->log, true); + write_to_log(msg, nick, ctx->log, true); } } @@ -438,7 +442,7 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_ { /* make sure message data is null-terminated */ data[length - 1] = 0; - PromptBuf *prt = self->promptbuf; + ChatContext *ctx = self->chatwin; prep_prompt_win(); wprintw(self->window, "\n"); @@ -447,14 +451,14 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_ uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data); wprintw(self->window, "%s", msg); - write_to_log(msg, "", prt->log, true); + write_to_log(msg, "", ctx->log, true); int n = add_friend_request(key); if (n == -1) { const uint8_t *errmsg = "Friend request queue is full. Discarding request.\n"; wprintw(self->window, "%s", errmsg); - write_to_log(errmsg, "", prt->log, true); + write_to_log(errmsg, "", ctx->log, true); return; } @@ -514,11 +518,11 @@ ToxWindow new_prompt(void) strcpy(ret.name, "prompt"); - PromptBuf *promptbuf = calloc(1, sizeof(PromptBuf)); + ChatContext *chatwin = calloc(1, sizeof(ChatContext)); StatusBar *stb = calloc(1, sizeof(StatusBar)); - if (stb != NULL && promptbuf != NULL) { - ret.promptbuf = promptbuf; + if (stb != NULL && chatwin != NULL) { + ret.chatwin = chatwin; ret.stb = stb; } else { endwin(); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index c88be02..ef941aa 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -148,7 +148,6 @@ struct ToxWindow { bool alert2; ChatContext *chatwin; - PromptBuf *promptbuf; StatusBar *stb; WINDOW *popup; @@ -189,31 +188,17 @@ struct ChatContext { struct history *hst; struct chatlog *log; + uint8_t self_is_typing; WINDOW *history; WINDOW *linewin; WINDOW *sidebar; -}; - -/* prompt window/buffer holder */ -struct PromptBuf { - wchar_t line[MAX_STR_SIZE]; - size_t pos; - size_t len; + /* specific for prompt */ bool at_bottom; /* true if line end is at bottom of window */ int orig_y; /* y axis point of line origin */ bool scroll; /* used for prompt window hack to determine when to scroll down */ - - wchar_t ln_history[MAX_LINE_HIST][MAX_STR_SIZE]; - int hst_pos; - int hst_tot; - - struct history *hst; - struct chatlog *log; - - WINDOW *linewin; }; /* Start file transfer code */ From 5e941427d394247df46b6bcc7ffc590acff50a92 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Mar 2014 03:17:22 -0400 Subject: [PATCH 3/8] refactor prompt to allow scrolling --- src/chat_commands.c | 11 +-- src/execute.c | 11 ++- src/friendlist.c | 7 +- src/global_commands.c | 197 +++++++++++++++++++++++++----------------- src/line_info.c | 58 +++++++++---- src/line_info.h | 1 + src/main.c | 15 ++-- src/prompt.c | 161 +++++++++++++++------------------- 8 files changed, 252 insertions(+), 209 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 4270343..3348f23 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -53,22 +53,19 @@ void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); #ifdef _SUPPORT_AUDIO - #define NUMLINES 11 + #define NUMLINES 12 #else - #define NUMLINES 7 + #define NUMLINES 8 #endif uint8_t lines[NUMLINES][MAX_STR_SIZE] = { #ifdef _SUPPORT_AUDIO - { " /call : Audio call" }, { " /cancel : Cancel call" }, { " /answer : Answer incomming call" }, { " /hangup : Hangup active call" }, - #endif /* _SUPPORT_AUDIO */ - { " /invite : Invite friend to a group chat" }, { " /join : Join a pending group chat" }, { " /log or : Enable/disable logging" }, @@ -76,7 +73,7 @@ void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg { " /savefile : Receive a file" }, { " /close : Close the current chat window" }, { " /help : Print this message again" }, - //{ " /help global : Show a list of global commands" }, + { " /help global : Show a list of global commands" }, }; int i; @@ -84,7 +81,7 @@ void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg for (i = 0; i < NUMLINES; ++i) line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0); - msg = " * Use ESC key to toggle history scroll mode"; + msg = " * Use ESC key to toggle history scroll mode\n"; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); } diff --git a/src/execute.c b/src/execute.c index 71e5958..53e2407 100644 --- a/src/execute.c +++ b/src/execute.c @@ -32,6 +32,7 @@ #include "execute.h" #include "chat_commands.h" #include "global_commands.h" +#include "line_info.h" struct cmd_func { const char *name; @@ -76,7 +77,7 @@ static struct cmd_func chat_commands[] = { /* Parses input command and puts args into arg array. Returns number of arguments on success, -1 on failure. */ -static int parse_command(WINDOW *w, char *cmd, char (*args)[MAX_STR_SIZE]) +static int parse_command(WINDOW *w, ToxWindow *self, char *cmd, char (*args)[MAX_STR_SIZE]) { int num_args = 0; bool cmd_end = false; /* flags when we get to the end of cmd */ @@ -88,7 +89,8 @@ static int parse_command(WINDOW *w, char *cmd, char (*args)[MAX_STR_SIZE]) end = strchr(cmd+1, '\"'); if (end++ == NULL) { /* Increment past the end quote */ - wprintw(w, "Invalid argument. Did you forget a closing \"?\n"); + uint8_t *errmsg = "Invalid argument. Did you forget a closing \"?"; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return -1; } @@ -131,7 +133,7 @@ void execute(WINDOW* w, ToxWindow *self, Tox *m, char *cmd, int mode) return; char args[MAX_NUM_ARGS][MAX_STR_SIZE] = {0}; - int num_args = parse_command(w, cmd, args); + int num_args = parse_command(w, self, cmd, args); if (num_args == -1) return; @@ -153,5 +155,6 @@ void execute(WINDOW* w, ToxWindow *self, Tox *m, char *cmd, int mode) if (do_command(w, self, m, num_args, GLOBAL_NUM_COMMANDS, global_commands, args) == 0) return; - wprintw(w, "Invalid command.\n"); + uint8_t *errmsg = "Invalid command."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); } diff --git a/src/friendlist.c b/src/friendlist.c index 538f803..57c73ea 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -106,7 +106,6 @@ static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t * nick[TOXIC_MAX_NAME_LENGTH] = '\0'; wprintw(prompt->window, "%s: %s\n", nick, str); - prep_prompt_win(); wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "* Warning: Too many windows are open.\n"); wattron(prompt->window, COLOR_PAIR(RED)); @@ -211,7 +210,6 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - prep_prompt_win(); wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "* File transfer from %s failed: too many windows are open.\n", nick); wattron(prompt->window, COLOR_PAIR(RED)); @@ -234,7 +232,6 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, uint8 tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - prep_prompt_win(); wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "* Group chat invite from %s failed: too many windows are open.\n", nick); wattron(prompt->window, COLOR_PAIR(RED)); @@ -343,7 +340,6 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) friends[f].chatwin = add_window(m, new_chat(m, friends[f].num)); set_active_window(friends[f].chatwin); } else { - prep_prompt_win(); wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "* Warning: Too many windows are open.\n"); wattron(prompt->window, COLOR_PAIR(RED)); @@ -539,8 +535,7 @@ static void friendlist_onAv(ToxWindow *self, ToxAv *av) tox_get_name(m, id, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; wprintw(prompt->window, "Audio action from: %s!\n", nick); - - prep_prompt_win(); + wattron(prompt->window, COLOR_PAIR(RED)); wprintw(prompt->window, "* Warning: Too many windows are open.\n"); wattron(prompt->window, COLOR_PAIR(RED)); diff --git a/src/global_commands.c b/src/global_commands.c index 7459548..084f424 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -30,6 +30,7 @@ #include "toxic_windows.h" #include "misc_tools.h" #include "friendlist.h" +#include "line_info.h" extern char *DATA_FILE; extern ToxWindow *prompt; @@ -42,30 +43,34 @@ extern uint8_t num_frnd_requests; /* command functions */ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - /* check arguments */ + uint8_t *msg; + if (argc != 1) { - wprintw(window, "Invalid syntax.\n"); - return; + msg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + return; } int req = atoi(argv[1]); if ((req == 0 && strcmp(argv[1], "0"))|| req >= MAX_FRIENDS_NUM) { - wprintw(window, "No pending friend request with that number.\n"); + msg = "No pending friend request with that number."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } if (!strlen(pending_frnd_requests[req])) { - wprintw(window, "No pending friend request with that number.\n"); + msg = "No pending friend request with that number."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } int32_t friendnum = tox_add_friend_norequest(m, pending_frnd_requests[req]); if (friendnum == -1) - wprintw(window, "Failed to add friend.\n"); + msg = "Failed to add friend."; else { - wprintw(window, "Friend request accepted.\n"); + msg = "Friend request accepted."; on_friendadded(m, friendnum, true); } @@ -79,12 +84,16 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ } num_frnd_requests = i; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (argc < 1) { - wprintw(window, "Invalid syntax.\n"); + errmsg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -95,7 +104,8 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX uint8_t *temp = argv[2]; if (temp[0] != '\"') { - wprintw(window, "Message must be enclosed in quotes.\n"); + errmsg = "Message must be enclosed in quotes."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -108,7 +118,8 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX } if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { - wprintw(window, "Invalid ID length.\n"); + errmsg = "Invalid ID length."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -123,7 +134,8 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX xx[2] = '\0'; if (sscanf(xx, "%02x", &x) != 1) { - wprintw(window, "Invalid ID.\n"); + errmsg = "Invalid ID."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -138,31 +150,33 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX switch (f_num) { case TOX_FAERR_TOOLONG: - wprintw(window, "Message is too long.\n"); + errmsg = "Message is too long."; break; case TOX_FAERR_NOMESSAGE: - wprintw(window, "Please add a message to your request.\n"); + errmsg = "Please add a message to your request."; break; case TOX_FAERR_OWNKEY: - wprintw(window, "That appears to be your own ID.\n"); + errmsg = "That appears to be your own ID."; break; case TOX_FAERR_ALREADYSENT: - wprintw(window, "Friend request has already been sent.\n"); + errmsg = "Friend request has already been sent."; break; case TOX_FAERR_UNKNOWN: - wprintw(window, "Undefined error when adding friend.\n"); + errmsg = "Undefined error when adding friend."; break; case TOX_FAERR_BADCHECKSUM: - wprintw(window, "Bad checksum in address.\n"); + errmsg = "Bad checksum in address."; break; case TOX_FAERR_SETNEWNOSPAM: - wprintw(window, "Nospam was different (is this contact already added?)\n"); + errmsg = "Nospam was different (is this contact already added?"; break; default: - wprintw(window, "Friend request sent.\n"); + errmsg = "Friend request sent."; on_friendadded(m, f_num, true); break; } + + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); } void cmd_clear(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -173,9 +187,12 @@ void cmd_clear(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + /* check arguments */ if (argc != 3) { - wprintw(window, "Invalid syntax.\n"); + errmsg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -185,7 +202,8 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv) char *key = argv[3]; if (atoi(port) == 0) { - wprintw(window, "Invalid syntax.\n"); + errmsg = "Invalid syntax."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -197,48 +215,46 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv) void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { - wattron(window, COLOR_PAIR(RED)); - wprintw(window, " * Warning: Too many windows are open.\n"); - wattron(window, COLOR_PAIR(RED)); + errmsg = " * Warning: Too many windows are open."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); return; } int groupnum = tox_add_groupchat(m); if (groupnum == -1) { - wprintw(window, "Group chat instance failed to initialize.\n"); + errmsg = "Group chat instance failed to initialize."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (init_groupchat_win(prompt, m, groupnum) == -1) { - wprintw(window, "Group chat window failed to initialize.\n"); + errmsg = "Group chat window failed to initialize."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); tox_del_groupchat(m, groupnum); return; } - wprintw(window, "Group chat created as %d.\n", groupnum); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Group chat created as %d.", groupnum); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *msg; struct chatlog *log = self->chatwin->log; if (argc == 0) { - if (log->log_on) { - wprintw(window, "Logging for this window is "); - wattron(window, COLOR_PAIR(GREEN) | A_BOLD); - wprintw(window, "[on]"); - wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); - wprintw(window, ". Type \"/log off\" to disable.\n"); - } else { - wprintw(window, "Logging for this window is "); - wattron(window, COLOR_PAIR(RED) | A_BOLD); - wprintw(window, "[off]"); - wattroff(window, COLOR_PAIR(RED) | A_BOLD); - wprintw(window, ". Type \"/log on\" to enable.\n"); - } + if (log->log_on) + msg = "Logging for this window is ON. Type \"/log off\" to disable."; + else + msg = "Logging for this window is OFF. Type \"/log on\" to enable."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } @@ -257,10 +273,8 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX log_enable(self->name, NULL, log); } - wprintw(window, "Logging "); - wattron(window, COLOR_PAIR(GREEN) | A_BOLD); - wprintw(window, "[on]\n"); - wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); + msg = "Logging enabled"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { if (self->is_chat) @@ -268,14 +282,13 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX log_disable(log); - wprintw(window, "Logging "); - wattron(window, COLOR_PAIR(RED) | A_BOLD); - wprintw(window, "[off]\n"); - wattroff(window, COLOR_PAIR(RED) | A_BOLD); + msg = "Logging disabled"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } - wprintw(window, "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging.\n"); + msg = "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging."; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -292,14 +305,17 @@ void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA strcat(id, xx); } - wprintw(window, "%s\n", id); + line_info_add(self, NULL, NULL, NULL, id, SYS_MSG, 0, 0); } void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + /* check arguments */ if (argc < 1) { - wprintw(window, "Invalid name.\n"); + errmsg = "Invalid name."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -313,7 +329,8 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA } if (!valid_nick(nick)) { - wprintw(window, "Invalid name.\n"); + errmsg = "Invalid name."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -330,15 +347,19 @@ void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + uint8_t *errmsg; + if (argc < 1) { - wprintw(window, "Wrong number of arguments.\n"); + errmsg = "Wrong number of arguments."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } uint8_t *msg = argv[1]; if (msg[0] != '\"') { - wprintw(window, "Note must be enclosed in quotes.\n"); + errmsg = "Note must be enclosed in quotes."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -351,33 +372,43 @@ void cmd_note(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - wclear(window); - wattron(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, "\n\nGlobal commands:\n"); - wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); + uint8_t *msg = "Global commands:"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); - wprintw(window, " /add : Add friend with optional message\n"); - wprintw(window, " /accept : Accept friend request\n"); - wprintw(window, " /connect : Manually connect to a DHT node\n"); - wprintw(window, " /status : Set status with optional note\n"); - wprintw(window, " /note : Set a personal note\n"); - wprintw(window, " /nick : Set your nickname\n"); - wprintw(window, " /log or : Enable/disable logging\n"); - wprintw(window, " /groupchat : Create a group chat\n"); - wprintw(window, " /myid : Print your ID\n"); - wprintw(window, " /help : Print this message again\n"); - wprintw(window, " /clear : Clear the window\n"); - wprintw(window, " /quit or /exit : Exit Toxic\n"); - #ifdef _SUPPORT_AUDIO - wprintw(window, " /lsdev : List devices where type: in|out\n"); - wprintw(window, " /sdev : Set active device\n"); + #define NUMLINES 14 +#else + #define NUMLINES 12 +#endif + + uint8_t lines[NUMLINES][MAX_STR_SIZE] = { + + { " /add : Add friend with optional message" }, + { " /accept : Accept friend request" }, + { " /connect : Manually connect to a DHT node" }, + { " /status : Set status with optional note" }, + { " /note : Set a personal note" }, + { " /nick : Set your nickname" }, + { " /log or : Enable/disable logging" }, + { " /groupchat : Create a group chat" }, + { " /myid : Print your ID" }, + { " /help : Print this message again" }, + { " /clear : Clear the window" }, + { " /quit or /exit : Exit Toxic" }, +#ifdef _SUPPORT_AUDIO + { " /lsdev : List devices where type: in|out" }, + { " /sdev : Set active device" }, #endif /* _SUPPORT_AUDIO */ - - wattron(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, " * Argument messages must be enclosed in quotation marks.\n"); - wprintw(window, " * Use ctrl-o and ctrl-p to navigate through the tabs.\n\n"); - wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); + +}; + int i; + + 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"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); } void cmd_quit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -388,16 +419,19 @@ void cmd_quit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { uint8_t *msg = NULL; + uint8_t *errmsg; if (argc >= 2) { msg = argv[2]; if (msg[0] != '\"') { - wprintw(window, "Note must be enclosed in quotes.\n"); + errmsg = "Note must be enclosed in quotes."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } } else if (argc != 1) { - wprintw(window, "Wrong number of arguments.\n"); + errmsg = "Wrong number of arguments."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } @@ -418,7 +452,8 @@ void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ else if (!strcmp(l_status, "busy")) status_kind = TOX_USERSTATUS_BUSY; else { - wprintw(window, "Invalid status. Valid statuses are: online, busy and away.\n"); + errmsg = "Invalid status. Valid statuses are: online, busy and away."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } diff --git a/src/line_info.c b/src/line_info.c index 9f9d626..f140569 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -34,7 +34,6 @@ void line_info_init(struct history *hst) { hst->line_root = malloc(sizeof(struct line_info)); - memset(hst->line_root, 0, sizeof(struct line_info)); if (hst->line_root == NULL) { endwin(); @@ -42,6 +41,7 @@ void line_info_init(struct history *hst) exit(EXIT_FAILURE); } + memset(hst->line_root, 0, sizeof(struct line_info)); hst->line_start = hst->line_root; hst->line_end = hst->line_start; } @@ -64,8 +64,7 @@ static void line_info_reset_start(struct history *hst) void line_info_toggle_scroll(ToxWindow *self, bool scroll) { - ChatContext *ctx = self->chatwin; - WINDOW *win = self->is_prompt ? self->window : ctx->history; + WINDOW *win = self->chatwin->history; struct history *hst = self->chatwin->hst; if (scroll) { @@ -94,10 +93,8 @@ void line_info_cleanup(struct history *hst) void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, uint8_t msgtype, uint8_t bold, uint8_t colour) { - WINDOW *win = self->is_prompt ? self->window : self->chatwin->history; struct history *hst = self->chatwin->hst; struct line_info *new_line = malloc(sizeof(struct line_info)); - memset(new_line, 0, sizeof(struct line_info)); if (new_line == NULL) { endwin(); @@ -105,17 +102,18 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na exit(EXIT_FAILURE); } + memset(new_line, 0, sizeof(struct line_info)); + int len = 1; /* there will always be a newline */ /* for type-specific formatting in print function */ switch (msgtype) { - case OUT_MSG: - case IN_MSG: - len += 2; - break; case ACTION: len += 3; break; + default: + len += 2; + break; } if (msg) { @@ -143,7 +141,7 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na hst->line_end = new_line; /* If chat history exceeds limit move root forward and free old root */ - if (++(hst->line_items) >= MAX_HISTORY) { + if (++hst->line_items > MAX_HISTORY) { --hst->line_items; struct line_info *tmp = hst->line_root->next; tmp->prev = NULL; @@ -159,10 +157,12 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na int y, y2, x, x2; getmaxyx(self->window, y2, x2); - getyx(win, y, x); + getyx(self->chatwin->history, y, x); + + int n = self->is_prompt ? 0 : CHATBOX_HEIGHT; /* move line_start forward proportionate to the number of new rows */ - if (y >= y2 - CHATBOX_HEIGHT) { + if (y >= y2 - n) { int i; int lines = 1 + (len / x2); @@ -177,7 +177,8 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na void line_info_print(ToxWindow *self) { ChatContext *ctx = self->chatwin; - WINDOW *win = self->is_prompt ? self->window : win; + WINDOW *win = ctx->history; + wclear(win); wmove(win, 1, 0); int y2, x2; @@ -186,7 +187,7 @@ void line_info_print(ToxWindow *self) struct line_info *line = ctx->hst->line_start; int numlines = 0; - while(line && numlines <= x2) { + while(line && numlines <= y2) { uint8_t type = line->msgtype; numlines += line->len / x2; @@ -199,10 +200,10 @@ void line_info_print(ToxWindow *self) int nameclr = GREEN; - if (type == IN_MSG) - nameclr = CYAN; if (line->colour) nameclr = line->colour; + else if (type == IN_MSG) + nameclr = CYAN; wattron(win, COLOR_PAIR(nameclr)); wprintw(win, "%s: ", line->name1); @@ -249,6 +250,31 @@ void line_info_print(ToxWindow *self) wattroff(win, COLOR_PAIR(line->colour)); break; + + case PROMPT: + wattron(win, COLOR_PAIR(GREEN)); + wprintw(win, "$ "); + wattroff(win, COLOR_PAIR(GREEN)); + + if (line->msg[0]) + wprintw(win, "%s", line->msg); + + wprintw(win, "\n"); + break; + + case CONNECTION: + wattron(win, COLOR_PAIR(BLUE)); + wprintw(win, "%s", line->timestamp); + wattroff(win, COLOR_PAIR(BLUE)); + + wattron(win, COLOR_PAIR(line->colour)); + wattron(win, A_BOLD); + wprintw(win, "* %s ", line->name1); + wattroff(win, A_BOLD); + wprintw(win, "%s\n", line->msg); + wattroff(win, COLOR_PAIR(line->colour)); + + break; } line = line->next; diff --git a/src/line_info.h b/src/line_info.h index 51fd5b0..ea2e1e7 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -26,6 +26,7 @@ enum { SYS_MSG, IN_MSG, OUT_MSG, + PROMPT, ACTION, CONNECTION, } LINE_TYPE; diff --git a/src/main.c b/src/main.c index 47793d9..0125b3b 100644 --- a/src/main.c +++ b/src/main.c @@ -60,6 +60,7 @@ #include "prompt.h" #include "misc_tools.h" #include "file_senders.h" +#include "line_info.h" #ifdef _SUPPORT_AUDIO #include "audio_call.h" @@ -272,6 +273,8 @@ int init_connection(Tox *m) static void do_connection(Tox *m, ToxWindow *prompt) { + uint8_t msg[MAX_STR_SIZE] = {0}; + static int conn_try = 0; static int conn_err = 0; static bool dht_on = false; @@ -281,21 +284,21 @@ static void do_connection(Tox *m, ToxWindow *prompt) if (!dht_on && !is_connected && !(conn_try++ % 100)) { if (!conn_err) { if ((conn_err = init_connection(m))) { - prep_prompt_win(); - wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); + snprintf(msg, sizeof(msg), "\nAuto-connect failed with error code %d", conn_err); } } } else if (!dht_on && is_connected) { dht_on = true; prompt_update_connectionstatus(prompt, dht_on); - prep_prompt_win(); - wprintw(prompt->window, "DHT connected.\n"); + snprintf(msg, sizeof(msg), "DHT connected."); } else if (dht_on && !is_connected) { dht_on = false; prompt_update_connectionstatus(prompt, dht_on); - prep_prompt_win(); - wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); + snprintf(msg, sizeof(msg), "\nDHT disconnected. Attempting to reconnect."); } + + if (msg[0]) + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } static void load_friendlist(Tox *m) diff --git a/src/prompt.c b/src/prompt.c index b2a8521..6d54481 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -65,25 +65,6 @@ const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = { #endif /* _SUPPORT_AUDIO */ }; -/* prevents input string from eating system messages: call this prior to printing a prompt message - TODO: This is only a partial fix */ -void prep_prompt_win(void) -{ - ChatContext *ctx = prompt->chatwin; - - if (ctx->len <= 0) - return; - - wprintw(prompt->window, "\n"); - - if (!ctx->at_bottom) { - wmove(prompt->window, ctx->orig_y - 1, X_OFST); - ++ctx->orig_y; - } else { - wmove(prompt->window, ctx->orig_y - 2, X_OFST); - } -} - /* Updates own nick in prompt statusbar */ void prompt_update_nick(ToxWindow *prompt, uint8_t *nick, uint16_t len) { @@ -142,14 +123,26 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) ChatContext *ctx = self->chatwin; int x, y, y2, x2; - getyx(self->window, y, x); - getmaxyx(self->window, y2, x2); + getyx(ctx->history, y, x); + getmaxyx(ctx->history, y2, x2); + + /* this is buggy */ + //if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */ + // bool scroll = ctx->hst->scroll_mode ? false : true; + // line_info_toggle_scroll(self, scroll); + //} + + /* If we're in scroll mode ignore rest of function */ + if (ctx->hst->scroll_mode) { + line_info_onKey(self, key); + return; + } /* BACKSPACE key: Remove one character from line */ if (key == 0x107 || key == 0x8 || key == 0x7f) { if (ctx->pos > 0) { del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); - wmove(self->window, y, x-1); /* not necessary but fixes a display glitch */ + wmove(ctx->history, y, x-1); /* not necessary but fixes a display glitch */ ctx->scroll = false; } else { beep(); @@ -167,8 +160,8 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */ if (ctx->pos > 0) { - wmove(self->window, ctx->orig_y, X_OFST); - wclrtobot(self->window); + wmove(ctx->history, ctx->orig_y, X_OFST); + wclrtobot(ctx->history); discard_buf(ctx->line, &ctx->pos, &ctx->len); } else { beep(); @@ -207,7 +200,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) } else if (key == KEY_UP) { /* fetches previous item in history */ - wmove(self->window, ctx->orig_y, X_OFST); + wmove(ctx->history, ctx->orig_y, X_OFST); fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, &ctx->hst_pos, MOVE_UP); @@ -222,14 +215,14 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) int k = ctx->orig_y + ((ctx->len + p_ofst) / px2); if (k >= y2) { - wprintw(self->window, "\n"); + wprintw(ctx->history, "\n"); --ctx->orig_y; } } } else if (key == KEY_DOWN) { /* fetches next item in history */ - wmove(self->window, ctx->orig_y, X_OFST); + wmove(ctx->history, ctx->orig_y, X_OFST); fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot, &ctx->hst_pos, MOVE_DOWN); } @@ -258,8 +251,8 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) } /* RETURN key: execute command */ else if (key == '\n') { - wprintw(self->window, "\n"); - uint8_t line[MAX_STR_SIZE]; + wprintw(ctx->history, "\n"); + uint8_t line[MAX_STR_SIZE] = {0}; if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) memset(&line, 0, sizeof(line)); @@ -267,7 +260,8 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) if (!string_is_empty(line)) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); - execute(self->window, self, m, line, GLOBAL_COMMAND_MODE); + line_info_add(self, NULL, NULL, NULL, line, PROMPT, 0, 0); + execute(ctx->history, self, m, line, GLOBAL_COMMAND_MODE); reset_buf(ctx->line, &ctx->pos, &ctx->len); } } @@ -277,9 +271,8 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) ChatContext *ctx = self->chatwin; int x, y, x2, y2; - getyx(self->window, y, x); - getmaxyx(self->window, y2, x2); - wclrtobot(self->window); + getyx(ctx->history, y, x); + getmaxyx(ctx->history, y2, x2); line_info_print(self); /* if len is >= screen width offset max x by X_OFST to account for prompt char */ @@ -297,7 +290,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) reset_buf(ctx->line, &ctx->pos, &ctx->len); else - mvwprintw(self->window, ctx->orig_y, X_OFST, line); + mvwprintw(ctx->history, ctx->orig_y, X_OFST, line); int k = ctx->orig_y + ((ctx->len + p_ofst) / px2); @@ -315,9 +308,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) ctx->orig_y = y; } - wattron(self->window, COLOR_PAIR(GREEN)); - mvwprintw(self->window, ctx->orig_y, 0, "$ "); - wattroff(self->window, COLOR_PAIR(GREEN)); + wattron(ctx->history, COLOR_PAIR(GREEN)); + mvwprintw(ctx->history, ctx->orig_y, 0, "$ "); + wattroff(ctx->history, COLOR_PAIR(GREEN)); StatusBar *statusbar = self->stb; werase(statusbar->topline); @@ -363,43 +356,18 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) if (statusbar->statusmsg[0]) wprintw(statusbar->topline, " - %s", statusbar->statusmsg); - wprintw(statusbar->topline, "\n"); - /* put cursor back in correct spot */ int y_m = ctx->orig_y + ((ctx->pos + p_ofst) / px2); int x_m = (ctx->pos + X_OFST) % x2; wmove(self->window, y_m, x_m); } -static void prompt_onInit(ToxWindow *self, Tox *m) -{ - curs_set(1); - scrollok(self->window, true); - ChatContext *ctx = self->chatwin; - - ctx->log = malloc(sizeof(struct chatlog)); - ctx->hst = malloc(sizeof(struct history)); - - if (ctx->log == NULL || ctx->hst == NULL) { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } - - memset(ctx->log, 0, sizeof(struct chatlog)); - memset(ctx->hst, 0, sizeof(struct history)); - - execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE); - wclrtoeol(self->window); -} - static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum , uint8_t status) { if (friendnum < 0) return; ChatContext *ctx = self->chatwin; - prep_prompt_win(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; @@ -409,31 +377,18 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum if (!nick[0]) snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); - wprintw(self->window, "\n"); - print_time(self->window); - - const uint8_t *msg; + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); + uint8_t *msg; if (status == 1) { msg = "has come online"; - wattron(self->window, COLOR_PAIR(GREEN)); - wattron(self->window, A_BOLD); - wprintw(self->window, "* %s ", nick); - wattroff(self->window, A_BOLD); - wprintw(self->window, "%s\n", msg); - wattroff(self->window, COLOR_PAIR(GREEN)); - + line_info_add(self, timefrmt, nick, NULL, msg, CONNECTION, 0, GREEN); write_to_log(msg, nick, ctx->log, true); alert_window(self, WINDOW_ALERT_2, false); } else { msg = "has gone offline"; - wattron(self->window, COLOR_PAIR(RED)); - wattron(self->window, A_BOLD); - wprintw(self->window, "* %s ", nick); - wattroff(self->window, A_BOLD); - wprintw(self->window, "%s\n", msg); - wattroff(self->window, COLOR_PAIR(RED)); - + line_info_add(self, timefrmt, nick, NULL, msg, CONNECTION, 0, RED); write_to_log(msg, nick, ctx->log, true); } } @@ -443,33 +398,32 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_ /* make sure message data is null-terminated */ data[length - 1] = 0; ChatContext *ctx = self->chatwin; - prep_prompt_win(); - wprintw(self->window, "\n"); - print_time(self->window); + wprintw(ctx->history, "\n"); + print_time(ctx->history); uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data); - wprintw(self->window, "%s", msg); + wprintw(ctx->history, "%s", msg); write_to_log(msg, "", ctx->log, true); int n = add_friend_request(key); if (n == -1) { const uint8_t *errmsg = "Friend request queue is full. Discarding request.\n"; - wprintw(self->window, "%s", errmsg); + wprintw(ctx->history, "%s", errmsg); write_to_log(errmsg, "", ctx->log, true); return; } - wprintw(self->window, "Type \"/accept %d\" to accept it.\n", n); + wprintw(ctx->history, "Type \"/accept %d\" to accept it.\n", n); alert_window(self, WINDOW_ALERT_1, true); } void prompt_init_statusbar(ToxWindow *self, Tox *m) { - int x, y; - getmaxyx(self->window, y, x); + int x2, y2; + getmaxyx(self->window, y2, x2); /* Init statusbar info */ StatusBar *statusbar = self->stb; @@ -499,7 +453,36 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) prompt_update_status(prompt, status); /* Init statusbar subwindow */ - statusbar->topline = subwin(self->window, 2, x, 0, 0); + statusbar->topline = subwin(self->window, 2, x2, 0, 0); +} + +static void prompt_onInit(ToxWindow *self, Tox *m) +{ + ChatContext *ctx = self->chatwin; + + curs_set(1); + int y2, x2; + getmaxyx(self->window, y2, x2); + + ctx->history = subwin(self->window, y2, x2, 0, 0); + scrollok(ctx->history, 1); + + ctx->log = malloc(sizeof(struct chatlog)); + ctx->hst = malloc(sizeof(struct history)); + + if (ctx->log == NULL || ctx->hst == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + memset(ctx->log, 0, sizeof(struct chatlog)); + memset(ctx->hst, 0, sizeof(struct history)); + + line_info_init(ctx->hst); + execute(ctx->history, self, m, "/help", GLOBAL_COMMAND_MODE); + + wmove(ctx->history, y2-1, 2); } ToxWindow new_prompt(void) From 2f981ecb1251524ee1a733aadf70d051a4439361 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Mar 2014 04:39:44 -0400 Subject: [PATCH 4/8] make audio messages compatible with new printing method --- src/audio_call.c | 130 +++++++++++++++++++++++++++++------------------ src/audio_call.h | 4 +- src/chat.c | 8 +-- src/main.c | 23 ++++----- src/prompt.c | 4 ++ 5 files changed, 101 insertions(+), 68 deletions(-) diff --git a/src/audio_call.c b/src/audio_call.c index 3fe6d40..4c6a3b5 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -11,6 +11,7 @@ #include "chat_commands.h" #include "global_commands.h" #include "toxic_windows.h" +#include "line_info.h" #include #include @@ -51,7 +52,6 @@ struct _ASettings { int ttas; /* Transmission thread active status (0 - stopped, 1- running) */ } ASettins; - void callback_recv_invite ( void *arg ); void callback_recv_ringing ( void *arg ); void callback_recv_starting ( void *arg ); @@ -65,16 +65,24 @@ void callback_requ_timeout ( void *arg ); void callback_peer_timeout ( void* arg ); +static void print_err (ToxWindow *self, char *error_str) +{ + line_info_add(self, NULL, NULL, NULL, error_str, SYS_MSG, 0, 0); +} + /* Opens device under current index */ -int device_open (WINDOW *window, _Devices type) +int device_open (ToxWindow *self, _Devices type) { + WINDOW *window = self->chatwin->history; + /* Do not error if no device */ if ( !ASettins.device[type].size ) return 0; ALCdevice* prev_device = ASettins.device[type].dhndl; - const char* error = NULL; + uint8_t msg[MAX_STR_SIZE]; + uint8_t* error = NULL; if ( type == input ) { ASettins.device[type].dhndl = alcCaptureOpenDevice( @@ -101,7 +109,10 @@ int device_open (WINDOW *window, _Devices type) if ( prev_device ) alcCaptureCloseDevice(prev_device); - if ( window ) wprintw(window, "Input device: %s\n", ASettins.device[type].devices[ASettins.device[type].index]); + if ( window ) { + snprintf(msg, sizeof(msg), "Input device: %s", ASettins.device[type].devices[ASettins.device[type].index]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + } } ASettins.device[type].ctx = NULL; @@ -136,20 +147,27 @@ int device_open (WINDOW *window, _Devices type) ASettins.device[type].ctx = alcCreateContext(ASettins.device[type].dhndl, NULL); - if ( window ) wprintw(window, "Output device: %s\n", ASettins.device[type].devices[ASettins.device[type].index]); + if ( window ) { + snprintf(msg, sizeof(msg), "Output device: %s", ASettins.device[type].devices[ASettins.device[type].index]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + } } } if ( error ) { - if ( window ) wprintw(window, "Error: %s\n", error); + if ( window ) { + snprintf(msg, sizeof(msg), "Error: %s", error); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + } + return -1; } else return 0; } -int device_close (WINDOW *window, _Devices type) +int device_close (ToxWindow *self, _Devices type) { - const char* device = NULL; + uint8_t* device = NULL; if ( ASettins.device[type].dhndl ) { if (type == input) { @@ -169,11 +187,14 @@ int device_close (WINDOW *window, _Devices type) ASettins.device[type].index = ASettins.device[type].dix; } - if ( window && device ) wprintw(window, "Closed %s device\n", device); + if ( self && device ) { + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Closed %s device", device); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + } } - -ToxAv* init_audio(ToxWindow* window, Tox* tox) +ToxAv* init_audio(ToxWindow* self, Tox* tox) { ASettins.errors = NoError; ASettins.ttas = 0; /* Not running */ @@ -214,7 +235,8 @@ ToxAv* init_audio(ToxWindow* window, Tox* tox) } if (!ASettins.device[input].size && !ASettins.device[output].size) { - wprintw(window->window, "No devices: disabling audio!\n"); + uint8_t *msg = "No devices: disabling audio!"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); ASettins.av = NULL; } else { @@ -226,19 +248,19 @@ ToxAv* init_audio(ToxWindow* window, Tox* tox) return NULL; } - toxav_register_callstate_callback(callback_call_started, av_OnStart, window); - toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, window); - toxav_register_callstate_callback(callback_call_rejected, av_OnReject, window); - toxav_register_callstate_callback(callback_call_ended, av_OnEnd, window); - toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, window); + toxav_register_callstate_callback(callback_call_started, av_OnStart, self); + toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, self); + toxav_register_callstate_callback(callback_call_rejected, av_OnReject, self); + toxav_register_callstate_callback(callback_call_ended, av_OnEnd, self); + toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, self); - toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, window); - toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, window); - toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, window); + toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, self); + toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, self); + toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, self); - toxav_register_callstate_callback(callback_recv_error, av_OnError, window); - toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, window); - toxav_register_callstate_callback(callback_peer_timeout, av_OnPeerTimeout, window); + toxav_register_callstate_callback(callback_recv_error, av_OnError, self); + toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, self); + toxav_register_callstate_callback(callback_peer_timeout, av_OnPeerTimeout, self); } return ASettins.av; @@ -371,7 +393,7 @@ cleanup: _cbend; } -int start_transmission() +int start_transmission(ToxWindow *self) { if ( !ASettins.av ) return -1; @@ -380,9 +402,9 @@ int start_transmission() return -1; /* Now open our devices */ - if ( -1 == device_open(NULL, input) ) + if ( -1 == device_open(self, input) ) return -1; - if ( -1 == device_open(NULL, output)) + if ( -1 == device_open(self, output)) return -1; /* Don't provide support for video */ @@ -478,7 +500,8 @@ void callback_peer_timeout ( void* arg ) */ void cmd_call(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - const char* error_str; + uint8_t msg[MAX_STR_SIZE]; + uint8_t* error_str; if (argc != 0) { error_str = "Invalid syntax!"; goto on_error; } @@ -492,17 +515,19 @@ void cmd_call(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA goto on_error; } - - wprintw(window, "Calling...\n"); - + + error_str = "Calling..."; + line_info_add(self, NULL, NULL, NULL, error_str, SYS_MSG, 0, 0); + return; -on_error: - wprintw(window, "%s %d\n", error_str, argc); +on_error: + snprintf(msg ,sizeof(msg), "%s %d", error_str, argc); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } void cmd_answer(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - const char* error_str; + uint8_t* error_str; if (argc != 0) { error_str = "Invalid syntax!"; goto on_error; } @@ -522,12 +547,12 @@ void cmd_answer(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ return; on_error: - wprintw(window, "%s\n", error_str); + print_err (self, error_str); } void cmd_hangup(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - const char* error_str; + uint8_t* error_str; if (argc != 0) { error_str = "Invalid syntax!"; goto on_error; } @@ -545,12 +570,12 @@ void cmd_hangup(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ return; on_error: - wprintw(window, "%s\n", error_str); + print_err (self, error_str); } void cmd_cancel(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - const char* error_str; + uint8_t* error_str; if (argc != 0) { error_str = "Invalid syntax!"; goto on_error; } @@ -569,13 +594,14 @@ void cmd_cancel(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[ return; on_error: - wprintw(window, "%s\n", error_str); + print_err (self, error_str); } void cmd_list_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - const char* error_str; + uint8_t msg[MAX_STR_SIZE]; + uint8_t* error_str; if ( argc != 1 ) { if ( argc < 1 ) error_str = "Type must be specified!"; @@ -593,22 +619,26 @@ void cmd_list_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (* type = output; else { - wprintw(window, "Invalid type: %s\n", argv[1]); + snprintf(msg, sizeof(msg), "Invalid type: %s", argv[1]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } int i = 0; - for ( ; i < ASettins.device[type].size; i ++) - wprintw(window, "%d: %s\n", i, ASettins.device[type].devices[i]); - + for ( ; i < ASettins.device[type].size; i ++) { + snprintf(msg, sizeof(msg), "%d: %s", i, ASettins.device[type].devices[i]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + } + return; on_error: - wprintw(window, "%s\n", error_str); + print_err (self, error_str); } void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - const char* error_str; + uint8_t msg[MAX_STR_SIZE]; + uint8_t* error_str; if ( argc != 2 ) { if ( argc < 1 ) error_str = "Type must be specified!"; @@ -632,7 +662,8 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, char ( type = output; else { - wprintw(window, "Invalid type: %s\n", argv[1]); + snprintf(msg, sizeof(msg), "Invalid type: %s", argv[1]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; } @@ -651,9 +682,10 @@ void cmd_change_device(WINDOW *window, ToxWindow *self, Tox *m, int argc, char ( } ASettins.device[type].index = selection; - wprintw(window, "Selected: %s\n", ASettins.device[type].devices[selection]); + snprintf(msg, sizeof(msg), "Selected: %s", ASettins.device[type].devices[selection]); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; on_error: - wprintw(window, "%s\n", error_str); -} \ No newline at end of file + print_err (self, error_str); +} diff --git a/src/audio_call.h b/src/audio_call.h index 1b8801d..49cdd68 100644 --- a/src/audio_call.h +++ b/src/audio_call.h @@ -20,11 +20,11 @@ typedef enum _AudioError /* You will have to pass pointer to first member of 'windows' * declared in windows.c otherwise undefined behaviour will */ -ToxAv* init_audio(ToxWindow* window, Tox* tox); +ToxAv* init_audio(ToxWindow* self, Tox* tox); void terminate_audio(); int errors(); -int start_transmission(); +int start_transmission(ToxWindow *self); #endif /* _audio_h */ \ No newline at end of file diff --git a/src/chat.c b/src/chat.c index 6dc4a93..78b65d4 100644 --- a/src/chat.c +++ b/src/chat.c @@ -364,7 +364,7 @@ void chat_onStarting (ToxWindow *self, ToxAv *av) uint8_t *msg; - if ( 0 != start_transmission() ) {/* YEAH! */ + if ( 0 != start_transmission(self) ) {/* YEAH! */ msg = "Error starting transmission!"; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; @@ -401,7 +401,7 @@ void chat_onStart (ToxWindow *self, ToxAv *av) uint8_t *msg; - if ( 0 != start_transmission() ) {/* YEAH! */ + if ( 0 != start_transmission(self) ) {/* YEAH! */ msg = "Error starting transmission!"; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); return; @@ -687,6 +687,9 @@ static void chat_onDraw(ToxWindow *self, Tox *m) ChatContext *ctx = self->chatwin; + if (!ctx->hst->scroll_mode) + curs_set(1); + wclear(ctx->linewin); line_info_print(self); @@ -790,7 +793,6 @@ static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m) { - curs_set(1); int x2, y2; getmaxyx(self->window, y2, x2); self->x = x2; diff --git a/src/main.c b/src/main.c index 0125b3b..64481a0 100644 --- a/src/main.c +++ b/src/main.c @@ -518,19 +518,18 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } -#ifdef _SUPPORT_AUDIO + uint8_t *msg; - attron(COLOR_PAIR(RED) | A_BOLD); - wprintw(prompt->window, "Starting audio...\n"); - attroff(COLOR_PAIR(RED) | A_BOLD); +#ifdef _SUPPORT_AUDIO av = init_audio(prompt, m); if ( errors() == NoError ) - wprintw(prompt->window, "Audio started with no problems.\n"); + msg = "Audio started with no problems."; else /* Get error code and stuff */ - wprintw(prompt->window, "Error starting audio!\n"); + msg = "Error starting audio!"; + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); #endif /* _SUPPORT_AUDIO */ @@ -538,17 +537,13 @@ int main(int argc, char *argv[]) load_data(m, DATA_FILE); if (f_flag == -1) { - attron(COLOR_PAIR(RED) | A_BOLD); - wprintw(prompt->window, "You passed '-f' without giving an argument.\n" - "defaulting to 'data' for a keyfile...\n"); - attroff(COLOR_PAIR(RED) | A_BOLD); + msg = "You passed '-f' without giving an argument. Defaulting to 'data' for a keyfile..."; + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } if (config_err) { - attron(COLOR_PAIR(RED) | A_BOLD); - wprintw(prompt->window, "Unable to determine configuration directory.\n" - "defaulting to 'data' for a keyfile...\n"); - attroff(COLOR_PAIR(RED) | A_BOLD); + msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile..."; + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); } sort_friendlist_index(); diff --git a/src/prompt.c b/src/prompt.c index 6d54481..1a2356e 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -273,6 +273,10 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) int x, y, x2, y2; getyx(ctx->history, y, x); getmaxyx(ctx->history, y2, x2); + + if (!ctx->hst->scroll_mode) + curs_set(1); + line_info_print(self); /* if len is >= screen width offset max x by X_OFST to account for prompt char */ From a5ce17f44eeb5a144b7e066f2faeb02edf402132 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Mar 2014 08:21:50 -0400 Subject: [PATCH 5/8] refactor groupchats to allow scrolling --- configure.ac | 2 +- src/chat.c | 3 + src/friendlist.c | 40 +++++++----- src/global_commands.c | 7 +- src/groupchat.c | 148 +++++++++++++++++++++--------------------- src/line_info.c | 38 +++++++++-- src/line_info.h | 7 +- src/misc_tools.c | 11 ---- src/misc_tools.h | 4 +- src/prompt.c | 16 ++--- 10 files changed, 148 insertions(+), 128 deletions(-) diff --git a/configure.ac b/configure.ac index 6720889..0becaaf 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.3.1], [https://tox.im/]) +AC_INIT([toxic], [0.3.2], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) diff --git a/src/chat.c b/src/chat.c index 78b65d4..16360ab 100644 --- a/src/chat.c +++ b/src/chat.c @@ -793,12 +793,14 @@ static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m) { + curs_set(1); int x2, y2; getmaxyx(self->window, y2, x2); self->x = x2; /* Init statusbar info */ StatusBar *statusbar = self->stb; + statusbar->status = tox_get_user_status(m, self->num); statusbar->is_online = tox_get_friend_connection_status(m, self->num) == 1; @@ -809,6 +811,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) /* Init subwindows */ ChatContext *ctx = self->chatwin; + statusbar->topline = subwin(self->window, 2, x2, 0, 0); ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0); scrollok(ctx->history, 1); diff --git a/src/friendlist.c b/src/friendlist.c index 57c73ea..929ece8 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -34,6 +34,7 @@ #include "chat.h" #include "friendlist.h" #include "misc_tools.h" +#include "line_info.h" #ifdef _SUPPORT_AUDIO #include "audio_call.h" @@ -104,12 +105,14 @@ static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t * uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - wprintw(prompt->window, "%s: %s\n", nick, str); - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "* Warning: Too many windows are open.\n"); - wattron(prompt->window, COLOR_PAIR(RED)); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); + line_info_add(prompt, timefrmt, nick, NULL, str, IN_MSG, 0, 0); + + uint8_t *msg = "* Warning: Too many windows are open."; + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_1, true); } } @@ -120,7 +123,7 @@ static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int32_t num, if (num >= max_friends_index) return; - friends[num].online = status == 1 ? true : false; + friends[num].online = status; update_friend_last_online(num, get_unix_time()); store_data(m, DATA_FILE); sort_friendlist_index(); @@ -210,9 +213,9 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "* File transfer from %s failed: too many windows are open.\n", nick); - wattron(prompt->window, COLOR_PAIR(RED)); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "* File transfer from %s failed: too many windows are open.", nick); + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_1, true); } @@ -232,9 +235,9 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, uint8 tox_get_name(m, num, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "* Group chat invite from %s failed: too many windows are open.\n", nick); - wattron(prompt->window, COLOR_PAIR(RED)); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "* Group chat invite from %s failed: too many windows are open.", nick); + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_1, true); } @@ -340,9 +343,8 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) friends[f].chatwin = add_window(m, new_chat(m, friends[f].num)); set_active_window(friends[f].chatwin); } else { - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "* Warning: Too many windows are open.\n"); - wattron(prompt->window, COLOR_PAIR(RED)); + uint8_t *msg = "* Warning: Too many windows are open."; + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_1, true); } @@ -534,11 +536,13 @@ static void friendlist_onAv(ToxWindow *self, ToxAv *av) uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_get_name(m, id, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; - wprintw(prompt->window, "Audio action from: %s!\n", nick); - wattron(prompt->window, COLOR_PAIR(RED)); - wprintw(prompt->window, "* Warning: Too many windows are open.\n"); - wattron(prompt->window, COLOR_PAIR(RED)); + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Audio action from: %s!", nick); + line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); + + uint8_t *errmsg = "* Warning: Too many windows are open."; + line_info_add(prompt, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_0, true); } diff --git a/src/global_commands.c b/src/global_commands.c index 084f424..44e10af 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -181,8 +181,9 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX void cmd_clear(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - wclear(window); - wprintw(window, "\n\n"); + struct history *hst = self->chatwin->hst; + line_info_cleanup(hst); + line_info_init(hst); } void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -393,7 +394,7 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a { " /groupchat : Create a group chat" }, { " /myid : Print your ID" }, { " /help : Print this message again" }, - { " /clear : Clear the window" }, + { " /clear : Clear window history" }, { " /quit or /exit : Exit Toxic" }, #ifdef _SUPPORT_AUDIO { " /lsdev : List devices where type: in|out" }, diff --git a/src/groupchat.c b/src/groupchat.c index 55d8a9d..b2991cd 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -35,6 +35,7 @@ #include "prompt.h" #include "toxic_strings.h" #include "log.h" +#include "line_info.h" extern char *DATA_FILE; extern int store_data(Tox *m, char *path); @@ -103,29 +104,38 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum) kill_groupchat_window(self); } -static void print_groupchat_help(ChatContext *ctx) +static void print_groupchat_help(ToxWindow *self) { - wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(ctx->history, "Group chat commands:\n"); - wattroff(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); + uint8_t *msg = "Group chat commands:"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); - wprintw(ctx->history, " /add : Add friend with optional message\n"); - wprintw(ctx->history, " /status : Set your status with optional note\n"); - wprintw(ctx->history, " /note : Set a personal note\n"); - wprintw(ctx->history, " /nick : Set your nickname\n"); - wprintw(ctx->history, " /groupchat : Create a group chat\n"); - wprintw(ctx->history, " /log or : Enable/disable logging\n"); - wprintw(ctx->history, " /close : Close the current group chat\n"); - wprintw(ctx->history, " /help : Print this message again\n"); - wprintw(ctx->history, " /help global : Show a list of global commands\n"); - - wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(ctx->history, " * Argument messages must be enclosed in quotation marks.\n"); - wprintw(ctx->history, " * Scroll peer list with the Page Up/Page Down keys.\n\n"); - wattroff(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); - wattron(ctx->history, COLOR_PAIR(WHITE) | A_BOLD); - wprintw(ctx->history, " Notice, some friends will be missing names while finding peers\n\n"); - wattroff(ctx->history, COLOR_PAIR(WHITE) | A_BOLD); + #define NUMLINES 9 + + uint8_t lines[NUMLINES][MAX_STR_SIZE] = { + + { " /add : Add friend with optional message" }, + { " /status : Set your status with optional note" }, + { " /note : Set a personal note" }, + { " /nick : Set your nickname" }, + { " /groupchat : Create a group chat" }, + { " /log or : Enable/disable logging" }, + { " /close : Close the current group chat" }, + { " /help : Print this message again" }, + { " /help global : Show a list of global commands" }, + +}; + + int i; + + for (i = 0; i < NUMLINES; ++i) + line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0); + + msg = " * Use ESC key to toggle history scroll mode"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); + msg = " * Scroll peer list with the Page Up/Page Down keys.\n"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); + msg = " * Notice, some friends will be missing names while finding peers\n"; + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, 0); } static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int peernum, @@ -158,19 +168,10 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int alert_window(self, alert_type, beep); - print_time(ctx->history); - wattron(ctx->history, COLOR_PAIR(nick_clr)); - wprintw(ctx->history, "%s: ", nick); - wattroff(ctx->history, COLOR_PAIR(nick_clr)); - - if (msg[0] == '>') { - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s\n", msg); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - } else { - wprintw(ctx->history, "%s\n", msg); - } + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); + line_info_add(self, timefrmt, nick, NULL, msg, IN_MSG, 0, nick_clr); write_to_log(msg, nick, ctx->log, false); } @@ -202,11 +203,10 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p tox_group_peername(m, groupnum, peernum, nick); nick[TOXIC_MAX_NAME_LENGTH] = '\0'; /* enforce client max name length */ - print_time(ctx->history); - wattron(ctx->history, COLOR_PAIR(YELLOW)); - wprintw(ctx->history, "* %s %s\n", nick, action); - wattroff(ctx->history, COLOR_PAIR(YELLOW)); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); + line_info_add(self, timefrmt, nick, NULL, action, ACTION, 0, 0); write_to_log(action, nick, ctx->log, true); } @@ -270,31 +270,21 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu qsort(groupchats[groupnum].peer_names, groupchats[groupnum].num_peers, TOX_MAX_NAME_LENGTH, qsort_strcasecmp_hlpr); ChatContext *ctx = self->chatwin; - print_time(ctx->history); - const uint8_t *event; + uint8_t *event; + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); switch (change) { case TOX_CHAT_CHANGE_PEER_ADD: event = "has joined the room"; - - wattron(ctx->history, COLOR_PAIR(GREEN)); - wattron(ctx->history, A_BOLD); - wprintw(ctx->history, "* %s", peername); - wattroff(ctx->history, A_BOLD); - wprintw(ctx->history, " %s\n", event); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - + line_info_add(self, timefrmt, peername, NULL, event, CONNECTION, 0, GREEN); write_to_log(event, peername, ctx->log, true); break; case TOX_CHAT_CHANGE_PEER_DEL: event = "has left the room"; - - wattron(ctx->history, A_BOLD); - wprintw(ctx->history, "* %s", oldpeername); - wattroff(ctx->history, A_BOLD); - wprintw(ctx->history, " %s\n", event); + line_info_add(self, timefrmt, oldpeername, NULL, event, CONNECTION, 0, 0); if (groupchats[self->num].side_pos > 0) --groupchats[self->num].side_pos; @@ -303,17 +293,8 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu break; case TOX_CHAT_CHANGE_PEER_NAME: - wattron(ctx->history, COLOR_PAIR(MAGENTA)); - wattron(ctx->history, A_BOLD); - wprintw(ctx->history, "* %s", oldpeername); - wattroff(ctx->history, A_BOLD); - - wprintw(ctx->history, " is now known as "); - - wattron(ctx->history, A_BOLD); - wprintw(ctx->history, "%s\n", peername); - wattroff(ctx->history, A_BOLD); - wattroff(ctx->history, COLOR_PAIR(MAGENTA)); + event = " is now known as "; + line_info_add(self, timefrmt, oldpeername, peername, event, NAME_CHANGE, 0, 0); uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32]; snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername); @@ -331,9 +312,8 @@ static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t } if (tox_group_action_send(m, self->num, action, strlen(action) + 1) == -1) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send action\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + uint8_t *errmsg = " * Failed to send action."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); } } @@ -346,6 +326,17 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) getmaxyx(self->window, y2, x2); int cur_len = 0; + if (key == T_KEY_ESC) { /* ESC key: Toggle history scroll mode */ + bool scroll = ctx->hst->scroll_mode ? false : true; + line_info_toggle_scroll(self, scroll); + } + + /* If we're in scroll mode ignore rest of function */ + if (ctx->hst->scroll_mode) { + line_info_onKey(self, key); + return; + } + if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1])); @@ -502,7 +493,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); - wclrtobot(self->window); + if (!string_is_empty(line)) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); @@ -515,7 +506,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) if (strcmp(line, "help global") == 0) execute(ctx->history, self, m, "/help", GLOBAL_COMMAND_MODE); else - print_groupchat_help(ctx); + print_groupchat_help(self); } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { send_group_action(self, ctx, m, line + strlen("/me ")); @@ -524,9 +515,8 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) } } else if (!string_is_empty(line)) { if (tox_group_message_send(m, self->num, line, strlen(line) + 1) == -1) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send message.\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); + uint8_t *errmsg = " * Failed to send message."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); } } @@ -536,13 +526,17 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) static void groupchat_onDraw(ToxWindow *self, Tox *m) { - curs_set(1); + int x2, y2; getmaxyx(self->window, y2, x2); ChatContext *ctx = self->chatwin; + if (!ctx->hst->scroll_mode) + scrollok(ctx->history, 1); + wclear(ctx->linewin); + line_info_print(self); if (ctx->len > 0) { uint8_t line[MAX_STR_SIZE]; @@ -593,22 +587,26 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) getmaxyx(self->window, y, x); ChatContext *ctx = self->chatwin; + ctx->history = subwin(self->window, y-CHATBOX_HEIGHT+1, x-SIDEBAR_WIDTH-1, 0, 0); - scrollok(ctx->history, 1); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0); ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH); + ctx->hst = malloc(sizeof(struct history)); ctx->log = malloc(sizeof(struct chatlog)); - if (ctx->log == NULL) { + if (ctx->log == NULL || ctx->hst == NULL) { endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } + memset(ctx->hst, 0, sizeof(struct history)); memset(ctx->log, 0, sizeof(struct chatlog)); - print_groupchat_help(ctx); + line_info_init(ctx->hst); + + print_groupchat_help(self); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); wmove(self->window, y-CURS_Y_OFFSET, 0); diff --git a/src/line_info.c b/src/line_info.c index f140569..13b8110 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -50,7 +50,7 @@ void line_info_init(struct history *hst) static void line_info_reset_start(struct history *hst) { struct line_info *line = hst->line_end; - uint32_t start_id = hst->start_id; + uint32_t start_id = hst->start_id + 1; while (line) { if (line->id == start_id) { @@ -91,7 +91,7 @@ void line_info_cleanup(struct history *hst) } void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, - uint8_t msgtype, uint8_t bold, uint8_t colour) + uint8_t type, uint8_t bold, uint8_t colour) { struct history *hst = self->chatwin->hst; struct line_info *new_line = malloc(sizeof(struct line_info)); @@ -107,8 +107,9 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na int len = 1; /* there will always be a newline */ /* for type-specific formatting in print function */ - switch (msgtype) { + switch (type) { case ACTION: + case NAME_CHANGE: len += 3; break; default: @@ -131,7 +132,7 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na } new_line->len = len; - new_line->msgtype = msgtype; + new_line->type = type; new_line->bold = bold; new_line->colour = colour; new_line->id = hst->line_end->id + 1; @@ -180,15 +181,19 @@ void line_info_print(ToxWindow *self) WINDOW *win = ctx->history; wclear(win); - wmove(win, 1, 0); int y2, x2; getmaxyx(self->window, y2, x2); - struct line_info *line = ctx->hst->line_start; + if (self->is_groupchat) + wmove(win, 0, 0); + else + wmove(win, 2, 0); + + struct line_info *line = ctx->hst->line_start->next; int numlines = 0; while(line && numlines <= y2) { - uint8_t type = line->msgtype; + uint8_t type = line->type; numlines += line->len / x2; switch (type) { @@ -274,6 +279,25 @@ void line_info_print(ToxWindow *self) wprintw(win, "%s\n", line->msg); wattroff(win, COLOR_PAIR(line->colour)); + break; + + case NAME_CHANGE: + wattron(win, COLOR_PAIR(BLUE)); + wprintw(win, "%s", line->timestamp); + wattroff(win, COLOR_PAIR(BLUE)); + + wattron(win, COLOR_PAIR(MAGENTA)); + wattron(win, A_BOLD); + wprintw(win, "* %s", line->name1); + wattroff(win, A_BOLD); + + wprintw(win, "%s", line->msg); + + wattron(win, A_BOLD); + wprintw(win, "%s\n", line->name2); + wattroff(win, A_BOLD); + wattroff(win, COLOR_PAIR(MAGENTA)); + break; } diff --git a/src/line_info.h b/src/line_info.h index ea2e1e7..faaa159 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -20,7 +20,7 @@ * */ -#define MAX_HISTORY 50 +#define MAX_HISTORY 200 enum { SYS_MSG, @@ -29,6 +29,7 @@ enum { PROMPT, ACTION, CONNECTION, + NAME_CHANGE, } LINE_TYPE; struct line_info { @@ -36,7 +37,7 @@ struct line_info { uint8_t name1[TOXIC_MAX_NAME_LENGTH]; uint8_t name2[TOXIC_MAX_NAME_LENGTH]; uint8_t msg[MAX_STR_SIZE]; - uint8_t msgtype; + uint8_t type; uint8_t bold; uint8_t colour; uint32_t id; @@ -57,7 +58,7 @@ struct history { }; void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg, - uint8_t msgtype, uint8_t bold, uint8_t colour); + uint8_t type, uint8_t bold, uint8_t colour); void line_info_cleanup(struct history *hst); void line_info_toggle_scroll(ToxWindow *self, bool scroll); void line_info_init(struct history *hst); diff --git a/src/misc_tools.c b/src/misc_tools.c index 658e105..0363f1d 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -55,17 +55,6 @@ struct tm *get_time(void) return timeinfo; } -/* Prints the time to given window */ -void print_time(WINDOW *window) -{ - uint8_t s[MAX_STR_SIZE]; - strftime(s, MAX_STR_SIZE, "[%H:%M:%S] ", get_time()); - - wattron(window, COLOR_PAIR(BLUE)); - wprintw(window, "%s", s); - wattroff(window,COLOR_PAIR(BLUE)); -} - void get_time_str(uint8_t *buf) { strftime(buf, TIME_STR_SIZE, "[%H:%M:%S] ", get_time()); diff --git a/src/misc_tools.h b/src/misc_tools.h index e6af8e9..b5b2a93 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -29,8 +29,8 @@ unsigned char *hex_string_to_bin(char hex_string[]); /* get the current unix time */ uint64_t get_unix_time(void); -/* Prints the time to given window */ -void print_time(WINDOW *window); +/*Puts the current time in buf in the format of [Hour:Min:Sec] */ +void get_time_str(uint8_t *buf); /* get the current local time */ struct tm *get_time(void); diff --git a/src/prompt.c b/src/prompt.c index 1a2356e..f09930e 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -215,7 +215,6 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) int k = ctx->orig_y + ((ctx->len + p_ofst) / px2); if (k >= y2) { - wprintw(ctx->history, "\n"); --ctx->orig_y; } } @@ -403,24 +402,25 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, uint8_t *key, uint8_ data[length - 1] = 0; ChatContext *ctx = self->chatwin; - wprintw(ctx->history, "\n"); - print_time(ctx->history); + uint8_t timefrmt[TIME_STR_SIZE]; + get_time_str(timefrmt); uint8_t msg[MAX_STR_SIZE]; - snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data); - wprintw(ctx->history, "%s", msg); + snprintf(msg, sizeof(msg), "Friend request with the message '%s'", data); + line_info_add(self, timefrmt, NULL, NULL, msg, SYS_MSG, 0, 0); write_to_log(msg, "", ctx->log, true); int n = add_friend_request(key); if (n == -1) { - const uint8_t *errmsg = "Friend request queue is full. Discarding request.\n"; - wprintw(ctx->history, "%s", errmsg); + uint8_t *errmsg = "Friend request queue is full. Discarding request."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); write_to_log(errmsg, "", ctx->log, true); return; } - wprintw(ctx->history, "Type \"/accept %d\" to accept it.\n", n); + snprintf(msg, sizeof(msg), "Type \"/accept %d\" to accept it.", n); + line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); alert_window(self, WINDOW_ALERT_1, true); } From 3baa830afb085941da4f759f0e0996ed60000693 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Mar 2014 08:25:10 -0400 Subject: [PATCH 6/8] move log struct to proper place --- src/global_commands.c | 1 + src/log.c | 1 + src/log.h | 9 +++++++++ src/prompt.c | 1 + src/toxic_windows.h | 9 --------- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/global_commands.c b/src/global_commands.c index 44e10af..a4260ef 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -30,6 +30,7 @@ #include "toxic_windows.h" #include "misc_tools.h" #include "friendlist.h" +#include "log.h" #include "line_info.h" extern char *DATA_FILE; diff --git a/src/log.c b/src/log.c index c86f804..72c7cdb 100644 --- a/src/log.c +++ b/src/log.c @@ -31,6 +31,7 @@ #include "configdir.h" #include "toxic_windows.h" #include "misc_tools.h" +#include "log.h" /* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) diff --git a/src/log.h b/src/log.h index 510a429..9473eeb 100644 --- a/src/log.h +++ b/src/log.h @@ -20,6 +20,15 @@ * */ +#define LOG_FLUSH_LIMIT 2 /* limits calls to fflush(logfile) to a max of one per LOG_FLUSH_LIMIT seconds */ + +struct chatlog { + FILE *file; + uint64_t lastwrite; + int pos; + bool log_on; /* specific to current chat window */ +}; + /* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log); diff --git a/src/prompt.c b/src/prompt.c index f09930e..d525a9c 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -32,6 +32,7 @@ #include "execute.h" #include "misc_tools.h" #include "toxic_strings.h" +#include "log.h" #include "line_info.h" uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE] = {0}; diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ef941aa..3d727db 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -165,15 +165,6 @@ struct StatusBar { bool is_online; }; -#define LOG_FLUSH_LIMIT 2 /* limits calls to fflush(logfile) to a max of one per LOG_FLUSH_LIMIT seconds */ - -struct chatlog { - FILE *file; - uint64_t lastwrite; - int pos; - bool log_on; /* specific to current chat window */ -}; - #define MAX_LINE_HIST 128 /* chat and groupchat window/buffer holder */ From 29b549e677e6314056c47edadb2e9a816fac3367 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Mar 2014 21:43:49 -0400 Subject: [PATCH 7/8] few fixes --- src/global_commands.c | 6 ++---- src/line_info.c | 26 ++++++++++++++++++++------ src/line_info.h | 19 ++++++++++++++++--- src/prompt.c | 7 +------ src/toxic_windows.h | 1 - 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/global_commands.c b/src/global_commands.c index a4260ef..f048ccd 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -182,9 +182,7 @@ void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX void cmd_clear(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - struct history *hst = self->chatwin->hst; - line_info_cleanup(hst); - line_info_init(hst); + line_info_clear(self->chatwin->hst); } void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -270,7 +268,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX } else if (self->is_prompt) { uint8_t myid[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(m, myid); - log_enable(self->name, &myid, log); + log_enable(self->name, myid, log); } else if (self->is_groupchat) { log_enable(self->name, NULL, log); } diff --git a/src/line_info.c b/src/line_info.c index 13b8110..7177408 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -30,6 +30,7 @@ #include "toxic_windows.h" #include "line_info.h" +#include "groupchat.h" void line_info_init(struct history *hst) { @@ -156,19 +157,27 @@ void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *na hst->line_root = tmp; } + int newlines = 0; + int i; + + for (i = 0; msg[i]; ++i) { + if (msg[i] == '\n') + ++newlines; + } + int y, y2, x, x2; getmaxyx(self->window, y2, x2); getyx(self->chatwin->history, y, x); - int n = self->is_prompt ? 0 : CHATBOX_HEIGHT; + int offst = self->is_groupchat ? SIDEBAR_WIDTH : 0; /* offset width of groupchat sidebar */ + int lines = 1 + (len / (x2 - offst)); - /* move line_start forward proportionate to the number of new rows */ - if (y >= y2 - n) { - int i; - int lines = 1 + (len / x2); + int max_y = self->is_prompt ? y2 : y2 - CHATBOX_HEIGHT; + /* move line_start forward proportionate to the number of new lines */ + if (y >= max_y) { while (lines > 0 && hst->line_start->next) { - lines -= (1 + hst->line_start->len / x2); + lines -= (1 + hst->line_start->len / (x2 - offst)); hst->line_start = hst->line_start->next; ++hst->start_id; } @@ -371,3 +380,8 @@ void line_info_onKey(ToxWindow *self, wint_t key) break; } } + +void line_info_clear(struct history *hst) +{ + hst->line_start = hst->line_end; +} diff --git a/src/line_info.h b/src/line_info.h index faaa159..0929e85 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -55,12 +55,25 @@ struct history { uint32_t start_id; /* keeps track of where line_start should be when at bottom of history */ uint32_t line_items; bool scroll_mode; + int y; + int old_y; }; +/* adds a line to history (also moves line_start and/or line_root forward if necessary) */ 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); -void line_info_cleanup(struct history *hst); -void line_info_toggle_scroll(ToxWindow *self, bool scroll); -void line_info_init(struct history *hst); + +/* Prints a section of history starting at line_start */ void line_info_print(ToxWindow *self); + +/* frees all history lines */ +void line_info_cleanup(struct history *hst); + +/* Toggles scroll mode for current window */ +void line_info_toggle_scroll(ToxWindow *self, bool scroll); + +/* clears the screen (does not delete anything) */ +void line_info_clear(struct history *hst); + +void line_info_init(struct history *hst); void line_info_onKey(ToxWindow *self, wint_t key); diff --git a/src/prompt.c b/src/prompt.c index d525a9c..b528e43 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -144,7 +144,6 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) if (ctx->pos > 0) { del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len); wmove(ctx->history, y, x-1); /* not necessary but fixes a display glitch */ - ctx->scroll = false; } else { beep(); } @@ -153,7 +152,6 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_DC) { /* DEL key: Remove character at pos */ if (ctx->pos != ctx->len) { del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len); - ctx->scroll = false; } else { beep(); } @@ -246,7 +244,6 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) { if (ctx->len < (MAX_STR_SIZE-1)) { add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key); - ctx->scroll = true; } } /* RETURN key: execute command */ @@ -303,10 +300,8 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) bool edge = (ctx->len + p_ofst) % px2 == 0; /* move point of line origin up when input scrolls screen down */ - if (ctx->scroll && edge && botm) { + if (edge && botm) --ctx->orig_y; - ctx->scroll = false; - } } else { /* Mark point of origin for new line */ ctx->orig_y = y; diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 3d727db..641d49c 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -189,7 +189,6 @@ struct ChatContext { /* specific for prompt */ bool at_bottom; /* true if line end is at bottom of window */ int orig_y; /* y axis point of line origin */ - bool scroll; /* used for prompt window hack to determine when to scroll down */ }; /* Start file transfer code */ From bb97f3e54300b8b37d8cf5cb0bf8361f708e7d06 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Mar 2014 22:10:05 -0400 Subject: [PATCH 8/8] merge --- src/audio_call.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/audio_call.c b/src/audio_call.c index 85158b9..c4a69a2 100644 --- a/src/audio_call.c +++ b/src/audio_call.c @@ -65,7 +65,7 @@ void callback_requ_timeout ( void *arg ); void callback_peer_timeout ( void* arg ); -static void print_err (ToxWindow *self, char *error_str) +static void print_err (ToxWindow *self, uint8_t *error_str) { line_info_add(self, NULL, NULL, NULL, error_str, SYS_MSG, 0, 0); }