1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-06-26 20:37:46 +02:00
toxic/src/line_info.c

452 lines
11 KiB
C
Raw Normal View History

/* 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 <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "toxic_windows.h"
#include "line_info.h"
2014-03-26 02:43:49 +01:00
#include "groupchat.h"
2014-05-26 01:54:34 +02:00
#include "settings.h"
extern struct user_settings *user_settings;
void line_info_init(struct history *hst)
{
hst->line_root = malloc(sizeof(struct line_info));
if (hst->line_root == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
2014-03-25 08:17:22 +01:00
memset(hst->line_root, 0, sizeof(struct line_info));
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;
2014-03-26 10:52:21 +01:00
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)
{
2014-03-25 08:17:22 +01:00
WINDOW *win = self->chatwin->history;
struct history *hst = self->chatwin->hst;
if (scroll) {
hst->scroll_mode = true;
scrollok(win, 0);
curs_set(0);
} else {
hst->scroll_mode = false;
scrollok(win, 1);
curs_set(1);
line_info_reset_start(hst);
}
}
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;
}
}
2014-04-01 03:26:09 +02:00
/* moves root forward and frees previous root */
static void line_info_root_fwd(struct history *hst)
{
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;
++hst->start_id;
}
free(hst->line_root);
hst->line_root = tmp;
}
void line_info_add(ToxWindow *self, uint8_t *tmstmp, uint8_t *name1, uint8_t *name2, uint8_t *msg,
2014-03-25 13:21:50 +01:00
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));
if (new_line == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
2014-03-25 08:17:22 +01:00
memset(new_line, 0, sizeof(struct line_info));
int len = 1; /* there will always be a newline */
/* for type-specific formatting in print function */
2014-03-25 13:21:50 +01:00
switch (type) {
case ACTION:
len += 3;
break;
default:
len += 2;
break;
}
if (msg) {
strcpy(new_line->msg, msg);
len += strlen(msg);
}
if (tmstmp) {
strcpy(new_line->timestamp, tmstmp);
len += strlen(tmstmp);
}
if (name1) {
strcpy(new_line->name1, name1);
len += strlen(name1);
}
if (name2) {
strcpy(new_line->name2, name2);
len += strlen(name2);
}
new_line->len = len;
2014-03-25 13:21:50 +01:00
new_line->type = type;
new_line->bold = bold;
new_line->colour = colour;
new_line->id = hst->line_end->id + 1;
new_line->prev = hst->line_end;
hst->line_end->next = new_line;
hst->line_end = new_line;
2014-05-26 01:54:34 +02:00
if (++hst->line_items > user_settings->history_size) {
2014-04-01 03:26:09 +02:00
--hst->line_items;
line_info_root_fwd(hst);
}
2014-03-26 02:43:49 +01:00
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);
2014-03-25 08:17:22 +01:00
getyx(self->chatwin->history, y, x);
2014-03-27 00:14:28 +01:00
if (x2 <= SIDEBAR_WIDTH)
return;
2014-03-26 02:43:49 +01:00
int offst = self->is_groupchat ? SIDEBAR_WIDTH : 0; /* offset width of groupchat sidebar */
2014-03-26 10:52:21 +01:00
int lines = (1 + newlines + (len / (x2 - offst)));
2014-03-27 00:14:28 +01:00
hst->queue_lns += lines;
++hst->queue;
2014-03-26 02:43:49 +01:00
int max_y = self->is_prompt ? y2 : y2 - CHATBOX_HEIGHT;
2014-03-26 02:43:49 +01:00
/* move line_start forward proportionate to the number of new lines */
2014-03-27 00:14:28 +01:00
if (y + hst->queue_lns - hst->queue >= max_y) {
2014-04-01 03:26:09 +02:00
while (lines > 0) {
++hst->start_id;
lines -= (1 + hst->line_start->len / (x2 - offst));
2014-04-01 03:26:09 +02:00
if (!hst->scroll_mode && hst->line_start->next)
hst->line_start = hst->line_start->next;
}
}
}
void line_info_print(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
2014-03-25 08:17:22 +01:00
WINDOW *win = ctx->history;
ctx->hst->queue = 0;
ctx->hst->queue_lns = 0;
wclear(win);
int y2, x2;
getmaxyx(self->window, y2, x2);
2014-03-30 03:16:25 +02:00
if (self->is_prompt)
2014-05-26 01:54:34 +02:00
y2 = user_settings->history_size; /* temporary fix to make prompt scroll */
2014-03-30 03:16:25 +02:00
2014-03-27 00:14:28 +01:00
if (x2 <= SIDEBAR_WIDTH)
return;
2014-03-25 13:21:50 +01:00
if (self->is_groupchat)
wmove(win, 0, 0);
else
wmove(win, 2, 0);
struct line_info *line = ctx->hst->line_start->next;
2014-03-27 00:14:28 +01:00
int offst = self->is_groupchat ? SIDEBAR_WIDTH : 0;
int numlines = 0;
while (line && numlines++ <= y2) {
2014-03-25 13:21:50 +01:00
uint8_t type = line->type;
switch (type) {
case OUT_MSG:
case IN_MSG:
wattron(win, COLOR_PAIR(BLUE));
wprintw(win, "%s", line->timestamp);
wattroff(win, COLOR_PAIR(BLUE));
int nameclr = GREEN;
if (line->colour)
nameclr = line->colour;
else if (type == IN_MSG)
nameclr = CYAN;
wattron(win, COLOR_PAIR(nameclr));
wprintw(win, "%s: ", line->name1);
wattroff(win, COLOR_PAIR(nameclr));
if (line->msg[0] == '>')
wattron(win, COLOR_PAIR(GREEN));
wprintw(win, "%s\n", line->msg);
if (line->msg[0] == '>')
wattroff(win, COLOR_PAIR(GREEN));
break;
case ACTION:
wattron(win, COLOR_PAIR(BLUE));
wprintw(win, "%s", line->timestamp);
wattroff(win, COLOR_PAIR(BLUE));
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(win, COLOR_PAIR(BLUE));
wprintw(win, "%s", line->timestamp);
wattroff(win, COLOR_PAIR(BLUE));
}
if (line->bold)
wattron(win, A_BOLD);
2014-03-25 08:17:22 +01:00
if (line->colour)
wattron(win, COLOR_PAIR(line->colour));
2014-03-25 08:17:22 +01:00
wprintw(win, "%s\n", line->msg);
2014-03-25 08:17:22 +01:00
if (line->bold)
wattroff(win, A_BOLD);
2014-03-25 08:17:22 +01:00
if (line->colour)
wattroff(win, COLOR_PAIR(line->colour));
2014-03-25 08:17:22 +01:00
break;
2014-03-25 08:17:22 +01:00
case PROMPT:
wattron(win, COLOR_PAIR(GREEN));
wprintw(win, "$ ");
wattroff(win, COLOR_PAIR(GREEN));
if (line->msg[0])
wprintw(win, "%s", line->msg);
2014-03-25 13:21:50 +01:00
wprintw(win, "\n");
break;
2014-03-25 13:21:50 +01:00
case CONNECTION:
wattron(win, COLOR_PAIR(BLUE));
wprintw(win, "%s", line->timestamp);
wattroff(win, COLOR_PAIR(BLUE));
2014-03-25 13:21:50 +01:00
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));
2014-03-25 13:21:50 +01:00
break;
2014-03-25 13:21:50 +01:00
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;
}
line = line->next;
}
}
void line_info_set(ToxWindow *self, uint32_t id, uint8_t *msg)
{
struct line_info *line = self->chatwin->hst->line_end;
while (line) {
if (line->id == id) {
snprintf(line->msg, sizeof(line->msg), "%s", msg);
return;
}
line = line->prev;
}
}
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;
}
}
2014-03-26 02:43:49 +01:00
void line_info_onDraw(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
wattron(ctx->linewin, A_BOLD | COLOR_PAIR(BLUE));
mvwprintw(ctx->linewin, 1, 0, "Scroll mode:\n");
wattroff(ctx->linewin, A_BOLD | COLOR_PAIR(BLUE));
mvwprintw(ctx->linewin, 1, 13, "Use up/down arrows, page up/page down, and home/end to navigate.\n"
" ESC to exit.\n");
}
2014-03-26 02:43:49 +01:00
void line_info_clear(struct history *hst)
{
hst->line_start = hst->line_end;
2014-03-27 00:14:28 +01:00
hst->start_id = hst->line_start->id;
2014-03-26 02:43:49 +01:00
}