xref: /freebsd/sys/dev/syscons/rain/rain_saver.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Dag-Erling Coïdan 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  * $FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/syslog.h>
38 #include <sys/consio.h>
39 #include <sys/fbio.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	 "rain_saver"
46 #ifdef MAX
47 #undef MAX
48 #endif
49 #define MAX		 63	/* number of colors (in addition to black) */
50 #define INCREMENT	 4	/* increment between colors */
51 
52 #define RED(n)		 ((n) * 3 + 0)
53 #define GREEN(n)	 ((n) * 3 + 1)
54 #define BLUE(n)		 ((n) * 3 + 2)
55 
56 #define SET_ORIGIN(adp, o) do {				\
57 	int oo = o;					\
58 	if (oo != last_origin)				\
59 	    vidd_set_win_org(adp, last_origin = oo);	\
60 	} while (0)
61 
62 static u_char		*vid;
63 static int		 banksize, scrmode, bpsl, scrw, scrh;
64 static u_char		 rain_pal[768];
65 static int		 blanked;
66 
67 static void
68 rain_update(video_adapter_t *adp)
69 {
70 	int i, t;
71 
72 	t = rain_pal[BLUE(MAX)];
73 	for (i = MAX; i > 1; i--)
74 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)];
75 	rain_pal[BLUE(1)] = t;
76 	vidd_load_palette(adp, rain_pal);
77 }
78 
79 static int
80 rain_saver(video_adapter_t *adp, int blank)
81 {
82 	int i, j, o, p, pl;
83 	u_char temp;
84 	int last_origin = -1;
85 
86 	if (blank) {
87 		/* switch to graphics mode */
88 		if (blanked <= 0) {
89 			pl = splhigh();
90 			vidd_set_mode(adp, scrmode);
91 			vidd_load_palette(adp, rain_pal);
92 			vidd_set_border(adp, 0);
93 			blanked++;
94 			vid = (u_char *)adp->va_window;
95 			banksize = adp->va_window_size;
96 			bpsl = adp->va_line_width;
97 			splx(pl);
98 			for (i = 0; i < bpsl*scrh; i += banksize) {
99 				SET_ORIGIN(adp, i);
100 				if ((bpsl * scrh - i) < banksize)
101 					bzero(vid, bpsl * scrh - i);
102 				else
103 					bzero(vid, banksize);
104 			}
105 			SET_ORIGIN(adp, 0);
106 			for (i = 0, o = 0, p = 0; i < scrw; i += 2, p += 2) {
107 				if (p > banksize) {
108 					p -= banksize;
109 					o += banksize;
110 					SET_ORIGIN(adp, o);
111 				}
112 				vid[p] = 1 + (random() % MAX);
113 			}
114 			o = 0; p = 0;
115 			for (j = 1; j < scrh; j++)
116 			  for (i = 0, p = bpsl * (j - 1) - o; i < scrw; i += 2, p+= 2) {
117 			  	while (p > banksize) {
118 					p -= banksize;
119 					o += banksize;
120 				}
121 				SET_ORIGIN(adp, o);
122 				temp = (vid[p] < MAX) ? 1 + vid[p] : 1;
123 				if (p + bpsl < banksize) {
124 					vid[p + bpsl] = temp;
125 				} else {
126 					SET_ORIGIN(adp, o + banksize);
127 					vid[p + bpsl - banksize] = temp;
128 				}
129 			  }
130 		}
131 
132 		/* update display */
133 		rain_update(adp);
134 	} else {
135 		blanked = 0;
136 	}
137 	return (0);
138 }
139 
140 static int
141 rain_init(video_adapter_t *adp)
142 {
143 	video_info_t info;
144 	int i;
145 
146 	if (!vidd_get_info(adp, M_VGA_CG320, &info)) {
147 		scrmode = M_VGA_CG320;
148 	} else {
149 		log(LOG_NOTICE,
150 		    "%s: the console does not support M_VGA_CG320\n",
151 		    SAVER_NAME);
152 		return (ENODEV);
153 	}
154 
155 	scrw = info.vi_width;
156 	scrh = info.vi_height;
157 
158 	/* intialize the palette */
159 	for (i = 1; i < MAX; i++)
160 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)] + INCREMENT;
161 
162 	return (0);
163 }
164 
165 static int
166 rain_term(video_adapter_t *adp)
167 {
168 	return (0);
169 }
170 
171 static scrn_saver_t rain_module = {
172 	SAVER_NAME,
173 	rain_init,
174 	rain_term,
175 	rain_saver,
176 	NULL
177 };
178 
179 SAVER_MODULE(rain_saver, rain_module);
180