xref: /freebsd/sys/dev/syscons/rain/rain_saver.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1 /*-
2  * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav
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$
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 static u_char		*vid;
55 static int		 banksize, scrmode, bpsl, scrw, scrh;
56 static u_char		 rain_pal[768];
57 static int		 blanked;
58 
59 static void
60 rain_update(video_adapter_t *adp)
61 {
62 	int i, t;
63 
64 	t = rain_pal[BLUE(MAX)];
65 	for (i = MAX; i > 1; i--)
66 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)];
67 	rain_pal[BLUE(1)] = t;
68 	load_palette(adp, rain_pal);
69 }
70 
71 static int
72 rain_saver(video_adapter_t *adp, int blank)
73 {
74 	int i, j, o, p, pl;
75 	u_char temp;
76 
77 	if (blank) {
78 		/* switch to graphics mode */
79 		if (blanked <= 0) {
80 			pl = splhigh();
81 			set_video_mode(adp, scrmode);
82 			load_palette(adp, rain_pal);
83 			set_border(adp, 0);
84 			blanked++;
85 			vid = (u_char *)adp->va_window;
86 			banksize = adp->va_window_size;
87 			bpsl = adp->va_line_width;
88 			splx(pl);
89 			for (i = 0; i < bpsl*scrh; i += banksize) {
90 				set_origin(adp, i);
91 				if ((bpsl * scrh - i) < banksize)
92 					bzero(vid, bpsl * scrh - i);
93 				else
94 					bzero(vid, banksize);
95 			}
96 			set_origin(adp, 0);
97 			for (i = 0, o = 0, p = 0; i < scrw; i += 2, p += 2) {
98 				if (p > banksize) {
99 					p -= banksize;
100 					o += banksize;
101 					set_origin(adp, o);
102 				}
103 				vid[p] = 1 + (random() % MAX);
104 			}
105 			o = 0; p = 0;
106 			for (j = 1; j < scrh; j++)
107 			  for (i = 0, p = bpsl * (j - 1) - o; i < scrw; i += 2, p+= 2) {
108 			  	while (p > banksize) {
109 					p -= banksize;
110 					o += banksize;
111 				}
112 				set_origin(adp, o);
113 				temp = (vid[p] < MAX) ? 1 + vid[p] : 1;
114 				if (p + bpsl < banksize) {
115 					vid[p + bpsl] = temp;
116 				} else {
117 					set_origin(adp, o + banksize);
118 					vid[p + bpsl - banksize] = temp;
119 				}
120 			  }
121 		}
122 
123 		/* update display */
124 		rain_update(adp);
125 	} else {
126 		blanked = 0;
127 	}
128 	return (0);
129 }
130 
131 static int
132 rain_init(video_adapter_t *adp)
133 {
134 	video_info_t info;
135 	int i;
136 
137 	if (!get_mode_info(adp, M_VGA_CG320, &info)) {
138 		scrmode = M_VGA_CG320;
139 	} else if (!get_mode_info(adp, M_PC98_PEGC640x480, &info)) {
140 		scrmode = M_PC98_PEGC640x480;
141 	} else if (!get_mode_info(adp, M_PC98_PEGC640x400, &info)) {
142 		scrmode = M_PC98_PEGC640x400;
143 	} else {
144 		log(LOG_NOTICE,
145 		    "%s: the console does not support M_VGA_CG320\n",
146 		    SAVER_NAME);
147 		return (ENODEV);
148 	}
149 
150 	scrw = info.vi_width;
151 	scrh = info.vi_height;
152 
153 	/* intialize the palette */
154 	for (i = 1; i < MAX; i++)
155 		rain_pal[BLUE(i)] = rain_pal[BLUE(i - 1)] + INCREMENT;
156 
157 	return (0);
158 }
159 
160 static int
161 rain_term(video_adapter_t *adp)
162 {
163 	return (0);
164 }
165 
166 static scrn_saver_t rain_module = {
167 	SAVER_NAME,
168 	rain_init,
169 	rain_term,
170 	rain_saver,
171 	NULL
172 };
173 
174 SAVER_MODULE(rain_saver, rain_module);
175