Skip to content

Commit

Permalink
Merge branch 'add-outgoing-typing-notifications' of https://github.co…
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed Oct 9, 2014
2 parents f99c4a0 + 5b5f638 commit d1f976f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
66 changes: 63 additions & 3 deletions tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void log_read(Tox *tox, int fid)
fclose(file);
}

static void tox_thread_message(Tox *tox, ToxAv *av, uint8_t msg, uint16_t param1, uint16_t param2, void *data);
static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg, uint16_t param1, uint16_t param2, void *data);

void tox_postmessage(uint8_t msg, uint16_t param1, uint16_t param2, void *data)
{
Expand Down Expand Up @@ -407,6 +407,38 @@ void tox_settingschanged(void)
list_start();
}

#define UTOX_TYPING_NOTIFICATION_TIMEOUT (5ul*1000*1000*1000)

static struct {
Tox *tox;
uint16_t friendnumber;
uint64_t time;
_Bool sent_value;
} typing_state = {
.tox = NULL,
.friendnumber = 0,
.time = 0,
.sent_value = 0,
};

static void utox_thread_work_for_typing_notifications(Tox *tox, uint64_t time)
{
if(typing_state.tox != tox) {
// Guard against Tox engine restarts.
return;
}

_Bool is_typing = (time < typing_state.time + UTOX_TYPING_NOTIFICATION_TIMEOUT);
if(typing_state.sent_value ^ is_typing) {
// Need to send an update.
if(!tox_set_user_is_typing(tox, typing_state.friendnumber, is_typing)){
// Successfully sent. Mark new state.
typing_state.sent_value = is_typing;
debug("Sent typing state to friend (%d): %d\n", typing_state.friendnumber, typing_state.sent_value);
}
}
}

void tox_thread(void *UNUSED(args))
{
Tox *tox;
Expand Down Expand Up @@ -487,11 +519,12 @@ TOP:;
tox_thread_msg = 0;
break;
}
tox_thread_message(tox, av, msg->msg, msg->param1, msg->param2, msg->data);
tox_thread_message(tox, av, time, msg->msg, msg->param1, msg->param2, msg->data);
tox_thread_msg = 0;
}

utox_thread_work_for_transfers(tox, time);
utox_thread_work_for_typing_notifications(tox, time);

uint32_t interval = tox_do_interval(tox);
yieldcpu((interval > 20) ? 20 : interval);
Expand All @@ -515,7 +548,7 @@ TOP:;
tox_thread_init = 0;
}

static void tox_thread_message(Tox *tox, ToxAv *av, uint8_t msg, uint16_t param1, uint16_t param2, void *data)
static void tox_thread_message(Tox *tox, ToxAv *av, uint64_t time, uint8_t msg, uint16_t param1, uint16_t param2, void *data)
{
switch(msg) {
case TOX_SETNAME: {
Expand Down Expand Up @@ -657,6 +690,33 @@ static void tox_thread_message(Tox *tox, ToxAv *av, uint8_t msg, uint16_t param1
*/
tox_group_action_send(tox, param1, data, param2);
free(data);
}

case TOX_SET_TYPING: {
/* param1: friend #
*/

// Check if user has switched to another friend window chat.
// Take care not to react on obsolete data from old Tox instance.
_Bool need_resetting = (typing_state.tox == tox) &&
(typing_state.friendnumber != param1) &&
(typing_state.sent_value);

if(need_resetting) {
// Tell previous friend that he's betrayed.
tox_set_user_is_typing(tox, typing_state.friendnumber, 0);
// Mark that new friend doesn't know that we're typing yet.
typing_state.sent_value = 0;
}

// Mark us as typing to this friend at the moment.
// utox_thread_work_for_typing_notifications() will
// send a notification if it deems necessary.
typing_state.tox = tox;
typing_state.friendnumber = param1;
typing_state.time = time;

//debug("Set typing state for friend (%d): %d\n", typing_state.friendnumber, typing_state.sent_value);
break;
}

Expand Down
1 change: 1 addition & 0 deletions tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum {
TOX_SENDACTION,
TOX_SENDMESSAGEGROUP,
TOX_SENDACTIONGROUP,
TOX_SET_TYPING,
TOX_CALL,
TOX_CALL_VIDEO,
TOX_CALL_VIDEO_ON,
Expand Down
14 changes: 14 additions & 0 deletions ui_edits.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ static void edit_msg_onenter(void)
edit_msg.length = 0;
}

static void edit_msg_onchange(void)
{
if(sitem->item == ITEM_FRIEND) {
FRIEND *f = sitem->data;

if(!f->online) {
return;
}

tox_postmessage(TOX_SET_TYPING, (f - friend), 0, NULL);
}
}

static void edit_search_onchange(void)
{
char_t *data = edit_search_data;
Expand Down Expand Up @@ -188,6 +201,7 @@ edit_msg = {
.maxlength = sizeof(edit_msg_data),
.data = edit_msg_data,
.onenter = edit_msg_onenter,
.onchange = edit_msg_onchange,
},

edit_search = {
Expand Down

0 comments on commit d1f976f

Please sign in to comment.