xref: /freebsd/sys/dev/syscons/rain/rain_saver.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Dag-Erling Smørgrav
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 <dev/fb/fbreg.h>
40 #include <dev/fb/splashreg.h>
41 #include <dev/syscons/syscons.h>
42 
43 #define SAVER_NAME	 "rain_saver"
44 #ifdef MAX
45 #undef MAX
46 #endif
47 #define MAX		 63	/* number of colors (in addition to black) */
48 #define INCREMENT	 4	/* increment between colors */
49 
50 #define RED(n)		 ((n) * 3 + 0)
51 #define GREEN(n)	 ((n) * 3 + 1)
52 #define BLUE(n)		 ((n) * 3 + 2)
53 
54 #define SET_ORIGIN(adp, o) do {				\
55 	int oo = o;					\
56 	if (oo != last_origin)				\
57 	    vidd_set_win_org(adp, last_origin = oo);	\
58 	} while (0)
59 
60 static u_char		*vid;
61 static int		 banksize, scrmode, bpsl, scrw, scrh;
62 static u_char		 rain_pal[768];
63 static int		 blanked;
64 
65 static void
66 rain_update(video_adapter_t *adp)
67 {
68 	int i, t;
69 
70 	t = rain_pal[BLUE(MAX)];
71 	for (i = MAX; i > 1; i--)
72 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)];
73 	rain_pal[BLUE(1)] = t;
74 	vidd_load_palette(adp, rain_pal);
75 }
76 
77 static int
78 rain_saver(video_adapter_t *adp, int blank)
79 {
80 	int i, j, o, p, pl;
81 	u_char temp;
82 	int last_origin = -1;
83 
84 	if (blank) {
85 		/* switch to graphics mode */
86 		if (blanked <= 0) {
87 			pl = splhigh();
88 			vidd_set_mode(adp, scrmode);
89 			vidd_load_palette(adp, rain_pal);
90 			vidd_set_border(adp, 0);
91 			blanked++;
92 			vid = (u_char *)adp->va_window;
93 			banksize = adp->va_window_size;
94 			bpsl = adp->va_line_width;
95 			splx(pl);
96 			for (i = 0; i < bpsl*scrh; i += banksize) {
97 				SET_ORIGIN(adp, i);
98 				if ((bpsl * scrh - i) < banksize)
99 					bzero(vid, bpsl * scrh - i);
100 				else
101 					bzero(vid, banksize);
102 			}
103 			SET_ORIGIN(adp, 0);
104 			for (i = 0, o = 0, p = 0; i < scrw; i += 2, p += 2) {
105 				if (p > banksize) {
106 					p -= banksize;
107 					o += banksize;
108 					SET_ORIGIN(adp, o);
109 				}
110 				vid[p] = 1 + (random() % MAX);
111 			}
112 			o = 0; p = 0;
113 			for (j = 1; j < scrh; j++)
114 			  for (i = 0, p = bpsl * (j - 1) - o; i < scrw; i += 2, p+= 2) {
115 			  	while (p > banksize) {
116 					p -= banksize;
117 					o += banksize;
118 				}
119 				SET_ORIGIN(adp, o);
120 				temp = (vid[p] < MAX) ? 1 + vid[p] : 1;
121 				if (p + bpsl < banksize) {
122 					vid[p + bpsl] = temp;
123 				} else {
124 					SET_ORIGIN(adp, o + banksize);
125 					vid[p + bpsl - banksize] = temp;
126 				}
127 			  }
128 		}
129 
130 		/* update display */
131 		rain_update(adp);
132 	} else {
133 		blanked = 0;
134 	}
135 	return (0);
136 }
137 
138 static int
139 rain_init(video_adapter_t *adp)
140 {
141 	video_info_t info;
142 	int i;
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 	/* intialize the palette */
157 	for (i = 1; i < MAX; i++)
158 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)] + INCREMENT;
159 
160 	return (0);
161 }
162 
163 static int
164 rain_term(video_adapter_t *adp)
165 {
166 	return (0);
167 }
168 
169 static scrn_saver_t rain_module = {
170 	SAVER_NAME,
171 	rain_init,
172 	rain_term,
173 	rain_saver,
174 	NULL
175 };
176 
177 SAVER_MODULE(rain_saver, rain_module);
178