1 /*-
2  * Copyright (c) 2000 Chiharu Shibata
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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  * $FreeBSD: src/sys/modules/syscons/dragon/dragon_saver.c,v 1.1.2.1 2003/05/11 01:17:02 murray Exp $
29  * $DragonFly: src/sys/dev/misc/syscons/dragon/dragon_saver.c,v 1.6 2006/09/03 18:52:26 dillon Exp $
30  */
31 
32 #include	<sys/param.h>
33 #include	<sys/systm.h>
34 #include	<sys/kernel.h>
35 #include	<sys/module.h>
36 #include	<sys/syslog.h>
37 #include	<sys/consio.h>
38 #include	<sys/fbio.h>
39 #include	<sys/thread2.h>
40 
41 #include	<sys/random.h>
42 
43 #include	<dev/video/fb/fbreg.h>
44 #include	<dev/video/fb/splashreg.h>
45 #include	"../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 		bzero(vid, SCRW*SCRH);
122 
123 		/* set palette of each curves */
124 		for (tmp = 0; tmp < 3*CURVE; ++tmp) {
125 			dragon_pal[3+tmp] = (u_char)krandom();
126 		}
127 		load_palette(adp, dragon_pal);
128 
129 		mul = ((krandom() & 7) + 1) * (SCRW / 320);
130 		org_x = krandom() % SCRW; org_y = krandom() % 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 	if (blank) {
188 		/* switch to graphics mode */
189 		if (blanked <= 0) {
190 			crit_enter();
191 			set_video_mode(adp, VIDEO_MODE);
192 			vid = (u_char *)adp->va_window;
193 			curve = CURVE + 1;
194 			++blanked;
195 			crit_exit();
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 (get_mode_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