From 2b707f1d808f06de5c641c55e487379e0b89ed1b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 23 Feb 2014 04:28:33 -0500 Subject: [PATCH] implemented typing status --- src/chat.c | 39 ++++++++++++++++++++++++++++++++++++++- src/friendlist.h | 1 + src/main.c | 1 + src/toxic_windows.h | 4 ++++ src/windows.c | 10 ++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/chat.c b/src/chat.c index d89bfdd..6292f39 100644 --- a/src/chat.c +++ b/src/chat.c @@ -45,6 +45,14 @@ static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = { { "/status" }, }; +static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing) +{ + ChatContext *ctx = self->chatwin; + + tox_set_user_is_typing(m, self->num, is_typing); + ctx->self_is_typing = is_typing; +} + static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -77,7 +85,22 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t st return; StatusBar *statusbar = self->stb; - statusbar->is_online = status == 1 ? true : false; + + if (status == 1) { + statusbar->is_online = true; + friends[num].is_typing = tox_get_is_typing(m, num); + } else { + statusbar->is_online = false; + friends[num].is_typing = false; + } +} + +static void chat_onTypingChange(ToxWindow *self, Tox *m, int num, int is_typing) +{ + if (self->num != num) + return; + + friends[num].is_typing = is_typing; } static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) @@ -416,6 +439,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else wmove(self->window, y, x + MAX(1, wcwidth(key))); } + + if (!ctx->self_is_typing) + set_typingstatus(self, m, true); } /* RETURN key: Execute command or print line */ else if (key == '\n') { @@ -473,6 +499,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) reset_buf(ctx->line, &ctx->pos, &ctx->len); } } + + if (ctx->len <= 0 && ctx->self_is_typing) + set_typingstatus(self, m, false); } static void chat_onDraw(ToxWindow *self, Tox *m) @@ -523,9 +552,16 @@ static void chat_onDraw(ToxWindow *self, Tox *m) break; } + if (friends[self->num].is_typing) + wattron(statusbar->topline, COLOR_PAIR(YELLOW)); + wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " %s ", self->name); wattroff(statusbar->topline, A_BOLD); + + if (friends[self->num].is_typing) + wattroff(statusbar->topline, COLOR_PAIR(YELLOW)); + wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wprintw(statusbar->topline, "[%s]", status_text); wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); @@ -611,6 +647,7 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onInit = &chat_onInit; ret.onMessage = &chat_onMessage; ret.onConnectionChange = &chat_onConnectionChange; + ret.onTypingChange = & chat_onTypingChange; ret.onGroupInvite = &chat_onGroupInvite; ret.onNickChange = &chat_onNickChange; ret.onStatusChange = &chat_onStatusChange; diff --git a/src/friendlist.h b/src/friendlist.h index 029c9a6..0f29f3c 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -14,6 +14,7 @@ typedef struct { int chatwin; bool active; bool online; + bool is_typing; TOX_USERSTATUS status; struct FileReceiver file_receiver; } ToxicFriend; diff --git a/src/main.c b/src/main.c index 3b9bcc1..9c30b43 100644 --- a/src/main.c +++ b/src/main.c @@ -108,6 +108,7 @@ static Tox *init_tox(int ipv4) /* Callbacks */ tox_callback_connection_status(m, on_connectionchange, NULL); + tox_callback_typing_change(m, on_typing_change, NULL); tox_callback_friend_request(m, on_request, NULL); tox_callback_friend_message(m, on_message, NULL); tox_callback_name_change(m, on_nickchange, NULL); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 774b790..eec6ca9 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -84,6 +84,7 @@ struct ToxWindow { 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); + void(*onTypingChange)(ToxWindow *, Tox *, int, int); char name[TOX_MAX_NAME_LENGTH]; int num; @@ -124,6 +125,8 @@ struct ChatContext { int hst_pos; int hst_tot; + bool self_is_typing; + WINDOW *history; WINDOW *linewin; WINDOW *sidebar; @@ -185,6 +188,7 @@ void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t ch 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); +void on_typing_change(Tox *m, int friendnumber, int is_typing, void *userdata); ToxWindow *init_windows(Tox *m); void draw_active_window(Tox *m); diff --git a/src/windows.c b/src/windows.c index 8e068f5..326b659 100644 --- a/src/windows.c +++ b/src/windows.c @@ -37,6 +37,16 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat } } +void on_typing_change(Tox *m, int friendnumber, int is_typing, void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onTypingChange != NULL) + windows[i].onTypingChange(&windows[i], m, friendnumber, is_typing); + } +} + void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { int i;