From a40b6b1b1b83c146e916452e98a22b1ccbcb6307 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 24 Mar 2014 07:37:52 -0400 Subject: [PATCH] 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 */