mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge pull request #479 from krisl/internalise_window_handling
remove WINDOW_STATUS, simplify adding and selecting chat windows
This commit is contained in:
commit
5e0a1328ef
|
@ -335,7 +335,8 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
|
|||
else if (strcmp(ctx->line, "/close") == 0) {
|
||||
int f_num = ctx->friendnum;
|
||||
delwin(ctx->linewin);
|
||||
del_window(self, f_num);
|
||||
del_window(self);
|
||||
disable_chatwin(f_num);
|
||||
}
|
||||
|
||||
else
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
#include "windows.h"
|
||||
#include "friendlist.h"
|
||||
|
||||
static char *WINDOW_STATUS;
|
||||
|
||||
typedef struct {
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
|
@ -48,17 +47,7 @@ void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str,
|
|||
return;
|
||||
|
||||
if (friends[num].chatwin == -1) {
|
||||
friends[num].chatwin = 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;
|
||||
}
|
||||
}
|
||||
friends[num].chatwin = add_window(m, new_chat(m, num));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -106,25 +95,9 @@ static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
|
|||
} else if (key == '\n') {
|
||||
/* 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) {
|
||||
set_active_window(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
set_active_window(friends[num_selected].chatwin);
|
||||
} else {
|
||||
int i;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include "windows.h"
|
||||
#include "chat.h"
|
||||
|
||||
ToxWindow new_friendlist(char *ws);
|
||||
ToxWindow new_friendlist();
|
||||
int friendlist_onFriendAdded(Messenger *m, int num);
|
||||
void disable_chatwin(int f_num);
|
||||
void fix_name(uint8_t *name);
|
||||
|
|
|
@ -295,7 +295,6 @@ int main(int argc, char *argv[])
|
|||
init_term();
|
||||
Messenger *m = init_tox();
|
||||
ToxWindow *prompt = init_windows(m);
|
||||
init_window_status();
|
||||
|
||||
if (f_loadfromfile)
|
||||
load_data(m, DATA_FILE);
|
||||
|
|
|
@ -19,7 +19,6 @@ extern int store_data(Messenger *m, char *path);
|
|||
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
|
||||
uint8_t num_requests = 0; // XXX
|
||||
|
||||
static friendAddedFn *on_friendadded_cb;
|
||||
static char prompt_buf[MAX_STR_SIZE] = {0};
|
||||
static int prompt_buf_pos = 0;
|
||||
|
||||
|
@ -97,7 +96,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args)
|
|||
wprintw(self->window, "Failed to add friend.\n");
|
||||
else {
|
||||
wprintw(self->window, "Friend accepted as: %d.\n", num);
|
||||
on_friendadded_cb(m, num);
|
||||
on_friendadded(m, num);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -174,7 +173,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
|
|||
|
||||
default:
|
||||
wprintw(self->window, "Friend added as %d.\n", num);
|
||||
on_friendadded_cb(m, num);
|
||||
on_friendadded(m, num);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -480,9 +479,8 @@ static void prompt_onInit(ToxWindow *self, Messenger *m)
|
|||
wclrtoeol(self->window);
|
||||
}
|
||||
|
||||
ToxWindow new_prompt(friendAddedFn *f)
|
||||
ToxWindow new_prompt()
|
||||
{
|
||||
on_friendadded_cb = f;
|
||||
ToxWindow ret;
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
ret.onKey = &prompt_onKey;
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
|
||||
#include "windows.h"
|
||||
|
||||
typedef void (friendAddedFn)(Messenger *m, int friendnumber);
|
||||
|
||||
ToxWindow new_prompt(friendAddedFn *f);
|
||||
ToxWindow new_prompt();
|
||||
int add_req(uint8_t *public_key);
|
||||
unsigned char *hex_string_to_bin(char hex_string[]);
|
||||
|
||||
|
|
|
@ -6,15 +6,10 @@
|
|||
extern char *DATA_FILE;
|
||||
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 Messenger *m;
|
||||
int active_window;
|
||||
|
||||
static ToxWindow windows[MAX_WINDOWS_NUM];
|
||||
static ToxWindow *active_window;
|
||||
static ToxWindow *prompt;
|
||||
static Messenger *m;
|
||||
|
||||
/* CALLBACKS START */
|
||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
|
||||
|
@ -31,7 +26,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd
|
|||
wprintw(prompt->window, "\nWith the message: %s\n", data);
|
||||
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n);
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onFriendRequest != NULL)
|
||||
windows[i].onFriendRequest(&windows[i], public_key, data, length);
|
||||
}
|
||||
|
@ -41,7 +36,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length
|
|||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onMessage != NULL)
|
||||
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
|
||||
}
|
||||
|
@ -51,7 +46,7 @@ void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length,
|
|||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onAction != NULL)
|
||||
windows[i].onAction(&windows[i], m, friendnumber, string, length);
|
||||
}
|
||||
|
@ -62,7 +57,7 @@ void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t len
|
|||
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onNickChange != NULL)
|
||||
windows[i].onNickChange(&windows[i], friendnumber, string, length);
|
||||
}
|
||||
|
@ -73,7 +68,7 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
|
|||
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onStatusChange != NULL)
|
||||
windows[i].onStatusChange(&windows[i], friendnumber, string, length);
|
||||
}
|
||||
|
@ -89,119 +84,89 @@ void on_friendadded(Messenger *m, int friendnumber)
|
|||
}
|
||||
/* 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)
|
||||
return -1;
|
||||
|
||||
w.window = newwin(LINES - 2, COLS, 0, 0);
|
||||
int i;
|
||||
for(i = 0; i < MAX_WINDOWS_NUM; i++) {
|
||||
if (windows[i].window)
|
||||
continue;
|
||||
|
||||
w.window = newwin(LINES - 2, COLS, 0, 0);
|
||||
if (w.window == NULL)
|
||||
return -1;
|
||||
|
||||
windows[n] = w;
|
||||
windows[i] = w;
|
||||
w.onInit(&w, m);
|
||||
w_num++;
|
||||
active_window = n;
|
||||
return n;
|
||||
|
||||
active_window = windows+i;
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (w->x)
|
||||
free(w->x);
|
||||
w->window = NULL;
|
||||
memset(w, 0, sizeof(ToxWindow));
|
||||
clear();
|
||||
refresh();
|
||||
}
|
||||
|
||||
/* 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;
|
||||
int max = MAX_WINDOW_SLOTS - 1;
|
||||
|
||||
ToxWindow *end = windows+MAX_WINDOWS_NUM-1;
|
||||
ToxWindow *inf = active_window;
|
||||
while(true) {
|
||||
if (ch == '\t') {
|
||||
int i = (active_window + 1) % max;
|
||||
if (++active_window > end)
|
||||
active_window = windows;
|
||||
} else
|
||||
if (--active_window < windows)
|
||||
active_window = end;
|
||||
|
||||
while (true) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
active_window = i;
|
||||
if (active_window->window)
|
||||
return;
|
||||
}
|
||||
|
||||
i = (i + 1) % max;
|
||||
|
||||
if (f_inf++ > max) { // infinite loop check
|
||||
endwin();
|
||||
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) {
|
||||
if (active_window == inf) { // infinite loop check
|
||||
endwin();
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_window_status()
|
||||
void set_active_window(int index)
|
||||
{
|
||||
/* Default window values decrement from -2 */
|
||||
int i;
|
||||
if (index < 0 || index >= MAX_WINDOWS_NUM)
|
||||
return;
|
||||
|
||||
for (i = 0; i < N_DEFAULT_WINS; ++i)
|
||||
WINDOW_STATUS[i] = -(i + 2);
|
||||
|
||||
int j;
|
||||
|
||||
for (j = N_DEFAULT_WINS; j < MAX_WINDOW_SLOTS; j++)
|
||||
WINDOW_STATUS[j] = -1;
|
||||
active_window = windows+index;
|
||||
}
|
||||
|
||||
ToxWindow *init_windows()
|
||||
{
|
||||
w_num = 0;
|
||||
int n_prompt = 0;
|
||||
int n_friendslist = 1;
|
||||
int n_dhtstatus = 2;
|
||||
int n_prompt = add_window(m, new_prompt());
|
||||
|
||||
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) {
|
||||
if (n_prompt == -1
|
||||
|| add_window(m, new_friendlist()) == -1
|
||||
|| add_window(m, new_dhtstatus()) == -1) {
|
||||
fprintf(stderr, "add_window() failed.\n");
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
active_window = n_prompt;
|
||||
prompt = &windows[n_prompt];
|
||||
active_window = prompt;
|
||||
|
||||
return prompt;
|
||||
}
|
||||
|
||||
|
@ -222,9 +187,9 @@ static void draw_bar()
|
|||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
|
||||
if (WINDOW_STATUS[i] != -1) {
|
||||
if (i == active_window)
|
||||
for (i = 0; i < (MAX_WINDOWS_NUM); ++i) {
|
||||
if (windows[i].window) {
|
||||
if (windows+i == active_window)
|
||||
attron(A_BOLD);
|
||||
|
||||
odd = (odd + 1) % blinkrate;
|
||||
|
@ -237,7 +202,7 @@ static void draw_bar()
|
|||
if (windows[i].blink && (odd < (blinkrate / 2)))
|
||||
attroff(COLOR_PAIR(3));
|
||||
|
||||
if (i == active_window) {
|
||||
if (windows+i == active_window) {
|
||||
attroff(A_BOLD);
|
||||
}
|
||||
}
|
||||
|
@ -255,7 +220,7 @@ void prepare_window(WINDOW *w)
|
|||
void draw_active_window(Messenger *m)
|
||||
{
|
||||
|
||||
ToxWindow *a = &windows[active_window];
|
||||
ToxWindow *a = active_window;
|
||||
prepare_window(a->window);
|
||||
a->blink = false;
|
||||
draw_bar();
|
||||
|
@ -265,7 +230,7 @@ void draw_active_window(Messenger *m)
|
|||
int ch = getch();
|
||||
|
||||
if (ch == '\t' || ch == KEY_BTAB)
|
||||
set_active_window(ch);
|
||||
set_next_window(ch);
|
||||
else if (ch != ERR)
|
||||
a->onKey(a, m, ch);
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../../core/Messenger.h"
|
||||
#define TOXWINDOWS_MAX_NUM 32
|
||||
#define MAX_WINDOWS_NUM 32
|
||||
#define MAX_FRIENDS_NUM 100
|
||||
#define MAX_STR_SIZE 256
|
||||
#define KEY_SIZE_BYTES 32
|
||||
|
@ -16,9 +16,6 @@
|
|||
/* number of permanent default windows */
|
||||
#define N_DEFAULT_WINS 3
|
||||
|
||||
/* maximum window slots for WINDOW_STATUS array */
|
||||
#define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM
|
||||
|
||||
#ifndef TOXICVER
|
||||
#define TOXICVER "NOVER" //Use the -D flag to set this
|
||||
#endif
|
||||
|
@ -48,11 +45,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_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
|
||||
void on_friendadded(Messenger *m, int friendnumber);
|
||||
void init_window_status();
|
||||
ToxWindow *init_windows();
|
||||
void draw_active_window(Messenger *m);
|
||||
int add_window(Messenger *m, ToxWindow w, int n);
|
||||
void del_window(ToxWindow *w, int f_num);
|
||||
int add_window(Messenger *m, ToxWindow w);
|
||||
void del_window(ToxWindow *w);
|
||||
void set_active_window(int ch);
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user