@@ -1190,15 +1190,48 @@ chafa_term_read_event (ChafaTerm *term, guint timeout_ms)
11901190}
11911191
11921192void
1193- chafa_term_write (ChafaTerm * term , gconstpointer data , gsize len )
1193+ chafa_term_write (ChafaTerm * term , gconstpointer data , gint len )
11941194{
1195- /* FIXME: Apply emergency brakes if buffer limit reached */
1195+ while (len > 0 )
1196+ {
1197+ gint n_written ;
11961198
1197- g_mutex_lock (& term -> mutex );
1198- term -> out_drained = FALSE;
1199- chafa_byte_fifo_push (term -> out_fifo , data , len );
1200- g_cond_broadcast (& term -> cond );
1201- g_mutex_unlock (& term -> mutex );
1199+ g_mutex_lock (& term -> mutex );
1200+
1201+ /* Wait for partial drain if necessary */
1202+
1203+ for (;;)
1204+ {
1205+ gint queued = chafa_byte_fifo_get_len (term -> out_fifo );
1206+
1207+ if (queued == 0 || queued + len <= term -> out_buf_max )
1208+ break ;
1209+
1210+ if (term -> out_shutdown_done )
1211+ {
1212+ g_mutex_unlock (& term -> mutex );
1213+ goto out ;
1214+ }
1215+
1216+ g_cond_wait (& term -> cond , & term -> mutex );
1217+ }
1218+
1219+ /* Push and signal */
1220+
1221+ term -> out_drained = FALSE;
1222+ n_written = MIN (len , term -> out_buf_max );
1223+
1224+ chafa_byte_fifo_push (term -> out_fifo , data , n_written );
1225+
1226+ len -= n_written ;
1227+ data = ((const gchar * ) data ) + n_written ;
1228+
1229+ g_cond_broadcast (& term -> cond );
1230+ g_mutex_unlock (& term -> mutex );
1231+ }
1232+
1233+ out :
1234+ return ;
12021235}
12031236
12041237gint
@@ -1247,7 +1280,7 @@ chafa_term_flush (ChafaTerm *term)
12471280}
12481281
12491282void
1250- chafa_term_write_err (ChafaTerm * term , gconstpointer data , gsize len )
1283+ chafa_term_write_err (ChafaTerm * term , gconstpointer data , gint len )
12511284{
12521285 g_mutex_lock (& term -> mutex );
12531286 term -> out_drained = FALSE;
0 commit comments