diff --git a/src/game_base.c b/src/game_base.c index 41fb352..253abcb 100644 --- a/src/game_base.c +++ b/src/game_base.c @@ -174,6 +174,10 @@ void game_kill(ToxWindow *self) kill_notifs(self->active_box); del_window(self); + + if (get_num_active_windows_type(WINDOW_TYPE_GAME) == 0) { + set_window_refresh_rate(NCURSES_DEFAULT_REFRESH_RATE); + } } static void game_init_abort(const ToxWindow *parent, ToxWindow *self) @@ -300,6 +304,8 @@ int game_initialize(const ToxWindow *parent, Tox *m, GameType type, uint32_t id, set_active_window_index(window_id); + set_window_refresh_rate(NCURSES_GAME_REFRESH_RATE); + return 0; } diff --git a/src/toxic.c b/src/toxic.c index fd572b4..45e678e 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -239,6 +239,12 @@ void cb_toxcore_logger(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t l } } +/* Sets ncurses refresh rate. Lower values make it refresh more often. */ +void set_window_refresh_rate(size_t refresh_rate) +{ + timeout(refresh_rate); +} + static void init_term(void) { #if HAVE_WIDECHAR @@ -256,7 +262,7 @@ static void init_term(void) keypad(stdscr, 1); noecho(); nonl(); - timeout(30); + set_window_refresh_rate(NCURSES_DEFAULT_REFRESH_RATE); if (has_colors()) { short bg_color = COLOR_BLACK; @@ -435,9 +441,7 @@ static void cleanup_init_messages(void) return; } - int i; - - for (i = 0; i < init_messages.num; ++i) { + for (int i = 0; i < init_messages.num; ++i) { free(init_messages.msgs[i]); } @@ -446,19 +450,16 @@ static void cleanup_init_messages(void) static void print_init_messages(ToxWindow *toxwin) { - int i; - - for (i = 0; i < init_messages.num; ++i) { + for (int i = 0; i < init_messages.num; ++i) { line_info_add(toxwin, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]); } } static void load_friendlist(Tox *m) { - size_t i; size_t numfriends = tox_self_get_friend_list_size(m); - for (i = 0; i < numfriends; ++i) { + for (size_t i = 0; i < numfriends; ++i) { friendlist_onFriendAdded(NULL, m, i, false); } diff --git a/src/toxic.h b/src/toxic.h index 6200ba3..943a5c9 100644 --- a/src/toxic.h +++ b/src/toxic.h @@ -50,6 +50,9 @@ #define TIME_STR_SIZE 32 #define COLOR_STR_SIZE 10 /* should fit every color option */ +#define NCURSES_DEFAULT_REFRESH_RATE 100 +#define NCURSES_GAME_REFRESH_RATE 25 + #ifndef MAX_PORT_RANGE #define MAX_PORT_RANGE 65535 #endif @@ -104,6 +107,9 @@ void unlock_status(void); void flag_interface_refresh(void); +/* Sets ncurses refresh rate. Lower values make it refresh more often. */ +void set_window_refresh_rate(size_t refresh_rate); + void exit_toxic_success(Tox *m); void exit_toxic_err(const char *errmsg, int errcode); diff --git a/src/windows.c b/src/windows.c index ee2f0ca..c6f8e67 100644 --- a/src/windows.c +++ b/src/windows.c @@ -934,6 +934,26 @@ int get_num_active_windows(void) return num_active_windows; } +/* Returns the number of active windows of given type. */ +size_t get_num_active_windows_type(WINDOW_TYPE type) +{ + size_t count = 0; + + for (size_t i = 0; i < MAX_WINDOWS_NUM; ++i) { + ToxWindow *w = windows[i]; + + if (w == NULL) { + continue; + } + + if (w->type == type) { + ++count; + } + } + + return count; +} + /* destroys all chat and conference windows (should only be called on shutdown) */ void kill_all_windows(Tox *m) { diff --git a/src/windows.h b/src/windows.h index 788c533..c4a2b12 100644 --- a/src/windows.h +++ b/src/windows.h @@ -309,6 +309,9 @@ ToxWindow *get_window_ptr(size_t i); ToxWindow *get_active_window(void); void draw_window_bar(ToxWindow *self); +/* Returns the number of active windows of given type. */ +size_t get_num_active_windows_type(WINDOW_TYPE type); + /* refresh inactive windows to prevent scrolling bugs. call at least once per second */ void refresh_inactive_windows(void);