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

remove WINDOW_STATUS, simplify adding and selecting chat windows

This commit is contained in:
Aaron Lipinski 2013-08-17 20:00:19 +12:00
parent 5d8ccde3d9
commit 61b72daf6a
6 changed files with 69 additions and 133 deletions

3
chat.c
View File

@ -335,7 +335,8 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
else if (strcmp(ctx->line, "/close") == 0) { else if (strcmp(ctx->line, "/close") == 0) {
int f_num = ctx->friendnum; int f_num = ctx->friendnum;
delwin(ctx->linewin); delwin(ctx->linewin);
del_window(self, f_num); del_window(self);
disable_chatwin(f_num);
} }
else else

View File

@ -13,7 +13,6 @@
#include "windows.h" #include "windows.h"
#include "friendlist.h" #include "friendlist.h"
static char *WINDOW_STATUS;
typedef struct { typedef struct {
uint8_t name[MAX_NAME_LENGTH]; uint8_t name[MAX_NAME_LENGTH];
@ -48,17 +47,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
return; return;
if (friends[num].chatwin == -1) { if (friends[num].chatwin == -1) {
friends[num].chatwin = num; friends[num].chatwin = add_window(m, new_chat(m, num));
int i;
/* Find first open slot to hold chat window */
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num;
add_window(m, new_chat(m, num), i);
break;
}
}
} }
} }
@ -106,25 +95,9 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
} else if (key == '\n') { } else if (key == '\n') {
/* Jump to chat window if already open */ /* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) { if (friends[num_selected].chatwin != -1) {
int i; set_active_window(friends[num_selected].chatwin);
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == num_selected) {
set_active_window(i);
break;
}
}
} else { } else {
int i; friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected));
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected;
friends[num_selected].chatwin = num_selected;
add_window(m, new_chat(m, num_selected), i);
break;
}
}
} }
} }
} }
@ -172,9 +145,8 @@ static void friendlist_onInit(ToxWindow *self, Messenger *m)
} }
ToxWindow new_friendlist(char *ws) ToxWindow new_friendlist()
{ {
WINDOW_STATUS = ws;
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));

View File

@ -4,7 +4,7 @@
#include "windows.h" #include "windows.h"
#include "chat.h" #include "chat.h"
ToxWindow new_friendlist(char *ws); ToxWindow new_friendlist();
int friendlist_onFriendAdded(Messenger *m, int num); int friendlist_onFriendAdded(Messenger *m, int num);
void disable_chatwin(int f_num); void disable_chatwin(int f_num);
void fix_name(uint8_t *name); void fix_name(uint8_t *name);

1
main.c
View File

@ -295,7 +295,6 @@ int main(int argc, char *argv[])
init_term(); init_term();
Messenger *m = init_tox(); Messenger *m = init_tox();
ToxWindow *prompt = init_windows(m); ToxWindow *prompt = init_windows(m);
init_window_status();
if (f_loadfromfile) if (f_loadfromfile)
load_data(m, DATA_FILE); load_data(m, DATA_FILE);

155
windows.c
View File

@ -6,15 +6,10 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Messenger *m, char *path); extern int store_data(Messenger *m, char *path);
/* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS];
static int w_num;
static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow windows[MAX_WINDOW_SLOTS];
static Messenger *m; static ToxWindow *active_window;
int active_window;
static ToxWindow *prompt; static ToxWindow *prompt;
static Messenger *m;
/* CALLBACKS START */ /* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
@ -89,119 +84,89 @@ void on_friendadded(Messenger *m, int friendnumber)
} }
/* CALLBACKS END */ /* CALLBACKS END */
int add_window(Messenger *m, ToxWindow w, int n) int add_window(Messenger *m, ToxWindow w)
{ {
if (w_num >= TOXWINDOWS_MAX_NUM)
return -1;
if (LINES < 2) if (LINES < 2)
return -1; return -1;
int i;
for(i = 0; i < MAX_WINDOW_SLOTS; i++) {
if (windows[i].window)
continue;
w.window = newwin(LINES - 2, COLS, 0, 0);
if (w.window == NULL)
return -1;
w.window = newwin(LINES - 2, COLS, 0, 0); windows[i] = w;
w.onInit(&w, m);
if (w.window == NULL)
return -1; active_window = windows+i;
return i;
windows[n] = w; }
w.onInit(&w, m);
w_num++; return -1;
active_window = n;
return n;
} }
/* Deletes window w and cleans up */ /* Deletes window w and cleans up */
void del_window(ToxWindow *w, int f_num) void del_window(ToxWindow *w)
{ {
active_window = 0; // Go to prompt screen active_window = windows; // Go to prompt screen
delwin(w->window); delwin(w->window);
int i; if (w->x)
free(w->x);
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) { w->window = NULL;
if (WINDOW_STATUS[i] == f_num) { memset(w, 0, sizeof(ToxWindow));
WINDOW_STATUS[i] = -1;
disable_chatwin(f_num);
break;
}
}
clear(); clear();
refresh(); refresh();
} }
/* Shows next window when tab or back-tab is pressed */ /* Shows next window when tab or back-tab is pressed */
void set_active_window(int ch) void set_next_window(int ch)
{ {
int f_inf = 0; ToxWindow *end = windows+MAX_WINDOW_SLOTS-1;
int max = MAX_WINDOW_SLOTS - 1; ToxWindow *inf = active_window;
while(true) {
if (ch == '\t') { if (ch == '\t') {
int i = (active_window + 1) % max; if (++active_window > end)
active_window = windows;
while (true) { } else
if (WINDOW_STATUS[i] != -1) { if (--active_window < windows)
active_window = i; active_window = end;
return;
} if (active_window->window)
return;
i = (i + 1) % max;
if (active_window == inf) { // infinite loop check
if (f_inf++ > max) { // infinite loop check endwin();
endwin(); exit(2);
exit(2);
}
}
} else {
int i = active_window - 1;
if (i < 0) i = max;
while (true) {
if (WINDOW_STATUS[i] != -1) {
active_window = i;
return;
}
if (--i < 0) i = max;
if (f_inf++ > max) {
endwin();
exit(2);
}
} }
} }
} }
void init_window_status() void set_active_window(int index)
{ {
/* Default window values decrement from -2 */ if (index < 0 || index >= MAX_WINDOW_SLOTS)
int i; return;
for (i = 0; i < N_DEFAULT_WINS; ++i) active_window = windows+index;
WINDOW_STATUS[i] = -(i + 2);
int j;
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
WINDOW_STATUS[j] = -1;
} }
ToxWindow *init_windows() ToxWindow *init_windows()
{ {
w_num = 0; int n_prompt = add_window(m, new_prompt(on_friendadded));
int n_prompt = 0;
int n_friendslist = 1; if (n_prompt == -1
int n_dhtstatus = 2; || add_window(m, new_friendlist()) == -1
|| add_window(m, new_dhtstatus()) == -1) {
if (add_window(m, new_prompt(on_friendadded), n_prompt) == -1
|| add_window(m, new_friendlist(WINDOW_STATUS), n_friendslist) == -1
|| add_window(m, new_dhtstatus(), n_dhtstatus) == -1) {
fprintf(stderr, "add_window() failed.\n"); fprintf(stderr, "add_window() failed.\n");
endwin(); endwin();
exit(1); exit(1);
} }
active_window = n_prompt;
prompt = &windows[n_prompt]; prompt = &windows[n_prompt];
active_window = prompt;
return prompt; return prompt;
} }
@ -223,8 +188,8 @@ static void draw_bar()
int i; int i;
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) { for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
if (WINDOW_STATUS[i] != -1) { if (windows[i].window) {
if (i == active_window) if (windows+i == active_window)
attron(A_BOLD); attron(A_BOLD);
odd = (odd + 1) % blinkrate; odd = (odd + 1) % blinkrate;
@ -237,7 +202,7 @@ static void draw_bar()
if (windows[i].blink && (odd < (blinkrate / 2))) if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(3)); attroff(COLOR_PAIR(3));
if (i == active_window) { if (windows+i == active_window) {
attroff(A_BOLD); attroff(A_BOLD);
} }
} }
@ -255,7 +220,7 @@ void prepare_window(WINDOW *w)
void draw_active_window(Messenger *m) void draw_active_window(Messenger *m)
{ {
ToxWindow *a = &windows[active_window]; ToxWindow *a = active_window;
prepare_window(a->window); prepare_window(a->window);
a->blink = false; a->blink = false;
draw_bar(); draw_bar();
@ -265,7 +230,7 @@ void draw_active_window(Messenger *m)
int ch = getch(); int ch = getch();
if (ch == '\t' || ch == KEY_BTAB) if (ch == '\t' || ch == KEY_BTAB)
set_active_window(ch); set_next_window(ch);
else if (ch != ERR) else if (ch != ERR)
a->onKey(a, m, ch); a->onKey(a, m, ch);
} }

View File

@ -48,11 +48,10 @@ void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length,
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_friendadded(Messenger *m, int friendnumber); void on_friendadded(Messenger *m, int friendnumber);
void init_window_status();
ToxWindow *init_windows(); ToxWindow *init_windows();
void draw_active_window(Messenger *m); void draw_active_window(Messenger *m);
int add_window(Messenger *m, ToxWindow w, int n); int add_window(Messenger *m, ToxWindow w);
void del_window(ToxWindow *w, int f_num); void del_window(ToxWindow *w);
void set_active_window(int ch); void set_active_window(int ch);
#endif #endif