xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/slk.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * slk.c
31  *
32  * XCurses Library
33  *
34  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35  *
36  */
37 
38 #if M_RCSID
39 #ifndef lint
40 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/slk.c 1.1 1995/07/19 16:38:06 ant Exp $";
41 #endif
42 #endif
43 
44 #include <private.h>
45 
46 /*
47  * Flag for initialisation soft label keys once setupterm() has been called.
48  */
49 int
50 slk_init(int fmt)
51 {
52 	int code = ERR;
53 
54 #ifdef M_CURSES_TRACE
55 	__m_trace("slk_init(%d)", fmt);
56 #endif
57 
58 	if (0 <= fmt && fmt <= 1) {
59 		__m_slk_format = fmt;
60 		code = OK;
61 	}
62 
63 	return __m_return_code("slk_init", code);
64 }
65 
66 int
67 slk_attron(const chtype at)
68 {
69 	int code = ERR;
70 
71 #ifdef M_CURSES_TRACE
72 	__m_trace("slk_attron(%lx)", at);
73 #endif
74 
75 	if (__m_screen->_slk._w != (WINDOW *) 0)
76 		code = wattron(__m_screen->_slk._w, at);
77 
78 	return __m_return_code("slk_attron", code);
79 }
80 
81 int
82 slk_attroff(const chtype at)
83 {
84 	int code = ERR;
85 
86 #ifdef M_CURSES_TRACE
87 	__m_trace("slk_attroff(%lx)", at);
88 #endif
89 
90 	if (__m_screen->_slk._w != (WINDOW *) 0)
91 		code = wattroff(__m_screen->_slk._w, at);
92 
93 	return __m_return_code("slk_attroff", code);
94 }
95 
96 int
97 slk_attrset(const chtype at)
98 {
99 	int code = ERR;
100 
101 #ifdef M_CURSES_TRACE
102 	__m_trace("slk_attrset(%lx)", at);
103 #endif
104 
105 	if (__m_screen->_slk._w != (WINDOW *) 0)
106 		code = wattrset(__m_screen->_slk._w, at);
107 
108 	return __m_return_code("slk_attrset", code);
109 }
110 
111 int
112 slk_attr_off(const attr_t at, void *opts)
113 {
114 	int code = ERR;
115 
116 #ifdef M_CURSES_TRACE
117 	__m_trace("slk_attr_off(%x, %p)", at, opts);
118 #endif
119 
120 	if (__m_screen->_slk._w != (WINDOW *) 0)
121 		code = wattr_off(__m_screen->_slk._w, at, opts);
122 
123 	return __m_return_code("slk_attr_off", code);
124 }
125 
126 int
127 slk_attr_on(const attr_t at, void *opts)
128 {
129 	int code = ERR;
130 
131 #ifdef M_CURSES_TRACE
132 	__m_trace("slk_attr_on(%x, %p)", at, opts);
133 #endif
134 
135 	if (__m_screen->_slk._w != (WINDOW *) 0)
136 		code = wattr_on(__m_screen->_slk._w, at, opts);
137 
138 	return __m_return_code("slk_attr_on", code);
139 }
140 
141 int
142 slk_attr_set(const attr_t at, short co, void *opts)
143 {
144 	int code = ERR;
145 
146 #ifdef M_CURSES_TRACE
147 	__m_trace("slk_attr_set(%x, %d, %p)", at, co, opts);
148 #endif
149 
150 	if (__m_screen->_slk._w != (WINDOW *) 0)
151 		code = wattr_set(__m_screen->_slk._w, at, co, opts);
152 
153 	return __m_return_code("slk_attr_set", code);
154 }
155 
156 int
157 slk_color(short co)
158 {
159 	int code = ERR;
160 
161 #ifdef M_CURSES_TRACE
162 	__m_trace("slk_color(%d)", co);
163 #endif
164 
165 	if (__m_screen->_slk._w != (WINDOW *) 0)
166 		code = wcolor_set(__m_screen->_slk._w, co, (void *) 0);
167 
168 	return __m_return_code("slk_color", code);
169 }
170 
171 int
172 slk_touch()
173 {
174 	int code = ERR;
175 
176 #ifdef M_CURSES_TRACE
177 	__m_trace("slk_touch(void)");
178 #endif
179 
180 	if (__m_screen->_slk._w != (WINDOW *) 0)
181 		code = wtouchln(__m_screen->_slk._w, 0, 1, 1);
182 
183 	return __m_return_code("slk_touch", code);
184 }
185 
186 int
187 slk_clear()
188 {
189 	int code = ERR;
190 
191 #ifdef M_CURSES_TRACE
192 	__m_trace("slk_clear(void)");
193 #endif
194 
195 	if (__m_screen->_slk._w != (WINDOW *) 0) {
196 		if (werase(__m_screen->_slk._w) == OK)
197 			code = wrefresh(__m_screen->_slk._w);
198 	} else if (label_off != (char *) 0) {
199 		(void) tputs(label_off, 1, __m_outc);
200 		(void) fflush(__m_screen->_of);
201 		code = OK;
202 	}
203 
204 	return __m_return_code("slk_clear", code);
205 }
206 
207 int
208 slk_restore()
209 {
210 	int i, code = ERR;
211 
212 #ifdef M_CURSES_TRACE
213 	__m_trace("slk_clear(void)");
214 #endif
215 
216 	if (__m_screen->_slk._w != (WINDOW *) 0) {
217 		for (i = 0; i < 8; ++i) {
218 			if (__m_screen->_slk._labels[i] != (char *) 0) {
219 				(void) slk_set(
220 					i, __m_screen->_slk._labels[i],
221 					__m_screen->_slk._justify[i]
222 				);
223 			}
224 		}
225 
226 		code = slk_refresh();
227 	} else if (label_on != (char *) 0) {
228 		(void) tputs(label_on, 1, __m_outc);
229 		(void) fflush(__m_screen->_of);
230 		code = OK;
231 	}
232 
233 	return __m_return_code("slk_clear", code);
234 }
235 
236 int
237 slk_noutrefresh()
238 {
239 	int code = ERR;
240 
241 #ifdef M_CURSES_TRACE
242 	__m_trace("slk_noutrefresh(void)");
243 #endif
244 
245 	if (__m_screen->_slk._w != (WINDOW *) 0)
246 		code = wnoutrefresh(__m_screen->_slk._w);
247 
248 	return __m_return_code("slk_noutrefresh", code);
249 }
250 
251 int
252 slk_refresh()
253 {
254 	int code = ERR;
255 
256 #ifdef M_CURSES_TRACE
257 	__m_trace("slk_refresh(void)");
258 #endif
259 
260 	if ((code = slk_noutrefresh()) == OK)
261 		code = doupdate();
262 
263 	return __m_return_code("slk_refresh", code);
264 }
265 
266 char *
267 slk_label(int index)
268 {
269 #ifdef M_CURSES_TRACE
270 	__m_trace("slk_label(%d)", index);
271 #endif
272 
273 	return __m_return_pointer("slk_label", __m_screen->_slk._labels[index]);
274 }
275 
276 int
277 slk_set(int index, const char *label, int justify)
278 {
279 	int code = ERR;
280 	wchar_t wcs[M_CCHAR_MAX * 8 + 1];
281 
282 #ifdef M_CURSES_TRACE
283 	__m_trace("slk_set(%d, %p, %d)", index, label, justify);
284 #endif
285 
286 	if (0 < mbstowcs(wcs, label, sizeof wcs))
287 		code = slk_wset(index, wcs, justify);
288 
289 	return __m_return_code("slk_set", code);
290 }
291 
292 int
293 slk_wset(int index, const wchar_t *label, int justify)
294 {
295 	cchar_t cc;
296 	short (*k)[2];
297 	int i, width, code = ERR;
298 	wchar_t wcs[M_CCHAR_MAX * 8 + 1], *wp;
299 	char mbs[MB_LEN_MAX * ((1 + M_CCHAR_MAX) * 8) + 1];
300 
301 	/*
302 	 * These label start columns assume 80 columns in order to
303 	 * fit 8 _slk._labels of 8 columns.
304 	 */
305 	static short format[][8] = {
306 		{ 0, 10, 20, 31, 41, 52, 62, 72 },
307 		{ 0, 10, 20, 30, 42, 52, 62, 72 },
308 	};
309 
310 #ifdef M_CURSES_TRACE
311 	__m_trace("slk_wset(%d, %p, %d)", index, label, justify);
312 #endif
313 
314 	if (index < 1 || 8 < index || justify < 0 || 2 < justify)
315 		goto error1;
316 
317 	if (label == (wchar_t *) 0)
318 		label = M_MB_L("");
319 
320 	/* Copy the characters that fill the first 8 columns of the label. */
321 	for (wp = wcs, width = 0; label != '\0'; label += i, wp += cc._n) {
322 		if ((i = __m_wcs_cc(label, A_NORMAL, 0, &cc)) < 0)
323 			goto error1;
324 
325 
326 		if (8 < (width += __m_cc_width(&cc)))
327 			break;
328 
329 		(void) wcsncpy(wp, cc._wc, cc._n);
330 	}
331 	*wp = '\0';
332 
333 	if (wcstombs(mbs, wcs, sizeof mbs) == (size_t) -1)
334 		goto error1;
335 
336 	/* Remember the new label. */
337 	__m_screen->_slk._justify[index] = (short) justify;
338 	if (__m_screen->_slk._labels[index] != (char *) 0)
339 		free(__m_screen->_slk._labels[index]);
340 	if ((__m_screen->_slk._labels[index] = m_strdup(mbs)) == (char *) 0)
341 		goto error1;
342 
343 	if (__m_screen->_slk._w != (WINDOW *) 0) {
344 		/* Write the justified label into the slk window. */
345 		i = format[__m_slk_format][index];
346 		(void) __m_cc_erase(__m_screen->_slk._w, 0, i, 0, i + 7);
347 
348 		switch (justify) {
349 		case 0:
350 			break;
351 		case 1:
352 			i += width / 2;
353 			break;
354 		case 2:
355 			i = i + 8 - width;
356 			break;
357 		}
358 
359 		(void) mvwaddstr(__m_screen->_slk._w, 0, i, mbs);
360 	} else if (plab_norm != (char *) 0) {
361 		(void) tputs(
362 			tparm(
363 				plab_norm, (long) index, (long) mbs,
364 				0L, 0L, 0L, 0L, 0L, 0L, 0L
365 			), 1, __m_outc
366 		);
367 	} else if (pkey_plab != (char *) 0) {
368 		/* Lookup multibyte sequence for the function key. */
369 		for (i = KEY_F(index), k = __m_keyindex; (*k)[1] != i; ++k)
370 			;
371 
372 		if (cur_term->_str[**k] != (char *) 0) {
373 			(void) tputs(
374 				tparm(
375 					pkey_plab, (long) index,
376 					(long) cur_term->_str[**k],
377 					(long) mbs, 0L, 0L, 0L, 0L, 0L, 0L
378 				), 1, __m_outc
379 			);
380 		}
381 	}
382 
383 	code = OK;
384 error1:
385 	return __m_return_code("slk_wset", code);
386 }
387 
388