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

Add groupchat side panel & add new callback

This commit is contained in:
Jfreegman 2013-11-23 21:19:59 -05:00
parent d8da80914d
commit f84ece13de
8 changed files with 88 additions and 30 deletions

View File

@ -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 <type> <message> : Set your status with optional note\n");
wprintw(window, " /note <message> : Set a personal note\n");
wprintw(window, " /nick <nickname> : Set your nickname\n");
wprintw(window, " /status <type> <msg> : Set your status with optional note\n");
wprintw(window, " /note <msg> : Set a personal note\n");
wprintw(window, " /nick <nick> : Set your nickname\n");
wprintw(window, " /invite <n> : Invite friend to a group chat\n");
wprintw(window, " /me <action> : Do an action\n");
wprintw(window, " /myid : Print your ID\n");

View File

@ -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 <id> <message> : Add friend with optional message\n");
wprintw(window, " /add <id> <msg> : Add friend with optional message\n");
wprintw(window, " /accept <n> : Accept friend request\n");
wprintw(window, " /connect <ip> <port> <key> : Manually connect to a DHT server\n");
wprintw(window, " /status <type> <message> : Set your status with optional note\n");
wprintw(window, " /note <message> : Set a personal note\n");
wprintw(window, " /nick <nickname> : Set your nickname\n");
wprintw(window, " /status <type> <msg> : Set your status with optional note\n");
wprintw(window, " /note <msg> : Set a personal note\n");
wprintw(window, " /nick <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));

View File

@ -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,10 +69,10 @@ static void print_groupchat_help(ChatContext *ctx)
wprintw(ctx->history, "Group chat commands:\n");
wattroff(ctx->history, A_BOLD);
wprintw(ctx->history, " /add <id> <message> : Add friend with optional message\n");
wprintw(ctx->history, " /status <type> <message> : Set your status with optional note\n");
wprintw(ctx->history, " /note <message> : Set a personal note\n");
wprintw(ctx->history, " /nick <nickname> : Set your nickname\n");
wprintw(ctx->history, " /add <id> <msg> : Add friend with optional message\n");
wprintw(ctx->history, " /status <type> <msg>: Set your status with optional note\n");
wprintw(ctx->history, " /note <msg> : Set a personal note\n");
wprintw(ctx->history, " /nick <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");
@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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[]);

View File

@ -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);

View File

@ -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;