1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-27 00:03:26 +01:00

Merge pull request #28 from JFreegman/master

Added a statusbar to chat windows and removed spammy messages
This commit is contained in:
JFreegman 2013-09-06 16:49:05 -07:00
commit 333643d655
6 changed files with 129 additions and 101 deletions

View File

@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script. # Process this file with autoconf to produce a configure script.
AC_PREREQ([2.65]) AC_PREREQ([2.65])
AC_INIT([toxic], [0.1.1], [http://tox.im/]) AC_INIT([toxic], [0.2.0], [http://tox.im/])
AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_AUX_DIR(configure_aux)
AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h]) AC_CONFIG_HEADERS([config.h])

View File

@ -9,6 +9,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include <ctype.h> #include <ctype.h>
#include <time.h> #include <time.h>
#include <limits.h> #include <limits.h>
@ -23,13 +24,20 @@ extern char *DATA_FILE;
extern int store_data(Tox *m, char *path); extern int store_data(Tox *m, char *path);
typedef struct { typedef struct {
int friendnum;
wchar_t line[MAX_STR_SIZE]; wchar_t line[MAX_STR_SIZE];
size_t pos; size_t pos;
WINDOW *history; WINDOW *history;
WINDOW *linewin; WINDOW *linewin;
} ChatContext; } ChatContext;
typedef struct {
WINDOW *topline;
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
TOX_USERSTATUS status;
bool is_online;
int max_len; /* set equal to the window's max x coordinate */
} StatusBar;
void print_help(ChatContext *self); void print_help(ChatContext *self);
void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd); void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd);
@ -44,16 +52,15 @@ struct tm *get_time(void)
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
{ {
ChatContext *ctx = (ChatContext *) self->x; if (self->friendnum != num)
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return; return;
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_getname(m, num, (uint8_t *) &nick); tox_getname(m, num, (uint8_t *) &nick);
msg[len-1] = '\0';
nick[TOX_MAX_NAME_LENGTH-1] = '\0';
wattron(ctx->history, COLOR_PAIR(CYAN)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
@ -69,35 +76,24 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{ {
ChatContext *ctx = (ChatContext *) self->x; if (self->friendnum != num)
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return; return;
wattron(ctx->history, COLOR_PAIR(CYAN)); StatusBar *statusbar = (StatusBar *) self->s;
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); statusbar->is_online = status == 1 ? true : false;
wattroff(ctx->history, COLOR_PAIR(CYAN));
if (status == 1)
wprintw(ctx->history, "* Chat partner has come online\n");
else
wprintw(ctx->history, "* Chat partner went offline\n");
} }
static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len)
{ {
if (self->friendnum != num)
return;
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
if (ctx->friendnum != num) uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
return;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
tox_getname(m, num, (uint8_t *) &nick); tox_getname(m, num, (uint8_t *) &nick);
action[len - 1] = '\0';
wattron(ctx->history, COLOR_PAIR(CYAN)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN)); wattroff(ctx->history, COLOR_PAIR(CYAN));
@ -112,70 +108,30 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin
static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
{ {
ChatContext *ctx = (ChatContext *) self->x; if (self->friendnum != num)
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return; return;
wattron(ctx->history, COLOR_PAIR(CYAN)); snprintf(self->name, sizeof(self->name), "%s", nick);
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN));
nick[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s]", nick);
wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick);
} }
static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
{ {
ChatContext *ctx = (ChatContext *) self->x; if (self->friendnum != num)
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return; return;
char *status_msg = NULL; StatusBar *statusbar = (StatusBar *) self->s;
int colour = 0; statusbar->status = status;
if (status == TOX_USERSTATUS_BUSY) {
status_msg = "[Busy]";
colour = RED;
}
else if (status == TOX_USERSTATUS_AWAY) {
status_msg = "[Away]";
colour = YELLOW;
}
if (status_msg != NULL) {
wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "* Chat partner set status to: ");
wattron(ctx->history, COLOR_PAIR(colour) | A_BOLD);
wprintw(ctx->history, "%s\n", status_msg);
wattroff(ctx->history, COLOR_PAIR(colour) | A_BOLD);
}
} }
static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{ {
ChatContext *ctx = (ChatContext *) self->x; if (self->friendnum != num)
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return; return;
status[len - 1] = '\0'; StatusBar *statusbar = (StatusBar *) self->s;
if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */ if (strncmp(status, "Online", strlen(status))) /* Ignore default "Online" message */
wattron(ctx->history, COLOR_PAIR(CYAN)); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", status);
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status);
}
} }
/* check that the string has one non-space character */ /* check that the string has one non-space character */
@ -227,6 +183,8 @@ static char *wc_to_char(wchar_t ch)
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
{ {
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
StatusBar *statusbar = (StatusBar *) self->s;
struct tm *timeinfo = get_time(); struct tm *timeinfo = get_time();
int x, y, y2, x2; int x, y, y2, x2;
@ -239,7 +197,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
#else #else
if (isprint(key)) { if (isprint(key)) {
#endif #endif
if (ctx->pos < MAX_STR_SIZE) { if (ctx->pos < (MAX_STR_SIZE-1)) {
mvwaddstr(self->window, y, x, wc_to_char(key)); mvwaddstr(self->window, y, x, wc_to_char(key));
ctx->line[ctx->pos++] = key; ctx->line[ctx->pos++] = key;
ctx->line[ctx->pos] = L'\0'; ctx->line[ctx->pos] = L'\0';
@ -268,8 +226,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
if (line[0] == '/') { if (line[0] == '/') {
if (close_win = !strncmp(line, "/close", strlen("/close"))) { if (close_win = !strncmp(line, "/close", strlen("/close"))) {
int f_num = ctx->friendnum; int f_num = self->friendnum;
delwin(ctx->linewin); delwin(ctx->linewin);
delwin(statusbar->topline);
del_window(self); del_window(self);
disable_chatwin(f_num); disable_chatwin(f_num);
} else { } else {
@ -289,7 +248,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
wattroff(ctx->history, COLOR_PAIR(GREEN)); wattroff(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s\n", line); wprintw(ctx->history, "%s\n", line);
if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { if (tox_sendmessage(m, self->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(RED)); wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send message.\n"); wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(RED)); wattroff(ctx->history, COLOR_PAIR(RED));
@ -297,9 +256,10 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
} }
} }
if (close_win) if (close_win) {
free(ctx); free(ctx);
else { free(statusbar);
} else {
ctx->line[0] = L'\0'; ctx->line[0] = L'\0';
ctx->pos = 0; ctx->pos = 0;
} }
@ -313,6 +273,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window); wclear(self->window);
wclear(ctx->history); wclear(ctx->history);
wprintw(ctx->history, "\n\n");
int x, y; int x, y;
getmaxyx(self->window, y, x); getmaxyx(self->window, y, x);
(void) x; (void) x;
@ -351,7 +312,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
wprintw(ctx->history, "* %s %s\n", selfname, action); wprintw(ctx->history, "* %s %s\n", selfname, action);
wattroff(ctx->history, COLOR_PAIR(YELLOW)); wattroff(ctx->history, COLOR_PAIR(YELLOW));
if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { if (tox_sendaction(m, self->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(RED)); wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send action\n"); wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(RED)); wattroff(ctx->history, COLOR_PAIR(RED));
@ -432,7 +393,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
} }
else if (!strcmp(cmd, "/myid")) { else if (!strcmp(cmd, "/myid")) {
char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {'\0'};
int i; int i;
uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
tox_getaddress(m, address); tox_getaddress(m, address);
@ -453,9 +414,55 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
static void chat_onDraw(ToxWindow *self, Tox *m) static void chat_onDraw(ToxWindow *self, Tox *m)
{ {
curs_set(1); curs_set(1);
int x, y; int x, y;
getmaxyx(self->window, y, x); getmaxyx(self->window, y, x);
(void) y;
/* Draw status bar */
StatusBar *statusbar = (StatusBar *) self->s;
mvwhline(statusbar->topline, 1, 0, '-', x);
wmove(statusbar->topline, 0, 0);
/* Draw name, status and note in statusbar */
if (statusbar->is_online) {
char *status_text = "Unknown";
int colour = WHITE;
TOX_USERSTATUS status = statusbar->status;
switch(status) {
case TOX_USERSTATUS_NONE:
status_text = "Online";
colour = GREEN;
break;
case TOX_USERSTATUS_AWAY:
status_text = "Away";
colour = YELLOW;
break;
case TOX_USERSTATUS_BUSY:
status_text = "Busy";
colour = RED;
break;
}
wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " %s ", self->name);
wattroff(statusbar->topline, A_BOLD);
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
wprintw(statusbar->topline, "[%s]", status_text);
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
} else {
wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " %s ", self->name);
wattroff(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, "[Offline]");
}
if (statusbar->statusmsg[0])
wprintw(statusbar->topline, " | %s", statusbar->statusmsg);
wprintw(statusbar->topline, "\n");
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
mvwhline(ctx->linewin, 0, 0, '_', x); mvwhline(ctx->linewin, 0, 0, '_', x);
wrefresh(self->window); wrefresh(self->window);
@ -464,11 +471,26 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
static void chat_onInit(ToxWindow *self, Tox *m) static void chat_onInit(ToxWindow *self, Tox *m)
{ {
int x, y; int x, y;
ChatContext *ctx = (ChatContext *) self->x;
getmaxyx(self->window, y, x); getmaxyx(self->window, y, x);
ctx->history = subwin(self->window, y - 4, x, 0, 0);
/* Init statusbar info */
StatusBar *statusbar = (StatusBar *) self->s;
statusbar->status = tox_get_userstatus(m, self->friendnum);
statusbar->is_online = tox_friendstatus(m, self->friendnum) == TOX_FRIEND_ONLINE;
statusbar->max_len = x;
char statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
if (strncmp(statusmsg, "Online", strlen(statusmsg)))
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
/* Init subwindows */
ChatContext *ctx = (ChatContext *) self->x;
statusbar->topline = subwin(self->window, 2, x, 0, 0);
ctx->history = subwin(self->window, y-3, x, 0, 0);
scrollok(ctx->history, 1); scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, 2, x, y - 4, 0); ctx->linewin = subwin(self->window, 0, x, y-4, 0);
wprintw(ctx->history, "\n\n");
print_help(ctx); print_help(ctx);
wmove(self->window, y - CURS_Y_OFFSET, 0); wmove(self->window, y - CURS_Y_OFFSET, 0);
} }
@ -507,13 +529,15 @@ ToxWindow new_chat(Tox *m, int friendnum)
ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onStatusMessageChange = &chat_onStatusMessageChange;
ret.onAction = &chat_onAction; ret.onAction = &chat_onAction;
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_getname(m, friendnum, (uint8_t *) &nick); tox_getname(m, friendnum, (uint8_t *) &name);
snprintf(ret.name, sizeof(ret.name), "%s", name);
snprintf(ret.title, sizeof(ret.title), "[%s]", nick);
ChatContext *x = calloc(1, sizeof(ChatContext)); ChatContext *x = calloc(1, sizeof(ChatContext));
StatusBar *s = calloc(1, sizeof(StatusBar));
ret.x = x; ret.x = x;
x->friendnum = friendnum; ret.s = s;
ret.friendnum = friendnum;
return ret; return ret;
} }

View File

@ -124,7 +124,8 @@ static void select_friend(Tox *m, wint_t key)
} }
} }
} else if (key == KEY_DOWN) { } else if (key == KEY_DOWN) {
while ((n = (n + 1) % num_friends) != num_selected) { while (++n != num_selected) {
n = n % num_friends;
if (friends[n].active) { if (friends[n].active) {
num_selected = n; num_selected = n;
return; return;
@ -166,6 +167,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
set_active_window(friends[num_selected].chatwin); set_active_window(friends[num_selected].chatwin);
} else { } else {
friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num)); friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num));
set_active_window(friends[num_selected].chatwin);
} }
} else if (key == 0x107 || key == 0x8 || key == 0x7f) } else if (key == 0x107 || key == 0x8 || key == 0x7f)
delete_friend(m, self, num_selected, key); delete_friend(m, self, num_selected, key);
@ -214,14 +216,14 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "O"); wprintw(self->window, "O");
wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "] %s", friends[i].name); wprintw(self->window, "]%s", friends[i].name);
if (friends[i].statusmsg[0]) if (friends[i].statusmsg[0])
wprintw(self->window, " (%s)\n", friends[i].statusmsg); wprintw(self->window, " (%s)\n", friends[i].statusmsg);
else else
wprintw(self->window, "\n"); wprintw(self->window, "\n");
} else { } else {
wprintw(self->window, "[O] %s\n", friends[i].name); wprintw(self->window, "[O]%s\n", friends[i].name);
} }
} }
} }
@ -254,6 +256,6 @@ ToxWindow new_friendlist()
ret.onStatusChange = &friendlist_onStatusChange; ret.onStatusChange = &friendlist_onStatusChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
strcpy(ret.title, "[friends]"); strcpy(ret.name, "friends");
return ret; return ret;
} }

View File

@ -565,6 +565,6 @@ ToxWindow new_prompt()
ret.onKey = &prompt_onKey; ret.onKey = &prompt_onKey;
ret.onDraw = &prompt_onDraw; ret.onDraw = &prompt_onDraw;
ret.onInit = &prompt_onInit; ret.onInit = &prompt_onInit;
strcpy(ret.title, "[prompt]"); strcpy(ret.name, "prompt");
return ret; return ret;
} }

View File

@ -47,9 +47,13 @@ struct ToxWindow_ {
void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS);
void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
char title[256];
char name[TOX_MAX_NAME_LENGTH];
int friendnum;
void *x; void *x;
void *s;
bool blink; bool blink;
WINDOW *window; WINDOW *window;

View File

@ -58,7 +58,6 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat
wprintw(prompt->window, "\n%s ", nick); wprintw(prompt->window, "\n%s ", nick);
wattroff(prompt->window, A_BOLD); wattroff(prompt->window, A_BOLD);
wprintw(prompt->window, "has gone offline\n"); wprintw(prompt->window, "has gone offline\n");
} }
int i; int i;
@ -147,7 +146,6 @@ int add_window(Tox *m, ToxWindow w)
windows[i] = w; windows[i] = w;
w.onInit(&w, m); w.onInit(&w, m);
active_window = windows + i;
return i; return i;
} }
@ -241,7 +239,7 @@ static void draw_bar()
attron(COLOR_PAIR(RED)); attron(COLOR_PAIR(RED));
clrtoeol(); clrtoeol();
printw(" %s", windows[i].title); printw(" [%s]", windows[i].name);
if (windows[i].blink && (odd < (blinkrate / 2))) if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(RED)); attroff(COLOR_PAIR(RED));