Source

siren / screen.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
 * Copyright (c) 2011 Tim van der Molen <tbvdm@xs4all.nl>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef __NetBSD__
#include <sys/param.h>
#endif

#include <curses.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "siren.h"

#ifdef HAVE_RESIZETERM
#include <sys/ioctl.h>
#endif

#define SCREEN_OBJ_ACTIVE	0
#define SCREEN_OBJ_ERROR	1
#define SCREEN_OBJ_INFO		2
#define SCREEN_OBJ_PLAYER	3
#define SCREEN_OBJ_PROMPT	4
#define SCREEN_OBJ_SELECTOR	5
#define SCREEN_OBJ_STATUS	6
#define SCREEN_OBJ_TITLE	7
#define SCREEN_OBJ_VIEW		8

#define SCREEN_PLAYER_NROWS	2
#define SCREEN_STATUS_NROWS	1
#define SCREEN_TITLE_NROWS	1

#define SCREEN_TITLE_ROW	0
#define SCREEN_VIEW_ROW		1

/*
 * NetBSD's curses implementation before version 6.0 has two bugs that need to
 * be worked around.
 */
#if defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000 && \
    !defined(NCURSES_VERSION)
#define SCREEN_HAVE_NETBSD_CURSES_BUGS
#endif

#ifdef SCREEN_HAVE_NETBSD_CURSES_BUGS
#undef clrtoeol
#define clrtoeol		screen_clrtoeol
#endif

static short int		 screen_get_colour(const char *, enum colour);
static void			 screen_msg_vprintf(int, const char *,
				    va_list);
static void			 screen_print_row(const char *);
static void			 screen_view_print_row(chtype, const char *);
static void			 screen_vprintf(const char *, va_list);
#if defined(HAVE_RESIZETERM) && defined(TIOCGWINSZ)
static void			 screen_resize(void);
#endif

static pthread_mutex_t		 screen_curses_mtx = PTHREAD_MUTEX_INITIALIZER;
static int			 screen_have_colours;
static int			 screen_player_row;
static int			 screen_status_col;
static int			 screen_status_row;
static int			 screen_view_current_row;
static int			 screen_view_selected_row;
static int			 screen_view_nrows;

static char			*screen_row = NULL;
static size_t			 screen_rowsize;

#ifdef HAVE_USE_DEFAULT_COLORS
static int			 screen_have_default_colours;
#endif

static const struct {
	const int		 attrib;
	const chtype		 curses_attrib;
} screen_attribs[] = {
	{ ATTRIB_BLINK,		 A_BLINK },
	{ ATTRIB_BOLD,		 A_BOLD },
	{ ATTRIB_DIM,		 A_DIM },
	{ ATTRIB_REVERSE,	 A_REVERSE },
	{ ATTRIB_STANDOUT,	 A_STANDOUT },
	{ ATTRIB_UNDERLINE,	 A_UNDERLINE }
};

static const struct {
	const enum colour	 colour;
	const short int		 curses_colour;
} screen_colours[] = {
	{ COLOUR_BLACK,		 COLOR_BLACK },
	{ COLOUR_BLUE,		 COLOR_BLUE },
	{ COLOUR_CYAN,		 COLOR_CYAN },
	{ COLOUR_DEFAULT,	 -1 },
	{ COLOUR_GREEN,		 COLOR_GREEN },
	{ COLOUR_MAGENTA,	 COLOR_MAGENTA },
	{ COLOUR_RED,		 COLOR_RED },
	{ COLOUR_WHITE,		 COLOR_WHITE },
	{ COLOUR_YELLOW,	 COLOR_YELLOW }
};

static const struct {
	const int		 key;
	const int		 curses_key;
} screen_keys[] = {
	{ K_BACKSPACE,		'\b' },
	{ K_BACKSPACE,		'\177' /* ^? */ },
	{ K_BACKSPACE,		KEY_BACKSPACE },
	{ K_BACKTAB,		KEY_BTAB },
	{ K_DELETE,		KEY_DC },
	{ K_DOWN,		KEY_DOWN },
	{ K_END,		KEY_END },
	{ K_ENTER,		'\n' },
	{ K_ENTER,		'\r' },
	{ K_ENTER,		KEY_ENTER },
	{ K_ESCAPE,		K_CTRL('[') },
	{ K_F1,			KEY_F(1) },
	{ K_F2,			KEY_F(2) },
	{ K_F3,			KEY_F(3) },
	{ K_F4,			KEY_F(4) },
	{ K_F5,			KEY_F(5) },
	{ K_F6,			KEY_F(6) },
	{ K_F7,			KEY_F(7) },
	{ K_F8,			KEY_F(8) },
	{ K_F9,			KEY_F(9) },
	{ K_F10,		KEY_F(10) },
	{ K_F11,		KEY_F(11) },
	{ K_F12,		KEY_F(12) },
	{ K_F13,		KEY_F(13) },
	{ K_F14,		KEY_F(14) },
	{ K_F15,		KEY_F(15) },
	{ K_F16,		KEY_F(16) },
	{ K_F17,		KEY_F(17) },
	{ K_F18,		KEY_F(18) },
	{ K_F19,		KEY_F(19) },
	{ K_F20,		KEY_F(20) },
	{ K_HOME,		KEY_HOME },
	{ K_INSERT,		KEY_IC },
	{ K_LEFT,		KEY_LEFT },
	{ K_PAGEDOWN,		KEY_NPAGE },
	{ K_PAGEUP,		KEY_PPAGE },
	{ K_RIGHT,		KEY_RIGHT },
	{ K_TAB,		'\t' },
	{ K_UP,			KEY_UP }
};

static struct {
	chtype			 attr;
	const short int		 colour_pair;
	const char		*option_attr;
	const char		*option_bg;
	const char		*option_fg;
} screen_objects[] = {
	{ A_NORMAL, 1, "active-attr",     "active-bg",     "active-fg" },
	{ A_NORMAL, 2, "error-attr",      "error-bg",      "error-fg" },
	{ A_NORMAL, 3, "info-attr",       "info-bg",       "info-fg" },
	{ A_NORMAL, 4, "player-attr",     "player-bg",     "player-fg" },
	{ A_NORMAL, 5, "prompt-attr",     "prompt-bg",     "prompt-fg" },
	{ A_NORMAL, 6, "selection-attr",  "selection-bg",  "selection-fg" },
	{ A_NORMAL, 7, "status-attr",     "status-bg",     "status-fg" },
	{ A_NORMAL, 8, "view-title-attr", "view-title-bg", "view-title-fg" },
	{ A_NORMAL, 9, "view-attr",       "view-bg",       "view-fg" }
};

#ifdef SCREEN_HAVE_NETBSD_CURSES_BUGS
/*
 * NetBSD's clrtoeol() does not set the background attributes of the characters
 * it clears. This has been fixed in NetBSD 6.0. We work around this by using
 * our own version which simply writes spaces to the end of the row.
 */
static void
screen_clrtoeol(void)
{
	int col, i, row;

	getyx(stdscr, row, col);
	for (i = col; i < COLS; i++)
		(void)addch(' ');
	(void)move(row, col);
}
#endif

static void
screen_configure_attribs(void)
{
	size_t	 i, j;
	int	 attr;
	chtype	 cattr;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	for (i = 0; i < NELEMENTS(screen_objects); i++) {
		attr = option_get_attrib(screen_objects[i].option_attr);
		cattr = A_NORMAL;

		for (j = 0; j < NELEMENTS(screen_attribs); j++)
			if (attr & screen_attribs[j].attrib)
				cattr |= screen_attribs[j].curses_attrib;

		screen_objects[i].attr = cattr;
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

static void
screen_configure_colours(void)
{
	size_t		i;
	short int	bg, fg;

	if (!screen_have_colours)
		return;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	for (i = 0; i < NELEMENTS(screen_objects); i++) {
		bg = screen_get_colour(screen_objects[i].option_bg,
		    COLOUR_BLACK);
		fg = screen_get_colour(screen_objects[i].option_fg,
		    COLOUR_WHITE);

		if (init_pair(screen_objects[i].colour_pair, fg, bg) == OK)
			screen_objects[i].attr |=
			    (chtype)COLOR_PAIR(screen_objects[i].colour_pair);
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_configure_cursor(void)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if (option_get_boolean("show-cursor"))
		(void)curs_set(1);
	else
		(void)curs_set(0);

#ifdef SCREEN_HAVE_NETBSD_CURSES_BUGS
	/*
	 * NetBSD's curs_set() does not let the cursor-visibility change take
	 * effect immediately. This has been fixed in NetBSD 6.0. We work
	 * around this by calling refresh() after curs_set().
	 */
	(void)refresh();
#endif
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_configure_objects(void)
{
	screen_configure_attribs();
	screen_configure_colours();
	screen_print();
}

static void
screen_configure_rows(void)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);

	/* Calculate the number of rows available to the view area. */
	if (LINES < SCREEN_TITLE_NROWS + SCREEN_PLAYER_NROWS +
	    SCREEN_STATUS_NROWS)
		screen_view_nrows = 0;
	else
		screen_view_nrows = LINES - SCREEN_TITLE_NROWS -
		    SCREEN_PLAYER_NROWS - SCREEN_STATUS_NROWS;

	/* Calculate the row offsets of the player and status areas. */
	screen_player_row = SCREEN_TITLE_NROWS + screen_view_nrows;
	screen_status_row = screen_player_row + SCREEN_PLAYER_NROWS;

	/* (Re)allocate memory for the row buffer. */
	if (screen_rowsize != (size_t)COLS + 1) {
		screen_rowsize = COLS + 1;
		screen_row = xrealloc(screen_row, screen_rowsize);
	}

	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_end(void)
{
	(void)endwin();
	free(screen_row);
}

static short int
screen_get_colour(const char *option, enum colour default_colour)
{
	size_t		i;
	enum colour	colour;

	colour = option_get_colour(option);
#ifdef HAVE_USE_DEFAULT_COLORS
	if (colour == COLOUR_DEFAULT && !screen_have_default_colours)
#else
	if (colour == COLOUR_DEFAULT)
#endif
		colour = default_colour;

	for (i = 0; i < NELEMENTS(screen_colours); i++)
		if (colour == screen_colours[i].colour)
			return screen_colours[i].curses_colour;

	LOG_FATALX("unknown colour");
	/* NOTREACHED */
}

int
screen_get_key(void)
{
	size_t	i;
	int	key;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	while ((key = getch()) == ERR && errno == EINTR);
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);

	if (key != ERR) {
		for (i = 0; i < NELEMENTS(screen_keys); i++)
			if (key == screen_keys[i].curses_key)
				return screen_keys[i].key;

		/* Only allow ASCII characters. */
		if (key > -1 && key < 128)
			return key;
	}

	return K_NONE;
}

unsigned int
screen_get_ncols(void)
{
	return (unsigned int)COLS;
}

void
screen_init(void)
{
	(void)initscr();
	(void)cbreak();
	(void)noecho();
	(void)nonl();
	(void)keypad(stdscr, TRUE);

	if (has_colors() == TRUE) {
		if (start_color() == ERR)
			LOG_ERRX("start_color() failed");
		else {
			screen_have_colours = 1;
#ifdef HAVE_USE_DEFAULT_COLORS
			if (use_default_colors() == OK)
				screen_have_default_colours = 1;
#endif
		}
	}

	screen_configure_rows();
	screen_configure_cursor();
	screen_configure_attribs();
	screen_configure_colours();
}

void
screen_msg_error_printf(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	screen_msg_vprintf(SCREEN_OBJ_ERROR, fmt, ap);
	va_end(ap);
}

void
screen_msg_error_vprintf(const char *fmt, va_list ap)
{
	screen_msg_vprintf(SCREEN_OBJ_ERROR, fmt, ap);
}

void
screen_msg_info_vprintf(const char *fmt, va_list ap)
{
	screen_msg_vprintf(SCREEN_OBJ_INFO, fmt, ap);
}

static void
screen_msg_vprintf(int obj, const char *fmt, va_list ap)
{
	int col, row;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	getyx(stdscr, row, col);
	if (move(screen_status_row, 0) == OK) {
		bkgdset(screen_objects[obj].attr);
		screen_vprintf(fmt, ap);
		(void)move(row, col);
		(void)refresh();
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_player_status_printf(const struct format *fmt,
    const struct format_variable *fmtvar, size_t nfmtvars)
{
	int col, row;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	format_snprintf(screen_row, screen_rowsize, fmt, fmtvar, nfmtvars);
	getyx(stdscr, row, col);
	if (move(screen_player_row + 1, 0) == OK) {
		bkgdset(screen_objects[SCREEN_OBJ_PLAYER].attr);
		screen_print_row(screen_row);
		(void)move(row, col);
		(void)refresh();
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_player_track_printf(const struct format *fmt, const struct track *track)
{
	int col, row;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if (track == NULL)
		screen_row[0] = '\0';
	else
		format_track_snprintf(screen_row, screen_rowsize, fmt, track);

	getyx(stdscr, row, col);
	if (move(screen_player_row, 0) == OK) {
		bkgdset(screen_objects[SCREEN_OBJ_PLAYER].attr);
		screen_print_row(screen_row);
		(void)move(row, col);
		(void)refresh();
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_print(void)
{
	view_print();
	player_print();
	if (input_get_mode() == INPUT_MODE_PROMPT)
		prompt_print();
	else
		screen_status_clear();
}

/*
 * The screen_curses_mtx mutex must be locked before calling this function.
 */
static void
screen_print_row(const char *s)
{
	int col UNUSED, row;

	(void)addnstr(s, COLS);

	if (strlen(s) < (size_t)COLS)
		(void)clrtoeol();
	else {
		/*
		 * If the length of the printed string is equal to the screen
		 * width, the cursor will advance to the next row. Undo this by
		 * moving the cursor back to the original row.
		 */
		getyx(stdscr, row, col);
		(void)move(row - 1, COLS - 1);
	}
}

void
screen_prompt_begin(void)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	(void)curs_set(1);
	screen_status_col = 0;
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_prompt_end(void)
{
	screen_status_clear();
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if (!option_get_boolean("show-cursor"))
		(void)curs_set(0);
	(void)move(screen_view_selected_row, 0);
	(void)refresh();
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_prompt_printf(size_t cursorpos, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if ((int)cursorpos >= COLS && COLS > 0)
		screen_status_col = COLS - 1;
	else
		screen_status_col = (int)cursorpos;

	if (move(screen_status_row, 0) == OK) {
		bkgdset(screen_objects[SCREEN_OBJ_PROMPT].attr);
		screen_vprintf(fmt, ap);
		(void)move(screen_status_row, screen_status_col);
		(void)refresh();
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
	va_end(ap);
}

void
screen_refresh(void)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	(void)clear();
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
#if defined(HAVE_RESIZETERM) && defined(TIOCGWINSZ)
	screen_resize();
#endif
	screen_print();
}

#if defined(HAVE_RESIZETERM) && defined(TIOCGWINSZ)
static void
screen_resize(void)
{
	struct winsize ws;

	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
		LOG_ERR("ioctl");
		return;
	}

	/*
	 * resizeterm() will fail if the width or height of the terminal window
	 * is not larger than zero.
	 */
	if (ws.ws_col == 0 || ws.ws_row == 0)
		return;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if (resizeterm(ws.ws_row, ws.ws_col) == ERR)
		/*
		 * resizeterm() might have failed to allocate memory, so treat
		 * this as fatal.
		 */
		LOG_FATALX("resizeterm() failed");
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);

	screen_configure_rows();
}
#endif

void
screen_status_clear(void)
{
	int col, row;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	getyx(stdscr, row, col);
	if (move(screen_status_row, 0) == OK) {
		bkgdset(screen_objects[SCREEN_OBJ_STATUS].attr);
		(void)clrtoeol();
		(void)move(row, col);
		(void)refresh();
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

unsigned int
screen_view_get_nrows(void)
{
	return (unsigned int)screen_view_nrows;
}

void
screen_view_print(const char *s)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	screen_view_print_row(screen_objects[SCREEN_OBJ_VIEW].attr, s);
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_view_print_active(const char *s)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	screen_view_print_row(screen_objects[SCREEN_OBJ_ACTIVE].attr, s);
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_view_print_begin(void)
{
	int i;

	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	/* Clear the view area. */
	bkgdset(screen_objects[SCREEN_OBJ_VIEW].attr);
	for (i = 0; i < screen_view_nrows; i++) {
		(void)move(SCREEN_VIEW_ROW + i, 0);
		(void)clrtoeol();
	}
	screen_view_current_row = screen_view_selected_row = SCREEN_VIEW_ROW;
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_view_print_end(void)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if (input_get_mode() == INPUT_MODE_PROMPT)
		(void)move(screen_status_row, screen_status_col);
	else
		(void)move(screen_view_selected_row, 0);
	(void)refresh();
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

/*
 * The screen_curses_mtx mutex must be locked before calling this function.
 */
static void
screen_view_print_row(chtype attr, const char *s)
{
	bkgdset(attr);
	(void)move(screen_view_current_row++, 0);
	screen_print_row(s);
}

void
screen_view_print_selected(const char *s)
{
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	screen_view_selected_row = screen_view_current_row;
	screen_view_print_row(screen_objects[SCREEN_OBJ_SELECTOR].attr, s);
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
}

void
screen_view_title_printf(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	if (move(SCREEN_TITLE_ROW, 0) == OK) {
		bkgdset(screen_objects[SCREEN_OBJ_TITLE].attr);
		screen_vprintf(fmt, ap);
		/* No refresh() yet; screen_view_print_end() will do that. */
	}
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
	va_end(ap);
}

void
screen_view_title_printf_right(const char *fmt, ...)
{
	va_list	ap;
	int	len;

	va_start(ap, fmt);
	XPTHREAD_MUTEX_LOCK(&screen_curses_mtx);
	len = xvsnprintf(screen_row, screen_rowsize, fmt, ap);
	if (len < COLS)
		(void)mvaddstr(SCREEN_TITLE_ROW, COLS - len, screen_row);
	else
		(void)mvaddstr(SCREEN_TITLE_ROW, 0, screen_row + len - COLS);
	/* No refresh() yet; screen_view_print_end() will do that. */
	XPTHREAD_MUTEX_UNLOCK(&screen_curses_mtx);
	va_end(ap);
}

/*
 * The screen_curses_mtx mutex must be locked before calling this function.
 */
static void
screen_vprintf(const char *fmt, va_list ap)
{
	int col UNUSED, len, row;

	len = xvsnprintf(screen_row, screen_rowsize, fmt, ap);
	(void)addnstr(screen_row, COLS);

	if (len < COLS)
		(void)clrtoeol();
	else {
		/*
		 * If the length of the printed string is equal to the screen
		 * width, the cursor will advance to the next row. Undo this by
		 * moving the cursor back to the original row.
		 */
		getyx(stdscr, row, col);
		(void)move(row - 1, COLS - 1);
	}
}