summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcurses/screen/init_costs.c
blob: cda368191dadfd95a2ec085874ea1ced57845fdf (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1988 AT&T	*/
/*	  All Rights Reserved	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*LINTLIBRARY*/

#include	<stdlib.h>
#include	<sys/types.h>
#include	"curses_inc.h"

/*
 * Figure out (roughly) how much each of these capabilities costs.
 * In the parameterized cases, we just take a typical case and
 * use that value.  This is done only once at startup, since it
 * would be too expensive for intensive use.
 */

static  int 	_cost_fn(char *, int);

static	short	offsets[] = {
		    52,		/* insert_character, */
		    21,		/* delete_character, */
		    12,		/* cursor_home, */
		    18,		/* cursor_to_ll, */
		    14,		/* cursor_left, */
		    17,		/* cursor_right, */
		    11,		/* cursor_down, */
		    19,		/* cursor_up, */
		    2,		/* carriage_return, */
		    134,	/* tab, */
		    0,		/* back_tab, */
		    6,		/* clr_eol, */
		    269,	/* clr_bol, */
#define	FIRST_LOOP	13
		    108,	/* parm_ich, */
		    105,	/* parm_dch, */
		    111,	/* parm_left_cursor, */
		    114,	/* parm_up_cursor, */
		    107,	/* parm_down_cursor, */
		    112,	/* parm_right_cursor, */
#define	SECOND_LOOP	19
		};

void
_init_costs(void)
{
	short	*costptr = &(SP->term_costs.icfixed);
	char	**str_array = (char **) cur_strs;
	int	i = 0;
	char	save_xflag = xon_xoff;

	xon_xoff = 0;
/*
 * This next block of code is actually correct in that it takes into
 * account many things that wrefresh has to keep figuring in the function
 * _useidch.  Wrefresh MUST be changed (in the words of Tony Hansen) !!!
 *
 * Wrefresh has been changed (in my words -Phong Vo) !!!!
 */
	*costptr++ = ((enter_insert_mode) && (exit_insert_mode)) ?
	    _cost_fn(enter_insert_mode, 0) + _cost_fn(exit_insert_mode, 0) : 0;

	*costptr++ = ((enter_delete_mode) && (exit_delete_mode)) ?
	    _cost_fn(enter_delete_mode, 0) + _cost_fn(exit_delete_mode, 0) : 0;

	while (i < FIRST_LOOP)
		*costptr++ = _cost_fn(str_array[offsets[i++]], 1);

	while (i < SECOND_LOOP)
		*costptr++ = _cost_fn(tparm_p1(str_array[offsets[i++]], 10), 1);

	*costptr++ = _cost_fn(tparm_p2(cursor_address, 8, 10), 1);
	*costptr++ = _cost_fn(tparm_p1(row_address, 8), 1);

	xon_xoff = save_xflag;
#ifdef	DEBUG
	if (outf) {
		fprintf(outf, "icfixed %d=%d+%d\n", _COST(icfixed),
		    _cost_fn(enter_insert_mode, 0),
		    _cost_fn(exit_insert_mode, 0));
		fprintf(outf, "from ich1 %x '%s' %d\n", insert_character,
		    insert_character, _cost_fn(insert_character, 1));
		fprintf(outf, "ip %x '%s' %d\n", insert_padding,
		    insert_padding, _cost_fn(insert_padding, 1));
		fprintf(outf, "dcfixed %d\n", _COST(dcfixed));
	}
#endif	/* DEBUG */
/*FALLTHROUGH*/
}

static int counter = 0;
int
/* ARGSUSED */
_countchar(char dummy)
{
	counter++;
	return (0);
}

/*
 * Figure out the _COST in characters to print this string.
 * Due to padding, we can't just use strlen, so instead we
 * feed it through tputs and trap the results.
 * Even if the terminal uses xon/xoff handshaking, count the
 * pad chars here since they estimate the real time to do the
 * operation, useful in calculating costs.
 */

static int
_cost_fn(char *str, int affcnt)
{
	if (str == NULL)
		return (LARGECOST);
	counter = 0;
	(void) tputs(str, affcnt, _countchar);
	return (counter);
}