diff --git a/chatclient-cli.c b/chatclient-cli.c index 80eb11f..74f2cbc 100755 --- a/chatclient-cli.c +++ b/chatclient-cli.c @@ -129,13 +129,13 @@ static void add_new_message(const char* str) { messages.data = realloc(messages.data, sizeof(*messages.data) * messages.capacity); } messages.data[messages.size++] = strdup(str); - pthread_mutex_unlock(&messages.lock); if (render_state.scroll_offset > 0) { render_state.scroll_offset++; render_state.unread++; render_state.title_header = 0; } render_state.message_field = 0; + pthread_mutex_unlock(&messages.lock); } #define PRINT_SPACES(n) printw("%*s", (n), "") @@ -206,7 +206,6 @@ static void cc_render_message_field( const unsigned int inner_w = w - x - 2; const unsigned int inner_h = h - y - 2; - pthread_mutex_lock(&messages.lock); const int max_offset = messages.size > inner_h ? messages.size - inner_h : 0; if (render_state.scroll_offset > max_offset) render_state.scroll_offset = max_offset; @@ -216,7 +215,6 @@ static void cc_render_message_field( for (int i = first; i < last; i++) { mvprintw(y + 1 + (i - first), x + 1, "%-*.*s", inner_w, inner_w, messages.data[i]); } - pthread_mutex_unlock(&messages.lock); } static void cc_render_input_field( @@ -264,6 +262,7 @@ static void cc_render_ui( const int w, const int h ) { + pthread_mutex_lock(&messages.lock); bool do_refresh = false; if (!render_state.title_header) { @@ -288,6 +287,7 @@ static void cc_render_ui( move(render_state.cursor.y, render_state.cursor.x); refresh(); } + pthread_mutex_unlock(&messages.lock); } static void update_terminal_size() { @@ -447,7 +447,6 @@ int main(int argc, char* argv[]) { } } - // Redraw title every second const time_t now = time(NULL); if (now > last_second) { last_second = now;