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

added ability to close and reopen chat windows & other minor changes

This commit is contained in:
Jfreegman 2013-08-05 01:57:29 -04:00
parent 19efe3727a
commit 765a722c90
5 changed files with 150 additions and 75 deletions

15
chat.c
View File

@ -25,6 +25,8 @@ typedef struct {
} ChatContext;
extern void del_window(ToxWindow *w, int f_num);
extern int focus_window(int num);
extern void fix_name(uint8_t* name);
void print_help(ChatContext* self);
void execute(ToxWindow* self, ChatContext* ctx, char* cmd);
@ -50,7 +52,7 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
fix_name(nick);
wattron(ctx->history, COLOR_PAIR(2));
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(2));
wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick);
@ -116,7 +118,7 @@ static void chat_onKey(ToxWindow* self, int key) {
if(!string_is_empty(ctx->line)) {
/* make sure the string has at least non-space character */
wattron(ctx->history, COLOR_PAIR(2));
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(2));
wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "you: ", ctx->line);
@ -185,6 +187,12 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
}
wprintw(ctx->history, "Your ID: %s\n", id);
}
else if (strcmp(ctx->line, "/close") == 0) {
focus_window(0); // Go to prompt screen
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self, f_num);
}
else
wprintw(ctx->history, "Invalid command.\n");
}
@ -256,6 +264,5 @@ ToxWindow new_chat(int friendnum) {
x->friendnum = friendnum;
ret.x = (void*) x;
free(x);
return ret;
}
}

View File

@ -12,12 +12,11 @@
#include "windows.h"
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
extern int add_window(ToxWindow w);
extern int focus_window(int num);
extern ToxWindow new_chat(int friendnum);
#define MAX_FRIENDS_NUM 100
typedef struct {
uint8_t name[MAX_NAME_LENGTH];
uint8_t status[MAX_USERSTATUS_LENGTH];
@ -86,14 +85,11 @@ int friendlist_onFriendAdded(int num) {
getname(num, friends[num_friends].name);
strcpy((char*) friends[num_friends].name, "unknown");
strcpy((char*) friends[num_friends].status, "unknown");
friends[num_friends].chatwin = -1;
num_friends++;
friends[num_friends++].chatwin = -1;
return 0;
}
static void friendlist_onKey(ToxWindow* self, int key) {
if(key == KEY_UP) {
if(num_selected != 0)
num_selected--;
@ -103,12 +99,26 @@ static void friendlist_onKey(ToxWindow* self, int key) {
num_selected = (num_selected+1) % num_friends;
}
else if(key == '\n') {
if(friends[num_selected].chatwin != -1)
return;
friends[num_selected].chatwin = add_window(new_chat(num_selected));
focus_window(friends[num_selected].chatwin);
/* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) {
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
if (WINDOW_STATUS[i] == num_selected) {
focus_window(i);
break;
}
}
}else {
friends[num_selected].chatwin = add_window(new_chat(num_selected));
focus_window(friends[num_selected].chatwin);
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { // Find open slot
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected;
break;
}
}
}
}
}
@ -145,6 +155,10 @@ static void friendlist_onDraw(ToxWindow* self) {
wrefresh(self->window);
}
void disable_chatwin(int f_num) {
friends[f_num].chatwin = -1;
}
static void friendlist_onInit(ToxWindow* self) {
}
@ -164,4 +178,4 @@ ToxWindow new_friendlist() {
strcpy(ret.title, "[friends]");
return ret;
}
}

157
main.c
View File

@ -17,13 +17,12 @@ extern ToxWindow new_prompt();
extern ToxWindow new_friendlist();
extern int friendlist_onFriendAdded(int num);
extern void disable_chatwin(int f_num);
extern int add_req(uint8_t* public_key); // XXX
#define TOXWINDOWS_MAX_NUM 32
static ToxWindow windows[TOXWINDOWS_MAX_NUM];
static int w_num;
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows
static ToxWindow windows[MAX_WINDOW_SLOTS];
int w_num;
static int w_active;
static ToxWindow* prompt;
@ -115,6 +114,16 @@ static void init_tox() {
m_callback_userstatus(on_statuschange);
}
void init_window_status() {
int i;
for (i = 0; i < N_DEFAULT_WINS; i++)
WINDOW_STATUS[i] = i;
int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1;
}
int add_window(ToxWindow w) {
if(w_num == TOXWINDOWS_MAX_NUM)
return -1;
@ -133,6 +142,21 @@ int add_window(ToxWindow w) {
return w_num - 1;
}
/* Deletes window w and cleans up */
void del_window(ToxWindow *w, int f_num) {
delwin(w->window);
int i;
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) {
if (WINDOW_STATUS[i] == f_num) {
WINDOW_STATUS[i] = -1;
disable_chatwin(f_num);
break;
}
}
clear();
refresh();
}
int focus_window(int num) {
if(num >= w_num || num < 0)
return -1;
@ -143,7 +167,6 @@ int focus_window(int num) {
static void init_windows() {
w_num = 0;
w_active = 0;
if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) {
fprintf(stderr, "add_window() failed.\n");
@ -151,7 +174,6 @@ static void init_windows() {
endwin();
exit(1);
}
prompt = &windows[0];
}
@ -238,7 +260,6 @@ static void load_data(char *path) {
static void draw_bar() {
static int odd = 0;
size_t i;
attron(COLOR_PAIR(4));
mvhline(LINES - 2, 0, '_', COLS);
@ -250,28 +271,26 @@ static void draw_bar() {
printw(" TOXIC 1.0 |");
attroff(COLOR_PAIR(4) | A_BOLD);
for(i=0; i<w_num; i++) {
if(i == w_active) {
attron(A_BOLD);
}
int i;
for (i = 0; i < (MAX_WINDOW_SLOTS-1); i++) {
if (WINDOW_STATUS[i] != -1) {
if (i == w_active)
attron(A_BOLD);
odd = (odd+1) % 10;
odd = (odd+1) % 10;
if(windows[i].blink && (odd < 5)) {
attron(COLOR_PAIR(3));
}
if(windows[i].blink && (odd < 5)) {
attron(COLOR_PAIR(3));
}
printw(" %s", windows[i].title);
if(windows[i].blink && (odd < 5)) {
attron(COLOR_PAIR(3));
}
if(i == w_active) {
attroff(A_BOLD);
printw(" %s", windows[i].title);
if(windows[i].blink && (odd < 5)) {
attron(COLOR_PAIR(3));
}
if(i == w_active) {
attroff(A_BOLD);
}
}
}
refresh();
}
@ -280,38 +299,66 @@ void prepare_window(WINDOW* w) {
wresize(w, LINES-2, COLS);
}
/* Shows next window when tab or back-tab is pressed */
void set_active_window(int ch) {
int f_inf = 0;
int max = MAX_WINDOW_SLOTS-1;
if (ch == '\t') {
int i = (w_active + 1) % max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
w_active = i;
return;
}
i = (i + 1) % max;
if (f_inf++ > max) { // infinite loop check
endwin();
clear();
exit(2);
}
}
}else {
int i = w_active - 1;
if (i < 0) i = max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
w_active = i;
return;
}
if (--i < 0) i = max;
if (f_inf++ > max) {
endwin();
clear();
exit(2);
}
}
}
}
int main(int argc, char* argv[]) {
int ch;
int i = 0;
int f_flag = 0;
char *filename = "data";
ToxWindow* a;
for(i = 0; i < argc; i++) {
if(argv[i][0] == '-') {
if(argv[i][1] == 'f') {
if(argv[i + 1] != NULL)
filename = argv[i + 1];
else {
f_flag = -1;
}
}
}
}
init_term();
init_tox();
load_data(filename);
init_window_status();
init_windows();
char *filename = "data";
load_data(filename);
if(f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD);
wprintw(prompt->window, "You passed '-f' without giving an argument!\n"
int i;
for(i = 0; i < argc; i++) {
if(argv[i][0] == '-' && argv[i][1] == 'f') {
if(argv[i + 1] != NULL)
filename = argv[i + 1];
else {
attron(COLOR_PAIR(3) | A_BOLD);
wprintw(prompt->window, "You passed '-f' without giving an argument!\n"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
attroff(COLOR_PAIR(3) | A_BOLD);
}
}
}
while(true) {
// Update tox.
do_tox();
@ -325,18 +372,14 @@ int main(int argc, char* argv[]) {
// Handle input.
ch = getch();
if(ch == '\t') {
w_active = (w_active + 1) % w_num;
}
else if(ch == KEY_BTAB) {
w_active = (w_active + w_num - 1) % w_num;
if(ch == '\t' || ch == KEY_BTAB)
set_active_window(ch);
else if(ch != ERR) {
a->onKey(a, ch);
}
else if(ch != ERR) {
a->onKey(a, ch);
}
}
return 0;
}
}

View File

@ -254,7 +254,9 @@ static void execute(ToxWindow* self, char* cmd) {
wprintw(self->window, "Message successfully sent.\n");
}
}
else if (!strncmp(cmd, "clear", strlen("clear"))) {
wclear(self->window);
}
else {
wprintw(self->window, "Invalid command.\n");
}
@ -314,6 +316,7 @@ static void print_usage(ToxWindow* self) {
wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n");
wprintw(self->window, " clear : Clear the screen\n");
wprintw(self->window, " quit/exit : Exit program\n");
wprintw(self->window, " help : Print this message again\n");

View File

@ -3,6 +3,14 @@
*/
#include <stdbool.h>
#define TOXWINDOWS_MAX_NUM 32
#define MAX_FRIENDS_NUM 100
/* number of permanent default windows */
#define N_DEFAULT_WINS 2
/* maximum window slots for WINDOW_STATUS array */
#define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM
typedef struct ToxWindow_ ToxWindow;