From f84ece13de03783737d510f1c1ea4982b5beff76 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 23 Nov 2013 21:19:59 -0500 Subject: [PATCH] Add groupchat side panel & add new callback --- src/chat_commands.c | 6 ++--- src/global_commands.c | 10 +++---- src/groupchat.c | 61 +++++++++++++++++++++++++++++++++---------- src/groupchat.h | 14 ++++++++++ src/main.c | 3 ++- src/misc_tools.h | 2 ++ src/toxic_windows.h | 10 +++---- src/windows.c | 12 ++++++++- 8 files changed, 88 insertions(+), 30 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 54100c7..78a8e56 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -20,9 +20,9 @@ void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg wprintw(window, "Chat commands:\n"); wattroff(window, A_BOLD); - wprintw(window, " /status : Set your status with optional note\n"); - wprintw(window, " /note : Set a personal note\n"); - wprintw(window, " /nick : Set your nickname\n"); + wprintw(window, " /status : Set your status with optional note\n"); + wprintw(window, " /note : Set a personal note\n"); + wprintw(window, " /nick : Set your nickname\n"); wprintw(window, " /invite : Invite friend to a group chat\n"); wprintw(window, " /me : Do an action\n"); wprintw(window, " /myid : Print your ID\n"); diff --git a/src/global_commands.c b/src/global_commands.c index 35d4ab6..7a1b5b1 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -271,12 +271,12 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wprintw(window, "\n\nGlobal commands:\n"); wattroff(window, A_BOLD); - wprintw(window, " /add : Add friend with optional message\n"); + wprintw(window, " /add : Add friend with optional message\n"); wprintw(window, " /accept : Accept friend request\n"); wprintw(window, " /connect : Manually connect to a DHT server\n"); - wprintw(window, " /status : Set your status with optional note\n"); - wprintw(window, " /note : Set a personal note\n"); - wprintw(window, " /nick : Set your nickname\n"); + wprintw(window, " /status : Set your status with optional note\n"); + wprintw(window, " /note : Set a personal note\n"); + wprintw(window, " /nick : Set your nickname\n"); wprintw(window, " /groupchat : Create a group chat\n"); wprintw(window, " /myid : Print your ID\n"); wprintw(window, " /quit or /exit : Exit Toxic\n"); @@ -285,7 +285,7 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wattron(window, A_BOLD); wprintw(window, " * Argument messages must be enclosed in quotation marks.\n"); - wprintw(window, " * Use the TAB key to navigate through the tabs.\n"); + wprintw(window, " * Use TAB and Shift-TAB to navigate through the tabs.\n"); wattroff(window, A_BOLD); wattroff(window, COLOR_PAIR(CYAN)); diff --git a/src/groupchat.c b/src/groupchat.c index 7c03cd2..a6ccaac 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -13,6 +13,7 @@ #include "toxic_windows.h" #include "execute.h" #include "misc_tools.h" +#include "groupchat.h" static GroupChat groupchats[MAX_GROUPCHAT_NUM]; static int max_groupchat_index = 0; @@ -31,6 +32,7 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum) if (!groupchats[i].active) { groupchats[i].chatwin = add_window(m, new_group_chat(m, groupnum)); groupchats[i].active = true; + groupchats[i].num_peers = 0; set_active_window(groupchats[i].chatwin); if (i == max_groupchat_index) @@ -67,16 +69,16 @@ static void print_groupchat_help(ChatContext *ctx) wprintw(ctx->history, "Group chat commands:\n"); wattroff(ctx->history, A_BOLD); - wprintw(ctx->history, " /add : Add friend with optional message\n"); - wprintw(ctx->history, " /status : Set your status with optional note\n"); - wprintw(ctx->history, " /note : Set a personal note\n"); - wprintw(ctx->history, " /nick : Set your nickname\n"); - wprintw(ctx->history, " /groupchat : Create a group chat\n"); - wprintw(ctx->history, " /myid : Print your ID\n"); - wprintw(ctx->history, " /clear : Clear the screen\n"); - wprintw(ctx->history, " /close : Close the current group chat\n"); - wprintw(ctx->history, " /quit or /exit : Exit Toxic\n"); - wprintw(ctx->history, " /help : Print this message again\n"); + wprintw(ctx->history, " /add : Add friend with optional message\n"); + wprintw(ctx->history, " /status : Set your status with optional note\n"); + wprintw(ctx->history, " /note : Set a personal note\n"); + wprintw(ctx->history, " /nick : Set your nickname\n"); + wprintw(ctx->history, " /groupchat : Create a group chat\n"); + wprintw(ctx->history, " /myid : Print your ID\n"); + wprintw(ctx->history, " /clear : Clear the screen\n"); + wprintw(ctx->history, " /close : Close the current group chat\n"); + wprintw(ctx->history, " /quit or /exit : Exit Toxic\n"); + wprintw(ctx->history, " /help : Print this message again\n"); wattron(ctx->history, A_BOLD); wprintw(ctx->history, "\n * Argument messages must be enclosed in quotation marks.\n"); @@ -112,6 +114,17 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int self->blink = true; } +static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum) +{ + if (self->num != groupnum) + return; + + memset(groupchats[groupnum].peer_names, 0, sizeof(groupchats[groupnum].peer_names); + int num_peers = tox_group_number_peers(m, groupnum); + groupchats[groupnum].num_peers = MIN(MAX_GROUP_PEERS, num_peers); + tox_group_copy_names(m, groupnum, groupchats[groupnum].peer_names, num_peers); +} + static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->chatwin; @@ -199,21 +212,40 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m) curs_set(1); int x, y; getmaxyx(self->window, y, x); + ChatContext *ctx = (ChatContext *) self->chatwin; + wclrtobot(ctx->sidebar); mvwhline(ctx->linewin, 0, 0, '_', x); + mvwvline(ctx->sidebar, 0, 0,'|', x); + + int num_peers = groupchats[self->num].num_peers; + if (num_peers) { + int i; + + for (i = 0; i < num_peers; ++i) { + wmove(ctx->sidebar, i, 1); + groupchats[self->num].peer_names[i][SIDEBAR_WIDTH-2] = '\0'; + uint8_t *nick = strlen(groupchats[self->num].peer_names[i]) ? groupchats[self->num].peer_names[i] : (uint8_t *) UNKNOWN_NAME; + wprintw(ctx->sidebar, "%s\n", nick); + } + } + wrefresh(self->window); } static void groupchat_onInit(ToxWindow *self, Tox *m) { int x, y; - ChatContext *ctx = (ChatContext *) self->chatwin; getmaxyx(self->window, y, x); - ctx->history = subwin(self->window, y-3, x, 0, 0); + + ChatContext *ctx = (ChatContext *) self->chatwin; + ctx->history = subwin(self->window, y-CHATBOX_HEIGHT+1, x-SIDEBAR_WIDTH-1, 0, 0); scrollok(ctx->history, 1); - ctx->linewin = subwin(self->window, 2, x, y-4, 0); + ctx->linewin = subwin(self->window, 2, x, y-CHATBOX_HEIGHT, 0); + ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH); + print_groupchat_help(ctx); - wmove(self->window, y - CURS_Y_OFFSET, 0); + wmove(self->window, y-CURS_Y_OFFSET, 0); } ToxWindow new_group_chat(Tox *m, int groupnum) @@ -225,6 +257,7 @@ ToxWindow new_group_chat(Tox *m, int groupnum) ret.onDraw = &groupchat_onDraw; ret.onInit = &groupchat_onInit; ret.onGroupMessage = &groupchat_onGroupMessage; + ret.onGroupNamelistChange = &groupchat_onGroupNamelistChange; // ret.onNickChange = &groupchat_onNickChange; // ret.onStatusChange = &groupchat_onStatusChange; // ret.onAction = &groupchat_onAction; diff --git a/src/groupchat.h b/src/groupchat.h index 3434430..a8b4f48 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -2,4 +2,18 @@ * Toxic -- Tox Curses Client */ +/* The sidebar will take up y/n of the window width where x is the full width of the window */ +#define SIDEBAR_WIDTH 16 +#define CHATBOX_HEIGHT 4 + +/* Limits # of peers in sidepanel (make this go away) */ +#define MAX_GROUP_PEERS 100 + +typedef struct { + int chatwin; + bool active; + int num_peers; + uint8_t peer_names[MAX_GROUP_PEERS][TOX_MAX_NAME_LENGTH]; +} GroupChat; + int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); diff --git a/src/main.c b/src/main.c index a9f1095..8f27df2 100644 --- a/src/main.c +++ b/src/main.c @@ -56,7 +56,7 @@ void on_window_resize(int sig) clear(); } -static void init_term() +static void init_term(void) { /* Setup terminal */ signal(SIGWINCH, on_window_resize); @@ -107,6 +107,7 @@ static Tox *init_tox() tox_callback_action(m, on_action, NULL); tox_callback_group_invite(m, on_groupinvite, NULL); tox_callback_group_message(m, on_groupmessage, NULL); + tox_callback_group_namelistchange(m, on_group_namelistchange, NULL); tox_callback_file_sendrequest(m, on_file_sendrequest, NULL); tox_callback_file_control(m, on_file_control, NULL); tox_callback_file_data(m, on_file_data, NULL); diff --git a/src/misc_tools.h b/src/misc_tools.h index c598fca..597b50d 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -2,6 +2,8 @@ * Toxic -- Tox Curses Client */ +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + /* convert a hex string to binary */ unsigned char *hex_string_to_bin(char hex_string[]); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 8b92ec7..8414786 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -61,6 +61,7 @@ struct ToxWindow_ { void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onGroupMessage)(ToxWindow *, Tox *, int, int, uint8_t *, uint16_t); void(*onGroupInvite)(ToxWindow *, Tox *, int, uint8_t *); + void(*onGroupNamelistChange)(ToxWindow *, Tox*, int); void(*onFileSendRequest)(ToxWindow *, Tox *, int, uint8_t, uint64_t, uint8_t *, uint16_t); void(*onFileControl)(ToxWindow *, Tox *, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t); void(*onFileData)(ToxWindow *, Tox *, int, uint8_t, uint8_t *, uint16_t); @@ -91,13 +92,9 @@ typedef struct { size_t pos; WINDOW *history; WINDOW *linewin; + WINDOW *sidebar; } ChatContext; -typedef struct { - int chatwin; - bool active; -} GroupChat; - /* Start file transfer code */ #define MAX_FILES 256 @@ -153,11 +150,12 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t void on_friendadded(Tox *m, int friendnumber); void on_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata); void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *userdata); +void on_group_namelistchange(Tox *m, int groupnumber, void *userdata); void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *pathname, uint16_t pathname_length, void *userdata); void on_file_control(Tox *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata); void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata); -ToxWindow *init_windows(); +ToxWindow *init_windows(Tox *mToAssign); void draw_active_window(Tox *m); int add_window(Tox *m, ToxWindow w); void del_window(ToxWindow *w); diff --git a/src/windows.c b/src/windows.c index 037e2d9..9cd4122 100644 --- a/src/windows.c +++ b/src/windows.c @@ -127,6 +127,16 @@ void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *user } } +void on_group_namelistchange(Tox *m, int groupnumber, void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupNamelistChange != NULL) + windows[i].onGroupNamelistChange(&windows[i], m, groupnumber); + } +} + void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata) { @@ -253,7 +263,7 @@ ToxWindow *init_windows(Tox *mToAssign) return prompt; } -static void draw_bar() +static void draw_bar(void) { static int odd = 0; int blinkrate = 30;