xref: /freebsd/sys/dev/syscons/dragon/dragon_saver.c (revision 148a8da8)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2000 Chiharu Shibata
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
12  *    in this position and unchanged.
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  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include	<sys/param.h>
34 #include	<sys/systm.h>
35 #include	<sys/kernel.h>
36 #include	<sys/module.h>
37 #include	<sys/syslog.h>
38 #include	<sys/consio.h>
39 #include	<sys/fbio.h>
40 
41 #include	<sys/random.h>
42 
43 #include	<dev/fb/fbreg.h>
44 #include	<dev/fb/splashreg.h>
45 #include	<dev/syscons/syscons.h>
46 
47 #define SAVER_NAME	 "dragon_saver"
48 
49 static u_char	*vid;
50 static int	blanked;
51 
52 #define	VIDEO_MODE	M_VGA_CG320
53 #define	VIDEO_MODE_NAME	"M_VGA_CG320"
54 #define	SCRW	320
55 #define	SCRH	200
56 #define	ORDER	13
57 #define	CURVE	3
58 #define	OUT	100
59 
60 static int	cur_x, cur_y;
61 static int	curve;
62 static u_char	dragon_pal[3*256];	/* zero-filled by the compiler */
63 
64 static __inline int
65 gpset(int x, int y, int val)
66 {
67 	if (x < 0 || y < 0 || SCRW <= x || SCRH <= y) {
68 		return 0;
69 	}
70 	vid[x + y * SCRW] = val;
71 	return 1;
72 }
73 
74 static int
75 gdraw(int dx, int dy, int val)
76 {
77 	int	i;
78 	int	set = 0;
79 
80 	if (dx != 0) {
81 		i = cur_x;
82 		cur_x += dx;
83 		if (dx < 0) {
84 			i += dx;
85 			dx = -dx;
86 		}
87 		/* horizontal line */
88 		for (; dx >= 0; --dx, ++i) {
89 			set |= gpset(i, cur_y, val);
90 		}
91 	}
92 	else {	/* dy != 0 */
93 		i = cur_y;
94 		cur_y += dy;
95 		if (dy < 0) {
96 			i += dy;
97 			dy = -dy;
98 		}
99 		/* vertical line */
100 		for (; dy >= 0; --dy, ++i) {
101 			set |= gpset(cur_x, i, val);
102 		}
103 	}
104 	return set;
105 }
106 
107 static void
108 dragon_update(video_adapter_t *adp)
109 {
110 	static int	i, p, q;
111 	static int	order, mul, out;
112 	static int	org_x, org_y;
113 	static int	dx, dy;
114 	static unsigned char	fold[1 << (ORDER - 3)];
115 #define	GET_FOLD(x)	(fold[(x) >> 3]  &  (1 << ((x) & 7)))
116 #define	SET_FOLD(x)	(fold[(x) >> 3] |=  (1 << ((x) & 7)))
117 #define	CLR_FOLD(x)	(fold[(x) >> 3] &= ~(1 << ((x) & 7)))
118 	int	tmp;
119 
120 	if (curve > CURVE) {
121 		vidd_clear(adp);
122 
123 		/* set palette of each curves */
124 		for (tmp = 0; tmp < 3*CURVE; ++tmp) {
125 			dragon_pal[3+tmp] = (u_char)random();
126 		}
127 		vidd_load_palette(adp, dragon_pal);
128 
129 		mul = ((random() & 7) + 1) * (SCRW / 320);
130 		org_x = random() % SCRW; org_y = random() % SCRH;
131 
132 		curve = 0;
133 		order = ORDER;
134 	}
135 
136 	if (order >= ORDER) {
137 		++curve;
138 
139 		cur_x = org_x; cur_y = org_y;
140 
141 		switch (curve) {
142 		case 1:
143 			dx = 0; dy = mul;
144 			break;
145 		case 2:
146 			dx = mul; dy = 0;
147 			break;
148 		case 3:
149 			dx = 0; dy = -mul;
150 			break;
151 		}
152 		(void)gdraw(dx, dy, curve); out = 0;
153 
154 		order = 0;
155 		q = p = 0; i = q + 1;
156 	}
157 
158 	if (i > q) {
159 		SET_FOLD(p); q = p * 2;
160 
161 		++order;
162 		i = p; p = q + 1;
163 	}
164 
165 	if (GET_FOLD(q-i) != 0) {
166 		CLR_FOLD(i);
167 		tmp = dx; dx =  dy; dy = -tmp;	/* turn right */
168 	}
169 	else {
170 		SET_FOLD(i);
171 		tmp = dx; dx = -dy; dy =  tmp;	/* turn left */
172 	}
173 	if (gdraw(dx, dy, curve)) {
174 		out = 0;
175 	}
176 	else {
177 		if (++out > OUT) {
178 			order = ORDER;	/* force to terminate this curve */
179 		}
180 	}
181 	++i;
182 }
183 
184 static int
185 dragon_saver(video_adapter_t *adp, int blank)
186 {
187 	int pl;
188 
189 	if (blank) {
190 		/* switch to graphics mode */
191 		if (blanked <= 0) {
192 			pl = splhigh();
193 			vidd_set_mode(adp, VIDEO_MODE);
194 			vid = (u_char *)adp->va_window;
195 			curve = CURVE + 1;
196 			++blanked;
197 			splx(pl);
198 		}
199 
200 		/* update display */
201 		dragon_update(adp);
202 	}
203 	else {
204 		blanked = 0;
205 	}
206 	return 0;
207 }
208 
209 static int
210 dragon_init(video_adapter_t *adp)
211 {
212 	video_info_t info;
213 
214 	/* check that the console is capable of running in 320x200x256 */
215 	if (vidd_get_info(adp, VIDEO_MODE, &info)) {
216 		log(LOG_NOTICE,
217 		    "%s: the console does not support " VIDEO_MODE_NAME "\n",
218 		    SAVER_NAME);
219 		return ENODEV;
220 	}
221 
222 	blanked = 0;
223 	return 0;
224 }
225 
226 static int
227 dragon_term(video_adapter_t *adp)
228 {
229 	return 0;
230 }
231 
232 static scrn_saver_t dragon_module = {
233 	SAVER_NAME,
234 	dragon_init,
235 	dragon_term,
236 	dragon_saver,
237 	NULL,
238 };
239 
240 SAVER_MODULE(dragon_saver, dragon_module);
241