xref: /freebsd/sys/dev/syscons/dragon/dragon_saver.c (revision e1c4c8dd8d2d10b6104f06856a77bd5b4813a801)
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 
31 #include	<sys/param.h>
32 #include	<sys/systm.h>
33 #include	<sys/kernel.h>
34 #include	<sys/module.h>
35 #include	<sys/syslog.h>
36 #include	<sys/consio.h>
37 #include	<sys/fbio.h>
38 
39 #include	<sys/random.h>
40 
41 #include	<dev/fb/fbreg.h>
42 #include	<dev/fb/splashreg.h>
43 #include	<dev/syscons/syscons.h>
44 
45 #define SAVER_NAME	 "dragon_saver"
46 
47 static u_char	*vid;
48 static int	blanked;
49 
50 #define	VIDEO_MODE	M_VGA_CG320
51 #define	VIDEO_MODE_NAME	"M_VGA_CG320"
52 #define	SCRW	320
53 #define	SCRH	200
54 #define	ORDER	13
55 #define	CURVE	3
56 #define	OUT	100
57 
58 static int	cur_x, cur_y;
59 static int	curve;
60 static u_char	dragon_pal[3*256];	/* zero-filled by the compiler */
61 
62 static __inline int
63 gpset(int x, int y, int val)
64 {
65 	if (x < 0 || y < 0 || SCRW <= x || SCRH <= y) {
66 		return 0;
67 	}
68 	vid[x + y * SCRW] = val;
69 	return 1;
70 }
71 
72 static int
73 gdraw(int dx, int dy, int val)
74 {
75 	int	i;
76 	int	set = 0;
77 
78 	if (dx != 0) {
79 		i = cur_x;
80 		cur_x += dx;
81 		if (dx < 0) {
82 			i += dx;
83 			dx = -dx;
84 		}
85 		/* horizontal line */
86 		for (; dx >= 0; --dx, ++i) {
87 			set |= gpset(i, cur_y, val);
88 		}
89 	}
90 	else {	/* dy != 0 */
91 		i = cur_y;
92 		cur_y += dy;
93 		if (dy < 0) {
94 			i += dy;
95 			dy = -dy;
96 		}
97 		/* vertical line */
98 		for (; dy >= 0; --dy, ++i) {
99 			set |= gpset(cur_x, i, val);
100 		}
101 	}
102 	return set;
103 }
104 
105 static void
106 dragon_update(video_adapter_t *adp)
107 {
108 	static int	i, p, q;
109 	static int	order, mul, out;
110 	static int	org_x, org_y;
111 	static int	dx, dy;
112 	static unsigned char	fold[1 << (ORDER - 3)];
113 #define	GET_FOLD(x)	(fold[(x) >> 3]  &  (1 << ((x) & 7)))
114 #define	SET_FOLD(x)	(fold[(x) >> 3] |=  (1 << ((x) & 7)))
115 #define	CLR_FOLD(x)	(fold[(x) >> 3] &= ~(1 << ((x) & 7)))
116 	int	tmp;
117 
118 	if (curve > CURVE) {
119 		vidd_clear(adp);
120 
121 		/* set palette of each curves */
122 		for (tmp = 0; tmp < 3*CURVE; ++tmp) {
123 			dragon_pal[3+tmp] = (u_char)random();
124 		}
125 		vidd_load_palette(adp, dragon_pal);
126 
127 		mul = ((random() & 7) + 1) * (SCRW / 320);
128 		org_x = random() % SCRW; org_y = random() % SCRH;
129 
130 		curve = 0;
131 		order = ORDER;
132 	}
133 
134 	if (order >= ORDER) {
135 		++curve;
136 
137 		cur_x = org_x; cur_y = org_y;
138 
139 		switch (curve) {
140 		case 1:
141 			dx = 0; dy = mul;
142 			break;
143 		case 2:
144 			dx = mul; dy = 0;
145 			break;
146 		case 3:
147 			dx = 0; dy = -mul;
148 			break;
149 		}
150 		(void)gdraw(dx, dy, curve); out = 0;
151 
152 		order = 0;
153 		q = p = 0; i = q + 1;
154 	}
155 
156 	if (i > q) {
157 		SET_FOLD(p); q = p * 2;
158 
159 		++order;
160 		i = p; p = q + 1;
161 	}
162 
163 	if (GET_FOLD(q-i) != 0) {
164 		CLR_FOLD(i);
165 		tmp = dx; dx =  dy; dy = -tmp;	/* turn right */
166 	}
167 	else {
168 		SET_FOLD(i);
169 		tmp = dx; dx = -dy; dy =  tmp;	/* turn left */
170 	}
171 	if (gdraw(dx, dy, curve)) {
172 		out = 0;
173 	}
174 	else {
175 		if (++out > OUT) {
176 			order = ORDER;	/* force to terminate this curve */
177 		}
178 	}
179 	++i;
180 }
181 
182 static int
183 dragon_saver(video_adapter_t *adp, int blank)
184 {
185 	int pl;
186 
187 	if (blank) {
188 		/* switch to graphics mode */
189 		if (blanked <= 0) {
190 			pl = splhigh();
191 			vidd_set_mode(adp, VIDEO_MODE);
192 			vid = (u_char *)adp->va_window;
193 			curve = CURVE + 1;
194 			++blanked;
195 			splx(pl);
196 		}
197 
198 		/* update display */
199 		dragon_update(adp);
200 	}
201 	else {
202 		blanked = 0;
203 	}
204 	return 0;
205 }
206 
207 static int
208 dragon_init(video_adapter_t *adp)
209 {
210 	video_info_t info;
211 
212 	/* check that the console is capable of running in 320x200x256 */
213 	if (vidd_get_info(adp, VIDEO_MODE, &info)) {
214 		log(LOG_NOTICE,
215 		    "%s: the console does not support " VIDEO_MODE_NAME "\n",
216 		    SAVER_NAME);
217 		return ENODEV;
218 	}
219 
220 	blanked = 0;
221 	return 0;
222 }
223 
224 static int
225 dragon_term(video_adapter_t *adp)
226 {
227 	return 0;
228 }
229 
230 static scrn_saver_t dragon_module = {
231 	SAVER_NAME,
232 	dragon_init,
233 	dragon_term,
234 	dragon_saver,
235 	NULL,
236 };
237 
238 SAVER_MODULE(dragon_saver, dragon_module);
239