diff --git a/src/friendlist.c b/src/friendlist.c index 71d6bb2..e3f5219 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -450,8 +450,6 @@ static void select_friend(ToxWindow *self, wint_t key, int *selected, int num) static void delete_friend(Tox *m, int32_t f_num) { - int i; - if (friends[f_num].chatwin >= 0) { ToxWindow *toxwin = get_window_ptr(friends[f_num].chatwin); @@ -464,6 +462,8 @@ static void delete_friend(Tox *m, int32_t f_num) tox_del_friend(m, f_num); memset(&friends[f_num], 0, sizeof(ToxicFriend)); + int i; + for (i = max_friends_index; i > 0; --i) { if (friends[i - 1].active) break; diff --git a/src/toxic.c b/src/toxic.c index 88f35c9..ac9ecc7 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -594,6 +594,25 @@ static int init_data_files(void) return config_err; } +#define REC_TOX_DO_LOOPS_PER_SEC 25 + +/* Adjusts usleep value so that tox_do runs close to the recommended number of times per second */ +useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, uint64_t cur_time, useconds_t msleepval) +{ + useconds_t new_sleep = msleepval; + ++(*loopcount); + + if (*looptimer == cur_time || *loopcount == REC_TOX_DO_LOOPS_PER_SEC) + return new_sleep; + + double d = (double) *loopcount / REC_TOX_DO_LOOPS_PER_SEC; + new_sleep *= d; + + *looptimer = cur_time; + *loopcount = 0; + return new_sleep; +} + int main(int argc, char *argv[]) { parse_args(argc, argv); @@ -662,6 +681,9 @@ int main(int argc, char *argv[]) } uint64_t last_save = (uint64_t) time(NULL); + uint64_t looptimer = last_save; + useconds_t msleepval = 40000; + uint64_t loopcount = 0; while (true) { update_unix_time(); @@ -676,7 +698,8 @@ int main(int argc, char *argv[]) last_save = cur_time; } - usleep(40000); + msleepval = optimal_msleepval(&looptimer, &loopcount, cur_time, msleepval); + usleep(msleepval); } return 0;