xref: /freebsd/sys/dev/syscons/fire/fire_saver.c (revision 61e21613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Brad Forschinger
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  *    without modification, immediately at the beginning of the file.
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 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 
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 {
147 		log(LOG_NOTICE,
148 		    "%s: the console does not support M_VGA_CG320\n",
149 		    SAVER_NAME);
150 		return (ENODEV);
151 	}
152 
153 	scrw = info.vi_width;
154 	scrh = info.vi_height;
155 
156 	buf = (u_char *)malloc(scrw * (scrh + 1), M_DEVBUF, M_NOWAIT);
157 	if (buf) {
158 		bzero(buf, scrw * (scrh + 1));
159 	} else {
160 		log(LOG_NOTICE,
161 		    "%s: buffer allocation is failed\n",
162 		    SAVER_NAME);
163 		return (ENODEV);
164 	}
165 
166 	/* intialize the palette */
167 	red = green = blue = 0;
168 	for (i = 0; i < 256; i++) {
169 		red++;
170 		if (red > 128)
171 			green += 2;
172 		fire_pal[RED(i)] = red;
173 		fire_pal[GREEN(i)] = green;
174 		fire_pal[BLUE(i)] = blue;
175 	}
176 
177 	return (0);
178 }
179 
180 static int
181 fire_term(video_adapter_t *adp)
182 {
183 	free(buf, M_DEVBUF);
184 	return (0);
185 }
186 
187 static scrn_saver_t fire_module = {
188 	SAVER_NAME,
189 	fire_init,
190 	fire_term,
191 	fire_saver,
192 	NULL
193 };
194 
195 SAVER_MODULE(fire_saver, fire_module);
196