xref: /freebsd/sys/dev/syscons/fire/fire_saver.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 1999 Brad Forschinger
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  *    without modification, immediately at the beginning of the file.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * brad forschinger, 19990504 <retch@flag.blackened.net>
31  *
32  * written with much help from warp_saver.c
33  *
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/syslog.h>
41 #include <sys/consio.h>
42 #include <sys/malloc.h>
43 #include <sys/fbio.h>
44 
45 #include <dev/fb/fbreg.h>
46 #include <dev/fb/splashreg.h>
47 #include <dev/syscons/syscons.h>
48 
49 #define SAVER_NAME	 "fire_saver"
50 
51 #define RED(n)		 ((n) * 3 + 0)
52 #define GREEN(n)	 ((n) * 3 + 1)
53 #define BLUE(n)		 ((n) * 3 + 2)
54 
55 #define SET_ORIGIN(adp, o) do {				\
56 	int oo = o;					\
57 	if (oo != last_origin)				\
58 	    vidd_set_win_org(adp, last_origin = oo);	\
59 	} while (0)
60 
61 static u_char		*buf;
62 static u_char		*vid;
63 static int		 banksize, scrmode, bpsl, scrw, scrh;
64 static u_char		 fire_pal[768];
65 static int		 blanked;
66 
67 static void
68 fire_update(video_adapter_t *adp)
69 {
70 	int x, y;
71 	int o, p;
72 	int last_origin = -1;
73 
74 	/* make a new bottom line */
75 	for (x = 0, y = scrh; x < scrw; x++)
76 		buf[x + (y * bpsl)] = random() % 160 + 96;
77 
78 	/* fade the flames out */
79 	for (y = 0; y < scrh; y++) {
80 		for (x = 0; x < scrw; x++) {
81 			buf[x + (y * scrw)] =
82 			    (buf[(x + 0) + ((y + 0) * scrw)] +
83 			     buf[(x - 1) + ((y + 1) * scrw)] +
84 			     buf[(x + 0) + ((y + 1) * scrw)] +
85 			     buf[(x + 1) + ((y + 1) * scrw)]) / 4;
86 			if (buf[x + (y * scrw)] > 0)
87 				buf[x + (y * scrw)]--;
88 		}
89 	}
90 
91 	/* blit our buffer into video ram */
92 	for (y = 0, p = 0, o = 0; y < scrh; y++, p += bpsl) {
93 		while (p > banksize) {
94 			p -= banksize;
95 			o += banksize;
96 		}
97 		SET_ORIGIN(adp, o);
98 		if (p + scrw < banksize) {
99 			bcopy(buf + y * scrw, vid + p, scrw);
100 		} else {
101 			bcopy(buf + y * scrw, vid + p, banksize - p);
102 			SET_ORIGIN(adp, o + banksize);
103 			bcopy(buf + y * scrw + (banksize - p), vid,
104 			      scrw - (banksize - p));
105 			p -= banksize;
106 			o += banksize;
107 		}
108 	}
109 
110 }
111 
112 static int
113 fire_saver(video_adapter_t *adp, int blank)
114 {
115 	int pl;
116 
117 	if (blank) {
118 		/* switch to graphics mode */
119       		if (blanked <= 0) {
120 			pl = splhigh();
121 			vidd_set_mode(adp, scrmode);
122 			vidd_load_palette(adp, fire_pal);
123 			blanked++;
124 			vid = (u_char *)adp->va_window;
125 			banksize = adp->va_window_size;
126 			bpsl = adp->va_line_width;
127 			splx(pl);
128 			vidd_clear(adp);
129 		}
130 		fire_update(adp);
131 	} else {
132 		blanked = 0;
133 	}
134 
135     return 0;
136 }
137 
138 static int
139 fire_init(video_adapter_t *adp)
140 {
141 	video_info_t info;
142 	int i, red, green, blue;
143 
144 	if (!vidd_get_info(adp, M_VGA_CG320, &info)) {
145 		scrmode = M_VGA_CG320;
146 	} else if (!vidd_get_info(adp, M_PC98_PEGC640x480, &info)) {
147 		scrmode = M_PC98_PEGC640x480;
148 	} else if (!vidd_get_info(adp, M_PC98_PEGC640x400, &info)) {
149 		scrmode = M_PC98_PEGC640x400;
150 	} else {
151 		log(LOG_NOTICE,
152 		    "%s: the console does not support M_VGA_CG320\n",
153 		    SAVER_NAME);
154 		return (ENODEV);
155 	}
156 
157 	scrw = info.vi_width;
158 	scrh = info.vi_height;
159 
160 	buf = (u_char *)malloc(scrw * (scrh + 1), M_DEVBUF, M_NOWAIT);
161 	if (buf) {
162 		bzero(buf, scrw * (scrh + 1));
163 	} else {
164 		log(LOG_NOTICE,
165 		    "%s: buffer allocation is failed\n",
166 		    SAVER_NAME);
167 		return (ENODEV);
168 	}
169 
170 	/* intialize the palette */
171 	red = green = blue = 0;
172 	for (i = 0; i < 256; i++) {
173 		red++;
174 		if (red > 128)
175 			green += 2;
176 		fire_pal[RED(i)] = red;
177 		fire_pal[GREEN(i)] = green;
178 		fire_pal[BLUE(i)] = blue;
179 	}
180 
181 	return (0);
182 }
183 
184 static int
185 fire_term(video_adapter_t *adp)
186 {
187 	free(buf, M_DEVBUF);
188 	return (0);
189 }
190 
191 static scrn_saver_t fire_module = {
192 	SAVER_NAME,
193 	fire_init,
194 	fire_term,
195 	fire_saver,
196 	NULL
197 };
198 
199 SAVER_MODULE(fire_saver, fire_module);
200