From 1d71e2eb1809d3f7fa04a99a570c7905a2b30a4d Mon Sep 17 00:00:00 2001 From: jfreegman Date: Sun, 18 Dec 2016 00:01:51 -0500 Subject: [PATCH] Port to toktok-c-toxcore branch --- src/chat_commands.c | 26 ++-- src/global_commands.c | 23 ++-- src/group_commands.c | 21 ++-- src/groupchat.c | 286 +++++++++--------------------------------- src/groupchat.h | 39 +----- src/misc_tools.c | 38 ++++-- src/misc_tools.h | 2 +- src/toxic.c | 37 +++--- src/toxic.h | 13 +- src/windows.c | 29 ++--- src/windows.h | 7 +- 11 files changed, 163 insertions(+), 358 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 6ed5f05..52eb8bf 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -92,8 +92,10 @@ void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a return; } - if (tox_invite_friend(m, self->num, groupnum) == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group."); + TOX_ERR_CONFERENCE_INVITE err; + + if (!tox_conference_invite(m, self->num, groupnum, &err)) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group (error %d)", err); return; } @@ -116,25 +118,23 @@ void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar return; } - int groupnum = -1; + if (type != TOX_CONFERENCE_TYPE_TEXT) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Toxic does not support audio groups."); + return; + } - if (type == TOX_GROUPCHAT_TYPE_TEXT) - groupnum = tox_join_groupchat(m, self->num, (uint8_t *) groupkey, length); + TOX_ERR_CONFERENCE_JOIN err; - /*#ifdef AUDIO - else - groupnum = toxav_join_av_groupchat(m, self->num, (uint8_t *) groupkey, length, - NULL, NULL); - #endif*/ + uint32_t groupnum = tox_conference_join(m, self->num, (uint8_t *) groupkey, length, &err); - if (groupnum == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize."); + if (err != TOX_ERR_CONFERENCE_JOIN_OK) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize (error %d)", err); return; } if (init_groupchat_win(prompt, m, groupnum, type) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); - tox_del_groupchat(m, groupnum); + tox_conference_delete(m, groupnum, NULL); return; } diff --git a/src/global_commands.c b/src/global_commands.c index 144ddb2..ebc0234 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -336,32 +336,31 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg uint8_t type; if (!strcasecmp(argv[1], "audio")) - type = TOX_GROUPCHAT_TYPE_AV; + type = TOX_CONFERENCE_TYPE_AV; else if (!strcasecmp(argv[1], "text")) - type = TOX_GROUPCHAT_TYPE_TEXT; + type = TOX_CONFERENCE_TYPE_TEXT; else { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Valid group types are: text | audio"); return; } - int groupnum = -1; + if (type != TOX_CONFERENCE_TYPE_TEXT) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Toxic does not support audio groups."); + return; + } - if (type == TOX_GROUPCHAT_TYPE_TEXT) - groupnum = tox_add_groupchat(m); + TOX_ERR_CONFERENCE_NEW err; - /*#ifdef AUDIO - else - groupnum = toxav_add_av_groupchat(m, NULL, NULL); - #endif*/ + uint32_t groupnum = tox_conference_new(m, &err); - if (groupnum == -1) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize."); + if (err != TOX_ERR_CONFERENCE_NEW_OK) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize (error %d)", err); return; } if (init_groupchat_win(prompt, m, groupnum, type) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); - tox_del_groupchat(m, groupnum); + tox_conference_delete(m, groupnum, NULL); return; } diff --git a/src/group_commands.c b/src/group_commands.c index fb73a0b..8265679 100644 --- a/src/group_commands.c +++ b/src/group_commands.c @@ -30,18 +30,25 @@ void cmd_set_title(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + TOX_ERR_CONFERENCE_TITLE err; char title[MAX_STR_SIZE]; if (argc < 1) { - int tlen = tox_group_get_title(m, self->num, (uint8_t *) title, TOX_MAX_NAME_LENGTH); + size_t tlen = tox_conference_get_title_size(m, self->num, &err); - if (tlen != -1) { - title[tlen] = '\0'; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is set to: %s", title); - } else { + if (err != TOX_ERR_CONFERENCE_TITLE_OK) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is not set"); + return; } + if (!tox_conference_get_title(m, self->num, (uint8_t *) title, &err)) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is not set"); + return; + } + + title[tlen] = '\0'; + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Title is set to: %s", title); + return; } @@ -55,8 +62,8 @@ void cmd_set_title(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg int len = strlen(title) - 1; title[len] = '\0'; - if (tox_group_set_title(m, self->num, (uint8_t *) title, len) != 0) { - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title."); + if (!tox_conference_set_title(m, self->num, (uint8_t *) title, len, &err)) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to set title (error %d)", err); return; } diff --git a/src/groupchat.c b/src/groupchat.c index 649b130..04f13e2 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -97,26 +97,13 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = { { "/requests" }, { "/status" }, { "/title" }, - -#ifdef AUDIO - - { "/lsdev" }, - { "/sdev" }, - { "/mute" }, - { "/sense" }, - -#endif /* AUDIO */ }; -#ifdef AUDIO -static int group_audio_open_out_device(int groupnum); -static int group_audio_close_out_device(int groupnum); -#endif /* AUDIO */ - -int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, uint8_t type) +int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type) { - if (groupnum > MAX_GROUPCHAT_NUM) + if (groupnum > MAX_GROUPCHAT_NUM) { return -1; + } ToxWindow self = new_group_chat(m, groupnum); int i; @@ -141,14 +128,6 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, uint8_t type) memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME)); groupchats[i].oldpeer_name_lengths[0] = (uint16_t) strlen(UNKNOWN_NAME); -#ifdef AUDIO - - if (type == TOX_GROUPCHAT_TYPE_AV) - if (group_audio_open_out_device(i) == -1) - fprintf(stderr, "Group Audio failed to init\n"); - -#endif /* AUDIO */ - set_active_window(groupchats[i].chatwin); if (i == max_groupchat_index) @@ -176,12 +155,9 @@ static void kill_groupchat_window(ToxWindow *self) del_window(self); } -void close_groupchat(ToxWindow *self, Tox *m, int groupnum) +void close_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum) { - tox_del_groupchat(m, groupnum); -#ifdef AUDIO - group_audio_close_out_device(groupnum); -#endif + tox_conference_delete(m, groupnum, NULL); free(groupchats[groupnum].peer_names); free(groupchats[groupnum].oldpeer_names); @@ -239,8 +215,8 @@ void redraw_groupchat_win(ToxWindow *self) } -static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int peernum, - const char *msg, uint16_t len) +static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, uint32_t groupnum, uint32_t peernum, + TOX_MESSAGE_TYPE type, const char *msg, size_t len) { if (self->num != groupnum) return; @@ -275,47 +251,12 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); - line_info_add(self, timefrmt, nick, NULL, IN_MSG, 0, nick_clr, "%s", msg); + line_info_add(self, timefrmt, nick, NULL, type == TOX_MESSAGE_TYPE_NORMAL ? IN_MSG : IN_ACTION, 0, nick_clr, "%s", msg); write_to_log(msg, nick, ctx->log, false); } -static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, const char *action, - uint16_t len) -{ - if (self->num != groupnum) - return; - - ChatContext *ctx = self->chatwin; - - char nick[TOX_MAX_NAME_LENGTH]; - get_group_nick_truncate(m, nick, peernum, groupnum); - - char selfnick[TOX_MAX_NAME_LENGTH]; - tox_self_get_name(m, (uint8_t *) selfnick); - - size_t n_len = tox_self_get_name_size(m); - selfnick[n_len] = '\0'; - - if (strcasestr(action, selfnick)) { - sound_notify(self, generic_message, NT_WNDALERT_0 | user_settings->bell_on_message, NULL); - - if (self->active_box != -1) - box_silent_notify2(self, NT_NOFOCUS, self->active_box, "* %s %s", nick, action ); - else - box_silent_notify(self, NT_NOFOCUS, &self->active_box, self->name, "* %s %s", nick, action); - } else { - sound_notify(self, silent, NT_WNDALERT_1, NULL); - } - - char timefrmt[TIME_STR_SIZE]; - get_time_str(timefrmt, sizeof(timefrmt)); - - line_info_add(self, timefrmt, nick, NULL, IN_ACTION, 0, 0, "%s", action); - write_to_log(action, nick, ctx->log, true); -} - -static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum, int peernum, const char *title, - uint8_t length) +static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, uint32_t groupnum, uint32_t peernum, const char *title, + size_t length) { ChatContext *ctx = self->chatwin; @@ -341,7 +282,7 @@ static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum, } /* Puts two copies of peerlist/lengths in chat instance */ -static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], int npeers) +static void copy_peernames(Tox *m, uint32_t gnum, size_t npeers) { /* Assumes these are initiated in init_groupchat_win */ free(groupchats[gnum].peer_names); @@ -351,27 +292,34 @@ static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], ui int N = TOX_MAX_NAME_LENGTH; - groupchats[gnum].peer_names = malloc(sizeof(uint8_t) * npeers * N); - groupchats[gnum].oldpeer_names = malloc(sizeof(uint8_t) * npeers * N); - groupchats[gnum].peer_name_lengths = malloc(sizeof(uint16_t) * npeers); - groupchats[gnum].oldpeer_name_lengths = malloc(sizeof(uint16_t) * npeers); + groupchats[gnum].peer_names = calloc(1, sizeof(uint8_t) * npeers * N); + groupchats[gnum].oldpeer_names = calloc(1, sizeof(uint8_t) * npeers * N); + groupchats[gnum].peer_name_lengths = calloc(1, sizeof(uint16_t) * npeers); + groupchats[gnum].oldpeer_name_lengths = calloc(1, sizeof(uint16_t) * npeers); if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL || groupchats[gnum].peer_name_lengths == NULL || groupchats[gnum].oldpeer_name_lengths == NULL) { - exit_toxic_err("failed in copy_peernames", FATALERR_MEMORY); + exit_toxic_err("failed in copy_peernames()", FATALERR_MEMORY); } uint16_t u_len = strlen(UNKNOWN_NAME); + int i; for (i = 0; i < npeers; ++i) { - if (!lengths[i]) { + uint8_t name[TOX_MAX_NAME_LENGTH]; + TOX_ERR_CONFERENCE_PEER_QUERY err; + + size_t n_len = tox_conference_peer_get_name_size(m, gnum, i, &err); + + if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) { memcpy(&groupchats[gnum].peer_names[i * N], UNKNOWN_NAME, u_len); groupchats[gnum].peer_names[i * N + u_len] = '\0'; groupchats[gnum].peer_name_lengths[i] = u_len; } else { - uint16_t n_len = MIN(lengths[i], TOXIC_MAX_NAME_LENGTH - 1); - memcpy(&groupchats[gnum].peer_names[i * N], peerlist[i], n_len); + tox_conference_peer_get_name(m, gnum, i, name, NULL); + n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1); + memcpy(&groupchats[gnum].peer_names[i * N], name, n_len); groupchats[gnum].peer_names[i * N + n_len] = '\0'; groupchats[gnum].peer_name_lengths[i] = n_len; filter_str((char *) &groupchats[gnum].peer_names[i * N], n_len); @@ -385,8 +333,8 @@ static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], ui struct group_add_thrd { Tox *m; ToxWindow *self; - int peernum; - int groupnum; + uint32_t peernum; + uint32_t groupnum; time_t timestamp; pthread_t tid; pthread_attr_t attr; @@ -429,7 +377,8 @@ void *group_add_wait(void *data) pthread_exit(NULL); } -static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t change) +static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, uint32_t groupnum, uint32_t peernum, + TOX_CONFERENCE_STATE_CHANGE change) { if (self->num != groupnum) return; @@ -437,8 +386,15 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu if (groupnum > max_groupchat_index) return; - groupchats[groupnum].num_peers = tox_group_number_peers(m, groupnum); - int num_peers = groupchats[groupnum].num_peers; + TOX_ERR_CONFERENCE_PEER_QUERY err; + + uint32_t num_peers = tox_conference_peer_count(m, groupnum, &err); + + if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) { + groupchats[groupnum].num_peers = num_peers; + } else { + num_peers = groupchats[groupnum].num_peers; + } if (peernum > num_peers) return; @@ -446,28 +402,19 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu /* get old peer name before updating name list */ uint8_t oldpeername[TOX_MAX_NAME_LENGTH]; - if (change != TOX_CHAT_CHANGE_PEER_ADD) { - memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum * TOX_MAX_NAME_LENGTH], - sizeof(oldpeername)); + if (change != TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN) { + memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum * TOX_MAX_NAME_LENGTH], sizeof(oldpeername)); uint16_t old_n_len = groupchats[groupnum].oldpeer_name_lengths[peernum]; oldpeername[old_n_len] = '\0'; } /* Update name/len lists */ - uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH]; - uint16_t tmp_peerlens[num_peers]; - - if (tox_group_get_names(m, groupnum, tmp_peerlist, tmp_peerlens, num_peers) == -1) { - memset(tmp_peerlist, 0, sizeof(tmp_peerlist)); - memset(tmp_peerlens, 0, sizeof(tmp_peerlens)); - } - - copy_peernames(groupnum, tmp_peerlist, tmp_peerlens, num_peers); + copy_peernames(m, groupnum, num_peers); /* get current peername then sort namelist */ uint8_t peername[TOX_MAX_NAME_LENGTH]; - if (change != TOX_CHAT_CHANGE_PEER_DEL) { + if (change != TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT) { uint16_t n_len = groupchats[groupnum].peer_name_lengths[peernum]; memcpy(peername, &groupchats[groupnum].peer_names[peernum * TOX_MAX_NAME_LENGTH], sizeof(peername)); peername[n_len] = '\0'; @@ -482,7 +429,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu get_time_str(timefrmt, sizeof(timefrmt)); switch (change) { - case TOX_CHAT_CHANGE_PEER_ADD: + case TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN: if (!timed_out(groupchats[groupnum].start_time, GROUP_EVENT_WAIT)) break; @@ -512,7 +459,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu break; - case TOX_CHAT_CHANGE_PEER_DEL: + case TOX_CONFERENCE_STATE_CHANGE_PEER_EXIT: event = "has left the room"; line_info_add(self, timefrmt, (char *) oldpeername, NULL, DISCONNECTION, 0, RED, event); @@ -522,7 +469,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu write_to_log(event, (char *) oldpeername, ctx->log, true); break; - case TOX_CHAT_CHANGE_PEER_NAME: + case TOX_CONFERENCE_STATE_CHANGE_PEER_NAME_CHANGE: if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT)) return; @@ -549,9 +496,10 @@ static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, char *a return; } - if (tox_group_action_send(m, self->num, (uint8_t *) action, strlen(action)) == -1) { - const char *errmsg = " * Failed to send action."; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); + TOX_ERR_CONFERENCE_SEND_MESSAGE err; + + if (!tox_conference_send_message(m, self->num, TOX_MESSAGE_TYPE_ACTION, (uint8_t *) action, strlen(action), &err)) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send action (error %d)", err); } } @@ -641,9 +589,10 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr) execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE); } } else { - if (tox_group_message_send(m, self->num, (uint8_t *) line, strlen(line)) == -1) { - const char *errmsg = " * Failed to send message."; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); + TOX_ERR_CONFERENCE_SEND_MESSAGE err; + + if (!tox_conference_send_message(m, self->num, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) line, strlen(line), &err)) { + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send message (error %d)", err); } } } @@ -701,7 +650,7 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m) wmove(ctx->sidebar, i + 2, 1); pthread_mutex_lock(&Winthread.lock); - int peer = i + groupchats[self->num].side_pos; + uint32_t peer = i + groupchats[self->num].side_pos; pthread_mutex_unlock(&Winthread.lock); /* truncate nick to fit in side panel without modifying list */ @@ -766,125 +715,7 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) wmove(self->window, y2 - CURS_Y_OFFSET, 0); } - -#ifdef AUDIO -static int group_audio_open_out_device(int groupnum) -{ - char dname[MAX_STR_SIZE]; - get_primary_device_name(output, dname, sizeof(dname)); - dname[MAX_STR_SIZE - 1] = '\0'; - - groupchats[groupnum].audio.dvhandle = alcOpenDevice(dname); - - if (groupchats[groupnum].audio.dvhandle == NULL) - return -1; - - groupchats[groupnum].audio.dvctx = alcCreateContext(groupchats[groupnum].audio.dvhandle, NULL); - alcMakeContextCurrent(groupchats[groupnum].audio.dvctx); - alGenBuffers(OPENAL_BUFS, groupchats[groupnum].audio.buffers); - alGenSources((uint32_t) 1, &groupchats[groupnum].audio.source); - alSourcei(groupchats[groupnum].audio.source, AL_LOOPING, AL_FALSE); - - if (alcGetError(groupchats[groupnum].audio.dvhandle) != AL_NO_ERROR) { - group_audio_close_out_device(groupnum); - groupchats[groupnum].audio.dvhandle = NULL; - groupchats[groupnum].audio.dvctx = NULL; - return -1; - } - - alSourceQueueBuffers(groupchats[groupnum].audio.source, OPENAL_BUFS, groupchats[groupnum].audio.buffers); - alSourcePlay(groupchats[groupnum].audio.source); - - return 0; -} - -static int group_audio_close_out_device(int groupnum) -{ - if (!groupchats[groupnum].audio.dvhandle) - return -1; - - if (!groupchats[groupnum].audio.dvctx) - return -1; - - if (alcGetCurrentContext() != groupchats[groupnum].audio.dvctx) - alcMakeContextCurrent(groupchats[groupnum].audio.dvctx); - - alDeleteSources((uint32_t) 1, &groupchats[groupnum].audio.source); - alDeleteBuffers(OPENAL_BUFS, groupchats[groupnum].audio.buffers); - - alcMakeContextCurrent(NULL); - alcDestroyContext(groupchats[groupnum].audio.dvctx); - - if (!alcCloseDevice(groupchats[groupnum].audio.dvhandle)) - return -1; - - return 0; -} - -// static int group_audio_write(int peernum, int groupnum, const int16_t *pcm, unsigned int samples, uint8_t channels, -// unsigned int sample_rate) -// { -// if (!pcm) -// return -1; - -// if (channels == 0 || channels > 2) -// return -2; - -// ALuint bufid; -// ALint processed = 0, queued = 0; - -// alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_PROCESSED, &processed); -// alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_QUEUED, &queued); -// fprintf(stderr, "source: %d, queued: %d, processed: %d\n", groupchats[groupnum].audio.source, queued, processed); - -// if (processed) { -// ALuint bufids[processed]; -// alSourceUnqueueBuffers(groupchats[groupnum].audio.source, processed, bufids); -// alDeleteBuffers(processed - 1, bufids + 1); -// bufid = bufids[0]; -// } else if (queued < 16) { -// alGenBuffers(1, &bufid); -// } else { -// return -3; -// } - -// int length = samples * channels * sizeof(int16_t); - -// alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, length, sample_rate); -// alSourceQueueBuffers(groupchats[groupnum].audio.source, 1, &bufid); - -// ALint state; -// alGetSourcei(groupchats[groupnum].audio.source, AL_SOURCE_STATE, &state); - -// if (state != AL_PLAYING) -// alSourcePlay(groupchats[groupnum].audio.source); - -// return 0; -// } - -static void groupchat_onWriteDevice(ToxWindow *self, Tox *m, uint32_t groupnum, int peernum, const int16_t *pcm, - unsigned int samples, uint8_t channels, unsigned int sample_rate) -{ - return; - - // if (groupnum != self->num) - // return; - - // if (peernum < 0) - // return; - - // if (groupchats[groupnum].audio.dvhandle == NULL) - // fprintf(stderr, "dvhandle is null)\n"); - - // if (groupchats[groupnum].audio.dvctx == NULL) - // fprintf(stderr, "ctx is null\n"); - - // int ret = group_audio_write(peernum, groupnum, pcm, samples, channels, sample_rate); - // fprintf(stderr, "write: %d\n", ret); -} -#endif /* AUDIO */ - -ToxWindow new_group_chat(Tox *m, int groupnum) +ToxWindow new_group_chat(Tox *m, uint32_t groupnum) { ToxWindow ret; memset(&ret, 0, sizeof(ret)); @@ -897,13 +728,8 @@ ToxWindow new_group_chat(Tox *m, int groupnum) ret.onInit = &groupchat_onInit; ret.onGroupMessage = &groupchat_onGroupMessage; ret.onGroupNamelistChange = &groupchat_onGroupNamelistChange; - ret.onGroupAction = &groupchat_onGroupAction; ret.onGroupTitleChange = &groupchat_onGroupTitleChange; -#ifdef AUDIO - ret.onWriteDevice = &groupchat_onWriteDevice; -#endif - snprintf(ret.name, sizeof(ret.name), "Group %d", groupnum); ChatContext *chatwin = calloc(1, sizeof(ChatContext)); diff --git a/src/groupchat.h b/src/groupchat.h index cee031c..4413efc 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -26,61 +26,30 @@ #include "toxic.h" #include "windows.h" -#ifdef AUDIO -#include "audio_call.h" -#endif - -#ifdef AUDIO -#ifdef __APPLE__ -#include -#include -#else -#include -#include -/* compatibility with older versions of OpenAL */ -#ifndef ALC_ALL_DEVICES_SPECIFIER -#include -#endif /* ALC_ALL_DEVICES_SPECIFIER */ -#endif /* __APPLE__ */ -#endif /* AUDIO */ - #define SIDEBAR_WIDTH 16 #define SDBAR_OFST 2 /* Offset for the peer number box at the top of the statusbar */ #define MAX_GROUPCHAT_NUM MAX_WINDOWS_NUM - 2 #define GROUP_EVENT_WAIT 3 -#ifdef AUDIO -struct GAudio { - ALCdevice *dvhandle; /* Handle of device selected/opened */ - ALCcontext *dvctx; - ALuint source; - ALuint buffers[OPENAL_BUFS]; -}; -#endif /* AUDIO */ - typedef struct { int chatwin; bool active; uint8_t type; - int num_peers; + uint32_t num_peers; int side_pos; /* current position of the sidebar - used for scrolling up and down */ time_t start_time; uint8_t *peer_names; uint8_t *oldpeer_names; uint16_t *peer_name_lengths; uint16_t *oldpeer_name_lengths; - -#ifdef AUDIO - struct GAudio audio; -#endif } GroupChat; -void close_groupchat(ToxWindow *self, Tox *m, int groupnum); -int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, uint8_t type); +void close_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum); +int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type); /* destroys and re-creates groupchat window with or without the peerlist */ void redraw_groupchat_win(ToxWindow *self); -ToxWindow new_group_chat(Tox *m, int groupnum); +ToxWindow new_group_chat(Tox *m, uint32_t groupnum); #endif /* #define GROUPCHAT_H */ diff --git a/src/misc_tools.c b/src/misc_tools.c index 1eda579..ccba40c 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -323,35 +323,53 @@ void str_to_lower(char *str) Returns nick len */ size_t get_nick_truncate(Tox *m, char *buf, uint32_t friendnum) { - size_t len = tox_friend_get_name_size(m, friendnum, NULL); + TOX_ERR_FRIEND_QUERY err; + size_t len = tox_friend_get_name_size(m, friendnum, &err); - if (len == 0) { - strcpy(buf, UNKNOWN_NAME); - len = strlen(UNKNOWN_NAME); + if (err != TOX_ERR_FRIEND_QUERY_OK) { + goto on_error; } else { - tox_friend_get_name(m, friendnum, (uint8_t *) buf, NULL); + if (!tox_friend_get_name(m, friendnum, (uint8_t *) buf, NULL)) { + goto on_error; + } } len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); buf[len] = '\0'; filter_str(buf, len); return len; + +on_error: + strcpy(buf, UNKNOWN_NAME); + len = strlen(UNKNOWN_NAME); + buf[len] = '\0'; + return len; } /* same as get_nick_truncate but for groupchats */ -int get_group_nick_truncate(Tox *m, char *buf, int peernum, int groupnum) +int get_group_nick_truncate(Tox *m, char *buf, uint32_t peernum, uint32_t groupnum) { - int len = tox_group_peername(m, groupnum, peernum, (uint8_t *) buf); + TOX_ERR_CONFERENCE_PEER_QUERY err; + size_t len = tox_conference_peer_get_name_size(m, groupnum, peernum, &err); - if (len == -1) { - strcpy(buf, UNKNOWN_NAME); - len = strlen(UNKNOWN_NAME); + if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) { + goto on_error; + } else { + if (!tox_conference_peer_get_name(m, groupnum, peernum, (uint8_t *) buf, NULL)) { + goto on_error; + } } len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); buf[len] = '\0'; filter_str(buf, len); return len; + +on_error: + strcpy(buf, UNKNOWN_NAME); + len = strlen(UNKNOWN_NAME); + buf[len] = '\0'; + return len; } /* copies data to msg buffer. diff --git a/src/misc_tools.h b/src/misc_tools.h index 8a5e36d..a7c964f 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -126,7 +126,7 @@ void str_to_lower(char *str); size_t get_nick_truncate(Tox *m, char *buf, uint32_t friendnum); /* same as get_nick_truncate but for groupchats */ -int get_group_nick_truncate(Tox *m, char *buf, int peernum, int groupnum); +int get_group_nick_truncate(Tox *m, char *buf, uint32_t peernum, uint32_t groupnum); /* copies data to msg buffer. returns length of msg, which will be no larger than size-1 */ diff --git a/src/toxic.c b/src/toxic.c index 6699872..14483a4 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -520,24 +520,23 @@ int store_data(Tox *m, const char *path) static void init_tox_callbacks(Tox *m) { - tox_callback_self_connection_status(m, prompt_onSelfConnectionChange, NULL); - tox_callback_friend_connection_status(m, on_connectionchange, NULL); - tox_callback_friend_typing(m, on_typing_change, NULL); - tox_callback_friend_request(m, on_request, NULL); - tox_callback_friend_message(m, on_message, NULL); - tox_callback_friend_name(m, on_nickchange, NULL); - tox_callback_friend_status(m, on_statuschange, NULL); - tox_callback_friend_status_message(m, on_statusmessagechange, NULL); - tox_callback_friend_read_receipt(m, on_read_receipt, NULL); - tox_callback_group_invite(m, on_groupinvite, NULL); - tox_callback_group_message(m, on_groupmessage, NULL); - tox_callback_group_action(m, on_groupaction, NULL); - tox_callback_group_namelist_change(m, on_group_namelistchange, NULL); - tox_callback_group_title(m, on_group_titlechange, NULL); - tox_callback_file_recv(m, on_file_recv, NULL); - tox_callback_file_chunk_request(m, on_file_chunk_request, NULL); - tox_callback_file_recv_control(m, on_file_control, NULL); - tox_callback_file_recv_chunk(m, on_file_recv_chunk, NULL); + tox_callback_self_connection_status(m, prompt_onSelfConnectionChange); + tox_callback_friend_connection_status(m, on_connectionchange); + tox_callback_friend_typing(m, on_typing_change); + tox_callback_friend_request(m, on_request); + tox_callback_friend_message(m, on_message); + tox_callback_friend_name(m, on_nickchange); + tox_callback_friend_status(m, on_statuschange); + tox_callback_friend_status_message(m, on_statusmessagechange); + tox_callback_friend_read_receipt(m, on_read_receipt); + tox_callback_conference_invite(m, on_groupinvite); + tox_callback_conference_message(m, on_groupmessage); + tox_callback_conference_namelist_change(m, on_group_namelistchange); + tox_callback_conference_title(m, on_group_titlechange); + tox_callback_file_recv(m, on_file_recv); + tox_callback_file_chunk_request(m, on_file_chunk_request); + tox_callback_file_recv_control(m, on_file_control); + tox_callback_file_recv_chunk(m, on_file_recv_chunk); } static void init_tox_options(struct Tox_Options *tox_opts) @@ -747,7 +746,7 @@ static void do_toxic(Tox *m) return; } - tox_iterate(m); + tox_iterate(m, NULL); do_tox_connection(m); pthread_mutex_unlock(&Winthread.lock); } diff --git a/src/toxic.h b/src/toxic.h index ba093b3..c4b97a2 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -114,12 +114,13 @@ void on_nickchange(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t void on_statuschange(Tox *m, uint32_t friendnumber, TOX_USER_STATUS status, void *userdata); void on_statusmessagechange(Tox *m, uint32_t friendnumber, const uint8_t *string, size_t length, void *userdata); void on_friendadded(Tox *m, uint32_t friendnumber, bool sort); -void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length, void *userdata); -void on_groupaction(Tox *m, int groupnumber, int peernumber, const uint8_t *action, uint16_t length, void *userdata); -void on_groupinvite(Tox *m, int32_t friendnumber, uint8_t type, const uint8_t *group_pub_key, uint16_t length, - void *userdata); -void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t change, void *userdata); -void on_group_titlechange(Tox *m, int groupnumber, int peernumber, const uint8_t *title, uint8_t length, +void on_groupmessage(Tox *m, uint32_t groupnumber, uint32_t peernumber, TOX_MESSAGE_TYPE type, + const uint8_t *message, size_t length, void *userdata); +void on_groupinvite(Tox *m, uint32_t friendnumber, TOX_CONFERENCE_TYPE type, const uint8_t *group_pub_key, + size_t length, void *userdata); +void on_group_namelistchange(Tox *m, uint32_t groupnumber, uint32_t peernumber, TOX_CONFERENCE_STATE_CHANGE change, + void *userdata); +void on_group_titlechange(Tox *m, uint32_t groupnumber, uint32_t peernumber, const uint8_t *title, size_t length, void *userdata); void on_file_chunk_request(Tox *m, uint32_t friendnumber, uint32_t filenumber, uint64_t position, size_t length, void *userdata); diff --git a/src/windows.c b/src/windows.c index 5b233be..4596ba1 100644 --- a/src/windows.c +++ b/src/windows.c @@ -151,8 +151,8 @@ void on_friendadded(Tox *m, uint32_t friendnumber, bool sort) store_data(m, DATA_FILE); } -void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *message, uint16_t length, - void *userdata) +void on_groupmessage(Tox *m, uint32_t groupnumber, uint32_t peernumber, TOX_MESSAGE_TYPE type, + const uint8_t *message, size_t length, void *userdata) { char msg[MAX_STR_SIZE + 1]; length = copy_tox_str(msg, sizeof(msg), (const char *) message, length); @@ -161,26 +161,12 @@ void on_groupmessage(Tox *m, int groupnumber, int peernumber, const uint8_t *mes for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onGroupMessage != NULL) - windows[i].onGroupMessage(&windows[i], m, groupnumber, peernumber, msg, length); + windows[i].onGroupMessage(&windows[i], m, groupnumber, peernumber, type, msg, length); } } -void on_groupaction(Tox *m, int groupnumber, int peernumber, const uint8_t *action, uint16_t length, - void *userdata) -{ - char msg[MAX_STR_SIZE + 1]; - length = copy_tox_str(msg, sizeof(msg), (const char *) action, length); - - size_t i; - - for (i = 0; i < MAX_WINDOWS_NUM; ++i) { - if (windows[i].onGroupAction != NULL) - windows[i].onGroupAction(&windows[i], m, groupnumber, peernumber, msg, length); - } -} - -void on_groupinvite(Tox *m, int32_t friendnumber, uint8_t type, const uint8_t *group_pub_key, uint16_t length, - void *userdata) +void on_groupinvite(Tox *m, uint32_t friendnumber, TOX_CONFERENCE_TYPE type, const uint8_t *group_pub_key, + size_t length, void *userdata) { size_t i; @@ -190,7 +176,8 @@ void on_groupinvite(Tox *m, int32_t friendnumber, uint8_t type, const uint8_t *g } } -void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t change, void *userdata) +void on_group_namelistchange(Tox *m, uint32_t groupnumber, uint32_t peernumber, TOX_CONFERENCE_STATE_CHANGE change, + void *userdata) { size_t i; @@ -200,7 +187,7 @@ void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t ch } } -void on_group_titlechange(Tox *m, int groupnumber, int peernumber, const uint8_t *title, uint8_t length, +void on_group_titlechange(Tox *m, uint32_t groupnumber, uint32_t peernumber, const uint8_t *title, size_t length, void *userdata) { char data[MAX_STR_SIZE + 1]; diff --git a/src/windows.h b/src/windows.h index 4484eaf..06d1abd 100644 --- a/src/windows.h +++ b/src/windows.h @@ -121,11 +121,10 @@ struct ToxWindow { void(*onNickChange)(ToxWindow *, Tox *, uint32_t, const char *, size_t); void(*onStatusChange)(ToxWindow *, Tox *, uint32_t, TOX_USER_STATUS); void(*onStatusMessageChange)(ToxWindow *, uint32_t, const char *, size_t); - void(*onGroupMessage)(ToxWindow *, Tox *, int, int, const char *, uint16_t); - void(*onGroupAction)(ToxWindow *, Tox *, int, int, const char *, uint16_t); + void(*onGroupMessage)(ToxWindow *, Tox *, uint32_t, uint32_t, TOX_MESSAGE_TYPE, const char *, size_t); void(*onGroupInvite)(ToxWindow *, Tox *, int32_t, uint8_t, const char *, uint16_t); - void(*onGroupNamelistChange)(ToxWindow *, Tox *, int, int, uint8_t); - void(*onGroupTitleChange)(ToxWindow *, Tox *, int, int, const char *, uint8_t); + void(*onGroupNamelistChange)(ToxWindow *, Tox *, uint32_t, uint32_t, TOX_CONFERENCE_STATE_CHANGE); + void(*onGroupTitleChange)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); void(*onFileChunkRequest)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, size_t); void(*onFileRecvChunk)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, const char *, size_t); void(*onFileControl)(ToxWindow *, Tox *, uint32_t, uint32_t, TOX_FILE_CONTROL);