1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer as
12 *    the first lines of this file unmodified.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD$");
32
33#include "opt_syscons.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/consio.h>
39
40#include <dev/syscons/syscons.h>
41
42SET_DECLARE(scterm_set, sc_term_sw_t);
43
44/* exported subroutines */
45
46void
47sc_move_cursor(scr_stat *scp, int x, int y)
48{
49	if (x < 0)
50		x = 0;
51	if (y < 0)
52		y = 0;
53	if (x >= scp->xsize)
54		x = scp->xsize - 1;
55	if (y >= scp->ysize)
56		y = scp->ysize - 1;
57	scp->xpos = x;
58	scp->ypos = y;
59	scp->cursor_pos = scp->ypos*scp->xsize + scp->xpos;
60}
61
62void
63sc_clear_screen(scr_stat *scp)
64{
65	(*scp->tsw->te_clear)(scp);
66	scp->cursor_oldpos = scp->cursor_pos;
67	sc_remove_cutmarking(scp);
68}
69
70/* terminal emulator manager routines */
71
72static LIST_HEAD(, sc_term_sw) sc_term_list =
73	LIST_HEAD_INITIALIZER(sc_term_list);
74
75int
76sc_term_add(sc_term_sw_t *sw)
77{
78	LIST_INSERT_HEAD(&sc_term_list, sw, link);
79	return 0;
80}
81
82int
83sc_term_remove(sc_term_sw_t *sw)
84{
85	LIST_REMOVE(sw, link);
86	return 0;
87}
88
89sc_term_sw_t
90*sc_term_match(char *name)
91{
92	sc_term_sw_t **list;
93	sc_term_sw_t *p;
94
95	if (!LIST_EMPTY(&sc_term_list)) {
96		LIST_FOREACH(p, &sc_term_list, link) {
97			if ((strcmp(name, p->te_name) == 0)
98			    || (strcmp(name, "*") == 0)) {
99				return p;
100			}
101		}
102	} else {
103		SET_FOREACH(list, scterm_set) {
104			p = *list;
105			if ((strcmp(name, p->te_name) == 0)
106			    || (strcmp(name, "*") == 0)) {
107				return p;
108			}
109		}
110	}
111
112	return NULL;
113}
114
115sc_term_sw_t
116*sc_term_match_by_number(int index)
117{
118	sc_term_sw_t *p;
119
120	if (index <= 0)
121		return NULL;
122	LIST_FOREACH(p, &sc_term_list, link) {
123		if (--index <= 0)
124			return p;
125	}
126
127	return NULL;
128}
129