Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: attributes.c,v 1.31 2019/07/25 20:18:50 uwe Exp $	*/

/*-
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Julian Coleman.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: attributes.c,v 1.31 2019/07/25 20:18:50 uwe Exp $");
#endif				/* not lint */

#include "curses.h"
#include "curses_private.h"

static int __wattr_off(WINDOW *, attr_t);
static int __wattr_on(WINDOW *, attr_t);
static void __wcolor_set(WINDOW *, attr_t);


#ifndef _CURSES_USE_MACROS
#ifdef HAVE_WCHAR
/*
 * attr_get --
 *	Get wide attributes and color pair from stdscr
 */
/* ARGSUSED */
int
attr_get(attr_t *attr, short *pair, void *opts)
{
	return wattr_get(stdscr, attr, pair, opts);
}

/*
 * attr_on --
 *	Test and set wide attributes on stdscr
 */
/* ARGSUSED */
int
attr_on(attr_t attr, void *opts)
{
	return wattr_on(stdscr, attr, opts);
}

/*
 * attr_off --
 *	Test and unset wide attributes on stdscr
 */
/* ARGSUSED */
int
attr_off(attr_t attr, void *opts)
{
	return wattr_off(stdscr, attr, opts);
}

/*
 * attr_set --
 *	Set wide attributes and color pair on stdscr
 */
/* ARGSUSED */
int
attr_set(attr_t attr, short pair, void *opts)
{
	return wattr_set(stdscr, attr, pair, opts);
}

/*
 * color_set --
 *	Set color pair on stdscr
 */
/* ARGSUSED */
int
color_set(short pair, void *opts)
{
	return wcolor_set(stdscr, pair, opts);
}
#endif /* HAVE_WCHAR */

/*
 * attron --
 *	Test and set attributes on stdscr
 */
int
attron(int attr)
{
	return wattron(stdscr, attr);
}

/*
 * attroff --
 *	Test and unset attributes on stdscr.
 */
int
attroff(int attr)
{
	return wattroff(stdscr, attr);
}

/*
 * attrset --
 *	Set specific attribute modes.
 *	Unset others.  On stdscr.
 */
int
attrset(int attr)
{
	return wattrset(stdscr, attr);
}
#endif	/* _CURSES_USE_MACROS */


#ifdef HAVE_WCHAR
/*
 * wattr_get --
 *	Get wide attributes and colour pair from window
 *	Note that attributes also includes colour.
 */
/* ARGSUSED */
int
wattr_get(WINDOW *win, attr_t *attr, short *pair, void *opts)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattr_get: win %p\n", win);
#endif
	if (attr != NULL) {
		*attr = win->wattr;
#ifdef HAVE_WCHAR
		*attr &= WA_ATTRIBUTES;
#endif
	}

	if (pair != NULL)
		*pair = PAIR_NUMBER(win->wattr);
	return OK;
}

/*
 * wattr_on --
 *	Test and set wide attributes on window
 */
int
wattr_on(WINDOW *win, attr_t attr, void *opts)
{
	if (__predict_false(opts != NULL))
		return ERR;

	return __wattr_on(win, attr);
}

/*
 * wattr_off --
 *	Test and unset wide attributes on window
 *
 *	Note that the 'me' sequence unsets all attributes.  We handle
 *	which attributes should really be set in refresh.c:makech().
 */
int
wattr_off(WINDOW *win, attr_t attr, void *opts)
{
	if (__predict_false(opts != NULL))
		return ERR;

	return __wattr_off(win, attr);
}


/*
 * wattr_set --
 *	Set wide attributes and color pair on window
 */
int
wattr_set(WINDOW *win, attr_t attr, short pair, void *opts)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattr_set: win %p, attr %08x, pair %d\n",
	    win, attr, pair);
#endif
	if (__predict_false(opts != NULL))
		return ERR;

	/*
	 * This overwrites any colour setting from the attributes
	 * and is compatible with ncurses.
	 */
	attr = (attr & ~__COLOR) | COLOR_PAIR(pair);

	__wattr_off(win, WA_ATTRIBUTES);
	__wattr_on(win, attr);
	return OK;
}

/*
 * wcolor_set --
 *	Set color pair on window
 */
/* ARGSUSED */
int
wcolor_set(WINDOW *win, short pair, void *opts)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_COLOR, "wolor_set: win %p, pair %d\n", win, pair);
#endif
	__wcolor_set(win, (attr_t) COLOR_PAIR(pair));
	return OK;
}
#endif /* HAVE_WCHAR */


/*
 * getattrs --
 *	Get window attributes.
 */
chtype
getattrs(WINDOW *win)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "getattrs: win %p\n", win);
#endif
	return((chtype) win->wattr);
}

/*
 * wattron --
 *	Test and set attributes.
 */
int
wattron(WINDOW *win, int attr)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattron: win %p, attr %08x\n", win, attr);
#endif
	return __wattr_on(win, (attr_t) attr);
}

/*
 * wattroff --
 *	Test and unset attributes.
 */
int
wattroff(WINDOW *win, int attr)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattroff: win %p, attr %08x\n", win, attr);
#endif
	return __wattr_off(win, (attr_t) attr);
}

/*
 * wattrset --
 *	Set specific attribute modes.
 *	Unset others.
 */
int
wattrset(WINDOW *win, int attr)
{
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattrset: win %p, attr %08x\n", win, attr);
#endif
	__wattr_off(win, __ATTRIBUTES);
	__wattr_on(win, (attr_t) attr);
	return OK;
}

/*
 * termattrs --
 *	Get terminal attributes
 */
chtype
termattrs(void)
{
	chtype ch = 0;

#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "termattrs\n");
#endif
	if (exit_attribute_mode != NULL) {
#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "termattrs: have exit attribute mode\n");
#endif
		if (enter_blink_mode != NULL)
			ch |= __BLINK;
		if (enter_bold_mode != NULL)
			ch |= __BOLD;
		if (enter_dim_mode != NULL)
			ch |= __DIM;
		if (enter_secure_mode != NULL)
			ch |= __BLANK;
		if (enter_protected_mode != NULL)
			ch |= __PROTECT;
		if (enter_reverse_mode != NULL)
			ch |= __REVERSE;
	}
	if (enter_standout_mode != NULL && exit_standout_mode != NULL)
		ch |= __STANDOUT;
	if (enter_underline_mode != NULL && exit_underline_mode != NULL)
		ch |= __UNDERSCORE;
	if (enter_alt_charset_mode != NULL && exit_alt_charset_mode != NULL)
		ch |= __ALTCHARSET;

	return ch;
}


#ifdef HAVE_WCHAR
/*
 * term_attrs --
 *	Get terminal wide attributes
 */
attr_t
term_attrs(void)
{
	attr_t attr = 0;

#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "term_attrs\n");
#endif
	if (exit_attribute_mode != NULL) {
		if (enter_blink_mode != NULL)
			attr |= __BLINK;
		if (enter_bold_mode != NULL)
			attr |= __BOLD;
		if (enter_dim_mode != NULL)
			attr |= __DIM;
		if (enter_secure_mode != NULL)
			attr |= __BLANK;
		if (enter_protected_mode != NULL)
			attr |= __PROTECT;
		if (enter_reverse_mode != NULL)
			attr |= __REVERSE;
#ifdef HAVE_WCHAR
		if (enter_low_hl_mode != NULL)
			attr |= WA_LOW;
		if (enter_top_hl_mode != NULL)
			attr |= WA_TOP;
		if (enter_left_hl_mode != NULL)
			attr |= WA_LEFT;
		if (enter_right_hl_mode != NULL)
			attr |= WA_RIGHT;
		if (enter_horizontal_hl_mode != NULL)
			attr |= WA_HORIZONTAL;
		if (enter_vertical_hl_mode != NULL)
			attr |= WA_VERTICAL;
#endif /* HAVE_WCHAR */
	}
	if (enter_standout_mode != NULL && exit_standout_mode != NULL)
		attr |= __STANDOUT;
	if (enter_underline_mode != NULL && exit_underline_mode != NULL)
		attr |= __UNDERSCORE;
	if (enter_alt_charset_mode != NULL && exit_alt_charset_mode != NULL)
		attr |= __ALTCHARSET;

	return attr;
}
#endif /* HAVE_WCHAR */


static int
__wattr_on(WINDOW *win, attr_t attr)
{
	const TERMINAL *t = win->screen->term;

#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattr_on: win %p, attr %08x\n", win, attr);
#endif
	/* If can enter modes, set the relevent attribute bits. */
	if (t_exit_attribute_mode(t) != NULL) {
		if (attr & __BLINK && t_enter_blink_mode(t) != NULL)
			win->wattr |= __BLINK;
		if (attr & __BOLD && t_enter_bold_mode(t) != NULL)
			win->wattr |= __BOLD;
		if (attr & __DIM && t_enter_dim_mode(t) != NULL)
			win->wattr |= __DIM;
		if (attr & __BLANK && t_enter_secure_mode(t) != NULL)
			win->wattr |= __BLANK;
		if (attr & __PROTECT && t_enter_protected_mode(t) != NULL)
			win->wattr |= __PROTECT;
		if (attr & __REVERSE && t_enter_reverse_mode(t) != NULL)
			win->wattr |= __REVERSE;
#ifdef HAVE_WCHAR
		if (attr & WA_LOW && t_enter_low_hl_mode(t) != NULL)
			win->wattr |= WA_LOW;
		if (attr & WA_TOP && t_enter_top_hl_mode(t) != NULL)
			win->wattr |= WA_TOP;
		if (attr & WA_LEFT && t_enter_left_hl_mode(t) != NULL)
			win->wattr |= WA_LEFT;
		if (attr & WA_RIGHT && t_enter_right_hl_mode(t) != NULL)
			win->wattr |= WA_RIGHT;
		if (attr & WA_HORIZONTAL && t_enter_horizontal_hl_mode(t) != NULL)
			win->wattr |= WA_HORIZONTAL;
		if (attr & WA_VERTICAL && t_enter_vertical_hl_mode(t) != NULL)
			win->wattr |= WA_VERTICAL;
#endif /* HAVE_WCHAR */
	}
	if (attr & __STANDOUT && t_enter_standout_mode(t) != NULL &&
	    t_exit_standout_mode(t) != NULL)
		wstandout(win);
	if (attr & __UNDERSCORE && t_enter_underline_mode(t) != NULL &&
	    t_exit_underline_mode(t) != NULL)
		wunderscore(win);
	if (attr & __COLOR)
		__wcolor_set(win, attr);
	return OK;
}


static int
__wattr_off(WINDOW *win, attr_t attr)
{
	const TERMINAL *t = win->screen->term;

#ifdef DEBUG
	__CTRACE(__CTRACE_ATTR, "wattr_off: win %p, attr %08x\n", win, attr);
#endif
	/* If can do exit modes, unset the relevent attribute bits. */
	if (t_exit_attribute_mode(t) != NULL) {
		if (attr & __BLINK)
			win->wattr &= ~__BLINK;
		if (attr & __BOLD)
			win->wattr &= ~__BOLD;
		if (attr & __DIM)
			win->wattr &= ~__DIM;
		if (attr & __BLANK)
			win->wattr &= ~__BLANK;
		if (attr & __PROTECT)
			win->wattr &= ~__PROTECT;
		if (attr & __REVERSE)
			win->wattr &= ~__REVERSE;
#ifdef HAVE_WCHAR
		if (attr & WA_LOW)
			win->wattr &= ~WA_LOW;
		if (attr & WA_TOP)
			win->wattr &= ~WA_TOP;
		if (attr & WA_LEFT)
			win->wattr &= ~WA_LEFT;
		if (attr & WA_RIGHT)
			win->wattr &= ~WA_RIGHT;
		if (attr & WA_HORIZONTAL)
			win->wattr &= ~WA_HORIZONTAL;
		if (attr & WA_VERTICAL)
			win->wattr &= ~WA_VERTICAL;
#endif /* HAVE_WCHAR */
	}
	if (attr & __STANDOUT)
		wstandend(win);
	if (attr & __UNDERSCORE)
		wunderend(win);
	if (attr & __COLOR) {
		if (max_colors != 0)
			win->wattr &= ~__COLOR;
	}
	return OK;
}


static void
__wcolor_set(WINDOW *win, attr_t attr)
{
	const TERMINAL *t = win->screen->term;

	/* If another color pair is set, turn that off first. */
	win->wattr &= ~__COLOR;
	/* If can do color video, set the color pair bits. */
	if (t_max_colors(t) != 0 && attr & __COLOR)
		win->wattr |= attr & __COLOR;
}