1203790Sfabient/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4203790Sfabient * Copyright (c) 2009, Fabien Thomas
5203790Sfabient * All rights reserved.
6203790Sfabient *
7203790Sfabient * Redistribution and use in source and binary forms, with or without
8203790Sfabient * modification, are permitted provided that the following conditions
9203790Sfabient * are met:
10203790Sfabient * 1. Redistributions of source code must retain the above copyright
11203790Sfabient *    notice, this list of conditions and the following disclaimer.
12203790Sfabient * 2. Redistributions in binary form must reproduce the above copyright
13203790Sfabient *    notice, this list of conditions and the following disclaimer in the
14203790Sfabient *    documentation and/or other materials provided with the distribution.
15203790Sfabient *
16203790Sfabient * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17203790Sfabient * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18203790Sfabient * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19203790Sfabient * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20203790Sfabient * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21203790Sfabient * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22203790Sfabient * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23203790Sfabient * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24203790Sfabient * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25203790Sfabient * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26203790Sfabient * SUCH DAMAGE.
27203790Sfabient *
28203790Sfabient * $FreeBSD: stable/11/usr.sbin/pmcstat/pmcstat_top.h 330449 2018-03-05 07:26:05Z eadler $
29203790Sfabient */
30203790Sfabient
31203790Sfabient#ifndef	_PMCSTAT_TOP_H_
32203790Sfabient#define	_PMCSTAT_TOP_H_
33203790Sfabient
34203790Sfabient/* Return the ncurses attributes for the given value. */
35203790Sfabient#define PMCSTAT_ATTRPERCENT(b) 					\
36203790Sfabient    ((b) > 10.0 ? (args.pa_topcolor ? COLOR_PAIR(1) : A_BOLD) :	\
37203790Sfabient    ((b) >  5.0 ? (args.pa_topcolor ? COLOR_PAIR(2) : 0) : 	\
38203790Sfabient    ((b) >  2.5 ? (args.pa_topcolor ? COLOR_PAIR(3) : 0) : 0)))
39203790Sfabient
40203790Sfabient/* Print to the default ncurse windows if on a terminal or to the file. */
41203790Sfabient#define PMCSTAT_PRINTW(...) do {			\
42203790Sfabient	if (args.pa_toptty)				\
43203790Sfabient		printw(__VA_ARGS__);			\
44203790Sfabient	else						\
45203790Sfabient		fprintf(args.pa_printfile, __VA_ARGS__);\
46203790Sfabient} while (0)
47203790Sfabient
48203790Sfabient/* If ncurses mode active set attributes. */
49203790Sfabient#define PMCSTAT_ATTRON(b) do {				\
50203790Sfabient	if (args.pa_toptty)				\
51203790Sfabient		attron(b);				\
52203790Sfabient} while (0)
53203790Sfabient
54203790Sfabient/* If ncurses mode active unset attributes. */
55203790Sfabient#define PMCSTAT_ATTROFF(b) do {				\
56203790Sfabient	if (args.pa_toptty)				\
57203790Sfabient		attroff(b);				\
58203790Sfabient} while (0)
59203790Sfabient
60203790Sfabient/* Erase screen and set cursor to top left. */
61203790Sfabient#define PMCSTAT_PRINTBEGIN() do {			\
62203790Sfabient	if (args.pa_toptty)				\
63203790Sfabient		clear();				\
64203790Sfabient} while (0)
65203790Sfabient
66203790Sfabient/* Flush buffer to backend. */
67203790Sfabient#define PMCSTAT_PRINTEND() do {				\
68203790Sfabient	if (!args.pa_toptty) {				\
69203790Sfabient		PMCSTAT_PRINTW("---\n");		\
70203790Sfabient		fflush(args.pa_printfile);		\
71203790Sfabient	} else						\
72203790Sfabient		refresh();				\
73203790Sfabient} while (0)
74203790Sfabient
75203790Sfabient/* Function prototypes */
76203790Sfabient
77203790Sfabient#endif	/* _PMCSTAT_TOP_H_ */
78